On Tue, Nov 06, 2007 at 02:33:53AM -0800, akpm@linux-foundation.org wrote:
[mmotm.git] / drivers / mfd / asic3.c
blobff79b00d14f2151c386ca3e1398a4f8941030d6a
1 /*
2 * driver/mfd/asic3.c
4 * Compaq ASIC3 support.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * Copyright 2001 Compaq Computer Corporation.
11 * Copyright 2004-2005 Phil Blundell
12 * Copyright 2007-2008 OpenedHand Ltd.
14 * Authors: Phil Blundell <pb@handhelds.org>,
15 * Samuel Ortiz <sameo@openedhand.com>
19 #include <linux/kernel.h>
20 #include <linux/delay.h>
21 #include <linux/irq.h>
22 #include <linux/gpio.h>
23 #include <linux/io.h>
24 #include <linux/spinlock.h>
25 #include <linux/platform_device.h>
27 #include <linux/mfd/asic3.h>
28 #include <linux/mfd/core.h>
29 #include <linux/mfd/ds1wm.h>
30 #include <linux/mfd/tmio.h>
32 enum {
33 ASIC3_CLOCK_SPI,
34 ASIC3_CLOCK_OWM,
35 ASIC3_CLOCK_PWM0,
36 ASIC3_CLOCK_PWM1,
37 ASIC3_CLOCK_LED0,
38 ASIC3_CLOCK_LED1,
39 ASIC3_CLOCK_LED2,
40 ASIC3_CLOCK_SD_HOST,
41 ASIC3_CLOCK_SD_BUS,
42 ASIC3_CLOCK_SMBUS,
43 ASIC3_CLOCK_EX0,
44 ASIC3_CLOCK_EX1,
47 struct asic3_clk {
48 int enabled;
49 unsigned int cdex;
50 unsigned long rate;
53 #define INIT_CDEX(_name, _rate) \
54 [ASIC3_CLOCK_##_name] = { \
55 .cdex = CLOCK_CDEX_##_name, \
56 .rate = _rate, \
59 struct asic3_clk asic3_clk_init[] __initdata = {
60 INIT_CDEX(SPI, 0),
61 INIT_CDEX(OWM, 5000000),
62 INIT_CDEX(PWM0, 0),
63 INIT_CDEX(PWM1, 0),
64 INIT_CDEX(LED0, 0),
65 INIT_CDEX(LED1, 0),
66 INIT_CDEX(LED2, 0),
67 INIT_CDEX(SD_HOST, 24576000),
68 INIT_CDEX(SD_BUS, 12288000),
69 INIT_CDEX(SMBUS, 0),
70 INIT_CDEX(EX0, 32768),
71 INIT_CDEX(EX1, 24576000),
74 struct asic3 {
75 void __iomem *mapping;
76 unsigned int bus_shift;
77 unsigned int irq_nr;
78 unsigned int irq_base;
79 spinlock_t lock;
80 u16 irq_bothedge[4];
81 struct gpio_chip gpio;
82 struct device *dev;
83 void __iomem *tmio_cnf;
85 struct asic3_clk clocks[ARRAY_SIZE(asic3_clk_init)];
88 static int asic3_gpio_get(struct gpio_chip *chip, unsigned offset);
90 static inline void asic3_write_register(struct asic3 *asic,
91 unsigned int reg, u32 value)
93 iowrite16(value, asic->mapping +
94 (reg >> asic->bus_shift));
97 static inline u32 asic3_read_register(struct asic3 *asic,
98 unsigned int reg)
100 return ioread16(asic->mapping +
101 (reg >> asic->bus_shift));
104 void asic3_set_register(struct asic3 *asic, u32 reg, u32 bits, bool set)
106 unsigned long flags;
107 u32 val;
109 spin_lock_irqsave(&asic->lock, flags);
110 val = asic3_read_register(asic, reg);
111 if (set)
112 val |= bits;
113 else
114 val &= ~bits;
115 asic3_write_register(asic, reg, val);
116 spin_unlock_irqrestore(&asic->lock, flags);
119 /* IRQs */
120 #define MAX_ASIC_ISR_LOOPS 20
121 #define ASIC3_GPIO_BASE_INCR \
122 (ASIC3_GPIO_B_BASE - ASIC3_GPIO_A_BASE)
124 static void asic3_irq_flip_edge(struct asic3 *asic,
125 u32 base, int bit)
127 u16 edge;
128 unsigned long flags;
130 spin_lock_irqsave(&asic->lock, flags);
131 edge = asic3_read_register(asic,
132 base + ASIC3_GPIO_EDGE_TRIGGER);
133 edge ^= bit;
134 asic3_write_register(asic,
135 base + ASIC3_GPIO_EDGE_TRIGGER, edge);
136 spin_unlock_irqrestore(&asic->lock, flags);
139 static void asic3_irq_demux(unsigned int irq, struct irq_desc *desc)
141 int iter, i;
142 unsigned long flags;
143 struct asic3 *asic;
145 desc->chip->ack(irq);
147 asic = desc->handler_data;
149 for (iter = 0 ; iter < MAX_ASIC_ISR_LOOPS; iter++) {
150 u32 status;
151 int bank;
153 spin_lock_irqsave(&asic->lock, flags);
154 status = asic3_read_register(asic,
155 ASIC3_OFFSET(INTR, P_INT_STAT));
156 spin_unlock_irqrestore(&asic->lock, flags);
158 /* Check all ten register bits */
159 if ((status & 0x3ff) == 0)
160 break;
162 /* Handle GPIO IRQs */
163 for (bank = 0; bank < ASIC3_NUM_GPIO_BANKS; bank++) {
164 if (status & (1 << bank)) {
165 unsigned long base, istat;
167 base = ASIC3_GPIO_A_BASE
168 + bank * ASIC3_GPIO_BASE_INCR;
170 spin_lock_irqsave(&asic->lock, flags);
171 istat = asic3_read_register(asic,
172 base +
173 ASIC3_GPIO_INT_STATUS);
174 /* Clearing IntStatus */
175 asic3_write_register(asic,
176 base +
177 ASIC3_GPIO_INT_STATUS, 0);
178 spin_unlock_irqrestore(&asic->lock, flags);
180 for (i = 0; i < ASIC3_GPIOS_PER_BANK; i++) {
181 int bit = (1 << i);
182 unsigned int irqnr;
184 if (!(istat & bit))
185 continue;
187 irqnr = asic->irq_base +
188 (ASIC3_GPIOS_PER_BANK * bank)
189 + i;
190 desc = irq_to_desc(irqnr);
191 desc->handle_irq(irqnr, desc);
192 if (asic->irq_bothedge[bank] & bit)
193 asic3_irq_flip_edge(asic, base,
194 bit);
199 /* Handle remaining IRQs in the status register */
200 for (i = ASIC3_NUM_GPIOS; i < ASIC3_NR_IRQS; i++) {
201 /* They start at bit 4 and go up */
202 if (status & (1 << (i - ASIC3_NUM_GPIOS + 4))) {
203 desc = irq_to_desc(asic->irq_base + i);
204 desc->handle_irq(asic->irq_base + i,
205 desc);
210 if (iter >= MAX_ASIC_ISR_LOOPS)
211 dev_err(asic->dev, "interrupt processing overrun\n");
214 static inline int asic3_irq_to_bank(struct asic3 *asic, int irq)
216 int n;
218 n = (irq - asic->irq_base) >> 4;
220 return (n * (ASIC3_GPIO_B_BASE - ASIC3_GPIO_A_BASE));
223 static inline int asic3_irq_to_index(struct asic3 *asic, int irq)
225 return (irq - asic->irq_base) & 0xf;
228 static void asic3_mask_gpio_irq(unsigned int irq)
230 struct asic3 *asic = get_irq_chip_data(irq);
231 u32 val, bank, index;
232 unsigned long flags;
234 bank = asic3_irq_to_bank(asic, irq);
235 index = asic3_irq_to_index(asic, irq);
237 spin_lock_irqsave(&asic->lock, flags);
238 val = asic3_read_register(asic, bank + ASIC3_GPIO_MASK);
239 val |= 1 << index;
240 asic3_write_register(asic, bank + ASIC3_GPIO_MASK, val);
241 spin_unlock_irqrestore(&asic->lock, flags);
244 static void asic3_mask_irq(unsigned int irq)
246 struct asic3 *asic = get_irq_chip_data(irq);
247 int regval;
248 unsigned long flags;
250 spin_lock_irqsave(&asic->lock, flags);
251 regval = asic3_read_register(asic,
252 ASIC3_INTR_BASE +
253 ASIC3_INTR_INT_MASK);
255 regval &= ~(ASIC3_INTMASK_MASK0 <<
256 (irq - (asic->irq_base + ASIC3_NUM_GPIOS)));
258 asic3_write_register(asic,
259 ASIC3_INTR_BASE +
260 ASIC3_INTR_INT_MASK,
261 regval);
262 spin_unlock_irqrestore(&asic->lock, flags);
265 static void asic3_unmask_gpio_irq(unsigned int irq)
267 struct asic3 *asic = get_irq_chip_data(irq);
268 u32 val, bank, index;
269 unsigned long flags;
271 bank = asic3_irq_to_bank(asic, irq);
272 index = asic3_irq_to_index(asic, irq);
274 spin_lock_irqsave(&asic->lock, flags);
275 val = asic3_read_register(asic, bank + ASIC3_GPIO_MASK);
276 val &= ~(1 << index);
277 asic3_write_register(asic, bank + ASIC3_GPIO_MASK, val);
278 spin_unlock_irqrestore(&asic->lock, flags);
281 static void asic3_unmask_irq(unsigned int irq)
283 struct asic3 *asic = get_irq_chip_data(irq);
284 int regval;
285 unsigned long flags;
287 spin_lock_irqsave(&asic->lock, flags);
288 regval = asic3_read_register(asic,
289 ASIC3_INTR_BASE +
290 ASIC3_INTR_INT_MASK);
292 regval |= (ASIC3_INTMASK_MASK0 <<
293 (irq - (asic->irq_base + ASIC3_NUM_GPIOS)));
295 asic3_write_register(asic,
296 ASIC3_INTR_BASE +
297 ASIC3_INTR_INT_MASK,
298 regval);
299 spin_unlock_irqrestore(&asic->lock, flags);
302 static int asic3_gpio_irq_type(unsigned int irq, unsigned int type)
304 struct asic3 *asic = get_irq_chip_data(irq);
305 u32 bank, index;
306 u16 trigger, level, edge, bit;
307 unsigned long flags;
309 bank = asic3_irq_to_bank(asic, irq);
310 index = asic3_irq_to_index(asic, irq);
311 bit = 1<<index;
313 spin_lock_irqsave(&asic->lock, flags);
314 level = asic3_read_register(asic,
315 bank + ASIC3_GPIO_LEVEL_TRIGGER);
316 edge = asic3_read_register(asic,
317 bank + ASIC3_GPIO_EDGE_TRIGGER);
318 trigger = asic3_read_register(asic,
319 bank + ASIC3_GPIO_TRIGGER_TYPE);
320 asic->irq_bothedge[(irq - asic->irq_base) >> 4] &= ~bit;
322 if (type == IRQ_TYPE_EDGE_RISING) {
323 trigger |= bit;
324 edge |= bit;
325 } else if (type == IRQ_TYPE_EDGE_FALLING) {
326 trigger |= bit;
327 edge &= ~bit;
328 } else if (type == IRQ_TYPE_EDGE_BOTH) {
329 trigger |= bit;
330 if (asic3_gpio_get(&asic->gpio, irq - asic->irq_base))
331 edge &= ~bit;
332 else
333 edge |= bit;
334 asic->irq_bothedge[(irq - asic->irq_base) >> 4] |= bit;
335 } else if (type == IRQ_TYPE_LEVEL_LOW) {
336 trigger &= ~bit;
337 level &= ~bit;
338 } else if (type == IRQ_TYPE_LEVEL_HIGH) {
339 trigger &= ~bit;
340 level |= bit;
341 } else {
343 * if type == IRQ_TYPE_NONE, we should mask interrupts, but
344 * be careful to not unmask them if mask was also called.
345 * Probably need internal state for mask.
347 dev_notice(asic->dev, "irq type not changed\n");
349 asic3_write_register(asic, bank + ASIC3_GPIO_LEVEL_TRIGGER,
350 level);
351 asic3_write_register(asic, bank + ASIC3_GPIO_EDGE_TRIGGER,
352 edge);
353 asic3_write_register(asic, bank + ASIC3_GPIO_TRIGGER_TYPE,
354 trigger);
355 spin_unlock_irqrestore(&asic->lock, flags);
356 return 0;
359 static struct irq_chip asic3_gpio_irq_chip = {
360 .name = "ASIC3-GPIO",
361 .ack = asic3_mask_gpio_irq,
362 .mask = asic3_mask_gpio_irq,
363 .unmask = asic3_unmask_gpio_irq,
364 .set_type = asic3_gpio_irq_type,
367 static struct irq_chip asic3_irq_chip = {
368 .name = "ASIC3",
369 .ack = asic3_mask_irq,
370 .mask = asic3_mask_irq,
371 .unmask = asic3_unmask_irq,
374 static int __init asic3_irq_probe(struct platform_device *pdev)
376 struct asic3 *asic = platform_get_drvdata(pdev);
377 unsigned long clksel = 0;
378 unsigned int irq, irq_base;
379 int ret;
381 ret = platform_get_irq(pdev, 0);
382 if (ret < 0)
383 return ret;
384 asic->irq_nr = ret;
386 /* turn on clock to IRQ controller */
387 clksel |= CLOCK_SEL_CX;
388 asic3_write_register(asic, ASIC3_OFFSET(CLOCK, SEL),
389 clksel);
391 irq_base = asic->irq_base;
393 for (irq = irq_base; irq < irq_base + ASIC3_NR_IRQS; irq++) {
394 if (irq < asic->irq_base + ASIC3_NUM_GPIOS)
395 set_irq_chip(irq, &asic3_gpio_irq_chip);
396 else
397 set_irq_chip(irq, &asic3_irq_chip);
399 set_irq_chip_data(irq, asic);
400 set_irq_handler(irq, handle_level_irq);
401 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
404 asic3_write_register(asic, ASIC3_OFFSET(INTR, INT_MASK),
405 ASIC3_INTMASK_GINTMASK);
407 set_irq_chained_handler(asic->irq_nr, asic3_irq_demux);
408 set_irq_type(asic->irq_nr, IRQ_TYPE_EDGE_RISING);
409 set_irq_data(asic->irq_nr, asic);
411 return 0;
414 static void asic3_irq_remove(struct platform_device *pdev)
416 struct asic3 *asic = platform_get_drvdata(pdev);
417 unsigned int irq, irq_base;
419 irq_base = asic->irq_base;
421 for (irq = irq_base; irq < irq_base + ASIC3_NR_IRQS; irq++) {
422 set_irq_flags(irq, 0);
423 set_irq_handler(irq, NULL);
424 set_irq_chip(irq, NULL);
425 set_irq_chip_data(irq, NULL);
427 set_irq_chained_handler(asic->irq_nr, NULL);
430 /* GPIOs */
431 static int asic3_gpio_direction(struct gpio_chip *chip,
432 unsigned offset, int out)
434 u32 mask = ASIC3_GPIO_TO_MASK(offset), out_reg;
435 unsigned int gpio_base;
436 unsigned long flags;
437 struct asic3 *asic;
439 asic = container_of(chip, struct asic3, gpio);
440 gpio_base = ASIC3_GPIO_TO_BASE(offset);
442 if (gpio_base > ASIC3_GPIO_D_BASE) {
443 dev_err(asic->dev, "Invalid base (0x%x) for gpio %d\n",
444 gpio_base, offset);
445 return -EINVAL;
448 spin_lock_irqsave(&asic->lock, flags);
450 out_reg = asic3_read_register(asic, gpio_base + ASIC3_GPIO_DIRECTION);
452 /* Input is 0, Output is 1 */
453 if (out)
454 out_reg |= mask;
455 else
456 out_reg &= ~mask;
458 asic3_write_register(asic, gpio_base + ASIC3_GPIO_DIRECTION, out_reg);
460 spin_unlock_irqrestore(&asic->lock, flags);
462 return 0;
466 static int asic3_gpio_direction_input(struct gpio_chip *chip,
467 unsigned offset)
469 return asic3_gpio_direction(chip, offset, 0);
472 static int asic3_gpio_direction_output(struct gpio_chip *chip,
473 unsigned offset, int value)
475 return asic3_gpio_direction(chip, offset, 1);
478 static int asic3_gpio_get(struct gpio_chip *chip,
479 unsigned offset)
481 unsigned int gpio_base;
482 u32 mask = ASIC3_GPIO_TO_MASK(offset);
483 struct asic3 *asic;
485 asic = container_of(chip, struct asic3, gpio);
486 gpio_base = ASIC3_GPIO_TO_BASE(offset);
488 if (gpio_base > ASIC3_GPIO_D_BASE) {
489 dev_err(asic->dev, "Invalid base (0x%x) for gpio %d\n",
490 gpio_base, offset);
491 return -EINVAL;
494 return asic3_read_register(asic, gpio_base + ASIC3_GPIO_STATUS) & mask;
497 static void asic3_gpio_set(struct gpio_chip *chip,
498 unsigned offset, int value)
500 u32 mask, out_reg;
501 unsigned int gpio_base;
502 unsigned long flags;
503 struct asic3 *asic;
505 asic = container_of(chip, struct asic3, gpio);
506 gpio_base = ASIC3_GPIO_TO_BASE(offset);
508 if (gpio_base > ASIC3_GPIO_D_BASE) {
509 dev_err(asic->dev, "Invalid base (0x%x) for gpio %d\n",
510 gpio_base, offset);
511 return;
514 mask = ASIC3_GPIO_TO_MASK(offset);
516 spin_lock_irqsave(&asic->lock, flags);
518 out_reg = asic3_read_register(asic, gpio_base + ASIC3_GPIO_OUT);
520 if (value)
521 out_reg |= mask;
522 else
523 out_reg &= ~mask;
525 asic3_write_register(asic, gpio_base + ASIC3_GPIO_OUT, out_reg);
527 spin_unlock_irqrestore(&asic->lock, flags);
529 return;
532 static __init int asic3_gpio_probe(struct platform_device *pdev,
533 u16 *gpio_config, int num)
535 struct asic3 *asic = platform_get_drvdata(pdev);
536 u16 alt_reg[ASIC3_NUM_GPIO_BANKS];
537 u16 out_reg[ASIC3_NUM_GPIO_BANKS];
538 u16 dir_reg[ASIC3_NUM_GPIO_BANKS];
539 int i;
541 memset(alt_reg, 0, ASIC3_NUM_GPIO_BANKS * sizeof(u16));
542 memset(out_reg, 0, ASIC3_NUM_GPIO_BANKS * sizeof(u16));
543 memset(dir_reg, 0, ASIC3_NUM_GPIO_BANKS * sizeof(u16));
545 /* Enable all GPIOs */
546 asic3_write_register(asic, ASIC3_GPIO_OFFSET(A, MASK), 0xffff);
547 asic3_write_register(asic, ASIC3_GPIO_OFFSET(B, MASK), 0xffff);
548 asic3_write_register(asic, ASIC3_GPIO_OFFSET(C, MASK), 0xffff);
549 asic3_write_register(asic, ASIC3_GPIO_OFFSET(D, MASK), 0xffff);
551 for (i = 0; i < num; i++) {
552 u8 alt, pin, dir, init, bank_num, bit_num;
553 u16 config = gpio_config[i];
555 pin = ASIC3_CONFIG_GPIO_PIN(config);
556 alt = ASIC3_CONFIG_GPIO_ALT(config);
557 dir = ASIC3_CONFIG_GPIO_DIR(config);
558 init = ASIC3_CONFIG_GPIO_INIT(config);
560 bank_num = ASIC3_GPIO_TO_BANK(pin);
561 bit_num = ASIC3_GPIO_TO_BIT(pin);
563 alt_reg[bank_num] |= (alt << bit_num);
564 out_reg[bank_num] |= (init << bit_num);
565 dir_reg[bank_num] |= (dir << bit_num);
568 for (i = 0; i < ASIC3_NUM_GPIO_BANKS; i++) {
569 asic3_write_register(asic,
570 ASIC3_BANK_TO_BASE(i) +
571 ASIC3_GPIO_DIRECTION,
572 dir_reg[i]);
573 asic3_write_register(asic,
574 ASIC3_BANK_TO_BASE(i) + ASIC3_GPIO_OUT,
575 out_reg[i]);
576 asic3_write_register(asic,
577 ASIC3_BANK_TO_BASE(i) +
578 ASIC3_GPIO_ALT_FUNCTION,
579 alt_reg[i]);
582 return gpiochip_add(&asic->gpio);
585 static int asic3_gpio_remove(struct platform_device *pdev)
587 struct asic3 *asic = platform_get_drvdata(pdev);
589 return gpiochip_remove(&asic->gpio);
592 static int asic3_clk_enable(struct asic3 *asic, struct asic3_clk *clk)
594 unsigned long flags;
595 u32 cdex;
597 spin_lock_irqsave(&asic->lock, flags);
598 if (clk->enabled++ == 0) {
599 cdex = asic3_read_register(asic, ASIC3_OFFSET(CLOCK, CDEX));
600 cdex |= clk->cdex;
601 asic3_write_register(asic, ASIC3_OFFSET(CLOCK, CDEX), cdex);
603 spin_unlock_irqrestore(&asic->lock, flags);
605 return 0;
608 static void asic3_clk_disable(struct asic3 *asic, struct asic3_clk *clk)
610 unsigned long flags;
611 u32 cdex;
613 WARN_ON(clk->enabled == 0);
615 spin_lock_irqsave(&asic->lock, flags);
616 if (--clk->enabled == 0) {
617 cdex = asic3_read_register(asic, ASIC3_OFFSET(CLOCK, CDEX));
618 cdex &= ~clk->cdex;
619 asic3_write_register(asic, ASIC3_OFFSET(CLOCK, CDEX), cdex);
621 spin_unlock_irqrestore(&asic->lock, flags);
624 /* MFD cells (SPI, PWM, LED, DS1WM, MMC) */
625 static struct ds1wm_driver_data ds1wm_pdata = {
626 .active_high = 1,
629 static struct resource ds1wm_resources[] = {
631 .start = ASIC3_OWM_BASE,
632 .end = ASIC3_OWM_BASE + 0x13,
633 .flags = IORESOURCE_MEM,
636 .start = ASIC3_IRQ_OWM,
637 .start = ASIC3_IRQ_OWM,
638 .flags = IORESOURCE_IRQ | IORESOURCE_IRQ_HIGHEDGE,
642 static int ds1wm_enable(struct platform_device *pdev)
644 struct asic3 *asic = dev_get_drvdata(pdev->dev.parent);
646 /* Turn on external clocks and the OWM clock */
647 asic3_clk_enable(asic, &asic->clocks[ASIC3_CLOCK_EX0]);
648 asic3_clk_enable(asic, &asic->clocks[ASIC3_CLOCK_EX1]);
649 asic3_clk_enable(asic, &asic->clocks[ASIC3_CLOCK_OWM]);
650 msleep(1);
652 /* Reset and enable DS1WM */
653 asic3_set_register(asic, ASIC3_OFFSET(EXTCF, RESET),
654 ASIC3_EXTCF_OWM_RESET, 1);
655 msleep(1);
656 asic3_set_register(asic, ASIC3_OFFSET(EXTCF, RESET),
657 ASIC3_EXTCF_OWM_RESET, 0);
658 msleep(1);
659 asic3_set_register(asic, ASIC3_OFFSET(EXTCF, SELECT),
660 ASIC3_EXTCF_OWM_EN, 1);
661 msleep(1);
663 return 0;
666 static int ds1wm_disable(struct platform_device *pdev)
668 struct asic3 *asic = dev_get_drvdata(pdev->dev.parent);
670 asic3_set_register(asic, ASIC3_OFFSET(EXTCF, SELECT),
671 ASIC3_EXTCF_OWM_EN, 0);
673 asic3_clk_disable(asic, &asic->clocks[ASIC3_CLOCK_OWM]);
674 asic3_clk_disable(asic, &asic->clocks[ASIC3_CLOCK_EX0]);
675 asic3_clk_disable(asic, &asic->clocks[ASIC3_CLOCK_EX1]);
677 return 0;
680 static struct mfd_cell asic3_cell_ds1wm = {
681 .name = "ds1wm",
682 .enable = ds1wm_enable,
683 .disable = ds1wm_disable,
684 .driver_data = &ds1wm_pdata,
685 .num_resources = ARRAY_SIZE(ds1wm_resources),
686 .resources = ds1wm_resources,
689 static void asic3_mmc_pwr(struct platform_device *pdev, int state)
691 struct asic3 *asic = dev_get_drvdata(pdev->dev.parent);
693 tmio_core_mmc_pwr(asic->tmio_cnf, state);
696 static void asic3_mmc_clk_div(struct platform_device *pdev, int state)
698 struct asic3 *asic = dev_get_drvdata(pdev->dev.parent);
700 tmio_core_mmc_clk_div(asic->tmio_cnf, state);
703 static struct tmio_mmc_data asic3_mmc_data = {
704 .hclk = 24576000,
705 .set_pwr = asic3_mmc_pwr,
706 .set_no_clk_div = asic3_mmc_clk_div,
709 static struct resource asic3_mmc_resources[] = {
711 .start = ASIC3_SD_CTRL_BASE,
712 .end = ASIC3_SD_CTRL_BASE + 0x3ff,
713 .flags = IORESOURCE_MEM,
716 .start = 0,
717 .end = 0,
718 .flags = IORESOURCE_IRQ,
722 static int asic3_mmc_enable(struct platform_device *pdev)
724 struct asic3 *asic = dev_get_drvdata(pdev->dev.parent);
726 /* Not sure if it must be done bit by bit, but leaving as-is */
727 asic3_set_register(asic, ASIC3_OFFSET(SDHWCTRL, SDCONF),
728 ASIC3_SDHWCTRL_LEVCD, 1);
729 asic3_set_register(asic, ASIC3_OFFSET(SDHWCTRL, SDCONF),
730 ASIC3_SDHWCTRL_LEVWP, 1);
731 asic3_set_register(asic, ASIC3_OFFSET(SDHWCTRL, SDCONF),
732 ASIC3_SDHWCTRL_SUSPEND, 0);
733 asic3_set_register(asic, ASIC3_OFFSET(SDHWCTRL, SDCONF),
734 ASIC3_SDHWCTRL_PCLR, 0);
736 asic3_clk_enable(asic, &asic->clocks[ASIC3_CLOCK_EX0]);
737 /* CLK32 used for card detection and for interruption detection
738 * when HCLK is stopped.
740 asic3_clk_enable(asic, &asic->clocks[ASIC3_CLOCK_EX1]);
741 msleep(1);
743 /* HCLK 24.576 MHz, BCLK 12.288 MHz: */
744 asic3_write_register(asic, ASIC3_OFFSET(CLOCK, SEL),
745 CLOCK_SEL_CX | CLOCK_SEL_SD_HCLK_SEL);
747 asic3_clk_enable(asic, &asic->clocks[ASIC3_CLOCK_SD_HOST]);
748 asic3_clk_enable(asic, &asic->clocks[ASIC3_CLOCK_SD_BUS]);
749 msleep(1);
751 asic3_set_register(asic, ASIC3_OFFSET(EXTCF, SELECT),
752 ASIC3_EXTCF_SD_MEM_ENABLE, 1);
754 /* Enable SD card slot 3.3V power supply */
755 asic3_set_register(asic, ASIC3_OFFSET(SDHWCTRL, SDCONF),
756 ASIC3_SDHWCTRL_SDPWR, 1);
758 /* ASIC3_SD_CTRL_BASE assumes 32-bit addressing, TMIO is 16-bit */
759 tmio_core_mmc_enable(asic->tmio_cnf, ASIC3_SD_CTRL_BASE >> 1);
761 return 0;
764 static int asic3_mmc_disable(struct platform_device *pdev)
766 struct asic3 *asic = dev_get_drvdata(pdev->dev.parent);
768 /* Put in suspend mode */
769 asic3_set_register(asic, ASIC3_OFFSET(SDHWCTRL, SDCONF),
770 ASIC3_SDHWCTRL_SUSPEND, 1);
772 /* Disable clocks */
773 asic3_clk_disable(asic, &asic->clocks[ASIC3_CLOCK_SD_HOST]);
774 asic3_clk_disable(asic, &asic->clocks[ASIC3_CLOCK_SD_BUS]);
775 asic3_clk_disable(asic, &asic->clocks[ASIC3_CLOCK_EX0]);
776 asic3_clk_disable(asic, &asic->clocks[ASIC3_CLOCK_EX1]);
777 return 0;
780 static struct mfd_cell asic3_cell_mmc = {
781 .name = "tmio-mmc",
782 .enable = asic3_mmc_enable,
783 .disable = asic3_mmc_disable,
784 .suspend = asic3_mmc_disable,
785 .resume = asic3_mmc_enable,
786 .driver_data = &asic3_mmc_data,
787 .num_resources = ARRAY_SIZE(asic3_mmc_resources),
788 .resources = asic3_mmc_resources,
791 static int __init asic3_mfd_probe(struct platform_device *pdev,
792 struct resource *mem)
794 struct asic3 *asic = platform_get_drvdata(pdev);
795 struct resource *mem_sdio;
796 int irq, ret;
798 mem_sdio = platform_get_resource(pdev, IORESOURCE_MEM, 1);
799 if (!mem_sdio)
800 dev_dbg(asic->dev, "no SDIO MEM resource\n");
802 irq = platform_get_irq(pdev, 1);
803 if (irq < 0)
804 dev_dbg(asic->dev, "no SDIO IRQ resource\n");
806 /* DS1WM */
807 asic3_set_register(asic, ASIC3_OFFSET(EXTCF, SELECT),
808 ASIC3_EXTCF_OWM_SMB, 0);
810 ds1wm_resources[0].start >>= asic->bus_shift;
811 ds1wm_resources[0].end >>= asic->bus_shift;
813 asic3_cell_ds1wm.platform_data = &asic3_cell_ds1wm;
814 asic3_cell_ds1wm.data_size = sizeof(asic3_cell_ds1wm);
816 /* MMC */
817 asic->tmio_cnf = ioremap((ASIC3_SD_CONFIG_BASE >> asic->bus_shift) +
818 mem_sdio->start, 0x400 >> asic->bus_shift);
819 if (!asic->tmio_cnf) {
820 ret = -ENOMEM;
821 dev_dbg(asic->dev, "Couldn't ioremap SD_CONFIG\n");
822 goto out;
824 asic3_mmc_resources[0].start >>= asic->bus_shift;
825 asic3_mmc_resources[0].end >>= asic->bus_shift;
827 asic3_cell_mmc.platform_data = &asic3_cell_mmc;
828 asic3_cell_mmc.data_size = sizeof(asic3_cell_mmc);
830 tmio_core_set_bus_shift(1 - asic->bus_shift);
832 ret = mfd_add_devices(&pdev->dev, pdev->id,
833 &asic3_cell_ds1wm, 1, mem, asic->irq_base);
834 if (ret < 0)
835 goto out;
837 if (mem_sdio && (irq >= 0))
838 ret = mfd_add_devices(&pdev->dev, pdev->id,
839 &asic3_cell_mmc, 1, mem_sdio, irq);
841 out:
842 return ret;
845 static void asic3_mfd_remove(struct platform_device *pdev)
847 struct asic3 *asic = platform_get_drvdata(pdev);
849 mfd_remove_devices(&pdev->dev);
850 iounmap(asic->tmio_cnf);
853 /* Core */
854 static int __init asic3_probe(struct platform_device *pdev)
856 struct asic3_platform_data *pdata = pdev->dev.platform_data;
857 struct asic3 *asic;
858 struct resource *mem;
859 unsigned long clksel;
860 int ret = 0;
862 asic = kzalloc(sizeof(struct asic3), GFP_KERNEL);
863 if (asic == NULL) {
864 printk(KERN_ERR "kzalloc failed\n");
865 return -ENOMEM;
868 spin_lock_init(&asic->lock);
869 platform_set_drvdata(pdev, asic);
870 asic->dev = &pdev->dev;
872 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
873 if (!mem) {
874 ret = -ENOMEM;
875 dev_err(asic->dev, "no MEM resource\n");
876 goto out_free;
879 asic->mapping = ioremap(mem->start, resource_size(mem));
880 if (!asic->mapping) {
881 ret = -ENOMEM;
882 dev_err(asic->dev, "Couldn't ioremap\n");
883 goto out_free;
886 asic->irq_base = pdata->irq_base;
888 /* calculate bus shift from mem resource */
889 asic->bus_shift = 2 - (resource_size(mem) >> 12);
891 clksel = 0;
892 asic3_write_register(asic, ASIC3_OFFSET(CLOCK, SEL), clksel);
894 ret = asic3_irq_probe(pdev);
895 if (ret < 0) {
896 dev_err(asic->dev, "Couldn't probe IRQs\n");
897 goto out_unmap;
900 asic->gpio.base = pdata->gpio_base;
901 asic->gpio.ngpio = ASIC3_NUM_GPIOS;
902 asic->gpio.get = asic3_gpio_get;
903 asic->gpio.set = asic3_gpio_set;
904 asic->gpio.direction_input = asic3_gpio_direction_input;
905 asic->gpio.direction_output = asic3_gpio_direction_output;
907 ret = asic3_gpio_probe(pdev,
908 pdata->gpio_config,
909 pdata->gpio_config_num);
910 if (ret < 0) {
911 dev_err(asic->dev, "GPIO probe failed\n");
912 goto out_irq;
915 /* Making a per-device copy is only needed for the
916 * theoretical case of multiple ASIC3s on one board:
918 memcpy(asic->clocks, asic3_clk_init, sizeof(asic3_clk_init));
920 asic3_mfd_probe(pdev, mem);
922 dev_info(asic->dev, "ASIC3 Core driver\n");
924 return 0;
926 out_irq:
927 asic3_irq_remove(pdev);
929 out_unmap:
930 iounmap(asic->mapping);
932 out_free:
933 kfree(asic);
935 return ret;
938 static int __devexit asic3_remove(struct platform_device *pdev)
940 int ret;
941 struct asic3 *asic = platform_get_drvdata(pdev);
943 asic3_mfd_remove(pdev);
945 ret = asic3_gpio_remove(pdev);
946 if (ret < 0)
947 return ret;
948 asic3_irq_remove(pdev);
950 asic3_write_register(asic, ASIC3_OFFSET(CLOCK, SEL), 0);
952 iounmap(asic->mapping);
954 kfree(asic);
956 return 0;
959 static void asic3_shutdown(struct platform_device *pdev)
963 static struct platform_driver asic3_device_driver = {
964 .driver = {
965 .name = "asic3",
967 .remove = __devexit_p(asic3_remove),
968 .shutdown = asic3_shutdown,
971 static int __init asic3_init(void)
973 int retval = 0;
974 retval = platform_driver_probe(&asic3_device_driver, asic3_probe);
975 return retval;
978 subsys_initcall(asic3_init);