twl6030-usb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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 otg_transceiver otg;
  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 xceiv_to_twl(x) container_of((x), struct twl6030_usb, otg)
  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. /*-------------------------------------------------------------------------*/
  119. static int twl6030_set_phy_clk(struct otg_transceiver *x, int on)
  120. {
  121. struct twl6030_usb *twl;
  122. struct device *dev;
  123. struct twl4030_usb_data *pdata;
  124. twl = xceiv_to_twl(x);
  125. dev = twl->dev;
  126. pdata = dev->platform_data;
  127. pdata->phy_set_clock(twl->dev, on);
  128. return 0;
  129. }
  130. static int twl6030_phy_init(struct otg_transceiver *x)
  131. {
  132. struct twl6030_usb *twl;
  133. struct device *dev;
  134. struct twl4030_usb_data *pdata;
  135. twl = xceiv_to_twl(x);
  136. dev = twl->dev;
  137. pdata = dev->platform_data;
  138. if (twl->linkstat == USB_EVENT_ID)
  139. pdata->phy_power(twl->dev, 1, 1);
  140. else
  141. pdata->phy_power(twl->dev, 0, 1);
  142. return 0;
  143. }
  144. static void twl6030_phy_shutdown(struct otg_transceiver *x)
  145. {
  146. struct twl6030_usb *twl;
  147. struct device *dev;
  148. struct twl4030_usb_data *pdata;
  149. twl = xceiv_to_twl(x);
  150. dev = twl->dev;
  151. pdata = dev->platform_data;
  152. pdata->phy_power(twl->dev, 0, 0);
  153. }
  154. static int twl6030_phy_suspend(struct otg_transceiver *x, int suspend)
  155. {
  156. struct twl6030_usb *twl = xceiv_to_twl(x);
  157. struct device *dev = twl->dev;
  158. struct twl4030_usb_data *pdata = dev->platform_data;
  159. pdata->phy_suspend(dev, suspend);
  160. return 0;
  161. }
  162. static int twl6030_start_srp(struct otg_transceiver *x)
  163. {
  164. struct twl6030_usb *twl = xceiv_to_twl(x);
  165. twl6030_writeb(twl, TWL_MODULE_USB, 0x24, USB_VBUS_CTRL_SET);
  166. twl6030_writeb(twl, TWL_MODULE_USB, 0x84, USB_VBUS_CTRL_SET);
  167. mdelay(100);
  168. twl6030_writeb(twl, TWL_MODULE_USB, 0xa0, USB_VBUS_CTRL_CLR);
  169. return 0;
  170. }
  171. static int twl6030_usb_ldo_init(struct twl6030_usb *twl)
  172. {
  173. char *regulator_name;
  174. if (twl->features & TWL6025_SUBCLASS)
  175. regulator_name = "ldousb";
  176. else
  177. regulator_name = "vusb";
  178. /* Set to OTG_REV 1.3 and turn on the ID_WAKEUP_COMP */
  179. twl6030_writeb(twl, TWL6030_MODULE_ID0 , 0x1, TWL6030_BACKUP_REG);
  180. /* Program CFG_LDO_PD2 register and set VUSB bit */
  181. twl6030_writeb(twl, TWL6030_MODULE_ID0 , 0x1, TWL6030_CFG_LDO_PD2);
  182. /* Program MISC2 register and set bit VUSB_IN_VBAT */
  183. twl6030_writeb(twl, TWL6030_MODULE_ID0 , 0x10, TWL6030_MISC2);
  184. twl->usb3v3 = regulator_get(twl->dev, regulator_name);
  185. if (IS_ERR(twl->usb3v3))
  186. return -ENODEV;
  187. /* Program the USB_VBUS_CTRL_SET and set VBUS_ACT_COMP bit */
  188. twl6030_writeb(twl, TWL_MODULE_USB, 0x4, USB_VBUS_CTRL_SET);
  189. /*
  190. * Program the USB_ID_CTRL_SET register to enable GND drive
  191. * and the ID comparators
  192. */
  193. twl6030_writeb(twl, TWL_MODULE_USB, 0x14, USB_ID_CTRL_SET);
  194. return 0;
  195. }
  196. static ssize_t twl6030_usb_vbus_show(struct device *dev,
  197. struct device_attribute *attr, char *buf)
  198. {
  199. struct twl6030_usb *twl = dev_get_drvdata(dev);
  200. unsigned long flags;
  201. int ret = -EINVAL;
  202. spin_lock_irqsave(&twl->lock, flags);
  203. switch (twl->linkstat) {
  204. case USB_EVENT_VBUS:
  205. ret = snprintf(buf, PAGE_SIZE, "vbus\n");
  206. break;
  207. case USB_EVENT_ID:
  208. ret = snprintf(buf, PAGE_SIZE, "id\n");
  209. break;
  210. case USB_EVENT_NONE:
  211. ret = snprintf(buf, PAGE_SIZE, "none\n");
  212. break;
  213. default:
  214. ret = snprintf(buf, PAGE_SIZE, "UNKNOWN\n");
  215. }
  216. spin_unlock_irqrestore(&twl->lock, flags);
  217. return ret;
  218. }
  219. static DEVICE_ATTR(vbus, 0444, twl6030_usb_vbus_show, NULL);
  220. static irqreturn_t twl6030_usb_irq(int irq, void *_twl)
  221. {
  222. struct twl6030_usb *twl = _twl;
  223. int status;
  224. u8 vbus_state, hw_state;
  225. hw_state = twl6030_readb(twl, TWL6030_MODULE_ID0, STS_HW_CONDITIONS);
  226. vbus_state = twl6030_readb(twl, TWL_MODULE_MAIN_CHARGE,
  227. CONTROLLER_STAT1);
  228. if (!(hw_state & STS_USB_ID)) {
  229. if (vbus_state & VBUS_DET) {
  230. regulator_enable(twl->usb3v3);
  231. twl->asleep = 1;
  232. status = USB_EVENT_VBUS;
  233. twl->otg.default_a = false;
  234. twl->otg.state = OTG_STATE_B_IDLE;
  235. twl->linkstat = status;
  236. twl->otg.last_event = status;
  237. atomic_notifier_call_chain(&twl->otg.notifier,
  238. status, twl->otg.gadget);
  239. } else {
  240. status = USB_EVENT_NONE;
  241. twl->linkstat = status;
  242. twl->otg.last_event = status;
  243. atomic_notifier_call_chain(&twl->otg.notifier,
  244. status, twl->otg.gadget);
  245. if (twl->asleep) {
  246. regulator_disable(twl->usb3v3);
  247. twl->asleep = 0;
  248. }
  249. }
  250. }
  251. sysfs_notify(&twl->dev->kobj, NULL, "vbus");
  252. return IRQ_HANDLED;
  253. }
  254. static irqreturn_t twl6030_usbotg_irq(int irq, void *_twl)
  255. {
  256. struct twl6030_usb *twl = _twl;
  257. int status = USB_EVENT_NONE;
  258. u8 hw_state;
  259. hw_state = twl6030_readb(twl, TWL6030_MODULE_ID0, STS_HW_CONDITIONS);
  260. if (hw_state & STS_USB_ID) {
  261. regulator_enable(twl->usb3v3);
  262. twl->asleep = 1;
  263. twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_CLR, 0x1);
  264. twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_SET,
  265. 0x10);
  266. status = USB_EVENT_ID;
  267. twl->otg.default_a = true;
  268. twl->otg.state = OTG_STATE_A_IDLE;
  269. twl->linkstat = status;
  270. twl->otg.last_event = status;
  271. atomic_notifier_call_chain(&twl->otg.notifier, status,
  272. twl->otg.gadget);
  273. } else {
  274. twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_CLR,
  275. 0x10);
  276. twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_SET,
  277. 0x1);
  278. }
  279. twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_LATCH_CLR, status);
  280. return IRQ_HANDLED;
  281. }
  282. static int twl6030_set_peripheral(struct otg_transceiver *x,
  283. struct usb_gadget *gadget)
  284. {
  285. struct twl6030_usb *twl;
  286. if (!x)
  287. return -ENODEV;
  288. twl = xceiv_to_twl(x);
  289. twl->otg.gadget = gadget;
  290. if (!gadget)
  291. twl->otg.state = OTG_STATE_UNDEFINED;
  292. return 0;
  293. }
  294. static int twl6030_enable_irq(struct otg_transceiver *x)
  295. {
  296. struct twl6030_usb *twl = xceiv_to_twl(x);
  297. twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_SET, 0x1);
  298. twl6030_interrupt_unmask(0x05, REG_INT_MSK_LINE_C);
  299. twl6030_interrupt_unmask(0x05, REG_INT_MSK_STS_C);
  300. twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK,
  301. REG_INT_MSK_LINE_C);
  302. twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK,
  303. REG_INT_MSK_STS_C);
  304. twl6030_usb_irq(twl->irq2, twl);
  305. twl6030_usbotg_irq(twl->irq1, twl);
  306. return 0;
  307. }
  308. static void otg_set_vbus_work(struct work_struct *data)
  309. {
  310. struct twl6030_usb *twl = container_of(data, struct twl6030_usb,
  311. set_vbus_work);
  312. /*
  313. * Start driving VBUS. Set OPA_MODE bit in CHARGERUSB_CTRL1
  314. * register. This enables boost mode.
  315. */
  316. if (twl->vbus_enable)
  317. twl6030_writeb(twl, TWL_MODULE_MAIN_CHARGE , 0x40,
  318. CHARGERUSB_CTRL1);
  319. else
  320. twl6030_writeb(twl, TWL_MODULE_MAIN_CHARGE , 0x00,
  321. CHARGERUSB_CTRL1);
  322. }
  323. static int twl6030_set_vbus(struct otg_transceiver *x, bool enabled)
  324. {
  325. struct twl6030_usb *twl = xceiv_to_twl(x);
  326. twl->vbus_enable = enabled;
  327. schedule_work(&twl->set_vbus_work);
  328. return 0;
  329. }
  330. static int twl6030_set_host(struct otg_transceiver *x, struct usb_bus *host)
  331. {
  332. struct twl6030_usb *twl;
  333. if (!x)
  334. return -ENODEV;
  335. twl = xceiv_to_twl(x);
  336. twl->otg.host = host;
  337. if (!host)
  338. twl->otg.state = OTG_STATE_UNDEFINED;
  339. return 0;
  340. }
  341. static int __devinit twl6030_usb_probe(struct platform_device *pdev)
  342. {
  343. struct twl6030_usb *twl;
  344. int status, err;
  345. struct twl4030_usb_data *pdata;
  346. struct device *dev = &pdev->dev;
  347. pdata = dev->platform_data;
  348. twl = kzalloc(sizeof *twl, GFP_KERNEL);
  349. if (!twl)
  350. return -ENOMEM;
  351. twl->dev = &pdev->dev;
  352. twl->irq1 = platform_get_irq(pdev, 0);
  353. twl->irq2 = platform_get_irq(pdev, 1);
  354. twl->features = pdata->features;
  355. twl->otg.dev = twl->dev;
  356. twl->otg.label = "twl6030";
  357. twl->otg.set_host = twl6030_set_host;
  358. twl->otg.set_peripheral = twl6030_set_peripheral;
  359. twl->otg.set_vbus = twl6030_set_vbus;
  360. twl->otg.init = twl6030_phy_init;
  361. twl->otg.shutdown = twl6030_phy_shutdown;
  362. twl->otg.set_suspend = twl6030_phy_suspend;
  363. twl->otg.start_srp = twl6030_start_srp;
  364. /* init spinlock for workqueue */
  365. spin_lock_init(&twl->lock);
  366. err = twl6030_usb_ldo_init(twl);
  367. if (err) {
  368. dev_err(&pdev->dev, "ldo init failed\n");
  369. kfree(twl);
  370. return err;
  371. }
  372. otg_set_transceiver(&twl->otg);
  373. platform_set_drvdata(pdev, twl);
  374. if (device_create_file(&pdev->dev, &dev_attr_vbus))
  375. dev_warn(&pdev->dev, "could not create sysfs file\n");
  376. ATOMIC_INIT_NOTIFIER_HEAD(&twl->otg.notifier);
  377. INIT_WORK(&twl->set_vbus_work, otg_set_vbus_work);
  378. twl->irq_enabled = true;
  379. status = request_threaded_irq(twl->irq1, NULL, twl6030_usbotg_irq,
  380. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  381. "twl6030_usb", twl);
  382. if (status < 0) {
  383. dev_err(&pdev->dev, "can't get IRQ %d, err %d\n",
  384. twl->irq1, status);
  385. device_remove_file(twl->dev, &dev_attr_vbus);
  386. kfree(twl);
  387. return status;
  388. }
  389. status = request_threaded_irq(twl->irq2, NULL, twl6030_usb_irq,
  390. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  391. "twl6030_usb", twl);
  392. if (status < 0) {
  393. dev_err(&pdev->dev, "can't get IRQ %d, err %d\n",
  394. twl->irq2, status);
  395. free_irq(twl->irq1, twl);
  396. device_remove_file(twl->dev, &dev_attr_vbus);
  397. kfree(twl);
  398. return status;
  399. }
  400. twl->asleep = 0;
  401. pdata->phy_init(dev);
  402. twl6030_phy_suspend(&twl->otg, 0);
  403. twl6030_enable_irq(&twl->otg);
  404. dev_info(&pdev->dev, "Initialized TWL6030 USB module\n");
  405. return 0;
  406. }
  407. static int __exit twl6030_usb_remove(struct platform_device *pdev)
  408. {
  409. struct twl6030_usb *twl = platform_get_drvdata(pdev);
  410. struct twl4030_usb_data *pdata;
  411. struct device *dev = &pdev->dev;
  412. pdata = dev->platform_data;
  413. twl6030_interrupt_mask(TWL6030_USBOTG_INT_MASK,
  414. REG_INT_MSK_LINE_C);
  415. twl6030_interrupt_mask(TWL6030_USBOTG_INT_MASK,
  416. REG_INT_MSK_STS_C);
  417. free_irq(twl->irq1, twl);
  418. free_irq(twl->irq2, twl);
  419. regulator_put(twl->usb3v3);
  420. pdata->phy_exit(twl->dev);
  421. device_remove_file(twl->dev, &dev_attr_vbus);
  422. cancel_work_sync(&twl->set_vbus_work);
  423. kfree(twl);
  424. return 0;
  425. }
  426. static struct platform_driver twl6030_usb_driver = {
  427. .probe = twl6030_usb_probe,
  428. .remove = __exit_p(twl6030_usb_remove),
  429. .driver = {
  430. .name = "twl6030_usb",
  431. .owner = THIS_MODULE,
  432. },
  433. };
  434. static int __init twl6030_usb_init(void)
  435. {
  436. return platform_driver_register(&twl6030_usb_driver);
  437. }
  438. subsys_initcall(twl6030_usb_init);
  439. static void __exit twl6030_usb_exit(void)
  440. {
  441. platform_driver_unregister(&twl6030_usb_driver);
  442. }
  443. module_exit(twl6030_usb_exit);
  444. MODULE_ALIAS("platform:twl6030_usb");
  445. MODULE_AUTHOR("Hema HK <hemahk@ti.com>");
  446. MODULE_DESCRIPTION("TWL6030 USB transceiver driver");
  447. MODULE_LICENSE("GPL");