extcon_usb.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Copyright (C) 2017 MediaTek Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. * See http://www.gnu.org/licenses/gpl-2.0.html for more details.
  12. */
  13. #include <linux/extcon.h>
  14. #include <linux/init.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/irq.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/slab.h>
  21. #include <linux/workqueue.h>
  22. #include "extcon_usb.h"
  23. struct usb_extcon_info {
  24. struct device *dev;
  25. struct extcon_dev *edev;
  26. unsigned int dr; /* data role */
  27. struct workqueue_struct *extcon_workq;
  28. };
  29. struct mt_usb_work {
  30. struct delayed_work dwork;
  31. unsigned int dr; /* data role */
  32. };
  33. static const unsigned int usb_extcon_cable[] = {
  34. EXTCON_USB,
  35. EXTCON_USB_HOST,
  36. EXTCON_NONE,
  37. };
  38. enum {
  39. DUAL_PROP_MODE_UFP = 0,
  40. DUAL_PROP_MODE_DFP,
  41. DUAL_PROP_MODE_NONE,
  42. };
  43. enum {
  44. DUAL_PROP_PR_SRC = 0,
  45. DUAL_PROP_PR_SNK,
  46. DUAL_PROP_PR_NONE,
  47. };
  48. enum {
  49. DUAL_PROP_DR_HOST = 0,
  50. DUAL_PROP_DR_DEVICE,
  51. DUAL_PROP_DR_NONE,
  52. };
  53. static struct usb_extcon_info *g_extcon_info;
  54. static void usb_extcon_detect_cable(struct work_struct *work)
  55. {
  56. struct mt_usb_work *info = container_of(to_delayed_work(work),
  57. struct mt_usb_work,
  58. dwork);
  59. unsigned int cur_dr, new_dr;
  60. if (!g_extcon_info) {
  61. pr_info("g_extcon_info = NULL\n");
  62. return;
  63. }
  64. cur_dr = g_extcon_info->dr;
  65. new_dr = info->dr;
  66. pr_info("cur_dr(%d) new_dr(%d)\n", cur_dr, new_dr);
  67. /* none -> device */
  68. if (cur_dr == DUAL_PROP_DR_NONE &&
  69. new_dr == DUAL_PROP_DR_DEVICE) {
  70. extcon_set_state_sync(g_extcon_info->edev,
  71. EXTCON_USB, true);
  72. /* none -> host */
  73. } else if (cur_dr == DUAL_PROP_DR_NONE &&
  74. new_dr == DUAL_PROP_DR_HOST) {
  75. extcon_set_state_sync(g_extcon_info->edev,
  76. EXTCON_USB_HOST, true);
  77. /* device -> none */
  78. } else if (cur_dr == DUAL_PROP_DR_DEVICE &&
  79. new_dr == DUAL_PROP_DR_NONE) {
  80. extcon_set_state_sync(g_extcon_info->edev,
  81. EXTCON_USB, false);
  82. /* host -> none */
  83. } else if (cur_dr == DUAL_PROP_DR_HOST &&
  84. new_dr == DUAL_PROP_DR_NONE) {
  85. extcon_set_state_sync(g_extcon_info->edev,
  86. EXTCON_USB_HOST, false);
  87. /* device -> host */
  88. } else if (cur_dr == DUAL_PROP_DR_DEVICE &&
  89. new_dr == DUAL_PROP_DR_HOST) {
  90. pr_info("device -> host, it's illegal\n");
  91. /* host -> device */
  92. } else if (cur_dr == DUAL_PROP_DR_HOST &&
  93. new_dr == DUAL_PROP_DR_DEVICE) {
  94. pr_info("host -> device, it's illegal\n");
  95. }
  96. g_extcon_info->dr = new_dr;
  97. kfree(info);
  98. }
  99. static void issue_connection_work(unsigned int dr)
  100. {
  101. struct mt_usb_work *work;
  102. if (!g_extcon_info) {
  103. pr_info("g_extcon_info = NULL\n");
  104. return;
  105. }
  106. /* create and prepare worker */
  107. work = kzalloc(sizeof(struct mt_usb_work), GFP_ATOMIC);
  108. if (!work)
  109. return;
  110. work->dr = dr;
  111. INIT_DELAYED_WORK(&work->dwork, usb_extcon_detect_cable);
  112. /* issue connection work */
  113. queue_delayed_work(g_extcon_info->extcon_workq, &work->dwork, 0);
  114. }
  115. #if !defined(CONFIG_USB_MU3D_DRV)
  116. void mt_usb_connect(void)
  117. {
  118. #ifndef CONFIG_TCPC_CLASS
  119. #ifdef CONFIG_DUAL_ROLE_USB_INTF
  120. mt_usb_dual_role_to_device();
  121. #endif
  122. #endif
  123. pr_info("%s\n", __func__);
  124. issue_connection_work(DUAL_PROP_DR_DEVICE);
  125. }
  126. EXPORT_SYMBOL_GPL(mt_usb_connect);
  127. void mt_usb_disconnect(void)
  128. {
  129. #ifndef CONFIG_TCPC_CLASS
  130. #ifdef CONFIG_DUAL_ROLE_USB_INTF
  131. mt_usb_dual_role_to_none();
  132. #endif
  133. #endif
  134. pr_info("%s\n", __func__);
  135. issue_connection_work(DUAL_PROP_DR_NONE);
  136. }
  137. EXPORT_SYMBOL_GPL(mt_usb_disconnect);
  138. #endif
  139. void mt_usbhost_connect(void)
  140. {
  141. #ifndef CONFIG_TCPC_CLASS
  142. #ifdef CONFIG_DUAL_ROLE_USB_INTF
  143. mt_usb_dual_role_to_host();
  144. #endif
  145. #endif
  146. pr_info("%s\n", __func__);
  147. issue_connection_work(DUAL_PROP_DR_HOST);
  148. }
  149. EXPORT_SYMBOL_GPL(mt_usbhost_connect);
  150. void mt_usbhost_disconnect(void)
  151. {
  152. #ifndef CONFIG_TCPC_CLASS
  153. #ifdef CONFIG_DUAL_ROLE_USB_INTF
  154. mt_usb_dual_role_to_none();
  155. #endif
  156. #endif
  157. pr_info("%s\n", __func__);
  158. issue_connection_work(DUAL_PROP_DR_NONE);
  159. }
  160. EXPORT_SYMBOL_GPL(mt_usbhost_disconnect);
  161. void mt_vbus_on(void)
  162. {
  163. usb_otg_set_vbus(true);
  164. }
  165. EXPORT_SYMBOL_GPL(mt_vbus_on);
  166. void mt_vbus_off(void)
  167. {
  168. usb_otg_set_vbus(false);
  169. }
  170. EXPORT_SYMBOL_GPL(mt_vbus_off);
  171. static int usb_extcon_probe(struct platform_device *pdev)
  172. {
  173. struct device *dev = &pdev->dev;
  174. struct usb_extcon_info *info;
  175. int ret;
  176. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  177. if (!info)
  178. return -ENOMEM;
  179. info->dev = dev;
  180. info->edev = devm_extcon_dev_allocate(dev, usb_extcon_cable);
  181. if (IS_ERR(info->edev)) {
  182. dev_info(dev, "failed to allocate extcon device\n");
  183. return -ENOMEM;
  184. }
  185. ret = devm_extcon_dev_register(dev, info->edev);
  186. if (ret < 0) {
  187. dev_info(dev, "failed to register extcon device\n");
  188. return ret;
  189. }
  190. platform_set_drvdata(pdev, info);
  191. info->dr = DUAL_PROP_DR_NONE;
  192. info->extcon_workq = create_singlethread_workqueue("usb_extcon_workq");
  193. g_extcon_info = info;
  194. #ifndef CONFIG_TCPC_CLASS
  195. #ifdef CONFIG_DUAL_ROLE_USB_INTF
  196. mt_usb_dual_role_init(g_extcon_info->dev);
  197. #endif
  198. #endif
  199. /* Perform initial detection */
  200. /* issue_connection_work(DUAL_PROP_DR_NONE); */
  201. return 0;
  202. }
  203. static int usb_extcon_remove(struct platform_device *pdev)
  204. {
  205. return 0;
  206. }
  207. static const struct of_device_id usb_extcon_dt_match[] = {
  208. { .compatible = "mediatek,extcon-usb", },
  209. { /* sentinel */ }
  210. };
  211. MODULE_DEVICE_TABLE(of, usb_extcon_dt_match);
  212. static struct platform_driver usb_extcon_driver = {
  213. .probe = usb_extcon_probe,
  214. .remove = usb_extcon_remove,
  215. .driver = {
  216. .name = "mediatek,extcon-usb",
  217. .of_match_table = usb_extcon_dt_match,
  218. },
  219. };
  220. module_platform_driver(usb_extcon_driver);