162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci
362306a36Sopenharmony_ci/*
462306a36Sopenharmony_ci * Sunplus SP7021 USB 2.0 phy driver
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * Copyright (C) 2022 Sunplus Technology Inc., All rights reserved.
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci * Note 1 : non-posted write command for the registers accesses of
962306a36Sopenharmony_ci * Sunplus SP7021.
1062306a36Sopenharmony_ci *
1162306a36Sopenharmony_ci */
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci#include <linux/bitfield.h>
1462306a36Sopenharmony_ci#include <linux/clk.h>
1562306a36Sopenharmony_ci#include <linux/delay.h>
1662306a36Sopenharmony_ci#include <linux/io.h>
1762306a36Sopenharmony_ci#include <linux/module.h>
1862306a36Sopenharmony_ci#include <linux/nvmem-consumer.h>
1962306a36Sopenharmony_ci#include <linux/of.h>
2062306a36Sopenharmony_ci#include <linux/phy/phy.h>
2162306a36Sopenharmony_ci#include <linux/platform_device.h>
2262306a36Sopenharmony_ci#include <linux/reset.h>
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci#define HIGH_MASK_BITS				GENMASK(31, 16)
2562306a36Sopenharmony_ci#define LOW_MASK_BITS				GENMASK(15, 0)
2662306a36Sopenharmony_ci#define OTP_DISC_LEVEL_DEFAULT			0xd
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci/* GROUP UPHY */
2962306a36Sopenharmony_ci#define CONFIG1					0x4
3062306a36Sopenharmony_ci#define J_HS_TX_PWRSAV				BIT(5)
3162306a36Sopenharmony_ci#define CONFIG3					0xc
3262306a36Sopenharmony_ci#define J_FORCE_DISC_ON				BIT(5)
3362306a36Sopenharmony_ci#define J_DEBUG_CTRL_ADDR_MACRO			BIT(0)
3462306a36Sopenharmony_ci#define CONFIG7					0x1c
3562306a36Sopenharmony_ci#define J_DISC					0X1f
3662306a36Sopenharmony_ci#define CONFIG9					0x24
3762306a36Sopenharmony_ci#define J_ECO_PATH				BIT(6)
3862306a36Sopenharmony_ci#define CONFIG16				0x40
3962306a36Sopenharmony_ci#define J_TBCWAIT_MASK				GENMASK(6, 5)
4062306a36Sopenharmony_ci#define J_TBCWAIT_1P1_MS			FIELD_PREP(J_TBCWAIT_MASK, 0)
4162306a36Sopenharmony_ci#define J_TVDM_SRC_DIS_MASK			GENMASK(4, 3)
4262306a36Sopenharmony_ci#define J_TVDM_SRC_DIS_8P2_MS			FIELD_PREP(J_TVDM_SRC_DIS_MASK, 3)
4362306a36Sopenharmony_ci#define J_TVDM_SRC_EN_MASK			GENMASK(2, 1)
4462306a36Sopenharmony_ci#define J_TVDM_SRC_EN_1P6_MS			FIELD_PREP(J_TVDM_SRC_EN_MASK, 0)
4562306a36Sopenharmony_ci#define J_BC_EN					BIT(0)
4662306a36Sopenharmony_ci#define CONFIG17				0x44
4762306a36Sopenharmony_ci#define IBG_TRIM0_MASK				GENMASK(7, 5)
4862306a36Sopenharmony_ci#define IBG_TRIM0_SSLVHT			FIELD_PREP(IBG_TRIM0_MASK, 4)
4962306a36Sopenharmony_ci#define J_VDATREE_TRIM_MASK			GENMASK(4, 1)
5062306a36Sopenharmony_ci#define J_VDATREE_TRIM_DEFAULT			FIELD_PREP(J_VDATREE_TRIM_MASK, 9)
5162306a36Sopenharmony_ci#define CONFIG23				0x5c
5262306a36Sopenharmony_ci#define PROB_MASK				GENMASK(5, 3)
5362306a36Sopenharmony_ci#define PROB					FIELD_PREP(PROB_MASK, 7)
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci/* GROUP MOON4 */
5662306a36Sopenharmony_ci#define UPHY_CONTROL0				0x0
5762306a36Sopenharmony_ci#define UPHY_CONTROL1				0x4
5862306a36Sopenharmony_ci#define UPHY_CONTROL2				0x8
5962306a36Sopenharmony_ci#define MO1_UPHY_RX_CLK_SEL			BIT(6)
6062306a36Sopenharmony_ci#define MASK_MO1_UPHY_RX_CLK_SEL		BIT(6 + 16)
6162306a36Sopenharmony_ci#define UPHY_CONTROL3				0xc
6262306a36Sopenharmony_ci#define MO1_UPHY_PLL_POWER_OFF_SEL		BIT(7)
6362306a36Sopenharmony_ci#define MASK_MO1_UPHY_PLL_POWER_OFF_SEL		BIT(7 + 16)
6462306a36Sopenharmony_ci#define MO1_UPHY_PLL_POWER_OFF			BIT(3)
6562306a36Sopenharmony_ci#define MASK_UPHY_PLL_POWER_OFF			BIT(3 + 16)
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_cistruct sp_usbphy {
6862306a36Sopenharmony_ci	struct device *dev;
6962306a36Sopenharmony_ci	struct resource *phy_res_mem;
7062306a36Sopenharmony_ci	struct resource *moon4_res_mem;
7162306a36Sopenharmony_ci	struct reset_control *rstc;
7262306a36Sopenharmony_ci	struct clk *phy_clk;
7362306a36Sopenharmony_ci	void __iomem *phy_regs;
7462306a36Sopenharmony_ci	void __iomem *moon4_regs;
7562306a36Sopenharmony_ci	u32 disc_vol_addr_off;
7662306a36Sopenharmony_ci};
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_cistatic int update_disc_vol(struct sp_usbphy *usbphy)
7962306a36Sopenharmony_ci{
8062306a36Sopenharmony_ci	struct nvmem_cell *cell;
8162306a36Sopenharmony_ci	char *disc_name = "disc_vol";
8262306a36Sopenharmony_ci	ssize_t otp_l = 0;
8362306a36Sopenharmony_ci	char *otp_v;
8462306a36Sopenharmony_ci	u32 val, set;
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci	cell = nvmem_cell_get(usbphy->dev, disc_name);
8762306a36Sopenharmony_ci	if (IS_ERR_OR_NULL(cell)) {
8862306a36Sopenharmony_ci		if (PTR_ERR(cell) == -EPROBE_DEFER)
8962306a36Sopenharmony_ci			return -EPROBE_DEFER;
9062306a36Sopenharmony_ci	}
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci	otp_v = nvmem_cell_read(cell, &otp_l);
9362306a36Sopenharmony_ci	nvmem_cell_put(cell);
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci	if (!IS_ERR(otp_v)) {
9662306a36Sopenharmony_ci		set = *(otp_v + 1);
9762306a36Sopenharmony_ci		set = (set << (sizeof(char) * 8)) | *otp_v;
9862306a36Sopenharmony_ci		set = (set >> usbphy->disc_vol_addr_off) & J_DISC;
9962306a36Sopenharmony_ci	}
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci	if (IS_ERR(otp_v) || set == 0)
10262306a36Sopenharmony_ci		set = OTP_DISC_LEVEL_DEFAULT;
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci	val = readl(usbphy->phy_regs + CONFIG7);
10562306a36Sopenharmony_ci	val = (val & ~J_DISC) | set;
10662306a36Sopenharmony_ci	writel(val, usbphy->phy_regs + CONFIG7);
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci	return 0;
10962306a36Sopenharmony_ci}
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_cistatic int sp_uphy_init(struct phy *phy)
11262306a36Sopenharmony_ci{
11362306a36Sopenharmony_ci	struct sp_usbphy *usbphy = phy_get_drvdata(phy);
11462306a36Sopenharmony_ci	u32 val;
11562306a36Sopenharmony_ci	int ret;
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci	ret = clk_prepare_enable(usbphy->phy_clk);
11862306a36Sopenharmony_ci	if (ret)
11962306a36Sopenharmony_ci		goto err_clk;
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci	ret = reset_control_deassert(usbphy->rstc);
12262306a36Sopenharmony_ci	if (ret)
12362306a36Sopenharmony_ci		goto err_reset;
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci	/* Default value modification */
12662306a36Sopenharmony_ci	writel(HIGH_MASK_BITS | 0x4002, usbphy->moon4_regs + UPHY_CONTROL0);
12762306a36Sopenharmony_ci	writel(HIGH_MASK_BITS | 0x8747, usbphy->moon4_regs + UPHY_CONTROL1);
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci	/* disconnect voltage */
13062306a36Sopenharmony_ci	ret = update_disc_vol(usbphy);
13162306a36Sopenharmony_ci	if (ret < 0)
13262306a36Sopenharmony_ci		return ret;
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci	/* board uphy 0 internal register modification for tid certification */
13562306a36Sopenharmony_ci	val = readl(usbphy->phy_regs + CONFIG9);
13662306a36Sopenharmony_ci	val &= ~(J_ECO_PATH);
13762306a36Sopenharmony_ci	writel(val, usbphy->phy_regs + CONFIG9);
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci	val = readl(usbphy->phy_regs + CONFIG1);
14062306a36Sopenharmony_ci	val &= ~(J_HS_TX_PWRSAV);
14162306a36Sopenharmony_ci	writel(val, usbphy->phy_regs + CONFIG1);
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci	val = readl(usbphy->phy_regs + CONFIG23);
14462306a36Sopenharmony_ci	val = (val & ~PROB) | PROB;
14562306a36Sopenharmony_ci	writel(val, usbphy->phy_regs + CONFIG23);
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ci	/* port 0 uphy clk fix */
14862306a36Sopenharmony_ci	writel(MASK_MO1_UPHY_RX_CLK_SEL | MO1_UPHY_RX_CLK_SEL,
14962306a36Sopenharmony_ci	       usbphy->moon4_regs + UPHY_CONTROL2);
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_ci	/* battery charger */
15262306a36Sopenharmony_ci	writel(J_TBCWAIT_1P1_MS | J_TVDM_SRC_DIS_8P2_MS | J_TVDM_SRC_EN_1P6_MS | J_BC_EN,
15362306a36Sopenharmony_ci	       usbphy->phy_regs + CONFIG16);
15462306a36Sopenharmony_ci	writel(IBG_TRIM0_SSLVHT | J_VDATREE_TRIM_DEFAULT, usbphy->phy_regs + CONFIG17);
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ci	/* chirp mode */
15762306a36Sopenharmony_ci	writel(J_FORCE_DISC_ON | J_DEBUG_CTRL_ADDR_MACRO, usbphy->phy_regs + CONFIG3);
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ci	return 0;
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_cierr_reset:
16262306a36Sopenharmony_ci	reset_control_assert(usbphy->rstc);
16362306a36Sopenharmony_cierr_clk:
16462306a36Sopenharmony_ci	clk_disable_unprepare(usbphy->phy_clk);
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci	return ret;
16762306a36Sopenharmony_ci}
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_cistatic int sp_uphy_power_on(struct phy *phy)
17062306a36Sopenharmony_ci{
17162306a36Sopenharmony_ci	struct sp_usbphy *usbphy = phy_get_drvdata(phy);
17262306a36Sopenharmony_ci	u32 pll_pwr_on, pll_pwr_off;
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci	/* PLL power off/on twice */
17562306a36Sopenharmony_ci	pll_pwr_off = (readl(usbphy->moon4_regs + UPHY_CONTROL3) & ~LOW_MASK_BITS)
17662306a36Sopenharmony_ci			| MO1_UPHY_PLL_POWER_OFF_SEL | MO1_UPHY_PLL_POWER_OFF;
17762306a36Sopenharmony_ci	pll_pwr_on = (readl(usbphy->moon4_regs + UPHY_CONTROL3) & ~LOW_MASK_BITS)
17862306a36Sopenharmony_ci			| MO1_UPHY_PLL_POWER_OFF_SEL;
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci	writel(MASK_MO1_UPHY_PLL_POWER_OFF_SEL | MASK_UPHY_PLL_POWER_OFF | pll_pwr_off,
18162306a36Sopenharmony_ci	       usbphy->moon4_regs + UPHY_CONTROL3);
18262306a36Sopenharmony_ci	mdelay(1);
18362306a36Sopenharmony_ci	writel(MASK_MO1_UPHY_PLL_POWER_OFF_SEL | MASK_UPHY_PLL_POWER_OFF | pll_pwr_on,
18462306a36Sopenharmony_ci	       usbphy->moon4_regs + UPHY_CONTROL3);
18562306a36Sopenharmony_ci	mdelay(1);
18662306a36Sopenharmony_ci	writel(MASK_MO1_UPHY_PLL_POWER_OFF_SEL | MASK_UPHY_PLL_POWER_OFF | pll_pwr_off,
18762306a36Sopenharmony_ci	       usbphy->moon4_regs + UPHY_CONTROL3);
18862306a36Sopenharmony_ci	mdelay(1);
18962306a36Sopenharmony_ci	writel(MASK_MO1_UPHY_PLL_POWER_OFF_SEL | MASK_UPHY_PLL_POWER_OFF | pll_pwr_on,
19062306a36Sopenharmony_ci	       usbphy->moon4_regs + UPHY_CONTROL3);
19162306a36Sopenharmony_ci	mdelay(1);
19262306a36Sopenharmony_ci	writel(MASK_MO1_UPHY_PLL_POWER_OFF_SEL | MASK_UPHY_PLL_POWER_OFF | 0x0,
19362306a36Sopenharmony_ci	       usbphy->moon4_regs + UPHY_CONTROL3);
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_ci	return 0;
19662306a36Sopenharmony_ci}
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_cistatic int sp_uphy_power_off(struct phy *phy)
19962306a36Sopenharmony_ci{
20062306a36Sopenharmony_ci	struct sp_usbphy *usbphy = phy_get_drvdata(phy);
20162306a36Sopenharmony_ci	u32 pll_pwr_off;
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_ci	pll_pwr_off = (readl(usbphy->moon4_regs + UPHY_CONTROL3) & ~LOW_MASK_BITS)
20462306a36Sopenharmony_ci			| MO1_UPHY_PLL_POWER_OFF_SEL | MO1_UPHY_PLL_POWER_OFF;
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_ci	writel(MASK_MO1_UPHY_PLL_POWER_OFF_SEL | MASK_UPHY_PLL_POWER_OFF | pll_pwr_off,
20762306a36Sopenharmony_ci	       usbphy->moon4_regs + UPHY_CONTROL3);
20862306a36Sopenharmony_ci	mdelay(1);
20962306a36Sopenharmony_ci	writel(MASK_MO1_UPHY_PLL_POWER_OFF_SEL | MASK_UPHY_PLL_POWER_OFF | 0x0,
21062306a36Sopenharmony_ci	       usbphy->moon4_regs + UPHY_CONTROL3);
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_ci	return 0;
21362306a36Sopenharmony_ci}
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_cistatic int sp_uphy_exit(struct phy *phy)
21662306a36Sopenharmony_ci{
21762306a36Sopenharmony_ci	struct sp_usbphy *usbphy = phy_get_drvdata(phy);
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_ci	reset_control_assert(usbphy->rstc);
22062306a36Sopenharmony_ci	clk_disable_unprepare(usbphy->phy_clk);
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_ci	return 0;
22362306a36Sopenharmony_ci}
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_cistatic const struct phy_ops sp_uphy_ops = {
22662306a36Sopenharmony_ci	.init		= sp_uphy_init,
22762306a36Sopenharmony_ci	.power_on	= sp_uphy_power_on,
22862306a36Sopenharmony_ci	.power_off	= sp_uphy_power_off,
22962306a36Sopenharmony_ci	.exit		= sp_uphy_exit,
23062306a36Sopenharmony_ci};
23162306a36Sopenharmony_ci
23262306a36Sopenharmony_cistatic const struct of_device_id sp_uphy_dt_ids[] = {
23362306a36Sopenharmony_ci	{.compatible = "sunplus,sp7021-usb2-phy", },
23462306a36Sopenharmony_ci	{ }
23562306a36Sopenharmony_ci};
23662306a36Sopenharmony_ciMODULE_DEVICE_TABLE(of, sp_uphy_dt_ids);
23762306a36Sopenharmony_ci
23862306a36Sopenharmony_cistatic int sp_usb_phy_probe(struct platform_device *pdev)
23962306a36Sopenharmony_ci{
24062306a36Sopenharmony_ci	struct sp_usbphy *usbphy;
24162306a36Sopenharmony_ci	struct phy_provider *phy_provider;
24262306a36Sopenharmony_ci	struct phy *phy;
24362306a36Sopenharmony_ci	int ret;
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ci	usbphy = devm_kzalloc(&pdev->dev, sizeof(*usbphy), GFP_KERNEL);
24662306a36Sopenharmony_ci	if (!usbphy)
24762306a36Sopenharmony_ci		return -ENOMEM;
24862306a36Sopenharmony_ci
24962306a36Sopenharmony_ci	usbphy->dev = &pdev->dev;
25062306a36Sopenharmony_ci
25162306a36Sopenharmony_ci	usbphy->phy_res_mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy");
25262306a36Sopenharmony_ci	usbphy->phy_regs = devm_ioremap_resource(&pdev->dev, usbphy->phy_res_mem);
25362306a36Sopenharmony_ci	if (IS_ERR(usbphy->phy_regs))
25462306a36Sopenharmony_ci		return PTR_ERR(usbphy->phy_regs);
25562306a36Sopenharmony_ci
25662306a36Sopenharmony_ci	usbphy->moon4_res_mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "moon4");
25762306a36Sopenharmony_ci	if (!usbphy->moon4_res_mem)
25862306a36Sopenharmony_ci		return -EINVAL;
25962306a36Sopenharmony_ci
26062306a36Sopenharmony_ci	usbphy->moon4_regs = devm_ioremap(&pdev->dev, usbphy->moon4_res_mem->start,
26162306a36Sopenharmony_ci					  resource_size(usbphy->moon4_res_mem));
26262306a36Sopenharmony_ci	if (!usbphy->moon4_regs)
26362306a36Sopenharmony_ci		return -ENOMEM;
26462306a36Sopenharmony_ci
26562306a36Sopenharmony_ci	usbphy->phy_clk = devm_clk_get(&pdev->dev, NULL);
26662306a36Sopenharmony_ci	if (IS_ERR(usbphy->phy_clk))
26762306a36Sopenharmony_ci		return PTR_ERR(usbphy->phy_clk);
26862306a36Sopenharmony_ci
26962306a36Sopenharmony_ci	usbphy->rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL);
27062306a36Sopenharmony_ci	if (IS_ERR(usbphy->rstc))
27162306a36Sopenharmony_ci		return PTR_ERR(usbphy->rstc);
27262306a36Sopenharmony_ci
27362306a36Sopenharmony_ci	of_property_read_u32(pdev->dev.of_node, "sunplus,disc-vol-addr-off",
27462306a36Sopenharmony_ci			     &usbphy->disc_vol_addr_off);
27562306a36Sopenharmony_ci
27662306a36Sopenharmony_ci	phy = devm_phy_create(&pdev->dev, NULL, &sp_uphy_ops);
27762306a36Sopenharmony_ci	if (IS_ERR(phy)) {
27862306a36Sopenharmony_ci		ret = PTR_ERR(phy);
27962306a36Sopenharmony_ci		return ret;
28062306a36Sopenharmony_ci	}
28162306a36Sopenharmony_ci
28262306a36Sopenharmony_ci	phy_set_drvdata(phy, usbphy);
28362306a36Sopenharmony_ci	phy_provider = devm_of_phy_provider_register(&pdev->dev, of_phy_simple_xlate);
28462306a36Sopenharmony_ci
28562306a36Sopenharmony_ci	return PTR_ERR_OR_ZERO(phy_provider);
28662306a36Sopenharmony_ci}
28762306a36Sopenharmony_ci
28862306a36Sopenharmony_cistatic struct platform_driver sunplus_usb_phy_driver = {
28962306a36Sopenharmony_ci	.probe		= sp_usb_phy_probe,
29062306a36Sopenharmony_ci	.driver		= {
29162306a36Sopenharmony_ci		.name	= "sunplus-usb2-phy",
29262306a36Sopenharmony_ci		.of_match_table = sp_uphy_dt_ids,
29362306a36Sopenharmony_ci	},
29462306a36Sopenharmony_ci};
29562306a36Sopenharmony_cimodule_platform_driver(sunplus_usb_phy_driver);
29662306a36Sopenharmony_ci
29762306a36Sopenharmony_ciMODULE_AUTHOR("Vincent Shih <vincent.shih@sunplus.com>");
29862306a36Sopenharmony_ciMODULE_DESCRIPTION("Sunplus USB 2.0 phy driver");
29962306a36Sopenharmony_ciMODULE_LICENSE("GPL");
300