1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2023, Linaro Limited
4  */
5 
6 #include <linux/module.h>
7 #include <linux/platform_device.h>
8 #include <linux/regulator/consumer.h>
9 #include <linux/regmap.h>
10 #include <linux/of.h>
11 #include <linux/phy/phy.h>
12 
13 /* eUSB2 status registers */
14 #define EUSB2_RPTR_STATUS		0x08
15 #define	RPTR_OK				BIT(7)
16 
17 /* eUSB2 control registers */
18 #define EUSB2_EN_CTL1			0x46
19 #define EUSB2_RPTR_EN			BIT(7)
20 
21 #define EUSB2_FORCE_EN_5		0xe8
22 #define F_CLK_19P2M_EN			BIT(6)
23 
24 #define EUSB2_FORCE_VAL_5		0xeD
25 #define V_CLK_19P2M_EN			BIT(6)
26 
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
39 
40 #define QCOM_EUSB2_REPEATER_INIT_CFG(r, v)	\
41 	{					\
42 		.reg = r,			\
43 		.val = v,			\
44 	}
45 
46 enum reg_fields {
47 	F_TUNE_EUSB_HS_COMP_CUR,
48 	F_TUNE_EUSB_EQU,
49 	F_TUNE_EUSB_SLEW,
50 	F_TUNE_USB2_HS_COMP_CUR,
51 	F_TUNE_USB2_PREEM,
52 	F_TUNE_USB2_EQU,
53 	F_TUNE_USB2_SLEW,
54 	F_TUNE_SQUELCH_U,
55 	F_TUNE_HSDISC,
56 	F_TUNE_RES_FSDIF,
57 	F_TUNE_IUSB2,
58 	F_TUNE_USB2_CROSSOVER,
59 	F_NUM_TUNE_FIELDS,
60 
61 	F_FORCE_VAL_5 = F_NUM_TUNE_FIELDS,
62 	F_FORCE_EN_5,
63 
64 	F_EN_CTL1,
65 
66 	F_RPTR_STATUS,
67 	F_NUM_FIELDS,
68 };
69 
70 static struct reg_field eusb2_repeater_tune_reg_fields[F_NUM_FIELDS] = {
71 	[F_TUNE_EUSB_HS_COMP_CUR] = REG_FIELD(EUSB2_TUNE_EUSB_HS_COMP_CUR, 0, 1),
72 	[F_TUNE_EUSB_EQU] = REG_FIELD(EUSB2_TUNE_EUSB_EQU, 0, 1),
73 	[F_TUNE_EUSB_SLEW] = REG_FIELD(EUSB2_TUNE_EUSB_SLEW, 0, 1),
74 	[F_TUNE_USB2_HS_COMP_CUR] = REG_FIELD(EUSB2_TUNE_USB2_HS_COMP_CUR, 0, 1),
75 	[F_TUNE_USB2_PREEM] = REG_FIELD(EUSB2_TUNE_USB2_PREEM, 0, 2),
76 	[F_TUNE_USB2_EQU] = REG_FIELD(EUSB2_TUNE_USB2_EQU, 0, 1),
77 	[F_TUNE_USB2_SLEW] = REG_FIELD(EUSB2_TUNE_USB2_SLEW, 0, 1),
78 	[F_TUNE_SQUELCH_U] = REG_FIELD(EUSB2_TUNE_SQUELCH_U, 0, 2),
79 	[F_TUNE_HSDISC] = REG_FIELD(EUSB2_TUNE_HSDISC, 0, 2),
80 	[F_TUNE_RES_FSDIF] = REG_FIELD(EUSB2_TUNE_RES_FSDIF, 0, 2),
81 	[F_TUNE_IUSB2] = REG_FIELD(EUSB2_TUNE_IUSB2, 0, 3),
82 	[F_TUNE_USB2_CROSSOVER] = REG_FIELD(EUSB2_TUNE_USB2_CROSSOVER, 0, 2),
83 
84 	[F_FORCE_VAL_5] = REG_FIELD(EUSB2_FORCE_VAL_5, 0, 7),
85 	[F_FORCE_EN_5] = REG_FIELD(EUSB2_FORCE_EN_5, 0, 7),
86 
87 	[F_EN_CTL1] = REG_FIELD(EUSB2_EN_CTL1, 0, 7),
88 
89 	[F_RPTR_STATUS] = REG_FIELD(EUSB2_RPTR_STATUS, 0, 7),
90 };
91 
92 struct eusb2_repeater_cfg {
93 	const u32 *init_tbl;
94 	int init_tbl_num;
95 	const char * const *vreg_list;
96 	int num_vregs;
97 };
98 
99 struct eusb2_repeater {
100 	struct device *dev;
101 	struct regmap_field *regs[F_NUM_FIELDS];
102 	struct phy *phy;
103 	struct regulator_bulk_data *vregs;
104 	const struct eusb2_repeater_cfg *cfg;
105 	enum phy_mode mode;
106 };
107 
108 static const char * const pm8550b_vreg_l[] = {
109 	"vdd18", "vdd3",
110 };
111 
112 static const u32 pm8550b_init_tbl[F_NUM_TUNE_FIELDS] = {
113 	[F_TUNE_IUSB2] = 0x8,
114 	[F_TUNE_SQUELCH_U] = 0x3,
115 	[F_TUNE_USB2_PREEM] = 0x5,
116 };
117 
118 static const struct eusb2_repeater_cfg pm8550b_eusb2_cfg = {
119 	.init_tbl	= pm8550b_init_tbl,
120 	.init_tbl_num	= ARRAY_SIZE(pm8550b_init_tbl),
121 	.vreg_list	= pm8550b_vreg_l,
122 	.num_vregs	= ARRAY_SIZE(pm8550b_vreg_l),
123 };
124 
125 static int eusb2_repeater_init_vregs(struct eusb2_repeater *rptr)
126 {
127 	int num = rptr->cfg->num_vregs;
128 	struct device *dev = rptr->dev;
129 	int i;
130 
131 	rptr->vregs = devm_kcalloc(dev, num, sizeof(*rptr->vregs), GFP_KERNEL);
132 	if (!rptr->vregs)
133 		return -ENOMEM;
134 
135 	for (i = 0; i < num; i++)
136 		rptr->vregs[i].supply = rptr->cfg->vreg_list[i];
137 
138 	return devm_regulator_bulk_get(dev, num, rptr->vregs);
139 }
140 
141 static int eusb2_repeater_init(struct phy *phy)
142 {
143 	struct reg_field *regfields = eusb2_repeater_tune_reg_fields;
144 	struct eusb2_repeater *rptr = phy_get_drvdata(phy);
145 	const u32 *init_tbl = rptr->cfg->init_tbl;
146 	u32 val;
147 	int ret;
148 	int i;
149 
150 	ret = regulator_bulk_enable(rptr->cfg->num_vregs, rptr->vregs);
151 	if (ret)
152 		return ret;
153 
154 	regmap_field_update_bits(rptr->regs[F_EN_CTL1], EUSB2_RPTR_EN, EUSB2_RPTR_EN);
155 
156 	for (i = 0; i < F_NUM_TUNE_FIELDS; i++) {
157 		if (init_tbl[i]) {
158 			regmap_field_update_bits(rptr->regs[i], init_tbl[i], init_tbl[i]);
159 		} else {
160 			/* Write 0 if there's no value set */
161 			u32 mask = GENMASK(regfields[i].msb, regfields[i].lsb);
162 
163 			regmap_field_update_bits(rptr->regs[i], mask, 0);
164 		}
165 	}
166 
167 	ret = regmap_field_read_poll_timeout(rptr->regs[F_RPTR_STATUS],
168 					     val, val & RPTR_OK, 10, 5);
169 	if (ret)
170 		dev_err(rptr->dev, "initialization timed-out\n");
171 
172 	return ret;
173 }
174 
175 static int eusb2_repeater_set_mode(struct phy *phy,
176 				   enum phy_mode mode, int submode)
177 {
178 	struct eusb2_repeater *rptr = phy_get_drvdata(phy);
179 
180 	switch (mode) {
181 	case PHY_MODE_USB_HOST:
182 		/*
183 		 * CM.Lx is prohibited when repeater is already into Lx state as
184 		 * per eUSB 1.2 Spec. Below implement software workaround until
185 		 * PHY and controller is fixing seen observation.
186 		 */
187 		regmap_field_update_bits(rptr->regs[F_FORCE_EN_5],
188 					 F_CLK_19P2M_EN, F_CLK_19P2M_EN);
189 		regmap_field_update_bits(rptr->regs[F_FORCE_VAL_5],
190 					 V_CLK_19P2M_EN, V_CLK_19P2M_EN);
191 		break;
192 	case PHY_MODE_USB_DEVICE:
193 		/*
194 		 * In device mode clear host mode related workaround as there
195 		 * is no repeater reset available, and enable/disable of
196 		 * repeater doesn't clear previous value due to shared
197 		 * regulators (say host <-> device mode switch).
198 		 */
199 		regmap_field_update_bits(rptr->regs[F_FORCE_EN_5],
200 					 F_CLK_19P2M_EN, 0);
201 		regmap_field_update_bits(rptr->regs[F_FORCE_VAL_5],
202 					 V_CLK_19P2M_EN, 0);
203 		break;
204 	default:
205 		return -EINVAL;
206 	}
207 
208 	return 0;
209 }
210 
211 static int eusb2_repeater_exit(struct phy *phy)
212 {
213 	struct eusb2_repeater *rptr = phy_get_drvdata(phy);
214 
215 	return regulator_bulk_disable(rptr->cfg->num_vregs, rptr->vregs);
216 }
217 
218 static const struct phy_ops eusb2_repeater_ops = {
219 	.init		= eusb2_repeater_init,
220 	.exit		= eusb2_repeater_exit,
221 	.set_mode	= eusb2_repeater_set_mode,
222 	.owner		= THIS_MODULE,
223 };
224 
225 static int eusb2_repeater_probe(struct platform_device *pdev)
226 {
227 	struct eusb2_repeater *rptr;
228 	struct device *dev = &pdev->dev;
229 	struct phy_provider *phy_provider;
230 	struct device_node *np = dev->of_node;
231 	struct regmap *regmap;
232 	int i, ret;
233 	u32 res;
234 
235 	rptr = devm_kzalloc(dev, sizeof(*rptr), GFP_KERNEL);
236 	if (!rptr)
237 		return -ENOMEM;
238 
239 	rptr->dev = dev;
240 	dev_set_drvdata(dev, rptr);
241 
242 	rptr->cfg = of_device_get_match_data(dev);
243 	if (!rptr->cfg)
244 		return -EINVAL;
245 
246 	regmap = dev_get_regmap(dev->parent, NULL);
247 	if (!regmap)
248 		return -ENODEV;
249 
250 	ret = of_property_read_u32(np, "reg", &res);
251 	if (ret < 0)
252 		return ret;
253 
254 	for (i = 0; i < F_NUM_FIELDS; i++)
255 		eusb2_repeater_tune_reg_fields[i].reg += res;
256 
257 	ret = devm_regmap_field_bulk_alloc(dev, regmap, rptr->regs,
258 					   eusb2_repeater_tune_reg_fields,
259 					   F_NUM_FIELDS);
260 	if (ret)
261 		return ret;
262 
263 	ret = eusb2_repeater_init_vregs(rptr);
264 	if (ret < 0) {
265 		dev_err(dev, "unable to get supplies\n");
266 		return ret;
267 	}
268 
269 	rptr->phy = devm_phy_create(dev, np, &eusb2_repeater_ops);
270 	if (IS_ERR(rptr->phy)) {
271 		dev_err(dev, "failed to create PHY: %d\n", ret);
272 		return PTR_ERR(rptr->phy);
273 	}
274 
275 	phy_set_drvdata(rptr->phy, rptr);
276 
277 	phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
278 	if (IS_ERR(phy_provider))
279 		return PTR_ERR(phy_provider);
280 
281 	dev_info(dev, "Registered Qcom-eUSB2 repeater\n");
282 
283 	return 0;
284 }
285 
286 static void eusb2_repeater_remove(struct platform_device *pdev)
287 {
288 	struct eusb2_repeater *rptr = platform_get_drvdata(pdev);
289 
290 	if (!rptr)
291 		return;
292 
293 	eusb2_repeater_exit(rptr->phy);
294 }
295 
296 static const struct of_device_id eusb2_repeater_of_match_table[] = {
297 	{
298 		.compatible = "qcom,pm8550b-eusb2-repeater",
299 		.data = &pm8550b_eusb2_cfg,
300 	},
301 	{ },
302 };
303 MODULE_DEVICE_TABLE(of, eusb2_repeater_of_match_table);
304 
305 static struct platform_driver eusb2_repeater_driver = {
306 	.probe		= eusb2_repeater_probe,
307 	.remove_new	= eusb2_repeater_remove,
308 	.driver = {
309 		.name	= "qcom-eusb2-repeater",
310 		.of_match_table = eusb2_repeater_of_match_table,
311 	},
312 };
313 
314 module_platform_driver(eusb2_repeater_driver);
315 
316 MODULE_DESCRIPTION("Qualcomm PMIC eUSB2 Repeater driver");
317 MODULE_LICENSE("GPL");
318