2 * Toshiba TC6393XB SoC support
4 * Copyright(c) 2005-2006 Chris Humbert
5 * Copyright(c) 2005 Dirk Opfer
6 * Copyright(c) 2005 Ian Molton <spyro@f2s.com>
7 * Copyright(c) 2007 Dmitry Baryshkov
9 * Based on code written by Sharp/Lineo for 2.4 kernels
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
17 #include <linux/kernel.h>
18 #include <linux/module.h>
20 #include <linux/irq.h>
21 #include <linux/platform_device.h>
23 #include <linux/clk.h>
24 #include <linux/mfd/core.h>
25 #include <linux/mfd/tmio.h>
26 #include <linux/mfd/tc6393xb.h>
27 #include <linux/gpio.h>
29 #define SCR_REVID 0x08 /* b Revision ID */
30 #define SCR_ISR 0x50 /* b Interrupt Status */
31 #define SCR_IMR 0x52 /* b Interrupt Mask */
32 #define SCR_IRR 0x54 /* b Interrupt Routing */
33 #define SCR_GPER 0x60 /* w GP Enable */
34 #define SCR_GPI_SR(i) (0x64 + (i)) /* b3 GPI Status */
35 #define SCR_GPI_IMR(i) (0x68 + (i)) /* b3 GPI INT Mask */
36 #define SCR_GPI_EDER(i) (0x6c + (i)) /* b3 GPI Edge Detect Enable */
37 #define SCR_GPI_LIR(i) (0x70 + (i)) /* b3 GPI Level Invert */
38 #define SCR_GPO_DSR(i) (0x78 + (i)) /* b3 GPO Data Set */
39 #define SCR_GPO_DOECR(i) (0x7c + (i)) /* b3 GPO Data OE Control */
40 #define SCR_GP_IARCR(i) (0x80 + (i)) /* b3 GP Internal Active Register Control */
41 #define SCR_GP_IARLCR(i) (0x84 + (i)) /* b3 GP INTERNAL Active Register Level Control */
42 #define SCR_GPI_BCR(i) (0x88 + (i)) /* b3 GPI Buffer Control */
43 #define SCR_GPA_IARCR 0x8c /* w GPa Internal Active Register Control */
44 #define SCR_GPA_IARLCR 0x90 /* w GPa Internal Active Register Level Control */
45 #define SCR_GPA_BCR 0x94 /* w GPa Buffer Control */
46 #define SCR_CCR 0x98 /* w Clock Control */
47 #define SCR_PLL2CR 0x9a /* w PLL2 Control */
48 #define SCR_PLL1CR 0x9c /* l PLL1 Control */
49 #define SCR_DIARCR 0xa0 /* b Device Internal Active Register Control */
50 #define SCR_DBOCR 0xa1 /* b Device Buffer Off Control */
51 #define SCR_FER 0xe0 /* b Function Enable */
52 #define SCR_MCR 0xe4 /* w Mode Control */
53 #define SCR_CONFIG 0xfc /* b Configuration Control */
54 #define SCR_DEBUG 0xff /* b Debug */
56 #define SCR_CCR_CK32K BIT(0)
57 #define SCR_CCR_USBCK BIT(1)
58 #define SCR_CCR_UNK1 BIT(4)
59 #define SCR_CCR_MCLK_MASK (7 << 8)
60 #define SCR_CCR_MCLK_OFF (0 << 8)
61 #define SCR_CCR_MCLK_12 (1 << 8)
62 #define SCR_CCR_MCLK_24 (2 << 8)
63 #define SCR_CCR_MCLK_48 (3 << 8)
64 #define SCR_CCR_HCLK_MASK (3 << 12)
65 #define SCR_CCR_HCLK_24 (0 << 12)
66 #define SCR_CCR_HCLK_48 (1 << 12)
68 #define SCR_FER_USBEN BIT(0) /* USB host enable */
69 #define SCR_FER_LCDCVEN BIT(1) /* polysilicon TFT enable */
70 #define SCR_FER_SLCDEN BIT(2) /* SLCD enable */
72 #define SCR_MCR_RDY_MASK (3 << 0)
73 #define SCR_MCR_RDY_OPENDRAIN (0 << 0)
74 #define SCR_MCR_RDY_TRISTATE (1 << 0)
75 #define SCR_MCR_RDY_PUSHPULL (2 << 0)
76 #define SCR_MCR_RDY_UNK BIT(2)
77 #define SCR_MCR_RDY_EN BIT(3)
78 #define SCR_MCR_INT_MASK (3 << 4)
79 #define SCR_MCR_INT_OPENDRAIN (0 << 4)
80 #define SCR_MCR_INT_TRISTATE (1 << 4)
81 #define SCR_MCR_INT_PUSHPULL (2 << 4)
82 #define SCR_MCR_INT_UNK BIT(6)
83 #define SCR_MCR_INT_EN BIT(7)
84 /* bits 8 - 16 are unknown */
86 #define TC_GPIO_BIT(i) (1 << (i & 0x7))
88 /*--------------------------------------------------------------------------*/
93 struct gpio_chip gpio;
95 struct clk *clk; /* 3,6 Mhz */
97 spinlock_t lock; /* protects RMW cycles */
107 struct resource rscr;
108 struct resource *iomem;
117 /*--------------------------------------------------------------------------*/
119 static int tc6393xb_nand_enable(struct platform_device *nand)
121 struct platform_device *dev = to_platform_device(nand->dev.parent);
122 struct tc6393xb *tc6393xb = platform_get_drvdata(dev);
125 spin_lock_irqsave(&tc6393xb->lock, flags);
128 dev_dbg(&dev->dev, "SMD buffer on\n");
129 iowrite8(0xff, tc6393xb->scr + SCR_GPI_BCR(1));
131 spin_unlock_irqrestore(&tc6393xb->lock, flags);
136 static struct resource __devinitdata tc6393xb_nand_resources[] = {
140 .flags = IORESOURCE_MEM,
145 .flags = IORESOURCE_MEM,
148 .start = IRQ_TC6393_NAND,
149 .end = IRQ_TC6393_NAND,
150 .flags = IORESOURCE_IRQ,
154 static struct mfd_cell __devinitdata tc6393xb_cells[] = {
155 [TC6393XB_CELL_NAND] = {
157 .enable = tc6393xb_nand_enable,
158 .num_resources = ARRAY_SIZE(tc6393xb_nand_resources),
159 .resources = tc6393xb_nand_resources,
163 /*--------------------------------------------------------------------------*/
165 static int tc6393xb_gpio_get(struct gpio_chip *chip,
168 struct tc6393xb *tc6393xb = container_of(chip, struct tc6393xb, gpio);
170 /* XXX: does dsr also represent inputs? */
171 return ioread8(tc6393xb->scr + SCR_GPO_DSR(offset / 8))
172 & TC_GPIO_BIT(offset);
175 static void __tc6393xb_gpio_set(struct gpio_chip *chip,
176 unsigned offset, int value)
178 struct tc6393xb *tc6393xb = container_of(chip, struct tc6393xb, gpio);
181 dsr = ioread8(tc6393xb->scr + SCR_GPO_DSR(offset / 8));
183 dsr |= TC_GPIO_BIT(offset);
185 dsr &= ~TC_GPIO_BIT(offset);
187 iowrite8(dsr, tc6393xb->scr + SCR_GPO_DSR(offset / 8));
190 static void tc6393xb_gpio_set(struct gpio_chip *chip,
191 unsigned offset, int value)
193 struct tc6393xb *tc6393xb = container_of(chip, struct tc6393xb, gpio);
196 spin_lock_irqsave(&tc6393xb->lock, flags);
198 __tc6393xb_gpio_set(chip, offset, value);
200 spin_unlock_irqrestore(&tc6393xb->lock, flags);
203 static int tc6393xb_gpio_direction_input(struct gpio_chip *chip,
206 struct tc6393xb *tc6393xb = container_of(chip, struct tc6393xb, gpio);
210 spin_lock_irqsave(&tc6393xb->lock, flags);
212 doecr = ioread8(tc6393xb->scr + SCR_GPO_DOECR(offset / 8));
213 doecr &= ~TC_GPIO_BIT(offset);
214 iowrite8(doecr, tc6393xb->scr + SCR_GPO_DOECR(offset / 8));
216 spin_unlock_irqrestore(&tc6393xb->lock, flags);
221 static int tc6393xb_gpio_direction_output(struct gpio_chip *chip,
222 unsigned offset, int value)
224 struct tc6393xb *tc6393xb = container_of(chip, struct tc6393xb, gpio);
228 spin_lock_irqsave(&tc6393xb->lock, flags);
230 __tc6393xb_gpio_set(chip, offset, value);
232 doecr = ioread8(tc6393xb->scr + SCR_GPO_DOECR(offset / 8));
233 doecr |= TC_GPIO_BIT(offset);
234 iowrite8(doecr, tc6393xb->scr + SCR_GPO_DOECR(offset / 8));
236 spin_unlock_irqrestore(&tc6393xb->lock, flags);
241 static int tc6393xb_register_gpio(struct tc6393xb *tc6393xb, int gpio_base)
243 tc6393xb->gpio.label = "tc6393xb";
244 tc6393xb->gpio.base = gpio_base;
245 tc6393xb->gpio.ngpio = 16;
246 tc6393xb->gpio.set = tc6393xb_gpio_set;
247 tc6393xb->gpio.get = tc6393xb_gpio_get;
248 tc6393xb->gpio.direction_input = tc6393xb_gpio_direction_input;
249 tc6393xb->gpio.direction_output = tc6393xb_gpio_direction_output;
251 return gpiochip_add(&tc6393xb->gpio);
254 /*--------------------------------------------------------------------------*/
257 tc6393xb_irq(unsigned int irq, struct irq_desc *desc)
259 struct tc6393xb *tc6393xb = get_irq_data(irq);
261 unsigned int i, irq_base;
263 irq_base = tc6393xb->irq_base;
265 while ((isr = ioread8(tc6393xb->scr + SCR_ISR) &
266 ~ioread8(tc6393xb->scr + SCR_IMR)))
267 for (i = 0; i < TC6393XB_NR_IRQS; i++) {
269 generic_handle_irq(irq_base + i);
273 static void tc6393xb_irq_ack(unsigned int irq)
277 static void tc6393xb_irq_mask(unsigned int irq)
279 struct tc6393xb *tc6393xb = get_irq_chip_data(irq);
283 spin_lock_irqsave(&tc6393xb->lock, flags);
284 imr = ioread8(tc6393xb->scr + SCR_IMR);
285 imr |= 1 << (irq - tc6393xb->irq_base);
286 iowrite8(imr, tc6393xb->scr + SCR_IMR);
287 spin_unlock_irqrestore(&tc6393xb->lock, flags);
290 static void tc6393xb_irq_unmask(unsigned int irq)
292 struct tc6393xb *tc6393xb = get_irq_chip_data(irq);
296 spin_lock_irqsave(&tc6393xb->lock, flags);
297 imr = ioread8(tc6393xb->scr + SCR_IMR);
298 imr &= ~(1 << (irq - tc6393xb->irq_base));
299 iowrite8(imr, tc6393xb->scr + SCR_IMR);
300 spin_unlock_irqrestore(&tc6393xb->lock, flags);
303 static struct irq_chip tc6393xb_chip = {
305 .ack = tc6393xb_irq_ack,
306 .mask = tc6393xb_irq_mask,
307 .unmask = tc6393xb_irq_unmask,
310 static void tc6393xb_attach_irq(struct platform_device *dev)
312 struct tc6393xb *tc6393xb = platform_get_drvdata(dev);
313 unsigned int irq, irq_base;
315 irq_base = tc6393xb->irq_base;
317 for (irq = irq_base; irq < irq_base + TC6393XB_NR_IRQS; irq++) {
318 set_irq_chip(irq, &tc6393xb_chip);
319 set_irq_chip_data(irq, tc6393xb);
320 set_irq_handler(irq, handle_edge_irq);
321 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
324 set_irq_type(tc6393xb->irq, IRQ_TYPE_EDGE_FALLING);
325 set_irq_data(tc6393xb->irq, tc6393xb);
326 set_irq_chained_handler(tc6393xb->irq, tc6393xb_irq);
329 static void tc6393xb_detach_irq(struct platform_device *dev)
331 struct tc6393xb *tc6393xb = platform_get_drvdata(dev);
332 unsigned int irq, irq_base;
334 set_irq_chained_handler(tc6393xb->irq, NULL);
335 set_irq_data(tc6393xb->irq, NULL);
337 irq_base = tc6393xb->irq_base;
339 for (irq = irq_base; irq < irq_base + TC6393XB_NR_IRQS; irq++) {
340 set_irq_flags(irq, 0);
341 set_irq_chip(irq, NULL);
342 set_irq_chip_data(irq, NULL);
346 /*--------------------------------------------------------------------------*/
348 static int tc6393xb_hw_init(struct platform_device *dev)
350 struct tc6393xb_platform_data *tcpd = dev->dev.platform_data;
351 struct tc6393xb *tc6393xb = platform_get_drvdata(dev);
354 iowrite8(tc6393xb->suspend_state.fer, tc6393xb->scr + SCR_FER);
355 iowrite16(tcpd->scr_pll2cr, tc6393xb->scr + SCR_PLL2CR);
356 iowrite16(tc6393xb->suspend_state.ccr, tc6393xb->scr + SCR_CCR);
357 iowrite16(SCR_MCR_RDY_OPENDRAIN | SCR_MCR_RDY_UNK | SCR_MCR_RDY_EN |
358 SCR_MCR_INT_OPENDRAIN | SCR_MCR_INT_UNK | SCR_MCR_INT_EN |
359 BIT(15), tc6393xb->scr + SCR_MCR);
360 iowrite16(tcpd->scr_gper, tc6393xb->scr + SCR_GPER);
361 iowrite8(0, tc6393xb->scr + SCR_IRR);
362 iowrite8(0xbf, tc6393xb->scr + SCR_IMR);
364 for (i = 0; i < 3; i++) {
365 iowrite8(tc6393xb->suspend_state.gpo_dsr[i],
366 tc6393xb->scr + SCR_GPO_DSR(i));
367 iowrite8(tc6393xb->suspend_state.gpo_doecr[i],
368 tc6393xb->scr + SCR_GPO_DOECR(i));
369 iowrite8(tc6393xb->suspend_state.gpi_bcr[i],
370 tc6393xb->scr + SCR_GPI_BCR(i));
376 static int __devinit tc6393xb_probe(struct platform_device *dev)
378 struct tc6393xb_platform_data *tcpd = dev->dev.platform_data;
379 struct tc6393xb *tc6393xb;
380 struct resource *iomem;
381 struct resource *rscr;
385 iomem = platform_get_resource(dev, IORESOURCE_MEM, 0);
389 tc6393xb = kzalloc(sizeof *tc6393xb, GFP_KERNEL);
395 spin_lock_init(&tc6393xb->lock);
397 platform_set_drvdata(dev, tc6393xb);
398 tc6393xb->iomem = iomem;
399 tc6393xb->irq = platform_get_irq(dev, 0);
400 tc6393xb->irq_base = tcpd->irq_base;
402 tc6393xb->clk = clk_get(&dev->dev, "GPIO27_CLK" /* "CK3P6MI" */);
403 if (IS_ERR(tc6393xb->clk)) {
404 retval = PTR_ERR(tc6393xb->clk);
408 rscr = &tc6393xb->rscr;
409 rscr->name = "tc6393xb-core";
410 rscr->start = iomem->start;
411 rscr->end = iomem->start + 0xff;
412 rscr->flags = IORESOURCE_MEM;
414 retval = request_resource(iomem, rscr);
416 goto err_request_scr;
418 tc6393xb->scr = ioremap(rscr->start, rscr->end - rscr->start + 1);
419 if (!tc6393xb->scr) {
424 retval = clk_enable(tc6393xb->clk);
428 retval = tcpd->enable(dev);
432 tc6393xb->suspend_state.fer = 0;
433 for (i = 0; i < 3; i++) {
434 tc6393xb->suspend_state.gpo_dsr[i] =
435 (tcpd->scr_gpo_dsr >> (8 * i)) & 0xff;
436 tc6393xb->suspend_state.gpo_doecr[i] =
437 (tcpd->scr_gpo_doecr >> (8 * i)) & 0xff;
440 * It may be necessary to change this back to
441 * platform-dependant code
443 tc6393xb->suspend_state.ccr = SCR_CCR_UNK1 |
446 retval = tc6393xb_hw_init(dev);
450 printk(KERN_INFO "Toshiba tc6393xb revision %d at 0x%08lx, irq %d\n",
451 ioread8(tc6393xb->scr + SCR_REVID),
452 (unsigned long) iomem->start, tc6393xb->irq);
454 tc6393xb->gpio.base = -1;
456 if (tcpd->gpio_base >= 0) {
457 retval = tc6393xb_register_gpio(tc6393xb, tcpd->gpio_base);
463 tc6393xb_attach_irq(dev);
465 tc6393xb_cells[TC6393XB_CELL_NAND].driver_data = tcpd->nand_data;
466 tc6393xb_cells[TC6393XB_CELL_NAND].platform_data =
467 &tc6393xb_cells[TC6393XB_CELL_NAND];
468 tc6393xb_cells[TC6393XB_CELL_NAND].data_size =
469 sizeof(tc6393xb_cells[TC6393XB_CELL_NAND]);
471 retval = mfd_add_devices(&dev->dev, dev->id,
472 tc6393xb_cells, ARRAY_SIZE(tc6393xb_cells),
473 iomem, tcpd->irq_base);
478 tc6393xb_detach_irq(dev);
481 if (tc6393xb->gpio.base != -1)
482 temp = gpiochip_remove(&tc6393xb->gpio);
486 clk_disable(tc6393xb->clk);
488 iounmap(tc6393xb->scr);
490 release_resource(&tc6393xb->rscr);
492 clk_put(tc6393xb->clk);
499 static int __devexit tc6393xb_remove(struct platform_device *dev)
501 struct tc6393xb_platform_data *tcpd = dev->dev.platform_data;
502 struct tc6393xb *tc6393xb = platform_get_drvdata(dev);
505 mfd_remove_devices(&dev->dev);
508 tc6393xb_detach_irq(dev);
510 if (tc6393xb->gpio.base != -1) {
511 ret = gpiochip_remove(&tc6393xb->gpio);
513 dev_err(&dev->dev, "Can't remove gpio chip: %d\n", ret);
518 ret = tcpd->disable(dev);
520 clk_disable(tc6393xb->clk);
522 iounmap(tc6393xb->scr);
524 release_resource(&tc6393xb->rscr);
526 platform_set_drvdata(dev, NULL);
528 clk_put(tc6393xb->clk);
536 static int tc6393xb_suspend(struct platform_device *dev, pm_message_t state)
538 struct tc6393xb_platform_data *tcpd = dev->dev.platform_data;
539 struct tc6393xb *tc6393xb = platform_get_drvdata(dev);
543 tc6393xb->suspend_state.ccr = ioread16(tc6393xb->scr + SCR_CCR);
544 tc6393xb->suspend_state.fer = ioread8(tc6393xb->scr + SCR_FER);
546 for (i = 0; i < 3; i++) {
547 tc6393xb->suspend_state.gpo_dsr[i] =
548 ioread8(tc6393xb->scr + SCR_GPO_DSR(i));
549 tc6393xb->suspend_state.gpo_doecr[i] =
550 ioread8(tc6393xb->scr + SCR_GPO_DOECR(i));
551 tc6393xb->suspend_state.gpi_bcr[i] =
552 ioread8(tc6393xb->scr + SCR_GPI_BCR(i));
555 return tcpd->suspend(dev);
558 static int tc6393xb_resume(struct platform_device *dev)
560 struct tc6393xb_platform_data *tcpd = dev->dev.platform_data;
561 int ret = tcpd->resume(dev);
566 return tc6393xb_hw_init(dev);
569 #define tc6393xb_suspend NULL
570 #define tc6393xb_resume NULL
573 static struct platform_driver tc6393xb_driver = {
574 .probe = tc6393xb_probe,
575 .remove = __devexit_p(tc6393xb_remove),
576 .suspend = tc6393xb_suspend,
577 .resume = tc6393xb_resume,
581 .owner = THIS_MODULE,
585 static int __init tc6393xb_init(void)
587 return platform_driver_register(&tc6393xb_driver);
590 static void __exit tc6393xb_exit(void)
592 platform_driver_unregister(&tc6393xb_driver);
595 subsys_initcall(tc6393xb_init);
596 module_exit(tc6393xb_exit);
598 MODULE_LICENSE("GPL");
599 MODULE_AUTHOR("Ian Molton, Dmitry Baryshkov and Dirk Opfer");
600 MODULE_DESCRIPTION("tc6393xb Toshiba Mobile IO Controller");
601 MODULE_ALIAS("platform:tc6393xb");