include: replace linux/module.h with "struct module" wherever possible
[linux-2.6/next.git] / drivers / gpio / gpio-u300.c
blob53e8255cb0bac91dd5562a0c15336257229d015a
1 /*
2 * U300 GPIO module.
4 * Copyright (C) 2007-2009 ST-Ericsson AB
5 * License terms: GNU General Public License (GPL) version 2
6 * This can driver either of the two basic GPIO cores
7 * available in the U300 platforms:
8 * COH 901 335 - Used in DB3150 (U300 1.0) and DB3200 (U330 1.0)
9 * COH 901 571/3 - Used in DB3210 (U365 2.0) and DB3350 (U335 1.0)
10 * Notice that you also have inline macros in <asm-arch/gpio.h>
11 * Author: Linus Walleij <linus.walleij@stericsson.com>
12 * Author: Jonas Aaberg <jonas.aberg@stericsson.com>
15 #include <linux/module.h>
16 #include <linux/interrupt.h>
17 #include <linux/delay.h>
18 #include <linux/errno.h>
19 #include <linux/io.h>
20 #include <linux/clk.h>
21 #include <linux/err.h>
22 #include <linux/platform_device.h>
23 #include <linux/gpio.h>
25 /* Reference to GPIO block clock */
26 static struct clk *clk;
28 /* Memory resource */
29 static struct resource *memres;
30 static void __iomem *virtbase;
31 static struct device *gpiodev;
33 struct u300_gpio_port {
34 const char *name;
35 int irq;
36 int number;
40 static struct u300_gpio_port gpio_ports[] = {
42 .name = "gpio0",
43 .number = 0,
46 .name = "gpio1",
47 .number = 1,
50 .name = "gpio2",
51 .number = 2,
53 #ifdef U300_COH901571_3
55 .name = "gpio3",
56 .number = 3,
59 .name = "gpio4",
60 .number = 4,
62 #ifdef CONFIG_MACH_U300_BS335
64 .name = "gpio5",
65 .number = 5,
68 .name = "gpio6",
69 .number = 6,
71 #endif
72 #endif
77 #ifdef U300_COH901571_3
79 /* Default input value */
80 #define DEFAULT_OUTPUT_LOW 0
81 #define DEFAULT_OUTPUT_HIGH 1
83 /* GPIO Pull-Up status */
84 #define DISABLE_PULL_UP 0
85 #define ENABLE_PULL_UP 1
87 #define GPIO_NOT_USED 0
88 #define GPIO_IN 1
89 #define GPIO_OUT 2
91 struct u300_gpio_configuration_data {
92 unsigned char pin_usage;
93 unsigned char default_output_value;
94 unsigned char pull_up;
97 /* Initial configuration */
98 const struct u300_gpio_configuration_data
99 u300_gpio_config[U300_GPIO_NUM_PORTS][U300_GPIO_PINS_PER_PORT] = {
100 #ifdef CONFIG_MACH_U300_BS335
101 /* Port 0, pins 0-7 */
103 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
104 {GPIO_OUT, DEFAULT_OUTPUT_HIGH, DISABLE_PULL_UP},
105 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
106 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
107 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
108 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
109 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
110 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP}
112 /* Port 1, pins 0-7 */
114 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
115 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
116 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
117 {GPIO_IN, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP},
118 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
119 {GPIO_OUT, DEFAULT_OUTPUT_HIGH, DISABLE_PULL_UP},
120 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
121 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP}
123 /* Port 2, pins 0-7 */
125 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
126 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
127 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
128 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
129 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
130 {GPIO_IN, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP},
131 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
132 {GPIO_IN, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP}
134 /* Port 3, pins 0-7 */
136 {GPIO_IN, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP},
137 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
138 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
139 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
140 {GPIO_IN, 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}
145 /* Port 4, pins 0-7 */
147 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
148 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
149 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
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}
156 /* Port 5, pins 0-7 */
158 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
159 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
160 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
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}
167 /* Port 6, pind 0-7 */
169 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
170 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
171 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
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}
178 #endif
180 #ifdef CONFIG_MACH_U300_BS365
181 /* Port 0, pins 0-7 */
183 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
184 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
185 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
186 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
187 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
188 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
189 {GPIO_IN, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP},
190 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP}
192 /* Port 1, pins 0-7 */
194 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
195 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
196 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
197 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
198 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
199 {GPIO_OUT, DEFAULT_OUTPUT_HIGH, DISABLE_PULL_UP},
200 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
201 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP}
203 /* Port 2, pins 0-7 */
205 {GPIO_IN, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
206 {GPIO_IN, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP},
207 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
208 {GPIO_OUT, DEFAULT_OUTPUT_LOW, DISABLE_PULL_UP},
209 {GPIO_IN, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP},
210 {GPIO_IN, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP},
211 {GPIO_IN, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP},
212 {GPIO_IN, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP}
214 /* Port 3, pins 0-7 */
216 {GPIO_IN, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP},
217 {GPIO_IN, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP},
218 {GPIO_IN, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP},
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}
225 /* Port 4, pins 0-7 */
227 {GPIO_IN, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP},
228 {GPIO_IN, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP},
229 {GPIO_IN, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP},
230 {GPIO_IN, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP},
231 /* These 4 pins doesn't exist on DB3210 */
232 {GPIO_OUT, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP},
233 {GPIO_OUT, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP},
234 {GPIO_OUT, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP},
235 {GPIO_OUT, DEFAULT_OUTPUT_LOW, ENABLE_PULL_UP}
237 #endif
239 #endif
242 /* No users == we can power down GPIO */
243 static int gpio_users;
245 struct gpio_struct {
246 int (*callback)(void *);
247 void *data;
248 int users;
251 static struct gpio_struct gpio_pin[U300_GPIO_MAX];
254 * Let drivers register callback in order to get notified when there is
255 * an interrupt on the gpio pin
257 int gpio_register_callback(unsigned gpio, int (*func)(void *arg), void *data)
259 if (gpio_pin[gpio].callback)
260 dev_warn(gpiodev, "%s: WARNING: callback already "
261 "registered for gpio pin#%d\n", __func__, gpio);
262 gpio_pin[gpio].callback = func;
263 gpio_pin[gpio].data = data;
265 return 0;
267 EXPORT_SYMBOL(gpio_register_callback);
269 int gpio_unregister_callback(unsigned gpio)
271 if (!gpio_pin[gpio].callback)
272 dev_warn(gpiodev, "%s: WARNING: callback already "
273 "unregistered for gpio pin#%d\n", __func__, gpio);
274 gpio_pin[gpio].callback = NULL;
275 gpio_pin[gpio].data = NULL;
277 return 0;
279 EXPORT_SYMBOL(gpio_unregister_callback);
281 /* Non-zero means valid */
282 int gpio_is_valid(int number)
284 if (number >= 0 &&
285 number < (U300_GPIO_NUM_PORTS * U300_GPIO_PINS_PER_PORT))
286 return 1;
287 return 0;
289 EXPORT_SYMBOL(gpio_is_valid);
291 int gpio_request(unsigned gpio, const char *label)
293 if (gpio_pin[gpio].users)
294 return -EINVAL;
295 else
296 gpio_pin[gpio].users++;
298 gpio_users++;
300 return 0;
302 EXPORT_SYMBOL(gpio_request);
304 void gpio_free(unsigned gpio)
306 gpio_users--;
307 gpio_pin[gpio].users--;
308 if (unlikely(gpio_pin[gpio].users < 0)) {
309 dev_warn(gpiodev, "warning: gpio#%d release mismatch\n",
310 gpio);
311 gpio_pin[gpio].users = 0;
314 return;
316 EXPORT_SYMBOL(gpio_free);
318 /* This returns zero or nonzero */
319 int gpio_get_value(unsigned gpio)
321 return readl(virtbase + U300_GPIO_PXPDIR +
322 PIN_TO_PORT(gpio) * U300_GPIO_PORTX_SPACING) & (1 << (gpio & 0x07));
324 EXPORT_SYMBOL(gpio_get_value);
327 * We hope that the compiler will optimize away the unused branch
328 * in case "value" is a constant
330 void gpio_set_value(unsigned gpio, int value)
332 u32 val;
333 unsigned long flags;
335 local_irq_save(flags);
336 if (value) {
337 /* set */
338 val = readl(virtbase + U300_GPIO_PXPDOR +
339 PIN_TO_PORT(gpio) * U300_GPIO_PORTX_SPACING)
340 & (1 << (gpio & 0x07));
341 writel(val | (1 << (gpio & 0x07)), virtbase +
342 U300_GPIO_PXPDOR +
343 PIN_TO_PORT(gpio) * U300_GPIO_PORTX_SPACING);
344 } else {
345 /* clear */
346 val = readl(virtbase + U300_GPIO_PXPDOR +
347 PIN_TO_PORT(gpio) * U300_GPIO_PORTX_SPACING)
348 & (1 << (gpio & 0x07));
349 writel(val & ~(1 << (gpio & 0x07)), virtbase +
350 U300_GPIO_PXPDOR +
351 PIN_TO_PORT(gpio) * U300_GPIO_PORTX_SPACING);
353 local_irq_restore(flags);
355 EXPORT_SYMBOL(gpio_set_value);
357 int gpio_direction_input(unsigned gpio)
359 unsigned long flags;
360 u32 val;
362 if (gpio > U300_GPIO_MAX)
363 return -EINVAL;
365 local_irq_save(flags);
366 val = readl(virtbase + U300_GPIO_PXPCR + PIN_TO_PORT(gpio) *
367 U300_GPIO_PORTX_SPACING);
368 /* Mask out this pin*/
369 val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK << ((gpio & 0x07) << 1));
370 /* This is not needed since it sets the bits to zero.*/
371 /* val |= (U300_GPIO_PXPCR_PIN_MODE_INPUT << (gpio*2)); */
372 writel(val, virtbase + U300_GPIO_PXPCR + PIN_TO_PORT(gpio) *
373 U300_GPIO_PORTX_SPACING);
374 local_irq_restore(flags);
375 return 0;
377 EXPORT_SYMBOL(gpio_direction_input);
379 int gpio_direction_output(unsigned gpio, int value)
381 unsigned long flags;
382 u32 val;
384 if (gpio > U300_GPIO_MAX)
385 return -EINVAL;
387 local_irq_save(flags);
388 val = readl(virtbase + U300_GPIO_PXPCR + PIN_TO_PORT(gpio) *
389 U300_GPIO_PORTX_SPACING);
390 /* Mask out this pin */
391 val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK << ((gpio & 0x07) << 1));
393 * FIXME: configure for push/pull, open drain or open source per pin
394 * in setup. The current driver will only support push/pull.
396 val |= (U300_GPIO_PXPCR_PIN_MODE_OUTPUT_PUSH_PULL
397 << ((gpio & 0x07) << 1));
398 writel(val, virtbase + U300_GPIO_PXPCR + PIN_TO_PORT(gpio) *
399 U300_GPIO_PORTX_SPACING);
400 gpio_set_value(gpio, value);
401 local_irq_restore(flags);
402 return 0;
404 EXPORT_SYMBOL(gpio_direction_output);
407 * Enable an IRQ, edge is rising edge (!= 0) or falling edge (==0).
409 void enable_irq_on_gpio_pin(unsigned gpio, int edge)
411 u32 val;
412 unsigned long flags;
413 local_irq_save(flags);
415 val = readl(virtbase + U300_GPIO_PXIEN + PIN_TO_PORT(gpio) *
416 U300_GPIO_PORTX_SPACING);
417 val |= (1 << (gpio & 0x07));
418 writel(val, virtbase + U300_GPIO_PXIEN + PIN_TO_PORT(gpio) *
419 U300_GPIO_PORTX_SPACING);
420 val = readl(virtbase + U300_GPIO_PXICR + PIN_TO_PORT(gpio) *
421 U300_GPIO_PORTX_SPACING);
422 if (edge)
423 val |= (1 << (gpio & 0x07));
424 else
425 val &= ~(1 << (gpio & 0x07));
426 writel(val, virtbase + U300_GPIO_PXICR + PIN_TO_PORT(gpio) *
427 U300_GPIO_PORTX_SPACING);
428 local_irq_restore(flags);
430 EXPORT_SYMBOL(enable_irq_on_gpio_pin);
432 void disable_irq_on_gpio_pin(unsigned gpio)
434 u32 val;
435 unsigned long flags;
437 local_irq_save(flags);
438 val = readl(virtbase + U300_GPIO_PXIEN + PIN_TO_PORT(gpio) *
439 U300_GPIO_PORTX_SPACING);
440 val &= ~(1 << (gpio & 0x07));
441 writel(val, virtbase + U300_GPIO_PXIEN + PIN_TO_PORT(gpio) *
442 U300_GPIO_PORTX_SPACING);
443 local_irq_restore(flags);
445 EXPORT_SYMBOL(disable_irq_on_gpio_pin);
447 /* Enable (value == 0) or disable (value == 1) internal pullup */
448 void gpio_pullup(unsigned gpio, int value)
450 u32 val;
451 unsigned long flags;
453 local_irq_save(flags);
454 if (value) {
455 val = readl(virtbase + U300_GPIO_PXPER + PIN_TO_PORT(gpio) *
456 U300_GPIO_PORTX_SPACING);
457 writel(val | (1 << (gpio & 0x07)), virtbase + U300_GPIO_PXPER +
458 PIN_TO_PORT(gpio) * U300_GPIO_PORTX_SPACING);
459 } else {
460 val = readl(virtbase + U300_GPIO_PXPER + PIN_TO_PORT(gpio) *
461 U300_GPIO_PORTX_SPACING);
462 writel(val & ~(1 << (gpio & 0x07)), virtbase + U300_GPIO_PXPER +
463 PIN_TO_PORT(gpio) * U300_GPIO_PORTX_SPACING);
465 local_irq_restore(flags);
467 EXPORT_SYMBOL(gpio_pullup);
469 static irqreturn_t gpio_irq_handler(int irq, void *dev_id)
471 struct u300_gpio_port *port = dev_id;
472 u32 val;
473 int pin;
475 /* Read event register */
476 val = readl(virtbase + U300_GPIO_PXIEV + port->number *
477 U300_GPIO_PORTX_SPACING);
478 /* Mask with enable register */
479 val &= readl(virtbase + U300_GPIO_PXIEV + port->number *
480 U300_GPIO_PORTX_SPACING);
481 /* Mask relevant bits */
482 val &= U300_GPIO_PXIEV_ALL_IRQ_EVENT_MASK;
483 /* ACK IRQ (clear event) */
484 writel(val, virtbase + U300_GPIO_PXIEV + port->number *
485 U300_GPIO_PORTX_SPACING);
486 /* Print message */
487 while (val != 0) {
488 unsigned gpio;
490 pin = __ffs(val);
491 /* mask off this pin */
492 val &= ~(1 << pin);
493 gpio = (port->number << 3) + pin;
495 if (gpio_pin[gpio].callback)
496 (void)gpio_pin[gpio].callback(gpio_pin[gpio].data);
497 else
498 dev_dbg(gpiodev, "stray GPIO IRQ on line %d\n",
499 gpio);
501 return IRQ_HANDLED;
504 static void gpio_set_initial_values(void)
506 #ifdef U300_COH901571_3
507 int i, j;
508 unsigned long flags;
509 u32 val;
511 /* Write default values to all pins */
512 for (i = 0; i < U300_GPIO_NUM_PORTS; i++) {
513 val = 0;
514 for (j = 0; j < 8; j++)
515 val |= (u32) (u300_gpio_config[i][j].default_output_value != DEFAULT_OUTPUT_LOW) << j;
516 local_irq_save(flags);
517 writel(val, virtbase + U300_GPIO_PXPDOR + i * U300_GPIO_PORTX_SPACING);
518 local_irq_restore(flags);
522 * Put all pins that are set to either 'GPIO_OUT' or 'GPIO_NOT_USED'
523 * to output and 'GPIO_IN' to input for each port. And initialize
524 * default value on outputs.
526 for (i = 0; i < U300_GPIO_NUM_PORTS; i++) {
527 for (j = 0; j < U300_GPIO_PINS_PER_PORT; j++) {
528 local_irq_save(flags);
529 val = readl(virtbase + U300_GPIO_PXPCR +
530 i * U300_GPIO_PORTX_SPACING);
531 /* Mask out this pin */
532 val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK << (j << 1));
534 if (u300_gpio_config[i][j].pin_usage != GPIO_IN)
535 val |= (U300_GPIO_PXPCR_PIN_MODE_OUTPUT_PUSH_PULL << (j << 1));
536 writel(val, virtbase + U300_GPIO_PXPCR +
537 i * U300_GPIO_PORTX_SPACING);
538 local_irq_restore(flags);
542 /* Enable or disable the internal pull-ups in the GPIO ASIC block */
543 for (i = 0; i < U300_GPIO_MAX; i++) {
544 val = 0;
545 for (j = 0; j < 8; j++)
546 val |= (u32)((u300_gpio_config[i][j].pull_up == DISABLE_PULL_UP) << j);
547 local_irq_save(flags);
548 writel(val, virtbase + U300_GPIO_PXPER + i * U300_GPIO_PORTX_SPACING);
549 local_irq_restore(flags);
551 #endif
554 static int __init gpio_probe(struct platform_device *pdev)
556 u32 val;
557 int err = 0;
558 int i;
559 int num_irqs;
561 gpiodev = &pdev->dev;
562 memset(gpio_pin, 0, sizeof(gpio_pin));
564 /* Get GPIO clock */
565 clk = clk_get(&pdev->dev, NULL);
566 if (IS_ERR(clk)) {
567 err = PTR_ERR(clk);
568 dev_err(gpiodev, "could not get GPIO clock\n");
569 goto err_no_clk;
571 err = clk_enable(clk);
572 if (err) {
573 dev_err(gpiodev, "could not enable GPIO clock\n");
574 goto err_no_clk_enable;
577 memres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
578 if (!memres)
579 goto err_no_resource;
581 if (!request_mem_region(memres->start, resource_size(memres),
582 "GPIO Controller")) {
583 err = -ENODEV;
584 goto err_no_ioregion;
587 virtbase = ioremap(memres->start, resource_size(memres));
588 if (!virtbase) {
589 err = -ENOMEM;
590 goto err_no_ioremap;
592 dev_info(gpiodev, "remapped 0x%08x to %p\n",
593 memres->start, virtbase);
595 #ifdef U300_COH901335
596 dev_info(gpiodev, "initializing GPIO Controller COH 901 335\n");
597 /* Turn on the GPIO block */
598 writel(U300_GPIO_CR_BLOCK_CLOCK_ENABLE, virtbase + U300_GPIO_CR);
599 #endif
601 #ifdef U300_COH901571_3
602 dev_info(gpiodev, "initializing GPIO Controller COH 901 571/3\n");
603 val = readl(virtbase + U300_GPIO_CR);
604 dev_info(gpiodev, "COH901571/3 block version: %d, " \
605 "number of cores: %d\n",
606 ((val & 0x0000FE00) >> 9),
607 ((val & 0x000001FC) >> 2));
608 writel(U300_GPIO_CR_BLOCK_CLKRQ_ENABLE, virtbase + U300_GPIO_CR);
609 #endif
611 gpio_set_initial_values();
613 for (num_irqs = 0 ; num_irqs < U300_GPIO_NUM_PORTS; num_irqs++) {
615 gpio_ports[num_irqs].irq =
616 platform_get_irq_byname(pdev,
617 gpio_ports[num_irqs].name);
619 err = request_irq(gpio_ports[num_irqs].irq,
620 gpio_irq_handler, IRQF_DISABLED,
621 gpio_ports[num_irqs].name,
622 &gpio_ports[num_irqs]);
623 if (err) {
624 dev_err(gpiodev, "cannot allocate IRQ for %s!\n",
625 gpio_ports[num_irqs].name);
626 goto err_no_irq;
628 /* Turns off PortX_irq_force */
629 writel(0x0, virtbase + U300_GPIO_PXIFR +
630 num_irqs * U300_GPIO_PORTX_SPACING);
633 return 0;
635 err_no_irq:
636 for (i = 0; i < num_irqs; i++)
637 free_irq(gpio_ports[i].irq, &gpio_ports[i]);
638 iounmap(virtbase);
639 err_no_ioremap:
640 release_mem_region(memres->start, resource_size(memres));
641 err_no_ioregion:
642 err_no_resource:
643 clk_disable(clk);
644 err_no_clk_enable:
645 clk_put(clk);
646 err_no_clk:
647 dev_info(gpiodev, "module ERROR:%d\n", err);
648 return err;
651 static int __exit gpio_remove(struct platform_device *pdev)
653 int i;
655 /* Turn off the GPIO block */
656 writel(0x00000000U, virtbase + U300_GPIO_CR);
657 for (i = 0 ; i < U300_GPIO_NUM_PORTS; i++)
658 free_irq(gpio_ports[i].irq, &gpio_ports[i]);
659 iounmap(virtbase);
660 release_mem_region(memres->start, resource_size(memres));
661 clk_disable(clk);
662 clk_put(clk);
663 return 0;
666 static struct platform_driver gpio_driver = {
667 .driver = {
668 .name = "u300-gpio",
670 .remove = __exit_p(gpio_remove),
674 static int __init u300_gpio_init(void)
676 return platform_driver_probe(&gpio_driver, gpio_probe);
679 static void __exit u300_gpio_exit(void)
681 platform_driver_unregister(&gpio_driver);
684 arch_initcall(u300_gpio_init);
685 module_exit(u300_gpio_exit);
687 MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
689 #ifdef U300_COH901571_3
690 MODULE_DESCRIPTION("ST-Ericsson AB COH 901 571/3 GPIO driver");
691 #endif
693 #ifdef U300_COH901335
694 MODULE_DESCRIPTION("ST-Ericsson AB COH 901 335 GPIO driver");
695 #endif
697 MODULE_LICENSE("GPL");