gpio.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * File: arch/blackfin/mach-bf548/gpio.c
  3. * Based on:
  4. * Author: Michael Hennerich (hennerich@blackfin.uclinux.org)
  5. *
  6. * Created:
  7. * Description: GPIO Abstraction Layer
  8. *
  9. * Modified:
  10. * Copyright 2007 Analog Devices Inc.
  11. *
  12. * Bugs: Enter bugs at http://blackfin.uclinux.org/
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, see the file COPYING, or write
  26. * to the Free Software Foundation, Inc.,
  27. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  28. */
  29. #include <linux/module.h>
  30. #include <linux/err.h>
  31. #include <asm/blackfin.h>
  32. #include <asm/gpio.h>
  33. #include <asm/portmux.h>
  34. #include <linux/irq.h>
  35. static struct gpio_port_t *gpio_array[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
  36. (struct gpio_port_t *)PORTA_FER,
  37. (struct gpio_port_t *)PORTB_FER,
  38. (struct gpio_port_t *)PORTC_FER,
  39. (struct gpio_port_t *)PORTD_FER,
  40. (struct gpio_port_t *)PORTE_FER,
  41. (struct gpio_port_t *)PORTF_FER,
  42. (struct gpio_port_t *)PORTG_FER,
  43. (struct gpio_port_t *)PORTH_FER,
  44. (struct gpio_port_t *)PORTI_FER,
  45. (struct gpio_port_t *)PORTJ_FER,
  46. };
  47. static unsigned short reserved_gpio_map[gpio_bank(MAX_BLACKFIN_GPIOS)];
  48. static unsigned short reserved_peri_map[gpio_bank(MAX_BLACKFIN_GPIOS)];
  49. char *str_ident = NULL;
  50. #define RESOURCE_LABEL_SIZE 16
  51. inline int check_gpio(unsigned short gpio)
  52. {
  53. if (gpio == GPIO_PB15 || gpio == GPIO_PC14 || gpio == GPIO_PC15
  54. || gpio == GPIO_PH14 || gpio == GPIO_PH15
  55. || gpio == GPIO_PJ14 || gpio == GPIO_PJ15
  56. || gpio > MAX_BLACKFIN_GPIOS)
  57. return -EINVAL;
  58. return 0;
  59. }
  60. inline void portmux_setup(unsigned short portno, unsigned short function)
  61. {
  62. u32 pmux;
  63. pmux = gpio_array[gpio_bank(portno)]->port_mux;
  64. pmux &= ~(0x3 << (2 * gpio_sub_n(portno)));
  65. pmux |= (function & 0x3) << (2 * gpio_sub_n(portno));
  66. gpio_array[gpio_bank(portno)]->port_mux = pmux;
  67. }
  68. inline u16 get_portmux(unsigned short portno)
  69. {
  70. u32 pmux;
  71. pmux = gpio_array[gpio_bank(portno)]->port_mux;
  72. return (pmux >> (2 * gpio_sub_n(portno)) & 0x3);
  73. }
  74. static void port_setup(unsigned short gpio, unsigned short usage)
  75. {
  76. if (usage == GPIO_USAGE) {
  77. gpio_array[gpio_bank(gpio)]->port_fer &= ~gpio_bit(gpio);
  78. } else
  79. gpio_array[gpio_bank(gpio)]->port_fer |= gpio_bit(gpio);
  80. SSYNC();
  81. }
  82. static int __init bfin_gpio_init(void)
  83. {
  84. str_ident = kzalloc(RESOURCE_LABEL_SIZE * 256, GFP_KERNEL);
  85. if (!str_ident)
  86. return -ENOMEM;
  87. printk(KERN_INFO "Blackfin GPIO Controller\n");
  88. return 0;
  89. }
  90. arch_initcall(bfin_gpio_init);
  91. static void set_label(unsigned short ident, const char *label)
  92. {
  93. if (label && str_ident) {
  94. strncpy(str_ident + ident * RESOURCE_LABEL_SIZE, label,
  95. RESOURCE_LABEL_SIZE);
  96. str_ident[ident * RESOURCE_LABEL_SIZE +
  97. RESOURCE_LABEL_SIZE - 1] = 0;
  98. }
  99. }
  100. static char *get_label(unsigned short ident)
  101. {
  102. if (!str_ident)
  103. return "UNKNOWN";
  104. return (str_ident[ident * RESOURCE_LABEL_SIZE] ?
  105. (str_ident + ident * RESOURCE_LABEL_SIZE) : "UNKNOWN");
  106. }
  107. static int cmp_label(unsigned short ident, const char *label)
  108. {
  109. if (label && str_ident)
  110. return strncmp(str_ident + ident * RESOURCE_LABEL_SIZE,
  111. label, strlen(label));
  112. else
  113. return -EINVAL;
  114. }
  115. int peripheral_request(unsigned short per, const char *label)
  116. {
  117. unsigned long flags;
  118. unsigned short ident = P_IDENT(per);
  119. /*
  120. * Don't cares are pins with only one dedicated function
  121. */
  122. if (per & P_DONTCARE)
  123. return 0;
  124. if (!(per & P_DEFINED))
  125. return -ENODEV;
  126. if (check_gpio(ident) < 0)
  127. return -EINVAL;
  128. local_irq_save(flags);
  129. if (unlikely(reserved_gpio_map[gpio_bank(ident)] & gpio_bit(ident))) {
  130. printk(KERN_ERR
  131. "%s: Peripheral %d is already reserved as GPIO by %s !\n",
  132. __FUNCTION__, ident, get_label(ident));
  133. dump_stack();
  134. local_irq_restore(flags);
  135. return -EBUSY;
  136. }
  137. if (unlikely(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident))) {
  138. u16 funct = get_portmux(ident);
  139. /*
  140. * Pin functions like AMC address strobes my
  141. * be requested and used by several drivers
  142. */
  143. if (!((per & P_MAYSHARE) && (funct == P_FUNCT2MUX(per)))) {
  144. /*
  145. * Allow that the identical pin function can
  146. * be requested from the same driver twice
  147. */
  148. if (cmp_label(ident, label) == 0)
  149. goto anyway;
  150. printk(KERN_ERR
  151. "%s: Peripheral %d function %d is already reserved by %s !\n",
  152. __FUNCTION__, ident, P_FUNCT2MUX(per), get_label(ident));
  153. dump_stack();
  154. local_irq_restore(flags);
  155. return -EBUSY;
  156. }
  157. }
  158. anyway:
  159. reserved_peri_map[gpio_bank(ident)] |= gpio_bit(ident);
  160. portmux_setup(ident, P_FUNCT2MUX(per));
  161. port_setup(ident, PERIPHERAL_USAGE);
  162. local_irq_restore(flags);
  163. set_label(ident, label);
  164. return 0;
  165. }
  166. EXPORT_SYMBOL(peripheral_request);
  167. int peripheral_request_list(unsigned short per[], const char *label)
  168. {
  169. u16 cnt;
  170. int ret;
  171. for (cnt = 0; per[cnt] != 0; cnt++) {
  172. ret = peripheral_request(per[cnt], label);
  173. if (ret < 0)
  174. return ret;
  175. }
  176. return 0;
  177. }
  178. EXPORT_SYMBOL(peripheral_request_list);
  179. void peripheral_free(unsigned short per)
  180. {
  181. unsigned long flags;
  182. unsigned short ident = P_IDENT(per);
  183. if (per & P_DONTCARE)
  184. return;
  185. if (!(per & P_DEFINED))
  186. return;
  187. if (check_gpio(ident) < 0)
  188. return;
  189. local_irq_save(flags);
  190. if (unlikely(!(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident)))) {
  191. local_irq_restore(flags);
  192. return;
  193. }
  194. if (!(per & P_MAYSHARE)) {
  195. port_setup(ident, GPIO_USAGE);
  196. }
  197. reserved_peri_map[gpio_bank(ident)] &= ~gpio_bit(ident);
  198. local_irq_restore(flags);
  199. }
  200. EXPORT_SYMBOL(peripheral_free);
  201. void peripheral_free_list(unsigned short per[])
  202. {
  203. u16 cnt;
  204. for (cnt = 0; per[cnt] != 0; cnt++) {
  205. peripheral_free(per[cnt]);
  206. }
  207. }
  208. EXPORT_SYMBOL(peripheral_free_list);
  209. /***********************************************************
  210. *
  211. * FUNCTIONS: Blackfin GPIO Driver
  212. *
  213. * INPUTS/OUTPUTS:
  214. * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
  215. *
  216. *
  217. * DESCRIPTION: Blackfin GPIO Driver API
  218. *
  219. * CAUTION:
  220. *************************************************************
  221. * MODIFICATION HISTORY :
  222. **************************************************************/
  223. int gpio_request(unsigned short gpio, const char *label)
  224. {
  225. unsigned long flags;
  226. if (check_gpio(gpio) < 0)
  227. return -EINVAL;
  228. local_irq_save(flags);
  229. if (unlikely(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
  230. printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved by %s !\n",
  231. gpio, get_label(gpio));
  232. dump_stack();
  233. local_irq_restore(flags);
  234. return -EBUSY;
  235. }
  236. if (unlikely(reserved_peri_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
  237. printk(KERN_ERR
  238. "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
  239. gpio, get_label(gpio));
  240. dump_stack();
  241. local_irq_restore(flags);
  242. return -EBUSY;
  243. }
  244. reserved_gpio_map[gpio_bank(gpio)] |= gpio_bit(gpio);
  245. local_irq_restore(flags);
  246. port_setup(gpio, GPIO_USAGE);
  247. set_label(gpio, label);
  248. return 0;
  249. }
  250. EXPORT_SYMBOL(gpio_request);
  251. void gpio_free(unsigned short gpio)
  252. {
  253. unsigned long flags;
  254. if (check_gpio(gpio) < 0)
  255. return;
  256. local_irq_save(flags);
  257. if (unlikely(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)))) {
  258. printk(KERN_ERR "bfin-gpio: GPIO %d wasn't reserved!\n", gpio);
  259. dump_stack();
  260. local_irq_restore(flags);
  261. return;
  262. }
  263. reserved_gpio_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
  264. local_irq_restore(flags);
  265. }
  266. EXPORT_SYMBOL(gpio_free);
  267. void gpio_direction_input(unsigned short gpio)
  268. {
  269. unsigned long flags;
  270. BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
  271. local_irq_save(flags);
  272. gpio_array[gpio_bank(gpio)]->port_dir_clear = gpio_bit(gpio);
  273. gpio_array[gpio_bank(gpio)]->port_inen |= gpio_bit(gpio);
  274. local_irq_restore(flags);
  275. }
  276. EXPORT_SYMBOL(gpio_direction_input);
  277. void gpio_direction_output(unsigned short gpio)
  278. {
  279. unsigned long flags;
  280. BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
  281. local_irq_save(flags);
  282. gpio_array[gpio_bank(gpio)]->port_inen &= ~gpio_bit(gpio);
  283. gpio_array[gpio_bank(gpio)]->port_dir_set = gpio_bit(gpio);
  284. local_irq_restore(flags);
  285. }
  286. EXPORT_SYMBOL(gpio_direction_output);
  287. void gpio_set_value(unsigned short gpio, unsigned short arg)
  288. {
  289. if (arg)
  290. gpio_array[gpio_bank(gpio)]->port_set = gpio_bit(gpio);
  291. else
  292. gpio_array[gpio_bank(gpio)]->port_clear = gpio_bit(gpio);
  293. }
  294. EXPORT_SYMBOL(gpio_set_value);
  295. unsigned short gpio_get_value(unsigned short gpio)
  296. {
  297. return (1 & (gpio_array[gpio_bank(gpio)]->port_data >> gpio_sub_n(gpio)));
  298. }
  299. EXPORT_SYMBOL(gpio_get_value);