blob: ca5305a5bd61d8021c50dbb452db5adc376b4a0b [file] [log] [blame]
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +10001/*
2 * MPC52xx gpio driver
3 *
4 * Copyright (c) 2008 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
5 *
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
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include <linux/of.h>
21#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +100023#include <linux/of_gpio.h>
24#include <linux/io.h>
25#include <linux/of_platform.h>
26
27#include <asm/gpio.h>
28#include <asm/mpc52xx.h>
29#include <sysdev/fsl_soc.h>
30
31static DEFINE_SPINLOCK(gpio_lock);
32
33struct mpc52xx_gpiochip {
34 struct of_mm_gpio_chip mmchip;
35 unsigned int shadow_dvo;
36 unsigned int shadow_gpioe;
37 unsigned int shadow_ddr;
38};
39
40/*
41 * GPIO LIB API implementation for wakeup GPIOs.
42 *
43 * There's a maximum of 8 wakeup GPIOs. Which of these are available
44 * for use depends on your board setup.
45 *
46 * 0 -> GPIO_WKUP_7
47 * 1 -> GPIO_WKUP_6
48 * 2 -> PSC6_1
49 * 3 -> PSC6_0
50 * 4 -> ETH_17
51 * 5 -> PSC3_9
52 * 6 -> PSC2_4
53 * 7 -> PSC1_4
54 *
55 */
56static int mpc52xx_wkup_gpio_get(struct gpio_chip *gc, unsigned int gpio)
57{
58 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
59 struct mpc52xx_gpio_wkup __iomem *regs = mm_gc->regs;
60 unsigned int ret;
61
62 ret = (in_8(&regs->wkup_ival) >> (7 - gpio)) & 1;
63
64 pr_debug("%s: gpio: %d ret: %d\n", __func__, gpio, ret);
65
66 return ret;
67}
68
69static inline void
70__mpc52xx_wkup_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
71{
72 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
73 struct mpc52xx_gpiochip *chip = container_of(mm_gc,
74 struct mpc52xx_gpiochip, mmchip);
75 struct mpc52xx_gpio_wkup __iomem *regs = mm_gc->regs;
76
77 if (val)
78 chip->shadow_dvo |= 1 << (7 - gpio);
79 else
80 chip->shadow_dvo &= ~(1 << (7 - gpio));
81
82 out_8(&regs->wkup_dvo, chip->shadow_dvo);
83}
84
85static void
86mpc52xx_wkup_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
87{
88 unsigned long flags;
89
90 spin_lock_irqsave(&gpio_lock, flags);
91
92 __mpc52xx_wkup_gpio_set(gc, gpio, val);
93
94 spin_unlock_irqrestore(&gpio_lock, flags);
95
96 pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
97}
98
99static int mpc52xx_wkup_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
100{
101 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
102 struct mpc52xx_gpiochip *chip = container_of(mm_gc,
103 struct mpc52xx_gpiochip, mmchip);
Al Viro93072452008-06-02 10:59:02 +0100104 struct mpc52xx_gpio_wkup __iomem *regs = mm_gc->regs;
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000105 unsigned long flags;
106
107 spin_lock_irqsave(&gpio_lock, flags);
108
109 /* set the direction */
110 chip->shadow_ddr &= ~(1 << (7 - gpio));
111 out_8(&regs->wkup_ddr, chip->shadow_ddr);
112
113 /* and enable the pin */
114 chip->shadow_gpioe |= 1 << (7 - gpio);
115 out_8(&regs->wkup_gpioe, chip->shadow_gpioe);
116
117 spin_unlock_irqrestore(&gpio_lock, flags);
118
119 return 0;
120}
121
122static int
123mpc52xx_wkup_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
124{
125 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
Al Viro93072452008-06-02 10:59:02 +0100126 struct mpc52xx_gpio_wkup __iomem *regs = mm_gc->regs;
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000127 struct mpc52xx_gpiochip *chip = container_of(mm_gc,
128 struct mpc52xx_gpiochip, mmchip);
129 unsigned long flags;
130
131 spin_lock_irqsave(&gpio_lock, flags);
132
133 __mpc52xx_wkup_gpio_set(gc, gpio, val);
134
135 /* Then set direction */
136 chip->shadow_ddr |= 1 << (7 - gpio);
137 out_8(&regs->wkup_ddr, chip->shadow_ddr);
138
139 /* Finally enable the pin */
140 chip->shadow_gpioe |= 1 << (7 - gpio);
141 out_8(&regs->wkup_gpioe, chip->shadow_gpioe);
142
143 spin_unlock_irqrestore(&gpio_lock, flags);
144
145 pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
146
147 return 0;
148}
149
150static int __devinit mpc52xx_wkup_gpiochip_probe(struct of_device *ofdev,
151 const struct of_device_id *match)
152{
153 struct mpc52xx_gpiochip *chip;
Al Viro93072452008-06-02 10:59:02 +0100154 struct mpc52xx_gpio_wkup __iomem *regs;
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000155 struct of_gpio_chip *ofchip;
156 int ret;
157
158 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
159 if (!chip)
160 return -ENOMEM;
161
162 ofchip = &chip->mmchip.of_gc;
163
164 ofchip->gpio_cells = 2;
165 ofchip->gc.ngpio = 8;
166 ofchip->gc.direction_input = mpc52xx_wkup_gpio_dir_in;
167 ofchip->gc.direction_output = mpc52xx_wkup_gpio_dir_out;
168 ofchip->gc.get = mpc52xx_wkup_gpio_get;
169 ofchip->gc.set = mpc52xx_wkup_gpio_set;
170
Grant Likely61c7a082010-04-13 16:12:29 -0700171 ret = of_mm_gpiochip_add(ofdev->dev.of_node, &chip->mmchip);
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000172 if (ret)
173 return ret;
174
175 regs = chip->mmchip.regs;
176 chip->shadow_gpioe = in_8(&regs->wkup_gpioe);
177 chip->shadow_ddr = in_8(&regs->wkup_ddr);
178 chip->shadow_dvo = in_8(&regs->wkup_dvo);
179
180 return 0;
181}
182
183static int mpc52xx_gpiochip_remove(struct of_device *ofdev)
184{
185 return -EBUSY;
186}
187
188static const struct of_device_id mpc52xx_wkup_gpiochip_match[] = {
189 {
190 .compatible = "fsl,mpc5200-gpio-wkup",
191 },
192 {}
193};
194
195static struct of_platform_driver mpc52xx_wkup_gpiochip_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700196 .driver = {
197 .name = "gpio_wkup",
198 .owner = THIS_MODULE,
199 .of_match_table = mpc52xx_wkup_gpiochip_match,
200 },
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000201 .probe = mpc52xx_wkup_gpiochip_probe,
202 .remove = mpc52xx_gpiochip_remove,
203};
204
205/*
206 * GPIO LIB API implementation for simple GPIOs
207 *
208 * There's a maximum of 32 simple GPIOs. Which of these are available
209 * for use depends on your board setup.
210 * The numbering reflects the bit numbering in the port registers:
211 *
212 * 0..1 > reserved
213 * 2..3 > IRDA
214 * 4..7 > ETHR
215 * 8..11 > reserved
216 * 12..15 > USB
217 * 16..17 > reserved
218 * 18..23 > PSC3
219 * 24..27 > PSC2
220 * 28..31 > PSC1
221 */
222static int mpc52xx_simple_gpio_get(struct gpio_chip *gc, unsigned int gpio)
223{
224 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
225 struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
226 unsigned int ret;
227
228 ret = (in_be32(&regs->simple_ival) >> (31 - gpio)) & 1;
229
230 return ret;
231}
232
233static inline void
234__mpc52xx_simple_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
235{
236 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
237 struct mpc52xx_gpiochip *chip = container_of(mm_gc,
238 struct mpc52xx_gpiochip, mmchip);
239 struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
240
241 if (val)
242 chip->shadow_dvo |= 1 << (31 - gpio);
243 else
244 chip->shadow_dvo &= ~(1 << (31 - gpio));
245 out_be32(&regs->simple_dvo, chip->shadow_dvo);
246}
247
248static void
249mpc52xx_simple_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
250{
251 unsigned long flags;
252
253 spin_lock_irqsave(&gpio_lock, flags);
254
255 __mpc52xx_simple_gpio_set(gc, gpio, val);
256
257 spin_unlock_irqrestore(&gpio_lock, flags);
258
259 pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
260}
261
262static int mpc52xx_simple_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
263{
264 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
265 struct mpc52xx_gpiochip *chip = container_of(mm_gc,
266 struct mpc52xx_gpiochip, mmchip);
Al Viro93072452008-06-02 10:59:02 +0100267 struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000268 unsigned long flags;
269
270 spin_lock_irqsave(&gpio_lock, flags);
271
272 /* set the direction */
273 chip->shadow_ddr &= ~(1 << (31 - gpio));
274 out_be32(&regs->simple_ddr, chip->shadow_ddr);
275
276 /* and enable the pin */
277 chip->shadow_gpioe |= 1 << (31 - gpio);
278 out_be32(&regs->simple_gpioe, chip->shadow_gpioe);
279
280 spin_unlock_irqrestore(&gpio_lock, flags);
281
282 return 0;
283}
284
285static int
286mpc52xx_simple_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
287{
288 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
289 struct mpc52xx_gpiochip *chip = container_of(mm_gc,
290 struct mpc52xx_gpiochip, mmchip);
Al Viro93072452008-06-02 10:59:02 +0100291 struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000292 unsigned long flags;
293
294 spin_lock_irqsave(&gpio_lock, flags);
295
296 /* First set initial value */
297 __mpc52xx_simple_gpio_set(gc, gpio, val);
298
299 /* Then set direction */
300 chip->shadow_ddr |= 1 << (31 - gpio);
301 out_be32(&regs->simple_ddr, chip->shadow_ddr);
302
303 /* Finally enable the pin */
304 chip->shadow_gpioe |= 1 << (31 - gpio);
305 out_be32(&regs->simple_gpioe, chip->shadow_gpioe);
306
307 spin_unlock_irqrestore(&gpio_lock, flags);
308
309 pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
310
311 return 0;
312}
313
314static int __devinit mpc52xx_simple_gpiochip_probe(struct of_device *ofdev,
315 const struct of_device_id *match)
316{
317 struct mpc52xx_gpiochip *chip;
318 struct of_gpio_chip *ofchip;
Al Viro93072452008-06-02 10:59:02 +0100319 struct mpc52xx_gpio __iomem *regs;
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000320 int ret;
321
322 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
323 if (!chip)
324 return -ENOMEM;
325
326 ofchip = &chip->mmchip.of_gc;
327
328 ofchip->gpio_cells = 2;
329 ofchip->gc.ngpio = 32;
330 ofchip->gc.direction_input = mpc52xx_simple_gpio_dir_in;
331 ofchip->gc.direction_output = mpc52xx_simple_gpio_dir_out;
332 ofchip->gc.get = mpc52xx_simple_gpio_get;
333 ofchip->gc.set = mpc52xx_simple_gpio_set;
334
Grant Likely61c7a082010-04-13 16:12:29 -0700335 ret = of_mm_gpiochip_add(ofdev->dev.of_node, &chip->mmchip);
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000336 if (ret)
337 return ret;
338
339 regs = chip->mmchip.regs;
340 chip->shadow_gpioe = in_be32(&regs->simple_gpioe);
341 chip->shadow_ddr = in_be32(&regs->simple_ddr);
342 chip->shadow_dvo = in_be32(&regs->simple_dvo);
343
344 return 0;
345}
346
347static const struct of_device_id mpc52xx_simple_gpiochip_match[] = {
348 {
349 .compatible = "fsl,mpc5200-gpio",
350 },
351 {}
352};
353
354static struct of_platform_driver mpc52xx_simple_gpiochip_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700355 .driver = {
356 .name = "gpio",
357 .owner = THIS_MODULE,
358 .of_match_table = mpc52xx_simple_gpiochip_match,
359 },
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000360 .probe = mpc52xx_simple_gpiochip_probe,
361 .remove = mpc52xx_gpiochip_remove,
362};
363
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000364static int __init mpc52xx_gpio_init(void)
365{
366 if (of_register_platform_driver(&mpc52xx_wkup_gpiochip_driver))
367 printk(KERN_ERR "Unable to register wakeup GPIO driver\n");
368
369 if (of_register_platform_driver(&mpc52xx_simple_gpiochip_driver))
370 printk(KERN_ERR "Unable to register simple GPIO driver\n");
371
s.hauer@pengutronix.de3cd2550c2008-04-25 20:56:04 +1000372 return 0;
373}
374
375
376/* Make sure we get initialised before anyone else tries to use us */
377subsys_initcall(mpc52xx_gpio_init);
378
379/* No exit call at the moment as we cannot unregister of gpio chips */
380
381MODULE_DESCRIPTION("Freescale MPC52xx gpio driver");
382MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de");
383MODULE_LICENSE("GPL v2");
384