18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Amlogic AXG MIPI + PCIE analog PHY driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2019 Remi Pommarel <repk@triplefau.lt>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci#include <linux/module.h>
88c2ecf20Sopenharmony_ci#include <linux/phy/phy.h>
98c2ecf20Sopenharmony_ci#include <linux/regmap.h>
108c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
118c2ecf20Sopenharmony_ci#include <dt-bindings/phy/phy.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#define HHI_MIPI_CNTL0 0x00
148c2ecf20Sopenharmony_ci#define		HHI_MIPI_CNTL0_COMMON_BLOCK	GENMASK(31, 28)
158c2ecf20Sopenharmony_ci#define		HHI_MIPI_CNTL0_ENABLE		BIT(29)
168c2ecf20Sopenharmony_ci#define		HHI_MIPI_CNTL0_BANDGAP		BIT(26)
178c2ecf20Sopenharmony_ci#define		HHI_MIPI_CNTL0_DECODE_TO_RTERM	GENMASK(15, 12)
188c2ecf20Sopenharmony_ci#define		HHI_MIPI_CNTL0_OUTPUT_EN	BIT(3)
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#define HHI_MIPI_CNTL1 0x01
218c2ecf20Sopenharmony_ci#define		HHI_MIPI_CNTL1_CH0_CML_PDR_EN	BIT(12)
228c2ecf20Sopenharmony_ci#define		HHI_MIPI_CNTL1_LP_ABILITY	GENMASK(5, 4)
238c2ecf20Sopenharmony_ci#define		HHI_MIPI_CNTL1_LP_RESISTER	BIT(3)
248c2ecf20Sopenharmony_ci#define		HHI_MIPI_CNTL1_INPUT_SETTING	BIT(2)
258c2ecf20Sopenharmony_ci#define		HHI_MIPI_CNTL1_INPUT_SEL	BIT(1)
268c2ecf20Sopenharmony_ci#define		HHI_MIPI_CNTL1_PRBS7_EN		BIT(0)
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#define HHI_MIPI_CNTL2 0x02
298c2ecf20Sopenharmony_ci#define		HHI_MIPI_CNTL2_CH_PU		GENMASK(31, 25)
308c2ecf20Sopenharmony_ci#define		HHI_MIPI_CNTL2_CH_CTL		GENMASK(24, 19)
318c2ecf20Sopenharmony_ci#define		HHI_MIPI_CNTL2_CH0_DIGDR_EN	BIT(18)
328c2ecf20Sopenharmony_ci#define		HHI_MIPI_CNTL2_CH_DIGDR_EN	BIT(17)
338c2ecf20Sopenharmony_ci#define		HHI_MIPI_CNTL2_LPULPS_EN	BIT(16)
348c2ecf20Sopenharmony_ci#define		HHI_MIPI_CNTL2_CH_EN(n)		BIT(15 - (n))
358c2ecf20Sopenharmony_ci#define		HHI_MIPI_CNTL2_CH0_LP_CTL	GENMASK(10, 1)
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cistruct phy_axg_mipi_pcie_analog_priv {
388c2ecf20Sopenharmony_ci	struct phy *phy;
398c2ecf20Sopenharmony_ci	unsigned int mode;
408c2ecf20Sopenharmony_ci	struct regmap *regmap;
418c2ecf20Sopenharmony_ci};
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic const struct regmap_config phy_axg_mipi_pcie_analog_regmap_conf = {
448c2ecf20Sopenharmony_ci	.reg_bits = 8,
458c2ecf20Sopenharmony_ci	.val_bits = 32,
468c2ecf20Sopenharmony_ci	.reg_stride = 4,
478c2ecf20Sopenharmony_ci	.max_register = HHI_MIPI_CNTL2,
488c2ecf20Sopenharmony_ci};
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cistatic int phy_axg_mipi_pcie_analog_power_on(struct phy *phy)
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	struct phy_axg_mipi_pcie_analog_priv *priv = phy_get_drvdata(phy);
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	/* MIPI not supported yet */
558c2ecf20Sopenharmony_ci	if (priv->mode != PHY_TYPE_PCIE)
568c2ecf20Sopenharmony_ci		return -EINVAL;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	regmap_update_bits(priv->regmap, HHI_MIPI_CNTL0,
598c2ecf20Sopenharmony_ci			   HHI_MIPI_CNTL0_BANDGAP, HHI_MIPI_CNTL0_BANDGAP);
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	regmap_update_bits(priv->regmap, HHI_MIPI_CNTL0,
628c2ecf20Sopenharmony_ci			   HHI_MIPI_CNTL0_ENABLE, HHI_MIPI_CNTL0_ENABLE);
638c2ecf20Sopenharmony_ci	return 0;
648c2ecf20Sopenharmony_ci}
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_cistatic int phy_axg_mipi_pcie_analog_power_off(struct phy *phy)
678c2ecf20Sopenharmony_ci{
688c2ecf20Sopenharmony_ci	struct phy_axg_mipi_pcie_analog_priv *priv = phy_get_drvdata(phy);
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	/* MIPI not supported yet */
718c2ecf20Sopenharmony_ci	if (priv->mode != PHY_TYPE_PCIE)
728c2ecf20Sopenharmony_ci		return -EINVAL;
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	regmap_update_bits(priv->regmap, HHI_MIPI_CNTL0,
758c2ecf20Sopenharmony_ci			   HHI_MIPI_CNTL0_BANDGAP, 0);
768c2ecf20Sopenharmony_ci	regmap_update_bits(priv->regmap, HHI_MIPI_CNTL0,
778c2ecf20Sopenharmony_ci			   HHI_MIPI_CNTL0_ENABLE, 0);
788c2ecf20Sopenharmony_ci	return 0;
798c2ecf20Sopenharmony_ci}
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_cistatic int phy_axg_mipi_pcie_analog_init(struct phy *phy)
828c2ecf20Sopenharmony_ci{
838c2ecf20Sopenharmony_ci	return 0;
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cistatic int phy_axg_mipi_pcie_analog_exit(struct phy *phy)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	return 0;
898c2ecf20Sopenharmony_ci}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistatic const struct phy_ops phy_axg_mipi_pcie_analog_ops = {
928c2ecf20Sopenharmony_ci	.init = phy_axg_mipi_pcie_analog_init,
938c2ecf20Sopenharmony_ci	.exit = phy_axg_mipi_pcie_analog_exit,
948c2ecf20Sopenharmony_ci	.power_on = phy_axg_mipi_pcie_analog_power_on,
958c2ecf20Sopenharmony_ci	.power_off = phy_axg_mipi_pcie_analog_power_off,
968c2ecf20Sopenharmony_ci	.owner = THIS_MODULE,
978c2ecf20Sopenharmony_ci};
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_cistatic struct phy *phy_axg_mipi_pcie_analog_xlate(struct device *dev,
1008c2ecf20Sopenharmony_ci						  struct of_phandle_args *args)
1018c2ecf20Sopenharmony_ci{
1028c2ecf20Sopenharmony_ci	struct phy_axg_mipi_pcie_analog_priv *priv = dev_get_drvdata(dev);
1038c2ecf20Sopenharmony_ci	unsigned int mode;
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	if (args->args_count != 1) {
1068c2ecf20Sopenharmony_ci		dev_err(dev, "invalid number of arguments\n");
1078c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
1088c2ecf20Sopenharmony_ci	}
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	mode = args->args[0];
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	/* MIPI mode is not supported yet */
1138c2ecf20Sopenharmony_ci	if (mode != PHY_TYPE_PCIE) {
1148c2ecf20Sopenharmony_ci		dev_err(dev, "invalid phy mode select argument\n");
1158c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
1168c2ecf20Sopenharmony_ci	}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	priv->mode = mode;
1198c2ecf20Sopenharmony_ci	return priv->phy;
1208c2ecf20Sopenharmony_ci}
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cistatic int phy_axg_mipi_pcie_analog_probe(struct platform_device *pdev)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	struct phy_provider *phy;
1258c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
1268c2ecf20Sopenharmony_ci	struct phy_axg_mipi_pcie_analog_priv *priv;
1278c2ecf20Sopenharmony_ci	struct device_node *np = dev->of_node;
1288c2ecf20Sopenharmony_ci	struct regmap *map;
1298c2ecf20Sopenharmony_ci	struct resource *res;
1308c2ecf20Sopenharmony_ci	void __iomem *base;
1318c2ecf20Sopenharmony_ci	int ret;
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	priv = devm_kmalloc(dev, sizeof(*priv), GFP_KERNEL);
1348c2ecf20Sopenharmony_ci	if (!priv)
1358c2ecf20Sopenharmony_ci		return -ENOMEM;
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1388c2ecf20Sopenharmony_ci	base = devm_ioremap_resource(dev, res);
1398c2ecf20Sopenharmony_ci	if (IS_ERR(base)) {
1408c2ecf20Sopenharmony_ci		dev_err(dev, "failed to get regmap base\n");
1418c2ecf20Sopenharmony_ci		return PTR_ERR(base);
1428c2ecf20Sopenharmony_ci	}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	map = devm_regmap_init_mmio(dev, base,
1458c2ecf20Sopenharmony_ci				    &phy_axg_mipi_pcie_analog_regmap_conf);
1468c2ecf20Sopenharmony_ci	if (IS_ERR(map)) {
1478c2ecf20Sopenharmony_ci		dev_err(dev, "failed to get HHI regmap\n");
1488c2ecf20Sopenharmony_ci		return PTR_ERR(map);
1498c2ecf20Sopenharmony_ci	}
1508c2ecf20Sopenharmony_ci	priv->regmap = map;
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	priv->phy = devm_phy_create(dev, np, &phy_axg_mipi_pcie_analog_ops);
1538c2ecf20Sopenharmony_ci	if (IS_ERR(priv->phy)) {
1548c2ecf20Sopenharmony_ci		ret = PTR_ERR(priv->phy);
1558c2ecf20Sopenharmony_ci		if (ret != -EPROBE_DEFER)
1568c2ecf20Sopenharmony_ci			dev_err(dev, "failed to create PHY\n");
1578c2ecf20Sopenharmony_ci		return ret;
1588c2ecf20Sopenharmony_ci	}
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	phy_set_drvdata(priv->phy, priv);
1618c2ecf20Sopenharmony_ci	dev_set_drvdata(dev, priv);
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	phy = devm_of_phy_provider_register(dev,
1648c2ecf20Sopenharmony_ci					    phy_axg_mipi_pcie_analog_xlate);
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	return PTR_ERR_OR_ZERO(phy);
1678c2ecf20Sopenharmony_ci}
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_cistatic const struct of_device_id phy_axg_mipi_pcie_analog_of_match[] = {
1708c2ecf20Sopenharmony_ci	{
1718c2ecf20Sopenharmony_ci		.compatible = "amlogic,axg-mipi-pcie-analog-phy",
1728c2ecf20Sopenharmony_ci	},
1738c2ecf20Sopenharmony_ci	{ },
1748c2ecf20Sopenharmony_ci};
1758c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, phy_axg_mipi_pcie_analog_of_match);
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_cistatic struct platform_driver phy_axg_mipi_pcie_analog_driver = {
1788c2ecf20Sopenharmony_ci	.probe = phy_axg_mipi_pcie_analog_probe,
1798c2ecf20Sopenharmony_ci	.driver = {
1808c2ecf20Sopenharmony_ci		.name = "phy-axg-mipi-pcie-analog",
1818c2ecf20Sopenharmony_ci		.of_match_table = phy_axg_mipi_pcie_analog_of_match,
1828c2ecf20Sopenharmony_ci	},
1838c2ecf20Sopenharmony_ci};
1848c2ecf20Sopenharmony_cimodule_platform_driver(phy_axg_mipi_pcie_analog_driver);
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ciMODULE_AUTHOR("Remi Pommarel <repk@triplefau.lt>");
1878c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Amlogic AXG MIPI + PCIE analog PHY driver");
1888c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
189