gpio.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. #define MAX_RESOURCES 256
  50. #define RESOURCE_LABEL_SIZE 16
  51. struct str_ident {
  52. char name[RESOURCE_LABEL_SIZE];
  53. } *str_ident;
  54. inline int check_gpio(unsigned short gpio)
  55. {
  56. if (gpio == GPIO_PB15 || gpio == GPIO_PC14 || gpio == GPIO_PC15
  57. || gpio == GPIO_PH14 || gpio == GPIO_PH15
  58. || gpio == GPIO_PJ14 || gpio == GPIO_PJ15
  59. || gpio > MAX_BLACKFIN_GPIOS)
  60. return -EINVAL;
  61. return 0;
  62. }
  63. inline void portmux_setup(unsigned short portno, unsigned short function)
  64. {
  65. u32 pmux;
  66. pmux = gpio_array[gpio_bank(portno)]->port_mux;
  67. pmux &= ~(0x3 << (2 * gpio_sub_n(portno)));
  68. pmux |= (function & 0x3) << (2 * gpio_sub_n(portno));
  69. gpio_array[gpio_bank(portno)]->port_mux = pmux;
  70. }
  71. inline u16 get_portmux(unsigned short portno)
  72. {
  73. u32 pmux;
  74. pmux = gpio_array[gpio_bank(portno)]->port_mux;
  75. return (pmux >> (2 * gpio_sub_n(portno)) & 0x3);
  76. }
  77. static void port_setup(unsigned short gpio, unsigned short usage)
  78. {
  79. if (usage == GPIO_USAGE) {
  80. gpio_array[gpio_bank(gpio)]->port_fer &= ~gpio_bit(gpio);
  81. } else
  82. gpio_array[gpio_bank(gpio)]->port_fer |= gpio_bit(gpio);
  83. SSYNC();
  84. }
  85. static int __init bfin_gpio_init(void)
  86. {
  87. str_ident = kcalloc(MAX_RESOURCES,
  88. sizeof(struct str_ident), GFP_KERNEL);
  89. if (str_ident == NULL)
  90. return -ENOMEM;
  91. memset(str_ident, 0, MAX_RESOURCES * sizeof(struct str_ident));
  92. printk(KERN_INFO "Blackfin GPIO Controller\n");
  93. return 0;
  94. }
  95. arch_initcall(bfin_gpio_init);
  96. static void set_label(unsigned short ident, const char *label)
  97. {
  98. if (label && str_ident) {
  99. strncpy(str_ident[ident].name, label,
  100. RESOURCE_LABEL_SIZE);
  101. str_ident[ident].name[RESOURCE_LABEL_SIZE - 1] = 0;
  102. }
  103. }
  104. static char *get_label(unsigned short ident)
  105. {
  106. if (!str_ident)
  107. return "UNKNOWN";
  108. return (*str_ident[ident].name ? str_ident[ident].name : "UNKNOWN");
  109. }
  110. static int cmp_label(unsigned short ident, const char *label)
  111. {
  112. if (label && str_ident)
  113. return strncmp(str_ident[ident].name,
  114. label, strlen(label));
  115. else
  116. return -EINVAL;
  117. }
  118. int peripheral_request(unsigned short per, const char *label)
  119. {
  120. unsigned long flags;
  121. unsigned short ident = P_IDENT(per);
  122. /*
  123. * Don't cares are pins with only one dedicated function
  124. */
  125. if (per & P_DONTCARE)
  126. return 0;
  127. if (!(per & P_DEFINED))
  128. return -ENODEV;
  129. if (check_gpio(ident) < 0)
  130. return -EINVAL;
  131. local_irq_save(flags);
  132. if (unlikely(reserved_gpio_map[gpio_bank(ident)] & gpio_bit(ident))) {
  133. printk(KERN_ERR
  134. "%s: Peripheral %d is already reserved as GPIO by %s !\n",
  135. __FUNCTION__, ident, get_label(ident));
  136. dump_stack();
  137. local_irq_restore(flags);
  138. return -EBUSY;
  139. }
  140. if (unlikely(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident))) {
  141. u16 funct = get_portmux(ident);
  142. /*
  143. * Pin functions like AMC address strobes my
  144. * be requested and used by several drivers
  145. */
  146. if (!((per & P_MAYSHARE) && (funct == P_FUNCT2MUX(per)))) {
  147. /*
  148. * Allow that the identical pin function can
  149. * be requested from the same driver twice
  150. */
  151. if (cmp_label(ident, label) == 0)
  152. goto anyway;
  153. printk(KERN_ERR
  154. "%s: Peripheral %d function %d is already reserved by %s !\n",
  155. __FUNCTION__, ident, P_FUNCT2MUX(per), get_label(ident));
  156. dump_stack();
  157. local_irq_restore(flags);
  158. return -EBUSY;
  159. }
  160. }
  161. anyway:
  162. reserved_peri_map[gpio_bank(ident)] |= gpio_bit(ident);
  163. portmux_setup(ident, P_FUNCT2MUX(per));
  164. port_setup(ident, PERIPHERAL_USAGE);
  165. local_irq_restore(flags);
  166. set_label(ident, label);
  167. return 0;
  168. }
  169. EXPORT_SYMBOL(peripheral_request);
  170. int peripheral_request_list(unsigned short per[], const char *label)
  171. {
  172. u16 cnt;
  173. int ret;
  174. for (cnt = 0; per[cnt] != 0; cnt++) {
  175. ret = peripheral_request(per[cnt], label);
  176. if (ret < 0) {
  177. for ( ; cnt > 0; cnt--) {
  178. peripheral_free(per[cnt - 1]);
  179. }
  180. return ret;
  181. }
  182. }
  183. return 0;
  184. }
  185. EXPORT_SYMBOL(peripheral_request_list);
  186. void peripheral_free(unsigned short per)
  187. {
  188. unsigned long flags;
  189. unsigned short ident = P_IDENT(per);
  190. if (per & P_DONTCARE)
  191. return;
  192. if (!(per & P_DEFINED))
  193. return;
  194. if (check_gpio(ident) < 0)
  195. return;
  196. local_irq_save(flags);
  197. if (unlikely(!(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident)))) {
  198. local_irq_restore(flags);
  199. return;
  200. }
  201. if (!(per & P_MAYSHARE)) {
  202. port_setup(ident, GPIO_USAGE);
  203. }
  204. reserved_peri_map[gpio_bank(ident)] &= ~gpio_bit(ident);
  205. local_irq_restore(flags);
  206. }
  207. EXPORT_SYMBOL(peripheral_free);
  208. void peripheral_free_list(unsigned short per[])
  209. {
  210. u16 cnt;
  211. for (cnt = 0; per[cnt] != 0; cnt++) {
  212. peripheral_free(per[cnt]);
  213. }
  214. }
  215. EXPORT_SYMBOL(peripheral_free_list);
  216. /***********************************************************
  217. *
  218. * FUNCTIONS: Blackfin GPIO Driver
  219. *
  220. * INPUTS/OUTPUTS:
  221. * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
  222. *
  223. *
  224. * DESCRIPTION: Blackfin GPIO Driver API
  225. *
  226. * CAUTION:
  227. *************************************************************
  228. * MODIFICATION HISTORY :
  229. **************************************************************/
  230. int gpio_request(unsigned short gpio, const char *label)
  231. {
  232. unsigned long flags;
  233. if (check_gpio(gpio) < 0)
  234. return -EINVAL;
  235. local_irq_save(flags);
  236. if (unlikely(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
  237. printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved by %s !\n",
  238. gpio, get_label(gpio));
  239. dump_stack();
  240. local_irq_restore(flags);
  241. return -EBUSY;
  242. }
  243. if (unlikely(reserved_peri_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
  244. printk(KERN_ERR
  245. "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
  246. gpio, get_label(gpio));
  247. dump_stack();
  248. local_irq_restore(flags);
  249. return -EBUSY;
  250. }
  251. reserved_gpio_map[gpio_bank(gpio)] |= gpio_bit(gpio);
  252. local_irq_restore(flags);
  253. port_setup(gpio, GPIO_USAGE);
  254. set_label(gpio, label);
  255. return 0;
  256. }
  257. EXPORT_SYMBOL(gpio_request);
  258. void gpio_free(unsigned short gpio)
  259. {
  260. unsigned long flags;
  261. if (check_gpio(gpio) < 0)
  262. return;
  263. local_irq_save(flags);
  264. if (unlikely(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)))) {
  265. printk(KERN_ERR "bfin-gpio: GPIO %d wasn't reserved!\n", gpio);
  266. dump_stack();
  267. local_irq_restore(flags);
  268. return;
  269. }
  270. reserved_gpio_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
  271. local_irq_restore(flags);
  272. }
  273. EXPORT_SYMBOL(gpio_free);
  274. void gpio_direction_input(unsigned short gpio)
  275. {
  276. unsigned long flags;
  277. BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
  278. local_irq_save(flags);
  279. gpio_array[gpio_bank(gpio)]->port_dir_clear = gpio_bit(gpio);
  280. gpio_array[gpio_bank(gpio)]->port_inen |= gpio_bit(gpio);
  281. local_irq_restore(flags);
  282. }
  283. EXPORT_SYMBOL(gpio_direction_input);
  284. void gpio_direction_output(unsigned short gpio)
  285. {
  286. unsigned long flags;
  287. BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
  288. local_irq_save(flags);
  289. gpio_array[gpio_bank(gpio)]->port_inen &= ~gpio_bit(gpio);
  290. gpio_array[gpio_bank(gpio)]->port_dir_set = gpio_bit(gpio);
  291. local_irq_restore(flags);
  292. }
  293. EXPORT_SYMBOL(gpio_direction_output);
  294. void gpio_set_value(unsigned short gpio, unsigned short arg)
  295. {
  296. if (arg)
  297. gpio_array[gpio_bank(gpio)]->port_set = gpio_bit(gpio);
  298. else
  299. gpio_array[gpio_bank(gpio)]->port_clear = gpio_bit(gpio);
  300. }
  301. EXPORT_SYMBOL(gpio_set_value);
  302. unsigned short gpio_get_value(unsigned short gpio)
  303. {
  304. return (1 & (gpio_array[gpio_bank(gpio)]->port_data >> gpio_sub_n(gpio)));
  305. }
  306. EXPORT_SYMBOL(gpio_get_value);