twl6030-usb.c 12 KB

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