xref: /linux/drivers/phy/mediatek/phy-mtk-mipi-csi-0-5.c (revision 79790b6818e96c58fe2bffee1b418c16e64e7b80)
1442f34edSPhi-bang Nguyen // SPDX-License-Identifier: GPL-2.0
2442f34edSPhi-bang Nguyen /*
3442f34edSPhi-bang Nguyen  * MediaTek MIPI CSI v0.5 driver
4442f34edSPhi-bang Nguyen  *
5442f34edSPhi-bang Nguyen  * Copyright (c) 2023, MediaTek Inc.
6442f34edSPhi-bang Nguyen  * Copyright (c) 2023, BayLibre Inc.
7442f34edSPhi-bang Nguyen  */
8442f34edSPhi-bang Nguyen 
9442f34edSPhi-bang Nguyen #include <dt-bindings/phy/phy.h>
10442f34edSPhi-bang Nguyen #include <linux/bitfield.h>
11442f34edSPhi-bang Nguyen #include <linux/delay.h>
12442f34edSPhi-bang Nguyen #include <linux/io.h>
13442f34edSPhi-bang Nguyen #include <linux/module.h>
14442f34edSPhi-bang Nguyen #include <linux/mutex.h>
15442f34edSPhi-bang Nguyen #include <linux/phy/phy.h>
16442f34edSPhi-bang Nguyen #include <linux/platform_device.h>
17442f34edSPhi-bang Nguyen #include <linux/slab.h>
18442f34edSPhi-bang Nguyen 
19442f34edSPhi-bang Nguyen #include "phy-mtk-io.h"
20442f34edSPhi-bang Nguyen #include "phy-mtk-mipi-csi-0-5-rx-reg.h"
21442f34edSPhi-bang Nguyen 
22442f34edSPhi-bang Nguyen #define CSIXB_OFFSET		0x1000
23442f34edSPhi-bang Nguyen 
24442f34edSPhi-bang Nguyen struct mtk_mipi_cdphy_port {
25442f34edSPhi-bang Nguyen 	struct device *dev;
26442f34edSPhi-bang Nguyen 	void __iomem *base;
27442f34edSPhi-bang Nguyen 	struct phy *phy;
28442f34edSPhi-bang Nguyen 	u32 type;
29442f34edSPhi-bang Nguyen 	u32 mode;
30442f34edSPhi-bang Nguyen 	u32 num_lanes;
31442f34edSPhi-bang Nguyen };
32442f34edSPhi-bang Nguyen 
33442f34edSPhi-bang Nguyen enum PHY_TYPE {
34442f34edSPhi-bang Nguyen 	DPHY = 0,
35442f34edSPhi-bang Nguyen 	CPHY,
36442f34edSPhi-bang Nguyen 	CDPHY,
37442f34edSPhi-bang Nguyen };
38442f34edSPhi-bang Nguyen 
mtk_phy_csi_cdphy_ana_eq_tune(void __iomem * base)39442f34edSPhi-bang Nguyen static void mtk_phy_csi_cdphy_ana_eq_tune(void __iomem *base)
40442f34edSPhi-bang Nguyen {
41442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + MIPI_RX_ANA18_CSIXA, RG_CSI0A_L0_T0AB_EQ_IS, 1);
42442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + MIPI_RX_ANA18_CSIXA, RG_CSI0A_L0_T0AB_EQ_BW, 1);
43442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + MIPI_RX_ANA1C_CSIXA, RG_CSI0A_L1_T1AB_EQ_IS, 1);
44442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + MIPI_RX_ANA1C_CSIXA, RG_CSI0A_L1_T1AB_EQ_BW, 1);
45442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + MIPI_RX_ANA20_CSI0A, RG_CSI0A_L2_T1BC_EQ_IS, 1);
46442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + MIPI_RX_ANA20_CSI0A, RG_CSI0A_L2_T1BC_EQ_BW, 1);
47442f34edSPhi-bang Nguyen 
48442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + CSIXB_OFFSET + MIPI_RX_ANA18_CSIXA, RG_CSI0A_L0_T0AB_EQ_IS, 1);
49442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + CSIXB_OFFSET + MIPI_RX_ANA18_CSIXA, RG_CSI0A_L0_T0AB_EQ_BW, 1);
50442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + CSIXB_OFFSET + MIPI_RX_ANA1C_CSIXA, RG_CSI0A_L1_T1AB_EQ_IS, 1);
51442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + CSIXB_OFFSET + MIPI_RX_ANA1C_CSIXA, RG_CSI0A_L1_T1AB_EQ_BW, 1);
52442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + CSIXB_OFFSET + MIPI_RX_ANA20_CSI0A, RG_CSI0A_L2_T1BC_EQ_IS, 1);
53442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + CSIXB_OFFSET + MIPI_RX_ANA20_CSI0A, RG_CSI0A_L2_T1BC_EQ_BW, 1);
54442f34edSPhi-bang Nguyen }
55442f34edSPhi-bang Nguyen 
mtk_phy_csi_dphy_ana_eq_tune(void __iomem * base)56442f34edSPhi-bang Nguyen static void mtk_phy_csi_dphy_ana_eq_tune(void __iomem *base)
57442f34edSPhi-bang Nguyen {
58442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + MIPI_RX_ANA18_CSIXA, RG_CSI1A_L0_EQ_IS, 1);
59442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + MIPI_RX_ANA18_CSIXA, RG_CSI1A_L0_EQ_BW, 1);
60442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + MIPI_RX_ANA18_CSIXA, RG_CSI1A_L1_EQ_IS, 1);
61442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + MIPI_RX_ANA18_CSIXA, RG_CSI1A_L1_EQ_BW, 1);
62442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + MIPI_RX_ANA1C_CSIXA, RG_CSI1A_L2_EQ_IS, 1);
63442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + MIPI_RX_ANA1C_CSIXA, RG_CSI1A_L2_EQ_BW, 1);
64442f34edSPhi-bang Nguyen 
65442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + CSIXB_OFFSET + MIPI_RX_ANA18_CSIXA, RG_CSI1A_L0_EQ_IS, 1);
66442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + CSIXB_OFFSET + MIPI_RX_ANA18_CSIXA, RG_CSI1A_L0_EQ_BW, 1);
67442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + CSIXB_OFFSET + MIPI_RX_ANA18_CSIXA, RG_CSI1A_L1_EQ_IS, 1);
68442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + CSIXB_OFFSET + MIPI_RX_ANA18_CSIXA, RG_CSI1A_L1_EQ_BW, 1);
69442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + CSIXB_OFFSET + MIPI_RX_ANA1C_CSIXA, RG_CSI1A_L2_EQ_IS, 1);
70442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + CSIXB_OFFSET + MIPI_RX_ANA1C_CSIXA, RG_CSI1A_L2_EQ_BW, 1);
71442f34edSPhi-bang Nguyen }
72442f34edSPhi-bang Nguyen 
mtk_mipi_phy_power_on(struct phy * phy)73442f34edSPhi-bang Nguyen static int mtk_mipi_phy_power_on(struct phy *phy)
74442f34edSPhi-bang Nguyen {
75442f34edSPhi-bang Nguyen 	struct mtk_mipi_cdphy_port *port = phy_get_drvdata(phy);
76442f34edSPhi-bang Nguyen 	void __iomem *base = port->base;
77442f34edSPhi-bang Nguyen 
78442f34edSPhi-bang Nguyen 	/*
79442f34edSPhi-bang Nguyen 	 * The driver currently supports DPHY and CD-PHY phys,
80442f34edSPhi-bang Nguyen 	 * but the only mode supported is DPHY,
81442f34edSPhi-bang Nguyen 	 * so CD-PHY capable phys must be configured in DPHY mode
82442f34edSPhi-bang Nguyen 	 */
83442f34edSPhi-bang Nguyen 	if (port->type == CDPHY) {
84442f34edSPhi-bang Nguyen 		mtk_phy_update_field(base + MIPI_RX_ANA00_CSIXA, RG_CSI0A_CPHY_EN, 0);
85442f34edSPhi-bang Nguyen 		mtk_phy_update_field(base + CSIXB_OFFSET + MIPI_RX_ANA00_CSIXA,
86442f34edSPhi-bang Nguyen 				     RG_CSI0A_CPHY_EN, 0);
87442f34edSPhi-bang Nguyen 	}
88442f34edSPhi-bang Nguyen 
89442f34edSPhi-bang Nguyen 	/*
90442f34edSPhi-bang Nguyen 	 * Lane configuration:
91442f34edSPhi-bang Nguyen 	 *
92442f34edSPhi-bang Nguyen 	 * Only 4 data + 1 clock is supported for now with the following mapping:
93442f34edSPhi-bang Nguyen 	 *
94442f34edSPhi-bang Nguyen 	 * CSIXA_LNR0 --> D2
95442f34edSPhi-bang Nguyen 	 * CSIXA_LNR1 --> D0
96442f34edSPhi-bang Nguyen 	 * CSIXA_LNR2 --> C
97442f34edSPhi-bang Nguyen 	 * CSIXB_LNR0 --> D1
98442f34edSPhi-bang Nguyen 	 * CSIXB_LNR1 --> D3
99442f34edSPhi-bang Nguyen 	 */
100442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + MIPI_RX_ANA00_CSIXA, RG_CSIXA_DPHY_L0_CKMODE_EN, 0);
101442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + MIPI_RX_ANA00_CSIXA, RG_CSIXA_DPHY_L0_CKSEL, 1);
102442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + MIPI_RX_ANA00_CSIXA, RG_CSIXA_DPHY_L1_CKMODE_EN, 0);
103442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + MIPI_RX_ANA00_CSIXA, RG_CSIXA_DPHY_L1_CKSEL, 1);
104442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + MIPI_RX_ANA00_CSIXA, RG_CSIXA_DPHY_L2_CKMODE_EN, 1);
105442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + MIPI_RX_ANA00_CSIXA, RG_CSIXA_DPHY_L2_CKSEL, 1);
106442f34edSPhi-bang Nguyen 
107442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + CSIXB_OFFSET + MIPI_RX_ANA00_CSIXA,
108442f34edSPhi-bang Nguyen 			     RG_CSIXA_DPHY_L0_CKMODE_EN, 0);
109442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + CSIXB_OFFSET + MIPI_RX_ANA00_CSIXA, RG_CSIXA_DPHY_L0_CKSEL, 1);
110442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + CSIXB_OFFSET + MIPI_RX_ANA00_CSIXA,
111442f34edSPhi-bang Nguyen 			     RG_CSIXA_DPHY_L1_CKMODE_EN, 0);
112442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + CSIXB_OFFSET + MIPI_RX_ANA00_CSIXA, RG_CSIXA_DPHY_L1_CKSEL, 1);
113442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + CSIXB_OFFSET + MIPI_RX_ANA00_CSIXA,
114442f34edSPhi-bang Nguyen 			     RG_CSIXA_DPHY_L2_CKMODE_EN, 0);
115442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + CSIXB_OFFSET + MIPI_RX_ANA00_CSIXA, RG_CSIXA_DPHY_L2_CKSEL, 1);
116442f34edSPhi-bang Nguyen 
117442f34edSPhi-bang Nguyen 	/* Byte clock invert */
118442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + MIPI_RX_ANAA8_CSIXA, RG_CSIXA_CDPHY_L0_T0_BYTECK_INVERT, 1);
119442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + MIPI_RX_ANAA8_CSIXA, RG_CSIXA_DPHY_L1_BYTECK_INVERT, 1);
120442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + MIPI_RX_ANAA8_CSIXA, RG_CSIXA_CDPHY_L2_T1_BYTECK_INVERT, 1);
121442f34edSPhi-bang Nguyen 
122442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + CSIXB_OFFSET + MIPI_RX_ANAA8_CSIXA,
123442f34edSPhi-bang Nguyen 			     RG_CSIXA_CDPHY_L0_T0_BYTECK_INVERT, 1);
124442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + CSIXB_OFFSET + MIPI_RX_ANAA8_CSIXA,
125442f34edSPhi-bang Nguyen 			     RG_CSIXA_DPHY_L1_BYTECK_INVERT, 1);
126442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + CSIXB_OFFSET + MIPI_RX_ANAA8_CSIXA,
127442f34edSPhi-bang Nguyen 			     RG_CSIXA_CDPHY_L2_T1_BYTECK_INVERT, 1);
128442f34edSPhi-bang Nguyen 
129442f34edSPhi-bang Nguyen 	/* Start ANA EQ tuning */
130442f34edSPhi-bang Nguyen 	if (port->type == CDPHY)
131442f34edSPhi-bang Nguyen 		mtk_phy_csi_cdphy_ana_eq_tune(base);
132442f34edSPhi-bang Nguyen 	else
133442f34edSPhi-bang Nguyen 		mtk_phy_csi_dphy_ana_eq_tune(base);
134442f34edSPhi-bang Nguyen 
135442f34edSPhi-bang Nguyen 	/* End ANA EQ tuning */
136442f34edSPhi-bang Nguyen 	mtk_phy_set_bits(base + MIPI_RX_ANA40_CSIXA, 0x90);
137442f34edSPhi-bang Nguyen 
138442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + MIPI_RX_ANA24_CSIXA, RG_CSIXA_RESERVE, 0x40);
139442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + CSIXB_OFFSET + MIPI_RX_ANA24_CSIXA, RG_CSIXA_RESERVE, 0x40);
140442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + MIPI_RX_WRAPPER80_CSIXA, CSR_CSI_RST_MODE, 0);
141442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + CSIXB_OFFSET + MIPI_RX_WRAPPER80_CSIXA, CSR_CSI_RST_MODE, 0);
142442f34edSPhi-bang Nguyen 	/* ANA power on */
143442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + MIPI_RX_ANA00_CSIXA, RG_CSIXA_BG_CORE_EN, 1);
144442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + CSIXB_OFFSET + MIPI_RX_ANA00_CSIXA, RG_CSIXA_BG_CORE_EN, 1);
145442f34edSPhi-bang Nguyen 	usleep_range(20, 40);
146442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + MIPI_RX_ANA00_CSIXA, RG_CSIXA_BG_LPF_EN, 1);
147442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + CSIXB_OFFSET + MIPI_RX_ANA00_CSIXA, RG_CSIXA_BG_LPF_EN, 1);
148442f34edSPhi-bang Nguyen 
149442f34edSPhi-bang Nguyen 	return 0;
150442f34edSPhi-bang Nguyen }
151442f34edSPhi-bang Nguyen 
mtk_mipi_phy_power_off(struct phy * phy)152442f34edSPhi-bang Nguyen static int mtk_mipi_phy_power_off(struct phy *phy)
153442f34edSPhi-bang Nguyen {
154442f34edSPhi-bang Nguyen 	struct mtk_mipi_cdphy_port *port = phy_get_drvdata(phy);
155442f34edSPhi-bang Nguyen 	void __iomem *base = port->base;
156442f34edSPhi-bang Nguyen 
157442f34edSPhi-bang Nguyen 	/* Disable MIPI BG. */
158442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + MIPI_RX_ANA00_CSIXA, RG_CSIXA_BG_CORE_EN, 0);
159442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + MIPI_RX_ANA00_CSIXA, RG_CSIXA_BG_LPF_EN, 0);
160442f34edSPhi-bang Nguyen 
161442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + CSIXB_OFFSET + MIPI_RX_ANA00_CSIXA, RG_CSIXA_BG_CORE_EN, 0);
162442f34edSPhi-bang Nguyen 	mtk_phy_update_field(base + CSIXB_OFFSET + MIPI_RX_ANA00_CSIXA, RG_CSIXA_BG_LPF_EN, 0);
163442f34edSPhi-bang Nguyen 
164442f34edSPhi-bang Nguyen 	return 0;
165442f34edSPhi-bang Nguyen }
166442f34edSPhi-bang Nguyen 
mtk_mipi_cdphy_xlate(struct device * dev,const struct of_phandle_args * args)167442f34edSPhi-bang Nguyen static struct phy *mtk_mipi_cdphy_xlate(struct device *dev,
168*00ca8a15SKrzysztof Kozlowski 					const struct of_phandle_args *args)
169442f34edSPhi-bang Nguyen {
170442f34edSPhi-bang Nguyen 	struct mtk_mipi_cdphy_port *priv = dev_get_drvdata(dev);
171442f34edSPhi-bang Nguyen 
172442f34edSPhi-bang Nguyen 	/*
173442f34edSPhi-bang Nguyen 	 * If PHY is CD-PHY then we need to get the operating mode
174442f34edSPhi-bang Nguyen 	 * For now only D-PHY mode is supported
175442f34edSPhi-bang Nguyen 	 */
176442f34edSPhi-bang Nguyen 	if (priv->type == CDPHY) {
177442f34edSPhi-bang Nguyen 		if (args->args_count != 1) {
178442f34edSPhi-bang Nguyen 			dev_err(dev, "invalid number of arguments\n");
179442f34edSPhi-bang Nguyen 			return ERR_PTR(-EINVAL);
180442f34edSPhi-bang Nguyen 		}
181442f34edSPhi-bang Nguyen 		switch (args->args[0]) {
182442f34edSPhi-bang Nguyen 		case PHY_TYPE_DPHY:
183442f34edSPhi-bang Nguyen 			priv->mode = DPHY;
184442f34edSPhi-bang Nguyen 			if (priv->num_lanes != 4) {
185442f34edSPhi-bang Nguyen 				dev_err(dev, "Only 4D1C mode is supported for now!\n");
186442f34edSPhi-bang Nguyen 				return ERR_PTR(-EINVAL);
187442f34edSPhi-bang Nguyen 			}
188442f34edSPhi-bang Nguyen 			break;
189442f34edSPhi-bang Nguyen 		default:
190442f34edSPhi-bang Nguyen 			dev_err(dev, "Unsupported PHY type: %i\n", args->args[0]);
191442f34edSPhi-bang Nguyen 			return ERR_PTR(-EINVAL);
192442f34edSPhi-bang Nguyen 		}
193442f34edSPhi-bang Nguyen 	} else {
194442f34edSPhi-bang Nguyen 		if (args->args_count) {
195442f34edSPhi-bang Nguyen 			dev_err(dev, "invalid number of arguments\n");
196442f34edSPhi-bang Nguyen 			return ERR_PTR(-EINVAL);
197442f34edSPhi-bang Nguyen 		}
198442f34edSPhi-bang Nguyen 		priv->mode = DPHY;
199442f34edSPhi-bang Nguyen 	}
200442f34edSPhi-bang Nguyen 
201442f34edSPhi-bang Nguyen 	return priv->phy;
202442f34edSPhi-bang Nguyen }
203442f34edSPhi-bang Nguyen 
204442f34edSPhi-bang Nguyen static const struct phy_ops mtk_cdphy_ops = {
205442f34edSPhi-bang Nguyen 	.power_on	= mtk_mipi_phy_power_on,
206442f34edSPhi-bang Nguyen 	.power_off	= mtk_mipi_phy_power_off,
207442f34edSPhi-bang Nguyen 	.owner		= THIS_MODULE,
208442f34edSPhi-bang Nguyen };
209442f34edSPhi-bang Nguyen 
mtk_mipi_cdphy_probe(struct platform_device * pdev)210442f34edSPhi-bang Nguyen static int mtk_mipi_cdphy_probe(struct platform_device *pdev)
211442f34edSPhi-bang Nguyen {
212442f34edSPhi-bang Nguyen 	struct device *dev = &pdev->dev;
213442f34edSPhi-bang Nguyen 	struct phy_provider *phy_provider;
214442f34edSPhi-bang Nguyen 	struct mtk_mipi_cdphy_port *port;
215442f34edSPhi-bang Nguyen 	struct phy *phy;
216442f34edSPhi-bang Nguyen 	int ret;
217442f34edSPhi-bang Nguyen 	u32 phy_type;
218442f34edSPhi-bang Nguyen 
219442f34edSPhi-bang Nguyen 	port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
220442f34edSPhi-bang Nguyen 	if (!port)
221442f34edSPhi-bang Nguyen 		return -ENOMEM;
222442f34edSPhi-bang Nguyen 
223442f34edSPhi-bang Nguyen 	dev_set_drvdata(dev, port);
224442f34edSPhi-bang Nguyen 
225442f34edSPhi-bang Nguyen 	port->dev = dev;
226442f34edSPhi-bang Nguyen 
227442f34edSPhi-bang Nguyen 	port->base = devm_platform_ioremap_resource(pdev, 0);
228442f34edSPhi-bang Nguyen 	if (IS_ERR(port->base))
229442f34edSPhi-bang Nguyen 		return PTR_ERR(port->base);
230442f34edSPhi-bang Nguyen 
231442f34edSPhi-bang Nguyen 	ret = of_property_read_u32(dev->of_node, "num-lanes", &port->num_lanes);
232442f34edSPhi-bang Nguyen 	if (ret) {
233442f34edSPhi-bang Nguyen 		dev_err(dev, "Failed to read num-lanes property: %i\n", ret);
234442f34edSPhi-bang Nguyen 		return ret;
235442f34edSPhi-bang Nguyen 	}
236442f34edSPhi-bang Nguyen 
237442f34edSPhi-bang Nguyen 	/*
238442f34edSPhi-bang Nguyen 	 * phy-type is optional, if not present, PHY is considered to be CD-PHY
239442f34edSPhi-bang Nguyen 	 */
240442f34edSPhi-bang Nguyen 	if (device_property_present(dev, "phy-type")) {
241442f34edSPhi-bang Nguyen 		ret = of_property_read_u32(dev->of_node, "phy-type", &phy_type);
242442f34edSPhi-bang Nguyen 		if (ret) {
243442f34edSPhi-bang Nguyen 			dev_err(dev, "Failed to read phy-type property: %i\n", ret);
244442f34edSPhi-bang Nguyen 			return ret;
245442f34edSPhi-bang Nguyen 		}
246442f34edSPhi-bang Nguyen 		switch (phy_type) {
247442f34edSPhi-bang Nguyen 		case PHY_TYPE_DPHY:
248442f34edSPhi-bang Nguyen 			port->type = DPHY;
249442f34edSPhi-bang Nguyen 			break;
250442f34edSPhi-bang Nguyen 		default:
251442f34edSPhi-bang Nguyen 			dev_err(dev, "Unsupported PHY type: %i\n", phy_type);
252442f34edSPhi-bang Nguyen 			return -EINVAL;
253442f34edSPhi-bang Nguyen 		}
254442f34edSPhi-bang Nguyen 	} else {
255442f34edSPhi-bang Nguyen 		port->type = CDPHY;
256442f34edSPhi-bang Nguyen 	}
257442f34edSPhi-bang Nguyen 
258442f34edSPhi-bang Nguyen 	phy = devm_phy_create(dev, NULL, &mtk_cdphy_ops);
259442f34edSPhi-bang Nguyen 	if (IS_ERR(phy)) {
260442f34edSPhi-bang Nguyen 		dev_err(dev, "Failed to create PHY: %ld\n", PTR_ERR(phy));
261442f34edSPhi-bang Nguyen 		return PTR_ERR(phy);
262442f34edSPhi-bang Nguyen 	}
263442f34edSPhi-bang Nguyen 
264442f34edSPhi-bang Nguyen 	port->phy = phy;
265442f34edSPhi-bang Nguyen 	phy_set_drvdata(phy, port);
266442f34edSPhi-bang Nguyen 
267442f34edSPhi-bang Nguyen 	phy_provider = devm_of_phy_provider_register(dev, mtk_mipi_cdphy_xlate);
268442f34edSPhi-bang Nguyen 	if (IS_ERR(phy_provider)) {
269442f34edSPhi-bang Nguyen 		dev_err(dev, "Failed to register PHY provider: %ld\n",
270442f34edSPhi-bang Nguyen 			PTR_ERR(phy_provider));
271442f34edSPhi-bang Nguyen 		return PTR_ERR(phy_provider);
272442f34edSPhi-bang Nguyen 	}
273442f34edSPhi-bang Nguyen 
274442f34edSPhi-bang Nguyen 	return 0;
275442f34edSPhi-bang Nguyen }
276442f34edSPhi-bang Nguyen 
277442f34edSPhi-bang Nguyen static const struct of_device_id mtk_mipi_cdphy_of_match[] = {
278442f34edSPhi-bang Nguyen 	{ .compatible = "mediatek,mt8365-csi-rx" },
279442f34edSPhi-bang Nguyen 	{ /* sentinel */},
280442f34edSPhi-bang Nguyen };
281442f34edSPhi-bang Nguyen MODULE_DEVICE_TABLE(of, mtk_mipi_cdphy_of_match);
282442f34edSPhi-bang Nguyen 
283442f34edSPhi-bang Nguyen static struct platform_driver mipi_cdphy_pdrv = {
284442f34edSPhi-bang Nguyen 	.probe = mtk_mipi_cdphy_probe,
285442f34edSPhi-bang Nguyen 	.driver	= {
286442f34edSPhi-bang Nguyen 		.name	= "mtk-mipi-csi-0-5",
287442f34edSPhi-bang Nguyen 		.of_match_table = mtk_mipi_cdphy_of_match,
288442f34edSPhi-bang Nguyen 	},
289442f34edSPhi-bang Nguyen };
290442f34edSPhi-bang Nguyen module_platform_driver(mipi_cdphy_pdrv);
291442f34edSPhi-bang Nguyen 
292442f34edSPhi-bang Nguyen MODULE_DESCRIPTION("MediaTek MIPI CSI CD-PHY v0.5 Driver");
293442f34edSPhi-bang Nguyen MODULE_AUTHOR("Louis Kuo <louis.kuo@mediatek.com>");
294442f34edSPhi-bang Nguyen MODULE_LICENSE("GPL");
295