twl4030-usb.c 21 KB

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