1 // SPDX-License-Identifier: GPL-2.0-only
4 * Toshiba T7L66XB core mfd support
6 * Copyright (c) 2005, 2007, 2008 Ian Molton
7 * Copyright (c) 2008 Dmitry Baryshkov
11 * Supported in this driver:
13 * SM/NAND flash controller
15 * As yet not supported
16 * GPIO interface (on NAND pins)
18 * TFT 'interface converter'
19 * PCMCIA interface logic
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/err.h>
26 #include <linux/slab.h>
27 #include <linux/irq.h>
28 #include <linux/clk.h>
29 #include <linux/platform_device.h>
30 #include <linux/mfd/core.h>
31 #include <linux/mfd/tmio.h>
32 #include <linux/mfd/t7l66xb.h>
39 static const struct resource t7l66xb_mmc_resources[] = {
40 DEFINE_RES_MEM(0x800, 0x200),
41 DEFINE_RES_IRQ(IRQ_T7L66XB_MMC)
44 #define SCR_REVID 0x08 /* b Revision ID */
45 #define SCR_IMR 0x42 /* b Interrupt Mask */
46 #define SCR_DEV_CTL 0xe0 /* b Device control */
47 #define SCR_ISR 0xe1 /* b Interrupt Status */
48 #define SCR_GPO_OC 0xf0 /* b GPO output control */
49 #define SCR_GPO_OS 0xf1 /* b GPO output enable */
50 #define SCR_GPI_S 0xf2 /* w GPI status */
51 #define SCR_APDC 0xf8 /* b Active pullup down ctrl */
53 #define SCR_DEV_CTL_USB BIT(0) /* USB enable */
54 #define SCR_DEV_CTL_MMC BIT(1) /* MMC enable */
56 /*--------------------------------------------------------------------------*/
60 /* Lock to protect registers requiring read/modify/write ops. */
70 /*--------------------------------------------------------------------------*/
72 static int t7l66xb_mmc_enable(struct platform_device *mmc)
74 struct t7l66xb *t7l66xb = dev_get_drvdata(mmc->dev.parent);
79 ret = clk_prepare_enable(t7l66xb->clk32k);
83 raw_spin_lock_irqsave(&t7l66xb->lock, flags);
85 dev_ctl = tmio_ioread8(t7l66xb->scr + SCR_DEV_CTL);
86 dev_ctl |= SCR_DEV_CTL_MMC;
87 tmio_iowrite8(dev_ctl, t7l66xb->scr + SCR_DEV_CTL);
89 raw_spin_unlock_irqrestore(&t7l66xb->lock, flags);
91 tmio_core_mmc_enable(t7l66xb->scr + 0x200, 0,
92 t7l66xb_mmc_resources[0].start & 0xfffe);
97 static int t7l66xb_mmc_disable(struct platform_device *mmc)
99 struct t7l66xb *t7l66xb = dev_get_drvdata(mmc->dev.parent);
103 raw_spin_lock_irqsave(&t7l66xb->lock, flags);
105 dev_ctl = tmio_ioread8(t7l66xb->scr + SCR_DEV_CTL);
106 dev_ctl &= ~SCR_DEV_CTL_MMC;
107 tmio_iowrite8(dev_ctl, t7l66xb->scr + SCR_DEV_CTL);
109 raw_spin_unlock_irqrestore(&t7l66xb->lock, flags);
111 clk_disable_unprepare(t7l66xb->clk32k);
116 static void t7l66xb_mmc_pwr(struct platform_device *mmc, int state)
118 struct t7l66xb *t7l66xb = dev_get_drvdata(mmc->dev.parent);
120 tmio_core_mmc_pwr(t7l66xb->scr + 0x200, 0, state);
123 static void t7l66xb_mmc_clk_div(struct platform_device *mmc, int state)
125 struct t7l66xb *t7l66xb = dev_get_drvdata(mmc->dev.parent);
127 tmio_core_mmc_clk_div(t7l66xb->scr + 0x200, 0, state);
130 /*--------------------------------------------------------------------------*/
132 static struct tmio_mmc_data t7166xb_mmc_data = {
134 .set_pwr = t7l66xb_mmc_pwr,
135 .set_clk_div = t7l66xb_mmc_clk_div,
138 static const struct resource t7l66xb_nand_resources[] = {
142 .flags = IORESOURCE_MEM,
147 .flags = IORESOURCE_MEM,
150 .start = IRQ_T7L66XB_NAND,
151 .end = IRQ_T7L66XB_NAND,
152 .flags = IORESOURCE_IRQ,
156 static struct mfd_cell t7l66xb_cells[] = {
157 [T7L66XB_CELL_MMC] = {
159 .enable = t7l66xb_mmc_enable,
160 .disable = t7l66xb_mmc_disable,
161 .platform_data = &t7166xb_mmc_data,
162 .pdata_size = sizeof(t7166xb_mmc_data),
163 .num_resources = ARRAY_SIZE(t7l66xb_mmc_resources),
164 .resources = t7l66xb_mmc_resources,
166 [T7L66XB_CELL_NAND] = {
168 .num_resources = ARRAY_SIZE(t7l66xb_nand_resources),
169 .resources = t7l66xb_nand_resources,
173 /*--------------------------------------------------------------------------*/
175 /* Handle the T7L66XB interrupt mux */
176 static void t7l66xb_irq(struct irq_desc *desc)
178 struct t7l66xb *t7l66xb = irq_desc_get_handler_data(desc);
180 unsigned int i, irq_base;
182 irq_base = t7l66xb->irq_base;
184 while ((isr = tmio_ioread8(t7l66xb->scr + SCR_ISR) &
185 ~tmio_ioread8(t7l66xb->scr + SCR_IMR)))
186 for (i = 0; i < T7L66XB_NR_IRQS; i++)
188 generic_handle_irq(irq_base + i);
191 static void t7l66xb_irq_mask(struct irq_data *data)
193 struct t7l66xb *t7l66xb = irq_data_get_irq_chip_data(data);
197 raw_spin_lock_irqsave(&t7l66xb->lock, flags);
198 imr = tmio_ioread8(t7l66xb->scr + SCR_IMR);
199 imr |= 1 << (data->irq - t7l66xb->irq_base);
200 tmio_iowrite8(imr, t7l66xb->scr + SCR_IMR);
201 raw_spin_unlock_irqrestore(&t7l66xb->lock, flags);
204 static void t7l66xb_irq_unmask(struct irq_data *data)
206 struct t7l66xb *t7l66xb = irq_data_get_irq_chip_data(data);
210 raw_spin_lock_irqsave(&t7l66xb->lock, flags);
211 imr = tmio_ioread8(t7l66xb->scr + SCR_IMR);
212 imr &= ~(1 << (data->irq - t7l66xb->irq_base));
213 tmio_iowrite8(imr, t7l66xb->scr + SCR_IMR);
214 raw_spin_unlock_irqrestore(&t7l66xb->lock, flags);
217 static struct irq_chip t7l66xb_chip = {
219 .irq_ack = t7l66xb_irq_mask,
220 .irq_mask = t7l66xb_irq_mask,
221 .irq_unmask = t7l66xb_irq_unmask,
224 /*--------------------------------------------------------------------------*/
226 /* Install the IRQ handler */
227 static void t7l66xb_attach_irq(struct platform_device *dev)
229 struct t7l66xb *t7l66xb = platform_get_drvdata(dev);
230 unsigned int irq, irq_base;
232 irq_base = t7l66xb->irq_base;
234 for (irq = irq_base; irq < irq_base + T7L66XB_NR_IRQS; irq++) {
235 irq_set_chip_and_handler(irq, &t7l66xb_chip, handle_level_irq);
236 irq_set_chip_data(irq, t7l66xb);
239 irq_set_irq_type(t7l66xb->irq, IRQ_TYPE_EDGE_FALLING);
240 irq_set_chained_handler_and_data(t7l66xb->irq, t7l66xb_irq, t7l66xb);
243 static void t7l66xb_detach_irq(struct platform_device *dev)
245 struct t7l66xb *t7l66xb = platform_get_drvdata(dev);
246 unsigned int irq, irq_base;
248 irq_base = t7l66xb->irq_base;
250 irq_set_chained_handler_and_data(t7l66xb->irq, NULL, NULL);
252 for (irq = irq_base; irq < irq_base + T7L66XB_NR_IRQS; irq++) {
253 irq_set_chip(irq, NULL);
254 irq_set_chip_data(irq, NULL);
258 /*--------------------------------------------------------------------------*/
261 static int t7l66xb_suspend(struct platform_device *dev, pm_message_t state)
263 struct t7l66xb *t7l66xb = platform_get_drvdata(dev);
264 struct t7l66xb_platform_data *pdata = dev_get_platdata(&dev->dev);
266 if (pdata && pdata->suspend)
268 clk_disable_unprepare(t7l66xb->clk48m);
273 static int t7l66xb_resume(struct platform_device *dev)
275 struct t7l66xb *t7l66xb = platform_get_drvdata(dev);
276 struct t7l66xb_platform_data *pdata = dev_get_platdata(&dev->dev);
279 ret = clk_prepare_enable(t7l66xb->clk48m);
283 if (pdata && pdata->resume)
286 tmio_core_mmc_enable(t7l66xb->scr + 0x200, 0,
287 t7l66xb_mmc_resources[0].start & 0xfffe);
292 #define t7l66xb_suspend NULL
293 #define t7l66xb_resume NULL
296 /*--------------------------------------------------------------------------*/
298 static int t7l66xb_probe(struct platform_device *dev)
300 struct t7l66xb_platform_data *pdata = dev_get_platdata(&dev->dev);
301 struct t7l66xb *t7l66xb;
302 struct resource *iomem, *rscr;
308 iomem = platform_get_resource(dev, IORESOURCE_MEM, 0);
312 t7l66xb = kzalloc(sizeof *t7l66xb, GFP_KERNEL);
316 raw_spin_lock_init(&t7l66xb->lock);
318 platform_set_drvdata(dev, t7l66xb);
320 ret = platform_get_irq(dev, 0);
326 t7l66xb->irq_base = pdata->irq_base;
328 t7l66xb->clk32k = clk_get(&dev->dev, "CLK_CK32K");
329 if (IS_ERR(t7l66xb->clk32k)) {
330 ret = PTR_ERR(t7l66xb->clk32k);
334 t7l66xb->clk48m = clk_get(&dev->dev, "CLK_CK48M");
335 if (IS_ERR(t7l66xb->clk48m)) {
336 ret = PTR_ERR(t7l66xb->clk48m);
340 rscr = &t7l66xb->rscr;
341 rscr->name = "t7l66xb-core";
342 rscr->start = iomem->start;
343 rscr->end = iomem->start + 0xff;
344 rscr->flags = IORESOURCE_MEM;
346 ret = request_resource(iomem, rscr);
348 goto err_request_scr;
350 t7l66xb->scr = ioremap(rscr->start, resource_size(rscr));
356 ret = clk_prepare_enable(t7l66xb->clk48m);
363 /* Mask all interrupts */
364 tmio_iowrite8(0xbf, t7l66xb->scr + SCR_IMR);
366 printk(KERN_INFO "%s rev %d @ 0x%08lx, irq %d\n",
367 dev->name, tmio_ioread8(t7l66xb->scr + SCR_REVID),
368 (unsigned long)iomem->start, t7l66xb->irq);
370 t7l66xb_attach_irq(dev);
372 t7l66xb_cells[T7L66XB_CELL_NAND].platform_data = pdata->nand_data;
373 t7l66xb_cells[T7L66XB_CELL_NAND].pdata_size = sizeof(*pdata->nand_data);
375 ret = mfd_add_devices(&dev->dev, dev->id,
376 t7l66xb_cells, ARRAY_SIZE(t7l66xb_cells),
377 iomem, t7l66xb->irq_base, NULL);
382 t7l66xb_detach_irq(dev);
383 clk_disable_unprepare(t7l66xb->clk48m);
385 iounmap(t7l66xb->scr);
387 release_resource(&t7l66xb->rscr);
389 clk_put(t7l66xb->clk48m);
391 clk_put(t7l66xb->clk32k);
398 static int t7l66xb_remove(struct platform_device *dev)
400 struct t7l66xb_platform_data *pdata = dev_get_platdata(&dev->dev);
401 struct t7l66xb *t7l66xb = platform_get_drvdata(dev);
404 ret = pdata->disable(dev);
405 clk_disable_unprepare(t7l66xb->clk48m);
406 clk_put(t7l66xb->clk48m);
407 clk_disable_unprepare(t7l66xb->clk32k);
408 clk_put(t7l66xb->clk32k);
409 t7l66xb_detach_irq(dev);
410 iounmap(t7l66xb->scr);
411 release_resource(&t7l66xb->rscr);
412 mfd_remove_devices(&dev->dev);
419 static struct platform_driver t7l66xb_platform_driver = {
423 .suspend = t7l66xb_suspend,
424 .resume = t7l66xb_resume,
425 .probe = t7l66xb_probe,
426 .remove = t7l66xb_remove,
429 /*--------------------------------------------------------------------------*/
431 module_platform_driver(t7l66xb_platform_driver);
433 MODULE_DESCRIPTION("Toshiba T7L66XB core driver");
434 MODULE_LICENSE("GPL v2");
435 MODULE_AUTHOR("Ian Molton");
436 MODULE_ALIAS("platform:t7l66xb");