1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (c) 2012-2020, The Linux Foundation. All rights reserved.
4 */
5
6#define pr_fmt(fmt)	"[drm-dp] %s: " fmt, __func__
7
8#include <drm/drm_print.h>
9
10#include "dp_reg.h"
11#include "dp_link.h"
12#include "dp_panel.h"
13
14#define DP_TEST_REQUEST_MASK		0x7F
15
16enum audio_sample_rate {
17	AUDIO_SAMPLE_RATE_32_KHZ	= 0x00,
18	AUDIO_SAMPLE_RATE_44_1_KHZ	= 0x01,
19	AUDIO_SAMPLE_RATE_48_KHZ	= 0x02,
20	AUDIO_SAMPLE_RATE_88_2_KHZ	= 0x03,
21	AUDIO_SAMPLE_RATE_96_KHZ	= 0x04,
22	AUDIO_SAMPLE_RATE_176_4_KHZ	= 0x05,
23	AUDIO_SAMPLE_RATE_192_KHZ	= 0x06,
24};
25
26enum audio_pattern_type {
27	AUDIO_TEST_PATTERN_OPERATOR_DEFINED	= 0x00,
28	AUDIO_TEST_PATTERN_SAWTOOTH		= 0x01,
29};
30
31struct dp_link_request {
32	u32 test_requested;
33	u32 test_link_rate;
34	u32 test_lane_count;
35};
36
37struct dp_link_private {
38	u32 prev_sink_count;
39	struct device *dev;
40	struct drm_device *drm_dev;
41	struct drm_dp_aux *aux;
42	struct dp_link dp_link;
43
44	struct dp_link_request request;
45	struct mutex psm_mutex;
46	u8 link_status[DP_LINK_STATUS_SIZE];
47};
48
49static int dp_aux_link_power_up(struct drm_dp_aux *aux,
50					struct dp_link_info *link)
51{
52	u8 value;
53	ssize_t len;
54	int i;
55
56	if (link->revision < 0x11)
57		return 0;
58
59	len = drm_dp_dpcd_readb(aux, DP_SET_POWER, &value);
60	if (len < 0)
61		return len;
62
63	value &= ~DP_SET_POWER_MASK;
64	value |= DP_SET_POWER_D0;
65
66	/* retry for 1ms to give the sink time to wake up */
67	for (i = 0; i < 3; i++) {
68		len = drm_dp_dpcd_writeb(aux, DP_SET_POWER, value);
69		usleep_range(1000, 2000);
70		if (len == 1)
71			break;
72	}
73
74	return 0;
75}
76
77static int dp_aux_link_power_down(struct drm_dp_aux *aux,
78					struct dp_link_info *link)
79{
80	u8 value;
81	int err;
82
83	if (link->revision < 0x11)
84		return 0;
85
86	err = drm_dp_dpcd_readb(aux, DP_SET_POWER, &value);
87	if (err < 0)
88		return err;
89
90	value &= ~DP_SET_POWER_MASK;
91	value |= DP_SET_POWER_D3;
92
93	err = drm_dp_dpcd_writeb(aux, DP_SET_POWER, value);
94	if (err < 0)
95		return err;
96
97	return 0;
98}
99
100static int dp_link_get_period(struct dp_link_private *link, int const addr)
101{
102	int ret = 0;
103	u8 data;
104	u32 const max_audio_period = 0xA;
105
106	/* TEST_AUDIO_PERIOD_CH_XX */
107	if (drm_dp_dpcd_readb(link->aux, addr, &data) < 0) {
108		DRM_ERROR("failed to read test_audio_period (0x%x)\n", addr);
109		ret = -EINVAL;
110		goto exit;
111	}
112
113	/* Period - Bits 3:0 */
114	data = data & 0xF;
115	if ((int)data > max_audio_period) {
116		DRM_ERROR("invalid test_audio_period_ch_1 = 0x%x\n", data);
117		ret = -EINVAL;
118		goto exit;
119	}
120
121	ret = data;
122exit:
123	return ret;
124}
125
126static int dp_link_parse_audio_channel_period(struct dp_link_private *link)
127{
128	int ret = 0;
129	struct dp_link_test_audio *req = &link->dp_link.test_audio;
130
131	ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH1);
132	if (ret == -EINVAL)
133		goto exit;
134
135	req->test_audio_period_ch_1 = ret;
136	drm_dbg_dp(link->drm_dev, "test_audio_period_ch_1 = 0x%x\n", ret);
137
138	ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH2);
139	if (ret == -EINVAL)
140		goto exit;
141
142	req->test_audio_period_ch_2 = ret;
143	drm_dbg_dp(link->drm_dev, "test_audio_period_ch_2 = 0x%x\n", ret);
144
145	/* TEST_AUDIO_PERIOD_CH_3 (Byte 0x275) */
146	ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH3);
147	if (ret == -EINVAL)
148		goto exit;
149
150	req->test_audio_period_ch_3 = ret;
151	drm_dbg_dp(link->drm_dev, "test_audio_period_ch_3 = 0x%x\n", ret);
152
153	ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH4);
154	if (ret == -EINVAL)
155		goto exit;
156
157	req->test_audio_period_ch_4 = ret;
158	drm_dbg_dp(link->drm_dev, "test_audio_period_ch_4 = 0x%x\n", ret);
159
160	ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH5);
161	if (ret == -EINVAL)
162		goto exit;
163
164	req->test_audio_period_ch_5 = ret;
165	drm_dbg_dp(link->drm_dev, "test_audio_period_ch_5 = 0x%x\n", ret);
166
167	ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH6);
168	if (ret == -EINVAL)
169		goto exit;
170
171	req->test_audio_period_ch_6 = ret;
172	drm_dbg_dp(link->drm_dev, "test_audio_period_ch_6 = 0x%x\n", ret);
173
174	ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH7);
175	if (ret == -EINVAL)
176		goto exit;
177
178	req->test_audio_period_ch_7 = ret;
179	drm_dbg_dp(link->drm_dev, "test_audio_period_ch_7 = 0x%x\n", ret);
180
181	ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH8);
182	if (ret == -EINVAL)
183		goto exit;
184
185	req->test_audio_period_ch_8 = ret;
186	drm_dbg_dp(link->drm_dev, "test_audio_period_ch_8 = 0x%x\n", ret);
187exit:
188	return ret;
189}
190
191static int dp_link_parse_audio_pattern_type(struct dp_link_private *link)
192{
193	int ret = 0;
194	u8 data;
195	ssize_t rlen;
196	int const max_audio_pattern_type = 0x1;
197
198	rlen = drm_dp_dpcd_readb(link->aux,
199				DP_TEST_AUDIO_PATTERN_TYPE, &data);
200	if (rlen < 0) {
201		DRM_ERROR("failed to read link audio mode. rlen=%zd\n", rlen);
202		return rlen;
203	}
204
205	/* Audio Pattern Type - Bits 7:0 */
206	if ((int)data > max_audio_pattern_type) {
207		DRM_ERROR("invalid audio pattern type = 0x%x\n", data);
208		ret = -EINVAL;
209		goto exit;
210	}
211
212	link->dp_link.test_audio.test_audio_pattern_type = data;
213	drm_dbg_dp(link->drm_dev, "audio pattern type = 0x%x\n", data);
214exit:
215	return ret;
216}
217
218static int dp_link_parse_audio_mode(struct dp_link_private *link)
219{
220	int ret = 0;
221	u8 data;
222	ssize_t rlen;
223	int const max_audio_sampling_rate = 0x6;
224	int const max_audio_channel_count = 0x8;
225	int sampling_rate = 0x0;
226	int channel_count = 0x0;
227
228	rlen = drm_dp_dpcd_readb(link->aux, DP_TEST_AUDIO_MODE, &data);
229	if (rlen < 0) {
230		DRM_ERROR("failed to read link audio mode. rlen=%zd\n", rlen);
231		return rlen;
232	}
233
234	/* Sampling Rate - Bits 3:0 */
235	sampling_rate = data & 0xF;
236	if (sampling_rate > max_audio_sampling_rate) {
237		DRM_ERROR("sampling rate (0x%x) greater than max (0x%x)\n",
238				sampling_rate, max_audio_sampling_rate);
239		ret = -EINVAL;
240		goto exit;
241	}
242
243	/* Channel Count - Bits 7:4 */
244	channel_count = ((data & 0xF0) >> 4) + 1;
245	if (channel_count > max_audio_channel_count) {
246		DRM_ERROR("channel_count (0x%x) greater than max (0x%x)\n",
247				channel_count, max_audio_channel_count);
248		ret = -EINVAL;
249		goto exit;
250	}
251
252	link->dp_link.test_audio.test_audio_sampling_rate = sampling_rate;
253	link->dp_link.test_audio.test_audio_channel_count = channel_count;
254	drm_dbg_dp(link->drm_dev,
255			"sampling_rate = 0x%x, channel_count = 0x%x\n",
256			sampling_rate, channel_count);
257exit:
258	return ret;
259}
260
261static int dp_link_parse_audio_pattern_params(struct dp_link_private *link)
262{
263	int ret = 0;
264
265	ret = dp_link_parse_audio_mode(link);
266	if (ret)
267		goto exit;
268
269	ret = dp_link_parse_audio_pattern_type(link);
270	if (ret)
271		goto exit;
272
273	ret = dp_link_parse_audio_channel_period(link);
274
275exit:
276	return ret;
277}
278
279static bool dp_link_is_video_pattern_valid(u32 pattern)
280{
281	switch (pattern) {
282	case DP_NO_TEST_PATTERN:
283	case DP_COLOR_RAMP:
284	case DP_BLACK_AND_WHITE_VERTICAL_LINES:
285	case DP_COLOR_SQUARE:
286		return true;
287	default:
288		return false;
289	}
290}
291
292/**
293 * dp_link_is_bit_depth_valid() - validates the bit depth requested
294 * @tbd: bit depth requested by the sink
295 *
296 * Returns true if the requested bit depth is supported.
297 */
298static bool dp_link_is_bit_depth_valid(u32 tbd)
299{
300	/* DP_TEST_VIDEO_PATTERN_NONE is treated as invalid */
301	switch (tbd) {
302	case DP_TEST_BIT_DEPTH_6:
303	case DP_TEST_BIT_DEPTH_8:
304	case DP_TEST_BIT_DEPTH_10:
305		return true;
306	default:
307		return false;
308	}
309}
310
311static int dp_link_parse_timing_params1(struct dp_link_private *link,
312					int addr, int len, u32 *val)
313{
314	u8 bp[2];
315	int rlen;
316
317	if (len != 2)
318		return -EINVAL;
319
320	/* Read the requested video link pattern (Byte 0x221). */
321	rlen = drm_dp_dpcd_read(link->aux, addr, bp, len);
322	if (rlen < len) {
323		DRM_ERROR("failed to read 0x%x\n", addr);
324		return -EINVAL;
325	}
326
327	*val = bp[1] | (bp[0] << 8);
328
329	return 0;
330}
331
332static int dp_link_parse_timing_params2(struct dp_link_private *link,
333					int addr, int len,
334					u32 *val1, u32 *val2)
335{
336	u8 bp[2];
337	int rlen;
338
339	if (len != 2)
340		return -EINVAL;
341
342	/* Read the requested video link pattern (Byte 0x221). */
343	rlen = drm_dp_dpcd_read(link->aux, addr, bp, len);
344	if (rlen < len) {
345		DRM_ERROR("failed to read 0x%x\n", addr);
346		return -EINVAL;
347	}
348
349	*val1 = (bp[0] & BIT(7)) >> 7;
350	*val2 = bp[1] | ((bp[0] & 0x7F) << 8);
351
352	return 0;
353}
354
355static int dp_link_parse_timing_params3(struct dp_link_private *link,
356					int addr, u32 *val)
357{
358	u8 bp;
359	u32 len = 1;
360	int rlen;
361
362	rlen = drm_dp_dpcd_read(link->aux, addr, &bp, len);
363	if (rlen < 1) {
364		DRM_ERROR("failed to read 0x%x\n", addr);
365		return -EINVAL;
366	}
367	*val = bp;
368
369	return 0;
370}
371
372/**
373 * dp_link_parse_video_pattern_params() - parses video pattern parameters from DPCD
374 * @link: Display Port Driver data
375 *
376 * Returns 0 if it successfully parses the video link pattern and the link
377 * bit depth requested by the sink and, and if the values parsed are valid.
378 */
379static int dp_link_parse_video_pattern_params(struct dp_link_private *link)
380{
381	int ret = 0;
382	ssize_t rlen;
383	u8 bp;
384
385	rlen = drm_dp_dpcd_readb(link->aux, DP_TEST_PATTERN, &bp);
386	if (rlen < 0) {
387		DRM_ERROR("failed to read link video pattern. rlen=%zd\n",
388			rlen);
389		return rlen;
390	}
391
392	if (!dp_link_is_video_pattern_valid(bp)) {
393		DRM_ERROR("invalid link video pattern = 0x%x\n", bp);
394		ret = -EINVAL;
395		return ret;
396	}
397
398	link->dp_link.test_video.test_video_pattern = bp;
399
400	/* Read the requested color bit depth and dynamic range (Byte 0x232) */
401	rlen = drm_dp_dpcd_readb(link->aux, DP_TEST_MISC0, &bp);
402	if (rlen < 0) {
403		DRM_ERROR("failed to read link bit depth. rlen=%zd\n", rlen);
404		return rlen;
405	}
406
407	/* Dynamic Range */
408	link->dp_link.test_video.test_dyn_range =
409			(bp & DP_TEST_DYNAMIC_RANGE_CEA);
410
411	/* Color bit depth */
412	bp &= DP_TEST_BIT_DEPTH_MASK;
413	if (!dp_link_is_bit_depth_valid(bp)) {
414		DRM_ERROR("invalid link bit depth = 0x%x\n", bp);
415		ret = -EINVAL;
416		return ret;
417	}
418
419	link->dp_link.test_video.test_bit_depth = bp;
420
421	/* resolution timing params */
422	ret = dp_link_parse_timing_params1(link, DP_TEST_H_TOTAL_HI, 2,
423			&link->dp_link.test_video.test_h_total);
424	if (ret) {
425		DRM_ERROR("failed to parse test_htotal(DP_TEST_H_TOTAL_HI)\n");
426		return ret;
427	}
428
429	ret = dp_link_parse_timing_params1(link, DP_TEST_V_TOTAL_HI, 2,
430			&link->dp_link.test_video.test_v_total);
431	if (ret) {
432		DRM_ERROR("failed to parse test_v_total(DP_TEST_V_TOTAL_HI)\n");
433		return ret;
434	}
435
436	ret = dp_link_parse_timing_params1(link, DP_TEST_H_START_HI, 2,
437			&link->dp_link.test_video.test_h_start);
438	if (ret) {
439		DRM_ERROR("failed to parse test_h_start(DP_TEST_H_START_HI)\n");
440		return ret;
441	}
442
443	ret = dp_link_parse_timing_params1(link, DP_TEST_V_START_HI, 2,
444			&link->dp_link.test_video.test_v_start);
445	if (ret) {
446		DRM_ERROR("failed to parse test_v_start(DP_TEST_V_START_HI)\n");
447		return ret;
448	}
449
450	ret = dp_link_parse_timing_params2(link, DP_TEST_HSYNC_HI, 2,
451			&link->dp_link.test_video.test_hsync_pol,
452			&link->dp_link.test_video.test_hsync_width);
453	if (ret) {
454		DRM_ERROR("failed to parse (DP_TEST_HSYNC_HI)\n");
455		return ret;
456	}
457
458	ret = dp_link_parse_timing_params2(link, DP_TEST_VSYNC_HI, 2,
459			&link->dp_link.test_video.test_vsync_pol,
460			&link->dp_link.test_video.test_vsync_width);
461	if (ret) {
462		DRM_ERROR("failed to parse (DP_TEST_VSYNC_HI)\n");
463		return ret;
464	}
465
466	ret = dp_link_parse_timing_params1(link, DP_TEST_H_WIDTH_HI, 2,
467			&link->dp_link.test_video.test_h_width);
468	if (ret) {
469		DRM_ERROR("failed to parse test_h_width(DP_TEST_H_WIDTH_HI)\n");
470		return ret;
471	}
472
473	ret = dp_link_parse_timing_params1(link, DP_TEST_V_HEIGHT_HI, 2,
474			&link->dp_link.test_video.test_v_height);
475	if (ret) {
476		DRM_ERROR("failed to parse test_v_height\n");
477		return ret;
478	}
479
480	ret = dp_link_parse_timing_params3(link, DP_TEST_MISC1,
481		&link->dp_link.test_video.test_rr_d);
482	link->dp_link.test_video.test_rr_d &= DP_TEST_REFRESH_DENOMINATOR;
483	if (ret) {
484		DRM_ERROR("failed to parse test_rr_d (DP_TEST_MISC1)\n");
485		return ret;
486	}
487
488	ret = dp_link_parse_timing_params3(link, DP_TEST_REFRESH_RATE_NUMERATOR,
489		&link->dp_link.test_video.test_rr_n);
490	if (ret) {
491		DRM_ERROR("failed to parse test_rr_n\n");
492		return ret;
493	}
494
495	drm_dbg_dp(link->drm_dev,
496		"link video pattern = 0x%x\n"
497		"link dynamic range = 0x%x\n"
498		"link bit depth = 0x%x\n"
499		"TEST_H_TOTAL = %d, TEST_V_TOTAL = %d\n"
500		"TEST_H_START = %d, TEST_V_START = %d\n"
501		"TEST_HSYNC_POL = %d\n"
502		"TEST_HSYNC_WIDTH = %d\n"
503		"TEST_VSYNC_POL = %d\n"
504		"TEST_VSYNC_WIDTH = %d\n"
505		"TEST_H_WIDTH = %d\n"
506		"TEST_V_HEIGHT = %d\n"
507		"TEST_REFRESH_DENOMINATOR = %d\n"
508		 "TEST_REFRESH_NUMERATOR = %d\n",
509		link->dp_link.test_video.test_video_pattern,
510		link->dp_link.test_video.test_dyn_range,
511		link->dp_link.test_video.test_bit_depth,
512		link->dp_link.test_video.test_h_total,
513		link->dp_link.test_video.test_v_total,
514		link->dp_link.test_video.test_h_start,
515		link->dp_link.test_video.test_v_start,
516		link->dp_link.test_video.test_hsync_pol,
517		link->dp_link.test_video.test_hsync_width,
518		link->dp_link.test_video.test_vsync_pol,
519		link->dp_link.test_video.test_vsync_width,
520		link->dp_link.test_video.test_h_width,
521		link->dp_link.test_video.test_v_height,
522		link->dp_link.test_video.test_rr_d,
523		link->dp_link.test_video.test_rr_n);
524
525	return ret;
526}
527
528/**
529 * dp_link_parse_link_training_params() - parses link training parameters from
530 * DPCD
531 * @link: Display Port Driver data
532 *
533 * Returns 0 if it successfully parses the link rate (Byte 0x219) and lane
534 * count (Byte 0x220), and if these values parse are valid.
535 */
536static int dp_link_parse_link_training_params(struct dp_link_private *link)
537{
538	u8 bp;
539	ssize_t rlen;
540
541	rlen = drm_dp_dpcd_readb(link->aux, DP_TEST_LINK_RATE,	&bp);
542	if (rlen < 0) {
543		DRM_ERROR("failed to read link rate. rlen=%zd\n", rlen);
544		return rlen;
545	}
546
547	if (!is_link_rate_valid(bp)) {
548		DRM_ERROR("invalid link rate = 0x%x\n", bp);
549		return -EINVAL;
550	}
551
552	link->request.test_link_rate = bp;
553	drm_dbg_dp(link->drm_dev, "link rate = 0x%x\n",
554				link->request.test_link_rate);
555
556	rlen = drm_dp_dpcd_readb(link->aux, DP_TEST_LANE_COUNT, &bp);
557	if (rlen < 0) {
558		DRM_ERROR("failed to read lane count. rlen=%zd\n", rlen);
559		return rlen;
560	}
561	bp &= DP_MAX_LANE_COUNT_MASK;
562
563	if (!is_lane_count_valid(bp)) {
564		DRM_ERROR("invalid lane count = 0x%x\n", bp);
565		return -EINVAL;
566	}
567
568	link->request.test_lane_count = bp;
569	drm_dbg_dp(link->drm_dev, "lane count = 0x%x\n",
570				link->request.test_lane_count);
571	return 0;
572}
573
574/**
575 * dp_link_parse_phy_test_params() - parses the phy link parameters
576 * @link: Display Port Driver data
577 *
578 * Parses the DPCD (Byte 0x248) for the DP PHY link pattern that is being
579 * requested.
580 */
581static int dp_link_parse_phy_test_params(struct dp_link_private *link)
582{
583	u8 data;
584	ssize_t rlen;
585
586	rlen = drm_dp_dpcd_readb(link->aux, DP_PHY_TEST_PATTERN,
587					&data);
588	if (rlen < 0) {
589		DRM_ERROR("failed to read phy link pattern. rlen=%zd\n", rlen);
590		return rlen;
591	}
592
593	link->dp_link.phy_params.phy_test_pattern_sel = data & 0x07;
594
595	drm_dbg_dp(link->drm_dev, "phy_test_pattern_sel = 0x%x\n", data);
596
597	switch (data) {
598	case DP_PHY_TEST_PATTERN_SEL_MASK:
599	case DP_PHY_TEST_PATTERN_NONE:
600	case DP_PHY_TEST_PATTERN_D10_2:
601	case DP_PHY_TEST_PATTERN_ERROR_COUNT:
602	case DP_PHY_TEST_PATTERN_PRBS7:
603	case DP_PHY_TEST_PATTERN_80BIT_CUSTOM:
604	case DP_PHY_TEST_PATTERN_CP2520:
605		return 0;
606	default:
607		return -EINVAL;
608	}
609}
610
611/**
612 * dp_link_is_video_audio_test_requested() - checks for audio/video link request
613 * @link: link requested by the sink
614 *
615 * Returns true if the requested link is a permitted audio/video link.
616 */
617static bool dp_link_is_video_audio_test_requested(u32 link)
618{
619	u8 video_audio_test = (DP_TEST_LINK_VIDEO_PATTERN |
620				DP_TEST_LINK_AUDIO_PATTERN |
621				DP_TEST_LINK_AUDIO_DISABLED_VIDEO);
622
623	return ((link & video_audio_test) &&
624		!(link & ~video_audio_test));
625}
626
627/**
628 * dp_link_parse_request() - parses link request parameters from sink
629 * @link: Display Port Driver data
630 *
631 * Parses the DPCD to check if an automated link is requested (Byte 0x201),
632 * and what type of link automation is being requested (Byte 0x218).
633 */
634static int dp_link_parse_request(struct dp_link_private *link)
635{
636	int ret = 0;
637	u8 data;
638	ssize_t rlen;
639
640	/**
641	 * Read the device service IRQ vector (Byte 0x201) to determine
642	 * whether an automated link has been requested by the sink.
643	 */
644	rlen = drm_dp_dpcd_readb(link->aux,
645				DP_DEVICE_SERVICE_IRQ_VECTOR, &data);
646	if (rlen < 0) {
647		DRM_ERROR("aux read failed. rlen=%zd\n", rlen);
648		return rlen;
649	}
650
651	drm_dbg_dp(link->drm_dev, "device service irq vector = 0x%x\n", data);
652
653	if (!(data & DP_AUTOMATED_TEST_REQUEST)) {
654		drm_dbg_dp(link->drm_dev, "no test requested\n");
655		return 0;
656	}
657
658	/**
659	 * Read the link request byte (Byte 0x218) to determine what type
660	 * of automated link has been requested by the sink.
661	 */
662	rlen = drm_dp_dpcd_readb(link->aux, DP_TEST_REQUEST, &data);
663	if (rlen < 0) {
664		DRM_ERROR("aux read failed. rlen=%zd\n", rlen);
665		return rlen;
666	}
667
668	if (!data || (data == DP_TEST_LINK_FAUX_PATTERN)) {
669		drm_dbg_dp(link->drm_dev, "link 0x%x not supported\n", data);
670		goto end;
671	}
672
673	drm_dbg_dp(link->drm_dev, "Test:(0x%x) requested\n", data);
674	link->request.test_requested = data;
675	if (link->request.test_requested == DP_TEST_LINK_PHY_TEST_PATTERN) {
676		ret = dp_link_parse_phy_test_params(link);
677		if (ret)
678			goto end;
679		ret = dp_link_parse_link_training_params(link);
680		if (ret)
681			goto end;
682	}
683
684	if (link->request.test_requested == DP_TEST_LINK_TRAINING) {
685		ret = dp_link_parse_link_training_params(link);
686		if (ret)
687			goto end;
688	}
689
690	if (dp_link_is_video_audio_test_requested(
691			link->request.test_requested)) {
692		ret = dp_link_parse_video_pattern_params(link);
693		if (ret)
694			goto end;
695
696		ret = dp_link_parse_audio_pattern_params(link);
697	}
698end:
699	/*
700	 * Send a DP_TEST_ACK if all link parameters are valid, otherwise send
701	 * a DP_TEST_NAK.
702	 */
703	if (ret) {
704		link->dp_link.test_response = DP_TEST_NAK;
705	} else {
706		if (link->request.test_requested != DP_TEST_LINK_EDID_READ)
707			link->dp_link.test_response = DP_TEST_ACK;
708		else
709			link->dp_link.test_response =
710				DP_TEST_EDID_CHECKSUM_WRITE;
711	}
712
713	return ret;
714}
715
716/**
717 * dp_link_parse_sink_count() - parses the sink count
718 * @dp_link: pointer to link module data
719 *
720 * Parses the DPCD to check if there is an update to the sink count
721 * (Byte 0x200), and whether all the sink devices connected have Content
722 * Protection enabled.
723 */
724static int dp_link_parse_sink_count(struct dp_link *dp_link)
725{
726	ssize_t rlen;
727	bool cp_ready;
728
729	struct dp_link_private *link = container_of(dp_link,
730			struct dp_link_private, dp_link);
731
732	rlen = drm_dp_dpcd_readb(link->aux, DP_SINK_COUNT,
733				 &link->dp_link.sink_count);
734	if (rlen < 0) {
735		DRM_ERROR("sink count read failed. rlen=%zd\n", rlen);
736		return rlen;
737	}
738
739	cp_ready = link->dp_link.sink_count & DP_SINK_CP_READY;
740
741	link->dp_link.sink_count =
742		DP_GET_SINK_COUNT(link->dp_link.sink_count);
743
744	drm_dbg_dp(link->drm_dev, "sink_count = 0x%x, cp_ready = 0x%x\n",
745				link->dp_link.sink_count, cp_ready);
746	return 0;
747}
748
749static int dp_link_parse_sink_status_field(struct dp_link_private *link)
750{
751	int len = 0;
752
753	link->prev_sink_count = link->dp_link.sink_count;
754	len = dp_link_parse_sink_count(&link->dp_link);
755	if (len < 0) {
756		DRM_ERROR("DP parse sink count failed\n");
757		return len;
758	}
759
760	len = drm_dp_dpcd_read_link_status(link->aux,
761		link->link_status);
762	if (len < DP_LINK_STATUS_SIZE) {
763		DRM_ERROR("DP link status read failed\n");
764		return len;
765	}
766
767	return dp_link_parse_request(link);
768}
769
770/**
771 * dp_link_process_link_training_request() - processes new training requests
772 * @link: Display Port link data
773 *
774 * This function will handle new link training requests that are initiated by
775 * the sink. In particular, it will update the requested lane count and link
776 * rate, and then trigger the link retraining procedure.
777 *
778 * The function will return 0 if a link training request has been processed,
779 * otherwise it will return -EINVAL.
780 */
781static int dp_link_process_link_training_request(struct dp_link_private *link)
782{
783	if (link->request.test_requested != DP_TEST_LINK_TRAINING)
784		return -EINVAL;
785
786	drm_dbg_dp(link->drm_dev,
787			"Test:0x%x link rate = 0x%x, lane count = 0x%x\n",
788			DP_TEST_LINK_TRAINING,
789			link->request.test_link_rate,
790			link->request.test_lane_count);
791
792	link->dp_link.link_params.num_lanes = link->request.test_lane_count;
793	link->dp_link.link_params.rate =
794		drm_dp_bw_code_to_link_rate(link->request.test_link_rate);
795
796	return 0;
797}
798
799bool dp_link_send_test_response(struct dp_link *dp_link)
800{
801	struct dp_link_private *link = NULL;
802	int ret = 0;
803
804	if (!dp_link) {
805		DRM_ERROR("invalid input\n");
806		return false;
807	}
808
809	link = container_of(dp_link, struct dp_link_private, dp_link);
810
811	ret = drm_dp_dpcd_writeb(link->aux, DP_TEST_RESPONSE,
812			dp_link->test_response);
813
814	return ret == 1;
815}
816
817int dp_link_psm_config(struct dp_link *dp_link,
818			      struct dp_link_info *link_info, bool enable)
819{
820	struct dp_link_private *link = NULL;
821	int ret = 0;
822
823	if (!dp_link) {
824		DRM_ERROR("invalid params\n");
825		return -EINVAL;
826	}
827
828	link = container_of(dp_link, struct dp_link_private, dp_link);
829
830	mutex_lock(&link->psm_mutex);
831	if (enable)
832		ret = dp_aux_link_power_down(link->aux, link_info);
833	else
834		ret = dp_aux_link_power_up(link->aux, link_info);
835
836	if (ret)
837		DRM_ERROR("Failed to %s low power mode\n", enable ?
838							"enter" : "exit");
839	else
840		dp_link->psm_enabled = enable;
841
842	mutex_unlock(&link->psm_mutex);
843	return ret;
844}
845
846bool dp_link_send_edid_checksum(struct dp_link *dp_link, u8 checksum)
847{
848	struct dp_link_private *link = NULL;
849	int ret = 0;
850
851	if (!dp_link) {
852		DRM_ERROR("invalid input\n");
853		return false;
854	}
855
856	link = container_of(dp_link, struct dp_link_private, dp_link);
857
858	ret = drm_dp_dpcd_writeb(link->aux, DP_TEST_EDID_CHECKSUM,
859						checksum);
860	return ret == 1;
861}
862
863static void dp_link_parse_vx_px(struct dp_link_private *link)
864{
865	drm_dbg_dp(link->drm_dev, "vx: 0=%d, 1=%d, 2=%d, 3=%d\n",
866		drm_dp_get_adjust_request_voltage(link->link_status, 0),
867		drm_dp_get_adjust_request_voltage(link->link_status, 1),
868		drm_dp_get_adjust_request_voltage(link->link_status, 2),
869		drm_dp_get_adjust_request_voltage(link->link_status, 3));
870
871	drm_dbg_dp(link->drm_dev, "px: 0=%d, 1=%d, 2=%d, 3=%d\n",
872		drm_dp_get_adjust_request_pre_emphasis(link->link_status, 0),
873		drm_dp_get_adjust_request_pre_emphasis(link->link_status, 1),
874		drm_dp_get_adjust_request_pre_emphasis(link->link_status, 2),
875		drm_dp_get_adjust_request_pre_emphasis(link->link_status, 3));
876
877	/**
878	 * Update the voltage and pre-emphasis levels as per DPCD request
879	 * vector.
880	 */
881	drm_dbg_dp(link->drm_dev,
882			 "Current: v_level = 0x%x, p_level = 0x%x\n",
883			link->dp_link.phy_params.v_level,
884			link->dp_link.phy_params.p_level);
885	link->dp_link.phy_params.v_level =
886		drm_dp_get_adjust_request_voltage(link->link_status, 0);
887	link->dp_link.phy_params.p_level =
888		drm_dp_get_adjust_request_pre_emphasis(link->link_status, 0);
889
890	link->dp_link.phy_params.p_level >>= DP_TRAIN_PRE_EMPHASIS_SHIFT;
891
892	drm_dbg_dp(link->drm_dev,
893			"Requested: v_level = 0x%x, p_level = 0x%x\n",
894			link->dp_link.phy_params.v_level,
895			link->dp_link.phy_params.p_level);
896}
897
898/**
899 * dp_link_process_phy_test_pattern_request() - process new phy link requests
900 * @link: Display Port Driver data
901 *
902 * This function will handle new phy link pattern requests that are initiated
903 * by the sink. The function will return 0 if a phy link pattern has been
904 * processed, otherwise it will return -EINVAL.
905 */
906static int dp_link_process_phy_test_pattern_request(
907		struct dp_link_private *link)
908{
909	if (!(link->request.test_requested & DP_TEST_LINK_PHY_TEST_PATTERN)) {
910		drm_dbg_dp(link->drm_dev, "no phy test\n");
911		return -EINVAL;
912	}
913
914	if (!is_link_rate_valid(link->request.test_link_rate) ||
915		!is_lane_count_valid(link->request.test_lane_count)) {
916		DRM_ERROR("Invalid: link rate = 0x%x,lane count = 0x%x\n",
917				link->request.test_link_rate,
918				link->request.test_lane_count);
919		return -EINVAL;
920	}
921
922	drm_dbg_dp(link->drm_dev,
923			"Current: rate = 0x%x, lane count = 0x%x\n",
924			link->dp_link.link_params.rate,
925			link->dp_link.link_params.num_lanes);
926
927	drm_dbg_dp(link->drm_dev,
928			"Requested: rate = 0x%x, lane count = 0x%x\n",
929			link->request.test_link_rate,
930			link->request.test_lane_count);
931
932	link->dp_link.link_params.num_lanes = link->request.test_lane_count;
933	link->dp_link.link_params.rate =
934		drm_dp_bw_code_to_link_rate(link->request.test_link_rate);
935
936	dp_link_parse_vx_px(link);
937
938	return 0;
939}
940
941static bool dp_link_read_psr_error_status(struct dp_link_private *link)
942{
943	u8 status;
944
945	drm_dp_dpcd_read(link->aux, DP_PSR_ERROR_STATUS, &status, 1);
946
947	if (status & DP_PSR_LINK_CRC_ERROR)
948		DRM_ERROR("PSR LINK CRC ERROR\n");
949	else if (status & DP_PSR_RFB_STORAGE_ERROR)
950		DRM_ERROR("PSR RFB STORAGE ERROR\n");
951	else if (status & DP_PSR_VSC_SDP_UNCORRECTABLE_ERROR)
952		DRM_ERROR("PSR VSC SDP UNCORRECTABLE ERROR\n");
953	else
954		return false;
955
956	return true;
957}
958
959static bool dp_link_psr_capability_changed(struct dp_link_private *link)
960{
961	u8 status;
962
963	drm_dp_dpcd_read(link->aux, DP_PSR_ESI, &status, 1);
964
965	if (status & DP_PSR_CAPS_CHANGE) {
966		drm_dbg_dp(link->drm_dev, "PSR Capability Change\n");
967		return true;
968	}
969
970	return false;
971}
972
973static u8 get_link_status(const u8 link_status[DP_LINK_STATUS_SIZE], int r)
974{
975	return link_status[r - DP_LANE0_1_STATUS];
976}
977
978/**
979 * dp_link_process_link_status_update() - processes link status updates
980 * @link: Display Port link module data
981 *
982 * This function will check for changes in the link status, e.g. clock
983 * recovery done on all lanes, and trigger link training if there is a
984 * failure/error on the link.
985 *
986 * The function will return 0 if the a link status update has been processed,
987 * otherwise it will return -EINVAL.
988 */
989static int dp_link_process_link_status_update(struct dp_link_private *link)
990{
991	bool channel_eq_done = drm_dp_channel_eq_ok(link->link_status,
992			link->dp_link.link_params.num_lanes);
993
994	bool clock_recovery_done = drm_dp_clock_recovery_ok(link->link_status,
995			link->dp_link.link_params.num_lanes);
996
997	drm_dbg_dp(link->drm_dev,
998		       "channel_eq_done = %d, clock_recovery_done = %d\n",
999                        channel_eq_done, clock_recovery_done);
1000
1001	if (channel_eq_done && clock_recovery_done)
1002		return -EINVAL;
1003
1004	return 0;
1005}
1006
1007/**
1008 * dp_link_process_ds_port_status_change() - process port status changes
1009 * @link: Display Port Driver data
1010 *
1011 * This function will handle downstream port updates that are initiated by
1012 * the sink. If the downstream port status has changed, the EDID is read via
1013 * AUX.
1014 *
1015 * The function will return 0 if a downstream port update has been
1016 * processed, otherwise it will return -EINVAL.
1017 */
1018static int dp_link_process_ds_port_status_change(struct dp_link_private *link)
1019{
1020	if (get_link_status(link->link_status, DP_LANE_ALIGN_STATUS_UPDATED) &
1021					DP_DOWNSTREAM_PORT_STATUS_CHANGED)
1022		goto reset;
1023
1024	if (link->prev_sink_count == link->dp_link.sink_count)
1025		return -EINVAL;
1026
1027reset:
1028	/* reset prev_sink_count */
1029	link->prev_sink_count = link->dp_link.sink_count;
1030
1031	return 0;
1032}
1033
1034static bool dp_link_is_video_pattern_requested(struct dp_link_private *link)
1035{
1036	return (link->request.test_requested & DP_TEST_LINK_VIDEO_PATTERN)
1037		&& !(link->request.test_requested &
1038		DP_TEST_LINK_AUDIO_DISABLED_VIDEO);
1039}
1040
1041static bool dp_link_is_audio_pattern_requested(struct dp_link_private *link)
1042{
1043	return (link->request.test_requested & DP_TEST_LINK_AUDIO_PATTERN);
1044}
1045
1046static void dp_link_reset_data(struct dp_link_private *link)
1047{
1048	link->request = (const struct dp_link_request){ 0 };
1049	link->dp_link.test_video = (const struct dp_link_test_video){ 0 };
1050	link->dp_link.test_video.test_bit_depth = DP_TEST_BIT_DEPTH_UNKNOWN;
1051	link->dp_link.test_audio = (const struct dp_link_test_audio){ 0 };
1052	link->dp_link.phy_params.phy_test_pattern_sel = 0;
1053	link->dp_link.sink_request = 0;
1054	link->dp_link.test_response = 0;
1055}
1056
1057/**
1058 * dp_link_process_request() - handle HPD IRQ transition to HIGH
1059 * @dp_link: pointer to link module data
1060 *
1061 * This function will handle the HPD IRQ state transitions from LOW to HIGH
1062 * (including cases when there are back to back HPD IRQ HIGH) indicating
1063 * the start of a new link training request or sink status update.
1064 */
1065int dp_link_process_request(struct dp_link *dp_link)
1066{
1067	int ret = 0;
1068	struct dp_link_private *link;
1069
1070	if (!dp_link) {
1071		DRM_ERROR("invalid input\n");
1072		return -EINVAL;
1073	}
1074
1075	link = container_of(dp_link, struct dp_link_private, dp_link);
1076
1077	dp_link_reset_data(link);
1078
1079	ret = dp_link_parse_sink_status_field(link);
1080	if (ret)
1081		return ret;
1082
1083	if (link->request.test_requested == DP_TEST_LINK_EDID_READ) {
1084		dp_link->sink_request |= DP_TEST_LINK_EDID_READ;
1085	} else if (!dp_link_process_ds_port_status_change(link)) {
1086		dp_link->sink_request |= DS_PORT_STATUS_CHANGED;
1087	} else if (!dp_link_process_link_training_request(link)) {
1088		dp_link->sink_request |= DP_TEST_LINK_TRAINING;
1089	} else if (!dp_link_process_phy_test_pattern_request(link)) {
1090		dp_link->sink_request |= DP_TEST_LINK_PHY_TEST_PATTERN;
1091	} else if (dp_link_read_psr_error_status(link)) {
1092		DRM_ERROR("PSR IRQ_HPD received\n");
1093	} else if (dp_link_psr_capability_changed(link)) {
1094		drm_dbg_dp(link->drm_dev, "PSR Capability changed\n");
1095	} else {
1096		ret = dp_link_process_link_status_update(link);
1097		if (!ret) {
1098			dp_link->sink_request |= DP_LINK_STATUS_UPDATED;
1099		} else {
1100			if (dp_link_is_video_pattern_requested(link)) {
1101				ret = 0;
1102				dp_link->sink_request |= DP_TEST_LINK_VIDEO_PATTERN;
1103			}
1104			if (dp_link_is_audio_pattern_requested(link)) {
1105				dp_link->sink_request |= DP_TEST_LINK_AUDIO_PATTERN;
1106				ret = -EINVAL;
1107			}
1108		}
1109	}
1110
1111	drm_dbg_dp(link->drm_dev, "sink request=%#x\n",
1112				dp_link->sink_request);
1113	return ret;
1114}
1115
1116int dp_link_get_colorimetry_config(struct dp_link *dp_link)
1117{
1118	u32 cc = DP_MISC0_COLORIMERY_CFG_LEGACY_RGB;
1119	struct dp_link_private *link;
1120
1121	if (!dp_link) {
1122		DRM_ERROR("invalid input\n");
1123		return -EINVAL;
1124	}
1125
1126	link = container_of(dp_link, struct dp_link_private, dp_link);
1127
1128	/*
1129	 * Unless a video pattern CTS test is ongoing, use RGB_VESA
1130	 * Only RGB_VESA and RGB_CEA supported for now
1131	 */
1132	if (dp_link_is_video_pattern_requested(link)) {
1133		if (link->dp_link.test_video.test_dyn_range &
1134					DP_TEST_DYNAMIC_RANGE_CEA)
1135			cc = DP_MISC0_COLORIMERY_CFG_CEA_RGB;
1136	}
1137
1138	return cc;
1139}
1140
1141int dp_link_adjust_levels(struct dp_link *dp_link, u8 *link_status)
1142{
1143	int i;
1144	int v_max = 0, p_max = 0;
1145	struct dp_link_private *link;
1146
1147	if (!dp_link) {
1148		DRM_ERROR("invalid input\n");
1149		return -EINVAL;
1150	}
1151
1152	link = container_of(dp_link, struct dp_link_private, dp_link);
1153
1154	/* use the max level across lanes */
1155	for (i = 0; i < dp_link->link_params.num_lanes; i++) {
1156		u8 data_v = drm_dp_get_adjust_request_voltage(link_status, i);
1157		u8 data_p = drm_dp_get_adjust_request_pre_emphasis(link_status,
1158									 i);
1159		drm_dbg_dp(link->drm_dev,
1160				"lane=%d req_vol_swing=%d req_pre_emphasis=%d\n",
1161				i, data_v, data_p);
1162		if (v_max < data_v)
1163			v_max = data_v;
1164		if (p_max < data_p)
1165			p_max = data_p;
1166	}
1167
1168	dp_link->phy_params.v_level = v_max >> DP_TRAIN_VOLTAGE_SWING_SHIFT;
1169	dp_link->phy_params.p_level = p_max >> DP_TRAIN_PRE_EMPHASIS_SHIFT;
1170
1171	/**
1172	 * Adjust the voltage swing and pre-emphasis level combination to within
1173	 * the allowable range.
1174	 */
1175	if (dp_link->phy_params.v_level > DP_TRAIN_VOLTAGE_SWING_MAX) {
1176		drm_dbg_dp(link->drm_dev,
1177			"Requested vSwingLevel=%d, change to %d\n",
1178			dp_link->phy_params.v_level,
1179			DP_TRAIN_VOLTAGE_SWING_MAX);
1180		dp_link->phy_params.v_level = DP_TRAIN_VOLTAGE_SWING_MAX;
1181	}
1182
1183	if (dp_link->phy_params.p_level > DP_TRAIN_PRE_EMPHASIS_MAX) {
1184		drm_dbg_dp(link->drm_dev,
1185			"Requested preEmphasisLevel=%d, change to %d\n",
1186			dp_link->phy_params.p_level,
1187			DP_TRAIN_PRE_EMPHASIS_MAX);
1188		dp_link->phy_params.p_level = DP_TRAIN_PRE_EMPHASIS_MAX;
1189	}
1190
1191	if ((dp_link->phy_params.p_level > DP_TRAIN_PRE_EMPHASIS_LVL_1)
1192		&& (dp_link->phy_params.v_level ==
1193			DP_TRAIN_VOLTAGE_SWING_LVL_2)) {
1194		drm_dbg_dp(link->drm_dev,
1195			"Requested preEmphasisLevel=%d, change to %d\n",
1196			dp_link->phy_params.p_level,
1197			DP_TRAIN_PRE_EMPHASIS_LVL_1);
1198		dp_link->phy_params.p_level = DP_TRAIN_PRE_EMPHASIS_LVL_1;
1199	}
1200
1201	drm_dbg_dp(link->drm_dev, "adjusted: v_level=%d, p_level=%d\n",
1202		dp_link->phy_params.v_level, dp_link->phy_params.p_level);
1203
1204	return 0;
1205}
1206
1207void dp_link_reset_phy_params_vx_px(struct dp_link *dp_link)
1208{
1209	dp_link->phy_params.v_level = 0;
1210	dp_link->phy_params.p_level = 0;
1211}
1212
1213u32 dp_link_get_test_bits_depth(struct dp_link *dp_link, u32 bpp)
1214{
1215	u32 tbd;
1216	struct dp_link_private *link;
1217
1218	link = container_of(dp_link, struct dp_link_private, dp_link);
1219
1220	/*
1221	 * Few simplistic rules and assumptions made here:
1222	 *    1. Test bit depth is bit depth per color component
1223	 *    2. Assume 3 color components
1224	 */
1225	switch (bpp) {
1226	case 18:
1227		tbd = DP_TEST_BIT_DEPTH_6;
1228		break;
1229	case 24:
1230		tbd = DP_TEST_BIT_DEPTH_8;
1231		break;
1232	case 30:
1233		tbd = DP_TEST_BIT_DEPTH_10;
1234		break;
1235	default:
1236		drm_dbg_dp(link->drm_dev, "bpp=%d not supported, use bpc=8\n",
1237			   bpp);
1238		tbd = DP_TEST_BIT_DEPTH_8;
1239		break;
1240	}
1241
1242	tbd = (tbd >> DP_TEST_BIT_DEPTH_SHIFT);
1243
1244	return tbd;
1245}
1246
1247struct dp_link *dp_link_get(struct device *dev, struct drm_dp_aux *aux)
1248{
1249	struct dp_link_private *link;
1250	struct dp_link *dp_link;
1251
1252	if (!dev || !aux) {
1253		DRM_ERROR("invalid input\n");
1254		return ERR_PTR(-EINVAL);
1255	}
1256
1257	link = devm_kzalloc(dev, sizeof(*link), GFP_KERNEL);
1258	if (!link)
1259		return ERR_PTR(-ENOMEM);
1260
1261	link->dev   = dev;
1262	link->aux   = aux;
1263
1264	mutex_init(&link->psm_mutex);
1265	dp_link = &link->dp_link;
1266
1267	return dp_link;
1268}
1269