phy-msm-qusb.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2014-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/module.h>
  7. #include <linux/kernel.h>
  8. #include <linux/err.h>
  9. #include <linux/slab.h>
  10. #include <linux/clk.h>
  11. #include <linux/delay.h>
  12. #include <linux/io.h>
  13. #include <linux/of.h>
  14. #include <linux/extcon.h>
  15. #include <linux/extcon-provider.h>
  16. #include <linux/debugfs.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/power_supply.h>
  19. #include <linux/qcom_scm.h>
  20. #include <linux/arm-smccc.h>
  21. #include <linux/regulator/consumer.h>
  22. #include <linux/regulator/driver.h>
  23. #include <linux/regulator/machine.h>
  24. #include <linux/usb/phy.h>
  25. #include <linux/usb/dwc3-msm.h>
  26. #include <linux/reset.h>
  27. #define QUSB2PHY_PLL_PWR_CTL 0x18
  28. #define REF_BUF_EN BIT(0)
  29. #define REXT_EN BIT(1)
  30. #define PLL_BYPASSNL BIT(2)
  31. #define REXT_TRIM_0 BIT(4)
  32. #define QUSB2PHY_PLL_AUTOPGM_CTL1 0x1C
  33. #define PLL_RESET_N_CNT_5 0x5
  34. #define PLL_RESET_N BIT(4)
  35. #define PLL_AUTOPGM_EN BIT(7)
  36. #define QUSB2PHY_PLL_STATUS 0x38
  37. #define QUSB2PHY_PLL_LOCK BIT(5)
  38. #define QUSB2PHY_PORT_QC1 0x70
  39. #define VDM_SRC_EN BIT(4)
  40. #define VDP_SRC_EN BIT(2)
  41. #define QUSB2PHY_PORT_QC2 0x74
  42. #define RDM_UP_EN BIT(1)
  43. #define RDP_UP_EN BIT(3)
  44. #define RPUM_LOW_EN BIT(4)
  45. #define RPUP_LOW_EN BIT(5)
  46. #define QUSB2PHY_PORT_POWERDOWN 0xB4
  47. #define CLAMP_N_EN BIT(5)
  48. #define FREEZIO_N BIT(1)
  49. #define POWER_DOWN BIT(0)
  50. #define QUSB2PHY_PORT_TEST_CTRL 0xB8
  51. #define QUSB2PHY_PWR_CTRL1 0x210
  52. #define PWR_CTRL1_CLAMP_N_EN BIT(1)
  53. #define PWR_CTRL1_POWR_DOWN BIT(0)
  54. #define QUSB2PHY_PLL_COMMON_STATUS_ONE 0x1A0
  55. #define CORE_READY_STATUS BIT(0)
  56. #define QUSB2PHY_PORT_UTMI_CTRL1 0xC0
  57. #define SUSPEND_N BIT(5)
  58. #define TERM_SELECT BIT(4)
  59. #define XCVR_SELECT_FS BIT(2)
  60. #define OP_MODE_NON_DRIVE BIT(0)
  61. #define QUSB2PHY_PORT_UTMI_CTRL2 0xC4
  62. #define UTMI_ULPI_SEL BIT(7)
  63. #define UTMI_TEST_MUX_SEL BIT(6)
  64. #define QUSB2PHY_PLL_AUTOPGM_CTL1 0x1C
  65. #define QUSB2PHY_PLL_PWR_CTL 0x18
  66. #define QUSB2PHY_PLL_TEST 0x04
  67. #define CLK_REF_SEL BIT(7)
  68. #define QUSB2PHY_PORT_TUNE1 0x80
  69. #define QUSB2PHY_PORT_TUNE2 0x84
  70. #define QUSB2PHY_PORT_TUNE3 0x88
  71. #define QUSB2PHY_PORT_TUNE4 0x8C
  72. #define QUSB2PHY_PORT_TUNE5 0x90
  73. /* Get TUNE2's high nibble value read from efuse */
  74. #define TUNE2_HIGH_NIBBLE_VAL(val, pos, mask) ((val >> pos) & mask)
  75. #define QUSB2PHY_PORT_INTR_CTRL 0xBC
  76. #define CHG_DET_INTR_EN BIT(4)
  77. #define DMSE_INTR_HIGH_SEL BIT(3)
  78. #define DMSE_INTR_EN BIT(2)
  79. #define DPSE_INTR_HIGH_SEL BIT(1)
  80. #define DPSE_INTR_EN BIT(0)
  81. #define QUSB2PHY_PORT_INTR_STATUS 0xF0
  82. #define DPSE_INTR_HIGH BIT(0)
  83. #define QUSB2PHY_PORT_UTMI_STATUS 0xF4
  84. #define LINESTATE_DP BIT(0)
  85. #define LINESTATE_DM BIT(1)
  86. #define QUSB2PHY_1P8_VOL_MIN 1800000 /* uV */
  87. #define QUSB2PHY_1P8_VOL_MAX 1800000 /* uV */
  88. #define QUSB2PHY_1P8_HPM_LOAD 30000 /* uA */
  89. #define QUSB2PHY_3P3_VOL_MIN 3075000 /* uV */
  90. #define QUSB2PHY_3P3_VOL_MAX 3200000 /* uV */
  91. #define QUSB2PHY_3P3_HPM_LOAD 30000 /* uA */
  92. #define QUSB2PHY_REFCLK_ENABLE BIT(0)
  93. #define HSTX_TRIMSIZE 4
  94. enum port_state {
  95. PORT_UNKNOWN,
  96. PORT_DISCONNECTED,
  97. PORT_DCD_IN_PROGRESS,
  98. PORT_PRIMARY_IN_PROGRESS,
  99. PORT_SECONDARY_IN_PROGRESS,
  100. PORT_CHG_DET_DONE,
  101. PORT_HOST_MODE,
  102. };
  103. enum chg_det_state {
  104. STATE_UNKNOWN,
  105. STATE_DCD,
  106. STATE_PRIMARY,
  107. STATE_SECONDARY,
  108. };
  109. struct qusb_phy {
  110. struct usb_phy phy;
  111. void __iomem *base;
  112. void __iomem *tune2_efuse_reg;
  113. void __iomem *ref_clk_base;
  114. void __iomem *tcsr_clamp_dig_n;
  115. void __iomem *tcsr_conn_box_spare;
  116. void __iomem *eud_enable_reg;
  117. struct clk *ref_clk_src;
  118. struct clk *ref_clk;
  119. struct clk *cfg_ahb_clk;
  120. struct reset_control *phy_reset;
  121. struct clk *iface_clk;
  122. struct clk *core_clk;
  123. struct regulator *gdsc;
  124. struct regulator *vdd;
  125. struct regulator *vdda33;
  126. struct regulator *vdda18;
  127. int vdd_levels[3]; /* none, low, high */
  128. int init_seq_len;
  129. int *qusb_phy_init_seq;
  130. u32 major_rev;
  131. u32 usb_hs_ac_bitmask;
  132. u32 usb_hs_ac_value;
  133. u32 tune2_val;
  134. int tune2_efuse_bit_pos;
  135. int tune2_efuse_num_of_bits;
  136. int tune2_efuse_correction;
  137. bool cable_connected;
  138. bool suspended;
  139. bool ulpi_mode;
  140. bool dpdm_enable;
  141. bool is_se_clk;
  142. bool scm_lvl_shifter;
  143. struct regulator_desc dpdm_rdesc;
  144. struct regulator_dev *dpdm_rdev;
  145. bool put_into_high_z_state;
  146. struct mutex phy_lock;
  147. struct extcon_dev *usb_extcon;
  148. bool vbus_active;
  149. bool id_state;
  150. struct power_supply *usb_psy;
  151. struct delayed_work port_det_w;
  152. enum port_state port_state;
  153. unsigned int dcd_timeout;
  154. /* debugfs entries */
  155. struct dentry *root;
  156. u8 tune1;
  157. u8 tune2;
  158. u8 tune3;
  159. u8 tune4;
  160. u8 tune5;
  161. };
  162. static void qusb_phy_update_tcsr_level_shifter(struct qusb_phy *qphy,
  163. u32 val)
  164. {
  165. if (qphy->tcsr_clamp_dig_n) {
  166. writel_relaxed(val, qphy->tcsr_clamp_dig_n);
  167. dev_dbg(qphy->phy.dev, "update tcsr level shifter: %d\n", val);
  168. } else if (qphy->scm_lvl_shifter) {
  169. dev_dbg(qphy->phy.dev, "update scm level shifter: %d\n", val);
  170. qcom_scm_phy_update_scm_level_shifter(val);
  171. }
  172. }
  173. static void qusb_phy_enable_clocks(struct qusb_phy *qphy, bool on)
  174. {
  175. dev_dbg(qphy->phy.dev, "%s(): on:%d\n", __func__, on);
  176. if (on) {
  177. clk_prepare_enable(qphy->ref_clk_src);
  178. clk_prepare_enable(qphy->ref_clk);
  179. clk_prepare_enable(qphy->iface_clk);
  180. clk_prepare_enable(qphy->core_clk);
  181. clk_prepare_enable(qphy->cfg_ahb_clk);
  182. } else {
  183. clk_disable_unprepare(qphy->cfg_ahb_clk);
  184. /*
  185. * FSM depedency beween iface_clk and core_clk.
  186. * Hence turned off core_clk before iface_clk.
  187. */
  188. clk_disable_unprepare(qphy->core_clk);
  189. clk_disable_unprepare(qphy->iface_clk);
  190. clk_disable_unprepare(qphy->ref_clk);
  191. clk_disable_unprepare(qphy->ref_clk_src);
  192. }
  193. }
  194. static int qusb_phy_gdsc(struct qusb_phy *qphy, bool on)
  195. {
  196. int ret;
  197. if (IS_ERR_OR_NULL(qphy->gdsc))
  198. return -EPERM;
  199. if (on) {
  200. dev_dbg(qphy->phy.dev, "TURNING ON GDSC\n");
  201. ret = regulator_enable(qphy->gdsc);
  202. if (ret) {
  203. dev_err(qphy->phy.dev, "unable to enable gdsc\n");
  204. return ret;
  205. }
  206. } else {
  207. dev_dbg(qphy->phy.dev, "TURNING OFF GDSC\n");
  208. ret = regulator_disable(qphy->gdsc);
  209. if (ret) {
  210. dev_err(qphy->phy.dev, "unable to disable gdsc\n");
  211. return ret;
  212. }
  213. }
  214. return ret;
  215. }
  216. static int qusb_phy_config_vdd(struct qusb_phy *qphy, int high)
  217. {
  218. int min, ret;
  219. min = high ? 1 : 0; /* low or none? */
  220. ret = regulator_set_voltage(qphy->vdd, qphy->vdd_levels[min],
  221. qphy->vdd_levels[2]);
  222. if (ret) {
  223. dev_err(qphy->phy.dev, "unable to set voltage for qusb vdd\n");
  224. return ret;
  225. }
  226. dev_dbg(qphy->phy.dev, "min_vol:%d max_vol:%d\n",
  227. qphy->vdd_levels[min], qphy->vdd_levels[2]);
  228. return ret;
  229. }
  230. static int qusb_phy_enable_power(struct qusb_phy *qphy, bool on)
  231. {
  232. int ret = 0;
  233. dev_dbg(qphy->phy.dev, "%s turn %s regulators\n",
  234. __func__, on ? "on" : "off");
  235. if (!on)
  236. goto disable_vdda33;
  237. ret = qusb_phy_config_vdd(qphy, true);
  238. if (ret) {
  239. dev_err(qphy->phy.dev, "Unable to config VDD:%d\n",
  240. ret);
  241. goto err_vdd;
  242. }
  243. ret = regulator_enable(qphy->vdd);
  244. if (ret) {
  245. dev_err(qphy->phy.dev, "Unable to enable VDD\n");
  246. goto unconfig_vdd;
  247. }
  248. ret = regulator_set_load(qphy->vdda18, QUSB2PHY_1P8_HPM_LOAD);
  249. if (ret < 0) {
  250. dev_err(qphy->phy.dev, "Unable to set HPM of vdda18:%d\n", ret);
  251. goto disable_vdd;
  252. }
  253. ret = regulator_set_voltage(qphy->vdda18, QUSB2PHY_1P8_VOL_MIN,
  254. QUSB2PHY_1P8_VOL_MAX);
  255. if (ret) {
  256. dev_err(qphy->phy.dev,
  257. "Unable to set voltage for vdda18:%d\n", ret);
  258. goto put_vdda18_lpm;
  259. }
  260. ret = regulator_enable(qphy->vdda18);
  261. if (ret) {
  262. dev_err(qphy->phy.dev, "Unable to enable vdda18:%d\n", ret);
  263. goto unset_vdda18;
  264. }
  265. ret = regulator_set_load(qphy->vdda33, QUSB2PHY_3P3_HPM_LOAD);
  266. if (ret < 0) {
  267. dev_err(qphy->phy.dev, "Unable to set HPM of vdda33:%d\n", ret);
  268. goto disable_vdda18;
  269. }
  270. ret = regulator_set_voltage(qphy->vdda33, QUSB2PHY_3P3_VOL_MIN,
  271. QUSB2PHY_3P3_VOL_MAX);
  272. if (ret) {
  273. dev_err(qphy->phy.dev,
  274. "Unable to set voltage for vdda33:%d\n", ret);
  275. goto put_vdda33_lpm;
  276. }
  277. ret = regulator_enable(qphy->vdda33);
  278. if (ret) {
  279. dev_err(qphy->phy.dev, "Unable to enable vdda33:%d\n", ret);
  280. goto unset_vdd33;
  281. }
  282. pr_debug("%s(): QUSB PHY's regulators are turned ON.\n", __func__);
  283. return ret;
  284. disable_vdda33:
  285. ret = regulator_disable(qphy->vdda33);
  286. if (ret)
  287. dev_err(qphy->phy.dev, "Unable to disable vdda33:%d\n", ret);
  288. unset_vdd33:
  289. ret = regulator_set_voltage(qphy->vdda33, 0, QUSB2PHY_3P3_VOL_MAX);
  290. if (ret)
  291. dev_err(qphy->phy.dev,
  292. "Unable to set (0) voltage for vdda33:%d\n", ret);
  293. put_vdda33_lpm:
  294. ret = regulator_set_load(qphy->vdda33, 0);
  295. if (ret < 0)
  296. dev_err(qphy->phy.dev, "Unable to set (0) HPM of vdda33\n");
  297. disable_vdda18:
  298. ret = regulator_disable(qphy->vdda18);
  299. if (ret)
  300. dev_err(qphy->phy.dev, "Unable to disable vdda18:%d\n", ret);
  301. unset_vdda18:
  302. ret = regulator_set_voltage(qphy->vdda18, 0, QUSB2PHY_1P8_VOL_MAX);
  303. if (ret)
  304. dev_err(qphy->phy.dev,
  305. "Unable to set (0) voltage for vdda18:%d\n", ret);
  306. put_vdda18_lpm:
  307. ret = regulator_set_load(qphy->vdda18, 0);
  308. if (ret < 0)
  309. dev_err(qphy->phy.dev, "Unable to set LPM of vdda18\n");
  310. disable_vdd:
  311. ret = regulator_disable(qphy->vdd);
  312. if (ret)
  313. dev_err(qphy->phy.dev, "Unable to disable vdd:%d\n",
  314. ret);
  315. unconfig_vdd:
  316. ret = qusb_phy_config_vdd(qphy, false);
  317. if (ret)
  318. dev_err(qphy->phy.dev, "Unable unconfig VDD:%d\n",
  319. ret);
  320. err_vdd:
  321. dev_dbg(qphy->phy.dev, "QUSB PHY's regulators are turned OFF.\n");
  322. return ret;
  323. }
  324. static void qusb_phy_get_tune2_param(struct qusb_phy *qphy)
  325. {
  326. u32 bit_mask = 1;
  327. u8 reg_val;
  328. pr_debug("%s(): num_of_bits:%d bit_pos:%d\n", __func__,
  329. qphy->tune2_efuse_num_of_bits,
  330. qphy->tune2_efuse_bit_pos);
  331. /* get bit mask based on number of bits to use with efuse reg */
  332. bit_mask = (bit_mask << qphy->tune2_efuse_num_of_bits) - 1;
  333. /*
  334. * Read EFUSE register having TUNE2 parameter's high nibble.
  335. * If efuse register shows value as 0x0, then use previous value
  336. * as it is. Otherwise use efuse register based value for this purpose.
  337. */
  338. if (qphy->tune2_efuse_num_of_bits < HSTX_TRIMSIZE) {
  339. qphy->tune2_val =
  340. TUNE2_HIGH_NIBBLE_VAL(readl_relaxed(qphy->tune2_efuse_reg),
  341. qphy->tune2_efuse_bit_pos, bit_mask);
  342. bit_mask =
  343. (1 << (HSTX_TRIMSIZE - qphy->tune2_efuse_num_of_bits)) - 1;
  344. qphy->tune2_val |=
  345. TUNE2_HIGH_NIBBLE_VAL(readl_relaxed(qphy->tune2_efuse_reg + 4),
  346. 0, bit_mask) << qphy->tune2_efuse_num_of_bits;
  347. } else {
  348. qphy->tune2_val = readl_relaxed(qphy->tune2_efuse_reg);
  349. qphy->tune2_val = TUNE2_HIGH_NIBBLE_VAL(qphy->tune2_val,
  350. qphy->tune2_efuse_bit_pos, bit_mask);
  351. }
  352. pr_debug("%s(): efuse based tune2 value:%d\n",
  353. __func__, qphy->tune2_val);
  354. /* Update higher nibble of TUNE2 value for better rise/fall times */
  355. if (qphy->tune2_efuse_correction && qphy->tune2_val) {
  356. if (qphy->tune2_efuse_correction > 5 ||
  357. qphy->tune2_efuse_correction < -10)
  358. pr_warn("Correction value is out of range : %d\n",
  359. qphy->tune2_efuse_correction);
  360. else
  361. qphy->tune2_val = qphy->tune2_val +
  362. qphy->tune2_efuse_correction;
  363. }
  364. reg_val = readb_relaxed(qphy->base + QUSB2PHY_PORT_TUNE2);
  365. if (qphy->tune2_val) {
  366. reg_val &= 0x0f;
  367. reg_val |= (qphy->tune2_val << 4);
  368. }
  369. qphy->tune2_val = reg_val;
  370. }
  371. static void qusb_phy_write_seq(void __iomem *base, u32 *seq, int cnt,
  372. unsigned long delay)
  373. {
  374. int i;
  375. pr_debug("Seq count:%d\n", cnt);
  376. for (i = 0; i < cnt; i = i+2) {
  377. pr_debug("write 0x%02x to 0x%02x\n", seq[i], seq[i+1]);
  378. writel_relaxed(seq[i], base + seq[i+1]);
  379. if (delay)
  380. usleep_range(delay, (delay + 2000));
  381. }
  382. }
  383. static int qusb_phy_init(struct usb_phy *phy)
  384. {
  385. struct qusb_phy *qphy = container_of(phy, struct qusb_phy, phy);
  386. int ret, reset_val = 0;
  387. u8 reg;
  388. bool pll_lock_fail = false;
  389. if (qphy->eud_enable_reg && readl_relaxed(qphy->eud_enable_reg)) {
  390. dev_err(qphy->phy.dev, "eud is enabled\n");
  391. return 0;
  392. }
  393. /*
  394. * ref clock is enabled by default after power on reset. Linux clock
  395. * driver will disable this clock as part of late init if peripheral
  396. * driver(s) does not explicitly votes for it. Linux clock driver also
  397. * does not disable the clock until late init even if peripheral
  398. * driver explicitly requests it and cannot defer the probe until late
  399. * init. Hence, Explicitly disable the clock using register write to
  400. * allow QUSB PHY PLL to lock properly.
  401. */
  402. if (qphy->ref_clk_base) {
  403. writel_relaxed((readl_relaxed(qphy->ref_clk_base) &
  404. ~QUSB2PHY_REFCLK_ENABLE),
  405. qphy->ref_clk_base);
  406. /* Make sure that above write complete to get ref clk OFF */
  407. wmb();
  408. }
  409. /* Perform phy reset */
  410. ret = reset_control_assert(qphy->phy_reset);
  411. if (ret)
  412. dev_err(phy->dev, "%s: phy_reset assert failed\n", __func__);
  413. usleep_range(100, 150);
  414. ret = reset_control_deassert(qphy->phy_reset);
  415. if (ret)
  416. dev_err(phy->dev, "%s: phy_reset deassert failed\n", __func__);
  417. /* Disable the PHY */
  418. if (qphy->major_rev < 2)
  419. writel_relaxed(CLAMP_N_EN | FREEZIO_N | POWER_DOWN,
  420. qphy->base + QUSB2PHY_PORT_POWERDOWN);
  421. else
  422. writel_relaxed(readl_relaxed(qphy->base + QUSB2PHY_PWR_CTRL1) |
  423. PWR_CTRL1_POWR_DOWN,
  424. qphy->base + QUSB2PHY_PWR_CTRL1);
  425. /* configure for ULPI mode if requested */
  426. if (qphy->ulpi_mode)
  427. writel_relaxed(0x0, qphy->base + QUSB2PHY_PORT_UTMI_CTRL2);
  428. /* save reset value to override based on clk scheme */
  429. if (qphy->ref_clk_base)
  430. reset_val = readl_relaxed(qphy->base + QUSB2PHY_PLL_TEST);
  431. if (qphy->qusb_phy_init_seq)
  432. qusb_phy_write_seq(qphy->base, qphy->qusb_phy_init_seq,
  433. qphy->init_seq_len, 0);
  434. /*
  435. * Check for EFUSE value only if tune2_efuse_reg is available
  436. * and try to read EFUSE value only once i.e. not every USB
  437. * cable connect case.
  438. */
  439. if (qphy->tune2_efuse_reg && !qphy->tune2) {
  440. if (!qphy->tune2_val)
  441. qusb_phy_get_tune2_param(qphy);
  442. pr_debug("%s(): Programming TUNE2 parameter as:%x\n", __func__,
  443. qphy->tune2_val);
  444. writel_relaxed(qphy->tune2_val,
  445. qphy->base + QUSB2PHY_PORT_TUNE2);
  446. }
  447. /* If tune modparam set, override tune value */
  448. if (qphy->tune1) {
  449. writel_relaxed(qphy->tune1,
  450. qphy->base + QUSB2PHY_PORT_TUNE1);
  451. }
  452. if (qphy->tune2) {
  453. writel_relaxed(qphy->tune2,
  454. qphy->base + QUSB2PHY_PORT_TUNE2);
  455. }
  456. if (qphy->tune3) {
  457. writel_relaxed(qphy->tune3,
  458. qphy->base + QUSB2PHY_PORT_TUNE3);
  459. }
  460. if (qphy->tune4) {
  461. writel_relaxed(qphy->tune4,
  462. qphy->base + QUSB2PHY_PORT_TUNE4);
  463. }
  464. if (qphy->tune5) {
  465. writel_relaxed(qphy->tune5,
  466. qphy->base + QUSB2PHY_PORT_TUNE5);
  467. }
  468. /* ensure above writes are completed before re-enabling PHY */
  469. wmb();
  470. /* Enable the PHY */
  471. if (qphy->major_rev < 2)
  472. writel_relaxed(CLAMP_N_EN | FREEZIO_N,
  473. qphy->base + QUSB2PHY_PORT_POWERDOWN);
  474. else
  475. writel_relaxed(readl_relaxed(qphy->base + QUSB2PHY_PWR_CTRL1) &
  476. ~PWR_CTRL1_POWR_DOWN,
  477. qphy->base + QUSB2PHY_PWR_CTRL1);
  478. /* Ensure above write is completed before turning ON ref clk */
  479. wmb();
  480. /* Require to get phy pll lock successfully */
  481. usleep_range(150, 160);
  482. /* Turn on phy ref_clk if DIFF_CLK else select SE_CLK */
  483. if (qphy->ref_clk_base) {
  484. if (!qphy->is_se_clk) {
  485. reset_val &= ~CLK_REF_SEL;
  486. writel_relaxed((readl_relaxed(qphy->ref_clk_base) |
  487. QUSB2PHY_REFCLK_ENABLE),
  488. qphy->ref_clk_base);
  489. } else {
  490. reset_val |= CLK_REF_SEL;
  491. writel_relaxed(reset_val,
  492. qphy->base + QUSB2PHY_PLL_TEST);
  493. }
  494. /* Make sure above write is completed to get PLL source clock */
  495. wmb();
  496. /* Required to get PHY PLL lock successfully */
  497. usleep_range(100, 110);
  498. }
  499. if (qphy->major_rev < 2) {
  500. reg = readb_relaxed(qphy->base + QUSB2PHY_PLL_STATUS);
  501. dev_dbg(phy->dev, "QUSB2PHY_PLL_STATUS:%x\n", reg);
  502. if (!(reg & QUSB2PHY_PLL_LOCK))
  503. pll_lock_fail = true;
  504. } else {
  505. reg = readb_relaxed(qphy->base +
  506. QUSB2PHY_PLL_COMMON_STATUS_ONE);
  507. dev_dbg(phy->dev, "QUSB2PHY_PLL_COMMON_STATUS_ONE:%x\n", reg);
  508. if (!(reg & CORE_READY_STATUS))
  509. pll_lock_fail = true;
  510. }
  511. if (pll_lock_fail)
  512. dev_err(phy->dev, "QUSB PHY PLL LOCK fails:%x\n", reg);
  513. return 0;
  514. }
  515. static void qusb_phy_shutdown(struct usb_phy *phy)
  516. {
  517. struct qusb_phy *qphy = container_of(phy, struct qusb_phy, phy);
  518. if (qphy->eud_enable_reg && readl_relaxed(qphy->eud_enable_reg)) {
  519. dev_err(qphy->phy.dev, "eud is enabled\n");
  520. return;
  521. }
  522. qusb_phy_enable_clocks(qphy, true);
  523. /* Disable the PHY */
  524. if (qphy->major_rev < 2)
  525. writel_relaxed(CLAMP_N_EN | FREEZIO_N | POWER_DOWN,
  526. qphy->base + QUSB2PHY_PORT_POWERDOWN);
  527. else
  528. writel_relaxed(readl_relaxed(qphy->base + QUSB2PHY_PWR_CTRL1) |
  529. PWR_CTRL1_POWR_DOWN,
  530. qphy->base + QUSB2PHY_PWR_CTRL1);
  531. /* Make sure above write complete before turning off clocks */
  532. wmb();
  533. qusb_phy_enable_clocks(qphy, false);
  534. }
  535. /**
  536. * Performs QUSB2 PHY suspend/resume functionality.
  537. *
  538. * @uphy - usb phy pointer.
  539. * @suspend - to enable suspend or not. 1 - suspend, 0 - resume
  540. *
  541. */
  542. static int qusb_phy_set_suspend(struct usb_phy *phy, int suspend)
  543. {
  544. struct qusb_phy *qphy = container_of(phy, struct qusb_phy, phy);
  545. u32 linestate = 0, intr_mask = 0;
  546. if (qphy->suspended == suspend) {
  547. dev_dbg(phy->dev, "%s: USB PHY is already suspended\n",
  548. __func__);
  549. return 0;
  550. }
  551. if (suspend) {
  552. /* Bus suspend case */
  553. if (qphy->cable_connected) {
  554. /* Clear all interrupts */
  555. writel_relaxed(0x00,
  556. qphy->base + QUSB2PHY_PORT_INTR_CTRL);
  557. linestate = readl_relaxed(qphy->base +
  558. QUSB2PHY_PORT_UTMI_STATUS);
  559. /*
  560. * D+/D- interrupts are level-triggered, but we are
  561. * only interested if the line state changes, so enable
  562. * the high/low trigger based on current state. In
  563. * other words, enable the triggers _opposite_ of what
  564. * the current D+/D- levels are.
  565. * e.g. if currently D+ high, D- low (HS 'J'/Suspend),
  566. * configure the mask to trigger on D+ low OR D- high
  567. */
  568. intr_mask = DPSE_INTR_EN | DMSE_INTR_EN;
  569. if (!(linestate & LINESTATE_DP)) /* D+ low */
  570. intr_mask |= DPSE_INTR_HIGH_SEL;
  571. if (!(linestate & LINESTATE_DM)) /* D- low */
  572. intr_mask |= DMSE_INTR_HIGH_SEL;
  573. writel_relaxed(intr_mask,
  574. qphy->base + QUSB2PHY_PORT_INTR_CTRL);
  575. if (linestate & (LINESTATE_DP | LINESTATE_DM)) {
  576. /* enable phy auto-resume */
  577. writel_relaxed(0x0C,
  578. qphy->base + QUSB2PHY_PORT_TEST_CTRL);
  579. /* flush the previous write before next write */
  580. wmb();
  581. writel_relaxed(0x04,
  582. qphy->base + QUSB2PHY_PORT_TEST_CTRL);
  583. }
  584. dev_dbg(phy->dev, "%s: intr_mask = %x\n",
  585. __func__, intr_mask);
  586. /* Makes sure that above write goes through */
  587. wmb();
  588. qusb_phy_enable_clocks(qphy, false);
  589. } else { /* Disconnect case */
  590. mutex_lock(&qphy->phy_lock);
  591. /* Disable all interrupts */
  592. writel_relaxed(0x00,
  593. qphy->base + QUSB2PHY_PORT_INTR_CTRL);
  594. if (!qphy->eud_enable_reg ||
  595. !readl_relaxed(qphy->eud_enable_reg)) {
  596. if (!(qphy->phy.flags & PHY_HOST_MODE)) {
  597. /* Disable PHY */
  598. writel_relaxed(POWER_DOWN |
  599. readl_relaxed(qphy->base +
  600. QUSB2PHY_PORT_POWERDOWN),
  601. qphy->base + QUSB2PHY_PORT_POWERDOWN);
  602. /* Make sure that above write is completed */
  603. wmb();
  604. qusb_phy_update_tcsr_level_shifter(qphy, 0);
  605. }
  606. }
  607. qusb_phy_enable_clocks(qphy, false);
  608. qusb_phy_enable_power(qphy, false);
  609. mutex_unlock(&qphy->phy_lock);
  610. /*
  611. * Set put_into_high_z_state to true so next USB
  612. * cable connect, DPF_DMF request performs PHY
  613. * reset and put it into high-z state. For bootup
  614. * with or without USB cable, it doesn't require
  615. * to put QUSB PHY into high-z state.
  616. */
  617. qphy->put_into_high_z_state = true;
  618. }
  619. qphy->suspended = true;
  620. } else {
  621. /* Bus suspend case */
  622. if (qphy->cable_connected) {
  623. qusb_phy_enable_clocks(qphy, true);
  624. /* Clear all interrupts on resume */
  625. writel_relaxed(0x00,
  626. qphy->base + QUSB2PHY_PORT_INTR_CTRL);
  627. } else {
  628. qusb_phy_enable_power(qphy, true);
  629. qusb_phy_update_tcsr_level_shifter(qphy, 1);
  630. qusb_phy_enable_clocks(qphy, true);
  631. }
  632. qphy->suspended = false;
  633. }
  634. return 0;
  635. }
  636. static int qusb_phy_notify_connect(struct usb_phy *phy,
  637. enum usb_device_speed speed)
  638. {
  639. struct qusb_phy *qphy = container_of(phy, struct qusb_phy, phy);
  640. qphy->cable_connected = true;
  641. dev_dbg(phy->dev, "QUSB PHY: connect notification cable_connected=%d\n",
  642. qphy->cable_connected);
  643. return 0;
  644. }
  645. static int qusb_phy_notify_disconnect(struct usb_phy *phy,
  646. enum usb_device_speed speed)
  647. {
  648. struct qusb_phy *qphy = container_of(phy, struct qusb_phy, phy);
  649. qphy->cable_connected = false;
  650. dev_dbg(phy->dev, "QUSB PHY: connect notification cable_connected=%d\n",
  651. qphy->cable_connected);
  652. return 0;
  653. }
  654. #define DP_PULSE_WIDTH_MSEC 200
  655. static enum usb_charger_type qusb_phy_drive_dp_pulse(struct usb_phy *phy)
  656. {
  657. struct qusb_phy *qphy = container_of(phy, struct qusb_phy, phy);
  658. int ret;
  659. dev_dbg(qphy->phy.dev, "connected to a CDP, drive DP up\n");
  660. ret = qusb_phy_enable_power(qphy, true);
  661. if (ret < 0) {
  662. dev_dbg(qphy->phy.dev,
  663. "dpdm regulator enable failed:%d\n", ret);
  664. return 0;
  665. }
  666. qusb_phy_gdsc(qphy, true);
  667. qusb_phy_enable_clocks(qphy, true);
  668. ret = reset_control_assert(qphy->phy_reset);
  669. if (ret)
  670. dev_err(qphy->phy.dev, "phyassert failed\n");
  671. usleep_range(100, 150);
  672. ret = reset_control_deassert(qphy->phy_reset);
  673. if (ret)
  674. dev_err(qphy->phy.dev, "deassert failed\n");
  675. /* Configure PHY to enable control on DP/DM lines */
  676. writel_relaxed(CLAMP_N_EN | FREEZIO_N | POWER_DOWN,
  677. qphy->base + QUSB2PHY_PORT_POWERDOWN);
  678. writel_relaxed(TERM_SELECT | XCVR_SELECT_FS | OP_MODE_NON_DRIVE |
  679. SUSPEND_N, qphy->base + QUSB2PHY_PORT_UTMI_CTRL1);
  680. writel_relaxed(UTMI_ULPI_SEL | UTMI_TEST_MUX_SEL,
  681. qphy->base + QUSB2PHY_PORT_UTMI_CTRL2);
  682. writel_relaxed(PLL_RESET_N_CNT_5,
  683. qphy->base + QUSB2PHY_PLL_AUTOPGM_CTL1);
  684. writel_relaxed(CLAMP_N_EN | FREEZIO_N,
  685. qphy->base + QUSB2PHY_PORT_POWERDOWN);
  686. writel_relaxed(REF_BUF_EN | REXT_EN | PLL_BYPASSNL | REXT_TRIM_0,
  687. qphy->base + QUSB2PHY_PLL_PWR_CTL);
  688. usleep_range(5, 10);
  689. writel_relaxed(0x15, qphy->base + QUSB2PHY_PLL_AUTOPGM_CTL1);
  690. writel_relaxed(PLL_RESET_N | PLL_RESET_N_CNT_5,
  691. qphy->base + QUSB2PHY_PLL_AUTOPGM_CTL1);
  692. writel_relaxed(0x00, qphy->base + QUSB2PHY_PORT_QC1);
  693. writel_relaxed(0x00, qphy->base + QUSB2PHY_PORT_QC2);
  694. usleep_range(50, 60);
  695. /* Enable Rdp_en to pull DP up to 3V */
  696. writel_relaxed(RDP_UP_EN, qphy->base + QUSB2PHY_PORT_QC2);
  697. msleep(DP_PULSE_WIDTH_MSEC);
  698. /* Put the PHY and DP back to normal state */
  699. writel_relaxed(CLAMP_N_EN | FREEZIO_N | POWER_DOWN,
  700. qphy->base + QUSB2PHY_PORT_POWERDOWN); /* 23 */
  701. writel_relaxed(PLL_AUTOPGM_EN | PLL_RESET_N | PLL_RESET_N_CNT_5,
  702. qphy->base + QUSB2PHY_PLL_AUTOPGM_CTL1);
  703. writel_relaxed(UTMI_ULPI_SEL, qphy->base + QUSB2PHY_PORT_UTMI_CTRL2);
  704. writel_relaxed(TERM_SELECT, qphy->base + QUSB2PHY_PORT_UTMI_CTRL1);
  705. qusb_phy_enable_clocks(qphy, false);
  706. qusb_phy_gdsc(qphy, false);
  707. ret = qusb_phy_enable_power(qphy, false);
  708. if (ret < 0) {
  709. dev_dbg(qphy->phy.dev,
  710. "dpdm regulator disable failed:%d\n", ret);
  711. }
  712. return 0;
  713. }
  714. static int qusb_phy_dpdm_regulator_enable(struct regulator_dev *rdev)
  715. {
  716. int ret = 0;
  717. struct qusb_phy *qphy = rdev_get_drvdata(rdev);
  718. dev_dbg(qphy->phy.dev, "%s dpdm_enable:%d\n",
  719. __func__, qphy->dpdm_enable);
  720. if (qphy->eud_enable_reg && readl_relaxed(qphy->eud_enable_reg)) {
  721. dev_err(qphy->phy.dev, "eud is enabled\n");
  722. return 0;
  723. }
  724. mutex_lock(&qphy->phy_lock);
  725. if (!qphy->dpdm_enable) {
  726. ret = qusb_phy_enable_power(qphy, true);
  727. if (ret < 0) {
  728. dev_dbg(qphy->phy.dev,
  729. "dpdm regulator enable failed:%d\n", ret);
  730. mutex_unlock(&qphy->phy_lock);
  731. return ret;
  732. }
  733. qphy->dpdm_enable = true;
  734. if (qphy->put_into_high_z_state) {
  735. qusb_phy_update_tcsr_level_shifter(qphy, 1);
  736. qusb_phy_gdsc(qphy, true);
  737. qusb_phy_enable_clocks(qphy, true);
  738. dev_dbg(qphy->phy.dev, "RESET QUSB PHY\n");
  739. ret = reset_control_assert(qphy->phy_reset);
  740. if (ret)
  741. dev_err(qphy->phy.dev, "phyassert failed\n");
  742. usleep_range(100, 150);
  743. ret = reset_control_deassert(qphy->phy_reset);
  744. if (ret)
  745. dev_err(qphy->phy.dev, "deassert failed\n");
  746. /*
  747. * Phy in non-driving mode leaves Dp and Dm
  748. * lines in high-Z state. Controller power
  749. * collapse is not switching phy to non-driving
  750. * mode causing charger detection failure. Bring
  751. * phy to non-driving mode by overriding
  752. * controller output via UTMI interface.
  753. */
  754. writel_relaxed(TERM_SELECT | XCVR_SELECT_FS |
  755. OP_MODE_NON_DRIVE,
  756. qphy->base + QUSB2PHY_PORT_UTMI_CTRL1);
  757. writel_relaxed(UTMI_ULPI_SEL |
  758. UTMI_TEST_MUX_SEL,
  759. qphy->base + QUSB2PHY_PORT_UTMI_CTRL2);
  760. /* Disable PHY */
  761. writel_relaxed(CLAMP_N_EN | FREEZIO_N |
  762. POWER_DOWN,
  763. qphy->base + QUSB2PHY_PORT_POWERDOWN);
  764. /* Make sure that above write is completed */
  765. wmb();
  766. qusb_phy_enable_clocks(qphy, false);
  767. qusb_phy_gdsc(qphy, false);
  768. }
  769. }
  770. mutex_unlock(&qphy->phy_lock);
  771. return ret;
  772. }
  773. static int qusb_phy_dpdm_regulator_disable(struct regulator_dev *rdev)
  774. {
  775. int ret = 0;
  776. struct qusb_phy *qphy = rdev_get_drvdata(rdev);
  777. dev_dbg(qphy->phy.dev, "%s dpdm_enable:%d\n",
  778. __func__, qphy->dpdm_enable);
  779. mutex_lock(&qphy->phy_lock);
  780. if (qphy->dpdm_enable) {
  781. /* If usb core is active, rely on set_suspend to clamp phy */
  782. if (!qphy->cable_connected)
  783. qusb_phy_update_tcsr_level_shifter(qphy, 0);
  784. ret = qusb_phy_enable_power(qphy, false);
  785. if (ret < 0) {
  786. dev_dbg(qphy->phy.dev,
  787. "dpdm regulator disable failed:%d\n", ret);
  788. mutex_unlock(&qphy->phy_lock);
  789. return ret;
  790. }
  791. qphy->dpdm_enable = false;
  792. }
  793. mutex_unlock(&qphy->phy_lock);
  794. return ret;
  795. }
  796. static int qusb_phy_dpdm_regulator_is_enabled(struct regulator_dev *rdev)
  797. {
  798. struct qusb_phy *qphy = rdev_get_drvdata(rdev);
  799. dev_dbg(qphy->phy.dev, "%s qphy->dpdm_enable = %d\n", __func__,
  800. qphy->dpdm_enable);
  801. return qphy->dpdm_enable;
  802. }
  803. static const struct regulator_ops qusb_phy_dpdm_regulator_ops = {
  804. .enable = qusb_phy_dpdm_regulator_enable,
  805. .disable = qusb_phy_dpdm_regulator_disable,
  806. .is_enabled = qusb_phy_dpdm_regulator_is_enabled,
  807. };
  808. static int qusb_phy_regulator_init(struct qusb_phy *qphy)
  809. {
  810. struct device *dev = qphy->phy.dev;
  811. struct regulator_config cfg = {};
  812. struct regulator_init_data *init_data;
  813. init_data = devm_kzalloc(dev, sizeof(*init_data), GFP_KERNEL);
  814. if (!init_data)
  815. return -ENOMEM;
  816. init_data->constraints.valid_ops_mask |= REGULATOR_CHANGE_STATUS;
  817. qphy->dpdm_rdesc.owner = THIS_MODULE;
  818. qphy->dpdm_rdesc.type = REGULATOR_VOLTAGE;
  819. qphy->dpdm_rdesc.ops = &qusb_phy_dpdm_regulator_ops;
  820. qphy->dpdm_rdesc.name = kbasename(dev->of_node->full_name);
  821. cfg.dev = dev;
  822. cfg.init_data = init_data;
  823. cfg.driver_data = qphy;
  824. cfg.of_node = dev->of_node;
  825. qphy->dpdm_rdev = devm_regulator_register(dev, &qphy->dpdm_rdesc, &cfg);
  826. return PTR_ERR_OR_ZERO(qphy->dpdm_rdev);
  827. }
  828. static void qusb_phy_create_debugfs(struct qusb_phy *qphy)
  829. {
  830. qphy->root = debugfs_create_dir(dev_name(qphy->phy.dev), NULL);
  831. debugfs_create_x8("tune1", 0644, qphy->root, &qphy->tune1);
  832. debugfs_create_x8("tune2", 0644, qphy->root, &qphy->tune2);
  833. debugfs_create_x8("tune3", 0644, qphy->root, &qphy->tune3);
  834. debugfs_create_x8("tune4", 0644, qphy->root, &qphy->tune4);
  835. debugfs_create_x8("tune5", 0644, qphy->root, &qphy->tune5);
  836. }
  837. static int qusb_phy_vbus_notifier(struct notifier_block *nb,
  838. unsigned long event, void *data)
  839. {
  840. struct usb_phy *phy = container_of(nb, struct usb_phy, vbus_nb);
  841. struct qusb_phy *qphy = container_of(phy, struct qusb_phy, phy);
  842. if (!qphy || !data) {
  843. pr_err("Failed to get PHY for vbus_notifier\n");
  844. return NOTIFY_DONE;
  845. }
  846. qphy->vbus_active = !!event;
  847. dev_dbg(qphy->phy.dev, "Got VBUS notification: %u\n", event);
  848. queue_delayed_work(system_freezable_wq, &qphy->port_det_w, 0);
  849. return NOTIFY_DONE;
  850. }
  851. static int qusb_phy_id_notifier(struct notifier_block *nb,
  852. unsigned long event, void *data)
  853. {
  854. struct usb_phy *phy = container_of(nb, struct usb_phy, id_nb);
  855. struct qusb_phy *qphy = container_of(phy, struct qusb_phy, phy);
  856. if (!qphy || !data) {
  857. pr_err("Failed to get PHY for vbus_notifier\n");
  858. return NOTIFY_DONE;
  859. }
  860. qphy->id_state = !event;
  861. dev_dbg(qphy->phy.dev, "Got id notification: %u\n", event);
  862. queue_delayed_work(system_freezable_wq, &qphy->port_det_w, 0);
  863. return NOTIFY_DONE;
  864. }
  865. static const unsigned int qusb_phy_extcon_cable[] = {
  866. EXTCON_USB,
  867. EXTCON_USB_HOST,
  868. EXTCON_NONE,
  869. };
  870. static int qusb_phy_notify_charger(struct qusb_phy *qphy,
  871. enum power_supply_type charger_type)
  872. {
  873. union power_supply_propval pval = {0};
  874. dev_dbg(qphy->phy.dev, "Notify charger type: %d\n", charger_type);
  875. if (!qphy->usb_psy) {
  876. qphy->usb_psy = power_supply_get_by_name("usb");
  877. if (!qphy->usb_psy) {
  878. dev_err(qphy->phy.dev, "Could not get usb psy\n");
  879. return -ENODEV;
  880. }
  881. }
  882. pval.intval = charger_type;
  883. power_supply_set_property(qphy->usb_psy, POWER_SUPPLY_PROP_USB_TYPE,
  884. &pval);
  885. return 0;
  886. }
  887. static void qusb_phy_notify_extcon(struct qusb_phy *qphy,
  888. int extcon_id, int event)
  889. {
  890. struct extcon_dev *edev = qphy->phy.edev;
  891. union extcon_property_value val;
  892. int ret;
  893. dev_dbg(qphy->phy.dev, "Notify event: %d for extcon_id: %d\n",
  894. event, extcon_id);
  895. if (event) {
  896. ret = extcon_get_property(edev, extcon_id,
  897. EXTCON_PROP_USB_TYPEC_POLARITY, &val);
  898. if (ret)
  899. dev_err(qphy->phy.dev, "Failed to get TYPEC POLARITY\n");
  900. extcon_set_property(qphy->usb_extcon, extcon_id,
  901. EXTCON_PROP_USB_TYPEC_POLARITY, val);
  902. ret = extcon_get_property(edev, extcon_id,
  903. EXTCON_PROP_USB_SS, &val);
  904. if (ret)
  905. dev_err(qphy->phy.dev, "Failed to get USB_SS property\n");
  906. extcon_set_property(qphy->usb_extcon, extcon_id,
  907. EXTCON_PROP_USB_SS, val);
  908. }
  909. extcon_set_state_sync(qphy->usb_extcon, extcon_id, event);
  910. }
  911. static bool qusb_phy_chg_det_status(struct qusb_phy *qphy,
  912. enum chg_det_state state)
  913. {
  914. u32 reg, status;
  915. reg = readl_relaxed(qphy->base + QUSB2PHY_PORT_INTR_STATUS);
  916. dev_dbg(qphy->phy.dev, "state: %d reg: 0x%x\n", state, reg);
  917. status = reg & 0xff;
  918. switch (state) {
  919. case STATE_DCD:
  920. return (status != DPSE_INTR_HIGH);
  921. case STATE_PRIMARY:
  922. return (status && (status != DPSE_INTR_HIGH));
  923. case STATE_SECONDARY:
  924. return status;
  925. case STATE_UNKNOWN:
  926. default:
  927. break;
  928. }
  929. return false;
  930. }
  931. /*
  932. * Different circuit blocks are enabled on DP and DM lines as part
  933. * of different phases of charger detection. Then the state of
  934. * DP and DM lines are monitored to identify different type of
  935. * chargers.
  936. * These circuit blocks can be enabled with the configuration of
  937. * the QUICKCHARGE1 and QUICKCHARGE2 registers and the DP/DM lines
  938. * can be monitored with the status of the INTR_STATUS register.
  939. */
  940. static void qusb_phy_chg_det_enable_seq(struct qusb_phy *qphy, int state)
  941. {
  942. dev_dbg(qphy->phy.dev, "state: %d\n", state);
  943. /* Power down the PHY*/
  944. writel_relaxed(0x23, qphy->base + QUSB2PHY_PORT_POWERDOWN);
  945. /* Put the PHY in non driving mode */
  946. writel_relaxed(0x35, qphy->base + QUSB2PHY_PORT_UTMI_CTRL1);
  947. /* Set the PHY to register mode */
  948. writel_relaxed(0xC0, qphy->base + QUSB2PHY_PORT_UTMI_CTRL2);
  949. /* Keep PLL in reset */
  950. writel_relaxed(0x05, qphy->base + QUSB2PHY_PLL_AUTOPGM_CTL1);
  951. /* Enable PHY */
  952. writel_relaxed(0x22, qphy->base + QUSB2PHY_PORT_POWERDOWN);
  953. writel_relaxed(0x17, qphy->base + QUSB2PHY_PLL_PWR_CTL);
  954. usleep_range(5, 10);
  955. writel_relaxed(0x15, qphy->base + QUSB2PHY_PLL_AUTOPGM_CTL1);
  956. writel_relaxed(0x00, qphy->base + QUSB2PHY_PORT_QC1);
  957. writel_relaxed(0x00, qphy->base + QUSB2PHY_PORT_QC2);
  958. usleep_range(50, 60);
  959. switch (state) {
  960. case STATE_DCD:
  961. /* Enable IDP_SRC */
  962. writel_relaxed(0x08, qphy->base + QUSB2PHY_PORT_QC1);
  963. /* Enable RDM_UP */
  964. writel_relaxed(0x01, qphy->base + QUSB2PHY_PORT_QC2);
  965. writel_relaxed(0x1F, qphy->base + QUSB2PHY_PORT_INTR_CTRL);
  966. break;
  967. case STATE_PRIMARY:
  968. /* Enable VDAT_REF_DM, VDP_SRC and IDM_SINK */
  969. writel_relaxed(0x25, qphy->base + QUSB2PHY_PORT_QC1);
  970. writel_relaxed(0x00, qphy->base + QUSB2PHY_PORT_QC2);
  971. writel_relaxed(0x1F, qphy->base + QUSB2PHY_PORT_INTR_CTRL);
  972. break;
  973. case STATE_SECONDARY:
  974. /* Enable VDAT_REF_DP, VDAT_REF_DM, VDP_SRC and IDM_SINK */
  975. writel_relaxed(0x72, qphy->base + QUSB2PHY_PORT_QC1);
  976. writel_relaxed(0x00, qphy->base + QUSB2PHY_PORT_QC2);
  977. writel_relaxed(0x1F, qphy->base + QUSB2PHY_PORT_INTR_CTRL);
  978. break;
  979. case STATE_UNKNOWN:
  980. default:
  981. break;
  982. }
  983. }
  984. #define CHG_DCD_TIMEOUT_MSEC 750
  985. #define CHG_DCD_POLL_TIME_MSEC 50
  986. #define CHG_PRIMARY_DET_TIME_MSEC 100
  987. #define CHG_SECONDARY_DET_TIME_MSEC 100
  988. static int qusb_phy_enable_phy(struct qusb_phy *qphy)
  989. {
  990. int ret;
  991. ret = qusb_phy_enable_power(qphy, true);
  992. if (ret)
  993. return ret;
  994. if (qphy->tcsr_clamp_dig_n)
  995. writel_relaxed(0x1, qphy->tcsr_clamp_dig_n);
  996. qusb_phy_enable_clocks(qphy, true);
  997. return 0;
  998. }
  999. static void qusb_phy_disable_phy(struct qusb_phy *qphy)
  1000. {
  1001. int ret;
  1002. ret = reset_control_assert(qphy->phy_reset);
  1003. if (ret)
  1004. dev_err(qphy->phy.dev, "phyassert failed\n");
  1005. usleep_range(100, 150);
  1006. ret = reset_control_deassert(qphy->phy_reset);
  1007. if (ret)
  1008. dev_err(qphy->phy.dev, "deassert failed\n");
  1009. qusb_phy_enable_clocks(qphy, false);
  1010. if (qphy->tcsr_clamp_dig_n)
  1011. writel_relaxed(0x0, qphy->tcsr_clamp_dig_n);
  1012. qusb_phy_enable_power(qphy, false);
  1013. }
  1014. static void qusb_phy_port_state_work(struct work_struct *w)
  1015. {
  1016. struct qusb_phy *qphy = container_of(w, struct qusb_phy,
  1017. port_det_w.work);
  1018. unsigned long delay = 0;
  1019. int status, ret;
  1020. dev_dbg(qphy->phy.dev, "state: %d\n", qphy->port_state);
  1021. switch (qphy->port_state) {
  1022. case PORT_UNKNOWN:
  1023. if (!qphy->id_state) {
  1024. qphy->port_state = PORT_HOST_MODE;
  1025. qusb_phy_notify_extcon(qphy, EXTCON_USB_HOST, 1);
  1026. return;
  1027. }
  1028. if (qphy->vbus_active) {
  1029. /* Enable DCD sequence */
  1030. ret = qusb_phy_enable_phy(qphy);
  1031. if (ret)
  1032. return;
  1033. qusb_phy_chg_det_enable_seq(qphy, STATE_DCD);
  1034. qphy->port_state = PORT_DCD_IN_PROGRESS;
  1035. qphy->dcd_timeout = 0;
  1036. delay = CHG_DCD_POLL_TIME_MSEC;
  1037. break;
  1038. }
  1039. return;
  1040. case PORT_DISCONNECTED:
  1041. qusb_phy_disable_phy(qphy);
  1042. qphy->port_state = PORT_UNKNOWN;
  1043. break;
  1044. case PORT_DCD_IN_PROGRESS:
  1045. if (!qphy->vbus_active) {
  1046. /* Disable PHY sequence */
  1047. qphy->port_state = PORT_DISCONNECTED;
  1048. break;
  1049. }
  1050. status = qusb_phy_chg_det_status(qphy, STATE_DCD);
  1051. if (!status && qphy->dcd_timeout < CHG_DCD_TIMEOUT_MSEC) {
  1052. delay = CHG_DCD_POLL_TIME_MSEC;
  1053. qphy->dcd_timeout += delay;
  1054. } else if (status) {
  1055. qusb_phy_chg_det_enable_seq(qphy, STATE_PRIMARY);
  1056. qphy->port_state = PORT_PRIMARY_IN_PROGRESS;
  1057. delay = CHG_PRIMARY_DET_TIME_MSEC;
  1058. } else if (qphy->dcd_timeout >= CHG_DCD_TIMEOUT_MSEC) {
  1059. qusb_phy_notify_charger(qphy,
  1060. POWER_SUPPLY_TYPE_USB_DCP);
  1061. qusb_phy_disable_phy(qphy);
  1062. qphy->port_state = PORT_CHG_DET_DONE;
  1063. }
  1064. break;
  1065. case PORT_PRIMARY_IN_PROGRESS:
  1066. if (!qphy->vbus_active) {
  1067. qphy->port_state = PORT_DISCONNECTED;
  1068. break;
  1069. }
  1070. status = qusb_phy_chg_det_status(qphy, STATE_PRIMARY);
  1071. if (status) {
  1072. qusb_phy_chg_det_enable_seq(qphy, STATE_SECONDARY);
  1073. qphy->port_state = PORT_SECONDARY_IN_PROGRESS;
  1074. delay = CHG_SECONDARY_DET_TIME_MSEC;
  1075. } else {
  1076. qusb_phy_disable_phy(qphy);
  1077. qusb_phy_notify_charger(qphy, POWER_SUPPLY_TYPE_USB);
  1078. qusb_phy_notify_extcon(qphy, EXTCON_USB, 1);
  1079. qphy->port_state = PORT_CHG_DET_DONE;
  1080. }
  1081. break;
  1082. case PORT_SECONDARY_IN_PROGRESS:
  1083. if (!qphy->vbus_active) {
  1084. qphy->port_state = PORT_DISCONNECTED;
  1085. break;
  1086. }
  1087. status = qusb_phy_chg_det_status(qphy, STATE_SECONDARY);
  1088. if (status) {
  1089. qusb_phy_notify_charger(qphy,
  1090. POWER_SUPPLY_TYPE_USB_DCP);
  1091. } else {
  1092. qusb_phy_notify_charger(qphy,
  1093. POWER_SUPPLY_TYPE_USB_CDP);
  1094. qusb_phy_notify_extcon(qphy, EXTCON_USB, 1);
  1095. }
  1096. qusb_phy_disable_phy(qphy);
  1097. qphy->port_state = PORT_CHG_DET_DONE;
  1098. break;
  1099. case PORT_CHG_DET_DONE:
  1100. if (!qphy->vbus_active) {
  1101. qphy->port_state = PORT_UNKNOWN;
  1102. qusb_phy_notify_extcon(qphy, EXTCON_USB, 0);
  1103. }
  1104. return;
  1105. case PORT_HOST_MODE:
  1106. if (qphy->id_state) {
  1107. qphy->port_state = PORT_UNKNOWN;
  1108. qusb_phy_notify_extcon(qphy, EXTCON_USB_HOST, 0);
  1109. }
  1110. if (!qphy->vbus_active)
  1111. return;
  1112. break;
  1113. default:
  1114. return;
  1115. }
  1116. queue_delayed_work(system_freezable_wq,
  1117. &qphy->port_det_w, msecs_to_jiffies(delay));
  1118. }
  1119. static int qusb_phy_extcon_register(struct qusb_phy *qphy)
  1120. {
  1121. int ret;
  1122. /* Register extcon for notifications from charger driver */
  1123. qphy->phy.vbus_nb.notifier_call = qusb_phy_vbus_notifier;
  1124. qphy->phy.id_nb.notifier_call = qusb_phy_id_notifier;
  1125. /* Register extcon to notify USB driver */
  1126. qphy->usb_extcon = devm_extcon_dev_allocate(qphy->phy.dev,
  1127. qusb_phy_extcon_cable);
  1128. if (IS_ERR(qphy->usb_extcon)) {
  1129. dev_err(qphy->phy.dev, "failed to allocate extcon device\n");
  1130. return PTR_ERR(qphy->usb_extcon);
  1131. }
  1132. ret = devm_extcon_dev_register(qphy->phy.dev, qphy->usb_extcon);
  1133. if (ret) {
  1134. dev_err(qphy->phy.dev, "failed to register extcon device\n");
  1135. return ret;
  1136. }
  1137. extcon_set_property_capability(qphy->usb_extcon, EXTCON_USB,
  1138. EXTCON_PROP_USB_TYPEC_POLARITY);
  1139. extcon_set_property_capability(qphy->usb_extcon, EXTCON_USB,
  1140. EXTCON_PROP_USB_SS);
  1141. extcon_set_property_capability(qphy->usb_extcon, EXTCON_USB_HOST,
  1142. EXTCON_PROP_USB_TYPEC_POLARITY);
  1143. extcon_set_property_capability(qphy->usb_extcon, EXTCON_USB_HOST,
  1144. EXTCON_PROP_USB_SS);
  1145. return 0;
  1146. }
  1147. static int qusb_phy_probe(struct platform_device *pdev)
  1148. {
  1149. struct qusb_phy *qphy;
  1150. struct device *dev = &pdev->dev;
  1151. struct resource *res;
  1152. int ret = 0, size = 0;
  1153. const char *phy_type;
  1154. bool hold_phy_reset;
  1155. u32 temp;
  1156. qphy = devm_kzalloc(dev, sizeof(*qphy), GFP_KERNEL);
  1157. if (!qphy)
  1158. return -ENOMEM;
  1159. qphy->phy.dev = dev;
  1160. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1161. "qusb_phy_base");
  1162. qphy->base = devm_ioremap_resource(dev, res);
  1163. if (IS_ERR(qphy->base))
  1164. return PTR_ERR(qphy->base);
  1165. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1166. "tune2_efuse_addr");
  1167. if (res) {
  1168. qphy->tune2_efuse_reg = devm_ioremap(dev, res->start,
  1169. resource_size(res));
  1170. if (!IS_ERR_OR_NULL(qphy->tune2_efuse_reg)) {
  1171. ret = of_property_read_u32(dev->of_node,
  1172. "qcom,tune2-efuse-bit-pos",
  1173. &qphy->tune2_efuse_bit_pos);
  1174. if (!ret) {
  1175. ret = of_property_read_u32(dev->of_node,
  1176. "qcom,tune2-efuse-num-bits",
  1177. &qphy->tune2_efuse_num_of_bits);
  1178. }
  1179. of_property_read_u32(dev->of_node,
  1180. "qcom,tune2-efuse-correction",
  1181. &qphy->tune2_efuse_correction);
  1182. if (ret) {
  1183. dev_err(dev, "DT Value for tune2 efuse is invalid.\n");
  1184. return -EINVAL;
  1185. }
  1186. }
  1187. }
  1188. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1189. "eud_enable_reg");
  1190. if (res) {
  1191. qphy->eud_enable_reg = devm_ioremap_resource(dev, res);
  1192. if (IS_ERR(qphy->eud_enable_reg)) {
  1193. dev_err(dev, "err getting eud_enable_reg address\n");
  1194. return PTR_ERR(qphy->eud_enable_reg);
  1195. }
  1196. }
  1197. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1198. "ref_clk_addr");
  1199. if (res) {
  1200. qphy->ref_clk_base = devm_ioremap(dev,
  1201. res->start, resource_size(res));
  1202. if (IS_ERR(qphy->ref_clk_base)) {
  1203. dev_dbg(dev, "ref_clk_address is not available.\n");
  1204. return PTR_ERR(qphy->ref_clk_base);
  1205. }
  1206. ret = of_property_read_string(dev->of_node,
  1207. "qcom,phy-clk-scheme", &phy_type);
  1208. if (ret) {
  1209. dev_err(dev, "error need qsub_phy_clk_scheme.\n");
  1210. return ret;
  1211. }
  1212. if (!strcasecmp(phy_type, "cml")) {
  1213. qphy->is_se_clk = false;
  1214. } else if (!strcasecmp(phy_type, "cmos")) {
  1215. qphy->is_se_clk = true;
  1216. } else {
  1217. dev_err(dev, "erro invalid qusb_phy_clk_scheme\n");
  1218. return -EINVAL;
  1219. }
  1220. }
  1221. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1222. "tcsr_clamp_dig_n_1p8");
  1223. if (res) {
  1224. qphy->tcsr_clamp_dig_n = devm_ioremap(dev,
  1225. res->start, resource_size(res));
  1226. if (IS_ERR(qphy->tcsr_clamp_dig_n)) {
  1227. dev_err(dev, "err reading tcsr_clamp_dig_n\n");
  1228. qphy->tcsr_clamp_dig_n = NULL;
  1229. }
  1230. }
  1231. qphy->scm_lvl_shifter = of_property_read_bool(dev->of_node,
  1232. "qcom,secure-level-shifter");
  1233. ret = of_property_read_u32(dev->of_node, "qcom,usb-hs-ac-bitmask",
  1234. &qphy->usb_hs_ac_bitmask);
  1235. if (!ret) {
  1236. ret = of_property_read_u32(dev->of_node, "qcom,usb-hs-ac-value",
  1237. &qphy->usb_hs_ac_value);
  1238. if (ret) {
  1239. dev_err(dev, "%s usb_hs_ac_value not passed\n", __func__);
  1240. return ret;
  1241. }
  1242. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1243. "tcsr_conn_box_spare_0");
  1244. if (!res) {
  1245. dev_err(dev, "%s tcsr_conn_box_spare_0 not passed\n",
  1246. __func__);
  1247. return -ENOENT;
  1248. }
  1249. qphy->tcsr_conn_box_spare = devm_ioremap(dev,
  1250. res->start, resource_size(res));
  1251. if (IS_ERR(qphy->tcsr_conn_box_spare)) {
  1252. dev_err(dev, "err reading tcsr_conn_box_spare\n");
  1253. return PTR_ERR(qphy->tcsr_conn_box_spare);
  1254. }
  1255. }
  1256. qphy->ref_clk_src = devm_clk_get(dev, "ref_clk_src");
  1257. if (IS_ERR(qphy->ref_clk_src)) {
  1258. qphy->ref_clk_src = NULL;
  1259. dev_dbg(dev, "clk get failed for ref_clk_src\n");
  1260. }
  1261. /* ref_clk is needed only for DIFF_CLK case, hence make it optional. */
  1262. if (of_property_match_string(pdev->dev.of_node,
  1263. "clock-names", "ref_clk") >= 0) {
  1264. qphy->ref_clk = devm_clk_get(dev, "ref_clk");
  1265. if (IS_ERR(qphy->ref_clk)) {
  1266. ret = PTR_ERR(qphy->ref_clk);
  1267. if (ret != -EPROBE_DEFER)
  1268. dev_dbg(dev,
  1269. "clk get failed for ref_clk\n");
  1270. return ret;
  1271. }
  1272. clk_set_rate(qphy->ref_clk, 19200000);
  1273. }
  1274. qphy->cfg_ahb_clk = devm_clk_get(dev, "cfg_ahb_clk");
  1275. if (IS_ERR(qphy->cfg_ahb_clk))
  1276. return PTR_ERR(qphy->cfg_ahb_clk);
  1277. qphy->phy_reset = devm_reset_control_get(dev, "phy_reset");
  1278. if (IS_ERR(qphy->phy_reset))
  1279. return PTR_ERR(qphy->phy_reset);
  1280. if (of_property_match_string(dev->of_node,
  1281. "clock-names", "iface_clk") >= 0) {
  1282. qphy->iface_clk = devm_clk_get(dev, "iface_clk");
  1283. if (IS_ERR(qphy->iface_clk)) {
  1284. ret = PTR_ERR(qphy->iface_clk);
  1285. qphy->iface_clk = NULL;
  1286. if (ret == -EPROBE_DEFER)
  1287. return ret;
  1288. dev_err(dev, "couldn't get iface_clk(%d)\n", ret);
  1289. }
  1290. }
  1291. if (of_property_match_string(dev->of_node,
  1292. "clock-names", "core_clk") >= 0) {
  1293. qphy->core_clk = devm_clk_get(dev, "core_clk");
  1294. if (IS_ERR(qphy->core_clk)) {
  1295. ret = PTR_ERR(qphy->core_clk);
  1296. qphy->core_clk = NULL;
  1297. if (ret == -EPROBE_DEFER)
  1298. return ret;
  1299. dev_err(dev, "couldn't get core_clk(%d)\n", ret);
  1300. }
  1301. }
  1302. qphy->gdsc = devm_regulator_get(dev, "USB3_GDSC");
  1303. if (IS_ERR(qphy->gdsc))
  1304. qphy->gdsc = NULL;
  1305. size = 0;
  1306. of_get_property(dev->of_node, "qcom,qusb-phy-init-seq", &size);
  1307. if (size) {
  1308. qphy->qusb_phy_init_seq = devm_kzalloc(dev,
  1309. size, GFP_KERNEL);
  1310. if (qphy->qusb_phy_init_seq) {
  1311. qphy->init_seq_len =
  1312. (size / sizeof(*qphy->qusb_phy_init_seq));
  1313. if (qphy->init_seq_len % 2) {
  1314. dev_err(dev, "invalid init_seq_len\n");
  1315. return -EINVAL;
  1316. }
  1317. of_property_read_u32_array(dev->of_node,
  1318. "qcom,qusb-phy-init-seq",
  1319. qphy->qusb_phy_init_seq,
  1320. qphy->init_seq_len);
  1321. } else {
  1322. dev_err(dev, "error allocating memory for phy_init_seq\n");
  1323. }
  1324. }
  1325. qphy->ulpi_mode = false;
  1326. ret = of_property_read_string(dev->of_node, "phy_type", &phy_type);
  1327. if (!ret) {
  1328. if (!strcasecmp(phy_type, "ulpi"))
  1329. qphy->ulpi_mode = true;
  1330. } else {
  1331. dev_err(dev, "error reading phy_type property\n");
  1332. return ret;
  1333. }
  1334. hold_phy_reset = of_property_read_bool(dev->of_node, "qcom,hold-reset");
  1335. /* use default major revision as 2 */
  1336. qphy->major_rev = 2;
  1337. ret = of_property_read_u32(dev->of_node, "qcom,major-rev",
  1338. &qphy->major_rev);
  1339. ret = of_property_read_u32_array(dev->of_node, "qcom,vdd-voltage-level",
  1340. (u32 *) qphy->vdd_levels,
  1341. ARRAY_SIZE(qphy->vdd_levels));
  1342. if (ret) {
  1343. dev_err(dev, "error reading qcom,vdd-voltage-level property\n");
  1344. return ret;
  1345. }
  1346. qphy->vdd = devm_regulator_get(dev, "vdd");
  1347. if (IS_ERR(qphy->vdd)) {
  1348. dev_err(dev, "unable to get vdd supply\n");
  1349. return PTR_ERR(qphy->vdd);
  1350. }
  1351. qphy->vdda33 = devm_regulator_get(dev, "vdda33");
  1352. if (IS_ERR(qphy->vdda33)) {
  1353. dev_err(dev, "unable to get vdda33 supply\n");
  1354. return PTR_ERR(qphy->vdda33);
  1355. }
  1356. qphy->vdda18 = devm_regulator_get(dev, "vdda18");
  1357. if (IS_ERR(qphy->vdda18)) {
  1358. dev_err(dev, "unable to get vdda18 supply\n");
  1359. return PTR_ERR(qphy->vdda18);
  1360. }
  1361. mutex_init(&qphy->phy_lock);
  1362. platform_set_drvdata(pdev, qphy);
  1363. qphy->phy.label = "msm-qusb-phy";
  1364. qphy->phy.init = qusb_phy_init;
  1365. qphy->phy.set_suspend = qusb_phy_set_suspend;
  1366. qphy->phy.shutdown = qusb_phy_shutdown;
  1367. qphy->phy.type = USB_PHY_TYPE_USB2;
  1368. qphy->phy.notify_connect = qusb_phy_notify_connect;
  1369. qphy->phy.notify_disconnect = qusb_phy_notify_disconnect;
  1370. qphy->phy.charger_detect = qusb_phy_drive_dp_pulse;
  1371. /*
  1372. * On some platforms multiple QUSB PHYs are available. If QUSB PHY is
  1373. * not used, there is leakage current seen with QUSB PHY related voltage
  1374. * rail. Hence keep QUSB PHY into reset state explicitly here.
  1375. */
  1376. if (hold_phy_reset) {
  1377. ret = reset_control_assert(qphy->phy_reset);
  1378. if (ret)
  1379. dev_err(dev, "%s:phy_reset assert failed\n", __func__);
  1380. }
  1381. if (of_property_read_bool(dev->of_node, "extcon")) {
  1382. INIT_DELAYED_WORK(&qphy->port_det_w, qusb_phy_port_state_work);
  1383. ret = qusb_phy_extcon_register(qphy);
  1384. if (ret)
  1385. return ret;
  1386. }
  1387. ret = usb_add_phy_dev(&qphy->phy);
  1388. if (ret)
  1389. return ret;
  1390. ret = qusb_phy_regulator_init(qphy);
  1391. if (ret)
  1392. usb_remove_phy(&qphy->phy);
  1393. /* de-assert clamp dig n to reduce leakage on 1p8 upon boot up */
  1394. if (qphy->tcsr_clamp_dig_n)
  1395. writel_relaxed(0x0, qphy->tcsr_clamp_dig_n);
  1396. /*
  1397. * Write the usb_hs_ac_value to usb_hs_ac_bitmask of tcsr_conn_box_spare
  1398. * reg to enable AC/DC coupling
  1399. */
  1400. if (qphy->tcsr_conn_box_spare) {
  1401. temp = readl_relaxed(qphy->tcsr_conn_box_spare) &
  1402. ~qphy->usb_hs_ac_bitmask;
  1403. writel_relaxed(temp | qphy->usb_hs_ac_value,
  1404. qphy->tcsr_conn_box_spare);
  1405. }
  1406. qphy->suspended = true;
  1407. if (of_property_read_bool(dev->of_node, "extcon")) {
  1408. qphy->id_state = true;
  1409. qphy->vbus_active = false;
  1410. if (extcon_get_state(qphy->phy.edev, EXTCON_USB_HOST)) {
  1411. qusb_phy_id_notifier(&qphy->phy.id_nb,
  1412. 1, qphy->phy.edev);
  1413. } else if (extcon_get_state(qphy->phy.edev, EXTCON_USB)) {
  1414. qusb_phy_vbus_notifier(&qphy->phy.vbus_nb,
  1415. 1, qphy->phy.edev);
  1416. }
  1417. }
  1418. qusb_phy_create_debugfs(qphy);
  1419. return ret;
  1420. }
  1421. static int qusb_phy_remove(struct platform_device *pdev)
  1422. {
  1423. struct qusb_phy *qphy = platform_get_drvdata(pdev);
  1424. debugfs_remove_recursive(qphy->root);
  1425. usb_remove_phy(&qphy->phy);
  1426. qphy->cable_connected = false;
  1427. qusb_phy_set_suspend(&qphy->phy, true);
  1428. return 0;
  1429. }
  1430. static const struct of_device_id qusb_phy_id_table[] = {
  1431. { .compatible = "qcom,qusb2phy", },
  1432. { },
  1433. };
  1434. MODULE_DEVICE_TABLE(of, qusb_phy_id_table);
  1435. static struct platform_driver qusb_phy_driver = {
  1436. .probe = qusb_phy_probe,
  1437. .remove = qusb_phy_remove,
  1438. .driver = {
  1439. .name = "msm-qusb-phy",
  1440. .of_match_table = of_match_ptr(qusb_phy_id_table),
  1441. },
  1442. };
  1443. module_platform_driver(qusb_phy_driver);
  1444. MODULE_DESCRIPTION("MSM QUSB2 PHY driver");
  1445. MODULE_LICENSE("GPL");