twl4030-usb.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  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/twl.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. #define STS_HW_CONDITIONS 0x0F
  199. /* In module TWL4030_MODULE_PM_RECEIVER */
  200. #define VUSB_DEDICATED1 0x7D
  201. #define VUSB_DEDICATED2 0x7E
  202. #define VUSB1V5_DEV_GRP 0x71
  203. #define VUSB1V5_TYPE 0x72
  204. #define VUSB1V5_REMAP 0x73
  205. #define VUSB1V8_DEV_GRP 0x74
  206. #define VUSB1V8_TYPE 0x75
  207. #define VUSB1V8_REMAP 0x76
  208. #define VUSB3V1_DEV_GRP 0x77
  209. #define VUSB3V1_TYPE 0x78
  210. #define VUSB3V1_REMAP 0x79
  211. /* In module TWL4030_MODULE_INTBR */
  212. #define PMBR1 0x0D
  213. #define GPIO_USB_4PIN_ULPI_2430C (3 << 0)
  214. enum linkstat {
  215. USB_LINK_UNKNOWN = 0,
  216. USB_LINK_NONE,
  217. USB_LINK_VBUS,
  218. USB_LINK_ID,
  219. };
  220. struct twl4030_usb {
  221. struct otg_transceiver otg;
  222. struct device *dev;
  223. /* TWL4030 internal USB regulator supplies */
  224. struct regulator *usb1v5;
  225. struct regulator *usb1v8;
  226. struct regulator *usb3v1;
  227. /* for vbus reporting with irqs disabled */
  228. spinlock_t lock;
  229. /* pin configuration */
  230. enum twl4030_usb_mode usb_mode;
  231. int irq;
  232. u8 linkstat;
  233. u8 asleep;
  234. bool irq_enabled;
  235. };
  236. /* internal define on top of container_of */
  237. #define xceiv_to_twl(x) container_of((x), struct twl4030_usb, otg);
  238. /*-------------------------------------------------------------------------*/
  239. static int twl4030_i2c_write_u8_verify(struct twl4030_usb *twl,
  240. u8 module, u8 data, u8 address)
  241. {
  242. u8 check;
  243. if ((twl_i2c_write_u8(module, data, address) >= 0) &&
  244. (twl_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. 1, module, address, check, data);
  249. /* Failed once: Try again */
  250. if ((twl_i2c_write_u8(module, data, address) >= 0) &&
  251. (twl_i2c_read_u8(module, &check, address) >= 0) &&
  252. (check == data))
  253. return 0;
  254. dev_dbg(twl->dev, "Write%d[%d,0x%x] wrote %02x but read %02x\n",
  255. 2, module, address, check, data);
  256. /* Failed again: Return error */
  257. return -EBUSY;
  258. }
  259. #define twl4030_usb_write_verify(twl, address, data) \
  260. twl4030_i2c_write_u8_verify(twl, TWL4030_MODULE_USB, (data), (address))
  261. static inline int twl4030_usb_write(struct twl4030_usb *twl,
  262. u8 address, u8 data)
  263. {
  264. int ret = 0;
  265. ret = twl_i2c_write_u8(TWL4030_MODULE_USB, data, address);
  266. if (ret < 0)
  267. dev_dbg(twl->dev,
  268. "TWL4030:USB:Write[0x%x] Error %d\n", address, ret);
  269. return ret;
  270. }
  271. static inline int twl4030_readb(struct twl4030_usb *twl, u8 module, u8 address)
  272. {
  273. u8 data;
  274. int ret = 0;
  275. ret = twl_i2c_read_u8(module, &data, address);
  276. if (ret >= 0)
  277. ret = data;
  278. else
  279. dev_dbg(twl->dev,
  280. "TWL4030:readb[0x%x,0x%x] Error %d\n",
  281. module, address, ret);
  282. return ret;
  283. }
  284. static inline int twl4030_usb_read(struct twl4030_usb *twl, u8 address)
  285. {
  286. return twl4030_readb(twl, TWL4030_MODULE_USB, address);
  287. }
  288. /*-------------------------------------------------------------------------*/
  289. static inline int
  290. twl4030_usb_set_bits(struct twl4030_usb *twl, u8 reg, u8 bits)
  291. {
  292. return twl4030_usb_write(twl, reg + 1, bits);
  293. }
  294. static inline int
  295. twl4030_usb_clear_bits(struct twl4030_usb *twl, u8 reg, u8 bits)
  296. {
  297. return twl4030_usb_write(twl, reg + 2, bits);
  298. }
  299. /*-------------------------------------------------------------------------*/
  300. static enum linkstat twl4030_usb_linkstat(struct twl4030_usb *twl)
  301. {
  302. int status;
  303. int linkstat = USB_LINK_UNKNOWN;
  304. /*
  305. * For ID/VBUS sensing, see manual section 15.4.8 ...
  306. * except when using only battery backup power, two
  307. * comparators produce VBUS_PRES and ID_PRES signals,
  308. * which don't match docs elsewhere. But ... BIT(7)
  309. * and BIT(2) of STS_HW_CONDITIONS, respectively, do
  310. * seem to match up. If either is true the USB_PRES
  311. * signal is active, the OTG module is activated, and
  312. * its interrupt may be raised (may wake the system).
  313. */
  314. status = twl4030_readb(twl, TWL4030_MODULE_PM_MASTER,
  315. STS_HW_CONDITIONS);
  316. if (status < 0)
  317. dev_err(twl->dev, "USB link status err %d\n", status);
  318. else if (status & (BIT(7) | BIT(2))) {
  319. if (status & BIT(2))
  320. linkstat = USB_LINK_ID;
  321. else
  322. linkstat = USB_LINK_VBUS;
  323. } else
  324. linkstat = USB_LINK_NONE;
  325. dev_dbg(twl->dev, "HW_CONDITIONS 0x%02x/%d; link %d\n",
  326. status, status, linkstat);
  327. /* REVISIT this assumes host and peripheral controllers
  328. * are registered, and that both are active...
  329. */
  330. spin_lock_irq(&twl->lock);
  331. twl->linkstat = linkstat;
  332. if (linkstat == USB_LINK_ID) {
  333. twl->otg.default_a = true;
  334. twl->otg.state = OTG_STATE_A_IDLE;
  335. } else {
  336. twl->otg.default_a = false;
  337. twl->otg.state = OTG_STATE_B_IDLE;
  338. }
  339. spin_unlock_irq(&twl->lock);
  340. return linkstat;
  341. }
  342. static void twl4030_usb_set_mode(struct twl4030_usb *twl, int mode)
  343. {
  344. twl->usb_mode = mode;
  345. switch (mode) {
  346. case T2_USB_MODE_ULPI:
  347. twl4030_usb_clear_bits(twl, IFC_CTRL, IFC_CTRL_CARKITMODE);
  348. twl4030_usb_set_bits(twl, POWER_CTRL, POWER_CTRL_OTG_ENAB);
  349. twl4030_usb_clear_bits(twl, FUNC_CTRL,
  350. FUNC_CTRL_XCVRSELECT_MASK |
  351. FUNC_CTRL_OPMODE_MASK);
  352. break;
  353. case -1:
  354. /* FIXME: power on defaults */
  355. break;
  356. default:
  357. dev_err(twl->dev, "unsupported T2 transceiver mode %d\n",
  358. mode);
  359. break;
  360. };
  361. }
  362. static void twl4030_i2c_access(struct twl4030_usb *twl, int on)
  363. {
  364. unsigned long timeout;
  365. int val = twl4030_usb_read(twl, PHY_CLK_CTRL);
  366. if (val >= 0) {
  367. if (on) {
  368. /* enable DPLL to access PHY registers over I2C */
  369. val |= REQ_PHY_DPLL_CLK;
  370. WARN_ON(twl4030_usb_write_verify(twl, PHY_CLK_CTRL,
  371. (u8)val) < 0);
  372. timeout = jiffies + HZ;
  373. while (!(twl4030_usb_read(twl, PHY_CLK_CTRL_STS) &
  374. PHY_DPLL_CLK)
  375. && time_before(jiffies, timeout))
  376. udelay(10);
  377. if (!(twl4030_usb_read(twl, PHY_CLK_CTRL_STS) &
  378. PHY_DPLL_CLK))
  379. dev_err(twl->dev, "Timeout setting T2 HSUSB "
  380. "PHY DPLL clock\n");
  381. } else {
  382. /* let ULPI control the DPLL clock */
  383. val &= ~REQ_PHY_DPLL_CLK;
  384. WARN_ON(twl4030_usb_write_verify(twl, PHY_CLK_CTRL,
  385. (u8)val) < 0);
  386. }
  387. }
  388. }
  389. static void twl4030_phy_power(struct twl4030_usb *twl, int on)
  390. {
  391. u8 pwr;
  392. pwr = twl4030_usb_read(twl, PHY_PWR_CTRL);
  393. if (on) {
  394. regulator_enable(twl->usb3v1);
  395. regulator_enable(twl->usb1v8);
  396. /*
  397. * Disabling usb3v1 regulator (= writing 0 to VUSB3V1_DEV_GRP
  398. * in twl4030) resets the VUSB_DEDICATED2 register. This reset
  399. * enables VUSB3V1_SLEEP bit that remaps usb3v1 ACTIVE state to
  400. * SLEEP. We work around this by clearing the bit after usv3v1
  401. * is re-activated. This ensures that VUSB3V1 is really active.
  402. */
  403. twl_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0,
  404. VUSB_DEDICATED2);
  405. regulator_enable(twl->usb1v5);
  406. pwr &= ~PHY_PWR_PHYPWD;
  407. WARN_ON(twl4030_usb_write_verify(twl, PHY_PWR_CTRL, pwr) < 0);
  408. twl4030_usb_write(twl, PHY_CLK_CTRL,
  409. twl4030_usb_read(twl, PHY_CLK_CTRL) |
  410. (PHY_CLK_CTRL_CLOCKGATING_EN |
  411. PHY_CLK_CTRL_CLK32K_EN));
  412. } else {
  413. pwr |= PHY_PWR_PHYPWD;
  414. WARN_ON(twl4030_usb_write_verify(twl, PHY_PWR_CTRL, pwr) < 0);
  415. regulator_disable(twl->usb1v5);
  416. regulator_disable(twl->usb1v8);
  417. regulator_disable(twl->usb3v1);
  418. }
  419. }
  420. static void twl4030_phy_suspend(struct twl4030_usb *twl, int controller_off)
  421. {
  422. if (twl->asleep)
  423. return;
  424. twl4030_phy_power(twl, 0);
  425. twl->asleep = 1;
  426. }
  427. static void twl4030_phy_resume(struct twl4030_usb *twl)
  428. {
  429. if (!twl->asleep)
  430. return;
  431. twl4030_phy_power(twl, 1);
  432. twl4030_i2c_access(twl, 1);
  433. twl4030_usb_set_mode(twl, twl->usb_mode);
  434. if (twl->usb_mode == T2_USB_MODE_ULPI)
  435. twl4030_i2c_access(twl, 0);
  436. twl->asleep = 0;
  437. }
  438. static int twl4030_usb_ldo_init(struct twl4030_usb *twl)
  439. {
  440. /* Enable writing to power configuration registers */
  441. twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER, 0xC0, PROTECT_KEY);
  442. twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER, 0x0C, PROTECT_KEY);
  443. /* put VUSB3V1 LDO in active state */
  444. twl_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0, VUSB_DEDICATED2);
  445. /* input to VUSB3V1 LDO is from VBAT, not VBUS */
  446. twl_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0x14, VUSB_DEDICATED1);
  447. /* Initialize 3.1V regulator */
  448. twl_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0, VUSB3V1_DEV_GRP);
  449. twl->usb3v1 = regulator_get(twl->dev, "usb3v1");
  450. if (IS_ERR(twl->usb3v1))
  451. return -ENODEV;
  452. twl_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0, VUSB3V1_TYPE);
  453. /* Initialize 1.5V regulator */
  454. twl_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0, VUSB1V5_DEV_GRP);
  455. twl->usb1v5 = regulator_get(twl->dev, "usb1v5");
  456. if (IS_ERR(twl->usb1v5))
  457. goto fail1;
  458. twl_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0, VUSB1V5_TYPE);
  459. /* Initialize 1.8V regulator */
  460. twl_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0, VUSB1V8_DEV_GRP);
  461. twl->usb1v8 = regulator_get(twl->dev, "usb1v8");
  462. if (IS_ERR(twl->usb1v8))
  463. goto fail2;
  464. twl_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0, VUSB1V8_TYPE);
  465. /* disable access to power configuration registers */
  466. twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER, 0, PROTECT_KEY);
  467. return 0;
  468. fail2:
  469. regulator_put(twl->usb1v5);
  470. twl->usb1v5 = NULL;
  471. fail1:
  472. regulator_put(twl->usb3v1);
  473. twl->usb3v1 = NULL;
  474. return -ENODEV;
  475. }
  476. static ssize_t twl4030_usb_vbus_show(struct device *dev,
  477. struct device_attribute *attr, char *buf)
  478. {
  479. struct twl4030_usb *twl = dev_get_drvdata(dev);
  480. unsigned long flags;
  481. int ret = -EINVAL;
  482. spin_lock_irqsave(&twl->lock, flags);
  483. ret = sprintf(buf, "%s\n",
  484. (twl->linkstat == USB_LINK_VBUS) ? "on" : "off");
  485. spin_unlock_irqrestore(&twl->lock, flags);
  486. return ret;
  487. }
  488. static DEVICE_ATTR(vbus, 0444, twl4030_usb_vbus_show, NULL);
  489. static irqreturn_t twl4030_usb_irq(int irq, void *_twl)
  490. {
  491. struct twl4030_usb *twl = _twl;
  492. int status;
  493. #ifdef CONFIG_LOCKDEP
  494. /* WORKAROUND for lockdep forcing IRQF_DISABLED on us, which
  495. * we don't want and can't tolerate. Although it might be
  496. * friendlier not to borrow this thread context...
  497. */
  498. local_irq_enable();
  499. #endif
  500. status = twl4030_usb_linkstat(twl);
  501. if (status != USB_LINK_UNKNOWN) {
  502. /* FIXME add a set_power() method so that B-devices can
  503. * configure the charger appropriately. It's not always
  504. * correct to consume VBUS power, and how much current to
  505. * consume is a function of the USB configuration chosen
  506. * by the host.
  507. *
  508. * REVISIT usb_gadget_vbus_connect(...) as needed, ditto
  509. * its disconnect() sibling, when changing to/from the
  510. * USB_LINK_VBUS state. musb_hdrc won't care until it
  511. * starts to handle softconnect right.
  512. */
  513. if (status == USB_LINK_NONE)
  514. twl4030_phy_suspend(twl, 0);
  515. else
  516. twl4030_phy_resume(twl);
  517. twl4030charger_usb_en(status == USB_LINK_VBUS);
  518. }
  519. sysfs_notify(&twl->dev->kobj, NULL, "vbus");
  520. return IRQ_HANDLED;
  521. }
  522. static int twl4030_set_suspend(struct otg_transceiver *x, int suspend)
  523. {
  524. struct twl4030_usb *twl = xceiv_to_twl(x);
  525. if (suspend)
  526. twl4030_phy_suspend(twl, 1);
  527. else
  528. twl4030_phy_resume(twl);
  529. return 0;
  530. }
  531. static int twl4030_set_peripheral(struct otg_transceiver *x,
  532. struct usb_gadget *gadget)
  533. {
  534. struct twl4030_usb *twl;
  535. if (!x)
  536. return -ENODEV;
  537. twl = xceiv_to_twl(x);
  538. twl->otg.gadget = gadget;
  539. if (!gadget)
  540. twl->otg.state = OTG_STATE_UNDEFINED;
  541. return 0;
  542. }
  543. static int twl4030_set_host(struct otg_transceiver *x, struct usb_bus *host)
  544. {
  545. struct twl4030_usb *twl;
  546. if (!x)
  547. return -ENODEV;
  548. twl = xceiv_to_twl(x);
  549. twl->otg.host = host;
  550. if (!host)
  551. twl->otg.state = OTG_STATE_UNDEFINED;
  552. return 0;
  553. }
  554. static int __devinit twl4030_usb_probe(struct platform_device *pdev)
  555. {
  556. struct twl4030_usb_data *pdata = pdev->dev.platform_data;
  557. struct twl4030_usb *twl;
  558. int status, err;
  559. if (!pdata) {
  560. dev_dbg(&pdev->dev, "platform_data not available\n");
  561. return -EINVAL;
  562. }
  563. twl = kzalloc(sizeof *twl, GFP_KERNEL);
  564. if (!twl)
  565. return -ENOMEM;
  566. twl->dev = &pdev->dev;
  567. twl->irq = platform_get_irq(pdev, 0);
  568. twl->otg.dev = twl->dev;
  569. twl->otg.label = "twl4030";
  570. twl->otg.set_host = twl4030_set_host;
  571. twl->otg.set_peripheral = twl4030_set_peripheral;
  572. twl->otg.set_suspend = twl4030_set_suspend;
  573. twl->usb_mode = pdata->usb_mode;
  574. twl->asleep = 1;
  575. /* init spinlock for workqueue */
  576. spin_lock_init(&twl->lock);
  577. err = twl4030_usb_ldo_init(twl);
  578. if (err) {
  579. dev_err(&pdev->dev, "ldo init failed\n");
  580. kfree(twl);
  581. return err;
  582. }
  583. otg_set_transceiver(&twl->otg);
  584. platform_set_drvdata(pdev, twl);
  585. if (device_create_file(&pdev->dev, &dev_attr_vbus))
  586. dev_warn(&pdev->dev, "could not create sysfs file\n");
  587. /* Our job is to use irqs and status from the power module
  588. * to keep the transceiver disabled when nothing's connected.
  589. *
  590. * FIXME we actually shouldn't start enabling it until the
  591. * USB controller drivers have said they're ready, by calling
  592. * set_host() and/or set_peripheral() ... OTG_capable boards
  593. * need both handles, otherwise just one suffices.
  594. */
  595. twl->irq_enabled = true;
  596. status = request_irq(twl->irq, twl4030_usb_irq,
  597. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  598. "twl4030_usb", twl);
  599. if (status < 0) {
  600. dev_dbg(&pdev->dev, "can't get IRQ %d, err %d\n",
  601. twl->irq, status);
  602. kfree(twl);
  603. return status;
  604. }
  605. /* The IRQ handler just handles changes from the previous states
  606. * of the ID and VBUS pins ... in probe() we must initialize that
  607. * previous state. The easy way: fake an IRQ.
  608. *
  609. * REVISIT: a real IRQ might have happened already, if PREEMPT is
  610. * enabled. Else the IRQ may not yet be configured or enabled,
  611. * because of scheduling delays.
  612. */
  613. twl4030_usb_irq(twl->irq, twl);
  614. dev_info(&pdev->dev, "Initialized TWL4030 USB module\n");
  615. return 0;
  616. }
  617. static int __exit twl4030_usb_remove(struct platform_device *pdev)
  618. {
  619. struct twl4030_usb *twl = platform_get_drvdata(pdev);
  620. int val;
  621. free_irq(twl->irq, twl);
  622. device_remove_file(twl->dev, &dev_attr_vbus);
  623. /* set transceiver mode to power on defaults */
  624. twl4030_usb_set_mode(twl, -1);
  625. /* autogate 60MHz ULPI clock,
  626. * clear dpll clock request for i2c access,
  627. * disable 32KHz
  628. */
  629. val = twl4030_usb_read(twl, PHY_CLK_CTRL);
  630. if (val >= 0) {
  631. val |= PHY_CLK_CTRL_CLOCKGATING_EN;
  632. val &= ~(PHY_CLK_CTRL_CLK32K_EN | REQ_PHY_DPLL_CLK);
  633. twl4030_usb_write(twl, PHY_CLK_CTRL, (u8)val);
  634. }
  635. /* disable complete OTG block */
  636. twl4030_usb_clear_bits(twl, POWER_CTRL, POWER_CTRL_OTG_ENAB);
  637. twl4030_phy_power(twl, 0);
  638. regulator_put(twl->usb1v5);
  639. regulator_put(twl->usb1v8);
  640. regulator_put(twl->usb3v1);
  641. kfree(twl);
  642. return 0;
  643. }
  644. static struct platform_driver twl4030_usb_driver = {
  645. .probe = twl4030_usb_probe,
  646. .remove = __exit_p(twl4030_usb_remove),
  647. .driver = {
  648. .name = "twl4030_usb",
  649. .owner = THIS_MODULE,
  650. },
  651. };
  652. static int __init twl4030_usb_init(void)
  653. {
  654. return platform_driver_register(&twl4030_usb_driver);
  655. }
  656. subsys_initcall(twl4030_usb_init);
  657. static void __exit twl4030_usb_exit(void)
  658. {
  659. platform_driver_unregister(&twl4030_usb_driver);
  660. }
  661. module_exit(twl4030_usb_exit);
  662. MODULE_ALIAS("platform:twl4030_usb");
  663. MODULE_AUTHOR("Texas Instruments, Inc, Nokia Corporation");
  664. MODULE_DESCRIPTION("TWL4030 USB transceiver driver");
  665. MODULE_LICENSE("GPL");