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