phy-ab8500-usb.c 21 KB

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