phy-twl6030-usb.c 12 KB

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