phy-msm-ssusb-qmp.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2013-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2024, 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/io.h>
  11. #include <linux/of.h>
  12. #include <linux/of_device.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/regulator/consumer.h>
  15. #include <linux/usb/phy.h>
  16. #include <linux/usb/dwc3-msm.h>
  17. #include <linux/clk.h>
  18. #include <linux/extcon.h>
  19. #include <linux/reset.h>
  20. #if IS_ENABLED(CONFIG_USB_PHY_TUNING_QCOM)
  21. #include <linux/mutex.h>
  22. #endif
  23. enum core_ldo_levels {
  24. CORE_LEVEL_NONE = 0,
  25. CORE_LEVEL_MIN,
  26. CORE_LEVEL_MAX,
  27. };
  28. #define INIT_MAX_TIME_USEC 1000
  29. /* default CORE votlage and load values */
  30. #define USB_SSPHY_1P2_VOL_MIN 1200000 /* uV */
  31. #define USB_SSPHY_1P2_VOL_MAX 1200000 /* uV */
  32. #define USB_SSPHY_HPM_LOAD 30000 /* uA */
  33. /* defining load value for Refgen */
  34. #define USB3PHY_REFGEN_HPM_LOAD 1200000 /* uA */
  35. /* USB3PHY_PCIE_USB3_PCS_PCS_STATUS bit */
  36. #define PHYSTATUS BIT(6)
  37. /* PCIE_USB3_PHY_AUTONOMOUS_MODE_CTRL bits */
  38. #define ARCVR_DTCT_EN BIT(0)
  39. #define ALFPS_DTCT_EN BIT(1)
  40. #define ARCVR_DTCT_EVENT_SEL BIT(4)
  41. /*
  42. * register bits
  43. * PCIE_USB3_PHY_PCS_MISC_TYPEC_CTRL - for QMP USB PHY
  44. * USB3_DP_COM_PHY_MODE_CTRL - for QMP USB DP Combo PHY
  45. */
  46. /* 0 - selects Lane A. 1 - selects Lane B */
  47. #define SW_PORTSELECT BIT(0)
  48. /* port select mux: 1 - sw control. 0 - HW control*/
  49. #define SW_PORTSELECT_MX BIT(1)
  50. /* USB3_DP_PHY_USB3_DP_COM_SWI_CTRL bits */
  51. /* LANE related register read/write with USB3 */
  52. #define USB3_SWI_ACT_ACCESS_EN BIT(0)
  53. /* LANE related register read/write with DP */
  54. #define DP_SWI_ACT_ACCESS_EN BIT(1)
  55. /* USB3_DP_COM_RESET_OVRD_CTRL bits */
  56. /* DP PHY soft reset */
  57. #define SW_DPPHY_RESET BIT(0)
  58. /* mux to select DP PHY reset control, 0:HW control, 1: software reset */
  59. #define SW_DPPHY_RESET_MUX BIT(1)
  60. /* USB3 PHY soft reset */
  61. #define SW_USB3PHY_RESET BIT(2)
  62. /* mux to select USB3 PHY reset control, 0:HW control, 1: software reset */
  63. #define SW_USB3PHY_RESET_MUX BIT(3)
  64. /* USB3_DP_COM_PHY_MODE_CTRL bits */
  65. #define USB3_MODE BIT(0) /* enables USB3 mode */
  66. #define DP_MODE BIT(1) /* enables DP mode */
  67. #define USB3_DP_COMBO_MODE (USB3_MODE | DP_MODE) /*enables combo mode */
  68. /* USB3_DP_COM_TYPEC_STATUS */
  69. #define PORTSELECT_RAW BIT(0)
  70. #if IS_ENABLED(CONFIG_USB_PHY_TUNING_QCOM)
  71. #define ADDRESS_START 0
  72. #define ADDRESS_END 0x1FFC
  73. #define TUNE_BUF_COUNT 20
  74. #define TUNE_BUF_SIZE 25
  75. #endif
  76. enum qmp_phy_rev_reg {
  77. USB3_PHY_PCS_STATUS,
  78. USB3_PHY_AUTONOMOUS_MODE_CTRL,
  79. USB3_PHY_LFPS_RXTERM_IRQ_CLEAR,
  80. USB3_PHY_POWER_DOWN_CONTROL,
  81. USB3_PHY_SW_RESET,
  82. USB3_PHY_START,
  83. /* TypeC port select configuration (optional) */
  84. USB3_PHY_PCS_MISC_TYPEC_CTRL,
  85. /* USB DP Combo PHY related */
  86. USB3_DP_COM_POWER_DOWN_CTRL,
  87. USB3_DP_COM_SW_RESET,
  88. USB3_DP_COM_RESET_OVRD_CTRL,
  89. USB3_DP_COM_PHY_MODE_CTRL,
  90. USB3_DP_COM_TYPEC_CTRL,
  91. USB3_PCS_MISC_CLAMP_ENABLE,
  92. USB3_DP_COM_TYPEC_STATUS,
  93. USB3_PHY_REG_MAX,
  94. };
  95. #define PHY_REG_SIZE (USB3_PHY_REG_MAX * sizeof(u32))
  96. enum qmp_phy_type {
  97. USB3,
  98. USB3_OR_DP,
  99. USB3_AND_DP,
  100. };
  101. /* reg values to write */
  102. struct qmp_reg_val {
  103. u32 offset;
  104. u32 val;
  105. };
  106. struct msm_ssphy_qmp {
  107. struct usb_phy phy;
  108. void __iomem *base;
  109. void __iomem *vls_clamp_reg;
  110. void __iomem *pcs_clamp_enable_reg;
  111. void __iomem *tcsr_usb3_dp_phymode;
  112. struct regulator *vdd;
  113. int vdd_levels[3]; /* none, low, high */
  114. int refgen_levels[3]; /* 0, REFGEN_VOL_MIN, REFGEN_VOL_MAX */
  115. int vdd_max_uA;
  116. struct regulator *core_ldo;
  117. int core_voltage_levels[3];
  118. int core_max_uA;
  119. struct regulator *usb3_dp_phy_gdsc;
  120. struct regulator *refgen;
  121. struct clk *ref_clk_src;
  122. struct clk *ref_clk;
  123. struct clk *aux_clk;
  124. struct clk *com_aux_clk;
  125. struct clk *cfg_ahb_clk;
  126. struct clk *pipe_clk;
  127. struct clk *pipe_clk_mux;
  128. struct clk *pipe_clk_ext_src;
  129. struct reset_control *phy_reset;
  130. struct reset_control *phy_phy_reset;
  131. struct reset_control *global_phy_reset;
  132. bool power_enabled;
  133. bool clk_enabled;
  134. bool cable_connected;
  135. bool in_suspend;
  136. u32 *phy_reg; /* revision based offset */
  137. int reg_offset_cnt;
  138. u32 *qmp_phy_init_seq;
  139. int init_seq_len;
  140. enum qmp_phy_type phy_type;
  141. #if IS_ENABLED(CONFIG_USB_PHY_TUNING_QCOM)
  142. struct mutex phy_tune_lock;
  143. u32 tune_addr;
  144. int tune_buf_cnt;
  145. int tune_buf[TUNE_BUF_COUNT][2];
  146. bool ssphy_tune_init_done;
  147. #endif
  148. };
  149. static const struct of_device_id msm_usb_id_table[] = {
  150. {
  151. .compatible = "qcom,usb-ssphy-qmp",
  152. },
  153. {
  154. .compatible = "qcom,usb-ssphy-qmp-v1",
  155. },
  156. {
  157. .compatible = "qcom,usb-ssphy-qmp-v2",
  158. },
  159. {
  160. .compatible = "qcom,usb-ssphy-qmp-dp-combo",
  161. },
  162. {
  163. .compatible = "qcom,usb-ssphy-qmp-usb3-or-dp",
  164. },
  165. { },
  166. };
  167. MODULE_DEVICE_TABLE(of, msm_usb_id_table);
  168. #undef dev_dbg
  169. #define dev_dbg dev_err
  170. #if IS_ENABLED(CONFIG_USB_PHY_TUNING_QCOM)
  171. static void ssphy_tune_buf_init(struct msm_ssphy_qmp *phy)
  172. {
  173. int i;
  174. for (i = 0; i < TUNE_BUF_COUNT; i++)
  175. phy->tune_buf[i][0] = phy->tune_buf[i][1] = 0;
  176. }
  177. static void ssphy_tune_set(struct msm_ssphy_qmp *phy)
  178. {
  179. int i;
  180. mutex_lock(&phy->phy_tune_lock);
  181. for (i = 0; i < phy->tune_buf_cnt; i++) {
  182. writel_relaxed(phy->tune_buf[i][1], phy->base + phy->tune_buf[i][0]);
  183. usleep_range(1, 10);
  184. pr_info("%s(): [%d] 0x%x 0x%x (%d/%d)\n", __func__, i, phy->tune_buf[i][0],
  185. (readl_relaxed(phy->base + phy->tune_buf[i][0]) & 0xff), phy->tune_buf_cnt, TUNE_BUF_COUNT);
  186. usleep_range(1, 2);
  187. }
  188. mutex_unlock(&phy->phy_tune_lock);
  189. }
  190. static ssize_t ssphy_read_show(struct device *dev,
  191. struct device_attribute *attr, char *buf)
  192. {
  193. struct msm_ssphy_qmp *phy = dev_get_drvdata(dev);
  194. if (!phy) {
  195. pr_err("ssphy is NULL\n");
  196. return -ENODEV;
  197. }
  198. return sprintf(buf, "0x%x 0x%x\n", phy->tune_addr,
  199. (readl_relaxed(phy->base + phy->tune_addr) & 0xff));
  200. }
  201. static ssize_t ssphy_read_store(struct device *dev,
  202. struct device_attribute *attr, const char *buf, size_t size)
  203. {
  204. struct msm_ssphy_qmp *phy = dev_get_drvdata(dev);
  205. u32 addr;
  206. if (!phy) {
  207. pr_err("ssphy is NULL\n");
  208. return -ENODEV;
  209. }
  210. sscanf(buf, "%x", &addr);
  211. pr_info("%s(): tuning address is set to 0x%x\n", __func__, addr);
  212. if (addr >= ADDRESS_START && addr <= ADDRESS_END && !(addr & 0x3))
  213. phy->tune_addr = addr;
  214. return size;
  215. }
  216. static DEVICE_ATTR_RW(ssphy_read);
  217. static ssize_t ssphy_set_show(struct device *dev,
  218. struct device_attribute *attr, char *buf)
  219. {
  220. struct msm_ssphy_qmp *phy = dev_get_drvdata(dev);
  221. char str[(TUNE_BUF_SIZE * TUNE_BUF_COUNT) + 35] = {0, };
  222. int i;
  223. if (!phy) {
  224. pr_err("ssphy is NULL\n");
  225. return -ENODEV;
  226. }
  227. mutex_lock(&phy->phy_tune_lock);
  228. sprintf(str, "\n Address Value Input [%2d/%2d]\n", phy->tune_buf_cnt, TUNE_BUF_COUNT);
  229. for (i = 0; i < phy->tune_buf_cnt; i++) {
  230. sprintf(str, "%s#%2d 0x%4x 0x%2x 0x%2x\n", str, i + 1, phy->tune_buf[i][0],
  231. (readl_relaxed(phy->base + phy->tune_buf[i][0]) & 0xff), phy->tune_buf[i][1]);
  232. }
  233. mutex_unlock(&phy->phy_tune_lock);
  234. return sprintf(buf, "%s\n", str);
  235. }
  236. static ssize_t ssphy_set_store(struct device *dev,
  237. struct device_attribute *attr, const char *buf, size_t size)
  238. {
  239. struct msm_ssphy_qmp *phy = dev_get_drvdata(dev);
  240. u32 addr, val;
  241. int i;
  242. if (!phy) {
  243. pr_err("ssphy is NULL\n");
  244. return -ENODEV;
  245. }
  246. sscanf(buf, "%x %x", &addr, &val);
  247. val = val & 0xff;
  248. mutex_lock(&phy->phy_tune_lock);
  249. if (addr >= ADDRESS_START && addr <= ADDRESS_END && !(addr & 0x3)) {
  250. for (i = 0; i < phy->tune_buf_cnt; i++) {
  251. if (phy->tune_buf[i][0] == addr) {
  252. writel_relaxed(val, phy->base + addr);
  253. phy->tune_buf[i][1] = val;
  254. usleep_range(1, 2);
  255. pr_info("%s(): [%d] 0x%x 0x%x (%d/%d)\n", __func__, i, addr,
  256. (readl_relaxed(phy->base + addr) & 0xff), phy->tune_buf_cnt, TUNE_BUF_COUNT);
  257. mutex_unlock(&phy->phy_tune_lock);
  258. return size;
  259. }
  260. }
  261. if (phy->tune_buf_cnt < TUNE_BUF_COUNT) {
  262. writel_relaxed(val, phy->base + addr);
  263. phy->tune_buf[i][0] = addr;
  264. phy->tune_buf[i][1] = val;
  265. usleep_range(1, 2);
  266. pr_info("%s(): [%d] 0x%x 0x%x (%d/%d)\n", __func__, i, addr,
  267. (readl_relaxed(phy->base + addr) & 0xff), phy->tune_buf_cnt, TUNE_BUF_COUNT);
  268. phy->tune_buf_cnt++;
  269. } else
  270. pr_info("%s(): tuning count is full\n", __func__);
  271. } else {
  272. pr_info("%s(): tuning address is invalid : 0x%x\n", __func__, addr);
  273. }
  274. mutex_unlock(&phy->phy_tune_lock);
  275. return size;
  276. }
  277. static DEVICE_ATTR_RW(ssphy_set);
  278. static struct attribute *ssphy_attrs[] = {
  279. &dev_attr_ssphy_read.attr,
  280. &dev_attr_ssphy_set.attr,
  281. NULL,
  282. };
  283. static struct attribute_group ssphy_attr_grp = {
  284. .attrs = ssphy_attrs,
  285. };
  286. #endif
  287. static void usb_qmp_powerup_phy(struct msm_ssphy_qmp *phy);
  288. static void msm_ssphy_qmp_enable_clks(struct msm_ssphy_qmp *phy, bool on);
  289. static int msm_ssphy_qmp_reset(struct usb_phy *uphy);
  290. static int msm_ssphy_qmp_dp_combo_reset(struct usb_phy *uphy);
  291. static inline char *get_cable_status_str(struct msm_ssphy_qmp *phy)
  292. {
  293. return phy->cable_connected ? "connected" : "disconnected";
  294. }
  295. static void msm_ssusb_qmp_clr_lfps_rxterm_int(struct msm_ssphy_qmp *phy)
  296. {
  297. writel_relaxed(1, phy->base +
  298. phy->phy_reg[USB3_PHY_LFPS_RXTERM_IRQ_CLEAR]);
  299. /* flush the previous write before next write */
  300. wmb();
  301. writel_relaxed(0, phy->base +
  302. phy->phy_reg[USB3_PHY_LFPS_RXTERM_IRQ_CLEAR]);
  303. }
  304. static void msm_ssusb_qmp_clamp_enable(struct msm_ssphy_qmp *phy, bool val)
  305. {
  306. switch (phy->phy_type) {
  307. case USB3_AND_DP:
  308. writel_relaxed(!val, phy->base +
  309. phy->phy_reg[USB3_PCS_MISC_CLAMP_ENABLE]);
  310. break;
  311. case USB3_OR_DP:
  312. case USB3:
  313. if (phy->vls_clamp_reg)
  314. writel_relaxed(!!val, phy->vls_clamp_reg);
  315. if (phy->pcs_clamp_enable_reg)
  316. writel_relaxed(!val, phy->pcs_clamp_enable_reg);
  317. break;
  318. default:
  319. break;
  320. }
  321. }
  322. static void msm_ssusb_qmp_enable_autonomous(struct msm_ssphy_qmp *phy,
  323. int enable)
  324. {
  325. u32 val;
  326. unsigned int autonomous_mode_offset =
  327. phy->phy_reg[USB3_PHY_AUTONOMOUS_MODE_CTRL];
  328. dev_dbg(phy->phy.dev, "enabling QMP autonomous mode with cable %s\n",
  329. get_cable_status_str(phy));
  330. if (enable) {
  331. msm_ssusb_qmp_clr_lfps_rxterm_int(phy);
  332. val = readl_relaxed(phy->base + autonomous_mode_offset);
  333. val |= ARCVR_DTCT_EN;
  334. if (phy->phy.flags & DEVICE_IN_SS_MODE) {
  335. val |= ALFPS_DTCT_EN;
  336. val &= ~ARCVR_DTCT_EVENT_SEL;
  337. } else {
  338. val &= ~ALFPS_DTCT_EN;
  339. val |= ARCVR_DTCT_EVENT_SEL;
  340. }
  341. writel_relaxed(val, phy->base + autonomous_mode_offset);
  342. msm_ssusb_qmp_clamp_enable(phy, true);
  343. } else {
  344. msm_ssusb_qmp_clamp_enable(phy, false);
  345. writel_relaxed(0, phy->base + autonomous_mode_offset);
  346. msm_ssusb_qmp_clr_lfps_rxterm_int(phy);
  347. }
  348. }
  349. static int msm_ssusb_qmp_gdsc(struct msm_ssphy_qmp *phy, bool on)
  350. {
  351. int ret;
  352. if (IS_ERR_OR_NULL(phy->usb3_dp_phy_gdsc))
  353. return 0;
  354. if (on)
  355. ret = regulator_enable(phy->usb3_dp_phy_gdsc);
  356. else
  357. ret = regulator_disable(phy->usb3_dp_phy_gdsc);
  358. if (ret)
  359. dev_err(phy->phy.dev, "err:%d fail to %s usb3_dp_phy_gdsc\n",
  360. ret, on ? "enable" : "disable");
  361. return ret;
  362. }
  363. static int msm_ssusb_qmp_ldo_enable(struct msm_ssphy_qmp *phy, int on)
  364. {
  365. int min, rc = 0;
  366. dev_dbg(phy->phy.dev, "reg (%s)\n", on ? "HPM" : "LPM");
  367. if (phy->power_enabled == on) {
  368. dev_dbg(phy->phy.dev, "PHYs' regulators status %d\n",
  369. phy->power_enabled);
  370. return 0;
  371. }
  372. phy->power_enabled = on;
  373. min = on ? 1 : 0; /* low or none? */
  374. if (!on) {
  375. if (phy->refgen)
  376. goto disable_refgen;
  377. else
  378. goto disable_regulators;
  379. }
  380. rc = msm_ssusb_qmp_gdsc(phy, true);
  381. if (rc < 0)
  382. return rc;
  383. rc = regulator_set_load(phy->vdd, phy->vdd_max_uA);
  384. if (rc < 0) {
  385. dev_err(phy->phy.dev, "Unable to set HPM of %s\n", "vdd");
  386. goto put_gdsc;
  387. }
  388. rc = regulator_set_voltage(phy->vdd, phy->vdd_levels[min],
  389. phy->vdd_levels[2]);
  390. if (rc) {
  391. dev_err(phy->phy.dev, "Unable to set voltage for %s\n", "vdd");
  392. goto put_vdd_lpm;
  393. }
  394. dev_dbg(phy->phy.dev, "min_vol:%d max_vol:%d\n",
  395. phy->vdd_levels[min], phy->vdd_levels[2]);
  396. rc = regulator_enable(phy->vdd);
  397. if (rc) {
  398. dev_err(phy->phy.dev, "Unable to enable %s\n", "vdd");
  399. goto unconfig_vdd;
  400. }
  401. rc = regulator_set_load(phy->core_ldo, phy->core_max_uA);
  402. if (rc < 0) {
  403. dev_err(phy->phy.dev, "Unable to set HPM of %s\n", "core_ldo");
  404. goto disable_vdd;
  405. }
  406. rc = regulator_set_voltage(phy->core_ldo,
  407. phy->core_voltage_levels[CORE_LEVEL_MIN],
  408. phy->core_voltage_levels[CORE_LEVEL_MAX]);
  409. if (rc) {
  410. dev_err(phy->phy.dev, "Unable to set voltage for %s\n",
  411. "core_ldo");
  412. goto put_core_ldo_lpm;
  413. }
  414. rc = regulator_enable(phy->core_ldo);
  415. if (rc) {
  416. dev_err(phy->phy.dev, "Unable to enable %s\n", "core_ldo");
  417. goto unset_core_ldo;
  418. }
  419. if (phy->refgen) {
  420. rc = regulator_set_load(phy->refgen, USB3PHY_REFGEN_HPM_LOAD);
  421. if (rc < 0) {
  422. dev_err(phy->phy.dev, "Unable to set HPM of refgen:%d\n", rc);
  423. goto disable_regulators;
  424. }
  425. rc = regulator_set_voltage(phy->refgen, phy->refgen_levels[1],
  426. phy->refgen_levels[2]);
  427. if (rc) {
  428. dev_err(phy->phy.dev,
  429. "Unable to set voltage for refgen:%d\n", rc);
  430. goto put_refgen_lpm;
  431. }
  432. rc = regulator_enable(phy->refgen);
  433. if (rc) {
  434. dev_err(phy->phy.dev, "Unable to enable refgen:%d\n", rc);
  435. goto unset_refgen;
  436. }
  437. }
  438. return 0;
  439. disable_refgen:
  440. rc = regulator_disable(phy->refgen);
  441. if (rc)
  442. dev_err(phy->phy.dev, "Unable to disable refgen\n");
  443. unset_refgen:
  444. rc = regulator_set_voltage(phy->refgen, phy->refgen_levels[0], phy->refgen_levels[2]);
  445. if (rc)
  446. dev_err(phy->phy.dev,
  447. "Unable to set (0) voltage for refgen:refgen\n");
  448. put_refgen_lpm:
  449. rc = regulator_set_load(phy->refgen, 0);
  450. if (rc < 0)
  451. dev_err(phy->phy.dev, "Unable to set (0) HPM of refgen\n");
  452. disable_regulators:
  453. rc = regulator_disable(phy->core_ldo);
  454. if (rc)
  455. dev_err(phy->phy.dev, "Unable to disable %s\n", "core_ldo");
  456. unset_core_ldo:
  457. rc = regulator_set_voltage(phy->core_ldo,
  458. phy->core_voltage_levels[CORE_LEVEL_NONE],
  459. phy->core_voltage_levels[CORE_LEVEL_MAX]);
  460. if (rc)
  461. dev_err(phy->phy.dev, "Unable to set voltage for %s\n",
  462. "core_ldo");
  463. put_core_ldo_lpm:
  464. rc = regulator_set_load(phy->core_ldo, 0);
  465. if (rc < 0)
  466. dev_err(phy->phy.dev, "Unable to set LPM of %s\n", "core_ldo");
  467. disable_vdd:
  468. rc = regulator_disable(phy->vdd);
  469. if (rc)
  470. dev_err(phy->phy.dev, "Unable to disable %s\n", "vdd");
  471. unconfig_vdd:
  472. rc = regulator_set_voltage(phy->vdd, phy->vdd_levels[min],
  473. phy->vdd_levels[2]);
  474. if (rc)
  475. dev_err(phy->phy.dev, "Unable to set voltage for %s\n", "vdd");
  476. put_vdd_lpm:
  477. rc = regulator_set_load(phy->vdd, 0);
  478. if (rc < 0)
  479. dev_err(phy->phy.dev, "Unable to set LPM of %s\n", "vdd");
  480. put_gdsc:
  481. rc = msm_ssusb_qmp_gdsc(phy, false);
  482. return rc < 0 ? rc : 0;
  483. }
  484. static int configure_phy_regs(struct usb_phy *uphy,
  485. const struct qmp_reg_val *reg)
  486. {
  487. struct msm_ssphy_qmp *phy = container_of(uphy, struct msm_ssphy_qmp,
  488. phy);
  489. int i;
  490. if (!reg) {
  491. dev_err(uphy->dev, "NULL PHY configuration\n");
  492. return -EINVAL;
  493. }
  494. for (i = 0; i < phy->init_seq_len/2; i++) {
  495. writel_relaxed(reg->val, phy->base + reg->offset);
  496. reg++;
  497. }
  498. return 0;
  499. }
  500. static void msm_ssphy_qmp_setmode(struct msm_ssphy_qmp *phy, u32 mode)
  501. {
  502. mode = mode & USB3_DP_COMBO_MODE;
  503. writel_relaxed(mode,
  504. phy->base + phy->phy_reg[USB3_DP_COM_PHY_MODE_CTRL]);
  505. /* flush the write by reading it */
  506. readl_relaxed(phy->base + phy->phy_reg[USB3_DP_COM_PHY_MODE_CTRL]);
  507. }
  508. static void usb_qmp_update_portselect_phymode(struct msm_ssphy_qmp *phy)
  509. {
  510. int val;
  511. /* perform lane selection */
  512. val = -EINVAL;
  513. if (phy->phy.flags & PHY_LANE_A)
  514. val = SW_PORTSELECT_MX;
  515. else if (phy->phy.flags & PHY_LANE_B)
  516. val = SW_PORTSELECT | SW_PORTSELECT_MX;
  517. /* PHY must be powered up before updating portselect and phymode. */
  518. usb_qmp_powerup_phy(phy);
  519. switch (phy->phy_type) {
  520. case USB3_AND_DP:
  521. writel_relaxed(0x01,
  522. phy->base + phy->phy_reg[USB3_DP_COM_SW_RESET]);
  523. writel_relaxed(0x00,
  524. phy->base + phy->phy_reg[USB3_DP_COM_SW_RESET]);
  525. if (phy->phy_reg[USB3_DP_COM_TYPEC_STATUS]) {
  526. u32 status = readl_relaxed(phy->base +
  527. phy->phy_reg[USB3_DP_COM_TYPEC_STATUS]);
  528. dev_dbg(phy->phy.dev, "hw port select %s\n",
  529. status & PORTSELECT_RAW ? "CC2" : "CC1");
  530. }
  531. if (!(phy->phy.flags & PHY_USB_DP_CONCURRENT_MODE))
  532. /* override hardware control for reset of qmp phy */
  533. writel_relaxed(SW_DPPHY_RESET_MUX | SW_DPPHY_RESET |
  534. SW_USB3PHY_RESET_MUX | SW_USB3PHY_RESET,
  535. phy->base + phy->phy_reg[USB3_DP_COM_RESET_OVRD_CTRL]);
  536. /* update port select */
  537. if (val > 0) {
  538. dev_err(phy->phy.dev,
  539. "USB DP QMP PHY: Update TYPEC CTRL(%d)\n", val);
  540. writel_relaxed(val, phy->base +
  541. phy->phy_reg[USB3_DP_COM_TYPEC_CTRL]);
  542. }
  543. #ifdef CONFIG_PINCTRL
  544. /*
  545. * if there is no default pinctrl state for orientation,
  546. * it need external module provide SW orientation info,
  547. * report an error if there is no such info.
  548. */
  549. if (val < 0 && !phy->phy.dev->pins)
  550. dev_err(phy->phy.dev,
  551. "USB DP QMP PHY: NO SW PORTSELECT\n");
  552. #endif
  553. if (!(phy->phy.flags & PHY_USB_DP_CONCURRENT_MODE)) {
  554. msm_ssphy_qmp_setmode(phy, USB3_DP_COMBO_MODE);
  555. /* bring both USB and DP PHYs PCS block out of reset */
  556. writel_relaxed(0x00, phy->base +
  557. phy->phy_reg[USB3_DP_COM_RESET_OVRD_CTRL]);
  558. }
  559. break;
  560. case USB3_OR_DP:
  561. if (val > 0) {
  562. dev_err(phy->phy.dev,
  563. "USB QMP PHY: Update TYPEC CTRL(%d)\n", val);
  564. writel_relaxed(val, phy->base +
  565. phy->phy_reg[USB3_PHY_PCS_MISC_TYPEC_CTRL]);
  566. }
  567. break;
  568. default:
  569. dev_dbg(phy->phy.dev, "no portselect for phy type %d\n",
  570. phy->phy_type);
  571. break;
  572. }
  573. /* Make sure above selection and reset sequence is gone through */
  574. mb();
  575. }
  576. static void usb_qmp_powerup_phy(struct msm_ssphy_qmp *phy)
  577. {
  578. switch (phy->phy_type) {
  579. case USB3_AND_DP:
  580. /* power up USB3 and DP common logic block */
  581. writel_relaxed(0x01,
  582. phy->base + phy->phy_reg[USB3_DP_COM_POWER_DOWN_CTRL]);
  583. /*
  584. * Don't write 0x0 to DP_COM_SW_RESET here as portselect and
  585. * phymode operation needs DP_COM_SW_RESET as 0x1.
  586. * msm_ssphy_qmp_init() writes 0x0 to DP_COM_SW_RESET before
  587. * initializing PHY.
  588. */
  589. fallthrough;
  590. case USB3_OR_DP:
  591. case USB3:
  592. /* power up USB3 PHY */
  593. writel_relaxed(0x01,
  594. phy->base + phy->phy_reg[USB3_PHY_POWER_DOWN_CONTROL]);
  595. break;
  596. default:
  597. dev_err(phy->phy.dev, "phy_powerup: Unknown USB QMP PHY type\n");
  598. break;
  599. }
  600. /* Make sure that above write completed to power up PHY */
  601. mb();
  602. }
  603. /* SSPHY Initialization */
  604. static int msm_ssphy_qmp_init(struct usb_phy *uphy)
  605. {
  606. struct msm_ssphy_qmp *phy = container_of(uphy, struct msm_ssphy_qmp,
  607. phy);
  608. int ret;
  609. unsigned int init_timeout_usec = INIT_MAX_TIME_USEC;
  610. const struct qmp_reg_val *reg = NULL;
  611. dev_dbg(uphy->dev, "Initializing QMP phy\n");
  612. if (uphy->flags & PHY_DP_MODE) {
  613. dev_info(uphy->dev, "QMP PHY currently in DP mode\n");
  614. return -EBUSY;
  615. }
  616. ret = msm_ssusb_qmp_ldo_enable(phy, 1);
  617. if (ret) {
  618. dev_err(phy->phy.dev,
  619. "msm_ssusb_qmp_ldo_enable(1) failed, ret=%d\n",
  620. ret);
  621. return ret;
  622. }
  623. msm_ssphy_qmp_enable_clks(phy, true);
  624. if (phy->phy_type == USB3_AND_DP)
  625. ret = msm_ssphy_qmp_dp_combo_reset(&phy->phy);
  626. else
  627. ret = msm_ssphy_qmp_reset(&phy->phy);
  628. /* select appropriate port select and PHY mode if applicable */
  629. usb_qmp_update_portselect_phymode(phy);
  630. /* power up PHY */
  631. usb_qmp_powerup_phy(phy);
  632. reg = (struct qmp_reg_val *)phy->qmp_phy_init_seq;
  633. /* Main configuration */
  634. ret = configure_phy_regs(uphy, reg);
  635. if (ret) {
  636. dev_err(uphy->dev, "Failed the main PHY configuration\n");
  637. goto fail;
  638. }
  639. /* perform software reset of PCS/Serdes */
  640. writel_relaxed(0x00, phy->base + phy->phy_reg[USB3_PHY_SW_RESET]);
  641. /* start PCS/Serdes to operation mode */
  642. writel_relaxed(0x03, phy->base + phy->phy_reg[USB3_PHY_START]);
  643. /* Make sure above write completed to bring PHY out of reset */
  644. mb();
  645. /* Wait for PHY initialization to be done */
  646. do {
  647. if (readl_relaxed(phy->base +
  648. phy->phy_reg[USB3_PHY_PCS_STATUS]) & PHYSTATUS)
  649. usleep_range(1, 2);
  650. else
  651. break;
  652. } while (--init_timeout_usec);
  653. if (!init_timeout_usec) {
  654. dev_err(uphy->dev, "QMP PHY initialization timeout\n");
  655. dev_err(uphy->dev, "USB3_PHY_PCS_STATUS:%x\n",
  656. readl_relaxed(phy->base +
  657. phy->phy_reg[USB3_PHY_PCS_STATUS]));
  658. ret = -EBUSY;
  659. goto fail;
  660. }
  661. #if IS_ENABLED(CONFIG_USB_PHY_TUNING_QCOM)
  662. if (phy->tune_buf_cnt && phy->ssphy_tune_init_done)
  663. ssphy_tune_set(phy);
  664. #endif
  665. return ret;
  666. fail:
  667. phy->in_suspend = true;
  668. writel_relaxed(0x00,
  669. phy->base + phy->phy_reg[USB3_PHY_POWER_DOWN_CONTROL]);
  670. msm_ssphy_qmp_enable_clks(phy, false);
  671. msm_ssusb_qmp_ldo_enable(phy, 0);
  672. return ret;
  673. }
  674. static int msm_ssphy_qmp_dp_combo_reset(struct usb_phy *uphy)
  675. {
  676. struct msm_ssphy_qmp *phy = container_of(uphy, struct msm_ssphy_qmp,
  677. phy);
  678. int ret = 0;
  679. if (phy->phy.flags & PHY_USB_DP_CONCURRENT_MODE) {
  680. dev_dbg(uphy->dev, "Resetting USB part of QMP phy\n");
  681. /* Assert USB3 PHY CSR reset */
  682. ret = reset_control_assert(phy->phy_reset);
  683. if (ret) {
  684. dev_err(uphy->dev, "phy_reset assert failed\n");
  685. goto exit;
  686. }
  687. /* Deassert USB3 PHY CSR reset */
  688. ret = reset_control_deassert(phy->phy_reset);
  689. if (ret) {
  690. dev_err(uphy->dev, "phy_reset deassert failed\n");
  691. goto exit;
  692. }
  693. return 0;
  694. }
  695. dev_dbg(uphy->dev, "Global reset of QMP DP combo phy\n");
  696. /* Assert global PHY reset */
  697. ret = reset_control_assert(phy->global_phy_reset);
  698. if (ret) {
  699. dev_err(uphy->dev, "global_phy_reset assert failed\n");
  700. goto exit;
  701. }
  702. /* Assert QMP USB PHY reset */
  703. ret = reset_control_assert(phy->phy_reset);
  704. if (ret) {
  705. dev_err(uphy->dev, "phy_reset assert failed\n");
  706. goto exit;
  707. }
  708. /* De-Assert QMP USB PHY reset */
  709. ret = reset_control_deassert(phy->phy_reset);
  710. if (ret)
  711. dev_err(uphy->dev, "phy_reset deassert failed\n");
  712. /* De-Assert global PHY reset */
  713. ret = reset_control_deassert(phy->global_phy_reset);
  714. if (ret)
  715. dev_err(uphy->dev, "global_phy_reset deassert failed\n");
  716. exit:
  717. return ret;
  718. }
  719. static int msm_ssphy_qmp_reset(struct usb_phy *uphy)
  720. {
  721. struct msm_ssphy_qmp *phy = container_of(uphy, struct msm_ssphy_qmp,
  722. phy);
  723. int ret;
  724. dev_dbg(uphy->dev, "Resetting QMP phy\n");
  725. /* Assert USB3 PHY reset */
  726. ret = reset_control_assert(phy->phy_phy_reset);
  727. if (ret) {
  728. dev_err(uphy->dev, "phy_phy_reset assert failed\n");
  729. goto exit;
  730. }
  731. /* Assert USB3 PHY CSR reset */
  732. ret = reset_control_assert(phy->phy_reset);
  733. if (ret) {
  734. dev_err(uphy->dev, "phy_reset assert failed\n");
  735. goto deassert_phy_phy_reset;
  736. }
  737. /* select usb3 phy mode */
  738. if (phy->tcsr_usb3_dp_phymode)
  739. writel_relaxed(0x0, phy->tcsr_usb3_dp_phymode);
  740. /* Deassert USB3 PHY CSR reset */
  741. ret = reset_control_deassert(phy->phy_reset);
  742. if (ret) {
  743. dev_err(uphy->dev, "phy_reset deassert failed\n");
  744. goto deassert_phy_phy_reset;
  745. }
  746. /* Deassert USB3 PHY reset */
  747. ret = reset_control_deassert(phy->phy_phy_reset);
  748. if (ret) {
  749. dev_err(uphy->dev, "phy_phy_reset deassert failed\n");
  750. goto exit;
  751. }
  752. return 0;
  753. deassert_phy_phy_reset:
  754. ret = reset_control_deassert(phy->phy_phy_reset);
  755. if (ret)
  756. dev_err(uphy->dev, "phy_phy_reset deassert failed\n");
  757. exit:
  758. phy->in_suspend = false;
  759. return ret;
  760. }
  761. static int msm_ssphy_power_enable(struct msm_ssphy_qmp *phy, bool on)
  762. {
  763. bool host = phy->phy.flags & PHY_HOST_MODE;
  764. int ret = 0;
  765. /*
  766. * Turn off the phy's LDOs when cable is disconnected for device mode
  767. * with external vbus_id indication.
  768. */
  769. if (!host && !phy->cable_connected) {
  770. if (on) {
  771. ret = msm_ssusb_qmp_ldo_enable(phy, 1);
  772. if (ret)
  773. dev_err(phy->phy.dev,
  774. "msm_ssusb_qmp_ldo_enable(1) failed, ret=%d\n",
  775. ret);
  776. } else {
  777. ret = msm_ssusb_qmp_ldo_enable(phy, 0);
  778. if (ret)
  779. dev_err(phy->phy.dev,
  780. "msm_ssusb_qmp_ldo_enable(0) failed, ret=%d\n",
  781. ret);
  782. }
  783. }
  784. return ret;
  785. }
  786. /**
  787. * Performs QMP PHY suspend/resume functionality.
  788. *
  789. * @uphy - usb phy pointer.
  790. * @suspend - to enable suspend or not. 1 - suspend, 0 - resume
  791. *
  792. */
  793. static int msm_ssphy_qmp_set_suspend(struct usb_phy *uphy, int suspend)
  794. {
  795. struct msm_ssphy_qmp *phy = container_of(uphy, struct msm_ssphy_qmp,
  796. phy);
  797. dev_dbg(uphy->dev, "QMP PHY set_suspend for %s called with cable %s\n",
  798. (suspend ? "suspend" : "resume"),
  799. get_cable_status_str(phy));
  800. if (phy->in_suspend == suspend) {
  801. dev_dbg(uphy->dev, "%s: USB PHY is already %s.\n",
  802. __func__, (suspend ? "suspended" : "resumed"));
  803. return 0;
  804. }
  805. if (suspend) {
  806. if (phy->cable_connected) {
  807. msm_ssusb_qmp_enable_autonomous(phy, 1);
  808. } else {
  809. /* Reset phy mode to USB only if DP not connected */
  810. if (phy->phy_type == USB3_AND_DP &&
  811. !((uphy->flags & PHY_DP_MODE) ||
  812. (uphy->flags & PHY_USB_DP_CONCURRENT_MODE)))
  813. msm_ssphy_qmp_setmode(phy, USB3_MODE);
  814. writel_relaxed(0x00,
  815. phy->base + phy->phy_reg[USB3_PHY_POWER_DOWN_CONTROL]);
  816. }
  817. /* Make sure above write completed with PHY */
  818. wmb();
  819. msm_ssphy_qmp_enable_clks(phy, false);
  820. phy->in_suspend = true;
  821. msm_ssphy_power_enable(phy, 0);
  822. dev_dbg(uphy->dev, "QMP PHY is suspend\n");
  823. } else {
  824. if (uphy->flags & PHY_DP_MODE) {
  825. dev_info(uphy->dev, "QMP PHY currently in DP mode\n");
  826. return -EBUSY;
  827. }
  828. msm_ssphy_power_enable(phy, 1);
  829. msm_ssphy_qmp_enable_clks(phy, true);
  830. if (!phy->cable_connected) {
  831. writel_relaxed(0x01,
  832. phy->base + phy->phy_reg[USB3_PHY_POWER_DOWN_CONTROL]);
  833. } else {
  834. msm_ssusb_qmp_enable_autonomous(phy, 0);
  835. }
  836. /* Make sure that above write completed with PHY */
  837. wmb();
  838. phy->in_suspend = false;
  839. dev_dbg(uphy->dev, "QMP PHY is resumed\n");
  840. }
  841. return 0;
  842. }
  843. static int msm_ssphy_qmp_notify_connect(struct usb_phy *uphy,
  844. enum usb_device_speed speed)
  845. {
  846. struct msm_ssphy_qmp *phy = container_of(uphy, struct msm_ssphy_qmp,
  847. phy);
  848. dev_dbg(uphy->dev, "QMP phy connect notification\n");
  849. phy->cable_connected = true;
  850. atomic_notifier_call_chain(&uphy->notifier, 1, uphy);
  851. return 0;
  852. }
  853. static int msm_ssphy_qmp_notify_disconnect(struct usb_phy *uphy,
  854. enum usb_device_speed speed)
  855. {
  856. struct msm_ssphy_qmp *phy = container_of(uphy, struct msm_ssphy_qmp,
  857. phy);
  858. atomic_notifier_call_chain(&uphy->notifier, 0, uphy);
  859. if (phy->phy.flags & PHY_HOST_MODE) {
  860. writel_relaxed(0x00,
  861. phy->base + phy->phy_reg[USB3_PHY_POWER_DOWN_CONTROL]);
  862. readl_relaxed(phy->base + phy->phy_reg[USB3_PHY_POWER_DOWN_CONTROL]);
  863. }
  864. dev_dbg(uphy->dev, "QMP phy disconnect notification\n");
  865. dev_dbg(uphy->dev, " cable_connected=%d\n", phy->cable_connected);
  866. phy->cable_connected = false;
  867. return 0;
  868. }
  869. static int msm_ssphy_qmp_get_clks(struct msm_ssphy_qmp *phy, struct device *dev)
  870. {
  871. int ret = 0;
  872. phy->aux_clk = devm_clk_get(dev, "aux_clk");
  873. if (IS_ERR(phy->aux_clk)) {
  874. ret = PTR_ERR(phy->aux_clk);
  875. phy->aux_clk = NULL;
  876. if (ret != -EPROBE_DEFER)
  877. dev_err(dev, "failed to get aux_clk\n");
  878. goto err;
  879. }
  880. clk_set_rate(phy->aux_clk, clk_round_rate(phy->aux_clk, ULONG_MAX));
  881. if (of_property_match_string(dev->of_node,
  882. "clock-names", "cfg_ahb_clk") >= 0) {
  883. phy->cfg_ahb_clk = devm_clk_get(dev, "cfg_ahb_clk");
  884. if (IS_ERR(phy->cfg_ahb_clk)) {
  885. ret = PTR_ERR(phy->cfg_ahb_clk);
  886. if (ret != -EPROBE_DEFER)
  887. dev_err(dev,
  888. "failed to get cfg_ahb_clk ret %d\n", ret);
  889. goto err;
  890. }
  891. }
  892. phy->pipe_clk = devm_clk_get(dev, "pipe_clk");
  893. if (IS_ERR(phy->pipe_clk)) {
  894. ret = PTR_ERR(phy->pipe_clk);
  895. phy->pipe_clk = NULL;
  896. if (ret != -EPROBE_DEFER)
  897. dev_err(dev, "failed to get pipe_clk\n");
  898. goto err;
  899. }
  900. phy->pipe_clk_mux = devm_clk_get(dev, "pipe_clk_mux");
  901. if (IS_ERR(phy->pipe_clk_mux))
  902. phy->pipe_clk_mux = NULL;
  903. phy->pipe_clk_ext_src = devm_clk_get(dev, "pipe_clk_ext_src");
  904. if (IS_ERR(phy->pipe_clk_ext_src))
  905. phy->pipe_clk_ext_src = NULL;
  906. phy->ref_clk_src = devm_clk_get(dev, "ref_clk_src");
  907. if (IS_ERR(phy->ref_clk_src))
  908. phy->ref_clk_src = NULL;
  909. phy->ref_clk = devm_clk_get(dev, "ref_clk");
  910. if (IS_ERR(phy->ref_clk))
  911. phy->ref_clk = NULL;
  912. if (of_property_match_string(dev->of_node,
  913. "clock-names", "com_aux_clk") >= 0) {
  914. phy->com_aux_clk = devm_clk_get(dev, "com_aux_clk");
  915. if (IS_ERR(phy->com_aux_clk)) {
  916. ret = PTR_ERR(phy->com_aux_clk);
  917. if (ret != -EPROBE_DEFER)
  918. dev_err(dev,
  919. "failed to get com_aux_clk ret %d\n", ret);
  920. goto err;
  921. }
  922. }
  923. err:
  924. return ret;
  925. }
  926. static void msm_ssphy_qmp_enable_clks(struct msm_ssphy_qmp *phy, bool on)
  927. {
  928. dev_dbg(phy->phy.dev, "%s(): clk_enabled:%d on:%d\n", __func__,
  929. phy->clk_enabled, on);
  930. if (!phy->clk_enabled && on) {
  931. if (phy->ref_clk_src)
  932. clk_prepare_enable(phy->ref_clk_src);
  933. if (phy->ref_clk)
  934. clk_prepare_enable(phy->ref_clk);
  935. if (phy->com_aux_clk)
  936. clk_prepare_enable(phy->com_aux_clk);
  937. clk_prepare_enable(phy->aux_clk);
  938. if (phy->cfg_ahb_clk)
  939. clk_prepare_enable(phy->cfg_ahb_clk);
  940. //select PHY pipe clock
  941. clk_set_parent(phy->pipe_clk_mux, phy->pipe_clk_ext_src);
  942. clk_prepare_enable(phy->pipe_clk);
  943. phy->clk_enabled = true;
  944. }
  945. if (phy->clk_enabled && !on) {
  946. clk_disable_unprepare(phy->pipe_clk);
  947. //select XO instead of PHY pipe clock
  948. clk_set_parent(phy->pipe_clk_mux, phy->ref_clk_src);
  949. if (phy->cfg_ahb_clk)
  950. clk_disable_unprepare(phy->cfg_ahb_clk);
  951. clk_disable_unprepare(phy->aux_clk);
  952. if (phy->com_aux_clk)
  953. clk_disable_unprepare(phy->com_aux_clk);
  954. if (phy->ref_clk)
  955. clk_disable_unprepare(phy->ref_clk);
  956. if (phy->ref_clk_src)
  957. clk_disable_unprepare(phy->ref_clk_src);
  958. phy->clk_enabled = false;
  959. }
  960. }
  961. static int usb3_get_regulators(struct msm_ssphy_qmp *phy)
  962. {
  963. struct device *dev = phy->phy.dev;
  964. int ret = 0;
  965. phy->refgen = NULL;
  966. phy->vdd = devm_regulator_get(dev, "vdd");
  967. if (IS_ERR(phy->vdd)) {
  968. ret = PTR_ERR(phy->vdd);
  969. if (ret != -EPROBE_DEFER)
  970. dev_err(dev, "fail to get vdd supply\n");
  971. return ret;
  972. }
  973. phy->core_ldo = devm_regulator_get(dev, "core");
  974. if (IS_ERR(phy->core_ldo)) {
  975. ret = PTR_ERR(phy->core_ldo);
  976. if (ret != -EPROBE_DEFER)
  977. dev_err(dev, "fail to get core ldo supply\n");
  978. return ret;
  979. }
  980. phy->usb3_dp_phy_gdsc = devm_regulator_get(dev, "usb3_dp_phy_gdsc");
  981. if (IS_ERR(phy->usb3_dp_phy_gdsc)) {
  982. ret = PTR_ERR(phy->usb3_dp_phy_gdsc);
  983. if (ret != -ENODEV) {
  984. dev_err(dev, "fail to get usb3_dp_phy_gdsc(%d)\n", ret);
  985. return ret;
  986. }
  987. dev_dbg(dev, "usb3_dp_phy_gdsc optional regulator missing\n");
  988. }
  989. if (of_property_read_bool(dev->of_node, "refgen-supply")) {
  990. phy->refgen = devm_regulator_get_optional(dev, "refgen");
  991. if (IS_ERR(phy->refgen))
  992. dev_err(dev, "fail to get refgen supply\n");
  993. }
  994. return 0;
  995. }
  996. static int msm_ssphy_qmp_probe(struct platform_device *pdev)
  997. {
  998. struct msm_ssphy_qmp *phy;
  999. struct device *dev = &pdev->dev;
  1000. struct resource *res;
  1001. int ret = 0, size = 0, len;
  1002. phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
  1003. if (!phy)
  1004. return -ENOMEM;
  1005. phy->phy_type = USB3;
  1006. if (of_device_is_compatible(dev->of_node,
  1007. "qcom,usb-ssphy-qmp-dp-combo"))
  1008. phy->phy_type = USB3_AND_DP;
  1009. if (of_device_is_compatible(dev->of_node,
  1010. "qcom,usb-ssphy-qmp-usb3-or-dp"))
  1011. phy->phy_type = USB3_OR_DP;
  1012. ret = msm_ssphy_qmp_get_clks(phy, dev);
  1013. if (ret)
  1014. goto err;
  1015. phy->phy_reset = devm_reset_control_get(dev, "phy_reset");
  1016. if (IS_ERR(phy->phy_reset)) {
  1017. ret = PTR_ERR(phy->phy_reset);
  1018. dev_dbg(dev, "failed to get phy_reset\n");
  1019. goto err;
  1020. }
  1021. if (phy->phy_type == USB3_AND_DP) {
  1022. phy->global_phy_reset = devm_reset_control_get(dev,
  1023. "global_phy_reset");
  1024. if (IS_ERR(phy->global_phy_reset)) {
  1025. ret = PTR_ERR(phy->global_phy_reset);
  1026. dev_dbg(dev, "failed to get global_phy_reset\n");
  1027. goto err;
  1028. }
  1029. } else {
  1030. phy->phy_phy_reset = devm_reset_control_get(dev,
  1031. "phy_phy_reset");
  1032. if (IS_ERR(phy->phy_phy_reset)) {
  1033. ret = PTR_ERR(phy->phy_phy_reset);
  1034. dev_dbg(dev, "failed to get phy_phy_reset\n");
  1035. goto err;
  1036. }
  1037. }
  1038. of_get_property(dev->of_node, "qcom,qmp-phy-reg-offset", &size);
  1039. if (size) {
  1040. phy->phy_reg = devm_kzalloc(dev, PHY_REG_SIZE, GFP_KERNEL);
  1041. if (phy->phy_reg) {
  1042. phy->reg_offset_cnt = (size / sizeof(*phy->phy_reg));
  1043. if (phy->reg_offset_cnt > USB3_PHY_REG_MAX) {
  1044. dev_err(dev, "invalid reg offset count\n");
  1045. return -EINVAL;
  1046. }
  1047. of_property_read_u32_array(dev->of_node,
  1048. "qcom,qmp-phy-reg-offset",
  1049. phy->phy_reg, phy->reg_offset_cnt);
  1050. } else {
  1051. dev_err(dev, "err mem alloc for qmp_phy_reg_offset\n");
  1052. return -ENOMEM;
  1053. }
  1054. } else {
  1055. dev_err(dev, "err provide qcom,qmp-phy-reg-offset\n");
  1056. return -EINVAL;
  1057. }
  1058. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1059. "qmp_phy_base");
  1060. if (!res) {
  1061. dev_err(dev, "failed getting qmp_phy_base\n");
  1062. return -ENODEV;
  1063. }
  1064. /*
  1065. * For USB QMP DP combo PHY, common set of registers shall be accessed
  1066. * by DP driver as well.
  1067. */
  1068. phy->base = devm_ioremap(dev, res->start, resource_size(res));
  1069. if (IS_ERR_OR_NULL(phy->base)) {
  1070. ret = PTR_ERR(phy->base);
  1071. goto err;
  1072. }
  1073. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1074. "vls_clamp_reg");
  1075. if (res) {
  1076. phy->vls_clamp_reg = devm_ioremap_resource(dev, res);
  1077. if (IS_ERR(phy->vls_clamp_reg)) {
  1078. dev_err(dev, "err getting vls_clamp_reg address\n");
  1079. return PTR_ERR(phy->vls_clamp_reg);
  1080. }
  1081. }
  1082. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1083. "pcs_clamp_enable_reg");
  1084. if (res) {
  1085. phy->pcs_clamp_enable_reg = devm_ioremap_resource(dev, res);
  1086. if (IS_ERR(phy->pcs_clamp_enable_reg)) {
  1087. dev_err(dev, "err getting pcs_clamp_enable_reg address.\n");
  1088. return PTR_ERR(phy->pcs_clamp_enable_reg);
  1089. }
  1090. }
  1091. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1092. "tcsr_usb3_dp_phymode");
  1093. if (res) {
  1094. phy->tcsr_usb3_dp_phymode = devm_ioremap_resource(dev, res);
  1095. if (IS_ERR(phy->tcsr_usb3_dp_phymode)) {
  1096. dev_err(dev, "err getting tcsr_usb3_dp_phymode addr\n");
  1097. return PTR_ERR(phy->tcsr_usb3_dp_phymode);
  1098. }
  1099. }
  1100. of_get_property(dev->of_node, "qcom,qmp-phy-init-seq", &size);
  1101. if (size) {
  1102. if (size % sizeof(*phy->qmp_phy_init_seq)) {
  1103. dev_err(dev, "invalid init_seq_len\n");
  1104. return -EINVAL;
  1105. }
  1106. phy->qmp_phy_init_seq = devm_kzalloc(dev, size, GFP_KERNEL);
  1107. if (!phy->qmp_phy_init_seq)
  1108. return -ENOMEM;
  1109. phy->init_seq_len = (size / sizeof(*phy->qmp_phy_init_seq));
  1110. of_property_read_u32_array(dev->of_node,
  1111. "qcom,qmp-phy-init-seq",
  1112. phy->qmp_phy_init_seq,
  1113. phy->init_seq_len);
  1114. } else {
  1115. dev_err(dev, "error need qmp-phy-init-seq\n");
  1116. return -EINVAL;
  1117. }
  1118. /* Set default core voltage values */
  1119. phy->core_voltage_levels[CORE_LEVEL_NONE] = 0;
  1120. phy->core_voltage_levels[CORE_LEVEL_MIN] = USB_SSPHY_1P2_VOL_MIN;
  1121. phy->core_voltage_levels[CORE_LEVEL_MAX] = USB_SSPHY_1P2_VOL_MAX;
  1122. if (of_get_property(dev->of_node, "qcom,core-voltage-level", &len) &&
  1123. len == sizeof(phy->core_voltage_levels)) {
  1124. ret = of_property_read_u32_array(dev->of_node,
  1125. "qcom,core-voltage-level",
  1126. (u32 *)phy->core_voltage_levels,
  1127. len / sizeof(u32));
  1128. if (ret) {
  1129. dev_err(dev, "err qcom,core-voltage-level property\n");
  1130. goto err;
  1131. }
  1132. }
  1133. if (of_property_read_s32(dev->of_node, "qcom,core-max-load-uA",
  1134. &phy->core_max_uA) || !phy->core_max_uA)
  1135. phy->core_max_uA = USB_SSPHY_HPM_LOAD;
  1136. if (of_get_property(dev->of_node, "qcom,vdd-voltage-level", &len) &&
  1137. len == sizeof(phy->vdd_levels)) {
  1138. ret = of_property_read_u32_array(dev->of_node,
  1139. "qcom,vdd-voltage-level",
  1140. (u32 *) phy->vdd_levels,
  1141. len / sizeof(u32));
  1142. if (ret) {
  1143. dev_err(dev, "err qcom,vdd-voltage-level property\n");
  1144. goto err;
  1145. }
  1146. } else {
  1147. ret = -EINVAL;
  1148. dev_err(dev, "error invalid inputs for vdd-voltage-level\n");
  1149. goto err;
  1150. }
  1151. if (of_get_property(dev->of_node, "qcom,refgen-voltage-level", &len) &&
  1152. len == sizeof(phy->refgen_levels)) {
  1153. ret = of_property_read_u32_array(dev->of_node,
  1154. "qcom,refgen-voltage-level",
  1155. (u32 *) phy->refgen_levels,
  1156. len / sizeof(u32));
  1157. if (ret)
  1158. dev_err(dev, "err qcom,refgen-voltage-level property\n");
  1159. }
  1160. if (of_property_read_s32(dev->of_node, "qcom,vdd-max-load-uA",
  1161. &phy->vdd_max_uA) || !phy->vdd_max_uA)
  1162. phy->vdd_max_uA = USB_SSPHY_HPM_LOAD;
  1163. platform_set_drvdata(pdev, phy);
  1164. phy->phy.dev = dev;
  1165. phy->phy.init = msm_ssphy_qmp_init;
  1166. phy->phy.set_suspend = msm_ssphy_qmp_set_suspend;
  1167. phy->phy.notify_connect = msm_ssphy_qmp_notify_connect;
  1168. phy->phy.notify_disconnect = msm_ssphy_qmp_notify_disconnect;
  1169. #if IS_ENABLED(CONFIG_USB_PHY_TUNING_QCOM)
  1170. phy->tune_addr = 0;
  1171. phy->tune_buf_cnt = 0;
  1172. phy->ssphy_tune_init_done = true;
  1173. ssphy_tune_buf_init(phy);
  1174. mutex_init(&phy->phy_tune_lock);
  1175. ret = sysfs_create_group(&pdev->dev.kobj, &ssphy_attr_grp);
  1176. if (ret) {
  1177. phy->ssphy_tune_init_done = false;
  1178. pr_err("%s: ssphy sysfs fail, ret %d", __func__, ret);
  1179. }
  1180. #endif
  1181. ret = usb_add_phy_dev(&phy->phy);
  1182. ret = usb3_get_regulators(phy);
  1183. if (ret)
  1184. goto err;
  1185. err:
  1186. return ret;
  1187. }
  1188. static int msm_ssphy_qmp_remove(struct platform_device *pdev)
  1189. {
  1190. struct msm_ssphy_qmp *phy = platform_get_drvdata(pdev);
  1191. if (!phy)
  1192. return 0;
  1193. usb_remove_phy(&phy->phy);
  1194. msm_ssphy_qmp_enable_clks(phy, false);
  1195. msm_ssusb_qmp_ldo_enable(phy, 0);
  1196. #if IS_ENABLED(CONFIG_USB_PHY_TUNING_QCOM)
  1197. if (phy->ssphy_tune_init_done)
  1198. sysfs_remove_group(&pdev->dev.kobj, &ssphy_attr_grp);
  1199. mutex_destroy(&phy->phy_tune_lock);
  1200. #endif
  1201. return 0;
  1202. }
  1203. static struct platform_driver msm_ssphy_qmp_driver = {
  1204. .probe = msm_ssphy_qmp_probe,
  1205. .remove = msm_ssphy_qmp_remove,
  1206. .driver = {
  1207. .name = "msm-usb-ssphy-qmp",
  1208. .of_match_table = of_match_ptr(msm_usb_id_table),
  1209. },
  1210. };
  1211. module_platform_driver(msm_ssphy_qmp_driver);
  1212. MODULE_DESCRIPTION("MSM USB SS QMP PHY driver");
  1213. MODULE_LICENSE("GPL v2");