Merge branch 'master' of /home/davem/src/GIT/linux-2.6/
[sfrench/cifs-2.6.git] / arch / powerpc / sysdev / mpc8xxx_gpio.c
1 /*
2  * GPIOs on MPC8349/8572/8610 and compatible
3  *
4  * Copyright (C) 2008 Peter Korsgaard <jacmet@sunsite.dk>
5  *
6  * This file is licensed under the terms of the GNU General Public License
7  * version 2.  This program is licensed "as is" without any warranty of any
8  * kind, whether express or implied.
9  */
10
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/spinlock.h>
14 #include <linux/io.h>
15 #include <linux/of.h>
16 #include <linux/of_gpio.h>
17 #include <linux/gpio.h>
18
19 #define MPC8XXX_GPIO_PINS       32
20
21 #define GPIO_DIR                0x00
22 #define GPIO_ODR                0x04
23 #define GPIO_DAT                0x08
24 #define GPIO_IER                0x0c
25 #define GPIO_IMR                0x10
26 #define GPIO_ICR                0x14
27
28 struct mpc8xxx_gpio_chip {
29         struct of_mm_gpio_chip mm_gc;
30         spinlock_t lock;
31
32         /*
33          * shadowed data register to be able to clear/set output pins in
34          * open drain mode safely
35          */
36         u32 data;
37 };
38
39 static inline u32 mpc8xxx_gpio2mask(unsigned int gpio)
40 {
41         return 1u << (MPC8XXX_GPIO_PINS - 1 - gpio);
42 }
43
44 static inline struct mpc8xxx_gpio_chip *
45 to_mpc8xxx_gpio_chip(struct of_mm_gpio_chip *mm)
46 {
47         return container_of(mm, struct mpc8xxx_gpio_chip, mm_gc);
48 }
49
50 static void mpc8xxx_gpio_save_regs(struct of_mm_gpio_chip *mm)
51 {
52         struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
53
54         mpc8xxx_gc->data = in_be32(mm->regs + GPIO_DAT);
55 }
56
57 /* Workaround GPIO 1 errata on MPC8572/MPC8536. The status of GPIOs
58  * defined as output cannot be determined by reading GPDAT register,
59  * so we use shadow data register instead. The status of input pins
60  * is determined by reading GPDAT register.
61  */
62 static int mpc8572_gpio_get(struct gpio_chip *gc, unsigned int gpio)
63 {
64         u32 val;
65         struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
66         struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
67
68         val = in_be32(mm->regs + GPIO_DAT) & ~in_be32(mm->regs + GPIO_DIR);
69
70         return (val | mpc8xxx_gc->data) & mpc8xxx_gpio2mask(gpio);
71 }
72
73 static int mpc8xxx_gpio_get(struct gpio_chip *gc, unsigned int gpio)
74 {
75         struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
76
77         return in_be32(mm->regs + GPIO_DAT) & mpc8xxx_gpio2mask(gpio);
78 }
79
80 static void mpc8xxx_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
81 {
82         struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
83         struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
84         unsigned long flags;
85
86         spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
87
88         if (val)
89                 mpc8xxx_gc->data |= mpc8xxx_gpio2mask(gpio);
90         else
91                 mpc8xxx_gc->data &= ~mpc8xxx_gpio2mask(gpio);
92
93         out_be32(mm->regs + GPIO_DAT, mpc8xxx_gc->data);
94
95         spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
96 }
97
98 static int mpc8xxx_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
99 {
100         struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
101         struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
102         unsigned long flags;
103
104         spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
105
106         clrbits32(mm->regs + GPIO_DIR, mpc8xxx_gpio2mask(gpio));
107
108         spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
109
110         return 0;
111 }
112
113 static int mpc8xxx_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
114 {
115         struct of_mm_gpio_chip *mm = to_of_mm_gpio_chip(gc);
116         struct mpc8xxx_gpio_chip *mpc8xxx_gc = to_mpc8xxx_gpio_chip(mm);
117         unsigned long flags;
118
119         mpc8xxx_gpio_set(gc, gpio, val);
120
121         spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
122
123         setbits32(mm->regs + GPIO_DIR, mpc8xxx_gpio2mask(gpio));
124
125         spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
126
127         return 0;
128 }
129
130 static void __init mpc8xxx_add_controller(struct device_node *np)
131 {
132         struct mpc8xxx_gpio_chip *mpc8xxx_gc;
133         struct of_mm_gpio_chip *mm_gc;
134         struct of_gpio_chip *of_gc;
135         struct gpio_chip *gc;
136         int ret;
137
138         mpc8xxx_gc = kzalloc(sizeof(*mpc8xxx_gc), GFP_KERNEL);
139         if (!mpc8xxx_gc) {
140                 ret = -ENOMEM;
141                 goto err;
142         }
143
144         spin_lock_init(&mpc8xxx_gc->lock);
145
146         mm_gc = &mpc8xxx_gc->mm_gc;
147         of_gc = &mm_gc->of_gc;
148         gc = &of_gc->gc;
149
150         mm_gc->save_regs = mpc8xxx_gpio_save_regs;
151         of_gc->gpio_cells = 2;
152         gc->ngpio = MPC8XXX_GPIO_PINS;
153         gc->direction_input = mpc8xxx_gpio_dir_in;
154         gc->direction_output = mpc8xxx_gpio_dir_out;
155         if (of_device_is_compatible(np, "fsl,mpc8572-gpio"))
156                 gc->get = mpc8572_gpio_get;
157         else
158                 gc->get = mpc8xxx_gpio_get;
159         gc->set = mpc8xxx_gpio_set;
160
161         ret = of_mm_gpiochip_add(np, mm_gc);
162         if (ret)
163                 goto err;
164
165         return;
166
167 err:
168         pr_err("%s: registration failed with status %d\n",
169                np->full_name, ret);
170         kfree(mpc8xxx_gc);
171
172         return;
173 }
174
175 static int __init mpc8xxx_add_gpiochips(void)
176 {
177         struct device_node *np;
178
179         for_each_compatible_node(np, NULL, "fsl,mpc8349-gpio")
180                 mpc8xxx_add_controller(np);
181
182         for_each_compatible_node(np, NULL, "fsl,mpc8572-gpio")
183                 mpc8xxx_add_controller(np);
184
185         for_each_compatible_node(np, NULL, "fsl,mpc8610-gpio")
186                 mpc8xxx_add_controller(np);
187
188         return 0;
189 }
190 arch_initcall(mpc8xxx_add_gpiochips);