phy-ab8500-usb.c 25 KB

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