sunxi_gpio.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. #include <common.h>
  13. #include <dm.h>
  14. #include <errno.h>
  15. #include <fdtdec.h>
  16. #include <malloc.h>
  17. #include <asm/arch/gpio.h>
  18. #include <asm/io.h>
  19. #include <asm/gpio.h>
  20. #include <dm/device-internal.h>
  21. DECLARE_GLOBAL_DATA_PTR;
  22. #define SUNXI_GPIOS_PER_BANK SUNXI_GPIO_A_NR
  23. struct sunxi_gpio_platdata {
  24. struct sunxi_gpio *regs;
  25. const char *bank_name; /* Name of bank, e.g. "B" */
  26. int gpio_count;
  27. };
  28. #ifndef CONFIG_DM_GPIO
  29. static int sunxi_gpio_output(u32 pin, u32 val)
  30. {
  31. u32 dat;
  32. u32 bank = GPIO_BANK(pin);
  33. u32 num = GPIO_NUM(pin);
  34. struct sunxi_gpio *pio = BANK_TO_GPIO(bank);
  35. dat = readl(&pio->dat);
  36. if (val)
  37. dat |= 0x1 << num;
  38. else
  39. dat &= ~(0x1 << num);
  40. writel(dat, &pio->dat);
  41. return 0;
  42. }
  43. static int sunxi_gpio_input(u32 pin)
  44. {
  45. u32 dat;
  46. u32 bank = GPIO_BANK(pin);
  47. u32 num = GPIO_NUM(pin);
  48. struct sunxi_gpio *pio = BANK_TO_GPIO(bank);
  49. dat = readl(&pio->dat);
  50. dat >>= num;
  51. return dat & 0x1;
  52. }
  53. int gpio_request(unsigned gpio, const char *label)
  54. {
  55. return 0;
  56. }
  57. int gpio_free(unsigned gpio)
  58. {
  59. return 0;
  60. }
  61. int gpio_direction_input(unsigned gpio)
  62. {
  63. sunxi_gpio_set_cfgpin(gpio, SUNXI_GPIO_INPUT);
  64. return 0;
  65. }
  66. int gpio_direction_output(unsigned gpio, int value)
  67. {
  68. sunxi_gpio_set_cfgpin(gpio, SUNXI_GPIO_OUTPUT);
  69. return sunxi_gpio_output(gpio, value);
  70. }
  71. int gpio_get_value(unsigned gpio)
  72. {
  73. return sunxi_gpio_input(gpio);
  74. }
  75. int gpio_set_value(unsigned gpio, int value)
  76. {
  77. return sunxi_gpio_output(gpio, value);
  78. }
  79. int sunxi_name_to_gpio(const char *name)
  80. {
  81. int group = 0;
  82. int groupsize = 9 * 32;
  83. long pin;
  84. char *eptr;
  85. if (*name == 'P' || *name == 'p')
  86. name++;
  87. if (*name >= 'A') {
  88. group = *name - (*name > 'a' ? 'a' : 'A');
  89. groupsize = 32;
  90. name++;
  91. }
  92. pin = simple_strtol(name, &eptr, 10);
  93. if (!*name || *eptr)
  94. return -1;
  95. if (pin < 0 || pin > groupsize || group >= 9)
  96. return -1;
  97. return group * 32 + pin;
  98. }
  99. #endif
  100. int sunxi_name_to_gpio_bank(const char *name)
  101. {
  102. int group = 0;
  103. if (*name == 'P' || *name == 'p')
  104. name++;
  105. if (*name >= 'A') {
  106. group = *name - (*name > 'a' ? 'a' : 'A');
  107. return group;
  108. }
  109. return -1;
  110. }
  111. #ifdef CONFIG_DM_GPIO
  112. /* TODO(sjg@chromium.org): Remove this function and use device tree */
  113. int sunxi_name_to_gpio(const char *name)
  114. {
  115. unsigned int gpio;
  116. int ret;
  117. #if !defined CONFIG_SPL_BUILD && defined CONFIG_AXP_GPIO
  118. char lookup[8];
  119. if (strcasecmp(name, "AXP0-VBUS-DETECT") == 0) {
  120. sprintf(lookup, SUNXI_GPIO_AXP0_PREFIX "%d",
  121. SUNXI_GPIO_AXP0_VBUS_DETECT);
  122. name = lookup;
  123. } else if (strcasecmp(name, "AXP0-VBUS-ENABLE") == 0) {
  124. sprintf(lookup, SUNXI_GPIO_AXP0_PREFIX "%d",
  125. SUNXI_GPIO_AXP0_VBUS_ENABLE);
  126. name = lookup;
  127. }
  128. #endif
  129. ret = gpio_lookup_name(name, NULL, NULL, &gpio);
  130. return ret ? ret : gpio;
  131. }
  132. static int sunxi_gpio_direction_input(struct udevice *dev, unsigned offset)
  133. {
  134. struct sunxi_gpio_platdata *plat = dev_get_platdata(dev);
  135. sunxi_gpio_set_cfgbank(plat->regs, offset, SUNXI_GPIO_INPUT);
  136. return 0;
  137. }
  138. static int sunxi_gpio_direction_output(struct udevice *dev, unsigned offset,
  139. int value)
  140. {
  141. struct sunxi_gpio_platdata *plat = dev_get_platdata(dev);
  142. u32 num = GPIO_NUM(offset);
  143. sunxi_gpio_set_cfgbank(plat->regs, offset, SUNXI_GPIO_OUTPUT);
  144. clrsetbits_le32(&plat->regs->dat, 1 << num, value ? (1 << num) : 0);
  145. return 0;
  146. }
  147. static int sunxi_gpio_get_value(struct udevice *dev, unsigned offset)
  148. {
  149. struct sunxi_gpio_platdata *plat = dev_get_platdata(dev);
  150. u32 num = GPIO_NUM(offset);
  151. unsigned dat;
  152. dat = readl(&plat->regs->dat);
  153. dat >>= num;
  154. return dat & 0x1;
  155. }
  156. static int sunxi_gpio_set_value(struct udevice *dev, unsigned offset,
  157. int value)
  158. {
  159. struct sunxi_gpio_platdata *plat = dev_get_platdata(dev);
  160. u32 num = GPIO_NUM(offset);
  161. clrsetbits_le32(&plat->regs->dat, 1 << num, value ? (1 << num) : 0);
  162. return 0;
  163. }
  164. static int sunxi_gpio_get_function(struct udevice *dev, unsigned offset)
  165. {
  166. struct sunxi_gpio_platdata *plat = dev_get_platdata(dev);
  167. int func;
  168. func = sunxi_gpio_get_cfgbank(plat->regs, offset);
  169. if (func == SUNXI_GPIO_OUTPUT)
  170. return GPIOF_OUTPUT;
  171. else if (func == SUNXI_GPIO_INPUT)
  172. return GPIOF_INPUT;
  173. else
  174. return GPIOF_FUNC;
  175. }
  176. static const struct dm_gpio_ops gpio_sunxi_ops = {
  177. .direction_input = sunxi_gpio_direction_input,
  178. .direction_output = sunxi_gpio_direction_output,
  179. .get_value = sunxi_gpio_get_value,
  180. .set_value = sunxi_gpio_set_value,
  181. .get_function = sunxi_gpio_get_function,
  182. };
  183. /**
  184. * Returns the name of a GPIO bank
  185. *
  186. * GPIO banks are named A, B, C, ...
  187. *
  188. * @bank: Bank number (0, 1..n-1)
  189. * @return allocated string containing the name
  190. */
  191. static char *gpio_bank_name(int bank)
  192. {
  193. char *name;
  194. name = malloc(3);
  195. if (name) {
  196. name[0] = 'P';
  197. name[1] = 'A' + bank;
  198. name[2] = '\0';
  199. }
  200. return name;
  201. }
  202. static int gpio_sunxi_probe(struct udevice *dev)
  203. {
  204. struct sunxi_gpio_platdata *plat = dev_get_platdata(dev);
  205. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  206. /* Tell the uclass how many GPIOs we have */
  207. if (plat) {
  208. uc_priv->gpio_count = plat->gpio_count;
  209. uc_priv->bank_name = plat->bank_name;
  210. }
  211. return 0;
  212. }
  213. /**
  214. * We have a top-level GPIO device with no actual GPIOs. It has a child
  215. * device for each Sunxi bank.
  216. */
  217. static int gpio_sunxi_bind(struct udevice *parent)
  218. {
  219. struct sunxi_gpio_platdata *plat = parent->platdata;
  220. struct sunxi_gpio_reg *ctlr;
  221. int bank;
  222. int ret;
  223. /* If this is a child device, there is nothing to do here */
  224. if (plat)
  225. return 0;
  226. ctlr = (struct sunxi_gpio_reg *)fdtdec_get_addr(gd->fdt_blob,
  227. parent->of_offset, "reg");
  228. for (bank = 0; bank < SUNXI_GPIO_BANKS; bank++) {
  229. struct sunxi_gpio_platdata *plat;
  230. struct udevice *dev;
  231. plat = calloc(1, sizeof(*plat));
  232. if (!plat)
  233. return -ENOMEM;
  234. plat->regs = &ctlr->gpio_bank[bank];
  235. plat->bank_name = gpio_bank_name(bank);
  236. plat->gpio_count = SUNXI_GPIOS_PER_BANK;
  237. ret = device_bind(parent, parent->driver,
  238. plat->bank_name, plat, -1, &dev);
  239. if (ret)
  240. return ret;
  241. dev->of_offset = parent->of_offset;
  242. }
  243. return 0;
  244. }
  245. static const struct udevice_id sunxi_gpio_ids[] = {
  246. { .compatible = "allwinner,sun4i-a10-pinctrl" },
  247. { .compatible = "allwinner,sun5i-a10s-pinctrl" },
  248. { .compatible = "allwinner,sun5i-a13-pinctrl" },
  249. { .compatible = "allwinner,sun6i-a31-pinctrl" },
  250. { .compatible = "allwinner,sun6i-a31s-pinctrl" },
  251. { .compatible = "allwinner,sun7i-a20-pinctrl" },
  252. { .compatible = "allwinner,sun8i-a23-pinctrl" },
  253. { .compatible = "allwinner,sun8i-a33-pinctrl" },
  254. { .compatible = "allwinner,sun9i-a80-pinctrl" },
  255. { }
  256. };
  257. U_BOOT_DRIVER(gpio_sunxi) = {
  258. .name = "gpio_sunxi",
  259. .id = UCLASS_GPIO,
  260. .ops = &gpio_sunxi_ops,
  261. .of_match = sunxi_gpio_ids,
  262. .bind = gpio_sunxi_bind,
  263. .probe = gpio_sunxi_probe,
  264. };
  265. #endif