phy-twl6030-usb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * twl6030_usb - TWL6030 USB transceiver, talking to OMAP OTG driver.
  4. *
  5. * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com
  6. *
  7. * Author: Hema HK <hemahk@ti.com>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/io.h>
  14. #include <linux/usb/musb.h>
  15. #include <linux/usb/phy_companion.h>
  16. #include <linux/phy/omap_usb.h>
  17. #include <linux/mfd/twl.h>
  18. #include <linux/regulator/consumer.h>
  19. #include <linux/err.h>
  20. #include <linux/slab.h>
  21. #include <linux/delay.h>
  22. #include <linux/of.h>
  23. /* usb register definitions */
  24. #define USB_VENDOR_ID_LSB 0x00
  25. #define USB_VENDOR_ID_MSB 0x01
  26. #define USB_PRODUCT_ID_LSB 0x02
  27. #define USB_PRODUCT_ID_MSB 0x03
  28. #define USB_VBUS_CTRL_SET 0x04
  29. #define USB_VBUS_CTRL_CLR 0x05
  30. #define USB_ID_CTRL_SET 0x06
  31. #define USB_ID_CTRL_CLR 0x07
  32. #define USB_VBUS_INT_SRC 0x08
  33. #define USB_VBUS_INT_LATCH_SET 0x09
  34. #define USB_VBUS_INT_LATCH_CLR 0x0A
  35. #define USB_VBUS_INT_EN_LO_SET 0x0B
  36. #define USB_VBUS_INT_EN_LO_CLR 0x0C
  37. #define USB_VBUS_INT_EN_HI_SET 0x0D
  38. #define USB_VBUS_INT_EN_HI_CLR 0x0E
  39. #define USB_ID_INT_SRC 0x0F
  40. #define USB_ID_INT_LATCH_SET 0x10
  41. #define USB_ID_INT_LATCH_CLR 0x11
  42. #define USB_ID_INT_EN_LO_SET 0x12
  43. #define USB_ID_INT_EN_LO_CLR 0x13
  44. #define USB_ID_INT_EN_HI_SET 0x14
  45. #define USB_ID_INT_EN_HI_CLR 0x15
  46. #define USB_OTG_ADP_CTRL 0x16
  47. #define USB_OTG_ADP_HIGH 0x17
  48. #define USB_OTG_ADP_LOW 0x18
  49. #define USB_OTG_ADP_RISE 0x19
  50. #define USB_OTG_REVISION 0x1A
  51. /* to be moved to LDO */
  52. #define TWL6030_MISC2 0xE5
  53. #define TWL6030_CFG_LDO_PD2 0xF5
  54. #define TWL6030_BACKUP_REG 0xFA
  55. #define STS_HW_CONDITIONS 0x21
  56. /* In module TWL6030_MODULE_PM_MASTER */
  57. #define STS_HW_CONDITIONS 0x21
  58. #define STS_USB_ID BIT(2)
  59. /* In module TWL6030_MODULE_PM_RECEIVER */
  60. #define VUSB_CFG_TRANS 0x71
  61. #define VUSB_CFG_STATE 0x72
  62. #define VUSB_CFG_VOLTAGE 0x73
  63. /* in module TWL6030_MODULE_MAIN_CHARGE */
  64. #define CHARGERUSB_CTRL1 0x8
  65. #define CONTROLLER_STAT1 0x03
  66. #define VBUS_DET BIT(2)
  67. struct twl6030_usb {
  68. struct phy_companion comparator;
  69. struct device *dev;
  70. /* for vbus reporting with irqs disabled */
  71. spinlock_t lock;
  72. struct regulator *usb3v3;
  73. /* used to check initial cable status after probe */
  74. struct delayed_work get_status_work;
  75. /* used to set vbus, in atomic path */
  76. struct work_struct set_vbus_work;
  77. int irq1;
  78. int irq2;
  79. enum musb_vbus_id_status linkstat;
  80. u8 asleep;
  81. bool vbus_enable;
  82. };
  83. #define comparator_to_twl(x) container_of((x), struct twl6030_usb, comparator)
  84. /*-------------------------------------------------------------------------*/
  85. static inline int twl6030_writeb(struct twl6030_usb *twl, u8 module,
  86. u8 data, u8 address)
  87. {
  88. int ret = 0;
  89. ret = twl_i2c_write_u8(module, data, address);
  90. if (ret < 0)
  91. dev_err(twl->dev,
  92. "Write[0x%x] Error %d\n", address, ret);
  93. return ret;
  94. }
  95. static inline u8 twl6030_readb(struct twl6030_usb *twl, u8 module, u8 address)
  96. {
  97. u8 data;
  98. int ret;
  99. ret = twl_i2c_read_u8(module, &data, address);
  100. if (ret >= 0)
  101. ret = data;
  102. else
  103. dev_err(twl->dev,
  104. "readb[0x%x,0x%x] Error %d\n",
  105. module, address, ret);
  106. return ret;
  107. }
  108. static int twl6030_start_srp(struct phy_companion *comparator)
  109. {
  110. struct twl6030_usb *twl = comparator_to_twl(comparator);
  111. twl6030_writeb(twl, TWL_MODULE_USB, 0x24, USB_VBUS_CTRL_SET);
  112. twl6030_writeb(twl, TWL_MODULE_USB, 0x84, USB_VBUS_CTRL_SET);
  113. mdelay(100);
  114. twl6030_writeb(twl, TWL_MODULE_USB, 0xa0, USB_VBUS_CTRL_CLR);
  115. return 0;
  116. }
  117. static int twl6030_usb_ldo_init(struct twl6030_usb *twl)
  118. {
  119. /* Set to OTG_REV 1.3 and turn on the ID_WAKEUP_COMP */
  120. twl6030_writeb(twl, TWL6030_MODULE_ID0, 0x1, TWL6030_BACKUP_REG);
  121. /* Program CFG_LDO_PD2 register and set VUSB bit */
  122. twl6030_writeb(twl, TWL6030_MODULE_ID0, 0x1, TWL6030_CFG_LDO_PD2);
  123. /* Program MISC2 register and set bit VUSB_IN_VBAT */
  124. twl6030_writeb(twl, TWL6030_MODULE_ID0, 0x10, TWL6030_MISC2);
  125. twl->usb3v3 = regulator_get(twl->dev, "usb");
  126. if (IS_ERR(twl->usb3v3))
  127. return -ENODEV;
  128. /* Program the USB_VBUS_CTRL_SET and set VBUS_ACT_COMP bit */
  129. twl6030_writeb(twl, TWL_MODULE_USB, 0x4, USB_VBUS_CTRL_SET);
  130. /*
  131. * Program the USB_ID_CTRL_SET register to enable GND drive
  132. * and the ID comparators
  133. */
  134. twl6030_writeb(twl, TWL_MODULE_USB, 0x14, USB_ID_CTRL_SET);
  135. return 0;
  136. }
  137. static ssize_t vbus_show(struct device *dev,
  138. struct device_attribute *attr, char *buf)
  139. {
  140. struct twl6030_usb *twl = dev_get_drvdata(dev);
  141. unsigned long flags;
  142. int ret = -EINVAL;
  143. spin_lock_irqsave(&twl->lock, flags);
  144. switch (twl->linkstat) {
  145. case MUSB_VBUS_VALID:
  146. ret = snprintf(buf, PAGE_SIZE, "vbus\n");
  147. break;
  148. case MUSB_ID_GROUND:
  149. ret = snprintf(buf, PAGE_SIZE, "id\n");
  150. break;
  151. case MUSB_VBUS_OFF:
  152. ret = snprintf(buf, PAGE_SIZE, "none\n");
  153. break;
  154. default:
  155. ret = snprintf(buf, PAGE_SIZE, "UNKNOWN\n");
  156. }
  157. spin_unlock_irqrestore(&twl->lock, flags);
  158. return ret;
  159. }
  160. static DEVICE_ATTR_RO(vbus);
  161. static irqreturn_t twl6030_usb_irq(int irq, void *_twl)
  162. {
  163. struct twl6030_usb *twl = _twl;
  164. enum musb_vbus_id_status status = MUSB_UNKNOWN;
  165. u8 vbus_state, hw_state;
  166. int ret;
  167. hw_state = twl6030_readb(twl, TWL6030_MODULE_ID0, STS_HW_CONDITIONS);
  168. vbus_state = twl6030_readb(twl, TWL_MODULE_MAIN_CHARGE,
  169. CONTROLLER_STAT1);
  170. if (!(hw_state & STS_USB_ID)) {
  171. if (vbus_state & VBUS_DET) {
  172. ret = regulator_enable(twl->usb3v3);
  173. if (ret)
  174. dev_err(twl->dev, "Failed to enable usb3v3\n");
  175. twl->asleep = 1;
  176. status = MUSB_VBUS_VALID;
  177. twl->linkstat = status;
  178. ret = musb_mailbox(status);
  179. if (ret)
  180. twl->linkstat = MUSB_UNKNOWN;
  181. } else {
  182. if (twl->linkstat != MUSB_UNKNOWN) {
  183. status = MUSB_VBUS_OFF;
  184. twl->linkstat = status;
  185. ret = musb_mailbox(status);
  186. if (ret)
  187. twl->linkstat = MUSB_UNKNOWN;
  188. if (twl->asleep) {
  189. regulator_disable(twl->usb3v3);
  190. twl->asleep = 0;
  191. }
  192. }
  193. }
  194. }
  195. sysfs_notify(&twl->dev->kobj, NULL, "vbus");
  196. return IRQ_HANDLED;
  197. }
  198. static irqreturn_t twl6030_usbotg_irq(int irq, void *_twl)
  199. {
  200. struct twl6030_usb *twl = _twl;
  201. enum musb_vbus_id_status status = MUSB_UNKNOWN;
  202. u8 hw_state;
  203. int ret;
  204. hw_state = twl6030_readb(twl, TWL6030_MODULE_ID0, STS_HW_CONDITIONS);
  205. if (hw_state & STS_USB_ID) {
  206. ret = regulator_enable(twl->usb3v3);
  207. if (ret)
  208. dev_err(twl->dev, "Failed to enable usb3v3\n");
  209. twl->asleep = 1;
  210. twl6030_writeb(twl, TWL_MODULE_USB, 0x1, USB_ID_INT_EN_HI_CLR);
  211. twl6030_writeb(twl, TWL_MODULE_USB, 0x10, USB_ID_INT_EN_HI_SET);
  212. status = MUSB_ID_GROUND;
  213. twl->linkstat = status;
  214. ret = musb_mailbox(status);
  215. if (ret)
  216. twl->linkstat = MUSB_UNKNOWN;
  217. } else {
  218. twl6030_writeb(twl, TWL_MODULE_USB, 0x10, USB_ID_INT_EN_HI_CLR);
  219. twl6030_writeb(twl, TWL_MODULE_USB, 0x1, USB_ID_INT_EN_HI_SET);
  220. }
  221. twl6030_writeb(twl, TWL_MODULE_USB, status, USB_ID_INT_LATCH_CLR);
  222. return IRQ_HANDLED;
  223. }
  224. static void twl6030_status_work(struct work_struct *work)
  225. {
  226. struct twl6030_usb *twl = container_of(work, struct twl6030_usb,
  227. get_status_work.work);
  228. twl6030_usb_irq(twl->irq2, twl);
  229. twl6030_usbotg_irq(twl->irq1, twl);
  230. }
  231. static int twl6030_enable_irq(struct twl6030_usb *twl)
  232. {
  233. twl6030_writeb(twl, TWL_MODULE_USB, 0x1, USB_ID_INT_EN_HI_SET);
  234. twl6030_interrupt_unmask(0x05, REG_INT_MSK_LINE_C);
  235. twl6030_interrupt_unmask(0x05, REG_INT_MSK_STS_C);
  236. twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK,
  237. REG_INT_MSK_LINE_C);
  238. twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK,
  239. REG_INT_MSK_STS_C);
  240. return 0;
  241. }
  242. static void otg_set_vbus_work(struct work_struct *data)
  243. {
  244. struct twl6030_usb *twl = container_of(data, struct twl6030_usb,
  245. set_vbus_work);
  246. /*
  247. * Start driving VBUS. Set OPA_MODE bit in CHARGERUSB_CTRL1
  248. * register. This enables boost mode.
  249. */
  250. if (twl->vbus_enable)
  251. twl6030_writeb(twl, TWL_MODULE_MAIN_CHARGE, 0x40,
  252. CHARGERUSB_CTRL1);
  253. else
  254. twl6030_writeb(twl, TWL_MODULE_MAIN_CHARGE, 0x00,
  255. CHARGERUSB_CTRL1);
  256. }
  257. static int twl6030_set_vbus(struct phy_companion *comparator, bool enabled)
  258. {
  259. struct twl6030_usb *twl = comparator_to_twl(comparator);
  260. twl->vbus_enable = enabled;
  261. schedule_work(&twl->set_vbus_work);
  262. return 0;
  263. }
  264. static int twl6030_usb_probe(struct platform_device *pdev)
  265. {
  266. u32 ret;
  267. struct twl6030_usb *twl;
  268. int status, err;
  269. struct device_node *np = pdev->dev.of_node;
  270. struct device *dev = &pdev->dev;
  271. if (!np) {
  272. dev_err(dev, "no DT info\n");
  273. return -EINVAL;
  274. }
  275. twl = devm_kzalloc(dev, sizeof(*twl), GFP_KERNEL);
  276. if (!twl)
  277. return -ENOMEM;
  278. twl->dev = &pdev->dev;
  279. twl->irq1 = platform_get_irq(pdev, 0);
  280. twl->irq2 = platform_get_irq(pdev, 1);
  281. twl->linkstat = MUSB_UNKNOWN;
  282. twl->comparator.set_vbus = twl6030_set_vbus;
  283. twl->comparator.start_srp = twl6030_start_srp;
  284. ret = omap_usb2_set_comparator(&twl->comparator);
  285. if (ret == -ENODEV) {
  286. dev_info(&pdev->dev, "phy not ready, deferring probe");
  287. return -EPROBE_DEFER;
  288. }
  289. /* init spinlock for workqueue */
  290. spin_lock_init(&twl->lock);
  291. err = twl6030_usb_ldo_init(twl);
  292. if (err) {
  293. dev_err(&pdev->dev, "ldo init failed\n");
  294. return err;
  295. }
  296. platform_set_drvdata(pdev, twl);
  297. if (device_create_file(&pdev->dev, &dev_attr_vbus))
  298. dev_warn(&pdev->dev, "could not create sysfs file\n");
  299. INIT_WORK(&twl->set_vbus_work, otg_set_vbus_work);
  300. INIT_DELAYED_WORK(&twl->get_status_work, twl6030_status_work);
  301. status = request_threaded_irq(twl->irq1, NULL, twl6030_usbotg_irq,
  302. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  303. "twl6030_usb", twl);
  304. if (status < 0) {
  305. dev_err(&pdev->dev, "can't get IRQ %d, err %d\n",
  306. twl->irq1, status);
  307. device_remove_file(twl->dev, &dev_attr_vbus);
  308. return status;
  309. }
  310. status = request_threaded_irq(twl->irq2, NULL, twl6030_usb_irq,
  311. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  312. "twl6030_usb", twl);
  313. if (status < 0) {
  314. dev_err(&pdev->dev, "can't get IRQ %d, err %d\n",
  315. twl->irq2, status);
  316. free_irq(twl->irq1, twl);
  317. device_remove_file(twl->dev, &dev_attr_vbus);
  318. return status;
  319. }
  320. twl->asleep = 0;
  321. twl6030_enable_irq(twl);
  322. schedule_delayed_work(&twl->get_status_work, HZ);
  323. dev_info(&pdev->dev, "Initialized TWL6030 USB module\n");
  324. return 0;
  325. }
  326. static int twl6030_usb_remove(struct platform_device *pdev)
  327. {
  328. struct twl6030_usb *twl = platform_get_drvdata(pdev);
  329. cancel_delayed_work_sync(&twl->get_status_work);
  330. twl6030_interrupt_mask(TWL6030_USBOTG_INT_MASK,
  331. REG_INT_MSK_LINE_C);
  332. twl6030_interrupt_mask(TWL6030_USBOTG_INT_MASK,
  333. REG_INT_MSK_STS_C);
  334. free_irq(twl->irq1, twl);
  335. free_irq(twl->irq2, twl);
  336. regulator_put(twl->usb3v3);
  337. device_remove_file(twl->dev, &dev_attr_vbus);
  338. cancel_work_sync(&twl->set_vbus_work);
  339. return 0;
  340. }
  341. static const struct of_device_id twl6030_usb_id_table[] = {
  342. { .compatible = "ti,twl6030-usb" },
  343. {}
  344. };
  345. MODULE_DEVICE_TABLE(of, twl6030_usb_id_table);
  346. static struct platform_driver twl6030_usb_driver = {
  347. .probe = twl6030_usb_probe,
  348. .remove = twl6030_usb_remove,
  349. .driver = {
  350. .name = "twl6030_usb",
  351. .of_match_table = of_match_ptr(twl6030_usb_id_table),
  352. },
  353. };
  354. static int __init twl6030_usb_init(void)
  355. {
  356. return platform_driver_register(&twl6030_usb_driver);
  357. }
  358. subsys_initcall(twl6030_usb_init);
  359. static void __exit twl6030_usb_exit(void)
  360. {
  361. platform_driver_unregister(&twl6030_usb_driver);
  362. }
  363. module_exit(twl6030_usb_exit);
  364. MODULE_ALIAS("platform:twl6030_usb");
  365. MODULE_AUTHOR("Hema HK <hemahk@ti.com>");
  366. MODULE_DESCRIPTION("TWL6030 USB transceiver driver");
  367. MODULE_LICENSE("GPL");