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
))
53 regs
= (struct gpio_regs
*)gpio_ports
[port
];
55 l
= readl(®s
->gpio_dir
);
58 case MXC_GPIO_DIRECTION_OUT
:
61 case MXC_GPIO_DIRECTION_IN
:
64 writel(l
, ®s
->gpio_dir
);
69 int gpio_set_value(unsigned gpio
, int value
)
71 unsigned int port
= GPIO_TO_PORT(gpio
);
72 struct gpio_regs
*regs
;
75 if (port
>= ARRAY_SIZE(gpio_ports
))
80 regs
= (struct gpio_regs
*)gpio_ports
[port
];
82 l
= readl(®s
->gpio_dr
);
87 writel(l
, ®s
->gpio_dr
);
92 int gpio_get_value(unsigned gpio
)
94 unsigned int port
= GPIO_TO_PORT(gpio
);
95 struct gpio_regs
*regs
;
98 if (port
>= ARRAY_SIZE(gpio_ports
))
103 regs
= (struct gpio_regs
*)gpio_ports
[port
];
105 val
= (readl(®s
->gpio_psr
) >> gpio
) & 0x01;
110 int gpio_request(unsigned gpio
, const char *label
)
112 unsigned int port
= GPIO_TO_PORT(gpio
);
113 if (port
>= ARRAY_SIZE(gpio_ports
))
118 int gpio_free(unsigned gpio
)
123 int gpio_direction_input(unsigned gpio
)
125 return mxc_gpio_direction(gpio
, MXC_GPIO_DIRECTION_IN
);
128 int gpio_direction_output(unsigned gpio
, int value
)
130 int ret
= gpio_set_value(gpio
, value
);
135 return mxc_gpio_direction(gpio
, MXC_GPIO_DIRECTION_OUT
);