1 /* 2 * (C) Copyright 2012 Henrik Nordstrom <henrik@henriknordstrom.net> 3 * 4 * Based on earlier arch/arm/cpu/armv7/sunxi/gpio.c: 5 * 6 * (C) Copyright 2007-2011 7 * Allwinner Technology Co., Ltd. <www.allwinnertech.com> 8 * Tom Cubie <tangliang@allwinnertech.com> 9 * 10 * SPDX-License-Identifier: GPL-2.0+ 11 */ 12 13 #include <common.h> 14 #include <asm/io.h> 15 #include <asm/gpio.h> 16 17 static int sunxi_gpio_output(u32 pin, u32 val) 18 { 19 u32 dat; 20 u32 bank = GPIO_BANK(pin); 21 u32 num = GPIO_NUM(pin); 22 struct sunxi_gpio *pio = BANK_TO_GPIO(bank); 23 24 dat = readl(&pio->dat); 25 if (val) 26 dat |= 0x1 << num; 27 else 28 dat &= ~(0x1 << num); 29 30 writel(dat, &pio->dat); 31 32 return 0; 33 } 34 35 static int sunxi_gpio_input(u32 pin) 36 { 37 u32 dat; 38 u32 bank = GPIO_BANK(pin); 39 u32 num = GPIO_NUM(pin); 40 struct sunxi_gpio *pio = BANK_TO_GPIO(bank); 41 42 dat = readl(&pio->dat); 43 dat >>= num; 44 45 return dat & 0x1; 46 } 47 48 int gpio_request(unsigned gpio, const char *label) 49 { 50 return 0; 51 } 52 53 int gpio_free(unsigned gpio) 54 { 55 return 0; 56 } 57 58 int gpio_direction_input(unsigned gpio) 59 { 60 sunxi_gpio_set_cfgpin(gpio, SUNXI_GPIO_INPUT); 61 62 return sunxi_gpio_input(gpio); 63 } 64 65 int gpio_direction_output(unsigned gpio, int value) 66 { 67 sunxi_gpio_set_cfgpin(gpio, SUNXI_GPIO_OUTPUT); 68 69 return sunxi_gpio_output(gpio, value); 70 } 71 72 int gpio_get_value(unsigned gpio) 73 { 74 return sunxi_gpio_input(gpio); 75 } 76 77 int gpio_set_value(unsigned gpio, int value) 78 { 79 return sunxi_gpio_output(gpio, value); 80 } 81 82 int sunxi_name_to_gpio(const char *name) 83 { 84 int group = 0; 85 int groupsize = 9 * 32; 86 long pin; 87 char *eptr; 88 if (*name == 'P' || *name == 'p') 89 name++; 90 if (*name >= 'A') { 91 group = *name - (*name > 'a' ? 'a' : 'A'); 92 groupsize = 32; 93 name++; 94 } 95 96 pin = simple_strtol(name, &eptr, 10); 97 if (!*name || *eptr) 98 return -1; 99 if (pin < 0 || pin > groupsize || group >= 9) 100 return -1; 101 return group * 32 + pin; 102 } 103