twl6030-usb.c 12 KB

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