phy-ab8500-usb.c 26 KB

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