sunxi_gpio.c 5.6 KB

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