18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Rockchip usb PHY driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2014 Yunzhi Li <lyz@rock-chips.com>
68c2ecf20Sopenharmony_ci * Copyright (C) 2014 ROCKCHIP, Inc.
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/clk.h>
108c2ecf20Sopenharmony_ci#include <linux/clk-provider.h>
118c2ecf20Sopenharmony_ci#include <linux/io.h>
128c2ecf20Sopenharmony_ci#include <linux/kernel.h>
138c2ecf20Sopenharmony_ci#include <linux/module.h>
148c2ecf20Sopenharmony_ci#include <linux/mutex.h>
158c2ecf20Sopenharmony_ci#include <linux/of.h>
168c2ecf20Sopenharmony_ci#include <linux/of_address.h>
178c2ecf20Sopenharmony_ci#include <linux/of_platform.h>
188c2ecf20Sopenharmony_ci#include <linux/phy/phy.h>
198c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
208c2ecf20Sopenharmony_ci#include <linux/regulator/consumer.h>
218c2ecf20Sopenharmony_ci#include <linux/reset.h>
228c2ecf20Sopenharmony_ci#include <linux/regmap.h>
238c2ecf20Sopenharmony_ci#include <linux/mfd/syscon.h>
248c2ecf20Sopenharmony_ci#include <linux/delay.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cistatic int enable_usb_uart;
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#define HIWORD_UPDATE(val, mask) \
298c2ecf20Sopenharmony_ci		((val) | (mask) << 16)
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#define UOC_CON0					0x00
328c2ecf20Sopenharmony_ci#define UOC_CON0_SIDDQ					BIT(13)
338c2ecf20Sopenharmony_ci#define UOC_CON0_DISABLE				BIT(4)
348c2ecf20Sopenharmony_ci#define UOC_CON0_COMMON_ON_N				BIT(0)
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#define UOC_CON2					0x08
378c2ecf20Sopenharmony_ci#define UOC_CON2_SOFT_CON_SEL				BIT(2)
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#define UOC_CON3					0x0c
408c2ecf20Sopenharmony_ci/* bits present on rk3188 and rk3288 phys */
418c2ecf20Sopenharmony_ci#define UOC_CON3_UTMI_TERMSEL_FULLSPEED			BIT(5)
428c2ecf20Sopenharmony_ci#define UOC_CON3_UTMI_XCVRSEELCT_FSTRANSC		(1 << 3)
438c2ecf20Sopenharmony_ci#define UOC_CON3_UTMI_XCVRSEELCT_MASK			(3 << 3)
448c2ecf20Sopenharmony_ci#define UOC_CON3_UTMI_OPMODE_NODRIVING			(1 << 1)
458c2ecf20Sopenharmony_ci#define UOC_CON3_UTMI_OPMODE_MASK			(3 << 1)
468c2ecf20Sopenharmony_ci#define UOC_CON3_UTMI_SUSPENDN				BIT(0)
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cistruct rockchip_usb_phys {
498c2ecf20Sopenharmony_ci	int reg;
508c2ecf20Sopenharmony_ci	const char *pll_name;
518c2ecf20Sopenharmony_ci};
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistruct rockchip_usb_phy_base;
548c2ecf20Sopenharmony_cistruct rockchip_usb_phy_pdata {
558c2ecf20Sopenharmony_ci	struct rockchip_usb_phys *phys;
568c2ecf20Sopenharmony_ci	int (*init_usb_uart)(struct regmap *grf,
578c2ecf20Sopenharmony_ci			     const struct rockchip_usb_phy_pdata *pdata);
588c2ecf20Sopenharmony_ci	int usb_uart_phy;
598c2ecf20Sopenharmony_ci};
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_cistruct rockchip_usb_phy_base {
628c2ecf20Sopenharmony_ci	struct device *dev;
638c2ecf20Sopenharmony_ci	struct regmap *reg_base;
648c2ecf20Sopenharmony_ci	const struct rockchip_usb_phy_pdata *pdata;
658c2ecf20Sopenharmony_ci};
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_cistruct rockchip_usb_phy {
688c2ecf20Sopenharmony_ci	struct rockchip_usb_phy_base *base;
698c2ecf20Sopenharmony_ci	struct device_node *np;
708c2ecf20Sopenharmony_ci	unsigned int	reg_offset;
718c2ecf20Sopenharmony_ci	struct clk	*clk;
728c2ecf20Sopenharmony_ci	struct clk      *clk480m;
738c2ecf20Sopenharmony_ci	struct clk_hw	clk480m_hw;
748c2ecf20Sopenharmony_ci	struct phy	*phy;
758c2ecf20Sopenharmony_ci	bool		uart_enabled;
768c2ecf20Sopenharmony_ci	struct reset_control *reset;
778c2ecf20Sopenharmony_ci	struct regulator *vbus;
788c2ecf20Sopenharmony_ci};
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_cistatic int rockchip_usb_phy_power(struct rockchip_usb_phy *phy,
818c2ecf20Sopenharmony_ci					   bool siddq)
828c2ecf20Sopenharmony_ci{
838c2ecf20Sopenharmony_ci	u32 val = HIWORD_UPDATE(siddq ? UOC_CON0_SIDDQ : 0, UOC_CON0_SIDDQ);
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	return regmap_write(phy->base->reg_base, phy->reg_offset, val);
868c2ecf20Sopenharmony_ci}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistatic unsigned long rockchip_usb_phy480m_recalc_rate(struct clk_hw *hw,
898c2ecf20Sopenharmony_ci						unsigned long parent_rate)
908c2ecf20Sopenharmony_ci{
918c2ecf20Sopenharmony_ci	return 480000000;
928c2ecf20Sopenharmony_ci}
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_cistatic void rockchip_usb_phy480m_disable(struct clk_hw *hw)
958c2ecf20Sopenharmony_ci{
968c2ecf20Sopenharmony_ci	struct rockchip_usb_phy *phy = container_of(hw,
978c2ecf20Sopenharmony_ci						    struct rockchip_usb_phy,
988c2ecf20Sopenharmony_ci						    clk480m_hw);
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	if (phy->vbus)
1018c2ecf20Sopenharmony_ci		regulator_disable(phy->vbus);
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	/* Power down usb phy analog blocks by set siddq 1 */
1048c2ecf20Sopenharmony_ci	rockchip_usb_phy_power(phy, 1);
1058c2ecf20Sopenharmony_ci}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cistatic int rockchip_usb_phy480m_enable(struct clk_hw *hw)
1088c2ecf20Sopenharmony_ci{
1098c2ecf20Sopenharmony_ci	struct rockchip_usb_phy *phy = container_of(hw,
1108c2ecf20Sopenharmony_ci						    struct rockchip_usb_phy,
1118c2ecf20Sopenharmony_ci						    clk480m_hw);
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	/* Power up usb phy analog blocks by set siddq 0 */
1148c2ecf20Sopenharmony_ci	return rockchip_usb_phy_power(phy, 0);
1158c2ecf20Sopenharmony_ci}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_cistatic int rockchip_usb_phy480m_is_enabled(struct clk_hw *hw)
1188c2ecf20Sopenharmony_ci{
1198c2ecf20Sopenharmony_ci	struct rockchip_usb_phy *phy = container_of(hw,
1208c2ecf20Sopenharmony_ci						    struct rockchip_usb_phy,
1218c2ecf20Sopenharmony_ci						    clk480m_hw);
1228c2ecf20Sopenharmony_ci	int ret;
1238c2ecf20Sopenharmony_ci	u32 val;
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	ret = regmap_read(phy->base->reg_base, phy->reg_offset, &val);
1268c2ecf20Sopenharmony_ci	if (ret < 0)
1278c2ecf20Sopenharmony_ci		return ret;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	return (val & UOC_CON0_SIDDQ) ? 0 : 1;
1308c2ecf20Sopenharmony_ci}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_cistatic const struct clk_ops rockchip_usb_phy480m_ops = {
1338c2ecf20Sopenharmony_ci	.enable = rockchip_usb_phy480m_enable,
1348c2ecf20Sopenharmony_ci	.disable = rockchip_usb_phy480m_disable,
1358c2ecf20Sopenharmony_ci	.is_enabled = rockchip_usb_phy480m_is_enabled,
1368c2ecf20Sopenharmony_ci	.recalc_rate = rockchip_usb_phy480m_recalc_rate,
1378c2ecf20Sopenharmony_ci};
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_cistatic int rockchip_usb_phy_power_off(struct phy *_phy)
1408c2ecf20Sopenharmony_ci{
1418c2ecf20Sopenharmony_ci	struct rockchip_usb_phy *phy = phy_get_drvdata(_phy);
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	if (phy->uart_enabled)
1448c2ecf20Sopenharmony_ci		return -EBUSY;
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	clk_disable_unprepare(phy->clk480m);
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	return 0;
1498c2ecf20Sopenharmony_ci}
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_cistatic int rockchip_usb_phy_power_on(struct phy *_phy)
1528c2ecf20Sopenharmony_ci{
1538c2ecf20Sopenharmony_ci	struct rockchip_usb_phy *phy = phy_get_drvdata(_phy);
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	if (phy->uart_enabled)
1568c2ecf20Sopenharmony_ci		return -EBUSY;
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	if (phy->vbus) {
1598c2ecf20Sopenharmony_ci		int ret;
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci		ret = regulator_enable(phy->vbus);
1628c2ecf20Sopenharmony_ci		if (ret)
1638c2ecf20Sopenharmony_ci			return ret;
1648c2ecf20Sopenharmony_ci	}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	return clk_prepare_enable(phy->clk480m);
1678c2ecf20Sopenharmony_ci}
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_cistatic int rockchip_usb_phy_reset(struct phy *_phy)
1708c2ecf20Sopenharmony_ci{
1718c2ecf20Sopenharmony_ci	struct rockchip_usb_phy *phy = phy_get_drvdata(_phy);
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	if (phy->reset) {
1748c2ecf20Sopenharmony_ci		reset_control_assert(phy->reset);
1758c2ecf20Sopenharmony_ci		udelay(10);
1768c2ecf20Sopenharmony_ci		reset_control_deassert(phy->reset);
1778c2ecf20Sopenharmony_ci	}
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	return 0;
1808c2ecf20Sopenharmony_ci}
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_cistatic const struct phy_ops ops = {
1838c2ecf20Sopenharmony_ci	.power_on	= rockchip_usb_phy_power_on,
1848c2ecf20Sopenharmony_ci	.power_off	= rockchip_usb_phy_power_off,
1858c2ecf20Sopenharmony_ci	.reset		= rockchip_usb_phy_reset,
1868c2ecf20Sopenharmony_ci	.owner		= THIS_MODULE,
1878c2ecf20Sopenharmony_ci};
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_cistatic void rockchip_usb_phy_action(void *data)
1908c2ecf20Sopenharmony_ci{
1918c2ecf20Sopenharmony_ci	struct rockchip_usb_phy *rk_phy = data;
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	if (!rk_phy->uart_enabled) {
1948c2ecf20Sopenharmony_ci		of_clk_del_provider(rk_phy->np);
1958c2ecf20Sopenharmony_ci		clk_unregister(rk_phy->clk480m);
1968c2ecf20Sopenharmony_ci	}
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	if (rk_phy->clk)
1998c2ecf20Sopenharmony_ci		clk_put(rk_phy->clk);
2008c2ecf20Sopenharmony_ci}
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_cistatic int rockchip_usb_phy_init(struct rockchip_usb_phy_base *base,
2038c2ecf20Sopenharmony_ci				 struct device_node *child)
2048c2ecf20Sopenharmony_ci{
2058c2ecf20Sopenharmony_ci	struct rockchip_usb_phy *rk_phy;
2068c2ecf20Sopenharmony_ci	unsigned int reg_offset;
2078c2ecf20Sopenharmony_ci	const char *clk_name;
2088c2ecf20Sopenharmony_ci	struct clk_init_data init;
2098c2ecf20Sopenharmony_ci	int err, i;
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	rk_phy = devm_kzalloc(base->dev, sizeof(*rk_phy), GFP_KERNEL);
2128c2ecf20Sopenharmony_ci	if (!rk_phy)
2138c2ecf20Sopenharmony_ci		return -ENOMEM;
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	rk_phy->base = base;
2168c2ecf20Sopenharmony_ci	rk_phy->np = child;
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	if (of_property_read_u32(child, "reg", &reg_offset)) {
2198c2ecf20Sopenharmony_ci		dev_err(base->dev, "missing reg property in node %pOFn\n",
2208c2ecf20Sopenharmony_ci			child);
2218c2ecf20Sopenharmony_ci		return -EINVAL;
2228c2ecf20Sopenharmony_ci	}
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	rk_phy->reset = of_reset_control_get(child, "phy-reset");
2258c2ecf20Sopenharmony_ci	if (IS_ERR(rk_phy->reset))
2268c2ecf20Sopenharmony_ci		rk_phy->reset = NULL;
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	rk_phy->reg_offset = reg_offset;
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	rk_phy->clk = of_clk_get_by_name(child, "phyclk");
2318c2ecf20Sopenharmony_ci	if (IS_ERR(rk_phy->clk))
2328c2ecf20Sopenharmony_ci		rk_phy->clk = NULL;
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	i = 0;
2358c2ecf20Sopenharmony_ci	init.name = NULL;
2368c2ecf20Sopenharmony_ci	while (base->pdata->phys[i].reg) {
2378c2ecf20Sopenharmony_ci		if (base->pdata->phys[i].reg == reg_offset) {
2388c2ecf20Sopenharmony_ci			init.name = base->pdata->phys[i].pll_name;
2398c2ecf20Sopenharmony_ci			break;
2408c2ecf20Sopenharmony_ci		}
2418c2ecf20Sopenharmony_ci		i++;
2428c2ecf20Sopenharmony_ci	}
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	if (!init.name) {
2458c2ecf20Sopenharmony_ci		dev_err(base->dev, "phy data not found\n");
2468c2ecf20Sopenharmony_ci		return -EINVAL;
2478c2ecf20Sopenharmony_ci	}
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	if (enable_usb_uart && base->pdata->usb_uart_phy == i) {
2508c2ecf20Sopenharmony_ci		dev_dbg(base->dev, "phy%d used as uart output\n", i);
2518c2ecf20Sopenharmony_ci		rk_phy->uart_enabled = true;
2528c2ecf20Sopenharmony_ci	} else {
2538c2ecf20Sopenharmony_ci		if (rk_phy->clk) {
2548c2ecf20Sopenharmony_ci			clk_name = __clk_get_name(rk_phy->clk);
2558c2ecf20Sopenharmony_ci			init.flags = 0;
2568c2ecf20Sopenharmony_ci			init.parent_names = &clk_name;
2578c2ecf20Sopenharmony_ci			init.num_parents = 1;
2588c2ecf20Sopenharmony_ci		} else {
2598c2ecf20Sopenharmony_ci			init.flags = 0;
2608c2ecf20Sopenharmony_ci			init.parent_names = NULL;
2618c2ecf20Sopenharmony_ci			init.num_parents = 0;
2628c2ecf20Sopenharmony_ci		}
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci		init.ops = &rockchip_usb_phy480m_ops;
2658c2ecf20Sopenharmony_ci		rk_phy->clk480m_hw.init = &init;
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci		rk_phy->clk480m = clk_register(base->dev, &rk_phy->clk480m_hw);
2688c2ecf20Sopenharmony_ci		if (IS_ERR(rk_phy->clk480m)) {
2698c2ecf20Sopenharmony_ci			err = PTR_ERR(rk_phy->clk480m);
2708c2ecf20Sopenharmony_ci			goto err_clk;
2718c2ecf20Sopenharmony_ci		}
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci		err = of_clk_add_provider(child, of_clk_src_simple_get,
2748c2ecf20Sopenharmony_ci					rk_phy->clk480m);
2758c2ecf20Sopenharmony_ci		if (err < 0)
2768c2ecf20Sopenharmony_ci			goto err_clk_prov;
2778c2ecf20Sopenharmony_ci	}
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	err = devm_add_action_or_reset(base->dev, rockchip_usb_phy_action,
2808c2ecf20Sopenharmony_ci				       rk_phy);
2818c2ecf20Sopenharmony_ci	if (err)
2828c2ecf20Sopenharmony_ci		return err;
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	rk_phy->phy = devm_phy_create(base->dev, child, &ops);
2858c2ecf20Sopenharmony_ci	if (IS_ERR(rk_phy->phy)) {
2868c2ecf20Sopenharmony_ci		dev_err(base->dev, "failed to create PHY\n");
2878c2ecf20Sopenharmony_ci		return PTR_ERR(rk_phy->phy);
2888c2ecf20Sopenharmony_ci	}
2898c2ecf20Sopenharmony_ci	phy_set_drvdata(rk_phy->phy, rk_phy);
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	rk_phy->vbus = devm_regulator_get_optional(&rk_phy->phy->dev, "vbus");
2928c2ecf20Sopenharmony_ci	if (IS_ERR(rk_phy->vbus)) {
2938c2ecf20Sopenharmony_ci		if (PTR_ERR(rk_phy->vbus) == -EPROBE_DEFER)
2948c2ecf20Sopenharmony_ci			return PTR_ERR(rk_phy->vbus);
2958c2ecf20Sopenharmony_ci		rk_phy->vbus = NULL;
2968c2ecf20Sopenharmony_ci	}
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci	/*
2998c2ecf20Sopenharmony_ci	 * When acting as uart-pipe, just keep clock on otherwise
3008c2ecf20Sopenharmony_ci	 * only power up usb phy when it use, so disable it when init
3018c2ecf20Sopenharmony_ci	 */
3028c2ecf20Sopenharmony_ci	if (rk_phy->uart_enabled)
3038c2ecf20Sopenharmony_ci		return clk_prepare_enable(rk_phy->clk);
3048c2ecf20Sopenharmony_ci	else
3058c2ecf20Sopenharmony_ci		return rockchip_usb_phy_power(rk_phy, 1);
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_cierr_clk_prov:
3088c2ecf20Sopenharmony_ci	if (!rk_phy->uart_enabled)
3098c2ecf20Sopenharmony_ci		clk_unregister(rk_phy->clk480m);
3108c2ecf20Sopenharmony_cierr_clk:
3118c2ecf20Sopenharmony_ci	if (rk_phy->clk)
3128c2ecf20Sopenharmony_ci		clk_put(rk_phy->clk);
3138c2ecf20Sopenharmony_ci	return err;
3148c2ecf20Sopenharmony_ci}
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_cistatic const struct rockchip_usb_phy_pdata rk3066a_pdata = {
3178c2ecf20Sopenharmony_ci	.phys = (struct rockchip_usb_phys[]){
3188c2ecf20Sopenharmony_ci		{ .reg = 0x17c, .pll_name = "sclk_otgphy0_480m" },
3198c2ecf20Sopenharmony_ci		{ .reg = 0x188, .pll_name = "sclk_otgphy1_480m" },
3208c2ecf20Sopenharmony_ci		{ /* sentinel */ }
3218c2ecf20Sopenharmony_ci	},
3228c2ecf20Sopenharmony_ci};
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_cistatic int __init rockchip_init_usb_uart_common(struct regmap *grf,
3258c2ecf20Sopenharmony_ci				const struct rockchip_usb_phy_pdata *pdata)
3268c2ecf20Sopenharmony_ci{
3278c2ecf20Sopenharmony_ci	int regoffs = pdata->phys[pdata->usb_uart_phy].reg;
3288c2ecf20Sopenharmony_ci	int ret;
3298c2ecf20Sopenharmony_ci	u32 val;
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	/*
3328c2ecf20Sopenharmony_ci	 * COMMON_ON and DISABLE settings are described in the TRM,
3338c2ecf20Sopenharmony_ci	 * but were not present in the original code.
3348c2ecf20Sopenharmony_ci	 * Also disable the analog phy components to save power.
3358c2ecf20Sopenharmony_ci	 */
3368c2ecf20Sopenharmony_ci	val = HIWORD_UPDATE(UOC_CON0_COMMON_ON_N
3378c2ecf20Sopenharmony_ci				| UOC_CON0_DISABLE
3388c2ecf20Sopenharmony_ci				| UOC_CON0_SIDDQ,
3398c2ecf20Sopenharmony_ci			    UOC_CON0_COMMON_ON_N
3408c2ecf20Sopenharmony_ci				| UOC_CON0_DISABLE
3418c2ecf20Sopenharmony_ci				| UOC_CON0_SIDDQ);
3428c2ecf20Sopenharmony_ci	ret = regmap_write(grf, regoffs + UOC_CON0, val);
3438c2ecf20Sopenharmony_ci	if (ret)
3448c2ecf20Sopenharmony_ci		return ret;
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	val = HIWORD_UPDATE(UOC_CON2_SOFT_CON_SEL,
3478c2ecf20Sopenharmony_ci			    UOC_CON2_SOFT_CON_SEL);
3488c2ecf20Sopenharmony_ci	ret = regmap_write(grf, regoffs + UOC_CON2, val);
3498c2ecf20Sopenharmony_ci	if (ret)
3508c2ecf20Sopenharmony_ci		return ret;
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci	val = HIWORD_UPDATE(UOC_CON3_UTMI_OPMODE_NODRIVING
3538c2ecf20Sopenharmony_ci				| UOC_CON3_UTMI_XCVRSEELCT_FSTRANSC
3548c2ecf20Sopenharmony_ci				| UOC_CON3_UTMI_TERMSEL_FULLSPEED,
3558c2ecf20Sopenharmony_ci			    UOC_CON3_UTMI_SUSPENDN
3568c2ecf20Sopenharmony_ci				| UOC_CON3_UTMI_OPMODE_MASK
3578c2ecf20Sopenharmony_ci				| UOC_CON3_UTMI_XCVRSEELCT_MASK
3588c2ecf20Sopenharmony_ci				| UOC_CON3_UTMI_TERMSEL_FULLSPEED);
3598c2ecf20Sopenharmony_ci	ret = regmap_write(grf, UOC_CON3, val);
3608c2ecf20Sopenharmony_ci	if (ret)
3618c2ecf20Sopenharmony_ci		return ret;
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	return 0;
3648c2ecf20Sopenharmony_ci}
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci#define RK3188_UOC0_CON0				0x10c
3678c2ecf20Sopenharmony_ci#define RK3188_UOC0_CON0_BYPASSSEL			BIT(9)
3688c2ecf20Sopenharmony_ci#define RK3188_UOC0_CON0_BYPASSDMEN			BIT(8)
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci/*
3718c2ecf20Sopenharmony_ci * Enable the bypass of uart2 data through the otg usb phy.
3728c2ecf20Sopenharmony_ci * See description of rk3288-variant for details.
3738c2ecf20Sopenharmony_ci */
3748c2ecf20Sopenharmony_cistatic int __init rk3188_init_usb_uart(struct regmap *grf,
3758c2ecf20Sopenharmony_ci				const struct rockchip_usb_phy_pdata *pdata)
3768c2ecf20Sopenharmony_ci{
3778c2ecf20Sopenharmony_ci	u32 val;
3788c2ecf20Sopenharmony_ci	int ret;
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	ret = rockchip_init_usb_uart_common(grf, pdata);
3818c2ecf20Sopenharmony_ci	if (ret)
3828c2ecf20Sopenharmony_ci		return ret;
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	val = HIWORD_UPDATE(RK3188_UOC0_CON0_BYPASSSEL
3858c2ecf20Sopenharmony_ci				| RK3188_UOC0_CON0_BYPASSDMEN,
3868c2ecf20Sopenharmony_ci			    RK3188_UOC0_CON0_BYPASSSEL
3878c2ecf20Sopenharmony_ci				| RK3188_UOC0_CON0_BYPASSDMEN);
3888c2ecf20Sopenharmony_ci	ret = regmap_write(grf, RK3188_UOC0_CON0, val);
3898c2ecf20Sopenharmony_ci	if (ret)
3908c2ecf20Sopenharmony_ci		return ret;
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	return 0;
3938c2ecf20Sopenharmony_ci}
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_cistatic const struct rockchip_usb_phy_pdata rk3188_pdata = {
3968c2ecf20Sopenharmony_ci	.phys = (struct rockchip_usb_phys[]){
3978c2ecf20Sopenharmony_ci		{ .reg = 0x10c, .pll_name = "sclk_otgphy0_480m" },
3988c2ecf20Sopenharmony_ci		{ .reg = 0x11c, .pll_name = "sclk_otgphy1_480m" },
3998c2ecf20Sopenharmony_ci		{ /* sentinel */ }
4008c2ecf20Sopenharmony_ci	},
4018c2ecf20Sopenharmony_ci	.init_usb_uart = rk3188_init_usb_uart,
4028c2ecf20Sopenharmony_ci	.usb_uart_phy = 0,
4038c2ecf20Sopenharmony_ci};
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci#define RK3288_UOC0_CON3				0x32c
4068c2ecf20Sopenharmony_ci#define RK3288_UOC0_CON3_BYPASSDMEN			BIT(6)
4078c2ecf20Sopenharmony_ci#define RK3288_UOC0_CON3_BYPASSSEL			BIT(7)
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci/*
4108c2ecf20Sopenharmony_ci * Enable the bypass of uart2 data through the otg usb phy.
4118c2ecf20Sopenharmony_ci * Original description in the TRM.
4128c2ecf20Sopenharmony_ci * 1. Disable the OTG block by setting OTGDISABLE0 to 1’b1.
4138c2ecf20Sopenharmony_ci * 2. Disable the pull-up resistance on the D+ line by setting
4148c2ecf20Sopenharmony_ci *    OPMODE0[1:0] to 2’b01.
4158c2ecf20Sopenharmony_ci * 3. To ensure that the XO, Bias, and PLL blocks are powered down in Suspend
4168c2ecf20Sopenharmony_ci *    mode, set COMMONONN to 1’b1.
4178c2ecf20Sopenharmony_ci * 4. Place the USB PHY in Suspend mode by setting SUSPENDM0 to 1’b0.
4188c2ecf20Sopenharmony_ci * 5. Set BYPASSSEL0 to 1’b1.
4198c2ecf20Sopenharmony_ci * 6. To transmit data, controls BYPASSDMEN0, and BYPASSDMDATA0.
4208c2ecf20Sopenharmony_ci * To receive data, monitor FSVPLUS0.
4218c2ecf20Sopenharmony_ci *
4228c2ecf20Sopenharmony_ci * The actual code in the vendor kernel does some things differently.
4238c2ecf20Sopenharmony_ci */
4248c2ecf20Sopenharmony_cistatic int __init rk3288_init_usb_uart(struct regmap *grf,
4258c2ecf20Sopenharmony_ci				const struct rockchip_usb_phy_pdata *pdata)
4268c2ecf20Sopenharmony_ci{
4278c2ecf20Sopenharmony_ci	u32 val;
4288c2ecf20Sopenharmony_ci	int ret;
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci	ret = rockchip_init_usb_uart_common(grf, pdata);
4318c2ecf20Sopenharmony_ci	if (ret)
4328c2ecf20Sopenharmony_ci		return ret;
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci	val = HIWORD_UPDATE(RK3288_UOC0_CON3_BYPASSSEL
4358c2ecf20Sopenharmony_ci				| RK3288_UOC0_CON3_BYPASSDMEN,
4368c2ecf20Sopenharmony_ci			    RK3288_UOC0_CON3_BYPASSSEL
4378c2ecf20Sopenharmony_ci				| RK3288_UOC0_CON3_BYPASSDMEN);
4388c2ecf20Sopenharmony_ci	ret = regmap_write(grf, RK3288_UOC0_CON3, val);
4398c2ecf20Sopenharmony_ci	if (ret)
4408c2ecf20Sopenharmony_ci		return ret;
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci	return 0;
4438c2ecf20Sopenharmony_ci}
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_cistatic const struct rockchip_usb_phy_pdata rk3288_pdata = {
4468c2ecf20Sopenharmony_ci	.phys = (struct rockchip_usb_phys[]){
4478c2ecf20Sopenharmony_ci		{ .reg = 0x320, .pll_name = "sclk_otgphy0_480m" },
4488c2ecf20Sopenharmony_ci		{ .reg = 0x334, .pll_name = "sclk_otgphy1_480m" },
4498c2ecf20Sopenharmony_ci		{ .reg = 0x348, .pll_name = "sclk_otgphy2_480m" },
4508c2ecf20Sopenharmony_ci		{ /* sentinel */ }
4518c2ecf20Sopenharmony_ci	},
4528c2ecf20Sopenharmony_ci	.init_usb_uart = rk3288_init_usb_uart,
4538c2ecf20Sopenharmony_ci	.usb_uart_phy = 0,
4548c2ecf20Sopenharmony_ci};
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_cistatic int rockchip_usb_phy_probe(struct platform_device *pdev)
4578c2ecf20Sopenharmony_ci{
4588c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
4598c2ecf20Sopenharmony_ci	struct rockchip_usb_phy_base *phy_base;
4608c2ecf20Sopenharmony_ci	struct phy_provider *phy_provider;
4618c2ecf20Sopenharmony_ci	const struct of_device_id *match;
4628c2ecf20Sopenharmony_ci	struct device_node *child;
4638c2ecf20Sopenharmony_ci	int err;
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci	phy_base = devm_kzalloc(dev, sizeof(*phy_base), GFP_KERNEL);
4668c2ecf20Sopenharmony_ci	if (!phy_base)
4678c2ecf20Sopenharmony_ci		return -ENOMEM;
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci	match = of_match_device(dev->driver->of_match_table, dev);
4708c2ecf20Sopenharmony_ci	if (!match || !match->data) {
4718c2ecf20Sopenharmony_ci		dev_err(dev, "missing phy data\n");
4728c2ecf20Sopenharmony_ci		return -EINVAL;
4738c2ecf20Sopenharmony_ci	}
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	phy_base->pdata = match->data;
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci	phy_base->dev = dev;
4788c2ecf20Sopenharmony_ci	phy_base->reg_base = ERR_PTR(-ENODEV);
4798c2ecf20Sopenharmony_ci	if (dev->parent && dev->parent->of_node)
4808c2ecf20Sopenharmony_ci		phy_base->reg_base = syscon_node_to_regmap(
4818c2ecf20Sopenharmony_ci						dev->parent->of_node);
4828c2ecf20Sopenharmony_ci	if (IS_ERR(phy_base->reg_base))
4838c2ecf20Sopenharmony_ci		phy_base->reg_base = syscon_regmap_lookup_by_phandle(
4848c2ecf20Sopenharmony_ci						dev->of_node, "rockchip,grf");
4858c2ecf20Sopenharmony_ci	if (IS_ERR(phy_base->reg_base)) {
4868c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Missing rockchip,grf property\n");
4878c2ecf20Sopenharmony_ci		return PTR_ERR(phy_base->reg_base);
4888c2ecf20Sopenharmony_ci	}
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci	for_each_available_child_of_node(dev->of_node, child) {
4918c2ecf20Sopenharmony_ci		err = rockchip_usb_phy_init(phy_base, child);
4928c2ecf20Sopenharmony_ci		if (err) {
4938c2ecf20Sopenharmony_ci			of_node_put(child);
4948c2ecf20Sopenharmony_ci			return err;
4958c2ecf20Sopenharmony_ci		}
4968c2ecf20Sopenharmony_ci	}
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci	phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
4998c2ecf20Sopenharmony_ci	return PTR_ERR_OR_ZERO(phy_provider);
5008c2ecf20Sopenharmony_ci}
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_cistatic const struct of_device_id rockchip_usb_phy_dt_ids[] = {
5038c2ecf20Sopenharmony_ci	{ .compatible = "rockchip,rk3066a-usb-phy", .data = &rk3066a_pdata },
5048c2ecf20Sopenharmony_ci	{ .compatible = "rockchip,rk3188-usb-phy", .data = &rk3188_pdata },
5058c2ecf20Sopenharmony_ci	{ .compatible = "rockchip,rk3288-usb-phy", .data = &rk3288_pdata },
5068c2ecf20Sopenharmony_ci	{}
5078c2ecf20Sopenharmony_ci};
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, rockchip_usb_phy_dt_ids);
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_cistatic struct platform_driver rockchip_usb_driver = {
5128c2ecf20Sopenharmony_ci	.probe		= rockchip_usb_phy_probe,
5138c2ecf20Sopenharmony_ci	.driver		= {
5148c2ecf20Sopenharmony_ci		.name	= "rockchip-usb-phy",
5158c2ecf20Sopenharmony_ci		.of_match_table = rockchip_usb_phy_dt_ids,
5168c2ecf20Sopenharmony_ci	},
5178c2ecf20Sopenharmony_ci};
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_cimodule_platform_driver(rockchip_usb_driver);
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci#ifndef MODULE
5228c2ecf20Sopenharmony_cistatic int __init rockchip_init_usb_uart(void)
5238c2ecf20Sopenharmony_ci{
5248c2ecf20Sopenharmony_ci	const struct of_device_id *match;
5258c2ecf20Sopenharmony_ci	const struct rockchip_usb_phy_pdata *data;
5268c2ecf20Sopenharmony_ci	struct device_node *np;
5278c2ecf20Sopenharmony_ci	struct regmap *grf;
5288c2ecf20Sopenharmony_ci	int ret;
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci	if (!enable_usb_uart)
5318c2ecf20Sopenharmony_ci		return 0;
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci	np = of_find_matching_node_and_match(NULL, rockchip_usb_phy_dt_ids,
5348c2ecf20Sopenharmony_ci					     &match);
5358c2ecf20Sopenharmony_ci	if (!np) {
5368c2ecf20Sopenharmony_ci		pr_err("%s: failed to find usbphy node\n", __func__);
5378c2ecf20Sopenharmony_ci		return -ENOTSUPP;
5388c2ecf20Sopenharmony_ci	}
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_ci	pr_debug("%s: using settings for %s\n", __func__, match->compatible);
5418c2ecf20Sopenharmony_ci	data = match->data;
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_ci	if (!data->init_usb_uart) {
5448c2ecf20Sopenharmony_ci		pr_err("%s: usb-uart not available on %s\n",
5458c2ecf20Sopenharmony_ci		       __func__, match->compatible);
5468c2ecf20Sopenharmony_ci		return -ENOTSUPP;
5478c2ecf20Sopenharmony_ci	}
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci	grf = ERR_PTR(-ENODEV);
5508c2ecf20Sopenharmony_ci	if (np->parent)
5518c2ecf20Sopenharmony_ci		grf = syscon_node_to_regmap(np->parent);
5528c2ecf20Sopenharmony_ci	if (IS_ERR(grf))
5538c2ecf20Sopenharmony_ci		grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
5548c2ecf20Sopenharmony_ci	if (IS_ERR(grf)) {
5558c2ecf20Sopenharmony_ci		pr_err("%s: Missing rockchip,grf property, %lu\n",
5568c2ecf20Sopenharmony_ci		       __func__, PTR_ERR(grf));
5578c2ecf20Sopenharmony_ci		return PTR_ERR(grf);
5588c2ecf20Sopenharmony_ci	}
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci	ret = data->init_usb_uart(grf, data);
5618c2ecf20Sopenharmony_ci	if (ret) {
5628c2ecf20Sopenharmony_ci		pr_err("%s: could not init usb_uart, %d\n", __func__, ret);
5638c2ecf20Sopenharmony_ci		enable_usb_uart = 0;
5648c2ecf20Sopenharmony_ci		return ret;
5658c2ecf20Sopenharmony_ci	}
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_ci	return 0;
5688c2ecf20Sopenharmony_ci}
5698c2ecf20Sopenharmony_ciearly_initcall(rockchip_init_usb_uart);
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_cistatic int __init rockchip_usb_uart(char *buf)
5728c2ecf20Sopenharmony_ci{
5738c2ecf20Sopenharmony_ci	enable_usb_uart = true;
5748c2ecf20Sopenharmony_ci	return 0;
5758c2ecf20Sopenharmony_ci}
5768c2ecf20Sopenharmony_ciearly_param("rockchip.usb_uart", rockchip_usb_uart);
5778c2ecf20Sopenharmony_ci#endif
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ciMODULE_AUTHOR("Yunzhi Li <lyz@rock-chips.com>");
5808c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Rockchip USB 2.0 PHY driver");
5818c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
582