3 * Guennadi Liakhovetski, DENX Software Engineering, <lg@denx.de>
6 * Stefano Babic, DENX Software Engineering, <sbabic@denx.de>
8 * SPDX-License-Identifier: GPL-2.0+
11 #include <asm/arch/imx-regs.h>
16 enum mxc_gpio_direction {
17 MXC_GPIO_DIRECTION_IN,
18 MXC_GPIO_DIRECTION_OUT,
21 #define GPIO_TO_PORT(n) ((n) / 32)
23 /* GPIO port description */
24 static unsigned long gpio_ports[] = {
25 [0] = GPIO1_BASE_ADDR,
26 [1] = GPIO2_BASE_ADDR,
27 [2] = GPIO3_BASE_ADDR,
28 #if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \
29 defined(CONFIG_MX53) || defined(CONFIG_MX6)
30 [3] = GPIO4_BASE_ADDR,
32 #if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6)
33 [4] = GPIO5_BASE_ADDR,
34 [5] = GPIO6_BASE_ADDR,
36 #if defined(CONFIG_MX53) || defined(CONFIG_MX6)
37 [6] = GPIO7_BASE_ADDR,
41 static int mxc_gpio_direction(unsigned int gpio,
42 enum mxc_gpio_direction direction)
44 unsigned int port = GPIO_TO_PORT(gpio);
45 struct gpio_regs *regs;
48 if (port >= ARRAY_SIZE(gpio_ports)) {
49 printf("%s: Invalid GPIO %d\n", __func__, gpio);
55 regs = (struct gpio_regs *)gpio_ports[port];
57 l = readl(®s->gpio_dir);
60 case MXC_GPIO_DIRECTION_OUT:
63 case MXC_GPIO_DIRECTION_IN:
66 writel(l, ®s->gpio_dir);
71 int gpio_set_value(unsigned gpio, int value)
73 unsigned int port = GPIO_TO_PORT(gpio);
74 struct gpio_regs *regs;
77 if (port >= ARRAY_SIZE(gpio_ports)) {
78 printf("%s: Invalid GPIO %d\n", __func__, gpio);
84 regs = (struct gpio_regs *)gpio_ports[port];
86 l = readl(®s->gpio_dr);
91 writel(l, ®s->gpio_dr);
96 int gpio_get_value(unsigned gpio)
98 unsigned int port = GPIO_TO_PORT(gpio);
99 struct gpio_regs *regs;
102 if (port >= ARRAY_SIZE(gpio_ports)) {
103 printf("%s: Invalid GPIO %d\n", __func__, gpio);
109 regs = (struct gpio_regs *)gpio_ports[port];
111 if (readl(®s->gpio_dir) & (1 << gpio)) {
112 printf("WARNING: Reading status of output GPIO_%d_%d\n",
113 port - GPIO_TO_PORT(0), gpio);
114 val = (readl(®s->gpio_dr) >> gpio) & 0x01;
116 val = (readl(®s->gpio_psr) >> gpio) & 0x01;
121 int gpio_request(unsigned gpio, const char *label)
123 unsigned int port = GPIO_TO_PORT(gpio);
124 if (port >= ARRAY_SIZE(gpio_ports)) {
125 printf("%s: Invalid GPIO %d\n", __func__, gpio);
131 int gpio_free(unsigned gpio)
133 unsigned int port = GPIO_TO_PORT(gpio);
134 if (port >= ARRAY_SIZE(gpio_ports)) {
135 printf("%s: Invalid GPIO %d\n", __func__, gpio);
141 int gpio_direction_input(unsigned gpio)
143 return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_IN);
146 int gpio_direction_output(unsigned gpio, int value)
148 int ret = gpio_set_value(gpio, value);
153 return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_OUT);