omap_gpio.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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/global_data.h>
  24. #include <asm/gpio.h>
  25. #include <asm/io.h>
  26. #include <dm/device-internal.h>
  27. #include <linux/errno.h>
  28. #include <malloc.h>
  29. DECLARE_GLOBAL_DATA_PTR;
  30. #define OMAP_GPIO_DIR_OUT 0
  31. #define OMAP_GPIO_DIR_IN 1
  32. #if CONFIG_IS_ENABLED(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. int gpio_is_valid(int gpio)
  40. {
  41. return (gpio >= 0) && (gpio < OMAP_MAX_GPIO);
  42. }
  43. static void _set_gpio_direction(const struct gpio_bank *bank, int gpio,
  44. int is_input)
  45. {
  46. void *reg = bank->base;
  47. u32 l;
  48. reg += OMAP_GPIO_OE;
  49. l = __raw_readl(reg);
  50. if (is_input)
  51. l |= 1 << gpio;
  52. else
  53. l &= ~(1 << gpio);
  54. __raw_writel(l, reg);
  55. }
  56. /**
  57. * Get the direction of the GPIO by reading the GPIO_OE register
  58. * corresponding to the specified bank.
  59. */
  60. static int _get_gpio_direction(const struct gpio_bank *bank, int gpio)
  61. {
  62. void *reg = bank->base;
  63. u32 v;
  64. reg += OMAP_GPIO_OE;
  65. v = __raw_readl(reg);
  66. if (v & (1 << gpio))
  67. return OMAP_GPIO_DIR_IN;
  68. else
  69. return OMAP_GPIO_DIR_OUT;
  70. }
  71. static void _set_gpio_dataout(const struct gpio_bank *bank, int gpio,
  72. int enable)
  73. {
  74. void *reg = bank->base;
  75. u32 l = 0;
  76. if (enable)
  77. reg += OMAP_GPIO_SETDATAOUT;
  78. else
  79. reg += OMAP_GPIO_CLEARDATAOUT;
  80. l = 1 << gpio;
  81. __raw_writel(l, reg);
  82. }
  83. static int _get_gpio_value(const struct gpio_bank *bank, int gpio)
  84. {
  85. void *reg = bank->base;
  86. int input;
  87. input = _get_gpio_direction(bank, gpio);
  88. switch (input) {
  89. case OMAP_GPIO_DIR_IN:
  90. reg += OMAP_GPIO_DATAIN;
  91. break;
  92. case OMAP_GPIO_DIR_OUT:
  93. reg += OMAP_GPIO_DATAOUT;
  94. break;
  95. default:
  96. return -1;
  97. }
  98. return (__raw_readl(reg) & (1 << gpio)) != 0;
  99. }
  100. #if !CONFIG_IS_ENABLED(DM_GPIO)
  101. static inline int get_gpio_index(int gpio)
  102. {
  103. return gpio & 0x1f;
  104. }
  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 const struct dm_gpio_ops gpio_omap_ops = {
  225. .direction_input = omap_gpio_direction_input,
  226. .direction_output = omap_gpio_direction_output,
  227. .get_value = omap_gpio_get_value,
  228. .set_value = omap_gpio_set_value,
  229. .get_function = omap_gpio_get_function,
  230. };
  231. static int omap_gpio_probe(struct udevice *dev)
  232. {
  233. struct gpio_bank *bank = dev_get_priv(dev);
  234. struct omap_gpio_plat *plat = dev_get_plat(dev);
  235. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  236. char name[18], *str;
  237. sprintf(name, "gpio@%4x_", (unsigned int)plat->base);
  238. str = strdup(name);
  239. if (!str)
  240. return -ENOMEM;
  241. uc_priv->bank_name = str;
  242. uc_priv->gpio_count = GPIO_PER_BANK;
  243. bank->base = (void *)plat->base;
  244. return 0;
  245. }
  246. #if !CONFIG_IS_ENABLED(OF_CONTROL)
  247. static int omap_gpio_bind(struct udevice *dev)
  248. {
  249. struct omap_gpio_plat *plat = dev_get_plat(dev);
  250. fdt_addr_t base_addr;
  251. if (plat)
  252. return 0;
  253. base_addr = dev_read_addr(dev);
  254. if (base_addr == FDT_ADDR_T_NONE)
  255. return -EINVAL;
  256. /*
  257. * TODO:
  258. * When every board is converted to driver model and DT is
  259. * supported, this can be done by auto-alloc feature, but
  260. * not using calloc to alloc memory for plat.
  261. *
  262. * For example am33xx_gpio uses platform data rather than device tree.
  263. *
  264. * NOTE: DO NOT COPY this code if you are using device tree.
  265. */
  266. plat = calloc(1, sizeof(*plat));
  267. if (!plat)
  268. return -ENOMEM;
  269. plat->base = base_addr;
  270. plat->port_name = fdt_get_name(gd->fdt_blob, dev_of_offset(dev), NULL);
  271. dev_set_plat(dev, plat);
  272. return 0;
  273. }
  274. #endif
  275. #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
  276. static const struct udevice_id omap_gpio_ids[] = {
  277. { .compatible = "ti,omap3-gpio" },
  278. { .compatible = "ti,omap4-gpio" },
  279. { .compatible = "ti,am4372-gpio" },
  280. { }
  281. };
  282. static int omap_gpio_of_to_plat(struct udevice *dev)
  283. {
  284. struct omap_gpio_plat *plat = dev_get_plat(dev);
  285. fdt_addr_t addr;
  286. addr = dev_read_addr(dev);
  287. if (addr == FDT_ADDR_T_NONE)
  288. return -EINVAL;
  289. plat->base = addr;
  290. return 0;
  291. }
  292. #endif
  293. U_BOOT_DRIVER(gpio_omap) = {
  294. .name = "gpio_omap",
  295. .id = UCLASS_GPIO,
  296. #if CONFIG_IS_ENABLED(OF_CONTROL)
  297. #if !CONFIG_IS_ENABLED(OF_PLATDATA)
  298. .of_match = omap_gpio_ids,
  299. .of_to_plat = of_match_ptr(omap_gpio_of_to_plat),
  300. .plat_auto = sizeof(struct omap_gpio_plat),
  301. #endif
  302. #else
  303. .bind = omap_gpio_bind,
  304. #endif
  305. .ops = &gpio_omap_ops,
  306. .probe = omap_gpio_probe,
  307. .priv_auto = sizeof(struct gpio_bank),
  308. #if !CONFIG_IS_ENABLED(OF_CONTROL)
  309. .flags = DM_FLAG_PRE_RELOC,
  310. #endif
  311. };
  312. #endif /* !DM_GPIO */