wm_gpio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. //hyj
  189. void tls_gpio_pulse(enum tls_io_name gpio_pin,u16 delay,u8* level,u16 len)
  190. {
  191. u32 cpu_sr = 0;
  192. u32 reg;
  193. u32 reg_en;
  194. u8 pin;
  195. u16 offset;
  196. u16 i;
  197. volatile u32 del=delay;
  198. if (gpio_pin >= WM_IO_PB_00)
  199. {
  200. pin = gpio_pin - WM_IO_PB_00;
  201. offset = TLS_IO_AB_OFFSET;
  202. }
  203. else
  204. {
  205. pin = gpio_pin;
  206. offset = 0;
  207. }
  208. cpu_sr = tls_os_set_critical();
  209. reg_en = tls_reg_read32(HR_GPIO_DATA_EN + offset);
  210. tls_reg_write32(HR_GPIO_DATA_EN + offset, reg_en | (1 << pin));
  211. reg = tls_reg_read32(HR_GPIO_DATA + offset);
  212. for(i=0;i<len;i++)
  213. {
  214. if(level[i/8]&(0x80>>(i%8)))
  215. tls_reg_write32(HR_GPIO_DATA + offset, reg | (1 << pin)); /* write high */
  216. else
  217. tls_reg_write32(HR_GPIO_DATA + offset, reg & (~(1 << pin)));/* write low */
  218. del = delay;
  219. while(del--);
  220. }
  221. tls_reg_write32(HR_GPIO_DATA_EN + offset, reg_en);
  222. tls_os_release_critical(cpu_sr);
  223. }
  224. /**
  225. * @brief This function is used to config gpio interrupt
  226. *
  227. * @param[in] gpio_pin gpio pin num
  228. * @param[in] mode interrupt trigger type
  229. *
  230. * @return None
  231. *
  232. * @note None
  233. */
  234. void tls_gpio_irq_enable(enum tls_io_name gpio_pin, enum tls_gpio_irq_trig mode)
  235. {
  236. u32 reg;
  237. u8 pin;
  238. u16 offset;
  239. u8 vec_no;
  240. if (gpio_pin >= WM_IO_PB_00)
  241. {
  242. pin = gpio_pin - WM_IO_PB_00;
  243. offset = TLS_IO_AB_OFFSET;
  244. vec_no = GPIOB_IRQn;
  245. }
  246. else
  247. {
  248. pin = gpio_pin;
  249. offset = 0;
  250. vec_no = GPIOA_IRQn;
  251. }
  252. // TLS_DBGPRT_INFO("\r\ntls_gpio_int_enable gpio pin =%d,mode==%d\r\n",gpio_pin,mode);
  253. switch(mode)
  254. {
  255. case WM_GPIO_IRQ_TRIG_RISING_EDGE:
  256. reg = tls_reg_read32(HR_GPIO_IS + offset);
  257. reg &= (~(0x1 << pin));
  258. // TLS_DBGPRT_INFO("\r\nrising edge is ret=%x\r\n",reg);
  259. tls_reg_write32(HR_GPIO_IS + offset, reg); /* 0 edge trigger */
  260. reg = tls_reg_read32(HR_GPIO_IBE + offset);
  261. reg &= (~(0x1 << pin));
  262. // TLS_DBGPRT_INFO("\r\nrising edge ibe ret=%x\r\n",reg);
  263. tls_reg_write32(HR_GPIO_IBE + offset, reg); /* 0 edge trigger */
  264. reg = tls_reg_read32(HR_GPIO_IEV + offset);
  265. reg |= (0x1 << pin);
  266. // TLS_DBGPRT_INFO("\r\nrising edge iev ret=%x\r\n",reg);
  267. tls_reg_write32(HR_GPIO_IEV + offset, reg); /* 1 rising edge trigger */
  268. break;
  269. case WM_GPIO_IRQ_TRIG_FALLING_EDGE:
  270. reg = tls_reg_read32(HR_GPIO_IS + offset);
  271. reg &= (~(0x1 << pin));
  272. // TLS_DBGPRT_INFO("\falling edge is ret=%x\n",reg);
  273. tls_reg_write32(HR_GPIO_IS + offset, reg); /* 0 edge trigger */
  274. reg = tls_reg_read32(HR_GPIO_IBE + offset);
  275. reg &= (~(0x1 << pin));
  276. // TLS_DBGPRT_INFO("\falling edge ibe ret=%x\n",reg);
  277. tls_reg_write32(HR_GPIO_IBE + offset, reg); /* 0 edge trigger */
  278. reg = tls_reg_read32(HR_GPIO_IEV + offset);
  279. reg &= (~(0x1 << pin));
  280. // TLS_DBGPRT_INFO("\falling edge iev ret=%x\n",reg);
  281. tls_reg_write32(HR_GPIO_IEV + offset, reg); /* 0 falling edge trigger */
  282. break;
  283. case WM_GPIO_IRQ_TRIG_DOUBLE_EDGE:
  284. reg = tls_reg_read32(HR_GPIO_IS + offset);
  285. tls_reg_write32(HR_GPIO_IS + offset, reg & (~(0x1 << pin))); /* 0 edge trigger */
  286. reg = tls_reg_read32(HR_GPIO_IBE + offset);
  287. tls_reg_write32(HR_GPIO_IBE + offset, reg | (0x1 << pin)); /* 1 double edge trigger */
  288. break;
  289. case WM_GPIO_IRQ_TRIG_HIGH_LEVEL:
  290. reg = tls_reg_read32(HR_GPIO_IS + offset);
  291. tls_reg_write32(HR_GPIO_IS + offset, reg | (0x1 << pin)); /* 1 level trigger */
  292. reg = tls_reg_read32(HR_GPIO_IEV + offset);
  293. tls_reg_write32(HR_GPIO_IEV + offset, reg | (0x1 << pin)); /* 1 high level trigger */
  294. break;
  295. case WM_GPIO_IRQ_TRIG_LOW_LEVEL:
  296. reg = tls_reg_read32(HR_GPIO_IS + offset);
  297. tls_reg_write32(HR_GPIO_IS + offset, reg | (0x1 << pin)); /* 1 level trigger */
  298. reg = tls_reg_read32(HR_GPIO_IEV + offset);
  299. tls_reg_write32(HR_GPIO_IEV + offset, reg & (~(0x1 << pin))); /* 0 low level trigger */
  300. break;
  301. }
  302. reg = tls_reg_read32(HR_GPIO_IE + offset);
  303. reg |= (0x1 << pin);
  304. // TLS_DBGPRT_INFO("\nie ret=%x\n",reg);
  305. tls_reg_write32(HR_GPIO_IE + offset, reg); /* enable interrupt */
  306. tls_irq_enable(vec_no);
  307. }
  308. /**
  309. * @brief This function is used to disable gpio interrupt
  310. *
  311. * @param[in] gpio_pin gpio pin num
  312. *
  313. * @return None
  314. *
  315. * @note None
  316. */
  317. void tls_gpio_irq_disable(enum tls_io_name gpio_pin)
  318. {
  319. u32 reg;
  320. u8 pin;
  321. u16 offset;
  322. if (gpio_pin >= WM_IO_PB_00)
  323. {
  324. pin = gpio_pin - WM_IO_PB_00;
  325. offset = TLS_IO_AB_OFFSET;
  326. reg = tls_reg_read32(HR_GPIO_IE + offset);
  327. tls_reg_write32(HR_GPIO_IE + offset, reg & (~(0x1 << pin))); /* disable interrupt */
  328. // tls_irq_disable(GPIOB_IRQn);
  329. }
  330. else
  331. {
  332. pin = gpio_pin;
  333. offset = 0;
  334. reg = tls_reg_read32(HR_GPIO_IE + offset);
  335. tls_reg_write32(HR_GPIO_IE + offset, reg & (~(0x1 << pin))); /* disable interrupt */
  336. // tls_irq_disable(GPIOA_IRQn);
  337. }
  338. }
  339. /**
  340. * @brief This function is used to get gpio interrupt status
  341. *
  342. * @param[in] gpio_pin gpio pin num
  343. *
  344. * @retval 0 no interrupt happened
  345. * @retval 1 interrupt happened
  346. *
  347. * @note None
  348. */
  349. u8 tls_get_gpio_irq_status(enum tls_io_name gpio_pin)
  350. {
  351. u32 reg;
  352. u8 pin;
  353. u16 offset;
  354. if (gpio_pin >= WM_IO_PB_00)
  355. {
  356. pin = gpio_pin - WM_IO_PB_00;
  357. offset = TLS_IO_AB_OFFSET;
  358. }
  359. else
  360. {
  361. pin = gpio_pin;
  362. offset = 0;
  363. }
  364. reg = tls_reg_read32(HR_GPIO_RIS + offset);
  365. if(reg & (0x1 << pin))
  366. return 1;
  367. else
  368. return 0;
  369. }
  370. /**
  371. * @brief This function is used to clear gpio interrupt flag
  372. *
  373. * @param[in] gpio_pin gpio pin num
  374. *
  375. * @return None
  376. *
  377. * @note None
  378. */
  379. void tls_clr_gpio_irq_status(enum tls_io_name gpio_pin)
  380. {
  381. u8 pin;
  382. u16 offset;
  383. if (gpio_pin >= WM_IO_PB_00)
  384. {
  385. pin = gpio_pin - WM_IO_PB_00;
  386. offset = TLS_IO_AB_OFFSET;
  387. }
  388. else
  389. {
  390. pin = gpio_pin;
  391. offset = 0;
  392. }
  393. tls_reg_write32(HR_GPIO_IC + offset, (0x1 << pin)); /* 1 clear interrupt status */
  394. }
  395. /**
  396. * @brief This function is used to register gpio interrupt
  397. *
  398. * @param[in] gpio_pin gpio pin num
  399. * @param[in] callback the gpio interrupt call back function
  400. * @param[in] arg parammeter for the callback
  401. *
  402. * @return None
  403. *
  404. * @note
  405. * gpio callback function is called in interrupt,
  406. * so can not operate the critical data in the callback fuuction,
  407. * recommendation to send messages to other tasks to operate it.
  408. */
  409. void tls_gpio_isr_register(enum tls_io_name gpio_pin,
  410. tls_gpio_irq_callback callback,
  411. void *arg)
  412. {
  413. gpio_context[gpio_pin].callback = callback;
  414. gpio_context[gpio_pin].arg = arg;
  415. }