summaryrefslogtreecommitdiff
path: root/drivers/video/bridge/dp501.c
blob: 0ad589304aad5bbada12ada9c2303c1b6ed10264 (plain)
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2024 Jonas Schwöbel <jonasschwoebel@yahoo.de>
 * Copyright (C) 2024 Svyatoslav Ryhel <clamor95@gmail.com>
 */

#include <dm.h>
#include <i2c.h>
#include <log.h>
#include <backlight.h>
#include <panel.h>
#include <video_bridge.h>
#include <linux/delay.h>
#include <linux/err.h>
#include <power/regulator.h>

#include <asm/gpio.h>

/* TOP */
#define TOPCFG0			0x00
#define ROMI2C_PRESCALE		0x01
#define HDCPI2C_PRESCALE	0x02
#define GPIO			0x03
#define GPIO_OUT_ENB		0x04
#define TESTI2C_CTL		0x05
#define I2CMTIMEOUT		0x06
#define TOPCFG1			0x07
#define TOPCFG2			0x08
#define TOPCFG3			0x09
#define TOPCFG4			0x0A
#define CLKSWRST		0x0B
#define CADETB_CTL		0x0C

/* Video Attribute */
#define HTOTAL_L		0x10
#define HTOTAL_H		0x11
#define HSTART_L		0x12
#define HSTART_H		0x13
#define HWIDTH_L		0x14
#define HWIDTH_H		0x15
#define VTOTAL_L		0x16
#define VTOTAL_H		0x17
#define VSTART_L		0x18
#define VSTART_H		0x19
#define VHEIGHT_L		0x1A
#define VHEIGHT_H		0x1B
#define HSPHSW_L		0x1C
#define HSPHSW_H		0x1D
#define VSPVSW_L		0x1E
#define VSPVSW_H		0x1F
#define MISC0			0x20
#define MISC1			0x21

/* Video Capture */
#define VCAPCTRL0		0x24
#define VCAPCTRL1		0x25
#define VCAPCTRL2		0x26
#define VCAPCTRL3		0x27
#define VCAPCTRL4		0x28
#define VCAP_MEASURE		0x29

/* Main Link Control */
#define NVID_L			0x2C
#define NVID_M			0x2D
#define NVID_H			0x2E
#define LINK_CTRL0		0x2F
#define LINK_CTRL1		0x30
#define LINK_DEBUG		0x31
#define ERR_POS			0x32
#define ERR_PAT			0x33
#define LINK_DEB_SEL		0x34
#define IDLE_PATTERN		0x35
#define TU_SIZE			0x36
#define CRC_CTRL		0x37
#define CRC_OUT			0x38

/* AVI-2 InfoFrame */
#define SD_CTRL0		0x3A
#define SD_CTRL1		0x3B
#define SD_HB0			0x3C
#define SD_HB1			0x3D
#define SD_HB2			0x3E
#define SD_HB3			0x3F
#define SD_DB0			0x40
#define SD_DB1			0x41
#define SD_DB2			0x42
#define SD_DB3			0x43
#define SD_DB4			0x44
#define SD_DB5			0x45
#define SD_DB6			0x46
#define SD_DB7			0x47
#define SD_DB8			0x48
#define SD_DB9			0x49
#define SD_DB10			0x4A
#define SD_DB11			0x4B
#define SD_DB12			0x4C
#define SD_DB13			0x4D
#define SD_DB14			0x4E
#define SD_DB15			0x4F

/* Aux Channel and PCS */
#define DPCD_REV		0x50
#define MAX_LINK_RATE		0x51
#define MAX_LANE_COUNT		0x52
#define MAX_DOWNSPREAD		0x53
#define NORP			0x54
#define DOWNSTRMPORT_PRE	0x55
#define MLINK_CH_CODING		0x56
#define RCV_P0_CAP0		0x58
#define RCV_P0_CAP1		0x59
#define RCV_P1_CAP0		0x5A
#define RCV_P1_CAP1		0x5B
#define DOWNSPREAD_CTL		0x5C
#define LINK_BW			0x5D
#define LANE_CNT		0x5E
#define TRAINING_CTL		0x5F
#define QUALTEST_CTL		0x60
#define SINK_COUNT		0x61
#define DEV_SERVICE_IRQ		0x62
#define LANE01_STATUS		0x63
#define LANE23_STATUS		0x64
#define LANE_STATUS_UPDATE	0x65
#define SINK_STATUS		0x66
#define AUX_NOISE		0x67
#define TEST_MODE		0x69
#define TEST_PATTERN0		0x6A
#define TEST_PATTERN1		0x6B
#define TEST_PATTERN2		0x6C
#define SIGNATURE		0x6D
#define PCSCFG			0x6E
#define AUXCTRL0		0x6f
#define AUXCTRL2		0x70
#define AUXCTRL1		0x71
#define HPDCTL0			0x72
#define HPDCTL1			0x73
#define LINK_STATE_CTRL		0x74
#define SWRST			0x75
#define LINK_IRQ		0x76
#define AUXIRQ_CTRL		0x77
#define HPD2_IRQ_CTRL		0x78
#define SW_TRAIN_CTRL		0x79
#define SW_DRV_SET		0x7A
#define SW_PRE_SET		0x7B
#define DPCD_ADDR_L		0x7D
#define DPCD_ADDR_M		0x7E
#define DPCD_ADDR_H		0x7F
#define DPCD_LENGTH		0x80
#define DPCD_WDATA		0x81
#define DPCD_RDATA		0x82
#define DPCD_CTL		0x83
#define DPCD_STATUS		0x84
#define AUX_STATUS		0x85
#define I2CTOAUX_RELENGTH	0x86
#define AUX_RETRY_CTRL		0x87
#define TIMEOUT_CTRL		0x88
#define I2CCMD_OPT1		0x89
#define AUXCMD_ERR_IRQ		0x8A
#define AUXCMD_OPT2		0x8B
#define HDCP_Reserved		0x8C

/* Audio InfoFrame */
#define TX_MVID0		0x90
#define TX_MVID1		0x91
#define TX_MVID2		0x92
#define TX_MVID_OFF		0x93
#define TX_MAUD0		0x94
#define TX_MAUD1		0x95
#define TX_MAUD2		0x96
#define TX_MAUD_OFF		0x97
#define MN_CTRL			0x98
#define MOUT0			0x99
#define MOUT1			0x9A
#define MOUT2			0x9B

/* Audio Control */
#define NAUD_L			0x9F
#define NAUD_M			0xA0
#define NAUD_H			0xA1
#define AUD_CTRL0		0xA2
#define AUD_CTRL1		0xA3
#define LANE_POL		0xAA
#define LANE_EN			0xAB
#define LANE_MAP		0xAC
#define SCR_POLY0		0xAD
#define SCR_POLY1		0xAE
#define PRBS7_POLY		0xAF

/* Video Pre-process */
#define MISC_SHDOW		0xB0
#define VCAPCPCTL0		0xB1
#define VCAPCPCTL1		0xB2
#define VCAPCPCTL2		0xB3
#define CSCPAR			0xB4
#define I2CTODPCDSTATUS2	0xBA
#define AUXCTL_REG		0xBB

/*   Page 2   */
#define SEL_PIO1		0x24
#define SEL_PIO2		0x25
#define SEL_PIO3		0x26
#define CHIP_VER_L		0x82

struct dp501_priv {
	struct udevice *panel;
	struct display_timing timing;

	struct udevice *chip2;

	struct udevice *vdd;
	struct gpio_desc enable_gpio;
};

static int dp501_sw_init(struct udevice *dev)
{
	struct dp501_priv *priv = dev_get_priv(dev);
	int i;
	u8 val;

	dm_i2c_reg_write(dev, TOPCFG4, 0x30);
	udelay(200);
	dm_i2c_reg_write(dev, TOPCFG4, 0x0c);
	dm_i2c_reg_write(dev, 0x8f, 0x02);

	/* check for connected panel during 1 msec */
	for (i = 0; i < 5; i++)	{
		val = dm_i2c_reg_read(dev, 0x8d);
		val &= BIT(2);
		if (val)
			break;

		udelay(200);
	}

	if (!val) {
		log_debug("%s: panel is not connected!\n", __func__);
		return -ENODEV;
	}

	dm_i2c_reg_write(priv->chip2, SEL_PIO1, 0x02);
	dm_i2c_reg_write(priv->chip2, SEL_PIO2, 0x04);
	dm_i2c_reg_write(priv->chip2, SEL_PIO3, 0x10);

	dm_i2c_reg_write(dev, LINK_STATE_CTRL, 0xa0);
	dm_i2c_reg_write(dev, 0x8f, 0x02);
	dm_i2c_reg_write(dev, TOPCFG1, 0x16);
	dm_i2c_reg_write(dev, TOPCFG0, 0x24);
	dm_i2c_reg_write(dev, HPD2_IRQ_CTRL, 0x30);
	dm_i2c_reg_write(dev, AUXIRQ_CTRL, 0xff);
	dm_i2c_reg_write(dev, LINK_IRQ, 0xff);

	/* auto detect DVO timing */
	dm_i2c_reg_write(dev, VCAPCTRL3, 0x30);

	/* reset tpfifo at v blank */
	dm_i2c_reg_write(dev, LINK_CTRL0, 0x82);

	dm_i2c_reg_write(dev, VCAPCTRL4, 0x07);
	dm_i2c_reg_write(dev, AUX_RETRY_CTRL, 0x7f);
	dm_i2c_reg_write(dev, TIMEOUT_CTRL, 0x1e);
	dm_i2c_reg_write(dev, AUXCTL_REG, 0x06);

	/* DPCD readable */
	dm_i2c_reg_write(dev, HPDCTL0, 0xa9);

	/* Scramble on */
	dm_i2c_reg_write(dev, QUALTEST_CTL, 0x00);

	dm_i2c_reg_write(dev, 0x8f, 0x02);

	dm_i2c_reg_write(dev, VCAPCTRL0, 0xc4);

	/* set color depth 8bit (0x00: 6bit; 0x20: 8bit; 0x40: 10bit) */
	dm_i2c_reg_write(dev, MISC0, 0x20);

	dm_i2c_reg_write(dev, VCAPCPCTL2, 0x01);

	/* check if bridge returns ready status */
	for (i = 0; i < 5; i++)	{
		val = dm_i2c_reg_read(dev, LINK_IRQ);
		val &= BIT(0);
		if (val)
			break;

		udelay(200);
	}

	if (!val) {
		log_debug("%s: bridge is not ready\n", __func__);
		return -ENODEV;
	}

	return 0;
}

static void dpcd_configure(struct udevice *dev, u32 config, bool write)
{
	dm_i2c_reg_write(dev, DPCD_ADDR_L, (u8)(config >> 8));
	dm_i2c_reg_write(dev, DPCD_ADDR_M, (u8)(config >> 16));
	dm_i2c_reg_write(dev, DPCD_ADDR_H, (u8)((config >> 24) | BIT(7)));
	dm_i2c_reg_write(dev, DPCD_LENGTH, 0x00);
	dm_i2c_reg_write(dev, LINK_IRQ, 0x20);

	if (write)
		dm_i2c_reg_write(dev, DPCD_WDATA, (u8)(config & 0xff));

	dm_i2c_reg_write(dev, DPCD_CTL, 0x01);

	udelay(10);
}

static int dump_dpcd_data(struct udevice *dev, u32 config, u8 *data)
{
	int i;
	u8 value;

	dpcd_configure(dev, config, false);

	value = dm_i2c_reg_read(dev, DPCD_CTL);
	if (value)
		return -ENODATA;

	for (i = 0; i < 5; i++) {
		value = dm_i2c_reg_read(dev, LINK_IRQ);
		value &= BIT(5);
		if (value)
			break;

		udelay(100);
	}

	if (!value)
		return -ENODATA;

	value = dm_i2c_reg_read(dev, DPCD_STATUS);
	if (!(value & 0xe0))
		*data = dm_i2c_reg_read(dev, DPCD_RDATA);
	else
		return -ENODATA;

	return 0;
}

static int dp501_dpcd_dump(struct udevice *dev, u32 config, u8 *data)
{
	int i, ret;

	for (i = 0; i < 5; i++) {
		ret = dump_dpcd_data(dev, config, data);
		if (!ret)
			break;

		udelay(100);
	}

	return ret;
}

static int dp501_reset_link(struct udevice *dev)
{
	dm_i2c_reg_write(dev, TRAINING_CTL, 0x00);
	dm_i2c_reg_write(dev, SWRST, 0xf8);
	dm_i2c_reg_write(dev, SWRST, 0x00);

	return -ENODEV;
}

static int dp501_link_training(struct udevice *dev)
{
	int i, ret;
	u8 lane, link, link_out;
	u8 lane_cnt, lane01, lane23;

	dpcd_configure(dev, 0x030000, true);
	dpcd_configure(dev, 0x03011c, true);
	dpcd_configure(dev, 0x0301f8, true);

	ret = dp501_dpcd_dump(dev, 0x90000100, &link);
	if (ret) {
		log_debug("%s: link dump failed %d\n", __func__, ret);
		return dp501_reset_link(dev);
	}

	ret = dp501_dpcd_dump(dev, 0x90000200, &lane);
	if (ret) {
		log_debug("%s: lane dump failed %d\n", __func__, ret);
		return dp501_reset_link(dev);
	}

	/* Software trainig */
	for (i = 10; i > 0; i--) {
		dm_i2c_reg_write(dev, LINK_BW, link);
		dm_i2c_reg_write(dev, LANE_CNT, lane | BIT(7));

		link_out = dm_i2c_reg_read(dev, LINK_BW);
		lane_cnt = dm_i2c_reg_read(dev, LANE_CNT);

		if (link_out == link &&
		    (lane_cnt == (lane | BIT(7))))
			break;

		udelay(500);
	}

	if (!i)
		return dp501_reset_link(dev);

	dm_i2c_reg_write(dev, LINK_STATE_CTRL, 0x00);
	dm_i2c_reg_write(dev, TRAINING_CTL, 0x0d);

	/* check if bridge returns link ready status */
	for (i = 0; i < 100; i++) {
		link_out = dm_i2c_reg_read(dev, LINK_IRQ);
		link_out &= BIT(1);
		if (link_out) {
			dm_i2c_reg_write(dev, LINK_IRQ, 0xff);
			break;
		}

		udelay(100);
	}

	if (!link_out) {
		log_debug("%s: link prepare failed %d\n",
			  __func__, link_out);
		return dp501_reset_link(dev);
	}

	lane01 = dm_i2c_reg_read(dev, LANE01_STATUS);
	lane23 = dm_i2c_reg_read(dev, LANE23_STATUS);

	switch (lane_cnt & 0xf) {
	case 4:
		if (lane01 == 0x77 &&
		    lane23 == 0x77)
			return 0;
		break;

	case 2:
		if (lane01 == 0x77)
			return 0;
		break;

	default:
		if ((lane01 & 7) == 7)
			return 0;
		break;
	}

	return dp501_reset_link(dev);
}

static int dp501_attach(struct udevice *dev)
{
	struct dp501_priv *priv = dev_get_priv(dev);
	int ret;

	ret = dp501_sw_init(dev);
	if (ret)
		return ret;

	mdelay(90);

	ret = dp501_link_training(dev);
	if (ret)
		return ret;

	/* Perform panel HW setup */
	return panel_enable_backlight(priv->panel);
}

static int dp501_set_backlight(struct udevice *dev, int percent)
{
	struct dp501_priv *priv = dev_get_priv(dev);

	return panel_set_backlight(priv->panel, percent);
}

static int dp501_panel_timings(struct udevice *dev,
			       struct display_timing *timing)
{
	struct dp501_priv *priv = dev_get_priv(dev);

	memcpy(timing, &priv->timing, sizeof(*timing));
	return 0;
}

static void dp501_hw_init(struct udevice *dev)
{
	struct dp501_priv *priv = dev_get_priv(dev);
	struct video_bridge_priv *uc_priv = dev_get_uclass_priv(dev);

	dm_gpio_set_value(&uc_priv->reset, 1);

	regulator_set_enable_if_allowed(priv->vdd, 1);
	dm_gpio_set_value(&priv->enable_gpio, 1);

	udelay(100);

	dm_gpio_set_value(&uc_priv->reset, 0);
	mdelay(80);
}

static int dp501_setup(struct udevice *dev)
{
	struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
	struct dp501_priv *priv = dev_get_priv(dev);
	struct udevice *bus = dev_get_parent(dev);
	int ret;

	/* get panel */
	ret = uclass_get_device_by_phandle(UCLASS_PANEL, dev,
					   "panel", &priv->panel);
	if (ret) {
		log_debug("%s: Cannot get panel: ret=%d\n", __func__, ret);
		return log_ret(ret);
	}

	/* get regulators */
	ret = device_get_supply_regulator(dev, "power-supply", &priv->vdd);
	if (ret) {
		log_debug("%s: vddc regulator error: %d\n", __func__, ret);
		if (ret != -ENOENT)
			return log_ret(ret);
	}

	/* get gpios */
	ret = gpio_request_by_name(dev, "enable-gpios", 0,
				   &priv->enable_gpio, GPIOD_IS_OUT);
	if (ret) {
		log_debug("%s: Could not decode enable-gpios (%d)\n",
			  __func__, ret);
		return ret;
	}

	ret = i2c_get_chip(bus, chip->chip_addr + 2, 1, &priv->chip2);
	if (ret) {
		log_debug("%s: cannot get second PMIC I2C chip (err %d)\n",
			  __func__, ret);
		return ret;
	}

	dp501_hw_init(dev);

	/* get EDID */
	return panel_get_display_timing(priv->panel, &priv->timing);
}

static int dp501_probe(struct udevice *dev)
{
	if (device_get_uclass_id(dev->parent) != UCLASS_I2C)
		return -EPROTONOSUPPORT;

	return dp501_setup(dev);
}

static const struct video_bridge_ops dp501_ops = {
	.attach			= dp501_attach,
	.set_backlight		= dp501_set_backlight,
	.get_display_timing	= dp501_panel_timings,
};

static const struct udevice_id dp501_ids[] = {
	{ .compatible = "parade,dp501" },
	{ }
};

U_BOOT_DRIVER(dp501) = {
	.name		= "dp501",
	.id		= UCLASS_VIDEO_BRIDGE,
	.of_match	= dp501_ids,
	.ops		= &dp501_ops,
	.probe		= dp501_probe,
	.priv_auto	= sizeof(struct dp501_priv),
};