GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / arch / arm / mach-u300 / gpio.c
blobc042689f32facdcaa2fc100cd5148809f03d746b
1 /*
3 * arch/arm/mach-u300/gpio.c
6 * Copyright (C) 2007-2009 ST-Ericsson AB
7 * License terms: GNU General Public License (GPL) version 2
8 * U300 GPIO module.
9 * This can driver either of the two basic GPIO cores
10 * available in the U300 platforms:
11 * COH 901 335 - Used in DB3150 (U300 1.0) and DB3200 (U330 1.0)
12 * COH 901 571/3 - Used in DB3210 (U365 2.0) and DB3350 (U335 1.0)
13 * Notice that you also have inline macros in <asm-arch/gpio.h>
14 * Author: Linus Walleij <linus.walleij@stericsson.com>
15 * Author: Jonas Aaberg <jonas.aberg@stericsson.com>
18 #include <linux/module.h>
19 #include <linux/interrupt.h>
20 #include <linux/delay.h>
21 #include <linux/errno.h>
22 #include <linux/io.h>
23 #include <linux/clk.h>
24 #include <linux/err.h>
25 #include <linux/platform_device.h>
26 #include <linux/gpio.h>
28 /* Reference to GPIO block clock */
29 static struct clk *clk;
31 /* Memory resource */
32 static struct resource *memres;
33 static void __iomem *virtbase;
34 static struct device *gpiodev;
36 struct u300_gpio_port {
37 const char *name;
38 int irq;
39 int number;
43 static struct u300_gpio_port gpio_ports[] = {
45 .name = "gpio0",
46 .number = 0,
49 .name = "gpio1",
50 .number = 1,
53 .name = "gpio2",
54 .number = 2,
56 #ifdef U300_COH901571_3
58 .name = "gpio3",
59 .number = 3,
62 .name = "gpio4",
63 .number = 4,
65 #ifdef CONFIG_MACH_U300_BS335
67 .name = "gpio5",
68 .number = 5,
71 .name = "gpio6",
72 .number = 6,
74 #endif
75 #endif
80 #ifdef U300_COH901571_3
82 /* Default input value */
83 #define DEFAULT_OUTPUT_LOW 0
84 #define DEFAULT_OUTPUT_HIGH 1
86 /* GPIO Pull-Up status */
87 #define DISABLE_PULL_UP 0
88 #define ENABLE_PULL_UP 1
90 #define GPIO_NOT_USED 0
91 #define GPIO_IN 1
92 #define GPIO_OUT 2
94 struct u300_gpio_configuration_data {
95 unsigned char pin_usage;
96 unsigned char default_output_value;
97 unsigned char pull_up;
100 /* Initial configuration */
101 const struct u300_gpio_configuration_data
102 u300_gpio_config[U300_GPIO_NUM_PORTS][U300_GPIO_PINS_PER_PORT] = {
103 #ifdef CONFIG_MACH_U300_BS335
104 /* Port 0, pins 0-7 */
106 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
107 {GPIO_OUT, DEFAULT_OUTPUT_HIGH, DISABLE_PULL_UP},
108 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
109 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
110 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
111 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
112 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
113 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP}
115 /* Port 1, pins 0-7 */
117 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
118 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
119 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
120 {GPIO_IN, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP},
121 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
122 {GPIO_OUT, DEFAULT_OUTPUT_HIGH, DISABLE_PULL_UP},
123 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
124 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP}
126 /* Port 2, pins 0-7 */
128 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
129 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
130 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
131 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
132 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
133 {GPIO_IN, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP},
134 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
135 {GPIO_IN, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP}
137 /* Port 3, pins 0-7 */
139 {GPIO_IN, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP},
140 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
141 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
142 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
143 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
144 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
145 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
146 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP}
148 /* Port 4, pins 0-7 */
150 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
151 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
152 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
153 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
154 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
155 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
156 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
157 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP}
159 /* Port 5, pins 0-7 */
161 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
162 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
163 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
164 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
165 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
166 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
167 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
168 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP}
170 /* Port 6, pind 0-7 */
172 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
173 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
174 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
175 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
176 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
177 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
178 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
179 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP}
181 #endif
183 #ifdef CONFIG_MACH_U300_BS365
184 /* Port 0, pins 0-7 */
186 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
187 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
188 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
189 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
190 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
191 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
192 {GPIO_IN, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP},
193 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP}
195 /* Port 1, pins 0-7 */
197 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
198 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
199 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
200 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
201 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
202 {GPIO_OUT, DEFAULT_OUTPUT_HIGH, DISABLE_PULL_UP},
203 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
204 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP}
206 /* Port 2, pins 0-7 */
208 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
209 {GPIO_IN, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP},
210 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
211 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
212 {GPIO_IN, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP},
213 {GPIO_IN, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP},
214 {GPIO_IN, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP},
215 {GPIO_IN, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP}
217 /* Port 3, pins 0-7 */
219 {GPIO_IN, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP},
220 {GPIO_IN, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP},
221 {GPIO_IN, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP},
222 {GPIO_IN, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP},
223 {GPIO_IN, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP},
224 {GPIO_IN, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP},
225 {GPIO_IN, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP},
226 {GPIO_IN, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP}
228 /* Port 4, pins 0-7 */
230 {GPIO_IN, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP},
231 {GPIO_IN, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP},
232 {GPIO_IN, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP},
233 {GPIO_IN, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP},
234 /* These 4 pins doesn't exist on DB3210 */
235 {GPIO_OUT, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP},
236 {GPIO_OUT, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP},
237 {GPIO_OUT, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP},
238 {GPIO_OUT, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP}
240 #endif
242 #endif
245 /* No users == we can power down GPIO */
246 static int gpio_users;
248 struct gpio_struct {
249 int (*callback)(void *);
250 void *data;
251 int users;
254 static struct gpio_struct gpio_pin[U300_GPIO_MAX];
257 * Let drivers register callback in order to get notified when there is
258 * an interrupt on the gpio pin
260 int gpio_register_callback(unsigned gpio, int (*func)(void *arg), void *data)
262 if (gpio_pin[gpio].callback)
263 dev_warn(gpiodev, "%s: WARNING: callback already "
264 "registered for gpio pin#%d\n", __func__, gpio);
265 gpio_pin[gpio].callback = func;
266 gpio_pin[gpio].data = data;
268 return 0;
270 EXPORT_SYMBOL(gpio_register_callback);
272 int gpio_unregister_callback(unsigned gpio)
274 if (!gpio_pin[gpio].callback)
275 dev_warn(gpiodev, "%s: WARNING: callback already "
276 "unregistered for gpio pin#%d\n", __func__, gpio);
277 gpio_pin[gpio].callback = NULL;
278 gpio_pin[gpio].data = NULL;
280 return 0;
282 EXPORT_SYMBOL(gpio_unregister_callback);
284 /* Non-zero means valid */
285 int gpio_is_valid(int number)
287 if (number >= 0 &&
288 number < (U300_GPIO_NUM_PORTS * U300_GPIO_PINS_PER_PORT))
289 return 1;
290 return 0;
292 EXPORT_SYMBOL(gpio_is_valid);
294 int gpio_request(unsigned gpio, const char *label)
296 if (gpio_pin[gpio].users)
297 return -EINVAL;
298 else
299 gpio_pin[gpio].users++;
301 gpio_users++;
303 return 0;
305 EXPORT_SYMBOL(gpio_request);
307 void gpio_free(unsigned gpio)
309 gpio_users--;
310 gpio_pin[gpio].users--;
311 if (unlikely(gpio_pin[gpio].users < 0)) {
312 dev_warn(gpiodev, "warning: gpio#%d release mismatch\n",
313 gpio);
314 gpio_pin[gpio].users = 0;
317 return;
319 EXPORT_SYMBOL(gpio_free);
321 /* This returns zero or nonzero */
322 int gpio_get_value(unsigned gpio)
324 return readl(virtbase + U300_GPIO_PXPDIR +
325 PIN_TO_PORT(gpio) * U300_GPIO_PORTX_SPACING) & (1 << (gpio & 0x07));
327 EXPORT_SYMBOL(gpio_get_value);
330 * We hope that the compiler will optimize away the unused branch
331 * in case "value" is a constant
333 void gpio_set_value(unsigned gpio, int value)
335 u32 val;
336 unsigned long flags;
338 local_irq_save(flags);
339 if (value) {
340 /* set */
341 val = readl(virtbase + U300_GPIO_PXPDOR +
342 PIN_TO_PORT(gpio) * U300_GPIO_PORTX_SPACING)
343 & (1 << (gpio & 0x07));
344 writel(val | (1 << (gpio & 0x07)), virtbase +
345 U300_GPIO_PXPDOR +
346 PIN_TO_PORT(gpio) * U300_GPIO_PORTX_SPACING);
347 } else {
348 /* clear */
349 val = readl(virtbase + U300_GPIO_PXPDOR +
350 PIN_TO_PORT(gpio) * U300_GPIO_PORTX_SPACING)
351 & (1 << (gpio & 0x07));
352 writel(val & ~(1 << (gpio & 0x07)), virtbase +
353 U300_GPIO_PXPDOR +
354 PIN_TO_PORT(gpio) * U300_GPIO_PORTX_SPACING);
356 local_irq_restore(flags);
358 EXPORT_SYMBOL(gpio_set_value);
360 int gpio_direction_input(unsigned gpio)
362 unsigned long flags;
363 u32 val;
365 if (gpio > U300_GPIO_MAX)
366 return -EINVAL;
368 local_irq_save(flags);
369 val = readl(virtbase + U300_GPIO_PXPCR + PIN_TO_PORT(gpio) *
370 U300_GPIO_PORTX_SPACING);
371 /* Mask out this pin*/
372 val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK << ((gpio & 0x07) << 1));
373 /* This is not needed since it sets the bits to zero.*/
374 /* val |= (U300_GPIO_PXPCR_PIN_MODE_INPUT << (gpio*2)); */
375 writel(val, virtbase + U300_GPIO_PXPCR + PIN_TO_PORT(gpio) *
376 U300_GPIO_PORTX_SPACING);
377 local_irq_restore(flags);
378 return 0;
380 EXPORT_SYMBOL(gpio_direction_input);
382 int gpio_direction_output(unsigned gpio, int value)
384 unsigned long flags;
385 u32 val;
387 if (gpio > U300_GPIO_MAX)
388 return -EINVAL;
390 local_irq_save(flags);
391 val = readl(virtbase + U300_GPIO_PXPCR + PIN_TO_PORT(gpio) *
392 U300_GPIO_PORTX_SPACING);
393 /* Mask out this pin */
394 val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK << ((gpio & 0x07) << 1));
395 val |= (U300_GPIO_PXPCR_PIN_MODE_OUTPUT_PUSH_PULL
396 << ((gpio & 0x07) << 1));
397 writel(val, virtbase + U300_GPIO_PXPCR + PIN_TO_PORT(gpio) *
398 U300_GPIO_PORTX_SPACING);
399 gpio_set_value(gpio, value);
400 local_irq_restore(flags);
401 return 0;
403 EXPORT_SYMBOL(gpio_direction_output);
406 * Enable an IRQ, edge is rising edge (!= 0) or falling edge (==0).
408 void enable_irq_on_gpio_pin(unsigned gpio, int edge)
410 u32 val;
411 unsigned long flags;
412 local_irq_save(flags);
414 val = readl(virtbase + U300_GPIO_PXIEN + PIN_TO_PORT(gpio) *
415 U300_GPIO_PORTX_SPACING);
416 val |= (1 << (gpio & 0x07));
417 writel(val, virtbase + U300_GPIO_PXIEN + PIN_TO_PORT(gpio) *
418 U300_GPIO_PORTX_SPACING);
419 val = readl(virtbase + U300_GPIO_PXICR + PIN_TO_PORT(gpio) *
420 U300_GPIO_PORTX_SPACING);
421 if (edge)
422 val |= (1 << (gpio & 0x07));
423 else
424 val &= ~(1 << (gpio & 0x07));
425 writel(val, virtbase + U300_GPIO_PXICR + PIN_TO_PORT(gpio) *
426 U300_GPIO_PORTX_SPACING);
427 local_irq_restore(flags);
429 EXPORT_SYMBOL(enable_irq_on_gpio_pin);
431 void disable_irq_on_gpio_pin(unsigned gpio)
433 u32 val;
434 unsigned long flags;
436 local_irq_save(flags);
437 val = readl(virtbase + U300_GPIO_PXIEN + PIN_TO_PORT(gpio) *
438 U300_GPIO_PORTX_SPACING);
439 val &= ~(1 << (gpio & 0x07));
440 writel(val, virtbase + U300_GPIO_PXIEN + PIN_TO_PORT(gpio) *
441 U300_GPIO_PORTX_SPACING);
442 local_irq_restore(flags);
444 EXPORT_SYMBOL(disable_irq_on_gpio_pin);
446 /* Enable (value == 0) or disable (value == 1) internal pullup */
447 void gpio_pullup(unsigned gpio, int value)
449 u32 val;
450 unsigned long flags;
452 local_irq_save(flags);
453 if (value) {
454 val = readl(virtbase + U300_GPIO_PXPER + PIN_TO_PORT(gpio) *
455 U300_GPIO_PORTX_SPACING);
456 writel(val | (1 << (gpio & 0x07)), virtbase + U300_GPIO_PXPER +
457 PIN_TO_PORT(gpio) * U300_GPIO_PORTX_SPACING);
458 } else {
459 val = readl(virtbase + U300_GPIO_PXPER + PIN_TO_PORT(gpio) *
460 U300_GPIO_PORTX_SPACING);
461 writel(val & ~(1 << (gpio & 0x07)), virtbase + U300_GPIO_PXPER +
462 PIN_TO_PORT(gpio) * U300_GPIO_PORTX_SPACING);
464 local_irq_restore(flags);
466 EXPORT_SYMBOL(gpio_pullup);
468 static irqreturn_t gpio_irq_handler(int irq, void *dev_id)
470 struct u300_gpio_port *port = dev_id;
471 u32 val;
472 int pin;
474 /* Read event register */
475 val = readl(virtbase + U300_GPIO_PXIEV + port->number *
476 U300_GPIO_PORTX_SPACING);
477 /* Mask with enable register */
478 val &= readl(virtbase + U300_GPIO_PXIEV + port->number *
479 U300_GPIO_PORTX_SPACING);
480 /* Mask relevant bits */
481 val &= U300_GPIO_PXIEV_ALL_IRQ_EVENT_MASK;
482 /* ACK IRQ (clear event) */
483 writel(val, virtbase + U300_GPIO_PXIEV + port->number *
484 U300_GPIO_PORTX_SPACING);
485 /* Print message */
486 while (val != 0) {
487 unsigned gpio;
489 pin = __ffs(val);
490 /* mask off this pin */
491 val &= ~(1 << pin);
492 gpio = (port->number << 3) + pin;
494 if (gpio_pin[gpio].callback)
495 (void)gpio_pin[gpio].callback(gpio_pin[gpio].data);
496 else
497 dev_dbg(gpiodev, "stray GPIO IRQ on line %d\n",
498 gpio);
500 return IRQ_HANDLED;
503 static void gpio_set_initial_values(void)
505 #ifdef U300_COH901571_3
506 int i, j;
507 unsigned long flags;
508 u32 val;
510 /* Write default values to all pins */
511 for (i = 0; i < U300_GPIO_NUM_PORTS; i++) {
512 val = 0;
513 for (j = 0; j < 8; j++)
514 val |= (u32) (u300_gpio_config[i][j].default_output_value != DEFAULT_OUTPUT_LOW) << j;
515 local_irq_save(flags);
516 writel(val, virtbase + U300_GPIO_PXPDOR + i * U300_GPIO_PORTX_SPACING);
517 local_irq_restore(flags);
521 * Put all pins that are set to either 'GPIO_OUT' or 'GPIO_NOT_USED'
522 * to output and 'GPIO_IN' to input for each port. And initialize
523 * default value on outputs.
525 for (i = 0; i < U300_GPIO_NUM_PORTS; i++) {
526 for (j = 0; j < U300_GPIO_PINS_PER_PORT; j++) {
527 local_irq_save(flags);
528 val = readl(virtbase + U300_GPIO_PXPCR +
529 i * U300_GPIO_PORTX_SPACING);
530 /* Mask out this pin */
531 val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK << (j << 1));
533 if (u300_gpio_config[i][j].pin_usage != GPIO_IN)
534 val |= (U300_GPIO_PXPCR_PIN_MODE_OUTPUT_PUSH_PULL << (j << 1));
535 writel(val, virtbase + U300_GPIO_PXPCR +
536 i * U300_GPIO_PORTX_SPACING);
537 local_irq_restore(flags);
541 /* Enable or disable the internal pull-ups in the GPIO ASIC block */
542 for (i = 0; i < U300_GPIO_MAX; i++) {
543 val = 0;
544 for (j = 0; j < 8; j++)
545 val |= (u32)((u300_gpio_config[i][j].pull_up == DISABLE_PULL_UP) << j);
546 local_irq_save(flags);
547 writel(val, virtbase + U300_GPIO_PXPER + i * U300_GPIO_PORTX_SPACING);
548 local_irq_restore(flags);
550 #endif
553 static int __init gpio_probe(struct platform_device *pdev)
555 u32 val;
556 int err = 0;
557 int i;
558 int num_irqs;
560 gpiodev = &pdev->dev;
561 memset(gpio_pin, 0, sizeof(gpio_pin));
563 /* Get GPIO clock */
564 clk = clk_get(&pdev->dev, NULL);
565 if (IS_ERR(clk)) {
566 err = PTR_ERR(clk);
567 dev_err(gpiodev, "could not get GPIO clock\n");
568 goto err_no_clk;
570 err = clk_enable(clk);
571 if (err) {
572 dev_err(gpiodev, "could not enable GPIO clock\n");
573 goto err_no_clk_enable;
576 memres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
577 if (!memres)
578 goto err_no_resource;
580 if (request_mem_region(memres->start, memres->end - memres->start, "GPIO Controller")
581 == NULL) {
582 err = -ENODEV;
583 goto err_no_ioregion;
586 virtbase = ioremap(memres->start, resource_size(memres));
587 if (!virtbase) {
588 err = -ENOMEM;
589 goto err_no_ioremap;
591 dev_info(gpiodev, "remapped 0x%08x to %p\n",
592 memres->start, virtbase);
594 #ifdef U300_COH901335
595 dev_info(gpiodev, "initializing GPIO Controller COH 901 335\n");
596 /* Turn on the GPIO block */
597 writel(U300_GPIO_CR_BLOCK_CLOCK_ENABLE, virtbase + U300_GPIO_CR);
598 #endif
600 #ifdef U300_COH901571_3
601 dev_info(gpiodev, "initializing GPIO Controller COH 901 571/3\n");
602 val = readl(virtbase + U300_GPIO_CR);
603 dev_info(gpiodev, "COH901571/3 block version: %d, " \
604 "number of cores: %d\n",
605 ((val & 0x0000FE00) >> 9),
606 ((val & 0x000001FC) >> 2));
607 writel(U300_GPIO_CR_BLOCK_CLKRQ_ENABLE, virtbase + U300_GPIO_CR);
608 #endif
610 gpio_set_initial_values();
612 for (num_irqs = 0 ; num_irqs < U300_GPIO_NUM_PORTS; num_irqs++) {
614 gpio_ports[num_irqs].irq =
615 platform_get_irq_byname(pdev,
616 gpio_ports[num_irqs].name);
618 err = request_irq(gpio_ports[num_irqs].irq,
619 gpio_irq_handler, IRQF_DISABLED,
620 gpio_ports[num_irqs].name,
621 &gpio_ports[num_irqs]);
622 if (err) {
623 dev_err(gpiodev, "cannot allocate IRQ for %s!\n",
624 gpio_ports[num_irqs].name);
625 goto err_no_irq;
627 /* Turns off PortX_irq_force */
628 writel(0x0, virtbase + U300_GPIO_PXIFR +
629 num_irqs * U300_GPIO_PORTX_SPACING);
632 return 0;
634 err_no_irq:
635 for (i = 0; i < num_irqs; i++)
636 free_irq(gpio_ports[i].irq, &gpio_ports[i]);
637 iounmap(virtbase);
638 err_no_ioremap:
639 release_mem_region(memres->start, memres->end - memres->start);
640 err_no_ioregion:
641 err_no_resource:
642 clk_disable(clk);
643 err_no_clk_enable:
644 clk_put(clk);
645 err_no_clk:
646 dev_info(gpiodev, "module ERROR:%d\n", err);
647 return err;
650 static int __exit gpio_remove(struct platform_device *pdev)
652 int i;
654 /* Turn off the GPIO block */
655 writel(0x00000000U, virtbase + U300_GPIO_CR);
656 for (i = 0 ; i < U300_GPIO_NUM_PORTS; i++)
657 free_irq(gpio_ports[i].irq, &gpio_ports[i]);
658 iounmap(virtbase);
659 release_mem_region(memres->start, memres->end - memres->start);
660 clk_disable(clk);
661 clk_put(clk);
662 return 0;
665 static struct platform_driver gpio_driver = {
666 .driver = {
667 .name = "u300-gpio",
669 .remove = __exit_p(gpio_remove),
673 static int __init u300_gpio_init(void)
675 return platform_driver_probe(&gpio_driver, gpio_probe);
678 static void __exit u300_gpio_exit(void)
680 platform_driver_unregister(&gpio_driver);
683 arch_initcall(u300_gpio_init);
684 module_exit(u300_gpio_exit);
686 MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
688 #ifdef U300_COH901571_3
689 MODULE_DESCRIPTION("ST-Ericsson AB COH 901 571/3 GPIO driver");
690 #endif
692 #ifdef U300_COH901335
693 MODULE_DESCRIPTION("ST-Ericsson AB COH 901 335 GPIO driver");
694 #endif
696 MODULE_LICENSE("GPL");