1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Allwinner sun4i USB phy driver
4  *
5  * Copyright (C) 2014-2015 Hans de Goede <hdegoede@redhat.com>
6  *
7  * Based on code from
8  * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
9  *
10  * Modelled after: Samsung S5P/Exynos SoC series MIPI CSIS/DSIM DPHY driver
11  * Copyright (C) 2013 Samsung Electronics Co., Ltd.
12  * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
13  */
14 
15 #include <linux/clk.h>
16 #include <linux/delay.h>
17 #include <linux/err.h>
18 #include <linux/extcon-provider.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/io.h>
21 #include <linux/interrupt.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/mutex.h>
25 #include <linux/of.h>
26 #include <linux/of_address.h>
27 #include <linux/of_device.h>
28 #include <linux/of_gpio.h>
29 #include <linux/phy/phy.h>
30 #include <linux/phy/phy-sun4i-usb.h>
31 #include <linux/platform_device.h>
32 #include <linux/power_supply.h>
33 #include <linux/regulator/consumer.h>
34 #include <linux/reset.h>
35 #include <linux/spinlock.h>
36 #include <linux/usb/of.h>
37 #include <linux/workqueue.h>
38 
39 #define REG_ISCR			0x00
40 #define REG_PHYCTL_A10			0x04
41 #define REG_PHYBIST			0x08
42 #define REG_PHYTUNE			0x0c
43 #define REG_PHYCTL_A33			0x10
44 #define REG_PHY_OTGCTL			0x20
45 
46 #define REG_HCI_PHY_CTL			0x10
47 
48 #define PHYCTL_DATA			BIT(7)
49 
50 #define OTGCTL_ROUTE_MUSB		BIT(0)
51 
52 #define SUNXI_AHB_ICHR8_EN		BIT(10)
53 #define SUNXI_AHB_INCR4_BURST_EN	BIT(9)
54 #define SUNXI_AHB_INCRX_ALIGN_EN	BIT(8)
55 #define SUNXI_ULPI_BYPASS_EN		BIT(0)
56 
57 /* ISCR, Interface Status and Control bits */
58 #define ISCR_ID_PULLUP_EN		(1 << 17)
59 #define ISCR_DPDM_PULLUP_EN	(1 << 16)
60 /* sunxi has the phy id/vbus pins not connected, so we use the force bits */
61 #define ISCR_FORCE_ID_MASK	(3 << 14)
62 #define ISCR_FORCE_ID_LOW		(2 << 14)
63 #define ISCR_FORCE_ID_HIGH	(3 << 14)
64 #define ISCR_FORCE_VBUS_MASK	(3 << 12)
65 #define ISCR_FORCE_VBUS_LOW	(2 << 12)
66 #define ISCR_FORCE_VBUS_HIGH	(3 << 12)
67 
68 /* Common Control Bits for Both PHYs */
69 #define PHY_PLL_BW			0x03
70 #define PHY_RES45_CAL_EN		0x0c
71 
72 /* Private Control Bits for Each PHY */
73 #define PHY_TX_AMPLITUDE_TUNE		0x20
74 #define PHY_TX_SLEWRATE_TUNE		0x22
75 #define PHY_VBUSVALID_TH_SEL		0x25
76 #define PHY_PULLUP_RES_SEL		0x27
77 #define PHY_OTG_FUNC_EN			0x28
78 #define PHY_VBUS_DET_EN			0x29
79 #define PHY_DISCON_TH_SEL		0x2a
80 #define PHY_SQUELCH_DETECT		0x3c
81 
82 /* A83T specific control bits for PHY0 */
83 #define PHY_CTL_VBUSVLDEXT		BIT(5)
84 #define PHY_CTL_SIDDQ			BIT(3)
85 #define PHY_CTL_H3_SIDDQ		BIT(1)
86 
87 /* A83T specific control bits for PHY2 HSIC */
88 #define SUNXI_EHCI_HS_FORCE		BIT(20)
89 #define SUNXI_HSIC_CONNECT_DET		BIT(17)
90 #define SUNXI_HSIC_CONNECT_INT		BIT(16)
91 #define SUNXI_HSIC			BIT(1)
92 
93 #define MAX_PHYS			4
94 
95 /*
96  * Note do not raise the debounce time, we must report Vusb high within 100ms
97  * otherwise we get Vbus errors
98  */
99 #define DEBOUNCE_TIME			msecs_to_jiffies(50)
100 #define POLL_TIME			msecs_to_jiffies(250)
101 
102 struct sun4i_usb_phy_cfg {
103 	int num_phys;
104 	int hsic_index;
105 	u32 disc_thresh;
106 	u32 hci_phy_ctl_clear;
107 	u8 phyctl_offset;
108 	bool dedicated_clocks;
109 	bool phy0_dual_route;
110 	bool needs_phy2_siddq;
111 	bool siddq_in_base;
112 	bool poll_vbusen;
113 	int missing_phys;
114 };
115 
116 struct sun4i_usb_phy_data {
117 	void __iomem *base;
118 	const struct sun4i_usb_phy_cfg *cfg;
119 	enum usb_dr_mode dr_mode;
120 	spinlock_t reg_lock; /* guard access to phyctl reg */
121 	struct sun4i_usb_phy {
122 		struct phy *phy;
123 		void __iomem *pmu;
124 		struct regulator *vbus;
125 		struct reset_control *reset;
126 		struct clk *clk;
127 		struct clk *clk2;
128 		bool regulator_on;
129 		int index;
130 	} phys[MAX_PHYS];
131 	/* phy0 / otg related variables */
132 	struct extcon_dev *extcon;
133 	bool phy0_init;
134 	struct gpio_desc *id_det_gpio;
135 	struct gpio_desc *vbus_det_gpio;
136 	struct power_supply *vbus_power_supply;
137 	struct notifier_block vbus_power_nb;
138 	bool vbus_power_nb_registered;
139 	bool force_session_end;
140 	int id_det_irq;
141 	int vbus_det_irq;
142 	int id_det;
143 	int vbus_det;
144 	struct delayed_work detect;
145 };
146 
147 #define to_sun4i_usb_phy_data(phy) \
148 	container_of((phy), struct sun4i_usb_phy_data, phys[(phy)->index])
149 
150 static void sun4i_usb_phy0_update_iscr(struct phy *_phy, u32 clr, u32 set)
151 {
152 	struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
153 	struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
154 	u32 iscr;
155 
156 	iscr = readl(data->base + REG_ISCR);
157 	iscr &= ~clr;
158 	iscr |= set;
159 	writel(iscr, data->base + REG_ISCR);
160 }
161 
162 static void sun4i_usb_phy0_set_id_detect(struct phy *phy, u32 val)
163 {
164 	if (val)
165 		val = ISCR_FORCE_ID_HIGH;
166 	else
167 		val = ISCR_FORCE_ID_LOW;
168 
169 	sun4i_usb_phy0_update_iscr(phy, ISCR_FORCE_ID_MASK, val);
170 }
171 
172 static void sun4i_usb_phy0_set_vbus_detect(struct phy *phy, u32 val)
173 {
174 	if (val)
175 		val = ISCR_FORCE_VBUS_HIGH;
176 	else
177 		val = ISCR_FORCE_VBUS_LOW;
178 
179 	sun4i_usb_phy0_update_iscr(phy, ISCR_FORCE_VBUS_MASK, val);
180 }
181 
182 static void sun4i_usb_phy_write(struct sun4i_usb_phy *phy, u32 addr, u32 data,
183 				int len)
184 {
185 	struct sun4i_usb_phy_data *phy_data = to_sun4i_usb_phy_data(phy);
186 	u32 temp, usbc_bit = BIT(phy->index * 2);
187 	void __iomem *phyctl = phy_data->base + phy_data->cfg->phyctl_offset;
188 	unsigned long flags;
189 	int i;
190 
191 	spin_lock_irqsave(&phy_data->reg_lock, flags);
192 
193 	if (phy_data->cfg->phyctl_offset == REG_PHYCTL_A33) {
194 		/* SoCs newer than A33 need us to set phyctl to 0 explicitly */
195 		writel(0, phyctl);
196 	}
197 
198 	for (i = 0; i < len; i++) {
199 		temp = readl(phyctl);
200 
201 		/* clear the address portion */
202 		temp &= ~(0xff << 8);
203 
204 		/* set the address */
205 		temp |= ((addr + i) << 8);
206 		writel(temp, phyctl);
207 
208 		/* set the data bit and clear usbc bit*/
209 		temp = readb(phyctl);
210 		if (data & 0x1)
211 			temp |= PHYCTL_DATA;
212 		else
213 			temp &= ~PHYCTL_DATA;
214 		temp &= ~usbc_bit;
215 		writeb(temp, phyctl);
216 
217 		/* pulse usbc_bit */
218 		temp = readb(phyctl);
219 		temp |= usbc_bit;
220 		writeb(temp, phyctl);
221 
222 		temp = readb(phyctl);
223 		temp &= ~usbc_bit;
224 		writeb(temp, phyctl);
225 
226 		data >>= 1;
227 	}
228 
229 	spin_unlock_irqrestore(&phy_data->reg_lock, flags);
230 }
231 
232 static void sun4i_usb_phy_passby(struct sun4i_usb_phy *phy, int enable)
233 {
234 	struct sun4i_usb_phy_data *phy_data = to_sun4i_usb_phy_data(phy);
235 	u32 bits, reg_value;
236 
237 	if (!phy->pmu)
238 		return;
239 
240 	bits = SUNXI_AHB_ICHR8_EN | SUNXI_AHB_INCR4_BURST_EN |
241 		SUNXI_AHB_INCRX_ALIGN_EN | SUNXI_ULPI_BYPASS_EN;
242 
243 	/* A83T USB2 is HSIC */
244 	if (phy_data->cfg->hsic_index &&
245 	    phy->index == phy_data->cfg->hsic_index)
246 		bits |= SUNXI_EHCI_HS_FORCE | SUNXI_HSIC_CONNECT_INT |
247 			SUNXI_HSIC;
248 
249 	reg_value = readl(phy->pmu);
250 
251 	if (enable)
252 		reg_value |= bits;
253 	else
254 		reg_value &= ~bits;
255 
256 	writel(reg_value, phy->pmu);
257 }
258 
259 static int sun4i_usb_phy_init(struct phy *_phy)
260 {
261 	struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
262 	struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
263 	int ret;
264 	u32 val;
265 
266 	ret = clk_prepare_enable(phy->clk);
267 	if (ret)
268 		return ret;
269 
270 	ret = clk_prepare_enable(phy->clk2);
271 	if (ret) {
272 		clk_disable_unprepare(phy->clk);
273 		return ret;
274 	}
275 
276 	ret = reset_control_deassert(phy->reset);
277 	if (ret) {
278 		clk_disable_unprepare(phy->clk2);
279 		clk_disable_unprepare(phy->clk);
280 		return ret;
281 	}
282 
283 	/* Some PHYs on some SoCs need the help of PHY2 to work. */
284 	if (data->cfg->needs_phy2_siddq && phy->index != 2) {
285 		struct sun4i_usb_phy *phy2 = &data->phys[2];
286 
287 		ret = clk_prepare_enable(phy2->clk);
288 		if (ret) {
289 			reset_control_assert(phy->reset);
290 			clk_disable_unprepare(phy->clk2);
291 			clk_disable_unprepare(phy->clk);
292 			return ret;
293 		}
294 
295 		ret = reset_control_deassert(phy2->reset);
296 		if (ret) {
297 			clk_disable_unprepare(phy2->clk);
298 			reset_control_assert(phy->reset);
299 			clk_disable_unprepare(phy->clk2);
300 			clk_disable_unprepare(phy->clk);
301 			return ret;
302 		}
303 
304 		/*
305 		 * This extra clock is just needed to access the
306 		 * REG_HCI_PHY_CTL PMU register for PHY2.
307 		 */
308 		ret = clk_prepare_enable(phy2->clk2);
309 		if (ret) {
310 			reset_control_assert(phy2->reset);
311 			clk_disable_unprepare(phy2->clk);
312 			reset_control_assert(phy->reset);
313 			clk_disable_unprepare(phy->clk2);
314 			clk_disable_unprepare(phy->clk);
315 			return ret;
316 		}
317 
318 		if (phy2->pmu && data->cfg->hci_phy_ctl_clear) {
319 			val = readl(phy2->pmu + REG_HCI_PHY_CTL);
320 			val &= ~data->cfg->hci_phy_ctl_clear;
321 			writel(val, phy2->pmu + REG_HCI_PHY_CTL);
322 		}
323 
324 		clk_disable_unprepare(phy->clk2);
325 	}
326 
327 	if (phy->pmu && data->cfg->hci_phy_ctl_clear) {
328 		val = readl(phy->pmu + REG_HCI_PHY_CTL);
329 		val &= ~data->cfg->hci_phy_ctl_clear;
330 		writel(val, phy->pmu + REG_HCI_PHY_CTL);
331 	}
332 
333 	if (data->cfg->siddq_in_base) {
334 		if (phy->index == 0) {
335 			val = readl(data->base + data->cfg->phyctl_offset);
336 			val |= PHY_CTL_VBUSVLDEXT;
337 			val &= ~PHY_CTL_SIDDQ;
338 			writel(val, data->base + data->cfg->phyctl_offset);
339 		}
340 	} else {
341 		/* Enable USB 45 Ohm resistor calibration */
342 		if (phy->index == 0)
343 			sun4i_usb_phy_write(phy, PHY_RES45_CAL_EN, 0x01, 1);
344 
345 		/* Adjust PHY's magnitude and rate */
346 		sun4i_usb_phy_write(phy, PHY_TX_AMPLITUDE_TUNE, 0x14, 5);
347 
348 		/* Disconnect threshold adjustment */
349 		sun4i_usb_phy_write(phy, PHY_DISCON_TH_SEL,
350 				    data->cfg->disc_thresh, 2);
351 	}
352 
353 	sun4i_usb_phy_passby(phy, 1);
354 
355 	if (phy->index == 0) {
356 		data->phy0_init = true;
357 
358 		/* Enable pull-ups */
359 		sun4i_usb_phy0_update_iscr(_phy, 0, ISCR_DPDM_PULLUP_EN);
360 		sun4i_usb_phy0_update_iscr(_phy, 0, ISCR_ID_PULLUP_EN);
361 
362 		/* Force ISCR and cable state updates */
363 		data->id_det = -1;
364 		data->vbus_det = -1;
365 		queue_delayed_work(system_wq, &data->detect, 0);
366 	}
367 
368 	return 0;
369 }
370 
371 static int sun4i_usb_phy_exit(struct phy *_phy)
372 {
373 	struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
374 	struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
375 
376 	if (phy->index == 0) {
377 		if (data->cfg->siddq_in_base) {
378 			void __iomem *phyctl = data->base +
379 				data->cfg->phyctl_offset;
380 
381 			writel(readl(phyctl) | PHY_CTL_SIDDQ, phyctl);
382 		}
383 
384 		/* Disable pull-ups */
385 		sun4i_usb_phy0_update_iscr(_phy, ISCR_DPDM_PULLUP_EN, 0);
386 		sun4i_usb_phy0_update_iscr(_phy, ISCR_ID_PULLUP_EN, 0);
387 		data->phy0_init = false;
388 	}
389 
390 	if (data->cfg->needs_phy2_siddq && phy->index != 2) {
391 		struct sun4i_usb_phy *phy2 = &data->phys[2];
392 
393 		clk_disable_unprepare(phy2->clk);
394 		reset_control_assert(phy2->reset);
395 	}
396 
397 	sun4i_usb_phy_passby(phy, 0);
398 	reset_control_assert(phy->reset);
399 	clk_disable_unprepare(phy->clk2);
400 	clk_disable_unprepare(phy->clk);
401 
402 	return 0;
403 }
404 
405 static int sun4i_usb_phy0_get_id_det(struct sun4i_usb_phy_data *data)
406 {
407 	switch (data->dr_mode) {
408 	case USB_DR_MODE_OTG:
409 		if (data->id_det_gpio)
410 			return gpiod_get_value_cansleep(data->id_det_gpio);
411 		else
412 			return 1; /* Fallback to peripheral mode */
413 	case USB_DR_MODE_HOST:
414 		return 0;
415 	case USB_DR_MODE_PERIPHERAL:
416 	default:
417 		return 1;
418 	}
419 }
420 
421 static int sun4i_usb_phy0_get_vbus_det(struct sun4i_usb_phy_data *data)
422 {
423 	if (data->vbus_det_gpio)
424 		return gpiod_get_value_cansleep(data->vbus_det_gpio);
425 
426 	if (data->vbus_power_supply) {
427 		union power_supply_propval val;
428 		int r;
429 
430 		r = power_supply_get_property(data->vbus_power_supply,
431 					      POWER_SUPPLY_PROP_PRESENT, &val);
432 		if (r == 0)
433 			return val.intval;
434 	}
435 
436 	/* Fallback: report vbus as high */
437 	return 1;
438 }
439 
440 static bool sun4i_usb_phy0_have_vbus_det(struct sun4i_usb_phy_data *data)
441 {
442 	return data->vbus_det_gpio || data->vbus_power_supply;
443 }
444 
445 static bool sun4i_usb_phy0_poll(struct sun4i_usb_phy_data *data)
446 {
447 	if ((data->id_det_gpio && data->id_det_irq <= 0) ||
448 	    (data->vbus_det_gpio && data->vbus_det_irq <= 0))
449 		return true;
450 
451 	/*
452 	 * The A31/A23/A33 companion pmics (AXP221/AXP223) do not
453 	 * generate vbus change interrupts when the board is driving
454 	 * vbus using the N_VBUSEN pin on the pmic, so we must poll
455 	 * when using the pmic for vbus-det _and_ we're driving vbus.
456 	 */
457 	if (data->cfg->poll_vbusen && data->vbus_power_supply &&
458 	    data->phys[0].regulator_on)
459 		return true;
460 
461 	return false;
462 }
463 
464 static int sun4i_usb_phy_power_on(struct phy *_phy)
465 {
466 	struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
467 	struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
468 	int ret;
469 
470 	if (!phy->vbus || phy->regulator_on)
471 		return 0;
472 
473 	/* For phy0 only turn on Vbus if we don't have an ext. Vbus */
474 	if (phy->index == 0 && sun4i_usb_phy0_have_vbus_det(data) &&
475 				data->vbus_det) {
476 		dev_warn(&_phy->dev, "External vbus detected, not enabling our own vbus\n");
477 		return 0;
478 	}
479 
480 	ret = regulator_enable(phy->vbus);
481 	if (ret)
482 		return ret;
483 
484 	phy->regulator_on = true;
485 
486 	/* We must report Vbus high within OTG_TIME_A_WAIT_VRISE msec. */
487 	if (phy->index == 0 && sun4i_usb_phy0_poll(data))
488 		mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
489 
490 	return 0;
491 }
492 
493 static int sun4i_usb_phy_power_off(struct phy *_phy)
494 {
495 	struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
496 	struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
497 
498 	if (!phy->vbus || !phy->regulator_on)
499 		return 0;
500 
501 	regulator_disable(phy->vbus);
502 	phy->regulator_on = false;
503 
504 	/*
505 	 * phy0 vbus typically slowly discharges, sometimes this causes the
506 	 * Vbus gpio to not trigger an edge irq on Vbus off, so force a rescan.
507 	 */
508 	if (phy->index == 0 && !sun4i_usb_phy0_poll(data))
509 		mod_delayed_work(system_wq, &data->detect, POLL_TIME);
510 
511 	return 0;
512 }
513 
514 static int sun4i_usb_phy_set_mode(struct phy *_phy,
515 				  enum phy_mode mode, int submode)
516 {
517 	struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
518 	struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
519 	int new_mode;
520 
521 	if (phy->index != 0) {
522 		if (mode == PHY_MODE_USB_HOST)
523 			return 0;
524 		return -EINVAL;
525 	}
526 
527 	switch (mode) {
528 	case PHY_MODE_USB_HOST:
529 		new_mode = USB_DR_MODE_HOST;
530 		break;
531 	case PHY_MODE_USB_DEVICE:
532 		new_mode = USB_DR_MODE_PERIPHERAL;
533 		break;
534 	case PHY_MODE_USB_OTG:
535 		new_mode = USB_DR_MODE_OTG;
536 		break;
537 	default:
538 		return -EINVAL;
539 	}
540 
541 	if (new_mode != data->dr_mode) {
542 		dev_info(&_phy->dev, "Changing dr_mode to %d\n", new_mode);
543 		data->dr_mode = new_mode;
544 	}
545 
546 	data->id_det = -1; /* Force reprocessing of id */
547 	data->force_session_end = true;
548 	queue_delayed_work(system_wq, &data->detect, 0);
549 
550 	return 0;
551 }
552 
553 void sun4i_usb_phy_set_squelch_detect(struct phy *_phy, bool enabled)
554 {
555 	struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
556 
557 	sun4i_usb_phy_write(phy, PHY_SQUELCH_DETECT, enabled ? 0 : 2, 2);
558 }
559 EXPORT_SYMBOL_GPL(sun4i_usb_phy_set_squelch_detect);
560 
561 static const struct phy_ops sun4i_usb_phy_ops = {
562 	.init		= sun4i_usb_phy_init,
563 	.exit		= sun4i_usb_phy_exit,
564 	.power_on	= sun4i_usb_phy_power_on,
565 	.power_off	= sun4i_usb_phy_power_off,
566 	.set_mode	= sun4i_usb_phy_set_mode,
567 	.owner		= THIS_MODULE,
568 };
569 
570 static void sun4i_usb_phy0_reroute(struct sun4i_usb_phy_data *data, int id_det)
571 {
572 	u32 regval;
573 
574 	regval = readl(data->base + REG_PHY_OTGCTL);
575 	if (id_det == 0) {
576 		/* Host mode. Route phy0 to EHCI/OHCI */
577 		regval &= ~OTGCTL_ROUTE_MUSB;
578 	} else {
579 		/* Peripheral mode. Route phy0 to MUSB */
580 		regval |= OTGCTL_ROUTE_MUSB;
581 	}
582 	writel(regval, data->base + REG_PHY_OTGCTL);
583 }
584 
585 static void sun4i_usb_phy0_id_vbus_det_scan(struct work_struct *work)
586 {
587 	struct sun4i_usb_phy_data *data =
588 		container_of(work, struct sun4i_usb_phy_data, detect.work);
589 	struct phy *phy0 = data->phys[0].phy;
590 	struct sun4i_usb_phy *phy;
591 	bool force_session_end, id_notify = false, vbus_notify = false;
592 	int id_det, vbus_det;
593 
594 	if (!phy0)
595 		return;
596 
597 	phy = phy_get_drvdata(phy0);
598 	id_det = sun4i_usb_phy0_get_id_det(data);
599 	vbus_det = sun4i_usb_phy0_get_vbus_det(data);
600 
601 	mutex_lock(&phy0->mutex);
602 
603 	if (!data->phy0_init) {
604 		mutex_unlock(&phy0->mutex);
605 		return;
606 	}
607 
608 	force_session_end = data->force_session_end;
609 	data->force_session_end = false;
610 
611 	if (id_det != data->id_det) {
612 		/* id-change, force session end if we've no vbus detection */
613 		if (data->dr_mode == USB_DR_MODE_OTG &&
614 		    !sun4i_usb_phy0_have_vbus_det(data))
615 			force_session_end = true;
616 
617 		/* When entering host mode (id = 0) force end the session now */
618 		if (force_session_end && id_det == 0) {
619 			sun4i_usb_phy0_set_vbus_detect(phy0, 0);
620 			msleep(200);
621 			sun4i_usb_phy0_set_vbus_detect(phy0, 1);
622 		}
623 		sun4i_usb_phy0_set_id_detect(phy0, id_det);
624 		data->id_det = id_det;
625 		id_notify = true;
626 	}
627 
628 	if (vbus_det != data->vbus_det) {
629 		sun4i_usb_phy0_set_vbus_detect(phy0, vbus_det);
630 		data->vbus_det = vbus_det;
631 		vbus_notify = true;
632 	}
633 
634 	mutex_unlock(&phy0->mutex);
635 
636 	if (id_notify) {
637 		extcon_set_state_sync(data->extcon, EXTCON_USB_HOST,
638 					!id_det);
639 		/* When leaving host mode force end the session here */
640 		if (force_session_end && id_det == 1) {
641 			mutex_lock(&phy0->mutex);
642 			sun4i_usb_phy0_set_vbus_detect(phy0, 0);
643 			msleep(1000);
644 			sun4i_usb_phy0_set_vbus_detect(phy0, 1);
645 			mutex_unlock(&phy0->mutex);
646 		}
647 
648 		/* Enable PHY0 passby for host mode only. */
649 		sun4i_usb_phy_passby(phy, !id_det);
650 
651 		/* Re-route PHY0 if necessary */
652 		if (data->cfg->phy0_dual_route)
653 			sun4i_usb_phy0_reroute(data, id_det);
654 	}
655 
656 	if (vbus_notify)
657 		extcon_set_state_sync(data->extcon, EXTCON_USB, vbus_det);
658 
659 	if (sun4i_usb_phy0_poll(data))
660 		queue_delayed_work(system_wq, &data->detect, POLL_TIME);
661 }
662 
663 static irqreturn_t sun4i_usb_phy0_id_vbus_det_irq(int irq, void *dev_id)
664 {
665 	struct sun4i_usb_phy_data *data = dev_id;
666 
667 	/* vbus or id changed, let the pins settle and then scan them */
668 	mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
669 
670 	return IRQ_HANDLED;
671 }
672 
673 static int sun4i_usb_phy0_vbus_notify(struct notifier_block *nb,
674 				      unsigned long val, void *v)
675 {
676 	struct sun4i_usb_phy_data *data =
677 		container_of(nb, struct sun4i_usb_phy_data, vbus_power_nb);
678 	struct power_supply *psy = v;
679 
680 	/* Properties on the vbus_power_supply changed, scan vbus_det */
681 	if (val == PSY_EVENT_PROP_CHANGED && psy == data->vbus_power_supply)
682 		mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
683 
684 	return NOTIFY_OK;
685 }
686 
687 static struct phy *sun4i_usb_phy_xlate(struct device *dev,
688 					struct of_phandle_args *args)
689 {
690 	struct sun4i_usb_phy_data *data = dev_get_drvdata(dev);
691 
692 	if (args->args[0] >= data->cfg->num_phys)
693 		return ERR_PTR(-ENODEV);
694 
695 	if (data->cfg->missing_phys & BIT(args->args[0]))
696 		return ERR_PTR(-ENODEV);
697 
698 	return data->phys[args->args[0]].phy;
699 }
700 
701 static void sun4i_usb_phy_remove(struct platform_device *pdev)
702 {
703 	struct device *dev = &pdev->dev;
704 	struct sun4i_usb_phy_data *data = dev_get_drvdata(dev);
705 
706 	if (data->vbus_power_nb_registered)
707 		power_supply_unreg_notifier(&data->vbus_power_nb);
708 	if (data->id_det_irq > 0)
709 		devm_free_irq(dev, data->id_det_irq, data);
710 	if (data->vbus_det_irq > 0)
711 		devm_free_irq(dev, data->vbus_det_irq, data);
712 
713 	cancel_delayed_work_sync(&data->detect);
714 }
715 
716 static const unsigned int sun4i_usb_phy0_cable[] = {
717 	EXTCON_USB,
718 	EXTCON_USB_HOST,
719 	EXTCON_NONE,
720 };
721 
722 static int sun4i_usb_phy_probe(struct platform_device *pdev)
723 {
724 	struct sun4i_usb_phy_data *data;
725 	struct device *dev = &pdev->dev;
726 	struct device_node *np = dev->of_node;
727 	struct phy_provider *phy_provider;
728 	int i, ret;
729 
730 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
731 	if (!data)
732 		return -ENOMEM;
733 
734 	spin_lock_init(&data->reg_lock);
735 	INIT_DELAYED_WORK(&data->detect, sun4i_usb_phy0_id_vbus_det_scan);
736 	dev_set_drvdata(dev, data);
737 	data->cfg = of_device_get_match_data(dev);
738 	if (!data->cfg)
739 		return -EINVAL;
740 
741 	data->base = devm_platform_ioremap_resource_byname(pdev, "phy_ctrl");
742 	if (IS_ERR(data->base))
743 		return PTR_ERR(data->base);
744 
745 	data->id_det_gpio = devm_gpiod_get_optional(dev, "usb0_id_det",
746 						    GPIOD_IN);
747 	if (IS_ERR(data->id_det_gpio)) {
748 		dev_err(dev, "Couldn't request ID GPIO\n");
749 		return PTR_ERR(data->id_det_gpio);
750 	}
751 
752 	data->vbus_det_gpio = devm_gpiod_get_optional(dev, "usb0_vbus_det",
753 						      GPIOD_IN);
754 	if (IS_ERR(data->vbus_det_gpio)) {
755 		dev_err(dev, "Couldn't request VBUS detect GPIO\n");
756 		return PTR_ERR(data->vbus_det_gpio);
757 	}
758 
759 	if (of_property_present(np, "usb0_vbus_power-supply")) {
760 		data->vbus_power_supply = devm_power_supply_get_by_phandle(dev,
761 						     "usb0_vbus_power-supply");
762 		if (IS_ERR(data->vbus_power_supply)) {
763 			dev_err(dev, "Couldn't get the VBUS power supply\n");
764 			return PTR_ERR(data->vbus_power_supply);
765 		}
766 
767 		if (!data->vbus_power_supply)
768 			return -EPROBE_DEFER;
769 	}
770 
771 	data->dr_mode = of_usb_get_dr_mode_by_phy(np, 0);
772 
773 	data->extcon = devm_extcon_dev_allocate(dev, sun4i_usb_phy0_cable);
774 	if (IS_ERR(data->extcon)) {
775 		dev_err(dev, "Couldn't allocate our extcon device\n");
776 		return PTR_ERR(data->extcon);
777 	}
778 
779 	ret = devm_extcon_dev_register(dev, data->extcon);
780 	if (ret) {
781 		dev_err(dev, "failed to register extcon: %d\n", ret);
782 		return ret;
783 	}
784 
785 	for (i = 0; i < data->cfg->num_phys; i++) {
786 		struct sun4i_usb_phy *phy = data->phys + i;
787 		char name[16];
788 
789 		if (data->cfg->missing_phys & BIT(i))
790 			continue;
791 
792 		snprintf(name, sizeof(name), "usb%d_vbus", i);
793 		phy->vbus = devm_regulator_get_optional(dev, name);
794 		if (IS_ERR(phy->vbus)) {
795 			if (PTR_ERR(phy->vbus) == -EPROBE_DEFER) {
796 				dev_err(dev,
797 					"Couldn't get regulator %s... Deferring probe\n",
798 					name);
799 				return -EPROBE_DEFER;
800 			}
801 
802 			phy->vbus = NULL;
803 		}
804 
805 		if (data->cfg->dedicated_clocks)
806 			snprintf(name, sizeof(name), "usb%d_phy", i);
807 		else
808 			strscpy(name, "usb_phy", sizeof(name));
809 
810 		phy->clk = devm_clk_get(dev, name);
811 		if (IS_ERR(phy->clk)) {
812 			dev_err(dev, "failed to get clock %s\n", name);
813 			return PTR_ERR(phy->clk);
814 		}
815 
816 		/* The first PHY is always tied to OTG, and never HSIC */
817 		if (data->cfg->hsic_index && i == data->cfg->hsic_index) {
818 			/* HSIC needs secondary clock */
819 			snprintf(name, sizeof(name), "usb%d_hsic_12M", i);
820 			phy->clk2 = devm_clk_get(dev, name);
821 			if (IS_ERR(phy->clk2)) {
822 				dev_err(dev, "failed to get clock %s\n", name);
823 				return PTR_ERR(phy->clk2);
824 			}
825 		} else {
826 			snprintf(name, sizeof(name), "pmu%d_clk", i);
827 			phy->clk2 = devm_clk_get_optional(dev, name);
828 			if (IS_ERR(phy->clk2)) {
829 				dev_err(dev, "failed to get clock %s\n", name);
830 				return PTR_ERR(phy->clk2);
831 			}
832 		}
833 
834 		snprintf(name, sizeof(name), "usb%d_reset", i);
835 		phy->reset = devm_reset_control_get(dev, name);
836 		if (IS_ERR(phy->reset)) {
837 			dev_err(dev, "failed to get reset %s\n", name);
838 			return PTR_ERR(phy->reset);
839 		}
840 
841 		if (i || data->cfg->phy0_dual_route) { /* No pmu for musb */
842 			snprintf(name, sizeof(name), "pmu%d", i);
843 			phy->pmu = devm_platform_ioremap_resource_byname(pdev, name);
844 			if (IS_ERR(phy->pmu))
845 				return PTR_ERR(phy->pmu);
846 		}
847 
848 		phy->phy = devm_phy_create(dev, NULL, &sun4i_usb_phy_ops);
849 		if (IS_ERR(phy->phy)) {
850 			dev_err(dev, "failed to create PHY %d\n", i);
851 			return PTR_ERR(phy->phy);
852 		}
853 
854 		phy->index = i;
855 		phy_set_drvdata(phy->phy, &data->phys[i]);
856 	}
857 
858 	data->id_det_irq = gpiod_to_irq(data->id_det_gpio);
859 	if (data->id_det_irq > 0) {
860 		ret = devm_request_irq(dev, data->id_det_irq,
861 				sun4i_usb_phy0_id_vbus_det_irq,
862 				IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
863 				"usb0-id-det", data);
864 		if (ret) {
865 			dev_err(dev, "Err requesting id-det-irq: %d\n", ret);
866 			return ret;
867 		}
868 	}
869 
870 	data->vbus_det_irq = gpiod_to_irq(data->vbus_det_gpio);
871 	if (data->vbus_det_irq > 0) {
872 		ret = devm_request_irq(dev, data->vbus_det_irq,
873 				sun4i_usb_phy0_id_vbus_det_irq,
874 				IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
875 				"usb0-vbus-det", data);
876 		if (ret) {
877 			dev_err(dev, "Err requesting vbus-det-irq: %d\n", ret);
878 			data->vbus_det_irq = -1;
879 			sun4i_usb_phy_remove(pdev); /* Stop detect work */
880 			return ret;
881 		}
882 	}
883 
884 	if (data->vbus_power_supply) {
885 		data->vbus_power_nb.notifier_call = sun4i_usb_phy0_vbus_notify;
886 		data->vbus_power_nb.priority = 0;
887 		ret = power_supply_reg_notifier(&data->vbus_power_nb);
888 		if (ret) {
889 			sun4i_usb_phy_remove(pdev); /* Stop detect work */
890 			return ret;
891 		}
892 		data->vbus_power_nb_registered = true;
893 	}
894 
895 	phy_provider = devm_of_phy_provider_register(dev, sun4i_usb_phy_xlate);
896 	if (IS_ERR(phy_provider)) {
897 		sun4i_usb_phy_remove(pdev); /* Stop detect work */
898 		return PTR_ERR(phy_provider);
899 	}
900 
901 	dev_dbg(dev, "successfully loaded\n");
902 
903 	return 0;
904 }
905 
906 static const struct sun4i_usb_phy_cfg suniv_f1c100s_cfg = {
907 	.num_phys = 1,
908 	.disc_thresh = 3,
909 	.phyctl_offset = REG_PHYCTL_A10,
910 	.dedicated_clocks = true,
911 };
912 
913 static const struct sun4i_usb_phy_cfg sun4i_a10_cfg = {
914 	.num_phys = 3,
915 	.disc_thresh = 3,
916 	.phyctl_offset = REG_PHYCTL_A10,
917 	.dedicated_clocks = false,
918 };
919 
920 static const struct sun4i_usb_phy_cfg sun5i_a13_cfg = {
921 	.num_phys = 2,
922 	.disc_thresh = 2,
923 	.phyctl_offset = REG_PHYCTL_A10,
924 	.dedicated_clocks = false,
925 };
926 
927 static const struct sun4i_usb_phy_cfg sun6i_a31_cfg = {
928 	.num_phys = 3,
929 	.disc_thresh = 3,
930 	.phyctl_offset = REG_PHYCTL_A10,
931 	.dedicated_clocks = true,
932 	.poll_vbusen = true,
933 };
934 
935 static const struct sun4i_usb_phy_cfg sun7i_a20_cfg = {
936 	.num_phys = 3,
937 	.disc_thresh = 2,
938 	.phyctl_offset = REG_PHYCTL_A10,
939 	.dedicated_clocks = false,
940 };
941 
942 static const struct sun4i_usb_phy_cfg sun8i_a23_cfg = {
943 	.num_phys = 2,
944 	.disc_thresh = 3,
945 	.phyctl_offset = REG_PHYCTL_A10,
946 	.dedicated_clocks = true,
947 	.poll_vbusen = true,
948 };
949 
950 static const struct sun4i_usb_phy_cfg sun8i_a33_cfg = {
951 	.num_phys = 2,
952 	.disc_thresh = 3,
953 	.phyctl_offset = REG_PHYCTL_A33,
954 	.dedicated_clocks = true,
955 	.poll_vbusen = true,
956 };
957 
958 static const struct sun4i_usb_phy_cfg sun8i_a83t_cfg = {
959 	.num_phys = 3,
960 	.hsic_index = 2,
961 	.phyctl_offset = REG_PHYCTL_A33,
962 	.dedicated_clocks = true,
963 	.siddq_in_base = true,
964 };
965 
966 static const struct sun4i_usb_phy_cfg sun8i_h3_cfg = {
967 	.num_phys = 4,
968 	.disc_thresh = 3,
969 	.phyctl_offset = REG_PHYCTL_A33,
970 	.dedicated_clocks = true,
971 	.hci_phy_ctl_clear = PHY_CTL_H3_SIDDQ,
972 	.phy0_dual_route = true,
973 };
974 
975 static const struct sun4i_usb_phy_cfg sun8i_r40_cfg = {
976 	.num_phys = 3,
977 	.disc_thresh = 3,
978 	.phyctl_offset = REG_PHYCTL_A33,
979 	.dedicated_clocks = true,
980 	.hci_phy_ctl_clear = PHY_CTL_H3_SIDDQ,
981 	.phy0_dual_route = true,
982 };
983 
984 static const struct sun4i_usb_phy_cfg sun8i_v3s_cfg = {
985 	.num_phys = 1,
986 	.disc_thresh = 3,
987 	.phyctl_offset = REG_PHYCTL_A33,
988 	.dedicated_clocks = true,
989 	.hci_phy_ctl_clear = PHY_CTL_H3_SIDDQ,
990 	.phy0_dual_route = true,
991 };
992 
993 static const struct sun4i_usb_phy_cfg sun20i_d1_cfg = {
994 	.num_phys = 2,
995 	.phyctl_offset = REG_PHYCTL_A33,
996 	.dedicated_clocks = true,
997 	.hci_phy_ctl_clear = PHY_CTL_SIDDQ,
998 	.phy0_dual_route = true,
999 	.siddq_in_base = true,
1000 };
1001 
1002 static const struct sun4i_usb_phy_cfg sun50i_a64_cfg = {
1003 	.num_phys = 2,
1004 	.disc_thresh = 3,
1005 	.phyctl_offset = REG_PHYCTL_A33,
1006 	.dedicated_clocks = true,
1007 	.hci_phy_ctl_clear = PHY_CTL_H3_SIDDQ,
1008 	.phy0_dual_route = true,
1009 };
1010 
1011 static const struct sun4i_usb_phy_cfg sun50i_h6_cfg = {
1012 	.num_phys = 4,
1013 	.phyctl_offset = REG_PHYCTL_A33,
1014 	.dedicated_clocks = true,
1015 	.phy0_dual_route = true,
1016 	.missing_phys = BIT(1) | BIT(2),
1017 	.siddq_in_base = true,
1018 };
1019 
1020 static const struct sun4i_usb_phy_cfg sun50i_h616_cfg = {
1021 	.num_phys = 4,
1022 	.disc_thresh = 3,
1023 	.phyctl_offset = REG_PHYCTL_A33,
1024 	.dedicated_clocks = true,
1025 	.phy0_dual_route = true,
1026 	.hci_phy_ctl_clear = PHY_CTL_SIDDQ,
1027 	.needs_phy2_siddq = true,
1028 	.siddq_in_base = true,
1029 };
1030 
1031 static const struct of_device_id sun4i_usb_phy_of_match[] = {
1032 	{ .compatible = "allwinner,sun4i-a10-usb-phy", .data = &sun4i_a10_cfg },
1033 	{ .compatible = "allwinner,sun5i-a13-usb-phy", .data = &sun5i_a13_cfg },
1034 	{ .compatible = "allwinner,sun6i-a31-usb-phy", .data = &sun6i_a31_cfg },
1035 	{ .compatible = "allwinner,sun7i-a20-usb-phy", .data = &sun7i_a20_cfg },
1036 	{ .compatible = "allwinner,sun8i-a23-usb-phy", .data = &sun8i_a23_cfg },
1037 	{ .compatible = "allwinner,sun8i-a33-usb-phy", .data = &sun8i_a33_cfg },
1038 	{ .compatible = "allwinner,sun8i-a83t-usb-phy", .data = &sun8i_a83t_cfg },
1039 	{ .compatible = "allwinner,sun8i-h3-usb-phy", .data = &sun8i_h3_cfg },
1040 	{ .compatible = "allwinner,sun8i-r40-usb-phy", .data = &sun8i_r40_cfg },
1041 	{ .compatible = "allwinner,sun8i-v3s-usb-phy", .data = &sun8i_v3s_cfg },
1042 	{ .compatible = "allwinner,sun20i-d1-usb-phy", .data = &sun20i_d1_cfg },
1043 	{ .compatible = "allwinner,sun50i-a64-usb-phy",
1044 	  .data = &sun50i_a64_cfg},
1045 	{ .compatible = "allwinner,sun50i-h6-usb-phy", .data = &sun50i_h6_cfg },
1046 	{ .compatible = "allwinner,sun50i-h616-usb-phy", .data = &sun50i_h616_cfg },
1047 	{ .compatible = "allwinner,suniv-f1c100s-usb-phy",
1048 	  .data = &suniv_f1c100s_cfg },
1049 	{ },
1050 };
1051 MODULE_DEVICE_TABLE(of, sun4i_usb_phy_of_match);
1052 
1053 static struct platform_driver sun4i_usb_phy_driver = {
1054 	.probe	= sun4i_usb_phy_probe,
1055 	.remove_new = sun4i_usb_phy_remove,
1056 	.driver = {
1057 		.of_match_table	= sun4i_usb_phy_of_match,
1058 		.name  = "sun4i-usb-phy",
1059 	}
1060 };
1061 module_platform_driver(sun4i_usb_phy_driver);
1062 
1063 MODULE_DESCRIPTION("Allwinner sun4i USB phy driver");
1064 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
1065 MODULE_LICENSE("GPL v2");
1066