phy-twl6030-usb.c 12 KB

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