phy-twl6030-usb.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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-omap.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 set vbus, in atomic path */
  87. struct work_struct set_vbus_work;
  88. int irq1;
  89. int irq2;
  90. enum omap_musb_vbus_id_status linkstat;
  91. u8 asleep;
  92. bool irq_enabled;
  93. bool vbus_enable;
  94. const char *regulator;
  95. };
  96. #define comparator_to_twl(x) container_of((x), struct twl6030_usb, comparator)
  97. /*-------------------------------------------------------------------------*/
  98. static inline int twl6030_writeb(struct twl6030_usb *twl, u8 module,
  99. u8 data, u8 address)
  100. {
  101. int ret = 0;
  102. ret = twl_i2c_write_u8(module, data, address);
  103. if (ret < 0)
  104. dev_err(twl->dev,
  105. "Write[0x%x] Error %d\n", address, ret);
  106. return ret;
  107. }
  108. static inline u8 twl6030_readb(struct twl6030_usb *twl, u8 module, u8 address)
  109. {
  110. u8 data;
  111. int ret;
  112. ret = twl_i2c_read_u8(module, &data, address);
  113. if (ret >= 0)
  114. ret = data;
  115. else
  116. dev_err(twl->dev,
  117. "readb[0x%x,0x%x] Error %d\n",
  118. module, address, ret);
  119. return ret;
  120. }
  121. static int twl6030_start_srp(struct phy_companion *comparator)
  122. {
  123. struct twl6030_usb *twl = comparator_to_twl(comparator);
  124. twl6030_writeb(twl, TWL_MODULE_USB, 0x24, USB_VBUS_CTRL_SET);
  125. twl6030_writeb(twl, TWL_MODULE_USB, 0x84, USB_VBUS_CTRL_SET);
  126. mdelay(100);
  127. twl6030_writeb(twl, TWL_MODULE_USB, 0xa0, USB_VBUS_CTRL_CLR);
  128. return 0;
  129. }
  130. static int twl6030_usb_ldo_init(struct twl6030_usb *twl)
  131. {
  132. /* Set to OTG_REV 1.3 and turn on the ID_WAKEUP_COMP */
  133. twl6030_writeb(twl, TWL6030_MODULE_ID0 , 0x1, TWL6030_BACKUP_REG);
  134. /* Program CFG_LDO_PD2 register and set VUSB bit */
  135. twl6030_writeb(twl, TWL6030_MODULE_ID0 , 0x1, TWL6030_CFG_LDO_PD2);
  136. /* Program MISC2 register and set bit VUSB_IN_VBAT */
  137. twl6030_writeb(twl, TWL6030_MODULE_ID0 , 0x10, TWL6030_MISC2);
  138. twl->usb3v3 = regulator_get(twl->dev, twl->regulator);
  139. if (IS_ERR(twl->usb3v3))
  140. return -ENODEV;
  141. /* Program the USB_VBUS_CTRL_SET and set VBUS_ACT_COMP bit */
  142. twl6030_writeb(twl, TWL_MODULE_USB, 0x4, USB_VBUS_CTRL_SET);
  143. /*
  144. * Program the USB_ID_CTRL_SET register to enable GND drive
  145. * and the ID comparators
  146. */
  147. twl6030_writeb(twl, TWL_MODULE_USB, 0x14, USB_ID_CTRL_SET);
  148. return 0;
  149. }
  150. static ssize_t twl6030_usb_vbus_show(struct device *dev,
  151. struct device_attribute *attr, char *buf)
  152. {
  153. struct twl6030_usb *twl = dev_get_drvdata(dev);
  154. unsigned long flags;
  155. int ret = -EINVAL;
  156. spin_lock_irqsave(&twl->lock, flags);
  157. switch (twl->linkstat) {
  158. case OMAP_MUSB_VBUS_VALID:
  159. ret = snprintf(buf, PAGE_SIZE, "vbus\n");
  160. break;
  161. case OMAP_MUSB_ID_GROUND:
  162. ret = snprintf(buf, PAGE_SIZE, "id\n");
  163. break;
  164. case OMAP_MUSB_VBUS_OFF:
  165. ret = snprintf(buf, PAGE_SIZE, "none\n");
  166. break;
  167. default:
  168. ret = snprintf(buf, PAGE_SIZE, "UNKNOWN\n");
  169. }
  170. spin_unlock_irqrestore(&twl->lock, flags);
  171. return ret;
  172. }
  173. static DEVICE_ATTR(vbus, 0444, twl6030_usb_vbus_show, NULL);
  174. static irqreturn_t twl6030_usb_irq(int irq, void *_twl)
  175. {
  176. struct twl6030_usb *twl = _twl;
  177. enum omap_musb_vbus_id_status status = OMAP_MUSB_UNKNOWN;
  178. u8 vbus_state, hw_state;
  179. int ret;
  180. hw_state = twl6030_readb(twl, TWL6030_MODULE_ID0, STS_HW_CONDITIONS);
  181. vbus_state = twl6030_readb(twl, TWL_MODULE_MAIN_CHARGE,
  182. CONTROLLER_STAT1);
  183. if (!(hw_state & STS_USB_ID)) {
  184. if (vbus_state & VBUS_DET) {
  185. ret = regulator_enable(twl->usb3v3);
  186. if (ret)
  187. dev_err(twl->dev, "Failed to enable usb3v3\n");
  188. twl->asleep = 1;
  189. status = OMAP_MUSB_VBUS_VALID;
  190. twl->linkstat = status;
  191. omap_musb_mailbox(status);
  192. } else {
  193. if (twl->linkstat != OMAP_MUSB_UNKNOWN) {
  194. status = OMAP_MUSB_VBUS_OFF;
  195. twl->linkstat = status;
  196. omap_musb_mailbox(status);
  197. if (twl->asleep) {
  198. regulator_disable(twl->usb3v3);
  199. twl->asleep = 0;
  200. }
  201. }
  202. }
  203. }
  204. sysfs_notify(&twl->dev->kobj, NULL, "vbus");
  205. return IRQ_HANDLED;
  206. }
  207. static irqreturn_t twl6030_usbotg_irq(int irq, void *_twl)
  208. {
  209. struct twl6030_usb *twl = _twl;
  210. enum omap_musb_vbus_id_status status = OMAP_MUSB_UNKNOWN;
  211. u8 hw_state;
  212. int ret;
  213. hw_state = twl6030_readb(twl, TWL6030_MODULE_ID0, STS_HW_CONDITIONS);
  214. if (hw_state & STS_USB_ID) {
  215. ret = regulator_enable(twl->usb3v3);
  216. if (ret)
  217. dev_err(twl->dev, "Failed to enable usb3v3\n");
  218. twl->asleep = 1;
  219. twl6030_writeb(twl, TWL_MODULE_USB, 0x1, USB_ID_INT_EN_HI_CLR);
  220. twl6030_writeb(twl, TWL_MODULE_USB, 0x10, USB_ID_INT_EN_HI_SET);
  221. status = OMAP_MUSB_ID_GROUND;
  222. twl->linkstat = status;
  223. omap_musb_mailbox(status);
  224. } else {
  225. twl6030_writeb(twl, TWL_MODULE_USB, 0x10, USB_ID_INT_EN_HI_CLR);
  226. twl6030_writeb(twl, TWL_MODULE_USB, 0x1, USB_ID_INT_EN_HI_SET);
  227. }
  228. twl6030_writeb(twl, TWL_MODULE_USB, status, USB_ID_INT_LATCH_CLR);
  229. return IRQ_HANDLED;
  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. twl6030_usb_irq(twl->irq2, twl);
  241. twl6030_usbotg_irq(twl->irq1, twl);
  242. return 0;
  243. }
  244. static void otg_set_vbus_work(struct work_struct *data)
  245. {
  246. struct twl6030_usb *twl = container_of(data, struct twl6030_usb,
  247. set_vbus_work);
  248. /*
  249. * Start driving VBUS. Set OPA_MODE bit in CHARGERUSB_CTRL1
  250. * register. This enables boost mode.
  251. */
  252. if (twl->vbus_enable)
  253. twl6030_writeb(twl, TWL_MODULE_MAIN_CHARGE , 0x40,
  254. CHARGERUSB_CTRL1);
  255. else
  256. twl6030_writeb(twl, TWL_MODULE_MAIN_CHARGE , 0x00,
  257. CHARGERUSB_CTRL1);
  258. }
  259. static int twl6030_set_vbus(struct phy_companion *comparator, bool enabled)
  260. {
  261. struct twl6030_usb *twl = comparator_to_twl(comparator);
  262. twl->vbus_enable = enabled;
  263. schedule_work(&twl->set_vbus_work);
  264. return 0;
  265. }
  266. static int twl6030_usb_probe(struct platform_device *pdev)
  267. {
  268. u32 ret;
  269. struct twl6030_usb *twl;
  270. int status, err;
  271. struct device_node *np = pdev->dev.of_node;
  272. struct device *dev = &pdev->dev;
  273. struct twl4030_usb_data *pdata = dev_get_platdata(dev);
  274. twl = devm_kzalloc(dev, sizeof(*twl), GFP_KERNEL);
  275. if (!twl)
  276. return -ENOMEM;
  277. twl->dev = &pdev->dev;
  278. twl->irq1 = platform_get_irq(pdev, 0);
  279. twl->irq2 = platform_get_irq(pdev, 1);
  280. twl->linkstat = OMAP_MUSB_UNKNOWN;
  281. twl->comparator.set_vbus = twl6030_set_vbus;
  282. twl->comparator.start_srp = twl6030_start_srp;
  283. ret = omap_usb2_set_comparator(&twl->comparator);
  284. if (ret == -ENODEV) {
  285. dev_info(&pdev->dev, "phy not ready, deferring probe");
  286. return -EPROBE_DEFER;
  287. }
  288. if (np) {
  289. twl->regulator = "usb";
  290. } else if (pdata) {
  291. if (pdata->features & TWL6032_SUBCLASS)
  292. twl->regulator = "ldousb";
  293. else
  294. twl->regulator = "vusb";
  295. } else {
  296. dev_err(&pdev->dev, "twl6030 initialized without pdata\n");
  297. return -EINVAL;
  298. }
  299. /* init spinlock for workqueue */
  300. spin_lock_init(&twl->lock);
  301. err = twl6030_usb_ldo_init(twl);
  302. if (err) {
  303. dev_err(&pdev->dev, "ldo init failed\n");
  304. return err;
  305. }
  306. platform_set_drvdata(pdev, twl);
  307. if (device_create_file(&pdev->dev, &dev_attr_vbus))
  308. dev_warn(&pdev->dev, "could not create sysfs file\n");
  309. INIT_WORK(&twl->set_vbus_work, otg_set_vbus_work);
  310. twl->irq_enabled = true;
  311. status = request_threaded_irq(twl->irq1, NULL, twl6030_usbotg_irq,
  312. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  313. "twl6030_usb", twl);
  314. if (status < 0) {
  315. dev_err(&pdev->dev, "can't get IRQ %d, err %d\n",
  316. twl->irq1, status);
  317. device_remove_file(twl->dev, &dev_attr_vbus);
  318. return status;
  319. }
  320. status = request_threaded_irq(twl->irq2, NULL, twl6030_usb_irq,
  321. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  322. "twl6030_usb", twl);
  323. if (status < 0) {
  324. dev_err(&pdev->dev, "can't get IRQ %d, err %d\n",
  325. twl->irq2, status);
  326. free_irq(twl->irq1, twl);
  327. device_remove_file(twl->dev, &dev_attr_vbus);
  328. return status;
  329. }
  330. twl->asleep = 0;
  331. twl6030_enable_irq(twl);
  332. dev_info(&pdev->dev, "Initialized TWL6030 USB module\n");
  333. return 0;
  334. }
  335. static int twl6030_usb_remove(struct platform_device *pdev)
  336. {
  337. struct twl6030_usb *twl = platform_get_drvdata(pdev);
  338. twl6030_interrupt_mask(TWL6030_USBOTG_INT_MASK,
  339. REG_INT_MSK_LINE_C);
  340. twl6030_interrupt_mask(TWL6030_USBOTG_INT_MASK,
  341. REG_INT_MSK_STS_C);
  342. free_irq(twl->irq1, twl);
  343. free_irq(twl->irq2, twl);
  344. regulator_put(twl->usb3v3);
  345. device_remove_file(twl->dev, &dev_attr_vbus);
  346. cancel_work_sync(&twl->set_vbus_work);
  347. return 0;
  348. }
  349. #ifdef CONFIG_OF
  350. static const struct of_device_id twl6030_usb_id_table[] = {
  351. { .compatible = "ti,twl6030-usb" },
  352. {}
  353. };
  354. MODULE_DEVICE_TABLE(of, twl6030_usb_id_table);
  355. #endif
  356. static struct platform_driver twl6030_usb_driver = {
  357. .probe = twl6030_usb_probe,
  358. .remove = twl6030_usb_remove,
  359. .driver = {
  360. .name = "twl6030_usb",
  361. .owner = THIS_MODULE,
  362. .of_match_table = of_match_ptr(twl6030_usb_id_table),
  363. },
  364. };
  365. static int __init twl6030_usb_init(void)
  366. {
  367. return platform_driver_register(&twl6030_usb_driver);
  368. }
  369. subsys_initcall(twl6030_usb_init);
  370. static void __exit twl6030_usb_exit(void)
  371. {
  372. platform_driver_unregister(&twl6030_usb_driver);
  373. }
  374. module_exit(twl6030_usb_exit);
  375. MODULE_ALIAS("platform:twl6030_usb");
  376. MODULE_AUTHOR("Hema HK <hemahk@ti.com>");
  377. MODULE_DESCRIPTION("TWL6030 USB transceiver driver");
  378. MODULE_LICENSE("GPL");