extcon-usb-gpio.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. #define USB_GPIO_DEBOUNCE_MS 20 /* ms */
  30. struct usb_extcon_info {
  31. struct device *dev;
  32. struct extcon_dev *edev;
  33. struct gpio_desc *id_gpiod;
  34. int id_irq;
  35. unsigned long debounce_jiffies;
  36. struct delayed_work wq_detcable;
  37. };
  38. static const unsigned int usb_extcon_cable[] = {
  39. EXTCON_USB,
  40. EXTCON_USB_HOST,
  41. EXTCON_NONE,
  42. };
  43. static void usb_extcon_detect_cable(struct work_struct *work)
  44. {
  45. int id;
  46. struct usb_extcon_info *info = container_of(to_delayed_work(work),
  47. struct usb_extcon_info,
  48. wq_detcable);
  49. /* check ID and update cable state */
  50. id = gpiod_get_value_cansleep(info->id_gpiod);
  51. if (id) {
  52. /*
  53. * ID = 1 means USB HOST cable detached.
  54. * As we don't have event for USB peripheral cable attached,
  55. * we simulate USB peripheral attach here.
  56. */
  57. extcon_set_cable_state_(info->edev, EXTCON_USB_HOST, false);
  58. extcon_set_cable_state_(info->edev, EXTCON_USB, true);
  59. } else {
  60. /*
  61. * ID = 0 means USB HOST cable attached.
  62. * As we don't have event for USB peripheral cable detached,
  63. * we simulate USB peripheral detach here.
  64. */
  65. extcon_set_cable_state_(info->edev, EXTCON_USB, false);
  66. extcon_set_cable_state_(info->edev, EXTCON_USB_HOST, true);
  67. }
  68. }
  69. static irqreturn_t usb_irq_handler(int irq, void *dev_id)
  70. {
  71. struct usb_extcon_info *info = dev_id;
  72. queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
  73. info->debounce_jiffies);
  74. return IRQ_HANDLED;
  75. }
  76. static int usb_extcon_probe(struct platform_device *pdev)
  77. {
  78. struct device *dev = &pdev->dev;
  79. struct device_node *np = dev->of_node;
  80. struct usb_extcon_info *info;
  81. int ret;
  82. if (!np)
  83. return -EINVAL;
  84. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  85. if (!info)
  86. return -ENOMEM;
  87. info->dev = dev;
  88. info->id_gpiod = devm_gpiod_get(&pdev->dev, "id", GPIOD_IN);
  89. if (IS_ERR(info->id_gpiod)) {
  90. dev_err(dev, "failed to get ID GPIO\n");
  91. return PTR_ERR(info->id_gpiod);
  92. }
  93. info->edev = devm_extcon_dev_allocate(dev, usb_extcon_cable);
  94. if (IS_ERR(info->edev)) {
  95. dev_err(dev, "failed to allocate extcon device\n");
  96. return -ENOMEM;
  97. }
  98. ret = devm_extcon_dev_register(dev, info->edev);
  99. if (ret < 0) {
  100. dev_err(dev, "failed to register extcon device\n");
  101. return ret;
  102. }
  103. ret = gpiod_set_debounce(info->id_gpiod,
  104. USB_GPIO_DEBOUNCE_MS * 1000);
  105. if (ret < 0)
  106. info->debounce_jiffies = msecs_to_jiffies(USB_GPIO_DEBOUNCE_MS);
  107. INIT_DELAYED_WORK(&info->wq_detcable, usb_extcon_detect_cable);
  108. info->id_irq = gpiod_to_irq(info->id_gpiod);
  109. if (info->id_irq < 0) {
  110. dev_err(dev, "failed to get ID IRQ\n");
  111. return info->id_irq;
  112. }
  113. ret = devm_request_threaded_irq(dev, info->id_irq, NULL,
  114. usb_irq_handler,
  115. IRQF_TRIGGER_RISING |
  116. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  117. pdev->name, info);
  118. if (ret < 0) {
  119. dev_err(dev, "failed to request handler for ID IRQ\n");
  120. return ret;
  121. }
  122. platform_set_drvdata(pdev, info);
  123. device_init_wakeup(dev, true);
  124. dev_pm_set_wake_irq(dev, info->id_irq);
  125. /* Perform initial detection */
  126. usb_extcon_detect_cable(&info->wq_detcable.work);
  127. return 0;
  128. }
  129. static int usb_extcon_remove(struct platform_device *pdev)
  130. {
  131. struct usb_extcon_info *info = platform_get_drvdata(pdev);
  132. cancel_delayed_work_sync(&info->wq_detcable);
  133. dev_pm_clear_wake_irq(&pdev->dev);
  134. device_init_wakeup(&pdev->dev, false);
  135. return 0;
  136. }
  137. #ifdef CONFIG_PM_SLEEP
  138. static int usb_extcon_suspend(struct device *dev)
  139. {
  140. struct usb_extcon_info *info = dev_get_drvdata(dev);
  141. int ret = 0;
  142. /*
  143. * We don't want to process any IRQs after this point
  144. * as GPIOs used behind I2C subsystem might not be
  145. * accessible until resume completes. So disable IRQ.
  146. */
  147. disable_irq(info->id_irq);
  148. return ret;
  149. }
  150. static int usb_extcon_resume(struct device *dev)
  151. {
  152. struct usb_extcon_info *info = dev_get_drvdata(dev);
  153. int ret = 0;
  154. enable_irq(info->id_irq);
  155. return ret;
  156. }
  157. #endif
  158. static SIMPLE_DEV_PM_OPS(usb_extcon_pm_ops,
  159. usb_extcon_suspend, usb_extcon_resume);
  160. static const struct of_device_id usb_extcon_dt_match[] = {
  161. { .compatible = "linux,extcon-usb-gpio", },
  162. { /* sentinel */ }
  163. };
  164. MODULE_DEVICE_TABLE(of, usb_extcon_dt_match);
  165. static struct platform_driver usb_extcon_driver = {
  166. .probe = usb_extcon_probe,
  167. .remove = usb_extcon_remove,
  168. .driver = {
  169. .name = "extcon-usb-gpio",
  170. .pm = &usb_extcon_pm_ops,
  171. .of_match_table = usb_extcon_dt_match,
  172. },
  173. };
  174. module_platform_driver(usb_extcon_driver);
  175. MODULE_AUTHOR("Roger Quadros <rogerq@ti.com>");
  176. MODULE_DESCRIPTION("USB GPIO extcon driver");
  177. MODULE_LICENSE("GPL v2");