twl6030-usb.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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/otg.h>
  28. #include <linux/i2c/twl.h>
  29. #include <linux/regulator/consumer.h>
  30. #include <linux/err.h>
  31. #include <linux/notifier.h>
  32. #include <linux/slab.h>
  33. #include <linux/delay.h>
  34. /* usb register definitions */
  35. #define USB_VENDOR_ID_LSB 0x00
  36. #define USB_VENDOR_ID_MSB 0x01
  37. #define USB_PRODUCT_ID_LSB 0x02
  38. #define USB_PRODUCT_ID_MSB 0x03
  39. #define USB_VBUS_CTRL_SET 0x04
  40. #define USB_VBUS_CTRL_CLR 0x05
  41. #define USB_ID_CTRL_SET 0x06
  42. #define USB_ID_CTRL_CLR 0x07
  43. #define USB_VBUS_INT_SRC 0x08
  44. #define USB_VBUS_INT_LATCH_SET 0x09
  45. #define USB_VBUS_INT_LATCH_CLR 0x0A
  46. #define USB_VBUS_INT_EN_LO_SET 0x0B
  47. #define USB_VBUS_INT_EN_LO_CLR 0x0C
  48. #define USB_VBUS_INT_EN_HI_SET 0x0D
  49. #define USB_VBUS_INT_EN_HI_CLR 0x0E
  50. #define USB_ID_INT_SRC 0x0F
  51. #define USB_ID_INT_LATCH_SET 0x10
  52. #define USB_ID_INT_LATCH_CLR 0x11
  53. #define USB_ID_INT_EN_LO_SET 0x12
  54. #define USB_ID_INT_EN_LO_CLR 0x13
  55. #define USB_ID_INT_EN_HI_SET 0x14
  56. #define USB_ID_INT_EN_HI_CLR 0x15
  57. #define USB_OTG_ADP_CTRL 0x16
  58. #define USB_OTG_ADP_HIGH 0x17
  59. #define USB_OTG_ADP_LOW 0x18
  60. #define USB_OTG_ADP_RISE 0x19
  61. #define USB_OTG_REVISION 0x1A
  62. /* to be moved to LDO */
  63. #define TWL6030_MISC2 0xE5
  64. #define TWL6030_CFG_LDO_PD2 0xF5
  65. #define TWL6030_BACKUP_REG 0xFA
  66. #define STS_HW_CONDITIONS 0x21
  67. /* In module TWL6030_MODULE_PM_MASTER */
  68. #define STS_HW_CONDITIONS 0x21
  69. #define STS_USB_ID BIT(2)
  70. /* In module TWL6030_MODULE_PM_RECEIVER */
  71. #define VUSB_CFG_TRANS 0x71
  72. #define VUSB_CFG_STATE 0x72
  73. #define VUSB_CFG_VOLTAGE 0x73
  74. /* in module TWL6030_MODULE_MAIN_CHARGE */
  75. #define CHARGERUSB_CTRL1 0x8
  76. #define CONTROLLER_STAT1 0x03
  77. #define VBUS_DET BIT(2)
  78. struct twl6030_usb {
  79. struct usb_phy phy;
  80. struct device *dev;
  81. /* for vbus reporting with irqs disabled */
  82. spinlock_t lock;
  83. struct regulator *usb3v3;
  84. /* used to set vbus, in atomic path */
  85. struct work_struct set_vbus_work;
  86. int irq1;
  87. int irq2;
  88. u8 linkstat;
  89. u8 asleep;
  90. bool irq_enabled;
  91. bool vbus_enable;
  92. unsigned long features;
  93. };
  94. #define phy_to_twl(x) container_of((x), struct twl6030_usb, phy)
  95. /*-------------------------------------------------------------------------*/
  96. static inline int twl6030_writeb(struct twl6030_usb *twl, u8 module,
  97. u8 data, u8 address)
  98. {
  99. int ret = 0;
  100. ret = twl_i2c_write_u8(module, data, address);
  101. if (ret < 0)
  102. dev_err(twl->dev,
  103. "Write[0x%x] Error %d\n", address, ret);
  104. return ret;
  105. }
  106. static inline u8 twl6030_readb(struct twl6030_usb *twl, u8 module, u8 address)
  107. {
  108. u8 data, ret = 0;
  109. ret = twl_i2c_read_u8(module, &data, address);
  110. if (ret >= 0)
  111. ret = data;
  112. else
  113. dev_err(twl->dev,
  114. "readb[0x%x,0x%x] Error %d\n",
  115. module, address, ret);
  116. return ret;
  117. }
  118. static int twl6030_phy_init(struct usb_phy *x)
  119. {
  120. struct twl6030_usb *twl;
  121. struct device *dev;
  122. struct twl4030_usb_data *pdata;
  123. twl = phy_to_twl(x);
  124. dev = twl->dev;
  125. pdata = dev->platform_data;
  126. if (twl->linkstat == USB_EVENT_ID)
  127. pdata->phy_power(twl->dev, 1, 1);
  128. else
  129. pdata->phy_power(twl->dev, 0, 1);
  130. return 0;
  131. }
  132. static void twl6030_phy_shutdown(struct usb_phy *x)
  133. {
  134. struct twl6030_usb *twl;
  135. struct device *dev;
  136. struct twl4030_usb_data *pdata;
  137. twl = phy_to_twl(x);
  138. dev = twl->dev;
  139. pdata = dev->platform_data;
  140. pdata->phy_power(twl->dev, 0, 0);
  141. }
  142. static int twl6030_phy_suspend(struct usb_phy *x, int suspend)
  143. {
  144. struct twl6030_usb *twl = phy_to_twl(x);
  145. struct device *dev = twl->dev;
  146. struct twl4030_usb_data *pdata = dev->platform_data;
  147. pdata->phy_suspend(dev, suspend);
  148. return 0;
  149. }
  150. static int twl6030_start_srp(struct usb_otg *otg)
  151. {
  152. struct twl6030_usb *twl = phy_to_twl(otg->phy);
  153. twl6030_writeb(twl, TWL_MODULE_USB, 0x24, USB_VBUS_CTRL_SET);
  154. twl6030_writeb(twl, TWL_MODULE_USB, 0x84, USB_VBUS_CTRL_SET);
  155. mdelay(100);
  156. twl6030_writeb(twl, TWL_MODULE_USB, 0xa0, USB_VBUS_CTRL_CLR);
  157. return 0;
  158. }
  159. static int twl6030_usb_ldo_init(struct twl6030_usb *twl)
  160. {
  161. char *regulator_name;
  162. if (twl->features & TWL6025_SUBCLASS)
  163. regulator_name = "ldousb";
  164. else
  165. regulator_name = "vusb";
  166. /* Set to OTG_REV 1.3 and turn on the ID_WAKEUP_COMP */
  167. twl6030_writeb(twl, TWL6030_MODULE_ID0 , 0x1, TWL6030_BACKUP_REG);
  168. /* Program CFG_LDO_PD2 register and set VUSB bit */
  169. twl6030_writeb(twl, TWL6030_MODULE_ID0 , 0x1, TWL6030_CFG_LDO_PD2);
  170. /* Program MISC2 register and set bit VUSB_IN_VBAT */
  171. twl6030_writeb(twl, TWL6030_MODULE_ID0 , 0x10, TWL6030_MISC2);
  172. twl->usb3v3 = regulator_get(twl->dev, regulator_name);
  173. if (IS_ERR(twl->usb3v3))
  174. return -ENODEV;
  175. /* Program the USB_VBUS_CTRL_SET and set VBUS_ACT_COMP bit */
  176. twl6030_writeb(twl, TWL_MODULE_USB, 0x4, USB_VBUS_CTRL_SET);
  177. /*
  178. * Program the USB_ID_CTRL_SET register to enable GND drive
  179. * and the ID comparators
  180. */
  181. twl6030_writeb(twl, TWL_MODULE_USB, 0x14, USB_ID_CTRL_SET);
  182. return 0;
  183. }
  184. static ssize_t twl6030_usb_vbus_show(struct device *dev,
  185. struct device_attribute *attr, char *buf)
  186. {
  187. struct twl6030_usb *twl = dev_get_drvdata(dev);
  188. unsigned long flags;
  189. int ret = -EINVAL;
  190. spin_lock_irqsave(&twl->lock, flags);
  191. switch (twl->linkstat) {
  192. case USB_EVENT_VBUS:
  193. ret = snprintf(buf, PAGE_SIZE, "vbus\n");
  194. break;
  195. case USB_EVENT_ID:
  196. ret = snprintf(buf, PAGE_SIZE, "id\n");
  197. break;
  198. case USB_EVENT_NONE:
  199. ret = snprintf(buf, PAGE_SIZE, "none\n");
  200. break;
  201. default:
  202. ret = snprintf(buf, PAGE_SIZE, "UNKNOWN\n");
  203. }
  204. spin_unlock_irqrestore(&twl->lock, flags);
  205. return ret;
  206. }
  207. static DEVICE_ATTR(vbus, 0444, twl6030_usb_vbus_show, NULL);
  208. static irqreturn_t twl6030_usb_irq(int irq, void *_twl)
  209. {
  210. struct twl6030_usb *twl = _twl;
  211. struct usb_otg *otg = twl->phy.otg;
  212. int status;
  213. u8 vbus_state, hw_state;
  214. hw_state = twl6030_readb(twl, TWL6030_MODULE_ID0, STS_HW_CONDITIONS);
  215. vbus_state = twl6030_readb(twl, TWL_MODULE_MAIN_CHARGE,
  216. CONTROLLER_STAT1);
  217. if (!(hw_state & STS_USB_ID)) {
  218. if (vbus_state & VBUS_DET) {
  219. regulator_enable(twl->usb3v3);
  220. twl->asleep = 1;
  221. status = USB_EVENT_VBUS;
  222. otg->default_a = false;
  223. twl->phy.state = OTG_STATE_B_IDLE;
  224. twl->linkstat = status;
  225. twl->phy.last_event = status;
  226. atomic_notifier_call_chain(&twl->phy.notifier,
  227. status, otg->gadget);
  228. } else {
  229. status = USB_EVENT_NONE;
  230. twl->linkstat = status;
  231. twl->phy.last_event = status;
  232. atomic_notifier_call_chain(&twl->phy.notifier,
  233. status, otg->gadget);
  234. if (twl->asleep) {
  235. regulator_disable(twl->usb3v3);
  236. twl->asleep = 0;
  237. }
  238. }
  239. }
  240. sysfs_notify(&twl->dev->kobj, NULL, "vbus");
  241. return IRQ_HANDLED;
  242. }
  243. static irqreturn_t twl6030_usbotg_irq(int irq, void *_twl)
  244. {
  245. struct twl6030_usb *twl = _twl;
  246. struct usb_otg *otg = twl->phy.otg;
  247. int status = USB_EVENT_NONE;
  248. u8 hw_state;
  249. hw_state = twl6030_readb(twl, TWL6030_MODULE_ID0, STS_HW_CONDITIONS);
  250. if (hw_state & STS_USB_ID) {
  251. regulator_enable(twl->usb3v3);
  252. twl->asleep = 1;
  253. twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_CLR, 0x1);
  254. twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_SET,
  255. 0x10);
  256. status = USB_EVENT_ID;
  257. otg->default_a = true;
  258. twl->phy.state = OTG_STATE_A_IDLE;
  259. twl->linkstat = status;
  260. twl->phy.last_event = status;
  261. atomic_notifier_call_chain(&twl->phy.notifier, status,
  262. otg->gadget);
  263. } else {
  264. twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_CLR,
  265. 0x10);
  266. twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_SET,
  267. 0x1);
  268. }
  269. twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_LATCH_CLR, status);
  270. return IRQ_HANDLED;
  271. }
  272. static int twl6030_set_peripheral(struct usb_otg *otg,
  273. struct usb_gadget *gadget)
  274. {
  275. if (!otg)
  276. return -ENODEV;
  277. otg->gadget = gadget;
  278. if (!gadget)
  279. otg->phy->state = OTG_STATE_UNDEFINED;
  280. return 0;
  281. }
  282. static int twl6030_enable_irq(struct usb_phy *x)
  283. {
  284. struct twl6030_usb *twl = phy_to_twl(x);
  285. twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_SET, 0x1);
  286. twl6030_interrupt_unmask(0x05, REG_INT_MSK_LINE_C);
  287. twl6030_interrupt_unmask(0x05, REG_INT_MSK_STS_C);
  288. twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK,
  289. REG_INT_MSK_LINE_C);
  290. twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK,
  291. REG_INT_MSK_STS_C);
  292. twl6030_usb_irq(twl->irq2, twl);
  293. twl6030_usbotg_irq(twl->irq1, twl);
  294. return 0;
  295. }
  296. static void otg_set_vbus_work(struct work_struct *data)
  297. {
  298. struct twl6030_usb *twl = container_of(data, struct twl6030_usb,
  299. set_vbus_work);
  300. /*
  301. * Start driving VBUS. Set OPA_MODE bit in CHARGERUSB_CTRL1
  302. * register. This enables boost mode.
  303. */
  304. if (twl->vbus_enable)
  305. twl6030_writeb(twl, TWL_MODULE_MAIN_CHARGE , 0x40,
  306. CHARGERUSB_CTRL1);
  307. else
  308. twl6030_writeb(twl, TWL_MODULE_MAIN_CHARGE , 0x00,
  309. CHARGERUSB_CTRL1);
  310. }
  311. static int twl6030_set_vbus(struct usb_otg *otg, bool enabled)
  312. {
  313. struct twl6030_usb *twl = phy_to_twl(otg->phy);
  314. twl->vbus_enable = enabled;
  315. schedule_work(&twl->set_vbus_work);
  316. return 0;
  317. }
  318. static int twl6030_set_host(struct usb_otg *otg, struct usb_bus *host)
  319. {
  320. if (!otg)
  321. return -ENODEV;
  322. otg->host = host;
  323. if (!host)
  324. otg->phy->state = OTG_STATE_UNDEFINED;
  325. return 0;
  326. }
  327. static int __devinit twl6030_usb_probe(struct platform_device *pdev)
  328. {
  329. struct twl6030_usb *twl;
  330. int status, err;
  331. struct twl4030_usb_data *pdata;
  332. struct usb_otg *otg;
  333. struct device *dev = &pdev->dev;
  334. pdata = dev->platform_data;
  335. twl = kzalloc(sizeof *twl, GFP_KERNEL);
  336. if (!twl)
  337. return -ENOMEM;
  338. otg = kzalloc(sizeof *otg, GFP_KERNEL);
  339. if (!otg) {
  340. kfree(twl);
  341. return -ENOMEM;
  342. }
  343. twl->dev = &pdev->dev;
  344. twl->irq1 = platform_get_irq(pdev, 0);
  345. twl->irq2 = platform_get_irq(pdev, 1);
  346. twl->features = pdata->features;
  347. twl->phy.dev = twl->dev;
  348. twl->phy.label = "twl6030";
  349. twl->phy.otg = otg;
  350. twl->phy.init = twl6030_phy_init;
  351. twl->phy.shutdown = twl6030_phy_shutdown;
  352. twl->phy.set_suspend = twl6030_phy_suspend;
  353. otg->phy = &twl->phy;
  354. otg->set_host = twl6030_set_host;
  355. otg->set_peripheral = twl6030_set_peripheral;
  356. otg->set_vbus = twl6030_set_vbus;
  357. otg->start_srp = twl6030_start_srp;
  358. /* init spinlock for workqueue */
  359. spin_lock_init(&twl->lock);
  360. err = twl6030_usb_ldo_init(twl);
  361. if (err) {
  362. dev_err(&pdev->dev, "ldo init failed\n");
  363. kfree(otg);
  364. kfree(twl);
  365. return err;
  366. }
  367. usb_set_transceiver(&twl->phy);
  368. platform_set_drvdata(pdev, twl);
  369. if (device_create_file(&pdev->dev, &dev_attr_vbus))
  370. dev_warn(&pdev->dev, "could not create sysfs file\n");
  371. ATOMIC_INIT_NOTIFIER_HEAD(&twl->phy.notifier);
  372. INIT_WORK(&twl->set_vbus_work, otg_set_vbus_work);
  373. twl->irq_enabled = true;
  374. status = request_threaded_irq(twl->irq1, NULL, twl6030_usbotg_irq,
  375. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  376. "twl6030_usb", twl);
  377. if (status < 0) {
  378. dev_err(&pdev->dev, "can't get IRQ %d, err %d\n",
  379. twl->irq1, status);
  380. device_remove_file(twl->dev, &dev_attr_vbus);
  381. kfree(otg);
  382. kfree(twl);
  383. return status;
  384. }
  385. status = request_threaded_irq(twl->irq2, NULL, twl6030_usb_irq,
  386. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  387. "twl6030_usb", twl);
  388. if (status < 0) {
  389. dev_err(&pdev->dev, "can't get IRQ %d, err %d\n",
  390. twl->irq2, status);
  391. free_irq(twl->irq1, twl);
  392. device_remove_file(twl->dev, &dev_attr_vbus);
  393. kfree(otg);
  394. kfree(twl);
  395. return status;
  396. }
  397. twl->asleep = 0;
  398. pdata->phy_init(dev);
  399. twl6030_phy_suspend(&twl->phy, 0);
  400. twl6030_enable_irq(&twl->phy);
  401. dev_info(&pdev->dev, "Initialized TWL6030 USB module\n");
  402. return 0;
  403. }
  404. static int __exit twl6030_usb_remove(struct platform_device *pdev)
  405. {
  406. struct twl6030_usb *twl = platform_get_drvdata(pdev);
  407. struct twl4030_usb_data *pdata;
  408. struct device *dev = &pdev->dev;
  409. pdata = dev->platform_data;
  410. twl6030_interrupt_mask(TWL6030_USBOTG_INT_MASK,
  411. REG_INT_MSK_LINE_C);
  412. twl6030_interrupt_mask(TWL6030_USBOTG_INT_MASK,
  413. REG_INT_MSK_STS_C);
  414. free_irq(twl->irq1, twl);
  415. free_irq(twl->irq2, twl);
  416. regulator_put(twl->usb3v3);
  417. pdata->phy_exit(twl->dev);
  418. device_remove_file(twl->dev, &dev_attr_vbus);
  419. cancel_work_sync(&twl->set_vbus_work);
  420. kfree(twl->phy.otg);
  421. kfree(twl);
  422. return 0;
  423. }
  424. static struct platform_driver twl6030_usb_driver = {
  425. .probe = twl6030_usb_probe,
  426. .remove = __exit_p(twl6030_usb_remove),
  427. .driver = {
  428. .name = "twl6030_usb",
  429. .owner = THIS_MODULE,
  430. },
  431. };
  432. static int __init twl6030_usb_init(void)
  433. {
  434. return platform_driver_register(&twl6030_usb_driver);
  435. }
  436. subsys_initcall(twl6030_usb_init);
  437. static void __exit twl6030_usb_exit(void)
  438. {
  439. platform_driver_unregister(&twl6030_usb_driver);
  440. }
  441. module_exit(twl6030_usb_exit);
  442. MODULE_ALIAS("platform:twl6030_usb");
  443. MODULE_AUTHOR("Hema HK <hemahk@ti.com>");
  444. MODULE_DESCRIPTION("TWL6030 USB transceiver driver");
  445. MODULE_LICENSE("GPL");