1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * STMicroelectronics STM32 USB PHY Controller driver
4  *
5  * Copyright (C) 2018 STMicroelectronics
6  * Author(s): Amelie Delaunay <amelie.delaunay@st.com>.
7  */
8 #include <linux/bitfield.h>
9 #include <linux/clk.h>
10 #include <linux/clk-provider.h>
11 #include <linux/delay.h>
12 #include <linux/iopoll.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/of_platform.h>
16 #include <linux/phy/phy.h>
17 #include <linux/reset.h>
18 #include <linux/units.h>
19 
20 #define STM32_USBPHYC_PLL	0x0
21 #define STM32_USBPHYC_MISC	0x8
22 #define STM32_USBPHYC_MONITOR(X) (0x108 + ((X) * 0x100))
23 #define STM32_USBPHYC_TUNE(X)	(0x10C + ((X) * 0x100))
24 #define STM32_USBPHYC_VERSION	0x3F4
25 
26 /* STM32_USBPHYC_PLL bit fields */
27 #define PLLNDIV			GENMASK(6, 0)
28 #define PLLFRACIN		GENMASK(25, 10)
29 #define PLLEN			BIT(26)
30 #define PLLSTRB			BIT(27)
31 #define PLLSTRBYP		BIT(28)
32 #define PLLFRACCTL		BIT(29)
33 #define PLLDITHEN0		BIT(30)
34 #define PLLDITHEN1		BIT(31)
35 
36 /* STM32_USBPHYC_MISC bit fields */
37 #define SWITHOST		BIT(0)
38 
39 /* STM32_USBPHYC_MONITOR bit fields */
40 #define STM32_USBPHYC_MON_OUT	GENMASK(3, 0)
41 #define STM32_USBPHYC_MON_SEL	GENMASK(8, 4)
42 #define STM32_USBPHYC_MON_SEL_LOCKP 0x1F
43 #define STM32_USBPHYC_MON_OUT_LOCKP BIT(3)
44 
45 /* STM32_USBPHYC_TUNE bit fields */
46 #define INCURREN		BIT(0)
47 #define INCURRINT		BIT(1)
48 #define LFSCAPEN		BIT(2)
49 #define HSDRVSLEW		BIT(3)
50 #define HSDRVDCCUR		BIT(4)
51 #define HSDRVDCLEV		BIT(5)
52 #define HSDRVCURINCR		BIT(6)
53 #define FSDRVRFADJ		BIT(7)
54 #define HSDRVRFRED		BIT(8)
55 #define HSDRVCHKITRM		GENMASK(12, 9)
56 #define HSDRVCHKZTRM		GENMASK(14, 13)
57 #define OTPCOMP			GENMASK(19, 15)
58 #define SQLCHCTL		GENMASK(21, 20)
59 #define HDRXGNEQEN		BIT(22)
60 #define HSRXOFF			GENMASK(24, 23)
61 #define HSFALLPREEM		BIT(25)
62 #define SHTCCTCTLPROT		BIT(26)
63 #define STAGSEL			BIT(27)
64 
65 enum boosting_vals {
66 	BOOST_1000_UA = 1000,
67 	BOOST_2000_UA = 2000,
68 };
69 
70 enum dc_level_vals {
71 	DC_NOMINAL,
72 	DC_PLUS_5_TO_7_MV,
73 	DC_PLUS_10_TO_14_MV,
74 	DC_MINUS_5_TO_7_MV,
75 	DC_MAX,
76 };
77 
78 enum current_trim {
79 	CUR_NOMINAL,
80 	CUR_PLUS_1_56_PCT,
81 	CUR_PLUS_3_12_PCT,
82 	CUR_PLUS_4_68_PCT,
83 	CUR_PLUS_6_24_PCT,
84 	CUR_PLUS_7_8_PCT,
85 	CUR_PLUS_9_36_PCT,
86 	CUR_PLUS_10_92_PCT,
87 	CUR_PLUS_12_48_PCT,
88 	CUR_PLUS_14_04_PCT,
89 	CUR_PLUS_15_6_PCT,
90 	CUR_PLUS_17_16_PCT,
91 	CUR_PLUS_19_01_PCT,
92 	CUR_PLUS_20_58_PCT,
93 	CUR_PLUS_22_16_PCT,
94 	CUR_PLUS_23_73_PCT,
95 	CUR_MAX,
96 };
97 
98 enum impedance_trim {
99 	IMP_NOMINAL,
100 	IMP_MINUS_2_OHMS,
101 	IMP_MINUS_4_OMHS,
102 	IMP_MINUS_6_OHMS,
103 	IMP_MAX,
104 };
105 
106 enum squelch_level {
107 	SQLCH_NOMINAL,
108 	SQLCH_PLUS_7_MV,
109 	SQLCH_MINUS_5_MV,
110 	SQLCH_PLUS_14_MV,
111 	SQLCH_MAX,
112 };
113 
114 enum rx_offset {
115 	NO_RX_OFFSET,
116 	RX_OFFSET_PLUS_5_MV,
117 	RX_OFFSET_PLUS_10_MV,
118 	RX_OFFSET_MINUS_5_MV,
119 	RX_OFFSET_MAX,
120 };
121 
122 /* STM32_USBPHYC_VERSION bit fields */
123 #define MINREV			GENMASK(3, 0)
124 #define MAJREV			GENMASK(7, 4)
125 
126 #define PLL_FVCO_MHZ		2880
127 #define PLL_INFF_MIN_RATE_HZ	19200000
128 #define PLL_INFF_MAX_RATE_HZ	38400000
129 
130 struct pll_params {
131 	u8 ndiv;
132 	u16 frac;
133 };
134 
135 struct stm32_usbphyc_phy {
136 	struct phy *phy;
137 	struct stm32_usbphyc *usbphyc;
138 	struct regulator *vbus;
139 	u32 index;
140 	bool active;
141 	u32 tune;
142 };
143 
144 struct stm32_usbphyc {
145 	struct device *dev;
146 	void __iomem *base;
147 	struct clk *clk;
148 	struct reset_control *rst;
149 	struct stm32_usbphyc_phy **phys;
150 	int nphys;
151 	struct regulator *vdda1v1;
152 	struct regulator *vdda1v8;
153 	atomic_t n_pll_cons;
154 	struct clk_hw clk48_hw;
155 	int switch_setup;
156 };
157 
158 static inline void stm32_usbphyc_set_bits(void __iomem *reg, u32 bits)
159 {
160 	writel_relaxed(readl_relaxed(reg) | bits, reg);
161 }
162 
163 static inline void stm32_usbphyc_clr_bits(void __iomem *reg, u32 bits)
164 {
165 	writel_relaxed(readl_relaxed(reg) & ~bits, reg);
166 }
167 
168 static int stm32_usbphyc_regulators_enable(struct stm32_usbphyc *usbphyc)
169 {
170 	int ret;
171 
172 	ret = regulator_enable(usbphyc->vdda1v1);
173 	if (ret)
174 		return ret;
175 
176 	ret = regulator_enable(usbphyc->vdda1v8);
177 	if (ret)
178 		goto vdda1v1_disable;
179 
180 	return 0;
181 
182 vdda1v1_disable:
183 	regulator_disable(usbphyc->vdda1v1);
184 
185 	return ret;
186 }
187 
188 static int stm32_usbphyc_regulators_disable(struct stm32_usbphyc *usbphyc)
189 {
190 	int ret;
191 
192 	ret = regulator_disable(usbphyc->vdda1v8);
193 	if (ret)
194 		return ret;
195 
196 	ret = regulator_disable(usbphyc->vdda1v1);
197 	if (ret)
198 		return ret;
199 
200 	return 0;
201 }
202 
203 static void stm32_usbphyc_get_pll_params(u32 clk_rate,
204 					 struct pll_params *pll_params)
205 {
206 	unsigned long long fvco, ndiv, frac;
207 
208 	/*    _
209 	 *   | FVCO = INFF*2*(NDIV + FRACT/2^16) when DITHER_DISABLE[1] = 1
210 	 *   | FVCO = 2880MHz
211 	 *  <
212 	 *   | NDIV = integer part of input bits to set the LDF
213 	 *   |_FRACT = fractional part of input bits to set the LDF
214 	 *  =>	PLLNDIV = integer part of (FVCO / (INFF*2))
215 	 *  =>	PLLFRACIN = fractional part of(FVCO / INFF*2) * 2^16
216 	 * <=>  PLLFRACIN = ((FVCO / (INFF*2)) - PLLNDIV) * 2^16
217 	 */
218 	fvco = (unsigned long long)PLL_FVCO_MHZ * HZ_PER_MHZ;
219 
220 	ndiv = fvco;
221 	do_div(ndiv, (clk_rate * 2));
222 	pll_params->ndiv = (u8)ndiv;
223 
224 	frac = fvco * (1 << 16);
225 	do_div(frac, (clk_rate * 2));
226 	frac = frac - (ndiv * (1 << 16));
227 	pll_params->frac = (u16)frac;
228 }
229 
230 static int stm32_usbphyc_pll_init(struct stm32_usbphyc *usbphyc)
231 {
232 	struct pll_params pll_params;
233 	u32 clk_rate = clk_get_rate(usbphyc->clk);
234 	u32 ndiv, frac;
235 	u32 usbphyc_pll;
236 
237 	if ((clk_rate < PLL_INFF_MIN_RATE_HZ) ||
238 	    (clk_rate > PLL_INFF_MAX_RATE_HZ)) {
239 		dev_err(usbphyc->dev, "input clk freq (%dHz) out of range\n",
240 			clk_rate);
241 		return -EINVAL;
242 	}
243 
244 	stm32_usbphyc_get_pll_params(clk_rate, &pll_params);
245 	ndiv = FIELD_PREP(PLLNDIV, pll_params.ndiv);
246 	frac = FIELD_PREP(PLLFRACIN, pll_params.frac);
247 
248 	usbphyc_pll = PLLDITHEN1 | PLLDITHEN0 | PLLSTRBYP | ndiv;
249 
250 	if (pll_params.frac)
251 		usbphyc_pll |= PLLFRACCTL | frac;
252 
253 	writel_relaxed(usbphyc_pll, usbphyc->base + STM32_USBPHYC_PLL);
254 
255 	dev_dbg(usbphyc->dev, "input clk freq=%dHz, ndiv=%lu, frac=%lu\n",
256 		clk_rate, FIELD_GET(PLLNDIV, usbphyc_pll),
257 		FIELD_GET(PLLFRACIN, usbphyc_pll));
258 
259 	return 0;
260 }
261 
262 static int __stm32_usbphyc_pll_disable(struct stm32_usbphyc *usbphyc)
263 {
264 	void __iomem *pll_reg = usbphyc->base + STM32_USBPHYC_PLL;
265 	u32 pllen;
266 
267 	stm32_usbphyc_clr_bits(pll_reg, PLLEN);
268 
269 	/* Wait for minimum width of powerdown pulse (ENABLE = Low) */
270 	if (readl_relaxed_poll_timeout(pll_reg, pllen, !(pllen & PLLEN), 5, 50))
271 		dev_err(usbphyc->dev, "PLL not reset\n");
272 
273 	return stm32_usbphyc_regulators_disable(usbphyc);
274 }
275 
276 static int stm32_usbphyc_pll_disable(struct stm32_usbphyc *usbphyc)
277 {
278 	/* Check if a phy port is still active or clk48 in use */
279 	if (atomic_dec_return(&usbphyc->n_pll_cons) > 0)
280 		return 0;
281 
282 	return __stm32_usbphyc_pll_disable(usbphyc);
283 }
284 
285 static int stm32_usbphyc_pll_enable(struct stm32_usbphyc *usbphyc)
286 {
287 	void __iomem *pll_reg = usbphyc->base + STM32_USBPHYC_PLL;
288 	bool pllen = readl_relaxed(pll_reg) & PLLEN;
289 	int ret;
290 
291 	/*
292 	 * Check if a phy port or clk48 prepare has configured the pll
293 	 * and ensure the PLL is enabled
294 	 */
295 	if (atomic_inc_return(&usbphyc->n_pll_cons) > 1 && pllen)
296 		return 0;
297 
298 	if (pllen) {
299 		/*
300 		 * PLL shouldn't be enabled without known consumer,
301 		 * disable it and reinit n_pll_cons
302 		 */
303 		dev_warn(usbphyc->dev, "PLL enabled without known consumers\n");
304 
305 		ret = __stm32_usbphyc_pll_disable(usbphyc);
306 		if (ret)
307 			goto dec_n_pll_cons;
308 	}
309 
310 	ret = stm32_usbphyc_regulators_enable(usbphyc);
311 	if (ret)
312 		goto dec_n_pll_cons;
313 
314 	ret = stm32_usbphyc_pll_init(usbphyc);
315 	if (ret)
316 		goto reg_disable;
317 
318 	stm32_usbphyc_set_bits(pll_reg, PLLEN);
319 
320 	/* Wait for maximum lock time */
321 	usleep_range(200, 300);
322 
323 	return 0;
324 
325 reg_disable:
326 	stm32_usbphyc_regulators_disable(usbphyc);
327 
328 dec_n_pll_cons:
329 	atomic_dec(&usbphyc->n_pll_cons);
330 
331 	return ret;
332 }
333 
334 static int stm32_usbphyc_phy_init(struct phy *phy)
335 {
336 	struct stm32_usbphyc_phy *usbphyc_phy = phy_get_drvdata(phy);
337 	struct stm32_usbphyc *usbphyc = usbphyc_phy->usbphyc;
338 	u32 reg_mon = STM32_USBPHYC_MONITOR(usbphyc_phy->index);
339 	u32 monsel = FIELD_PREP(STM32_USBPHYC_MON_SEL,
340 				STM32_USBPHYC_MON_SEL_LOCKP);
341 	u32 monout;
342 	int ret;
343 
344 	ret = stm32_usbphyc_pll_enable(usbphyc);
345 	if (ret)
346 		return ret;
347 
348 	/* Check that PLL Lock input to PHY is High */
349 	writel_relaxed(monsel, usbphyc->base + reg_mon);
350 	ret = readl_relaxed_poll_timeout(usbphyc->base + reg_mon, monout,
351 					 (monout & STM32_USBPHYC_MON_OUT_LOCKP),
352 					 100, 1000);
353 	if (ret) {
354 		dev_err(usbphyc->dev, "PLL Lock input to PHY is Low (val=%x)\n",
355 			(u32)(monout & STM32_USBPHYC_MON_OUT));
356 		goto pll_disable;
357 	}
358 
359 	usbphyc_phy->active = true;
360 
361 	return 0;
362 
363 pll_disable:
364 	stm32_usbphyc_pll_disable(usbphyc);
365 
366 	return ret;
367 }
368 
369 static int stm32_usbphyc_phy_exit(struct phy *phy)
370 {
371 	struct stm32_usbphyc_phy *usbphyc_phy = phy_get_drvdata(phy);
372 	struct stm32_usbphyc *usbphyc = usbphyc_phy->usbphyc;
373 
374 	usbphyc_phy->active = false;
375 
376 	return stm32_usbphyc_pll_disable(usbphyc);
377 }
378 
379 static int stm32_usbphyc_phy_power_on(struct phy *phy)
380 {
381 	struct stm32_usbphyc_phy *usbphyc_phy = phy_get_drvdata(phy);
382 
383 	if (usbphyc_phy->vbus)
384 		return regulator_enable(usbphyc_phy->vbus);
385 
386 	return 0;
387 }
388 
389 static int stm32_usbphyc_phy_power_off(struct phy *phy)
390 {
391 	struct stm32_usbphyc_phy *usbphyc_phy = phy_get_drvdata(phy);
392 
393 	if (usbphyc_phy->vbus)
394 		return regulator_disable(usbphyc_phy->vbus);
395 
396 	return 0;
397 }
398 
399 static const struct phy_ops stm32_usbphyc_phy_ops = {
400 	.init = stm32_usbphyc_phy_init,
401 	.exit = stm32_usbphyc_phy_exit,
402 	.power_on = stm32_usbphyc_phy_power_on,
403 	.power_off = stm32_usbphyc_phy_power_off,
404 	.owner = THIS_MODULE,
405 };
406 
407 static int stm32_usbphyc_clk48_prepare(struct clk_hw *hw)
408 {
409 	struct stm32_usbphyc *usbphyc = container_of(hw, struct stm32_usbphyc, clk48_hw);
410 
411 	return stm32_usbphyc_pll_enable(usbphyc);
412 }
413 
414 static void stm32_usbphyc_clk48_unprepare(struct clk_hw *hw)
415 {
416 	struct stm32_usbphyc *usbphyc = container_of(hw, struct stm32_usbphyc, clk48_hw);
417 
418 	stm32_usbphyc_pll_disable(usbphyc);
419 }
420 
421 static unsigned long stm32_usbphyc_clk48_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
422 {
423 	return 48000000;
424 }
425 
426 static const struct clk_ops usbphyc_clk48_ops = {
427 	.prepare = stm32_usbphyc_clk48_prepare,
428 	.unprepare = stm32_usbphyc_clk48_unprepare,
429 	.recalc_rate = stm32_usbphyc_clk48_recalc_rate,
430 };
431 
432 static void stm32_usbphyc_clk48_unregister(void *data)
433 {
434 	struct stm32_usbphyc *usbphyc = data;
435 
436 	of_clk_del_provider(usbphyc->dev->of_node);
437 	clk_hw_unregister(&usbphyc->clk48_hw);
438 }
439 
440 static int stm32_usbphyc_clk48_register(struct stm32_usbphyc *usbphyc)
441 {
442 	struct device_node *node = usbphyc->dev->of_node;
443 	struct clk_init_data init = { };
444 	int ret = 0;
445 
446 	init.name = "ck_usbo_48m";
447 	init.ops = &usbphyc_clk48_ops;
448 
449 	usbphyc->clk48_hw.init = &init;
450 
451 	ret = clk_hw_register(usbphyc->dev, &usbphyc->clk48_hw);
452 	if (ret)
453 		return ret;
454 
455 	ret = of_clk_add_hw_provider(node, of_clk_hw_simple_get, &usbphyc->clk48_hw);
456 	if (ret)
457 		clk_hw_unregister(&usbphyc->clk48_hw);
458 
459 	return ret;
460 }
461 
462 static void stm32_usbphyc_phy_tuning(struct stm32_usbphyc *usbphyc,
463 				     struct device_node *np, u32 index)
464 {
465 	struct stm32_usbphyc_phy *usbphyc_phy = usbphyc->phys[index];
466 	u32 reg = STM32_USBPHYC_TUNE(index);
467 	u32 otpcomp, val;
468 	int ret;
469 
470 	/* Backup OTP compensation code */
471 	otpcomp = FIELD_GET(OTPCOMP, readl_relaxed(usbphyc->base + reg));
472 
473 	ret = of_property_read_u32(np, "st,current-boost-microamp", &val);
474 	if (ret != -EINVAL) {
475 		if (!ret && (val == BOOST_1000_UA || val == BOOST_2000_UA)) {
476 			val = (val == BOOST_2000_UA) ? 1 : 0;
477 			usbphyc_phy->tune |= INCURREN | FIELD_PREP(INCURRINT, val);
478 		} else {
479 			dev_warn(usbphyc->dev, "phy%d: invalid st,current-boost-microamp\n", index);
480 		}
481 	}
482 
483 	if (!of_property_read_bool(np, "st,no-lsfs-fb-cap"))
484 		usbphyc_phy->tune |= LFSCAPEN;
485 
486 	if (of_property_read_bool(np, "st,decrease-hs-slew-rate"))
487 		usbphyc_phy->tune |= HSDRVSLEW;
488 
489 	ret = of_property_read_u32(np, "st,tune-hs-dc-level", &val);
490 	if (ret != -EINVAL) {
491 		if (!ret && val < DC_MAX) {
492 			if (val == DC_MINUS_5_TO_7_MV) {/* Decreases HS driver DC level */
493 				usbphyc_phy->tune |= HSDRVDCCUR;
494 			} else if (val > 0) {		/* Increases HS driver DC level */
495 				val = (val == DC_PLUS_10_TO_14_MV) ? 1 : 0;
496 				usbphyc_phy->tune |= HSDRVCURINCR | FIELD_PREP(HSDRVDCLEV, val);
497 			}
498 		} else {
499 			dev_warn(usbphyc->dev, "phy%d: invalid st,tune-hs-dc-level\n", index);
500 		}
501 	}
502 
503 	if (of_property_read_bool(np, "st,enable-fs-rftime-tuning"))
504 		usbphyc_phy->tune |= FSDRVRFADJ;
505 
506 	if (of_property_read_bool(np, "st,enable-hs-rftime-reduction"))
507 		usbphyc_phy->tune |= HSDRVRFRED;
508 
509 	ret = of_property_read_u32(np, "st,trim-hs-current", &val);
510 	if (ret != -EINVAL) {
511 		if (!ret && val < CUR_MAX)
512 			usbphyc_phy->tune |= FIELD_PREP(HSDRVCHKITRM, val);
513 		else
514 			dev_warn(usbphyc->dev, "phy%d: invalid st,trim-hs-current\n", index);
515 	}
516 
517 	ret = of_property_read_u32(np, "st,trim-hs-impedance", &val);
518 	if (ret != -EINVAL) {
519 		if (!ret && val < IMP_MAX)
520 			usbphyc_phy->tune |= FIELD_PREP(HSDRVCHKZTRM, val);
521 		else
522 			dev_warn(usbphyc->dev, "phy%d: invalid st,trim-hs-impedance\n", index);
523 	}
524 
525 	ret = of_property_read_u32(np, "st,tune-squelch-level", &val);
526 	if (ret != -EINVAL) {
527 		if (!ret && val < SQLCH_MAX)
528 			usbphyc_phy->tune |= FIELD_PREP(SQLCHCTL, val);
529 		else
530 			dev_warn(usbphyc->dev, "phy%d: invalid st,tune-squelch\n", index);
531 	}
532 
533 	if (of_property_read_bool(np, "st,enable-hs-rx-gain-eq"))
534 		usbphyc_phy->tune |= HDRXGNEQEN;
535 
536 	ret = of_property_read_u32(np, "st,tune-hs-rx-offset", &val);
537 	if (ret != -EINVAL) {
538 		if (!ret && val < RX_OFFSET_MAX)
539 			usbphyc_phy->tune |= FIELD_PREP(HSRXOFF, val);
540 		else
541 			dev_warn(usbphyc->dev, "phy%d: invalid st,tune-hs-rx-offset\n", index);
542 	}
543 
544 	if (of_property_read_bool(np, "st,no-hs-ftime-ctrl"))
545 		usbphyc_phy->tune |= HSFALLPREEM;
546 
547 	if (!of_property_read_bool(np, "st,no-lsfs-sc"))
548 		usbphyc_phy->tune |= SHTCCTCTLPROT;
549 
550 	if (of_property_read_bool(np, "st,enable-hs-tx-staggering"))
551 		usbphyc_phy->tune |= STAGSEL;
552 
553 	/* Restore OTP compensation code */
554 	usbphyc_phy->tune |= FIELD_PREP(OTPCOMP, otpcomp);
555 
556 	/*
557 	 * By default, if no st,xxx tuning property is used, usbphyc_phy->tune is equal to
558 	 * STM32_USBPHYC_TUNE reset value (LFSCAPEN | SHTCCTCTLPROT | OTPCOMP).
559 	 */
560 	writel_relaxed(usbphyc_phy->tune, usbphyc->base + reg);
561 }
562 
563 static void stm32_usbphyc_switch_setup(struct stm32_usbphyc *usbphyc,
564 				       u32 utmi_switch)
565 {
566 	if (!utmi_switch)
567 		stm32_usbphyc_clr_bits(usbphyc->base + STM32_USBPHYC_MISC,
568 				       SWITHOST);
569 	else
570 		stm32_usbphyc_set_bits(usbphyc->base + STM32_USBPHYC_MISC,
571 				       SWITHOST);
572 	usbphyc->switch_setup = utmi_switch;
573 }
574 
575 static struct phy *stm32_usbphyc_of_xlate(struct device *dev,
576 					  struct of_phandle_args *args)
577 {
578 	struct stm32_usbphyc *usbphyc = dev_get_drvdata(dev);
579 	struct stm32_usbphyc_phy *usbphyc_phy = NULL;
580 	struct device_node *phynode = args->np;
581 	int port = 0;
582 
583 	for (port = 0; port < usbphyc->nphys; port++) {
584 		if (phynode == usbphyc->phys[port]->phy->dev.of_node) {
585 			usbphyc_phy = usbphyc->phys[port];
586 			break;
587 		}
588 	}
589 	if (!usbphyc_phy) {
590 		dev_err(dev, "failed to find phy\n");
591 		return ERR_PTR(-EINVAL);
592 	}
593 
594 	if (((usbphyc_phy->index == 0) && (args->args_count != 0)) ||
595 	    ((usbphyc_phy->index == 1) && (args->args_count != 1))) {
596 		dev_err(dev, "invalid number of cells for phy port%d\n",
597 			usbphyc_phy->index);
598 		return ERR_PTR(-EINVAL);
599 	}
600 
601 	/* Configure the UTMI switch for PHY port#2 */
602 	if (usbphyc_phy->index == 1) {
603 		if (usbphyc->switch_setup < 0) {
604 			stm32_usbphyc_switch_setup(usbphyc, args->args[0]);
605 		} else {
606 			if (args->args[0] != usbphyc->switch_setup) {
607 				dev_err(dev, "phy port1 already used\n");
608 				return ERR_PTR(-EBUSY);
609 			}
610 		}
611 	}
612 
613 	return usbphyc_phy->phy;
614 }
615 
616 static int stm32_usbphyc_probe(struct platform_device *pdev)
617 {
618 	struct stm32_usbphyc *usbphyc;
619 	struct device *dev = &pdev->dev;
620 	struct device_node *child, *np = dev->of_node;
621 	struct phy_provider *phy_provider;
622 	u32 pllen, version;
623 	int ret, port = 0;
624 
625 	usbphyc = devm_kzalloc(dev, sizeof(*usbphyc), GFP_KERNEL);
626 	if (!usbphyc)
627 		return -ENOMEM;
628 	usbphyc->dev = dev;
629 	dev_set_drvdata(dev, usbphyc);
630 
631 	usbphyc->base = devm_platform_ioremap_resource(pdev, 0);
632 	if (IS_ERR(usbphyc->base))
633 		return PTR_ERR(usbphyc->base);
634 
635 	usbphyc->clk = devm_clk_get(dev, NULL);
636 	if (IS_ERR(usbphyc->clk))
637 		return dev_err_probe(dev, PTR_ERR(usbphyc->clk), "clk get_failed\n");
638 
639 	ret = clk_prepare_enable(usbphyc->clk);
640 	if (ret) {
641 		dev_err(dev, "clk enable failed: %d\n", ret);
642 		return ret;
643 	}
644 
645 	usbphyc->rst = devm_reset_control_get(dev, NULL);
646 	if (!IS_ERR(usbphyc->rst)) {
647 		reset_control_assert(usbphyc->rst);
648 		udelay(2);
649 		reset_control_deassert(usbphyc->rst);
650 	} else {
651 		ret = PTR_ERR(usbphyc->rst);
652 		if (ret == -EPROBE_DEFER)
653 			goto clk_disable;
654 
655 		stm32_usbphyc_clr_bits(usbphyc->base + STM32_USBPHYC_PLL, PLLEN);
656 	}
657 
658 	/*
659 	 * Wait for minimum width of powerdown pulse (ENABLE = Low):
660 	 * we have to ensure the PLL is disabled before phys initialization.
661 	 */
662 	if (readl_relaxed_poll_timeout(usbphyc->base + STM32_USBPHYC_PLL,
663 				       pllen, !(pllen & PLLEN), 5, 50)) {
664 		dev_warn(usbphyc->dev, "PLL not reset\n");
665 		ret = -EPROBE_DEFER;
666 		goto clk_disable;
667 	}
668 
669 	usbphyc->switch_setup = -EINVAL;
670 	usbphyc->nphys = of_get_child_count(np);
671 	usbphyc->phys = devm_kcalloc(dev, usbphyc->nphys,
672 				     sizeof(*usbphyc->phys), GFP_KERNEL);
673 	if (!usbphyc->phys) {
674 		ret = -ENOMEM;
675 		goto clk_disable;
676 	}
677 
678 	usbphyc->vdda1v1 = devm_regulator_get(dev, "vdda1v1");
679 	if (IS_ERR(usbphyc->vdda1v1)) {
680 		ret = dev_err_probe(dev, PTR_ERR(usbphyc->vdda1v1),
681 				    "failed to get vdda1v1 supply\n");
682 		goto clk_disable;
683 	}
684 
685 	usbphyc->vdda1v8 = devm_regulator_get(dev, "vdda1v8");
686 	if (IS_ERR(usbphyc->vdda1v8)) {
687 		ret = dev_err_probe(dev, PTR_ERR(usbphyc->vdda1v8),
688 				    "failed to get vdda1v8 supply\n");
689 		goto clk_disable;
690 	}
691 
692 	for_each_child_of_node(np, child) {
693 		struct stm32_usbphyc_phy *usbphyc_phy;
694 		struct phy *phy;
695 		u32 index;
696 
697 		phy = devm_phy_create(dev, child, &stm32_usbphyc_phy_ops);
698 		if (IS_ERR(phy)) {
699 			ret = PTR_ERR(phy);
700 			if (ret != -EPROBE_DEFER)
701 				dev_err(dev, "failed to create phy%d: %d\n",
702 					port, ret);
703 			goto put_child;
704 		}
705 
706 		usbphyc_phy = devm_kzalloc(dev, sizeof(*usbphyc_phy),
707 					   GFP_KERNEL);
708 		if (!usbphyc_phy) {
709 			ret = -ENOMEM;
710 			goto put_child;
711 		}
712 
713 		ret = of_property_read_u32(child, "reg", &index);
714 		if (ret || index > usbphyc->nphys) {
715 			dev_err(&phy->dev, "invalid reg property: %d\n", ret);
716 			if (!ret)
717 				ret = -EINVAL;
718 			goto put_child;
719 		}
720 
721 		usbphyc->phys[port] = usbphyc_phy;
722 		phy_set_bus_width(phy, 8);
723 		phy_set_drvdata(phy, usbphyc_phy);
724 
725 		usbphyc->phys[port]->phy = phy;
726 		usbphyc->phys[port]->usbphyc = usbphyc;
727 		usbphyc->phys[port]->index = index;
728 		usbphyc->phys[port]->active = false;
729 
730 		usbphyc->phys[port]->vbus = devm_regulator_get_optional(&phy->dev, "vbus");
731 		if (IS_ERR(usbphyc->phys[port]->vbus)) {
732 			ret = PTR_ERR(usbphyc->phys[port]->vbus);
733 			if (ret == -EPROBE_DEFER)
734 				goto put_child;
735 			usbphyc->phys[port]->vbus = NULL;
736 		}
737 
738 		/* Configure phy tuning */
739 		stm32_usbphyc_phy_tuning(usbphyc, child, index);
740 
741 		port++;
742 	}
743 
744 	phy_provider = devm_of_phy_provider_register(dev,
745 						     stm32_usbphyc_of_xlate);
746 	if (IS_ERR(phy_provider)) {
747 		ret = PTR_ERR(phy_provider);
748 		dev_err(dev, "failed to register phy provider: %d\n", ret);
749 		goto clk_disable;
750 	}
751 
752 	ret = stm32_usbphyc_clk48_register(usbphyc);
753 	if (ret) {
754 		dev_err(dev, "failed to register ck_usbo_48m clock: %d\n", ret);
755 		goto clk_disable;
756 	}
757 
758 	version = readl_relaxed(usbphyc->base + STM32_USBPHYC_VERSION);
759 	dev_info(dev, "registered rev:%lu.%lu\n",
760 		 FIELD_GET(MAJREV, version), FIELD_GET(MINREV, version));
761 
762 	return 0;
763 
764 put_child:
765 	of_node_put(child);
766 clk_disable:
767 	clk_disable_unprepare(usbphyc->clk);
768 
769 	return ret;
770 }
771 
772 static void stm32_usbphyc_remove(struct platform_device *pdev)
773 {
774 	struct stm32_usbphyc *usbphyc = dev_get_drvdata(&pdev->dev);
775 	int port;
776 
777 	/* Ensure PHYs are not active, to allow PLL disabling */
778 	for (port = 0; port < usbphyc->nphys; port++)
779 		if (usbphyc->phys[port]->active)
780 			stm32_usbphyc_phy_exit(usbphyc->phys[port]->phy);
781 
782 	stm32_usbphyc_clk48_unregister(usbphyc);
783 
784 	clk_disable_unprepare(usbphyc->clk);
785 }
786 
787 static int __maybe_unused stm32_usbphyc_resume(struct device *dev)
788 {
789 	struct stm32_usbphyc *usbphyc = dev_get_drvdata(dev);
790 	struct stm32_usbphyc_phy *usbphyc_phy;
791 	int port;
792 
793 	if (usbphyc->switch_setup >= 0)
794 		stm32_usbphyc_switch_setup(usbphyc, usbphyc->switch_setup);
795 
796 	for (port = 0; port < usbphyc->nphys; port++) {
797 		usbphyc_phy = usbphyc->phys[port];
798 		writel_relaxed(usbphyc_phy->tune, usbphyc->base + STM32_USBPHYC_TUNE(port));
799 	}
800 
801 	return 0;
802 }
803 
804 static SIMPLE_DEV_PM_OPS(stm32_usbphyc_pm_ops, NULL, stm32_usbphyc_resume);
805 
806 static const struct of_device_id stm32_usbphyc_of_match[] = {
807 	{ .compatible = "st,stm32mp1-usbphyc", },
808 	{ },
809 };
810 MODULE_DEVICE_TABLE(of, stm32_usbphyc_of_match);
811 
812 static struct platform_driver stm32_usbphyc_driver = {
813 	.probe = stm32_usbphyc_probe,
814 	.remove_new = stm32_usbphyc_remove,
815 	.driver = {
816 		.of_match_table = stm32_usbphyc_of_match,
817 		.name = "stm32-usbphyc",
818 		.pm = &stm32_usbphyc_pm_ops,
819 	}
820 };
821 module_platform_driver(stm32_usbphyc_driver);
822 
823 MODULE_DESCRIPTION("STMicroelectronics STM32 USBPHYC driver");
824 MODULE_AUTHOR("Amelie Delaunay <amelie.delaunay@st.com>");
825 MODULE_LICENSE("GPL v2");
826