extcon-usb-gpio.c 7.9 KB

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