twl4030-usb.c 21 KB

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