]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/gpio/gpio-iop.c
ARM: plat-iop: instantiate GPIO from platform device
[karo-tx-linux.git] / drivers / gpio / gpio-iop.c
1 /*
2  * arch/arm/plat-iop/gpio.c
3  * GPIO handling for Intel IOP3xx processors.
4  *
5  * Copyright (C) 2006 Lennert Buytenhek <buytenh@wantstofly.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or (at
10  * your option) any later version.
11  */
12
13 #include <linux/device.h>
14 #include <linux/init.h>
15 #include <linux/types.h>
16 #include <linux/errno.h>
17 #include <linux/gpio.h>
18 #include <linux/export.h>
19 #include <linux/platform_device.h>
20
21 #define IOP3XX_N_GPIOS  8
22
23 #define GPIO_IN                 0
24 #define GPIO_OUT                1
25 #define GPIO_LOW                0
26 #define GPIO_HIGH               1
27
28 /* Memory base offset */
29 static void __iomem *base;
30
31 #define IOP3XX_GPIO_REG(reg)    (base + (reg))
32 #define IOP3XX_GPOE             (volatile u32 *)IOP3XX_GPIO_REG(0x0000)
33 #define IOP3XX_GPID             (volatile u32 *)IOP3XX_GPIO_REG(0x0004)
34 #define IOP3XX_GPOD             (volatile u32 *)IOP3XX_GPIO_REG(0x0008)
35
36 static void gpio_line_config(int line, int direction)
37 {
38         unsigned long flags;
39
40         local_irq_save(flags);
41         if (direction == GPIO_IN) {
42                 *IOP3XX_GPOE |= 1 << line;
43         } else if (direction == GPIO_OUT) {
44                 *IOP3XX_GPOE &= ~(1 << line);
45         }
46         local_irq_restore(flags);
47 }
48
49 static int gpio_line_get(int line)
50 {
51         return !!(*IOP3XX_GPID & (1 << line));
52 }
53
54 static void gpio_line_set(int line, int value)
55 {
56         unsigned long flags;
57
58         local_irq_save(flags);
59         if (value == GPIO_LOW) {
60                 *IOP3XX_GPOD &= ~(1 << line);
61         } else if (value == GPIO_HIGH) {
62                 *IOP3XX_GPOD |= 1 << line;
63         }
64         local_irq_restore(flags);
65 }
66
67 static int iop3xx_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
68 {
69         gpio_line_config(gpio, GPIO_IN);
70         return 0;
71 }
72
73 static int iop3xx_gpio_direction_output(struct gpio_chip *chip, unsigned gpio, int level)
74 {
75         gpio_line_set(gpio, level);
76         gpio_line_config(gpio, GPIO_OUT);
77         return 0;
78 }
79
80 static int iop3xx_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
81 {
82         return gpio_line_get(gpio);
83 }
84
85 static void iop3xx_gpio_set_value(struct gpio_chip *chip, unsigned gpio, int value)
86 {
87         gpio_line_set(gpio, value);
88 }
89
90 static struct gpio_chip iop3xx_chip = {
91         .label                  = "iop3xx",
92         .direction_input        = iop3xx_gpio_direction_input,
93         .get                    = iop3xx_gpio_get_value,
94         .direction_output       = iop3xx_gpio_direction_output,
95         .set                    = iop3xx_gpio_set_value,
96         .base                   = 0,
97         .ngpio                  = IOP3XX_N_GPIOS,
98 };
99
100 static int iop3xx_gpio_probe(struct platform_device *pdev)
101 {
102         struct resource *res;
103
104         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
105         base = (void *) res->start;
106
107         return gpiochip_add(&iop3xx_chip);
108 }
109
110 static struct platform_driver iop3xx_gpio_driver = {
111         .driver = {
112                 .name = "gpio-iop",
113                 .owner = THIS_MODULE,
114         },
115         .probe = iop3xx_gpio_probe,
116 };
117
118 static int __init iop3xx_gpio_init(void)
119 {
120         return platform_driver_register(&iop3xx_gpio_driver);
121 }
122 arch_initcall(iop3xx_gpio_init);