phy-twl6030-usb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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 - https://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 struct attribute *twl6030_attrs[] = {
  162. &dev_attr_vbus.attr,
  163. NULL,
  164. };
  165. ATTRIBUTE_GROUPS(twl6030);
  166. static irqreturn_t twl6030_usb_irq(int irq, void *_twl)
  167. {
  168. struct twl6030_usb *twl = _twl;
  169. enum musb_vbus_id_status status = MUSB_UNKNOWN;
  170. u8 vbus_state, hw_state;
  171. int ret;
  172. hw_state = twl6030_readb(twl, TWL6030_MODULE_ID0, STS_HW_CONDITIONS);
  173. vbus_state = twl6030_readb(twl, TWL_MODULE_MAIN_CHARGE,
  174. CONTROLLER_STAT1);
  175. if (!(hw_state & STS_USB_ID)) {
  176. if (vbus_state & VBUS_DET) {
  177. ret = regulator_enable(twl->usb3v3);
  178. if (ret)
  179. dev_err(twl->dev, "Failed to enable usb3v3\n");
  180. twl->asleep = 1;
  181. status = MUSB_VBUS_VALID;
  182. twl->linkstat = status;
  183. ret = musb_mailbox(status);
  184. if (ret)
  185. twl->linkstat = MUSB_UNKNOWN;
  186. } else {
  187. if (twl->linkstat != MUSB_UNKNOWN) {
  188. status = MUSB_VBUS_OFF;
  189. twl->linkstat = status;
  190. ret = musb_mailbox(status);
  191. if (ret)
  192. twl->linkstat = MUSB_UNKNOWN;
  193. if (twl->asleep) {
  194. regulator_disable(twl->usb3v3);
  195. twl->asleep = 0;
  196. }
  197. }
  198. }
  199. }
  200. sysfs_notify(&twl->dev->kobj, NULL, "vbus");
  201. return IRQ_HANDLED;
  202. }
  203. static irqreturn_t twl6030_usbotg_irq(int irq, void *_twl)
  204. {
  205. struct twl6030_usb *twl = _twl;
  206. enum musb_vbus_id_status status = MUSB_UNKNOWN;
  207. u8 hw_state;
  208. int ret;
  209. hw_state = twl6030_readb(twl, TWL6030_MODULE_ID0, STS_HW_CONDITIONS);
  210. if (hw_state & STS_USB_ID) {
  211. ret = regulator_enable(twl->usb3v3);
  212. if (ret)
  213. dev_err(twl->dev, "Failed to enable usb3v3\n");
  214. twl->asleep = 1;
  215. twl6030_writeb(twl, TWL_MODULE_USB, 0x1, USB_ID_INT_EN_HI_CLR);
  216. twl6030_writeb(twl, TWL_MODULE_USB, 0x10, USB_ID_INT_EN_HI_SET);
  217. status = MUSB_ID_GROUND;
  218. twl->linkstat = status;
  219. ret = musb_mailbox(status);
  220. if (ret)
  221. twl->linkstat = MUSB_UNKNOWN;
  222. } else {
  223. twl6030_writeb(twl, TWL_MODULE_USB, 0x10, USB_ID_INT_EN_HI_CLR);
  224. twl6030_writeb(twl, TWL_MODULE_USB, 0x1, USB_ID_INT_EN_HI_SET);
  225. }
  226. twl6030_writeb(twl, TWL_MODULE_USB, status, USB_ID_INT_LATCH_CLR);
  227. return IRQ_HANDLED;
  228. }
  229. static void twl6030_status_work(struct work_struct *work)
  230. {
  231. struct twl6030_usb *twl = container_of(work, struct twl6030_usb,
  232. get_status_work.work);
  233. twl6030_usb_irq(twl->irq2, twl);
  234. twl6030_usbotg_irq(twl->irq1, twl);
  235. }
  236. static int twl6030_enable_irq(struct twl6030_usb *twl)
  237. {
  238. twl6030_writeb(twl, TWL_MODULE_USB, 0x1, USB_ID_INT_EN_HI_SET);
  239. twl6030_interrupt_unmask(0x05, REG_INT_MSK_LINE_C);
  240. twl6030_interrupt_unmask(0x05, REG_INT_MSK_STS_C);
  241. twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK,
  242. REG_INT_MSK_LINE_C);
  243. twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK,
  244. REG_INT_MSK_STS_C);
  245. return 0;
  246. }
  247. static void otg_set_vbus_work(struct work_struct *data)
  248. {
  249. struct twl6030_usb *twl = container_of(data, struct twl6030_usb,
  250. set_vbus_work);
  251. /*
  252. * Start driving VBUS. Set OPA_MODE bit in CHARGERUSB_CTRL1
  253. * register. This enables boost mode.
  254. */
  255. if (twl->vbus_enable)
  256. twl6030_writeb(twl, TWL_MODULE_MAIN_CHARGE, 0x40,
  257. CHARGERUSB_CTRL1);
  258. else
  259. twl6030_writeb(twl, TWL_MODULE_MAIN_CHARGE, 0x00,
  260. CHARGERUSB_CTRL1);
  261. }
  262. static int twl6030_set_vbus(struct phy_companion *comparator, bool enabled)
  263. {
  264. struct twl6030_usb *twl = comparator_to_twl(comparator);
  265. twl->vbus_enable = enabled;
  266. schedule_work(&twl->set_vbus_work);
  267. return 0;
  268. }
  269. static int twl6030_usb_probe(struct platform_device *pdev)
  270. {
  271. u32 ret;
  272. struct twl6030_usb *twl;
  273. int status, err;
  274. struct device_node *np = pdev->dev.of_node;
  275. struct device *dev = &pdev->dev;
  276. if (!np) {
  277. dev_err(dev, "no DT info\n");
  278. return -EINVAL;
  279. }
  280. twl = devm_kzalloc(dev, sizeof(*twl), GFP_KERNEL);
  281. if (!twl)
  282. return -ENOMEM;
  283. twl->dev = &pdev->dev;
  284. twl->irq1 = platform_get_irq(pdev, 0);
  285. twl->irq2 = platform_get_irq(pdev, 1);
  286. twl->linkstat = MUSB_UNKNOWN;
  287. if (twl->irq1 < 0)
  288. return twl->irq1;
  289. if (twl->irq2 < 0)
  290. return twl->irq2;
  291. twl->comparator.set_vbus = twl6030_set_vbus;
  292. twl->comparator.start_srp = twl6030_start_srp;
  293. ret = omap_usb2_set_comparator(&twl->comparator);
  294. if (ret == -ENODEV) {
  295. dev_info(&pdev->dev, "phy not ready, deferring probe");
  296. return -EPROBE_DEFER;
  297. }
  298. /* init spinlock for workqueue */
  299. spin_lock_init(&twl->lock);
  300. err = twl6030_usb_ldo_init(twl);
  301. if (err) {
  302. dev_err(&pdev->dev, "ldo init failed\n");
  303. return err;
  304. }
  305. platform_set_drvdata(pdev, twl);
  306. INIT_WORK(&twl->set_vbus_work, otg_set_vbus_work);
  307. INIT_DELAYED_WORK(&twl->get_status_work, twl6030_status_work);
  308. status = request_threaded_irq(twl->irq1, NULL, twl6030_usbotg_irq,
  309. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  310. "twl6030_usb", twl);
  311. if (status < 0) {
  312. dev_err(&pdev->dev, "can't get IRQ %d, err %d\n",
  313. twl->irq1, status);
  314. goto err_put_regulator;
  315. }
  316. status = request_threaded_irq(twl->irq2, NULL, twl6030_usb_irq,
  317. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  318. "twl6030_usb", twl);
  319. if (status < 0) {
  320. dev_err(&pdev->dev, "can't get IRQ %d, err %d\n",
  321. twl->irq2, status);
  322. goto err_free_irq1;
  323. }
  324. twl->asleep = 0;
  325. twl6030_enable_irq(twl);
  326. schedule_delayed_work(&twl->get_status_work, HZ);
  327. dev_info(&pdev->dev, "Initialized TWL6030 USB module\n");
  328. return 0;
  329. err_free_irq1:
  330. free_irq(twl->irq1, twl);
  331. err_put_regulator:
  332. regulator_put(twl->usb3v3);
  333. return status;
  334. }
  335. static int twl6030_usb_remove(struct platform_device *pdev)
  336. {
  337. struct twl6030_usb *twl = platform_get_drvdata(pdev);
  338. cancel_delayed_work_sync(&twl->get_status_work);
  339. twl6030_interrupt_mask(TWL6030_USBOTG_INT_MASK,
  340. REG_INT_MSK_LINE_C);
  341. twl6030_interrupt_mask(TWL6030_USBOTG_INT_MASK,
  342. REG_INT_MSK_STS_C);
  343. free_irq(twl->irq1, twl);
  344. free_irq(twl->irq2, twl);
  345. regulator_put(twl->usb3v3);
  346. cancel_work_sync(&twl->set_vbus_work);
  347. return 0;
  348. }
  349. static const struct of_device_id twl6030_usb_id_table[] = {
  350. { .compatible = "ti,twl6030-usb" },
  351. {}
  352. };
  353. MODULE_DEVICE_TABLE(of, twl6030_usb_id_table);
  354. static struct platform_driver twl6030_usb_driver = {
  355. .probe = twl6030_usb_probe,
  356. .remove = twl6030_usb_remove,
  357. .driver = {
  358. .name = "twl6030_usb",
  359. .of_match_table = of_match_ptr(twl6030_usb_id_table),
  360. .dev_groups = twl6030_groups,
  361. },
  362. };
  363. static int __init twl6030_usb_init(void)
  364. {
  365. return platform_driver_register(&twl6030_usb_driver);
  366. }
  367. subsys_initcall(twl6030_usb_init);
  368. static void __exit twl6030_usb_exit(void)
  369. {
  370. platform_driver_unregister(&twl6030_usb_driver);
  371. }
  372. module_exit(twl6030_usb_exit);
  373. MODULE_ALIAS("platform:twl6030_usb");
  374. MODULE_AUTHOR("Hema HK <hemahk@ti.com>");
  375. MODULE_DESCRIPTION("TWL6030 USB transceiver driver");
  376. MODULE_LICENSE("GPL");