2 * arch/arm/mach-lpc32xx/gpiolib.c
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.h>
25 #include <mach/hardware.h>
26 #include <mach/platform.h>
27 #include <mach/gpio-lpc32xx.h>
29 #define LPC32XX_GPIO_P3_INP_STATE _GPREG(0x000)
30 #define LPC32XX_GPIO_P3_OUTP_SET _GPREG(0x004)
31 #define LPC32XX_GPIO_P3_OUTP_CLR _GPREG(0x008)
32 #define LPC32XX_GPIO_P3_OUTP_STATE _GPREG(0x00C)
33 #define LPC32XX_GPIO_P2_DIR_SET _GPREG(0x010)
34 #define LPC32XX_GPIO_P2_DIR_CLR _GPREG(0x014)
35 #define LPC32XX_GPIO_P2_DIR_STATE _GPREG(0x018)
36 #define LPC32XX_GPIO_P2_INP_STATE _GPREG(0x01C)
37 #define LPC32XX_GPIO_P2_OUTP_SET _GPREG(0x020)
38 #define LPC32XX_GPIO_P2_OUTP_CLR _GPREG(0x024)
39 #define LPC32XX_GPIO_P2_MUX_SET _GPREG(0x028)
40 #define LPC32XX_GPIO_P2_MUX_CLR _GPREG(0x02C)
41 #define LPC32XX_GPIO_P2_MUX_STATE _GPREG(0x030)
42 #define LPC32XX_GPIO_P0_INP_STATE _GPREG(0x040)
43 #define LPC32XX_GPIO_P0_OUTP_SET _GPREG(0x044)
44 #define LPC32XX_GPIO_P0_OUTP_CLR _GPREG(0x048)
45 #define LPC32XX_GPIO_P0_OUTP_STATE _GPREG(0x04C)
46 #define LPC32XX_GPIO_P0_DIR_SET _GPREG(0x050)
47 #define LPC32XX_GPIO_P0_DIR_CLR _GPREG(0x054)
48 #define LPC32XX_GPIO_P0_DIR_STATE _GPREG(0x058)
49 #define LPC32XX_GPIO_P1_INP_STATE _GPREG(0x060)
50 #define LPC32XX_GPIO_P1_OUTP_SET _GPREG(0x064)
51 #define LPC32XX_GPIO_P1_OUTP_CLR _GPREG(0x068)
52 #define LPC32XX_GPIO_P1_OUTP_STATE _GPREG(0x06C)
53 #define LPC32XX_GPIO_P1_DIR_SET _GPREG(0x070)
54 #define LPC32XX_GPIO_P1_DIR_CLR _GPREG(0x074)
55 #define LPC32XX_GPIO_P1_DIR_STATE _GPREG(0x078)
57 #define GPIO012_PIN_TO_BIT(x) (1 << (x))
58 #define GPIO3_PIN_TO_BIT(x) (1 << ((x) + 25))
59 #define GPO3_PIN_TO_BIT(x) (1 << (x))
60 #define GPIO012_PIN_IN_SEL(x, y) (((x) >> (y)) & 1)
61 #define GPIO3_PIN_IN_SHIFT(x) ((x) == 5 ? 24 : 10 + (x))
62 #define GPIO3_PIN_IN_SEL(x, y) (((x) >> GPIO3_PIN_IN_SHIFT(y)) & 1)
63 #define GPIO3_PIN5_IN_SEL(x) (((x) >> 24) & 1)
64 #define GPI3_PIN_IN_SEL(x, y) (((x) >> (y)) & 1)
65 #define GPO3_PIN_IN_SEL(x, y) (((x) >> (y)) & 1)
68 void __iomem
*inp_state
;
69 void __iomem
*outp_state
;
70 void __iomem
*outp_set
;
71 void __iomem
*outp_clr
;
72 void __iomem
*dir_set
;
73 void __iomem
*dir_clr
;
79 static const char *gpio_p0_names
[LPC32XX_GPIO_P0_MAX
] = {
80 "p0.0", "p0.1", "p0.2", "p0.3",
81 "p0.4", "p0.5", "p0.6", "p0.7"
84 static const char *gpio_p1_names
[LPC32XX_GPIO_P1_MAX
] = {
85 "p1.0", "p1.1", "p1.2", "p1.3",
86 "p1.4", "p1.5", "p1.6", "p1.7",
87 "p1.8", "p1.9", "p1.10", "p1.11",
88 "p1.12", "p1.13", "p1.14", "p1.15",
89 "p1.16", "p1.17", "p1.18", "p1.19",
90 "p1.20", "p1.21", "p1.22", "p1.23",
93 static const char *gpio_p2_names
[LPC32XX_GPIO_P2_MAX
] = {
94 "p2.0", "p2.1", "p2.2", "p2.3",
95 "p2.4", "p2.5", "p2.6", "p2.7",
96 "p2.8", "p2.9", "p2.10", "p2.11",
100 static const char *gpio_p3_names
[LPC32XX_GPIO_P3_MAX
] = {
101 "gpio00", "gpio01", "gpio02", "gpio03",
105 static const char *gpi_p3_names
[LPC32XX_GPI_P3_MAX
] = {
106 "gpi00", "gpi01", "gpi02", "gpi03",
107 "gpi04", "gpi05", "gpi06", "gpi07",
108 "gpi08", "gpi09", NULL
, NULL
,
109 NULL
, NULL
, NULL
, "gpi15",
110 "gpi16", "gpi17", "gpi18", "gpi19",
111 "gpi20", "gpi21", "gpi22", "gpi23",
112 "gpi24", "gpi25", "gpi26", "gpi27"
115 static const char *gpo_p3_names
[LPC32XX_GPO_P3_MAX
] = {
116 "gpo00", "gpo01", "gpo02", "gpo03",
117 "gpo04", "gpo05", "gpo06", "gpo07",
118 "gpo08", "gpo09", "gpo10", "gpo11",
119 "gpo12", "gpo13", "gpo14", "gpo15",
120 "gpo16", "gpo17", "gpo18", "gpo19",
121 "gpo20", "gpo21", "gpo22", "gpo23"
124 static struct gpio_regs gpio_grp_regs_p0
= {
125 .inp_state
= LPC32XX_GPIO_P0_INP_STATE
,
126 .outp_set
= LPC32XX_GPIO_P0_OUTP_SET
,
127 .outp_clr
= LPC32XX_GPIO_P0_OUTP_CLR
,
128 .dir_set
= LPC32XX_GPIO_P0_DIR_SET
,
129 .dir_clr
= LPC32XX_GPIO_P0_DIR_CLR
,
132 static struct gpio_regs gpio_grp_regs_p1
= {
133 .inp_state
= LPC32XX_GPIO_P1_INP_STATE
,
134 .outp_set
= LPC32XX_GPIO_P1_OUTP_SET
,
135 .outp_clr
= LPC32XX_GPIO_P1_OUTP_CLR
,
136 .dir_set
= LPC32XX_GPIO_P1_DIR_SET
,
137 .dir_clr
= LPC32XX_GPIO_P1_DIR_CLR
,
140 static struct gpio_regs gpio_grp_regs_p2
= {
141 .inp_state
= LPC32XX_GPIO_P2_INP_STATE
,
142 .outp_set
= LPC32XX_GPIO_P2_OUTP_SET
,
143 .outp_clr
= LPC32XX_GPIO_P2_OUTP_CLR
,
144 .dir_set
= LPC32XX_GPIO_P2_DIR_SET
,
145 .dir_clr
= LPC32XX_GPIO_P2_DIR_CLR
,
148 static struct gpio_regs gpio_grp_regs_p3
= {
149 .inp_state
= LPC32XX_GPIO_P3_INP_STATE
,
150 .outp_state
= LPC32XX_GPIO_P3_OUTP_STATE
,
151 .outp_set
= LPC32XX_GPIO_P3_OUTP_SET
,
152 .outp_clr
= LPC32XX_GPIO_P3_OUTP_CLR
,
153 .dir_set
= LPC32XX_GPIO_P2_DIR_SET
,
154 .dir_clr
= LPC32XX_GPIO_P2_DIR_CLR
,
157 struct lpc32xx_gpio_chip
{
158 struct gpio_chip chip
;
159 struct gpio_regs
*gpio_grp
;
162 static inline struct lpc32xx_gpio_chip
*to_lpc32xx_gpio(
163 struct gpio_chip
*gpc
)
165 return container_of(gpc
, struct lpc32xx_gpio_chip
, chip
);
168 static void __set_gpio_dir_p012(struct lpc32xx_gpio_chip
*group
,
169 unsigned pin
, int input
)
172 __raw_writel(GPIO012_PIN_TO_BIT(pin
),
173 group
->gpio_grp
->dir_clr
);
175 __raw_writel(GPIO012_PIN_TO_BIT(pin
),
176 group
->gpio_grp
->dir_set
);
179 static void __set_gpio_dir_p3(struct lpc32xx_gpio_chip
*group
,
180 unsigned pin
, int input
)
182 u32 u
= GPIO3_PIN_TO_BIT(pin
);
185 __raw_writel(u
, group
->gpio_grp
->dir_clr
);
187 __raw_writel(u
, group
->gpio_grp
->dir_set
);
190 static void __set_gpio_level_p012(struct lpc32xx_gpio_chip
*group
,
191 unsigned pin
, int high
)
194 __raw_writel(GPIO012_PIN_TO_BIT(pin
),
195 group
->gpio_grp
->outp_set
);
197 __raw_writel(GPIO012_PIN_TO_BIT(pin
),
198 group
->gpio_grp
->outp_clr
);
201 static void __set_gpio_level_p3(struct lpc32xx_gpio_chip
*group
,
202 unsigned pin
, int high
)
204 u32 u
= GPIO3_PIN_TO_BIT(pin
);
207 __raw_writel(u
, group
->gpio_grp
->outp_set
);
209 __raw_writel(u
, group
->gpio_grp
->outp_clr
);
212 static void __set_gpo_level_p3(struct lpc32xx_gpio_chip
*group
,
213 unsigned pin
, int high
)
216 __raw_writel(GPO3_PIN_TO_BIT(pin
), group
->gpio_grp
->outp_set
);
218 __raw_writel(GPO3_PIN_TO_BIT(pin
), group
->gpio_grp
->outp_clr
);
221 static int __get_gpio_state_p012(struct lpc32xx_gpio_chip
*group
,
224 return GPIO012_PIN_IN_SEL(__raw_readl(group
->gpio_grp
->inp_state
),
228 static int __get_gpio_state_p3(struct lpc32xx_gpio_chip
*group
,
231 int state
= __raw_readl(group
->gpio_grp
->inp_state
);
234 * P3 GPIO pin input mapping is not contiguous, GPIOP3-0..4 is mapped
235 * to bits 10..14, while GPIOP3-5 is mapped to bit 24.
237 return GPIO3_PIN_IN_SEL(state
, pin
);
240 static int __get_gpi_state_p3(struct lpc32xx_gpio_chip
*group
,
243 return GPI3_PIN_IN_SEL(__raw_readl(group
->gpio_grp
->inp_state
), pin
);
246 static int __get_gpo_state_p3(struct lpc32xx_gpio_chip
*group
,
249 return GPO3_PIN_IN_SEL(__raw_readl(group
->gpio_grp
->outp_state
), pin
);
253 * GENERIC_GPIO primitives.
255 static int lpc32xx_gpio_dir_input_p012(struct gpio_chip
*chip
,
258 struct lpc32xx_gpio_chip
*group
= to_lpc32xx_gpio(chip
);
260 __set_gpio_dir_p012(group
, pin
, 1);
265 static int lpc32xx_gpio_dir_input_p3(struct gpio_chip
*chip
,
268 struct lpc32xx_gpio_chip
*group
= to_lpc32xx_gpio(chip
);
270 __set_gpio_dir_p3(group
, pin
, 1);
275 static int lpc32xx_gpio_dir_in_always(struct gpio_chip
*chip
,
281 static int lpc32xx_gpio_get_value_p012(struct gpio_chip
*chip
, unsigned pin
)
283 struct lpc32xx_gpio_chip
*group
= to_lpc32xx_gpio(chip
);
285 return __get_gpio_state_p012(group
, pin
);
288 static int lpc32xx_gpio_get_value_p3(struct gpio_chip
*chip
, unsigned pin
)
290 struct lpc32xx_gpio_chip
*group
= to_lpc32xx_gpio(chip
);
292 return __get_gpio_state_p3(group
, pin
);
295 static int lpc32xx_gpi_get_value(struct gpio_chip
*chip
, unsigned pin
)
297 struct lpc32xx_gpio_chip
*group
= to_lpc32xx_gpio(chip
);
299 return __get_gpi_state_p3(group
, pin
);
302 static int lpc32xx_gpio_dir_output_p012(struct gpio_chip
*chip
, unsigned pin
,
305 struct lpc32xx_gpio_chip
*group
= to_lpc32xx_gpio(chip
);
307 __set_gpio_level_p012(group
, pin
, value
);
308 __set_gpio_dir_p012(group
, pin
, 0);
313 static int lpc32xx_gpio_dir_output_p3(struct gpio_chip
*chip
, unsigned pin
,
316 struct lpc32xx_gpio_chip
*group
= to_lpc32xx_gpio(chip
);
318 __set_gpio_level_p3(group
, pin
, value
);
319 __set_gpio_dir_p3(group
, pin
, 0);
324 static int lpc32xx_gpio_dir_out_always(struct gpio_chip
*chip
, unsigned pin
,
327 struct lpc32xx_gpio_chip
*group
= to_lpc32xx_gpio(chip
);
329 __set_gpo_level_p3(group
, pin
, value
);
333 static void lpc32xx_gpio_set_value_p012(struct gpio_chip
*chip
, unsigned pin
,
336 struct lpc32xx_gpio_chip
*group
= to_lpc32xx_gpio(chip
);
338 __set_gpio_level_p012(group
, pin
, value
);
341 static void lpc32xx_gpio_set_value_p3(struct gpio_chip
*chip
, unsigned pin
,
344 struct lpc32xx_gpio_chip
*group
= to_lpc32xx_gpio(chip
);
346 __set_gpio_level_p3(group
, pin
, value
);
349 static void lpc32xx_gpo_set_value(struct gpio_chip
*chip
, unsigned pin
,
352 struct lpc32xx_gpio_chip
*group
= to_lpc32xx_gpio(chip
);
354 __set_gpo_level_p3(group
, pin
, value
);
357 static int lpc32xx_gpo_get_value(struct gpio_chip
*chip
, unsigned pin
)
359 struct lpc32xx_gpio_chip
*group
= to_lpc32xx_gpio(chip
);
361 return __get_gpo_state_p3(group
, pin
);
364 static int lpc32xx_gpio_request(struct gpio_chip
*chip
, unsigned pin
)
366 if (pin
< chip
->ngpio
)
372 static struct lpc32xx_gpio_chip lpc32xx_gpiochip
[] = {
376 .direction_input
= lpc32xx_gpio_dir_input_p012
,
377 .get
= lpc32xx_gpio_get_value_p012
,
378 .direction_output
= lpc32xx_gpio_dir_output_p012
,
379 .set
= lpc32xx_gpio_set_value_p012
,
380 .request
= lpc32xx_gpio_request
,
381 .base
= LPC32XX_GPIO_P0_GRP
,
382 .ngpio
= LPC32XX_GPIO_P0_MAX
,
383 .names
= gpio_p0_names
,
386 .gpio_grp
= &gpio_grp_regs_p0
,
391 .direction_input
= lpc32xx_gpio_dir_input_p012
,
392 .get
= lpc32xx_gpio_get_value_p012
,
393 .direction_output
= lpc32xx_gpio_dir_output_p012
,
394 .set
= lpc32xx_gpio_set_value_p012
,
395 .request
= lpc32xx_gpio_request
,
396 .base
= LPC32XX_GPIO_P1_GRP
,
397 .ngpio
= LPC32XX_GPIO_P1_MAX
,
398 .names
= gpio_p1_names
,
401 .gpio_grp
= &gpio_grp_regs_p1
,
406 .direction_input
= lpc32xx_gpio_dir_input_p012
,
407 .get
= lpc32xx_gpio_get_value_p012
,
408 .direction_output
= lpc32xx_gpio_dir_output_p012
,
409 .set
= lpc32xx_gpio_set_value_p012
,
410 .request
= lpc32xx_gpio_request
,
411 .base
= LPC32XX_GPIO_P2_GRP
,
412 .ngpio
= LPC32XX_GPIO_P2_MAX
,
413 .names
= gpio_p2_names
,
416 .gpio_grp
= &gpio_grp_regs_p2
,
421 .direction_input
= lpc32xx_gpio_dir_input_p3
,
422 .get
= lpc32xx_gpio_get_value_p3
,
423 .direction_output
= lpc32xx_gpio_dir_output_p3
,
424 .set
= lpc32xx_gpio_set_value_p3
,
425 .request
= lpc32xx_gpio_request
,
426 .base
= LPC32XX_GPIO_P3_GRP
,
427 .ngpio
= LPC32XX_GPIO_P3_MAX
,
428 .names
= gpio_p3_names
,
431 .gpio_grp
= &gpio_grp_regs_p3
,
436 .direction_input
= lpc32xx_gpio_dir_in_always
,
437 .get
= lpc32xx_gpi_get_value
,
438 .request
= lpc32xx_gpio_request
,
439 .base
= LPC32XX_GPI_P3_GRP
,
440 .ngpio
= LPC32XX_GPI_P3_MAX
,
441 .names
= gpi_p3_names
,
444 .gpio_grp
= &gpio_grp_regs_p3
,
449 .direction_output
= lpc32xx_gpio_dir_out_always
,
450 .set
= lpc32xx_gpo_set_value
,
451 .get
= lpc32xx_gpo_get_value
,
452 .request
= lpc32xx_gpio_request
,
453 .base
= LPC32XX_GPO_P3_GRP
,
454 .ngpio
= LPC32XX_GPO_P3_MAX
,
455 .names
= gpo_p3_names
,
458 .gpio_grp
= &gpio_grp_regs_p3
,
462 void __init
lpc32xx_gpio_init(void)
466 for (i
= 0; i
< ARRAY_SIZE(lpc32xx_gpiochip
); i
++)
467 gpiochip_add(&lpc32xx_gpiochip
[i
].chip
);