halmac_gpio_88xx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2016 - 2018 Realtek Corporation. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. ******************************************************************************/
  15. #include "halmac_gpio_88xx.h"
  16. #if HALMAC_88XX_SUPPORT
  17. /**
  18. * pinmux_wl_led_mode_88xx() -control wlan led gpio function
  19. * @adapter : the adapter of halmac
  20. * @mode : wlan led mode
  21. * Author : Ivan Lin
  22. * Return : enum halmac_ret_status
  23. * More details of status code can be found in prototype document
  24. */
  25. enum halmac_ret_status
  26. pinmux_wl_led_mode_88xx(struct halmac_adapter *adapter,
  27. enum halmac_wlled_mode mode)
  28. {
  29. u8 value8;
  30. struct halmac_api *api = (struct halmac_api *)adapter->halmac_api;
  31. PLTFM_MSG_TRACE("[TRACE]%s ===>\n", __func__);
  32. value8 = HALMAC_REG_R8(REG_LED_CFG + 2);
  33. value8 &= ~(BIT(6));
  34. value8 |= BIT(3);
  35. value8 &= ~(BIT(0) | BIT(1) | BIT(2));
  36. switch (mode) {
  37. case HALMAC_WLLED_MODE_TRX:
  38. value8 |= 2;
  39. break;
  40. case HALMAC_WLLED_MODE_TX:
  41. value8 |= 4;
  42. break;
  43. case HALMAC_WLLED_MODE_RX:
  44. value8 |= 6;
  45. break;
  46. case HALMAC_WLLED_MODE_SW_CTRL:
  47. value8 |= 0;
  48. break;
  49. default:
  50. return HALMAC_RET_SWITCH_CASE_ERROR;
  51. }
  52. HALMAC_REG_W8(REG_LED_CFG + 2, value8);
  53. PLTFM_MSG_TRACE("[TRACE]%s <===\n", __func__);
  54. return HALMAC_RET_SUCCESS;
  55. }
  56. /**
  57. * pinmux_wl_led_sw_ctrl_88xx() -control wlan led on/off
  58. * @adapter : the adapter of halmac
  59. * @on : on(1), off(0)
  60. * Author : Ivan Lin
  61. * Return : enum halmac_ret_status
  62. * More details of status code can be found in prototype document
  63. */
  64. void
  65. pinmux_wl_led_sw_ctrl_88xx(struct halmac_adapter *adapter, u8 on)
  66. {
  67. u8 value8;
  68. struct halmac_api *api = (struct halmac_api *)adapter->halmac_api;
  69. value8 = HALMAC_REG_R8(REG_LED_CFG + 2);
  70. value8 = (on == 0) ? value8 | BIT(3) : value8 & ~(BIT(3));
  71. HALMAC_REG_W8(REG_LED_CFG + 2, value8);
  72. }
  73. /**
  74. * pinmux_sdio_int_polarity_88xx() -control sdio int polarity
  75. * @adapter : the adapter of halmac
  76. * @low_active : low active(1), high active(0)
  77. * Author : Ivan Lin
  78. * Return : enum halmac_ret_status
  79. * More details of status code can be found in prototype document
  80. */
  81. void
  82. pinmux_sdio_int_polarity_88xx(struct halmac_adapter *adapter, u8 low_active)
  83. {
  84. u8 value8;
  85. struct halmac_api *api = (struct halmac_api *)adapter->halmac_api;
  86. value8 = HALMAC_REG_R8(REG_SYS_SDIO_CTRL + 2);
  87. value8 = (low_active == 0) ? value8 | BIT(3) : value8 & ~(BIT(3));
  88. HALMAC_REG_W8(REG_SYS_SDIO_CTRL + 2, value8);
  89. }
  90. /**
  91. * pinmux_gpio_mode_88xx() -control gpio io mode
  92. * @adapter : the adapter of halmac
  93. * @gpio_id : gpio0~15(0~15)
  94. * @output : output(1), input(0)
  95. * Author : Ivan Lin
  96. * Return : enum halmac_ret_status
  97. * More details of status code can be found in prototype document
  98. */
  99. enum halmac_ret_status
  100. pinmux_gpio_mode_88xx(struct halmac_adapter *adapter, u8 gpio_id, u8 output)
  101. {
  102. u16 value16;
  103. u8 in_out;
  104. u32 offset;
  105. struct halmac_api *api = (struct halmac_api *)adapter->halmac_api;
  106. if (gpio_id <= 7)
  107. offset = REG_GPIO_PIN_CTRL + 2;
  108. else if (gpio_id >= 8 && gpio_id <= 15)
  109. offset = REG_GPIO_EXT_CTRL + 2;
  110. else
  111. return HALMAC_RET_WRONG_GPIO;
  112. in_out = (output == 0) ? 0 : 1;
  113. gpio_id &= (8 - 1);
  114. value16 = HALMAC_REG_R16(offset);
  115. value16 &= ~((1 << gpio_id) | (1 << gpio_id << 8));
  116. value16 |= (in_out << gpio_id);
  117. HALMAC_REG_W16(offset, value16);
  118. return HALMAC_RET_SUCCESS;
  119. }
  120. /**
  121. * pinmux_gpio_output_88xx() -control gpio output high/low
  122. * @adapter : the adapter of halmac
  123. * @gpio_id : gpio0~15(0~15)
  124. * @high : high(1), low(0)
  125. * Author : Ivan Lin
  126. * Return : enum halmac_ret_status
  127. * More details of status code can be found in prototype document
  128. */
  129. enum halmac_ret_status
  130. pinmux_gpio_output_88xx(struct halmac_adapter *adapter, u8 gpio_id, u8 high)
  131. {
  132. u8 value8;
  133. u8 hi_low;
  134. u32 offset;
  135. struct halmac_api *api = (struct halmac_api *)adapter->halmac_api;
  136. if (gpio_id <= 7)
  137. offset = REG_GPIO_PIN_CTRL + 1;
  138. else if (gpio_id >= 8 && gpio_id <= 15)
  139. offset = REG_GPIO_EXT_CTRL + 1;
  140. else
  141. return HALMAC_RET_WRONG_GPIO;
  142. hi_low = (high == 0) ? 0 : 1;
  143. gpio_id &= (8 - 1);
  144. value8 = HALMAC_REG_R8(offset);
  145. value8 &= ~(1 << gpio_id);
  146. value8 |= (hi_low << gpio_id);
  147. HALMAC_REG_W8(offset, value8);
  148. return HALMAC_RET_SUCCESS;
  149. }
  150. /**
  151. * halmac_pinmux_status_88xx() -get current gpio status(high/low)
  152. * @adapter : the adapter of halmac
  153. * @pin_id : 0~15(0~15)
  154. * @phigh : high(1), low(0)
  155. * Author : Ivan Lin
  156. * Return : enum halmac_ret_status
  157. * More details of status code can be found in prototype document
  158. */
  159. enum halmac_ret_status
  160. pinmux_pin_status_88xx(struct halmac_adapter *adapter, u8 pin_id, u8 *high)
  161. {
  162. u8 value8;
  163. u32 offset;
  164. struct halmac_api *api = (struct halmac_api *)adapter->halmac_api;
  165. if (pin_id <= 7)
  166. offset = REG_GPIO_PIN_CTRL;
  167. else if (pin_id >= 8 && pin_id <= 15)
  168. offset = REG_GPIO_EXT_CTRL;
  169. else
  170. return HALMAC_RET_WRONG_GPIO;
  171. pin_id &= (8 - 1);
  172. value8 = HALMAC_REG_R8(offset);
  173. *high = (value8 & (1 << pin_id)) >> pin_id;
  174. return HALMAC_RET_SUCCESS;
  175. }
  176. enum halmac_ret_status
  177. pinmux_parser_88xx(struct halmac_adapter *adapter,
  178. const struct halmac_gpio_pimux_list *list, u32 size,
  179. u32 gpio_id, u32 *cur_func)
  180. {
  181. u32 i;
  182. u8 value8;
  183. const struct halmac_gpio_pimux_list *cur_list = list;
  184. enum halmac_gpio_cfg_state *state;
  185. struct halmac_api *api = (struct halmac_api *)adapter->halmac_api;
  186. state = &adapter->halmac_state.gpio_cfg_state;
  187. if (*state == HALMAC_GPIO_CFG_STATE_BUSY)
  188. return HALMAC_RET_BUSY_STATE;
  189. *state = HALMAC_GPIO_CFG_STATE_BUSY;
  190. for (i = 0; i < size; i++) {
  191. if (gpio_id != cur_list->id) {
  192. PLTFM_MSG_ERR("[ERR]offset:%X, value:%X, func:%X\n",
  193. cur_list->offset, cur_list->value,
  194. cur_list->func);
  195. PLTFM_MSG_ERR("[ERR]id1 : %X, id2 : %X\n",
  196. gpio_id, cur_list->id);
  197. *state = HALMAC_GPIO_CFG_STATE_IDLE;
  198. return HALMAC_RET_GET_PINMUX_ERR;
  199. }
  200. value8 = HALMAC_REG_R8(cur_list->offset);
  201. value8 &= cur_list->msk;
  202. if (value8 == cur_list->value) {
  203. *cur_func = cur_list->func;
  204. break;
  205. }
  206. cur_list++;
  207. }
  208. *state = HALMAC_GPIO_CFG_STATE_IDLE;
  209. if (i == size)
  210. return HALMAC_RET_GET_PINMUX_ERR;
  211. return HALMAC_RET_SUCCESS;
  212. }
  213. enum halmac_ret_status
  214. pinmux_switch_88xx(struct halmac_adapter *adapter,
  215. const struct halmac_gpio_pimux_list *list, u32 size,
  216. u32 gpio_id, enum halmac_gpio_func gpio_func)
  217. {
  218. u32 i;
  219. u8 value8;
  220. u16 switch_func;
  221. const struct halmac_gpio_pimux_list *cur_list = list;
  222. enum halmac_gpio_cfg_state *state;
  223. struct halmac_api *api = (struct halmac_api *)adapter->halmac_api;
  224. state = &adapter->halmac_state.gpio_cfg_state;
  225. if (*state == HALMAC_GPIO_CFG_STATE_BUSY)
  226. return HALMAC_RET_BUSY_STATE;
  227. switch (gpio_func) {
  228. case HALMAC_GPIO_FUNC_WL_LED:
  229. switch_func = HALMAC_WL_LED;
  230. break;
  231. case HALMAC_GPIO_FUNC_SDIO_INT:
  232. switch_func = HALMAC_SDIO_INT;
  233. break;
  234. case HALMAC_GPIO_FUNC_BT_HOST_WAKE1:
  235. case HALMAC_GPIO_FUNC_BT_DEV_WAKE1:
  236. switch_func = HALMAC_GPIO13_14_WL_CTRL_EN;
  237. break;
  238. case HALMAC_GPIO_FUNC_SW_IO_0:
  239. case HALMAC_GPIO_FUNC_SW_IO_1:
  240. case HALMAC_GPIO_FUNC_SW_IO_2:
  241. case HALMAC_GPIO_FUNC_SW_IO_3:
  242. case HALMAC_GPIO_FUNC_SW_IO_4:
  243. case HALMAC_GPIO_FUNC_SW_IO_5:
  244. case HALMAC_GPIO_FUNC_SW_IO_6:
  245. case HALMAC_GPIO_FUNC_SW_IO_7:
  246. case HALMAC_GPIO_FUNC_SW_IO_8:
  247. case HALMAC_GPIO_FUNC_SW_IO_9:
  248. case HALMAC_GPIO_FUNC_SW_IO_10:
  249. case HALMAC_GPIO_FUNC_SW_IO_11:
  250. case HALMAC_GPIO_FUNC_SW_IO_12:
  251. case HALMAC_GPIO_FUNC_SW_IO_13:
  252. case HALMAC_GPIO_FUNC_SW_IO_14:
  253. case HALMAC_GPIO_FUNC_SW_IO_15:
  254. switch_func = HALMAC_SW_IO;
  255. break;
  256. default:
  257. return HALMAC_RET_SWITCH_CASE_ERROR;
  258. }
  259. for (i = 0; i < size; i++) {
  260. if (gpio_id != cur_list->id) {
  261. PLTFM_MSG_ERR("[ERR]offset:%X, value:%X, func:%X\n",
  262. cur_list->offset, cur_list->value,
  263. cur_list->func);
  264. PLTFM_MSG_ERR("[ERR]id1 : %X, id2 : %X\n",
  265. gpio_id, cur_list->id);
  266. return HALMAC_RET_GET_PINMUX_ERR;
  267. }
  268. if (switch_func == cur_list->func)
  269. break;
  270. cur_list++;
  271. }
  272. if (i == size) {
  273. PLTFM_MSG_ERR("[ERR]gpio func error:%X %X\n",
  274. gpio_id, cur_list->id);
  275. return HALMAC_RET_GET_PINMUX_ERR;
  276. }
  277. *state = HALMAC_GPIO_CFG_STATE_BUSY;
  278. cur_list = list;
  279. for (i = 0; i < size; i++) {
  280. value8 = HALMAC_REG_R8(cur_list->offset);
  281. value8 &= ~(cur_list->msk);
  282. if (switch_func == cur_list->func) {
  283. value8 |= (cur_list->value & cur_list->msk);
  284. HALMAC_REG_W8(cur_list->offset, value8);
  285. break;
  286. }
  287. value8 |= (~cur_list->value & cur_list->msk);
  288. HALMAC_REG_W8(cur_list->offset, value8);
  289. cur_list++;
  290. }
  291. *state = HALMAC_GPIO_CFG_STATE_IDLE;
  292. return HALMAC_RET_SUCCESS;
  293. }
  294. enum halmac_ret_status
  295. pinmux_record_88xx(struct halmac_adapter *adapter,
  296. enum halmac_gpio_func gpio_func, u8 val)
  297. {
  298. switch (gpio_func) {
  299. case HALMAC_GPIO_FUNC_WL_LED:
  300. adapter->pinmux_info.wl_led = val;
  301. break;
  302. case HALMAC_GPIO_FUNC_SDIO_INT:
  303. adapter->pinmux_info.sdio_int = val;
  304. break;
  305. case HALMAC_GPIO_FUNC_BT_HOST_WAKE1:
  306. adapter->pinmux_info.bt_host_wake = val;
  307. break;
  308. case HALMAC_GPIO_FUNC_BT_DEV_WAKE1:
  309. adapter->pinmux_info.bt_dev_wake = val;
  310. break;
  311. case HALMAC_GPIO_FUNC_SW_IO_0:
  312. adapter->pinmux_info.sw_io_0 = val;
  313. break;
  314. case HALMAC_GPIO_FUNC_SW_IO_1:
  315. adapter->pinmux_info.sw_io_1 = val;
  316. break;
  317. case HALMAC_GPIO_FUNC_SW_IO_2:
  318. adapter->pinmux_info.sw_io_2 = val;
  319. break;
  320. case HALMAC_GPIO_FUNC_SW_IO_3:
  321. adapter->pinmux_info.sw_io_3 = val;
  322. break;
  323. case HALMAC_GPIO_FUNC_SW_IO_4:
  324. adapter->pinmux_info.sw_io_4 = val;
  325. break;
  326. case HALMAC_GPIO_FUNC_SW_IO_5:
  327. adapter->pinmux_info.sw_io_5 = val;
  328. break;
  329. case HALMAC_GPIO_FUNC_SW_IO_6:
  330. adapter->pinmux_info.sw_io_6 = val;
  331. break;
  332. case HALMAC_GPIO_FUNC_SW_IO_7:
  333. adapter->pinmux_info.sw_io_7 = val;
  334. break;
  335. case HALMAC_GPIO_FUNC_SW_IO_8:
  336. adapter->pinmux_info.sw_io_8 = val;
  337. break;
  338. case HALMAC_GPIO_FUNC_SW_IO_9:
  339. adapter->pinmux_info.sw_io_9 = val;
  340. break;
  341. case HALMAC_GPIO_FUNC_SW_IO_10:
  342. adapter->pinmux_info.sw_io_10 = val;
  343. break;
  344. case HALMAC_GPIO_FUNC_SW_IO_11:
  345. adapter->pinmux_info.sw_io_11 = val;
  346. break;
  347. case HALMAC_GPIO_FUNC_SW_IO_12:
  348. adapter->pinmux_info.sw_io_12 = val;
  349. break;
  350. case HALMAC_GPIO_FUNC_SW_IO_13:
  351. adapter->pinmux_info.sw_io_13 = val;
  352. break;
  353. case HALMAC_GPIO_FUNC_SW_IO_14:
  354. adapter->pinmux_info.sw_io_14 = val;
  355. break;
  356. case HALMAC_GPIO_FUNC_SW_IO_15:
  357. adapter->pinmux_info.sw_io_15 = val;
  358. break;
  359. default:
  360. return HALMAC_RET_GET_PINMUX_ERR;
  361. }
  362. return HALMAC_RET_SUCCESS;
  363. }
  364. #endif /* HALMAC_88XX_SUPPORT */