phy-ab8500-usb.c 25 KB

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