extcon-usb-gpio.c 5.7 KB

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