phy-twl6030-usb.c 12 KB

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