extcon-qcom-spmi-misc.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /**
  3. * extcon-qcom-spmi-misc.c - Qualcomm USB extcon driver to support USB ID
  4. * and VBUS detection based on extcon-usb-gpio.c.
  5. *
  6. * Copyright (C) 2016 Linaro, Ltd.
  7. * Stephen Boyd <[email protected]>
  8. */
  9. #include <linux/devm-helpers.h>
  10. #include <linux/extcon-provider.h>
  11. #include <linux/init.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/mod_devicetable.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/slab.h>
  18. #include <linux/workqueue.h>
  19. #define USB_ID_DEBOUNCE_MS 5 /* ms */
  20. struct qcom_usb_extcon_info {
  21. struct extcon_dev *edev;
  22. int id_irq;
  23. int vbus_irq;
  24. struct delayed_work wq_detcable;
  25. unsigned long debounce_jiffies;
  26. };
  27. static const unsigned int qcom_usb_extcon_cable[] = {
  28. EXTCON_USB,
  29. EXTCON_USB_HOST,
  30. EXTCON_NONE,
  31. };
  32. static void qcom_usb_extcon_detect_cable(struct work_struct *work)
  33. {
  34. bool state = false;
  35. int ret;
  36. union extcon_property_value val;
  37. struct qcom_usb_extcon_info *info = container_of(to_delayed_work(work),
  38. struct qcom_usb_extcon_info,
  39. wq_detcable);
  40. if (info->id_irq > 0) {
  41. /* check ID and update cable state */
  42. ret = irq_get_irqchip_state(info->id_irq,
  43. IRQCHIP_STATE_LINE_LEVEL, &state);
  44. if (ret)
  45. return;
  46. if (!state) {
  47. val.intval = true;
  48. extcon_set_property(info->edev, EXTCON_USB_HOST,
  49. EXTCON_PROP_USB_SS, val);
  50. }
  51. extcon_set_state_sync(info->edev, EXTCON_USB_HOST, !state);
  52. }
  53. if (info->vbus_irq > 0) {
  54. /* check VBUS and update cable state */
  55. ret = irq_get_irqchip_state(info->vbus_irq,
  56. IRQCHIP_STATE_LINE_LEVEL, &state);
  57. if (ret)
  58. return;
  59. if (state) {
  60. val.intval = true;
  61. extcon_set_property(info->edev, EXTCON_USB,
  62. EXTCON_PROP_USB_SS, val);
  63. }
  64. extcon_set_state_sync(info->edev, EXTCON_USB, state);
  65. }
  66. }
  67. static irqreturn_t qcom_usb_irq_handler(int irq, void *dev_id)
  68. {
  69. struct qcom_usb_extcon_info *info = dev_id;
  70. queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
  71. info->debounce_jiffies);
  72. return IRQ_HANDLED;
  73. }
  74. static int qcom_usb_extcon_probe(struct platform_device *pdev)
  75. {
  76. struct device *dev = &pdev->dev;
  77. struct qcom_usb_extcon_info *info;
  78. int ret;
  79. info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
  80. if (!info)
  81. return -ENOMEM;
  82. info->edev = devm_extcon_dev_allocate(dev, qcom_usb_extcon_cable);
  83. if (IS_ERR(info->edev)) {
  84. dev_err(dev, "failed to allocate extcon device\n");
  85. return -ENOMEM;
  86. }
  87. ret = devm_extcon_dev_register(dev, info->edev);
  88. if (ret < 0) {
  89. dev_err(dev, "failed to register extcon device\n");
  90. return ret;
  91. }
  92. ret = extcon_set_property_capability(info->edev,
  93. EXTCON_USB, EXTCON_PROP_USB_SS);
  94. ret |= extcon_set_property_capability(info->edev,
  95. EXTCON_USB_HOST, EXTCON_PROP_USB_SS);
  96. if (ret) {
  97. dev_err(dev, "failed to register extcon props rc=%d\n",
  98. ret);
  99. return ret;
  100. }
  101. info->debounce_jiffies = msecs_to_jiffies(USB_ID_DEBOUNCE_MS);
  102. ret = devm_delayed_work_autocancel(dev, &info->wq_detcable,
  103. qcom_usb_extcon_detect_cable);
  104. if (ret)
  105. return ret;
  106. info->id_irq = platform_get_irq_byname(pdev, "usb_id");
  107. if (info->id_irq > 0) {
  108. ret = devm_request_threaded_irq(dev, info->id_irq, NULL,
  109. qcom_usb_irq_handler,
  110. IRQF_TRIGGER_RISING |
  111. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  112. pdev->name, info);
  113. if (ret < 0) {
  114. dev_err(dev, "failed to request handler for ID IRQ\n");
  115. return ret;
  116. }
  117. }
  118. info->vbus_irq = platform_get_irq_byname(pdev, "usb_vbus");
  119. if (info->vbus_irq > 0) {
  120. ret = devm_request_threaded_irq(dev, info->vbus_irq, NULL,
  121. qcom_usb_irq_handler,
  122. IRQF_TRIGGER_RISING |
  123. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  124. pdev->name, info);
  125. if (ret < 0) {
  126. dev_err(dev, "failed to request handler for VBUS IRQ\n");
  127. return ret;
  128. }
  129. }
  130. if (info->id_irq < 0 && info->vbus_irq < 0) {
  131. dev_err(dev, "ID and VBUS IRQ not found\n");
  132. return -EINVAL;
  133. }
  134. platform_set_drvdata(pdev, info);
  135. device_init_wakeup(dev, 1);
  136. /* Perform initial detection */
  137. qcom_usb_extcon_detect_cable(&info->wq_detcable.work);
  138. return 0;
  139. }
  140. #ifdef CONFIG_PM_SLEEP
  141. static int qcom_usb_extcon_suspend(struct device *dev)
  142. {
  143. struct qcom_usb_extcon_info *info = dev_get_drvdata(dev);
  144. int ret = 0;
  145. if (device_may_wakeup(dev)) {
  146. if (info->id_irq > 0)
  147. ret = enable_irq_wake(info->id_irq);
  148. if (info->vbus_irq > 0)
  149. ret = enable_irq_wake(info->vbus_irq);
  150. }
  151. return ret;
  152. }
  153. static int qcom_usb_extcon_resume(struct device *dev)
  154. {
  155. struct qcom_usb_extcon_info *info = dev_get_drvdata(dev);
  156. int ret = 0;
  157. if (device_may_wakeup(dev)) {
  158. if (info->id_irq > 0)
  159. ret = disable_irq_wake(info->id_irq);
  160. if (info->vbus_irq > 0)
  161. ret = disable_irq_wake(info->vbus_irq);
  162. }
  163. return ret;
  164. }
  165. #endif
  166. static SIMPLE_DEV_PM_OPS(qcom_usb_extcon_pm_ops,
  167. qcom_usb_extcon_suspend, qcom_usb_extcon_resume);
  168. static const struct of_device_id qcom_usb_extcon_dt_match[] = {
  169. { .compatible = "qcom,pm8941-misc", },
  170. { }
  171. };
  172. MODULE_DEVICE_TABLE(of, qcom_usb_extcon_dt_match);
  173. static struct platform_driver qcom_usb_extcon_driver = {
  174. .probe = qcom_usb_extcon_probe,
  175. .driver = {
  176. .name = "extcon-pm8941-misc",
  177. .pm = &qcom_usb_extcon_pm_ops,
  178. .of_match_table = qcom_usb_extcon_dt_match,
  179. },
  180. };
  181. module_platform_driver(qcom_usb_extcon_driver);
  182. MODULE_DESCRIPTION("QCOM USB ID extcon driver");
  183. MODULE_AUTHOR("Stephen Boyd <[email protected]>");
  184. MODULE_LICENSE("GPL v2");