extcon-qcom-spmi-misc.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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.h>
  18. #include <linux/init.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/slab.h>
  24. #include <linux/workqueue.h>
  25. #define USB_ID_DEBOUNCE_MS 5 /* ms */
  26. struct qcom_usb_extcon_info {
  27. struct extcon_dev *edev;
  28. int irq;
  29. struct delayed_work wq_detcable;
  30. unsigned long debounce_jiffies;
  31. };
  32. static const unsigned int qcom_usb_extcon_cable[] = {
  33. EXTCON_USB_HOST,
  34. EXTCON_NONE,
  35. };
  36. static void qcom_usb_extcon_detect_cable(struct work_struct *work)
  37. {
  38. bool id;
  39. int ret;
  40. struct qcom_usb_extcon_info *info = container_of(to_delayed_work(work),
  41. struct qcom_usb_extcon_info,
  42. wq_detcable);
  43. /* check ID and update cable state */
  44. ret = irq_get_irqchip_state(info->irq, IRQCHIP_STATE_LINE_LEVEL, &id);
  45. if (ret)
  46. return;
  47. extcon_set_state_sync(info->edev, EXTCON_USB_HOST, !id);
  48. }
  49. static irqreturn_t qcom_usb_irq_handler(int irq, void *dev_id)
  50. {
  51. struct qcom_usb_extcon_info *info = dev_id;
  52. queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
  53. info->debounce_jiffies);
  54. return IRQ_HANDLED;
  55. }
  56. static int qcom_usb_extcon_probe(struct platform_device *pdev)
  57. {
  58. struct device *dev = &pdev->dev;
  59. struct qcom_usb_extcon_info *info;
  60. int ret;
  61. info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
  62. if (!info)
  63. return -ENOMEM;
  64. info->edev = devm_extcon_dev_allocate(dev, qcom_usb_extcon_cable);
  65. if (IS_ERR(info->edev)) {
  66. dev_err(dev, "failed to allocate extcon device\n");
  67. return -ENOMEM;
  68. }
  69. ret = devm_extcon_dev_register(dev, info->edev);
  70. if (ret < 0) {
  71. dev_err(dev, "failed to register extcon device\n");
  72. return ret;
  73. }
  74. info->debounce_jiffies = msecs_to_jiffies(USB_ID_DEBOUNCE_MS);
  75. INIT_DELAYED_WORK(&info->wq_detcable, qcom_usb_extcon_detect_cable);
  76. info->irq = platform_get_irq_byname(pdev, "usb_id");
  77. if (info->irq < 0)
  78. return info->irq;
  79. ret = devm_request_threaded_irq(dev, info->irq, NULL,
  80. qcom_usb_irq_handler,
  81. IRQF_TRIGGER_RISING |
  82. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  83. pdev->name, info);
  84. if (ret < 0) {
  85. dev_err(dev, "failed to request handler for ID IRQ\n");
  86. return ret;
  87. }
  88. platform_set_drvdata(pdev, info);
  89. device_init_wakeup(dev, 1);
  90. /* Perform initial detection */
  91. qcom_usb_extcon_detect_cable(&info->wq_detcable.work);
  92. return 0;
  93. }
  94. static int qcom_usb_extcon_remove(struct platform_device *pdev)
  95. {
  96. struct qcom_usb_extcon_info *info = platform_get_drvdata(pdev);
  97. cancel_delayed_work_sync(&info->wq_detcable);
  98. return 0;
  99. }
  100. #ifdef CONFIG_PM_SLEEP
  101. static int qcom_usb_extcon_suspend(struct device *dev)
  102. {
  103. struct qcom_usb_extcon_info *info = dev_get_drvdata(dev);
  104. int ret = 0;
  105. if (device_may_wakeup(dev))
  106. ret = enable_irq_wake(info->irq);
  107. return ret;
  108. }
  109. static int qcom_usb_extcon_resume(struct device *dev)
  110. {
  111. struct qcom_usb_extcon_info *info = dev_get_drvdata(dev);
  112. int ret = 0;
  113. if (device_may_wakeup(dev))
  114. ret = disable_irq_wake(info->irq);
  115. return ret;
  116. }
  117. #endif
  118. static SIMPLE_DEV_PM_OPS(qcom_usb_extcon_pm_ops,
  119. qcom_usb_extcon_suspend, qcom_usb_extcon_resume);
  120. static const struct of_device_id qcom_usb_extcon_dt_match[] = {
  121. { .compatible = "qcom,pm8941-misc", },
  122. { }
  123. };
  124. MODULE_DEVICE_TABLE(of, qcom_usb_extcon_dt_match);
  125. static struct platform_driver qcom_usb_extcon_driver = {
  126. .probe = qcom_usb_extcon_probe,
  127. .remove = qcom_usb_extcon_remove,
  128. .driver = {
  129. .name = "extcon-pm8941-misc",
  130. .pm = &qcom_usb_extcon_pm_ops,
  131. .of_match_table = qcom_usb_extcon_dt_match,
  132. },
  133. };
  134. module_platform_driver(qcom_usb_extcon_driver);
  135. MODULE_DESCRIPTION("QCOM USB ID extcon driver");
  136. MODULE_AUTHOR("Stephen Boyd <stephen.boyd@linaro.org>");
  137. MODULE_LICENSE("GPL v2");