wmt_detect.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * Copyright (C) 2011-2014 MediaTek Inc.
  3. *
  4. * This program is free software: you can redistribute it and/or modify it under the terms of the
  5. * GNU General Public License version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  8. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. * See the GNU General Public License for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License along with this program.
  12. * If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <mtk_wcn_cmb_stub.h>
  15. #include <linux/platform_device.h>
  16. #ifdef DFT_TAG
  17. #undef DFT_TAG
  18. #endif
  19. #define DFT_TAG "[WMT-DETECT]"
  20. #include "wmt_detect.h"
  21. #include "wmt_gpio.h"
  22. #if MTK_WCN_REMOVE_KO
  23. #include "conn_drv_init.h"
  24. #endif
  25. #ifdef CONFIG_COMPAT
  26. #include <linux/compat.h>
  27. #endif
  28. #define WMT_DETECT_MAJOR 154
  29. #define WMT_DETECT_DEV_NUM 1
  30. #define WMT_DETECT_DRVIER_NAME "mtk_wcn_detect"
  31. #define WMT_DETECT_DEVICE_NAME "wmtdetect"
  32. struct class *pDetectClass = NULL;
  33. struct device *pDetectDev = NULL;
  34. static int gWmtDetectMajor = WMT_DETECT_MAJOR;
  35. static struct cdev gWmtDetectCdev;
  36. unsigned int gWmtDetectDbgLvl = WMT_DETECT_LOG_INFO;
  37. #ifdef MTK_WCN_COMBO_CHIP_SUPPORT
  38. inline unsigned int wmt_plat_get_soc_chipid(void)
  39. {
  40. WMT_DETECT_INFO_FUNC("no soc chip supported, due to MTK_WCN_SOC_CHIP_SUPPORT is not set.\n");
  41. return -1;
  42. }
  43. #endif
  44. static int wmt_detect_open(struct inode *inode, struct file *file)
  45. {
  46. WMT_DETECT_INFO_FUNC("open major %d minor %d (pid %d)\n", imajor(inode), iminor(inode), current->pid);
  47. return 0;
  48. }
  49. static int wmt_detect_close(struct inode *inode, struct file *file)
  50. {
  51. WMT_DETECT_INFO_FUNC("close major %d minor %d (pid %d)\n", imajor(inode), iminor(inode), current->pid);
  52. return 0;
  53. }
  54. static ssize_t wmt_detect_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
  55. {
  56. WMT_DETECT_INFO_FUNC(" ++\n");
  57. WMT_DETECT_INFO_FUNC(" --\n");
  58. return 0;
  59. }
  60. ssize_t wmt_detect_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos)
  61. {
  62. WMT_DETECT_INFO_FUNC(" ++\n");
  63. WMT_DETECT_INFO_FUNC(" --\n");
  64. return 0;
  65. }
  66. static long wmt_detect_unlocked_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  67. {
  68. int retval = 0;
  69. WMT_DETECT_INFO_FUNC("cmd (%d),arg(%ld)\n", cmd, arg);
  70. switch (cmd) {
  71. case COMBO_IOCTL_GET_CHIP_ID:
  72. /*just get chipid from sdio-detect module */
  73. /*check if external combo chip exists or not */
  74. /*if yes, just return combo chip id */
  75. /*if no, get soc chipid */
  76. retval = mtk_wcn_wmt_chipid_query();
  77. break;
  78. case COMBO_IOCTL_SET_CHIP_ID:
  79. mtk_wcn_wmt_set_chipid(arg);
  80. break;
  81. case COMBO_IOCTL_EXT_CHIP_PWR_ON:
  82. retval = wmt_detect_ext_chip_pwr_on();
  83. break;
  84. case COMBO_IOCTL_EXT_CHIP_DETECT:
  85. retval = wmt_detect_ext_chip_detect();
  86. break;
  87. case COMBO_IOCTL_EXT_CHIP_PWR_OFF:
  88. retval = wmt_detect_ext_chip_pwr_off();
  89. break;
  90. case COMBO_IOCTL_DO_SDIO_AUDOK:
  91. retval = sdio_detect_do_autok(arg);
  92. break;
  93. case COMBO_IOCTL_GET_SOC_CHIP_ID:
  94. retval = wmt_plat_get_soc_chipid();
  95. /*get soc chipid by HAL interface */
  96. break;
  97. case COMBO_IOCTL_MODULE_CLEANUP:
  98. #if (MTK_WCN_REMOVE_KO)
  99. /*deinit SDIO-DETECT module */
  100. retval = sdio_detect_exit();
  101. #else
  102. WMT_DETECT_INFO_FUNC("no MTK_WCN_REMOVE_KO defined\n");
  103. #endif
  104. break;
  105. case COMBO_IOCTL_DO_MODULE_INIT:
  106. #if (MTK_WCN_REMOVE_KO)
  107. /*deinit SDIO-DETECT module */
  108. retval = do_connectivity_driver_init(arg);
  109. #else
  110. WMT_DETECT_INFO_FUNC("no MTK_WCN_REMOVE_KO defined\n");
  111. #endif
  112. break;
  113. default:
  114. WMT_DETECT_WARN_FUNC("unknown cmd (%d)\n", cmd);
  115. retval = 0;
  116. break;
  117. }
  118. return retval;
  119. }
  120. #ifdef CONFIG_COMPAT
  121. static long WMT_compat_detect_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  122. {
  123. long ret;
  124. WMT_DETECT_INFO_FUNC("cmd (%d)\n", cmd);
  125. ret = wmt_detect_unlocked_ioctl(filp, cmd, arg);
  126. return ret;
  127. }
  128. #endif
  129. const struct file_operations gWmtDetectFops = {
  130. .open = wmt_detect_open,
  131. .release = wmt_detect_close,
  132. .read = wmt_detect_read,
  133. .write = wmt_detect_write,
  134. .unlocked_ioctl = wmt_detect_unlocked_ioctl,
  135. #ifdef CONFIG_COMPAT
  136. .compat_ioctl = WMT_compat_detect_ioctl,
  137. #endif
  138. };
  139. int wmt_detect_ext_chip_pwr_on(void)
  140. {
  141. /*pre power on external chip */
  142. /* wmt_plat_pwr_ctrl(FUNC_ON); */
  143. #ifdef MTK_WCN_COMBO_CHIP_SUPPORT
  144. WMT_DETECT_INFO_FUNC("++\n");
  145. if (0 != wmt_detect_chip_pwr_ctrl(1))
  146. return -1;
  147. if (0 != wmt_detect_sdio_pwr_ctrl(1))
  148. return -2;
  149. return 0;
  150. #else
  151. WMT_DETECT_INFO_FUNC("combo chip is not supported\n");
  152. return -1;
  153. #endif
  154. }
  155. int wmt_detect_ext_chip_pwr_off(void)
  156. {
  157. /*pre power off external chip */
  158. /* wmt_plat_pwr_ctrl(FUNC_OFF); */
  159. #ifdef MTK_WCN_COMBO_CHIP_SUPPORT
  160. WMT_DETECT_INFO_FUNC("--\n");
  161. wmt_detect_sdio_pwr_ctrl(0);
  162. return wmt_detect_chip_pwr_ctrl(0);
  163. #else
  164. WMT_DETECT_INFO_FUNC("combo chip is not supported\n");
  165. return 0;
  166. #endif
  167. }
  168. int wmt_detect_ext_chip_detect(void)
  169. {
  170. int iRet = -1;
  171. unsigned int chipId = -1;
  172. /*if there is no external combo chip, return -1 */
  173. int bgfEintStatus = -1;
  174. WMT_DETECT_INFO_FUNC("++\n");
  175. /*wait for a stable time */
  176. msleep(20);
  177. /*read BGF_EINT_PIN status */
  178. bgfEintStatus = wmt_detect_read_ext_cmb_status();
  179. if (0 == bgfEintStatus) {
  180. /*external chip does not exist */
  181. WMT_DETECT_INFO_FUNC("external combo chip not detected\n");
  182. } else if (1 == bgfEintStatus) {
  183. /*combo chip exists */
  184. WMT_DETECT_INFO_FUNC("external combo chip detected\n");
  185. /*detect chipid by sdio_detect module */
  186. chipId = sdio_detect_query_chipid(1);
  187. if (0 <= hif_sdio_is_chipid_valid(chipId))
  188. WMT_DETECT_INFO_FUNC("valid external combo chip id (0x%x)\n", chipId);
  189. else
  190. WMT_DETECT_INFO_FUNC("invalid external combo chip id (0x%x)\n", chipId);
  191. iRet = 0;
  192. } else {
  193. /*Error exists */
  194. WMT_DETECT_ERR_FUNC("error happens when detecting combo chip\n");
  195. }
  196. WMT_DETECT_INFO_FUNC("--\n");
  197. /*return 0 */
  198. return iRet;
  199. /*todo: if there is external combo chip, power on chip return 0 */
  200. }
  201. #ifdef MTK_WCN_COMBO_CHIP_SUPPORT
  202. static int wmt_detect_probe(struct platform_device *pdev)
  203. {
  204. int ret = 0;
  205. WMT_DETECT_ERR_FUNC("platform name: %s\n", pdev->name);
  206. ret = wmt_gpio_init(pdev);
  207. if (-1 == ret)
  208. WMT_DETECT_ERR_FUNC("gpio init fail ret:%d\n", ret);
  209. return ret;
  210. }
  211. static int wmt_detect_remove(struct platform_device *pdev)
  212. {
  213. wmt_gpio_deinit();
  214. return 0;
  215. }
  216. #endif
  217. #ifdef MTK_WCN_COMBO_CHIP_SUPPORT
  218. static struct of_device_id wmt_detect_match[] = {
  219. { .compatible = "mediatek,connectivity-combo", },
  220. {}
  221. };
  222. MODULE_DEVICE_TABLE(of, wmt_detect_match);
  223. static struct platform_driver wmt_detect_driver = {
  224. .probe = wmt_detect_probe,
  225. .remove = wmt_detect_remove,
  226. .driver = {
  227. .owner = THIS_MODULE,
  228. .name = "mediatek,connectivity-combo",
  229. .of_match_table = wmt_detect_match,
  230. },
  231. };
  232. #endif
  233. /*module_platform_driver(wmt_detect_driver);*/
  234. static int wmt_detect_driver_init(void)
  235. {
  236. dev_t devID = MKDEV(gWmtDetectMajor, 0);
  237. int cdevErr = -1;
  238. int ret = -1;
  239. ret = register_chrdev_region(devID, WMT_DETECT_DEV_NUM, WMT_DETECT_DRVIER_NAME);
  240. if (ret) {
  241. WMT_DETECT_ERR_FUNC("fail to register chrdev\n");
  242. return ret;
  243. }
  244. cdev_init(&gWmtDetectCdev, &gWmtDetectFops);
  245. gWmtDetectCdev.owner = THIS_MODULE;
  246. cdevErr = cdev_add(&gWmtDetectCdev, devID, WMT_DETECT_DEV_NUM);
  247. if (cdevErr) {
  248. WMT_DETECT_ERR_FUNC("cdev_add() fails (%d)\n", cdevErr);
  249. goto err1;
  250. }
  251. pDetectClass = class_create(THIS_MODULE, WMT_DETECT_DEVICE_NAME);
  252. if (IS_ERR(pDetectClass)) {
  253. WMT_DETECT_ERR_FUNC("class create fail, error code(%ld)\n", PTR_ERR(pDetectClass));
  254. goto err1;
  255. }
  256. pDetectDev = device_create(pDetectClass, NULL, devID, NULL, WMT_DETECT_DEVICE_NAME);
  257. if (IS_ERR(pDetectDev)) {
  258. WMT_DETECT_ERR_FUNC("device create fail, error code(%ld)\n", PTR_ERR(pDetectDev));
  259. goto err2;
  260. }
  261. WMT_DETECT_INFO_FUNC("driver(major %d) installed success\n", gWmtDetectMajor);
  262. /*init SDIO-DETECT module */
  263. sdio_detect_init();
  264. #ifdef MTK_WCN_COMBO_CHIP_SUPPORT
  265. ret = platform_driver_register(&wmt_detect_driver);
  266. if (ret)
  267. WMT_DETECT_ERR_FUNC("platform driver register fail ret:%d\n", ret);
  268. #endif
  269. return 0;
  270. err2:
  271. if (pDetectClass) {
  272. class_destroy(pDetectClass);
  273. pDetectClass = NULL;
  274. }
  275. err1:
  276. if (cdevErr == 0)
  277. cdev_del(&gWmtDetectCdev);
  278. if (ret == 0) {
  279. unregister_chrdev_region(devID, WMT_DETECT_DEV_NUM);
  280. gWmtDetectMajor = -1;
  281. }
  282. WMT_DETECT_ERR_FUNC("fail\n");
  283. return -1;
  284. }
  285. static void wmt_detect_driver_exit(void)
  286. {
  287. dev_t dev = MKDEV(gWmtDetectMajor, 0);
  288. if (pDetectDev) {
  289. device_destroy(pDetectClass, dev);
  290. pDetectDev = NULL;
  291. }
  292. if (pDetectClass) {
  293. class_destroy(pDetectClass);
  294. pDetectClass = NULL;
  295. }
  296. cdev_del(&gWmtDetectCdev);
  297. unregister_chrdev_region(dev, WMT_DETECT_DEV_NUM);
  298. #if !(MTK_WCN_REMOVE_KO)
  299. /*deinit SDIO-DETECT module*/
  300. sdio_detect_exit();
  301. #endif
  302. #ifdef MTK_WCN_COMBO_CHIP_SUPPORT
  303. platform_driver_unregister(&wmt_detect_driver);
  304. #endif
  305. WMT_DETECT_INFO_FUNC("done\n");
  306. }
  307. module_init(wmt_detect_driver_init);
  308. module_exit(wmt_detect_driver_exit);
  309. MODULE_LICENSE("GPL");
  310. MODULE_AUTHOR("Zhiguo.Niu & Chaozhong.Liang @ MBJ/WCNSE/SS1");
  311. module_param(gWmtDetectMajor, uint, 0);