xref: /linux/drivers/phy/allwinner/phy-sun4i-usb.c (revision 79790b6818e96c58fe2bffee1b418c16e64e7b80)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
20b56e9a7SVivek Gautam /*
30b56e9a7SVivek Gautam  * Allwinner sun4i USB phy driver
40b56e9a7SVivek Gautam  *
50b56e9a7SVivek Gautam  * Copyright (C) 2014-2015 Hans de Goede <hdegoede@redhat.com>
60b56e9a7SVivek Gautam  *
70b56e9a7SVivek Gautam  * Based on code from
80b56e9a7SVivek Gautam  * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
90b56e9a7SVivek Gautam  *
10c233a2edSKrzysztof Kozlowski  * Modelled after: Samsung S5P/Exynos SoC series MIPI CSIS/DSIM DPHY driver
110b56e9a7SVivek Gautam  * Copyright (C) 2013 Samsung Electronics Co., Ltd.
120b56e9a7SVivek Gautam  * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
130b56e9a7SVivek Gautam  */
140b56e9a7SVivek Gautam 
150b56e9a7SVivek Gautam #include <linux/clk.h>
160b56e9a7SVivek Gautam #include <linux/delay.h>
170b56e9a7SVivek Gautam #include <linux/err.h>
18176aa360SChanwoo Choi #include <linux/extcon-provider.h>
198b34a289SVinod Koul #include <linux/gpio/consumer.h>
200b56e9a7SVivek Gautam #include <linux/io.h>
210b56e9a7SVivek Gautam #include <linux/interrupt.h>
220b56e9a7SVivek Gautam #include <linux/kernel.h>
230b56e9a7SVivek Gautam #include <linux/module.h>
240b56e9a7SVivek Gautam #include <linux/mutex.h>
250b56e9a7SVivek Gautam #include <linux/of.h>
260b56e9a7SVivek Gautam #include <linux/of_gpio.h>
270b56e9a7SVivek Gautam #include <linux/phy/phy.h>
280b56e9a7SVivek Gautam #include <linux/phy/phy-sun4i-usb.h>
290b56e9a7SVivek Gautam #include <linux/platform_device.h>
300b56e9a7SVivek Gautam #include <linux/power_supply.h>
310b56e9a7SVivek Gautam #include <linux/regulator/consumer.h>
320b56e9a7SVivek Gautam #include <linux/reset.h>
330b56e9a7SVivek Gautam #include <linux/spinlock.h>
340b56e9a7SVivek Gautam #include <linux/usb/of.h>
350b56e9a7SVivek Gautam #include <linux/workqueue.h>
360b56e9a7SVivek Gautam 
370b56e9a7SVivek Gautam #define REG_ISCR			0x00
380b56e9a7SVivek Gautam #define REG_PHYCTL_A10			0x04
390b56e9a7SVivek Gautam #define REG_PHYBIST			0x08
400b56e9a7SVivek Gautam #define REG_PHYTUNE			0x0c
410b56e9a7SVivek Gautam #define REG_PHYCTL_A33			0x10
420b56e9a7SVivek Gautam #define REG_PHY_OTGCTL			0x20
430b56e9a7SVivek Gautam 
441743dea7SAndre Przywara #define REG_HCI_PHY_CTL			0x10
450b56e9a7SVivek Gautam 
460b56e9a7SVivek Gautam #define PHYCTL_DATA			BIT(7)
470b56e9a7SVivek Gautam 
480b56e9a7SVivek Gautam #define OTGCTL_ROUTE_MUSB		BIT(0)
490b56e9a7SVivek Gautam 
500b56e9a7SVivek Gautam #define SUNXI_AHB_ICHR8_EN		BIT(10)
510b56e9a7SVivek Gautam #define SUNXI_AHB_INCR4_BURST_EN	BIT(9)
520b56e9a7SVivek Gautam #define SUNXI_AHB_INCRX_ALIGN_EN	BIT(8)
530b56e9a7SVivek Gautam #define SUNXI_ULPI_BYPASS_EN		BIT(0)
540b56e9a7SVivek Gautam 
550b56e9a7SVivek Gautam /* ISCR, Interface Status and Control bits */
560b56e9a7SVivek Gautam #define ISCR_ID_PULLUP_EN		(1 << 17)
570b56e9a7SVivek Gautam #define ISCR_DPDM_PULLUP_EN	(1 << 16)
580b56e9a7SVivek Gautam /* sunxi has the phy id/vbus pins not connected, so we use the force bits */
590b56e9a7SVivek Gautam #define ISCR_FORCE_ID_MASK	(3 << 14)
600b56e9a7SVivek Gautam #define ISCR_FORCE_ID_LOW		(2 << 14)
610b56e9a7SVivek Gautam #define ISCR_FORCE_ID_HIGH	(3 << 14)
620b56e9a7SVivek Gautam #define ISCR_FORCE_VBUS_MASK	(3 << 12)
630b56e9a7SVivek Gautam #define ISCR_FORCE_VBUS_LOW	(2 << 12)
640b56e9a7SVivek Gautam #define ISCR_FORCE_VBUS_HIGH	(3 << 12)
650b56e9a7SVivek Gautam 
660b56e9a7SVivek Gautam /* Common Control Bits for Both PHYs */
670b56e9a7SVivek Gautam #define PHY_PLL_BW			0x03
680b56e9a7SVivek Gautam #define PHY_RES45_CAL_EN		0x0c
690b56e9a7SVivek Gautam 
700b56e9a7SVivek Gautam /* Private Control Bits for Each PHY */
710b56e9a7SVivek Gautam #define PHY_TX_AMPLITUDE_TUNE		0x20
720b56e9a7SVivek Gautam #define PHY_TX_SLEWRATE_TUNE		0x22
730b56e9a7SVivek Gautam #define PHY_VBUSVALID_TH_SEL		0x25
740b56e9a7SVivek Gautam #define PHY_PULLUP_RES_SEL		0x27
750b56e9a7SVivek Gautam #define PHY_OTG_FUNC_EN			0x28
760b56e9a7SVivek Gautam #define PHY_VBUS_DET_EN			0x29
770b56e9a7SVivek Gautam #define PHY_DISCON_TH_SEL		0x2a
780b56e9a7SVivek Gautam #define PHY_SQUELCH_DETECT		0x3c
790b56e9a7SVivek Gautam 
804b63743cSChen-Yu Tsai /* A83T specific control bits for PHY0 */
814b63743cSChen-Yu Tsai #define PHY_CTL_VBUSVLDEXT		BIT(5)
824b63743cSChen-Yu Tsai #define PHY_CTL_SIDDQ			BIT(3)
831743dea7SAndre Przywara #define PHY_CTL_H3_SIDDQ		BIT(1)
844b63743cSChen-Yu Tsai 
854b63743cSChen-Yu Tsai /* A83T specific control bits for PHY2 HSIC */
864b63743cSChen-Yu Tsai #define SUNXI_EHCI_HS_FORCE		BIT(20)
874b63743cSChen-Yu Tsai #define SUNXI_HSIC_CONNECT_DET		BIT(17)
884b63743cSChen-Yu Tsai #define SUNXI_HSIC_CONNECT_INT		BIT(16)
894b63743cSChen-Yu Tsai #define SUNXI_HSIC			BIT(1)
904b63743cSChen-Yu Tsai 
910b56e9a7SVivek Gautam #define MAX_PHYS			4
920b56e9a7SVivek Gautam 
930b56e9a7SVivek Gautam /*
940b56e9a7SVivek Gautam  * Note do not raise the debounce time, we must report Vusb high within 100ms
950b56e9a7SVivek Gautam  * otherwise we get Vbus errors
960b56e9a7SVivek Gautam  */
970b56e9a7SVivek Gautam #define DEBOUNCE_TIME			msecs_to_jiffies(50)
980b56e9a7SVivek Gautam #define POLL_TIME			msecs_to_jiffies(250)
990b56e9a7SVivek Gautam 
1000b56e9a7SVivek Gautam struct sun4i_usb_phy_cfg {
1010b56e9a7SVivek Gautam 	int num_phys;
102f0152c58SChen-Yu Tsai 	int hsic_index;
1030b56e9a7SVivek Gautam 	u32 disc_thresh;
1041743dea7SAndre Przywara 	u32 hci_phy_ctl_clear;
1050b56e9a7SVivek Gautam 	u8 phyctl_offset;
1060b56e9a7SVivek Gautam 	bool dedicated_clocks;
1070b56e9a7SVivek Gautam 	bool phy0_dual_route;
108b45c6d80SAndre Przywara 	bool needs_phy2_siddq;
1098dd256baSAndre Przywara 	bool siddq_in_base;
1108dd256baSAndre Przywara 	bool poll_vbusen;
1112659392eSIcenowy Zheng 	int missing_phys;
1120b56e9a7SVivek Gautam };
1130b56e9a7SVivek Gautam 
1140b56e9a7SVivek Gautam struct sun4i_usb_phy_data {
1150b56e9a7SVivek Gautam 	void __iomem *base;
1160b56e9a7SVivek Gautam 	const struct sun4i_usb_phy_cfg *cfg;
1170b56e9a7SVivek Gautam 	enum usb_dr_mode dr_mode;
1180b56e9a7SVivek Gautam 	spinlock_t reg_lock; /* guard access to phyctl reg */
1190b56e9a7SVivek Gautam 	struct sun4i_usb_phy {
1200b56e9a7SVivek Gautam 		struct phy *phy;
1210b56e9a7SVivek Gautam 		void __iomem *pmu;
1220b56e9a7SVivek Gautam 		struct regulator *vbus;
1230b56e9a7SVivek Gautam 		struct reset_control *reset;
1240b56e9a7SVivek Gautam 		struct clk *clk;
125f0152c58SChen-Yu Tsai 		struct clk *clk2;
1260b56e9a7SVivek Gautam 		bool regulator_on;
1270b56e9a7SVivek Gautam 		int index;
1280b56e9a7SVivek Gautam 	} phys[MAX_PHYS];
1290b56e9a7SVivek Gautam 	/* phy0 / otg related variables */
1300b56e9a7SVivek Gautam 	struct extcon_dev *extcon;
1310b56e9a7SVivek Gautam 	bool phy0_init;
1320b56e9a7SVivek Gautam 	struct gpio_desc *id_det_gpio;
1330b56e9a7SVivek Gautam 	struct gpio_desc *vbus_det_gpio;
1340b56e9a7SVivek Gautam 	struct power_supply *vbus_power_supply;
1350b56e9a7SVivek Gautam 	struct notifier_block vbus_power_nb;
1360b56e9a7SVivek Gautam 	bool vbus_power_nb_registered;
1370b56e9a7SVivek Gautam 	bool force_session_end;
1380b56e9a7SVivek Gautam 	int id_det_irq;
1390b56e9a7SVivek Gautam 	int vbus_det_irq;
1400b56e9a7SVivek Gautam 	int id_det;
1410b56e9a7SVivek Gautam 	int vbus_det;
1420b56e9a7SVivek Gautam 	struct delayed_work detect;
1430b56e9a7SVivek Gautam };
1440b56e9a7SVivek Gautam 
1450b56e9a7SVivek Gautam #define to_sun4i_usb_phy_data(phy) \
1460b56e9a7SVivek Gautam 	container_of((phy), struct sun4i_usb_phy_data, phys[(phy)->index])
1470b56e9a7SVivek Gautam 
sun4i_usb_phy0_update_iscr(struct phy * _phy,u32 clr,u32 set)1480b56e9a7SVivek Gautam static void sun4i_usb_phy0_update_iscr(struct phy *_phy, u32 clr, u32 set)
1490b56e9a7SVivek Gautam {
1500b56e9a7SVivek Gautam 	struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
1510b56e9a7SVivek Gautam 	struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
1520b56e9a7SVivek Gautam 	u32 iscr;
1530b56e9a7SVivek Gautam 
1540b56e9a7SVivek Gautam 	iscr = readl(data->base + REG_ISCR);
1550b56e9a7SVivek Gautam 	iscr &= ~clr;
1560b56e9a7SVivek Gautam 	iscr |= set;
1570b56e9a7SVivek Gautam 	writel(iscr, data->base + REG_ISCR);
1580b56e9a7SVivek Gautam }
1590b56e9a7SVivek Gautam 
sun4i_usb_phy0_set_id_detect(struct phy * phy,u32 val)1600b56e9a7SVivek Gautam static void sun4i_usb_phy0_set_id_detect(struct phy *phy, u32 val)
1610b56e9a7SVivek Gautam {
1620b56e9a7SVivek Gautam 	if (val)
1630b56e9a7SVivek Gautam 		val = ISCR_FORCE_ID_HIGH;
1640b56e9a7SVivek Gautam 	else
1650b56e9a7SVivek Gautam 		val = ISCR_FORCE_ID_LOW;
1660b56e9a7SVivek Gautam 
1670b56e9a7SVivek Gautam 	sun4i_usb_phy0_update_iscr(phy, ISCR_FORCE_ID_MASK, val);
1680b56e9a7SVivek Gautam }
1690b56e9a7SVivek Gautam 
sun4i_usb_phy0_set_vbus_detect(struct phy * phy,u32 val)1700b56e9a7SVivek Gautam static void sun4i_usb_phy0_set_vbus_detect(struct phy *phy, u32 val)
1710b56e9a7SVivek Gautam {
1720b56e9a7SVivek Gautam 	if (val)
1730b56e9a7SVivek Gautam 		val = ISCR_FORCE_VBUS_HIGH;
1740b56e9a7SVivek Gautam 	else
1750b56e9a7SVivek Gautam 		val = ISCR_FORCE_VBUS_LOW;
1760b56e9a7SVivek Gautam 
1770b56e9a7SVivek Gautam 	sun4i_usb_phy0_update_iscr(phy, ISCR_FORCE_VBUS_MASK, val);
1780b56e9a7SVivek Gautam }
1790b56e9a7SVivek Gautam 
sun4i_usb_phy_write(struct sun4i_usb_phy * phy,u32 addr,u32 data,int len)1800b56e9a7SVivek Gautam static void sun4i_usb_phy_write(struct sun4i_usb_phy *phy, u32 addr, u32 data,
1810b56e9a7SVivek Gautam 				int len)
1820b56e9a7SVivek Gautam {
1830b56e9a7SVivek Gautam 	struct sun4i_usb_phy_data *phy_data = to_sun4i_usb_phy_data(phy);
1840b56e9a7SVivek Gautam 	u32 temp, usbc_bit = BIT(phy->index * 2);
1850b56e9a7SVivek Gautam 	void __iomem *phyctl = phy_data->base + phy_data->cfg->phyctl_offset;
1860b56e9a7SVivek Gautam 	unsigned long flags;
1870b56e9a7SVivek Gautam 	int i;
1880b56e9a7SVivek Gautam 
1890b56e9a7SVivek Gautam 	spin_lock_irqsave(&phy_data->reg_lock, flags);
1900b56e9a7SVivek Gautam 
1910b56e9a7SVivek Gautam 	if (phy_data->cfg->phyctl_offset == REG_PHYCTL_A33) {
1920b56e9a7SVivek Gautam 		/* SoCs newer than A33 need us to set phyctl to 0 explicitly */
1930b56e9a7SVivek Gautam 		writel(0, phyctl);
1940b56e9a7SVivek Gautam 	}
1950b56e9a7SVivek Gautam 
1960b56e9a7SVivek Gautam 	for (i = 0; i < len; i++) {
1970b56e9a7SVivek Gautam 		temp = readl(phyctl);
1980b56e9a7SVivek Gautam 
1990b56e9a7SVivek Gautam 		/* clear the address portion */
2000b56e9a7SVivek Gautam 		temp &= ~(0xff << 8);
2010b56e9a7SVivek Gautam 
2020b56e9a7SVivek Gautam 		/* set the address */
2030b56e9a7SVivek Gautam 		temp |= ((addr + i) << 8);
2040b56e9a7SVivek Gautam 		writel(temp, phyctl);
2050b56e9a7SVivek Gautam 
2060b56e9a7SVivek Gautam 		/* set the data bit and clear usbc bit*/
2070b56e9a7SVivek Gautam 		temp = readb(phyctl);
2080b56e9a7SVivek Gautam 		if (data & 0x1)
2090b56e9a7SVivek Gautam 			temp |= PHYCTL_DATA;
2100b56e9a7SVivek Gautam 		else
2110b56e9a7SVivek Gautam 			temp &= ~PHYCTL_DATA;
2120b56e9a7SVivek Gautam 		temp &= ~usbc_bit;
2130b56e9a7SVivek Gautam 		writeb(temp, phyctl);
2140b56e9a7SVivek Gautam 
2150b56e9a7SVivek Gautam 		/* pulse usbc_bit */
2160b56e9a7SVivek Gautam 		temp = readb(phyctl);
2170b56e9a7SVivek Gautam 		temp |= usbc_bit;
2180b56e9a7SVivek Gautam 		writeb(temp, phyctl);
2190b56e9a7SVivek Gautam 
2200b56e9a7SVivek Gautam 		temp = readb(phyctl);
2210b56e9a7SVivek Gautam 		temp &= ~usbc_bit;
2220b56e9a7SVivek Gautam 		writeb(temp, phyctl);
2230b56e9a7SVivek Gautam 
2240b56e9a7SVivek Gautam 		data >>= 1;
2250b56e9a7SVivek Gautam 	}
2260b56e9a7SVivek Gautam 
2270b56e9a7SVivek Gautam 	spin_unlock_irqrestore(&phy_data->reg_lock, flags);
2280b56e9a7SVivek Gautam }
2290b56e9a7SVivek Gautam 
sun4i_usb_phy_passby(struct sun4i_usb_phy * phy,int enable)2300b56e9a7SVivek Gautam static void sun4i_usb_phy_passby(struct sun4i_usb_phy *phy, int enable)
2310b56e9a7SVivek Gautam {
2324b63743cSChen-Yu Tsai 	struct sun4i_usb_phy_data *phy_data = to_sun4i_usb_phy_data(phy);
2330b56e9a7SVivek Gautam 	u32 bits, reg_value;
2340b56e9a7SVivek Gautam 
2350b56e9a7SVivek Gautam 	if (!phy->pmu)
2360b56e9a7SVivek Gautam 		return;
2370b56e9a7SVivek Gautam 
2380b56e9a7SVivek Gautam 	bits = SUNXI_AHB_ICHR8_EN | SUNXI_AHB_INCR4_BURST_EN |
2390b56e9a7SVivek Gautam 		SUNXI_AHB_INCRX_ALIGN_EN | SUNXI_ULPI_BYPASS_EN;
2400b56e9a7SVivek Gautam 
2414b63743cSChen-Yu Tsai 	/* A83T USB2 is HSIC */
2428dd256baSAndre Przywara 	if (phy_data->cfg->hsic_index &&
2438dd256baSAndre Przywara 	    phy->index == phy_data->cfg->hsic_index)
2444b63743cSChen-Yu Tsai 		bits |= SUNXI_EHCI_HS_FORCE | SUNXI_HSIC_CONNECT_INT |
2454b63743cSChen-Yu Tsai 			SUNXI_HSIC;
2464b63743cSChen-Yu Tsai 
2470b56e9a7SVivek Gautam 	reg_value = readl(phy->pmu);
2480b56e9a7SVivek Gautam 
2490b56e9a7SVivek Gautam 	if (enable)
2500b56e9a7SVivek Gautam 		reg_value |= bits;
2510b56e9a7SVivek Gautam 	else
2520b56e9a7SVivek Gautam 		reg_value &= ~bits;
2530b56e9a7SVivek Gautam 
2540b56e9a7SVivek Gautam 	writel(reg_value, phy->pmu);
2550b56e9a7SVivek Gautam }
2560b56e9a7SVivek Gautam 
sun4i_usb_phy_init(struct phy * _phy)2570b56e9a7SVivek Gautam static int sun4i_usb_phy_init(struct phy *_phy)
2580b56e9a7SVivek Gautam {
2590b56e9a7SVivek Gautam 	struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
2600b56e9a7SVivek Gautam 	struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
2610b56e9a7SVivek Gautam 	int ret;
2620b56e9a7SVivek Gautam 	u32 val;
2630b56e9a7SVivek Gautam 
2640b56e9a7SVivek Gautam 	ret = clk_prepare_enable(phy->clk);
2650b56e9a7SVivek Gautam 	if (ret)
2660b56e9a7SVivek Gautam 		return ret;
2670b56e9a7SVivek Gautam 
268f0152c58SChen-Yu Tsai 	ret = clk_prepare_enable(phy->clk2);
269f0152c58SChen-Yu Tsai 	if (ret) {
270f0152c58SChen-Yu Tsai 		clk_disable_unprepare(phy->clk);
271f0152c58SChen-Yu Tsai 		return ret;
272f0152c58SChen-Yu Tsai 	}
273f0152c58SChen-Yu Tsai 
2740b56e9a7SVivek Gautam 	ret = reset_control_deassert(phy->reset);
2750b56e9a7SVivek Gautam 	if (ret) {
276f0152c58SChen-Yu Tsai 		clk_disable_unprepare(phy->clk2);
2770b56e9a7SVivek Gautam 		clk_disable_unprepare(phy->clk);
2780b56e9a7SVivek Gautam 		return ret;
2790b56e9a7SVivek Gautam 	}
2800b56e9a7SVivek Gautam 
281b45c6d80SAndre Przywara 	/* Some PHYs on some SoCs need the help of PHY2 to work. */
282b45c6d80SAndre Przywara 	if (data->cfg->needs_phy2_siddq && phy->index != 2) {
283b45c6d80SAndre Przywara 		struct sun4i_usb_phy *phy2 = &data->phys[2];
284b45c6d80SAndre Przywara 
285b45c6d80SAndre Przywara 		ret = clk_prepare_enable(phy2->clk);
286b45c6d80SAndre Przywara 		if (ret) {
287b45c6d80SAndre Przywara 			reset_control_assert(phy->reset);
288b45c6d80SAndre Przywara 			clk_disable_unprepare(phy->clk2);
289b45c6d80SAndre Przywara 			clk_disable_unprepare(phy->clk);
290b45c6d80SAndre Przywara 			return ret;
291b45c6d80SAndre Przywara 		}
292b45c6d80SAndre Przywara 
293b45c6d80SAndre Przywara 		ret = reset_control_deassert(phy2->reset);
294b45c6d80SAndre Przywara 		if (ret) {
295b45c6d80SAndre Przywara 			clk_disable_unprepare(phy2->clk);
296b45c6d80SAndre Przywara 			reset_control_assert(phy->reset);
297b45c6d80SAndre Przywara 			clk_disable_unprepare(phy->clk2);
298b45c6d80SAndre Przywara 			clk_disable_unprepare(phy->clk);
299b45c6d80SAndre Przywara 			return ret;
300b45c6d80SAndre Przywara 		}
301b45c6d80SAndre Przywara 
302b45c6d80SAndre Przywara 		/*
303b45c6d80SAndre Przywara 		 * This extra clock is just needed to access the
304b45c6d80SAndre Przywara 		 * REG_HCI_PHY_CTL PMU register for PHY2.
305b45c6d80SAndre Przywara 		 */
306b45c6d80SAndre Przywara 		ret = clk_prepare_enable(phy2->clk2);
307b45c6d80SAndre Przywara 		if (ret) {
308b45c6d80SAndre Przywara 			reset_control_assert(phy2->reset);
309b45c6d80SAndre Przywara 			clk_disable_unprepare(phy2->clk);
310b45c6d80SAndre Przywara 			reset_control_assert(phy->reset);
311b45c6d80SAndre Przywara 			clk_disable_unprepare(phy->clk2);
312b45c6d80SAndre Przywara 			clk_disable_unprepare(phy->clk);
313b45c6d80SAndre Przywara 			return ret;
314b45c6d80SAndre Przywara 		}
315b45c6d80SAndre Przywara 
316b45c6d80SAndre Przywara 		if (phy2->pmu && data->cfg->hci_phy_ctl_clear) {
317b45c6d80SAndre Przywara 			val = readl(phy2->pmu + REG_HCI_PHY_CTL);
318b45c6d80SAndre Przywara 			val &= ~data->cfg->hci_phy_ctl_clear;
319b45c6d80SAndre Przywara 			writel(val, phy2->pmu + REG_HCI_PHY_CTL);
320b45c6d80SAndre Przywara 		}
321b45c6d80SAndre Przywara 
322b45c6d80SAndre Przywara 		clk_disable_unprepare(phy->clk2);
323b45c6d80SAndre Przywara 	}
324b45c6d80SAndre Przywara 
3251743dea7SAndre Przywara 	if (phy->pmu && data->cfg->hci_phy_ctl_clear) {
3261743dea7SAndre Przywara 		val = readl(phy->pmu + REG_HCI_PHY_CTL);
3271743dea7SAndre Przywara 		val &= ~data->cfg->hci_phy_ctl_clear;
3281743dea7SAndre Przywara 		writel(val, phy->pmu + REG_HCI_PHY_CTL);
3291743dea7SAndre Przywara 	}
3301743dea7SAndre Przywara 
3318dd256baSAndre Przywara 	if (data->cfg->siddq_in_base) {
3324b63743cSChen-Yu Tsai 		if (phy->index == 0) {
3334b63743cSChen-Yu Tsai 			val = readl(data->base + data->cfg->phyctl_offset);
3344b63743cSChen-Yu Tsai 			val |= PHY_CTL_VBUSVLDEXT;
3354b63743cSChen-Yu Tsai 			val &= ~PHY_CTL_SIDDQ;
3364b63743cSChen-Yu Tsai 			writel(val, data->base + data->cfg->phyctl_offset);
3374b63743cSChen-Yu Tsai 		}
3384b63743cSChen-Yu Tsai 	} else {
3390b56e9a7SVivek Gautam 		/* Enable USB 45 Ohm resistor calibration */
3400b56e9a7SVivek Gautam 		if (phy->index == 0)
3410b56e9a7SVivek Gautam 			sun4i_usb_phy_write(phy, PHY_RES45_CAL_EN, 0x01, 1);
3420b56e9a7SVivek Gautam 
3430b56e9a7SVivek Gautam 		/* Adjust PHY's magnitude and rate */
3440b56e9a7SVivek Gautam 		sun4i_usb_phy_write(phy, PHY_TX_AMPLITUDE_TUNE, 0x14, 5);
3450b56e9a7SVivek Gautam 
3460b56e9a7SVivek Gautam 		/* Disconnect threshold adjustment */
3470b56e9a7SVivek Gautam 		sun4i_usb_phy_write(phy, PHY_DISCON_TH_SEL,
3480b56e9a7SVivek Gautam 				    data->cfg->disc_thresh, 2);
3494b63743cSChen-Yu Tsai 	}
3500b56e9a7SVivek Gautam 
3510b56e9a7SVivek Gautam 	sun4i_usb_phy_passby(phy, 1);
3520b56e9a7SVivek Gautam 
3530b56e9a7SVivek Gautam 	if (phy->index == 0) {
3540b56e9a7SVivek Gautam 		data->phy0_init = true;
3550b56e9a7SVivek Gautam 
3560b56e9a7SVivek Gautam 		/* Enable pull-ups */
3570b56e9a7SVivek Gautam 		sun4i_usb_phy0_update_iscr(_phy, 0, ISCR_DPDM_PULLUP_EN);
3580b56e9a7SVivek Gautam 		sun4i_usb_phy0_update_iscr(_phy, 0, ISCR_ID_PULLUP_EN);
3590b56e9a7SVivek Gautam 
3600b56e9a7SVivek Gautam 		/* Force ISCR and cable state updates */
3610b56e9a7SVivek Gautam 		data->id_det = -1;
3620b56e9a7SVivek Gautam 		data->vbus_det = -1;
3630b56e9a7SVivek Gautam 		queue_delayed_work(system_wq, &data->detect, 0);
3640b56e9a7SVivek Gautam 	}
3650b56e9a7SVivek Gautam 
3660b56e9a7SVivek Gautam 	return 0;
3670b56e9a7SVivek Gautam }
3680b56e9a7SVivek Gautam 
sun4i_usb_phy_exit(struct phy * _phy)3690b56e9a7SVivek Gautam static int sun4i_usb_phy_exit(struct phy *_phy)
3700b56e9a7SVivek Gautam {
3710b56e9a7SVivek Gautam 	struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
3720b56e9a7SVivek Gautam 	struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
3730b56e9a7SVivek Gautam 
3740b56e9a7SVivek Gautam 	if (phy->index == 0) {
3758dd256baSAndre Przywara 		if (data->cfg->siddq_in_base) {
3764b63743cSChen-Yu Tsai 			void __iomem *phyctl = data->base +
3774b63743cSChen-Yu Tsai 				data->cfg->phyctl_offset;
3784b63743cSChen-Yu Tsai 
3794b63743cSChen-Yu Tsai 			writel(readl(phyctl) | PHY_CTL_SIDDQ, phyctl);
3804b63743cSChen-Yu Tsai 		}
3814b63743cSChen-Yu Tsai 
3820b56e9a7SVivek Gautam 		/* Disable pull-ups */
3830b56e9a7SVivek Gautam 		sun4i_usb_phy0_update_iscr(_phy, ISCR_DPDM_PULLUP_EN, 0);
3840b56e9a7SVivek Gautam 		sun4i_usb_phy0_update_iscr(_phy, ISCR_ID_PULLUP_EN, 0);
3850b56e9a7SVivek Gautam 		data->phy0_init = false;
3860b56e9a7SVivek Gautam 	}
3870b56e9a7SVivek Gautam 
388b45c6d80SAndre Przywara 	if (data->cfg->needs_phy2_siddq && phy->index != 2) {
389b45c6d80SAndre Przywara 		struct sun4i_usb_phy *phy2 = &data->phys[2];
390b45c6d80SAndre Przywara 
391b45c6d80SAndre Przywara 		clk_disable_unprepare(phy2->clk);
392b45c6d80SAndre Przywara 		reset_control_assert(phy2->reset);
393b45c6d80SAndre Przywara 	}
394b45c6d80SAndre Przywara 
3950b56e9a7SVivek Gautam 	sun4i_usb_phy_passby(phy, 0);
3960b56e9a7SVivek Gautam 	reset_control_assert(phy->reset);
397f0152c58SChen-Yu Tsai 	clk_disable_unprepare(phy->clk2);
3980b56e9a7SVivek Gautam 	clk_disable_unprepare(phy->clk);
3990b56e9a7SVivek Gautam 
4000b56e9a7SVivek Gautam 	return 0;
4010b56e9a7SVivek Gautam }
4020b56e9a7SVivek Gautam 
sun4i_usb_phy0_get_id_det(struct sun4i_usb_phy_data * data)4030b56e9a7SVivek Gautam static int sun4i_usb_phy0_get_id_det(struct sun4i_usb_phy_data *data)
4040b56e9a7SVivek Gautam {
4050b56e9a7SVivek Gautam 	switch (data->dr_mode) {
4060b56e9a7SVivek Gautam 	case USB_DR_MODE_OTG:
4070b56e9a7SVivek Gautam 		if (data->id_det_gpio)
4080b56e9a7SVivek Gautam 			return gpiod_get_value_cansleep(data->id_det_gpio);
4090b56e9a7SVivek Gautam 		else
4100b56e9a7SVivek Gautam 			return 1; /* Fallback to peripheral mode */
4110b56e9a7SVivek Gautam 	case USB_DR_MODE_HOST:
4120b56e9a7SVivek Gautam 		return 0;
4130b56e9a7SVivek Gautam 	case USB_DR_MODE_PERIPHERAL:
4140b56e9a7SVivek Gautam 	default:
4150b56e9a7SVivek Gautam 		return 1;
4160b56e9a7SVivek Gautam 	}
4170b56e9a7SVivek Gautam }
4180b56e9a7SVivek Gautam 
sun4i_usb_phy0_get_vbus_det(struct sun4i_usb_phy_data * data)4190b56e9a7SVivek Gautam static int sun4i_usb_phy0_get_vbus_det(struct sun4i_usb_phy_data *data)
4200b56e9a7SVivek Gautam {
4210b56e9a7SVivek Gautam 	if (data->vbus_det_gpio)
4220b56e9a7SVivek Gautam 		return gpiod_get_value_cansleep(data->vbus_det_gpio);
4230b56e9a7SVivek Gautam 
4240b56e9a7SVivek Gautam 	if (data->vbus_power_supply) {
4250b56e9a7SVivek Gautam 		union power_supply_propval val;
4260b56e9a7SVivek Gautam 		int r;
4270b56e9a7SVivek Gautam 
4280b56e9a7SVivek Gautam 		r = power_supply_get_property(data->vbus_power_supply,
4290b56e9a7SVivek Gautam 					      POWER_SUPPLY_PROP_PRESENT, &val);
4300b56e9a7SVivek Gautam 		if (r == 0)
4310b56e9a7SVivek Gautam 			return val.intval;
4320b56e9a7SVivek Gautam 	}
4330b56e9a7SVivek Gautam 
4340b56e9a7SVivek Gautam 	/* Fallback: report vbus as high */
4350b56e9a7SVivek Gautam 	return 1;
4360b56e9a7SVivek Gautam }
4370b56e9a7SVivek Gautam 
sun4i_usb_phy0_have_vbus_det(struct sun4i_usb_phy_data * data)4380b56e9a7SVivek Gautam static bool sun4i_usb_phy0_have_vbus_det(struct sun4i_usb_phy_data *data)
4390b56e9a7SVivek Gautam {
4400b56e9a7SVivek Gautam 	return data->vbus_det_gpio || data->vbus_power_supply;
4410b56e9a7SVivek Gautam }
4420b56e9a7SVivek Gautam 
sun4i_usb_phy0_poll(struct sun4i_usb_phy_data * data)4430b56e9a7SVivek Gautam static bool sun4i_usb_phy0_poll(struct sun4i_usb_phy_data *data)
4440b56e9a7SVivek Gautam {
4450b56e9a7SVivek Gautam 	if ((data->id_det_gpio && data->id_det_irq <= 0) ||
4460b56e9a7SVivek Gautam 	    (data->vbus_det_gpio && data->vbus_det_irq <= 0))
4470b56e9a7SVivek Gautam 		return true;
4480b56e9a7SVivek Gautam 
4490b56e9a7SVivek Gautam 	/*
450d7119224SChen-Yu Tsai 	 * The A31/A23/A33 companion pmics (AXP221/AXP223) do not
451d7119224SChen-Yu Tsai 	 * generate vbus change interrupts when the board is driving
452d7119224SChen-Yu Tsai 	 * vbus using the N_VBUSEN pin on the pmic, so we must poll
4530b56e9a7SVivek Gautam 	 * when using the pmic for vbus-det _and_ we're driving vbus.
4540b56e9a7SVivek Gautam 	 */
4558dd256baSAndre Przywara 	if (data->cfg->poll_vbusen && data->vbus_power_supply &&
4568dd256baSAndre Przywara 	    data->phys[0].regulator_on)
4570b56e9a7SVivek Gautam 		return true;
4580b56e9a7SVivek Gautam 
4590b56e9a7SVivek Gautam 	return false;
4600b56e9a7SVivek Gautam }
4610b56e9a7SVivek Gautam 
sun4i_usb_phy_power_on(struct phy * _phy)4620b56e9a7SVivek Gautam static int sun4i_usb_phy_power_on(struct phy *_phy)
4630b56e9a7SVivek Gautam {
4640b56e9a7SVivek Gautam 	struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
4650b56e9a7SVivek Gautam 	struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
4660b56e9a7SVivek Gautam 	int ret;
4670b56e9a7SVivek Gautam 
4680b56e9a7SVivek Gautam 	if (!phy->vbus || phy->regulator_on)
4690b56e9a7SVivek Gautam 		return 0;
4700b56e9a7SVivek Gautam 
4710b56e9a7SVivek Gautam 	/* For phy0 only turn on Vbus if we don't have an ext. Vbus */
4720b56e9a7SVivek Gautam 	if (phy->index == 0 && sun4i_usb_phy0_have_vbus_det(data) &&
4730b56e9a7SVivek Gautam 				data->vbus_det) {
4740b56e9a7SVivek Gautam 		dev_warn(&_phy->dev, "External vbus detected, not enabling our own vbus\n");
4750b56e9a7SVivek Gautam 		return 0;
4760b56e9a7SVivek Gautam 	}
4770b56e9a7SVivek Gautam 
4780b56e9a7SVivek Gautam 	ret = regulator_enable(phy->vbus);
4790b56e9a7SVivek Gautam 	if (ret)
4800b56e9a7SVivek Gautam 		return ret;
4810b56e9a7SVivek Gautam 
4820b56e9a7SVivek Gautam 	phy->regulator_on = true;
4830b56e9a7SVivek Gautam 
4840b56e9a7SVivek Gautam 	/* We must report Vbus high within OTG_TIME_A_WAIT_VRISE msec. */
4850b56e9a7SVivek Gautam 	if (phy->index == 0 && sun4i_usb_phy0_poll(data))
4860b56e9a7SVivek Gautam 		mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
4870b56e9a7SVivek Gautam 
4880b56e9a7SVivek Gautam 	return 0;
4890b56e9a7SVivek Gautam }
4900b56e9a7SVivek Gautam 
sun4i_usb_phy_power_off(struct phy * _phy)4910b56e9a7SVivek Gautam static int sun4i_usb_phy_power_off(struct phy *_phy)
4920b56e9a7SVivek Gautam {
4930b56e9a7SVivek Gautam 	struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
4940b56e9a7SVivek Gautam 	struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
4950b56e9a7SVivek Gautam 
4960b56e9a7SVivek Gautam 	if (!phy->vbus || !phy->regulator_on)
4970b56e9a7SVivek Gautam 		return 0;
4980b56e9a7SVivek Gautam 
4990b56e9a7SVivek Gautam 	regulator_disable(phy->vbus);
5000b56e9a7SVivek Gautam 	phy->regulator_on = false;
5010b56e9a7SVivek Gautam 
5020b56e9a7SVivek Gautam 	/*
5030b56e9a7SVivek Gautam 	 * phy0 vbus typically slowly discharges, sometimes this causes the
5040b56e9a7SVivek Gautam 	 * Vbus gpio to not trigger an edge irq on Vbus off, so force a rescan.
5050b56e9a7SVivek Gautam 	 */
5060b56e9a7SVivek Gautam 	if (phy->index == 0 && !sun4i_usb_phy0_poll(data))
5070b56e9a7SVivek Gautam 		mod_delayed_work(system_wq, &data->detect, POLL_TIME);
5080b56e9a7SVivek Gautam 
5090b56e9a7SVivek Gautam 	return 0;
5100b56e9a7SVivek Gautam }
5110b56e9a7SVivek Gautam 
sun4i_usb_phy_set_mode(struct phy * _phy,enum phy_mode mode,int submode)51279a5a18aSGrygorii Strashko static int sun4i_usb_phy_set_mode(struct phy *_phy,
51379a5a18aSGrygorii Strashko 				  enum phy_mode mode, int submode)
5140b56e9a7SVivek Gautam {
5150b56e9a7SVivek Gautam 	struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
5160b56e9a7SVivek Gautam 	struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
5170b56e9a7SVivek Gautam 	int new_mode;
5180b56e9a7SVivek Gautam 
5191396929eSChen-Yu Tsai 	if (phy->index != 0) {
5201396929eSChen-Yu Tsai 		if (mode == PHY_MODE_USB_HOST)
5211396929eSChen-Yu Tsai 			return 0;
5220b56e9a7SVivek Gautam 		return -EINVAL;
5231396929eSChen-Yu Tsai 	}
5240b56e9a7SVivek Gautam 
5250b56e9a7SVivek Gautam 	switch (mode) {
5260b56e9a7SVivek Gautam 	case PHY_MODE_USB_HOST:
5270b56e9a7SVivek Gautam 		new_mode = USB_DR_MODE_HOST;
5280b56e9a7SVivek Gautam 		break;
5290b56e9a7SVivek Gautam 	case PHY_MODE_USB_DEVICE:
5300b56e9a7SVivek Gautam 		new_mode = USB_DR_MODE_PERIPHERAL;
5310b56e9a7SVivek Gautam 		break;
5320b56e9a7SVivek Gautam 	case PHY_MODE_USB_OTG:
5330b56e9a7SVivek Gautam 		new_mode = USB_DR_MODE_OTG;
5340b56e9a7SVivek Gautam 		break;
5350b56e9a7SVivek Gautam 	default:
5360b56e9a7SVivek Gautam 		return -EINVAL;
5370b56e9a7SVivek Gautam 	}
5380b56e9a7SVivek Gautam 
5390b56e9a7SVivek Gautam 	if (new_mode != data->dr_mode) {
5400b56e9a7SVivek Gautam 		dev_info(&_phy->dev, "Changing dr_mode to %d\n", new_mode);
5410b56e9a7SVivek Gautam 		data->dr_mode = new_mode;
5420b56e9a7SVivek Gautam 	}
5430b56e9a7SVivek Gautam 
5440b56e9a7SVivek Gautam 	data->id_det = -1; /* Force reprocessing of id */
5450b56e9a7SVivek Gautam 	data->force_session_end = true;
5460b56e9a7SVivek Gautam 	queue_delayed_work(system_wq, &data->detect, 0);
5470b56e9a7SVivek Gautam 
5480b56e9a7SVivek Gautam 	return 0;
5490b56e9a7SVivek Gautam }
5500b56e9a7SVivek Gautam 
sun4i_usb_phy_set_squelch_detect(struct phy * _phy,bool enabled)5510b56e9a7SVivek Gautam void sun4i_usb_phy_set_squelch_detect(struct phy *_phy, bool enabled)
5520b56e9a7SVivek Gautam {
5530b56e9a7SVivek Gautam 	struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
5540b56e9a7SVivek Gautam 
5550b56e9a7SVivek Gautam 	sun4i_usb_phy_write(phy, PHY_SQUELCH_DETECT, enabled ? 0 : 2, 2);
5560b56e9a7SVivek Gautam }
5570b56e9a7SVivek Gautam EXPORT_SYMBOL_GPL(sun4i_usb_phy_set_squelch_detect);
5580b56e9a7SVivek Gautam 
5590b56e9a7SVivek Gautam static const struct phy_ops sun4i_usb_phy_ops = {
5600b56e9a7SVivek Gautam 	.init		= sun4i_usb_phy_init,
5610b56e9a7SVivek Gautam 	.exit		= sun4i_usb_phy_exit,
5620b56e9a7SVivek Gautam 	.power_on	= sun4i_usb_phy_power_on,
5630b56e9a7SVivek Gautam 	.power_off	= sun4i_usb_phy_power_off,
5640b56e9a7SVivek Gautam 	.set_mode	= sun4i_usb_phy_set_mode,
5650b56e9a7SVivek Gautam 	.owner		= THIS_MODULE,
5660b56e9a7SVivek Gautam };
5670b56e9a7SVivek Gautam 
sun4i_usb_phy0_reroute(struct sun4i_usb_phy_data * data,int id_det)5680b56e9a7SVivek Gautam static void sun4i_usb_phy0_reroute(struct sun4i_usb_phy_data *data, int id_det)
5690b56e9a7SVivek Gautam {
5700b56e9a7SVivek Gautam 	u32 regval;
5710b56e9a7SVivek Gautam 
5720b56e9a7SVivek Gautam 	regval = readl(data->base + REG_PHY_OTGCTL);
5730b56e9a7SVivek Gautam 	if (id_det == 0) {
5740b56e9a7SVivek Gautam 		/* Host mode. Route phy0 to EHCI/OHCI */
5750b56e9a7SVivek Gautam 		regval &= ~OTGCTL_ROUTE_MUSB;
5760b56e9a7SVivek Gautam 	} else {
5770b56e9a7SVivek Gautam 		/* Peripheral mode. Route phy0 to MUSB */
5780b56e9a7SVivek Gautam 		regval |= OTGCTL_ROUTE_MUSB;
5790b56e9a7SVivek Gautam 	}
5800b56e9a7SVivek Gautam 	writel(regval, data->base + REG_PHY_OTGCTL);
5810b56e9a7SVivek Gautam }
5820b56e9a7SVivek Gautam 
sun4i_usb_phy0_id_vbus_det_scan(struct work_struct * work)5830b56e9a7SVivek Gautam static void sun4i_usb_phy0_id_vbus_det_scan(struct work_struct *work)
5840b56e9a7SVivek Gautam {
5850b56e9a7SVivek Gautam 	struct sun4i_usb_phy_data *data =
5860b56e9a7SVivek Gautam 		container_of(work, struct sun4i_usb_phy_data, detect.work);
5870b56e9a7SVivek Gautam 	struct phy *phy0 = data->phys[0].phy;
58838b1927eSColin Ian King 	struct sun4i_usb_phy *phy;
5890b56e9a7SVivek Gautam 	bool force_session_end, id_notify = false, vbus_notify = false;
5900b56e9a7SVivek Gautam 	int id_det, vbus_det;
5910b56e9a7SVivek Gautam 
59238b1927eSColin Ian King 	if (!phy0)
5930b56e9a7SVivek Gautam 		return;
5940b56e9a7SVivek Gautam 
59538b1927eSColin Ian King 	phy = phy_get_drvdata(phy0);
5960b56e9a7SVivek Gautam 	id_det = sun4i_usb_phy0_get_id_det(data);
5970b56e9a7SVivek Gautam 	vbus_det = sun4i_usb_phy0_get_vbus_det(data);
5980b56e9a7SVivek Gautam 
5990b56e9a7SVivek Gautam 	mutex_lock(&phy0->mutex);
6000b56e9a7SVivek Gautam 
6010b56e9a7SVivek Gautam 	if (!data->phy0_init) {
6020b56e9a7SVivek Gautam 		mutex_unlock(&phy0->mutex);
6030b56e9a7SVivek Gautam 		return;
6040b56e9a7SVivek Gautam 	}
6050b56e9a7SVivek Gautam 
6060b56e9a7SVivek Gautam 	force_session_end = data->force_session_end;
6070b56e9a7SVivek Gautam 	data->force_session_end = false;
6080b56e9a7SVivek Gautam 
6090b56e9a7SVivek Gautam 	if (id_det != data->id_det) {
6100b56e9a7SVivek Gautam 		/* id-change, force session end if we've no vbus detection */
6110b56e9a7SVivek Gautam 		if (data->dr_mode == USB_DR_MODE_OTG &&
6120b56e9a7SVivek Gautam 		    !sun4i_usb_phy0_have_vbus_det(data))
6130b56e9a7SVivek Gautam 			force_session_end = true;
6140b56e9a7SVivek Gautam 
6150b56e9a7SVivek Gautam 		/* When entering host mode (id = 0) force end the session now */
6160b56e9a7SVivek Gautam 		if (force_session_end && id_det == 0) {
6170b56e9a7SVivek Gautam 			sun4i_usb_phy0_set_vbus_detect(phy0, 0);
6180b56e9a7SVivek Gautam 			msleep(200);
6190b56e9a7SVivek Gautam 			sun4i_usb_phy0_set_vbus_detect(phy0, 1);
6200b56e9a7SVivek Gautam 		}
6210b56e9a7SVivek Gautam 		sun4i_usb_phy0_set_id_detect(phy0, id_det);
6220b56e9a7SVivek Gautam 		data->id_det = id_det;
6230b56e9a7SVivek Gautam 		id_notify = true;
6240b56e9a7SVivek Gautam 	}
6250b56e9a7SVivek Gautam 
6260b56e9a7SVivek Gautam 	if (vbus_det != data->vbus_det) {
6270b56e9a7SVivek Gautam 		sun4i_usb_phy0_set_vbus_detect(phy0, vbus_det);
6280b56e9a7SVivek Gautam 		data->vbus_det = vbus_det;
6290b56e9a7SVivek Gautam 		vbus_notify = true;
6300b56e9a7SVivek Gautam 	}
6310b56e9a7SVivek Gautam 
6320b56e9a7SVivek Gautam 	mutex_unlock(&phy0->mutex);
6330b56e9a7SVivek Gautam 
6340b56e9a7SVivek Gautam 	if (id_notify) {
6350b56e9a7SVivek Gautam 		extcon_set_state_sync(data->extcon, EXTCON_USB_HOST,
6360b56e9a7SVivek Gautam 					!id_det);
6370b56e9a7SVivek Gautam 		/* When leaving host mode force end the session here */
6380b56e9a7SVivek Gautam 		if (force_session_end && id_det == 1) {
6390b56e9a7SVivek Gautam 			mutex_lock(&phy0->mutex);
6400b56e9a7SVivek Gautam 			sun4i_usb_phy0_set_vbus_detect(phy0, 0);
6410b56e9a7SVivek Gautam 			msleep(1000);
6420b56e9a7SVivek Gautam 			sun4i_usb_phy0_set_vbus_detect(phy0, 1);
6430b56e9a7SVivek Gautam 			mutex_unlock(&phy0->mutex);
6440b56e9a7SVivek Gautam 		}
6450b56e9a7SVivek Gautam 
646e6f32efbSPaul Kocialkowski 		/* Enable PHY0 passby for host mode only. */
647e6f32efbSPaul Kocialkowski 		sun4i_usb_phy_passby(phy, !id_det);
648e6f32efbSPaul Kocialkowski 
6490b56e9a7SVivek Gautam 		/* Re-route PHY0 if necessary */
6500b56e9a7SVivek Gautam 		if (data->cfg->phy0_dual_route)
6510b56e9a7SVivek Gautam 			sun4i_usb_phy0_reroute(data, id_det);
6520b56e9a7SVivek Gautam 	}
6530b56e9a7SVivek Gautam 
6540b56e9a7SVivek Gautam 	if (vbus_notify)
6550b56e9a7SVivek Gautam 		extcon_set_state_sync(data->extcon, EXTCON_USB, vbus_det);
6560b56e9a7SVivek Gautam 
6570b56e9a7SVivek Gautam 	if (sun4i_usb_phy0_poll(data))
6580b56e9a7SVivek Gautam 		queue_delayed_work(system_wq, &data->detect, POLL_TIME);
6590b56e9a7SVivek Gautam }
6600b56e9a7SVivek Gautam 
sun4i_usb_phy0_id_vbus_det_irq(int irq,void * dev_id)6610b56e9a7SVivek Gautam static irqreturn_t sun4i_usb_phy0_id_vbus_det_irq(int irq, void *dev_id)
6620b56e9a7SVivek Gautam {
6630b56e9a7SVivek Gautam 	struct sun4i_usb_phy_data *data = dev_id;
6640b56e9a7SVivek Gautam 
6650b56e9a7SVivek Gautam 	/* vbus or id changed, let the pins settle and then scan them */
6660b56e9a7SVivek Gautam 	mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
6670b56e9a7SVivek Gautam 
6680b56e9a7SVivek Gautam 	return IRQ_HANDLED;
6690b56e9a7SVivek Gautam }
6700b56e9a7SVivek Gautam 
sun4i_usb_phy0_vbus_notify(struct notifier_block * nb,unsigned long val,void * v)6710b56e9a7SVivek Gautam static int sun4i_usb_phy0_vbus_notify(struct notifier_block *nb,
6720b56e9a7SVivek Gautam 				      unsigned long val, void *v)
6730b56e9a7SVivek Gautam {
6740b56e9a7SVivek Gautam 	struct sun4i_usb_phy_data *data =
6750b56e9a7SVivek Gautam 		container_of(nb, struct sun4i_usb_phy_data, vbus_power_nb);
6760b56e9a7SVivek Gautam 	struct power_supply *psy = v;
6770b56e9a7SVivek Gautam 
6780b56e9a7SVivek Gautam 	/* Properties on the vbus_power_supply changed, scan vbus_det */
6790b56e9a7SVivek Gautam 	if (val == PSY_EVENT_PROP_CHANGED && psy == data->vbus_power_supply)
6800b56e9a7SVivek Gautam 		mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
6810b56e9a7SVivek Gautam 
6820b56e9a7SVivek Gautam 	return NOTIFY_OK;
6830b56e9a7SVivek Gautam }
6840b56e9a7SVivek Gautam 
sun4i_usb_phy_xlate(struct device * dev,const struct of_phandle_args * args)6850b56e9a7SVivek Gautam static struct phy *sun4i_usb_phy_xlate(struct device *dev,
686*00ca8a15SKrzysztof Kozlowski 					const struct of_phandle_args *args)
6870b56e9a7SVivek Gautam {
6880b56e9a7SVivek Gautam 	struct sun4i_usb_phy_data *data = dev_get_drvdata(dev);
6890b56e9a7SVivek Gautam 
6900b56e9a7SVivek Gautam 	if (args->args[0] >= data->cfg->num_phys)
6910b56e9a7SVivek Gautam 		return ERR_PTR(-ENODEV);
6920b56e9a7SVivek Gautam 
6932659392eSIcenowy Zheng 	if (data->cfg->missing_phys & BIT(args->args[0]))
6942659392eSIcenowy Zheng 		return ERR_PTR(-ENODEV);
6952659392eSIcenowy Zheng 
6960b56e9a7SVivek Gautam 	return data->phys[args->args[0]].phy;
6970b56e9a7SVivek Gautam }
6980b56e9a7SVivek Gautam 
sun4i_usb_phy_remove(struct platform_device * pdev)699b4700ed8SUwe Kleine-König static void sun4i_usb_phy_remove(struct platform_device *pdev)
7000b56e9a7SVivek Gautam {
7010b56e9a7SVivek Gautam 	struct device *dev = &pdev->dev;
7020b56e9a7SVivek Gautam 	struct sun4i_usb_phy_data *data = dev_get_drvdata(dev);
7030b56e9a7SVivek Gautam 
7040b56e9a7SVivek Gautam 	if (data->vbus_power_nb_registered)
7050b56e9a7SVivek Gautam 		power_supply_unreg_notifier(&data->vbus_power_nb);
7060b56e9a7SVivek Gautam 	if (data->id_det_irq > 0)
7070b56e9a7SVivek Gautam 		devm_free_irq(dev, data->id_det_irq, data);
7080b56e9a7SVivek Gautam 	if (data->vbus_det_irq > 0)
7090b56e9a7SVivek Gautam 		devm_free_irq(dev, data->vbus_det_irq, data);
7100b56e9a7SVivek Gautam 
7110b56e9a7SVivek Gautam 	cancel_delayed_work_sync(&data->detect);
7120b56e9a7SVivek Gautam }
7130b56e9a7SVivek Gautam 
7140b56e9a7SVivek Gautam static const unsigned int sun4i_usb_phy0_cable[] = {
7150b56e9a7SVivek Gautam 	EXTCON_USB,
7160b56e9a7SVivek Gautam 	EXTCON_USB_HOST,
7170b56e9a7SVivek Gautam 	EXTCON_NONE,
7180b56e9a7SVivek Gautam };
7190b56e9a7SVivek Gautam 
sun4i_usb_phy_probe(struct platform_device * pdev)7200b56e9a7SVivek Gautam static int sun4i_usb_phy_probe(struct platform_device *pdev)
7210b56e9a7SVivek Gautam {
7220b56e9a7SVivek Gautam 	struct sun4i_usb_phy_data *data;
7230b56e9a7SVivek Gautam 	struct device *dev = &pdev->dev;
7240b56e9a7SVivek Gautam 	struct device_node *np = dev->of_node;
7250b56e9a7SVivek Gautam 	struct phy_provider *phy_provider;
7260b56e9a7SVivek Gautam 	int i, ret;
7270b56e9a7SVivek Gautam 
7280b56e9a7SVivek Gautam 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
7290b56e9a7SVivek Gautam 	if (!data)
7300b56e9a7SVivek Gautam 		return -ENOMEM;
7310b56e9a7SVivek Gautam 
7320b56e9a7SVivek Gautam 	spin_lock_init(&data->reg_lock);
7330b56e9a7SVivek Gautam 	INIT_DELAYED_WORK(&data->detect, sun4i_usb_phy0_id_vbus_det_scan);
7340b56e9a7SVivek Gautam 	dev_set_drvdata(dev, data);
7350b56e9a7SVivek Gautam 	data->cfg = of_device_get_match_data(dev);
7360b56e9a7SVivek Gautam 	if (!data->cfg)
7370b56e9a7SVivek Gautam 		return -EINVAL;
7380b56e9a7SVivek Gautam 
73908d4dedaSChunfeng Yun 	data->base = devm_platform_ioremap_resource_byname(pdev, "phy_ctrl");
7400b56e9a7SVivek Gautam 	if (IS_ERR(data->base))
7410b56e9a7SVivek Gautam 		return PTR_ERR(data->base);
7420b56e9a7SVivek Gautam 
7430b56e9a7SVivek Gautam 	data->id_det_gpio = devm_gpiod_get_optional(dev, "usb0_id_det",
7440b56e9a7SVivek Gautam 						    GPIOD_IN);
745e7cded27SQuentin Schulz 	if (IS_ERR(data->id_det_gpio)) {
746e7cded27SQuentin Schulz 		dev_err(dev, "Couldn't request ID GPIO\n");
7470b56e9a7SVivek Gautam 		return PTR_ERR(data->id_det_gpio);
748e7cded27SQuentin Schulz 	}
7490b56e9a7SVivek Gautam 
7500b56e9a7SVivek Gautam 	data->vbus_det_gpio = devm_gpiod_get_optional(dev, "usb0_vbus_det",
7510b56e9a7SVivek Gautam 						      GPIOD_IN);
752e7cded27SQuentin Schulz 	if (IS_ERR(data->vbus_det_gpio)) {
753e7cded27SQuentin Schulz 		dev_err(dev, "Couldn't request VBUS detect GPIO\n");
7540b56e9a7SVivek Gautam 		return PTR_ERR(data->vbus_det_gpio);
755e7cded27SQuentin Schulz 	}
7560b56e9a7SVivek Gautam 
7578e9930e1SRob Herring 	if (of_property_present(np, "usb0_vbus_power-supply")) {
7580b56e9a7SVivek Gautam 		data->vbus_power_supply = devm_power_supply_get_by_phandle(dev,
7590b56e9a7SVivek Gautam 						     "usb0_vbus_power-supply");
760e7cded27SQuentin Schulz 		if (IS_ERR(data->vbus_power_supply)) {
761e7cded27SQuentin Schulz 			dev_err(dev, "Couldn't get the VBUS power supply\n");
7620b56e9a7SVivek Gautam 			return PTR_ERR(data->vbus_power_supply);
763e7cded27SQuentin Schulz 		}
7640b56e9a7SVivek Gautam 
7650b56e9a7SVivek Gautam 		if (!data->vbus_power_supply)
7660b56e9a7SVivek Gautam 			return -EPROBE_DEFER;
7670b56e9a7SVivek Gautam 	}
7680b56e9a7SVivek Gautam 
7690b56e9a7SVivek Gautam 	data->dr_mode = of_usb_get_dr_mode_by_phy(np, 0);
7700b56e9a7SVivek Gautam 
7710b56e9a7SVivek Gautam 	data->extcon = devm_extcon_dev_allocate(dev, sun4i_usb_phy0_cable);
772e7cded27SQuentin Schulz 	if (IS_ERR(data->extcon)) {
773e7cded27SQuentin Schulz 		dev_err(dev, "Couldn't allocate our extcon device\n");
7740b56e9a7SVivek Gautam 		return PTR_ERR(data->extcon);
775e7cded27SQuentin Schulz 	}
7760b56e9a7SVivek Gautam 
7770b56e9a7SVivek Gautam 	ret = devm_extcon_dev_register(dev, data->extcon);
7780b56e9a7SVivek Gautam 	if (ret) {
7790b56e9a7SVivek Gautam 		dev_err(dev, "failed to register extcon: %d\n", ret);
7800b56e9a7SVivek Gautam 		return ret;
7810b56e9a7SVivek Gautam 	}
7820b56e9a7SVivek Gautam 
7830b56e9a7SVivek Gautam 	for (i = 0; i < data->cfg->num_phys; i++) {
7840b56e9a7SVivek Gautam 		struct sun4i_usb_phy *phy = data->phys + i;
7859e34abc7SChristophe JAILLET 		char name[32];
7860b56e9a7SVivek Gautam 
7872659392eSIcenowy Zheng 		if (data->cfg->missing_phys & BIT(i))
7882659392eSIcenowy Zheng 			continue;
7892659392eSIcenowy Zheng 
7900b56e9a7SVivek Gautam 		snprintf(name, sizeof(name), "usb%d_vbus", i);
7910b56e9a7SVivek Gautam 		phy->vbus = devm_regulator_get_optional(dev, name);
7920b56e9a7SVivek Gautam 		if (IS_ERR(phy->vbus)) {
793e7cded27SQuentin Schulz 			if (PTR_ERR(phy->vbus) == -EPROBE_DEFER) {
794e7cded27SQuentin Schulz 				dev_err(dev,
795e7cded27SQuentin Schulz 					"Couldn't get regulator %s... Deferring probe\n",
796e7cded27SQuentin Schulz 					name);
7970b56e9a7SVivek Gautam 				return -EPROBE_DEFER;
798e7cded27SQuentin Schulz 			}
799e7cded27SQuentin Schulz 
8000b56e9a7SVivek Gautam 			phy->vbus = NULL;
8010b56e9a7SVivek Gautam 		}
8020b56e9a7SVivek Gautam 
8030b56e9a7SVivek Gautam 		if (data->cfg->dedicated_clocks)
8040b56e9a7SVivek Gautam 			snprintf(name, sizeof(name), "usb%d_phy", i);
8050b56e9a7SVivek Gautam 		else
80671351b0eSWolfram Sang 			strscpy(name, "usb_phy", sizeof(name));
8070b56e9a7SVivek Gautam 
8080b56e9a7SVivek Gautam 		phy->clk = devm_clk_get(dev, name);
8090b56e9a7SVivek Gautam 		if (IS_ERR(phy->clk)) {
8100b56e9a7SVivek Gautam 			dev_err(dev, "failed to get clock %s\n", name);
8110b56e9a7SVivek Gautam 			return PTR_ERR(phy->clk);
8120b56e9a7SVivek Gautam 		}
8130b56e9a7SVivek Gautam 
814f0152c58SChen-Yu Tsai 		/* The first PHY is always tied to OTG, and never HSIC */
815f0152c58SChen-Yu Tsai 		if (data->cfg->hsic_index && i == data->cfg->hsic_index) {
816f0152c58SChen-Yu Tsai 			/* HSIC needs secondary clock */
817f0152c58SChen-Yu Tsai 			snprintf(name, sizeof(name), "usb%d_hsic_12M", i);
818f0152c58SChen-Yu Tsai 			phy->clk2 = devm_clk_get(dev, name);
819f0152c58SChen-Yu Tsai 			if (IS_ERR(phy->clk2)) {
820f0152c58SChen-Yu Tsai 				dev_err(dev, "failed to get clock %s\n", name);
821f0152c58SChen-Yu Tsai 				return PTR_ERR(phy->clk2);
822f0152c58SChen-Yu Tsai 			}
823b45c6d80SAndre Przywara 		} else {
824b45c6d80SAndre Przywara 			snprintf(name, sizeof(name), "pmu%d_clk", i);
825b45c6d80SAndre Przywara 			phy->clk2 = devm_clk_get_optional(dev, name);
826b45c6d80SAndre Przywara 			if (IS_ERR(phy->clk2)) {
827b45c6d80SAndre Przywara 				dev_err(dev, "failed to get clock %s\n", name);
828b45c6d80SAndre Przywara 				return PTR_ERR(phy->clk2);
829b45c6d80SAndre Przywara 			}
830f0152c58SChen-Yu Tsai 		}
831f0152c58SChen-Yu Tsai 
8320b56e9a7SVivek Gautam 		snprintf(name, sizeof(name), "usb%d_reset", i);
8330b56e9a7SVivek Gautam 		phy->reset = devm_reset_control_get(dev, name);
8340b56e9a7SVivek Gautam 		if (IS_ERR(phy->reset)) {
8350b56e9a7SVivek Gautam 			dev_err(dev, "failed to get reset %s\n", name);
8360b56e9a7SVivek Gautam 			return PTR_ERR(phy->reset);
8370b56e9a7SVivek Gautam 		}
8380b56e9a7SVivek Gautam 
8390b56e9a7SVivek Gautam 		if (i || data->cfg->phy0_dual_route) { /* No pmu for musb */
8400b56e9a7SVivek Gautam 			snprintf(name, sizeof(name), "pmu%d", i);
84108d4dedaSChunfeng Yun 			phy->pmu = devm_platform_ioremap_resource_byname(pdev, name);
8420b56e9a7SVivek Gautam 			if (IS_ERR(phy->pmu))
8430b56e9a7SVivek Gautam 				return PTR_ERR(phy->pmu);
8440b56e9a7SVivek Gautam 		}
8450b56e9a7SVivek Gautam 
8460b56e9a7SVivek Gautam 		phy->phy = devm_phy_create(dev, NULL, &sun4i_usb_phy_ops);
8470b56e9a7SVivek Gautam 		if (IS_ERR(phy->phy)) {
8480b56e9a7SVivek Gautam 			dev_err(dev, "failed to create PHY %d\n", i);
8490b56e9a7SVivek Gautam 			return PTR_ERR(phy->phy);
8500b56e9a7SVivek Gautam 		}
8510b56e9a7SVivek Gautam 
8520b56e9a7SVivek Gautam 		phy->index = i;
8530b56e9a7SVivek Gautam 		phy_set_drvdata(phy->phy, &data->phys[i]);
8540b56e9a7SVivek Gautam 	}
8550b56e9a7SVivek Gautam 
8560b56e9a7SVivek Gautam 	data->id_det_irq = gpiod_to_irq(data->id_det_gpio);
8570b56e9a7SVivek Gautam 	if (data->id_det_irq > 0) {
8580b56e9a7SVivek Gautam 		ret = devm_request_irq(dev, data->id_det_irq,
8590b56e9a7SVivek Gautam 				sun4i_usb_phy0_id_vbus_det_irq,
8600b56e9a7SVivek Gautam 				IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
8610b56e9a7SVivek Gautam 				"usb0-id-det", data);
8620b56e9a7SVivek Gautam 		if (ret) {
8630b56e9a7SVivek Gautam 			dev_err(dev, "Err requesting id-det-irq: %d\n", ret);
8640b56e9a7SVivek Gautam 			return ret;
8650b56e9a7SVivek Gautam 		}
8660b56e9a7SVivek Gautam 	}
8670b56e9a7SVivek Gautam 
8680b56e9a7SVivek Gautam 	data->vbus_det_irq = gpiod_to_irq(data->vbus_det_gpio);
8690b56e9a7SVivek Gautam 	if (data->vbus_det_irq > 0) {
8700b56e9a7SVivek Gautam 		ret = devm_request_irq(dev, data->vbus_det_irq,
8710b56e9a7SVivek Gautam 				sun4i_usb_phy0_id_vbus_det_irq,
8720b56e9a7SVivek Gautam 				IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
8730b56e9a7SVivek Gautam 				"usb0-vbus-det", data);
8740b56e9a7SVivek Gautam 		if (ret) {
8750b56e9a7SVivek Gautam 			dev_err(dev, "Err requesting vbus-det-irq: %d\n", ret);
8760b56e9a7SVivek Gautam 			data->vbus_det_irq = -1;
8770b56e9a7SVivek Gautam 			sun4i_usb_phy_remove(pdev); /* Stop detect work */
8780b56e9a7SVivek Gautam 			return ret;
8790b56e9a7SVivek Gautam 		}
8800b56e9a7SVivek Gautam 	}
8810b56e9a7SVivek Gautam 
8820b56e9a7SVivek Gautam 	if (data->vbus_power_supply) {
8830b56e9a7SVivek Gautam 		data->vbus_power_nb.notifier_call = sun4i_usb_phy0_vbus_notify;
8840b56e9a7SVivek Gautam 		data->vbus_power_nb.priority = 0;
8850b56e9a7SVivek Gautam 		ret = power_supply_reg_notifier(&data->vbus_power_nb);
8860b56e9a7SVivek Gautam 		if (ret) {
8870b56e9a7SVivek Gautam 			sun4i_usb_phy_remove(pdev); /* Stop detect work */
8880b56e9a7SVivek Gautam 			return ret;
8890b56e9a7SVivek Gautam 		}
8900b56e9a7SVivek Gautam 		data->vbus_power_nb_registered = true;
8910b56e9a7SVivek Gautam 	}
8920b56e9a7SVivek Gautam 
8930b56e9a7SVivek Gautam 	phy_provider = devm_of_phy_provider_register(dev, sun4i_usb_phy_xlate);
8940b56e9a7SVivek Gautam 	if (IS_ERR(phy_provider)) {
8950b56e9a7SVivek Gautam 		sun4i_usb_phy_remove(pdev); /* Stop detect work */
8960b56e9a7SVivek Gautam 		return PTR_ERR(phy_provider);
8970b56e9a7SVivek Gautam 	}
8980b56e9a7SVivek Gautam 
899e7cded27SQuentin Schulz 	dev_dbg(dev, "successfully loaded\n");
900e7cded27SQuentin Schulz 
9010b56e9a7SVivek Gautam 	return 0;
9020b56e9a7SVivek Gautam }
9030b56e9a7SVivek Gautam 
90450bd67abSIcenowy Zheng static const struct sun4i_usb_phy_cfg suniv_f1c100s_cfg = {
90550bd67abSIcenowy Zheng 	.num_phys = 1,
90650bd67abSIcenowy Zheng 	.disc_thresh = 3,
90750bd67abSIcenowy Zheng 	.phyctl_offset = REG_PHYCTL_A10,
90850bd67abSIcenowy Zheng 	.dedicated_clocks = true,
90950bd67abSIcenowy Zheng };
91050bd67abSIcenowy Zheng 
9110b56e9a7SVivek Gautam static const struct sun4i_usb_phy_cfg sun4i_a10_cfg = {
9120b56e9a7SVivek Gautam 	.num_phys = 3,
9130b56e9a7SVivek Gautam 	.disc_thresh = 3,
9140b56e9a7SVivek Gautam 	.phyctl_offset = REG_PHYCTL_A10,
9150b56e9a7SVivek Gautam 	.dedicated_clocks = false,
9160b56e9a7SVivek Gautam };
9170b56e9a7SVivek Gautam 
9180b56e9a7SVivek Gautam static const struct sun4i_usb_phy_cfg sun5i_a13_cfg = {
9190b56e9a7SVivek Gautam 	.num_phys = 2,
9200b56e9a7SVivek Gautam 	.disc_thresh = 2,
9210b56e9a7SVivek Gautam 	.phyctl_offset = REG_PHYCTL_A10,
9220b56e9a7SVivek Gautam 	.dedicated_clocks = false,
9230b56e9a7SVivek Gautam };
9240b56e9a7SVivek Gautam 
9250b56e9a7SVivek Gautam static const struct sun4i_usb_phy_cfg sun6i_a31_cfg = {
9260b56e9a7SVivek Gautam 	.num_phys = 3,
9270b56e9a7SVivek Gautam 	.disc_thresh = 3,
9280b56e9a7SVivek Gautam 	.phyctl_offset = REG_PHYCTL_A10,
9290b56e9a7SVivek Gautam 	.dedicated_clocks = true,
9308dd256baSAndre Przywara 	.poll_vbusen = true,
9310b56e9a7SVivek Gautam };
9320b56e9a7SVivek Gautam 
9330b56e9a7SVivek Gautam static const struct sun4i_usb_phy_cfg sun7i_a20_cfg = {
9340b56e9a7SVivek Gautam 	.num_phys = 3,
9350b56e9a7SVivek Gautam 	.disc_thresh = 2,
9360b56e9a7SVivek Gautam 	.phyctl_offset = REG_PHYCTL_A10,
9370b56e9a7SVivek Gautam 	.dedicated_clocks = false,
9380b56e9a7SVivek Gautam };
9390b56e9a7SVivek Gautam 
9400b56e9a7SVivek Gautam static const struct sun4i_usb_phy_cfg sun8i_a23_cfg = {
9410b56e9a7SVivek Gautam 	.num_phys = 2,
9420b56e9a7SVivek Gautam 	.disc_thresh = 3,
9430b56e9a7SVivek Gautam 	.phyctl_offset = REG_PHYCTL_A10,
9440b56e9a7SVivek Gautam 	.dedicated_clocks = true,
9458dd256baSAndre Przywara 	.poll_vbusen = true,
9460b56e9a7SVivek Gautam };
9470b56e9a7SVivek Gautam 
9480b56e9a7SVivek Gautam static const struct sun4i_usb_phy_cfg sun8i_a33_cfg = {
9490b56e9a7SVivek Gautam 	.num_phys = 2,
9500b56e9a7SVivek Gautam 	.disc_thresh = 3,
9510b56e9a7SVivek Gautam 	.phyctl_offset = REG_PHYCTL_A33,
9520b56e9a7SVivek Gautam 	.dedicated_clocks = true,
9538dd256baSAndre Przywara 	.poll_vbusen = true,
9540b56e9a7SVivek Gautam };
9550b56e9a7SVivek Gautam 
9564b63743cSChen-Yu Tsai static const struct sun4i_usb_phy_cfg sun8i_a83t_cfg = {
9574b63743cSChen-Yu Tsai 	.num_phys = 3,
9584b63743cSChen-Yu Tsai 	.hsic_index = 2,
9594b63743cSChen-Yu Tsai 	.phyctl_offset = REG_PHYCTL_A33,
9604b63743cSChen-Yu Tsai 	.dedicated_clocks = true,
9618dd256baSAndre Przywara 	.siddq_in_base = true,
9624b63743cSChen-Yu Tsai };
9634b63743cSChen-Yu Tsai 
9640b56e9a7SVivek Gautam static const struct sun4i_usb_phy_cfg sun8i_h3_cfg = {
9650b56e9a7SVivek Gautam 	.num_phys = 4,
9660b56e9a7SVivek Gautam 	.disc_thresh = 3,
9670b56e9a7SVivek Gautam 	.phyctl_offset = REG_PHYCTL_A33,
9680b56e9a7SVivek Gautam 	.dedicated_clocks = true,
9691743dea7SAndre Przywara 	.hci_phy_ctl_clear = PHY_CTL_H3_SIDDQ,
9700b56e9a7SVivek Gautam 	.phy0_dual_route = true,
9710b56e9a7SVivek Gautam };
9720b56e9a7SVivek Gautam 
973f3d96f8dSIcenowy Zheng static const struct sun4i_usb_phy_cfg sun8i_r40_cfg = {
974f3d96f8dSIcenowy Zheng 	.num_phys = 3,
975f3d96f8dSIcenowy Zheng 	.disc_thresh = 3,
976f3d96f8dSIcenowy Zheng 	.phyctl_offset = REG_PHYCTL_A33,
977f3d96f8dSIcenowy Zheng 	.dedicated_clocks = true,
9781743dea7SAndre Przywara 	.hci_phy_ctl_clear = PHY_CTL_H3_SIDDQ,
979f3d96f8dSIcenowy Zheng 	.phy0_dual_route = true,
980f3d96f8dSIcenowy Zheng };
981f3d96f8dSIcenowy Zheng 
9820b56e9a7SVivek Gautam static const struct sun4i_usb_phy_cfg sun8i_v3s_cfg = {
9830b56e9a7SVivek Gautam 	.num_phys = 1,
9840b56e9a7SVivek Gautam 	.disc_thresh = 3,
9850b56e9a7SVivek Gautam 	.phyctl_offset = REG_PHYCTL_A33,
9860b56e9a7SVivek Gautam 	.dedicated_clocks = true,
9871743dea7SAndre Przywara 	.hci_phy_ctl_clear = PHY_CTL_H3_SIDDQ,
988a06173baSIcenowy Zheng 	.phy0_dual_route = true,
9890b56e9a7SVivek Gautam };
9900b56e9a7SVivek Gautam 
991204642e7SSamuel Holland static const struct sun4i_usb_phy_cfg sun20i_d1_cfg = {
992204642e7SSamuel Holland 	.num_phys = 2,
993204642e7SSamuel Holland 	.phyctl_offset = REG_PHYCTL_A33,
994204642e7SSamuel Holland 	.dedicated_clocks = true,
995204642e7SSamuel Holland 	.hci_phy_ctl_clear = PHY_CTL_SIDDQ,
996204642e7SSamuel Holland 	.phy0_dual_route = true,
9978dd256baSAndre Przywara 	.siddq_in_base = true,
998204642e7SSamuel Holland };
999204642e7SSamuel Holland 
10000b56e9a7SVivek Gautam static const struct sun4i_usb_phy_cfg sun50i_a64_cfg = {
10010b56e9a7SVivek Gautam 	.num_phys = 2,
10020b56e9a7SVivek Gautam 	.disc_thresh = 3,
10030b56e9a7SVivek Gautam 	.phyctl_offset = REG_PHYCTL_A33,
10040b56e9a7SVivek Gautam 	.dedicated_clocks = true,
10051743dea7SAndre Przywara 	.hci_phy_ctl_clear = PHY_CTL_H3_SIDDQ,
10060b56e9a7SVivek Gautam 	.phy0_dual_route = true,
10070b56e9a7SVivek Gautam };
10080b56e9a7SVivek Gautam 
1009ae409cc7SIcenowy Zheng static const struct sun4i_usb_phy_cfg sun50i_h6_cfg = {
1010ae409cc7SIcenowy Zheng 	.num_phys = 4,
1011ae409cc7SIcenowy Zheng 	.phyctl_offset = REG_PHYCTL_A33,
1012ae409cc7SIcenowy Zheng 	.dedicated_clocks = true,
1013ae409cc7SIcenowy Zheng 	.phy0_dual_route = true,
1014ae409cc7SIcenowy Zheng 	.missing_phys = BIT(1) | BIT(2),
10158dd256baSAndre Przywara 	.siddq_in_base = true,
1016ae409cc7SIcenowy Zheng };
1017ae409cc7SIcenowy Zheng 
10180f607406SAndre Przywara static const struct sun4i_usb_phy_cfg sun50i_h616_cfg = {
10190f607406SAndre Przywara 	.num_phys = 4,
10200f607406SAndre Przywara 	.disc_thresh = 3,
10210f607406SAndre Przywara 	.phyctl_offset = REG_PHYCTL_A33,
10220f607406SAndre Przywara 	.dedicated_clocks = true,
10230f607406SAndre Przywara 	.phy0_dual_route = true,
10240f607406SAndre Przywara 	.hci_phy_ctl_clear = PHY_CTL_SIDDQ,
10250f607406SAndre Przywara 	.needs_phy2_siddq = true,
10268dd256baSAndre Przywara 	.siddq_in_base = true,
10270f607406SAndre Przywara };
10280f607406SAndre Przywara 
10290b56e9a7SVivek Gautam static const struct of_device_id sun4i_usb_phy_of_match[] = {
10300b56e9a7SVivek Gautam 	{ .compatible = "allwinner,sun4i-a10-usb-phy", .data = &sun4i_a10_cfg },
10310b56e9a7SVivek Gautam 	{ .compatible = "allwinner,sun5i-a13-usb-phy", .data = &sun5i_a13_cfg },
10320b56e9a7SVivek Gautam 	{ .compatible = "allwinner,sun6i-a31-usb-phy", .data = &sun6i_a31_cfg },
10330b56e9a7SVivek Gautam 	{ .compatible = "allwinner,sun7i-a20-usb-phy", .data = &sun7i_a20_cfg },
10340b56e9a7SVivek Gautam 	{ .compatible = "allwinner,sun8i-a23-usb-phy", .data = &sun8i_a23_cfg },
10350b56e9a7SVivek Gautam 	{ .compatible = "allwinner,sun8i-a33-usb-phy", .data = &sun8i_a33_cfg },
10364b63743cSChen-Yu Tsai 	{ .compatible = "allwinner,sun8i-a83t-usb-phy", .data = &sun8i_a83t_cfg },
10370b56e9a7SVivek Gautam 	{ .compatible = "allwinner,sun8i-h3-usb-phy", .data = &sun8i_h3_cfg },
1038f3d96f8dSIcenowy Zheng 	{ .compatible = "allwinner,sun8i-r40-usb-phy", .data = &sun8i_r40_cfg },
10390b56e9a7SVivek Gautam 	{ .compatible = "allwinner,sun8i-v3s-usb-phy", .data = &sun8i_v3s_cfg },
1040204642e7SSamuel Holland 	{ .compatible = "allwinner,sun20i-d1-usb-phy", .data = &sun20i_d1_cfg },
10410b56e9a7SVivek Gautam 	{ .compatible = "allwinner,sun50i-a64-usb-phy",
10420b56e9a7SVivek Gautam 	  .data = &sun50i_a64_cfg},
1043ae409cc7SIcenowy Zheng 	{ .compatible = "allwinner,sun50i-h6-usb-phy", .data = &sun50i_h6_cfg },
10440f607406SAndre Przywara 	{ .compatible = "allwinner,sun50i-h616-usb-phy", .data = &sun50i_h616_cfg },
104550bd67abSIcenowy Zheng 	{ .compatible = "allwinner,suniv-f1c100s-usb-phy",
104650bd67abSIcenowy Zheng 	  .data = &suniv_f1c100s_cfg },
10470b56e9a7SVivek Gautam 	{ },
10480b56e9a7SVivek Gautam };
10490b56e9a7SVivek Gautam MODULE_DEVICE_TABLE(of, sun4i_usb_phy_of_match);
10500b56e9a7SVivek Gautam 
10510b56e9a7SVivek Gautam static struct platform_driver sun4i_usb_phy_driver = {
10520b56e9a7SVivek Gautam 	.probe	= sun4i_usb_phy_probe,
1053b4700ed8SUwe Kleine-König 	.remove_new = sun4i_usb_phy_remove,
10540b56e9a7SVivek Gautam 	.driver = {
10550b56e9a7SVivek Gautam 		.of_match_table	= sun4i_usb_phy_of_match,
10560b56e9a7SVivek Gautam 		.name  = "sun4i-usb-phy",
10570b56e9a7SVivek Gautam 	}
10580b56e9a7SVivek Gautam };
10590b56e9a7SVivek Gautam module_platform_driver(sun4i_usb_phy_driver);
10600b56e9a7SVivek Gautam 
10610b56e9a7SVivek Gautam MODULE_DESCRIPTION("Allwinner sun4i USB phy driver");
10620b56e9a7SVivek Gautam MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
10630b56e9a7SVivek Gautam MODULE_LICENSE("GPL v2");
1064