162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Amlogic AXG MIPI + PCIE analog PHY driver
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (C) 2019 Remi Pommarel <repk@triplefau.lt>
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci#include <linux/bitfield.h>
862306a36Sopenharmony_ci#include <linux/bitops.h>
962306a36Sopenharmony_ci#include <linux/module.h>
1062306a36Sopenharmony_ci#include <linux/phy/phy.h>
1162306a36Sopenharmony_ci#include <linux/regmap.h>
1262306a36Sopenharmony_ci#include <linux/delay.h>
1362306a36Sopenharmony_ci#include <linux/mfd/syscon.h>
1462306a36Sopenharmony_ci#include <linux/of.h>
1562306a36Sopenharmony_ci#include <linux/platform_device.h>
1662306a36Sopenharmony_ci#include <dt-bindings/phy/phy.h>
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci#define HHI_MIPI_CNTL0 0x00
1962306a36Sopenharmony_ci#define		HHI_MIPI_CNTL0_COMMON_BLOCK	GENMASK(31, 28)
2062306a36Sopenharmony_ci#define		HHI_MIPI_CNTL0_ENABLE		BIT(29)
2162306a36Sopenharmony_ci#define		HHI_MIPI_CNTL0_BANDGAP		BIT(26)
2262306a36Sopenharmony_ci#define		HHI_MIPI_CNTL0_DIF_REF_CTL1	GENMASK(25, 16)
2362306a36Sopenharmony_ci#define		HHI_MIPI_CNTL0_DIF_REF_CTL0	GENMASK(15, 0)
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci#define HHI_MIPI_CNTL1 0x04
2662306a36Sopenharmony_ci#define		HHI_MIPI_CNTL1_CH0_CML_PDR_EN	BIT(12)
2762306a36Sopenharmony_ci#define		HHI_MIPI_CNTL1_LP_ABILITY	GENMASK(5, 4)
2862306a36Sopenharmony_ci#define		HHI_MIPI_CNTL1_LP_RESISTER	BIT(3)
2962306a36Sopenharmony_ci#define		HHI_MIPI_CNTL1_INPUT_SETTING	BIT(2)
3062306a36Sopenharmony_ci#define		HHI_MIPI_CNTL1_INPUT_SEL	BIT(1)
3162306a36Sopenharmony_ci#define		HHI_MIPI_CNTL1_PRBS7_EN		BIT(0)
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci#define HHI_MIPI_CNTL2 0x08
3462306a36Sopenharmony_ci#define		HHI_MIPI_CNTL2_CH_PU		GENMASK(31, 25)
3562306a36Sopenharmony_ci#define		HHI_MIPI_CNTL2_CH_CTL		GENMASK(24, 19)
3662306a36Sopenharmony_ci#define		HHI_MIPI_CNTL2_CH0_DIGDR_EN	BIT(18)
3762306a36Sopenharmony_ci#define		HHI_MIPI_CNTL2_CH_DIGDR_EN	BIT(17)
3862306a36Sopenharmony_ci#define		HHI_MIPI_CNTL2_LPULPS_EN	BIT(16)
3962306a36Sopenharmony_ci#define		HHI_MIPI_CNTL2_CH_EN		GENMASK(15, 11)
4062306a36Sopenharmony_ci#define		HHI_MIPI_CNTL2_CH0_LP_CTL	GENMASK(10, 1)
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci#define DSI_LANE_0              BIT(4)
4362306a36Sopenharmony_ci#define DSI_LANE_1              BIT(3)
4462306a36Sopenharmony_ci#define DSI_LANE_CLK            BIT(2)
4562306a36Sopenharmony_ci#define DSI_LANE_2              BIT(1)
4662306a36Sopenharmony_ci#define DSI_LANE_3              BIT(0)
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_cistruct phy_axg_mipi_pcie_analog_priv {
4962306a36Sopenharmony_ci	struct phy *phy;
5062306a36Sopenharmony_ci	struct regmap *regmap;
5162306a36Sopenharmony_ci	bool dsi_configured;
5262306a36Sopenharmony_ci	bool dsi_enabled;
5362306a36Sopenharmony_ci	bool powered;
5462306a36Sopenharmony_ci	struct phy_configure_opts_mipi_dphy config;
5562306a36Sopenharmony_ci};
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_cistatic void phy_bandgap_enable(struct phy_axg_mipi_pcie_analog_priv *priv)
5862306a36Sopenharmony_ci{
5962306a36Sopenharmony_ci	regmap_update_bits(priv->regmap, HHI_MIPI_CNTL0,
6062306a36Sopenharmony_ci			HHI_MIPI_CNTL0_BANDGAP, HHI_MIPI_CNTL0_BANDGAP);
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci	regmap_update_bits(priv->regmap, HHI_MIPI_CNTL0,
6362306a36Sopenharmony_ci			HHI_MIPI_CNTL0_ENABLE, HHI_MIPI_CNTL0_ENABLE);
6462306a36Sopenharmony_ci}
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_cistatic void phy_bandgap_disable(struct phy_axg_mipi_pcie_analog_priv *priv)
6762306a36Sopenharmony_ci{
6862306a36Sopenharmony_ci	regmap_update_bits(priv->regmap, HHI_MIPI_CNTL0,
6962306a36Sopenharmony_ci			HHI_MIPI_CNTL0_BANDGAP, 0);
7062306a36Sopenharmony_ci	regmap_update_bits(priv->regmap, HHI_MIPI_CNTL0,
7162306a36Sopenharmony_ci			HHI_MIPI_CNTL0_ENABLE, 0);
7262306a36Sopenharmony_ci}
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_cistatic void phy_dsi_analog_enable(struct phy_axg_mipi_pcie_analog_priv *priv)
7562306a36Sopenharmony_ci{
7662306a36Sopenharmony_ci	u32 reg;
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci	regmap_update_bits(priv->regmap, HHI_MIPI_CNTL0,
7962306a36Sopenharmony_ci			   HHI_MIPI_CNTL0_DIF_REF_CTL1,
8062306a36Sopenharmony_ci			   FIELD_PREP(HHI_MIPI_CNTL0_DIF_REF_CTL1, 0x1b8));
8162306a36Sopenharmony_ci	regmap_update_bits(priv->regmap, HHI_MIPI_CNTL0,
8262306a36Sopenharmony_ci			   BIT(31), BIT(31));
8362306a36Sopenharmony_ci	regmap_update_bits(priv->regmap, HHI_MIPI_CNTL0,
8462306a36Sopenharmony_ci			   HHI_MIPI_CNTL0_DIF_REF_CTL0,
8562306a36Sopenharmony_ci			   FIELD_PREP(HHI_MIPI_CNTL0_DIF_REF_CTL0, 0x8));
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci	regmap_write(priv->regmap, HHI_MIPI_CNTL1, 0x001e);
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci	regmap_write(priv->regmap, HHI_MIPI_CNTL2,
9062306a36Sopenharmony_ci		     (0x26e0 << 16) | (0x459 << 0));
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci	reg = DSI_LANE_CLK;
9362306a36Sopenharmony_ci	switch (priv->config.lanes) {
9462306a36Sopenharmony_ci	case 4:
9562306a36Sopenharmony_ci		reg |= DSI_LANE_3;
9662306a36Sopenharmony_ci		fallthrough;
9762306a36Sopenharmony_ci	case 3:
9862306a36Sopenharmony_ci		reg |= DSI_LANE_2;
9962306a36Sopenharmony_ci		fallthrough;
10062306a36Sopenharmony_ci	case 2:
10162306a36Sopenharmony_ci		reg |= DSI_LANE_1;
10262306a36Sopenharmony_ci		fallthrough;
10362306a36Sopenharmony_ci	case 1:
10462306a36Sopenharmony_ci		reg |= DSI_LANE_0;
10562306a36Sopenharmony_ci		break;
10662306a36Sopenharmony_ci	default:
10762306a36Sopenharmony_ci		reg = 0;
10862306a36Sopenharmony_ci	}
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci	regmap_update_bits(priv->regmap, HHI_MIPI_CNTL2,
11162306a36Sopenharmony_ci			   HHI_MIPI_CNTL2_CH_EN,
11262306a36Sopenharmony_ci			   FIELD_PREP(HHI_MIPI_CNTL2_CH_EN, reg));
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci	priv->dsi_enabled = true;
11562306a36Sopenharmony_ci}
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_cistatic void phy_dsi_analog_disable(struct phy_axg_mipi_pcie_analog_priv *priv)
11862306a36Sopenharmony_ci{
11962306a36Sopenharmony_ci	regmap_update_bits(priv->regmap, HHI_MIPI_CNTL0,
12062306a36Sopenharmony_ci			HHI_MIPI_CNTL0_DIF_REF_CTL1,
12162306a36Sopenharmony_ci			FIELD_PREP(HHI_MIPI_CNTL0_DIF_REF_CTL1, 0));
12262306a36Sopenharmony_ci	regmap_update_bits(priv->regmap, HHI_MIPI_CNTL0, BIT(31), 0);
12362306a36Sopenharmony_ci	regmap_update_bits(priv->regmap, HHI_MIPI_CNTL0,
12462306a36Sopenharmony_ci			HHI_MIPI_CNTL0_DIF_REF_CTL1, 0);
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ci	regmap_write(priv->regmap, HHI_MIPI_CNTL1, 0x6);
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ci	regmap_write(priv->regmap, HHI_MIPI_CNTL2, 0x00200000);
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci	priv->dsi_enabled = false;
13162306a36Sopenharmony_ci}
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_cistatic int phy_axg_mipi_pcie_analog_configure(struct phy *phy,
13462306a36Sopenharmony_ci					      union phy_configure_opts *opts)
13562306a36Sopenharmony_ci{
13662306a36Sopenharmony_ci	struct phy_axg_mipi_pcie_analog_priv *priv = phy_get_drvdata(phy);
13762306a36Sopenharmony_ci	int ret;
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci	ret = phy_mipi_dphy_config_validate(&opts->mipi_dphy);
14062306a36Sopenharmony_ci	if (ret)
14162306a36Sopenharmony_ci		return ret;
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci	memcpy(&priv->config, opts, sizeof(priv->config));
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ci	priv->dsi_configured = true;
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ci	/* If PHY was already powered on, setup the DSI analog part */
14862306a36Sopenharmony_ci	if (priv->powered) {
14962306a36Sopenharmony_ci		/* If reconfiguring, disable & reconfigure */
15062306a36Sopenharmony_ci		if (priv->dsi_enabled)
15162306a36Sopenharmony_ci			phy_dsi_analog_disable(priv);
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci		usleep_range(100, 200);
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci		phy_dsi_analog_enable(priv);
15662306a36Sopenharmony_ci	}
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci	return 0;
15962306a36Sopenharmony_ci}
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_cistatic int phy_axg_mipi_pcie_analog_power_on(struct phy *phy)
16262306a36Sopenharmony_ci{
16362306a36Sopenharmony_ci	struct phy_axg_mipi_pcie_analog_priv *priv = phy_get_drvdata(phy);
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ci	phy_bandgap_enable(priv);
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci	if (priv->dsi_configured)
16862306a36Sopenharmony_ci		phy_dsi_analog_enable(priv);
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ci	priv->powered = true;
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci	return 0;
17362306a36Sopenharmony_ci}
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_cistatic int phy_axg_mipi_pcie_analog_power_off(struct phy *phy)
17662306a36Sopenharmony_ci{
17762306a36Sopenharmony_ci	struct phy_axg_mipi_pcie_analog_priv *priv = phy_get_drvdata(phy);
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci	phy_bandgap_disable(priv);
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ci	if (priv->dsi_enabled)
18262306a36Sopenharmony_ci		phy_dsi_analog_disable(priv);
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_ci	priv->powered = false;
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ci	return 0;
18762306a36Sopenharmony_ci}
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_cistatic const struct phy_ops phy_axg_mipi_pcie_analog_ops = {
19062306a36Sopenharmony_ci	.configure = phy_axg_mipi_pcie_analog_configure,
19162306a36Sopenharmony_ci	.power_on = phy_axg_mipi_pcie_analog_power_on,
19262306a36Sopenharmony_ci	.power_off = phy_axg_mipi_pcie_analog_power_off,
19362306a36Sopenharmony_ci	.owner = THIS_MODULE,
19462306a36Sopenharmony_ci};
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_cistatic int phy_axg_mipi_pcie_analog_probe(struct platform_device *pdev)
19762306a36Sopenharmony_ci{
19862306a36Sopenharmony_ci	struct phy_provider *phy;
19962306a36Sopenharmony_ci	struct device *dev = &pdev->dev;
20062306a36Sopenharmony_ci	struct phy_axg_mipi_pcie_analog_priv *priv;
20162306a36Sopenharmony_ci	struct device_node *np = dev->of_node, *parent_np;
20262306a36Sopenharmony_ci	struct regmap *map;
20362306a36Sopenharmony_ci	int ret;
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_ci	priv = devm_kmalloc(dev, sizeof(*priv), GFP_KERNEL);
20662306a36Sopenharmony_ci	if (!priv)
20762306a36Sopenharmony_ci		return -ENOMEM;
20862306a36Sopenharmony_ci
20962306a36Sopenharmony_ci	/* Get the hhi system controller node */
21062306a36Sopenharmony_ci	parent_np = of_get_parent(dev->of_node);
21162306a36Sopenharmony_ci	map = syscon_node_to_regmap(parent_np);
21262306a36Sopenharmony_ci	of_node_put(parent_np);
21362306a36Sopenharmony_ci	if (IS_ERR(map)) {
21462306a36Sopenharmony_ci		dev_err(dev,
21562306a36Sopenharmony_ci			"failed to get HHI regmap\n");
21662306a36Sopenharmony_ci		return PTR_ERR(map);
21762306a36Sopenharmony_ci	}
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_ci	priv->regmap = map;
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_ci	priv->phy = devm_phy_create(dev, np, &phy_axg_mipi_pcie_analog_ops);
22262306a36Sopenharmony_ci	if (IS_ERR(priv->phy)) {
22362306a36Sopenharmony_ci		ret = PTR_ERR(priv->phy);
22462306a36Sopenharmony_ci		if (ret != -EPROBE_DEFER)
22562306a36Sopenharmony_ci			dev_err(dev, "failed to create PHY\n");
22662306a36Sopenharmony_ci		return ret;
22762306a36Sopenharmony_ci	}
22862306a36Sopenharmony_ci
22962306a36Sopenharmony_ci	phy_set_drvdata(priv->phy, priv);
23062306a36Sopenharmony_ci	dev_set_drvdata(dev, priv);
23162306a36Sopenharmony_ci
23262306a36Sopenharmony_ci	phy = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_ci	return PTR_ERR_OR_ZERO(phy);
23562306a36Sopenharmony_ci}
23662306a36Sopenharmony_ci
23762306a36Sopenharmony_cistatic const struct of_device_id phy_axg_mipi_pcie_analog_of_match[] = {
23862306a36Sopenharmony_ci	{
23962306a36Sopenharmony_ci		.compatible = "amlogic,axg-mipi-pcie-analog-phy",
24062306a36Sopenharmony_ci	},
24162306a36Sopenharmony_ci	{ },
24262306a36Sopenharmony_ci};
24362306a36Sopenharmony_ciMODULE_DEVICE_TABLE(of, phy_axg_mipi_pcie_analog_of_match);
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_cistatic struct platform_driver phy_axg_mipi_pcie_analog_driver = {
24662306a36Sopenharmony_ci	.probe = phy_axg_mipi_pcie_analog_probe,
24762306a36Sopenharmony_ci	.driver = {
24862306a36Sopenharmony_ci		.name = "phy-axg-mipi-pcie-analog",
24962306a36Sopenharmony_ci		.of_match_table = phy_axg_mipi_pcie_analog_of_match,
25062306a36Sopenharmony_ci	},
25162306a36Sopenharmony_ci};
25262306a36Sopenharmony_cimodule_platform_driver(phy_axg_mipi_pcie_analog_driver);
25362306a36Sopenharmony_ci
25462306a36Sopenharmony_ciMODULE_AUTHOR("Remi Pommarel <repk@triplefau.lt>");
25562306a36Sopenharmony_ciMODULE_DESCRIPTION("Amlogic AXG MIPI + PCIE analog PHY driver");
25662306a36Sopenharmony_ciMODULE_LICENSE("GPL v2");
257