extcon-palmas.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. * Palmas USB transceiver driver
  3. *
  4. * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * Author: Graeme Gregory <gg@slimlogic.co.uk>
  11. * Author: Kishon Vijay Abraham I <kishon@ti.com>
  12. *
  13. * Based on twl6030_usb.c
  14. *
  15. * Author: Hema HK <hemahk@ti.com>
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/slab.h>
  26. #include <linux/err.h>
  27. #include <linux/mfd/palmas.h>
  28. #include <linux/of.h>
  29. #include <linux/of_platform.h>
  30. #include <linux/of_gpio.h>
  31. #include <linux/gpio/consumer.h>
  32. #include <linux/workqueue.h>
  33. #define USB_GPIO_DEBOUNCE_MS 20 /* ms */
  34. static const unsigned int palmas_extcon_cable[] = {
  35. EXTCON_USB,
  36. EXTCON_USB_HOST,
  37. EXTCON_NONE,
  38. };
  39. static void palmas_usb_wakeup(struct palmas *palmas, int enable)
  40. {
  41. if (enable)
  42. palmas_write(palmas, PALMAS_USB_OTG_BASE, PALMAS_USB_WAKEUP,
  43. PALMAS_USB_WAKEUP_ID_WK_UP_COMP);
  44. else
  45. palmas_write(palmas, PALMAS_USB_OTG_BASE, PALMAS_USB_WAKEUP, 0);
  46. }
  47. static irqreturn_t palmas_vbus_irq_handler(int irq, void *_palmas_usb)
  48. {
  49. struct palmas_usb *palmas_usb = _palmas_usb;
  50. struct extcon_dev *edev = palmas_usb->edev;
  51. unsigned int vbus_line_state;
  52. palmas_read(palmas_usb->palmas, PALMAS_INTERRUPT_BASE,
  53. PALMAS_INT3_LINE_STATE, &vbus_line_state);
  54. if (vbus_line_state & PALMAS_INT3_LINE_STATE_VBUS) {
  55. if (palmas_usb->linkstat != PALMAS_USB_STATE_VBUS) {
  56. palmas_usb->linkstat = PALMAS_USB_STATE_VBUS;
  57. extcon_set_state_sync(edev, EXTCON_USB, true);
  58. dev_info(palmas_usb->dev, "USB cable is attached\n");
  59. } else {
  60. dev_dbg(palmas_usb->dev,
  61. "Spurious connect event detected\n");
  62. }
  63. } else if (!(vbus_line_state & PALMAS_INT3_LINE_STATE_VBUS)) {
  64. if (palmas_usb->linkstat == PALMAS_USB_STATE_VBUS) {
  65. palmas_usb->linkstat = PALMAS_USB_STATE_DISCONNECT;
  66. extcon_set_state_sync(edev, EXTCON_USB, false);
  67. dev_info(palmas_usb->dev, "USB cable is detached\n");
  68. } else {
  69. dev_dbg(palmas_usb->dev,
  70. "Spurious disconnect event detected\n");
  71. }
  72. }
  73. return IRQ_HANDLED;
  74. }
  75. static irqreturn_t palmas_id_irq_handler(int irq, void *_palmas_usb)
  76. {
  77. unsigned int set, id_src;
  78. struct palmas_usb *palmas_usb = _palmas_usb;
  79. struct extcon_dev *edev = palmas_usb->edev;
  80. palmas_read(palmas_usb->palmas, PALMAS_USB_OTG_BASE,
  81. PALMAS_USB_ID_INT_LATCH_SET, &set);
  82. palmas_read(palmas_usb->palmas, PALMAS_USB_OTG_BASE,
  83. PALMAS_USB_ID_INT_SRC, &id_src);
  84. if ((set & PALMAS_USB_ID_INT_SRC_ID_GND) &&
  85. (id_src & PALMAS_USB_ID_INT_SRC_ID_GND)) {
  86. palmas_write(palmas_usb->palmas, PALMAS_USB_OTG_BASE,
  87. PALMAS_USB_ID_INT_LATCH_CLR,
  88. PALMAS_USB_ID_INT_EN_HI_CLR_ID_GND);
  89. palmas_usb->linkstat = PALMAS_USB_STATE_ID;
  90. extcon_set_state_sync(edev, EXTCON_USB_HOST, true);
  91. dev_info(palmas_usb->dev, "USB-HOST cable is attached\n");
  92. } else if ((set & PALMAS_USB_ID_INT_SRC_ID_FLOAT) &&
  93. (id_src & PALMAS_USB_ID_INT_SRC_ID_FLOAT)) {
  94. palmas_write(palmas_usb->palmas, PALMAS_USB_OTG_BASE,
  95. PALMAS_USB_ID_INT_LATCH_CLR,
  96. PALMAS_USB_ID_INT_EN_HI_CLR_ID_FLOAT);
  97. palmas_usb->linkstat = PALMAS_USB_STATE_DISCONNECT;
  98. extcon_set_state_sync(edev, EXTCON_USB_HOST, false);
  99. dev_info(palmas_usb->dev, "USB-HOST cable is detached\n");
  100. } else if ((palmas_usb->linkstat == PALMAS_USB_STATE_ID) &&
  101. (!(set & PALMAS_USB_ID_INT_SRC_ID_GND))) {
  102. palmas_usb->linkstat = PALMAS_USB_STATE_DISCONNECT;
  103. extcon_set_state_sync(edev, EXTCON_USB_HOST, false);
  104. dev_info(palmas_usb->dev, "USB-HOST cable is detached\n");
  105. } else if ((palmas_usb->linkstat == PALMAS_USB_STATE_DISCONNECT) &&
  106. (id_src & PALMAS_USB_ID_INT_SRC_ID_GND)) {
  107. palmas_usb->linkstat = PALMAS_USB_STATE_ID;
  108. extcon_set_state_sync(edev, EXTCON_USB_HOST, true);
  109. dev_info(palmas_usb->dev, " USB-HOST cable is attached\n");
  110. }
  111. return IRQ_HANDLED;
  112. }
  113. static void palmas_gpio_id_detect(struct work_struct *work)
  114. {
  115. int id;
  116. struct palmas_usb *palmas_usb = container_of(to_delayed_work(work),
  117. struct palmas_usb,
  118. wq_detectid);
  119. struct extcon_dev *edev = palmas_usb->edev;
  120. if (!palmas_usb->id_gpiod)
  121. return;
  122. id = gpiod_get_value_cansleep(palmas_usb->id_gpiod);
  123. if (id) {
  124. extcon_set_state_sync(edev, EXTCON_USB_HOST, false);
  125. dev_info(palmas_usb->dev, "USB-HOST cable is detached\n");
  126. } else {
  127. extcon_set_state_sync(edev, EXTCON_USB_HOST, true);
  128. dev_info(palmas_usb->dev, "USB-HOST cable is attached\n");
  129. }
  130. }
  131. static irqreturn_t palmas_gpio_id_irq_handler(int irq, void *_palmas_usb)
  132. {
  133. struct palmas_usb *palmas_usb = _palmas_usb;
  134. queue_delayed_work(system_power_efficient_wq, &palmas_usb->wq_detectid,
  135. palmas_usb->sw_debounce_jiffies);
  136. return IRQ_HANDLED;
  137. }
  138. static void palmas_enable_irq(struct palmas_usb *palmas_usb)
  139. {
  140. palmas_write(palmas_usb->palmas, PALMAS_USB_OTG_BASE,
  141. PALMAS_USB_VBUS_CTRL_SET,
  142. PALMAS_USB_VBUS_CTRL_SET_VBUS_ACT_COMP);
  143. if (palmas_usb->enable_id_detection) {
  144. palmas_write(palmas_usb->palmas, PALMAS_USB_OTG_BASE,
  145. PALMAS_USB_ID_CTRL_SET,
  146. PALMAS_USB_ID_CTRL_SET_ID_ACT_COMP);
  147. palmas_write(palmas_usb->palmas, PALMAS_USB_OTG_BASE,
  148. PALMAS_USB_ID_INT_EN_HI_SET,
  149. PALMAS_USB_ID_INT_EN_HI_SET_ID_GND |
  150. PALMAS_USB_ID_INT_EN_HI_SET_ID_FLOAT);
  151. }
  152. if (palmas_usb->enable_vbus_detection)
  153. palmas_vbus_irq_handler(palmas_usb->vbus_irq, palmas_usb);
  154. /* cold plug for host mode needs this delay */
  155. if (palmas_usb->enable_id_detection) {
  156. msleep(30);
  157. palmas_id_irq_handler(palmas_usb->id_irq, palmas_usb);
  158. }
  159. }
  160. static int palmas_usb_probe(struct platform_device *pdev)
  161. {
  162. struct palmas *palmas = dev_get_drvdata(pdev->dev.parent);
  163. struct palmas_usb_platform_data *pdata = dev_get_platdata(&pdev->dev);
  164. struct device_node *node = pdev->dev.of_node;
  165. struct palmas_usb *palmas_usb;
  166. int status;
  167. if (!palmas) {
  168. dev_err(&pdev->dev, "failed to get valid parent\n");
  169. return -EINVAL;
  170. }
  171. palmas_usb = devm_kzalloc(&pdev->dev, sizeof(*palmas_usb), GFP_KERNEL);
  172. if (!palmas_usb)
  173. return -ENOMEM;
  174. if (node && !pdata) {
  175. palmas_usb->wakeup = of_property_read_bool(node, "ti,wakeup");
  176. palmas_usb->enable_id_detection = of_property_read_bool(node,
  177. "ti,enable-id-detection");
  178. palmas_usb->enable_vbus_detection = of_property_read_bool(node,
  179. "ti,enable-vbus-detection");
  180. } else {
  181. palmas_usb->wakeup = true;
  182. palmas_usb->enable_id_detection = true;
  183. palmas_usb->enable_vbus_detection = true;
  184. if (pdata)
  185. palmas_usb->wakeup = pdata->wakeup;
  186. }
  187. palmas_usb->id_gpiod = devm_gpiod_get_optional(&pdev->dev, "id",
  188. GPIOD_IN);
  189. if (IS_ERR(palmas_usb->id_gpiod)) {
  190. dev_err(&pdev->dev, "failed to get id gpio\n");
  191. return PTR_ERR(palmas_usb->id_gpiod);
  192. }
  193. palmas_usb->vbus_gpiod = devm_gpiod_get_optional(&pdev->dev, "vbus",
  194. GPIOD_IN);
  195. if (IS_ERR(palmas_usb->vbus_gpiod)) {
  196. dev_err(&pdev->dev, "failed to get vbus gpio\n");
  197. return PTR_ERR(palmas_usb->vbus_gpiod);
  198. }
  199. if (palmas_usb->enable_id_detection && palmas_usb->id_gpiod) {
  200. palmas_usb->enable_id_detection = false;
  201. palmas_usb->enable_gpio_id_detection = true;
  202. }
  203. if (palmas_usb->enable_vbus_detection && palmas_usb->vbus_gpiod) {
  204. palmas_usb->enable_vbus_detection = false;
  205. palmas_usb->enable_gpio_vbus_detection = true;
  206. }
  207. if (palmas_usb->enable_gpio_id_detection) {
  208. u32 debounce;
  209. if (of_property_read_u32(node, "debounce-delay-ms", &debounce))
  210. debounce = USB_GPIO_DEBOUNCE_MS;
  211. status = gpiod_set_debounce(palmas_usb->id_gpiod,
  212. debounce * 1000);
  213. if (status < 0)
  214. palmas_usb->sw_debounce_jiffies = msecs_to_jiffies(debounce);
  215. }
  216. INIT_DELAYED_WORK(&palmas_usb->wq_detectid, palmas_gpio_id_detect);
  217. palmas->usb = palmas_usb;
  218. palmas_usb->palmas = palmas;
  219. palmas_usb->dev = &pdev->dev;
  220. palmas_usb_wakeup(palmas, palmas_usb->wakeup);
  221. platform_set_drvdata(pdev, palmas_usb);
  222. palmas_usb->edev = devm_extcon_dev_allocate(&pdev->dev,
  223. palmas_extcon_cable);
  224. if (IS_ERR(palmas_usb->edev)) {
  225. dev_err(&pdev->dev, "failed to allocate extcon device\n");
  226. return -ENOMEM;
  227. }
  228. status = devm_extcon_dev_register(&pdev->dev, palmas_usb->edev);
  229. if (status) {
  230. dev_err(&pdev->dev, "failed to register extcon device\n");
  231. return status;
  232. }
  233. if (palmas_usb->enable_id_detection) {
  234. palmas_usb->id_otg_irq = regmap_irq_get_virq(palmas->irq_data,
  235. PALMAS_ID_OTG_IRQ);
  236. palmas_usb->id_irq = regmap_irq_get_virq(palmas->irq_data,
  237. PALMAS_ID_IRQ);
  238. status = devm_request_threaded_irq(palmas_usb->dev,
  239. palmas_usb->id_irq,
  240. NULL, palmas_id_irq_handler,
  241. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING |
  242. IRQF_ONESHOT,
  243. "palmas_usb_id", palmas_usb);
  244. if (status < 0) {
  245. dev_err(&pdev->dev, "can't get IRQ %d, err %d\n",
  246. palmas_usb->id_irq, status);
  247. return status;
  248. }
  249. } else if (palmas_usb->enable_gpio_id_detection) {
  250. palmas_usb->gpio_id_irq = gpiod_to_irq(palmas_usb->id_gpiod);
  251. if (palmas_usb->gpio_id_irq < 0) {
  252. dev_err(&pdev->dev, "failed to get id irq\n");
  253. return palmas_usb->gpio_id_irq;
  254. }
  255. status = devm_request_threaded_irq(&pdev->dev,
  256. palmas_usb->gpio_id_irq,
  257. NULL,
  258. palmas_gpio_id_irq_handler,
  259. IRQF_TRIGGER_RISING |
  260. IRQF_TRIGGER_FALLING |
  261. IRQF_ONESHOT,
  262. "palmas_usb_id",
  263. palmas_usb);
  264. if (status < 0) {
  265. dev_err(&pdev->dev,
  266. "failed to request handler for id irq\n");
  267. return status;
  268. }
  269. }
  270. if (palmas_usb->enable_vbus_detection) {
  271. palmas_usb->vbus_otg_irq = regmap_irq_get_virq(palmas->irq_data,
  272. PALMAS_VBUS_OTG_IRQ);
  273. palmas_usb->vbus_irq = regmap_irq_get_virq(palmas->irq_data,
  274. PALMAS_VBUS_IRQ);
  275. status = devm_request_threaded_irq(palmas_usb->dev,
  276. palmas_usb->vbus_irq, NULL,
  277. palmas_vbus_irq_handler,
  278. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING |
  279. IRQF_ONESHOT,
  280. "palmas_usb_vbus", palmas_usb);
  281. if (status < 0) {
  282. dev_err(&pdev->dev, "can't get IRQ %d, err %d\n",
  283. palmas_usb->vbus_irq, status);
  284. return status;
  285. }
  286. } else if (palmas_usb->enable_gpio_vbus_detection) {
  287. /* remux GPIO_1 as VBUSDET */
  288. status = palmas_update_bits(palmas,
  289. PALMAS_PU_PD_OD_BASE,
  290. PALMAS_PRIMARY_SECONDARY_PAD1,
  291. PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_MASK,
  292. (1 << PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_SHIFT));
  293. if (status < 0) {
  294. dev_err(&pdev->dev, "can't remux GPIO1\n");
  295. return status;
  296. }
  297. palmas_usb->vbus_otg_irq = regmap_irq_get_virq(palmas->irq_data,
  298. PALMAS_VBUS_OTG_IRQ);
  299. palmas_usb->gpio_vbus_irq = gpiod_to_irq(palmas_usb->vbus_gpiod);
  300. if (palmas_usb->gpio_vbus_irq < 0) {
  301. dev_err(&pdev->dev, "failed to get vbus irq\n");
  302. return palmas_usb->gpio_vbus_irq;
  303. }
  304. status = devm_request_threaded_irq(&pdev->dev,
  305. palmas_usb->gpio_vbus_irq,
  306. NULL,
  307. palmas_vbus_irq_handler,
  308. IRQF_TRIGGER_FALLING |
  309. IRQF_TRIGGER_RISING |
  310. IRQF_ONESHOT,
  311. "palmas_usb_vbus",
  312. palmas_usb);
  313. if (status < 0) {
  314. dev_err(&pdev->dev,
  315. "failed to request handler for vbus irq\n");
  316. return status;
  317. }
  318. }
  319. palmas_enable_irq(palmas_usb);
  320. /* perform initial detection */
  321. if (palmas_usb->enable_gpio_vbus_detection)
  322. palmas_vbus_irq_handler(palmas_usb->gpio_vbus_irq, palmas_usb);
  323. palmas_gpio_id_detect(&palmas_usb->wq_detectid.work);
  324. device_set_wakeup_capable(&pdev->dev, true);
  325. return 0;
  326. }
  327. static int palmas_usb_remove(struct platform_device *pdev)
  328. {
  329. struct palmas_usb *palmas_usb = platform_get_drvdata(pdev);
  330. cancel_delayed_work_sync(&palmas_usb->wq_detectid);
  331. return 0;
  332. }
  333. #ifdef CONFIG_PM_SLEEP
  334. static int palmas_usb_suspend(struct device *dev)
  335. {
  336. struct palmas_usb *palmas_usb = dev_get_drvdata(dev);
  337. if (device_may_wakeup(dev)) {
  338. if (palmas_usb->enable_vbus_detection)
  339. enable_irq_wake(palmas_usb->vbus_irq);
  340. if (palmas_usb->enable_gpio_vbus_detection)
  341. enable_irq_wake(palmas_usb->gpio_vbus_irq);
  342. if (palmas_usb->enable_id_detection)
  343. enable_irq_wake(palmas_usb->id_irq);
  344. if (palmas_usb->enable_gpio_id_detection)
  345. enable_irq_wake(palmas_usb->gpio_id_irq);
  346. }
  347. return 0;
  348. }
  349. static int palmas_usb_resume(struct device *dev)
  350. {
  351. struct palmas_usb *palmas_usb = dev_get_drvdata(dev);
  352. if (device_may_wakeup(dev)) {
  353. if (palmas_usb->enable_vbus_detection)
  354. disable_irq_wake(palmas_usb->vbus_irq);
  355. if (palmas_usb->enable_gpio_vbus_detection)
  356. disable_irq_wake(palmas_usb->gpio_vbus_irq);
  357. if (palmas_usb->enable_id_detection)
  358. disable_irq_wake(palmas_usb->id_irq);
  359. if (palmas_usb->enable_gpio_id_detection)
  360. disable_irq_wake(palmas_usb->gpio_id_irq);
  361. }
  362. return 0;
  363. };
  364. #endif
  365. static SIMPLE_DEV_PM_OPS(palmas_pm_ops, palmas_usb_suspend, palmas_usb_resume);
  366. static const struct of_device_id of_palmas_match_tbl[] = {
  367. { .compatible = "ti,palmas-usb", },
  368. { .compatible = "ti,palmas-usb-vid", },
  369. { .compatible = "ti,twl6035-usb", },
  370. { .compatible = "ti,twl6035-usb-vid", },
  371. { /* end */ }
  372. };
  373. static struct platform_driver palmas_usb_driver = {
  374. .probe = palmas_usb_probe,
  375. .remove = palmas_usb_remove,
  376. .driver = {
  377. .name = "palmas-usb",
  378. .of_match_table = of_palmas_match_tbl,
  379. .pm = &palmas_pm_ops,
  380. },
  381. };
  382. module_platform_driver(palmas_usb_driver);
  383. MODULE_ALIAS("platform:palmas-usb");
  384. MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
  385. MODULE_DESCRIPTION("Palmas USB transceiver driver");
  386. MODULE_LICENSE("GPL");
  387. MODULE_DEVICE_TABLE(of, of_palmas_match_tbl);