1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2017, The Linux Foundation. All rights reserved.
7 #include <linux/delay.h>
10 #include <linux/kernel.h>
11 #include <linux/mfd/syscon.h>
12 #include <linux/module.h>
13 #include <linux/nvmem-consumer.h>
15 #include <linux/of_device.h>
16 #include <linux/phy/phy.h>
17 #include <linux/platform_device.h>
18 #include <linux/regmap.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/reset.h>
21 #include <linux/slab.h>
23 #include <dt-bindings/phy/phy-qcom-qusb2.h>
25 #define QUSB2PHY_PLL_TEST 0x04
26 #define CLK_REF_SEL BIT(7)
28 #define QUSB2PHY_PLL_TUNE 0x08
29 #define QUSB2PHY_PLL_USER_CTL1 0x0c
30 #define QUSB2PHY_PLL_USER_CTL2 0x10
31 #define QUSB2PHY_PLL_AUTOPGM_CTL1 0x1c
32 #define QUSB2PHY_PLL_PWR_CTRL 0x18
34 /* QUSB2PHY_PLL_STATUS register bits */
35 #define PLL_LOCKED BIT(5)
37 /* QUSB2PHY_PLL_COMMON_STATUS_ONE register bits */
38 #define CORE_READY_STATUS BIT(0)
40 /* QUSB2PHY_PORT_POWERDOWN register bits */
41 #define CLAMP_N_EN BIT(5)
42 #define FREEZIO_N BIT(1)
43 #define POWER_DOWN BIT(0)
45 /* QUSB2PHY_PWR_CTRL1 register bits */
46 #define PWR_CTRL1_VREF_SUPPLY_TRIM BIT(5)
47 #define PWR_CTRL1_CLAMP_N_EN BIT(1)
49 #define QUSB2PHY_REFCLK_ENABLE BIT(0)
51 #define PHY_CLK_SCHEME_SEL BIT(0)
53 /* QUSB2PHY_INTR_CTRL register bits */
54 #define DMSE_INTR_HIGH_SEL BIT(4)
55 #define DPSE_INTR_HIGH_SEL BIT(3)
56 #define CHG_DET_INTR_EN BIT(2)
57 #define DMSE_INTR_EN BIT(1)
58 #define DPSE_INTR_EN BIT(0)
60 /* QUSB2PHY_PLL_CORE_INPUT_OVERRIDE register bits */
61 #define CORE_PLL_EN_FROM_RESET BIT(4)
62 #define CORE_RESET BIT(5)
63 #define CORE_RESET_MUX BIT(6)
65 /* QUSB2PHY_IMP_CTRL1 register bits */
66 #define IMP_RES_OFFSET_MASK GENMASK(5, 0)
67 #define IMP_RES_OFFSET_SHIFT 0x0
69 /* QUSB2PHY_PORT_TUNE1 register bits */
70 #define HSTX_TRIM_MASK GENMASK(7, 4)
71 #define HSTX_TRIM_SHIFT 0x4
72 #define PREEMPH_WIDTH_HALF_BIT BIT(2)
73 #define PREEMPHASIS_EN_MASK GENMASK(1, 0)
74 #define PREEMPHASIS_EN_SHIFT 0x0
76 #define QUSB2PHY_PLL_ANALOG_CONTROLS_TWO 0x04
77 #define QUSB2PHY_PLL_CLOCK_INVERTERS 0x18c
78 #define QUSB2PHY_PLL_CMODE 0x2c
79 #define QUSB2PHY_PLL_LOCK_DELAY 0x184
80 #define QUSB2PHY_PLL_DIGITAL_TIMERS_TWO 0xb4
81 #define QUSB2PHY_PLL_BIAS_CONTROL_1 0x194
82 #define QUSB2PHY_PLL_BIAS_CONTROL_2 0x198
83 #define QUSB2PHY_PWR_CTRL2 0x214
84 #define QUSB2PHY_IMP_CTRL1 0x220
85 #define QUSB2PHY_IMP_CTRL2 0x224
86 #define QUSB2PHY_CHG_CTRL2 0x23c
88 struct qusb2_phy_init_tbl {
92 * register part of layout ?
93 * if yes, then offset gives index in the reg-layout
98 #define QUSB2_PHY_INIT_CFG(o, v) \
104 #define QUSB2_PHY_INIT_CFG_L(o, v) \
111 /* set of registers with offsets different per-PHY */
112 enum qusb2phy_reg_layout {
113 QUSB2PHY_PLL_CORE_INPUT_OVERRIDE,
122 QUSB2PHY_PORT_POWERDOWN,
126 static const unsigned int msm8996_regs_layout[] = {
127 [QUSB2PHY_PLL_STATUS] = 0x38,
128 [QUSB2PHY_PORT_TUNE1] = 0x80,
129 [QUSB2PHY_PORT_TUNE2] = 0x84,
130 [QUSB2PHY_PORT_TUNE3] = 0x88,
131 [QUSB2PHY_PORT_TUNE4] = 0x8c,
132 [QUSB2PHY_PORT_TUNE5] = 0x90,
133 [QUSB2PHY_PORT_TEST1] = 0xb8,
134 [QUSB2PHY_PORT_TEST2] = 0x9c,
135 [QUSB2PHY_PORT_POWERDOWN] = 0xb4,
136 [QUSB2PHY_INTR_CTRL] = 0xbc,
139 static const struct qusb2_phy_init_tbl msm8996_init_tbl[] = {
140 QUSB2_PHY_INIT_CFG_L(QUSB2PHY_PORT_TUNE1, 0xf8),
141 QUSB2_PHY_INIT_CFG_L(QUSB2PHY_PORT_TUNE2, 0xb3),
142 QUSB2_PHY_INIT_CFG_L(QUSB2PHY_PORT_TUNE3, 0x83),
143 QUSB2_PHY_INIT_CFG_L(QUSB2PHY_PORT_TUNE4, 0xc0),
145 QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_TUNE, 0x30),
146 QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_USER_CTL1, 0x79),
147 QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_USER_CTL2, 0x21),
149 QUSB2_PHY_INIT_CFG_L(QUSB2PHY_PORT_TEST2, 0x14),
151 QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_AUTOPGM_CTL1, 0x9f),
152 QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_PWR_CTRL, 0x00),
155 static const unsigned int sdm845_regs_layout[] = {
156 [QUSB2PHY_PLL_CORE_INPUT_OVERRIDE] = 0xa8,
157 [QUSB2PHY_PLL_STATUS] = 0x1a0,
158 [QUSB2PHY_PORT_TUNE1] = 0x240,
159 [QUSB2PHY_PORT_TUNE2] = 0x244,
160 [QUSB2PHY_PORT_TUNE3] = 0x248,
161 [QUSB2PHY_PORT_TUNE4] = 0x24c,
162 [QUSB2PHY_PORT_TUNE5] = 0x250,
163 [QUSB2PHY_PORT_TEST1] = 0x254,
164 [QUSB2PHY_PORT_TEST2] = 0x258,
165 [QUSB2PHY_PORT_POWERDOWN] = 0x210,
166 [QUSB2PHY_INTR_CTRL] = 0x230,
169 static const struct qusb2_phy_init_tbl sdm845_init_tbl[] = {
170 QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_ANALOG_CONTROLS_TWO, 0x03),
171 QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_CLOCK_INVERTERS, 0x7c),
172 QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_CMODE, 0x80),
173 QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_LOCK_DELAY, 0x0a),
174 QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_DIGITAL_TIMERS_TWO, 0x19),
175 QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_BIAS_CONTROL_1, 0x40),
176 QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_BIAS_CONTROL_2, 0x20),
177 QUSB2_PHY_INIT_CFG(QUSB2PHY_PWR_CTRL2, 0x21),
178 QUSB2_PHY_INIT_CFG(QUSB2PHY_IMP_CTRL1, 0x0),
179 QUSB2_PHY_INIT_CFG(QUSB2PHY_IMP_CTRL2, 0x58),
181 QUSB2_PHY_INIT_CFG_L(QUSB2PHY_PORT_TUNE1, 0x30),
182 QUSB2_PHY_INIT_CFG_L(QUSB2PHY_PORT_TUNE2, 0x29),
183 QUSB2_PHY_INIT_CFG_L(QUSB2PHY_PORT_TUNE3, 0xca),
184 QUSB2_PHY_INIT_CFG_L(QUSB2PHY_PORT_TUNE4, 0x04),
185 QUSB2_PHY_INIT_CFG_L(QUSB2PHY_PORT_TUNE5, 0x03),
187 QUSB2_PHY_INIT_CFG(QUSB2PHY_CHG_CTRL2, 0x0),
190 struct qusb2_phy_cfg {
191 const struct qusb2_phy_init_tbl *tbl;
192 /* number of entries in the table */
193 unsigned int tbl_num;
194 /* offset to PHY_CLK_SCHEME register in TCSR map */
195 unsigned int clk_scheme_offset;
197 /* array of registers with different offsets */
198 const unsigned int *regs;
199 unsigned int mask_core_ready;
200 unsigned int disable_ctrl;
201 unsigned int autoresume_en;
203 /* true if PHY has PLL_TEST register to select clk_scheme */
206 /* true if TUNE1 register must be updated by fused value, else TUNE2 */
207 bool update_tune1_with_efuse;
209 /* true if PHY has PLL_CORE_INPUT_OVERRIDE register to reset PLL */
210 bool has_pll_override;
213 static const struct qusb2_phy_cfg msm8996_phy_cfg = {
214 .tbl = msm8996_init_tbl,
215 .tbl_num = ARRAY_SIZE(msm8996_init_tbl),
216 .regs = msm8996_regs_layout,
218 .has_pll_test = true,
219 .disable_ctrl = (CLAMP_N_EN | FREEZIO_N | POWER_DOWN),
220 .mask_core_ready = PLL_LOCKED,
221 .autoresume_en = BIT(3),
224 static const struct qusb2_phy_cfg sdm845_phy_cfg = {
225 .tbl = sdm845_init_tbl,
226 .tbl_num = ARRAY_SIZE(sdm845_init_tbl),
227 .regs = sdm845_regs_layout,
229 .disable_ctrl = (PWR_CTRL1_VREF_SUPPLY_TRIM | PWR_CTRL1_CLAMP_N_EN |
231 .mask_core_ready = CORE_READY_STATUS,
232 .has_pll_override = true,
233 .autoresume_en = BIT(0),
234 .update_tune1_with_efuse = true,
237 static const char * const qusb2_phy_vreg_names[] = {
238 "vdda-pll", "vdda-phy-dpdm",
241 #define QUSB2_NUM_VREGS ARRAY_SIZE(qusb2_phy_vreg_names)
244 * struct qusb2_phy - structure holding qusb2 phy attributes
247 * @base: iomapped memory space for qubs2 phy
249 * @cfg_ahb_clk: AHB2PHY interface clock
250 * @ref_clk: phy reference clock
251 * @iface_clk: phy interface clock
252 * @phy_reset: phy reset control
253 * @vregs: regulator supplies bulk data
255 * @tcsr: TCSR syscon register map
256 * @cell: nvmem cell containing phy tuning value
258 * @override_imp_res_offset: PHY should use different rescode offset
259 * @imp_res_offset_value: rescode offset to be updated in IMP_CTRL1 register
260 * @override_hstx_trim: PHY should use different HSTX o/p current value
261 * @hstx_trim_value: HSTX_TRIM value to be updated in TUNE1 register
262 * @override_preemphasis: PHY should use different pre-amphasis amplitude
263 * @preemphasis_level: Amplitude Pre-Emphasis to be updated in TUNE1 register
264 * @override_preemphasis_width: PHY should use different pre-emphasis duration
265 * @preemphasis_width: half/full-width Pre-Emphasis updated via TUNE1
267 * @cfg: phy config data
268 * @has_se_clk_scheme: indicate if PHY has single-ended ref clock scheme
269 * @phy_initialized: indicate if PHY has been initialized
270 * @mode: current PHY mode
276 struct clk *cfg_ahb_clk;
278 struct clk *iface_clk;
279 struct reset_control *phy_reset;
280 struct regulator_bulk_data vregs[QUSB2_NUM_VREGS];
283 struct nvmem_cell *cell;
285 bool override_imp_res_offset;
286 u8 imp_res_offset_value;
287 bool override_hstx_trim;
289 bool override_preemphasis;
290 u8 preemphasis_level;
291 bool override_preemphasis_width;
292 u8 preemphasis_width;
294 const struct qusb2_phy_cfg *cfg;
295 bool has_se_clk_scheme;
296 bool phy_initialized;
300 static inline void qusb2_write_mask(void __iomem *base, u32 offset,
305 reg = readl(base + offset);
308 writel(reg, base + offset);
310 /* Ensure above write is completed */
311 readl(base + offset);
314 static inline void qusb2_setbits(void __iomem *base, u32 offset, u32 val)
318 reg = readl(base + offset);
320 writel(reg, base + offset);
322 /* Ensure above write is completed */
323 readl(base + offset);
326 static inline void qusb2_clrbits(void __iomem *base, u32 offset, u32 val)
330 reg = readl(base + offset);
332 writel(reg, base + offset);
334 /* Ensure above write is completed */
335 readl(base + offset);
339 void qcom_qusb2_phy_configure(void __iomem *base,
340 const unsigned int *regs,
341 const struct qusb2_phy_init_tbl tbl[], int num)
345 for (i = 0; i < num; i++) {
346 if (tbl[i].in_layout)
347 writel(tbl[i].val, base + regs[tbl[i].offset]);
349 writel(tbl[i].val, base + tbl[i].offset);
354 * Update board specific PHY tuning override values if specified from
357 static void qusb2_phy_override_phy_params(struct qusb2_phy *qphy)
359 const struct qusb2_phy_cfg *cfg = qphy->cfg;
361 if (qphy->override_imp_res_offset)
362 qusb2_write_mask(qphy->base, QUSB2PHY_IMP_CTRL1,
363 qphy->imp_res_offset_value << IMP_RES_OFFSET_SHIFT,
364 IMP_RES_OFFSET_MASK);
366 if (qphy->override_hstx_trim)
367 qusb2_write_mask(qphy->base, cfg->regs[QUSB2PHY_PORT_TUNE1],
368 qphy->hstx_trim_value << HSTX_TRIM_SHIFT,
371 if (qphy->override_preemphasis)
372 qusb2_write_mask(qphy->base, cfg->regs[QUSB2PHY_PORT_TUNE1],
373 qphy->preemphasis_level << PREEMPHASIS_EN_SHIFT,
374 PREEMPHASIS_EN_MASK);
376 if (qphy->override_preemphasis_width) {
377 if (qphy->preemphasis_width ==
378 QUSB2_V2_PREEMPHASIS_WIDTH_HALF_BIT)
379 qusb2_setbits(qphy->base,
380 cfg->regs[QUSB2PHY_PORT_TUNE1],
381 PREEMPH_WIDTH_HALF_BIT);
383 qusb2_clrbits(qphy->base,
384 cfg->regs[QUSB2PHY_PORT_TUNE1],
385 PREEMPH_WIDTH_HALF_BIT);
390 * Fetches HS Tx tuning value from nvmem and sets the
391 * QUSB2PHY_PORT_TUNE1/2 register.
392 * For error case, skip setting the value and use the default value.
394 static void qusb2_phy_set_tune2_param(struct qusb2_phy *qphy)
396 struct device *dev = &qphy->phy->dev;
397 const struct qusb2_phy_cfg *cfg = qphy->cfg;
400 /* efuse register is optional */
405 * Read efuse register having TUNE2/1 parameter's high nibble.
406 * If efuse register shows value as 0x0 (indicating value is not
407 * fused), or if we fail to find a valid efuse register setting,
408 * then use default value for high nibble that we have already
409 * set while configuring the phy.
411 val = nvmem_cell_read(qphy->cell, NULL);
412 if (IS_ERR(val) || !val[0]) {
413 dev_dbg(dev, "failed to read a valid hs-tx trim value\n");
417 /* Fused TUNE1/2 value is the higher nibble only */
418 if (cfg->update_tune1_with_efuse)
419 qusb2_write_mask(qphy->base, cfg->regs[QUSB2PHY_PORT_TUNE1],
420 val[0] << HSTX_TRIM_SHIFT,
423 qusb2_write_mask(qphy->base, cfg->regs[QUSB2PHY_PORT_TUNE2],
424 val[0] << HSTX_TRIM_SHIFT,
428 static int qusb2_phy_set_mode(struct phy *phy,
429 enum phy_mode mode, int submode)
431 struct qusb2_phy *qphy = phy_get_drvdata(phy);
438 static int __maybe_unused qusb2_phy_runtime_suspend(struct device *dev)
440 struct qusb2_phy *qphy = dev_get_drvdata(dev);
441 const struct qusb2_phy_cfg *cfg = qphy->cfg;
444 dev_vdbg(dev, "Suspending QUSB2 Phy, mode:%d\n", qphy->mode);
446 if (!qphy->phy_initialized) {
447 dev_vdbg(dev, "PHY not initialized, bailing out\n");
452 * Enable DP/DM interrupts to detect line state changes based on current
453 * speed. In other words, enable the triggers _opposite_ of what the
454 * current D+/D- levels are e.g. if currently D+ high, D- low
455 * (HS 'J'/Suspend), configure the mask to trigger on D+ low OR D- high
457 intr_mask = DPSE_INTR_EN | DMSE_INTR_EN;
458 switch (qphy->mode) {
459 case PHY_MODE_USB_HOST_HS:
460 case PHY_MODE_USB_HOST_FS:
461 case PHY_MODE_USB_DEVICE_HS:
462 case PHY_MODE_USB_DEVICE_FS:
463 intr_mask |= DMSE_INTR_HIGH_SEL;
465 case PHY_MODE_USB_HOST_LS:
466 case PHY_MODE_USB_DEVICE_LS:
467 intr_mask |= DPSE_INTR_HIGH_SEL;
470 /* No device connected, enable both DP/DM high interrupt */
471 intr_mask |= DMSE_INTR_HIGH_SEL;
472 intr_mask |= DPSE_INTR_HIGH_SEL;
476 writel(intr_mask, qphy->base + cfg->regs[QUSB2PHY_INTR_CTRL]);
478 /* hold core PLL into reset */
479 if (cfg->has_pll_override) {
480 qusb2_setbits(qphy->base,
481 cfg->regs[QUSB2PHY_PLL_CORE_INPUT_OVERRIDE],
482 CORE_PLL_EN_FROM_RESET | CORE_RESET |
486 /* enable phy auto-resume only if device is connected on bus */
487 if (qphy->mode != PHY_MODE_INVALID) {
488 qusb2_setbits(qphy->base, cfg->regs[QUSB2PHY_PORT_TEST1],
490 /* Autoresume bit has to be toggled in order to enable it */
491 qusb2_clrbits(qphy->base, cfg->regs[QUSB2PHY_PORT_TEST1],
495 if (!qphy->has_se_clk_scheme)
496 clk_disable_unprepare(qphy->ref_clk);
498 clk_disable_unprepare(qphy->cfg_ahb_clk);
499 clk_disable_unprepare(qphy->iface_clk);
504 static int __maybe_unused qusb2_phy_runtime_resume(struct device *dev)
506 struct qusb2_phy *qphy = dev_get_drvdata(dev);
507 const struct qusb2_phy_cfg *cfg = qphy->cfg;
510 dev_vdbg(dev, "Resuming QUSB2 phy, mode:%d\n", qphy->mode);
512 if (!qphy->phy_initialized) {
513 dev_vdbg(dev, "PHY not initialized, bailing out\n");
517 ret = clk_prepare_enable(qphy->iface_clk);
519 dev_err(dev, "failed to enable iface_clk, %d\n", ret);
523 ret = clk_prepare_enable(qphy->cfg_ahb_clk);
525 dev_err(dev, "failed to enable cfg ahb clock, %d\n", ret);
526 goto disable_iface_clk;
529 if (!qphy->has_se_clk_scheme) {
530 clk_prepare_enable(qphy->ref_clk);
532 dev_err(dev, "failed to enable ref clk, %d\n", ret);
533 goto disable_ahb_clk;
537 writel(0x0, qphy->base + cfg->regs[QUSB2PHY_INTR_CTRL]);
539 /* bring core PLL out of reset */
540 if (cfg->has_pll_override) {
541 qusb2_clrbits(qphy->base,
542 cfg->regs[QUSB2PHY_PLL_CORE_INPUT_OVERRIDE],
543 CORE_RESET | CORE_RESET_MUX);
549 clk_disable_unprepare(qphy->cfg_ahb_clk);
551 clk_disable_unprepare(qphy->iface_clk);
556 static int qusb2_phy_init(struct phy *phy)
558 struct qusb2_phy *qphy = phy_get_drvdata(phy);
559 const struct qusb2_phy_cfg *cfg = qphy->cfg;
560 unsigned int val = 0;
561 unsigned int clk_scheme;
564 dev_vdbg(&phy->dev, "%s(): Initializing QUSB2 phy\n", __func__);
566 /* turn on regulator supplies */
567 ret = regulator_bulk_enable(ARRAY_SIZE(qphy->vregs), qphy->vregs);
571 ret = clk_prepare_enable(qphy->iface_clk);
573 dev_err(&phy->dev, "failed to enable iface_clk, %d\n", ret);
577 /* enable ahb interface clock to program phy */
578 ret = clk_prepare_enable(qphy->cfg_ahb_clk);
580 dev_err(&phy->dev, "failed to enable cfg ahb clock, %d\n", ret);
581 goto disable_iface_clk;
584 /* Perform phy reset */
585 ret = reset_control_assert(qphy->phy_reset);
587 dev_err(&phy->dev, "failed to assert phy_reset, %d\n", ret);
588 goto disable_ahb_clk;
591 /* 100 us delay to keep PHY in reset mode */
592 usleep_range(100, 150);
594 ret = reset_control_deassert(qphy->phy_reset);
596 dev_err(&phy->dev, "failed to de-assert phy_reset, %d\n", ret);
597 goto disable_ahb_clk;
600 /* Disable the PHY */
601 qusb2_setbits(qphy->base, cfg->regs[QUSB2PHY_PORT_POWERDOWN],
602 qphy->cfg->disable_ctrl);
604 if (cfg->has_pll_test) {
605 /* save reset value to override reference clock scheme later */
606 val = readl(qphy->base + QUSB2PHY_PLL_TEST);
609 qcom_qusb2_phy_configure(qphy->base, cfg->regs, cfg->tbl,
612 /* Override board specific PHY tuning values */
613 qusb2_phy_override_phy_params(qphy);
615 /* Set efuse value for tuning the PHY */
616 qusb2_phy_set_tune2_param(qphy);
619 qusb2_clrbits(qphy->base, cfg->regs[QUSB2PHY_PORT_POWERDOWN],
622 /* Required to get phy pll lock successfully */
623 usleep_range(150, 160);
625 /* Default is single-ended clock on msm8996 */
626 qphy->has_se_clk_scheme = true;
628 * read TCSR_PHY_CLK_SCHEME register to check if single-ended
629 * clock scheme is selected. If yes, then disable differential
630 * ref_clk and use single-ended clock, otherwise use differential
634 ret = regmap_read(qphy->tcsr, qphy->cfg->clk_scheme_offset,
637 dev_err(&phy->dev, "failed to read clk scheme reg\n");
638 goto assert_phy_reset;
641 /* is it a differential clock scheme ? */
642 if (!(clk_scheme & PHY_CLK_SCHEME_SEL)) {
643 dev_vdbg(&phy->dev, "%s(): select differential clk\n",
645 qphy->has_se_clk_scheme = false;
647 dev_vdbg(&phy->dev, "%s(): select single-ended clk\n",
652 if (!qphy->has_se_clk_scheme) {
653 ret = clk_prepare_enable(qphy->ref_clk);
655 dev_err(&phy->dev, "failed to enable ref clk, %d\n",
657 goto assert_phy_reset;
661 if (cfg->has_pll_test) {
662 if (!qphy->has_se_clk_scheme)
667 writel(val, qphy->base + QUSB2PHY_PLL_TEST);
669 /* ensure above write is through */
670 readl(qphy->base + QUSB2PHY_PLL_TEST);
673 /* Required to get phy pll lock successfully */
674 usleep_range(100, 110);
676 val = readb(qphy->base + cfg->regs[QUSB2PHY_PLL_STATUS]);
677 if (!(val & cfg->mask_core_ready)) {
679 "QUSB2PHY pll lock failed: status reg = %x\n", val);
681 goto disable_ref_clk;
683 qphy->phy_initialized = true;
688 if (!qphy->has_se_clk_scheme)
689 clk_disable_unprepare(qphy->ref_clk);
691 reset_control_assert(qphy->phy_reset);
693 clk_disable_unprepare(qphy->cfg_ahb_clk);
695 clk_disable_unprepare(qphy->iface_clk);
697 regulator_bulk_disable(ARRAY_SIZE(qphy->vregs), qphy->vregs);
702 static int qusb2_phy_exit(struct phy *phy)
704 struct qusb2_phy *qphy = phy_get_drvdata(phy);
706 /* Disable the PHY */
707 qusb2_setbits(qphy->base, qphy->cfg->regs[QUSB2PHY_PORT_POWERDOWN],
708 qphy->cfg->disable_ctrl);
710 if (!qphy->has_se_clk_scheme)
711 clk_disable_unprepare(qphy->ref_clk);
713 reset_control_assert(qphy->phy_reset);
715 clk_disable_unprepare(qphy->cfg_ahb_clk);
716 clk_disable_unprepare(qphy->iface_clk);
718 regulator_bulk_disable(ARRAY_SIZE(qphy->vregs), qphy->vregs);
720 qphy->phy_initialized = false;
725 static const struct phy_ops qusb2_phy_gen_ops = {
726 .init = qusb2_phy_init,
727 .exit = qusb2_phy_exit,
728 .set_mode = qusb2_phy_set_mode,
729 .owner = THIS_MODULE,
732 static const struct of_device_id qusb2_phy_of_match_table[] = {
734 .compatible = "qcom,msm8996-qusb2-phy",
735 .data = &msm8996_phy_cfg,
737 .compatible = "qcom,sdm845-qusb2-phy",
738 .data = &sdm845_phy_cfg,
742 MODULE_DEVICE_TABLE(of, qusb2_phy_of_match_table);
744 static const struct dev_pm_ops qusb2_phy_pm_ops = {
745 SET_RUNTIME_PM_OPS(qusb2_phy_runtime_suspend,
746 qusb2_phy_runtime_resume, NULL)
749 static int qusb2_phy_probe(struct platform_device *pdev)
751 struct device *dev = &pdev->dev;
752 struct qusb2_phy *qphy;
753 struct phy_provider *phy_provider;
754 struct phy *generic_phy;
755 struct resource *res;
760 qphy = devm_kzalloc(dev, sizeof(*qphy), GFP_KERNEL);
764 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
765 qphy->base = devm_ioremap_resource(dev, res);
766 if (IS_ERR(qphy->base))
767 return PTR_ERR(qphy->base);
769 qphy->cfg_ahb_clk = devm_clk_get(dev, "cfg_ahb");
770 if (IS_ERR(qphy->cfg_ahb_clk)) {
771 ret = PTR_ERR(qphy->cfg_ahb_clk);
772 if (ret != -EPROBE_DEFER)
773 dev_err(dev, "failed to get cfg ahb clk, %d\n", ret);
777 qphy->ref_clk = devm_clk_get(dev, "ref");
778 if (IS_ERR(qphy->ref_clk)) {
779 ret = PTR_ERR(qphy->ref_clk);
780 if (ret != -EPROBE_DEFER)
781 dev_err(dev, "failed to get ref clk, %d\n", ret);
785 qphy->iface_clk = devm_clk_get(dev, "iface");
786 if (IS_ERR(qphy->iface_clk)) {
787 ret = PTR_ERR(qphy->iface_clk);
788 if (ret == -EPROBE_DEFER)
790 qphy->iface_clk = NULL;
791 dev_dbg(dev, "failed to get iface clk, %d\n", ret);
794 qphy->phy_reset = devm_reset_control_get_by_index(&pdev->dev, 0);
795 if (IS_ERR(qphy->phy_reset)) {
796 dev_err(dev, "failed to get phy core reset\n");
797 return PTR_ERR(qphy->phy_reset);
800 num = ARRAY_SIZE(qphy->vregs);
801 for (i = 0; i < num; i++)
802 qphy->vregs[i].supply = qusb2_phy_vreg_names[i];
804 ret = devm_regulator_bulk_get(dev, num, qphy->vregs);
806 if (ret != -EPROBE_DEFER)
807 dev_err(dev, "failed to get regulator supplies: %d\n",
812 /* Get the specific init parameters of QMP phy */
813 qphy->cfg = of_device_get_match_data(dev);
815 qphy->tcsr = syscon_regmap_lookup_by_phandle(dev->of_node,
817 if (IS_ERR(qphy->tcsr)) {
818 dev_dbg(dev, "failed to lookup TCSR regmap\n");
822 qphy->cell = devm_nvmem_cell_get(dev, NULL);
823 if (IS_ERR(qphy->cell)) {
824 if (PTR_ERR(qphy->cell) == -EPROBE_DEFER)
825 return -EPROBE_DEFER;
827 dev_dbg(dev, "failed to lookup tune2 hstx trim value\n");
830 if (!of_property_read_u32(dev->of_node, "qcom,imp-res-offset-value",
832 qphy->imp_res_offset_value = (u8)value;
833 qphy->override_imp_res_offset = true;
836 if (!of_property_read_u32(dev->of_node, "qcom,hstx-trim-value",
838 qphy->hstx_trim_value = (u8)value;
839 qphy->override_hstx_trim = true;
842 if (!of_property_read_u32(dev->of_node, "qcom,preemphasis-level",
844 qphy->preemphasis_level = (u8)value;
845 qphy->override_preemphasis = true;
848 if (!of_property_read_u32(dev->of_node, "qcom,preemphasis-width",
850 qphy->preemphasis_width = (u8)value;
851 qphy->override_preemphasis_width = true;
854 pm_runtime_set_active(dev);
855 pm_runtime_enable(dev);
857 * Prevent runtime pm from being ON by default. Users can enable
858 * it using power/control in sysfs.
860 pm_runtime_forbid(dev);
862 generic_phy = devm_phy_create(dev, NULL, &qusb2_phy_gen_ops);
863 if (IS_ERR(generic_phy)) {
864 ret = PTR_ERR(generic_phy);
865 dev_err(dev, "failed to create phy, %d\n", ret);
866 pm_runtime_disable(dev);
869 qphy->phy = generic_phy;
871 dev_set_drvdata(dev, qphy);
872 phy_set_drvdata(generic_phy, qphy);
874 phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
875 if (!IS_ERR(phy_provider))
876 dev_info(dev, "Registered Qcom-QUSB2 phy\n");
878 pm_runtime_disable(dev);
880 return PTR_ERR_OR_ZERO(phy_provider);
883 static struct platform_driver qusb2_phy_driver = {
884 .probe = qusb2_phy_probe,
886 .name = "qcom-qusb2-phy",
887 .pm = &qusb2_phy_pm_ops,
888 .of_match_table = qusb2_phy_of_match_table,
892 module_platform_driver(qusb2_phy_driver);
894 MODULE_AUTHOR("Vivek Gautam <vivek.gautam@codeaurora.org>");
895 MODULE_DESCRIPTION("Qualcomm QUSB2 PHY driver");
896 MODULE_LICENSE("GPL v2");