1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2017, The Linux Foundation. All rights reserved.
7 #include <linux/clk-provider.h>
8 #include <linux/delay.h>
11 #include <linux/iopoll.h>
12 #include <linux/kernel.h>
13 #include <linux/mfd/syscon.h>
14 #include <linux/module.h>
16 #include <linux/of_address.h>
17 #include <linux/phy/phy.h>
18 #include <linux/platform_device.h>
19 #include <linux/regmap.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/reset.h>
22 #include <linux/slab.h>
23 #include <linux/usb/typec.h>
24 #include <linux/usb/typec_mux.h>
26 #include "phy-qcom-qmp-common.h"
28 #include "phy-qcom-qmp.h"
29 #include "phy-qcom-qmp-pcs-misc-v3.h"
31 #define PHY_INIT_COMPLETE_TIMEOUT 10000
33 /* set of registers with offsets different per-PHY */
34 enum qphy_reg_layout {
39 QPHY_PCS_AUTONOMOUS_MODE_CTRL,
40 QPHY_PCS_LFPS_RXTERM_IRQ_CLEAR,
41 QPHY_PCS_POWER_DOWN_CONTROL,
42 /* Keep last to ensure regs_layout arrays are properly initialized */
46 static const unsigned int qmp_v3_usb3phy_regs_layout[QPHY_LAYOUT_SIZE] = {
47 [QPHY_SW_RESET] = QPHY_V3_PCS_SW_RESET,
48 [QPHY_START_CTRL] = QPHY_V3_PCS_START_CONTROL,
49 [QPHY_PCS_STATUS] = QPHY_V3_PCS_PCS_STATUS,
50 [QPHY_PCS_AUTONOMOUS_MODE_CTRL] = QPHY_V3_PCS_AUTONOMOUS_MODE_CTRL,
51 [QPHY_PCS_LFPS_RXTERM_IRQ_CLEAR] = QPHY_V3_PCS_LFPS_RXTERM_IRQ_CLEAR,
52 [QPHY_PCS_POWER_DOWN_CONTROL] = QPHY_V3_PCS_POWER_DOWN_CONTROL,
55 static const unsigned int qmp_v3_usb3phy_regs_layout_qcm2290[QPHY_LAYOUT_SIZE] = {
56 [QPHY_SW_RESET] = QPHY_V3_PCS_SW_RESET,
57 [QPHY_START_CTRL] = QPHY_V3_PCS_START_CONTROL,
58 [QPHY_PCS_STATUS] = QPHY_V3_PCS_PCS_STATUS,
59 [QPHY_PCS_AUTONOMOUS_MODE_CTRL] = QPHY_V3_PCS_AUTONOMOUS_MODE_CTRL,
60 [QPHY_PCS_LFPS_RXTERM_IRQ_CLEAR] = QPHY_V3_PCS_LFPS_RXTERM_IRQ_CLEAR,
61 [QPHY_PCS_POWER_DOWN_CONTROL] = QPHY_V3_PCS_POWER_DOWN_CONTROL,
64 static const struct qmp_phy_init_tbl msm8998_usb3_serdes_tbl[] = {
65 QMP_PHY_INIT_CFG(QSERDES_V3_COM_CLK_SELECT, 0x30),
66 QMP_PHY_INIT_CFG(QSERDES_V3_COM_BIAS_EN_CLKBUFLR_EN, 0x04),
67 QMP_PHY_INIT_CFG(QSERDES_V3_COM_SYSCLK_EN_SEL, 0x14),
68 QMP_PHY_INIT_CFG(QSERDES_V3_COM_SYS_CLK_CTRL, 0x06),
69 QMP_PHY_INIT_CFG(QSERDES_V3_COM_RESETSM_CNTRL2, 0x08),
70 QMP_PHY_INIT_CFG(QSERDES_V3_COM_CMN_CONFIG, 0x06),
71 QMP_PHY_INIT_CFG(QSERDES_V3_COM_SVS_MODE_CLK_SEL, 0x01),
72 QMP_PHY_INIT_CFG(QSERDES_V3_COM_HSCLK_SEL, 0x80),
73 QMP_PHY_INIT_CFG(QSERDES_V3_COM_DEC_START_MODE0, 0x82),
74 QMP_PHY_INIT_CFG(QSERDES_V3_COM_DIV_FRAC_START1_MODE0, 0xab),
75 QMP_PHY_INIT_CFG(QSERDES_V3_COM_DIV_FRAC_START2_MODE0, 0xea),
76 QMP_PHY_INIT_CFG(QSERDES_V3_COM_DIV_FRAC_START3_MODE0, 0x02),
77 QMP_PHY_INIT_CFG(QSERDES_V3_COM_CP_CTRL_MODE0, 0x06),
78 QMP_PHY_INIT_CFG(QSERDES_V3_COM_PLL_RCTRL_MODE0, 0x16),
79 QMP_PHY_INIT_CFG(QSERDES_V3_COM_PLL_CCTRL_MODE0, 0x36),
80 QMP_PHY_INIT_CFG(QSERDES_V3_COM_INTEGLOOP_GAIN1_MODE0, 0x00),
81 QMP_PHY_INIT_CFG(QSERDES_V3_COM_INTEGLOOP_GAIN0_MODE0, 0x3f),
82 QMP_PHY_INIT_CFG(QSERDES_V3_COM_VCO_TUNE2_MODE0, 0x01),
83 QMP_PHY_INIT_CFG(QSERDES_V3_COM_VCO_TUNE1_MODE0, 0xc9),
84 QMP_PHY_INIT_CFG(QSERDES_V3_COM_CORECLK_DIV_MODE0, 0x0a),
85 QMP_PHY_INIT_CFG(QSERDES_V3_COM_LOCK_CMP3_MODE0, 0x00),
86 QMP_PHY_INIT_CFG(QSERDES_V3_COM_LOCK_CMP2_MODE0, 0x34),
87 QMP_PHY_INIT_CFG(QSERDES_V3_COM_LOCK_CMP1_MODE0, 0x15),
88 QMP_PHY_INIT_CFG(QSERDES_V3_COM_LOCK_CMP_EN, 0x04),
89 QMP_PHY_INIT_CFG(QSERDES_V3_COM_CORE_CLK_EN, 0x00),
90 QMP_PHY_INIT_CFG(QSERDES_V3_COM_LOCK_CMP_CFG, 0x00),
91 QMP_PHY_INIT_CFG(QSERDES_V3_COM_VCO_TUNE_MAP, 0x00),
92 QMP_PHY_INIT_CFG(QSERDES_V3_COM_BG_TIMER, 0x0a),
93 QMP_PHY_INIT_CFG(QSERDES_V3_COM_PLL_IVCO, 0x07),
94 QMP_PHY_INIT_CFG(QSERDES_V3_COM_INTEGLOOP_INITVAL, 0x80),
95 QMP_PHY_INIT_CFG(QSERDES_V3_COM_CMN_MODE, 0x01),
96 QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_EN_CENTER, 0x01),
97 QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_PER1, 0x31),
98 QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_PER2, 0x01),
99 QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_ADJ_PER1, 0x00),
100 QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_ADJ_PER2, 0x00),
101 QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_STEP_SIZE1, 0x85),
102 QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_STEP_SIZE2, 0x07),
105 static const struct qmp_phy_init_tbl msm8998_usb3_tx_tbl[] = {
106 QMP_PHY_INIT_CFG(QSERDES_V3_TX_HIGHZ_DRVR_EN, 0x10),
107 QMP_PHY_INIT_CFG(QSERDES_V3_TX_RCV_DETECT_LVL_2, 0x12),
108 QMP_PHY_INIT_CFG(QSERDES_V3_TX_LANE_MODE_1, 0x16),
109 QMP_PHY_INIT_CFG(QSERDES_V3_TX_RES_CODE_LANE_OFFSET_TX, 0x00),
112 static const struct qmp_phy_init_tbl msm8998_usb3_rx_tbl[] = {
113 QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_FASTLOCK_FO_GAIN, 0x0b),
114 QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_EQU_ADAPTOR_CNTRL2, 0x0f),
115 QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_EQU_ADAPTOR_CNTRL3, 0x4e),
116 QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_EQU_ADAPTOR_CNTRL4, 0x18),
117 QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_EQ_OFFSET_ADAPTOR_CNTRL1, 0x07),
118 QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_OFFSET_ADAPTOR_CNTRL2, 0x80),
119 QMP_PHY_INIT_CFG(QSERDES_V3_RX_SIGDET_CNTRL, 0x43),
120 QMP_PHY_INIT_CFG(QSERDES_V3_RX_SIGDET_DEGLITCH_CNTRL, 0x1c),
121 QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_SO_SATURATION_AND_ENABLE, 0x75),
122 QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_FASTLOCK_COUNT_LOW, 0x00),
123 QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_FASTLOCK_COUNT_HIGH, 0x00),
124 QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_PI_CONTROLS, 0x80),
125 QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_FO_GAIN, 0x0a),
126 QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_SO_GAIN, 0x06),
127 QMP_PHY_INIT_CFG(QSERDES_V3_RX_SIGDET_ENABLES, 0x00),
128 QMP_PHY_INIT_CFG(QSERDES_V3_RX_VGA_CAL_CNTRL2, 0x03),
129 QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_MODE_00, 0x05),
132 static const struct qmp_phy_init_tbl msm8998_usb3_pcs_tbl[] = {
133 QMP_PHY_INIT_CFG(QPHY_V3_PCS_FLL_CNTRL2, 0x83),
134 QMP_PHY_INIT_CFG(QPHY_V3_PCS_FLL_CNT_VAL_L, 0x09),
135 QMP_PHY_INIT_CFG(QPHY_V3_PCS_FLL_CNT_VAL_H_TOL, 0xa2),
136 QMP_PHY_INIT_CFG(QPHY_V3_PCS_FLL_MAN_CODE, 0x40),
137 QMP_PHY_INIT_CFG(QPHY_V3_PCS_FLL_CNTRL1, 0x02),
138 QMP_PHY_INIT_CFG(QPHY_V3_PCS_LOCK_DETECT_CONFIG1, 0xd1),
139 QMP_PHY_INIT_CFG(QPHY_V3_PCS_LOCK_DETECT_CONFIG2, 0x1f),
140 QMP_PHY_INIT_CFG(QPHY_V3_PCS_LOCK_DETECT_CONFIG3, 0x47),
141 QMP_PHY_INIT_CFG(QPHY_V3_PCS_POWER_STATE_CONFIG2, 0x1b),
142 QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXMGN_V0, 0x9f),
143 QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXMGN_V1, 0x9f),
144 QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXMGN_V2, 0xb7),
145 QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXMGN_V3, 0x4e),
146 QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXMGN_V4, 0x65),
147 QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXMGN_LS, 0x6b),
148 QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M6DB_V0, 0x15),
149 QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M3P5DB_V0, 0x0d),
150 QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M6DB_V1, 0x15),
151 QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M3P5DB_V1, 0x0d),
152 QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M6DB_V2, 0x15),
153 QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M3P5DB_V2, 0x0d),
154 QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M6DB_V3, 0x15),
155 QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M3P5DB_V3, 0x0d),
156 QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M6DB_V4, 0x15),
157 QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M3P5DB_V4, 0x0d),
158 QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M6DB_LS, 0x15),
159 QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M3P5DB_LS, 0x0d),
160 QMP_PHY_INIT_CFG(QPHY_V3_PCS_RATE_SLEW_CNTRL, 0x02),
161 QMP_PHY_INIT_CFG(QPHY_V3_PCS_PWRUP_RESET_DLY_TIME_AUXCLK, 0x04),
162 QMP_PHY_INIT_CFG(QPHY_V3_PCS_TSYNC_RSYNC_TIME, 0x44),
163 QMP_PHY_INIT_CFG(QPHY_V3_PCS_RCVR_DTCT_DLY_P1U2_L, 0xe7),
164 QMP_PHY_INIT_CFG(QPHY_V3_PCS_RCVR_DTCT_DLY_P1U2_H, 0x03),
165 QMP_PHY_INIT_CFG(QPHY_V3_PCS_RCVR_DTCT_DLY_U3_L, 0x40),
166 QMP_PHY_INIT_CFG(QPHY_V3_PCS_RCVR_DTCT_DLY_U3_H, 0x00),
167 QMP_PHY_INIT_CFG(QPHY_V3_PCS_RX_SIGDET_LVL, 0x8a),
168 QMP_PHY_INIT_CFG(QPHY_V3_PCS_RXEQTRAINING_WAIT_TIME, 0x75),
169 QMP_PHY_INIT_CFG(QPHY_V3_PCS_LFPS_TX_ECSTART_EQTLOCK, 0x86),
170 QMP_PHY_INIT_CFG(QPHY_V3_PCS_RXEQTRAINING_RUN_TIME, 0x13),
173 static const struct qmp_phy_init_tbl qcm2290_usb3_serdes_tbl[] = {
174 QMP_PHY_INIT_CFG(QSERDES_COM_SYSCLK_EN_SEL, 0x14),
175 QMP_PHY_INIT_CFG(QSERDES_COM_BIAS_EN_CLKBUFLR_EN, 0x08),
176 QMP_PHY_INIT_CFG(QSERDES_COM_CLK_SELECT, 0x30),
177 QMP_PHY_INIT_CFG(QSERDES_COM_SYS_CLK_CTRL, 0x06),
178 QMP_PHY_INIT_CFG(QSERDES_COM_RESETSM_CNTRL, 0x00),
179 QMP_PHY_INIT_CFG(QSERDES_COM_RESETSM_CNTRL2, 0x08),
180 QMP_PHY_INIT_CFG(QSERDES_COM_BG_TRIM, 0x0f),
181 QMP_PHY_INIT_CFG(QSERDES_COM_SVS_MODE_CLK_SEL, 0x01),
182 QMP_PHY_INIT_CFG(QSERDES_COM_HSCLK_SEL, 0x00),
183 QMP_PHY_INIT_CFG(QSERDES_COM_DEC_START_MODE0, 0x82),
184 QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START1_MODE0, 0x55),
185 QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START2_MODE0, 0x55),
186 QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START3_MODE0, 0x03),
187 QMP_PHY_INIT_CFG(QSERDES_COM_CP_CTRL_MODE0, 0x0b),
188 QMP_PHY_INIT_CFG(QSERDES_COM_PLL_RCTRL_MODE0, 0x16),
189 QMP_PHY_INIT_CFG(QSERDES_COM_PLL_CCTRL_MODE0, 0x28),
190 QMP_PHY_INIT_CFG(QSERDES_COM_INTEGLOOP_GAIN0_MODE0, 0x80),
191 QMP_PHY_INIT_CFG(QSERDES_COM_INTEGLOOP_GAIN1_MODE0, 0x00),
192 QMP_PHY_INIT_CFG(QSERDES_COM_CORECLK_DIV, 0x0a),
193 QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP1_MODE0, 0x15),
194 QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP2_MODE0, 0x34),
195 QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP3_MODE0, 0x00),
196 QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP_EN, 0x00),
197 QMP_PHY_INIT_CFG(QSERDES_COM_CORE_CLK_EN, 0x00),
198 QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP_CFG, 0x00),
199 QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE_MAP, 0x00),
200 QMP_PHY_INIT_CFG(QSERDES_COM_BG_TIMER, 0x0a),
201 QMP_PHY_INIT_CFG(QSERDES_COM_SSC_EN_CENTER, 0x01),
202 QMP_PHY_INIT_CFG(QSERDES_COM_SSC_PER1, 0x31),
203 QMP_PHY_INIT_CFG(QSERDES_COM_SSC_PER2, 0x01),
204 QMP_PHY_INIT_CFG(QSERDES_COM_SSC_ADJ_PER1, 0x00),
205 QMP_PHY_INIT_CFG(QSERDES_COM_SSC_ADJ_PER2, 0x00),
206 QMP_PHY_INIT_CFG(QSERDES_COM_SSC_STEP_SIZE1, 0xde),
207 QMP_PHY_INIT_CFG(QSERDES_COM_SSC_STEP_SIZE2, 0x07),
208 QMP_PHY_INIT_CFG(QSERDES_COM_PLL_IVCO, 0x0f),
209 QMP_PHY_INIT_CFG(QSERDES_COM_CMN_CONFIG, 0x06),
210 QMP_PHY_INIT_CFG(QSERDES_COM_INTEGLOOP_INITVAL, 0x80),
211 QMP_PHY_INIT_CFG(QSERDES_COM_BIAS_EN_CTRL_BY_PSM, 0x01),
214 static const struct qmp_phy_init_tbl qcm2290_usb3_tx_tbl[] = {
215 QMP_PHY_INIT_CFG(QSERDES_V3_TX_HIGHZ_DRVR_EN, 0x10),
216 QMP_PHY_INIT_CFG(QSERDES_V3_TX_RCV_DETECT_LVL_2, 0x12),
217 QMP_PHY_INIT_CFG(QSERDES_V3_TX_LANE_MODE_1, 0xc6),
218 QMP_PHY_INIT_CFG(QSERDES_V3_TX_RES_CODE_LANE_OFFSET_TX, 0x00),
219 QMP_PHY_INIT_CFG(QSERDES_V3_TX_RES_CODE_LANE_OFFSET_RX, 0x00),
222 static const struct qmp_phy_init_tbl qcm2290_usb3_rx_tbl[] = {
223 QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_FASTLOCK_FO_GAIN, 0x0b),
224 QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_PI_CONTROLS, 0x80),
225 QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_FASTLOCK_COUNT_LOW, 0x00),
226 QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_FASTLOCK_COUNT_HIGH, 0x00),
227 QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_FO_GAIN, 0x0a),
228 QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_SO_GAIN, 0x06),
229 QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_SO_SATURATION_AND_ENABLE, 0x75),
230 QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_EQU_ADAPTOR_CNTRL2, 0x02),
231 QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_EQU_ADAPTOR_CNTRL3, 0x4e),
232 QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_EQU_ADAPTOR_CNTRL4, 0x18),
233 QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_EQ_OFFSET_ADAPTOR_CNTRL1, 0x77),
234 QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_OFFSET_ADAPTOR_CNTRL2, 0x80),
235 QMP_PHY_INIT_CFG(QSERDES_V3_RX_VGA_CAL_CNTRL2, 0x0a),
236 QMP_PHY_INIT_CFG(QSERDES_V3_RX_SIGDET_CNTRL, 0x03),
237 QMP_PHY_INIT_CFG(QSERDES_V3_RX_SIGDET_DEGLITCH_CNTRL, 0x16),
238 QMP_PHY_INIT_CFG(QSERDES_V3_RX_SIGDET_ENABLES, 0x00),
239 QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_MODE_00, 0x00),
242 /* the only difference is QSERDES_V3_RX_UCDR_PI_CONTROLS */
243 static const struct qmp_phy_init_tbl sdm660_usb3_rx_tbl[] = {
244 QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_FASTLOCK_FO_GAIN, 0x0b),
245 QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_PI_CONTROLS, 0x00),
246 QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_FASTLOCK_COUNT_LOW, 0x00),
247 QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_FASTLOCK_COUNT_HIGH, 0x00),
248 QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_FO_GAIN, 0x0a),
249 QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_SO_GAIN, 0x06),
250 QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_SO_SATURATION_AND_ENABLE, 0x75),
251 QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_EQU_ADAPTOR_CNTRL2, 0x02),
252 QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_EQU_ADAPTOR_CNTRL3, 0x4e),
253 QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_EQU_ADAPTOR_CNTRL4, 0x18),
254 QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_EQ_OFFSET_ADAPTOR_CNTRL1, 0x77),
255 QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_OFFSET_ADAPTOR_CNTRL2, 0x80),
256 QMP_PHY_INIT_CFG(QSERDES_V3_RX_VGA_CAL_CNTRL2, 0x0a),
257 QMP_PHY_INIT_CFG(QSERDES_V3_RX_SIGDET_CNTRL, 0x03),
258 QMP_PHY_INIT_CFG(QSERDES_V3_RX_SIGDET_DEGLITCH_CNTRL, 0x16),
259 QMP_PHY_INIT_CFG(QSERDES_V3_RX_SIGDET_ENABLES, 0x00),
260 QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_MODE_00, 0x00),
263 static const struct qmp_phy_init_tbl qcm2290_usb3_pcs_tbl[] = {
264 QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXMGN_V0, 0x9f),
265 QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M6DB_V0, 0x17),
266 QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M3P5DB_V0, 0x0f),
267 QMP_PHY_INIT_CFG(QPHY_V3_PCS_FLL_CNTRL2, 0x83),
268 QMP_PHY_INIT_CFG(QPHY_V3_PCS_FLL_CNTRL1, 0x02),
269 QMP_PHY_INIT_CFG(QPHY_V3_PCS_FLL_CNT_VAL_L, 0x09),
270 QMP_PHY_INIT_CFG(QPHY_V3_PCS_FLL_CNT_VAL_H_TOL, 0xa2),
271 QMP_PHY_INIT_CFG(QPHY_V3_PCS_FLL_MAN_CODE, 0x85),
272 QMP_PHY_INIT_CFG(QPHY_V3_PCS_LOCK_DETECT_CONFIG1, 0xd1),
273 QMP_PHY_INIT_CFG(QPHY_V3_PCS_LOCK_DETECT_CONFIG2, 0x1f),
274 QMP_PHY_INIT_CFG(QPHY_V3_PCS_LOCK_DETECT_CONFIG3, 0x47),
275 QMP_PHY_INIT_CFG(QPHY_V3_PCS_RXEQTRAINING_WAIT_TIME, 0x75),
276 QMP_PHY_INIT_CFG(QPHY_V3_PCS_RXEQTRAINING_RUN_TIME, 0x13),
277 QMP_PHY_INIT_CFG(QPHY_V3_PCS_LFPS_TX_ECSTART_EQTLOCK, 0x86),
278 QMP_PHY_INIT_CFG(QPHY_V3_PCS_PWRUP_RESET_DLY_TIME_AUXCLK, 0x04),
279 QMP_PHY_INIT_CFG(QPHY_V3_PCS_TSYNC_RSYNC_TIME, 0x44),
280 QMP_PHY_INIT_CFG(QPHY_V3_PCS_RCVR_DTCT_DLY_P1U2_L, 0xe7),
281 QMP_PHY_INIT_CFG(QPHY_V3_PCS_RCVR_DTCT_DLY_P1U2_H, 0x03),
282 QMP_PHY_INIT_CFG(QPHY_V3_PCS_RCVR_DTCT_DLY_U3_L, 0x40),
283 QMP_PHY_INIT_CFG(QPHY_V3_PCS_RCVR_DTCT_DLY_U3_H, 0x00),
284 QMP_PHY_INIT_CFG(QPHY_V3_PCS_RX_SIGDET_LVL, 0x88),
287 struct qmp_usbc_offsets {
293 /* for PHYs with >= 2 lanes */
298 /* struct qmp_phy_cfg - per-PHY initialization config */
300 const struct qmp_usbc_offsets *offsets;
302 /* Init sequence for PHY blocks - serdes, tx, rx, pcs */
303 const struct qmp_phy_init_tbl *serdes_tbl;
305 const struct qmp_phy_init_tbl *tx_tbl;
307 const struct qmp_phy_init_tbl *rx_tbl;
309 const struct qmp_phy_init_tbl *pcs_tbl;
312 /* regulators to be requested */
313 const char * const *vreg_list;
316 /* array of registers with different offsets */
317 const unsigned int *regs;
323 const struct qmp_phy_cfg *cfg;
325 void __iomem *serdes;
327 void __iomem *pcs_misc;
333 struct regmap *tcsr_map;
336 struct clk *pipe_clk;
337 struct clk_bulk_data *clks;
340 struct reset_control_bulk_data *resets;
341 struct regulator_bulk_data *vregs;
343 struct mutex phy_mutex;
346 unsigned int usb_init_count;
350 struct clk_fixed_rate pipe_clk_fixed;
352 struct typec_switch_dev *sw;
353 enum typec_orientation orientation;
356 static inline void qphy_setbits(void __iomem *base, u32 offset, u32 val)
360 reg = readl(base + offset);
362 writel(reg, base + offset);
364 /* ensure that above write is through */
365 readl(base + offset);
368 static inline void qphy_clrbits(void __iomem *base, u32 offset, u32 val)
372 reg = readl(base + offset);
374 writel(reg, base + offset);
376 /* ensure that above write is through */
377 readl(base + offset);
380 /* list of clocks required by phy */
381 static const char * const qmp_usbc_phy_clk_l[] = {
382 "aux", "cfg_ahb", "ref", "com_aux",
386 static const char * const usb3phy_legacy_reset_l[] = {
390 static const char * const usb3phy_reset_l[] = {
394 /* list of regulators */
395 static const char * const qmp_phy_vreg_l[] = {
396 "vdda-phy", "vdda-pll",
399 static const struct qmp_usbc_offsets qmp_usbc_offsets_v3_qcm2290 = {
409 static const struct qmp_phy_cfg msm8998_usb3phy_cfg = {
410 .offsets = &qmp_usbc_offsets_v3_qcm2290,
412 .serdes_tbl = msm8998_usb3_serdes_tbl,
413 .serdes_tbl_num = ARRAY_SIZE(msm8998_usb3_serdes_tbl),
414 .tx_tbl = msm8998_usb3_tx_tbl,
415 .tx_tbl_num = ARRAY_SIZE(msm8998_usb3_tx_tbl),
416 .rx_tbl = msm8998_usb3_rx_tbl,
417 .rx_tbl_num = ARRAY_SIZE(msm8998_usb3_rx_tbl),
418 .pcs_tbl = msm8998_usb3_pcs_tbl,
419 .pcs_tbl_num = ARRAY_SIZE(msm8998_usb3_pcs_tbl),
420 .vreg_list = qmp_phy_vreg_l,
421 .num_vregs = ARRAY_SIZE(qmp_phy_vreg_l),
422 .regs = qmp_v3_usb3phy_regs_layout,
425 static const struct qmp_phy_cfg qcm2290_usb3phy_cfg = {
426 .offsets = &qmp_usbc_offsets_v3_qcm2290,
428 .serdes_tbl = qcm2290_usb3_serdes_tbl,
429 .serdes_tbl_num = ARRAY_SIZE(qcm2290_usb3_serdes_tbl),
430 .tx_tbl = qcm2290_usb3_tx_tbl,
431 .tx_tbl_num = ARRAY_SIZE(qcm2290_usb3_tx_tbl),
432 .rx_tbl = qcm2290_usb3_rx_tbl,
433 .rx_tbl_num = ARRAY_SIZE(qcm2290_usb3_rx_tbl),
434 .pcs_tbl = qcm2290_usb3_pcs_tbl,
435 .pcs_tbl_num = ARRAY_SIZE(qcm2290_usb3_pcs_tbl),
436 .vreg_list = qmp_phy_vreg_l,
437 .num_vregs = ARRAY_SIZE(qmp_phy_vreg_l),
438 .regs = qmp_v3_usb3phy_regs_layout_qcm2290,
441 static const struct qmp_phy_cfg sdm660_usb3phy_cfg = {
442 .offsets = &qmp_usbc_offsets_v3_qcm2290,
444 .serdes_tbl = qcm2290_usb3_serdes_tbl,
445 .serdes_tbl_num = ARRAY_SIZE(qcm2290_usb3_serdes_tbl),
446 .tx_tbl = qcm2290_usb3_tx_tbl,
447 .tx_tbl_num = ARRAY_SIZE(qcm2290_usb3_tx_tbl),
448 .rx_tbl = sdm660_usb3_rx_tbl,
449 .rx_tbl_num = ARRAY_SIZE(sdm660_usb3_rx_tbl),
450 .pcs_tbl = qcm2290_usb3_pcs_tbl,
451 .pcs_tbl_num = ARRAY_SIZE(qcm2290_usb3_pcs_tbl),
452 .vreg_list = qmp_phy_vreg_l,
453 .num_vregs = ARRAY_SIZE(qmp_phy_vreg_l),
454 .regs = qmp_v3_usb3phy_regs_layout_qcm2290,
457 static int qmp_usbc_init(struct phy *phy)
459 struct qmp_usbc *qmp = phy_get_drvdata(phy);
460 const struct qmp_phy_cfg *cfg = qmp->cfg;
461 void __iomem *pcs = qmp->pcs;
465 ret = regulator_bulk_enable(cfg->num_vregs, qmp->vregs);
467 dev_err(qmp->dev, "failed to enable regulators, err=%d\n", ret);
471 ret = reset_control_bulk_assert(qmp->num_resets, qmp->resets);
473 dev_err(qmp->dev, "reset assert failed\n");
474 goto err_disable_regulators;
477 ret = reset_control_bulk_deassert(qmp->num_resets, qmp->resets);
479 dev_err(qmp->dev, "reset deassert failed\n");
480 goto err_disable_regulators;
483 ret = clk_bulk_prepare_enable(qmp->num_clks, qmp->clks);
485 goto err_assert_reset;
487 qphy_setbits(pcs, cfg->regs[QPHY_PCS_POWER_DOWN_CONTROL], SW_PWRDN);
489 #define SW_PORTSELECT_VAL BIT(0)
490 #define SW_PORTSELECT_MUX BIT(1)
491 /* Use software based port select and switch on typec orientation */
492 val = SW_PORTSELECT_MUX;
493 if (qmp->orientation == TYPEC_ORIENTATION_REVERSE)
494 val |= SW_PORTSELECT_VAL;
495 writel(val, qmp->pcs_misc);
500 reset_control_bulk_assert(qmp->num_resets, qmp->resets);
501 err_disable_regulators:
502 regulator_bulk_disable(cfg->num_vregs, qmp->vregs);
507 static int qmp_usbc_exit(struct phy *phy)
509 struct qmp_usbc *qmp = phy_get_drvdata(phy);
510 const struct qmp_phy_cfg *cfg = qmp->cfg;
512 reset_control_bulk_assert(qmp->num_resets, qmp->resets);
514 clk_bulk_disable_unprepare(qmp->num_clks, qmp->clks);
516 regulator_bulk_disable(cfg->num_vregs, qmp->vregs);
521 static int qmp_usbc_power_on(struct phy *phy)
523 struct qmp_usbc *qmp = phy_get_drvdata(phy);
524 const struct qmp_phy_cfg *cfg = qmp->cfg;
525 void __iomem *status;
529 qmp_configure(qmp->serdes, cfg->serdes_tbl, cfg->serdes_tbl_num);
531 ret = clk_prepare_enable(qmp->pipe_clk);
533 dev_err(qmp->dev, "pipe_clk enable failed err=%d\n", ret);
537 /* Tx, Rx, and PCS configurations */
538 qmp_configure_lane(qmp->tx, cfg->tx_tbl, cfg->tx_tbl_num, 1);
539 qmp_configure_lane(qmp->rx, cfg->rx_tbl, cfg->rx_tbl_num, 1);
541 qmp_configure_lane(qmp->tx2, cfg->tx_tbl, cfg->tx_tbl_num, 2);
542 qmp_configure_lane(qmp->rx2, cfg->rx_tbl, cfg->rx_tbl_num, 2);
544 qmp_configure(qmp->pcs, cfg->pcs_tbl, cfg->pcs_tbl_num);
546 /* Pull PHY out of reset state */
547 qphy_clrbits(qmp->pcs, cfg->regs[QPHY_SW_RESET], SW_RESET);
549 /* start SerDes and Phy-Coding-Sublayer */
550 qphy_setbits(qmp->pcs, cfg->regs[QPHY_START_CTRL], SERDES_START | PCS_START);
552 status = qmp->pcs + cfg->regs[QPHY_PCS_STATUS];
553 ret = readl_poll_timeout(status, val, !(val & PHYSTATUS), 200,
554 PHY_INIT_COMPLETE_TIMEOUT);
556 dev_err(qmp->dev, "phy initialization timed-out\n");
557 goto err_disable_pipe_clk;
562 err_disable_pipe_clk:
563 clk_disable_unprepare(qmp->pipe_clk);
568 static int qmp_usbc_power_off(struct phy *phy)
570 struct qmp_usbc *qmp = phy_get_drvdata(phy);
571 const struct qmp_phy_cfg *cfg = qmp->cfg;
573 clk_disable_unprepare(qmp->pipe_clk);
576 qphy_setbits(qmp->pcs, cfg->regs[QPHY_SW_RESET], SW_RESET);
578 /* stop SerDes and Phy-Coding-Sublayer */
579 qphy_clrbits(qmp->pcs, cfg->regs[QPHY_START_CTRL],
580 SERDES_START | PCS_START);
582 /* Put PHY into POWER DOWN state: active low */
583 qphy_clrbits(qmp->pcs, cfg->regs[QPHY_PCS_POWER_DOWN_CONTROL],
589 static int qmp_usbc_enable(struct phy *phy)
591 struct qmp_usbc *qmp = phy_get_drvdata(phy);
594 mutex_lock(&qmp->phy_mutex);
596 ret = qmp_usbc_init(phy);
600 ret = qmp_usbc_power_on(phy);
606 qmp->usb_init_count++;
608 mutex_unlock(&qmp->phy_mutex);
613 static int qmp_usbc_disable(struct phy *phy)
615 struct qmp_usbc *qmp = phy_get_drvdata(phy);
618 qmp->usb_init_count--;
619 ret = qmp_usbc_power_off(phy);
622 return qmp_usbc_exit(phy);
625 static int qmp_usbc_set_mode(struct phy *phy, enum phy_mode mode, int submode)
627 struct qmp_usbc *qmp = phy_get_drvdata(phy);
634 static const struct phy_ops qmp_usbc_phy_ops = {
635 .init = qmp_usbc_enable,
636 .exit = qmp_usbc_disable,
637 .set_mode = qmp_usbc_set_mode,
638 .owner = THIS_MODULE,
641 static void qmp_usbc_enable_autonomous_mode(struct qmp_usbc *qmp)
643 const struct qmp_phy_cfg *cfg = qmp->cfg;
644 void __iomem *pcs = qmp->pcs;
647 if (qmp->mode == PHY_MODE_USB_HOST_SS ||
648 qmp->mode == PHY_MODE_USB_DEVICE_SS)
649 intr_mask = ARCVR_DTCT_EN | ALFPS_DTCT_EN;
651 intr_mask = ARCVR_DTCT_EN | ARCVR_DTCT_EVENT_SEL;
653 /* Clear any pending interrupts status */
654 qphy_setbits(pcs, cfg->regs[QPHY_PCS_LFPS_RXTERM_IRQ_CLEAR], IRQ_CLEAR);
655 /* Writing 1 followed by 0 clears the interrupt */
656 qphy_clrbits(pcs, cfg->regs[QPHY_PCS_LFPS_RXTERM_IRQ_CLEAR], IRQ_CLEAR);
658 qphy_clrbits(pcs, cfg->regs[QPHY_PCS_AUTONOMOUS_MODE_CTRL],
659 ARCVR_DTCT_EN | ALFPS_DTCT_EN | ARCVR_DTCT_EVENT_SEL);
661 /* Enable required PHY autonomous mode interrupts */
662 qphy_setbits(pcs, cfg->regs[QPHY_PCS_AUTONOMOUS_MODE_CTRL], intr_mask);
664 /* Enable i/o clamp_n for autonomous mode */
665 if (qmp->tcsr_map && qmp->vls_clamp_reg)
666 regmap_write(qmp->tcsr_map, qmp->vls_clamp_reg, 1);
669 static void qmp_usbc_disable_autonomous_mode(struct qmp_usbc *qmp)
671 const struct qmp_phy_cfg *cfg = qmp->cfg;
672 void __iomem *pcs = qmp->pcs;
674 /* Disable i/o clamp_n on resume for normal mode */
675 if (qmp->tcsr_map && qmp->vls_clamp_reg)
676 regmap_write(qmp->tcsr_map, qmp->vls_clamp_reg, 0);
678 qphy_clrbits(pcs, cfg->regs[QPHY_PCS_AUTONOMOUS_MODE_CTRL],
679 ARCVR_DTCT_EN | ARCVR_DTCT_EVENT_SEL | ALFPS_DTCT_EN);
681 qphy_setbits(pcs, cfg->regs[QPHY_PCS_LFPS_RXTERM_IRQ_CLEAR], IRQ_CLEAR);
682 /* Writing 1 followed by 0 clears the interrupt */
683 qphy_clrbits(pcs, cfg->regs[QPHY_PCS_LFPS_RXTERM_IRQ_CLEAR], IRQ_CLEAR);
686 static int __maybe_unused qmp_usbc_runtime_suspend(struct device *dev)
688 struct qmp_usbc *qmp = dev_get_drvdata(dev);
690 dev_vdbg(dev, "Suspending QMP phy, mode:%d\n", qmp->mode);
692 if (!qmp->phy->init_count) {
693 dev_vdbg(dev, "PHY not initialized, bailing out\n");
697 qmp_usbc_enable_autonomous_mode(qmp);
699 clk_disable_unprepare(qmp->pipe_clk);
700 clk_bulk_disable_unprepare(qmp->num_clks, qmp->clks);
705 static int __maybe_unused qmp_usbc_runtime_resume(struct device *dev)
707 struct qmp_usbc *qmp = dev_get_drvdata(dev);
710 dev_vdbg(dev, "Resuming QMP phy, mode:%d\n", qmp->mode);
712 if (!qmp->phy->init_count) {
713 dev_vdbg(dev, "PHY not initialized, bailing out\n");
717 ret = clk_bulk_prepare_enable(qmp->num_clks, qmp->clks);
721 ret = clk_prepare_enable(qmp->pipe_clk);
723 dev_err(dev, "pipe_clk enable failed, err=%d\n", ret);
724 clk_bulk_disable_unprepare(qmp->num_clks, qmp->clks);
728 qmp_usbc_disable_autonomous_mode(qmp);
733 static const struct dev_pm_ops qmp_usbc_pm_ops = {
734 SET_RUNTIME_PM_OPS(qmp_usbc_runtime_suspend,
735 qmp_usbc_runtime_resume, NULL)
738 static int qmp_usbc_vreg_init(struct qmp_usbc *qmp)
740 const struct qmp_phy_cfg *cfg = qmp->cfg;
741 struct device *dev = qmp->dev;
742 int num = cfg->num_vregs;
745 qmp->vregs = devm_kcalloc(dev, num, sizeof(*qmp->vregs), GFP_KERNEL);
749 for (i = 0; i < num; i++)
750 qmp->vregs[i].supply = cfg->vreg_list[i];
752 return devm_regulator_bulk_get(dev, num, qmp->vregs);
755 static int qmp_usbc_reset_init(struct qmp_usbc *qmp,
756 const char *const *reset_list,
759 struct device *dev = qmp->dev;
763 qmp->resets = devm_kcalloc(dev, num_resets,
764 sizeof(*qmp->resets), GFP_KERNEL);
768 for (i = 0; i < num_resets; i++)
769 qmp->resets[i].id = reset_list[i];
771 qmp->num_resets = num_resets;
773 ret = devm_reset_control_bulk_get_exclusive(dev, num_resets, qmp->resets);
775 return dev_err_probe(dev, ret, "failed to get resets\n");
780 static int qmp_usbc_clk_init(struct qmp_usbc *qmp)
782 struct device *dev = qmp->dev;
783 int num = ARRAY_SIZE(qmp_usbc_phy_clk_l);
786 qmp->clks = devm_kcalloc(dev, num, sizeof(*qmp->clks), GFP_KERNEL);
790 for (i = 0; i < num; i++)
791 qmp->clks[i].id = qmp_usbc_phy_clk_l[i];
795 return devm_clk_bulk_get_optional(dev, num, qmp->clks);
798 static void phy_clk_release_provider(void *res)
800 of_clk_del_provider(res);
804 * Register a fixed rate pipe clock.
806 * The <s>_pipe_clksrc generated by PHY goes to the GCC that gate
807 * controls it. The <s>_pipe_clk coming out of the GCC is requested
808 * by the PHY driver for its operations.
809 * We register the <s>_pipe_clksrc here. The gcc driver takes care
810 * of assigning this <s>_pipe_clksrc as parent to <s>_pipe_clk.
811 * Below picture shows this relationship.
814 * | PHY block |<<---------------------------------------+
816 * | +-------+ | +-----+ |
817 * I/P---^-->| PLL |---^--->pipe_clksrc--->| GCC |--->pipe_clk---+
818 * clk | +-------+ | +-----+
821 static int phy_pipe_clk_register(struct qmp_usbc *qmp, struct device_node *np)
823 struct clk_fixed_rate *fixed = &qmp->pipe_clk_fixed;
824 struct clk_init_data init = { };
827 ret = of_property_read_string(np, "clock-output-names", &init.name);
829 dev_err(qmp->dev, "%pOFn: No clock-output-names\n", np);
833 init.ops = &clk_fixed_rate_ops;
835 /* controllers using QMP phys use 125MHz pipe clock interface */
836 fixed->fixed_rate = 125000000;
837 fixed->hw.init = &init;
839 ret = devm_clk_hw_register(qmp->dev, &fixed->hw);
843 ret = of_clk_add_hw_provider(np, of_clk_hw_simple_get, &fixed->hw);
848 * Roll a devm action because the clock provider is the child node, but
849 * the child node is not actually a device.
851 return devm_add_action_or_reset(qmp->dev, phy_clk_release_provider, np);
854 #if IS_ENABLED(CONFIG_TYPEC)
855 static int qmp_usbc_typec_switch_set(struct typec_switch_dev *sw,
856 enum typec_orientation orientation)
858 struct qmp_usbc *qmp = typec_switch_get_drvdata(sw);
860 if (orientation == qmp->orientation || orientation == TYPEC_ORIENTATION_NONE)
863 mutex_lock(&qmp->phy_mutex);
864 qmp->orientation = orientation;
866 if (qmp->usb_init_count) {
867 qmp_usbc_power_off(qmp->phy);
868 qmp_usbc_exit(qmp->phy);
870 qmp_usbc_init(qmp->phy);
871 qmp_usbc_power_on(qmp->phy);
874 mutex_unlock(&qmp->phy_mutex);
879 static void qmp_usbc_typec_unregister(void *data)
881 struct qmp_usbc *qmp = data;
883 typec_switch_unregister(qmp->sw);
886 static int qmp_usbc_typec_switch_register(struct qmp_usbc *qmp)
888 struct typec_switch_desc sw_desc = {};
889 struct device *dev = qmp->dev;
891 sw_desc.drvdata = qmp;
892 sw_desc.fwnode = dev->fwnode;
893 sw_desc.set = qmp_usbc_typec_switch_set;
894 qmp->sw = typec_switch_register(dev, &sw_desc);
895 if (IS_ERR(qmp->sw)) {
896 dev_err(dev, "Unable to register typec switch: %pe\n", qmp->sw);
897 return PTR_ERR(qmp->sw);
900 return devm_add_action_or_reset(dev, qmp_usbc_typec_unregister, qmp);
903 static int qmp_usbc_typec_switch_register(struct qmp_usbc *qmp)
909 static int qmp_usbc_parse_dt_legacy(struct qmp_usbc *qmp, struct device_node *np)
911 struct platform_device *pdev = to_platform_device(qmp->dev);
912 struct device *dev = qmp->dev;
915 qmp->serdes = devm_platform_ioremap_resource(pdev, 0);
916 if (IS_ERR(qmp->serdes))
917 return PTR_ERR(qmp->serdes);
920 * Get memory resources for the PHY:
921 * Resources are indexed as: tx -> 0; rx -> 1; pcs -> 2.
922 * For dual lane PHYs: tx2 -> 3, rx2 -> 4, pcs_misc (optional) -> 5
923 * For single lane PHYs: pcs_misc (optional) -> 3.
925 qmp->tx = devm_of_iomap(dev, np, 0, NULL);
927 return PTR_ERR(qmp->tx);
929 qmp->rx = devm_of_iomap(dev, np, 1, NULL);
931 return PTR_ERR(qmp->rx);
933 qmp->pcs = devm_of_iomap(dev, np, 2, NULL);
934 if (IS_ERR(qmp->pcs))
935 return PTR_ERR(qmp->pcs);
937 qmp->tx2 = devm_of_iomap(dev, np, 3, NULL);
938 if (IS_ERR(qmp->tx2))
939 return PTR_ERR(qmp->tx2);
941 qmp->rx2 = devm_of_iomap(dev, np, 4, NULL);
942 if (IS_ERR(qmp->rx2))
943 return PTR_ERR(qmp->rx2);
945 qmp->pcs_misc = devm_of_iomap(dev, np, 5, NULL);
946 if (IS_ERR(qmp->pcs_misc)) {
947 dev_vdbg(dev, "PHY pcs_misc-reg not used\n");
948 qmp->pcs_misc = NULL;
951 qmp->pipe_clk = devm_get_clk_from_child(dev, np, NULL);
952 if (IS_ERR(qmp->pipe_clk)) {
953 return dev_err_probe(dev, PTR_ERR(qmp->pipe_clk),
954 "failed to get pipe clock\n");
957 ret = devm_clk_bulk_get_all(qmp->dev, &qmp->clks);
963 ret = qmp_usbc_reset_init(qmp, usb3phy_legacy_reset_l,
964 ARRAY_SIZE(usb3phy_legacy_reset_l));
971 static int qmp_usbc_parse_dt(struct qmp_usbc *qmp)
973 struct platform_device *pdev = to_platform_device(qmp->dev);
974 const struct qmp_phy_cfg *cfg = qmp->cfg;
975 const struct qmp_usbc_offsets *offs = cfg->offsets;
976 struct device *dev = qmp->dev;
983 base = devm_platform_ioremap_resource(pdev, 0);
985 return PTR_ERR(base);
987 qmp->serdes = base + offs->serdes;
988 qmp->pcs = base + offs->pcs;
990 qmp->pcs_misc = base + offs->pcs_misc;
991 qmp->tx = base + offs->tx;
992 qmp->rx = base + offs->rx;
994 qmp->tx2 = base + offs->tx2;
995 qmp->rx2 = base + offs->rx2;
997 ret = qmp_usbc_clk_init(qmp);
1001 qmp->pipe_clk = devm_clk_get(dev, "pipe");
1002 if (IS_ERR(qmp->pipe_clk)) {
1003 return dev_err_probe(dev, PTR_ERR(qmp->pipe_clk),
1004 "failed to get pipe clock\n");
1007 ret = qmp_usbc_reset_init(qmp, usb3phy_reset_l,
1008 ARRAY_SIZE(usb3phy_reset_l));
1015 static int qmp_usbc_parse_vls_clamp(struct qmp_usbc *qmp)
1017 struct of_phandle_args tcsr_args;
1018 struct device *dev = qmp->dev;
1021 /* for backwards compatibility ignore if there is no property */
1022 ret = of_parse_phandle_with_fixed_args(dev->of_node, "qcom,tcsr-reg", 1, 0,
1027 return dev_err_probe(dev, ret, "Failed to parse qcom,tcsr-reg\n");
1029 qmp->tcsr_map = syscon_node_to_regmap(tcsr_args.np);
1030 of_node_put(tcsr_args.np);
1031 if (IS_ERR(qmp->tcsr_map))
1032 return PTR_ERR(qmp->tcsr_map);
1034 qmp->vls_clamp_reg = tcsr_args.args[0];
1039 static int qmp_usbc_probe(struct platform_device *pdev)
1041 struct device *dev = &pdev->dev;
1042 struct phy_provider *phy_provider;
1043 struct device_node *np;
1044 struct qmp_usbc *qmp;
1047 qmp = devm_kzalloc(dev, sizeof(*qmp), GFP_KERNEL);
1053 qmp->orientation = TYPEC_ORIENTATION_NORMAL;
1055 qmp->cfg = of_device_get_match_data(dev);
1059 mutex_init(&qmp->phy_mutex);
1061 ret = qmp_usbc_vreg_init(qmp);
1065 ret = qmp_usbc_typec_switch_register(qmp);
1069 ret = qmp_usbc_parse_vls_clamp(qmp);
1073 /* Check for legacy binding with child node. */
1074 np = of_get_child_by_name(dev->of_node, "phy");
1076 ret = qmp_usbc_parse_dt_legacy(qmp, np);
1078 np = of_node_get(dev->of_node);
1079 ret = qmp_usbc_parse_dt(qmp);
1084 pm_runtime_set_active(dev);
1085 ret = devm_pm_runtime_enable(dev);
1089 * Prevent runtime pm from being ON by default. Users can enable
1090 * it using power/control in sysfs.
1092 pm_runtime_forbid(dev);
1094 ret = phy_pipe_clk_register(qmp, np);
1098 qmp->phy = devm_phy_create(dev, np, &qmp_usbc_phy_ops);
1099 if (IS_ERR(qmp->phy)) {
1100 ret = PTR_ERR(qmp->phy);
1101 dev_err(dev, "failed to create PHY: %d\n", ret);
1105 phy_set_drvdata(qmp->phy, qmp);
1109 phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
1111 return PTR_ERR_OR_ZERO(phy_provider);
1118 static const struct of_device_id qmp_usbc_of_match_table[] = {
1120 .compatible = "qcom,msm8998-qmp-usb3-phy",
1121 .data = &msm8998_usb3phy_cfg,
1123 .compatible = "qcom,qcm2290-qmp-usb3-phy",
1124 .data = &qcm2290_usb3phy_cfg,
1126 .compatible = "qcom,sdm660-qmp-usb3-phy",
1127 .data = &sdm660_usb3phy_cfg,
1129 .compatible = "qcom,sm6115-qmp-usb3-phy",
1130 .data = &qcm2290_usb3phy_cfg,
1134 MODULE_DEVICE_TABLE(of, qmp_usbc_of_match_table);
1136 static struct platform_driver qmp_usbc_driver = {
1137 .probe = qmp_usbc_probe,
1139 .name = "qcom-qmp-usbc-phy",
1140 .pm = &qmp_usbc_pm_ops,
1141 .of_match_table = qmp_usbc_of_match_table,
1145 module_platform_driver(qmp_usbc_driver);
1147 MODULE_AUTHOR("Vivek Gautam <vivek.gautam@codeaurora.org>");
1148 MODULE_DESCRIPTION("Qualcomm QMP USB-C PHY driver");
1149 MODULE_LICENSE("GPL");