162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Meson G12A MIPI DSI Analog PHY
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (C) 2018 Amlogic, Inc. All rights reserved
662306a36Sopenharmony_ci * Copyright (C) 2022 BayLibre, SAS
762306a36Sopenharmony_ci * Author: Neil Armstrong <narmstrong@baylibre.com>
862306a36Sopenharmony_ci */
962306a36Sopenharmony_ci#include <linux/bitfield.h>
1062306a36Sopenharmony_ci#include <linux/bitops.h>
1162306a36Sopenharmony_ci#include <linux/module.h>
1262306a36Sopenharmony_ci#include <linux/phy/phy.h>
1362306a36Sopenharmony_ci#include <linux/regmap.h>
1462306a36Sopenharmony_ci#include <linux/delay.h>
1562306a36Sopenharmony_ci#include <linux/mfd/syscon.h>
1662306a36Sopenharmony_ci#include <linux/of.h>
1762306a36Sopenharmony_ci#include <linux/platform_device.h>
1862306a36Sopenharmony_ci#include <dt-bindings/phy/phy.h>
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci#define HHI_MIPI_CNTL0 0x00
2162306a36Sopenharmony_ci#define		HHI_MIPI_CNTL0_DIF_REF_CTL1	GENMASK(31, 16)
2262306a36Sopenharmony_ci#define		HHI_MIPI_CNTL0_DIF_REF_CTL0	GENMASK(15, 0)
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci#define HHI_MIPI_CNTL1 0x04
2562306a36Sopenharmony_ci#define		HHI_MIPI_CNTL1_BANDGAP		BIT(16)
2662306a36Sopenharmony_ci#define		HHI_MIPI_CNTL2_DIF_REF_CTL2	GENMASK(15, 0)
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci#define HHI_MIPI_CNTL2 0x08
2962306a36Sopenharmony_ci#define		HHI_MIPI_CNTL2_DIF_TX_CTL1	GENMASK(31, 16)
3062306a36Sopenharmony_ci#define		HHI_MIPI_CNTL2_CH_EN		GENMASK(15, 11)
3162306a36Sopenharmony_ci#define		HHI_MIPI_CNTL2_DIF_TX_CTL0	GENMASK(10, 0)
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci#define DSI_LANE_0				BIT(4)
3462306a36Sopenharmony_ci#define DSI_LANE_1				BIT(3)
3562306a36Sopenharmony_ci#define DSI_LANE_CLK				BIT(2)
3662306a36Sopenharmony_ci#define DSI_LANE_2				BIT(1)
3762306a36Sopenharmony_ci#define DSI_LANE_3				BIT(0)
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_cistruct phy_g12a_mipi_dphy_analog_priv {
4062306a36Sopenharmony_ci	struct phy *phy;
4162306a36Sopenharmony_ci	struct regmap *regmap;
4262306a36Sopenharmony_ci	struct phy_configure_opts_mipi_dphy config;
4362306a36Sopenharmony_ci};
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_cistatic int phy_g12a_mipi_dphy_analog_configure(struct phy *phy,
4662306a36Sopenharmony_ci					       union phy_configure_opts *opts)
4762306a36Sopenharmony_ci{
4862306a36Sopenharmony_ci	struct phy_g12a_mipi_dphy_analog_priv *priv = phy_get_drvdata(phy);
4962306a36Sopenharmony_ci	int ret;
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci	ret = phy_mipi_dphy_config_validate(&opts->mipi_dphy);
5262306a36Sopenharmony_ci	if (ret)
5362306a36Sopenharmony_ci		return ret;
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci	memcpy(&priv->config, opts, sizeof(priv->config));
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci	return 0;
5862306a36Sopenharmony_ci}
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_cistatic int phy_g12a_mipi_dphy_analog_power_on(struct phy *phy)
6162306a36Sopenharmony_ci{
6262306a36Sopenharmony_ci	struct phy_g12a_mipi_dphy_analog_priv *priv = phy_get_drvdata(phy);
6362306a36Sopenharmony_ci	unsigned int reg;
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci	regmap_write(priv->regmap, HHI_MIPI_CNTL0,
6662306a36Sopenharmony_ci		     FIELD_PREP(HHI_MIPI_CNTL0_DIF_REF_CTL0, 0x8) |
6762306a36Sopenharmony_ci		     FIELD_PREP(HHI_MIPI_CNTL0_DIF_REF_CTL1, 0xa487));
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci	regmap_write(priv->regmap, HHI_MIPI_CNTL1,
7062306a36Sopenharmony_ci		     FIELD_PREP(HHI_MIPI_CNTL2_DIF_REF_CTL2, 0x2e) |
7162306a36Sopenharmony_ci		     HHI_MIPI_CNTL1_BANDGAP);
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci	regmap_write(priv->regmap, HHI_MIPI_CNTL2,
7462306a36Sopenharmony_ci		     FIELD_PREP(HHI_MIPI_CNTL2_DIF_TX_CTL0, 0x45a) |
7562306a36Sopenharmony_ci		     FIELD_PREP(HHI_MIPI_CNTL2_DIF_TX_CTL1, 0x2680));
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci	reg = DSI_LANE_CLK;
7862306a36Sopenharmony_ci	switch (priv->config.lanes) {
7962306a36Sopenharmony_ci	case 4:
8062306a36Sopenharmony_ci		reg |= DSI_LANE_3;
8162306a36Sopenharmony_ci		fallthrough;
8262306a36Sopenharmony_ci	case 3:
8362306a36Sopenharmony_ci		reg |= DSI_LANE_2;
8462306a36Sopenharmony_ci		fallthrough;
8562306a36Sopenharmony_ci	case 2:
8662306a36Sopenharmony_ci		reg |= DSI_LANE_1;
8762306a36Sopenharmony_ci		fallthrough;
8862306a36Sopenharmony_ci	case 1:
8962306a36Sopenharmony_ci		reg |= DSI_LANE_0;
9062306a36Sopenharmony_ci		break;
9162306a36Sopenharmony_ci	default:
9262306a36Sopenharmony_ci		reg = 0;
9362306a36Sopenharmony_ci	}
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci	regmap_update_bits(priv->regmap, HHI_MIPI_CNTL2,
9662306a36Sopenharmony_ci			   HHI_MIPI_CNTL2_CH_EN,
9762306a36Sopenharmony_ci			   FIELD_PREP(HHI_MIPI_CNTL2_CH_EN, reg));
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ci	return 0;
10062306a36Sopenharmony_ci}
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_cistatic int phy_g12a_mipi_dphy_analog_power_off(struct phy *phy)
10362306a36Sopenharmony_ci{
10462306a36Sopenharmony_ci	struct phy_g12a_mipi_dphy_analog_priv *priv = phy_get_drvdata(phy);
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci	regmap_write(priv->regmap, HHI_MIPI_CNTL0, 0);
10762306a36Sopenharmony_ci	regmap_write(priv->regmap, HHI_MIPI_CNTL1, 0);
10862306a36Sopenharmony_ci	regmap_write(priv->regmap, HHI_MIPI_CNTL2, 0);
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci	return 0;
11162306a36Sopenharmony_ci}
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_cistatic const struct phy_ops phy_g12a_mipi_dphy_analog_ops = {
11462306a36Sopenharmony_ci	.configure = phy_g12a_mipi_dphy_analog_configure,
11562306a36Sopenharmony_ci	.power_on = phy_g12a_mipi_dphy_analog_power_on,
11662306a36Sopenharmony_ci	.power_off = phy_g12a_mipi_dphy_analog_power_off,
11762306a36Sopenharmony_ci	.owner = THIS_MODULE,
11862306a36Sopenharmony_ci};
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_cistatic int phy_g12a_mipi_dphy_analog_probe(struct platform_device *pdev)
12162306a36Sopenharmony_ci{
12262306a36Sopenharmony_ci	struct phy_provider *phy;
12362306a36Sopenharmony_ci	struct device *dev = &pdev->dev;
12462306a36Sopenharmony_ci	struct phy_g12a_mipi_dphy_analog_priv *priv;
12562306a36Sopenharmony_ci	struct device_node *np = dev->of_node, *parent_np;
12662306a36Sopenharmony_ci	struct regmap *map;
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ci	priv = devm_kmalloc(dev, sizeof(*priv), GFP_KERNEL);
12962306a36Sopenharmony_ci	if (!priv)
13062306a36Sopenharmony_ci		return -ENOMEM;
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ci	/* Get the hhi system controller node */
13362306a36Sopenharmony_ci	parent_np = of_get_parent(np);
13462306a36Sopenharmony_ci	map = syscon_node_to_regmap(parent_np);
13562306a36Sopenharmony_ci	of_node_put(parent_np);
13662306a36Sopenharmony_ci	if (IS_ERR(map))
13762306a36Sopenharmony_ci		return dev_err_probe(dev, PTR_ERR(map), "failed to get HHI regmap\n");
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci	priv->regmap = map;
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci	priv->phy = devm_phy_create(dev, np, &phy_g12a_mipi_dphy_analog_ops);
14262306a36Sopenharmony_ci	if (IS_ERR(priv->phy))
14362306a36Sopenharmony_ci		return dev_err_probe(dev, PTR_ERR(priv->phy), "failed to create PHY\n");
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ci	phy_set_drvdata(priv->phy, priv);
14662306a36Sopenharmony_ci	dev_set_drvdata(dev, priv);
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ci	phy = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ci	return PTR_ERR_OR_ZERO(phy);
15162306a36Sopenharmony_ci}
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_cistatic const struct of_device_id phy_g12a_mipi_dphy_analog_of_match[] = {
15462306a36Sopenharmony_ci	{
15562306a36Sopenharmony_ci		.compatible = "amlogic,g12a-mipi-dphy-analog",
15662306a36Sopenharmony_ci	},
15762306a36Sopenharmony_ci	{ /* sentinel */ }
15862306a36Sopenharmony_ci};
15962306a36Sopenharmony_ciMODULE_DEVICE_TABLE(of, phy_g12a_mipi_dphy_analog_of_match);
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_cistatic struct platform_driver phy_g12a_mipi_dphy_analog_driver = {
16262306a36Sopenharmony_ci	.probe = phy_g12a_mipi_dphy_analog_probe,
16362306a36Sopenharmony_ci	.driver = {
16462306a36Sopenharmony_ci		.name = "phy-meson-g12a-mipi-dphy-analog",
16562306a36Sopenharmony_ci		.of_match_table = phy_g12a_mipi_dphy_analog_of_match,
16662306a36Sopenharmony_ci	},
16762306a36Sopenharmony_ci};
16862306a36Sopenharmony_cimodule_platform_driver(phy_g12a_mipi_dphy_analog_driver);
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ciMODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
17162306a36Sopenharmony_ciMODULE_DESCRIPTION("Meson G12A MIPI Analog D-PHY driver");
17262306a36Sopenharmony_ciMODULE_LICENSE("GPL v2");
173