Blame view

drivers/phy/phy-mixel-mipi-dsi.c 13.5 KB
81f7e3824   Eric Lee   Initial Release, ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
  /*
   * Copyright 2018 NXP
   *
   * This program is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License as published by the
   * Free Software Foundation; either version 2 of the License, or (at your
   * option) any later version.
   *
   * This program is distributed in the hope that it will be useful, but
   * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   * for more details.
   */
  
  #include <linux/delay.h>
  #include <linux/io.h>
  #include <linux/kernel.h>
  #include <linux/module.h>
  #include <linux/of.h>
  #include <linux/of_platform.h>
  #include <linux/phy/phy-mixel-mipi-dsi.h>
  #include <linux/phy/phy.h>
  #include <linux/platform_device.h>
  #include <soc/imx8/sc/sci.h>
  
  #define DPHY_PD_DPHY			0x00
  #define DPHY_M_PRG_HS_PREPARE		0x04
  #define DPHY_MC_PRG_HS_PREPARE		0x08
  #define DPHY_M_PRG_HS_ZERO		0x0c
  #define DPHY_MC_PRG_HS_ZERO		0x10
  #define DPHY_M_PRG_HS_TRAIL		0x14
  #define DPHY_MC_PRG_HS_TRAIL		0x18
  #define DPHY_PD_PLL			0x1c
  #define DPHY_TST			0x20
  #define DPHY_CN				0x24
  #define DPHY_CM				0x28
  #define DPHY_CO				0x2c
  #define DPHY_LOCK			0x30
  #define DPHY_LOCK_BYP			0x34
  
  #define MBPS(x) ((x) * 1000000)
  
  #define DATA_RATE_MAX_SPEED MBPS(1500)
  #define DATA_RATE_MIN_SPEED MBPS(80)
  
  #define CN_BUF	0xcb7a89c0
  #define CO_BUF	0x63
  #define CM(x)	( \
  		((x) <  32)?0xe0|((x)-16) : \
  		((x) <  64)?0xc0|((x)-32) : \
  		((x) < 128)?0x80|((x)-64) : \
  		((x) - 128))
  #define CN(x)	(((x) == 1)?0x1f : (((CN_BUF)>>((x)-1))&0x1f))
  #define CO(x)	((CO_BUF)>>(8-(x))&0x3)
  
  /* PHY power on is LOW_ENABLE */
  #define PWR_ON	0
  #define PWR_OFF	1
  
  struct pll_divider {
  	u32 cm;
  	u32 cn;
  	u32 co;
  };
  
  struct devtype {
  	bool have_sc;
  	u8 reg_tx_rcal;
  	u8 reg_auto_pd_en;
  	u8 reg_rxlprp;
  	u8 reg_rxcdrp;
  	u8 reg_rxhs_settle;
  	u8 reg_bypass_pll;
  };
  
  struct mixel_mipi_phy_priv {
  	struct device	*dev;
  	void __iomem	*base;
  	const struct devtype	*plat_data;
  	sc_rsrc_t	mipi_id;
  	struct pll_divider divider;
  	struct mutex	lock;
  	unsigned long	data_rate;
  };
  
  
  static inline u32 phy_read(struct phy *phy, unsigned int reg)
  {
  	struct mixel_mipi_phy_priv *priv = phy_get_drvdata(phy);
  
  	return readl(priv->base + reg);
  }
  
  static inline void phy_write(struct phy *phy, u32 value, unsigned int reg)
  {
  	struct mixel_mipi_phy_priv *priv = phy_get_drvdata(phy);
  
  	writel(value, priv->base + reg);
  }
  
  /*
   * mixel_phy_mipi_set_phy_speed:
   * Input params:
   *	bit_clk: PHY PLL needed output clock
   *	ref_clk: reference input clock for the PHY PLL
   *
   * Returns:
   *	0: if the bit_clk can be achieved for the given ref_clk
   *	-EINVAL: otherwise
   */
  int mixel_phy_mipi_set_phy_speed(struct phy *phy,
  				 unsigned long bit_clk,
  				 unsigned long ref_clk,
  				 bool best_match)
  {
  	struct mixel_mipi_phy_priv *priv = dev_get_drvdata(phy->dev.parent);
  	u32 div_rate;
  	u32 numerator = 0;
  	u32 denominator = 1;
  
  	if (bit_clk > DATA_RATE_MAX_SPEED || bit_clk < DATA_RATE_MIN_SPEED)
  		return -EINVAL;
  
  	/* simulated fixed point with 3 decimals */
  	div_rate = (bit_clk * 1000) / ref_clk;
  
  	while (denominator <= 256) {
  		if (div_rate % 1000 == 0)
  			numerator = div_rate / 1000;
  		if (numerator > 15)
  			break;
  		denominator = denominator << 1;
  		div_rate = div_rate << 1;
  	}
  
  	/* CM ranges between 16 and 255 */
  	/* CN ranges between 1 and 32 */
  	/* CO is power of 2: 1, 2, 4, 8 */
  	if (best_match && numerator < 16)
  		numerator = div_rate / 1000;
  
  	if (best_match && numerator > 255) {
  		while (numerator > 255 && denominator > 1) {
  			numerator = DIV_ROUND_UP(numerator, 2);
  			denominator = denominator >> 1;
  		}
  	}
  
  	if (numerator < 16 || numerator > 255)
  		return -EINVAL;
  
  	if (best_match)
  		numerator = DIV_ROUND_UP(numerator, denominator) * denominator;
  
  	priv->divider.cn = 1;
  	if (denominator > 8) {
  		priv->divider.cn = denominator >> 3;
  		denominator = 8;
  	}
  	priv->divider.co = denominator;
  	priv->divider.cm = numerator;
  
  	priv->data_rate = bit_clk;
  
  	return 0;
  }
  EXPORT_SYMBOL_GPL(mixel_phy_mipi_set_phy_speed);
  
  static int mixel_mipi_phy_enable(struct phy *phy, u32 reset)
  {
  	struct mixel_mipi_phy_priv *priv = phy_get_drvdata(phy);
  	sc_err_t sci_err = 0;
  	sc_ipc_t ipc_handle = 0;
  	u32 mu_id;
  
  	sci_err = sc_ipc_getMuID(&mu_id);
  	if (sci_err != SC_ERR_NONE) {
  		dev_err(&phy->dev, "Failed to get MU ID (%d)
  ", sci_err);
  		return -ENODEV;
  	}
  	sci_err = sc_ipc_open(&ipc_handle, mu_id);
  	if (sci_err != SC_ERR_NONE) {
  		dev_err(&phy->dev, "Failed to open IPC (%d)
  ", sci_err);
  		return -ENODEV;
  	}
  
  	sci_err = sc_misc_set_control(ipc_handle,
  				      priv->mipi_id,
  				      SC_C_PHY_RESET,
  				      reset);
  	if (sci_err != SC_ERR_NONE) {
  		dev_err(&phy->dev, "Failed to reset DPHY (%d)
  ", sci_err);
  		sc_ipc_close(ipc_handle);
  		return -ENODEV;
  	}
  
  	sc_ipc_close(ipc_handle);
  
  	return 0;
  }
  
  /*
   * We tried our best here to use the values as specified in
   * Reference Manual, but we got unstable results. So, these values
   * are hacked from their original explanation as found in RM.
   */
  static void mixel_phy_set_prg_regs(struct phy *phy)
  {
  	struct mixel_mipi_phy_priv *priv = phy_get_drvdata(phy);
  	unsigned int hs_reg;
  
  	/* MC_PRG_HS_PREPARE = 1.0 * Ttxescape if DPHY_MC_PRG_HS_PREPARE = 0
  	 *
  	 * MC_PRG_HS_PREPARE = 1.5 * Ttxescape if DPHY_MC_PRG_HS_PREPARE = 1
  	 *
  	 * Assume Ftxescape is 18-20 MHz with DPHY_MC_PRG_HS_PREPARE = 0,
  	 * this gives 55-50 ns.
  	 * The specification is 38 to 95 ns.
  	 */
  	phy_write(phy, 0x00, DPHY_MC_PRG_HS_PREPARE);
  
  	/* PRG_HS_PREPARE
  	 * for  PRG_HS_PREPARE = 00, THS-PREPARE = 1   * TxClkEsc Period
  	 *      PRG_HS_PREPARE = 01, THS-PREPARE = 1.5 * TxClkEsc Period
  	 *      PRG_HS_PREPARE = 10, THS-PREPARE = 2   * TxClkEsc Period
  	 *      PRG_HS_PREPARE = 11, THS-PREPARE = 2.5 * TxClkEsc Period
  	 *
  	 *      The specification for THS-PREPARE is
  	 *	     Min (40ns + 4*UI)
  	 *           Max 85ns +6*UI
  	 */
  	if (priv->data_rate <= MBPS(61))
  		phy_write(phy, 0x03, DPHY_M_PRG_HS_PREPARE);
  	else if (priv->data_rate <= MBPS(90))
  		phy_write(phy, 0x02, DPHY_M_PRG_HS_PREPARE);
  	else if (priv->data_rate <= MBPS(500))
  		phy_write(phy, 0x01, DPHY_M_PRG_HS_PREPARE);
  	else
  		phy_write(phy, 0x00, DPHY_M_PRG_HS_PREPARE);
  
  	/* MC_PRG_HS_ZERO
  	 *
  	 *  T-CLK-ZERO = ( MC_PRG_HS_ZERO + 3) * (TxByteClkHS Period)
  	 *
  	 *  The minimum specification for THS-PREPARE is 262 ns.
  	 *
  	 */
  	hs_reg =
  		/* simplified equation y = .034x - 2.5
  		 *
  		 * This a linear interpolation of the values from the
  		 * PHY user guide
  		 */
  		(34 * (priv->data_rate/1000000) - 2500) / 1000;
  
  	if (hs_reg < 1)
  		hs_reg = 1;
  	phy_write(phy, hs_reg, DPHY_MC_PRG_HS_ZERO);
  
  	/* M_PRG_HS_ZERO
  	 *
  	 *  TT-HS-ZERO =(M_PRG_HS_ZERO + 6) * (TxByteClkHS Period)
  	 *
  	 *  The minimum specification for THS-ZERO 105ns + 6*UI.
  	 *
  	 */
  	hs_reg =
  		/* simplified equation y = .0144x - 4.75
  		 *
  		 * This a linear interpolation of the values from the
  		 * PHY user guide
  		 */
  
  		(144 * (priv->data_rate/1000000) - 47500) / 10000;
  
  	if (hs_reg < 1)
  		hs_reg = 1;
  	phy_write(phy, hs_reg, DPHY_M_PRG_HS_ZERO);
  
  	/* MC_PRG_HS_TRAIL and M_PRG_HS_TRAIL
  	 *
  	 *  THS-TRAIL =(PRG_HS_TRAIL) * (TxByteClkHS Period)
  	 *
  	 *  The specification for THS-TRAIL is
  	 *	     Min     (60ns   + 4*UI)
  	 *           Typical (82.5ns + 8*UI)
  	 *           Max     (105ns  + 12*UI)
  	 *
  	 */
  
  	hs_reg =
  		/* simplified equation y = .0103x + 1
  		 *
  		 * This a linear interpolation of the values from the
  		 * PHY user guide
  		 */
  		(103 * (priv->data_rate/1000000) + 10000) / 10000;
  
  	if (hs_reg > 15)
  		hs_reg = 15;
  	if (hs_reg < 1)
  		hs_reg = 1;
  
  	phy_write(phy, hs_reg, DPHY_MC_PRG_HS_TRAIL);
  	phy_write(phy, hs_reg, DPHY_M_PRG_HS_TRAIL);
  
  	/* M_PRG_RXHS_SETTLE */
  	if (priv->plat_data->reg_rxhs_settle == 0xFF)
  		return;
  	if (priv->data_rate < MBPS(80))
  		phy_write(phy, 0x0d, priv->plat_data->reg_rxhs_settle);
  	else if (priv->data_rate < MBPS(90))
  		phy_write(phy, 0x0c, priv->plat_data->reg_rxhs_settle);
  	else if (priv->data_rate < MBPS(125))
  		phy_write(phy, 0x0b, priv->plat_data->reg_rxhs_settle);
  	else if (priv->data_rate < MBPS(150))
  		phy_write(phy, 0x0a, priv->plat_data->reg_rxhs_settle);
  	else if (priv->data_rate < MBPS(225))
  		phy_write(phy, 0x09, priv->plat_data->reg_rxhs_settle);
  	else if (priv->data_rate < MBPS(500))
  		phy_write(phy, 0x08, priv->plat_data->reg_rxhs_settle);
  	else
  		phy_write(phy, 0x07, priv->plat_data->reg_rxhs_settle);
  
  }
  
  static int mixel_mipi_phy_init(struct phy *phy)
  {
  	struct mixel_mipi_phy_priv *priv = dev_get_drvdata(phy->dev.parent);
  
  	mutex_lock(&priv->lock);
  
  	phy_write(phy, PWR_OFF, DPHY_PD_PLL);
  	phy_write(phy, PWR_OFF, DPHY_PD_DPHY);
  
  	if (priv->plat_data->have_sc) {
  		int ret;
  		ret = mixel_mipi_phy_enable(phy, 0);
  		if (ret)
  			return ret;
  	}
  
  	mixel_phy_set_prg_regs(phy);
  
  	phy_write(phy, 0x00, DPHY_LOCK_BYP);
  	if (priv->plat_data->reg_tx_rcal != 0xFF)
  		phy_write(phy, 0x01, priv->plat_data->reg_tx_rcal);
  	if (priv->plat_data->reg_auto_pd_en != 0xFF)
  		phy_write(phy, 0x00, priv->plat_data->reg_auto_pd_en);
  	if (priv->plat_data->reg_rxlprp != 0xFF)
  		phy_write(phy, 0x02, priv->plat_data->reg_rxlprp);
  	if (priv->plat_data->reg_rxcdrp != 0xFF)
  		phy_write(phy, 0x02, priv->plat_data->reg_rxcdrp);
  	phy_write(phy, 0x25, DPHY_TST);
  
  	/* VCO = REF_CLK * CM / CN * CO */
  	if (priv->divider.cm < 16 || priv->divider.cm > 255 ||
  		priv->divider.cn < 1 || priv->divider.cn > 32 ||
  		priv->divider.co < 1 || priv->divider.co > 8) {
  		dev_err(&phy->dev, "Invalid CM/CN/CO values! (%u/%u/%u)
  ",
  			priv->divider.cm, priv->divider.cn, priv->divider.co);
  		mutex_unlock(&priv->lock);
  		return -EINVAL;
  	}
  	dev_dbg(&phy->dev, "Using CM:%u CN:%u CO:%u
  ",
  		 priv->divider.cm, priv->divider.cn, priv->divider.co);
  	phy_write(phy, CM(priv->divider.cm), DPHY_CM);
  	phy_write(phy, CN(priv->divider.cn), DPHY_CN);
  	phy_write(phy, CO(priv->divider.co), DPHY_CO);
  
  	mutex_unlock(&priv->lock);
  
  	return 0;
  }
  
  static int mixel_mipi_phy_exit(struct phy *phy)
  {
  	phy_write(phy, 0, DPHY_CM);
  	phy_write(phy, 0, DPHY_CN);
  	phy_write(phy, 0, DPHY_CO);
  
  	return 0;
  }
  
  static int mixel_mipi_phy_power_on(struct phy *phy)
  {
  	struct mixel_mipi_phy_priv *priv = phy_get_drvdata(phy);
  	u32 lock, timeout;
  	int ret = 0;
  
  	mutex_lock(&priv->lock);
  
  	phy_write(phy, PWR_ON, DPHY_PD_PLL);
  
  	timeout = 100;
  	while (!(lock = phy_read(phy, DPHY_LOCK))) {
  		udelay(10);
  		if (--timeout == 0) {
  			dev_err(&phy->dev, "Could not get DPHY lock!
  ");
  			phy_write(phy, PWR_OFF, DPHY_PD_PLL);
  			mutex_unlock(&priv->lock);
  			return -EINVAL;
  		}
  	}
  	dev_dbg(&phy->dev, "DPHY lock acquired after %d tries
  ",
  		(100 - timeout));
  
  	phy_write(phy, PWR_ON, DPHY_PD_DPHY);
  
  	if (priv->plat_data->have_sc)
  		ret = mixel_mipi_phy_enable(phy, 1);
  
  	mutex_unlock(&priv->lock);
  
  	return ret;
  }
  
  static int mixel_mipi_phy_power_off(struct phy *phy)
  {
  	struct mixel_mipi_phy_priv *priv = phy_get_drvdata(phy);
  	int ret = 0;
  
  	mutex_lock(&priv->lock);
  
  	phy_write(phy, PWR_OFF, DPHY_PD_PLL);
  	phy_write(phy, PWR_OFF, DPHY_PD_DPHY);
  
  	if (priv->plat_data->have_sc)
  		ret = mixel_mipi_phy_enable(phy, 0);
  
  	mutex_unlock(&priv->lock);
  
  	return ret;
  }
  
  static const struct phy_ops mixel_mipi_phy_ops = {
  	.init = mixel_mipi_phy_init,
  	.exit = mixel_mipi_phy_exit,
  	.power_on = mixel_mipi_phy_power_on,
  	.power_off = mixel_mipi_phy_power_off,
  	.owner = THIS_MODULE,
  };
  
  static struct devtype imx8qm_dev = {
  	.have_sc = true,
  	.reg_tx_rcal = 0xFF,
  	.reg_auto_pd_en = 0x38,
  	.reg_rxlprp = 0x3c,
  	.reg_rxcdrp = 0x40,
  	.reg_rxhs_settle = 0x44,
  	.reg_bypass_pll = 0xFF,
  };
  static struct devtype imx8qxp_dev = {
  	.have_sc = true,
  	.reg_tx_rcal = 0xFF,
  	.reg_auto_pd_en = 0x38,
  	.reg_rxlprp = 0x3c,
  	.reg_rxcdrp = 0x40,
  	.reg_rxhs_settle = 0x44,
  	.reg_bypass_pll = 0xFF,
  };
  static struct devtype imx8mq_dev = {
  	.have_sc = false,
  	.reg_tx_rcal = 0x38,
  	.reg_auto_pd_en = 0x3c,
  	.reg_rxlprp = 0x40,
  	.reg_rxcdrp = 0x44,
  	.reg_rxhs_settle = 0x48,
  	.reg_bypass_pll = 0x4c,
  };
  
  static const struct of_device_id mixel_mipi_phy_of_match[] = {
  	{ .compatible = "mixel,imx8qm-mipi-dsi-phy", .data = &imx8qm_dev },
  	{ .compatible = "mixel,imx8qxp-mipi-dsi-phy", .data = &imx8qxp_dev },
  	{ .compatible = "mixel,imx8mq-mipi-dsi-phy", .data = &imx8mq_dev },
  	{}
  };
  MODULE_DEVICE_TABLE(of, mixel_mipi_phy_of_match);
  
  static int mixel_mipi_phy_probe(struct platform_device *pdev)
  {
  	struct device *dev = &pdev->dev;
  	struct device_node *np = dev->of_node;
  	const struct of_device_id *of_id =
  		of_match_device(mixel_mipi_phy_of_match, dev);
  	struct phy_provider *phy_provider;
  	struct mixel_mipi_phy_priv *priv;
  	struct resource *res;
  	struct phy *phy;
  	int phy_id = 0;
  
  	if (!np)
  		return -ENODEV;
  
  	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  	if (!priv)
  		return -ENOMEM;
  
  	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  	if (!res)
  		return -ENODEV;
  
  	priv->base = devm_ioremap(dev, res->start, SZ_256);
  	if (IS_ERR(priv->base))
  		return PTR_ERR(priv->base);
  
  	priv->plat_data = of_id->data;
  
  	phy_id = of_alias_get_id(np, "dsi_phy");
  	if (phy_id < 0) {
  		dev_err(dev, "No dsi_phy alias found!");
  		return phy_id;
  	}
  
  	priv->mipi_id = phy_id?SC_R_MIPI_1:SC_R_MIPI_0;
  
  	priv->dev = dev;
  
  	mutex_init(&priv->lock);
  	dev_set_drvdata(dev, priv);
  
  	phy = devm_phy_create(dev, np, &mixel_mipi_phy_ops);
  	if (IS_ERR(phy)) {
  		dev_err(dev, "Failed to create phy
  ");
  		return PTR_ERR(phy);
  	}
  	phy_set_drvdata(phy, priv);
  
  	phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  
  	return PTR_ERR_OR_ZERO(phy_provider);
  }
  
  static struct platform_driver mixel_mipi_phy_driver = {
  	.probe	= mixel_mipi_phy_probe,
  	.driver = {
  		.name = "mixel-mipi-dsi-phy",
  		.of_match_table	= mixel_mipi_phy_of_match,
  	}
  };
  module_platform_driver(mixel_mipi_phy_driver);
  
  MODULE_AUTHOR("NXP Semiconductor");
  MODULE_DESCRIPTION("Mixel MIPI-DSI PHY driver");
  MODULE_LICENSE("GPL v2");