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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
|
/*
* Copyright (c) 2015 NVIDIA Corporation. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sub license,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <linux/bitfield.h>
#include <linux/debugfs.h>
#include <linux/delay.h>
#include <linux/export.h>
#include <linux/i2c.h>
#include <linux/minmax.h>
#include <linux/overflow.h>
#include <linux/slab.h>
#include <drm/display/drm_scdc_helper.h>
#include <drm/drm_connector.h>
#include <drm/drm_device.h>
#include <drm/drm_print.h>
/**
* DOC: scdc helpers
*
* Status and Control Data Channel (SCDC) is a mechanism introduced by the
* HDMI 2.0 specification. It is a point-to-point protocol that allows the
* HDMI source and HDMI sink to exchange data. The same I2C interface that
* is used to access EDID serves as the transport mechanism for SCDC.
*
* Note: The SCDC status is going to be lost when the display is
* disconnected. This can happen physically when the user disconnects
* the cable, but also when a display is switched on (such as waking up
* a TV).
*
* This is further complicated by the fact that, upon a disconnection /
* reconnection, KMS won't change the mode on its own. This means that
* one can't just rely on setting the SCDC status on enable, but also
* has to track the connector status changes using interrupts and
* restore the SCDC status. The typical solution for this is to trigger an
* empty modeset in drm_connector_helper_funcs.detect_ctx(), like what vc4 does
* in vc4_hdmi_reset_link().
*/
#define SCDC_I2C_SLAVE_ADDRESS 0x54
#define drm_scdc_dbg(connector, fmt, ...) \
drm_dbg_kms((connector)->dev, "[CONNECTOR:%d:%s] " fmt, \
(connector)->base.id, (connector)->name, ##__VA_ARGS__)
static const char *drm_scdc_frl_rate_str(enum drm_scdc_frl_rate rate)
{
switch (rate) {
case SCDC_FRL_RATE_OFF:
return "Off";
case SCDC_FRL_RATE_3X3:
return "3 Gbit/s x 3 lanes";
case SCDC_FRL_RATE_6X3:
return "6 Gbit/s x 3 lanes";
case SCDC_FRL_RATE_6X4:
return "6 Gbit/s x 4 lanes";
case SCDC_FRL_RATE_8X4:
return "8 Gbit/s x 4 lanes";
case SCDC_FRL_RATE_10X4:
return "10 Gbit/s x 4 lanes";
case SCDC_FRL_RATE_12X4:
return "12 Gbit/s x 4 lanes";
case SCDC_FRL_RATE_RESV_7:
case SCDC_FRL_RATE_RESV_8:
case SCDC_FRL_RATE_RESV_9:
case SCDC_FRL_RATE_RESV_10:
case SCDC_FRL_RATE_RESV_11:
case SCDC_FRL_RATE_RESV_12:
case SCDC_FRL_RATE_RESV_13:
case SCDC_FRL_RATE_RESV_14:
case SCDC_FRL_RATE_RESV_15:
return "(Reserved)";
default:
return NULL;
}
}
/**
* drm_scdc_read - read a block of data from SCDC
* @adapter: I2C controller
* @offset: start offset of block to read
* @buffer: return location for the block to read
* @size: size of the block to read
*
* Reads a block of data from SCDC, starting at a given offset.
*
* Returns:
* 0 on success, negative error code on failure.
*/
int drm_scdc_read(struct i2c_adapter *adapter, u8 offset, void *buffer,
size_t size)
{
int ret;
struct i2c_msg msgs[2] = {
{
.addr = SCDC_I2C_SLAVE_ADDRESS,
.flags = 0,
.len = 1,
.buf = &offset,
}, {
.addr = SCDC_I2C_SLAVE_ADDRESS,
.flags = I2C_M_RD,
.len = size,
.buf = buffer,
}
};
ret = i2c_transfer(adapter, msgs, ARRAY_SIZE(msgs));
if (ret < 0)
return ret;
if (ret != ARRAY_SIZE(msgs))
return -EPROTO;
return 0;
}
EXPORT_SYMBOL(drm_scdc_read);
/**
* drm_scdc_write - write a block of data to SCDC
* @adapter: I2C controller
* @offset: start offset of block to write
* @buffer: block of data to write
* @size: size of the block to write
*
* Writes a block of data to SCDC, starting at a given offset.
*
* Returns:
* 0 on success, negative error code on failure.
*/
int drm_scdc_write(struct i2c_adapter *adapter, u8 offset, const void *buffer,
size_t size)
{
struct i2c_msg msg = {
.addr = SCDC_I2C_SLAVE_ADDRESS,
.flags = 0,
.len = 1 + size,
.buf = NULL,
};
void *data;
int err;
data = kmalloc(1 + size, GFP_KERNEL);
if (!data)
return -ENOMEM;
msg.buf = data;
memcpy(data, &offset, sizeof(offset));
memcpy(data + 1, buffer, size);
err = i2c_transfer(adapter, &msg, 1);
kfree(data);
if (err < 0)
return err;
if (err != 1)
return -EPROTO;
return 0;
}
EXPORT_SYMBOL(drm_scdc_write);
/**
* drm_scdc_get_scrambling_status - what is status of scrambling?
* @connector: connector
*
* Reads the scrambler status over SCDC, and checks the
* scrambling status.
*
* Returns:
* True if the scrambling is enabled, false otherwise.
*/
bool drm_scdc_get_scrambling_status(struct drm_connector *connector)
{
u8 status;
int ret;
ret = drm_scdc_readb(connector->ddc, SCDC_SCRAMBLER_STATUS, &status);
if (ret < 0) {
drm_scdc_dbg(connector, "Failed to read scrambling status: %d\n", ret);
return false;
}
return status & SCDC_SCRAMBLING_STATUS;
}
EXPORT_SYMBOL(drm_scdc_get_scrambling_status);
/**
* drm_scdc_set_scrambling - enable scrambling
* @connector: connector
* @enable: bool to indicate if scrambling is to be enabled/disabled
*
* Writes the TMDS config register over SCDC channel, and:
* enables scrambling when enable = 1
* disables scrambling when enable = 0
*
* Returns:
* True if scrambling is set/reset successfully, false otherwise.
*/
bool drm_scdc_set_scrambling(struct drm_connector *connector,
bool enable)
{
u8 config;
int ret;
ret = drm_scdc_readb(connector->ddc, SCDC_TMDS_CONFIG, &config);
if (ret < 0) {
drm_scdc_dbg(connector, "Failed to read TMDS config: %d\n", ret);
return false;
}
if (enable)
config |= SCDC_SCRAMBLING_ENABLE;
else
config &= ~SCDC_SCRAMBLING_ENABLE;
ret = drm_scdc_writeb(connector->ddc, SCDC_TMDS_CONFIG, config);
if (ret < 0) {
drm_scdc_dbg(connector, "Failed to enable scrambling: %d\n", ret);
return false;
}
return true;
}
EXPORT_SYMBOL(drm_scdc_set_scrambling);
/**
* drm_scdc_set_high_tmds_clock_ratio - set TMDS clock ratio
* @connector: connector
* @set: ret or reset the high clock ratio
*
*
* TMDS clock ratio calculations go like this:
* TMDS character = 10 bit TMDS encoded value
*
* TMDS character rate = The rate at which TMDS characters are
* transmitted (Mcsc)
*
* TMDS bit rate = 10x TMDS character rate
*
* As per the spec:
* TMDS clock rate for pixel clock < 340 MHz = 1x the character
* rate = 1/10 pixel clock rate
*
* TMDS clock rate for pixel clock > 340 MHz = 0.25x the character
* rate = 1/40 pixel clock rate
*
* Writes to the TMDS config register over SCDC channel, and:
* sets TMDS clock ratio to 1/40 when set = 1
*
* sets TMDS clock ratio to 1/10 when set = 0
*
* Returns:
* True if write is successful, false otherwise.
*/
bool drm_scdc_set_high_tmds_clock_ratio(struct drm_connector *connector,
bool set)
{
u8 config;
int ret;
ret = drm_scdc_readb(connector->ddc, SCDC_TMDS_CONFIG, &config);
if (ret < 0) {
drm_scdc_dbg(connector, "Failed to read TMDS config: %d\n", ret);
return false;
}
if (set)
config |= SCDC_TMDS_BIT_CLOCK_RATIO_BY_40;
else
config &= ~SCDC_TMDS_BIT_CLOCK_RATIO_BY_40;
ret = drm_scdc_writeb(connector->ddc, SCDC_TMDS_CONFIG, config);
if (ret < 0) {
drm_scdc_dbg(connector, "Failed to set TMDS clock ratio: %d\n", ret);
return false;
}
/*
* The spec says that a source should wait minimum 1ms and maximum
* 100ms after writing the TMDS config for clock ratio. Lets allow a
* wait of up to 2ms here.
*/
usleep_range(1000, 2000);
return true;
}
EXPORT_SYMBOL(drm_scdc_set_high_tmds_clock_ratio);
/**
* drm_scdc_set_source_version - set SCDC source version on the sink
* @connector: connector
* @ver: source version to advertise (per spec, 1)
*
* Reads the sink version for diagnostics and as a guard, then writes
* the source version unless the sink reports version 0.
*
* Returns:
* 0 on success or when skipped; a negative error code when either
* the read or the write failed.
*/
int drm_scdc_set_source_version(struct drm_connector *connector, u8 ver)
{
u8 sink_ver;
int ret;
ret = drm_scdc_readb(connector->ddc, SCDC_SINK_VERSION, &sink_ver);
if (ret) {
drm_scdc_dbg(connector, "Failed to read SCDC_SINK_VERSION: %d\n", ret);
return ret;
}
drm_scdc_dbg(connector, "Sink reported SCDC ver. %u\n", sink_ver);
/*
* Only advertise our source version to sinks that report a
* non-zero sink version. A sink reporting version 0 is either
* not SCDC-version-aware or non-conformant; writing the source
* version gains nothing and may upset broken hardware.
*/
if (sink_ver) {
ret = drm_scdc_writeb(connector->ddc, SCDC_SOURCE_VERSION,
min_t(u8, sink_ver, ver));
if (ret)
drm_scdc_dbg(connector,
"Failed to write SCDC_SOURCE_VERSION: %d\n", ret);
}
return ret;
}
EXPORT_SYMBOL(drm_scdc_set_source_version);
static void
drm_scdc_parse_status0_flags(u8 val, struct drm_scdc_status_flags *flags)
{
flags->clock_detected = val & SCDC_CLOCK_DETECT;
flags->ch0_locked = val & SCDC_CH0_LOCK;
flags->ch1_locked = val & SCDC_CH1_LOCK;
flags->ch2_locked = val & SCDC_CH2_LOCK;
flags->ln3_locked = val & SCDC_LN3_LOCK;
flags->flt_ready = val & SCDC_FLT_READY;
flags->dsc_fail = val & SCDC_DSC_FAIL;
}
static void
drm_scdc_parse_status1_2_flags(u8 val_flag1, u8 val_flag2,
struct drm_scdc_status_flags *flags)
{
flags->ln0_training_pattern = FIELD_GET(SCDC_LN_EVEN_TRAIN_PTRN, val_flag1);
flags->ln1_training_pattern = FIELD_GET(SCDC_LN_ODD_TRAIN_PTRN, val_flag1);
flags->ln2_training_pattern = FIELD_GET(SCDC_LN_EVEN_TRAIN_PTRN, val_flag2);
flags->ln3_training_pattern = FIELD_GET(SCDC_LN_ODD_TRAIN_PTRN, val_flag2);
}
static int drm_scdc_parse_error_counters(const u8 scdc[256], u16 counter[4],
unsigned int num_lanes)
{
u8 end_reg;
u8 sum = 0;
int i;
switch (num_lanes) {
case 3:
end_reg = SCDC_ERR_DET_CHECKSUM;
break;
case 4:
end_reg = SCDC_ERR_DET_3_H;
break;
default:
return -EINVAL;
}
for (i = SCDC_ERR_DET_0_L; i <= end_reg; i++)
sum = wrapping_add(u8, sum, scdc[i]);
if (sum)
return -EPROTO;
for (i = 0; i < 3; i++) {
if (scdc[SCDC_ERR_DET_0_H + i * 2] & SCDC_CHANNEL_VALID)
counter[i] = (scdc[SCDC_ERR_DET_0_H + i * 2] &
~SCDC_CHANNEL_VALID) << 8 |
scdc[SCDC_ERR_DET_0_L + i * 2];
else
counter[i] = 0;
}
if (num_lanes == 4 && scdc[SCDC_ERR_DET_3_H] & SCDC_CHANNEL_VALID)
counter[3] = (scdc[SCDC_ERR_DET_3_H] & ~SCDC_CHANNEL_VALID) << 8 |
scdc[SCDC_ERR_DET_3_L];
else
counter[3] = 0;
return 0;
}
/**
* drm_scdc_read_state - Update state from SCDC
* @connector: pointer to a &struct drm_connector on which to operate on
* @state: pointer to a &struct drm_scdc_state to fill
*
* Reads the entire 256 byte SCDC state and parses it.
*
* Returns: %0 on success, negative errno on failure.
*/
int drm_scdc_read_state(struct drm_connector *connector, struct drm_scdc_state *state)
{
struct i2c_adapter *ddc;
struct drm_scdc *scdc;
u8 *buf = state->scdc;
int num_lanes;
int ret;
if (!state || !connector)
return -ENODEV;
scdc = &connector->display_info.hdmi.scdc;
ddc = connector->ddc;
if (!scdc->supported || !ddc)
return -EOPNOTSUPP;
/* Read in 128-byte chunks, to work around DP<->HDMI converters with issues. */
ret = drm_scdc_read(ddc, 0, buf, 128);
if (ret)
return ret;
ret = drm_scdc_read(ddc, 128, &buf[128], 128);
if (ret)
return ret;
state->scrambling_enabled = buf[SCDC_TMDS_CONFIG] & SCDC_SCRAMBLING_ENABLE;
state->tmds_bclk_x40 = buf[SCDC_TMDS_CONFIG] & SCDC_TMDS_BIT_CLOCK_RATIO_BY_40;
state->scrambling_detected = buf[SCDC_SCRAMBLER_STATUS] & SCDC_SCRAMBLING_STATUS;
state->rate = FIELD_GET(SCDC_FRL_RATE, buf[SCDC_CONFIG_1]);
num_lanes = drm_scdc_num_frl_lanes(state->rate);
if (num_lanes < 0)
return num_lanes;
if (!num_lanes)
num_lanes = 3;
state->ffe_levels = FIELD_GET(SCDC_FFE_LEVELS, buf[SCDC_CONFIG_1]);
drm_scdc_parse_status0_flags(buf[SCDC_STATUS_FLAGS_0], &state->stf);
drm_scdc_parse_status1_2_flags(buf[SCDC_STATUS_FLAGS_1],
buf[SCDC_STATUS_FLAGS_2], &state->stf);
ret = drm_scdc_parse_error_counters(buf, state->error_count, num_lanes);
if (ret)
return ret;
if (state->rate && (buf[SCDC_ERR_DET_RS_H] & SCDC_CHANNEL_VALID))
state->rs_corrections = (buf[SCDC_ERR_DET_RS_H] & ~SCDC_CHANNEL_VALID) << 8 |
buf[SCDC_ERR_DET_RS_L];
return 0;
}
EXPORT_SYMBOL(drm_scdc_read_state);
#define scdc_print_str(_f, key, s) \
(seq_printf((_f), "%-30s: %s\n", (key), (s)))
#define scdc_print_flag(_f, key, val) \
(scdc_print_str((_f), (key), str_yes_no((val))))
#define scdc_print_dec(_f, key, val) \
(seq_printf((_f), "%-30s: %d\n", (key), (val)))
static int scdc_status_show(struct seq_file *m, void *data)
{
struct drm_connector *connector = m->private;
struct drm_scdc *scdc = &connector->display_info.hdmi.scdc;
struct drm_scdc_state *st;
int i, ret, frl_lanes;
drm_connector_get(connector);
ret = mutex_lock_interruptible(&connector->dev->mode_config.mutex);
if (ret)
goto err_conn_put;
if (connector->status != connector_status_connected) {
ret = -ENODEV;
goto err_unlock;
}
if (scdc->supported) {
st = kzalloc_obj(*st);
if (!st) {
ret = -ENOMEM;
goto err_unlock;
}
ret = drm_scdc_read_state(connector, st);
if (ret)
goto err_free_state;
for (i = 0; i < ARRAY_SIZE(st->scdc); i += 16)
seq_printf(m, "%*ph\n", 16, &st->scdc[i]);
seq_puts(m, "\n----------------\n\n");
}
scdc_print_flag(m, "SCDC Supported", scdc->supported);
if (!scdc->supported) {
ret = 0;
goto err_unlock;
}
scdc_print_flag(m, "Sink Read Request Capable", scdc->read_request);
scdc_print_flag(m, "Scrambling Supported", scdc->scrambling.supported);
scdc_print_flag(m, "Low Rate Scrambling Supported", scdc->scrambling.low_rates);
mutex_unlock(&connector->dev->mode_config.mutex);
drm_connector_put(connector);
frl_lanes = drm_scdc_num_frl_lanes(st->rate);
scdc_print_flag(m, "Scrambling Enabled", st->scrambling_enabled);
scdc_print_flag(m, "Scrambling Detected", st->scrambling_detected);
scdc_print_str(m, "FRL Rate", drm_scdc_frl_rate_str(st->rate));
scdc_print_dec(m, "FFE Levels", st->ffe_levels);
if (st->tmds_bclk_x40)
scdc_print_str(m, "TMDS Bit Clock Ratio", "1/40");
else
scdc_print_str(m, "TMDS Bit Clock Ratio", "1/10");
scdc_print_flag(m, "Clock Detected", st->stf.clock_detected);
scdc_print_flag(m, "Channel 0 Locked", st->stf.ch0_locked);
scdc_print_flag(m, "Channel 1 Locked", st->stf.ch1_locked);
scdc_print_flag(m, "Channel 2 Locked", st->stf.ch2_locked);
if (frl_lanes == 4)
scdc_print_flag(m, "Lane 3 Locked", st->stf.ln3_locked);
scdc_print_flag(m, "Sink Ready For Link Training", st->stf.flt_ready);
scdc_print_flag(m, "Sink Failed To Decode DSC", st->stf.dsc_fail);
scdc_print_dec(m, "Channel 0 Errors", st->error_count[0]);
scdc_print_dec(m, "Channel 1 Errors", st->error_count[1]);
scdc_print_dec(m, "Channel 2 Errors", st->error_count[2]);
if (frl_lanes == 4)
scdc_print_dec(m, "Lane 3 Errors", st->error_count[3]);
if (frl_lanes > 0)
scdc_print_dec(m, "Reed-Solomon Corrections", st->rs_corrections);
kfree(st);
return 0;
err_free_state:
kfree(st);
err_unlock:
mutex_unlock(&connector->dev->mode_config.mutex);
err_conn_put:
drm_connector_put(connector);
return ret;
}
DEFINE_SHOW_ATTRIBUTE(scdc_status);
/**
* drm_scdc_debugfs_init - Initialize scdc files in connector debugfs
* @connector: pointer to &struct drm_connector to operate on
* @root: debugfs &struct dentry for the debugfs root of @connector
*
* Creates SCDC-related debugfs files for @connector. Must be called after
* @root is already created.
*/
void drm_scdc_debugfs_init(struct drm_connector *connector, struct dentry *root)
{
if (!root || !connector)
return;
debugfs_create_file("scdc_status", 0444, root, connector, &scdc_status_fops);
}
EXPORT_SYMBOL(drm_scdc_debugfs_init);
|