4 * Copyright (c) 2008 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
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.
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.
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
21 #include <linux/kernel.h>
22 #include <linux/of_gpio.h>
24 #include <linux/of_platform.h>
27 #include <asm/mpc52xx.h>
28 #include <sysdev/fsl_soc.h>
30 static DEFINE_SPINLOCK(gpio_lock);
32 struct mpc52xx_gpiochip {
33 struct of_mm_gpio_chip mmchip;
34 unsigned int shadow_dvo;
35 unsigned int shadow_gpioe;
36 unsigned int shadow_ddr;
40 * GPIO LIB API implementation for wakeup GPIOs.
42 * There's a maximum of 8 wakeup GPIOs. Which of these are available
43 * for use depends on your board setup.
55 static int mpc52xx_wkup_gpio_get(struct gpio_chip *gc, unsigned int gpio)
57 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
58 struct mpc52xx_gpio_wkup __iomem *regs = mm_gc->regs;
61 ret = (in_8(®s->wkup_ival) >> (7 - gpio)) & 1;
63 pr_debug("%s: gpio: %d ret: %d\n", __func__, gpio, ret);
69 __mpc52xx_wkup_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
71 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
72 struct mpc52xx_gpiochip *chip = container_of(mm_gc,
73 struct mpc52xx_gpiochip, mmchip);
74 struct mpc52xx_gpio_wkup __iomem *regs = mm_gc->regs;
77 chip->shadow_dvo |= 1 << (7 - gpio);
79 chip->shadow_dvo &= ~(1 << (7 - gpio));
81 out_8(®s->wkup_dvo, chip->shadow_dvo);
85 mpc52xx_wkup_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
89 spin_lock_irqsave(&gpio_lock, flags);
91 __mpc52xx_wkup_gpio_set(gc, gpio, val);
93 spin_unlock_irqrestore(&gpio_lock, flags);
95 pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
98 static int mpc52xx_wkup_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
100 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
101 struct mpc52xx_gpiochip *chip = container_of(mm_gc,
102 struct mpc52xx_gpiochip, mmchip);
103 struct mpc52xx_gpio_wkup *regs = mm_gc->regs;
106 spin_lock_irqsave(&gpio_lock, flags);
108 /* set the direction */
109 chip->shadow_ddr &= ~(1 << (7 - gpio));
110 out_8(®s->wkup_ddr, chip->shadow_ddr);
112 /* and enable the pin */
113 chip->shadow_gpioe |= 1 << (7 - gpio);
114 out_8(®s->wkup_gpioe, chip->shadow_gpioe);
116 spin_unlock_irqrestore(&gpio_lock, flags);
122 mpc52xx_wkup_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
124 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
125 struct mpc52xx_gpio_wkup *regs = mm_gc->regs;
126 struct mpc52xx_gpiochip *chip = container_of(mm_gc,
127 struct mpc52xx_gpiochip, mmchip);
130 spin_lock_irqsave(&gpio_lock, flags);
132 __mpc52xx_wkup_gpio_set(gc, gpio, val);
134 /* Then set direction */
135 chip->shadow_ddr |= 1 << (7 - gpio);
136 out_8(®s->wkup_ddr, chip->shadow_ddr);
138 /* Finally enable the pin */
139 chip->shadow_gpioe |= 1 << (7 - gpio);
140 out_8(®s->wkup_gpioe, chip->shadow_gpioe);
142 spin_unlock_irqrestore(&gpio_lock, flags);
144 pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
149 static int __devinit mpc52xx_wkup_gpiochip_probe(struct of_device *ofdev,
150 const struct of_device_id *match)
152 struct mpc52xx_gpiochip *chip;
153 struct mpc52xx_gpio_wkup *regs;
154 struct of_gpio_chip *ofchip;
157 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
161 ofchip = &chip->mmchip.of_gc;
163 ofchip->gpio_cells = 2;
164 ofchip->gc.ngpio = 8;
165 ofchip->gc.direction_input = mpc52xx_wkup_gpio_dir_in;
166 ofchip->gc.direction_output = mpc52xx_wkup_gpio_dir_out;
167 ofchip->gc.get = mpc52xx_wkup_gpio_get;
168 ofchip->gc.set = mpc52xx_wkup_gpio_set;
170 ret = of_mm_gpiochip_add(ofdev->node, &chip->mmchip);
174 regs = chip->mmchip.regs;
175 chip->shadow_gpioe = in_8(®s->wkup_gpioe);
176 chip->shadow_ddr = in_8(®s->wkup_ddr);
177 chip->shadow_dvo = in_8(®s->wkup_dvo);
182 static int mpc52xx_gpiochip_remove(struct of_device *ofdev)
187 static const struct of_device_id mpc52xx_wkup_gpiochip_match[] = {
189 .compatible = "fsl,mpc5200-gpio-wkup",
194 static struct of_platform_driver mpc52xx_wkup_gpiochip_driver = {
196 .match_table = mpc52xx_wkup_gpiochip_match,
197 .probe = mpc52xx_wkup_gpiochip_probe,
198 .remove = mpc52xx_gpiochip_remove,
202 * GPIO LIB API implementation for simple GPIOs
204 * There's a maximum of 32 simple GPIOs. Which of these are available
205 * for use depends on your board setup.
206 * The numbering reflects the bit numbering in the port registers:
218 static int mpc52xx_simple_gpio_get(struct gpio_chip *gc, unsigned int gpio)
220 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
221 struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
224 ret = (in_be32(®s->simple_ival) >> (31 - gpio)) & 1;
230 __mpc52xx_simple_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
232 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
233 struct mpc52xx_gpiochip *chip = container_of(mm_gc,
234 struct mpc52xx_gpiochip, mmchip);
235 struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
238 chip->shadow_dvo |= 1 << (31 - gpio);
240 chip->shadow_dvo &= ~(1 << (31 - gpio));
241 out_be32(®s->simple_dvo, chip->shadow_dvo);
245 mpc52xx_simple_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
249 spin_lock_irqsave(&gpio_lock, flags);
251 __mpc52xx_simple_gpio_set(gc, gpio, val);
253 spin_unlock_irqrestore(&gpio_lock, flags);
255 pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
258 static int mpc52xx_simple_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
260 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
261 struct mpc52xx_gpiochip *chip = container_of(mm_gc,
262 struct mpc52xx_gpiochip, mmchip);
263 struct mpc52xx_gpio *regs = mm_gc->regs;
266 spin_lock_irqsave(&gpio_lock, flags);
268 /* set the direction */
269 chip->shadow_ddr &= ~(1 << (31 - gpio));
270 out_be32(®s->simple_ddr, chip->shadow_ddr);
272 /* and enable the pin */
273 chip->shadow_gpioe |= 1 << (31 - gpio);
274 out_be32(®s->simple_gpioe, chip->shadow_gpioe);
276 spin_unlock_irqrestore(&gpio_lock, flags);
282 mpc52xx_simple_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
284 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
285 struct mpc52xx_gpiochip *chip = container_of(mm_gc,
286 struct mpc52xx_gpiochip, mmchip);
287 struct mpc52xx_gpio *regs = mm_gc->regs;
290 spin_lock_irqsave(&gpio_lock, flags);
292 /* First set initial value */
293 __mpc52xx_simple_gpio_set(gc, gpio, val);
295 /* Then set direction */
296 chip->shadow_ddr |= 1 << (31 - gpio);
297 out_be32(®s->simple_ddr, chip->shadow_ddr);
299 /* Finally enable the pin */
300 chip->shadow_gpioe |= 1 << (31 - gpio);
301 out_be32(®s->simple_gpioe, chip->shadow_gpioe);
303 spin_unlock_irqrestore(&gpio_lock, flags);
305 pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
310 static int __devinit mpc52xx_simple_gpiochip_probe(struct of_device *ofdev,
311 const struct of_device_id *match)
313 struct mpc52xx_gpiochip *chip;
314 struct of_gpio_chip *ofchip;
315 struct mpc52xx_gpio *regs;
318 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
322 ofchip = &chip->mmchip.of_gc;
324 ofchip->gpio_cells = 2;
325 ofchip->gc.ngpio = 32;
326 ofchip->gc.direction_input = mpc52xx_simple_gpio_dir_in;
327 ofchip->gc.direction_output = mpc52xx_simple_gpio_dir_out;
328 ofchip->gc.get = mpc52xx_simple_gpio_get;
329 ofchip->gc.set = mpc52xx_simple_gpio_set;
331 ret = of_mm_gpiochip_add(ofdev->node, &chip->mmchip);
335 regs = chip->mmchip.regs;
336 chip->shadow_gpioe = in_be32(®s->simple_gpioe);
337 chip->shadow_ddr = in_be32(®s->simple_ddr);
338 chip->shadow_dvo = in_be32(®s->simple_dvo);
343 static const struct of_device_id mpc52xx_simple_gpiochip_match[] = {
345 .compatible = "fsl,mpc5200-gpio",
350 static struct of_platform_driver mpc52xx_simple_gpiochip_driver = {
352 .match_table = mpc52xx_simple_gpiochip_match,
353 .probe = mpc52xx_simple_gpiochip_probe,
354 .remove = mpc52xx_gpiochip_remove,
358 * GPIO LIB API implementation for gpt GPIOs.
360 * Each gpt only has a single GPIO.
362 static int mpc52xx_gpt_gpio_get(struct gpio_chip *gc, unsigned int gpio)
364 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
365 struct mpc52xx_gpt __iomem *regs = mm_gc->regs;
368 return (in_be32(®s->status) & (1 << (31 - 23))) ? 1 : 0;
374 mpc52xx_gpt_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
376 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
377 struct mpc52xx_gpt __iomem *regs = mm_gc->regs;
380 out_be32(®s->mode, 0x34);
382 out_be32(®s->mode, 0x24);
384 pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
387 static int mpc52xx_gpt_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
389 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
390 struct mpc52xx_gpt *regs = mm_gc->regs;
392 out_be32(®s->mode, 0x04);
398 mpc52xx_gpt_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
400 mpc52xx_gpt_gpio_set(gc, gpio, val);
401 pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
406 static int __devinit mpc52xx_gpt_gpiochip_probe(struct of_device *ofdev,
407 const struct of_device_id *match)
409 struct of_mm_gpio_chip *mmchip;
410 struct of_gpio_chip *chip;
412 mmchip = kzalloc(sizeof(*mmchip), GFP_KERNEL);
416 chip = &mmchip->of_gc;
418 chip->gpio_cells = 2;
420 chip->gc.direction_input = mpc52xx_gpt_gpio_dir_in;
421 chip->gc.direction_output = mpc52xx_gpt_gpio_dir_out;
422 chip->gc.get = mpc52xx_gpt_gpio_get;
423 chip->gc.set = mpc52xx_gpt_gpio_set;
425 return of_mm_gpiochip_add(ofdev->node, mmchip);
428 static const struct of_device_id mpc52xx_gpt_gpiochip_match[] = {
430 .compatible = "fsl,mpc5200-gpt-gpio",
435 static struct of_platform_driver mpc52xx_gpt_gpiochip_driver = {
437 .match_table = mpc52xx_gpt_gpiochip_match,
438 .probe = mpc52xx_gpt_gpiochip_probe,
439 .remove = mpc52xx_gpiochip_remove,
442 static int __init mpc52xx_gpio_init(void)
444 if (of_register_platform_driver(&mpc52xx_wkup_gpiochip_driver))
445 printk(KERN_ERR "Unable to register wakeup GPIO driver\n");
447 if (of_register_platform_driver(&mpc52xx_simple_gpiochip_driver))
448 printk(KERN_ERR "Unable to register simple GPIO driver\n");
450 if (of_register_platform_driver(&mpc52xx_gpt_gpiochip_driver))
451 printk(KERN_ERR "Unable to register gpt GPIO driver\n");
457 /* Make sure we get initialised before anyone else tries to use us */
458 subsys_initcall(mpc52xx_gpio_init);
460 /* No exit call at the moment as we cannot unregister of gpio chips */
462 MODULE_DESCRIPTION("Freescale MPC52xx gpio driver");
463 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de");
464 MODULE_LICENSE("GPL v2");