omap_gpio.c 7.7 KB

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