xref: /linux/drivers/gpu/drm/display/drm_scdc_helper.c (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
1 /*
2  * Copyright (c) 2015 NVIDIA Corporation. All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sub license,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the
12  * next paragraph) shall be included in all copies or substantial portions
13  * of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include <linux/bitfield.h>
25 #include <linux/debugfs.h>
26 #include <linux/delay.h>
27 #include <linux/export.h>
28 #include <linux/i2c.h>
29 #include <linux/minmax.h>
30 #include <linux/overflow.h>
31 #include <linux/slab.h>
32 
33 #include <drm/display/drm_scdc_helper.h>
34 #include <drm/drm_connector.h>
35 #include <drm/drm_device.h>
36 #include <drm/drm_print.h>
37 
38 /**
39  * DOC: scdc helpers
40  *
41  * Status and Control Data Channel (SCDC) is a mechanism introduced by the
42  * HDMI 2.0 specification. It is a point-to-point protocol that allows the
43  * HDMI source and HDMI sink to exchange data. The same I2C interface that
44  * is used to access EDID serves as the transport mechanism for SCDC.
45  *
46  * Note: The SCDC status is going to be lost when the display is
47  * disconnected. This can happen physically when the user disconnects
48  * the cable, but also when a display is switched on (such as waking up
49  * a TV).
50  *
51  * This is further complicated by the fact that, upon a disconnection /
52  * reconnection, KMS won't change the mode on its own. This means that
53  * one can't just rely on setting the SCDC status on enable, but also
54  * has to track the connector status changes using interrupts and
55  * restore the SCDC status. The typical solution for this is to trigger an
56  * empty modeset in drm_connector_helper_funcs.detect_ctx(), like what vc4 does
57  * in vc4_hdmi_reset_link().
58  */
59 
60 #define SCDC_I2C_SLAVE_ADDRESS 0x54
61 
62 #define drm_scdc_dbg(connector, fmt, ...)					\
63 	drm_dbg_kms((connector)->dev, "[CONNECTOR:%d:%s] " fmt,			\
64 		    (connector)->base.id, (connector)->name, ##__VA_ARGS__)
65 
66 static const char *drm_scdc_frl_rate_str(enum drm_scdc_frl_rate rate)
67 {
68 	switch (rate) {
69 	case SCDC_FRL_RATE_OFF:
70 		return "Off";
71 	case SCDC_FRL_RATE_3X3:
72 		return "3 Gbit/s x 3 lanes";
73 	case SCDC_FRL_RATE_6X3:
74 		return "6 Gbit/s x 3 lanes";
75 	case SCDC_FRL_RATE_6X4:
76 		return "6 Gbit/s x 4 lanes";
77 	case SCDC_FRL_RATE_8X4:
78 		return "8 Gbit/s x 4 lanes";
79 	case SCDC_FRL_RATE_10X4:
80 		return "10 Gbit/s x 4 lanes";
81 	case SCDC_FRL_RATE_12X4:
82 		return "12 Gbit/s x 4 lanes";
83 	case SCDC_FRL_RATE_RESV_7:
84 	case SCDC_FRL_RATE_RESV_8:
85 	case SCDC_FRL_RATE_RESV_9:
86 	case SCDC_FRL_RATE_RESV_10:
87 	case SCDC_FRL_RATE_RESV_11:
88 	case SCDC_FRL_RATE_RESV_12:
89 	case SCDC_FRL_RATE_RESV_13:
90 	case SCDC_FRL_RATE_RESV_14:
91 	case SCDC_FRL_RATE_RESV_15:
92 		return "(Reserved)";
93 	default:
94 		return NULL;
95 	}
96 }
97 
98 /**
99  * drm_scdc_read - read a block of data from SCDC
100  * @adapter: I2C controller
101  * @offset: start offset of block to read
102  * @buffer: return location for the block to read
103  * @size: size of the block to read
104  *
105  * Reads a block of data from SCDC, starting at a given offset.
106  *
107  * Returns:
108  * 0 on success, negative error code on failure.
109  */
110 int drm_scdc_read(struct i2c_adapter *adapter, u8 offset, void *buffer,
111 		  size_t size)
112 {
113 	int ret;
114 	struct i2c_msg msgs[2] = {
115 		{
116 			.addr = SCDC_I2C_SLAVE_ADDRESS,
117 			.flags = 0,
118 			.len = 1,
119 			.buf = &offset,
120 		}, {
121 			.addr = SCDC_I2C_SLAVE_ADDRESS,
122 			.flags = I2C_M_RD,
123 			.len = size,
124 			.buf = buffer,
125 		}
126 	};
127 
128 	ret = i2c_transfer(adapter, msgs, ARRAY_SIZE(msgs));
129 	if (ret < 0)
130 		return ret;
131 	if (ret != ARRAY_SIZE(msgs))
132 		return -EPROTO;
133 
134 	return 0;
135 }
136 EXPORT_SYMBOL(drm_scdc_read);
137 
138 /**
139  * drm_scdc_write - write a block of data to SCDC
140  * @adapter: I2C controller
141  * @offset: start offset of block to write
142  * @buffer: block of data to write
143  * @size: size of the block to write
144  *
145  * Writes a block of data to SCDC, starting at a given offset.
146  *
147  * Returns:
148  * 0 on success, negative error code on failure.
149  */
150 int drm_scdc_write(struct i2c_adapter *adapter, u8 offset, const void *buffer,
151 		   size_t size)
152 {
153 	struct i2c_msg msg = {
154 		.addr = SCDC_I2C_SLAVE_ADDRESS,
155 		.flags = 0,
156 		.len = 1 + size,
157 		.buf = NULL,
158 	};
159 	void *data;
160 	int err;
161 
162 	data = kmalloc(1 + size, GFP_KERNEL);
163 	if (!data)
164 		return -ENOMEM;
165 
166 	msg.buf = data;
167 
168 	memcpy(data, &offset, sizeof(offset));
169 	memcpy(data + 1, buffer, size);
170 
171 	err = i2c_transfer(adapter, &msg, 1);
172 
173 	kfree(data);
174 
175 	if (err < 0)
176 		return err;
177 	if (err != 1)
178 		return -EPROTO;
179 
180 	return 0;
181 }
182 EXPORT_SYMBOL(drm_scdc_write);
183 
184 /**
185  * drm_scdc_get_scrambling_status - what is status of scrambling?
186  * @connector: connector
187  *
188  * Reads the scrambler status over SCDC, and checks the
189  * scrambling status.
190  *
191  * Returns:
192  * True if the scrambling is enabled, false otherwise.
193  */
194 bool drm_scdc_get_scrambling_status(struct drm_connector *connector)
195 {
196 	u8 status;
197 	int ret;
198 
199 	ret = drm_scdc_readb(connector->ddc, SCDC_SCRAMBLER_STATUS, &status);
200 	if (ret < 0) {
201 		drm_scdc_dbg(connector, "Failed to read scrambling status: %d\n", ret);
202 		return false;
203 	}
204 
205 	return status & SCDC_SCRAMBLING_STATUS;
206 }
207 EXPORT_SYMBOL(drm_scdc_get_scrambling_status);
208 
209 /**
210  * drm_scdc_set_scrambling - enable scrambling
211  * @connector: connector
212  * @enable: bool to indicate if scrambling is to be enabled/disabled
213  *
214  * Writes the TMDS config register over SCDC channel, and:
215  * enables scrambling when enable = 1
216  * disables scrambling when enable = 0
217  *
218  * Returns:
219  * True if scrambling is set/reset successfully, false otherwise.
220  */
221 bool drm_scdc_set_scrambling(struct drm_connector *connector,
222 			     bool enable)
223 {
224 	u8 config;
225 	int ret;
226 
227 	ret = drm_scdc_readb(connector->ddc, SCDC_TMDS_CONFIG, &config);
228 	if (ret < 0) {
229 		drm_scdc_dbg(connector, "Failed to read TMDS config: %d\n", ret);
230 		return false;
231 	}
232 
233 	if (enable)
234 		config |= SCDC_SCRAMBLING_ENABLE;
235 	else
236 		config &= ~SCDC_SCRAMBLING_ENABLE;
237 
238 	ret = drm_scdc_writeb(connector->ddc, SCDC_TMDS_CONFIG, config);
239 	if (ret < 0) {
240 		drm_scdc_dbg(connector, "Failed to enable scrambling: %d\n", ret);
241 		return false;
242 	}
243 
244 	return true;
245 }
246 EXPORT_SYMBOL(drm_scdc_set_scrambling);
247 
248 /**
249  * drm_scdc_set_high_tmds_clock_ratio - set TMDS clock ratio
250  * @connector: connector
251  * @set: ret or reset the high clock ratio
252  *
253  *
254  *	TMDS clock ratio calculations go like this:
255  *		TMDS character = 10 bit TMDS encoded value
256  *
257  *		TMDS character rate = The rate at which TMDS characters are
258  *		transmitted (Mcsc)
259  *
260  *		TMDS bit rate = 10x TMDS character rate
261  *
262  *	As per the spec:
263  *		TMDS clock rate for pixel clock < 340 MHz = 1x the character
264  *		rate = 1/10 pixel clock rate
265  *
266  *		TMDS clock rate for pixel clock > 340 MHz = 0.25x the character
267  *		rate = 1/40 pixel clock rate
268  *
269  *	Writes to the TMDS config register over SCDC channel, and:
270  *		sets TMDS clock ratio to 1/40 when set = 1
271  *
272  *		sets TMDS clock ratio to 1/10 when set = 0
273  *
274  * Returns:
275  * True if write is successful, false otherwise.
276  */
277 bool drm_scdc_set_high_tmds_clock_ratio(struct drm_connector *connector,
278 					bool set)
279 {
280 	u8 config;
281 	int ret;
282 
283 	ret = drm_scdc_readb(connector->ddc, SCDC_TMDS_CONFIG, &config);
284 	if (ret < 0) {
285 		drm_scdc_dbg(connector, "Failed to read TMDS config: %d\n", ret);
286 		return false;
287 	}
288 
289 	if (set)
290 		config |= SCDC_TMDS_BIT_CLOCK_RATIO_BY_40;
291 	else
292 		config &= ~SCDC_TMDS_BIT_CLOCK_RATIO_BY_40;
293 
294 	ret = drm_scdc_writeb(connector->ddc, SCDC_TMDS_CONFIG, config);
295 	if (ret < 0) {
296 		drm_scdc_dbg(connector, "Failed to set TMDS clock ratio: %d\n", ret);
297 		return false;
298 	}
299 
300 	/*
301 	 * The spec says that a source should wait minimum 1ms and maximum
302 	 * 100ms after writing the TMDS config for clock ratio. Lets allow a
303 	 * wait of up to 2ms here.
304 	 */
305 	usleep_range(1000, 2000);
306 	return true;
307 }
308 EXPORT_SYMBOL(drm_scdc_set_high_tmds_clock_ratio);
309 
310 /**
311  * drm_scdc_set_source_version - set SCDC source version on the sink
312  * @connector: connector
313  * @ver: source version to advertise (per spec, 1)
314  *
315  * Reads the sink version for diagnostics and as a guard, then writes
316  * the source version unless the sink reports version 0.
317  *
318  * Returns:
319  * 0 on success or when skipped; a negative error code when either
320  * the read or the write failed.
321  */
322 int drm_scdc_set_source_version(struct drm_connector *connector, u8 ver)
323 {
324 	u8 sink_ver;
325 	int ret;
326 
327 	ret = drm_scdc_readb(connector->ddc, SCDC_SINK_VERSION, &sink_ver);
328 	if (ret) {
329 		drm_scdc_dbg(connector, "Failed to read SCDC_SINK_VERSION: %d\n", ret);
330 		return ret;
331 	}
332 
333 	drm_scdc_dbg(connector, "Sink reported SCDC ver. %u\n", sink_ver);
334 
335 	/*
336 	 * Only advertise our source version to sinks that report a
337 	 * non-zero sink version.  A sink reporting version 0 is either
338 	 * not SCDC-version-aware or non-conformant; writing the source
339 	 * version gains nothing and may upset broken hardware.
340 	 */
341 	if (sink_ver) {
342 		ret = drm_scdc_writeb(connector->ddc, SCDC_SOURCE_VERSION,
343 				      min_t(u8, sink_ver, ver));
344 		if (ret)
345 			drm_scdc_dbg(connector,
346 				     "Failed to write SCDC_SOURCE_VERSION: %d\n", ret);
347 	}
348 
349 	return ret;
350 }
351 EXPORT_SYMBOL(drm_scdc_set_source_version);
352 
353 static void
354 drm_scdc_parse_status0_flags(u8 val, struct drm_scdc_status_flags *flags)
355 {
356 	flags->clock_detected = val & SCDC_CLOCK_DETECT;
357 	flags->ch0_locked = val & SCDC_CH0_LOCK;
358 	flags->ch1_locked = val & SCDC_CH1_LOCK;
359 	flags->ch2_locked = val & SCDC_CH2_LOCK;
360 	flags->ln3_locked = val & SCDC_LN3_LOCK;
361 	flags->flt_ready = val & SCDC_FLT_READY;
362 	flags->dsc_fail = val & SCDC_DSC_FAIL;
363 }
364 
365 static void
366 drm_scdc_parse_status1_2_flags(u8 val_flag1, u8 val_flag2,
367 			       struct drm_scdc_status_flags *flags)
368 {
369 	flags->ln0_training_pattern = FIELD_GET(SCDC_LN_EVEN_TRAIN_PTRN, val_flag1);
370 	flags->ln1_training_pattern = FIELD_GET(SCDC_LN_ODD_TRAIN_PTRN, val_flag1);
371 
372 	flags->ln2_training_pattern = FIELD_GET(SCDC_LN_EVEN_TRAIN_PTRN, val_flag2);
373 	flags->ln3_training_pattern = FIELD_GET(SCDC_LN_ODD_TRAIN_PTRN, val_flag2);
374 }
375 
376 static int drm_scdc_parse_error_counters(const u8 scdc[256], u16 counter[4],
377 					 unsigned int num_lanes)
378 {
379 	u8 end_reg;
380 	u8 sum = 0;
381 	int i;
382 
383 	switch (num_lanes) {
384 	case 3:
385 		end_reg = SCDC_ERR_DET_CHECKSUM;
386 		break;
387 	case 4:
388 		end_reg = SCDC_ERR_DET_3_H;
389 		break;
390 	default:
391 		return -EINVAL;
392 	}
393 
394 	for (i = SCDC_ERR_DET_0_L; i <= end_reg; i++)
395 		sum = wrapping_add(u8, sum, scdc[i]);
396 
397 	if (sum)
398 		return -EPROTO;
399 
400 	for (i = 0; i < 3; i++) {
401 		if (scdc[SCDC_ERR_DET_0_H + i * 2] & SCDC_CHANNEL_VALID)
402 			counter[i] =  (scdc[SCDC_ERR_DET_0_H + i * 2] &
403 				       ~SCDC_CHANNEL_VALID) << 8 |
404 				       scdc[SCDC_ERR_DET_0_L + i * 2];
405 		else
406 			counter[i] = 0;
407 	}
408 
409 	if (num_lanes == 4 && scdc[SCDC_ERR_DET_3_H] & SCDC_CHANNEL_VALID)
410 		counter[3] =  (scdc[SCDC_ERR_DET_3_H] & ~SCDC_CHANNEL_VALID) << 8 |
411 			      scdc[SCDC_ERR_DET_3_L];
412 	else
413 		counter[3] = 0;
414 
415 	return 0;
416 }
417 
418 /**
419  * drm_scdc_read_state - Update state from SCDC
420  * @connector: pointer to a &struct drm_connector on which to operate on
421  * @state: pointer to a &struct drm_scdc_state to fill
422  *
423  * Reads the entire 256 byte SCDC state and parses it.
424  *
425  * Returns: %0 on success, negative errno on failure.
426  */
427 int drm_scdc_read_state(struct drm_connector *connector, struct drm_scdc_state *state)
428 {
429 	struct i2c_adapter *ddc;
430 	struct drm_scdc *scdc;
431 	u8 *buf = state->scdc;
432 	int num_lanes;
433 	int ret;
434 
435 	if (!state || !connector)
436 		return -ENODEV;
437 
438 	scdc = &connector->display_info.hdmi.scdc;
439 	ddc = connector->ddc;
440 
441 	if (!scdc->supported || !ddc)
442 		return -EOPNOTSUPP;
443 
444 	/* Read in 128-byte chunks, to work around DP<->HDMI converters with issues. */
445 	ret = drm_scdc_read(ddc, 0, buf, 128);
446 	if (ret)
447 		return ret;
448 
449 	ret = drm_scdc_read(ddc, 128, &buf[128], 128);
450 	if (ret)
451 		return ret;
452 
453 	state->scrambling_enabled = buf[SCDC_TMDS_CONFIG] & SCDC_SCRAMBLING_ENABLE;
454 	state->tmds_bclk_x40 = buf[SCDC_TMDS_CONFIG] & SCDC_TMDS_BIT_CLOCK_RATIO_BY_40;
455 
456 	state->scrambling_detected = buf[SCDC_SCRAMBLER_STATUS] & SCDC_SCRAMBLING_STATUS;
457 
458 	state->rate = FIELD_GET(SCDC_FRL_RATE, buf[SCDC_CONFIG_1]);
459 	num_lanes = drm_scdc_num_frl_lanes(state->rate);
460 	if (num_lanes < 0)
461 		return num_lanes;
462 	if (!num_lanes)
463 		num_lanes = 3;
464 
465 	state->ffe_levels = FIELD_GET(SCDC_FFE_LEVELS, buf[SCDC_CONFIG_1]);
466 
467 	drm_scdc_parse_status0_flags(buf[SCDC_STATUS_FLAGS_0], &state->stf);
468 	drm_scdc_parse_status1_2_flags(buf[SCDC_STATUS_FLAGS_1],
469 				       buf[SCDC_STATUS_FLAGS_2], &state->stf);
470 	ret = drm_scdc_parse_error_counters(buf, state->error_count, num_lanes);
471 	if (ret)
472 		return ret;
473 
474 	if (state->rate && (buf[SCDC_ERR_DET_RS_H] & SCDC_CHANNEL_VALID))
475 		state->rs_corrections = (buf[SCDC_ERR_DET_RS_H] & ~SCDC_CHANNEL_VALID) << 8 |
476 					buf[SCDC_ERR_DET_RS_L];
477 
478 	return 0;
479 }
480 EXPORT_SYMBOL(drm_scdc_read_state);
481 
482 #define scdc_print_str(_f, key, s) \
483 	(seq_printf((_f), "%-30s: %s\n", (key), (s)))
484 #define scdc_print_flag(_f, key, val) \
485 	(scdc_print_str((_f), (key), str_yes_no((val))))
486 #define scdc_print_dec(_f, key, val) \
487 	(seq_printf((_f), "%-30s: %d\n", (key), (val)))
488 
489 static int scdc_status_show(struct seq_file *m, void *data)
490 {
491 	struct drm_connector *connector = m->private;
492 	struct drm_scdc *scdc = &connector->display_info.hdmi.scdc;
493 	struct drm_scdc_state *st;
494 	int i, ret, frl_lanes;
495 
496 	drm_connector_get(connector);
497 
498 	ret = mutex_lock_interruptible(&connector->dev->mode_config.mutex);
499 	if (ret)
500 		goto err_conn_put;
501 
502 	if (connector->status != connector_status_connected) {
503 		ret = -ENODEV;
504 		goto err_unlock;
505 	}
506 
507 	if (scdc->supported) {
508 		st = kzalloc_obj(*st);
509 		if (!st) {
510 			ret = -ENOMEM;
511 			goto err_unlock;
512 		}
513 		ret = drm_scdc_read_state(connector, st);
514 		if (ret)
515 			goto err_free_state;
516 
517 		for (i = 0; i < ARRAY_SIZE(st->scdc); i += 16)
518 			seq_printf(m, "%*ph\n", 16, &st->scdc[i]);
519 
520 		seq_puts(m, "\n----------------\n\n");
521 	}
522 
523 	scdc_print_flag(m, "SCDC Supported", scdc->supported);
524 	if (!scdc->supported) {
525 		ret = 0;
526 		goto err_unlock;
527 	}
528 
529 	scdc_print_flag(m, "Sink Read Request Capable", scdc->read_request);
530 	scdc_print_flag(m, "Scrambling Supported", scdc->scrambling.supported);
531 	scdc_print_flag(m, "Low Rate Scrambling Supported", scdc->scrambling.low_rates);
532 
533 	mutex_unlock(&connector->dev->mode_config.mutex);
534 
535 	drm_connector_put(connector);
536 
537 	frl_lanes = drm_scdc_num_frl_lanes(st->rate);
538 
539 	scdc_print_flag(m, "Scrambling Enabled", st->scrambling_enabled);
540 	scdc_print_flag(m, "Scrambling Detected", st->scrambling_detected);
541 	scdc_print_str(m, "FRL Rate", drm_scdc_frl_rate_str(st->rate));
542 	scdc_print_dec(m, "FFE Levels", st->ffe_levels);
543 
544 	if (st->tmds_bclk_x40)
545 		scdc_print_str(m, "TMDS Bit Clock Ratio", "1/40");
546 	else
547 		scdc_print_str(m, "TMDS Bit Clock Ratio", "1/10");
548 
549 	scdc_print_flag(m, "Clock Detected", st->stf.clock_detected);
550 	scdc_print_flag(m, "Channel 0 Locked", st->stf.ch0_locked);
551 	scdc_print_flag(m, "Channel 1 Locked", st->stf.ch1_locked);
552 	scdc_print_flag(m, "Channel 2 Locked", st->stf.ch2_locked);
553 	if (frl_lanes == 4)
554 		scdc_print_flag(m, "Lane 3 Locked", st->stf.ln3_locked);
555 
556 	scdc_print_flag(m, "Sink Ready For Link Training", st->stf.flt_ready);
557 	scdc_print_flag(m, "Sink Failed To Decode DSC", st->stf.dsc_fail);
558 
559 	scdc_print_dec(m, "Channel 0 Errors", st->error_count[0]);
560 	scdc_print_dec(m, "Channel 1 Errors", st->error_count[1]);
561 	scdc_print_dec(m, "Channel 2 Errors", st->error_count[2]);
562 	if (frl_lanes == 4)
563 		scdc_print_dec(m, "Lane 3 Errors", st->error_count[3]);
564 	if (frl_lanes > 0)
565 		scdc_print_dec(m, "Reed-Solomon Corrections", st->rs_corrections);
566 
567 	kfree(st);
568 
569 	return 0;
570 
571 err_free_state:
572 	kfree(st);
573 err_unlock:
574 	mutex_unlock(&connector->dev->mode_config.mutex);
575 err_conn_put:
576 	drm_connector_put(connector);
577 
578 	return ret;
579 }
580 DEFINE_SHOW_ATTRIBUTE(scdc_status);
581 
582 /**
583  * drm_scdc_debugfs_init - Initialize scdc files in connector debugfs
584  * @connector: pointer to &struct drm_connector to operate on
585  * @root: debugfs &struct dentry for the debugfs root of @connector
586  *
587  * Creates SCDC-related debugfs files for @connector. Must be called after
588  * @root is already created.
589  */
590 void drm_scdc_debugfs_init(struct drm_connector *connector, struct dentry *root)
591 {
592 	if (!root || !connector)
593 		return;
594 
595 	debugfs_create_file("scdc_status", 0444, root, connector, &scdc_status_fops);
596 }
597 EXPORT_SYMBOL(drm_scdc_debugfs_init);
598