phy-ab8500-usb.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. /*
  2. * drivers/usb/otg/ab8500_usb.c
  3. *
  4. * USB transceiver driver for AB8500 chip
  5. *
  6. * Copyright (C) 2010 ST-Ericsson AB
  7. * Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. *
  23. */
  24. #include <linux/module.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/usb/otg.h>
  27. #include <linux/slab.h>
  28. #include <linux/notifier.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/delay.h>
  31. #include <linux/mfd/abx500.h>
  32. #include <linux/mfd/abx500/ab8500.h>
  33. #include <linux/usb/musb-ux500.h>
  34. #include <linux/regulator/consumer.h>
  35. /* Bank AB8500_SYS_CTRL2_BLOCK */
  36. #define AB8500_MAIN_WD_CTRL_REG 0x01
  37. /* Bank AB8500_USB */
  38. #define AB8500_USB_LINE_STAT_REG 0x80
  39. #define AB8505_USB_LINE_STAT_REG 0x94
  40. #define AB8500_USB_PHY_CTRL_REG 0x8A
  41. /* Bank AB8500_DEVELOPMENT */
  42. #define AB8500_BANK12_ACCESS 0x00
  43. /* Bank AB8500_DEBUG */
  44. #define AB8500_USB_PHY_TUNE1 0x05
  45. #define AB8500_USB_PHY_TUNE2 0x06
  46. #define AB8500_USB_PHY_TUNE3 0x07
  47. #define AB8500_BIT_OTG_STAT_ID (1 << 0)
  48. #define AB8500_BIT_PHY_CTRL_HOST_EN (1 << 0)
  49. #define AB8500_BIT_PHY_CTRL_DEVICE_EN (1 << 1)
  50. #define AB8500_BIT_WD_CTRL_ENABLE (1 << 0)
  51. #define AB8500_BIT_WD_CTRL_KICK (1 << 1)
  52. #define AB8500_WD_KICK_DELAY_US 100 /* usec */
  53. #define AB8500_WD_V11_DISABLE_DELAY_US 100 /* usec */
  54. #define AB8500_V20_31952_DISABLE_DELAY_US 100 /* usec */
  55. /* Usb line status register */
  56. enum ab8500_usb_link_status {
  57. USB_LINK_NOT_CONFIGURED_8500 = 0,
  58. USB_LINK_STD_HOST_NC_8500,
  59. USB_LINK_STD_HOST_C_NS_8500,
  60. USB_LINK_STD_HOST_C_S_8500,
  61. USB_LINK_HOST_CHG_NM_8500,
  62. USB_LINK_HOST_CHG_HS_8500,
  63. USB_LINK_HOST_CHG_HS_CHIRP_8500,
  64. USB_LINK_DEDICATED_CHG_8500,
  65. USB_LINK_ACA_RID_A_8500,
  66. USB_LINK_ACA_RID_B_8500,
  67. USB_LINK_ACA_RID_C_NM_8500,
  68. USB_LINK_ACA_RID_C_HS_8500,
  69. USB_LINK_ACA_RID_C_HS_CHIRP_8500,
  70. USB_LINK_HM_IDGND_8500,
  71. USB_LINK_RESERVED_8500,
  72. USB_LINK_NOT_VALID_LINK_8500,
  73. };
  74. enum ab8505_usb_link_status {
  75. USB_LINK_NOT_CONFIGURED_8505 = 0,
  76. USB_LINK_STD_HOST_NC_8505,
  77. USB_LINK_STD_HOST_C_NS_8505,
  78. USB_LINK_STD_HOST_C_S_8505,
  79. USB_LINK_CDP_8505,
  80. USB_LINK_RESERVED0_8505,
  81. USB_LINK_RESERVED1_8505,
  82. USB_LINK_DEDICATED_CHG_8505,
  83. USB_LINK_ACA_RID_A_8505,
  84. USB_LINK_ACA_RID_B_8505,
  85. USB_LINK_ACA_RID_C_NM_8505,
  86. USB_LINK_RESERVED2_8505,
  87. USB_LINK_RESERVED3_8505,
  88. USB_LINK_HM_IDGND_8505,
  89. USB_LINK_CHARGERPORT_NOT_OK_8505,
  90. USB_LINK_CHARGER_DM_HIGH_8505,
  91. USB_LINK_PHYEN_NO_VBUS_NO_IDGND_8505,
  92. USB_LINK_STD_UPSTREAM_NO_IDGNG_NO_VBUS_8505,
  93. USB_LINK_STD_UPSTREAM_8505,
  94. USB_LINK_CHARGER_SE1_8505,
  95. USB_LINK_CARKIT_CHGR_1_8505,
  96. USB_LINK_CARKIT_CHGR_2_8505,
  97. USB_LINK_ACA_DOCK_CHGR_8505,
  98. USB_LINK_SAMSUNG_BOOT_CBL_PHY_EN_8505,
  99. USB_LINK_SAMSUNG_BOOT_CBL_PHY_DISB_8505,
  100. USB_LINK_SAMSUNG_UART_CBL_PHY_EN_8505,
  101. USB_LINK_SAMSUNG_UART_CBL_PHY_DISB_8505,
  102. USB_LINK_MOTOROLA_FACTORY_CBL_PHY_EN_8505,
  103. };
  104. enum ab8500_usb_mode {
  105. USB_IDLE = 0,
  106. USB_PERIPHERAL,
  107. USB_HOST,
  108. USB_DEDICATED_CHG
  109. };
  110. struct ab8500_usb {
  111. struct usb_phy phy;
  112. struct device *dev;
  113. struct ab8500 *ab8500;
  114. unsigned vbus_draw;
  115. struct delayed_work dwork;
  116. struct work_struct phy_dis_work;
  117. unsigned long link_status_wait;
  118. enum ab8500_usb_mode mode;
  119. struct regulator *v_ape;
  120. struct regulator *v_musb;
  121. struct regulator *v_ulpi;
  122. int previous_link_status_state;
  123. };
  124. static inline struct ab8500_usb *phy_to_ab(struct usb_phy *x)
  125. {
  126. return container_of(x, struct ab8500_usb, phy);
  127. }
  128. static void ab8500_usb_wd_workaround(struct ab8500_usb *ab)
  129. {
  130. abx500_set_register_interruptible(ab->dev,
  131. AB8500_SYS_CTRL2_BLOCK,
  132. AB8500_MAIN_WD_CTRL_REG,
  133. AB8500_BIT_WD_CTRL_ENABLE);
  134. udelay(AB8500_WD_KICK_DELAY_US);
  135. abx500_set_register_interruptible(ab->dev,
  136. AB8500_SYS_CTRL2_BLOCK,
  137. AB8500_MAIN_WD_CTRL_REG,
  138. (AB8500_BIT_WD_CTRL_ENABLE
  139. | AB8500_BIT_WD_CTRL_KICK));
  140. udelay(AB8500_WD_V11_DISABLE_DELAY_US);
  141. abx500_set_register_interruptible(ab->dev,
  142. AB8500_SYS_CTRL2_BLOCK,
  143. AB8500_MAIN_WD_CTRL_REG,
  144. 0);
  145. }
  146. static void ab8500_usb_wd_linkstatus(struct ab8500_usb *ab, u8 bit)
  147. {
  148. /* Workaround for v2.0 bug # 31952 */
  149. if (is_ab8500_2p0(ab->ab8500)) {
  150. abx500_mask_and_set_register_interruptible(ab->dev,
  151. AB8500_USB, AB8500_USB_PHY_CTRL_REG,
  152. bit, bit);
  153. udelay(AB8500_V20_31952_DISABLE_DELAY_US);
  154. }
  155. }
  156. static void ab8500_usb_phy_ctrl(struct ab8500_usb *ab, bool sel_host,
  157. bool enable)
  158. {
  159. u8 ctrl_reg;
  160. abx500_get_register_interruptible(ab->dev,
  161. AB8500_USB,
  162. AB8500_USB_PHY_CTRL_REG,
  163. &ctrl_reg);
  164. if (sel_host) {
  165. if (enable)
  166. ctrl_reg |= AB8500_BIT_PHY_CTRL_HOST_EN;
  167. else
  168. ctrl_reg &= ~AB8500_BIT_PHY_CTRL_HOST_EN;
  169. } else {
  170. if (enable)
  171. ctrl_reg |= AB8500_BIT_PHY_CTRL_DEVICE_EN;
  172. else
  173. ctrl_reg &= ~AB8500_BIT_PHY_CTRL_DEVICE_EN;
  174. }
  175. abx500_set_register_interruptible(ab->dev,
  176. AB8500_USB,
  177. AB8500_USB_PHY_CTRL_REG,
  178. ctrl_reg);
  179. /* Needed to enable the phy.*/
  180. if (enable)
  181. ab8500_usb_wd_workaround(ab);
  182. }
  183. #define ab8500_usb_host_phy_en(ab) ab8500_usb_phy_ctrl(ab, true, true)
  184. #define ab8500_usb_host_phy_dis(ab) ab8500_usb_phy_ctrl(ab, true, false)
  185. #define ab8500_usb_peri_phy_en(ab) ab8500_usb_phy_ctrl(ab, false, true)
  186. #define ab8500_usb_peri_phy_dis(ab) ab8500_usb_phy_ctrl(ab, false, false)
  187. static int ab8505_usb_link_status_update(struct ab8500_usb *ab,
  188. enum ab8505_usb_link_status lsts)
  189. {
  190. enum ux500_musb_vbus_id_status event = 0;
  191. dev_dbg(ab->dev, "ab8505_usb_link_status_update %d\n", lsts);
  192. /*
  193. * Spurious link_status interrupts are seen at the time of
  194. * disconnection of a device in RIDA state
  195. */
  196. if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_8505 &&
  197. (lsts == USB_LINK_STD_HOST_NC_8505))
  198. return 0;
  199. ab->previous_link_status_state = lsts;
  200. switch (lsts) {
  201. case USB_LINK_ACA_RID_B_8505:
  202. event = UX500_MUSB_RIDB;
  203. case USB_LINK_NOT_CONFIGURED_8505:
  204. case USB_LINK_RESERVED0_8505:
  205. case USB_LINK_RESERVED1_8505:
  206. case USB_LINK_RESERVED2_8505:
  207. case USB_LINK_RESERVED3_8505:
  208. ab->mode = USB_IDLE;
  209. ab->phy.otg->default_a = false;
  210. ab->vbus_draw = 0;
  211. if (event != UX500_MUSB_RIDB)
  212. event = UX500_MUSB_NONE;
  213. /*
  214. * Fallback to default B_IDLE as nothing
  215. * is connected
  216. */
  217. ab->phy.state = OTG_STATE_B_IDLE;
  218. break;
  219. case USB_LINK_ACA_RID_C_NM_8505:
  220. event = UX500_MUSB_RIDC;
  221. case USB_LINK_STD_HOST_NC_8505:
  222. case USB_LINK_STD_HOST_C_NS_8505:
  223. case USB_LINK_STD_HOST_C_S_8505:
  224. case USB_LINK_CDP_8505:
  225. if (ab->mode == USB_IDLE) {
  226. ab->mode = USB_PERIPHERAL;
  227. ab8500_usb_peri_phy_en(ab);
  228. atomic_notifier_call_chain(&ab->phy.notifier,
  229. UX500_MUSB_PREPARE, &ab->vbus_draw);
  230. }
  231. if (event != UX500_MUSB_RIDC)
  232. event = UX500_MUSB_VBUS;
  233. break;
  234. case USB_LINK_ACA_RID_A_8505:
  235. case USB_LINK_ACA_DOCK_CHGR_8505:
  236. event = UX500_MUSB_RIDA;
  237. case USB_LINK_HM_IDGND_8505:
  238. if (ab->mode == USB_IDLE) {
  239. ab->mode = USB_HOST;
  240. ab8500_usb_host_phy_en(ab);
  241. atomic_notifier_call_chain(&ab->phy.notifier,
  242. UX500_MUSB_PREPARE, &ab->vbus_draw);
  243. }
  244. ab->phy.otg->default_a = true;
  245. if (event != UX500_MUSB_RIDA)
  246. event = UX500_MUSB_ID;
  247. atomic_notifier_call_chain(&ab->phy.notifier,
  248. event, &ab->vbus_draw);
  249. break;
  250. case USB_LINK_DEDICATED_CHG_8505:
  251. ab->mode = USB_DEDICATED_CHG;
  252. event = UX500_MUSB_CHARGER;
  253. atomic_notifier_call_chain(&ab->phy.notifier,
  254. event, &ab->vbus_draw);
  255. break;
  256. default:
  257. break;
  258. }
  259. return 0;
  260. }
  261. static int ab8500_usb_link_status_update(struct ab8500_usb *ab,
  262. enum ab8500_usb_link_status lsts)
  263. {
  264. enum ux500_musb_vbus_id_status event = 0;
  265. dev_dbg(ab->dev, "ab8500_usb_link_status_update %d\n", lsts);
  266. /*
  267. * Spurious link_status interrupts are seen in case of a
  268. * disconnection of a device in IDGND and RIDA stage
  269. */
  270. if (ab->previous_link_status_state == USB_LINK_HM_IDGND_8500 &&
  271. (lsts == USB_LINK_STD_HOST_C_NS_8500 ||
  272. lsts == USB_LINK_STD_HOST_NC_8500))
  273. return 0;
  274. if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_8500 &&
  275. lsts == USB_LINK_STD_HOST_NC_8500)
  276. return 0;
  277. ab->previous_link_status_state = lsts;
  278. switch (lsts) {
  279. case USB_LINK_ACA_RID_B_8500:
  280. event = UX500_MUSB_RIDB;
  281. case USB_LINK_NOT_CONFIGURED_8500:
  282. case USB_LINK_NOT_VALID_LINK_8500:
  283. ab->mode = USB_IDLE;
  284. ab->phy.otg->default_a = false;
  285. ab->vbus_draw = 0;
  286. if (event != UX500_MUSB_RIDB)
  287. event = UX500_MUSB_NONE;
  288. /* Fallback to default B_IDLE as nothing is connected */
  289. ab->phy.state = OTG_STATE_B_IDLE;
  290. break;
  291. case USB_LINK_ACA_RID_C_NM_8500:
  292. case USB_LINK_ACA_RID_C_HS_8500:
  293. case USB_LINK_ACA_RID_C_HS_CHIRP_8500:
  294. event = UX500_MUSB_RIDC;
  295. case USB_LINK_STD_HOST_NC_8500:
  296. case USB_LINK_STD_HOST_C_NS_8500:
  297. case USB_LINK_STD_HOST_C_S_8500:
  298. case USB_LINK_HOST_CHG_NM_8500:
  299. case USB_LINK_HOST_CHG_HS_8500:
  300. case USB_LINK_HOST_CHG_HS_CHIRP_8500:
  301. if (ab->mode == USB_IDLE) {
  302. ab->mode = USB_PERIPHERAL;
  303. ab8500_usb_peri_phy_en(ab);
  304. atomic_notifier_call_chain(&ab->phy.notifier,
  305. UX500_MUSB_PREPARE, &ab->vbus_draw);
  306. }
  307. if (event != UX500_MUSB_RIDC)
  308. event = UX500_MUSB_VBUS;
  309. break;
  310. case USB_LINK_ACA_RID_A_8500:
  311. event = UX500_MUSB_RIDA;
  312. case USB_LINK_HM_IDGND_8500:
  313. if (ab->mode == USB_IDLE) {
  314. ab->mode = USB_HOST;
  315. ab8500_usb_host_phy_en(ab);
  316. atomic_notifier_call_chain(&ab->phy.notifier,
  317. UX500_MUSB_PREPARE, &ab->vbus_draw);
  318. }
  319. ab->phy.otg->default_a = true;
  320. if (event != UX500_MUSB_RIDA)
  321. event = UX500_MUSB_ID;
  322. atomic_notifier_call_chain(&ab->phy.notifier,
  323. event, &ab->vbus_draw);
  324. break;
  325. case USB_LINK_DEDICATED_CHG_8500:
  326. ab->mode = USB_DEDICATED_CHG;
  327. event = UX500_MUSB_CHARGER;
  328. atomic_notifier_call_chain(&ab->phy.notifier,
  329. event, &ab->vbus_draw);
  330. break;
  331. case USB_LINK_RESERVED_8500:
  332. break;
  333. }
  334. return 0;
  335. }
  336. /*
  337. * Connection Sequence:
  338. * 1. Link Status Interrupt
  339. * 2. Enable AB clock
  340. * 3. Enable AB regulators
  341. * 4. Enable USB phy
  342. * 5. Reset the musb controller
  343. * 6. Switch the ULPI GPIO pins to fucntion mode
  344. * 7. Enable the musb Peripheral5 clock
  345. * 8. Restore MUSB context
  346. */
  347. static int abx500_usb_link_status_update(struct ab8500_usb *ab)
  348. {
  349. u8 reg;
  350. int ret = 0;
  351. if (is_ab8500(ab->ab8500)) {
  352. enum ab8500_usb_link_status lsts;
  353. abx500_get_register_interruptible(ab->dev,
  354. AB8500_USB, AB8500_USB_LINE_STAT_REG, &reg);
  355. lsts = (reg >> 3) & 0x0F;
  356. ret = ab8500_usb_link_status_update(ab, lsts);
  357. } else if (is_ab8505(ab->ab8500)) {
  358. enum ab8505_usb_link_status lsts;
  359. abx500_get_register_interruptible(ab->dev,
  360. AB8500_USB, AB8505_USB_LINE_STAT_REG, &reg);
  361. lsts = (reg >> 3) & 0x1F;
  362. ret = ab8505_usb_link_status_update(ab, lsts);
  363. }
  364. return ret;
  365. }
  366. /*
  367. * Disconnection Sequence:
  368. * 1. Disconect Interrupt
  369. * 2. Disable regulators
  370. * 3. Disable AB clock
  371. * 4. Disable the Phy
  372. * 5. Link Status Interrupt
  373. * 6. Disable Musb Clock
  374. */
  375. static irqreturn_t ab8500_usb_disconnect_irq(int irq, void *data)
  376. {
  377. struct ab8500_usb *ab = (struct ab8500_usb *) data;
  378. enum usb_phy_events event = UX500_MUSB_NONE;
  379. /* Link status will not be updated till phy is disabled. */
  380. if (ab->mode == USB_HOST) {
  381. ab->phy.otg->default_a = false;
  382. ab->vbus_draw = 0;
  383. atomic_notifier_call_chain(&ab->phy.notifier,
  384. event, &ab->vbus_draw);
  385. ab8500_usb_host_phy_dis(ab);
  386. ab->mode = USB_IDLE;
  387. }
  388. if (ab->mode == USB_PERIPHERAL) {
  389. atomic_notifier_call_chain(&ab->phy.notifier,
  390. event, &ab->vbus_draw);
  391. ab8500_usb_peri_phy_dis(ab);
  392. atomic_notifier_call_chain(&ab->phy.notifier,
  393. UX500_MUSB_CLEAN, &ab->vbus_draw);
  394. ab->mode = USB_IDLE;
  395. ab->phy.otg->default_a = false;
  396. ab->vbus_draw = 0;
  397. }
  398. if (is_ab8500_2p0(ab->ab8500)) {
  399. if (ab->mode == USB_DEDICATED_CHG) {
  400. ab8500_usb_wd_linkstatus(ab,
  401. AB8500_BIT_PHY_CTRL_DEVICE_EN);
  402. abx500_mask_and_set_register_interruptible(ab->dev,
  403. AB8500_USB, AB8500_USB_PHY_CTRL_REG,
  404. AB8500_BIT_PHY_CTRL_DEVICE_EN, 0);
  405. }
  406. }
  407. return IRQ_HANDLED;
  408. }
  409. static irqreturn_t ab8500_usb_link_status_irq(int irq, void *data)
  410. {
  411. struct ab8500_usb *ab = (struct ab8500_usb *) data;
  412. abx500_usb_link_status_update(ab);
  413. return IRQ_HANDLED;
  414. }
  415. static void ab8500_usb_delayed_work(struct work_struct *work)
  416. {
  417. struct ab8500_usb *ab = container_of(work, struct ab8500_usb,
  418. dwork.work);
  419. abx500_usb_link_status_update(ab);
  420. }
  421. static void ab8500_usb_phy_disable_work(struct work_struct *work)
  422. {
  423. struct ab8500_usb *ab = container_of(work, struct ab8500_usb,
  424. phy_dis_work);
  425. if (!ab->phy.otg->host)
  426. ab8500_usb_host_phy_dis(ab);
  427. if (!ab->phy.otg->gadget)
  428. ab8500_usb_peri_phy_dis(ab);
  429. }
  430. static unsigned ab8500_eyediagram_workaroud(struct ab8500_usb *ab, unsigned mA)
  431. {
  432. /*
  433. * AB8500 V2 has eye diagram issues when drawing more than 100mA from
  434. * VBUS. Set charging current to 100mA in case of standard host
  435. */
  436. if (is_ab8500_2p0_or_earlier(ab->ab8500))
  437. if (mA > 100)
  438. mA = 100;
  439. return mA;
  440. }
  441. static int ab8500_usb_set_power(struct usb_phy *phy, unsigned mA)
  442. {
  443. struct ab8500_usb *ab;
  444. if (!phy)
  445. return -ENODEV;
  446. ab = phy_to_ab(phy);
  447. mA = ab8500_eyediagram_workaroud(ab, mA);
  448. ab->vbus_draw = mA;
  449. if (mA)
  450. atomic_notifier_call_chain(&ab->phy.notifier,
  451. UX500_MUSB_ENUMERATED, ab->phy.otg->gadget);
  452. return 0;
  453. }
  454. /* TODO: Implement some way for charging or other drivers to read
  455. * ab->vbus_draw.
  456. */
  457. static int ab8500_usb_set_suspend(struct usb_phy *x, int suspend)
  458. {
  459. /* TODO */
  460. return 0;
  461. }
  462. static int ab8500_usb_set_peripheral(struct usb_otg *otg,
  463. struct usb_gadget *gadget)
  464. {
  465. struct ab8500_usb *ab;
  466. if (!otg)
  467. return -ENODEV;
  468. ab = phy_to_ab(otg->phy);
  469. /* Some drivers call this function in atomic context.
  470. * Do not update ab8500 registers directly till this
  471. * is fixed.
  472. */
  473. if (!gadget) {
  474. /* TODO: Disable regulators. */
  475. otg->gadget = NULL;
  476. schedule_work(&ab->phy_dis_work);
  477. } else {
  478. otg->gadget = gadget;
  479. otg->phy->state = OTG_STATE_B_IDLE;
  480. /* Phy will not be enabled if cable is already
  481. * plugged-in. Schedule to enable phy.
  482. * Use same delay to avoid any race condition.
  483. */
  484. schedule_delayed_work(&ab->dwork, ab->link_status_wait);
  485. }
  486. return 0;
  487. }
  488. static int ab8500_usb_set_host(struct usb_otg *otg, struct usb_bus *host)
  489. {
  490. struct ab8500_usb *ab;
  491. if (!otg)
  492. return -ENODEV;
  493. ab = phy_to_ab(otg->phy);
  494. /* Some drivers call this function in atomic context.
  495. * Do not update ab8500 registers directly till this
  496. * is fixed.
  497. */
  498. if (!host) {
  499. /* TODO: Disable regulators. */
  500. otg->host = NULL;
  501. schedule_work(&ab->phy_dis_work);
  502. } else {
  503. otg->host = host;
  504. /* Phy will not be enabled if cable is already
  505. * plugged-in. Schedule to enable phy.
  506. * Use same delay to avoid any race condition.
  507. */
  508. schedule_delayed_work(&ab->dwork, ab->link_status_wait);
  509. }
  510. return 0;
  511. }
  512. static int ab8500_usb_regulator_get(struct ab8500_usb *ab)
  513. {
  514. int err;
  515. ab->v_ape = devm_regulator_get(ab->dev, "v-ape");
  516. if (IS_ERR(ab->v_ape)) {
  517. dev_err(ab->dev, "Could not get v-ape supply\n");
  518. err = PTR_ERR(ab->v_ape);
  519. return err;
  520. }
  521. ab->v_ulpi = devm_regulator_get(ab->dev, "vddulpivio18");
  522. if (IS_ERR(ab->v_ulpi)) {
  523. dev_err(ab->dev, "Could not get vddulpivio18 supply\n");
  524. err = PTR_ERR(ab->v_ulpi);
  525. return err;
  526. }
  527. ab->v_musb = devm_regulator_get(ab->dev, "musb_1v8");
  528. if (IS_ERR(ab->v_musb)) {
  529. dev_err(ab->dev, "Could not get musb_1v8 supply\n");
  530. err = PTR_ERR(ab->v_musb);
  531. return err;
  532. }
  533. return 0;
  534. }
  535. static int ab8500_usb_irq_setup(struct platform_device *pdev,
  536. struct ab8500_usb *ab)
  537. {
  538. int err;
  539. int irq;
  540. irq = platform_get_irq_byname(pdev, "USB_LINK_STATUS");
  541. if (irq < 0) {
  542. dev_err(&pdev->dev, "Link status irq not found\n");
  543. return irq;
  544. }
  545. err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  546. ab8500_usb_link_status_irq,
  547. IRQF_NO_SUSPEND | IRQF_SHARED, "usb-link-status", ab);
  548. if (err < 0) {
  549. dev_err(ab->dev, "request_irq failed for link status irq\n");
  550. return err;
  551. }
  552. irq = platform_get_irq_byname(pdev, "ID_WAKEUP_F");
  553. if (irq < 0) {
  554. dev_err(&pdev->dev, "ID fall irq not found\n");
  555. return irq;
  556. }
  557. err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  558. ab8500_usb_disconnect_irq,
  559. IRQF_NO_SUSPEND | IRQF_SHARED, "usb-id-fall", ab);
  560. if (err < 0) {
  561. dev_err(ab->dev, "request_irq failed for ID fall irq\n");
  562. return err;
  563. }
  564. irq = platform_get_irq_byname(pdev, "VBUS_DET_F");
  565. if (irq < 0) {
  566. dev_err(&pdev->dev, "VBUS fall irq not found\n");
  567. return irq;
  568. }
  569. err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  570. ab8500_usb_disconnect_irq,
  571. IRQF_NO_SUSPEND | IRQF_SHARED, "usb-vbus-fall", ab);
  572. if (err < 0) {
  573. dev_err(ab->dev, "request_irq failed for Vbus fall irq\n");
  574. return err;
  575. }
  576. return 0;
  577. }
  578. static int ab8500_usb_probe(struct platform_device *pdev)
  579. {
  580. struct ab8500_usb *ab;
  581. struct ab8500 *ab8500;
  582. struct usb_otg *otg;
  583. int err;
  584. int rev;
  585. ab8500 = dev_get_drvdata(pdev->dev.parent);
  586. rev = abx500_get_chip_id(&pdev->dev);
  587. if (is_ab8500_1p1_or_earlier(ab8500)) {
  588. dev_err(&pdev->dev, "Unsupported AB8500 chip rev=%d\n", rev);
  589. return -ENODEV;
  590. }
  591. ab = devm_kzalloc(&pdev->dev, sizeof(*ab), GFP_KERNEL);
  592. if (!ab)
  593. return -ENOMEM;
  594. otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
  595. if (!otg)
  596. return -ENOMEM;
  597. ab->dev = &pdev->dev;
  598. ab->ab8500 = ab8500;
  599. ab->phy.dev = ab->dev;
  600. ab->phy.otg = otg;
  601. ab->phy.label = "ab8500";
  602. ab->phy.set_suspend = ab8500_usb_set_suspend;
  603. ab->phy.set_power = ab8500_usb_set_power;
  604. ab->phy.state = OTG_STATE_UNDEFINED;
  605. otg->phy = &ab->phy;
  606. otg->set_host = ab8500_usb_set_host;
  607. otg->set_peripheral = ab8500_usb_set_peripheral;
  608. platform_set_drvdata(pdev, ab);
  609. ATOMIC_INIT_NOTIFIER_HEAD(&ab->phy.notifier);
  610. /* v1: Wait for link status to become stable.
  611. * all: Updates form set_host and set_peripheral as they are atomic.
  612. */
  613. INIT_DELAYED_WORK(&ab->dwork, ab8500_usb_delayed_work);
  614. /* all: Disable phy when called from set_host and set_peripheral */
  615. INIT_WORK(&ab->phy_dis_work, ab8500_usb_phy_disable_work);
  616. err = ab8500_usb_regulator_get(ab);
  617. if (err)
  618. return err;
  619. err = ab8500_usb_irq_setup(pdev, ab);
  620. if (err < 0)
  621. return err;
  622. err = usb_add_phy(&ab->phy, USB_PHY_TYPE_USB2);
  623. if (err) {
  624. dev_err(&pdev->dev, "Can't register transceiver\n");
  625. return err;
  626. }
  627. /* Phy tuning values for AB8500 */
  628. if (!is_ab8500_2p0_or_earlier(ab->ab8500)) {
  629. /* Enable the PBT/Bank 0x12 access */
  630. err = abx500_set_register_interruptible(ab->dev,
  631. AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x01);
  632. if (err < 0)
  633. dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
  634. err);
  635. err = abx500_set_register_interruptible(ab->dev,
  636. AB8500_DEBUG, AB8500_USB_PHY_TUNE1, 0xC8);
  637. if (err < 0)
  638. dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
  639. err);
  640. err = abx500_set_register_interruptible(ab->dev,
  641. AB8500_DEBUG, AB8500_USB_PHY_TUNE2, 0x00);
  642. if (err < 0)
  643. dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
  644. err);
  645. err = abx500_set_register_interruptible(ab->dev,
  646. AB8500_DEBUG, AB8500_USB_PHY_TUNE3, 0x78);
  647. if (err < 0)
  648. dev_err(ab->dev, "Failed to set PHY_TUNE3 regester err=%d\n",
  649. err);
  650. /* Switch to normal mode/disable Bank 0x12 access */
  651. err = abx500_set_register_interruptible(ab->dev,
  652. AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x00);
  653. if (err < 0)
  654. dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
  655. err);
  656. }
  657. /* Phy tuning values for AB8505 */
  658. if (is_ab8505(ab->ab8500)) {
  659. /* Enable the PBT/Bank 0x12 access */
  660. err = abx500_mask_and_set_register_interruptible(ab->dev,
  661. AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS,
  662. 0x01, 0x01);
  663. if (err < 0)
  664. dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
  665. err);
  666. err = abx500_mask_and_set_register_interruptible(ab->dev,
  667. AB8500_DEBUG, AB8500_USB_PHY_TUNE1,
  668. 0xC8, 0xC8);
  669. if (err < 0)
  670. dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
  671. err);
  672. err = abx500_mask_and_set_register_interruptible(ab->dev,
  673. AB8500_DEBUG, AB8500_USB_PHY_TUNE2,
  674. 0x60, 0x60);
  675. if (err < 0)
  676. dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
  677. err);
  678. err = abx500_mask_and_set_register_interruptible(ab->dev,
  679. AB8500_DEBUG, AB8500_USB_PHY_TUNE3,
  680. 0xFC, 0x80);
  681. if (err < 0)
  682. dev_err(ab->dev, "Failed to set PHY_TUNE3 regester err=%d\n",
  683. err);
  684. /* Switch to normal mode/disable Bank 0x12 access */
  685. err = abx500_mask_and_set_register_interruptible(ab->dev,
  686. AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS,
  687. 0x00, 0x00);
  688. if (err < 0)
  689. dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
  690. err);
  691. }
  692. /* Needed to enable ID detection. */
  693. ab8500_usb_wd_workaround(ab);
  694. dev_info(&pdev->dev, "revision 0x%2x driver initialized\n", rev);
  695. return 0;
  696. }
  697. static int ab8500_usb_remove(struct platform_device *pdev)
  698. {
  699. struct ab8500_usb *ab = platform_get_drvdata(pdev);
  700. cancel_delayed_work_sync(&ab->dwork);
  701. cancel_work_sync(&ab->phy_dis_work);
  702. usb_remove_phy(&ab->phy);
  703. ab8500_usb_host_phy_dis(ab);
  704. ab8500_usb_peri_phy_dis(ab);
  705. platform_set_drvdata(pdev, NULL);
  706. return 0;
  707. }
  708. static struct platform_driver ab8500_usb_driver = {
  709. .probe = ab8500_usb_probe,
  710. .remove = ab8500_usb_remove,
  711. .driver = {
  712. .name = "ab8500-usb",
  713. .owner = THIS_MODULE,
  714. },
  715. };
  716. static int __init ab8500_usb_init(void)
  717. {
  718. return platform_driver_register(&ab8500_usb_driver);
  719. }
  720. subsys_initcall(ab8500_usb_init);
  721. static void __exit ab8500_usb_exit(void)
  722. {
  723. platform_driver_unregister(&ab8500_usb_driver);
  724. }
  725. module_exit(ab8500_usb_exit);
  726. MODULE_ALIAS("platform:ab8500_usb");
  727. MODULE_AUTHOR("ST-Ericsson AB");
  728. MODULE_DESCRIPTION("AB8500 usb transceiver driver");
  729. MODULE_LICENSE("GPL");