blob: 946e9b05f0ae634f1f4a5d4a4adaa57976a00766 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2017, The Linux Foundation. All rights reserved.
4 */
5
6#include <linux/clk.h>
7#include <linux/delay.h>
8#include <linux/err.h>
9#include <linux/io.h>
10#include <linux/kernel.h>
11#include <linux/mfd/syscon.h>
12#include <linux/module.h>
13#include <linux/nvmem-consumer.h>
14#include <linux/of.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>
22
23#include <dt-bindings/phy/phy-qcom-qusb2.h>
24
25#define QUSB2PHY_PLL_TEST 0x04
26#define CLK_REF_SEL BIT(7)
27
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
33
34/* QUSB2PHY_PLL_STATUS register bits */
35#define PLL_LOCKED BIT(5)
36
37/* QUSB2PHY_PLL_COMMON_STATUS_ONE register bits */
38#define CORE_READY_STATUS BIT(0)
39
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)
44
45/* QUSB2PHY_PWR_CTRL1 register bits */
46#define PWR_CTRL1_VREF_SUPPLY_TRIM BIT(5)
47#define PWR_CTRL1_CLAMP_N_EN BIT(1)
48
49#define QUSB2PHY_REFCLK_ENABLE BIT(0)
50
51#define PHY_CLK_SCHEME_SEL BIT(0)
52
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)
59
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)
64
65/* QUSB2PHY_IMP_CTRL1 register bits */
66#define IMP_RES_OFFSET_MASK GENMASK(5, 0)
67#define IMP_RES_OFFSET_SHIFT 0x0
68
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
75
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
87
88struct qusb2_phy_init_tbl {
89 unsigned int offset;
90 unsigned int val;
91 /*
92 * register part of layout ?
93 * if yes, then offset gives index in the reg-layout
94 */
95 int in_layout;
96};
97
98#define QUSB2_PHY_INIT_CFG(o, v) \
99 { \
100 .offset = o, \
101 .val = v, \
102 }
103
104#define QUSB2_PHY_INIT_CFG_L(o, v) \
105 { \
106 .offset = o, \
107 .val = v, \
108 .in_layout = 1, \
109 }
110
111/* set of registers with offsets different per-PHY */
112enum qusb2phy_reg_layout {
113 QUSB2PHY_PLL_CORE_INPUT_OVERRIDE,
114 QUSB2PHY_PLL_STATUS,
115 QUSB2PHY_PORT_TUNE1,
116 QUSB2PHY_PORT_TUNE2,
117 QUSB2PHY_PORT_TUNE3,
118 QUSB2PHY_PORT_TUNE4,
119 QUSB2PHY_PORT_TUNE5,
120 QUSB2PHY_PORT_TEST1,
121 QUSB2PHY_PORT_TEST2,
122 QUSB2PHY_PORT_POWERDOWN,
123 QUSB2PHY_INTR_CTRL,
124};
125
126static 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,
137};
138
139static 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),
144
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),
148
149 QUSB2_PHY_INIT_CFG_L(QUSB2PHY_PORT_TEST2, 0x14),
150
151 QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_AUTOPGM_CTL1, 0x9f),
152 QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_PWR_CTRL, 0x00),
153};
154
155static const unsigned int msm8998_regs_layout[] = {
156 [QUSB2PHY_PLL_CORE_INPUT_OVERRIDE] = 0xa8,
157 [QUSB2PHY_PLL_STATUS] = 0x1a0,
158 [QUSB2PHY_PORT_TUNE1] = 0x23c,
159 [QUSB2PHY_PORT_TUNE2] = 0x240,
160 [QUSB2PHY_PORT_TUNE3] = 0x244,
161 [QUSB2PHY_PORT_TUNE4] = 0x248,
162 [QUSB2PHY_PORT_TEST1] = 0x24c,
163 [QUSB2PHY_PORT_TEST2] = 0x250,
164 [QUSB2PHY_PORT_POWERDOWN] = 0x210,
165 [QUSB2PHY_INTR_CTRL] = 0x22c,
166};
167
168static const struct qusb2_phy_init_tbl msm8998_init_tbl[] = {
169 QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_ANALOG_CONTROLS_TWO, 0x13),
170 QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_CLOCK_INVERTERS, 0x7c),
171 QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_CMODE, 0x80),
172 QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_LOCK_DELAY, 0x0a),
173
174 QUSB2_PHY_INIT_CFG_L(QUSB2PHY_PORT_TUNE1, 0xa5),
175 QUSB2_PHY_INIT_CFG_L(QUSB2PHY_PORT_TUNE2, 0x09),
176
177 QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_DIGITAL_TIMERS_TWO, 0x19),
178};
179
180static const unsigned int sdm845_regs_layout[] = {
181 [QUSB2PHY_PLL_CORE_INPUT_OVERRIDE] = 0xa8,
182 [QUSB2PHY_PLL_STATUS] = 0x1a0,
183 [QUSB2PHY_PORT_TUNE1] = 0x240,
184 [QUSB2PHY_PORT_TUNE2] = 0x244,
185 [QUSB2PHY_PORT_TUNE3] = 0x248,
186 [QUSB2PHY_PORT_TUNE4] = 0x24c,
187 [QUSB2PHY_PORT_TUNE5] = 0x250,
188 [QUSB2PHY_PORT_TEST1] = 0x254,
189 [QUSB2PHY_PORT_TEST2] = 0x258,
190 [QUSB2PHY_PORT_POWERDOWN] = 0x210,
191 [QUSB2PHY_INTR_CTRL] = 0x230,
192};
193
194static const struct qusb2_phy_init_tbl sdm845_init_tbl[] = {
195 QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_ANALOG_CONTROLS_TWO, 0x03),
196 QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_CLOCK_INVERTERS, 0x7c),
197 QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_CMODE, 0x80),
198 QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_LOCK_DELAY, 0x0a),
199 QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_DIGITAL_TIMERS_TWO, 0x19),
200 QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_BIAS_CONTROL_1, 0x40),
201 QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_BIAS_CONTROL_2, 0x20),
202 QUSB2_PHY_INIT_CFG(QUSB2PHY_PWR_CTRL2, 0x21),
203 QUSB2_PHY_INIT_CFG(QUSB2PHY_IMP_CTRL1, 0x0),
204 QUSB2_PHY_INIT_CFG(QUSB2PHY_IMP_CTRL2, 0x58),
205
206 QUSB2_PHY_INIT_CFG_L(QUSB2PHY_PORT_TUNE1, 0x30),
207 QUSB2_PHY_INIT_CFG_L(QUSB2PHY_PORT_TUNE2, 0x29),
208 QUSB2_PHY_INIT_CFG_L(QUSB2PHY_PORT_TUNE3, 0xca),
209 QUSB2_PHY_INIT_CFG_L(QUSB2PHY_PORT_TUNE4, 0x04),
210 QUSB2_PHY_INIT_CFG_L(QUSB2PHY_PORT_TUNE5, 0x03),
211
212 QUSB2_PHY_INIT_CFG(QUSB2PHY_CHG_CTRL2, 0x0),
213};
214
215struct qusb2_phy_cfg {
216 const struct qusb2_phy_init_tbl *tbl;
217 /* number of entries in the table */
218 unsigned int tbl_num;
219 /* offset to PHY_CLK_SCHEME register in TCSR map */
220 unsigned int clk_scheme_offset;
221
222 /* array of registers with different offsets */
223 const unsigned int *regs;
224 unsigned int mask_core_ready;
225 unsigned int disable_ctrl;
226 unsigned int autoresume_en;
227
228 /* true if PHY has PLL_TEST register to select clk_scheme */
229 bool has_pll_test;
230
231 /* true if TUNE1 register must be updated by fused value, else TUNE2 */
232 bool update_tune1_with_efuse;
233
234 /* true if PHY has PLL_CORE_INPUT_OVERRIDE register to reset PLL */
235 bool has_pll_override;
236};
237
238static const struct qusb2_phy_cfg msm8996_phy_cfg = {
239 .tbl = msm8996_init_tbl,
240 .tbl_num = ARRAY_SIZE(msm8996_init_tbl),
241 .regs = msm8996_regs_layout,
242
243 .has_pll_test = true,
244 .disable_ctrl = (CLAMP_N_EN | FREEZIO_N | POWER_DOWN),
245 .mask_core_ready = PLL_LOCKED,
246 .autoresume_en = BIT(3),
247};
248
249static const struct qusb2_phy_cfg msm8998_phy_cfg = {
250 .tbl = msm8998_init_tbl,
251 .tbl_num = ARRAY_SIZE(msm8998_init_tbl),
252 .regs = msm8998_regs_layout,
253
254 .disable_ctrl = POWER_DOWN,
255 .mask_core_ready = CORE_READY_STATUS,
256 .has_pll_override = true,
257 .autoresume_en = BIT(0),
258 .update_tune1_with_efuse = true,
259};
260
261static const struct qusb2_phy_cfg sdm845_phy_cfg = {
262 .tbl = sdm845_init_tbl,
263 .tbl_num = ARRAY_SIZE(sdm845_init_tbl),
264 .regs = sdm845_regs_layout,
265
266 .disable_ctrl = (PWR_CTRL1_VREF_SUPPLY_TRIM | PWR_CTRL1_CLAMP_N_EN |
267 POWER_DOWN),
268 .mask_core_ready = CORE_READY_STATUS,
269 .has_pll_override = true,
270 .autoresume_en = BIT(0),
271 .update_tune1_with_efuse = true,
272};
273
274static const char * const qusb2_phy_vreg_names[] = {
275 "vdda-pll", "vdda-phy-dpdm",
276};
277
278#define QUSB2_NUM_VREGS ARRAY_SIZE(qusb2_phy_vreg_names)
279
280/**
281 * struct qusb2_phy - structure holding qusb2 phy attributes
282 *
283 * @phy: generic phy
284 * @base: iomapped memory space for qubs2 phy
285 *
286 * @cfg_ahb_clk: AHB2PHY interface clock
287 * @ref_clk: phy reference clock
288 * @iface_clk: phy interface clock
289 * @phy_reset: phy reset control
290 * @vregs: regulator supplies bulk data
291 *
292 * @tcsr: TCSR syscon register map
293 * @cell: nvmem cell containing phy tuning value
294 *
295 * @override_imp_res_offset: PHY should use different rescode offset
296 * @imp_res_offset_value: rescode offset to be updated in IMP_CTRL1 register
297 * @override_hstx_trim: PHY should use different HSTX o/p current value
298 * @hstx_trim_value: HSTX_TRIM value to be updated in TUNE1 register
299 * @override_preemphasis: PHY should use different pre-amphasis amplitude
300 * @preemphasis_level: Amplitude Pre-Emphasis to be updated in TUNE1 register
301 * @override_preemphasis_width: PHY should use different pre-emphasis duration
302 * @preemphasis_width: half/full-width Pre-Emphasis updated via TUNE1
303 *
304 * @cfg: phy config data
305 * @has_se_clk_scheme: indicate if PHY has single-ended ref clock scheme
306 * @phy_initialized: indicate if PHY has been initialized
307 * @mode: current PHY mode
308 */
309struct qusb2_phy {
310 struct phy *phy;
311 void __iomem *base;
312
313 struct clk *cfg_ahb_clk;
314 struct clk *ref_clk;
315 struct clk *iface_clk;
316 struct reset_control *phy_reset;
317 struct regulator_bulk_data vregs[QUSB2_NUM_VREGS];
318
319 struct regmap *tcsr;
320 struct nvmem_cell *cell;
321
322 bool override_imp_res_offset;
323 u8 imp_res_offset_value;
324 bool override_hstx_trim;
325 u8 hstx_trim_value;
326 bool override_preemphasis;
327 u8 preemphasis_level;
328 bool override_preemphasis_width;
329 u8 preemphasis_width;
330
331 const struct qusb2_phy_cfg *cfg;
332 bool has_se_clk_scheme;
333 bool phy_initialized;
334 enum phy_mode mode;
335};
336
337static inline void qusb2_write_mask(void __iomem *base, u32 offset,
338 u32 val, u32 mask)
339{
340 u32 reg;
341
342 reg = readl(base + offset);
343 reg &= ~mask;
344 reg |= val & mask;
345 writel(reg, base + offset);
346
347 /* Ensure above write is completed */
348 readl(base + offset);
349}
350
351static inline void qusb2_setbits(void __iomem *base, u32 offset, u32 val)
352{
353 u32 reg;
354
355 reg = readl(base + offset);
356 reg |= val;
357 writel(reg, base + offset);
358
359 /* Ensure above write is completed */
360 readl(base + offset);
361}
362
363static inline void qusb2_clrbits(void __iomem *base, u32 offset, u32 val)
364{
365 u32 reg;
366
367 reg = readl(base + offset);
368 reg &= ~val;
369 writel(reg, base + offset);
370
371 /* Ensure above write is completed */
372 readl(base + offset);
373}
374
375static inline
376void qcom_qusb2_phy_configure(void __iomem *base,
377 const unsigned int *regs,
378 const struct qusb2_phy_init_tbl tbl[], int num)
379{
380 int i;
381
382 for (i = 0; i < num; i++) {
383 if (tbl[i].in_layout)
384 writel(tbl[i].val, base + regs[tbl[i].offset]);
385 else
386 writel(tbl[i].val, base + tbl[i].offset);
387 }
388}
389
390/*
391 * Update board specific PHY tuning override values if specified from
392 * device tree.
393 */
394static void qusb2_phy_override_phy_params(struct qusb2_phy *qphy)
395{
396 const struct qusb2_phy_cfg *cfg = qphy->cfg;
397
398 if (qphy->override_imp_res_offset)
399 qusb2_write_mask(qphy->base, QUSB2PHY_IMP_CTRL1,
400 qphy->imp_res_offset_value << IMP_RES_OFFSET_SHIFT,
401 IMP_RES_OFFSET_MASK);
402
403 if (qphy->override_hstx_trim)
404 qusb2_write_mask(qphy->base, cfg->regs[QUSB2PHY_PORT_TUNE1],
405 qphy->hstx_trim_value << HSTX_TRIM_SHIFT,
406 HSTX_TRIM_MASK);
407
408 if (qphy->override_preemphasis)
409 qusb2_write_mask(qphy->base, cfg->regs[QUSB2PHY_PORT_TUNE1],
410 qphy->preemphasis_level << PREEMPHASIS_EN_SHIFT,
411 PREEMPHASIS_EN_MASK);
412
413 if (qphy->override_preemphasis_width) {
414 if (qphy->preemphasis_width ==
415 QUSB2_V2_PREEMPHASIS_WIDTH_HALF_BIT)
416 qusb2_setbits(qphy->base,
417 cfg->regs[QUSB2PHY_PORT_TUNE1],
418 PREEMPH_WIDTH_HALF_BIT);
419 else
420 qusb2_clrbits(qphy->base,
421 cfg->regs[QUSB2PHY_PORT_TUNE1],
422 PREEMPH_WIDTH_HALF_BIT);
423 }
424}
425
426/*
427 * Fetches HS Tx tuning value from nvmem and sets the
428 * QUSB2PHY_PORT_TUNE1/2 register.
429 * For error case, skip setting the value and use the default value.
430 */
431static void qusb2_phy_set_tune2_param(struct qusb2_phy *qphy)
432{
433 struct device *dev = &qphy->phy->dev;
434 const struct qusb2_phy_cfg *cfg = qphy->cfg;
435 u8 *val, hstx_trim;
436
437 /* efuse register is optional */
438 if (!qphy->cell)
439 return;
440
441 /*
442 * Read efuse register having TUNE2/1 parameter's high nibble.
443 * If efuse register shows value as 0x0 (indicating value is not
444 * fused), or if we fail to find a valid efuse register setting,
445 * then use default value for high nibble that we have already
446 * set while configuring the phy.
447 */
448 val = nvmem_cell_read(qphy->cell, NULL);
449 if (IS_ERR(val)) {
450 dev_dbg(dev, "failed to read a valid hs-tx trim value\n");
451 return;
452 }
453 hstx_trim = val[0];
454 kfree(val);
455 if (!hstx_trim) {
456 dev_dbg(dev, "failed to read a valid hs-tx trim value\n");
457 return;
458 }
459
460 /* Fused TUNE1/2 value is the higher nibble only */
461 if (cfg->update_tune1_with_efuse)
462 qusb2_write_mask(qphy->base, cfg->regs[QUSB2PHY_PORT_TUNE1],
463 hstx_trim << HSTX_TRIM_SHIFT, HSTX_TRIM_MASK);
464 else
465 qusb2_write_mask(qphy->base, cfg->regs[QUSB2PHY_PORT_TUNE2],
466 hstx_trim << HSTX_TRIM_SHIFT, HSTX_TRIM_MASK);
467}
468
469static int qusb2_phy_set_mode(struct phy *phy,
470 enum phy_mode mode, int submode)
471{
472 struct qusb2_phy *qphy = phy_get_drvdata(phy);
473
474 qphy->mode = mode;
475
476 return 0;
477}
478
479static int __maybe_unused qusb2_phy_runtime_suspend(struct device *dev)
480{
481 struct qusb2_phy *qphy = dev_get_drvdata(dev);
482 const struct qusb2_phy_cfg *cfg = qphy->cfg;
483 u32 intr_mask;
484
485 dev_vdbg(dev, "Suspending QUSB2 Phy, mode:%d\n", qphy->mode);
486
487 if (!qphy->phy_initialized) {
488 dev_vdbg(dev, "PHY not initialized, bailing out\n");
489 return 0;
490 }
491
492 /*
493 * Enable DP/DM interrupts to detect line state changes based on current
494 * speed. In other words, enable the triggers _opposite_ of what the
495 * current D+/D- levels are e.g. if currently D+ high, D- low
496 * (HS 'J'/Suspend), configure the mask to trigger on D+ low OR D- high
497 */
498 intr_mask = DPSE_INTR_EN | DMSE_INTR_EN;
499 switch (qphy->mode) {
500 case PHY_MODE_USB_HOST_HS:
501 case PHY_MODE_USB_HOST_FS:
502 case PHY_MODE_USB_DEVICE_HS:
503 case PHY_MODE_USB_DEVICE_FS:
504 intr_mask |= DMSE_INTR_HIGH_SEL;
505 break;
506 case PHY_MODE_USB_HOST_LS:
507 case PHY_MODE_USB_DEVICE_LS:
508 intr_mask |= DPSE_INTR_HIGH_SEL;
509 break;
510 default:
511 /* No device connected, enable both DP/DM high interrupt */
512 intr_mask |= DMSE_INTR_HIGH_SEL;
513 intr_mask |= DPSE_INTR_HIGH_SEL;
514 break;
515 }
516
517 writel(intr_mask, qphy->base + cfg->regs[QUSB2PHY_INTR_CTRL]);
518
519 /* hold core PLL into reset */
520 if (cfg->has_pll_override) {
521 qusb2_setbits(qphy->base,
522 cfg->regs[QUSB2PHY_PLL_CORE_INPUT_OVERRIDE],
523 CORE_PLL_EN_FROM_RESET | CORE_RESET |
524 CORE_RESET_MUX);
525 }
526
527 /* enable phy auto-resume only if device is connected on bus */
528 if (qphy->mode != PHY_MODE_INVALID) {
529 qusb2_setbits(qphy->base, cfg->regs[QUSB2PHY_PORT_TEST1],
530 cfg->autoresume_en);
531 /* Autoresume bit has to be toggled in order to enable it */
532 qusb2_clrbits(qphy->base, cfg->regs[QUSB2PHY_PORT_TEST1],
533 cfg->autoresume_en);
534 }
535
536 if (!qphy->has_se_clk_scheme)
537 clk_disable_unprepare(qphy->ref_clk);
538
539 clk_disable_unprepare(qphy->cfg_ahb_clk);
540 clk_disable_unprepare(qphy->iface_clk);
541
542 return 0;
543}
544
545static int __maybe_unused qusb2_phy_runtime_resume(struct device *dev)
546{
547 struct qusb2_phy *qphy = dev_get_drvdata(dev);
548 const struct qusb2_phy_cfg *cfg = qphy->cfg;
549 int ret;
550
551 dev_vdbg(dev, "Resuming QUSB2 phy, mode:%d\n", qphy->mode);
552
553 if (!qphy->phy_initialized) {
554 dev_vdbg(dev, "PHY not initialized, bailing out\n");
555 return 0;
556 }
557
558 ret = clk_prepare_enable(qphy->iface_clk);
559 if (ret) {
560 dev_err(dev, "failed to enable iface_clk, %d\n", ret);
561 return ret;
562 }
563
564 ret = clk_prepare_enable(qphy->cfg_ahb_clk);
565 if (ret) {
566 dev_err(dev, "failed to enable cfg ahb clock, %d\n", ret);
567 goto disable_iface_clk;
568 }
569
570 if (!qphy->has_se_clk_scheme) {
571 ret = clk_prepare_enable(qphy->ref_clk);
572 if (ret) {
573 dev_err(dev, "failed to enable ref clk, %d\n", ret);
574 goto disable_ahb_clk;
575 }
576 }
577
578 writel(0x0, qphy->base + cfg->regs[QUSB2PHY_INTR_CTRL]);
579
580 /* bring core PLL out of reset */
581 if (cfg->has_pll_override) {
582 qusb2_clrbits(qphy->base,
583 cfg->regs[QUSB2PHY_PLL_CORE_INPUT_OVERRIDE],
584 CORE_RESET | CORE_RESET_MUX);
585 }
586
587 return 0;
588
589disable_ahb_clk:
590 clk_disable_unprepare(qphy->cfg_ahb_clk);
591disable_iface_clk:
592 clk_disable_unprepare(qphy->iface_clk);
593
594 return ret;
595}
596
597static int qusb2_phy_init(struct phy *phy)
598{
599 struct qusb2_phy *qphy = phy_get_drvdata(phy);
600 const struct qusb2_phy_cfg *cfg = qphy->cfg;
601 unsigned int val = 0;
602 unsigned int clk_scheme;
603 int ret;
604
605 dev_vdbg(&phy->dev, "%s(): Initializing QUSB2 phy\n", __func__);
606
607 /* turn on regulator supplies */
608 ret = regulator_bulk_enable(ARRAY_SIZE(qphy->vregs), qphy->vregs);
609 if (ret)
610 return ret;
611
612 ret = clk_prepare_enable(qphy->iface_clk);
613 if (ret) {
614 dev_err(&phy->dev, "failed to enable iface_clk, %d\n", ret);
615 goto poweroff_phy;
616 }
617
618 /* enable ahb interface clock to program phy */
619 ret = clk_prepare_enable(qphy->cfg_ahb_clk);
620 if (ret) {
621 dev_err(&phy->dev, "failed to enable cfg ahb clock, %d\n", ret);
622 goto disable_iface_clk;
623 }
624
625 /* Perform phy reset */
626 ret = reset_control_assert(qphy->phy_reset);
627 if (ret) {
628 dev_err(&phy->dev, "failed to assert phy_reset, %d\n", ret);
629 goto disable_ahb_clk;
630 }
631
632 /* 100 us delay to keep PHY in reset mode */
633 usleep_range(100, 150);
634
635 ret = reset_control_deassert(qphy->phy_reset);
636 if (ret) {
637 dev_err(&phy->dev, "failed to de-assert phy_reset, %d\n", ret);
638 goto disable_ahb_clk;
639 }
640
641 /* Disable the PHY */
642 qusb2_setbits(qphy->base, cfg->regs[QUSB2PHY_PORT_POWERDOWN],
643 qphy->cfg->disable_ctrl);
644
645 if (cfg->has_pll_test) {
646 /* save reset value to override reference clock scheme later */
647 val = readl(qphy->base + QUSB2PHY_PLL_TEST);
648 }
649
650 qcom_qusb2_phy_configure(qphy->base, cfg->regs, cfg->tbl,
651 cfg->tbl_num);
652
653 /* Override board specific PHY tuning values */
654 qusb2_phy_override_phy_params(qphy);
655
656 /* Set efuse value for tuning the PHY */
657 qusb2_phy_set_tune2_param(qphy);
658
659 /* Enable the PHY */
660 qusb2_clrbits(qphy->base, cfg->regs[QUSB2PHY_PORT_POWERDOWN],
661 POWER_DOWN);
662
663 /* Required to get phy pll lock successfully */
664 usleep_range(150, 160);
665
666 /* Default is single-ended clock on msm8996 */
667 qphy->has_se_clk_scheme = true;
668 /*
669 * read TCSR_PHY_CLK_SCHEME register to check if single-ended
670 * clock scheme is selected. If yes, then disable differential
671 * ref_clk and use single-ended clock, otherwise use differential
672 * ref_clk only.
673 */
674 if (qphy->tcsr) {
675 ret = regmap_read(qphy->tcsr, qphy->cfg->clk_scheme_offset,
676 &clk_scheme);
677 if (ret) {
678 dev_err(&phy->dev, "failed to read clk scheme reg\n");
679 goto assert_phy_reset;
680 }
681
682 /* is it a differential clock scheme ? */
683 if (!(clk_scheme & PHY_CLK_SCHEME_SEL)) {
684 dev_vdbg(&phy->dev, "%s(): select differential clk\n",
685 __func__);
686 qphy->has_se_clk_scheme = false;
687 } else {
688 dev_vdbg(&phy->dev, "%s(): select single-ended clk\n",
689 __func__);
690 }
691 }
692
693 if (!qphy->has_se_clk_scheme) {
694 ret = clk_prepare_enable(qphy->ref_clk);
695 if (ret) {
696 dev_err(&phy->dev, "failed to enable ref clk, %d\n",
697 ret);
698 goto assert_phy_reset;
699 }
700 }
701
702 if (cfg->has_pll_test) {
703 if (!qphy->has_se_clk_scheme)
704 val &= ~CLK_REF_SEL;
705 else
706 val |= CLK_REF_SEL;
707
708 writel(val, qphy->base + QUSB2PHY_PLL_TEST);
709
710 /* ensure above write is through */
711 readl(qphy->base + QUSB2PHY_PLL_TEST);
712 }
713
714 /* Required to get phy pll lock successfully */
715 usleep_range(100, 110);
716
717 val = readb(qphy->base + cfg->regs[QUSB2PHY_PLL_STATUS]);
718 if (!(val & cfg->mask_core_ready)) {
719 dev_err(&phy->dev,
720 "QUSB2PHY pll lock failed: status reg = %x\n", val);
721 ret = -EBUSY;
722 goto disable_ref_clk;
723 }
724 qphy->phy_initialized = true;
725
726 return 0;
727
728disable_ref_clk:
729 if (!qphy->has_se_clk_scheme)
730 clk_disable_unprepare(qphy->ref_clk);
731assert_phy_reset:
732 reset_control_assert(qphy->phy_reset);
733disable_ahb_clk:
734 clk_disable_unprepare(qphy->cfg_ahb_clk);
735disable_iface_clk:
736 clk_disable_unprepare(qphy->iface_clk);
737poweroff_phy:
738 regulator_bulk_disable(ARRAY_SIZE(qphy->vregs), qphy->vregs);
739
740 return ret;
741}
742
743static int qusb2_phy_exit(struct phy *phy)
744{
745 struct qusb2_phy *qphy = phy_get_drvdata(phy);
746
747 /* Disable the PHY */
748 qusb2_setbits(qphy->base, qphy->cfg->regs[QUSB2PHY_PORT_POWERDOWN],
749 qphy->cfg->disable_ctrl);
750
751 if (!qphy->has_se_clk_scheme)
752 clk_disable_unprepare(qphy->ref_clk);
753
754 reset_control_assert(qphy->phy_reset);
755
756 clk_disable_unprepare(qphy->cfg_ahb_clk);
757 clk_disable_unprepare(qphy->iface_clk);
758
759 regulator_bulk_disable(ARRAY_SIZE(qphy->vregs), qphy->vregs);
760
761 qphy->phy_initialized = false;
762
763 return 0;
764}
765
766static const struct phy_ops qusb2_phy_gen_ops = {
767 .init = qusb2_phy_init,
768 .exit = qusb2_phy_exit,
769 .set_mode = qusb2_phy_set_mode,
770 .owner = THIS_MODULE,
771};
772
773static const struct of_device_id qusb2_phy_of_match_table[] = {
774 {
775 .compatible = "qcom,msm8996-qusb2-phy",
776 .data = &msm8996_phy_cfg,
777 }, {
778 .compatible = "qcom,msm8998-qusb2-phy",
779 .data = &msm8998_phy_cfg,
780 }, {
781 .compatible = "qcom,sdm845-qusb2-phy",
782 .data = &sdm845_phy_cfg,
783 },
784 { },
785};
786MODULE_DEVICE_TABLE(of, qusb2_phy_of_match_table);
787
788static const struct dev_pm_ops qusb2_phy_pm_ops = {
789 SET_RUNTIME_PM_OPS(qusb2_phy_runtime_suspend,
790 qusb2_phy_runtime_resume, NULL)
791};
792
793static int qusb2_phy_probe(struct platform_device *pdev)
794{
795 struct device *dev = &pdev->dev;
796 struct qusb2_phy *qphy;
797 struct phy_provider *phy_provider;
798 struct phy *generic_phy;
799 struct resource *res;
800 int ret, i;
801 int num;
802 u32 value;
803
804 qphy = devm_kzalloc(dev, sizeof(*qphy), GFP_KERNEL);
805 if (!qphy)
806 return -ENOMEM;
807
808 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
809 qphy->base = devm_ioremap_resource(dev, res);
810 if (IS_ERR(qphy->base))
811 return PTR_ERR(qphy->base);
812
813 qphy->cfg_ahb_clk = devm_clk_get(dev, "cfg_ahb");
814 if (IS_ERR(qphy->cfg_ahb_clk)) {
815 ret = PTR_ERR(qphy->cfg_ahb_clk);
816 if (ret != -EPROBE_DEFER)
817 dev_err(dev, "failed to get cfg ahb clk, %d\n", ret);
818 return ret;
819 }
820
821 qphy->ref_clk = devm_clk_get(dev, "ref");
822 if (IS_ERR(qphy->ref_clk)) {
823 ret = PTR_ERR(qphy->ref_clk);
824 if (ret != -EPROBE_DEFER)
825 dev_err(dev, "failed to get ref clk, %d\n", ret);
826 return ret;
827 }
828
829 qphy->iface_clk = devm_clk_get_optional(dev, "iface");
830 if (IS_ERR(qphy->iface_clk))
831 return PTR_ERR(qphy->iface_clk);
832
833 qphy->phy_reset = devm_reset_control_get_by_index(&pdev->dev, 0);
834 if (IS_ERR(qphy->phy_reset)) {
835 dev_err(dev, "failed to get phy core reset\n");
836 return PTR_ERR(qphy->phy_reset);
837 }
838
839 num = ARRAY_SIZE(qphy->vregs);
840 for (i = 0; i < num; i++)
841 qphy->vregs[i].supply = qusb2_phy_vreg_names[i];
842
843 ret = devm_regulator_bulk_get(dev, num, qphy->vregs);
844 if (ret) {
845 if (ret != -EPROBE_DEFER)
846 dev_err(dev, "failed to get regulator supplies: %d\n",
847 ret);
848 return ret;
849 }
850
851 /* Get the specific init parameters of QMP phy */
852 qphy->cfg = of_device_get_match_data(dev);
853
854 qphy->tcsr = syscon_regmap_lookup_by_phandle(dev->of_node,
855 "qcom,tcsr-syscon");
856 if (IS_ERR(qphy->tcsr)) {
857 dev_dbg(dev, "failed to lookup TCSR regmap\n");
858 qphy->tcsr = NULL;
859 }
860
861 qphy->cell = devm_nvmem_cell_get(dev, NULL);
862 if (IS_ERR(qphy->cell)) {
863 if (PTR_ERR(qphy->cell) == -EPROBE_DEFER)
864 return -EPROBE_DEFER;
865 qphy->cell = NULL;
866 dev_dbg(dev, "failed to lookup tune2 hstx trim value\n");
867 }
868
869 if (!of_property_read_u32(dev->of_node, "qcom,imp-res-offset-value",
870 &value)) {
871 qphy->imp_res_offset_value = (u8)value;
872 qphy->override_imp_res_offset = true;
873 }
874
875 if (!of_property_read_u32(dev->of_node, "qcom,hstx-trim-value",
876 &value)) {
877 qphy->hstx_trim_value = (u8)value;
878 qphy->override_hstx_trim = true;
879 }
880
881 if (!of_property_read_u32(dev->of_node, "qcom,preemphasis-level",
882 &value)) {
883 qphy->preemphasis_level = (u8)value;
884 qphy->override_preemphasis = true;
885 }
886
887 if (!of_property_read_u32(dev->of_node, "qcom,preemphasis-width",
888 &value)) {
889 qphy->preemphasis_width = (u8)value;
890 qphy->override_preemphasis_width = true;
891 }
892
893 pm_runtime_set_active(dev);
894 pm_runtime_enable(dev);
895 /*
896 * Prevent runtime pm from being ON by default. Users can enable
897 * it using power/control in sysfs.
898 */
899 pm_runtime_forbid(dev);
900
901 generic_phy = devm_phy_create(dev, NULL, &qusb2_phy_gen_ops);
902 if (IS_ERR(generic_phy)) {
903 ret = PTR_ERR(generic_phy);
904 dev_err(dev, "failed to create phy, %d\n", ret);
905 pm_runtime_disable(dev);
906 return ret;
907 }
908 qphy->phy = generic_phy;
909
910 dev_set_drvdata(dev, qphy);
911 phy_set_drvdata(generic_phy, qphy);
912
913 phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
914 if (!IS_ERR(phy_provider))
915 dev_info(dev, "Registered Qcom-QUSB2 phy\n");
916 else
917 pm_runtime_disable(dev);
918
919 return PTR_ERR_OR_ZERO(phy_provider);
920}
921
922static struct platform_driver qusb2_phy_driver = {
923 .probe = qusb2_phy_probe,
924 .driver = {
925 .name = "qcom-qusb2-phy",
926 .pm = &qusb2_phy_pm_ops,
927 .of_match_table = qusb2_phy_of_match_table,
928 },
929};
930
931module_platform_driver(qusb2_phy_driver);
932
933MODULE_AUTHOR("Vivek Gautam <vivek.gautam@codeaurora.org>");
934MODULE_DESCRIPTION("Qualcomm QUSB2 PHY driver");
935MODULE_LICENSE("GPL v2");