phy-ab8500-usb.c 18 KB

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