extcon-qcom-spmi-misc.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**
  2. * extcon-qcom-spmi-misc.c - Qualcomm USB extcon driver to support USB ID
  3. * detection based on extcon-usb-gpio.c.
  4. *
  5. * Copyright (C) 2016 Linaro, Ltd.
  6. * Stephen Boyd <stephen.boyd@linaro.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/extcon-provider.h>
  18. #include <linux/init.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/mod_devicetable.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/slab.h>
  25. #include <linux/workqueue.h>
  26. #define USB_ID_DEBOUNCE_MS 5 /* ms */
  27. struct qcom_usb_extcon_info {
  28. struct extcon_dev *edev;
  29. int irq;
  30. struct delayed_work wq_detcable;
  31. unsigned long debounce_jiffies;
  32. };
  33. static const unsigned int qcom_usb_extcon_cable[] = {
  34. EXTCON_USB_HOST,
  35. EXTCON_NONE,
  36. };
  37. static void qcom_usb_extcon_detect_cable(struct work_struct *work)
  38. {
  39. bool id;
  40. int ret;
  41. struct qcom_usb_extcon_info *info = container_of(to_delayed_work(work),
  42. struct qcom_usb_extcon_info,
  43. wq_detcable);
  44. /* check ID and update cable state */
  45. ret = irq_get_irqchip_state(info->irq, IRQCHIP_STATE_LINE_LEVEL, &id);
  46. if (ret)
  47. return;
  48. extcon_set_state_sync(info->edev, EXTCON_USB_HOST, !id);
  49. }
  50. static irqreturn_t qcom_usb_irq_handler(int irq, void *dev_id)
  51. {
  52. struct qcom_usb_extcon_info *info = dev_id;
  53. queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
  54. info->debounce_jiffies);
  55. return IRQ_HANDLED;
  56. }
  57. static int qcom_usb_extcon_probe(struct platform_device *pdev)
  58. {
  59. struct device *dev = &pdev->dev;
  60. struct qcom_usb_extcon_info *info;
  61. int ret;
  62. info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
  63. if (!info)
  64. return -ENOMEM;
  65. info->edev = devm_extcon_dev_allocate(dev, qcom_usb_extcon_cable);
  66. if (IS_ERR(info->edev)) {
  67. dev_err(dev, "failed to allocate extcon device\n");
  68. return -ENOMEM;
  69. }
  70. ret = devm_extcon_dev_register(dev, info->edev);
  71. if (ret < 0) {
  72. dev_err(dev, "failed to register extcon device\n");
  73. return ret;
  74. }
  75. info->debounce_jiffies = msecs_to_jiffies(USB_ID_DEBOUNCE_MS);
  76. INIT_DELAYED_WORK(&info->wq_detcable, qcom_usb_extcon_detect_cable);
  77. info->irq = platform_get_irq_byname(pdev, "usb_id");
  78. if (info->irq < 0)
  79. return info->irq;
  80. ret = devm_request_threaded_irq(dev, info->irq, NULL,
  81. qcom_usb_irq_handler,
  82. IRQF_TRIGGER_RISING |
  83. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  84. pdev->name, info);
  85. if (ret < 0) {
  86. dev_err(dev, "failed to request handler for ID IRQ\n");
  87. return ret;
  88. }
  89. platform_set_drvdata(pdev, info);
  90. device_init_wakeup(dev, 1);
  91. /* Perform initial detection */
  92. qcom_usb_extcon_detect_cable(&info->wq_detcable.work);
  93. return 0;
  94. }
  95. static int qcom_usb_extcon_remove(struct platform_device *pdev)
  96. {
  97. struct qcom_usb_extcon_info *info = platform_get_drvdata(pdev);
  98. cancel_delayed_work_sync(&info->wq_detcable);
  99. return 0;
  100. }
  101. #ifdef CONFIG_PM_SLEEP
  102. static int qcom_usb_extcon_suspend(struct device *dev)
  103. {
  104. struct qcom_usb_extcon_info *info = dev_get_drvdata(dev);
  105. int ret = 0;
  106. if (device_may_wakeup(dev))
  107. ret = enable_irq_wake(info->irq);
  108. return ret;
  109. }
  110. static int qcom_usb_extcon_resume(struct device *dev)
  111. {
  112. struct qcom_usb_extcon_info *info = dev_get_drvdata(dev);
  113. int ret = 0;
  114. if (device_may_wakeup(dev))
  115. ret = disable_irq_wake(info->irq);
  116. return ret;
  117. }
  118. #endif
  119. static SIMPLE_DEV_PM_OPS(qcom_usb_extcon_pm_ops,
  120. qcom_usb_extcon_suspend, qcom_usb_extcon_resume);
  121. static const struct of_device_id qcom_usb_extcon_dt_match[] = {
  122. { .compatible = "qcom,pm8941-misc", },
  123. { }
  124. };
  125. MODULE_DEVICE_TABLE(of, qcom_usb_extcon_dt_match);
  126. static struct platform_driver qcom_usb_extcon_driver = {
  127. .probe = qcom_usb_extcon_probe,
  128. .remove = qcom_usb_extcon_remove,
  129. .driver = {
  130. .name = "extcon-pm8941-misc",
  131. .pm = &qcom_usb_extcon_pm_ops,
  132. .of_match_table = qcom_usb_extcon_dt_match,
  133. },
  134. };
  135. module_platform_driver(qcom_usb_extcon_driver);
  136. MODULE_DESCRIPTION("QCOM USB ID extcon driver");
  137. MODULE_AUTHOR("Stephen Boyd <stephen.boyd@linaro.org>");
  138. MODULE_LICENSE("GPL v2");