2 * Copyright (C) ST-Ericsson SA 2009
4 * See file CREDITS for list of people who contributed to this
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 #include <asm/arch/gpio.h>
26 static struct gpio_register *addr_gpio_register[] = {
27 (void *)U8500_GPIO_0_BASE,
28 (void *)U8500_GPIO_1_BASE,
29 (void *)U8500_GPIO_2_BASE,
30 (void *)U8500_GPIO_3_BASE,
31 (void *)U8500_GPIO_4_BASE,
32 (void *)U8500_GPIO_5_BASE,
33 (void *)U8500_GPIO_6_BASE,
34 (void *)U8500_GPIO_7_BASE,
35 (void *)U8500_GPIO_8_BASE,
38 struct gpio_altfun_data altfun_table[] = {
40 .altfun = GPIO_ALT_I2C_0,
47 .altfun = GPIO_ALT_I2C_1,
54 .altfun = GPIO_ALT_I2C_2,
61 .altfun = GPIO_ALT_I2C_3,
68 .altfun = GPIO_ALT_UART_0_MODEM,
75 .altfun = GPIO_ALT_UART_0_MODEM,
82 .altfun = GPIO_ALT_UART_1,
90 .altfun = GPIO_ALT_UART_2,
97 .altfun = GPIO_ALT_UART_2,
104 .altfun = GPIO_ALT_MSP_0,
111 .altfun = GPIO_ALT_MSP_0,
118 .altfun = GPIO_ALT_MSP_1,
125 .altfun = GPIO_ALT_MSP_2,
132 .altfun = GPIO_ALT_LCD_PANEL,
139 .altfun = GPIO_ALT_LCD_PANEL,
146 .altfun = GPIO_ALT_SD_CARD0,
153 .altfun = GPIO_ALT_MM_CARD0,
160 .altfun = GPIO_ALT_USB_OTG,
167 .altfun = GPIO_ALT_EMMC,
174 .altfun = GPIO_ALT_POP_EMMC,
183 * Static Function declarations
185 enum gpio_error gpio_setpinconfig(int pin_id, struct gpio_config *config)
187 struct gpio_register *p_gpio_register =
188 addr_gpio_register[GPIO_BLOCK(pin_id)];
189 u32 mask = 1UL << (pin_id % GPIO_PINS_PER_BLOCK);
190 enum gpio_error error = GPIO_OK;
193 switch (config->mode) {
195 temp_reg = readl(&p_gpio_register->gpio_afsa);
197 writel(temp_reg, &p_gpio_register->gpio_afsa);
198 temp_reg = readl(&p_gpio_register->gpio_afsb);
200 writel(temp_reg, &p_gpio_register->gpio_afsb);
203 temp_reg = readl(&p_gpio_register->gpio_afsa);
205 writel(temp_reg, &p_gpio_register->gpio_afsa);
206 temp_reg = readl(&p_gpio_register->gpio_afsb);
208 writel(temp_reg, &p_gpio_register->gpio_afsb);
211 temp_reg = readl(&p_gpio_register->gpio_afsa);
213 writel(temp_reg, &p_gpio_register->gpio_afsa);
214 temp_reg = readl(&p_gpio_register->gpio_afsb);
216 writel(temp_reg, &p_gpio_register->gpio_afsb);
218 case GPIO_MODE_SOFTWARE:
219 temp_reg = readl(&p_gpio_register->gpio_afsa);
221 writel(temp_reg, &p_gpio_register->gpio_afsa);
222 temp_reg = readl(&p_gpio_register->gpio_afsb);
224 writel(temp_reg, &p_gpio_register->gpio_afsb);
226 switch (config->direction) {
228 writel(mask, &p_gpio_register->gpio_dirc);
230 case GPIO_DIR_OUTPUT:
231 writel(mask, &p_gpio_register->gpio_dirs);
233 case GPIO_DIR_LEAVE_UNCHANGED:
236 return GPIO_INVALID_PARAMETER;
240 case GPIO_MODE_LEAVE_UNCHANGED:
243 return GPIO_INVALID_PARAMETER;
248 enum gpio_error gpio_resetgpiopin(int pin_id, char *dev_name)
250 struct gpio_register *p_gpio_register =
251 addr_gpio_register[GPIO_BLOCK(pin_id)];
252 u32 mask = 1UL << (pin_id % GPIO_PINS_PER_BLOCK);
253 enum gpio_error error = GPIO_OK;
256 temp_reg = readl(&p_gpio_register->gpio_afsa);
258 writel(temp_reg, &p_gpio_register->gpio_afsa);
259 temp_reg = readl(&p_gpio_register->gpio_afsb);
261 writel(temp_reg, &p_gpio_register->gpio_afsb);
262 writel(mask, &p_gpio_register->gpio_dirc);
267 struct gpio_config altfun_pinconfig;
268 enum gpio_error gpio_altfunction(enum gpio_alt_function alt_func,
269 int which_altfunc, char *dev_name)
271 int i, j, start, end;
272 enum gpio_error error = -1;
274 for (i = 0; i < ARRAY_SIZE(altfun_table); i++) {
275 if (altfun_table[i].altfun != alt_func)
278 start = altfun_table[i].start;
279 end = altfun_table[i].end;
280 for (j = start; j <= end; j++) {
281 if (which_altfunc == GPIO_ALTF_FIND)
282 altfun_pinconfig.mode = altfun_table[i].type;
284 altfun_pinconfig.mode = which_altfunc;
285 altfun_pinconfig.direction = GPIO_DIR_OUTPUT;
286 altfun_pinconfig.dev_name = dev_name;
288 if (which_altfunc != GPIO_ALTF_DISABLE)
289 error = gpio_setpinconfig(j, &altfun_pinconfig);
291 error = gpio_resetgpiopin(j, dev_name);
294 printf("GPIO %d configuration failure (nmdk_error:%d)",
296 error = GPIO_INVALID_PARAMETER;
300 if (!altfun_table[i].cont)
306 int gpio_writepin(int pin_id, enum gpio_data value, char *dev_name)
308 struct gpio_register *p_gpio_register =
309 addr_gpio_register[GPIO_BLOCK(pin_id)];
310 u32 mask = 1UL << (pin_id % GPIO_PINS_PER_BLOCK);
314 writel(mask, &p_gpio_register->gpio_dats);
317 writel(mask, &p_gpio_register->gpio_datc);
320 printf("Invalid value passed in %s", __FUNCTION__);
321 return GPIO_INVALID_PARAMETER;
326 int gpio_readpin(int pin_id, enum gpio_data *rv)
328 struct gpio_register *p_gpio_register =
329 addr_gpio_register[GPIO_BLOCK(pin_id)];
330 u32 mask = 1UL << (pin_id % GPIO_PINS_PER_BLOCK);
332 if ((readl(&p_gpio_register->gpio_dat) & mask) != 0)
333 *rv = GPIO_DATA_HIGH;
339 int gpio_altfuncenable(enum gpio_alt_function altfunc, char *dev_name)
341 return (int)gpio_altfunction(altfunc, GPIO_ALTF_FIND, dev_name);
344 int gpio_altfuncdisable(enum gpio_alt_function altfunc, char *dev_name)
346 return (int)gpio_altfunction(altfunc, GPIO_ALTF_DISABLE, dev_name);