extcon-usb-gpio.c 5.5 KB

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