1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * GPIO driver for LPC32xx SoC
7 * Copyright (C) 2010 NXP Semiconductors
10 #include <linux/kernel.h>
11 #include <linux/init.h>
13 #include <linux/errno.h>
14 #include <linux/gpio/driver.h>
16 #include <linux/platform_device.h>
17 #include <linux/module.h>
19 #include <mach/hardware.h>
20 #include <mach/platform.h>
22 #define LPC32XX_GPIO_P3_INP_STATE _GPREG(0x000)
23 #define LPC32XX_GPIO_P3_OUTP_SET _GPREG(0x004)
24 #define LPC32XX_GPIO_P3_OUTP_CLR _GPREG(0x008)
25 #define LPC32XX_GPIO_P3_OUTP_STATE _GPREG(0x00C)
26 #define LPC32XX_GPIO_P2_DIR_SET _GPREG(0x010)
27 #define LPC32XX_GPIO_P2_DIR_CLR _GPREG(0x014)
28 #define LPC32XX_GPIO_P2_DIR_STATE _GPREG(0x018)
29 #define LPC32XX_GPIO_P2_INP_STATE _GPREG(0x01C)
30 #define LPC32XX_GPIO_P2_OUTP_SET _GPREG(0x020)
31 #define LPC32XX_GPIO_P2_OUTP_CLR _GPREG(0x024)
32 #define LPC32XX_GPIO_P2_MUX_SET _GPREG(0x028)
33 #define LPC32XX_GPIO_P2_MUX_CLR _GPREG(0x02C)
34 #define LPC32XX_GPIO_P2_MUX_STATE _GPREG(0x030)
35 #define LPC32XX_GPIO_P0_INP_STATE _GPREG(0x040)
36 #define LPC32XX_GPIO_P0_OUTP_SET _GPREG(0x044)
37 #define LPC32XX_GPIO_P0_OUTP_CLR _GPREG(0x048)
38 #define LPC32XX_GPIO_P0_OUTP_STATE _GPREG(0x04C)
39 #define LPC32XX_GPIO_P0_DIR_SET _GPREG(0x050)
40 #define LPC32XX_GPIO_P0_DIR_CLR _GPREG(0x054)
41 #define LPC32XX_GPIO_P0_DIR_STATE _GPREG(0x058)
42 #define LPC32XX_GPIO_P1_INP_STATE _GPREG(0x060)
43 #define LPC32XX_GPIO_P1_OUTP_SET _GPREG(0x064)
44 #define LPC32XX_GPIO_P1_OUTP_CLR _GPREG(0x068)
45 #define LPC32XX_GPIO_P1_OUTP_STATE _GPREG(0x06C)
46 #define LPC32XX_GPIO_P1_DIR_SET _GPREG(0x070)
47 #define LPC32XX_GPIO_P1_DIR_CLR _GPREG(0x074)
48 #define LPC32XX_GPIO_P1_DIR_STATE _GPREG(0x078)
50 #define GPIO012_PIN_TO_BIT(x) (1 << (x))
51 #define GPIO3_PIN_TO_BIT(x) (1 << ((x) + 25))
52 #define GPO3_PIN_TO_BIT(x) (1 << (x))
53 #define GPIO012_PIN_IN_SEL(x, y) (((x) >> (y)) & 1)
54 #define GPIO3_PIN_IN_SHIFT(x) ((x) == 5 ? 24 : 10 + (x))
55 #define GPIO3_PIN_IN_SEL(x, y) (((x) >> GPIO3_PIN_IN_SHIFT(y)) & 1)
56 #define GPIO3_PIN5_IN_SEL(x) (((x) >> 24) & 1)
57 #define GPI3_PIN_IN_SEL(x, y) (((x) >> (y)) & 1)
58 #define GPO3_PIN_IN_SEL(x, y) (((x) >> (y)) & 1)
60 #define LPC32XX_GPIO_P0_MAX 8
61 #define LPC32XX_GPIO_P1_MAX 24
62 #define LPC32XX_GPIO_P2_MAX 13
63 #define LPC32XX_GPIO_P3_MAX 6
64 #define LPC32XX_GPI_P3_MAX 29
65 #define LPC32XX_GPO_P3_MAX 24
67 #define LPC32XX_GPIO_P0_GRP 0
68 #define LPC32XX_GPIO_P1_GRP (LPC32XX_GPIO_P0_GRP + LPC32XX_GPIO_P0_MAX)
69 #define LPC32XX_GPIO_P2_GRP (LPC32XX_GPIO_P1_GRP + LPC32XX_GPIO_P1_MAX)
70 #define LPC32XX_GPIO_P3_GRP (LPC32XX_GPIO_P2_GRP + LPC32XX_GPIO_P2_MAX)
71 #define LPC32XX_GPI_P3_GRP (LPC32XX_GPIO_P3_GRP + LPC32XX_GPIO_P3_MAX)
72 #define LPC32XX_GPO_P3_GRP (LPC32XX_GPI_P3_GRP + LPC32XX_GPI_P3_MAX)
75 void __iomem *inp_state;
76 void __iomem *outp_state;
77 void __iomem *outp_set;
78 void __iomem *outp_clr;
79 void __iomem *dir_set;
80 void __iomem *dir_clr;
86 static const char *gpio_p0_names[LPC32XX_GPIO_P0_MAX] = {
87 "p0.0", "p0.1", "p0.2", "p0.3",
88 "p0.4", "p0.5", "p0.6", "p0.7"
91 static const char *gpio_p1_names[LPC32XX_GPIO_P1_MAX] = {
92 "p1.0", "p1.1", "p1.2", "p1.3",
93 "p1.4", "p1.5", "p1.6", "p1.7",
94 "p1.8", "p1.9", "p1.10", "p1.11",
95 "p1.12", "p1.13", "p1.14", "p1.15",
96 "p1.16", "p1.17", "p1.18", "p1.19",
97 "p1.20", "p1.21", "p1.22", "p1.23",
100 static const char *gpio_p2_names[LPC32XX_GPIO_P2_MAX] = {
101 "p2.0", "p2.1", "p2.2", "p2.3",
102 "p2.4", "p2.5", "p2.6", "p2.7",
103 "p2.8", "p2.9", "p2.10", "p2.11",
107 static const char *gpio_p3_names[LPC32XX_GPIO_P3_MAX] = {
108 "gpio00", "gpio01", "gpio02", "gpio03",
112 static const char *gpi_p3_names[LPC32XX_GPI_P3_MAX] = {
113 "gpi00", "gpi01", "gpi02", "gpi03",
114 "gpi04", "gpi05", "gpi06", "gpi07",
115 "gpi08", "gpi09", NULL, NULL,
116 NULL, NULL, NULL, "gpi15",
117 "gpi16", "gpi17", "gpi18", "gpi19",
118 "gpi20", "gpi21", "gpi22", "gpi23",
119 "gpi24", "gpi25", "gpi26", "gpi27",
123 static const char *gpo_p3_names[LPC32XX_GPO_P3_MAX] = {
124 "gpo00", "gpo01", "gpo02", "gpo03",
125 "gpo04", "gpo05", "gpo06", "gpo07",
126 "gpo08", "gpo09", "gpo10", "gpo11",
127 "gpo12", "gpo13", "gpo14", "gpo15",
128 "gpo16", "gpo17", "gpo18", "gpo19",
129 "gpo20", "gpo21", "gpo22", "gpo23"
132 static struct gpio_regs gpio_grp_regs_p0 = {
133 .inp_state = LPC32XX_GPIO_P0_INP_STATE,
134 .outp_set = LPC32XX_GPIO_P0_OUTP_SET,
135 .outp_clr = LPC32XX_GPIO_P0_OUTP_CLR,
136 .dir_set = LPC32XX_GPIO_P0_DIR_SET,
137 .dir_clr = LPC32XX_GPIO_P0_DIR_CLR,
140 static struct gpio_regs gpio_grp_regs_p1 = {
141 .inp_state = LPC32XX_GPIO_P1_INP_STATE,
142 .outp_set = LPC32XX_GPIO_P1_OUTP_SET,
143 .outp_clr = LPC32XX_GPIO_P1_OUTP_CLR,
144 .dir_set = LPC32XX_GPIO_P1_DIR_SET,
145 .dir_clr = LPC32XX_GPIO_P1_DIR_CLR,
148 static struct gpio_regs gpio_grp_regs_p2 = {
149 .inp_state = LPC32XX_GPIO_P2_INP_STATE,
150 .outp_set = LPC32XX_GPIO_P2_OUTP_SET,
151 .outp_clr = LPC32XX_GPIO_P2_OUTP_CLR,
152 .dir_set = LPC32XX_GPIO_P2_DIR_SET,
153 .dir_clr = LPC32XX_GPIO_P2_DIR_CLR,
156 static struct gpio_regs gpio_grp_regs_p3 = {
157 .inp_state = LPC32XX_GPIO_P3_INP_STATE,
158 .outp_state = LPC32XX_GPIO_P3_OUTP_STATE,
159 .outp_set = LPC32XX_GPIO_P3_OUTP_SET,
160 .outp_clr = LPC32XX_GPIO_P3_OUTP_CLR,
161 .dir_set = LPC32XX_GPIO_P2_DIR_SET,
162 .dir_clr = LPC32XX_GPIO_P2_DIR_CLR,
165 struct lpc32xx_gpio_chip {
166 struct gpio_chip chip;
167 struct gpio_regs *gpio_grp;
170 static void __set_gpio_dir_p012(struct lpc32xx_gpio_chip *group,
171 unsigned pin, int input)
174 __raw_writel(GPIO012_PIN_TO_BIT(pin),
175 group->gpio_grp->dir_clr);
177 __raw_writel(GPIO012_PIN_TO_BIT(pin),
178 group->gpio_grp->dir_set);
181 static void __set_gpio_dir_p3(struct lpc32xx_gpio_chip *group,
182 unsigned pin, int input)
184 u32 u = GPIO3_PIN_TO_BIT(pin);
187 __raw_writel(u, group->gpio_grp->dir_clr);
189 __raw_writel(u, group->gpio_grp->dir_set);
192 static void __set_gpio_level_p012(struct lpc32xx_gpio_chip *group,
193 unsigned pin, int high)
196 __raw_writel(GPIO012_PIN_TO_BIT(pin),
197 group->gpio_grp->outp_set);
199 __raw_writel(GPIO012_PIN_TO_BIT(pin),
200 group->gpio_grp->outp_clr);
203 static void __set_gpio_level_p3(struct lpc32xx_gpio_chip *group,
204 unsigned pin, int high)
206 u32 u = GPIO3_PIN_TO_BIT(pin);
209 __raw_writel(u, group->gpio_grp->outp_set);
211 __raw_writel(u, group->gpio_grp->outp_clr);
214 static void __set_gpo_level_p3(struct lpc32xx_gpio_chip *group,
215 unsigned pin, int high)
218 __raw_writel(GPO3_PIN_TO_BIT(pin), group->gpio_grp->outp_set);
220 __raw_writel(GPO3_PIN_TO_BIT(pin), group->gpio_grp->outp_clr);
223 static int __get_gpio_state_p012(struct lpc32xx_gpio_chip *group,
226 return GPIO012_PIN_IN_SEL(__raw_readl(group->gpio_grp->inp_state),
230 static int __get_gpio_state_p3(struct lpc32xx_gpio_chip *group,
233 int state = __raw_readl(group->gpio_grp->inp_state);
236 * P3 GPIO pin input mapping is not contiguous, GPIOP3-0..4 is mapped
237 * to bits 10..14, while GPIOP3-5 is mapped to bit 24.
239 return GPIO3_PIN_IN_SEL(state, pin);
242 static int __get_gpi_state_p3(struct lpc32xx_gpio_chip *group,
245 return GPI3_PIN_IN_SEL(__raw_readl(group->gpio_grp->inp_state), pin);
248 static int __get_gpo_state_p3(struct lpc32xx_gpio_chip *group,
251 return GPO3_PIN_IN_SEL(__raw_readl(group->gpio_grp->outp_state), pin);
257 static int lpc32xx_gpio_dir_input_p012(struct gpio_chip *chip,
260 struct lpc32xx_gpio_chip *group = gpiochip_get_data(chip);
262 __set_gpio_dir_p012(group, pin, 1);
267 static int lpc32xx_gpio_dir_input_p3(struct gpio_chip *chip,
270 struct lpc32xx_gpio_chip *group = gpiochip_get_data(chip);
272 __set_gpio_dir_p3(group, pin, 1);
277 static int lpc32xx_gpio_dir_in_always(struct gpio_chip *chip,
283 static int lpc32xx_gpio_get_value_p012(struct gpio_chip *chip, unsigned pin)
285 struct lpc32xx_gpio_chip *group = gpiochip_get_data(chip);
287 return !!__get_gpio_state_p012(group, pin);
290 static int lpc32xx_gpio_get_value_p3(struct gpio_chip *chip, unsigned pin)
292 struct lpc32xx_gpio_chip *group = gpiochip_get_data(chip);
294 return !!__get_gpio_state_p3(group, pin);
297 static int lpc32xx_gpi_get_value(struct gpio_chip *chip, unsigned pin)
299 struct lpc32xx_gpio_chip *group = gpiochip_get_data(chip);
301 return !!__get_gpi_state_p3(group, pin);
304 static int lpc32xx_gpio_dir_output_p012(struct gpio_chip *chip, unsigned pin,
307 struct lpc32xx_gpio_chip *group = gpiochip_get_data(chip);
309 __set_gpio_level_p012(group, pin, value);
310 __set_gpio_dir_p012(group, pin, 0);
315 static int lpc32xx_gpio_dir_output_p3(struct gpio_chip *chip, unsigned pin,
318 struct lpc32xx_gpio_chip *group = gpiochip_get_data(chip);
320 __set_gpio_level_p3(group, pin, value);
321 __set_gpio_dir_p3(group, pin, 0);
326 static int lpc32xx_gpio_dir_out_always(struct gpio_chip *chip, unsigned pin,
329 struct lpc32xx_gpio_chip *group = gpiochip_get_data(chip);
331 __set_gpo_level_p3(group, pin, value);
335 static void lpc32xx_gpio_set_value_p012(struct gpio_chip *chip, unsigned pin,
338 struct lpc32xx_gpio_chip *group = gpiochip_get_data(chip);
340 __set_gpio_level_p012(group, pin, value);
343 static void lpc32xx_gpio_set_value_p3(struct gpio_chip *chip, unsigned pin,
346 struct lpc32xx_gpio_chip *group = gpiochip_get_data(chip);
348 __set_gpio_level_p3(group, pin, value);
351 static void lpc32xx_gpo_set_value(struct gpio_chip *chip, unsigned pin,
354 struct lpc32xx_gpio_chip *group = gpiochip_get_data(chip);
356 __set_gpo_level_p3(group, pin, value);
359 static int lpc32xx_gpo_get_value(struct gpio_chip *chip, unsigned pin)
361 struct lpc32xx_gpio_chip *group = gpiochip_get_data(chip);
363 return !!__get_gpo_state_p3(group, pin);
366 static int lpc32xx_gpio_request(struct gpio_chip *chip, unsigned pin)
368 if (pin < chip->ngpio)
374 static int lpc32xx_gpio_to_irq_p01(struct gpio_chip *chip, unsigned offset)
379 static int lpc32xx_gpio_to_irq_gpio_p3(struct gpio_chip *chip, unsigned offset)
384 static int lpc32xx_gpio_to_irq_gpi_p3(struct gpio_chip *chip, unsigned offset)
389 static struct lpc32xx_gpio_chip lpc32xx_gpiochip[] = {
393 .direction_input = lpc32xx_gpio_dir_input_p012,
394 .get = lpc32xx_gpio_get_value_p012,
395 .direction_output = lpc32xx_gpio_dir_output_p012,
396 .set = lpc32xx_gpio_set_value_p012,
397 .request = lpc32xx_gpio_request,
398 .to_irq = lpc32xx_gpio_to_irq_p01,
399 .base = LPC32XX_GPIO_P0_GRP,
400 .ngpio = LPC32XX_GPIO_P0_MAX,
401 .names = gpio_p0_names,
404 .gpio_grp = &gpio_grp_regs_p0,
409 .direction_input = lpc32xx_gpio_dir_input_p012,
410 .get = lpc32xx_gpio_get_value_p012,
411 .direction_output = lpc32xx_gpio_dir_output_p012,
412 .set = lpc32xx_gpio_set_value_p012,
413 .request = lpc32xx_gpio_request,
414 .to_irq = lpc32xx_gpio_to_irq_p01,
415 .base = LPC32XX_GPIO_P1_GRP,
416 .ngpio = LPC32XX_GPIO_P1_MAX,
417 .names = gpio_p1_names,
420 .gpio_grp = &gpio_grp_regs_p1,
425 .direction_input = lpc32xx_gpio_dir_input_p012,
426 .get = lpc32xx_gpio_get_value_p012,
427 .direction_output = lpc32xx_gpio_dir_output_p012,
428 .set = lpc32xx_gpio_set_value_p012,
429 .request = lpc32xx_gpio_request,
430 .base = LPC32XX_GPIO_P2_GRP,
431 .ngpio = LPC32XX_GPIO_P2_MAX,
432 .names = gpio_p2_names,
435 .gpio_grp = &gpio_grp_regs_p2,
440 .direction_input = lpc32xx_gpio_dir_input_p3,
441 .get = lpc32xx_gpio_get_value_p3,
442 .direction_output = lpc32xx_gpio_dir_output_p3,
443 .set = lpc32xx_gpio_set_value_p3,
444 .request = lpc32xx_gpio_request,
445 .to_irq = lpc32xx_gpio_to_irq_gpio_p3,
446 .base = LPC32XX_GPIO_P3_GRP,
447 .ngpio = LPC32XX_GPIO_P3_MAX,
448 .names = gpio_p3_names,
451 .gpio_grp = &gpio_grp_regs_p3,
456 .direction_input = lpc32xx_gpio_dir_in_always,
457 .get = lpc32xx_gpi_get_value,
458 .request = lpc32xx_gpio_request,
459 .to_irq = lpc32xx_gpio_to_irq_gpi_p3,
460 .base = LPC32XX_GPI_P3_GRP,
461 .ngpio = LPC32XX_GPI_P3_MAX,
462 .names = gpi_p3_names,
465 .gpio_grp = &gpio_grp_regs_p3,
470 .direction_output = lpc32xx_gpio_dir_out_always,
471 .set = lpc32xx_gpo_set_value,
472 .get = lpc32xx_gpo_get_value,
473 .request = lpc32xx_gpio_request,
474 .base = LPC32XX_GPO_P3_GRP,
475 .ngpio = LPC32XX_GPO_P3_MAX,
476 .names = gpo_p3_names,
479 .gpio_grp = &gpio_grp_regs_p3,
483 static int lpc32xx_of_xlate(struct gpio_chip *gc,
484 const struct of_phandle_args *gpiospec, u32 *flags)
486 /* Is this the correct bank? */
487 u32 bank = gpiospec->args[0];
488 if ((bank >= ARRAY_SIZE(lpc32xx_gpiochip) ||
489 (gc != &lpc32xx_gpiochip[bank].chip)))
493 *flags = gpiospec->args[2];
494 return gpiospec->args[1];
497 static int lpc32xx_gpio_probe(struct platform_device *pdev)
501 for (i = 0; i < ARRAY_SIZE(lpc32xx_gpiochip); i++) {
502 if (pdev->dev.of_node) {
503 lpc32xx_gpiochip[i].chip.of_xlate = lpc32xx_of_xlate;
504 lpc32xx_gpiochip[i].chip.of_gpio_n_cells = 3;
505 lpc32xx_gpiochip[i].chip.of_node = pdev->dev.of_node;
507 devm_gpiochip_add_data(&pdev->dev, &lpc32xx_gpiochip[i].chip,
508 &lpc32xx_gpiochip[i]);
515 static const struct of_device_id lpc32xx_gpio_of_match[] = {
516 { .compatible = "nxp,lpc3220-gpio", },
521 static struct platform_driver lpc32xx_gpio_driver = {
523 .name = "lpc32xx-gpio",
524 .of_match_table = of_match_ptr(lpc32xx_gpio_of_match),
526 .probe = lpc32xx_gpio_probe,
529 module_platform_driver(lpc32xx_gpio_driver);