phy-ab8500-usb.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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. ret = abx500_get_register_interruptible(ab->dev,
  436. AB8500_USB, AB8500_USB_LINE_STAT_REG, &reg);
  437. if (ret < 0)
  438. return ret;
  439. lsts = (reg >> 3) & 0x0F;
  440. ret = ab8500_usb_link_status_update(ab, lsts);
  441. } else if (is_ab8505(ab->ab8500)) {
  442. enum ab8505_usb_link_status lsts;
  443. ret = abx500_get_register_interruptible(ab->dev,
  444. AB8500_USB, AB8505_USB_LINE_STAT_REG, &reg);
  445. if (ret < 0)
  446. return ret;
  447. lsts = (reg >> 3) & 0x1F;
  448. ret = ab8505_usb_link_status_update(ab, lsts);
  449. }
  450. return ret;
  451. }
  452. /*
  453. * Disconnection Sequence:
  454. * 1. Disconnect Interrupt
  455. * 2. Disable regulators
  456. * 3. Disable AB clock
  457. * 4. Disable the Phy
  458. * 5. Link Status Interrupt
  459. * 6. Disable Musb Clock
  460. */
  461. static irqreturn_t ab8500_usb_disconnect_irq(int irq, void *data)
  462. {
  463. struct ab8500_usb *ab = (struct ab8500_usb *) data;
  464. enum usb_phy_events event = USB_EVENT_NONE;
  465. /* Link status will not be updated till phy is disabled. */
  466. if (ab->mode == USB_HOST) {
  467. ab->phy.otg->default_a = false;
  468. ab->vbus_draw = 0;
  469. atomic_notifier_call_chain(&ab->phy.notifier,
  470. event, &ab->vbus_draw);
  471. ab8500_usb_host_phy_dis(ab);
  472. ab->mode = USB_IDLE;
  473. }
  474. if (ab->mode == USB_PERIPHERAL) {
  475. atomic_notifier_call_chain(&ab->phy.notifier,
  476. event, &ab->vbus_draw);
  477. ab8500_usb_peri_phy_dis(ab);
  478. atomic_notifier_call_chain(&ab->phy.notifier,
  479. UX500_MUSB_CLEAN, &ab->vbus_draw);
  480. ab->mode = USB_IDLE;
  481. ab->phy.otg->default_a = false;
  482. ab->vbus_draw = 0;
  483. }
  484. if (is_ab8500_2p0(ab->ab8500)) {
  485. if (ab->mode == USB_DEDICATED_CHG) {
  486. ab8500_usb_wd_linkstatus(ab,
  487. AB8500_BIT_PHY_CTRL_DEVICE_EN);
  488. abx500_mask_and_set_register_interruptible(ab->dev,
  489. AB8500_USB, AB8500_USB_PHY_CTRL_REG,
  490. AB8500_BIT_PHY_CTRL_DEVICE_EN, 0);
  491. }
  492. }
  493. return IRQ_HANDLED;
  494. }
  495. static irqreturn_t ab8500_usb_link_status_irq(int irq, void *data)
  496. {
  497. struct ab8500_usb *ab = (struct ab8500_usb *)data;
  498. abx500_usb_link_status_update(ab);
  499. return IRQ_HANDLED;
  500. }
  501. static void ab8500_usb_phy_disable_work(struct work_struct *work)
  502. {
  503. struct ab8500_usb *ab = container_of(work, struct ab8500_usb,
  504. phy_dis_work);
  505. if (!ab->phy.otg->host)
  506. ab8500_usb_host_phy_dis(ab);
  507. if (!ab->phy.otg->gadget)
  508. ab8500_usb_peri_phy_dis(ab);
  509. }
  510. static int ab8500_usb_set_suspend(struct usb_phy *x, int suspend)
  511. {
  512. /* TODO */
  513. return 0;
  514. }
  515. static int ab8500_usb_set_peripheral(struct usb_otg *otg,
  516. struct usb_gadget *gadget)
  517. {
  518. struct ab8500_usb *ab;
  519. if (!otg)
  520. return -ENODEV;
  521. ab = phy_to_ab(otg->usb_phy);
  522. ab->phy.otg->gadget = gadget;
  523. /* Some drivers call this function in atomic context.
  524. * Do not update ab8500 registers directly till this
  525. * is fixed.
  526. */
  527. if ((ab->mode != USB_IDLE) && !gadget) {
  528. ab->mode = USB_IDLE;
  529. schedule_work(&ab->phy_dis_work);
  530. }
  531. return 0;
  532. }
  533. static int ab8500_usb_set_host(struct usb_otg *otg, struct usb_bus *host)
  534. {
  535. struct ab8500_usb *ab;
  536. if (!otg)
  537. return -ENODEV;
  538. ab = phy_to_ab(otg->usb_phy);
  539. ab->phy.otg->host = host;
  540. /* Some drivers call this function in atomic context.
  541. * Do not update ab8500 registers directly till this
  542. * is fixed.
  543. */
  544. if ((ab->mode != USB_IDLE) && !host) {
  545. ab->mode = USB_IDLE;
  546. schedule_work(&ab->phy_dis_work);
  547. }
  548. return 0;
  549. }
  550. static void ab8500_usb_restart_phy(struct ab8500_usb *ab)
  551. {
  552. abx500_mask_and_set_register_interruptible(ab->dev,
  553. AB8500_USB, AB8500_USB_PHY_CTRL_REG,
  554. AB8500_BIT_PHY_CTRL_DEVICE_EN,
  555. AB8500_BIT_PHY_CTRL_DEVICE_EN);
  556. udelay(100);
  557. abx500_mask_and_set_register_interruptible(ab->dev,
  558. AB8500_USB, AB8500_USB_PHY_CTRL_REG,
  559. AB8500_BIT_PHY_CTRL_DEVICE_EN,
  560. 0);
  561. abx500_mask_and_set_register_interruptible(ab->dev,
  562. AB8500_USB, AB8500_USB_PHY_CTRL_REG,
  563. AB8500_BIT_PHY_CTRL_HOST_EN,
  564. AB8500_BIT_PHY_CTRL_HOST_EN);
  565. udelay(100);
  566. abx500_mask_and_set_register_interruptible(ab->dev,
  567. AB8500_USB, AB8500_USB_PHY_CTRL_REG,
  568. AB8500_BIT_PHY_CTRL_HOST_EN,
  569. 0);
  570. }
  571. static int ab8500_usb_regulator_get(struct ab8500_usb *ab)
  572. {
  573. int err;
  574. ab->v_ape = devm_regulator_get(ab->dev, "v-ape");
  575. if (IS_ERR(ab->v_ape)) {
  576. dev_err(ab->dev, "Could not get v-ape supply\n");
  577. err = PTR_ERR(ab->v_ape);
  578. return err;
  579. }
  580. ab->v_ulpi = devm_regulator_get(ab->dev, "vddulpivio18");
  581. if (IS_ERR(ab->v_ulpi)) {
  582. dev_err(ab->dev, "Could not get vddulpivio18 supply\n");
  583. err = PTR_ERR(ab->v_ulpi);
  584. return err;
  585. }
  586. ab->v_musb = devm_regulator_get(ab->dev, "musb_1v8");
  587. if (IS_ERR(ab->v_musb)) {
  588. dev_err(ab->dev, "Could not get musb_1v8 supply\n");
  589. err = PTR_ERR(ab->v_musb);
  590. return err;
  591. }
  592. return 0;
  593. }
  594. static int ab8500_usb_irq_setup(struct platform_device *pdev,
  595. struct ab8500_usb *ab)
  596. {
  597. int err;
  598. int irq;
  599. if (ab->flags & AB8500_USB_FLAG_USE_LINK_STATUS_IRQ) {
  600. irq = platform_get_irq_byname(pdev, "USB_LINK_STATUS");
  601. if (irq < 0) {
  602. dev_err(&pdev->dev, "Link status irq not found\n");
  603. return irq;
  604. }
  605. err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  606. ab8500_usb_link_status_irq,
  607. IRQF_NO_SUSPEND | IRQF_SHARED | IRQF_ONESHOT,
  608. "usb-link-status", ab);
  609. if (err < 0) {
  610. dev_err(ab->dev, "request_irq failed for link status irq\n");
  611. return err;
  612. }
  613. }
  614. if (ab->flags & AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ) {
  615. irq = platform_get_irq_byname(pdev, "ID_WAKEUP_F");
  616. if (irq < 0) {
  617. dev_err(&pdev->dev, "ID fall irq not found\n");
  618. return irq;
  619. }
  620. err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  621. ab8500_usb_disconnect_irq,
  622. IRQF_NO_SUSPEND | IRQF_SHARED | IRQF_ONESHOT,
  623. "usb-id-fall", ab);
  624. if (err < 0) {
  625. dev_err(ab->dev, "request_irq failed for ID fall irq\n");
  626. return err;
  627. }
  628. }
  629. if (ab->flags & AB8500_USB_FLAG_USE_VBUS_DET_IRQ) {
  630. irq = platform_get_irq_byname(pdev, "VBUS_DET_F");
  631. if (irq < 0) {
  632. dev_err(&pdev->dev, "VBUS fall irq not found\n");
  633. return irq;
  634. }
  635. err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  636. ab8500_usb_disconnect_irq,
  637. IRQF_NO_SUSPEND | IRQF_SHARED | IRQF_ONESHOT,
  638. "usb-vbus-fall", ab);
  639. if (err < 0) {
  640. dev_err(ab->dev, "request_irq failed for Vbus fall irq\n");
  641. return err;
  642. }
  643. }
  644. return 0;
  645. }
  646. static void ab8500_usb_set_ab8500_tuning_values(struct ab8500_usb *ab)
  647. {
  648. int err;
  649. /* Enable the PBT/Bank 0x12 access */
  650. err = abx500_set_register_interruptible(ab->dev,
  651. AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x01);
  652. if (err < 0)
  653. dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
  654. err);
  655. err = abx500_set_register_interruptible(ab->dev,
  656. AB8500_DEBUG, AB8500_USB_PHY_TUNE1, 0xC8);
  657. if (err < 0)
  658. dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
  659. err);
  660. err = abx500_set_register_interruptible(ab->dev,
  661. AB8500_DEBUG, AB8500_USB_PHY_TUNE2, 0x00);
  662. if (err < 0)
  663. dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
  664. err);
  665. err = abx500_set_register_interruptible(ab->dev,
  666. AB8500_DEBUG, AB8500_USB_PHY_TUNE3, 0x78);
  667. if (err < 0)
  668. dev_err(ab->dev, "Failed to set PHY_TUNE3 register err=%d\n",
  669. err);
  670. /* Switch to normal mode/disable Bank 0x12 access */
  671. err = abx500_set_register_interruptible(ab->dev,
  672. AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x00);
  673. if (err < 0)
  674. dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
  675. err);
  676. }
  677. static void ab8500_usb_set_ab8505_tuning_values(struct ab8500_usb *ab)
  678. {
  679. int err;
  680. /* Enable the PBT/Bank 0x12 access */
  681. err = abx500_mask_and_set_register_interruptible(ab->dev,
  682. AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS,
  683. 0x01, 0x01);
  684. if (err < 0)
  685. dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
  686. err);
  687. err = abx500_mask_and_set_register_interruptible(ab->dev,
  688. AB8500_DEBUG, AB8500_USB_PHY_TUNE1,
  689. 0xC8, 0xC8);
  690. if (err < 0)
  691. dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
  692. err);
  693. err = abx500_mask_and_set_register_interruptible(ab->dev,
  694. AB8500_DEBUG, AB8500_USB_PHY_TUNE2,
  695. 0x60, 0x60);
  696. if (err < 0)
  697. dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
  698. err);
  699. err = abx500_mask_and_set_register_interruptible(ab->dev,
  700. AB8500_DEBUG, AB8500_USB_PHY_TUNE3,
  701. 0xFC, 0x80);
  702. if (err < 0)
  703. dev_err(ab->dev, "Failed to set PHY_TUNE3 register err=%d\n",
  704. err);
  705. /* Switch to normal mode/disable Bank 0x12 access */
  706. err = abx500_mask_and_set_register_interruptible(ab->dev,
  707. AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS,
  708. 0x00, 0x00);
  709. if (err < 0)
  710. dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
  711. err);
  712. }
  713. static int ab8500_usb_probe(struct platform_device *pdev)
  714. {
  715. struct ab8500_usb *ab;
  716. struct ab8500 *ab8500;
  717. struct usb_otg *otg;
  718. int err;
  719. int rev;
  720. ab8500 = dev_get_drvdata(pdev->dev.parent);
  721. rev = abx500_get_chip_id(&pdev->dev);
  722. if (is_ab8500_1p1_or_earlier(ab8500)) {
  723. dev_err(&pdev->dev, "Unsupported AB8500 chip rev=%d\n", rev);
  724. return -ENODEV;
  725. }
  726. ab = devm_kzalloc(&pdev->dev, sizeof(*ab), GFP_KERNEL);
  727. if (!ab)
  728. return -ENOMEM;
  729. otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
  730. if (!otg)
  731. return -ENOMEM;
  732. ab->dev = &pdev->dev;
  733. ab->ab8500 = ab8500;
  734. ab->phy.dev = ab->dev;
  735. ab->phy.otg = otg;
  736. ab->phy.label = "ab8500";
  737. ab->phy.set_suspend = ab8500_usb_set_suspend;
  738. ab->phy.otg->state = OTG_STATE_UNDEFINED;
  739. otg->usb_phy = &ab->phy;
  740. otg->set_host = ab8500_usb_set_host;
  741. otg->set_peripheral = ab8500_usb_set_peripheral;
  742. if (is_ab8500(ab->ab8500)) {
  743. ab->flags |= AB8500_USB_FLAG_USE_LINK_STATUS_IRQ |
  744. AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ |
  745. AB8500_USB_FLAG_USE_VBUS_DET_IRQ |
  746. AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE;
  747. } else if (is_ab8505(ab->ab8500)) {
  748. ab->flags |= AB8500_USB_FLAG_USE_LINK_STATUS_IRQ |
  749. AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ |
  750. AB8500_USB_FLAG_USE_VBUS_DET_IRQ |
  751. AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE;
  752. }
  753. /* Disable regulator voltage setting for AB8500 <= v2.0 */
  754. if (is_ab8500_2p0_or_earlier(ab->ab8500))
  755. ab->flags &= ~AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE;
  756. platform_set_drvdata(pdev, ab);
  757. /* all: Disable phy when called from set_host and set_peripheral */
  758. INIT_WORK(&ab->phy_dis_work, ab8500_usb_phy_disable_work);
  759. err = ab8500_usb_regulator_get(ab);
  760. if (err)
  761. return err;
  762. ab->sysclk = devm_clk_get(ab->dev, "sysclk");
  763. if (IS_ERR(ab->sysclk)) {
  764. dev_err(ab->dev, "Could not get sysclk.\n");
  765. return PTR_ERR(ab->sysclk);
  766. }
  767. err = ab8500_usb_irq_setup(pdev, ab);
  768. if (err < 0)
  769. return err;
  770. err = usb_add_phy(&ab->phy, USB_PHY_TYPE_USB2);
  771. if (err) {
  772. dev_err(&pdev->dev, "Can't register transceiver\n");
  773. return err;
  774. }
  775. if (is_ab8500(ab->ab8500) && !is_ab8500_2p0_or_earlier(ab->ab8500))
  776. /* Phy tuning values for AB8500 > v2.0 */
  777. ab8500_usb_set_ab8500_tuning_values(ab);
  778. else if (is_ab8505(ab->ab8500))
  779. /* Phy tuning values for AB8505 */
  780. ab8500_usb_set_ab8505_tuning_values(ab);
  781. /* Needed to enable ID detection. */
  782. ab8500_usb_wd_workaround(ab);
  783. /*
  784. * This is required for usb-link-status to work properly when a
  785. * cable is connected at boot time.
  786. */
  787. ab8500_usb_restart_phy(ab);
  788. abx500_usb_link_status_update(ab);
  789. dev_info(&pdev->dev, "revision 0x%2x driver initialized\n", rev);
  790. return 0;
  791. }
  792. static int ab8500_usb_remove(struct platform_device *pdev)
  793. {
  794. struct ab8500_usb *ab = platform_get_drvdata(pdev);
  795. cancel_work_sync(&ab->phy_dis_work);
  796. usb_remove_phy(&ab->phy);
  797. if (ab->mode == USB_HOST)
  798. ab8500_usb_host_phy_dis(ab);
  799. else if (ab->mode == USB_PERIPHERAL)
  800. ab8500_usb_peri_phy_dis(ab);
  801. return 0;
  802. }
  803. static const struct platform_device_id ab8500_usb_devtype[] = {
  804. { .name = "ab8500-usb", },
  805. { /* sentinel */ }
  806. };
  807. MODULE_DEVICE_TABLE(platform, ab8500_usb_devtype);
  808. static struct platform_driver ab8500_usb_driver = {
  809. .probe = ab8500_usb_probe,
  810. .remove = ab8500_usb_remove,
  811. .id_table = ab8500_usb_devtype,
  812. .driver = {
  813. .name = "abx5x0-usb",
  814. },
  815. };
  816. static int __init ab8500_usb_init(void)
  817. {
  818. return platform_driver_register(&ab8500_usb_driver);
  819. }
  820. subsys_initcall(ab8500_usb_init);
  821. static void __exit ab8500_usb_exit(void)
  822. {
  823. platform_driver_unregister(&ab8500_usb_driver);
  824. }
  825. module_exit(ab8500_usb_exit);
  826. MODULE_AUTHOR("ST-Ericsson AB");
  827. MODULE_DESCRIPTION("AB8500 family usb transceiver driver");
  828. MODULE_LICENSE("GPL");