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>
22 #include <linux/errno.h>
23 #include <linux/gpio/driver.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)
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
;
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",
116 static const char *gpio_p3_names
[LPC32XX_GPIO_P3_MAX
] = {
117 "gpio00", "gpio01", "gpio02", "gpio03",
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",
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
)
183 __raw_writel(GPIO012_PIN_TO_BIT(pin
),
184 group
->gpio_grp
->dir_clr
);
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
);
196 __raw_writel(u
, group
->gpio_grp
->dir_clr
);
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
)
205 __raw_writel(GPIO012_PIN_TO_BIT(pin
),
206 group
->gpio_grp
->outp_set
);
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
);
218 __raw_writel(u
, group
->gpio_grp
->outp_set
);
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
)
227 __raw_writel(GPO3_PIN_TO_BIT(pin
), group
->gpio_grp
->outp_set
);
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
,
235 return GPIO012_PIN_IN_SEL(__raw_readl(group
->gpio_grp
->inp_state
),
239 static int __get_gpio_state_p3(struct lpc32xx_gpio_chip
*group
,
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
,
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
,
260 return GPO3_PIN_IN_SEL(__raw_readl(group
->gpio_grp
->outp_state
), pin
);
266 static int lpc32xx_gpio_dir_input_p012(struct gpio_chip
*chip
,
269 struct lpc32xx_gpio_chip
*group
= gpiochip_get_data(chip
);
271 __set_gpio_dir_p012(group
, pin
, 1);
276 static int lpc32xx_gpio_dir_input_p3(struct gpio_chip
*chip
,
279 struct lpc32xx_gpio_chip
*group
= gpiochip_get_data(chip
);
281 __set_gpio_dir_p3(group
, pin
, 1);
286 static int lpc32xx_gpio_dir_in_always(struct gpio_chip
*chip
,
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
,
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);
324 static int lpc32xx_gpio_dir_output_p3(struct gpio_chip
*chip
, unsigned pin
,
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);
335 static int lpc32xx_gpio_dir_out_always(struct gpio_chip
*chip
, unsigned pin
,
338 struct lpc32xx_gpio_chip
*group
= gpiochip_get_data(chip
);
340 __set_gpo_level_p3(group
, pin
, value
);
344 static void lpc32xx_gpio_set_value_p012(struct gpio_chip
*chip
, unsigned pin
,
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
,
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
,
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
)
383 static int lpc32xx_gpio_to_irq_p01(struct gpio_chip
*chip
, unsigned offset
)
388 static int lpc32xx_gpio_to_irq_gpio_p3(struct gpio_chip
*chip
, unsigned offset
)
393 static int lpc32xx_gpio_to_irq_gpi_p3(struct gpio_chip
*chip
, unsigned offset
)
398 static struct lpc32xx_gpio_chip lpc32xx_gpiochip
[] = {
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
,
413 .gpio_grp
= &gpio_grp_regs_p0
,
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
,
429 .gpio_grp
= &gpio_grp_regs_p1
,
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
,
444 .gpio_grp
= &gpio_grp_regs_p2
,
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
,
460 .gpio_grp
= &gpio_grp_regs_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
,
474 .gpio_grp
= &gpio_grp_regs_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
,
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
)))
502 *flags
= gpiospec
->args
[2];
503 return gpiospec
->args
[1];
506 static int lpc32xx_gpio_probe(struct platform_device
*pdev
)
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
]);
524 static const struct of_device_id lpc32xx_gpio_of_match
[] = {
525 { .compatible
= "nxp,lpc3220-gpio", },
530 static struct platform_driver lpc32xx_gpio_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
);