wm_gpio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /**
  2. * @file wm_gpio.c
  3. *
  4. * @brief GPIO Driver Module
  5. *
  6. * @author dave
  7. *
  8. * Copyright (c) 2014 Winner Microelectronics Co., Ltd.
  9. */
  10. #include "wm_gpio.h"
  11. #include "wm_regs.h"
  12. #include "wm_irq.h"
  13. #include "wm_osal.h"
  14. #include "tls_common.h"
  15. struct gpio_irq_context{
  16. tls_gpio_irq_callback callback;
  17. void *arg;
  18. };
  19. static struct gpio_irq_context gpio_context[WM_IO_PB_31 - WM_IO_PA_00 + 1] = {{0,0}};
  20. ATTRIBUTE_ISR void GPIOA_IRQHandler(void)
  21. {
  22. u8 i = 0;
  23. u8 found = 0;
  24. u32 reg = 0;
  25. csi_kernel_intrpt_enter();
  26. reg = tls_reg_read32(HR_GPIO_MIS);
  27. for (i = 0; i <= WM_IO_PA_15; i++)
  28. {
  29. if (reg & BIT(i))
  30. {
  31. found = 1;
  32. break;
  33. }
  34. }
  35. if (found)
  36. {
  37. if (NULL != gpio_context[i].callback)
  38. gpio_context[i].callback(gpio_context[i].arg);
  39. }
  40. csi_kernel_intrpt_exit();
  41. }
  42. ATTRIBUTE_ISR void GPIOB_IRQHandler(void)
  43. {
  44. u8 i = 0;
  45. u8 found = 0;
  46. u32 reg = 0;
  47. csi_kernel_intrpt_enter();
  48. reg = tls_reg_read32(HR_GPIO_MIS + TLS_IO_AB_OFFSET);
  49. for (i = WM_IO_PB_00; i <= WM_IO_PB_31; i++)
  50. {
  51. if (reg & BIT(i - WM_IO_PB_00))
  52. {
  53. found = 1;
  54. break;
  55. }
  56. }
  57. if (found)
  58. {
  59. if (NULL != gpio_context[i].callback)
  60. gpio_context[i].callback(gpio_context[i].arg);
  61. }
  62. csi_kernel_intrpt_exit();
  63. }
  64. /**
  65. * @brief This function is used to config gpio function
  66. *
  67. * @param[in] gpio_pin gpio pin num
  68. * @param[in] dir gpio direction
  69. * @param[in] attr gpio attribute
  70. *
  71. * @return None
  72. *
  73. * @note
  74. */
  75. void tls_gpio_cfg(enum tls_io_name gpio_pin, enum tls_gpio_dir dir, enum tls_gpio_attr attr)
  76. {
  77. u8 pin;
  78. u16 offset;
  79. if (gpio_pin >= WM_IO_PB_00)
  80. {
  81. pin = gpio_pin - WM_IO_PB_00;
  82. offset = TLS_IO_AB_OFFSET;
  83. }
  84. else
  85. {
  86. pin = gpio_pin;
  87. offset = 0;
  88. }
  89. /* enable gpio function */
  90. tls_io_cfg_set(gpio_pin, WM_IO_OPT5_GPIO);
  91. /* gpio direction */
  92. if (WM_GPIO_DIR_OUTPUT == dir)
  93. tls_reg_write32(HR_GPIO_DIR + offset, tls_reg_read32(HR_GPIO_DIR + offset) | BIT(pin)); /* 1 set output */
  94. else if (WM_GPIO_DIR_INPUT == dir)
  95. tls_reg_write32(HR_GPIO_DIR + offset, tls_reg_read32(HR_GPIO_DIR + offset) & (~BIT(pin))); /* 0 set input */
  96. /* gpio attribute */
  97. if (WM_GPIO_ATTR_FLOATING == attr)
  98. {
  99. tls_reg_write32(HR_GPIO_PULLUP_EN + offset, tls_reg_read32(HR_GPIO_PULLUP_EN + offset) | BIT(pin)); /* 1 disable pullup */
  100. tls_reg_write32(HR_GPIO_PULLDOWN_EN + offset, tls_reg_read32(HR_GPIO_PULLDOWN_EN + offset)&(~BIT(pin))); /* 1 disable pulldown */
  101. }
  102. if (WM_GPIO_ATTR_PULLHIGH == attr)
  103. {
  104. tls_reg_write32(HR_GPIO_PULLUP_EN + offset, tls_reg_read32(HR_GPIO_PULLUP_EN + offset) & (~BIT(pin))); /* 0 enable pullup */
  105. tls_reg_write32(HR_GPIO_PULLDOWN_EN + offset, tls_reg_read32(HR_GPIO_PULLDOWN_EN + offset) &(~BIT(pin))); /* 0 disable pulldown */
  106. }
  107. if (WM_GPIO_ATTR_PULLLOW == attr)
  108. {
  109. tls_reg_write32(HR_GPIO_PULLUP_EN + offset, tls_reg_read32(HR_GPIO_PULLUP_EN + offset) | BIT(pin)); /* 0 disable pullup */
  110. tls_reg_write32(HR_GPIO_PULLDOWN_EN + offset, tls_reg_read32(HR_GPIO_PULLDOWN_EN + offset) | BIT(pin)); /* 1 disable pulldown */
  111. }
  112. }
  113. /**
  114. * @brief This function is used to read gpio status
  115. *
  116. * @param[in] gpio_pin gpio pin num
  117. *
  118. * @retval 0 power level is low
  119. * @retval 1 power level is high
  120. *
  121. * @note None
  122. */
  123. u8 tls_gpio_read(enum tls_io_name gpio_pin)
  124. {
  125. u32 reg_en;
  126. u32 reg;
  127. u8 pin;
  128. u16 offset;
  129. if (gpio_pin >= WM_IO_PB_00)
  130. {
  131. pin = gpio_pin - WM_IO_PB_00;
  132. offset = TLS_IO_AB_OFFSET;
  133. }
  134. else
  135. {
  136. pin = gpio_pin;
  137. offset = 0;
  138. }
  139. reg_en = tls_reg_read32(HR_GPIO_DATA_EN + offset);
  140. tls_reg_write32(HR_GPIO_DATA_EN + offset, reg_en | (1 << pin));
  141. reg = tls_reg_read32(HR_GPIO_DATA + offset);
  142. tls_reg_write32(HR_GPIO_DATA_EN + offset, reg_en);
  143. if(reg & (0x1 << pin))
  144. return 1;
  145. else
  146. return 0;
  147. }
  148. /**
  149. * @brief This function is used to modify gpio status
  150. *
  151. * @param[in] gpio_pin gpio pin num
  152. * @param[in] value power level
  153. * 0: low power level
  154. * 1: high power level
  155. *
  156. * @return None
  157. *
  158. * @note None
  159. */
  160. void tls_gpio_write(enum tls_io_name gpio_pin, u8 value)
  161. {
  162. u32 cpu_sr = 0;
  163. u32 reg;
  164. u32 reg_en;
  165. u8 pin;
  166. u16 offset;
  167. if (gpio_pin >= WM_IO_PB_00)
  168. {
  169. pin = gpio_pin - WM_IO_PB_00;
  170. offset = TLS_IO_AB_OFFSET;
  171. }
  172. else
  173. {
  174. pin = gpio_pin;
  175. offset = 0;
  176. }
  177. cpu_sr = tls_os_set_critical();
  178. reg_en = tls_reg_read32(HR_GPIO_DATA_EN + offset);
  179. tls_reg_write32(HR_GPIO_DATA_EN + offset, reg_en | (1 << pin));
  180. reg = tls_reg_read32(HR_GPIO_DATA + offset);
  181. if(value)
  182. tls_reg_write32(HR_GPIO_DATA + offset, reg | (1 << pin)); /* write high */
  183. else
  184. tls_reg_write32(HR_GPIO_DATA + offset, reg & (~(1 << pin)));/* write low */
  185. tls_reg_write32(HR_GPIO_DATA_EN + offset, reg_en);
  186. tls_os_release_critical(cpu_sr);
  187. }
  188. /**
  189. * @brief This function is used to config gpio interrupt
  190. *
  191. * @param[in] gpio_pin gpio pin num
  192. * @param[in] mode interrupt trigger type
  193. *
  194. * @return None
  195. *
  196. * @note None
  197. */
  198. void tls_gpio_irq_enable(enum tls_io_name gpio_pin, enum tls_gpio_irq_trig mode)
  199. {
  200. u32 reg;
  201. u8 pin;
  202. u16 offset;
  203. u8 vec_no;
  204. if (gpio_pin >= WM_IO_PB_00)
  205. {
  206. pin = gpio_pin - WM_IO_PB_00;
  207. offset = TLS_IO_AB_OFFSET;
  208. vec_no = GPIOB_IRQn;
  209. }
  210. else
  211. {
  212. pin = gpio_pin;
  213. offset = 0;
  214. vec_no = GPIOA_IRQn;
  215. }
  216. // TLS_DBGPRT_INFO("\r\ntls_gpio_int_enable gpio pin =%d,mode==%d\r\n",gpio_pin,mode);
  217. switch(mode)
  218. {
  219. case WM_GPIO_IRQ_TRIG_RISING_EDGE:
  220. reg = tls_reg_read32(HR_GPIO_IS + offset);
  221. reg &= (~(0x1 << pin));
  222. // TLS_DBGPRT_INFO("\r\nrising edge is ret=%x\r\n",reg);
  223. tls_reg_write32(HR_GPIO_IS + offset, reg); /* 0 edge trigger */
  224. reg = tls_reg_read32(HR_GPIO_IBE + offset);
  225. reg &= (~(0x1 << pin));
  226. // TLS_DBGPRT_INFO("\r\nrising edge ibe ret=%x\r\n",reg);
  227. tls_reg_write32(HR_GPIO_IBE + offset, reg); /* 0 edge trigger */
  228. reg = tls_reg_read32(HR_GPIO_IEV + offset);
  229. reg |= (0x1 << pin);
  230. // TLS_DBGPRT_INFO("\r\nrising edge iev ret=%x\r\n",reg);
  231. tls_reg_write32(HR_GPIO_IEV + offset, reg); /* 1 rising edge trigger */
  232. break;
  233. case WM_GPIO_IRQ_TRIG_FALLING_EDGE:
  234. reg = tls_reg_read32(HR_GPIO_IS + offset);
  235. reg &= (~(0x1 << pin));
  236. // TLS_DBGPRT_INFO("\falling edge is ret=%x\n",reg);
  237. tls_reg_write32(HR_GPIO_IS + offset, reg); /* 0 edge trigger */
  238. reg = tls_reg_read32(HR_GPIO_IBE + offset);
  239. reg &= (~(0x1 << pin));
  240. // TLS_DBGPRT_INFO("\falling edge ibe ret=%x\n",reg);
  241. tls_reg_write32(HR_GPIO_IBE + offset, reg); /* 0 edge trigger */
  242. reg = tls_reg_read32(HR_GPIO_IEV + offset);
  243. reg &= (~(0x1 << pin));
  244. // TLS_DBGPRT_INFO("\falling edge iev ret=%x\n",reg);
  245. tls_reg_write32(HR_GPIO_IEV + offset, reg); /* 0 falling edge trigger */
  246. break;
  247. case WM_GPIO_IRQ_TRIG_DOUBLE_EDGE:
  248. reg = tls_reg_read32(HR_GPIO_IS + offset);
  249. tls_reg_write32(HR_GPIO_IS + offset, reg & (~(0x1 << pin))); /* 0 edge trigger */
  250. reg = tls_reg_read32(HR_GPIO_IBE + offset);
  251. tls_reg_write32(HR_GPIO_IBE + offset, reg | (0x1 << pin)); /* 1 double edge trigger */
  252. break;
  253. case WM_GPIO_IRQ_TRIG_HIGH_LEVEL:
  254. reg = tls_reg_read32(HR_GPIO_IS + offset);
  255. tls_reg_write32(HR_GPIO_IS + offset, reg | (0x1 << pin)); /* 1 level trigger */
  256. reg = tls_reg_read32(HR_GPIO_IEV + offset);
  257. tls_reg_write32(HR_GPIO_IEV + offset, reg | (0x1 << pin)); /* 1 high level trigger */
  258. break;
  259. case WM_GPIO_IRQ_TRIG_LOW_LEVEL:
  260. reg = tls_reg_read32(HR_GPIO_IS + offset);
  261. tls_reg_write32(HR_GPIO_IS + offset, reg | (0x1 << pin)); /* 1 level trigger */
  262. reg = tls_reg_read32(HR_GPIO_IEV + offset);
  263. tls_reg_write32(HR_GPIO_IEV + offset, reg & (~(0x1 << pin))); /* 0 low level trigger */
  264. break;
  265. }
  266. reg = tls_reg_read32(HR_GPIO_IE + offset);
  267. reg |= (0x1 << pin);
  268. // TLS_DBGPRT_INFO("\nie ret=%x\n",reg);
  269. tls_reg_write32(HR_GPIO_IE + offset, reg); /* enable interrupt */
  270. tls_irq_enable(vec_no);
  271. }
  272. /**
  273. * @brief This function is used to disable gpio interrupt
  274. *
  275. * @param[in] gpio_pin gpio pin num
  276. *
  277. * @return None
  278. *
  279. * @note None
  280. */
  281. void tls_gpio_irq_disable(enum tls_io_name gpio_pin)
  282. {
  283. u32 reg;
  284. u8 pin;
  285. u16 offset;
  286. if (gpio_pin >= WM_IO_PB_00)
  287. {
  288. pin = gpio_pin - WM_IO_PB_00;
  289. offset = TLS_IO_AB_OFFSET;
  290. reg = tls_reg_read32(HR_GPIO_IE + offset);
  291. tls_reg_write32(HR_GPIO_IE + offset, reg & (~(0x1 << pin))); /* disable interrupt */
  292. // tls_irq_disable(GPIOB_IRQn);
  293. }
  294. else
  295. {
  296. pin = gpio_pin;
  297. offset = 0;
  298. reg = tls_reg_read32(HR_GPIO_IE + offset);
  299. tls_reg_write32(HR_GPIO_IE + offset, reg & (~(0x1 << pin))); /* disable interrupt */
  300. // tls_irq_disable(GPIOA_IRQn);
  301. }
  302. }
  303. /**
  304. * @brief This function is used to get gpio interrupt status
  305. *
  306. * @param[in] gpio_pin gpio pin num
  307. *
  308. * @retval 0 no interrupt happened
  309. * @retval 1 interrupt happened
  310. *
  311. * @note None
  312. */
  313. u8 tls_get_gpio_irq_status(enum tls_io_name gpio_pin)
  314. {
  315. u32 reg;
  316. u8 pin;
  317. u16 offset;
  318. if (gpio_pin >= WM_IO_PB_00)
  319. {
  320. pin = gpio_pin - WM_IO_PB_00;
  321. offset = TLS_IO_AB_OFFSET;
  322. }
  323. else
  324. {
  325. pin = gpio_pin;
  326. offset = 0;
  327. }
  328. reg = tls_reg_read32(HR_GPIO_RIS + offset);
  329. if(reg & (0x1 << pin))
  330. return 1;
  331. else
  332. return 0;
  333. }
  334. /**
  335. * @brief This function is used to clear gpio interrupt flag
  336. *
  337. * @param[in] gpio_pin gpio pin num
  338. *
  339. * @return None
  340. *
  341. * @note None
  342. */
  343. void tls_clr_gpio_irq_status(enum tls_io_name gpio_pin)
  344. {
  345. u8 pin;
  346. u16 offset;
  347. if (gpio_pin >= WM_IO_PB_00)
  348. {
  349. pin = gpio_pin - WM_IO_PB_00;
  350. offset = TLS_IO_AB_OFFSET;
  351. }
  352. else
  353. {
  354. pin = gpio_pin;
  355. offset = 0;
  356. }
  357. tls_reg_write32(HR_GPIO_IC + offset, (0x1 << pin)); /* 1 clear interrupt status */
  358. }
  359. /**
  360. * @brief This function is used to register gpio interrupt
  361. *
  362. * @param[in] gpio_pin gpio pin num
  363. * @param[in] callback the gpio interrupt call back function
  364. * @param[in] arg parammeter for the callback
  365. *
  366. * @return None
  367. *
  368. * @note
  369. * gpio callback function is called in interrupt,
  370. * so can not operate the critical data in the callback fuuction,
  371. * recommendation to send messages to other tasks to operate it.
  372. */
  373. void tls_gpio_isr_register(enum tls_io_name gpio_pin,
  374. tls_gpio_irq_callback callback,
  375. void *arg)
  376. {
  377. gpio_context[gpio_pin].callback = callback;
  378. gpio_context[gpio_pin].arg = arg;
  379. }