da8xx_gpio.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * GPIO driver for TI DaVinci DA8xx SOCs.
  3. *
  4. * (C) Copyright 2011 Guralp Systems Ltd.
  5. * Laurence Withers <lwithers@guralp.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. */
  22. #include <common.h>
  23. #include <asm/io.h>
  24. #include <asm/gpio.h>
  25. #include <asm/arch/hardware.h>
  26. #include <asm/arch/davinci_misc.h>
  27. static struct gpio_registry {
  28. int is_registered;
  29. char name[GPIO_NAME_SIZE];
  30. } gpio_registry[MAX_NUM_GPIOS];
  31. #define pinmux(x) (&davinci_syscfg_regs->pinmux[x])
  32. static const struct pinmux_config gpio_pinmux[] = {
  33. { pinmux(1), 8, 7 }, /* GP0[0] */
  34. { pinmux(1), 8, 6 },
  35. { pinmux(1), 8, 5 },
  36. { pinmux(1), 8, 4 },
  37. { pinmux(1), 8, 3 },
  38. { pinmux(1), 8, 2 },
  39. { pinmux(1), 8, 1 },
  40. { pinmux(1), 8, 0 },
  41. { pinmux(0), 8, 7 },
  42. { pinmux(0), 8, 6 },
  43. { pinmux(0), 8, 5 },
  44. { pinmux(0), 8, 4 },
  45. { pinmux(0), 8, 3 },
  46. { pinmux(0), 8, 2 },
  47. { pinmux(0), 8, 1 },
  48. { pinmux(0), 8, 0 },
  49. { pinmux(4), 8, 7 }, /* GP1[0] */
  50. { pinmux(4), 8, 6 },
  51. { pinmux(4), 8, 5 },
  52. { pinmux(4), 8, 4 },
  53. { pinmux(4), 8, 3 },
  54. { pinmux(4), 8, 2 },
  55. { pinmux(4), 4, 1 },
  56. { pinmux(4), 4, 0 },
  57. { pinmux(3), 4, 0 },
  58. { pinmux(2), 4, 6 },
  59. { pinmux(2), 4, 5 },
  60. { pinmux(2), 4, 4 },
  61. { pinmux(2), 4, 3 },
  62. { pinmux(2), 4, 2 },
  63. { pinmux(2), 4, 1 },
  64. { pinmux(2), 8, 0 },
  65. { pinmux(6), 8, 7 }, /* GP2[0] */
  66. { pinmux(6), 8, 6 },
  67. { pinmux(6), 8, 5 },
  68. { pinmux(6), 8, 4 },
  69. { pinmux(6), 8, 3 },
  70. { pinmux(6), 8, 2 },
  71. { pinmux(6), 8, 1 },
  72. { pinmux(6), 8, 0 },
  73. { pinmux(5), 8, 7 },
  74. { pinmux(5), 8, 6 },
  75. { pinmux(5), 8, 5 },
  76. { pinmux(5), 8, 4 },
  77. { pinmux(5), 8, 3 },
  78. { pinmux(5), 8, 2 },
  79. { pinmux(5), 8, 1 },
  80. { pinmux(5), 8, 0 },
  81. { pinmux(8), 8, 7 }, /* GP3[0] */
  82. { pinmux(8), 8, 6 },
  83. { pinmux(8), 8, 5 },
  84. { pinmux(8), 8, 4 },
  85. { pinmux(8), 8, 3 },
  86. { pinmux(8), 8, 2 },
  87. { pinmux(8), 8, 1 },
  88. { pinmux(8), 8, 0 },
  89. { pinmux(7), 8, 7 },
  90. { pinmux(7), 8, 6 },
  91. { pinmux(7), 8, 5 },
  92. { pinmux(7), 8, 4 },
  93. { pinmux(7), 8, 3 },
  94. { pinmux(7), 8, 2 },
  95. { pinmux(7), 8, 1 },
  96. { pinmux(7), 8, 0 },
  97. { pinmux(10), 8, 7 }, /* GP4[0] */
  98. { pinmux(10), 8, 6 },
  99. { pinmux(10), 8, 5 },
  100. { pinmux(10), 8, 4 },
  101. { pinmux(10), 8, 3 },
  102. { pinmux(10), 8, 2 },
  103. { pinmux(10), 8, 1 },
  104. { pinmux(10), 8, 0 },
  105. { pinmux(9), 8, 7 },
  106. { pinmux(9), 8, 6 },
  107. { pinmux(9), 8, 5 },
  108. { pinmux(9), 8, 4 },
  109. { pinmux(9), 8, 3 },
  110. { pinmux(9), 8, 2 },
  111. { pinmux(9), 8, 1 },
  112. { pinmux(9), 8, 0 },
  113. { pinmux(12), 8, 7 }, /* GP5[0] */
  114. { pinmux(12), 8, 6 },
  115. { pinmux(12), 8, 5 },
  116. { pinmux(12), 8, 4 },
  117. { pinmux(12), 8, 3 },
  118. { pinmux(12), 8, 2 },
  119. { pinmux(12), 8, 1 },
  120. { pinmux(12), 8, 0 },
  121. { pinmux(11), 8, 7 },
  122. { pinmux(11), 8, 6 },
  123. { pinmux(11), 8, 5 },
  124. { pinmux(11), 8, 4 },
  125. { pinmux(11), 8, 3 },
  126. { pinmux(11), 8, 2 },
  127. { pinmux(11), 8, 1 },
  128. { pinmux(11), 8, 0 },
  129. { pinmux(19), 8, 6 }, /* GP6[0] */
  130. { pinmux(19), 8, 5 },
  131. { pinmux(19), 8, 4 },
  132. { pinmux(19), 8, 3 },
  133. { pinmux(19), 8, 2 },
  134. { pinmux(16), 8, 1 },
  135. { pinmux(14), 8, 1 },
  136. { pinmux(14), 8, 0 },
  137. { pinmux(13), 8, 7 },
  138. { pinmux(13), 8, 6 },
  139. { pinmux(13), 8, 5 },
  140. { pinmux(13), 8, 4 },
  141. { pinmux(13), 8, 3 },
  142. { pinmux(13), 8, 2 },
  143. { pinmux(13), 8, 1 },
  144. { pinmux(13), 8, 0 },
  145. { pinmux(18), 8, 1 }, /* GP7[0] */
  146. { pinmux(18), 8, 0 },
  147. { pinmux(17), 8, 7 },
  148. { pinmux(17), 8, 6 },
  149. { pinmux(17), 8, 5 },
  150. { pinmux(17), 8, 4 },
  151. { pinmux(17), 8, 3 },
  152. { pinmux(17), 8, 2 },
  153. { pinmux(17), 8, 1 },
  154. { pinmux(17), 8, 0 },
  155. { pinmux(16), 8, 7 },
  156. { pinmux(16), 8, 6 },
  157. { pinmux(16), 8, 5 },
  158. { pinmux(16), 8, 4 },
  159. { pinmux(16), 8, 3 },
  160. { pinmux(16), 8, 2 },
  161. { pinmux(19), 8, 0 }, /* GP8[0] */
  162. { pinmux(3), 4, 7 },
  163. { pinmux(3), 4, 6 },
  164. { pinmux(3), 4, 5 },
  165. { pinmux(3), 4, 4 },
  166. { pinmux(3), 4, 3 },
  167. { pinmux(3), 4, 2 },
  168. { pinmux(2), 4, 7 },
  169. { pinmux(19), 8, 1 },
  170. { pinmux(19), 8, 0 },
  171. { pinmux(18), 8, 7 },
  172. { pinmux(18), 8, 6 },
  173. { pinmux(18), 8, 5 },
  174. { pinmux(18), 8, 4 },
  175. { pinmux(18), 8, 3 },
  176. { pinmux(18), 8, 2 },
  177. };
  178. int gpio_request(unsigned gpio, const char *label)
  179. {
  180. if (gpio >= MAX_NUM_GPIOS)
  181. return -1;
  182. if (gpio_registry[gpio].is_registered)
  183. return -1;
  184. gpio_registry[gpio].is_registered = 1;
  185. strncpy(gpio_registry[gpio].name, label, GPIO_NAME_SIZE);
  186. gpio_registry[gpio].name[GPIO_NAME_SIZE - 1] = 0;
  187. davinci_configure_pin_mux(&gpio_pinmux[gpio], 1);
  188. return 0;
  189. }
  190. int gpio_free(unsigned gpio)
  191. {
  192. if (gpio >= MAX_NUM_GPIOS)
  193. return -1;
  194. if (!gpio_registry[gpio].is_registered)
  195. return -1;
  196. gpio_registry[gpio].is_registered = 0;
  197. gpio_registry[gpio].name[0] = '\0';
  198. /* Do not configure as input or change pin mux here */
  199. return 0;
  200. }
  201. int gpio_direction_input(unsigned gpio)
  202. {
  203. struct davinci_gpio *bank;
  204. bank = GPIO_BANK(gpio);
  205. setbits_le32(&bank->dir, 1U << GPIO_BIT(gpio));
  206. return 0;
  207. }
  208. int gpio_direction_output(unsigned gpio, int value)
  209. {
  210. struct davinci_gpio *bank;
  211. bank = GPIO_BANK(gpio);
  212. clrbits_le32(&bank->dir, 1U << GPIO_BIT(gpio));
  213. gpio_set_value(gpio, value);
  214. return 0;
  215. }
  216. int gpio_get_value(unsigned gpio)
  217. {
  218. struct davinci_gpio *bank;
  219. unsigned int ip;
  220. bank = GPIO_BANK(gpio);
  221. ip = in_le32(&bank->in_data) & (1U << GPIO_BIT(gpio));
  222. return ip ? 1 : 0;
  223. }
  224. int gpio_set_value(unsigned gpio, int value)
  225. {
  226. struct davinci_gpio *bank;
  227. bank = GPIO_BANK(gpio);
  228. if (value)
  229. bank->set_data = 1U << GPIO_BIT(gpio);
  230. else
  231. bank->clr_data = 1U << GPIO_BIT(gpio);
  232. return 0;
  233. }
  234. void gpio_info(void)
  235. {
  236. unsigned gpio, dir, val;
  237. struct davinci_gpio *bank;
  238. for (gpio = 0; gpio < MAX_NUM_GPIOS; ++gpio) {
  239. bank = GPIO_BANK(gpio);
  240. dir = in_le32(&bank->dir) & (1U << GPIO_BIT(gpio));
  241. val = gpio_get_value(gpio);
  242. printf("% 4d: %s: %d [%c] %s\n",
  243. gpio, dir ? " in" : "out", val,
  244. gpio_registry[gpio].is_registered ? 'x' : ' ',
  245. gpio_registry[gpio].name);
  246. }
  247. }