phy-ab8500-usb.c 24 KB

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