Linux 4.19.133
[linux/fpc-iii.git] / drivers / gpio / gpio-lpc32xx.c
blobaa74cc4d8b14f3ad9a80a63e51caeb74f39bbe91
1 /*
2 * GPIO driver for LPC32xx SoC
4 * Author: Kevin Wells <kevin.wells@nxp.com>
6 * Copyright (C) 2010 NXP Semiconductors
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/io.h>
22 #include <linux/errno.h>
23 #include <linux/gpio/driver.h>
24 #include <linux/of.h>
25 #include <linux/platform_device.h>
26 #include <linux/module.h>
28 #include <mach/hardware.h>
29 #include <mach/platform.h>
31 #define LPC32XX_GPIO_P3_INP_STATE _GPREG(0x000)
32 #define LPC32XX_GPIO_P3_OUTP_SET _GPREG(0x004)
33 #define LPC32XX_GPIO_P3_OUTP_CLR _GPREG(0x008)
34 #define LPC32XX_GPIO_P3_OUTP_STATE _GPREG(0x00C)
35 #define LPC32XX_GPIO_P2_DIR_SET _GPREG(0x010)
36 #define LPC32XX_GPIO_P2_DIR_CLR _GPREG(0x014)
37 #define LPC32XX_GPIO_P2_DIR_STATE _GPREG(0x018)
38 #define LPC32XX_GPIO_P2_INP_STATE _GPREG(0x01C)
39 #define LPC32XX_GPIO_P2_OUTP_SET _GPREG(0x020)
40 #define LPC32XX_GPIO_P2_OUTP_CLR _GPREG(0x024)
41 #define LPC32XX_GPIO_P2_MUX_SET _GPREG(0x028)
42 #define LPC32XX_GPIO_P2_MUX_CLR _GPREG(0x02C)
43 #define LPC32XX_GPIO_P2_MUX_STATE _GPREG(0x030)
44 #define LPC32XX_GPIO_P0_INP_STATE _GPREG(0x040)
45 #define LPC32XX_GPIO_P0_OUTP_SET _GPREG(0x044)
46 #define LPC32XX_GPIO_P0_OUTP_CLR _GPREG(0x048)
47 #define LPC32XX_GPIO_P0_OUTP_STATE _GPREG(0x04C)
48 #define LPC32XX_GPIO_P0_DIR_SET _GPREG(0x050)
49 #define LPC32XX_GPIO_P0_DIR_CLR _GPREG(0x054)
50 #define LPC32XX_GPIO_P0_DIR_STATE _GPREG(0x058)
51 #define LPC32XX_GPIO_P1_INP_STATE _GPREG(0x060)
52 #define LPC32XX_GPIO_P1_OUTP_SET _GPREG(0x064)
53 #define LPC32XX_GPIO_P1_OUTP_CLR _GPREG(0x068)
54 #define LPC32XX_GPIO_P1_OUTP_STATE _GPREG(0x06C)
55 #define LPC32XX_GPIO_P1_DIR_SET _GPREG(0x070)
56 #define LPC32XX_GPIO_P1_DIR_CLR _GPREG(0x074)
57 #define LPC32XX_GPIO_P1_DIR_STATE _GPREG(0x078)
59 #define GPIO012_PIN_TO_BIT(x) (1 << (x))
60 #define GPIO3_PIN_TO_BIT(x) (1 << ((x) + 25))
61 #define GPO3_PIN_TO_BIT(x) (1 << (x))
62 #define GPIO012_PIN_IN_SEL(x, y) (((x) >> (y)) & 1)
63 #define GPIO3_PIN_IN_SHIFT(x) ((x) == 5 ? 24 : 10 + (x))
64 #define GPIO3_PIN_IN_SEL(x, y) (((x) >> GPIO3_PIN_IN_SHIFT(y)) & 1)
65 #define GPIO3_PIN5_IN_SEL(x) (((x) >> 24) & 1)
66 #define GPI3_PIN_IN_SEL(x, y) (((x) >> (y)) & 1)
67 #define GPO3_PIN_IN_SEL(x, y) (((x) >> (y)) & 1)
69 #define LPC32XX_GPIO_P0_MAX 8
70 #define LPC32XX_GPIO_P1_MAX 24
71 #define LPC32XX_GPIO_P2_MAX 13
72 #define LPC32XX_GPIO_P3_MAX 6
73 #define LPC32XX_GPI_P3_MAX 29
74 #define LPC32XX_GPO_P3_MAX 24
76 #define LPC32XX_GPIO_P0_GRP 0
77 #define LPC32XX_GPIO_P1_GRP (LPC32XX_GPIO_P0_GRP + LPC32XX_GPIO_P0_MAX)
78 #define LPC32XX_GPIO_P2_GRP (LPC32XX_GPIO_P1_GRP + LPC32XX_GPIO_P1_MAX)
79 #define LPC32XX_GPIO_P3_GRP (LPC32XX_GPIO_P2_GRP + LPC32XX_GPIO_P2_MAX)
80 #define LPC32XX_GPI_P3_GRP (LPC32XX_GPIO_P3_GRP + LPC32XX_GPIO_P3_MAX)
81 #define LPC32XX_GPO_P3_GRP (LPC32XX_GPI_P3_GRP + LPC32XX_GPI_P3_MAX)
83 struct gpio_regs {
84 void __iomem *inp_state;
85 void __iomem *outp_state;
86 void __iomem *outp_set;
87 void __iomem *outp_clr;
88 void __iomem *dir_set;
89 void __iomem *dir_clr;
93 * GPIO names
95 static const char *gpio_p0_names[LPC32XX_GPIO_P0_MAX] = {
96 "p0.0", "p0.1", "p0.2", "p0.3",
97 "p0.4", "p0.5", "p0.6", "p0.7"
100 static const char *gpio_p1_names[LPC32XX_GPIO_P1_MAX] = {
101 "p1.0", "p1.1", "p1.2", "p1.3",
102 "p1.4", "p1.5", "p1.6", "p1.7",
103 "p1.8", "p1.9", "p1.10", "p1.11",
104 "p1.12", "p1.13", "p1.14", "p1.15",
105 "p1.16", "p1.17", "p1.18", "p1.19",
106 "p1.20", "p1.21", "p1.22", "p1.23",
109 static const char *gpio_p2_names[LPC32XX_GPIO_P2_MAX] = {
110 "p2.0", "p2.1", "p2.2", "p2.3",
111 "p2.4", "p2.5", "p2.6", "p2.7",
112 "p2.8", "p2.9", "p2.10", "p2.11",
113 "p2.12"
116 static const char *gpio_p3_names[LPC32XX_GPIO_P3_MAX] = {
117 "gpio00", "gpio01", "gpio02", "gpio03",
118 "gpio04", "gpio05"
121 static const char *gpi_p3_names[LPC32XX_GPI_P3_MAX] = {
122 "gpi00", "gpi01", "gpi02", "gpi03",
123 "gpi04", "gpi05", "gpi06", "gpi07",
124 "gpi08", "gpi09", NULL, NULL,
125 NULL, NULL, NULL, "gpi15",
126 "gpi16", "gpi17", "gpi18", "gpi19",
127 "gpi20", "gpi21", "gpi22", "gpi23",
128 "gpi24", "gpi25", "gpi26", "gpi27",
129 "gpi28"
132 static const char *gpo_p3_names[LPC32XX_GPO_P3_MAX] = {
133 "gpo00", "gpo01", "gpo02", "gpo03",
134 "gpo04", "gpo05", "gpo06", "gpo07",
135 "gpo08", "gpo09", "gpo10", "gpo11",
136 "gpo12", "gpo13", "gpo14", "gpo15",
137 "gpo16", "gpo17", "gpo18", "gpo19",
138 "gpo20", "gpo21", "gpo22", "gpo23"
141 static struct gpio_regs gpio_grp_regs_p0 = {
142 .inp_state = LPC32XX_GPIO_P0_INP_STATE,
143 .outp_set = LPC32XX_GPIO_P0_OUTP_SET,
144 .outp_clr = LPC32XX_GPIO_P0_OUTP_CLR,
145 .dir_set = LPC32XX_GPIO_P0_DIR_SET,
146 .dir_clr = LPC32XX_GPIO_P0_DIR_CLR,
149 static struct gpio_regs gpio_grp_regs_p1 = {
150 .inp_state = LPC32XX_GPIO_P1_INP_STATE,
151 .outp_set = LPC32XX_GPIO_P1_OUTP_SET,
152 .outp_clr = LPC32XX_GPIO_P1_OUTP_CLR,
153 .dir_set = LPC32XX_GPIO_P1_DIR_SET,
154 .dir_clr = LPC32XX_GPIO_P1_DIR_CLR,
157 static struct gpio_regs gpio_grp_regs_p2 = {
158 .inp_state = LPC32XX_GPIO_P2_INP_STATE,
159 .outp_set = LPC32XX_GPIO_P2_OUTP_SET,
160 .outp_clr = LPC32XX_GPIO_P2_OUTP_CLR,
161 .dir_set = LPC32XX_GPIO_P2_DIR_SET,
162 .dir_clr = LPC32XX_GPIO_P2_DIR_CLR,
165 static struct gpio_regs gpio_grp_regs_p3 = {
166 .inp_state = LPC32XX_GPIO_P3_INP_STATE,
167 .outp_state = LPC32XX_GPIO_P3_OUTP_STATE,
168 .outp_set = LPC32XX_GPIO_P3_OUTP_SET,
169 .outp_clr = LPC32XX_GPIO_P3_OUTP_CLR,
170 .dir_set = LPC32XX_GPIO_P2_DIR_SET,
171 .dir_clr = LPC32XX_GPIO_P2_DIR_CLR,
174 struct lpc32xx_gpio_chip {
175 struct gpio_chip chip;
176 struct gpio_regs *gpio_grp;
179 static void __set_gpio_dir_p012(struct lpc32xx_gpio_chip *group,
180 unsigned pin, int input)
182 if (input)
183 __raw_writel(GPIO012_PIN_TO_BIT(pin),
184 group->gpio_grp->dir_clr);
185 else
186 __raw_writel(GPIO012_PIN_TO_BIT(pin),
187 group->gpio_grp->dir_set);
190 static void __set_gpio_dir_p3(struct lpc32xx_gpio_chip *group,
191 unsigned pin, int input)
193 u32 u = GPIO3_PIN_TO_BIT(pin);
195 if (input)
196 __raw_writel(u, group->gpio_grp->dir_clr);
197 else
198 __raw_writel(u, group->gpio_grp->dir_set);
201 static void __set_gpio_level_p012(struct lpc32xx_gpio_chip *group,
202 unsigned pin, int high)
204 if (high)
205 __raw_writel(GPIO012_PIN_TO_BIT(pin),
206 group->gpio_grp->outp_set);
207 else
208 __raw_writel(GPIO012_PIN_TO_BIT(pin),
209 group->gpio_grp->outp_clr);
212 static void __set_gpio_level_p3(struct lpc32xx_gpio_chip *group,
213 unsigned pin, int high)
215 u32 u = GPIO3_PIN_TO_BIT(pin);
217 if (high)
218 __raw_writel(u, group->gpio_grp->outp_set);
219 else
220 __raw_writel(u, group->gpio_grp->outp_clr);
223 static void __set_gpo_level_p3(struct lpc32xx_gpio_chip *group,
224 unsigned pin, int high)
226 if (high)
227 __raw_writel(GPO3_PIN_TO_BIT(pin), group->gpio_grp->outp_set);
228 else
229 __raw_writel(GPO3_PIN_TO_BIT(pin), group->gpio_grp->outp_clr);
232 static int __get_gpio_state_p012(struct lpc32xx_gpio_chip *group,
233 unsigned pin)
235 return GPIO012_PIN_IN_SEL(__raw_readl(group->gpio_grp->inp_state),
236 pin);
239 static int __get_gpio_state_p3(struct lpc32xx_gpio_chip *group,
240 unsigned pin)
242 int state = __raw_readl(group->gpio_grp->inp_state);
245 * P3 GPIO pin input mapping is not contiguous, GPIOP3-0..4 is mapped
246 * to bits 10..14, while GPIOP3-5 is mapped to bit 24.
248 return GPIO3_PIN_IN_SEL(state, pin);
251 static int __get_gpi_state_p3(struct lpc32xx_gpio_chip *group,
252 unsigned pin)
254 return GPI3_PIN_IN_SEL(__raw_readl(group->gpio_grp->inp_state), pin);
257 static int __get_gpo_state_p3(struct lpc32xx_gpio_chip *group,
258 unsigned pin)
260 return GPO3_PIN_IN_SEL(__raw_readl(group->gpio_grp->outp_state), pin);
264 * GPIO primitives.
266 static int lpc32xx_gpio_dir_input_p012(struct gpio_chip *chip,
267 unsigned pin)
269 struct lpc32xx_gpio_chip *group = gpiochip_get_data(chip);
271 __set_gpio_dir_p012(group, pin, 1);
273 return 0;
276 static int lpc32xx_gpio_dir_input_p3(struct gpio_chip *chip,
277 unsigned pin)
279 struct lpc32xx_gpio_chip *group = gpiochip_get_data(chip);
281 __set_gpio_dir_p3(group, pin, 1);
283 return 0;
286 static int lpc32xx_gpio_dir_in_always(struct gpio_chip *chip,
287 unsigned pin)
289 return 0;
292 static int lpc32xx_gpio_get_value_p012(struct gpio_chip *chip, unsigned pin)
294 struct lpc32xx_gpio_chip *group = gpiochip_get_data(chip);
296 return !!__get_gpio_state_p012(group, pin);
299 static int lpc32xx_gpio_get_value_p3(struct gpio_chip *chip, unsigned pin)
301 struct lpc32xx_gpio_chip *group = gpiochip_get_data(chip);
303 return !!__get_gpio_state_p3(group, pin);
306 static int lpc32xx_gpi_get_value(struct gpio_chip *chip, unsigned pin)
308 struct lpc32xx_gpio_chip *group = gpiochip_get_data(chip);
310 return !!__get_gpi_state_p3(group, pin);
313 static int lpc32xx_gpio_dir_output_p012(struct gpio_chip *chip, unsigned pin,
314 int value)
316 struct lpc32xx_gpio_chip *group = gpiochip_get_data(chip);
318 __set_gpio_level_p012(group, pin, value);
319 __set_gpio_dir_p012(group, pin, 0);
321 return 0;
324 static int lpc32xx_gpio_dir_output_p3(struct gpio_chip *chip, unsigned pin,
325 int value)
327 struct lpc32xx_gpio_chip *group = gpiochip_get_data(chip);
329 __set_gpio_level_p3(group, pin, value);
330 __set_gpio_dir_p3(group, pin, 0);
332 return 0;
335 static int lpc32xx_gpio_dir_out_always(struct gpio_chip *chip, unsigned pin,
336 int value)
338 struct lpc32xx_gpio_chip *group = gpiochip_get_data(chip);
340 __set_gpo_level_p3(group, pin, value);
341 return 0;
344 static void lpc32xx_gpio_set_value_p012(struct gpio_chip *chip, unsigned pin,
345 int value)
347 struct lpc32xx_gpio_chip *group = gpiochip_get_data(chip);
349 __set_gpio_level_p012(group, pin, value);
352 static void lpc32xx_gpio_set_value_p3(struct gpio_chip *chip, unsigned pin,
353 int value)
355 struct lpc32xx_gpio_chip *group = gpiochip_get_data(chip);
357 __set_gpio_level_p3(group, pin, value);
360 static void lpc32xx_gpo_set_value(struct gpio_chip *chip, unsigned pin,
361 int value)
363 struct lpc32xx_gpio_chip *group = gpiochip_get_data(chip);
365 __set_gpo_level_p3(group, pin, value);
368 static int lpc32xx_gpo_get_value(struct gpio_chip *chip, unsigned pin)
370 struct lpc32xx_gpio_chip *group = gpiochip_get_data(chip);
372 return !!__get_gpo_state_p3(group, pin);
375 static int lpc32xx_gpio_request(struct gpio_chip *chip, unsigned pin)
377 if (pin < chip->ngpio)
378 return 0;
380 return -EINVAL;
383 static int lpc32xx_gpio_to_irq_p01(struct gpio_chip *chip, unsigned offset)
385 return -ENXIO;
388 static int lpc32xx_gpio_to_irq_gpio_p3(struct gpio_chip *chip, unsigned offset)
390 return -ENXIO;
393 static int lpc32xx_gpio_to_irq_gpi_p3(struct gpio_chip *chip, unsigned offset)
395 return -ENXIO;
398 static struct lpc32xx_gpio_chip lpc32xx_gpiochip[] = {
400 .chip = {
401 .label = "gpio_p0",
402 .direction_input = lpc32xx_gpio_dir_input_p012,
403 .get = lpc32xx_gpio_get_value_p012,
404 .direction_output = lpc32xx_gpio_dir_output_p012,
405 .set = lpc32xx_gpio_set_value_p012,
406 .request = lpc32xx_gpio_request,
407 .to_irq = lpc32xx_gpio_to_irq_p01,
408 .base = LPC32XX_GPIO_P0_GRP,
409 .ngpio = LPC32XX_GPIO_P0_MAX,
410 .names = gpio_p0_names,
411 .can_sleep = false,
413 .gpio_grp = &gpio_grp_regs_p0,
416 .chip = {
417 .label = "gpio_p1",
418 .direction_input = lpc32xx_gpio_dir_input_p012,
419 .get = lpc32xx_gpio_get_value_p012,
420 .direction_output = lpc32xx_gpio_dir_output_p012,
421 .set = lpc32xx_gpio_set_value_p012,
422 .request = lpc32xx_gpio_request,
423 .to_irq = lpc32xx_gpio_to_irq_p01,
424 .base = LPC32XX_GPIO_P1_GRP,
425 .ngpio = LPC32XX_GPIO_P1_MAX,
426 .names = gpio_p1_names,
427 .can_sleep = false,
429 .gpio_grp = &gpio_grp_regs_p1,
432 .chip = {
433 .label = "gpio_p2",
434 .direction_input = lpc32xx_gpio_dir_input_p012,
435 .get = lpc32xx_gpio_get_value_p012,
436 .direction_output = lpc32xx_gpio_dir_output_p012,
437 .set = lpc32xx_gpio_set_value_p012,
438 .request = lpc32xx_gpio_request,
439 .base = LPC32XX_GPIO_P2_GRP,
440 .ngpio = LPC32XX_GPIO_P2_MAX,
441 .names = gpio_p2_names,
442 .can_sleep = false,
444 .gpio_grp = &gpio_grp_regs_p2,
447 .chip = {
448 .label = "gpio_p3",
449 .direction_input = lpc32xx_gpio_dir_input_p3,
450 .get = lpc32xx_gpio_get_value_p3,
451 .direction_output = lpc32xx_gpio_dir_output_p3,
452 .set = lpc32xx_gpio_set_value_p3,
453 .request = lpc32xx_gpio_request,
454 .to_irq = lpc32xx_gpio_to_irq_gpio_p3,
455 .base = LPC32XX_GPIO_P3_GRP,
456 .ngpio = LPC32XX_GPIO_P3_MAX,
457 .names = gpio_p3_names,
458 .can_sleep = false,
460 .gpio_grp = &gpio_grp_regs_p3,
463 .chip = {
464 .label = "gpi_p3",
465 .direction_input = lpc32xx_gpio_dir_in_always,
466 .get = lpc32xx_gpi_get_value,
467 .request = lpc32xx_gpio_request,
468 .to_irq = lpc32xx_gpio_to_irq_gpi_p3,
469 .base = LPC32XX_GPI_P3_GRP,
470 .ngpio = LPC32XX_GPI_P3_MAX,
471 .names = gpi_p3_names,
472 .can_sleep = false,
474 .gpio_grp = &gpio_grp_regs_p3,
477 .chip = {
478 .label = "gpo_p3",
479 .direction_output = lpc32xx_gpio_dir_out_always,
480 .set = lpc32xx_gpo_set_value,
481 .get = lpc32xx_gpo_get_value,
482 .request = lpc32xx_gpio_request,
483 .base = LPC32XX_GPO_P3_GRP,
484 .ngpio = LPC32XX_GPO_P3_MAX,
485 .names = gpo_p3_names,
486 .can_sleep = false,
488 .gpio_grp = &gpio_grp_regs_p3,
492 static int lpc32xx_of_xlate(struct gpio_chip *gc,
493 const struct of_phandle_args *gpiospec, u32 *flags)
495 /* Is this the correct bank? */
496 u32 bank = gpiospec->args[0];
497 if ((bank >= ARRAY_SIZE(lpc32xx_gpiochip) ||
498 (gc != &lpc32xx_gpiochip[bank].chip)))
499 return -EINVAL;
501 if (flags)
502 *flags = gpiospec->args[2];
503 return gpiospec->args[1];
506 static int lpc32xx_gpio_probe(struct platform_device *pdev)
508 int i;
510 for (i = 0; i < ARRAY_SIZE(lpc32xx_gpiochip); i++) {
511 if (pdev->dev.of_node) {
512 lpc32xx_gpiochip[i].chip.of_xlate = lpc32xx_of_xlate;
513 lpc32xx_gpiochip[i].chip.of_gpio_n_cells = 3;
514 lpc32xx_gpiochip[i].chip.of_node = pdev->dev.of_node;
516 devm_gpiochip_add_data(&pdev->dev, &lpc32xx_gpiochip[i].chip,
517 &lpc32xx_gpiochip[i]);
520 return 0;
523 #ifdef CONFIG_OF
524 static const struct of_device_id lpc32xx_gpio_of_match[] = {
525 { .compatible = "nxp,lpc3220-gpio", },
526 { },
528 #endif
530 static struct platform_driver lpc32xx_gpio_driver = {
531 .driver = {
532 .name = "lpc32xx-gpio",
533 .of_match_table = of_match_ptr(lpc32xx_gpio_of_match),
535 .probe = lpc32xx_gpio_probe,
538 module_platform_driver(lpc32xx_gpio_driver);