xref: /linux/drivers/phy/freescale/phy-fsl-imx8qm-lvds-phy.c (revision 9b725d5959a1d9bb670ccdb23add45f0eaafefb5)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2017-2020,2022 NXP
4  */
5 
6 #include <linux/bitfield.h>
7 #include <linux/bits.h>
8 #include <linux/clk.h>
9 #include <linux/mfd/syscon.h>
10 #include <linux/module.h>
11 #include <linux/of.h>
12 #include <linux/phy/phy.h>
13 #include <linux/platform_device.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/regmap.h>
16 #include <linux/units.h>
17 
18 #define REG_SET		0x4
19 #define REG_CLR		0x8
20 
21 #define PHY_CTRL	0x0
22 #define  M_MASK		GENMASK(18, 17)
23 #define  M(n)		FIELD_PREP(M_MASK, (n))
24 #define  CCM_MASK	GENMASK(16, 14)
25 #define  CCM(n)		FIELD_PREP(CCM_MASK, (n))
26 #define  CA_MASK	GENMASK(13, 11)
27 #define  CA(n)		FIELD_PREP(CA_MASK, (n))
28 #define  TST_MASK	GENMASK(10, 5)
29 #define  TST(n)		FIELD_PREP(TST_MASK, (n))
30 #define  CH_EN(id)	BIT(3 + (id))
31 #define  NB		BIT(2)
32 #define  RFB		BIT(1)
33 #define  PD		BIT(0)
34 
35 /* Power On Reset(POR) value */
36 #define  CTRL_RESET_VAL	(M(0x0) | CCM(0x4) | CA(0x4) | TST(0x25))
37 
38 /* PHY initialization value and mask */
39 #define  CTRL_INIT_MASK	(M_MASK | CCM_MASK | CA_MASK | TST_MASK | NB | RFB)
40 #define  CTRL_INIT_VAL	(M(0x0) | CCM(0x5) | CA(0x4) | TST(0x25) | RFB)
41 
42 #define PHY_STATUS	0x10
43 #define  LOCK		BIT(0)
44 
45 #define PHY_NUM		2
46 
47 #define MIN_CLKIN_FREQ	(25 * MEGA)
48 #define MAX_CLKIN_FREQ	(165 * MEGA)
49 
50 #define PLL_LOCK_SLEEP		10
51 #define PLL_LOCK_TIMEOUT	1000
52 
53 struct mixel_lvds_phy {
54 	struct phy *phy;
55 	struct phy_configure_opts_lvds cfg;
56 	unsigned int id;
57 };
58 
59 struct mixel_lvds_phy_priv {
60 	struct regmap *regmap;
61 	struct mutex lock;	/* protect remap access and cfg of our own */
62 	struct clk *phy_ref_clk;
63 	struct mixel_lvds_phy *phys[PHY_NUM];
64 };
65 
mixel_lvds_phy_init(struct phy * phy)66 static int mixel_lvds_phy_init(struct phy *phy)
67 {
68 	struct mixel_lvds_phy_priv *priv = dev_get_drvdata(phy->dev.parent);
69 
70 	mutex_lock(&priv->lock);
71 	regmap_update_bits(priv->regmap,
72 			   PHY_CTRL, CTRL_INIT_MASK, CTRL_INIT_VAL);
73 	mutex_unlock(&priv->lock);
74 
75 	return 0;
76 }
77 
mixel_lvds_phy_power_on(struct phy * phy)78 static int mixel_lvds_phy_power_on(struct phy *phy)
79 {
80 	struct mixel_lvds_phy_priv *priv = dev_get_drvdata(phy->dev.parent);
81 	struct mixel_lvds_phy *lvds_phy = phy_get_drvdata(phy);
82 	struct mixel_lvds_phy *companion = priv->phys[lvds_phy->id ^ 1];
83 	struct phy_configure_opts_lvds *cfg = &lvds_phy->cfg;
84 	u32 val = 0;
85 	u32 locked;
86 	int ret;
87 
88 	/* The master PHY would power on the slave PHY. */
89 	if (cfg->is_slave)
90 		return 0;
91 
92 	ret = clk_prepare_enable(priv->phy_ref_clk);
93 	if (ret < 0) {
94 		dev_err(&phy->dev,
95 			"failed to enable PHY reference clock: %d\n", ret);
96 		return ret;
97 	}
98 
99 	mutex_lock(&priv->lock);
100 	if (cfg->bits_per_lane_and_dclk_cycle == 7) {
101 		if (cfg->differential_clk_rate < 44000000)
102 			val |= M(0x2);
103 		else if (cfg->differential_clk_rate < 90000000)
104 			val |= M(0x1);
105 		else
106 			val |= M(0x0);
107 	} else {
108 		val = NB;
109 
110 		if (cfg->differential_clk_rate < 32000000)
111 			val |= M(0x2);
112 		else if (cfg->differential_clk_rate < 63000000)
113 			val |= M(0x1);
114 		else
115 			val |= M(0x0);
116 	}
117 	regmap_update_bits(priv->regmap, PHY_CTRL, M_MASK | NB, val);
118 
119 	/*
120 	 * Enable two channels synchronously,
121 	 * if the companion PHY is a slave PHY.
122 	 */
123 	if (companion->cfg.is_slave)
124 		val = CH_EN(0) | CH_EN(1);
125 	else
126 		val = CH_EN(lvds_phy->id);
127 	regmap_write(priv->regmap, PHY_CTRL + REG_SET, val);
128 
129 	ret = regmap_read_poll_timeout(priv->regmap, PHY_STATUS, locked,
130 				       locked, PLL_LOCK_SLEEP,
131 				       PLL_LOCK_TIMEOUT);
132 	if (ret < 0) {
133 		dev_err(&phy->dev, "failed to get PHY lock: %d\n", ret);
134 		clk_disable_unprepare(priv->phy_ref_clk);
135 	}
136 	mutex_unlock(&priv->lock);
137 
138 	return ret;
139 }
140 
mixel_lvds_phy_power_off(struct phy * phy)141 static int mixel_lvds_phy_power_off(struct phy *phy)
142 {
143 	struct mixel_lvds_phy_priv *priv = dev_get_drvdata(phy->dev.parent);
144 	struct mixel_lvds_phy *lvds_phy = phy_get_drvdata(phy);
145 	struct mixel_lvds_phy *companion = priv->phys[lvds_phy->id ^ 1];
146 	struct phy_configure_opts_lvds *cfg = &lvds_phy->cfg;
147 
148 	/* The master PHY would power off the slave PHY. */
149 	if (cfg->is_slave)
150 		return 0;
151 
152 	mutex_lock(&priv->lock);
153 	if (companion->cfg.is_slave)
154 		regmap_write(priv->regmap, PHY_CTRL + REG_CLR,
155 			     CH_EN(0) | CH_EN(1));
156 	else
157 		regmap_write(priv->regmap, PHY_CTRL + REG_CLR,
158 			     CH_EN(lvds_phy->id));
159 	mutex_unlock(&priv->lock);
160 
161 	clk_disable_unprepare(priv->phy_ref_clk);
162 
163 	return 0;
164 }
165 
mixel_lvds_phy_configure(struct phy * phy,union phy_configure_opts * opts)166 static int mixel_lvds_phy_configure(struct phy *phy,
167 				    union phy_configure_opts *opts)
168 {
169 	struct mixel_lvds_phy_priv *priv = dev_get_drvdata(phy->dev.parent);
170 	struct phy_configure_opts_lvds *cfg = &opts->lvds;
171 	int ret;
172 
173 	ret = clk_set_rate(priv->phy_ref_clk, cfg->differential_clk_rate);
174 	if (ret)
175 		dev_err(&phy->dev, "failed to set PHY reference clock rate(%lu): %d\n",
176 			cfg->differential_clk_rate, ret);
177 
178 	return ret;
179 }
180 
181 /* Assume the master PHY's configuration set is cached first. */
mixel_lvds_phy_check_slave(struct phy * slave_phy)182 static int mixel_lvds_phy_check_slave(struct phy *slave_phy)
183 {
184 	struct device *dev = &slave_phy->dev;
185 	struct mixel_lvds_phy_priv *priv = dev_get_drvdata(dev->parent);
186 	struct mixel_lvds_phy *slv = phy_get_drvdata(slave_phy);
187 	struct mixel_lvds_phy *mst = priv->phys[slv->id ^ 1];
188 	struct phy_configure_opts_lvds *mst_cfg = &mst->cfg;
189 	struct phy_configure_opts_lvds *slv_cfg = &slv->cfg;
190 
191 	if (mst_cfg->bits_per_lane_and_dclk_cycle !=
192 	    slv_cfg->bits_per_lane_and_dclk_cycle) {
193 		dev_err(dev, "number bits mismatch(mst: %u vs slv: %u)\n",
194 			mst_cfg->bits_per_lane_and_dclk_cycle,
195 			slv_cfg->bits_per_lane_and_dclk_cycle);
196 		return -EINVAL;
197 	}
198 
199 	if (mst_cfg->differential_clk_rate !=
200 	    slv_cfg->differential_clk_rate) {
201 		dev_err(dev, "dclk rate mismatch(mst: %lu vs slv: %lu)\n",
202 			mst_cfg->differential_clk_rate,
203 			slv_cfg->differential_clk_rate);
204 		return -EINVAL;
205 	}
206 
207 	if (mst_cfg->lanes != slv_cfg->lanes) {
208 		dev_err(dev, "lanes mismatch(mst: %u vs slv: %u)\n",
209 			mst_cfg->lanes, slv_cfg->lanes);
210 		return -EINVAL;
211 	}
212 
213 	if (mst_cfg->is_slave == slv_cfg->is_slave) {
214 		dev_err(dev, "master PHY is not found\n");
215 		return -EINVAL;
216 	}
217 
218 	return 0;
219 }
220 
mixel_lvds_phy_validate(struct phy * phy,enum phy_mode mode,int submode,union phy_configure_opts * opts)221 static int mixel_lvds_phy_validate(struct phy *phy, enum phy_mode mode,
222 				   int submode, union phy_configure_opts *opts)
223 {
224 	struct mixel_lvds_phy_priv *priv = dev_get_drvdata(phy->dev.parent);
225 	struct mixel_lvds_phy *lvds_phy = phy_get_drvdata(phy);
226 	struct phy_configure_opts_lvds *cfg = &opts->lvds;
227 	int ret = 0;
228 
229 	if (mode != PHY_MODE_LVDS) {
230 		dev_err(&phy->dev, "invalid PHY mode(%d)\n", mode);
231 		return -EINVAL;
232 	}
233 
234 	if (cfg->bits_per_lane_and_dclk_cycle != 7 &&
235 	    cfg->bits_per_lane_and_dclk_cycle != 10) {
236 		dev_err(&phy->dev, "invalid bits per data lane(%u)\n",
237 			cfg->bits_per_lane_and_dclk_cycle);
238 		return -EINVAL;
239 	}
240 
241 	if (cfg->lanes != 4 && cfg->lanes != 3) {
242 		dev_err(&phy->dev, "invalid data lanes(%u)\n", cfg->lanes);
243 		return -EINVAL;
244 	}
245 
246 	if (cfg->differential_clk_rate < MIN_CLKIN_FREQ ||
247 	    cfg->differential_clk_rate > MAX_CLKIN_FREQ) {
248 		dev_err(&phy->dev, "invalid differential clock rate(%lu)\n",
249 			cfg->differential_clk_rate);
250 		return -EINVAL;
251 	}
252 
253 	mutex_lock(&priv->lock);
254 	/* cache configuration set of our own for check */
255 	memcpy(&lvds_phy->cfg, cfg, sizeof(*cfg));
256 
257 	if (cfg->is_slave) {
258 		ret = mixel_lvds_phy_check_slave(phy);
259 		if (ret)
260 			dev_err(&phy->dev, "failed to check slave PHY: %d\n", ret);
261 	}
262 	mutex_unlock(&priv->lock);
263 
264 	return ret;
265 }
266 
267 static const struct phy_ops mixel_lvds_phy_ops = {
268 	.init = mixel_lvds_phy_init,
269 	.power_on = mixel_lvds_phy_power_on,
270 	.power_off = mixel_lvds_phy_power_off,
271 	.configure = mixel_lvds_phy_configure,
272 	.validate = mixel_lvds_phy_validate,
273 	.owner = THIS_MODULE,
274 };
275 
mixel_lvds_phy_reset(struct device * dev)276 static int mixel_lvds_phy_reset(struct device *dev)
277 {
278 	struct mixel_lvds_phy_priv *priv = dev_get_drvdata(dev);
279 	int ret;
280 
281 	ret = pm_runtime_resume_and_get(dev);
282 	if (ret < 0) {
283 		dev_err(dev, "failed to get PM runtime: %d\n", ret);
284 		return ret;
285 	}
286 
287 	regmap_write(priv->regmap, PHY_CTRL, CTRL_RESET_VAL);
288 
289 	pm_runtime_put(dev);
290 
291 	return 0;
292 }
293 
mixel_lvds_phy_xlate(struct device * dev,const struct of_phandle_args * args)294 static struct phy *mixel_lvds_phy_xlate(struct device *dev,
295 					const struct of_phandle_args *args)
296 {
297 	struct mixel_lvds_phy_priv *priv = dev_get_drvdata(dev);
298 	unsigned int phy_id;
299 
300 	if (args->args_count != 1) {
301 		dev_err(dev,
302 			"invalid argument number(%d) for 'phys' property\n",
303 			args->args_count);
304 		return ERR_PTR(-EINVAL);
305 	}
306 
307 	phy_id = args->args[0];
308 
309 	if (phy_id >= PHY_NUM) {
310 		dev_err(dev, "invalid PHY index(%d)\n", phy_id);
311 		return ERR_PTR(-ENODEV);
312 	}
313 
314 	return priv->phys[phy_id]->phy;
315 }
316 
mixel_lvds_phy_probe(struct platform_device * pdev)317 static int mixel_lvds_phy_probe(struct platform_device *pdev)
318 {
319 	struct device *dev = &pdev->dev;
320 	struct phy_provider *phy_provider;
321 	struct mixel_lvds_phy_priv *priv;
322 	struct mixel_lvds_phy *lvds_phy;
323 	struct phy *phy;
324 	int i;
325 	int ret;
326 
327 	if (!dev->of_node)
328 		return -ENODEV;
329 
330 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
331 	if (!priv)
332 		return -ENOMEM;
333 
334 	priv->regmap = syscon_node_to_regmap(dev->of_node->parent);
335 	if (IS_ERR(priv->regmap))
336 		return dev_err_probe(dev, PTR_ERR(priv->regmap),
337 				     "failed to get regmap\n");
338 
339 	priv->phy_ref_clk = devm_clk_get(dev, NULL);
340 	if (IS_ERR(priv->phy_ref_clk))
341 		return dev_err_probe(dev, PTR_ERR(priv->phy_ref_clk),
342 				     "failed to get PHY reference clock\n");
343 
344 	mutex_init(&priv->lock);
345 
346 	dev_set_drvdata(dev, priv);
347 
348 	pm_runtime_enable(dev);
349 
350 	ret = mixel_lvds_phy_reset(dev);
351 	if (ret) {
352 		dev_err(dev, "failed to do POR reset: %d\n", ret);
353 		return ret;
354 	}
355 
356 	for (i = 0; i < PHY_NUM; i++) {
357 		lvds_phy = devm_kzalloc(dev, sizeof(*lvds_phy), GFP_KERNEL);
358 		if (!lvds_phy) {
359 			ret = -ENOMEM;
360 			goto err;
361 		}
362 
363 		phy = devm_phy_create(dev, NULL, &mixel_lvds_phy_ops);
364 		if (IS_ERR(phy)) {
365 			ret = PTR_ERR(phy);
366 			dev_err(dev, "failed to create PHY for channel%d: %d\n",
367 				i, ret);
368 			goto err;
369 		}
370 
371 		lvds_phy->phy = phy;
372 		lvds_phy->id = i;
373 		priv->phys[i] = lvds_phy;
374 
375 		phy_set_drvdata(phy, lvds_phy);
376 	}
377 
378 	phy_provider = devm_of_phy_provider_register(dev, mixel_lvds_phy_xlate);
379 	if (IS_ERR(phy_provider)) {
380 		ret = PTR_ERR(phy_provider);
381 		dev_err(dev, "failed to register PHY provider: %d\n", ret);
382 		goto err;
383 	}
384 
385 	return 0;
386 err:
387 	pm_runtime_disable(dev);
388 
389 	return ret;
390 }
391 
mixel_lvds_phy_remove(struct platform_device * pdev)392 static void mixel_lvds_phy_remove(struct platform_device *pdev)
393 {
394 	pm_runtime_disable(&pdev->dev);
395 }
396 
mixel_lvds_phy_runtime_suspend(struct device * dev)397 static int __maybe_unused mixel_lvds_phy_runtime_suspend(struct device *dev)
398 {
399 	struct mixel_lvds_phy_priv *priv = dev_get_drvdata(dev);
400 
401 	/* power down */
402 	mutex_lock(&priv->lock);
403 	regmap_write(priv->regmap, PHY_CTRL + REG_SET, PD);
404 	mutex_unlock(&priv->lock);
405 
406 	return 0;
407 }
408 
mixel_lvds_phy_runtime_resume(struct device * dev)409 static int __maybe_unused mixel_lvds_phy_runtime_resume(struct device *dev)
410 {
411 	struct mixel_lvds_phy_priv *priv = dev_get_drvdata(dev);
412 
413 	/* power up + control initialization */
414 	mutex_lock(&priv->lock);
415 	regmap_update_bits(priv->regmap, PHY_CTRL,
416 			   CTRL_INIT_MASK | PD, CTRL_INIT_VAL);
417 	mutex_unlock(&priv->lock);
418 
419 	return 0;
420 }
421 
422 static const struct dev_pm_ops mixel_lvds_phy_pm_ops = {
423 	SET_RUNTIME_PM_OPS(mixel_lvds_phy_runtime_suspend,
424 			   mixel_lvds_phy_runtime_resume, NULL)
425 };
426 
427 static const struct of_device_id mixel_lvds_phy_of_match[] = {
428 	{ .compatible = "fsl,imx8qm-lvds-phy" },
429 	{ /* sentinel */ }
430 };
431 MODULE_DEVICE_TABLE(of, mixel_lvds_phy_of_match);
432 
433 static struct platform_driver mixel_lvds_phy_driver = {
434 	.probe = mixel_lvds_phy_probe,
435 	.remove = mixel_lvds_phy_remove,
436 	.driver = {
437 		.pm = &mixel_lvds_phy_pm_ops,
438 		.name = "mixel-lvds-phy",
439 		.of_match_table = mixel_lvds_phy_of_match,
440 	}
441 };
442 module_platform_driver(mixel_lvds_phy_driver);
443 
444 MODULE_DESCRIPTION("Mixel LVDS PHY driver");
445 MODULE_AUTHOR("Liu Ying <victor.liu@nxp.com>");
446 MODULE_LICENSE("GPL");
447