1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2012-2020, The Linux Foundation. All rights reserved.
4 */
5
6 #include "dp_panel.h"
7
8 #include <drm/drm_connector.h>
9 #include <drm/drm_edid.h>
10 #include <drm/drm_print.h>
11
12 struct dp_panel_private {
13 struct device *dev;
14 struct dp_panel dp_panel;
15 struct drm_dp_aux *aux;
16 struct dp_link *link;
17 struct dp_catalog *catalog;
18 bool panel_on;
19 bool aux_cfg_update_done;
20 };
21
dp_panel_read_dpcd(struct dp_panel * dp_panel)22 static int dp_panel_read_dpcd(struct dp_panel *dp_panel)
23 {
24 int rc = 0;
25 size_t len;
26 ssize_t rlen;
27 struct dp_panel_private *panel;
28 struct dp_link_info *link_info;
29 u8 *dpcd, major = 0, minor = 0, temp;
30 u32 offset = DP_DPCD_REV;
31
32 dpcd = dp_panel->dpcd;
33
34 panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
35 link_info = &dp_panel->link_info;
36
37 rlen = drm_dp_dpcd_read(panel->aux, offset,
38 dpcd, (DP_RECEIVER_CAP_SIZE + 1));
39 if (rlen < (DP_RECEIVER_CAP_SIZE + 1)) {
40 DRM_ERROR("dpcd read failed, rlen=%zd\n", rlen);
41 if (rlen == -ETIMEDOUT)
42 rc = rlen;
43 else
44 rc = -EINVAL;
45
46 goto end;
47 }
48
49 temp = dpcd[DP_TRAINING_AUX_RD_INTERVAL];
50
51 /* check for EXTENDED_RECEIVER_CAPABILITY_FIELD_PRESENT */
52 if (temp & BIT(7)) {
53 DRM_DEBUG_DP("using EXTENDED_RECEIVER_CAPABILITY_FIELD\n");
54 offset = DPRX_EXTENDED_DPCD_FIELD;
55 }
56
57 rlen = drm_dp_dpcd_read(panel->aux, offset,
58 dpcd, (DP_RECEIVER_CAP_SIZE + 1));
59 if (rlen < (DP_RECEIVER_CAP_SIZE + 1)) {
60 DRM_ERROR("dpcd read failed, rlen=%zd\n", rlen);
61 if (rlen == -ETIMEDOUT)
62 rc = rlen;
63 else
64 rc = -EINVAL;
65
66 goto end;
67 }
68
69 link_info->revision = dpcd[DP_DPCD_REV];
70 major = (link_info->revision >> 4) & 0x0f;
71 minor = link_info->revision & 0x0f;
72
73 link_info->rate = drm_dp_bw_code_to_link_rate(dpcd[DP_MAX_LINK_RATE]);
74 link_info->num_lanes = dpcd[DP_MAX_LANE_COUNT] & DP_MAX_LANE_COUNT_MASK;
75
76 if (link_info->num_lanes > dp_panel->max_dp_lanes)
77 link_info->num_lanes = dp_panel->max_dp_lanes;
78
79 /* Limit support upto HBR2 until HBR3 support is added */
80 if (link_info->rate >= (drm_dp_bw_code_to_link_rate(DP_LINK_BW_5_4)))
81 link_info->rate = drm_dp_bw_code_to_link_rate(DP_LINK_BW_5_4);
82
83 DRM_DEBUG_DP("version: %d.%d\n", major, minor);
84 DRM_DEBUG_DP("link_rate=%d\n", link_info->rate);
85 DRM_DEBUG_DP("lane_count=%d\n", link_info->num_lanes);
86
87 if (drm_dp_enhanced_frame_cap(dpcd))
88 link_info->capabilities |= DP_LINK_CAP_ENHANCED_FRAMING;
89
90 dp_panel->dfp_present = dpcd[DP_DOWNSTREAMPORT_PRESENT];
91 dp_panel->dfp_present &= DP_DWN_STRM_PORT_PRESENT;
92
93 if (dp_panel->dfp_present && (dpcd[DP_DPCD_REV] > 0x10)) {
94 dp_panel->ds_port_cnt = dpcd[DP_DOWN_STREAM_PORT_COUNT];
95 dp_panel->ds_port_cnt &= DP_PORT_COUNT_MASK;
96 len = DP_DOWNSTREAM_PORTS * DP_DOWNSTREAM_CAP_SIZE;
97
98 rlen = drm_dp_dpcd_read(panel->aux,
99 DP_DOWNSTREAM_PORT_0, dp_panel->ds_cap_info, len);
100 if (rlen < len) {
101 DRM_ERROR("ds port status failed, rlen=%zd\n", rlen);
102 rc = -EINVAL;
103 goto end;
104 }
105 }
106
107 end:
108 return rc;
109 }
110
dp_panel_get_supported_bpp(struct dp_panel * dp_panel,u32 mode_edid_bpp,u32 mode_pclk_khz)111 static u32 dp_panel_get_supported_bpp(struct dp_panel *dp_panel,
112 u32 mode_edid_bpp, u32 mode_pclk_khz)
113 {
114 struct dp_link_info *link_info;
115 const u32 max_supported_bpp = 30, min_supported_bpp = 18;
116 u32 bpp = 0, data_rate_khz = 0;
117
118 bpp = min_t(u32, mode_edid_bpp, max_supported_bpp);
119
120 link_info = &dp_panel->link_info;
121 data_rate_khz = link_info->num_lanes * link_info->rate * 8;
122
123 while (bpp > min_supported_bpp) {
124 if (mode_pclk_khz * bpp <= data_rate_khz)
125 break;
126 bpp -= 6;
127 }
128
129 return bpp;
130 }
131
dp_panel_update_modes(struct drm_connector * connector,struct edid * edid)132 static int dp_panel_update_modes(struct drm_connector *connector,
133 struct edid *edid)
134 {
135 int rc = 0;
136
137 if (edid) {
138 rc = drm_connector_update_edid_property(connector, edid);
139 if (rc) {
140 DRM_ERROR("failed to update edid property %d\n", rc);
141 return rc;
142 }
143 rc = drm_add_edid_modes(connector, edid);
144 DRM_DEBUG_DP("%s -", __func__);
145 return rc;
146 }
147
148 rc = drm_connector_update_edid_property(connector, NULL);
149 if (rc)
150 DRM_ERROR("failed to update edid property %d\n", rc);
151
152 return rc;
153 }
154
dp_panel_read_sink_caps(struct dp_panel * dp_panel,struct drm_connector * connector)155 int dp_panel_read_sink_caps(struct dp_panel *dp_panel,
156 struct drm_connector *connector)
157 {
158 int rc = 0, bw_code;
159 int rlen, count;
160 struct dp_panel_private *panel;
161
162 if (!dp_panel || !connector) {
163 DRM_ERROR("invalid input\n");
164 return -EINVAL;
165 }
166
167 panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
168
169 rc = dp_panel_read_dpcd(dp_panel);
170 bw_code = drm_dp_link_rate_to_bw_code(dp_panel->link_info.rate);
171 if (rc || !is_link_rate_valid(bw_code) ||
172 !is_lane_count_valid(dp_panel->link_info.num_lanes) ||
173 (bw_code > dp_panel->max_bw_code)) {
174 DRM_ERROR("read dpcd failed %d\n", rc);
175 return rc;
176 }
177
178 if (dp_panel->dfp_present) {
179 rlen = drm_dp_dpcd_read(panel->aux, DP_SINK_COUNT,
180 &count, 1);
181 if (rlen == 1) {
182 count = DP_GET_SINK_COUNT(count);
183 if (!count) {
184 DRM_ERROR("no downstream ports connected\n");
185 panel->link->sink_count = 0;
186 rc = -ENOTCONN;
187 goto end;
188 }
189 }
190 }
191
192 kfree(dp_panel->edid);
193 dp_panel->edid = NULL;
194
195 dp_panel->edid = drm_get_edid(connector,
196 &panel->aux->ddc);
197 if (!dp_panel->edid) {
198 DRM_ERROR("panel edid read failed\n");
199
200 /* fail safe edid */
201 mutex_lock(&connector->dev->mode_config.mutex);
202 if (drm_add_modes_noedid(connector, 640, 480))
203 drm_set_preferred_mode(connector, 640, 480);
204 mutex_unlock(&connector->dev->mode_config.mutex);
205 }
206
207 if (panel->aux_cfg_update_done) {
208 DRM_DEBUG_DP("read DPCD with updated AUX config\n");
209 rc = dp_panel_read_dpcd(dp_panel);
210 bw_code = drm_dp_link_rate_to_bw_code(dp_panel->link_info.rate);
211 if (rc || !is_link_rate_valid(bw_code) ||
212 !is_lane_count_valid(dp_panel->link_info.num_lanes)
213 || (bw_code > dp_panel->max_bw_code)) {
214 DRM_ERROR("read dpcd failed %d\n", rc);
215 return rc;
216 }
217 panel->aux_cfg_update_done = false;
218 }
219 end:
220 return rc;
221 }
222
dp_panel_get_mode_bpp(struct dp_panel * dp_panel,u32 mode_edid_bpp,u32 mode_pclk_khz)223 u32 dp_panel_get_mode_bpp(struct dp_panel *dp_panel,
224 u32 mode_edid_bpp, u32 mode_pclk_khz)
225 {
226 struct dp_panel_private *panel;
227 u32 bpp = mode_edid_bpp;
228
229 if (!dp_panel || !mode_edid_bpp || !mode_pclk_khz) {
230 DRM_ERROR("invalid input\n");
231 return 0;
232 }
233
234 panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
235
236 if (dp_panel->video_test)
237 bpp = dp_link_bit_depth_to_bpp(
238 panel->link->test_video.test_bit_depth);
239 else
240 bpp = dp_panel_get_supported_bpp(dp_panel, mode_edid_bpp,
241 mode_pclk_khz);
242
243 return bpp;
244 }
245
dp_panel_get_modes(struct dp_panel * dp_panel,struct drm_connector * connector,struct dp_display_mode * mode)246 int dp_panel_get_modes(struct dp_panel *dp_panel,
247 struct drm_connector *connector, struct dp_display_mode *mode)
248 {
249 if (!dp_panel) {
250 DRM_ERROR("invalid input\n");
251 return -EINVAL;
252 }
253
254 if (dp_panel->edid)
255 return dp_panel_update_modes(connector, dp_panel->edid);
256
257 return 0;
258 }
259
dp_panel_get_edid_checksum(struct edid * edid)260 static u8 dp_panel_get_edid_checksum(struct edid *edid)
261 {
262 struct edid *last_block;
263 u8 *raw_edid;
264 bool is_edid_corrupt;
265
266 if (!edid) {
267 DRM_ERROR("invalid edid input\n");
268 return 0;
269 }
270
271 raw_edid = (u8 *)edid;
272 raw_edid += (edid->extensions * EDID_LENGTH);
273 last_block = (struct edid *)raw_edid;
274
275 /* block type extension */
276 drm_edid_block_valid(raw_edid, 1, false, &is_edid_corrupt);
277 if (!is_edid_corrupt)
278 return last_block->checksum;
279
280 DRM_ERROR("Invalid block, no checksum\n");
281 return 0;
282 }
283
dp_panel_handle_sink_request(struct dp_panel * dp_panel)284 void dp_panel_handle_sink_request(struct dp_panel *dp_panel)
285 {
286 struct dp_panel_private *panel;
287
288 if (!dp_panel) {
289 DRM_ERROR("invalid input\n");
290 return;
291 }
292
293 panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
294
295 if (panel->link->sink_request & DP_TEST_LINK_EDID_READ) {
296 u8 checksum = dp_panel_get_edid_checksum(dp_panel->edid);
297
298 dp_link_send_edid_checksum(panel->link, checksum);
299 dp_link_send_test_response(panel->link);
300 }
301 }
302
dp_panel_tpg_config(struct dp_panel * dp_panel,bool enable)303 void dp_panel_tpg_config(struct dp_panel *dp_panel, bool enable)
304 {
305 struct dp_catalog *catalog;
306 struct dp_panel_private *panel;
307
308 if (!dp_panel) {
309 DRM_ERROR("invalid input\n");
310 return;
311 }
312
313 panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
314 catalog = panel->catalog;
315
316 if (!panel->panel_on) {
317 DRM_DEBUG_DP("DP panel not enabled, handle TPG on next on\n");
318 return;
319 }
320
321 if (!enable) {
322 dp_catalog_panel_tpg_disable(catalog);
323 return;
324 }
325
326 DRM_DEBUG_DP("%s: calling catalog tpg_enable\n", __func__);
327 dp_catalog_panel_tpg_enable(catalog, &panel->dp_panel.dp_mode.drm_mode);
328 }
329
dp_panel_dump_regs(struct dp_panel * dp_panel)330 void dp_panel_dump_regs(struct dp_panel *dp_panel)
331 {
332 struct dp_catalog *catalog;
333 struct dp_panel_private *panel;
334
335 panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
336 catalog = panel->catalog;
337
338 dp_catalog_dump_regs(catalog);
339 }
340
dp_panel_timing_cfg(struct dp_panel * dp_panel)341 int dp_panel_timing_cfg(struct dp_panel *dp_panel)
342 {
343 int rc = 0;
344 u32 data, total_ver, total_hor;
345 struct dp_catalog *catalog;
346 struct dp_panel_private *panel;
347 struct drm_display_mode *drm_mode;
348
349 panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
350 catalog = panel->catalog;
351 drm_mode = &panel->dp_panel.dp_mode.drm_mode;
352
353 DRM_DEBUG_DP("width=%d hporch= %d %d %d\n",
354 drm_mode->hdisplay, drm_mode->htotal - drm_mode->hsync_end,
355 drm_mode->hsync_start - drm_mode->hdisplay,
356 drm_mode->hsync_end - drm_mode->hsync_start);
357
358 DRM_DEBUG_DP("height=%d vporch= %d %d %d\n",
359 drm_mode->vdisplay, drm_mode->vtotal - drm_mode->vsync_end,
360 drm_mode->vsync_start - drm_mode->vdisplay,
361 drm_mode->vsync_end - drm_mode->vsync_start);
362
363 total_hor = drm_mode->htotal;
364
365 total_ver = drm_mode->vtotal;
366
367 data = total_ver;
368 data <<= 16;
369 data |= total_hor;
370
371 catalog->total = data;
372
373 data = (drm_mode->vtotal - drm_mode->vsync_start);
374 data <<= 16;
375 data |= (drm_mode->htotal - drm_mode->hsync_start);
376
377 catalog->sync_start = data;
378
379 data = drm_mode->vsync_end - drm_mode->vsync_start;
380 data <<= 16;
381 data |= (panel->dp_panel.dp_mode.v_active_low << 31);
382 data |= drm_mode->hsync_end - drm_mode->hsync_start;
383 data |= (panel->dp_panel.dp_mode.h_active_low << 15);
384
385 catalog->width_blanking = data;
386
387 data = drm_mode->vdisplay;
388 data <<= 16;
389 data |= drm_mode->hdisplay;
390
391 catalog->dp_active = data;
392
393 dp_catalog_panel_timing_cfg(catalog);
394 panel->panel_on = true;
395
396 return rc;
397 }
398
dp_panel_init_panel_info(struct dp_panel * dp_panel)399 int dp_panel_init_panel_info(struct dp_panel *dp_panel)
400 {
401 int rc = 0;
402 struct drm_display_mode *drm_mode;
403
404 drm_mode = &dp_panel->dp_mode.drm_mode;
405
406 /*
407 * print resolution info as this is a result
408 * of user initiated action of cable connection
409 */
410 DRM_DEBUG_DP("SET NEW RESOLUTION:\n");
411 DRM_DEBUG_DP("%dx%d@%dfps\n", drm_mode->hdisplay,
412 drm_mode->vdisplay, drm_mode_vrefresh(drm_mode));
413 DRM_DEBUG_DP("h_porches(back|front|width) = (%d|%d|%d)\n",
414 drm_mode->htotal - drm_mode->hsync_end,
415 drm_mode->hsync_start - drm_mode->hdisplay,
416 drm_mode->hsync_end - drm_mode->hsync_start);
417 DRM_DEBUG_DP("v_porches(back|front|width) = (%d|%d|%d)\n",
418 drm_mode->vtotal - drm_mode->vsync_end,
419 drm_mode->vsync_start - drm_mode->vdisplay,
420 drm_mode->vsync_end - drm_mode->vsync_start);
421 DRM_DEBUG_DP("pixel clock (KHz)=(%d)\n", drm_mode->clock);
422 DRM_DEBUG_DP("bpp = %d\n", dp_panel->dp_mode.bpp);
423
424 dp_panel->dp_mode.bpp = max_t(u32, 18,
425 min_t(u32, dp_panel->dp_mode.bpp, 30));
426 DRM_DEBUG_DP("updated bpp = %d\n", dp_panel->dp_mode.bpp);
427
428 return rc;
429 }
430
dp_panel_get(struct dp_panel_in * in)431 struct dp_panel *dp_panel_get(struct dp_panel_in *in)
432 {
433 struct dp_panel_private *panel;
434 struct dp_panel *dp_panel;
435
436 if (!in->dev || !in->catalog || !in->aux || !in->link) {
437 DRM_ERROR("invalid input\n");
438 return ERR_PTR(-EINVAL);
439 }
440
441 panel = devm_kzalloc(in->dev, sizeof(*panel), GFP_KERNEL);
442 if (!panel)
443 return ERR_PTR(-ENOMEM);
444
445 panel->dev = in->dev;
446 panel->aux = in->aux;
447 panel->catalog = in->catalog;
448 panel->link = in->link;
449
450 dp_panel = &panel->dp_panel;
451 dp_panel->max_bw_code = DP_LINK_BW_8_1;
452 panel->aux_cfg_update_done = false;
453
454 return dp_panel;
455 }
456
dp_panel_put(struct dp_panel * dp_panel)457 void dp_panel_put(struct dp_panel *dp_panel)
458 {
459 if (!dp_panel)
460 return;
461
462 kfree(dp_panel->edid);
463 }
464