Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / gpio / gpio-mpc5200.c
blob000494e0c533b1f054b8a98f1226307a1a4b1864
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * MPC52xx gpio driver
5 * Copyright (c) 2008 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
6 */
8 #include <linux/of.h>
9 #include <linux/kernel.h>
10 #include <linux/slab.h>
11 #include <linux/of_gpio.h>
12 #include <linux/io.h>
13 #include <linux/of_platform.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.
34 * 0 -> GPIO_WKUP_7
35 * 1 -> GPIO_WKUP_6
36 * 2 -> PSC6_1
37 * 3 -> PSC6_0
38 * 4 -> ETH_17
39 * 5 -> PSC3_9
40 * 6 -> PSC2_4
41 * 7 -> PSC1_4
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;
48 unsigned int ret;
50 ret = (in_8(&regs->wkup_ival) >> (7 - gpio)) & 1;
52 pr_debug("%s: gpio: %d ret: %d\n", __func__, gpio, ret);
54 return ret;
57 static inline void
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;
64 if (val)
65 chip->shadow_dvo |= 1 << (7 - gpio);
66 else
67 chip->shadow_dvo &= ~(1 << (7 - gpio));
69 out_8(&regs->wkup_dvo, chip->shadow_dvo);
72 static void
73 mpc52xx_wkup_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
75 unsigned long flags;
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;
91 unsigned long flags;
93 spin_lock_irqsave(&gpio_lock, flags);
95 /* set the direction */
96 chip->shadow_ddr &= ~(1 << (7 - gpio));
97 out_8(&regs->wkup_ddr, chip->shadow_ddr);
99 /* and enable the pin */
100 chip->shadow_gpioe |= 1 << (7 - gpio);
101 out_8(&regs->wkup_gpioe, chip->shadow_gpioe);
103 spin_unlock_irqrestore(&gpio_lock, flags);
105 return 0;
108 static int
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);
114 unsigned long flags;
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(&regs->wkup_ddr, chip->shadow_ddr);
124 /* Finally enable the pin */
125 chip->shadow_gpioe |= 1 << (7 - gpio);
126 out_8(&regs->wkup_gpioe, chip->shadow_gpioe);
128 spin_unlock_irqrestore(&gpio_lock, flags);
130 pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
132 return 0;
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;
140 int ret;
142 chip = devm_kzalloc(&ofdev->dev, sizeof(*chip), GFP_KERNEL);
143 if (!chip)
144 return -ENOMEM;
146 platform_set_drvdata(ofdev, chip);
148 gc = &chip->mmchip.gc;
150 gc->ngpio = 8;
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);
157 if (ret)
158 return ret;
160 regs = chip->mmchip.regs;
161 chip->shadow_gpioe = in_8(&regs->wkup_gpioe);
162 chip->shadow_ddr = in_8(&regs->wkup_ddr);
163 chip->shadow_dvo = in_8(&regs->wkup_dvo);
165 return 0;
168 static int mpc52xx_gpiochip_remove(struct platform_device *ofdev)
170 struct mpc52xx_gpiochip *chip = platform_get_drvdata(ofdev);
172 of_mm_gpiochip_remove(&chip->mmchip);
174 return 0;
177 static const struct of_device_id mpc52xx_wkup_gpiochip_match[] = {
178 { .compatible = "fsl,mpc5200-gpio-wkup", },
182 static struct platform_driver mpc52xx_wkup_gpiochip_driver = {
183 .driver = {
184 .name = "mpc5200-gpio-wkup",
185 .of_match_table = mpc52xx_wkup_gpiochip_match,
187 .probe = mpc52xx_wkup_gpiochip_probe,
188 .remove = mpc52xx_gpiochip_remove,
192 * GPIO LIB API implementation for simple GPIOs
194 * There's a maximum of 32 simple GPIOs. Which of these are available
195 * for use depends on your board setup.
196 * The numbering reflects the bit numbering in the port registers:
198 * 0..1 > reserved
199 * 2..3 > IRDA
200 * 4..7 > ETHR
201 * 8..11 > reserved
202 * 12..15 > USB
203 * 16..17 > reserved
204 * 18..23 > PSC3
205 * 24..27 > PSC2
206 * 28..31 > PSC1
208 static int mpc52xx_simple_gpio_get(struct gpio_chip *gc, unsigned int gpio)
210 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
211 struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
212 unsigned int ret;
214 ret = (in_be32(&regs->simple_ival) >> (31 - gpio)) & 1;
216 return ret;
219 static inline void
220 __mpc52xx_simple_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
222 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
223 struct mpc52xx_gpiochip *chip = gpiochip_get_data(gc);
224 struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
226 if (val)
227 chip->shadow_dvo |= 1 << (31 - gpio);
228 else
229 chip->shadow_dvo &= ~(1 << (31 - gpio));
230 out_be32(&regs->simple_dvo, chip->shadow_dvo);
233 static void
234 mpc52xx_simple_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
236 unsigned long flags;
238 spin_lock_irqsave(&gpio_lock, flags);
240 __mpc52xx_simple_gpio_set(gc, gpio, val);
242 spin_unlock_irqrestore(&gpio_lock, flags);
244 pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
247 static int mpc52xx_simple_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
249 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
250 struct mpc52xx_gpiochip *chip = gpiochip_get_data(gc);
251 struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
252 unsigned long flags;
254 spin_lock_irqsave(&gpio_lock, flags);
256 /* set the direction */
257 chip->shadow_ddr &= ~(1 << (31 - gpio));
258 out_be32(&regs->simple_ddr, chip->shadow_ddr);
260 /* and enable the pin */
261 chip->shadow_gpioe |= 1 << (31 - gpio);
262 out_be32(&regs->simple_gpioe, chip->shadow_gpioe);
264 spin_unlock_irqrestore(&gpio_lock, flags);
266 return 0;
269 static int
270 mpc52xx_simple_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
272 struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
273 struct mpc52xx_gpiochip *chip = gpiochip_get_data(gc);
274 struct mpc52xx_gpio __iomem *regs = mm_gc->regs;
275 unsigned long flags;
277 spin_lock_irqsave(&gpio_lock, flags);
279 /* First set initial value */
280 __mpc52xx_simple_gpio_set(gc, gpio, val);
282 /* Then set direction */
283 chip->shadow_ddr |= 1 << (31 - gpio);
284 out_be32(&regs->simple_ddr, chip->shadow_ddr);
286 /* Finally enable the pin */
287 chip->shadow_gpioe |= 1 << (31 - gpio);
288 out_be32(&regs->simple_gpioe, chip->shadow_gpioe);
290 spin_unlock_irqrestore(&gpio_lock, flags);
292 pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
294 return 0;
297 static int mpc52xx_simple_gpiochip_probe(struct platform_device *ofdev)
299 struct mpc52xx_gpiochip *chip;
300 struct gpio_chip *gc;
301 struct mpc52xx_gpio __iomem *regs;
302 int ret;
304 chip = devm_kzalloc(&ofdev->dev, sizeof(*chip), GFP_KERNEL);
305 if (!chip)
306 return -ENOMEM;
308 platform_set_drvdata(ofdev, chip);
310 gc = &chip->mmchip.gc;
312 gc->ngpio = 32;
313 gc->direction_input = mpc52xx_simple_gpio_dir_in;
314 gc->direction_output = mpc52xx_simple_gpio_dir_out;
315 gc->get = mpc52xx_simple_gpio_get;
316 gc->set = mpc52xx_simple_gpio_set;
318 ret = of_mm_gpiochip_add_data(ofdev->dev.of_node, &chip->mmchip, chip);
319 if (ret)
320 return ret;
322 regs = chip->mmchip.regs;
323 chip->shadow_gpioe = in_be32(&regs->simple_gpioe);
324 chip->shadow_ddr = in_be32(&regs->simple_ddr);
325 chip->shadow_dvo = in_be32(&regs->simple_dvo);
327 return 0;
330 static const struct of_device_id mpc52xx_simple_gpiochip_match[] = {
331 { .compatible = "fsl,mpc5200-gpio", },
335 static struct platform_driver mpc52xx_simple_gpiochip_driver = {
336 .driver = {
337 .name = "mpc5200-gpio",
338 .of_match_table = mpc52xx_simple_gpiochip_match,
340 .probe = mpc52xx_simple_gpiochip_probe,
341 .remove = mpc52xx_gpiochip_remove,
344 static struct platform_driver * const drivers[] = {
345 &mpc52xx_wkup_gpiochip_driver,
346 &mpc52xx_simple_gpiochip_driver,
349 static int __init mpc52xx_gpio_init(void)
351 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
354 /* Make sure we get initialised before anyone else tries to use us */
355 subsys_initcall(mpc52xx_gpio_init);
357 static void __exit mpc52xx_gpio_exit(void)
359 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
361 module_exit(mpc52xx_gpio_exit);
363 MODULE_DESCRIPTION("Freescale MPC52xx gpio driver");
364 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de");
365 MODULE_LICENSE("GPL v2");