This source file includes following definitions.
- drm_dp_dual_mode_read
- drm_dp_dual_mode_write
- is_hdmi_adaptor
- is_type1_adaptor
- is_type2_adaptor
- is_lspcon_adaptor
- drm_dp_dual_mode_detect
- drm_dp_dual_mode_max_tmds_clock
- drm_dp_dual_mode_get_tmds_output
- drm_dp_dual_mode_set_tmds_output
- drm_dp_get_dual_mode_type_name
- drm_lspcon_get_mode
- drm_lspcon_set_mode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 #include <linux/delay.h>
24 #include <linux/errno.h>
25 #include <linux/export.h>
26 #include <linux/i2c.h>
27 #include <linux/slab.h>
28 #include <linux/string.h>
29
30 #include <drm/drm_dp_dual_mode_helper.h>
31 #include <drm/drm_print.h>
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 #define DP_DUAL_MODE_SLAVE_ADDRESS 0x40
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 ssize_t drm_dp_dual_mode_read(struct i2c_adapter *adapter,
63 u8 offset, void *buffer, size_t size)
64 {
65 struct i2c_msg msgs[] = {
66 {
67 .addr = DP_DUAL_MODE_SLAVE_ADDRESS,
68 .flags = 0,
69 .len = 1,
70 .buf = &offset,
71 },
72 {
73 .addr = DP_DUAL_MODE_SLAVE_ADDRESS,
74 .flags = I2C_M_RD,
75 .len = size,
76 .buf = buffer,
77 },
78 };
79 int ret;
80
81 ret = i2c_transfer(adapter, msgs, ARRAY_SIZE(msgs));
82 if (ret < 0)
83 return ret;
84 if (ret != ARRAY_SIZE(msgs))
85 return -EPROTO;
86
87 return 0;
88 }
89 EXPORT_SYMBOL(drm_dp_dual_mode_read);
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104 ssize_t drm_dp_dual_mode_write(struct i2c_adapter *adapter,
105 u8 offset, const void *buffer, size_t size)
106 {
107 struct i2c_msg msg = {
108 .addr = DP_DUAL_MODE_SLAVE_ADDRESS,
109 .flags = 0,
110 .len = 1 + size,
111 .buf = NULL,
112 };
113 void *data;
114 int ret;
115
116 data = kmalloc(msg.len, GFP_KERNEL);
117 if (!data)
118 return -ENOMEM;
119
120 msg.buf = data;
121
122 memcpy(data, &offset, 1);
123 memcpy(data + 1, buffer, size);
124
125 ret = i2c_transfer(adapter, &msg, 1);
126
127 kfree(data);
128
129 if (ret < 0)
130 return ret;
131 if (ret != 1)
132 return -EPROTO;
133
134 return 0;
135 }
136 EXPORT_SYMBOL(drm_dp_dual_mode_write);
137
138 static bool is_hdmi_adaptor(const char hdmi_id[DP_DUAL_MODE_HDMI_ID_LEN])
139 {
140 static const char dp_dual_mode_hdmi_id[DP_DUAL_MODE_HDMI_ID_LEN] =
141 "DP-HDMI ADAPTOR\x04";
142
143 return memcmp(hdmi_id, dp_dual_mode_hdmi_id,
144 sizeof(dp_dual_mode_hdmi_id)) == 0;
145 }
146
147 static bool is_type1_adaptor(uint8_t adaptor_id)
148 {
149 return adaptor_id == 0 || adaptor_id == 0xff;
150 }
151
152 static bool is_type2_adaptor(uint8_t adaptor_id)
153 {
154 return adaptor_id == (DP_DUAL_MODE_TYPE_TYPE2 |
155 DP_DUAL_MODE_REV_TYPE2);
156 }
157
158 static bool is_lspcon_adaptor(const char hdmi_id[DP_DUAL_MODE_HDMI_ID_LEN],
159 const uint8_t adaptor_id)
160 {
161 return is_hdmi_adaptor(hdmi_id) &&
162 (adaptor_id == (DP_DUAL_MODE_TYPE_TYPE2 |
163 DP_DUAL_MODE_TYPE_HAS_DPCD));
164 }
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181 enum drm_dp_dual_mode_type drm_dp_dual_mode_detect(struct i2c_adapter *adapter)
182 {
183 char hdmi_id[DP_DUAL_MODE_HDMI_ID_LEN] = {};
184 uint8_t adaptor_id = 0x00;
185 ssize_t ret;
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201 ret = drm_dp_dual_mode_read(adapter, DP_DUAL_MODE_HDMI_ID,
202 hdmi_id, sizeof(hdmi_id));
203 DRM_DEBUG_KMS("DP dual mode HDMI ID: %*pE (err %zd)\n",
204 ret ? 0 : (int)sizeof(hdmi_id), hdmi_id, ret);
205 if (ret)
206 return DRM_DP_DUAL_MODE_UNKNOWN;
207
208
209
210
211
212
213
214
215
216
217
218
219
220 ret = drm_dp_dual_mode_read(adapter, DP_DUAL_MODE_ADAPTOR_ID,
221 &adaptor_id, sizeof(adaptor_id));
222 DRM_DEBUG_KMS("DP dual mode adaptor ID: %02x (err %zd)\n",
223 adaptor_id, ret);
224 if (ret == 0) {
225 if (is_lspcon_adaptor(hdmi_id, adaptor_id))
226 return DRM_DP_DUAL_MODE_LSPCON;
227 if (is_type2_adaptor(adaptor_id)) {
228 if (is_hdmi_adaptor(hdmi_id))
229 return DRM_DP_DUAL_MODE_TYPE2_HDMI;
230 else
231 return DRM_DP_DUAL_MODE_TYPE2_DVI;
232 }
233
234
235
236
237
238 if (!is_type1_adaptor(adaptor_id) && adaptor_id != hdmi_id[0])
239 DRM_ERROR("Unexpected DP dual mode adaptor ID %02x\n",
240 adaptor_id);
241
242 }
243
244 if (is_hdmi_adaptor(hdmi_id))
245 return DRM_DP_DUAL_MODE_TYPE1_HDMI;
246 else
247 return DRM_DP_DUAL_MODE_TYPE1_DVI;
248 }
249 EXPORT_SYMBOL(drm_dp_dual_mode_detect);
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266 int drm_dp_dual_mode_max_tmds_clock(enum drm_dp_dual_mode_type type,
267 struct i2c_adapter *adapter)
268 {
269 uint8_t max_tmds_clock;
270 ssize_t ret;
271
272
273 if (type == DRM_DP_DUAL_MODE_NONE)
274 return 0;
275
276
277
278
279
280 if (type < DRM_DP_DUAL_MODE_TYPE2_DVI)
281 return 165000;
282
283 ret = drm_dp_dual_mode_read(adapter, DP_DUAL_MODE_MAX_TMDS_CLOCK,
284 &max_tmds_clock, sizeof(max_tmds_clock));
285 if (ret || max_tmds_clock == 0x00 || max_tmds_clock == 0xff) {
286 DRM_DEBUG_KMS("Failed to query max TMDS clock\n");
287 return 165000;
288 }
289
290 return max_tmds_clock * 5000 / 2;
291 }
292 EXPORT_SYMBOL(drm_dp_dual_mode_max_tmds_clock);
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310 int drm_dp_dual_mode_get_tmds_output(enum drm_dp_dual_mode_type type,
311 struct i2c_adapter *adapter,
312 bool *enabled)
313 {
314 uint8_t tmds_oen;
315 ssize_t ret;
316
317 if (type < DRM_DP_DUAL_MODE_TYPE2_DVI) {
318 *enabled = true;
319 return 0;
320 }
321
322 ret = drm_dp_dual_mode_read(adapter, DP_DUAL_MODE_TMDS_OEN,
323 &tmds_oen, sizeof(tmds_oen));
324 if (ret) {
325 DRM_DEBUG_KMS("Failed to query state of TMDS output buffers\n");
326 return ret;
327 }
328
329 *enabled = !(tmds_oen & DP_DUAL_MODE_TMDS_DISABLE);
330
331 return 0;
332 }
333 EXPORT_SYMBOL(drm_dp_dual_mode_get_tmds_output);
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350 int drm_dp_dual_mode_set_tmds_output(enum drm_dp_dual_mode_type type,
351 struct i2c_adapter *adapter, bool enable)
352 {
353 uint8_t tmds_oen = enable ? 0 : DP_DUAL_MODE_TMDS_DISABLE;
354 ssize_t ret;
355 int retry;
356
357 if (type < DRM_DP_DUAL_MODE_TYPE2_DVI)
358 return 0;
359
360
361
362
363
364 for (retry = 0; retry < 3; retry++) {
365 uint8_t tmp;
366
367 ret = drm_dp_dual_mode_write(adapter, DP_DUAL_MODE_TMDS_OEN,
368 &tmds_oen, sizeof(tmds_oen));
369 if (ret) {
370 DRM_DEBUG_KMS("Failed to %s TMDS output buffers (%d attempts)\n",
371 enable ? "enable" : "disable",
372 retry + 1);
373 return ret;
374 }
375
376 ret = drm_dp_dual_mode_read(adapter, DP_DUAL_MODE_TMDS_OEN,
377 &tmp, sizeof(tmp));
378 if (ret) {
379 DRM_DEBUG_KMS("I2C read failed during TMDS output buffer %s (%d attempts)\n",
380 enable ? "enabling" : "disabling",
381 retry + 1);
382 return ret;
383 }
384
385 if (tmp == tmds_oen)
386 return 0;
387 }
388
389 DRM_DEBUG_KMS("I2C write value mismatch during TMDS output buffer %s\n",
390 enable ? "enabling" : "disabling");
391
392 return -EIO;
393 }
394 EXPORT_SYMBOL(drm_dp_dual_mode_set_tmds_output);
395
396
397
398
399
400
401
402
403 const char *drm_dp_get_dual_mode_type_name(enum drm_dp_dual_mode_type type)
404 {
405 switch (type) {
406 case DRM_DP_DUAL_MODE_NONE:
407 return "none";
408 case DRM_DP_DUAL_MODE_TYPE1_DVI:
409 return "type 1 DVI";
410 case DRM_DP_DUAL_MODE_TYPE1_HDMI:
411 return "type 1 HDMI";
412 case DRM_DP_DUAL_MODE_TYPE2_DVI:
413 return "type 2 DVI";
414 case DRM_DP_DUAL_MODE_TYPE2_HDMI:
415 return "type 2 HDMI";
416 case DRM_DP_DUAL_MODE_LSPCON:
417 return "lspcon";
418 default:
419 WARN_ON(type != DRM_DP_DUAL_MODE_UNKNOWN);
420 return "unknown";
421 }
422 }
423 EXPORT_SYMBOL(drm_dp_get_dual_mode_type_name);
424
425
426
427
428
429
430
431
432
433
434
435 int drm_lspcon_get_mode(struct i2c_adapter *adapter,
436 enum drm_lspcon_mode *mode)
437 {
438 u8 data;
439 int ret = 0;
440 int retry;
441
442 if (!mode) {
443 DRM_ERROR("NULL input\n");
444 return -EINVAL;
445 }
446
447
448 for (retry = 0; retry < 6; retry++) {
449 if (retry)
450 usleep_range(500, 1000);
451
452 ret = drm_dp_dual_mode_read(adapter,
453 DP_DUAL_MODE_LSPCON_CURRENT_MODE,
454 &data, sizeof(data));
455 if (!ret)
456 break;
457 }
458
459 if (ret < 0) {
460 DRM_DEBUG_KMS("LSPCON read(0x80, 0x41) failed\n");
461 return -EFAULT;
462 }
463
464 if (data & DP_DUAL_MODE_LSPCON_MODE_PCON)
465 *mode = DRM_LSPCON_MODE_PCON;
466 else
467 *mode = DRM_LSPCON_MODE_LS;
468 return 0;
469 }
470 EXPORT_SYMBOL(drm_lspcon_get_mode);
471
472
473
474
475
476
477
478
479
480
481 int drm_lspcon_set_mode(struct i2c_adapter *adapter,
482 enum drm_lspcon_mode mode)
483 {
484 u8 data = 0;
485 int ret;
486 int time_out = 200;
487 enum drm_lspcon_mode current_mode;
488
489 if (mode == DRM_LSPCON_MODE_PCON)
490 data = DP_DUAL_MODE_LSPCON_MODE_PCON;
491
492
493 ret = drm_dp_dual_mode_write(adapter, DP_DUAL_MODE_LSPCON_MODE_CHANGE,
494 &data, sizeof(data));
495 if (ret < 0) {
496 DRM_ERROR("LSPCON mode change failed\n");
497 return ret;
498 }
499
500
501
502
503
504
505 do {
506 ret = drm_lspcon_get_mode(adapter, ¤t_mode);
507 if (ret) {
508 DRM_ERROR("can't confirm LSPCON mode change\n");
509 return ret;
510 } else {
511 if (current_mode != mode) {
512 msleep(10);
513 time_out -= 10;
514 } else {
515 DRM_DEBUG_KMS("LSPCON mode changed to %s\n",
516 mode == DRM_LSPCON_MODE_LS ?
517 "LS" : "PCON");
518 return 0;
519 }
520 }
521 } while (time_out);
522
523 DRM_ERROR("LSPCON mode change timed out\n");
524 return -ETIMEDOUT;
525 }
526 EXPORT_SYMBOL(drm_lspcon_set_mode);