extcon-usb-charger.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. #include <linux/pinctrl/consumer.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. /*
  44. * "USB" = VBUS and "USB-HOST" = !ID, so we have:
  45. * Both "USB" and "USB-HOST" can't be set as active at the
  46. * same time so if "USB-HOST" is active (i.e. ID is 0) we keep "USB" inactive
  47. * even if VBUS is on.
  48. *
  49. * State | ID | VBUS
  50. * ----------------------------------------
  51. * [1] USB | H | H
  52. * [2] none | H | L
  53. * [3] USB-HOST | L | H
  54. * [4] USB-HOST | L | L
  55. *
  56. * In case we have only one of these signals:
  57. * - VBUS only - we want to distinguish between [1] and [2], so ID is always 1.
  58. * - ID only - we want to distinguish between [1] and [4], so VBUS = ID.
  59. */
  60. static void usb_extcon_detect_cable(struct work_struct *work)
  61. {
  62. int id;
  63. struct usb_extcon_info *info = container_of(to_delayed_work(work),
  64. struct usb_extcon_info,
  65. wq_detcable);
  66. /* check ID and VBUS and update cable state */
  67. id = info->id_gpiod ?
  68. gpiod_get_value_cansleep(info->id_gpiod) : 1;
  69. /* at first we clean states which are no longer active */
  70. if (id)
  71. extcon_set_state_sync(info->edev, EXTCON_USB_HOST, false);
  72. else
  73. extcon_set_state_sync(info->edev, EXTCON_USB_HOST, true);
  74. }
  75. static irqreturn_t usb_irq_handler(int irq, void *dev_id)
  76. {
  77. struct usb_extcon_info *info = dev_id;
  78. queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
  79. info->debounce_jiffies);
  80. return IRQ_HANDLED;
  81. }
  82. static int usb_extcon_probe(struct platform_device *pdev)
  83. {
  84. struct device *dev = &pdev->dev;
  85. struct device_node *np = dev->of_node;
  86. struct usb_extcon_info *info;
  87. int ret;
  88. if (!np)
  89. return -EINVAL;
  90. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  91. if (!info)
  92. return -ENOMEM;
  93. info->dev = dev;
  94. info->id_gpiod = devm_gpiod_get(&pdev->dev, "id", GPIOD_IN);
  95. if (!info->id_gpiod) {
  96. dev_err(dev, "failed to get gpios\n");
  97. return -ENODEV;
  98. }
  99. if (IS_ERR(info->id_gpiod))
  100. return PTR_ERR(info->id_gpiod);
  101. info->edev = devm_extcon_dev_allocate(dev, usb_extcon_cable);
  102. if (IS_ERR(info->edev)) {
  103. dev_err(dev, "failed to allocate extcon device\n");
  104. return -ENOMEM;
  105. }
  106. ret = devm_extcon_dev_register(dev, info->edev);
  107. if (ret < 0) {
  108. dev_err(dev, "failed to register extcon device\n");
  109. return ret;
  110. }
  111. ret = gpiod_set_debounce(info->id_gpiod,
  112. USB_GPIO_DEBOUNCE_MS * 1000);
  113. if (ret < 0)
  114. info->debounce_jiffies = msecs_to_jiffies(USB_GPIO_DEBOUNCE_MS);
  115. INIT_DELAYED_WORK(&info->wq_detcable, usb_extcon_detect_cable);
  116. info->id_irq = gpiod_to_irq(info->id_gpiod);
  117. if (info->id_irq < 0) {
  118. dev_err(dev, "failed to get ID IRQ\n");
  119. return info->id_irq;
  120. }
  121. ret = devm_request_threaded_irq(dev, info->id_irq, NULL,
  122. usb_irq_handler,
  123. IRQF_TRIGGER_RISING |
  124. IRQF_TRIGGER_FALLING |
  125. IRQF_ONESHOT,
  126. pdev->name, info);
  127. if (ret < 0) {
  128. dev_err(dev, "failed to request handler for ID IRQ\n");
  129. return ret;
  130. }
  131. platform_set_drvdata(pdev, info);
  132. device_set_wakeup_capable(&pdev->dev, true);
  133. /* Perform initial detection */
  134. usb_extcon_detect_cable(&info->wq_detcable.work);
  135. return 0;
  136. }
  137. static int usb_extcon_remove(struct platform_device *pdev)
  138. {
  139. struct usb_extcon_info *info = platform_get_drvdata(pdev);
  140. cancel_delayed_work_sync(&info->wq_detcable);
  141. device_init_wakeup(&pdev->dev, false);
  142. return 0;
  143. }
  144. #ifdef CONFIG_PM_SLEEP
  145. static int usb_extcon_suspend(struct device *dev)
  146. {
  147. struct usb_extcon_info *info = dev_get_drvdata(dev);
  148. int ret = 0;
  149. if (device_may_wakeup(dev)) {
  150. if (info->id_gpiod) {
  151. ret = enable_irq_wake(info->id_irq);
  152. if (ret)
  153. return ret;
  154. }
  155. }
  156. /*
  157. * We don't want to process any IRQs after this point
  158. * as GPIOs used behind I2C subsystem might not be
  159. * accessible until resume completes. So disable IRQ.
  160. */
  161. if (info->id_gpiod)
  162. disable_irq(info->id_irq);
  163. if (!device_may_wakeup(dev))
  164. pinctrl_pm_select_sleep_state(dev);
  165. return ret;
  166. }
  167. static int usb_extcon_resume(struct device *dev)
  168. {
  169. struct usb_extcon_info *info = dev_get_drvdata(dev);
  170. int ret = 0;
  171. if (!device_may_wakeup(dev))
  172. pinctrl_pm_select_default_state(dev);
  173. if (device_may_wakeup(dev)) {
  174. if (info->id_gpiod) {
  175. ret = disable_irq_wake(info->id_irq);
  176. if (ret)
  177. return ret;
  178. }
  179. }
  180. if (info->id_gpiod)
  181. enable_irq(info->id_irq);
  182. queue_delayed_work(system_power_efficient_wq,
  183. &info->wq_detcable, 0);
  184. return ret;
  185. }
  186. #endif
  187. static SIMPLE_DEV_PM_OPS(usb_extcon_pm_ops,
  188. usb_extcon_suspend, usb_extcon_resume);
  189. static const struct of_device_id usb_extcon_dt_match[] = {
  190. { .compatible = "linux,extcon-usb-chg", },
  191. { /* sentinel */ }
  192. };
  193. MODULE_DEVICE_TABLE(of, usb_extcon_dt_match);
  194. static const struct platform_device_id usb_extcon_platform_ids[] = {
  195. { .name = "extcon-usb-chg", },
  196. { /* sentinel */ }
  197. };
  198. MODULE_DEVICE_TABLE(platform, usb_extcon_platform_ids);
  199. static struct platform_driver usb_extcon_driver = {
  200. .probe = usb_extcon_probe,
  201. .remove = usb_extcon_remove,
  202. .driver = {
  203. .name = "extcon-usb-chg",
  204. .pm = &usb_extcon_pm_ops,
  205. .of_match_table = usb_extcon_dt_match,
  206. },
  207. .id_table = usb_extcon_platform_ids,
  208. };
  209. module_platform_driver(usb_extcon_driver);
  210. MODULE_AUTHOR("Roger Quadros <rogerq@ti.com>");
  211. MODULE_DESCRIPTION("USB GPIO extcon driver");
  212. MODULE_LICENSE("GPL v2");