extcon-usb-gpio.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /**
  2. * drivers/extcon/extcon-usb-gpio.c - USB GPIO extcon driver
  3. *
  4. * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com
  5. * Author: Roger Quadros <rogerq@ti.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/extcon.h>
  17. #include <linux/gpio.h>
  18. #include <linux/gpio/consumer.h>
  19. #include <linux/init.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/irq.h>
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/of_gpio.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/pm_wakeirq.h>
  27. #include <linux/slab.h>
  28. #include <linux/workqueue.h>
  29. #include <linux/acpi.h>
  30. #define USB_GPIO_DEBOUNCE_MS 20 /* ms */
  31. struct usb_extcon_info {
  32. struct device *dev;
  33. struct extcon_dev *edev;
  34. struct gpio_desc *id_gpiod;
  35. int id_irq;
  36. unsigned long debounce_jiffies;
  37. struct delayed_work wq_detcable;
  38. };
  39. static const unsigned int usb_extcon_cable[] = {
  40. EXTCON_USB,
  41. EXTCON_USB_HOST,
  42. EXTCON_NONE,
  43. };
  44. static void usb_extcon_detect_cable(struct work_struct *work)
  45. {
  46. int id;
  47. struct usb_extcon_info *info = container_of(to_delayed_work(work),
  48. struct usb_extcon_info,
  49. wq_detcable);
  50. /* check ID and update cable state */
  51. id = gpiod_get_value_cansleep(info->id_gpiod);
  52. if (id) {
  53. /*
  54. * ID = 1 means USB HOST cable detached.
  55. * As we don't have event for USB peripheral cable attached,
  56. * we simulate USB peripheral attach here.
  57. */
  58. extcon_set_state_sync(info->edev, EXTCON_USB_HOST, false);
  59. extcon_set_state_sync(info->edev, EXTCON_USB, true);
  60. } else {
  61. /*
  62. * ID = 0 means USB HOST cable attached.
  63. * As we don't have event for USB peripheral cable detached,
  64. * we simulate USB peripheral detach here.
  65. */
  66. extcon_set_state_sync(info->edev, EXTCON_USB, false);
  67. extcon_set_state_sync(info->edev, EXTCON_USB_HOST, true);
  68. }
  69. }
  70. static irqreturn_t usb_irq_handler(int irq, void *dev_id)
  71. {
  72. struct usb_extcon_info *info = dev_id;
  73. queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
  74. info->debounce_jiffies);
  75. return IRQ_HANDLED;
  76. }
  77. static int usb_extcon_probe(struct platform_device *pdev)
  78. {
  79. struct device *dev = &pdev->dev;
  80. struct device_node *np = dev->of_node;
  81. struct usb_extcon_info *info;
  82. int ret;
  83. if (!np && !ACPI_HANDLE(dev))
  84. return -EINVAL;
  85. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  86. if (!info)
  87. return -ENOMEM;
  88. info->dev = dev;
  89. info->id_gpiod = devm_gpiod_get(&pdev->dev, "id", GPIOD_IN);
  90. if (IS_ERR(info->id_gpiod)) {
  91. dev_err(dev, "failed to get ID GPIO\n");
  92. return PTR_ERR(info->id_gpiod);
  93. }
  94. info->edev = devm_extcon_dev_allocate(dev, usb_extcon_cable);
  95. if (IS_ERR(info->edev)) {
  96. dev_err(dev, "failed to allocate extcon device\n");
  97. return -ENOMEM;
  98. }
  99. ret = devm_extcon_dev_register(dev, info->edev);
  100. if (ret < 0) {
  101. dev_err(dev, "failed to register extcon device\n");
  102. return ret;
  103. }
  104. ret = gpiod_set_debounce(info->id_gpiod,
  105. USB_GPIO_DEBOUNCE_MS * 1000);
  106. if (ret < 0)
  107. info->debounce_jiffies = msecs_to_jiffies(USB_GPIO_DEBOUNCE_MS);
  108. INIT_DELAYED_WORK(&info->wq_detcable, usb_extcon_detect_cable);
  109. info->id_irq = gpiod_to_irq(info->id_gpiod);
  110. if (info->id_irq < 0) {
  111. dev_err(dev, "failed to get ID IRQ\n");
  112. return info->id_irq;
  113. }
  114. ret = devm_request_threaded_irq(dev, info->id_irq, NULL,
  115. usb_irq_handler,
  116. IRQF_TRIGGER_RISING |
  117. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  118. pdev->name, info);
  119. if (ret < 0) {
  120. dev_err(dev, "failed to request handler for ID IRQ\n");
  121. return ret;
  122. }
  123. platform_set_drvdata(pdev, info);
  124. device_init_wakeup(dev, true);
  125. dev_pm_set_wake_irq(dev, info->id_irq);
  126. /* Perform initial detection */
  127. usb_extcon_detect_cable(&info->wq_detcable.work);
  128. return 0;
  129. }
  130. static int usb_extcon_remove(struct platform_device *pdev)
  131. {
  132. struct usb_extcon_info *info = platform_get_drvdata(pdev);
  133. cancel_delayed_work_sync(&info->wq_detcable);
  134. dev_pm_clear_wake_irq(&pdev->dev);
  135. device_init_wakeup(&pdev->dev, false);
  136. return 0;
  137. }
  138. #ifdef CONFIG_PM_SLEEP
  139. static int usb_extcon_suspend(struct device *dev)
  140. {
  141. struct usb_extcon_info *info = dev_get_drvdata(dev);
  142. int ret = 0;
  143. /*
  144. * We don't want to process any IRQs after this point
  145. * as GPIOs used behind I2C subsystem might not be
  146. * accessible until resume completes. So disable IRQ.
  147. */
  148. disable_irq(info->id_irq);
  149. return ret;
  150. }
  151. static int usb_extcon_resume(struct device *dev)
  152. {
  153. struct usb_extcon_info *info = dev_get_drvdata(dev);
  154. int ret = 0;
  155. enable_irq(info->id_irq);
  156. if (!device_may_wakeup(dev))
  157. queue_delayed_work(system_power_efficient_wq,
  158. &info->wq_detcable, 0);
  159. return ret;
  160. }
  161. #endif
  162. static SIMPLE_DEV_PM_OPS(usb_extcon_pm_ops,
  163. usb_extcon_suspend, usb_extcon_resume);
  164. static const struct of_device_id usb_extcon_dt_match[] = {
  165. { .compatible = "linux,extcon-usb-gpio", },
  166. { /* sentinel */ }
  167. };
  168. MODULE_DEVICE_TABLE(of, usb_extcon_dt_match);
  169. static const struct platform_device_id usb_extcon_platform_ids[] = {
  170. { .name = "extcon-usb-gpio", },
  171. { /* sentinel */ }
  172. };
  173. MODULE_DEVICE_TABLE(platform, usb_extcon_platform_ids);
  174. static struct platform_driver usb_extcon_driver = {
  175. .probe = usb_extcon_probe,
  176. .remove = usb_extcon_remove,
  177. .driver = {
  178. .name = "extcon-usb-gpio",
  179. .pm = &usb_extcon_pm_ops,
  180. .of_match_table = usb_extcon_dt_match,
  181. },
  182. .id_table = usb_extcon_platform_ids,
  183. };
  184. module_platform_driver(usb_extcon_driver);
  185. MODULE_AUTHOR("Roger Quadros <rogerq@ti.com>");
  186. MODULE_DESCRIPTION("USB GPIO extcon driver");
  187. MODULE_LICENSE("GPL v2");