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/slab.h>
23 #include <linux/of_gpio.h>
25 #include <linux/of_platform.h>
28 #include <asm/mpc52xx.h>
29 #include <sysdev/fsl_soc.h>
31 static DEFINE_SPINLOCK(gpio_lock);
33 struct mpc52xx_gpiochip {
34 struct of_mm_gpio_chip mmchip;
35 unsigned int shadow_dvo;
36 unsigned int shadow_gpioe;
37 unsigned int shadow_ddr;
41 * GPIO LIB API implementation for wakeup GPIOs.
43 * There's a maximum of 8 wakeup GPIOs. Which of these are available
44 * for use depends on your board setup.
56 static int mpc52xx_wkup_gpio_get(struct gpio_chip *gc, unsigned int gpio)
58 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
59 struct mpc52xx_gpio_wkup __iomem *regs = mm_gc->regs;
62 ret = (in_8(®s->wkup_ival) >> (7 - gpio)) & 1;
64 pr_debug("%s: gpio: %d ret: %d\n", __func__, gpio, ret);
70 __mpc52xx_wkup_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
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;
78 chip->shadow_dvo |= 1 << (7 - gpio);
80 chip->shadow_dvo &= ~(1 << (7 - gpio));
82 out_8(®s->wkup_dvo, chip->shadow_dvo);
86 mpc52xx_wkup_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
90 spin_lock_irqsave(&gpio_lock, flags);
92 __mpc52xx_wkup_gpio_set(gc, gpio, val);
94 spin_unlock_irqrestore(&gpio_lock, flags);
96 pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
99 static int mpc52xx_wkup_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
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);
104 struct mpc52xx_gpio_wkup __iomem *regs = mm_gc->regs;
107 spin_lock_irqsave(&gpio_lock, flags);
109 /* set the direction */
110 chip->shadow_ddr &= ~(1 << (7 - gpio));
111 out_8(®s->wkup_ddr, chip->shadow_ddr);
113 /* and enable the pin */
114 chip->shadow_gpioe |= 1 << (7 - gpio);
115 out_8(®s->wkup_gpioe, chip->shadow_gpioe);
117 spin_unlock_irqrestore(&gpio_lock, flags);
123 mpc52xx_wkup_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
125 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
126 struct mpc52xx_gpio_wkup __iomem *regs = mm_gc->regs;
127 struct mpc52xx_gpiochip *chip = container_of(mm_gc,
128 struct mpc52xx_gpiochip, mmchip);
131 spin_lock_irqsave(&gpio_lock, flags);
133 __mpc52xx_wkup_gpio_set(gc, gpio, val);
135 /* Then set direction */
136 chip->shadow_ddr |= 1 << (7 - gpio);
137 out_8(®s->wkup_ddr, chip->shadow_ddr);
139 /* Finally enable the pin */
140 chip->shadow_gpioe |= 1 << (7 - gpio);
141 out_8(®s->wkup_gpioe, chip->shadow_gpioe);
143 spin_unlock_irqrestore(&gpio_lock, flags);
145 pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
150 static int __devinit mpc52xx_wkup_gpiochip_probe(struct of_device *ofdev,
151 const struct of_device_id *match)
153 struct mpc52xx_gpiochip *chip;
154 struct mpc52xx_gpio_wkup __iomem *regs;
155 struct of_gpio_chip *ofchip;
158 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
162 ofchip = &chip->mmchip.of_gc;
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;
171 ret = of_mm_gpiochip_add(ofdev->node, &chip->mmchip);
175 regs = chip->mmchip.regs;
176 chip->shadow_gpioe = in_8(®s->wkup_gpioe);
177 chip->shadow_ddr = in_8(®s->wkup_ddr);
178 chip->shadow_dvo = in_8(®s->wkup_dvo);
183 static int mpc52xx_gpiochip_remove(struct of_device *ofdev)
188 static const struct of_device_id mpc52xx_wkup_gpiochip_match[] = {
190 .compatible = "fsl,mpc5200-gpio-wkup",
195 static struct of_platform_driver mpc52xx_wkup_gpiochip_driver = {
197 .match_table = mpc52xx_wkup_gpiochip_match,
198 .probe = mpc52xx_wkup_gpiochip_probe,
199 .remove = mpc52xx_gpiochip_remove,
203 * GPIO LIB API implementation for simple GPIOs
205 * There's a maximum of 32 simple GPIOs. Which of these are available
206 * for use depends on your board setup.
207 * The numbering reflects the bit numbering in the port registers:
219 static int mpc52xx_simple_gpio_get(struct gpio_chip *gc, unsigned int gpio)
221 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
222 struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
225 ret = (in_be32(®s->simple_ival) >> (31 - gpio)) & 1;
231 __mpc52xx_simple_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
233 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
234 struct mpc52xx_gpiochip *chip = container_of(mm_gc,
235 struct mpc52xx_gpiochip, mmchip);
236 struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
239 chip->shadow_dvo |= 1 << (31 - gpio);
241 chip->shadow_dvo &= ~(1 << (31 - gpio));
242 out_be32(®s->simple_dvo, chip->shadow_dvo);
246 mpc52xx_simple_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
250 spin_lock_irqsave(&gpio_lock, flags);
252 __mpc52xx_simple_gpio_set(gc, gpio, val);
254 spin_unlock_irqrestore(&gpio_lock, flags);
256 pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
259 static int mpc52xx_simple_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
261 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
262 struct mpc52xx_gpiochip *chip = container_of(mm_gc,
263 struct mpc52xx_gpiochip, mmchip);
264 struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
267 spin_lock_irqsave(&gpio_lock, flags);
269 /* set the direction */
270 chip->shadow_ddr &= ~(1 << (31 - gpio));
271 out_be32(®s->simple_ddr, chip->shadow_ddr);
273 /* and enable the pin */
274 chip->shadow_gpioe |= 1 << (31 - gpio);
275 out_be32(®s->simple_gpioe, chip->shadow_gpioe);
277 spin_unlock_irqrestore(&gpio_lock, flags);
283 mpc52xx_simple_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
285 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
286 struct mpc52xx_gpiochip *chip = container_of(mm_gc,
287 struct mpc52xx_gpiochip, mmchip);
288 struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
291 spin_lock_irqsave(&gpio_lock, flags);
293 /* First set initial value */
294 __mpc52xx_simple_gpio_set(gc, gpio, val);
296 /* Then set direction */
297 chip->shadow_ddr |= 1 << (31 - gpio);
298 out_be32(®s->simple_ddr, chip->shadow_ddr);
300 /* Finally enable the pin */
301 chip->shadow_gpioe |= 1 << (31 - gpio);
302 out_be32(®s->simple_gpioe, chip->shadow_gpioe);
304 spin_unlock_irqrestore(&gpio_lock, flags);
306 pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
311 static int __devinit mpc52xx_simple_gpiochip_probe(struct of_device *ofdev,
312 const struct of_device_id *match)
314 struct mpc52xx_gpiochip *chip;
315 struct of_gpio_chip *ofchip;
316 struct mpc52xx_gpio __iomem *regs;
319 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
323 ofchip = &chip->mmchip.of_gc;
325 ofchip->gpio_cells = 2;
326 ofchip->gc.ngpio = 32;
327 ofchip->gc.direction_input = mpc52xx_simple_gpio_dir_in;
328 ofchip->gc.direction_output = mpc52xx_simple_gpio_dir_out;
329 ofchip->gc.get = mpc52xx_simple_gpio_get;
330 ofchip->gc.set = mpc52xx_simple_gpio_set;
332 ret = of_mm_gpiochip_add(ofdev->node, &chip->mmchip);
336 regs = chip->mmchip.regs;
337 chip->shadow_gpioe = in_be32(®s->simple_gpioe);
338 chip->shadow_ddr = in_be32(®s->simple_ddr);
339 chip->shadow_dvo = in_be32(®s->simple_dvo);
344 static const struct of_device_id mpc52xx_simple_gpiochip_match[] = {
346 .compatible = "fsl,mpc5200-gpio",
351 static struct of_platform_driver mpc52xx_simple_gpiochip_driver = {
353 .match_table = mpc52xx_simple_gpiochip_match,
354 .probe = mpc52xx_simple_gpiochip_probe,
355 .remove = mpc52xx_gpiochip_remove,
358 static int __init mpc52xx_gpio_init(void)
360 if (of_register_platform_driver(&mpc52xx_wkup_gpiochip_driver))
361 printk(KERN_ERR "Unable to register wakeup GPIO driver\n");
363 if (of_register_platform_driver(&mpc52xx_simple_gpiochip_driver))
364 printk(KERN_ERR "Unable to register simple GPIO driver\n");
370 /* Make sure we get initialised before anyone else tries to use us */
371 subsys_initcall(mpc52xx_gpio_init);
373 /* No exit call at the moment as we cannot unregister of gpio chips */
375 MODULE_DESCRIPTION("Freescale MPC52xx gpio driver");
376 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de");
377 MODULE_LICENSE("GPL v2");