gpio.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * This file is part of FH8620 BSP for RT-Thread distribution.
  3. *
  4. * Copyright (c) 2016 Shanghai Fullhan Microelectronics Co., Ltd.
  5. * All rights reserved
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (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 along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * Visit http://www.fullhan.com to get contact with Fullhan.
  22. *
  23. * Change Logs:
  24. * Date Author Notes
  25. */
  26. #include "fh_def.h"
  27. #include "gpio.h"
  28. #include "libraries/inc/fh_gpio.h"
  29. #include "interrupt.h"
  30. #include "board_info.h"
  31. #include <rtdevice.h>
  32. #include "fh_arch.h"
  33. //#define FH_GPIO_DEBUG
  34. #ifdef FH_GPIO_DEBUG
  35. #define PRINT_GPIO_DBG(fmt, args...) \
  36. do \
  37. { \
  38. rt_kprintf("FH_GPIO_DEBUG: "); \
  39. rt_kprintf(fmt, ## args); \
  40. } \
  41. while(0)
  42. #else
  43. #define PRINT_GPIO_DBG(fmt, args...) do { } while (0)
  44. #endif
  45. int gpio_available[NUM_OF_GPIO];
  46. extern struct rt_irq_desc irq_desc[];
  47. static inline rt_uint32_t gpio_to_base(rt_uint32_t gpio)
  48. {
  49. if (gpio >= 32 && gpio < 64)
  50. {
  51. return GPIO1_REG_BASE;
  52. }
  53. else if(gpio < 32)
  54. {
  55. return GPIO0_REG_BASE;
  56. }
  57. else
  58. {
  59. rt_kprintf("ERROR: %s, incorrect GPIO num\n", __func__);
  60. return -RT_ERROR;
  61. }
  62. }
  63. static inline rt_uint32_t irq_to_base(rt_uint32_t irq)
  64. {
  65. return (irq-NR_INTERNAL_IRQS > 32) ? GPIO1_REG_BASE : GPIO0_REG_BASE;
  66. }
  67. static inline rt_uint32_t irq_to_bit(rt_uint32_t irq)
  68. {
  69. if(irq >= NR_INTERNAL_IRQS && irq < NR_INTERNAL_IRQS + 32)
  70. return 0;
  71. else
  72. return 32;
  73. }
  74. rt_uint32_t gpio_to_irq(rt_uint32_t gpio)
  75. {
  76. return (gpio + NR_INTERNAL_IRQS);
  77. }
  78. void gpio_enable_debounce(rt_uint32_t gpio)
  79. {
  80. rt_uint32_t tmp, base, offset;
  81. offset = gpio % 32;
  82. base = gpio_to_base(gpio);
  83. tmp = GET_REG(base + REG_GPIO_DEBOUNCE);
  84. tmp |= BIT(offset);
  85. SET_REG(base + REG_GPIO_DEBOUNCE, tmp);
  86. }
  87. void gpio_disable_debounce(rt_uint32_t gpio)
  88. {
  89. rt_uint32_t tmp, base, offset;
  90. offset = gpio % 32;
  91. base = gpio_to_base(gpio);
  92. tmp = GET_REG(base + REG_GPIO_DEBOUNCE);
  93. tmp &= ~BIT(offset);
  94. SET_REG(base + REG_GPIO_DEBOUNCE, tmp);
  95. }
  96. int gpio_get_value(rt_uint32_t gpio)
  97. {
  98. rt_uint32_t tmp, base, offset;
  99. offset = gpio % 32;
  100. base = gpio_to_base(gpio);
  101. tmp = GET_REG(base + REG_GPIO_SWPORTA_DDR);
  102. tmp &= BIT(offset);
  103. if (tmp) {
  104. tmp = GET_REG(base + REG_GPIO_SWPORTA_DR);
  105. } else {
  106. tmp = GET_REG(base + REG_GPIO_EXT_PORTA);
  107. }
  108. tmp &= BIT(offset);
  109. tmp = tmp >> offset;
  110. return tmp;
  111. }
  112. void gpio_set_value(rt_uint32_t gpio, int val)
  113. {
  114. rt_uint32_t tmp, base, offset;
  115. offset = gpio % 32;
  116. base = gpio_to_base(gpio);
  117. tmp = GET_REG(base + REG_GPIO_SWPORTA_DR);
  118. if(val)
  119. tmp |= BIT(offset);
  120. else
  121. tmp &= ~BIT(offset);
  122. SET_REG(base + REG_GPIO_SWPORTA_DR, tmp);
  123. }
  124. int gpio_get_direction(rt_uint32_t gpio)
  125. {
  126. rt_uint32_t tmp, base, offset;
  127. offset = gpio % 32;
  128. base = gpio_to_base(gpio);
  129. tmp = GET_REG(base + REG_GPIO_SWPORTA_DDR);
  130. tmp &= BIT(offset);
  131. tmp = tmp >> offset;
  132. return tmp;
  133. }
  134. void gpio_set_direction(rt_uint32_t gpio, rt_uint32_t direction)
  135. {
  136. rt_uint32_t tmp, base, offset;
  137. offset = gpio % 32;
  138. base = gpio_to_base(gpio);
  139. tmp = GET_REG(base + REG_GPIO_SWPORTA_DDR);
  140. if(direction == GPIO_DIR_OUTPUT)
  141. tmp |= BIT(offset);
  142. else
  143. tmp &= ~BIT(offset);
  144. SET_REG(base + REG_GPIO_SWPORTA_DDR, tmp);
  145. }
  146. int gpio_set_irq_type(rt_uint32_t gpio, rt_uint32_t type)
  147. {
  148. rt_uint32_t int_type, int_polarity;
  149. rt_uint32_t bit = gpio % 32;
  150. rt_uint32_t base;
  151. base = gpio_to_base(gpio);
  152. int_type = GET_REG(base + REG_GPIO_INTTYPE_LEVEL);
  153. int_polarity = GET_REG(base + REG_GPIO_INT_POLARITY);
  154. switch (type & IRQ_TYPE_TRIGGER_MASK) {
  155. case IRQ_TYPE_EDGE_BOTH:
  156. int_type |= BIT(bit);
  157. // toggle trigger
  158. if (gpio_get_value(gpio))
  159. int_polarity &= ~BIT(bit);
  160. else
  161. int_polarity |= BIT(bit);
  162. break;
  163. case IRQ_TYPE_EDGE_RISING:
  164. int_type |= BIT(bit);
  165. int_polarity |= BIT(bit);
  166. break;
  167. case IRQ_TYPE_EDGE_FALLING:
  168. int_type |= BIT(bit);
  169. int_polarity &= ~BIT(bit);
  170. break;
  171. case IRQ_TYPE_LEVEL_HIGH:
  172. int_type &= ~BIT(bit);
  173. int_polarity |= BIT(bit);
  174. break;
  175. case IRQ_TYPE_LEVEL_LOW:
  176. int_type &= ~BIT(bit);
  177. int_polarity &= ~BIT(bit);
  178. break;
  179. case IRQ_TYPE_NONE:
  180. return 0;
  181. default:
  182. return -RT_ERROR;
  183. }
  184. SET_REG(base + REG_GPIO_INTTYPE_LEVEL, int_type);
  185. SET_REG(base + REG_GPIO_INT_POLARITY, int_polarity);
  186. return 0;
  187. }
  188. int gpio_irq_mask(rt_uint32_t irq)
  189. {
  190. rt_uint32_t tmp, base, bit;
  191. base = irq_to_base(irq);
  192. bit = irq_to_bit(irq);
  193. tmp = GET_REG(base + REG_GPIO_INTMASK);
  194. tmp |= BIT(irq - NR_INTERNAL_IRQS - bit);
  195. SET_REG(base + REG_GPIO_INTMASK, tmp);
  196. return 0;
  197. }
  198. int gpio_irq_unmask(rt_uint32_t irq)
  199. {
  200. rt_uint32_t tmp, base, bit;
  201. base = irq_to_base(irq);
  202. bit = irq_to_bit(irq);
  203. tmp = GET_REG(base + REG_GPIO_INTMASK);
  204. tmp &= ~BIT((irq - NR_INTERNAL_IRQS - bit));
  205. SET_REG(base + REG_GPIO_INTMASK, tmp);
  206. return 0;
  207. }
  208. void gpio_irq_enable(rt_uint32_t irq)
  209. {
  210. rt_uint32_t tmp, base, bit;
  211. base = irq_to_base(irq);
  212. bit = irq_to_bit(irq);
  213. tmp = GET_REG(base + REG_GPIO_INTEN);
  214. tmp |= BIT(irq - NR_INTERNAL_IRQS - bit);
  215. SET_REG(base + REG_GPIO_INTEN, tmp);
  216. }
  217. void gpio_irq_disable(rt_uint32_t irq)
  218. {
  219. rt_uint32_t tmp, base, bit;
  220. base = irq_to_base(irq);
  221. bit = irq_to_bit(irq);
  222. tmp = GET_REG(base + REG_GPIO_INTEN);
  223. tmp &= ~BIT((irq - NR_INTERNAL_IRQS - bit));
  224. SET_REG(base + REG_GPIO_INTEN, tmp);
  225. }
  226. static void fh_gpio_interrupt(int irq, void *param)
  227. {
  228. rt_uint32_t irq_status;
  229. int gpio_num, gpio;
  230. rt_uint32_t base;
  231. struct fh_gpio_obj *gpio_obj = (struct fh_gpio_obj *)param;
  232. //rt_kprintf("fh_gpio_interrupt start\n");
  233. //fixme: spin lock???
  234. base = (irq==40) ? GPIO0_REG_BASE : GPIO1_REG_BASE;
  235. irq_status = GET_REG(base + REG_GPIO_INTSTATUS);
  236. if (irq_status == 0) {
  237. rt_kprintf("gpio irq status is zero.\n");
  238. return;
  239. }
  240. /* temporarily mask (level sensitive) parent IRQ */
  241. gpio_irq_mask(irq);
  242. gpio_num = __rt_ffs(irq_status) - 1;
  243. SET_REG(base + REG_GPIO_PORTA_EOI, BIT(gpio_num));
  244. gpio = gpio_num + ((irq==40) ? 0 : 32);
  245. //generic_handle_irq(gpio_to_irq(gpio));
  246. if(irq_desc[gpio_to_irq(gpio)].handler)
  247. irq_desc[gpio_to_irq(gpio)].handler(gpio_to_irq(gpio), irq_desc[gpio_to_irq(gpio)].param);
  248. gpio_irq_mask(irq);
  249. /* now it may re-trigger */
  250. }
  251. int gpio_direction_input(rt_uint32_t gpio)
  252. {
  253. rt_uint32_t reg, base;
  254. if(gpio > NUM_OF_GPIO)
  255. {
  256. rt_kprintf("ERROR: %s, incorrect GPIO num\n", __func__);
  257. return -RT_ERROR;
  258. }
  259. if(!gpio_available[gpio])
  260. {
  261. rt_kprintf("ERROR: %s, GPIO %d is not available\n", __func__, gpio);
  262. return -RT_EBUSY;
  263. }
  264. base = gpio_to_base(gpio);
  265. gpio = gpio % 32;
  266. //fixme: lock
  267. //spin_lock_irqsave(&chip->lock, flags);
  268. reg = GET_REG(base + REG_GPIO_SWPORTA_DDR);
  269. reg &= ~(1 << gpio);
  270. SET_REG(base + REG_GPIO_SWPORTA_DDR, reg);
  271. //spin_unlock_irqrestore(&chip->lock, flags);
  272. return 0;
  273. }
  274. int gpio_direction_output(rt_uint32_t gpio, rt_uint32_t val)
  275. {
  276. rt_uint32_t reg, base;
  277. if(gpio > NUM_OF_GPIO)
  278. {
  279. rt_kprintf("ERROR: %s, incorrect GPIO num\n", __func__);
  280. return -RT_ERROR;
  281. }
  282. if(!gpio_available[gpio])
  283. {
  284. rt_kprintf("ERROR: %s, GPIO %d is not available\n", __func__, gpio);
  285. return -RT_EBUSY;
  286. }
  287. base = gpio_to_base(gpio);
  288. gpio = gpio % 32;
  289. //fixme: lock
  290. //spin_lock_irqsave(&chip->lock, flags);
  291. reg = GET_REG(base + REG_GPIO_SWPORTA_DDR);
  292. reg |= (1 << gpio);
  293. SET_REG(base + REG_GPIO_SWPORTA_DDR, reg);
  294. reg = GET_REG(base + REG_GPIO_SWPORTA_DR);
  295. if(val)
  296. reg |= (1 << gpio);
  297. else
  298. reg &= ~(1 << gpio);
  299. SET_REG(base + REG_GPIO_SWPORTA_DR, reg);
  300. //spin_unlock_irqrestore(&chip->lock, flags);
  301. return 0;
  302. }
  303. int gpio_request(rt_uint32_t gpio)
  304. {
  305. if(gpio > NUM_OF_GPIO)
  306. {
  307. rt_kprintf("ERROR: %s, incorrect GPIO num\n", __func__);
  308. return -RT_ERROR;
  309. }
  310. gpio_available[gpio] = 1;
  311. return 0;
  312. }
  313. int gpio_release(rt_uint32_t gpio)
  314. {
  315. if(gpio > NUM_OF_GPIO)
  316. {
  317. rt_kprintf("ERROR: %s, incorrect GPIO num\n", __func__);
  318. return -RT_ERROR;
  319. }
  320. gpio_available[gpio] = 0;
  321. return 0;
  322. }
  323. int fh_gpio_probe(void *priv_data)
  324. {
  325. struct fh_gpio_obj *gpio_obj = (struct fh_gpio_obj *)priv_data;
  326. int i;
  327. if(gpio_obj->id == 0){
  328. rt_hw_interrupt_install(gpio_obj->irq, fh_gpio_interrupt, (void *)gpio_obj, "gpio_0");
  329. }
  330. else if(gpio_obj->id == 1){
  331. rt_hw_interrupt_install(gpio_obj->irq, fh_gpio_interrupt, (void *)gpio_obj, "gpio_1");
  332. }
  333. rt_hw_interrupt_umask(gpio_obj->irq);
  334. for(i=0; i<32; i++)
  335. {
  336. irq_desc[NR_INTERNAL_IRQS + 32 * gpio_obj->id + i].param = gpio_obj;
  337. }
  338. return 0;
  339. }
  340. int fh_gpio_exit(void *priv_data)
  341. {
  342. return 0;
  343. }
  344. struct fh_board_ops gpio_driver_ops =
  345. {
  346. .probe = fh_gpio_probe,
  347. .exit = fh_gpio_exit,
  348. };
  349. void rt_hw_gpio_init(void)
  350. {
  351. PRINT_GPIO_DBG("%s start\n", __func__);
  352. rt_memset(gpio_available, 0, sizeof(int) * NUM_OF_GPIO);
  353. fh_board_driver_register("gpio", &gpio_driver_ops);
  354. PRINT_GPIO_DBG("%s end\n", __func__);
  355. }