sunxi_gpio.c 6.8 KB

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