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>
26 #include <linux/module.h>
29 #include <asm/mpc52xx.h>
30 #include <sysdev/fsl_soc.h>
32 static DEFINE_SPINLOCK(gpio_lock);
34 struct mpc52xx_gpiochip {
35 struct of_mm_gpio_chip mmchip;
36 unsigned int shadow_dvo;
37 unsigned int shadow_gpioe;
38 unsigned int shadow_ddr;
42 * GPIO LIB API implementation for wakeup GPIOs.
44 * There's a maximum of 8 wakeup GPIOs. Which of these are available
45 * for use depends on your board setup.
57 static int mpc52xx_wkup_gpio_get(struct gpio_chip *gc, unsigned int gpio)
59 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
60 struct mpc52xx_gpio_wkup __iomem *regs = mm_gc->regs;
63 ret = (in_8(®s->wkup_ival) >> (7 - gpio)) & 1;
65 pr_debug("%s: gpio: %d ret: %d\n", __func__, gpio, ret);
71 __mpc52xx_wkup_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
73 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
74 struct mpc52xx_gpiochip *chip = gpiochip_get_data(gc);
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 = gpiochip_get_data(gc);
103 struct mpc52xx_gpio_wkup __iomem *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 __iomem *regs = mm_gc->regs;
126 struct mpc52xx_gpiochip *chip = gpiochip_get_data(gc);
129 spin_lock_irqsave(&gpio_lock, flags);
131 __mpc52xx_wkup_gpio_set(gc, gpio, val);
133 /* Then set direction */
134 chip->shadow_ddr |= 1 << (7 - gpio);
135 out_8(®s->wkup_ddr, chip->shadow_ddr);
137 /* Finally enable the pin */
138 chip->shadow_gpioe |= 1 << (7 - gpio);
139 out_8(®s->wkup_gpioe, chip->shadow_gpioe);
141 spin_unlock_irqrestore(&gpio_lock, flags);
143 pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
148 static int mpc52xx_wkup_gpiochip_probe(struct platform_device *ofdev)
150 struct mpc52xx_gpiochip *chip;
151 struct mpc52xx_gpio_wkup __iomem *regs;
152 struct gpio_chip *gc;
155 chip = devm_kzalloc(&ofdev->dev, sizeof(*chip), GFP_KERNEL);
159 platform_set_drvdata(ofdev, chip);
161 gc = &chip->mmchip.gc;
164 gc->direction_input = mpc52xx_wkup_gpio_dir_in;
165 gc->direction_output = mpc52xx_wkup_gpio_dir_out;
166 gc->get = mpc52xx_wkup_gpio_get;
167 gc->set = mpc52xx_wkup_gpio_set;
169 ret = of_mm_gpiochip_add_data(ofdev->dev.of_node, &chip->mmchip, chip);
173 regs = chip->mmchip.regs;
174 chip->shadow_gpioe = in_8(®s->wkup_gpioe);
175 chip->shadow_ddr = in_8(®s->wkup_ddr);
176 chip->shadow_dvo = in_8(®s->wkup_dvo);
181 static int mpc52xx_gpiochip_remove(struct platform_device *ofdev)
183 struct mpc52xx_gpiochip *chip = platform_get_drvdata(ofdev);
185 of_mm_gpiochip_remove(&chip->mmchip);
190 static const struct of_device_id mpc52xx_wkup_gpiochip_match[] = {
191 { .compatible = "fsl,mpc5200-gpio-wkup", },
195 static struct platform_driver mpc52xx_wkup_gpiochip_driver = {
197 .name = "mpc5200-gpio-wkup",
198 .of_match_table = mpc52xx_wkup_gpiochip_match,
200 .probe = mpc52xx_wkup_gpiochip_probe,
201 .remove = mpc52xx_gpiochip_remove,
205 * GPIO LIB API implementation for simple GPIOs
207 * There's a maximum of 32 simple GPIOs. Which of these are available
208 * for use depends on your board setup.
209 * The numbering reflects the bit numbering in the port registers:
221 static int mpc52xx_simple_gpio_get(struct gpio_chip *gc, unsigned int gpio)
223 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
224 struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
227 ret = (in_be32(®s->simple_ival) >> (31 - gpio)) & 1;
233 __mpc52xx_simple_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
235 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
236 struct mpc52xx_gpiochip *chip = gpiochip_get_data(gc);
237 struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
240 chip->shadow_dvo |= 1 << (31 - gpio);
242 chip->shadow_dvo &= ~(1 << (31 - gpio));
243 out_be32(®s->simple_dvo, chip->shadow_dvo);
247 mpc52xx_simple_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
251 spin_lock_irqsave(&gpio_lock, flags);
253 __mpc52xx_simple_gpio_set(gc, gpio, val);
255 spin_unlock_irqrestore(&gpio_lock, flags);
257 pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
260 static int mpc52xx_simple_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
262 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
263 struct mpc52xx_gpiochip *chip = gpiochip_get_data(gc);
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 = gpiochip_get_data(gc);
287 struct mpc52xx_gpio __iomem *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 mpc52xx_simple_gpiochip_probe(struct platform_device *ofdev)
312 struct mpc52xx_gpiochip *chip;
313 struct gpio_chip *gc;
314 struct mpc52xx_gpio __iomem *regs;
317 chip = devm_kzalloc(&ofdev->dev, sizeof(*chip), GFP_KERNEL);
321 platform_set_drvdata(ofdev, chip);
323 gc = &chip->mmchip.gc;
326 gc->direction_input = mpc52xx_simple_gpio_dir_in;
327 gc->direction_output = mpc52xx_simple_gpio_dir_out;
328 gc->get = mpc52xx_simple_gpio_get;
329 gc->set = mpc52xx_simple_gpio_set;
331 ret = of_mm_gpiochip_add_data(ofdev->dev.of_node, &chip->mmchip, chip);
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[] = {
344 { .compatible = "fsl,mpc5200-gpio", },
348 static struct platform_driver mpc52xx_simple_gpiochip_driver = {
350 .name = "mpc5200-gpio",
351 .of_match_table = mpc52xx_simple_gpiochip_match,
353 .probe = mpc52xx_simple_gpiochip_probe,
354 .remove = mpc52xx_gpiochip_remove,
357 static struct platform_driver * const drivers[] = {
358 &mpc52xx_wkup_gpiochip_driver,
359 &mpc52xx_simple_gpiochip_driver,
362 static int __init mpc52xx_gpio_init(void)
364 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
367 /* Make sure we get initialised before anyone else tries to use us */
368 subsys_initcall(mpc52xx_gpio_init);
370 static void __exit mpc52xx_gpio_exit(void)
372 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
374 module_exit(mpc52xx_gpio_exit);
376 MODULE_DESCRIPTION("Freescale MPC52xx gpio driver");
378 MODULE_LICENSE("GPL v2");