1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2023, Linaro Limited
6 #include <linux/module.h>
7 #include <linux/platform_device.h>
8 #include <linux/regulator/consumer.h>
9 #include <linux/regmap.h>
11 #include <linux/phy/phy.h>
13 /* eUSB2 status registers */
14 #define EUSB2_RPTR_STATUS 0x08
15 #define RPTR_OK BIT(7)
17 /* eUSB2 control registers */
18 #define EUSB2_EN_CTL1 0x46
19 #define EUSB2_RPTR_EN BIT(7)
21 #define EUSB2_FORCE_EN_5 0xe8
22 #define F_CLK_19P2M_EN BIT(6)
24 #define EUSB2_FORCE_VAL_5 0xeD
25 #define V_CLK_19P2M_EN BIT(6)
27 #define EUSB2_TUNE_USB2_CROSSOVER 0x50
28 #define EUSB2_TUNE_IUSB2 0x51
29 #define EUSB2_TUNE_RES_FSDIF 0x52
30 #define EUSB2_TUNE_HSDISC 0x53
31 #define EUSB2_TUNE_SQUELCH_U 0x54
32 #define EUSB2_TUNE_USB2_SLEW 0x55
33 #define EUSB2_TUNE_USB2_EQU 0x56
34 #define EUSB2_TUNE_USB2_PREEM 0x57
35 #define EUSB2_TUNE_USB2_HS_COMP_CUR 0x58
36 #define EUSB2_TUNE_EUSB_SLEW 0x59
37 #define EUSB2_TUNE_EUSB_EQU 0x5A
38 #define EUSB2_TUNE_EUSB_HS_COMP_CUR 0x5B
40 enum eusb2_reg_layout
{
41 TUNE_EUSB_HS_COMP_CUR
,
44 TUNE_USB2_HS_COMP_CUR
,
55 FORCE_VAL_5
= NUM_TUNE_FIELDS
,
64 struct eusb2_repeater_cfg
{
67 const char * const *vreg_list
;
71 struct eusb2_repeater
{
73 struct regmap
*regmap
;
75 struct regulator_bulk_data
*vregs
;
76 const struct eusb2_repeater_cfg
*cfg
;
81 static const char * const pm8550b_vreg_l
[] = {
85 static const u32 pm8550b_init_tbl
[NUM_TUNE_FIELDS
] = {
87 [TUNE_SQUELCH_U
] = 0x3,
88 [TUNE_USB2_PREEM
] = 0x5,
91 static const u32 smb2360_init_tbl
[NUM_TUNE_FIELDS
] = {
93 [TUNE_SQUELCH_U
] = 0x3,
94 [TUNE_USB2_PREEM
] = 0x2,
97 static const struct eusb2_repeater_cfg pm8550b_eusb2_cfg
= {
98 .init_tbl
= pm8550b_init_tbl
,
99 .init_tbl_num
= ARRAY_SIZE(pm8550b_init_tbl
),
100 .vreg_list
= pm8550b_vreg_l
,
101 .num_vregs
= ARRAY_SIZE(pm8550b_vreg_l
),
104 static const struct eusb2_repeater_cfg smb2360_eusb2_cfg
= {
105 .init_tbl
= smb2360_init_tbl
,
106 .init_tbl_num
= ARRAY_SIZE(smb2360_init_tbl
),
107 .vreg_list
= pm8550b_vreg_l
,
108 .num_vregs
= ARRAY_SIZE(pm8550b_vreg_l
),
111 static int eusb2_repeater_init_vregs(struct eusb2_repeater
*rptr
)
113 int num
= rptr
->cfg
->num_vregs
;
114 struct device
*dev
= rptr
->dev
;
117 rptr
->vregs
= devm_kcalloc(dev
, num
, sizeof(*rptr
->vregs
), GFP_KERNEL
);
121 for (i
= 0; i
< num
; i
++)
122 rptr
->vregs
[i
].supply
= rptr
->cfg
->vreg_list
[i
];
124 return devm_regulator_bulk_get(dev
, num
, rptr
->vregs
);
127 static int eusb2_repeater_init(struct phy
*phy
)
129 struct eusb2_repeater
*rptr
= phy_get_drvdata(phy
);
130 struct device_node
*np
= rptr
->dev
->of_node
;
131 struct regmap
*regmap
= rptr
->regmap
;
132 const u32
*init_tbl
= rptr
->cfg
->init_tbl
;
133 u8 tune_usb2_preem
= init_tbl
[TUNE_USB2_PREEM
];
134 u8 tune_hsdisc
= init_tbl
[TUNE_HSDISC
];
135 u8 tune_iusb2
= init_tbl
[TUNE_IUSB2
];
136 u32 base
= rptr
->base
;
140 of_property_read_u8(np
, "qcom,tune-usb2-amplitude", &tune_iusb2
);
141 of_property_read_u8(np
, "qcom,tune-usb2-disc-thres", &tune_hsdisc
);
142 of_property_read_u8(np
, "qcom,tune-usb2-preem", &tune_usb2_preem
);
144 ret
= regulator_bulk_enable(rptr
->cfg
->num_vregs
, rptr
->vregs
);
148 regmap_write(regmap
, base
+ EUSB2_EN_CTL1
, EUSB2_RPTR_EN
);
150 regmap_write(regmap
, base
+ EUSB2_TUNE_EUSB_HS_COMP_CUR
, init_tbl
[TUNE_EUSB_HS_COMP_CUR
]);
151 regmap_write(regmap
, base
+ EUSB2_TUNE_EUSB_EQU
, init_tbl
[TUNE_EUSB_EQU
]);
152 regmap_write(regmap
, base
+ EUSB2_TUNE_EUSB_SLEW
, init_tbl
[TUNE_EUSB_SLEW
]);
153 regmap_write(regmap
, base
+ EUSB2_TUNE_USB2_HS_COMP_CUR
, init_tbl
[TUNE_USB2_HS_COMP_CUR
]);
154 regmap_write(regmap
, base
+ EUSB2_TUNE_USB2_EQU
, init_tbl
[TUNE_USB2_EQU
]);
155 regmap_write(regmap
, base
+ EUSB2_TUNE_USB2_SLEW
, init_tbl
[TUNE_USB2_SLEW
]);
156 regmap_write(regmap
, base
+ EUSB2_TUNE_SQUELCH_U
, init_tbl
[TUNE_SQUELCH_U
]);
157 regmap_write(regmap
, base
+ EUSB2_TUNE_RES_FSDIF
, init_tbl
[TUNE_RES_FSDIF
]);
158 regmap_write(regmap
, base
+ EUSB2_TUNE_USB2_CROSSOVER
, init_tbl
[TUNE_USB2_CROSSOVER
]);
160 regmap_write(regmap
, base
+ EUSB2_TUNE_USB2_PREEM
, tune_usb2_preem
);
161 regmap_write(regmap
, base
+ EUSB2_TUNE_HSDISC
, tune_hsdisc
);
162 regmap_write(regmap
, base
+ EUSB2_TUNE_IUSB2
, tune_iusb2
);
164 ret
= regmap_read_poll_timeout(regmap
, base
+ EUSB2_RPTR_STATUS
, val
, val
& RPTR_OK
, 10, 5);
166 dev_err(rptr
->dev
, "initialization timed-out\n");
171 static int eusb2_repeater_set_mode(struct phy
*phy
,
172 enum phy_mode mode
, int submode
)
174 struct eusb2_repeater
*rptr
= phy_get_drvdata(phy
);
175 struct regmap
*regmap
= rptr
->regmap
;
176 u32 base
= rptr
->base
;
179 case PHY_MODE_USB_HOST
:
181 * CM.Lx is prohibited when repeater is already into Lx state as
182 * per eUSB 1.2 Spec. Below implement software workaround until
183 * PHY and controller is fixing seen observation.
185 regmap_write(regmap
, base
+ EUSB2_FORCE_EN_5
, F_CLK_19P2M_EN
);
186 regmap_write(regmap
, base
+ EUSB2_FORCE_VAL_5
, V_CLK_19P2M_EN
);
188 case PHY_MODE_USB_DEVICE
:
190 * In device mode clear host mode related workaround as there
191 * is no repeater reset available, and enable/disable of
192 * repeater doesn't clear previous value due to shared
193 * regulators (say host <-> device mode switch).
195 regmap_write(regmap
, base
+ EUSB2_FORCE_EN_5
, 0);
196 regmap_write(regmap
, base
+ EUSB2_FORCE_VAL_5
, 0);
205 static int eusb2_repeater_exit(struct phy
*phy
)
207 struct eusb2_repeater
*rptr
= phy_get_drvdata(phy
);
209 return regulator_bulk_disable(rptr
->cfg
->num_vregs
, rptr
->vregs
);
212 static const struct phy_ops eusb2_repeater_ops
= {
213 .init
= eusb2_repeater_init
,
214 .exit
= eusb2_repeater_exit
,
215 .set_mode
= eusb2_repeater_set_mode
,
216 .owner
= THIS_MODULE
,
219 static int eusb2_repeater_probe(struct platform_device
*pdev
)
221 struct eusb2_repeater
*rptr
;
222 struct device
*dev
= &pdev
->dev
;
223 struct phy_provider
*phy_provider
;
224 struct device_node
*np
= dev
->of_node
;
228 rptr
= devm_kzalloc(dev
, sizeof(*rptr
), GFP_KERNEL
);
233 dev_set_drvdata(dev
, rptr
);
235 rptr
->cfg
= of_device_get_match_data(dev
);
239 rptr
->regmap
= dev_get_regmap(dev
->parent
, NULL
);
243 ret
= of_property_read_u32(np
, "reg", &res
);
249 ret
= eusb2_repeater_init_vregs(rptr
);
251 dev_err(dev
, "unable to get supplies\n");
255 rptr
->phy
= devm_phy_create(dev
, np
, &eusb2_repeater_ops
);
256 if (IS_ERR(rptr
->phy
)) {
257 dev_err(dev
, "failed to create PHY: %d\n", ret
);
258 return PTR_ERR(rptr
->phy
);
261 phy_set_drvdata(rptr
->phy
, rptr
);
263 phy_provider
= devm_of_phy_provider_register(dev
, of_phy_simple_xlate
);
264 if (IS_ERR(phy_provider
))
265 return PTR_ERR(phy_provider
);
267 dev_info(dev
, "Registered Qcom-eUSB2 repeater\n");
272 static void eusb2_repeater_remove(struct platform_device
*pdev
)
274 struct eusb2_repeater
*rptr
= platform_get_drvdata(pdev
);
279 eusb2_repeater_exit(rptr
->phy
);
282 static const struct of_device_id eusb2_repeater_of_match_table
[] = {
284 .compatible
= "qcom,pm8550b-eusb2-repeater",
285 .data
= &pm8550b_eusb2_cfg
,
288 .compatible
= "qcom,smb2360-eusb2-repeater",
289 .data
= &smb2360_eusb2_cfg
,
293 MODULE_DEVICE_TABLE(of
, eusb2_repeater_of_match_table
);
295 static struct platform_driver eusb2_repeater_driver
= {
296 .probe
= eusb2_repeater_probe
,
297 .remove
= eusb2_repeater_remove
,
299 .name
= "qcom-eusb2-repeater",
300 .of_match_table
= eusb2_repeater_of_match_table
,
304 module_platform_driver(eusb2_repeater_driver
);
306 MODULE_DESCRIPTION("Qualcomm PMIC eUSB2 Repeater driver");
307 MODULE_LICENSE("GPL");