2 * arch/arm/plat-orion/gpio.c
4 * Marvell Orion SoC GPIO handling.
6 * See file CREDITS for list of people who contributed to this
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26 * Based on (mostly copied from) plat-orion based Linux 2.6 kernel driver.
27 * Removed orion_gpiochip struct and kernel level irq handling.
29 * Dieter Kiermaier dk-arm-linux@gmx.de
33 #include <asm/bitops.h>
35 #include <asm/arch/kirkwood.h>
36 #include <asm/arch/gpio.h>
38 static unsigned long gpio_valid_input[BITS_TO_LONGS(GPIO_MAX)];
39 static unsigned long gpio_valid_output[BITS_TO_LONGS(GPIO_MAX)];
41 void __set_direction(unsigned pin, int input)
45 u = readl(GPIO_IO_CONF(pin));
49 u &= ~(1 << (pin & 31));
50 writel(u, GPIO_IO_CONF(pin));
52 u = readl(GPIO_IO_CONF(pin));
55 void __set_level(unsigned pin, int high)
59 u = readl(GPIO_OUT(pin));
63 u &= ~(1 << (pin & 31));
64 writel(u, GPIO_OUT(pin));
67 void __set_blinking(unsigned pin, int blink)
71 u = readl(GPIO_BLINK_EN(pin));
75 u &= ~(1 << (pin & 31));
76 writel(u, GPIO_BLINK_EN(pin));
79 int kw_gpio_is_valid(unsigned pin, int mode)
82 if ((mode & GPIO_INPUT_OK) && !test_bit(pin, gpio_valid_input))
85 if ((mode & GPIO_OUTPUT_OK) && !test_bit(pin, gpio_valid_output))
91 printf("%s: invalid GPIO %d\n", __func__, pin);
95 void kw_gpio_set_valid(unsigned pin, int mode)
98 mode = GPIO_INPUT_OK | GPIO_OUTPUT_OK;
99 if (mode & GPIO_INPUT_OK)
100 __set_bit(pin, gpio_valid_input);
102 __clear_bit(pin, gpio_valid_input);
103 if (mode & GPIO_OUTPUT_OK)
104 __set_bit(pin, gpio_valid_output);
106 __clear_bit(pin, gpio_valid_output);
109 * GENERIC_GPIO primitives.
111 int kw_gpio_direction_input(unsigned pin)
113 if (kw_gpio_is_valid(pin, GPIO_INPUT_OK) != 0)
116 /* Configure GPIO direction. */
117 __set_direction(pin, 1);
122 int kw_gpio_direction_output(unsigned pin, int value)
124 if (kw_gpio_is_valid(pin, GPIO_OUTPUT_OK) != 0)
126 printf("%s: invalid GPIO %d\n", __func__, pin);
130 __set_blinking(pin, 0);
132 /* Configure GPIO output value. */
133 __set_level(pin, value);
135 /* Configure GPIO direction. */
136 __set_direction(pin, 0);
141 int kw_gpio_get_value(unsigned pin)
145 if (readl(GPIO_IO_CONF(pin)) & (1 << (pin & 31)))
146 val = readl(GPIO_DATA_IN(pin)) ^ readl(GPIO_IN_POL(pin));
148 val = readl(GPIO_OUT(pin));
150 return (val >> (pin & 31)) & 1;
153 void kw_gpio_set_value(unsigned pin, int value)
155 /* Configure GPIO output value. */
156 __set_level(pin, value);
159 void kw_gpio_set_blink(unsigned pin, int blink)
161 /* Set output value to zero. */
165 __set_blinking(pin, blink);