phy-ab8500-usb.c 26 KB

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