blob: f7b5cfc8d81a4e1295a759753cf6af0935a9dedf [file] [log] [blame]
Kuogee Hsiehad69c3c2013-08-01 14:34:29 -07001/* Copyright (c) 2013, The Linux Foundation. All rights reserved.
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions
5 * are met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in
10 * the documentation and/or other materials provided with the
11 * distribution.
12 * * Neither the name of The Linux Foundation nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
23 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include "edp.h"
31
32struct edp_aux_ctrl edpctrl;
33
34int edp_hpd_done = 0;
35int edp_video_ready = 0;
36
37/*
38 * edid
39 */
40static char edid_hdr[8] = {0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00};
41
42
43int edp_edid_buf_error(char *buf, int len)
44{
45 char *bp;
46 int i;
47 char csum = 0;
48 int ret = 0;
49
50 bp = buf;
51 if (len < 128) {
52 dprintf(INFO, "edp_edid_bur_error: Error: len=%x\n", len);
53 return -1;
54 }
55
56 for (i = 0; i < 128; i++)
57 csum += *bp++;
58
59 if (csum != 0) {
60 dprintf(INFO, "edp_edid_bur_error: Error: csum=%x\n", csum);
61 return -1;
62 }
63
64 if (buf[1] != 0xff) {
65 dprintf(INFO, "edp_edid_buf_error: Error: header\n");
66 return -1;
67 }
68
69 return ret;
70}
71
72
73void edp_extract_edid_manufacturer(struct edp_edid *edid, char *buf)
74{
75 char *bp;
76 char data;
77
78 bp = &buf[8];
79 data = *bp & 0x7f;
80 data >>= 2;
81 edid->id_name[0] = 'A' + data - 1;
82 data = *bp & 0x03;
83 data <<= 3;
84 bp++;
85 data |= (*bp >> 5);
86 edid->id_name[1] = 'A' + data - 1;
87 data = *bp & 0x1f;
88 edid->id_name[2] = 'A' + data - 1;
89 edid->id_name[3] = 0;
90
91 dprintf(INFO, "%s: edid manufacturer = %s", __func__,edid->id_name);
92}
93
94void edp_extract_edid_product(struct edp_edid *edid, char *buf)
95{
96 char *bp;
97 int data;
98
99 bp = &buf[0x0a];
100 data = *bp;
101 edid->id_product = *bp++;
102 edid->id_product &= 0x0ff;
103 data = *bp & 0x0ff;
104 data <<= 8;
105 edid->id_product |= data;
106
107 dprintf(INFO, "edid product = 0x%x", edid->id_product);
108};
109
110void edp_extract_edid_version(struct edp_edid *edid, char *buf)
111{
112 edid->version = buf[0x12];
113 edid->revision = buf[0x13];
114 dprintf(INFO, "edid version = %d.%d", edid->version,
115 edid->revision);
116};
117
118void edp_extract_edid_ext_block_cnt(struct edp_edid *edid, char *buf)
119{
120 edid->ext_block_cnt = buf[0x7e];
121 dprintf(INFO, "edid extension = %d", edid->ext_block_cnt);
122};
123
124void edp_extract_edid_video_support(struct edp_edid *edid, char *buf)
125{
126 char *bp;
127
128 bp = &buf[0x14];
129 if (*bp & 0x80) {
130 edid->video_intf = *bp & 0x0f;
131 /* 6, 8, 10, 12, 14 and 16 bit per component */
132 edid->color_depth = ((*bp & 0x70) >> 4); /* color bit depth */
133 if (edid->color_depth) {
134 edid->color_depth *= 2;
135 edid->color_depth += 4;
136 }
137 dprintf(INFO, "Digital Video intf=%d color_depth=%d\n",
138 edid->video_intf, edid->color_depth);
139 return;
140 }
141 dprintf(INFO, "Error, Analog video interface");
142};
143
144void edp_extract_edid_feature(struct edp_edid *edid, char *buf)
145{
146 char *bp;
147 char data;
148
149 bp = &buf[0x18];
150 data = *bp;
151 data &= 0xe0;
152 data >>= 5;
153 if (data == 0x01)
154 edid->dpm = 1; /* display power management */
155
156 if (edid->video_intf) {
157 if (*bp & 0x80) {
158 /* RGB 4:4:4, YcrCb 4:4:4 and YCrCb 4:2:2 */
159 edid->color_format = *bp & 0x18;
160 edid->color_format >>= 3;
161 }
162 }
163
164 dprintf(INFO, "edid dpm=%d color_format=%d",
165 edid->dpm, edid->color_format);
166};
167
168void edp_extract_edid_detailed_timing_description(struct edp_edid *edid,
169 char *buf)
170{
171 char *bp;
172 int data;
173 struct display_timing_desc *dp;
174
175 dp = &edid->timing[0];
176
177 bp = &buf[0x36];
178 dp->pclk = 0;
179 dp->pclk = *bp++; /* byte 0x36 */
180 dp->pclk |= (*bp++ << 8); /* byte 0x37 */
181
182 dp->h_addressable = *bp++; /* byte 0x38 */
183
184 if (dp->pclk == 0 && dp->h_addressable == 0)
185 return; /* Not detailed timing definition */
186
187 dp->pclk *= 10000;
188
189 dp->h_blank = *bp++;/* byte 0x39 */
190 data = *bp & 0xf0; /* byte 0x3A */
191 data <<= 4;
192 dp->h_addressable |= data;
193
194 data = *bp++ & 0x0f;
195 data <<= 8;
196 dp->h_blank |= data;
197
198 dp->v_addressable = *bp++; /* byte 0x3B */
199 dp->v_blank = *bp++; /* byte 0x3C */
200 data = *bp & 0xf0; /* byte 0x3D */
201 data <<= 4;
202 dp->v_addressable |= data;
203
204 data = *bp++ & 0x0f;
205 data <<= 8;
206 dp->v_blank |= data;
207
208 dp->h_fporch = *bp++; /* byte 0x3E */
209 dp->h_sync_pulse = *bp++; /* byte 0x3F */
210
211 dp->v_fporch = *bp & 0x0f0; /* byte 0x40 */
212 dp->v_fporch >>= 4;
213 dp->v_sync_pulse = *bp & 0x0f;
214
215 bp++;
216 data = *bp & 0xc0; /* byte 0x41 */
217 data <<= 2;
218 dp->h_fporch |= data;
219
220 data = *bp & 0x30;
221 data <<= 4;
222 dp->h_sync_pulse |= data;
223
224 data = *bp & 0x0c;
225 data <<= 2;
226 dp->v_fporch |= data;
227
228 data = *bp & 0x03;
229 data <<= 4;
230 dp->v_sync_pulse |= data;
231
232 bp++;
233 dp->width_mm = *bp++; /* byte 0x42 */
234 dp->height_mm = *bp++; /* byte 0x43 */
235 data = *bp & 0x0f0; /* byte 0x44 */
236 data <<= 4;
237 dp->width_mm |= data;
238 data = *bp & 0x0f;
239 data <<= 8;
240 dp->height_mm |= data;
241
242 bp++;
243 dp->h_border = *bp++; /* byte 0x45 */
244 dp->v_border = *bp++; /* byte 0x46 */
245
246 dp->interlaced = *bp & 0x80; /* byte 0x47 */
247
248 dp->stereo = *bp & 0x60;
249 dp->stereo >>= 5;
250
251 data = *bp & 0x1e; /* bit 4,3,2 1*/
252 data >>= 1;
253 dp->sync_type = data & 0x08;
254 dp->sync_type >>= 3; /* analog or digital */
255 if (dp->sync_type) {
256 dp->sync_separate = data & 0x04;
257 dp->sync_separate >>= 2;
258 if (dp->sync_separate) {
259 if (data & 0x02)
260 dp->vsync_pol = 1; /* positive */
261 else
262 dp->vsync_pol = 0;/* negative */
263
264 if (data & 0x01)
265 dp->hsync_pol = 1; /* positive */
266 else
267 dp->hsync_pol = 0; /* negative */
268 }
269 }
270
271 dprintf(INFO, "pixel_clock = %d\n", dp->pclk);
272
273 dprintf(INFO, "horizontal=%d, blank=%d, porch=%d, sync=%d\n"
274 , dp->h_addressable, dp->h_blank,
275 dp->h_fporch, dp->h_sync_pulse);
276 dprintf(INFO, "vertical=%d, blank=%d, porch=%d, vsync=%d\n"
277 , dp->v_addressable, dp->v_blank,
278 dp->v_fporch, dp->v_sync_pulse);
279 dprintf(INFO, "panel size in mm, width=%d height=%d\n",
280 dp->width_mm, dp->height_mm);
281 dprintf(INFO, "panel border horizontal=%d vertical=%d\n",
282 dp->h_border, dp->v_border);
283 dprintf(INFO, "flags: interlaced=%d stereo=%d sync_type=%d sync_sep=%d\n"
284 , dp->interlaced, dp->stereo,
285 dp->sync_type, dp->sync_separate);
286 dprintf(INFO, "polarity vsync=%d, hsync=%d\n",
287 dp->vsync_pol, dp->hsync_pol);
288}
289
290
291/*
292 * EDID structure can be found in VESA standart here:
293 * http://read.pudn.com/downloads110/ebook/456020/E-EDID%20Standard.pdf
294 *
295 * following table contains default edid
296 * static char edid_raw_data[128] = {
297 * 0, 255, 255, 255, 255, 255, 255, 0,
298 * 6, 175, 93, 48, 0, 0, 0, 0, 0, 22,
299 * 1, 4,
300 * 149, 26, 14, 120, 2,
301 * 164, 21,158, 85, 78, 155, 38, 15, 80, 84,
302 * 0, 0, 0,
303 * 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
304 * 29, 54, 128, 160, 112, 56, 30, 64, 48, 32, 142, 0, 0, 144, 16,0,0,24,
305 * 19, 36, 128, 160, 112, 56, 30, 64, 48, 32, 142, 0, 0, 144, 16,0,0,24,
306 * 0, 0, 0, 254, 0, 65, 85, 79, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32,
307 * 0, 0, 0, 254, 0, 66, 49, 49, 54, 72, 65, 78, 48, 51, 46, 48, 32, 10,
308 * 0, 75 };
309 */
310
311static int edp_aux_chan_ready(struct edp_aux_ctrl *ep)
312{
313 int cnt, ret;
314 char data = 0;
315
316 cnt = 5;
317 while(cnt--) {
318 ret = edp_aux_write_buf(ep, 0x50, &data, 1, 1);
319 dprintf(INFO, "edp_aux_chan_ready: ret=%d\n", ret);
320 if (ret >= 0)
321 break;
322 dprintf(INFO, "edp_aux_chan_ready: failed in write\n");
323 mdelay(100);
324 }
325
326 if (cnt == 0)
327 return 0;
328
329 return 1;
330}
331
332static int edp_sink_edid_read(struct edp_aux_ctrl *ep, int block)
333{
334 struct edp_buf *rp;
335 int cnt, rlen;
336 char data = 0;
337 int ret = 0;
338
339start:
340 cnt = 5;
341dprintf(INFO, "%s: cnt=%d\n", __func__, cnt);
342 /* need to write a dummy byte before read edid */
343 while(cnt--) {
344 ret = edp_aux_write_buf(ep, 0x50, &data, 1, 1);
345 if (ret >= 0)
346 break;
347 dprintf(INFO, "edp_sink_edid_read: failed in write\n");
348 mdelay(100);
349 }
350
351 if (cnt == 0)
352 return -1;
353
354 rlen = edp_aux_read_buf(ep, 0x50, 128, 1);
355
356dprintf(INFO, "edp_sink_edid_read: rlen=%d\n", rlen);
357
358 if (rlen < 0)
359 goto start;
360
361 rp = &ep->rxp;
362 if (edp_edid_buf_error(rp->data, rp->len))
363 goto start;
364
365 edp_extract_edid_manufacturer(&ep->edid, rp->data);
366 edp_extract_edid_product(&ep->edid, rp->data);
367 edp_extract_edid_version(&ep->edid, rp->data);
368 edp_extract_edid_ext_block_cnt(&ep->edid, rp->data);
369 edp_extract_edid_video_support(&ep->edid, rp->data);
370 edp_extract_edid_feature(&ep->edid, rp->data);
371 edp_extract_edid_detailed_timing_description(&ep->edid, rp->data);
372
373 return 128;
374}
375
376/*
377 * Converts from EDID struct to msm_panel_info
378 */
379void edp_edid2pinfo(struct msm_panel_info *pinfo)
380{
381 struct display_timing_desc *dp;
382
383 dp = &edpctrl.edid.timing[0];
384
385 pinfo->clk_rate = dp->pclk;
386
387 dprintf(SPEW, "%s: pclk=%d\n", __func__, pinfo->clk_rate);
388
389 pinfo->xres = dp->h_addressable + dp->h_border * 2;
390 pinfo->yres = dp->v_addressable + dp->v_border * 2;
391
392 pinfo->lcdc.h_back_porch = dp->h_blank - dp->h_fporch \
393 - dp->h_sync_pulse;
394 pinfo->lcdc.h_front_porch = dp->h_fporch;
395 pinfo->lcdc.h_pulse_width = dp->h_sync_pulse;
396
397 pinfo->lcdc.v_back_porch = dp->v_blank - dp->v_fporch \
398 - dp->v_sync_pulse;
399 pinfo->lcdc.v_front_porch = dp->v_fporch;
400 pinfo->lcdc.v_pulse_width = dp->v_sync_pulse;
401
402 pinfo->type = EDP_PANEL;
403 pinfo->wait_cycle = 0;
404 pinfo->bpp = 24;
405
406 pinfo->lcdc.border_clr = 0; /* black */
407 pinfo->lcdc.underflow_clr = 0xff; /* blue */
408 pinfo->lcdc.hsync_skew = 0;
409}
410
411void edp_cap2pinfo(struct msm_panel_info *pinfo)
412{
413 struct dpcd_cap *cap;
414
415 cap = &edpctrl.dpcd;
416
417 pinfo->edp.max_lane_count = cap->max_lane_count;
418 pinfo->edp.max_link_clk = cap->max_link_rate;
419
420 dprintf(SPEW, "%s: clk=%d lane=%d\n", __func__,
421 pinfo->edp.max_lane_count, pinfo->edp.max_link_clk);
422}
423
424static void edp_sink_capability_read(struct edp_aux_ctrl *ep,
425 int len)
426{
427 char *bp;
428 char data;
429 struct dpcd_cap *cap;
430 struct edp_buf *rp;
431 int rlen;
432
433 dprintf(INFO, "%s:\n",__func__);
434
435 rlen = edp_aux_read_buf(ep, 0, len, 0);
436 if (rlen <= 0) {
437 dprintf(INFO, "edp_sink_capability_read: edp aux read failed\n");
438 return;
439 }
440 rp = &ep->rxp;
441 cap = &ep->dpcd;
442 bp = rp->data;
443
444 data = *bp++; /* byte 0 */
445 cap->major = (data >> 4) & 0x0f;
446 cap->minor = data & 0x0f;
447 if (--rlen <= 0)
448 return;
449 dprintf(INFO, "edp_sink_cap_read: version: %d.%d\n", cap->major, cap->minor);
450
451 data = *bp++; /* byte 1 */
452 /* 162, 270 and 540 MB, symbol rate, NOT bit rate */
453 cap->max_link_rate = data * 27;
454 if (--rlen <= 0)
455 return;
456 dprintf(INFO, "edp_sink_cap_read: link_rate=%d\n", cap->max_link_rate);
457
458 data = *bp++; /* byte 2 */
459 if (data & BIT(7))
460 cap->flags |= DPCD_ENHANCED_FRAME;
461 if (data & 0x40)
462 cap->flags |= DPCD_TPS3;
463 data &= 0x0f;
464 cap->max_lane_count = data;
465 if (--rlen <= 0)
466 return;
467 dprintf(INFO, "edp_sink_cap_read: lane_count=%d\n", cap->max_lane_count);
468
469 data = *bp++; /* byte 3 */
470 if (data & BIT(0)) {
471 cap->flags |= DPCD_MAX_DOWNSPREAD_0_5;
472 dprintf(INFO, "edp_sink_cap_read: max_downspread\n");
473 }
474
475 if (data & BIT(6)) {
476 cap->flags |= DPCD_NO_AUX_HANDSHAKE;
477 dprintf(INFO, "edp_sink_cap_read: NO Link Training\n");
478 }
479 if (--rlen <= 0)
480 return;
481
482 data = *bp++; /* byte 4 */
483 cap->num_rx_port = (data & BIT(0)) + 1;
484 dprintf(INFO, "edp_sink_cap_read: rx_ports=%d", cap->num_rx_port);
485 if (--rlen <= 0)
486 return;
487
488 bp += 3; /* skip 5, 6 and 7 */
489 rlen -= 3;
490 if (rlen <= 0)
491 return;
492
493 data = *bp++; /* byte 8 */
494 if (data & BIT(1)) {
495 cap->flags |= DPCD_PORT_0_EDID_PRESENTED;
496 dprintf(INFO, "edp_sink_cap_read: edid presented\n");
497 }
498 if (--rlen <= 0)
499 return;
500
501 data = *bp++; /* byte 9 */
502 cap->rx_port0_buf_size = (data + 1) * 32;
503 dprintf(INFO, "edp_sink_cap_read: lane_buf_size=%d", cap->rx_port0_buf_size);
504 if (--rlen <= 0)
505 return;
506
507 bp += 2; /* skip 10, 11 port1 capability */
508 rlen -= 2;
509 if (rlen <= 0)
510 return;
511
512 data = *bp++; /* byte 12 */
513 cap->i2c_speed_ctrl = data;
514 if (cap->i2c_speed_ctrl > 0)
515 dprintf(INFO, "edp_sink_cap_read: i2c_rate=%d", cap->i2c_speed_ctrl);
516 if (--rlen <= 0)
517 return;
518
519 data = *bp++; /* byte 13 */
520 cap->scrambler_reset = data & BIT(0);
521 dprintf(INFO, "edp_sink_cap_read: scrambler_reset=%d\n",
522 cap->scrambler_reset);
523
524 cap->enhanced_frame = data & BIT(1);
525 dprintf(INFO, "edp_sink_cap_read: enhanced_framing=%d\n",
526 cap->enhanced_frame);
527 if (--rlen <= 0)
528 return;
529
530 data = *bp++; /* byte 14 */
531 if (data == 0)
532 cap->training_read_interval = 100; /* us */
533 else
534 cap->training_read_interval = 4000 * data; /* us */
535 dprintf(INFO, "edp_sink_cap_read: training_interval=%d\n",
536 cap->training_read_interval);
537}
538
539static void edp_link_status_read(struct edp_aux_ctrl *ep, int len)
540{
541 char *bp;
542 char data;
543 struct dpcd_link_status *sp;
544 struct edp_buf *rp;
545 int rlen;
546
547
548 /* skip byte 0x200 and 0x201 */
549 rlen = edp_aux_read_buf(ep, 0x202, len, 0);
550 dprintf(INFO, "%s: rlen=%d\n", __func__, rlen);
551 if (rlen <= 0) {
552 dprintf(INFO, "edp_link_status_read: edp aux read failed\n");
553 return;
554 }
555 rp = &ep->rxp;
556 bp = rp->data;
557 sp = &ep->link_status;
558
559 data = *bp++; /* byte 0x202 */
560 sp->lane_01_status = data; /* lane 0, 1 */
561 if (--rlen <= 0)
562 return;
563
564 data = *bp++; /* byte 0x203 */
565 sp->lane_23_status = data; /* lane 2, 3 */
566 if (--rlen <= 0)
567 return;
568
569 data = *bp++; /* byte 0x204 */
570 sp->interlane_align_done = (data & BIT(0));
571 sp->downstream_port_status_changed = (data & BIT(6));
572 sp->link_status_updated = (data & BIT(7));
573 if (--rlen <= 0)
574 return;
575
576 data = *bp++; /* byte 0x205 */
577 sp->port_0_in_sync = (data & BIT(0));
578 sp->port_1_in_sync = (data & BIT(1));
579 if (--rlen <= 0)
580 return;
581
582 data = *bp++; /* byte 0x206 */
583 sp->req_voltage_swing[0] = data & 0x03;
584 data >>= 2;
585 sp->req_pre_emphasis[0] = data & 0x03;
586 data >>= 2;
587 sp->req_voltage_swing[1] = data & 0x03;
588 data >>= 2;
589 sp->req_pre_emphasis[1] = data & 0x03;
590 if (--rlen <= 0)
591 return;
592
593 data = *bp++; /* byte 0x207 */
594 sp->req_voltage_swing[2] = data & 0x03;
595 data >>= 2;
596 sp->req_pre_emphasis[2] = data & 0x03;
597 data >>= 2;
598 sp->req_voltage_swing[3] = data & 0x03;
599 data >>= 2;
600 sp->req_pre_emphasis[3] = data & 0x03;
601
602 bp = rp->data;
603dprintf(INFO, "%s: %x %x %x %x %x %x\n", __func__, *bp,
604 *(bp+1), *(bp+2), *(bp+3), *(bp+4), *(bp+5));
605
606 dprintf(INFO, "%s: align=%d v=%d p=%d\n", __func__,
607 sp->interlane_align_done, sp->req_voltage_swing[0], sp->req_pre_emphasis[0]);
608}
609
610
611static int edp_cap_lane_rate_set(struct edp_aux_ctrl *ep)
612{
613 char buf[4];
614 int len = 0;
615
616 dprintf(INFO, "cap_lane_set: bw=%x lane=%d\n", ep->link_rate, ep->lane_cnt);
617 buf[0] = ep->link_rate;
618 buf[1] = ep->lane_cnt;
619 len = edp_aux_write_buf(ep, 0x100, buf, 2, 0);
620
621 return len;
622}
623
624static int edp_lane_set_write(struct edp_aux_ctrl *ep, int voltage_level,
625 int pre_emphasis_level)
626{
627 int i;
628 char buf[4];
629
630
631 if (voltage_level >= DPCD_LINK_VOLTAGE_MAX)
632 voltage_level |= 0x04;
633
634 if (pre_emphasis_level >= DPCD_LINK_PRE_EMPHASIS_MAX)
635 pre_emphasis_level |= 0x04;
636
637 pre_emphasis_level <<= 3;
638
639 for (i = 0; i < 4; i++)
640 buf[i] = voltage_level | pre_emphasis_level;
641
642 dprintf(INFO, "%s: p|v=0x%x\n", __func__, voltage_level | pre_emphasis_level);
643 return edp_aux_write_buf(ep, 0x103, buf, 4, 0);
644}
645
646static int edp_powerstate_write(struct edp_aux_ctrl *ep,
647 char powerstate)
648{
649 return edp_aux_write_buf(ep, 0x600, &powerstate, 1, 0);
650}
651
652static int edp_train_pattern_set_write(struct edp_aux_ctrl *ep,
653 int pattern)
654{
655 char buf[4];
656
657 buf[0] = pattern;
658 return edp_aux_write_buf(ep, 0x102, buf, 1, 0);
659}
660
661static int edp_sink_clock_recovery_done(struct edp_aux_ctrl *ep)
662{
663 int mask;
664 int data;
665
666
667 if (ep->lane_cnt == 1) {
668 mask = 0x01; /* lane 0 */
669 data = ep->link_status.lane_01_status;
670 } else if (ep->lane_cnt == 2) {
671 mask = 0x011; /*B lane 0, 1 */
672 data = ep->link_status.lane_01_status;
673 } else {
674 mask = 0x01111; /*B lane 0, 1 */
675 data = ep->link_status.lane_23_status;
676 data <<= 8;
677 data |= ep->link_status.lane_01_status;
678 }
679
680dprintf(INFO, "clock_recovery_done: data=%x mask=%x\n", data, mask);
681 data &= mask;
682 if (data == mask) /* all done */
683 return 1;
684
685 return 0;
686}
687
688static int edp_sink_channel_eq_done(struct edp_aux_ctrl *ep)
689{
690 int mask;
691 int data;
692
693
694 if (!ep->link_status.interlane_align_done) /* not align */
695 return 0;
696
697 if (ep->lane_cnt == 1) {
698 mask = 0x7;
699 data = ep->link_status.lane_01_status;
700 } else if (ep->lane_cnt == 2) {
701 mask = 0x77;
702 data = ep->link_status.lane_01_status;
703 } else {
704 mask = 0x7777;
705 data = ep->link_status.lane_23_status;
706 data <<= 8;
707 data |= ep->link_status.lane_01_status;
708 }
709
710dprintf(INFO, "%s: data=%x mask=%x\n", __func__, data, mask);
711
712 data &= mask;
713 if (data == mask)/* all done */
714 return 1;
715
716 return 0;
717}
718
719void edp_sink_train_set_adjust(struct edp_aux_ctrl *ep)
720{
721 int i;
722 int max = 0;
723
724
725 /* use the max level across lanes */
726 for (i = 0; i < ep->lane_cnt; i++) {
727 if (max < ep->link_status.req_voltage_swing[i])
728 max = ep->link_status.req_voltage_swing[i];
729 }
730
731 ep->v_level = max;
732
733 /* use the max level across lanes */
734 max = 0;
735 for (i = 0; i < ep->lane_cnt; i++) {
736 if (max < ep->link_status.req_pre_emphasis[i])
737 max = ep->link_status.req_pre_emphasis[i];
738 }
739
740 ep->p_level = max;
741 dprintf(INFO, "train_set_adjust: v_level=%d, p_level=%d\n",
742 ep->v_level, ep->p_level);
743}
744
745static void edp_host_train_set(struct edp_aux_ctrl *ep, int train)
746{
747 int bit, cnt;
748 int data;
749
750
751 bit = 1;
752 bit <<= (train - 1);
753 edp_write(EDP_BASE + EDP_STATE_CTRL, bit);
754
755 bit = 8;
756 bit <<= (train - 1);
757 cnt = 10;
758 while (cnt--) {
759 data = edp_read(EDP_BASE + EDP_MAINLINK_READY);
760 if (data & bit)
761 break;
762 }
763
764 if (cnt == 0)
765 dprintf(INFO, "%s: set link_train=%d failed\n", __func__, train);
766}
767
768char vm_pre_emphasis[4][4] = {
769 {0x03, 0x06, 0x09, 0x0C},
770 {0x03, 0x06, 0x09, 0xFF},
771 {0x03, 0x06, 0xFF, 0xFF},
772 {0x03, 0xFF, 0xFF, 0xFF}
773};
774
775char vm_voltage_swing[4][4] = {
776 {0x64, 0x68, 0x6A, 0x6E},
777 {0x68, 0x6A, 0x6E, 0xFF},
778 {0x6A, 0x6E, 0xFF, 0xFF},
779 {0x6E, 0xFF, 0xFF, 0xFF}
780};
781
782static void edp_voltage_pre_emphasise_set(struct edp_aux_ctrl *ep)
783{
784 int value0 = 0;
785 int value1 = 0;
786
787 dprintf(INFO, "voltage_pre_emphasis_set: v=%d p=%d\n", ep->v_level, ep->p_level);
788
789 value0 = vm_pre_emphasis[(int)(ep->v_level)][(int)(ep->p_level)];
790 value1 = vm_voltage_swing[(int)(ep->v_level)][(int)(ep->p_level)];
791
792 /* Configure host and panel only if both values are allowed */
793 if (value0 != 0xFF && value1 != 0xFF) {
794 edp_write(EDP_BASE + EDP_PHY_EDPPHY_GLB_VM_CFG0, value0);
795 edp_write(EDP_BASE + EDP_PHY_EDPPHY_GLB_VM_CFG1, value1);
796 dprintf(INFO, "voltage_pre_emphasis_set: value0=0x%x value1=0x%x\n",
797 value0, value1);
798 edp_lane_set_write(ep, ep->v_level, ep->p_level);
799 }
800
801}
802
803static int edp_start_link_train_1(struct edp_aux_ctrl *ep)
804{
805 int tries, old_v_level;
806 int ret = 0;
807
808 dprintf(INFO, "link_train_1\n");
809
810 edp_host_train_set(ep, 0x01); /* train_1 */
811 edp_voltage_pre_emphasise_set(ep);
812 edp_train_pattern_set_write(ep, 0x21); /* train_1 */
813
814 tries = 0;
815 old_v_level = ep->v_level;
816 while (1) {
817 udelay(ep->dpcd.training_read_interval * 10);
818
819 edp_link_status_read(ep, 6);
820 if (edp_sink_clock_recovery_done(ep)) {
821 ret = 0;
822 break;
823 }
824
825 if (ep->v_level == DPCD_LINK_VOLTAGE_MAX) {
826 ret = -1;
827 break; /* quit */
828 }
829
830 if (old_v_level == ep->v_level) {
831 tries++;
832 if (tries >= 5) {
833 ret = -1;
834 break; /* quit */
835 }
836 } else {
837 tries = 0;
838 old_v_level = ep->v_level;
839 }
840
841 edp_sink_train_set_adjust(ep);
842 edp_voltage_pre_emphasise_set(ep);
843 }
844
845 return ret;
846}
847
848static int edp_start_link_train_2(struct edp_aux_ctrl *ep)
849{
850 int tries;
851 int ret = 0;
852 char pattern;
853
854 dprintf(INFO, "link_train_2\n");
855
856 if (ep->dpcd.flags & DPCD_TPS3)
857 pattern = 0x03;
858 else
859 pattern = 0x02;
860
861 edp_host_train_set(ep, pattern); /* train_2 */
862 edp_voltage_pre_emphasise_set(ep);
863 edp_train_pattern_set_write(ep, pattern | 0x20);/* train_2 */
864
865 tries = 0;
866 while (1) {
867 udelay(ep->dpcd.training_read_interval);
868
869 edp_link_status_read(ep, 6);
870
871 if (edp_sink_channel_eq_done(ep)) {
872 ret = 0;
873 break;
874 }
875
876 tries++;
877 if (tries > 5) {
878 ret = -1;
879 break;
880 }
881
882 edp_sink_train_set_adjust(ep);
883 edp_voltage_pre_emphasise_set(ep);
884 }
885
886 return ret;
887}
888
889static int edp_link_rate_shift(struct edp_aux_ctrl *ep)
890{
891 /* add calculation later */
892 return -1;
893}
894
895static void edp_clear_training_pattern(struct edp_aux_ctrl *ep)
896{
897 dprintf(INFO, "clear_training_pattern:\n");
898 edp_write(EDP_BASE + EDP_STATE_CTRL, 0);
899 edp_train_pattern_set_write(ep, 0);
900 udelay(ep->dpcd.training_read_interval);
901}
902
903static int edp_aux_link_train(struct edp_aux_ctrl *ep)
904{
905 int ret = 0;
906
907 dprintf(INFO, "%s:\n", __func__);
908 ret = edp_aux_chan_ready(ep);
909 if (ret == 0) {
910 dprintf(INFO, "link_train: LINK Train failed: aux chan NOT ready\n");
911 return ret;
912 }
913
914 /* start with max rate and lane */
915 ep->lane_cnt = ep->dpcd.max_lane_count;
916 ep->link_rate = ep->dpcd.max_link_rate;
917 edp_write(EDP_BASE + EDP_MAINLINK_CTRL, 0x1);
918
919train_start:
920 ep->v_level = 0; /* start from default level */
921 ep->p_level = 0;
922 edp_cap_lane_rate_set(ep);
923
924 edp_clear_training_pattern(ep);
925 udelay(ep->dpcd.training_read_interval);
926 edp_powerstate_write(ep, 1);
927
928 ret = edp_start_link_train_1(ep);
929 if (ret < 0) {
930 if (edp_link_rate_shift(ep) == 0) {
931 goto train_start;
932 } else {
933 dprintf(INFO, "Training 1 failed\n");
934 ret = -1;
935 goto clear;
936 }
937 }
938
939 dprintf(INFO, "%s: Training 1 completed successfully\n", __func__);
940
941 edp_clear_training_pattern(ep);
942 ret = edp_start_link_train_2(ep);
943 if (ret < 0) {
944 if (edp_link_rate_shift(ep) == 0) {
945 goto train_start;
946 } else {
947 dprintf(INFO, "Training 2 failed\n");
948 ret = -1;
949 goto clear;
950 }
951 }
952
953 dprintf(INFO, "%s: Training 2 completed successfully\n", __func__);
954
955clear:
956 edp_clear_training_pattern(ep);
957
958 return ret;
959}
960
961void mdss_edp_wait_for_hpd(void)
962{
963 while(1) {
964 udelay(1000);
965 edp_isr_poll();
966 if (edp_hpd_done) {
967 edp_hpd_done = 0;
968 break;
969 }
970 }
971}
972
973void mdss_edp_wait_for_video_ready(void)
974{
975 while(1) {
976 udelay(1000);
977 edp_isr_poll();
978 if (edp_video_ready) {
979 edp_video_ready = 0;
980 break;
981 }
982 }
983}
984
985void mdss_edp_dpcd_cap_read(void)
986{
987 edp_sink_capability_read(&edpctrl, 16);
988}
989void mdss_edp_pll_configure(void)
990{
991 struct display_timing_desc *dp;
992
993 dp = &edpctrl.edid.timing[0];
994 edp_pll_configure(dp->pclk);
995}
996
997void mdss_edp_lane_power_ctrl(int up)
998{
999
1000 dprintf(SPEW, "%s: max_lane=%d\n", __func__, edpctrl.dpcd.max_lane_count);
1001 edp_lane_power_ctrl(edpctrl.dpcd.max_lane_count, up);
1002
1003}
1004
1005void mdss_edp_dpcd_status_read(void)
1006{
1007 edp_link_status_read(&edpctrl, 6);
1008}
1009
1010void mdss_edp_edid_read(void)
1011{
1012 edp_sink_edid_read(&edpctrl, 0);
1013}
1014
1015int mdss_edp_link_train(void)
1016{
1017 return edp_aux_link_train(&edpctrl);
1018}
1019
1020void mdss_edp_aux_init(void)
1021{
1022 edp_buf_init(&edpctrl.txp, edpctrl.txbuf, sizeof(edpctrl.txbuf));
1023 edp_buf_init(&edpctrl.rxp, edpctrl.rxbuf, sizeof(edpctrl.rxbuf));
1024}