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#include <drm/drm_edid.h>
26#include <drm/drm_atomic_helper.h>
27#include <drm/drm_dp_dual_mode_helper.h>
28#include "intel_drv.h"
29
30
31#define LSPCON_VENDOR_PARADE_OUI 0x001CF8
32#define LSPCON_VENDOR_MCA_OUI 0x0060AD
33
34
35#define LSPCON_MCA_AVI_IF_WRITE_OFFSET 0x5C0
36#define LSPCON_MCA_AVI_IF_CTRL 0x5DF
37#define LSPCON_MCA_AVI_IF_KICKOFF (1 << 0)
38#define LSPCON_MCA_AVI_IF_HANDLED (1 << 1)
39
40
41#define LSPCON_PARADE_AVI_IF_WRITE_OFFSET 0x516
42#define LSPCON_PARADE_AVI_IF_CTRL 0x51E
43#define LSPCON_PARADE_AVI_IF_KICKOFF (1 << 7)
44#define LSPCON_PARADE_AVI_IF_DATA_SIZE 32
45
46static struct intel_dp *lspcon_to_intel_dp(struct intel_lspcon *lspcon)
47{
48 struct intel_digital_port *dig_port =
49 container_of(lspcon, struct intel_digital_port, lspcon);
50
51 return &dig_port->dp;
52}
53
54static const char *lspcon_mode_name(enum drm_lspcon_mode mode)
55{
56 switch (mode) {
57 case DRM_LSPCON_MODE_PCON:
58 return "PCON";
59 case DRM_LSPCON_MODE_LS:
60 return "LS";
61 case DRM_LSPCON_MODE_INVALID:
62 return "INVALID";
63 default:
64 MISSING_CASE(mode);
65 return "INVALID";
66 }
67}
68
69static bool lspcon_detect_vendor(struct intel_lspcon *lspcon)
70{
71 struct intel_dp *dp = lspcon_to_intel_dp(lspcon);
72 struct drm_dp_dpcd_ident *ident;
73 u32 vendor_oui;
74
75 if (drm_dp_read_desc(&dp->aux, &dp->desc, drm_dp_is_branch(dp->dpcd))) {
76 DRM_ERROR("Can't read description\n");
77 return false;
78 }
79
80 ident = &dp->desc.ident;
81 vendor_oui = (ident->oui[0] << 16) | (ident->oui[1] << 8) |
82 ident->oui[2];
83
84 switch (vendor_oui) {
85 case LSPCON_VENDOR_MCA_OUI:
86 lspcon->vendor = LSPCON_VENDOR_MCA;
87 DRM_DEBUG_KMS("Vendor: Mega Chips\n");
88 break;
89
90 case LSPCON_VENDOR_PARADE_OUI:
91 lspcon->vendor = LSPCON_VENDOR_PARADE;
92 DRM_DEBUG_KMS("Vendor: Parade Tech\n");
93 break;
94
95 default:
96 DRM_ERROR("Invalid/Unknown vendor OUI\n");
97 return false;
98 }
99
100 return true;
101}
102
103static enum drm_lspcon_mode lspcon_get_current_mode(struct intel_lspcon *lspcon)
104{
105 enum drm_lspcon_mode current_mode;
106 struct i2c_adapter *adapter = &lspcon_to_intel_dp(lspcon)->aux.ddc;
107
108 if (drm_lspcon_get_mode(adapter, ¤t_mode)) {
109 DRM_DEBUG_KMS("Error reading LSPCON mode\n");
110 return DRM_LSPCON_MODE_INVALID;
111 }
112 return current_mode;
113}
114
115static enum drm_lspcon_mode lspcon_wait_mode(struct intel_lspcon *lspcon,
116 enum drm_lspcon_mode mode)
117{
118 enum drm_lspcon_mode current_mode;
119
120 current_mode = lspcon_get_current_mode(lspcon);
121 if (current_mode == mode)
122 goto out;
123
124 DRM_DEBUG_KMS("Waiting for LSPCON mode %s to settle\n",
125 lspcon_mode_name(mode));
126
127 wait_for((current_mode = lspcon_get_current_mode(lspcon)) == mode, 400);
128 if (current_mode != mode)
129 DRM_ERROR("LSPCON mode hasn't settled\n");
130
131out:
132 DRM_DEBUG_KMS("Current LSPCON mode %s\n",
133 lspcon_mode_name(current_mode));
134
135 return current_mode;
136}
137
138static int lspcon_change_mode(struct intel_lspcon *lspcon,
139 enum drm_lspcon_mode mode)
140{
141 int err;
142 enum drm_lspcon_mode current_mode;
143 struct i2c_adapter *adapter = &lspcon_to_intel_dp(lspcon)->aux.ddc;
144
145 err = drm_lspcon_get_mode(adapter, ¤t_mode);
146 if (err) {
147 DRM_ERROR("Error reading LSPCON mode\n");
148 return err;
149 }
150
151 if (current_mode == mode) {
152 DRM_DEBUG_KMS("Current mode = desired LSPCON mode\n");
153 return 0;
154 }
155
156 err = drm_lspcon_set_mode(adapter, mode);
157 if (err < 0) {
158 DRM_ERROR("LSPCON mode change failed\n");
159 return err;
160 }
161
162 lspcon->mode = mode;
163 DRM_DEBUG_KMS("LSPCON mode changed done\n");
164 return 0;
165}
166
167static bool lspcon_wake_native_aux_ch(struct intel_lspcon *lspcon)
168{
169 u8 rev;
170
171 if (drm_dp_dpcd_readb(&lspcon_to_intel_dp(lspcon)->aux, DP_DPCD_REV,
172 &rev) != 1) {
173 DRM_DEBUG_KMS("Native AUX CH down\n");
174 return false;
175 }
176
177 DRM_DEBUG_KMS("Native AUX CH up, DPCD version: %d.%d\n",
178 rev >> 4, rev & 0xf);
179
180 return true;
181}
182
183void lspcon_ycbcr420_config(struct drm_connector *connector,
184 struct intel_crtc_state *crtc_state)
185{
186 const struct drm_display_info *info = &connector->display_info;
187 const struct drm_display_mode *adjusted_mode =
188 &crtc_state->base.adjusted_mode;
189
190 if (drm_mode_is_420_only(info, adjusted_mode) &&
191 connector->ycbcr_420_allowed) {
192 crtc_state->port_clock /= 2;
193 crtc_state->output_format = INTEL_OUTPUT_FORMAT_YCBCR444;
194 crtc_state->lspcon_downsampling = true;
195 }
196}
197
198static bool lspcon_probe(struct intel_lspcon *lspcon)
199{
200 int retry;
201 enum drm_dp_dual_mode_type adaptor_type;
202 struct i2c_adapter *adapter = &lspcon_to_intel_dp(lspcon)->aux.ddc;
203 enum drm_lspcon_mode expected_mode;
204
205 expected_mode = lspcon_wake_native_aux_ch(lspcon) ?
206 DRM_LSPCON_MODE_PCON : DRM_LSPCON_MODE_LS;
207
208
209 for (retry = 0; retry < 6; retry++) {
210 if (retry)
211 usleep_range(500, 1000);
212
213 adaptor_type = drm_dp_dual_mode_detect(adapter);
214 if (adaptor_type == DRM_DP_DUAL_MODE_LSPCON)
215 break;
216 }
217
218 if (adaptor_type != DRM_DP_DUAL_MODE_LSPCON) {
219 DRM_DEBUG_KMS("No LSPCON detected, found %s\n",
220 drm_dp_get_dual_mode_type_name(adaptor_type));
221 return false;
222 }
223
224
225 DRM_DEBUG_KMS("LSPCON detected\n");
226 lspcon->mode = lspcon_wait_mode(lspcon, expected_mode);
227
228
229
230
231
232
233 if (lspcon->mode != DRM_LSPCON_MODE_PCON) {
234 if (lspcon_change_mode(lspcon, DRM_LSPCON_MODE_PCON) < 0) {
235 DRM_ERROR("LSPCON mode change to PCON failed\n");
236 return false;
237 }
238 }
239 return true;
240}
241
242static void lspcon_resume_in_pcon_wa(struct intel_lspcon *lspcon)
243{
244 struct intel_dp *intel_dp = lspcon_to_intel_dp(lspcon);
245 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
246 unsigned long start = jiffies;
247
248 while (1) {
249 if (intel_digital_port_connected(&dig_port->base)) {
250 DRM_DEBUG_KMS("LSPCON recovering in PCON mode after %u ms\n",
251 jiffies_to_msecs(jiffies - start));
252 return;
253 }
254
255 if (time_after(jiffies, start + msecs_to_jiffies(1000)))
256 break;
257
258 usleep_range(10000, 15000);
259 }
260
261 DRM_DEBUG_KMS("LSPCON DP descriptor mismatch after resume\n");
262}
263
264static bool lspcon_parade_fw_ready(struct drm_dp_aux *aux)
265{
266 u8 avi_if_ctrl;
267 u8 retry;
268 ssize_t ret;
269
270
271 for (retry = 0; retry < 5; retry++) {
272 if (retry)
273 usleep_range(200, 300);
274
275 ret = drm_dp_dpcd_read(aux, LSPCON_PARADE_AVI_IF_CTRL,
276 &avi_if_ctrl, 1);
277 if (ret < 0) {
278 DRM_ERROR("Failed to read AVI IF control\n");
279 return false;
280 }
281
282 if ((avi_if_ctrl & LSPCON_PARADE_AVI_IF_KICKOFF) == 0)
283 return true;
284 }
285
286 DRM_ERROR("Parade FW not ready to accept AVI IF\n");
287 return false;
288}
289
290static bool _lspcon_parade_write_infoframe_blocks(struct drm_dp_aux *aux,
291 u8 *avi_buf)
292{
293 u8 avi_if_ctrl;
294 u8 block_count = 0;
295 u8 *data;
296 u16 reg;
297 ssize_t ret;
298
299 while (block_count < 4) {
300 if (!lspcon_parade_fw_ready(aux)) {
301 DRM_DEBUG_KMS("LSPCON FW not ready, block %d\n",
302 block_count);
303 return false;
304 }
305
306 reg = LSPCON_PARADE_AVI_IF_WRITE_OFFSET;
307 data = avi_buf + block_count * 8;
308 ret = drm_dp_dpcd_write(aux, reg, data, 8);
309 if (ret < 0) {
310 DRM_ERROR("Failed to write AVI IF block %d\n",
311 block_count);
312 return false;
313 }
314
315
316
317
318
319
320
321 reg = LSPCON_PARADE_AVI_IF_CTRL;
322 avi_if_ctrl = LSPCON_PARADE_AVI_IF_KICKOFF | block_count;
323 ret = drm_dp_dpcd_write(aux, reg, &avi_if_ctrl, 1);
324 if (ret < 0) {
325 DRM_ERROR("Failed to update (0x%x), block %d\n",
326 reg, block_count);
327 return false;
328 }
329
330 block_count++;
331 }
332
333 DRM_DEBUG_KMS("Wrote AVI IF blocks successfully\n");
334 return true;
335}
336
337static bool _lspcon_write_avi_infoframe_parade(struct drm_dp_aux *aux,
338 const u8 *frame,
339 ssize_t len)
340{
341 u8 avi_if[LSPCON_PARADE_AVI_IF_DATA_SIZE] = {1, };
342
343
344
345
346
347
348
349
350
351
352
353
354 if (len > LSPCON_PARADE_AVI_IF_DATA_SIZE - 1) {
355 DRM_ERROR("Invalid length of infoframes\n");
356 return false;
357 }
358
359 memcpy(&avi_if[1], frame, len);
360
361 if (!_lspcon_parade_write_infoframe_blocks(aux, avi_if)) {
362 DRM_DEBUG_KMS("Failed to write infoframe blocks\n");
363 return false;
364 }
365
366 return true;
367}
368
369static bool _lspcon_write_avi_infoframe_mca(struct drm_dp_aux *aux,
370 const u8 *buffer, ssize_t len)
371{
372 int ret;
373 u32 val = 0;
374 u32 retry;
375 u16 reg;
376 const u8 *data = buffer;
377
378 reg = LSPCON_MCA_AVI_IF_WRITE_OFFSET;
379 while (val < len) {
380
381 for (retry = 0; retry < 5; retry++) {
382 ret = drm_dp_dpcd_write(aux, reg, (void *)data, 1);
383 if (ret == 1) {
384 break;
385 } else if (retry < 4) {
386 mdelay(50);
387 continue;
388 } else {
389 DRM_ERROR("DPCD write failed at:0x%x\n", reg);
390 return false;
391 }
392 }
393 val++; reg++; data++;
394 }
395
396 val = 0;
397 reg = LSPCON_MCA_AVI_IF_CTRL;
398 ret = drm_dp_dpcd_read(aux, reg, &val, 1);
399 if (ret < 0) {
400 DRM_ERROR("DPCD read failed, address 0x%x\n", reg);
401 return false;
402 }
403
404
405 val &= ~LSPCON_MCA_AVI_IF_HANDLED;
406 val |= LSPCON_MCA_AVI_IF_KICKOFF;
407
408 ret = drm_dp_dpcd_write(aux, reg, &val, 1);
409 if (ret < 0) {
410 DRM_ERROR("DPCD read failed, address 0x%x\n", reg);
411 return false;
412 }
413
414 val = 0;
415 ret = drm_dp_dpcd_read(aux, reg, &val, 1);
416 if (ret < 0) {
417 DRM_ERROR("DPCD read failed, address 0x%x\n", reg);
418 return false;
419 }
420
421 if (val == LSPCON_MCA_AVI_IF_HANDLED)
422 DRM_DEBUG_KMS("AVI IF handled by FW\n");
423
424 return true;
425}
426
427void lspcon_write_infoframe(struct intel_encoder *encoder,
428 const struct intel_crtc_state *crtc_state,
429 unsigned int type,
430 const void *frame, ssize_t len)
431{
432 bool ret;
433 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
434 struct intel_lspcon *lspcon = enc_to_intel_lspcon(&encoder->base);
435
436
437 if (type != HDMI_INFOFRAME_TYPE_AVI)
438 return;
439
440 if (lspcon->vendor == LSPCON_VENDOR_MCA)
441 ret = _lspcon_write_avi_infoframe_mca(&intel_dp->aux,
442 frame, len);
443 else
444 ret = _lspcon_write_avi_infoframe_parade(&intel_dp->aux,
445 frame, len);
446
447 if (!ret) {
448 DRM_ERROR("Failed to write AVI infoframes\n");
449 return;
450 }
451
452 DRM_DEBUG_DRIVER("AVI infoframes updated successfully\n");
453}
454
455void lspcon_set_infoframes(struct intel_encoder *encoder,
456 bool enable,
457 const struct intel_crtc_state *crtc_state,
458 const struct drm_connector_state *conn_state)
459{
460 ssize_t ret;
461 union hdmi_infoframe frame;
462 u8 buf[VIDEO_DIP_DATA_SIZE];
463 struct intel_digital_port *dig_port = enc_to_dig_port(&encoder->base);
464 struct intel_lspcon *lspcon = &dig_port->lspcon;
465 const struct drm_display_mode *adjusted_mode =
466 &crtc_state->base.adjusted_mode;
467
468 if (!lspcon->active) {
469 DRM_ERROR("Writing infoframes while LSPCON disabled ?\n");
470 return;
471 }
472
473 ret = drm_hdmi_avi_infoframe_from_display_mode(&frame.avi,
474 conn_state->connector,
475 adjusted_mode);
476 if (ret < 0) {
477 DRM_ERROR("couldn't fill AVI infoframe\n");
478 return;
479 }
480
481 if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR444) {
482 if (crtc_state->lspcon_downsampling)
483 frame.avi.colorspace = HDMI_COLORSPACE_YUV420;
484 else
485 frame.avi.colorspace = HDMI_COLORSPACE_YUV444;
486 } else {
487 frame.avi.colorspace = HDMI_COLORSPACE_RGB;
488 }
489
490 drm_hdmi_avi_infoframe_quant_range(&frame.avi,
491 conn_state->connector,
492 adjusted_mode,
493 crtc_state->limited_color_range ?
494 HDMI_QUANTIZATION_RANGE_LIMITED :
495 HDMI_QUANTIZATION_RANGE_FULL);
496
497 ret = hdmi_infoframe_pack(&frame, buf, sizeof(buf));
498 if (ret < 0) {
499 DRM_ERROR("Failed to pack AVI IF\n");
500 return;
501 }
502
503 dig_port->write_infoframe(encoder, crtc_state, HDMI_INFOFRAME_TYPE_AVI,
504 buf, ret);
505}
506
507bool lspcon_infoframe_enabled(struct intel_encoder *encoder,
508 const struct intel_crtc_state *pipe_config)
509{
510 return enc_to_intel_lspcon(&encoder->base)->active;
511}
512
513void lspcon_resume(struct intel_lspcon *lspcon)
514{
515 enum drm_lspcon_mode expected_mode;
516
517 if (lspcon_wake_native_aux_ch(lspcon)) {
518 expected_mode = DRM_LSPCON_MODE_PCON;
519 lspcon_resume_in_pcon_wa(lspcon);
520 } else {
521 expected_mode = DRM_LSPCON_MODE_LS;
522 }
523
524 if (lspcon_wait_mode(lspcon, expected_mode) == DRM_LSPCON_MODE_PCON)
525 return;
526
527 if (lspcon_change_mode(lspcon, DRM_LSPCON_MODE_PCON))
528 DRM_ERROR("LSPCON resume failed\n");
529 else
530 DRM_DEBUG_KMS("LSPCON resume success\n");
531}
532
533void lspcon_wait_pcon_mode(struct intel_lspcon *lspcon)
534{
535 lspcon_wait_mode(lspcon, DRM_LSPCON_MODE_PCON);
536}
537
538bool lspcon_init(struct intel_digital_port *intel_dig_port)
539{
540 struct intel_dp *dp = &intel_dig_port->dp;
541 struct intel_lspcon *lspcon = &intel_dig_port->lspcon;
542 struct drm_device *dev = intel_dig_port->base.base.dev;
543 struct drm_i915_private *dev_priv = to_i915(dev);
544 struct drm_connector *connector = &dp->attached_connector->base;
545
546 if (!HAS_LSPCON(dev_priv)) {
547 DRM_ERROR("LSPCON is not supported on this platform\n");
548 return false;
549 }
550
551 lspcon->active = false;
552 lspcon->mode = DRM_LSPCON_MODE_INVALID;
553
554 if (!lspcon_probe(lspcon)) {
555 DRM_ERROR("Failed to probe lspcon\n");
556 return false;
557 }
558
559 if (!intel_dp_read_dpcd(dp)) {
560 DRM_ERROR("LSPCON DPCD read failed\n");
561 return false;
562 }
563
564 if (!lspcon_detect_vendor(lspcon)) {
565 DRM_ERROR("LSPCON vendor detection failed\n");
566 return false;
567 }
568
569 connector->ycbcr_420_allowed = true;
570 lspcon->active = true;
571 DRM_DEBUG_KMS("Success: LSPCON init\n");
572 return true;
573}
574