twl4030-usb.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. /*
  2. * twl4030_usb - TWL4030 USB transceiver, talking to OMAP OTG controller
  3. *
  4. * Copyright (C) 2004-2007 Texas Instruments
  5. * Copyright (C) 2008 Nokia Corporation
  6. * Contact: Felipe Balbi <felipe.balbi@nokia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. *
  22. * Current status:
  23. * - HS USB ULPI mode works.
  24. * - 3-pin mode support may be added in future.
  25. */
  26. #include <linux/module.h>
  27. #include <linux/init.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/workqueue.h>
  32. #include <linux/io.h>
  33. #include <linux/delay.h>
  34. #include <linux/usb/otg.h>
  35. #include <linux/i2c/twl4030.h>
  36. /* Register defines */
  37. #define VENDOR_ID_LO 0x00
  38. #define VENDOR_ID_HI 0x01
  39. #define PRODUCT_ID_LO 0x02
  40. #define PRODUCT_ID_HI 0x03
  41. #define FUNC_CTRL 0x04
  42. #define FUNC_CTRL_SET 0x05
  43. #define FUNC_CTRL_CLR 0x06
  44. #define FUNC_CTRL_SUSPENDM (1 << 6)
  45. #define FUNC_CTRL_RESET (1 << 5)
  46. #define FUNC_CTRL_OPMODE_MASK (3 << 3) /* bits 3 and 4 */
  47. #define FUNC_CTRL_OPMODE_NORMAL (0 << 3)
  48. #define FUNC_CTRL_OPMODE_NONDRIVING (1 << 3)
  49. #define FUNC_CTRL_OPMODE_DISABLE_BIT_NRZI (2 << 3)
  50. #define FUNC_CTRL_TERMSELECT (1 << 2)
  51. #define FUNC_CTRL_XCVRSELECT_MASK (3 << 0) /* bits 0 and 1 */
  52. #define FUNC_CTRL_XCVRSELECT_HS (0 << 0)
  53. #define FUNC_CTRL_XCVRSELECT_FS (1 << 0)
  54. #define FUNC_CTRL_XCVRSELECT_LS (2 << 0)
  55. #define FUNC_CTRL_XCVRSELECT_FS4LS (3 << 0)
  56. #define IFC_CTRL 0x07
  57. #define IFC_CTRL_SET 0x08
  58. #define IFC_CTRL_CLR 0x09
  59. #define IFC_CTRL_INTERFACE_PROTECT_DISABLE (1 << 7)
  60. #define IFC_CTRL_AUTORESUME (1 << 4)
  61. #define IFC_CTRL_CLOCKSUSPENDM (1 << 3)
  62. #define IFC_CTRL_CARKITMODE (1 << 2)
  63. #define IFC_CTRL_FSLSSERIALMODE_3PIN (1 << 1)
  64. #define TWL4030_OTG_CTRL 0x0A
  65. #define TWL4030_OTG_CTRL_SET 0x0B
  66. #define TWL4030_OTG_CTRL_CLR 0x0C
  67. #define TWL4030_OTG_CTRL_DRVVBUS (1 << 5)
  68. #define TWL4030_OTG_CTRL_CHRGVBUS (1 << 4)
  69. #define TWL4030_OTG_CTRL_DISCHRGVBUS (1 << 3)
  70. #define TWL4030_OTG_CTRL_DMPULLDOWN (1 << 2)
  71. #define TWL4030_OTG_CTRL_DPPULLDOWN (1 << 1)
  72. #define TWL4030_OTG_CTRL_IDPULLUP (1 << 0)
  73. #define USB_INT_EN_RISE 0x0D
  74. #define USB_INT_EN_RISE_SET 0x0E
  75. #define USB_INT_EN_RISE_CLR 0x0F
  76. #define USB_INT_EN_FALL 0x10
  77. #define USB_INT_EN_FALL_SET 0x11
  78. #define USB_INT_EN_FALL_CLR 0x12
  79. #define USB_INT_STS 0x13
  80. #define USB_INT_LATCH 0x14
  81. #define USB_INT_IDGND (1 << 4)
  82. #define USB_INT_SESSEND (1 << 3)
  83. #define USB_INT_SESSVALID (1 << 2)
  84. #define USB_INT_VBUSVALID (1 << 1)
  85. #define USB_INT_HOSTDISCONNECT (1 << 0)
  86. #define CARKIT_CTRL 0x19
  87. #define CARKIT_CTRL_SET 0x1A
  88. #define CARKIT_CTRL_CLR 0x1B
  89. #define CARKIT_CTRL_MICEN (1 << 6)
  90. #define CARKIT_CTRL_SPKRIGHTEN (1 << 5)
  91. #define CARKIT_CTRL_SPKLEFTEN (1 << 4)
  92. #define CARKIT_CTRL_RXDEN (1 << 3)
  93. #define CARKIT_CTRL_TXDEN (1 << 2)
  94. #define CARKIT_CTRL_IDGNDDRV (1 << 1)
  95. #define CARKIT_CTRL_CARKITPWR (1 << 0)
  96. #define CARKIT_PLS_CTRL 0x22
  97. #define CARKIT_PLS_CTRL_SET 0x23
  98. #define CARKIT_PLS_CTRL_CLR 0x24
  99. #define CARKIT_PLS_CTRL_SPKRRIGHT_BIASEN (1 << 3)
  100. #define CARKIT_PLS_CTRL_SPKRLEFT_BIASEN (1 << 2)
  101. #define CARKIT_PLS_CTRL_RXPLSEN (1 << 1)
  102. #define CARKIT_PLS_CTRL_TXPLSEN (1 << 0)
  103. #define MCPC_CTRL 0x30
  104. #define MCPC_CTRL_SET 0x31
  105. #define MCPC_CTRL_CLR 0x32
  106. #define MCPC_CTRL_RTSOL (1 << 7)
  107. #define MCPC_CTRL_EXTSWR (1 << 6)
  108. #define MCPC_CTRL_EXTSWC (1 << 5)
  109. #define MCPC_CTRL_VOICESW (1 << 4)
  110. #define MCPC_CTRL_OUT64K (1 << 3)
  111. #define MCPC_CTRL_RTSCTSSW (1 << 2)
  112. #define MCPC_CTRL_HS_UART (1 << 0)
  113. #define MCPC_IO_CTRL 0x33
  114. #define MCPC_IO_CTRL_SET 0x34
  115. #define MCPC_IO_CTRL_CLR 0x35
  116. #define MCPC_IO_CTRL_MICBIASEN (1 << 5)
  117. #define MCPC_IO_CTRL_CTS_NPU (1 << 4)
  118. #define MCPC_IO_CTRL_RXD_PU (1 << 3)
  119. #define MCPC_IO_CTRL_TXDTYP (1 << 2)
  120. #define MCPC_IO_CTRL_CTSTYP (1 << 1)
  121. #define MCPC_IO_CTRL_RTSTYP (1 << 0)
  122. #define MCPC_CTRL2 0x36
  123. #define MCPC_CTRL2_SET 0x37
  124. #define MCPC_CTRL2_CLR 0x38
  125. #define MCPC_CTRL2_MCPC_CK_EN (1 << 0)
  126. #define OTHER_FUNC_CTRL 0x80
  127. #define OTHER_FUNC_CTRL_SET 0x81
  128. #define OTHER_FUNC_CTRL_CLR 0x82
  129. #define OTHER_FUNC_CTRL_BDIS_ACON_EN (1 << 4)
  130. #define OTHER_FUNC_CTRL_FIVEWIRE_MODE (1 << 2)
  131. #define OTHER_IFC_CTRL 0x83
  132. #define OTHER_IFC_CTRL_SET 0x84
  133. #define OTHER_IFC_CTRL_CLR 0x85
  134. #define OTHER_IFC_CTRL_OE_INT_EN (1 << 6)
  135. #define OTHER_IFC_CTRL_CEA2011_MODE (1 << 5)
  136. #define OTHER_IFC_CTRL_FSLSSERIALMODE_4PIN (1 << 4)
  137. #define OTHER_IFC_CTRL_HIZ_ULPI_60MHZ_OUT (1 << 3)
  138. #define OTHER_IFC_CTRL_HIZ_ULPI (1 << 2)
  139. #define OTHER_IFC_CTRL_ALT_INT_REROUTE (1 << 0)
  140. #define OTHER_INT_EN_RISE 0x86
  141. #define OTHER_INT_EN_RISE_SET 0x87
  142. #define OTHER_INT_EN_RISE_CLR 0x88
  143. #define OTHER_INT_EN_FALL 0x89
  144. #define OTHER_INT_EN_FALL_SET 0x8A
  145. #define OTHER_INT_EN_FALL_CLR 0x8B
  146. #define OTHER_INT_STS 0x8C
  147. #define OTHER_INT_LATCH 0x8D
  148. #define OTHER_INT_VB_SESS_VLD (1 << 7)
  149. #define OTHER_INT_DM_HI (1 << 6) /* not valid for "latch" reg */
  150. #define OTHER_INT_DP_HI (1 << 5) /* not valid for "latch" reg */
  151. #define OTHER_INT_BDIS_ACON (1 << 3) /* not valid for "fall" regs */
  152. #define OTHER_INT_MANU (1 << 1)
  153. #define OTHER_INT_ABNORMAL_STRESS (1 << 0)
  154. #define ID_STATUS 0x96
  155. #define ID_RES_FLOAT (1 << 4)
  156. #define ID_RES_440K (1 << 3)
  157. #define ID_RES_200K (1 << 2)
  158. #define ID_RES_102K (1 << 1)
  159. #define ID_RES_GND (1 << 0)
  160. #define POWER_CTRL 0xAC
  161. #define POWER_CTRL_SET 0xAD
  162. #define POWER_CTRL_CLR 0xAE
  163. #define POWER_CTRL_OTG_ENAB (1 << 5)
  164. #define OTHER_IFC_CTRL2 0xAF
  165. #define OTHER_IFC_CTRL2_SET 0xB0
  166. #define OTHER_IFC_CTRL2_CLR 0xB1
  167. #define OTHER_IFC_CTRL2_ULPI_STP_LOW (1 << 4)
  168. #define OTHER_IFC_CTRL2_ULPI_TXEN_POL (1 << 3)
  169. #define OTHER_IFC_CTRL2_ULPI_4PIN_2430 (1 << 2)
  170. #define OTHER_IFC_CTRL2_USB_INT_OUTSEL_MASK (3 << 0) /* bits 0 and 1 */
  171. #define OTHER_IFC_CTRL2_USB_INT_OUTSEL_INT1N (0 << 0)
  172. #define OTHER_IFC_CTRL2_USB_INT_OUTSEL_INT2N (1 << 0)
  173. #define REG_CTRL_EN 0xB2
  174. #define REG_CTRL_EN_SET 0xB3
  175. #define REG_CTRL_EN_CLR 0xB4
  176. #define REG_CTRL_ERROR 0xB5
  177. #define ULPI_I2C_CONFLICT_INTEN (1 << 0)
  178. #define OTHER_FUNC_CTRL2 0xB8
  179. #define OTHER_FUNC_CTRL2_SET 0xB9
  180. #define OTHER_FUNC_CTRL2_CLR 0xBA
  181. #define OTHER_FUNC_CTRL2_VBAT_TIMER_EN (1 << 0)
  182. /* following registers do not have separate _clr and _set registers */
  183. #define VBUS_DEBOUNCE 0xC0
  184. #define ID_DEBOUNCE 0xC1
  185. #define VBAT_TIMER 0xD3
  186. #define PHY_PWR_CTRL 0xFD
  187. #define PHY_PWR_PHYPWD (1 << 0)
  188. #define PHY_CLK_CTRL 0xFE
  189. #define PHY_CLK_CTRL_CLOCKGATING_EN (1 << 2)
  190. #define PHY_CLK_CTRL_CLK32K_EN (1 << 1)
  191. #define REQ_PHY_DPLL_CLK (1 << 0)
  192. #define PHY_CLK_CTRL_STS 0xFF
  193. #define PHY_DPLL_CLK (1 << 0)
  194. /* In module TWL4030_MODULE_PM_MASTER */
  195. #define PROTECT_KEY 0x0E
  196. /* In module TWL4030_MODULE_PM_RECEIVER */
  197. #define VUSB_DEDICATED1 0x7D
  198. #define VUSB_DEDICATED2 0x7E
  199. #define VUSB1V5_DEV_GRP 0x71
  200. #define VUSB1V5_TYPE 0x72
  201. #define VUSB1V5_REMAP 0x73
  202. #define VUSB1V8_DEV_GRP 0x74
  203. #define VUSB1V8_TYPE 0x75
  204. #define VUSB1V8_REMAP 0x76
  205. #define VUSB3V1_DEV_GRP 0x77
  206. #define VUSB3V1_TYPE 0x78
  207. #define VUSB3V1_REMAP 0x79
  208. /* In module TWL4030_MODULE_INTBR */
  209. #define PMBR1 0x0D
  210. #define GPIO_USB_4PIN_ULPI_2430C (3 << 0)
  211. enum linkstat {
  212. USB_LINK_UNKNOWN = 0,
  213. USB_LINK_NONE,
  214. USB_LINK_VBUS,
  215. USB_LINK_ID,
  216. };
  217. struct twl4030_usb {
  218. struct otg_transceiver otg;
  219. struct device *dev;
  220. /* for vbus reporting with irqs disabled */
  221. spinlock_t lock;
  222. /* pin configuration */
  223. enum twl4030_usb_mode usb_mode;
  224. int irq;
  225. u8 linkstat;
  226. u8 asleep;
  227. bool irq_enabled;
  228. };
  229. /* internal define on top of container_of */
  230. #define xceiv_to_twl(x) container_of((x), struct twl4030_usb, otg);
  231. /*-------------------------------------------------------------------------*/
  232. static int twl4030_i2c_write_u8_verify(struct twl4030_usb *twl,
  233. u8 module, u8 data, u8 address)
  234. {
  235. u8 check;
  236. if ((twl4030_i2c_write_u8(module, data, address) >= 0) &&
  237. (twl4030_i2c_read_u8(module, &check, address) >= 0) &&
  238. (check == data))
  239. return 0;
  240. dev_dbg(twl->dev, "Write%d[%d,0x%x] wrote %02x but read %02x\n",
  241. 1, module, address, check, data);
  242. /* Failed once: Try again */
  243. if ((twl4030_i2c_write_u8(module, data, address) >= 0) &&
  244. (twl4030_i2c_read_u8(module, &check, address) >= 0) &&
  245. (check == data))
  246. return 0;
  247. dev_dbg(twl->dev, "Write%d[%d,0x%x] wrote %02x but read %02x\n",
  248. 2, module, address, check, data);
  249. /* Failed again: Return error */
  250. return -EBUSY;
  251. }
  252. #define twl4030_usb_write_verify(twl, address, data) \
  253. twl4030_i2c_write_u8_verify(twl, TWL4030_MODULE_USB, (data), (address))
  254. static inline int twl4030_usb_write(struct twl4030_usb *twl,
  255. u8 address, u8 data)
  256. {
  257. int ret = 0;
  258. ret = twl4030_i2c_write_u8(TWL4030_MODULE_USB, data, address);
  259. if (ret < 0)
  260. dev_dbg(twl->dev,
  261. "TWL4030:USB:Write[0x%x] Error %d\n", address, ret);
  262. return ret;
  263. }
  264. static inline int twl4030_readb(struct twl4030_usb *twl, u8 module, u8 address)
  265. {
  266. u8 data;
  267. int ret = 0;
  268. ret = twl4030_i2c_read_u8(module, &data, address);
  269. if (ret >= 0)
  270. ret = data;
  271. else
  272. dev_dbg(twl->dev,
  273. "TWL4030:readb[0x%x,0x%x] Error %d\n",
  274. module, address, ret);
  275. return ret;
  276. }
  277. static inline int twl4030_usb_read(struct twl4030_usb *twl, u8 address)
  278. {
  279. return twl4030_readb(twl, TWL4030_MODULE_USB, address);
  280. }
  281. /*-------------------------------------------------------------------------*/
  282. static inline int
  283. twl4030_usb_set_bits(struct twl4030_usb *twl, u8 reg, u8 bits)
  284. {
  285. return twl4030_usb_write(twl, reg + 1, bits);
  286. }
  287. static inline int
  288. twl4030_usb_clear_bits(struct twl4030_usb *twl, u8 reg, u8 bits)
  289. {
  290. return twl4030_usb_write(twl, reg + 2, bits);
  291. }
  292. /*-------------------------------------------------------------------------*/
  293. static enum linkstat twl4030_usb_linkstat(struct twl4030_usb *twl)
  294. {
  295. int status;
  296. int linkstat = USB_LINK_UNKNOWN;
  297. /* STS_HW_CONDITIONS */
  298. status = twl4030_readb(twl, TWL4030_MODULE_PM_MASTER, 0x0f);
  299. if (status < 0)
  300. dev_err(twl->dev, "USB link status err %d\n", status);
  301. else if (status & BIT(7))
  302. linkstat = USB_LINK_VBUS;
  303. else if (status & BIT(2))
  304. linkstat = USB_LINK_ID;
  305. else
  306. linkstat = USB_LINK_NONE;
  307. dev_dbg(twl->dev, "HW_CONDITIONS 0x%02x/%d; link %d\n",
  308. status, status, linkstat);
  309. /* REVISIT this assumes host and peripheral controllers
  310. * are registered, and that both are active...
  311. */
  312. spin_lock_irq(&twl->lock);
  313. twl->linkstat = linkstat;
  314. if (linkstat == USB_LINK_ID) {
  315. twl->otg.default_a = true;
  316. twl->otg.state = OTG_STATE_A_IDLE;
  317. } else {
  318. twl->otg.default_a = false;
  319. twl->otg.state = OTG_STATE_B_IDLE;
  320. }
  321. spin_unlock_irq(&twl->lock);
  322. return linkstat;
  323. }
  324. static void twl4030_usb_set_mode(struct twl4030_usb *twl, int mode)
  325. {
  326. twl->usb_mode = mode;
  327. switch (mode) {
  328. case T2_USB_MODE_ULPI:
  329. twl4030_usb_clear_bits(twl, IFC_CTRL, IFC_CTRL_CARKITMODE);
  330. twl4030_usb_set_bits(twl, POWER_CTRL, POWER_CTRL_OTG_ENAB);
  331. twl4030_usb_clear_bits(twl, FUNC_CTRL,
  332. FUNC_CTRL_XCVRSELECT_MASK |
  333. FUNC_CTRL_OPMODE_MASK);
  334. break;
  335. case -1:
  336. /* FIXME: power on defaults */
  337. break;
  338. default:
  339. dev_err(twl->dev, "unsupported T2 transceiver mode %d\n",
  340. mode);
  341. break;
  342. };
  343. }
  344. static void twl4030_i2c_access(struct twl4030_usb *twl, int on)
  345. {
  346. unsigned long timeout;
  347. int val = twl4030_usb_read(twl, PHY_CLK_CTRL);
  348. if (val >= 0) {
  349. if (on) {
  350. /* enable DPLL to access PHY registers over I2C */
  351. val |= REQ_PHY_DPLL_CLK;
  352. WARN_ON(twl4030_usb_write_verify(twl, PHY_CLK_CTRL,
  353. (u8)val) < 0);
  354. timeout = jiffies + HZ;
  355. while (!(twl4030_usb_read(twl, PHY_CLK_CTRL_STS) &
  356. PHY_DPLL_CLK)
  357. && time_before(jiffies, timeout))
  358. udelay(10);
  359. if (!(twl4030_usb_read(twl, PHY_CLK_CTRL_STS) &
  360. PHY_DPLL_CLK))
  361. dev_err(twl->dev, "Timeout setting T2 HSUSB "
  362. "PHY DPLL clock\n");
  363. } else {
  364. /* let ULPI control the DPLL clock */
  365. val &= ~REQ_PHY_DPLL_CLK;
  366. WARN_ON(twl4030_usb_write_verify(twl, PHY_CLK_CTRL,
  367. (u8)val) < 0);
  368. }
  369. }
  370. }
  371. static void twl4030_phy_power(struct twl4030_usb *twl, int on)
  372. {
  373. u8 pwr;
  374. pwr = twl4030_usb_read(twl, PHY_PWR_CTRL);
  375. if (on) {
  376. pwr &= ~PHY_PWR_PHYPWD;
  377. WARN_ON(twl4030_usb_write_verify(twl, PHY_PWR_CTRL, pwr) < 0);
  378. twl4030_usb_write(twl, PHY_CLK_CTRL,
  379. twl4030_usb_read(twl, PHY_CLK_CTRL) |
  380. (PHY_CLK_CTRL_CLOCKGATING_EN |
  381. PHY_CLK_CTRL_CLK32K_EN));
  382. } else {
  383. pwr |= PHY_PWR_PHYPWD;
  384. WARN_ON(twl4030_usb_write_verify(twl, PHY_PWR_CTRL, pwr) < 0);
  385. }
  386. }
  387. static void twl4030_phy_suspend(struct twl4030_usb *twl, int controller_off)
  388. {
  389. if (twl->asleep)
  390. return;
  391. twl4030_phy_power(twl, 0);
  392. twl->asleep = 1;
  393. }
  394. static void twl4030_phy_resume(struct twl4030_usb *twl)
  395. {
  396. if (!twl->asleep)
  397. return;
  398. twl4030_phy_power(twl, 1);
  399. twl4030_i2c_access(twl, 1);
  400. twl4030_usb_set_mode(twl, twl->usb_mode);
  401. if (twl->usb_mode == T2_USB_MODE_ULPI)
  402. twl4030_i2c_access(twl, 0);
  403. twl->asleep = 0;
  404. }
  405. static void twl4030_usb_ldo_init(struct twl4030_usb *twl)
  406. {
  407. /* Enable writing to power configuration registers */
  408. twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, 0xC0, PROTECT_KEY);
  409. twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, 0x0C, PROTECT_KEY);
  410. /* put VUSB3V1 LDO in active state */
  411. twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0, VUSB_DEDICATED2);
  412. /* input to VUSB3V1 LDO is from VBAT, not VBUS */
  413. twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0x14, VUSB_DEDICATED1);
  414. /* turn on 3.1V regulator */
  415. twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0x20, VUSB3V1_DEV_GRP);
  416. twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0, VUSB3V1_TYPE);
  417. /* turn on 1.5V regulator */
  418. twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0x20, VUSB1V5_DEV_GRP);
  419. twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0, VUSB1V5_TYPE);
  420. /* turn on 1.8V regulator */
  421. twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0x20, VUSB1V8_DEV_GRP);
  422. twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0, VUSB1V8_TYPE);
  423. /* disable access to power configuration registers */
  424. twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, 0, PROTECT_KEY);
  425. }
  426. static ssize_t twl4030_usb_vbus_show(struct device *dev,
  427. struct device_attribute *attr, char *buf)
  428. {
  429. struct twl4030_usb *twl = dev_get_drvdata(dev);
  430. unsigned long flags;
  431. int ret = -EINVAL;
  432. spin_lock_irqsave(&twl->lock, flags);
  433. ret = sprintf(buf, "%s\n",
  434. (twl->linkstat == USB_LINK_VBUS) ? "on" : "off");
  435. spin_unlock_irqrestore(&twl->lock, flags);
  436. return ret;
  437. }
  438. static DEVICE_ATTR(vbus, 0444, twl4030_usb_vbus_show, NULL);
  439. static irqreturn_t twl4030_usb_irq(int irq, void *_twl)
  440. {
  441. struct twl4030_usb *twl = _twl;
  442. int status;
  443. #ifdef CONFIG_LOCKDEP
  444. /* WORKAROUND for lockdep forcing IRQF_DISABLED on us, which
  445. * we don't want and can't tolerate. Although it might be
  446. * friendlier not to borrow this thread context...
  447. */
  448. local_irq_enable();
  449. #endif
  450. status = twl4030_usb_linkstat(twl);
  451. if (status != USB_LINK_UNKNOWN) {
  452. /* FIXME add a set_power() method so that B-devices can
  453. * configure the charger appropriately. It's not always
  454. * correct to consume VBUS power, and how much current to
  455. * consume is a function of the USB configuration chosen
  456. * by the host.
  457. *
  458. * REVISIT usb_gadget_vbus_connect(...) as needed, ditto
  459. * its disconnect() sibling, when changing to/from the
  460. * USB_LINK_VBUS state. musb_hdrc won't care until it
  461. * starts to handle softconnect right.
  462. */
  463. twl4030charger_usb_en(status == USB_LINK_VBUS);
  464. if (status == USB_LINK_NONE)
  465. twl4030_phy_suspend(twl, 0);
  466. else
  467. twl4030_phy_resume(twl);
  468. }
  469. sysfs_notify(&twl->dev->kobj, NULL, "vbus");
  470. return IRQ_HANDLED;
  471. }
  472. static int twl4030_set_suspend(struct otg_transceiver *x, int suspend)
  473. {
  474. struct twl4030_usb *twl = xceiv_to_twl(x);
  475. if (suspend)
  476. twl4030_phy_suspend(twl, 1);
  477. else
  478. twl4030_phy_resume(twl);
  479. return 0;
  480. }
  481. static int twl4030_set_peripheral(struct otg_transceiver *x,
  482. struct usb_gadget *gadget)
  483. {
  484. struct twl4030_usb *twl;
  485. if (!x)
  486. return -ENODEV;
  487. twl = xceiv_to_twl(x);
  488. twl->otg.gadget = gadget;
  489. if (!gadget)
  490. twl->otg.state = OTG_STATE_UNDEFINED;
  491. return 0;
  492. }
  493. static int twl4030_set_host(struct otg_transceiver *x, struct usb_bus *host)
  494. {
  495. struct twl4030_usb *twl;
  496. if (!x)
  497. return -ENODEV;
  498. twl = xceiv_to_twl(x);
  499. twl->otg.host = host;
  500. if (!host)
  501. twl->otg.state = OTG_STATE_UNDEFINED;
  502. return 0;
  503. }
  504. static int __init twl4030_usb_probe(struct platform_device *pdev)
  505. {
  506. struct twl4030_usb_data *pdata = pdev->dev.platform_data;
  507. struct twl4030_usb *twl;
  508. int status;
  509. if (!pdata) {
  510. dev_dbg(&pdev->dev, "platform_data not available\n");
  511. return -EINVAL;
  512. }
  513. twl = kzalloc(sizeof *twl, GFP_KERNEL);
  514. if (!twl)
  515. return -ENOMEM;
  516. twl->dev = &pdev->dev;
  517. twl->irq = platform_get_irq(pdev, 0);
  518. twl->otg.dev = twl->dev;
  519. twl->otg.label = "twl4030";
  520. twl->otg.set_host = twl4030_set_host;
  521. twl->otg.set_peripheral = twl4030_set_peripheral;
  522. twl->otg.set_suspend = twl4030_set_suspend;
  523. twl->usb_mode = pdata->usb_mode;
  524. twl->asleep = 1;
  525. /* init spinlock for workqueue */
  526. spin_lock_init(&twl->lock);
  527. twl4030_usb_ldo_init(twl);
  528. otg_set_transceiver(&twl->otg);
  529. platform_set_drvdata(pdev, twl);
  530. if (device_create_file(&pdev->dev, &dev_attr_vbus))
  531. dev_warn(&pdev->dev, "could not create sysfs file\n");
  532. /* Our job is to use irqs and status from the power module
  533. * to keep the transceiver disabled when nothing's connected.
  534. *
  535. * FIXME we actually shouldn't start enabling it until the
  536. * USB controller drivers have said they're ready, by calling
  537. * set_host() and/or set_peripheral() ... OTG_capable boards
  538. * need both handles, otherwise just one suffices.
  539. */
  540. twl->irq_enabled = true;
  541. status = request_irq(twl->irq, twl4030_usb_irq,
  542. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  543. "twl4030_usb", twl);
  544. if (status < 0) {
  545. dev_dbg(&pdev->dev, "can't get IRQ %d, err %d\n",
  546. twl->irq, status);
  547. kfree(twl);
  548. return status;
  549. }
  550. /* The IRQ handler just handles changes from the previous states
  551. * of the ID and VBUS pins ... in probe() we must initialize that
  552. * previous state. The easy way: fake an IRQ.
  553. *
  554. * REVISIT: a real IRQ might have happened already, if PREEMPT is
  555. * enabled. Else the IRQ may not yet be configured or enabled,
  556. * because of scheduling delays.
  557. */
  558. twl4030_usb_irq(twl->irq, twl);
  559. dev_info(&pdev->dev, "Initialized TWL4030 USB module\n");
  560. return 0;
  561. }
  562. static int __exit twl4030_usb_remove(struct platform_device *pdev)
  563. {
  564. struct twl4030_usb *twl = platform_get_drvdata(pdev);
  565. int val;
  566. free_irq(twl->irq, twl);
  567. device_remove_file(twl->dev, &dev_attr_vbus);
  568. /* set transceiver mode to power on defaults */
  569. twl4030_usb_set_mode(twl, -1);
  570. /* autogate 60MHz ULPI clock,
  571. * clear dpll clock request for i2c access,
  572. * disable 32KHz
  573. */
  574. val = twl4030_usb_read(twl, PHY_CLK_CTRL);
  575. if (val >= 0) {
  576. val |= PHY_CLK_CTRL_CLOCKGATING_EN;
  577. val &= ~(PHY_CLK_CTRL_CLK32K_EN | REQ_PHY_DPLL_CLK);
  578. twl4030_usb_write(twl, PHY_CLK_CTRL, (u8)val);
  579. }
  580. /* disable complete OTG block */
  581. twl4030_usb_clear_bits(twl, POWER_CTRL, POWER_CTRL_OTG_ENAB);
  582. twl4030_phy_power(twl, 0);
  583. kfree(twl);
  584. return 0;
  585. }
  586. static struct platform_driver twl4030_usb_driver = {
  587. .probe = twl4030_usb_probe,
  588. .remove = __exit_p(twl4030_usb_remove),
  589. .driver = {
  590. .name = "twl4030_usb",
  591. .owner = THIS_MODULE,
  592. },
  593. };
  594. static int __init twl4030_usb_init(void)
  595. {
  596. return platform_driver_register(&twl4030_usb_driver);
  597. }
  598. subsys_initcall(twl4030_usb_init);
  599. static void __exit twl4030_usb_exit(void)
  600. {
  601. platform_driver_unregister(&twl4030_usb_driver);
  602. }
  603. module_exit(twl4030_usb_exit);
  604. MODULE_ALIAS("platform:twl4030_usb");
  605. MODULE_AUTHOR("Texas Instruments, Inc, Nokia Corporation");
  606. MODULE_DESCRIPTION("TWL4030 USB transceiver driver");
  607. MODULE_LICENSE("GPL");