sunxi_gpio.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. #if !defined CONFIG_SPL_BUILD && defined CONFIG_AXP_GPIO
  64. if (gpio >= SUNXI_GPIO_AXP0_START)
  65. return axp_gpio_direction_input(NULL, gpio - SUNXI_GPIO_AXP0_START);
  66. #endif
  67. sunxi_gpio_set_cfgpin(gpio, SUNXI_GPIO_INPUT);
  68. return 0;
  69. }
  70. int gpio_direction_output(unsigned gpio, int value)
  71. {
  72. #if !defined CONFIG_SPL_BUILD && defined CONFIG_AXP_GPIO
  73. if (gpio >= SUNXI_GPIO_AXP0_START)
  74. return axp_gpio_direction_output(NULL, gpio - SUNXI_GPIO_AXP0_START,
  75. value);
  76. #endif
  77. sunxi_gpio_set_cfgpin(gpio, SUNXI_GPIO_OUTPUT);
  78. return sunxi_gpio_output(gpio, value);
  79. }
  80. int gpio_get_value(unsigned gpio)
  81. {
  82. #if !defined CONFIG_SPL_BUILD && defined CONFIG_AXP_GPIO
  83. if (gpio >= SUNXI_GPIO_AXP0_START)
  84. return axp_gpio_get_value(NULL, gpio - SUNXI_GPIO_AXP0_START);
  85. #endif
  86. return sunxi_gpio_input(gpio);
  87. }
  88. int gpio_set_value(unsigned gpio, int value)
  89. {
  90. #if !defined CONFIG_SPL_BUILD && defined CONFIG_AXP_GPIO
  91. if (gpio >= SUNXI_GPIO_AXP0_START)
  92. return axp_gpio_set_value(NULL, gpio - SUNXI_GPIO_AXP0_START, value);
  93. #endif
  94. return sunxi_gpio_output(gpio, value);
  95. }
  96. int sunxi_name_to_gpio(const char *name)
  97. {
  98. int group = 0;
  99. int groupsize = 9 * 32;
  100. long pin;
  101. char *eptr;
  102. #if !defined CONFIG_SPL_BUILD && defined CONFIG_AXP_GPIO
  103. if (strncasecmp(name, "AXP0-", 5) == 0) {
  104. name += 5;
  105. if (strcmp(name, "VBUS-DETECT") == 0)
  106. return SUNXI_GPIO_AXP0_START +
  107. SUNXI_GPIO_AXP0_VBUS_DETECT;
  108. if (strcmp(name, "VBUS-ENABLE") == 0)
  109. return SUNXI_GPIO_AXP0_START +
  110. SUNXI_GPIO_AXP0_VBUS_ENABLE;
  111. pin = simple_strtol(name, &eptr, 10);
  112. if (!*name || *eptr)
  113. return -1;
  114. return SUNXI_GPIO_AXP0_START + pin;
  115. }
  116. #endif
  117. if (*name == 'P' || *name == 'p')
  118. name++;
  119. if (*name >= 'A') {
  120. group = *name - (*name > 'a' ? 'a' : 'A');
  121. groupsize = 32;
  122. name++;
  123. }
  124. pin = simple_strtol(name, &eptr, 10);
  125. if (!*name || *eptr)
  126. return -1;
  127. if (pin < 0 || pin > groupsize || group >= 9)
  128. return -1;
  129. return group * 32 + pin;
  130. }
  131. #endif
  132. int sunxi_name_to_gpio_bank(const char *name)
  133. {
  134. int group = 0;
  135. if (*name == 'P' || *name == 'p')
  136. name++;
  137. if (*name >= 'A') {
  138. group = *name - (*name > 'a' ? 'a' : 'A');
  139. return group;
  140. }
  141. return -1;
  142. }
  143. #ifdef CONFIG_DM_GPIO
  144. /* TODO(sjg@chromium.org): Remove this function and use device tree */
  145. int sunxi_name_to_gpio(const char *name)
  146. {
  147. unsigned int gpio;
  148. int ret;
  149. ret = gpio_lookup_name(name, NULL, NULL, &gpio);
  150. return ret ? ret : gpio;
  151. }
  152. static int sunxi_gpio_direction_input(struct udevice *dev, unsigned offset)
  153. {
  154. struct sunxi_gpio_platdata *plat = dev_get_platdata(dev);
  155. sunxi_gpio_set_cfgbank(plat->regs, offset, SUNXI_GPIO_INPUT);
  156. return 0;
  157. }
  158. static int sunxi_gpio_direction_output(struct udevice *dev, unsigned offset,
  159. int value)
  160. {
  161. struct sunxi_gpio_platdata *plat = dev_get_platdata(dev);
  162. u32 num = GPIO_NUM(offset);
  163. sunxi_gpio_set_cfgbank(plat->regs, offset, SUNXI_GPIO_OUTPUT);
  164. clrsetbits_le32(&plat->regs->dat, 1 << num, value ? (1 << num) : 0);
  165. return 0;
  166. }
  167. static int sunxi_gpio_get_value(struct udevice *dev, unsigned offset)
  168. {
  169. struct sunxi_gpio_platdata *plat = dev_get_platdata(dev);
  170. u32 num = GPIO_NUM(offset);
  171. unsigned dat;
  172. dat = readl(&plat->regs->dat);
  173. dat >>= num;
  174. return dat & 0x1;
  175. }
  176. static int sunxi_gpio_set_value(struct udevice *dev, unsigned offset,
  177. int value)
  178. {
  179. struct sunxi_gpio_platdata *plat = dev_get_platdata(dev);
  180. u32 num = GPIO_NUM(offset);
  181. clrsetbits_le32(&plat->regs->dat, 1 << num, value ? (1 << num) : 0);
  182. return 0;
  183. }
  184. static int sunxi_gpio_get_function(struct udevice *dev, unsigned offset)
  185. {
  186. struct sunxi_gpio_platdata *plat = dev_get_platdata(dev);
  187. int func;
  188. func = sunxi_gpio_get_cfgbank(plat->regs, offset);
  189. if (func == SUNXI_GPIO_OUTPUT)
  190. return GPIOF_OUTPUT;
  191. else if (func == SUNXI_GPIO_INPUT)
  192. return GPIOF_INPUT;
  193. else
  194. return GPIOF_FUNC;
  195. }
  196. static const struct dm_gpio_ops gpio_sunxi_ops = {
  197. .direction_input = sunxi_gpio_direction_input,
  198. .direction_output = sunxi_gpio_direction_output,
  199. .get_value = sunxi_gpio_get_value,
  200. .set_value = sunxi_gpio_set_value,
  201. .get_function = sunxi_gpio_get_function,
  202. };
  203. /**
  204. * Returns the name of a GPIO bank
  205. *
  206. * GPIO banks are named A, B, C, ...
  207. *
  208. * @bank: Bank number (0, 1..n-1)
  209. * @return allocated string containing the name
  210. */
  211. static char *gpio_bank_name(int bank)
  212. {
  213. char *name;
  214. name = malloc(3);
  215. if (name) {
  216. name[0] = 'P';
  217. name[1] = 'A' + bank;
  218. name[2] = '\0';
  219. }
  220. return name;
  221. }
  222. static int gpio_sunxi_probe(struct udevice *dev)
  223. {
  224. struct sunxi_gpio_platdata *plat = dev_get_platdata(dev);
  225. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  226. /* Tell the uclass how many GPIOs we have */
  227. if (plat) {
  228. uc_priv->gpio_count = plat->gpio_count;
  229. uc_priv->bank_name = plat->bank_name;
  230. }
  231. return 0;
  232. }
  233. /**
  234. * We have a top-level GPIO device with no actual GPIOs. It has a child
  235. * device for each Sunxi bank.
  236. */
  237. static int gpio_sunxi_bind(struct udevice *parent)
  238. {
  239. struct sunxi_gpio_platdata *plat = parent->platdata;
  240. struct sunxi_gpio_reg *ctlr;
  241. int bank;
  242. int ret;
  243. /* If this is a child device, there is nothing to do here */
  244. if (plat)
  245. return 0;
  246. ctlr = (struct sunxi_gpio_reg *)fdtdec_get_addr(gd->fdt_blob,
  247. parent->of_offset, "reg");
  248. for (bank = 0; bank < SUNXI_GPIO_BANKS; bank++) {
  249. struct sunxi_gpio_platdata *plat;
  250. struct udevice *dev;
  251. plat = calloc(1, sizeof(*plat));
  252. if (!plat)
  253. return -ENOMEM;
  254. plat->regs = &ctlr->gpio_bank[bank];
  255. plat->bank_name = gpio_bank_name(bank);
  256. plat->gpio_count = SUNXI_GPIOS_PER_BANK;
  257. ret = device_bind(parent, parent->driver,
  258. plat->bank_name, plat, -1, &dev);
  259. if (ret)
  260. return ret;
  261. dev->of_offset = parent->of_offset;
  262. }
  263. return 0;
  264. }
  265. static const struct udevice_id sunxi_gpio_ids[] = {
  266. { .compatible = "allwinner,sun4i-a10-pinctrl" },
  267. { .compatible = "allwinner,sun5i-a10s-pinctrl" },
  268. { .compatible = "allwinner,sun5i-a13-pinctrl" },
  269. { .compatible = "allwinner,sun6i-a31-pinctrl" },
  270. { .compatible = "allwinner,sun6i-a31s-pinctrl" },
  271. { .compatible = "allwinner,sun7i-a20-pinctrl" },
  272. { .compatible = "allwinner,sun8i-a23-pinctrl" },
  273. { .compatible = "allwinner,sun9i-a80-pinctrl" },
  274. { }
  275. };
  276. U_BOOT_DRIVER(gpio_sunxi) = {
  277. .name = "gpio_sunxi",
  278. .id = UCLASS_GPIO,
  279. .ops = &gpio_sunxi_ops,
  280. .of_match = sunxi_gpio_ids,
  281. .bind = gpio_sunxi_bind,
  282. .probe = gpio_sunxi_probe,
  283. };
  284. #endif