twl4030-usb.c 21 KB

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