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
->dev
, qmp
->serdes
, cfg
->serdes_tbl
,
530 cfg
->serdes_tbl_num
);
532 ret
= clk_prepare_enable(qmp
->pipe_clk
);
534 dev_err(qmp
->dev
, "pipe_clk enable failed err=%d\n", ret
);
538 /* Tx, Rx, and PCS configurations */
539 qmp_configure_lane(qmp
->dev
, qmp
->tx
, cfg
->tx_tbl
, cfg
->tx_tbl_num
, 1);
540 qmp_configure_lane(qmp
->dev
, qmp
->rx
, cfg
->rx_tbl
, cfg
->rx_tbl_num
, 1);
542 qmp_configure_lane(qmp
->dev
, qmp
->tx2
, cfg
->tx_tbl
, cfg
->tx_tbl_num
, 2);
543 qmp_configure_lane(qmp
->dev
, qmp
->rx2
, cfg
->rx_tbl
, cfg
->rx_tbl_num
, 2);
545 qmp_configure(qmp
->dev
, qmp
->pcs
, cfg
->pcs_tbl
, cfg
->pcs_tbl_num
);
547 /* Pull PHY out of reset state */
548 qphy_clrbits(qmp
->pcs
, cfg
->regs
[QPHY_SW_RESET
], SW_RESET
);
550 /* start SerDes and Phy-Coding-Sublayer */
551 qphy_setbits(qmp
->pcs
, cfg
->regs
[QPHY_START_CTRL
], SERDES_START
| PCS_START
);
553 status
= qmp
->pcs
+ cfg
->regs
[QPHY_PCS_STATUS
];
554 ret
= readl_poll_timeout(status
, val
, !(val
& PHYSTATUS
), 200,
555 PHY_INIT_COMPLETE_TIMEOUT
);
557 dev_err(qmp
->dev
, "phy initialization timed-out\n");
558 goto err_disable_pipe_clk
;
563 err_disable_pipe_clk
:
564 clk_disable_unprepare(qmp
->pipe_clk
);
569 static int qmp_usbc_power_off(struct phy
*phy
)
571 struct qmp_usbc
*qmp
= phy_get_drvdata(phy
);
572 const struct qmp_phy_cfg
*cfg
= qmp
->cfg
;
574 clk_disable_unprepare(qmp
->pipe_clk
);
577 qphy_setbits(qmp
->pcs
, cfg
->regs
[QPHY_SW_RESET
], SW_RESET
);
579 /* stop SerDes and Phy-Coding-Sublayer */
580 qphy_clrbits(qmp
->pcs
, cfg
->regs
[QPHY_START_CTRL
],
581 SERDES_START
| PCS_START
);
583 /* Put PHY into POWER DOWN state: active low */
584 qphy_clrbits(qmp
->pcs
, cfg
->regs
[QPHY_PCS_POWER_DOWN_CONTROL
],
590 static int qmp_usbc_enable(struct phy
*phy
)
592 struct qmp_usbc
*qmp
= phy_get_drvdata(phy
);
595 mutex_lock(&qmp
->phy_mutex
);
597 ret
= qmp_usbc_init(phy
);
601 ret
= qmp_usbc_power_on(phy
);
607 qmp
->usb_init_count
++;
609 mutex_unlock(&qmp
->phy_mutex
);
614 static int qmp_usbc_disable(struct phy
*phy
)
616 struct qmp_usbc
*qmp
= phy_get_drvdata(phy
);
619 qmp
->usb_init_count
--;
620 ret
= qmp_usbc_power_off(phy
);
623 return qmp_usbc_exit(phy
);
626 static int qmp_usbc_set_mode(struct phy
*phy
, enum phy_mode mode
, int submode
)
628 struct qmp_usbc
*qmp
= phy_get_drvdata(phy
);
635 static const struct phy_ops qmp_usbc_phy_ops
= {
636 .init
= qmp_usbc_enable
,
637 .exit
= qmp_usbc_disable
,
638 .set_mode
= qmp_usbc_set_mode
,
639 .owner
= THIS_MODULE
,
642 static void qmp_usbc_enable_autonomous_mode(struct qmp_usbc
*qmp
)
644 const struct qmp_phy_cfg
*cfg
= qmp
->cfg
;
645 void __iomem
*pcs
= qmp
->pcs
;
648 if (qmp
->mode
== PHY_MODE_USB_HOST_SS
||
649 qmp
->mode
== PHY_MODE_USB_DEVICE_SS
)
650 intr_mask
= ARCVR_DTCT_EN
| ALFPS_DTCT_EN
;
652 intr_mask
= ARCVR_DTCT_EN
| ARCVR_DTCT_EVENT_SEL
;
654 /* Clear any pending interrupts status */
655 qphy_setbits(pcs
, cfg
->regs
[QPHY_PCS_LFPS_RXTERM_IRQ_CLEAR
], IRQ_CLEAR
);
656 /* Writing 1 followed by 0 clears the interrupt */
657 qphy_clrbits(pcs
, cfg
->regs
[QPHY_PCS_LFPS_RXTERM_IRQ_CLEAR
], IRQ_CLEAR
);
659 qphy_clrbits(pcs
, cfg
->regs
[QPHY_PCS_AUTONOMOUS_MODE_CTRL
],
660 ARCVR_DTCT_EN
| ALFPS_DTCT_EN
| ARCVR_DTCT_EVENT_SEL
);
662 /* Enable required PHY autonomous mode interrupts */
663 qphy_setbits(pcs
, cfg
->regs
[QPHY_PCS_AUTONOMOUS_MODE_CTRL
], intr_mask
);
665 /* Enable i/o clamp_n for autonomous mode */
666 if (qmp
->tcsr_map
&& qmp
->vls_clamp_reg
)
667 regmap_write(qmp
->tcsr_map
, qmp
->vls_clamp_reg
, 1);
670 static void qmp_usbc_disable_autonomous_mode(struct qmp_usbc
*qmp
)
672 const struct qmp_phy_cfg
*cfg
= qmp
->cfg
;
673 void __iomem
*pcs
= qmp
->pcs
;
675 /* Disable i/o clamp_n on resume for normal mode */
676 if (qmp
->tcsr_map
&& qmp
->vls_clamp_reg
)
677 regmap_write(qmp
->tcsr_map
, qmp
->vls_clamp_reg
, 0);
679 qphy_clrbits(pcs
, cfg
->regs
[QPHY_PCS_AUTONOMOUS_MODE_CTRL
],
680 ARCVR_DTCT_EN
| ARCVR_DTCT_EVENT_SEL
| ALFPS_DTCT_EN
);
682 qphy_setbits(pcs
, cfg
->regs
[QPHY_PCS_LFPS_RXTERM_IRQ_CLEAR
], IRQ_CLEAR
);
683 /* Writing 1 followed by 0 clears the interrupt */
684 qphy_clrbits(pcs
, cfg
->regs
[QPHY_PCS_LFPS_RXTERM_IRQ_CLEAR
], IRQ_CLEAR
);
687 static int __maybe_unused
qmp_usbc_runtime_suspend(struct device
*dev
)
689 struct qmp_usbc
*qmp
= dev_get_drvdata(dev
);
691 dev_vdbg(dev
, "Suspending QMP phy, mode:%d\n", qmp
->mode
);
693 if (!qmp
->phy
->init_count
) {
694 dev_vdbg(dev
, "PHY not initialized, bailing out\n");
698 qmp_usbc_enable_autonomous_mode(qmp
);
700 clk_disable_unprepare(qmp
->pipe_clk
);
701 clk_bulk_disable_unprepare(qmp
->num_clks
, qmp
->clks
);
706 static int __maybe_unused
qmp_usbc_runtime_resume(struct device
*dev
)
708 struct qmp_usbc
*qmp
= dev_get_drvdata(dev
);
711 dev_vdbg(dev
, "Resuming QMP phy, mode:%d\n", qmp
->mode
);
713 if (!qmp
->phy
->init_count
) {
714 dev_vdbg(dev
, "PHY not initialized, bailing out\n");
718 ret
= clk_bulk_prepare_enable(qmp
->num_clks
, qmp
->clks
);
722 ret
= clk_prepare_enable(qmp
->pipe_clk
);
724 dev_err(dev
, "pipe_clk enable failed, err=%d\n", ret
);
725 clk_bulk_disable_unprepare(qmp
->num_clks
, qmp
->clks
);
729 qmp_usbc_disable_autonomous_mode(qmp
);
734 static const struct dev_pm_ops qmp_usbc_pm_ops
= {
735 SET_RUNTIME_PM_OPS(qmp_usbc_runtime_suspend
,
736 qmp_usbc_runtime_resume
, NULL
)
739 static int qmp_usbc_vreg_init(struct qmp_usbc
*qmp
)
741 const struct qmp_phy_cfg
*cfg
= qmp
->cfg
;
742 struct device
*dev
= qmp
->dev
;
743 int num
= cfg
->num_vregs
;
746 qmp
->vregs
= devm_kcalloc(dev
, num
, sizeof(*qmp
->vregs
), GFP_KERNEL
);
750 for (i
= 0; i
< num
; i
++)
751 qmp
->vregs
[i
].supply
= cfg
->vreg_list
[i
];
753 return devm_regulator_bulk_get(dev
, num
, qmp
->vregs
);
756 static int qmp_usbc_reset_init(struct qmp_usbc
*qmp
,
757 const char *const *reset_list
,
760 struct device
*dev
= qmp
->dev
;
764 qmp
->resets
= devm_kcalloc(dev
, num_resets
,
765 sizeof(*qmp
->resets
), GFP_KERNEL
);
769 for (i
= 0; i
< num_resets
; i
++)
770 qmp
->resets
[i
].id
= reset_list
[i
];
772 qmp
->num_resets
= num_resets
;
774 ret
= devm_reset_control_bulk_get_exclusive(dev
, num_resets
, qmp
->resets
);
776 return dev_err_probe(dev
, ret
, "failed to get resets\n");
781 static int qmp_usbc_clk_init(struct qmp_usbc
*qmp
)
783 struct device
*dev
= qmp
->dev
;
784 int num
= ARRAY_SIZE(qmp_usbc_phy_clk_l
);
787 qmp
->clks
= devm_kcalloc(dev
, num
, sizeof(*qmp
->clks
), GFP_KERNEL
);
791 for (i
= 0; i
< num
; i
++)
792 qmp
->clks
[i
].id
= qmp_usbc_phy_clk_l
[i
];
796 return devm_clk_bulk_get_optional(dev
, num
, qmp
->clks
);
799 static void phy_clk_release_provider(void *res
)
801 of_clk_del_provider(res
);
805 * Register a fixed rate pipe clock.
807 * The <s>_pipe_clksrc generated by PHY goes to the GCC that gate
808 * controls it. The <s>_pipe_clk coming out of the GCC is requested
809 * by the PHY driver for its operations.
810 * We register the <s>_pipe_clksrc here. The gcc driver takes care
811 * of assigning this <s>_pipe_clksrc as parent to <s>_pipe_clk.
812 * Below picture shows this relationship.
815 * | PHY block |<<---------------------------------------+
817 * | +-------+ | +-----+ |
818 * I/P---^-->| PLL |---^--->pipe_clksrc--->| GCC |--->pipe_clk---+
819 * clk | +-------+ | +-----+
822 static int phy_pipe_clk_register(struct qmp_usbc
*qmp
, struct device_node
*np
)
824 struct clk_fixed_rate
*fixed
= &qmp
->pipe_clk_fixed
;
825 struct clk_init_data init
= { };
828 ret
= of_property_read_string(np
, "clock-output-names", &init
.name
);
830 dev_err(qmp
->dev
, "%pOFn: No clock-output-names\n", np
);
834 init
.ops
= &clk_fixed_rate_ops
;
836 /* controllers using QMP phys use 125MHz pipe clock interface */
837 fixed
->fixed_rate
= 125000000;
838 fixed
->hw
.init
= &init
;
840 ret
= devm_clk_hw_register(qmp
->dev
, &fixed
->hw
);
844 ret
= of_clk_add_hw_provider(np
, of_clk_hw_simple_get
, &fixed
->hw
);
849 * Roll a devm action because the clock provider is the child node, but
850 * the child node is not actually a device.
852 return devm_add_action_or_reset(qmp
->dev
, phy_clk_release_provider
, np
);
855 #if IS_ENABLED(CONFIG_TYPEC)
856 static int qmp_usbc_typec_switch_set(struct typec_switch_dev
*sw
,
857 enum typec_orientation orientation
)
859 struct qmp_usbc
*qmp
= typec_switch_get_drvdata(sw
);
861 if (orientation
== qmp
->orientation
|| orientation
== TYPEC_ORIENTATION_NONE
)
864 mutex_lock(&qmp
->phy_mutex
);
865 qmp
->orientation
= orientation
;
867 if (qmp
->usb_init_count
) {
868 qmp_usbc_power_off(qmp
->phy
);
869 qmp_usbc_exit(qmp
->phy
);
871 qmp_usbc_init(qmp
->phy
);
872 qmp_usbc_power_on(qmp
->phy
);
875 mutex_unlock(&qmp
->phy_mutex
);
880 static void qmp_usbc_typec_unregister(void *data
)
882 struct qmp_usbc
*qmp
= data
;
884 typec_switch_unregister(qmp
->sw
);
887 static int qmp_usbc_typec_switch_register(struct qmp_usbc
*qmp
)
889 struct typec_switch_desc sw_desc
= {};
890 struct device
*dev
= qmp
->dev
;
892 sw_desc
.drvdata
= qmp
;
893 sw_desc
.fwnode
= dev
->fwnode
;
894 sw_desc
.set
= qmp_usbc_typec_switch_set
;
895 qmp
->sw
= typec_switch_register(dev
, &sw_desc
);
896 if (IS_ERR(qmp
->sw
)) {
897 dev_err(dev
, "Unable to register typec switch: %pe\n", qmp
->sw
);
898 return PTR_ERR(qmp
->sw
);
901 return devm_add_action_or_reset(dev
, qmp_usbc_typec_unregister
, qmp
);
904 static int qmp_usbc_typec_switch_register(struct qmp_usbc
*qmp
)
910 static int qmp_usbc_parse_dt_legacy(struct qmp_usbc
*qmp
, struct device_node
*np
)
912 struct platform_device
*pdev
= to_platform_device(qmp
->dev
);
913 struct device
*dev
= qmp
->dev
;
916 qmp
->serdes
= devm_platform_ioremap_resource(pdev
, 0);
917 if (IS_ERR(qmp
->serdes
))
918 return PTR_ERR(qmp
->serdes
);
921 * Get memory resources for the PHY:
922 * Resources are indexed as: tx -> 0; rx -> 1; pcs -> 2.
923 * For dual lane PHYs: tx2 -> 3, rx2 -> 4, pcs_misc (optional) -> 5
924 * For single lane PHYs: pcs_misc (optional) -> 3.
926 qmp
->tx
= devm_of_iomap(dev
, np
, 0, NULL
);
928 return PTR_ERR(qmp
->tx
);
930 qmp
->rx
= devm_of_iomap(dev
, np
, 1, NULL
);
932 return PTR_ERR(qmp
->rx
);
934 qmp
->pcs
= devm_of_iomap(dev
, np
, 2, NULL
);
935 if (IS_ERR(qmp
->pcs
))
936 return PTR_ERR(qmp
->pcs
);
938 qmp
->tx2
= devm_of_iomap(dev
, np
, 3, NULL
);
939 if (IS_ERR(qmp
->tx2
))
940 return PTR_ERR(qmp
->tx2
);
942 qmp
->rx2
= devm_of_iomap(dev
, np
, 4, NULL
);
943 if (IS_ERR(qmp
->rx2
))
944 return PTR_ERR(qmp
->rx2
);
946 qmp
->pcs_misc
= devm_of_iomap(dev
, np
, 5, NULL
);
947 if (IS_ERR(qmp
->pcs_misc
)) {
948 dev_vdbg(dev
, "PHY pcs_misc-reg not used\n");
949 qmp
->pcs_misc
= NULL
;
952 qmp
->pipe_clk
= devm_get_clk_from_child(dev
, np
, NULL
);
953 if (IS_ERR(qmp
->pipe_clk
)) {
954 return dev_err_probe(dev
, PTR_ERR(qmp
->pipe_clk
),
955 "failed to get pipe clock\n");
958 ret
= devm_clk_bulk_get_all(qmp
->dev
, &qmp
->clks
);
964 ret
= qmp_usbc_reset_init(qmp
, usb3phy_legacy_reset_l
,
965 ARRAY_SIZE(usb3phy_legacy_reset_l
));
972 static int qmp_usbc_parse_dt(struct qmp_usbc
*qmp
)
974 struct platform_device
*pdev
= to_platform_device(qmp
->dev
);
975 const struct qmp_phy_cfg
*cfg
= qmp
->cfg
;
976 const struct qmp_usbc_offsets
*offs
= cfg
->offsets
;
977 struct device
*dev
= qmp
->dev
;
984 base
= devm_platform_ioremap_resource(pdev
, 0);
986 return PTR_ERR(base
);
988 qmp
->serdes
= base
+ offs
->serdes
;
989 qmp
->pcs
= base
+ offs
->pcs
;
991 qmp
->pcs_misc
= base
+ offs
->pcs_misc
;
992 qmp
->tx
= base
+ offs
->tx
;
993 qmp
->rx
= base
+ offs
->rx
;
995 qmp
->tx2
= base
+ offs
->tx2
;
996 qmp
->rx2
= base
+ offs
->rx2
;
998 ret
= qmp_usbc_clk_init(qmp
);
1002 qmp
->pipe_clk
= devm_clk_get(dev
, "pipe");
1003 if (IS_ERR(qmp
->pipe_clk
)) {
1004 return dev_err_probe(dev
, PTR_ERR(qmp
->pipe_clk
),
1005 "failed to get pipe clock\n");
1008 ret
= qmp_usbc_reset_init(qmp
, usb3phy_reset_l
,
1009 ARRAY_SIZE(usb3phy_reset_l
));
1016 static int qmp_usbc_parse_vls_clamp(struct qmp_usbc
*qmp
)
1018 struct of_phandle_args tcsr_args
;
1019 struct device
*dev
= qmp
->dev
;
1022 /* for backwards compatibility ignore if there is no property */
1023 ret
= of_parse_phandle_with_fixed_args(dev
->of_node
, "qcom,tcsr-reg", 1, 0,
1028 return dev_err_probe(dev
, ret
, "Failed to parse qcom,tcsr-reg\n");
1030 qmp
->tcsr_map
= syscon_node_to_regmap(tcsr_args
.np
);
1031 of_node_put(tcsr_args
.np
);
1032 if (IS_ERR(qmp
->tcsr_map
))
1033 return PTR_ERR(qmp
->tcsr_map
);
1035 qmp
->vls_clamp_reg
= tcsr_args
.args
[0];
1040 static int qmp_usbc_probe(struct platform_device
*pdev
)
1042 struct device
*dev
= &pdev
->dev
;
1043 struct phy_provider
*phy_provider
;
1044 struct device_node
*np
;
1045 struct qmp_usbc
*qmp
;
1048 qmp
= devm_kzalloc(dev
, sizeof(*qmp
), GFP_KERNEL
);
1053 dev_set_drvdata(dev
, qmp
);
1055 qmp
->orientation
= TYPEC_ORIENTATION_NORMAL
;
1057 qmp
->cfg
= of_device_get_match_data(dev
);
1061 mutex_init(&qmp
->phy_mutex
);
1063 ret
= qmp_usbc_vreg_init(qmp
);
1067 ret
= qmp_usbc_typec_switch_register(qmp
);
1071 ret
= qmp_usbc_parse_vls_clamp(qmp
);
1075 /* Check for legacy binding with child node. */
1076 np
= of_get_child_by_name(dev
->of_node
, "phy");
1078 ret
= qmp_usbc_parse_dt_legacy(qmp
, np
);
1080 np
= of_node_get(dev
->of_node
);
1081 ret
= qmp_usbc_parse_dt(qmp
);
1086 pm_runtime_set_active(dev
);
1087 ret
= devm_pm_runtime_enable(dev
);
1091 * Prevent runtime pm from being ON by default. Users can enable
1092 * it using power/control in sysfs.
1094 pm_runtime_forbid(dev
);
1096 ret
= phy_pipe_clk_register(qmp
, np
);
1100 qmp
->phy
= devm_phy_create(dev
, np
, &qmp_usbc_phy_ops
);
1101 if (IS_ERR(qmp
->phy
)) {
1102 ret
= PTR_ERR(qmp
->phy
);
1103 dev_err(dev
, "failed to create PHY: %d\n", ret
);
1107 phy_set_drvdata(qmp
->phy
, qmp
);
1111 phy_provider
= devm_of_phy_provider_register(dev
, of_phy_simple_xlate
);
1113 return PTR_ERR_OR_ZERO(phy_provider
);
1120 static const struct of_device_id qmp_usbc_of_match_table
[] = {
1122 .compatible
= "qcom,msm8998-qmp-usb3-phy",
1123 .data
= &msm8998_usb3phy_cfg
,
1125 .compatible
= "qcom,qcm2290-qmp-usb3-phy",
1126 .data
= &qcm2290_usb3phy_cfg
,
1128 .compatible
= "qcom,sdm660-qmp-usb3-phy",
1129 .data
= &sdm660_usb3phy_cfg
,
1131 .compatible
= "qcom,sm6115-qmp-usb3-phy",
1132 .data
= &qcm2290_usb3phy_cfg
,
1136 MODULE_DEVICE_TABLE(of
, qmp_usbc_of_match_table
);
1138 static struct platform_driver qmp_usbc_driver
= {
1139 .probe
= qmp_usbc_probe
,
1141 .name
= "qcom-qmp-usbc-phy",
1142 .pm
= &qmp_usbc_pm_ops
,
1143 .of_match_table
= qmp_usbc_of_match_table
,
1147 module_platform_driver(qmp_usbc_driver
);
1149 MODULE_AUTHOR("Vivek Gautam <vivek.gautam@codeaurora.org>");
1150 MODULE_DESCRIPTION("Qualcomm QMP USB-C PHY driver");
1151 MODULE_LICENSE("GPL");