omap_gpio.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * Copyright (c) 2009 Wind River Systems, Inc.
  3. * Tom Rix <Tom.Rix@windriver.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0
  6. *
  7. * This work is derived from the linux 2.6.27 kernel source
  8. * To fetch, use the kernel repository
  9. * git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git
  10. * Use the v2.6.27 tag.
  11. *
  12. * Below is the original's header including its copyright
  13. *
  14. * linux/arch/arm/plat-omap/gpio.c
  15. *
  16. * Support functions for OMAP GPIO
  17. *
  18. * Copyright (C) 2003-2005 Nokia Corporation
  19. * Written by Juha Yrjölä <juha.yrjola@nokia.com>
  20. */
  21. #include <common.h>
  22. #include <dm.h>
  23. #include <fdtdec.h>
  24. #include <asm/gpio.h>
  25. #include <asm/io.h>
  26. #include <asm/errno.h>
  27. #include <malloc.h>
  28. #include <dt-bindings/gpio/gpio.h>
  29. DECLARE_GLOBAL_DATA_PTR;
  30. #define OMAP_GPIO_DIR_OUT 0
  31. #define OMAP_GPIO_DIR_IN 1
  32. #ifdef CONFIG_DM_GPIO
  33. #define GPIO_PER_BANK 32
  34. struct gpio_bank {
  35. /* TODO(sjg@chromium.org): Can we use a struct here? */
  36. void *base; /* address of registers in physical memory */
  37. };
  38. #endif
  39. static inline int get_gpio_index(int gpio)
  40. {
  41. return gpio & 0x1f;
  42. }
  43. int gpio_is_valid(int gpio)
  44. {
  45. return (gpio >= 0) && (gpio < OMAP_MAX_GPIO);
  46. }
  47. static void _set_gpio_direction(const struct gpio_bank *bank, int gpio,
  48. int is_input)
  49. {
  50. void *reg = bank->base;
  51. u32 l;
  52. reg += OMAP_GPIO_OE;
  53. l = __raw_readl(reg);
  54. if (is_input)
  55. l |= 1 << gpio;
  56. else
  57. l &= ~(1 << gpio);
  58. __raw_writel(l, reg);
  59. }
  60. /**
  61. * Get the direction of the GPIO by reading the GPIO_OE register
  62. * corresponding to the specified bank.
  63. */
  64. static int _get_gpio_direction(const struct gpio_bank *bank, int gpio)
  65. {
  66. void *reg = bank->base;
  67. u32 v;
  68. reg += OMAP_GPIO_OE;
  69. v = __raw_readl(reg);
  70. if (v & (1 << gpio))
  71. return OMAP_GPIO_DIR_IN;
  72. else
  73. return OMAP_GPIO_DIR_OUT;
  74. }
  75. static void _set_gpio_dataout(const struct gpio_bank *bank, int gpio,
  76. int enable)
  77. {
  78. void *reg = bank->base;
  79. u32 l = 0;
  80. if (enable)
  81. reg += OMAP_GPIO_SETDATAOUT;
  82. else
  83. reg += OMAP_GPIO_CLEARDATAOUT;
  84. l = 1 << gpio;
  85. __raw_writel(l, reg);
  86. }
  87. static int _get_gpio_value(const struct gpio_bank *bank, int gpio)
  88. {
  89. void *reg = bank->base;
  90. int input;
  91. input = _get_gpio_direction(bank, gpio);
  92. switch (input) {
  93. case OMAP_GPIO_DIR_IN:
  94. reg += OMAP_GPIO_DATAIN;
  95. break;
  96. case OMAP_GPIO_DIR_OUT:
  97. reg += OMAP_GPIO_DATAOUT;
  98. break;
  99. default:
  100. return -1;
  101. }
  102. return (__raw_readl(reg) & (1 << gpio)) != 0;
  103. }
  104. #ifndef CONFIG_DM_GPIO
  105. static inline const struct gpio_bank *get_gpio_bank(int gpio)
  106. {
  107. return &omap_gpio_bank[gpio >> 5];
  108. }
  109. static int check_gpio(int gpio)
  110. {
  111. if (!gpio_is_valid(gpio)) {
  112. printf("ERROR : check_gpio: invalid GPIO %d\n", gpio);
  113. return -1;
  114. }
  115. return 0;
  116. }
  117. /**
  118. * Set value of the specified gpio
  119. */
  120. int gpio_set_value(unsigned gpio, int value)
  121. {
  122. const struct gpio_bank *bank;
  123. if (check_gpio(gpio) < 0)
  124. return -1;
  125. bank = get_gpio_bank(gpio);
  126. _set_gpio_dataout(bank, get_gpio_index(gpio), value);
  127. return 0;
  128. }
  129. /**
  130. * Get value of the specified gpio
  131. */
  132. int gpio_get_value(unsigned gpio)
  133. {
  134. const struct gpio_bank *bank;
  135. if (check_gpio(gpio) < 0)
  136. return -1;
  137. bank = get_gpio_bank(gpio);
  138. return _get_gpio_value(bank, get_gpio_index(gpio));
  139. }
  140. /**
  141. * Set gpio direction as input
  142. */
  143. int gpio_direction_input(unsigned gpio)
  144. {
  145. const struct gpio_bank *bank;
  146. if (check_gpio(gpio) < 0)
  147. return -1;
  148. bank = get_gpio_bank(gpio);
  149. _set_gpio_direction(bank, get_gpio_index(gpio), 1);
  150. return 0;
  151. }
  152. /**
  153. * Set gpio direction as output
  154. */
  155. int gpio_direction_output(unsigned gpio, int value)
  156. {
  157. const struct gpio_bank *bank;
  158. if (check_gpio(gpio) < 0)
  159. return -1;
  160. bank = get_gpio_bank(gpio);
  161. _set_gpio_dataout(bank, get_gpio_index(gpio), value);
  162. _set_gpio_direction(bank, get_gpio_index(gpio), 0);
  163. return 0;
  164. }
  165. /**
  166. * Request a gpio before using it.
  167. *
  168. * NOTE: Argument 'label' is unused.
  169. */
  170. int gpio_request(unsigned gpio, const char *label)
  171. {
  172. if (check_gpio(gpio) < 0)
  173. return -1;
  174. return 0;
  175. }
  176. /**
  177. * Reset and free the gpio after using it.
  178. */
  179. int gpio_free(unsigned gpio)
  180. {
  181. return 0;
  182. }
  183. #else /* new driver model interface CONFIG_DM_GPIO */
  184. /* set GPIO pin 'gpio' as an input */
  185. static int omap_gpio_direction_input(struct udevice *dev, unsigned offset)
  186. {
  187. struct gpio_bank *bank = dev_get_priv(dev);
  188. /* Configure GPIO direction as input. */
  189. _set_gpio_direction(bank, offset, 1);
  190. return 0;
  191. }
  192. /* set GPIO pin 'gpio' as an output, with polarity 'value' */
  193. static int omap_gpio_direction_output(struct udevice *dev, unsigned offset,
  194. int value)
  195. {
  196. struct gpio_bank *bank = dev_get_priv(dev);
  197. _set_gpio_dataout(bank, offset, value);
  198. _set_gpio_direction(bank, offset, 0);
  199. return 0;
  200. }
  201. /* read GPIO IN value of pin 'gpio' */
  202. static int omap_gpio_get_value(struct udevice *dev, unsigned offset)
  203. {
  204. struct gpio_bank *bank = dev_get_priv(dev);
  205. return _get_gpio_value(bank, offset);
  206. }
  207. /* write GPIO OUT value to pin 'gpio' */
  208. static int omap_gpio_set_value(struct udevice *dev, unsigned offset,
  209. int value)
  210. {
  211. struct gpio_bank *bank = dev_get_priv(dev);
  212. _set_gpio_dataout(bank, offset, value);
  213. return 0;
  214. }
  215. static int omap_gpio_get_function(struct udevice *dev, unsigned offset)
  216. {
  217. struct gpio_bank *bank = dev_get_priv(dev);
  218. /* GPIOF_FUNC is not implemented yet */
  219. if (_get_gpio_direction(bank, offset) == OMAP_GPIO_DIR_OUT)
  220. return GPIOF_OUTPUT;
  221. else
  222. return GPIOF_INPUT;
  223. }
  224. static int omap_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
  225. struct fdtdec_phandle_args *args)
  226. {
  227. desc->offset = args->args[0];
  228. desc->flags = args->args[1] & GPIO_ACTIVE_LOW ? GPIOD_ACTIVE_LOW : 0;
  229. return 0;
  230. }
  231. static const struct dm_gpio_ops gpio_omap_ops = {
  232. .direction_input = omap_gpio_direction_input,
  233. .direction_output = omap_gpio_direction_output,
  234. .get_value = omap_gpio_get_value,
  235. .set_value = omap_gpio_set_value,
  236. .get_function = omap_gpio_get_function,
  237. .xlate = omap_gpio_xlate,
  238. };
  239. static int omap_gpio_probe(struct udevice *dev)
  240. {
  241. struct gpio_bank *bank = dev_get_priv(dev);
  242. struct omap_gpio_platdata *plat = dev_get_platdata(dev);
  243. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  244. uc_priv->bank_name = plat->port_name;
  245. uc_priv->gpio_count = GPIO_PER_BANK;
  246. bank->base = (void *)plat->base;
  247. return 0;
  248. }
  249. static int omap_gpio_bind(struct udevice *dev)
  250. {
  251. struct omap_gpio_platdata *plat = dev->platdata;
  252. fdt_addr_t base_addr;
  253. if (plat)
  254. return 0;
  255. base_addr = dev_get_addr(dev);
  256. if (base_addr == FDT_ADDR_T_NONE)
  257. return -ENODEV;
  258. /*
  259. * TODO:
  260. * When every board is converted to driver model and DT is
  261. * supported, this can be done by auto-alloc feature, but
  262. * not using calloc to alloc memory for platdata.
  263. */
  264. plat = calloc(1, sizeof(*plat));
  265. if (!plat)
  266. return -ENOMEM;
  267. plat->base = base_addr;
  268. plat->port_name = fdt_get_name(gd->fdt_blob, dev->of_offset, NULL);
  269. dev->platdata = plat;
  270. return 0;
  271. }
  272. static const struct udevice_id omap_gpio_ids[] = {
  273. { .compatible = "ti,omap3-gpio" },
  274. { .compatible = "ti,omap4-gpio" },
  275. { .compatible = "ti,am4372-gpio" },
  276. { }
  277. };
  278. U_BOOT_DRIVER(gpio_omap) = {
  279. .name = "gpio_omap",
  280. .id = UCLASS_GPIO,
  281. .ops = &gpio_omap_ops,
  282. .of_match = omap_gpio_ids,
  283. .bind = omap_gpio_bind,
  284. .probe = omap_gpio_probe,
  285. .priv_auto_alloc_size = sizeof(struct gpio_bank),
  286. };
  287. #endif /* CONFIG_DM_GPIO */