extcon-usb-gpio.c 5.4 KB

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