1 // SPDX-License-Identifier: GPL-2.0-only
9 #include <linux/kernel.h>
10 #include <linux/slab.h>
11 #include <linux/gpio/legacy-of-mm-gpiochip.h>
13 #include <linux/platform_device.h>
14 #include <linux/module.h>
16 #include <asm/mpc52xx.h>
17 #include <sysdev/fsl_soc.h>
19 static DEFINE_SPINLOCK(gpio_lock);
21 struct mpc52xx_gpiochip {
22 struct of_mm_gpio_chip mmchip;
23 unsigned int shadow_dvo;
24 unsigned int shadow_gpioe;
25 unsigned int shadow_ddr;
29 * GPIO LIB API implementation for wakeup GPIOs.
31 * There's a maximum of 8 wakeup GPIOs. Which of these are available
32 * for use depends on your board setup.
44 static int mpc52xx_wkup_gpio_get(struct gpio_chip *gc, unsigned int gpio)
46 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
47 struct mpc52xx_gpio_wkup __iomem *regs = mm_gc->regs;
50 ret = (in_8(®s->wkup_ival) >> (7 - gpio)) & 1;
52 pr_debug("%s: gpio: %d ret: %d\n", __func__, gpio, ret);
58 __mpc52xx_wkup_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
60 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
61 struct mpc52xx_gpiochip *chip = gpiochip_get_data(gc);
62 struct mpc52xx_gpio_wkup __iomem *regs = mm_gc->regs;
65 chip->shadow_dvo |= 1 << (7 - gpio);
67 chip->shadow_dvo &= ~(1 << (7 - gpio));
69 out_8(®s->wkup_dvo, chip->shadow_dvo);
73 mpc52xx_wkup_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
77 spin_lock_irqsave(&gpio_lock, flags);
79 __mpc52xx_wkup_gpio_set(gc, gpio, val);
81 spin_unlock_irqrestore(&gpio_lock, flags);
83 pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
86 static int mpc52xx_wkup_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
88 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
89 struct mpc52xx_gpiochip *chip = gpiochip_get_data(gc);
90 struct mpc52xx_gpio_wkup __iomem *regs = mm_gc->regs;
93 spin_lock_irqsave(&gpio_lock, flags);
95 /* set the direction */
96 chip->shadow_ddr &= ~(1 << (7 - gpio));
97 out_8(®s->wkup_ddr, chip->shadow_ddr);
99 /* and enable the pin */
100 chip->shadow_gpioe |= 1 << (7 - gpio);
101 out_8(®s->wkup_gpioe, chip->shadow_gpioe);
103 spin_unlock_irqrestore(&gpio_lock, flags);
109 mpc52xx_wkup_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
111 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
112 struct mpc52xx_gpio_wkup __iomem *regs = mm_gc->regs;
113 struct mpc52xx_gpiochip *chip = gpiochip_get_data(gc);
116 spin_lock_irqsave(&gpio_lock, flags);
118 __mpc52xx_wkup_gpio_set(gc, gpio, val);
120 /* Then set direction */
121 chip->shadow_ddr |= 1 << (7 - gpio);
122 out_8(®s->wkup_ddr, chip->shadow_ddr);
124 /* Finally enable the pin */
125 chip->shadow_gpioe |= 1 << (7 - gpio);
126 out_8(®s->wkup_gpioe, chip->shadow_gpioe);
128 spin_unlock_irqrestore(&gpio_lock, flags);
130 pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
135 static int mpc52xx_wkup_gpiochip_probe(struct platform_device *ofdev)
137 struct mpc52xx_gpiochip *chip;
138 struct mpc52xx_gpio_wkup __iomem *regs;
139 struct gpio_chip *gc;
142 chip = devm_kzalloc(&ofdev->dev, sizeof(*chip), GFP_KERNEL);
146 platform_set_drvdata(ofdev, chip);
148 gc = &chip->mmchip.gc;
151 gc->direction_input = mpc52xx_wkup_gpio_dir_in;
152 gc->direction_output = mpc52xx_wkup_gpio_dir_out;
153 gc->get = mpc52xx_wkup_gpio_get;
154 gc->set = mpc52xx_wkup_gpio_set;
156 ret = of_mm_gpiochip_add_data(ofdev->dev.of_node, &chip->mmchip, chip);
160 regs = chip->mmchip.regs;
161 chip->shadow_gpioe = in_8(®s->wkup_gpioe);
162 chip->shadow_ddr = in_8(®s->wkup_ddr);
163 chip->shadow_dvo = in_8(®s->wkup_dvo);
168 static void mpc52xx_gpiochip_remove(struct platform_device *ofdev)
170 struct mpc52xx_gpiochip *chip = platform_get_drvdata(ofdev);
172 of_mm_gpiochip_remove(&chip->mmchip);
175 static const struct of_device_id mpc52xx_wkup_gpiochip_match[] = {
176 { .compatible = "fsl,mpc5200-gpio-wkup", },
180 static struct platform_driver mpc52xx_wkup_gpiochip_driver = {
182 .name = "mpc5200-gpio-wkup",
183 .of_match_table = mpc52xx_wkup_gpiochip_match,
185 .probe = mpc52xx_wkup_gpiochip_probe,
186 .remove_new = mpc52xx_gpiochip_remove,
190 * GPIO LIB API implementation for simple GPIOs
192 * There's a maximum of 32 simple GPIOs. Which of these are available
193 * for use depends on your board setup.
194 * The numbering reflects the bit numbering in the port registers:
206 static int mpc52xx_simple_gpio_get(struct gpio_chip *gc, unsigned int gpio)
208 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
209 struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
212 ret = (in_be32(®s->simple_ival) >> (31 - gpio)) & 1;
218 __mpc52xx_simple_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
220 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
221 struct mpc52xx_gpiochip *chip = gpiochip_get_data(gc);
222 struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
225 chip->shadow_dvo |= 1 << (31 - gpio);
227 chip->shadow_dvo &= ~(1 << (31 - gpio));
228 out_be32(®s->simple_dvo, chip->shadow_dvo);
232 mpc52xx_simple_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
236 spin_lock_irqsave(&gpio_lock, flags);
238 __mpc52xx_simple_gpio_set(gc, gpio, val);
240 spin_unlock_irqrestore(&gpio_lock, flags);
242 pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
245 static int mpc52xx_simple_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
247 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
248 struct mpc52xx_gpiochip *chip = gpiochip_get_data(gc);
249 struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
252 spin_lock_irqsave(&gpio_lock, flags);
254 /* set the direction */
255 chip->shadow_ddr &= ~(1 << (31 - gpio));
256 out_be32(®s->simple_ddr, chip->shadow_ddr);
258 /* and enable the pin */
259 chip->shadow_gpioe |= 1 << (31 - gpio);
260 out_be32(®s->simple_gpioe, chip->shadow_gpioe);
262 spin_unlock_irqrestore(&gpio_lock, flags);
268 mpc52xx_simple_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
270 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
271 struct mpc52xx_gpiochip *chip = gpiochip_get_data(gc);
272 struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
275 spin_lock_irqsave(&gpio_lock, flags);
277 /* First set initial value */
278 __mpc52xx_simple_gpio_set(gc, gpio, val);
280 /* Then set direction */
281 chip->shadow_ddr |= 1 << (31 - gpio);
282 out_be32(®s->simple_ddr, chip->shadow_ddr);
284 /* Finally enable the pin */
285 chip->shadow_gpioe |= 1 << (31 - gpio);
286 out_be32(®s->simple_gpioe, chip->shadow_gpioe);
288 spin_unlock_irqrestore(&gpio_lock, flags);
290 pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
295 static int mpc52xx_simple_gpiochip_probe(struct platform_device *ofdev)
297 struct mpc52xx_gpiochip *chip;
298 struct gpio_chip *gc;
299 struct mpc52xx_gpio __iomem *regs;
302 chip = devm_kzalloc(&ofdev->dev, sizeof(*chip), GFP_KERNEL);
306 platform_set_drvdata(ofdev, chip);
308 gc = &chip->mmchip.gc;
311 gc->direction_input = mpc52xx_simple_gpio_dir_in;
312 gc->direction_output = mpc52xx_simple_gpio_dir_out;
313 gc->get = mpc52xx_simple_gpio_get;
314 gc->set = mpc52xx_simple_gpio_set;
316 ret = of_mm_gpiochip_add_data(ofdev->dev.of_node, &chip->mmchip, chip);
320 regs = chip->mmchip.regs;
321 chip->shadow_gpioe = in_be32(®s->simple_gpioe);
322 chip->shadow_ddr = in_be32(®s->simple_ddr);
323 chip->shadow_dvo = in_be32(®s->simple_dvo);
328 static const struct of_device_id mpc52xx_simple_gpiochip_match[] = {
329 { .compatible = "fsl,mpc5200-gpio", },
333 static struct platform_driver mpc52xx_simple_gpiochip_driver = {
335 .name = "mpc5200-gpio",
336 .of_match_table = mpc52xx_simple_gpiochip_match,
338 .probe = mpc52xx_simple_gpiochip_probe,
339 .remove_new = mpc52xx_gpiochip_remove,
342 static struct platform_driver * const drivers[] = {
343 &mpc52xx_wkup_gpiochip_driver,
344 &mpc52xx_simple_gpiochip_driver,
347 static int __init mpc52xx_gpio_init(void)
349 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
352 /* Make sure we get initialised before anyone else tries to use us */
353 subsys_initcall(mpc52xx_gpio_init);
355 static void __exit mpc52xx_gpio_exit(void)
357 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
359 module_exit(mpc52xx_gpio_exit);
361 MODULE_DESCRIPTION("Freescale MPC52xx gpio driver");
363 MODULE_LICENSE("GPL v2");