extcon-usb-gpio.c 8.0 KB

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