OSDN Git Service

initial import
[android-x86/device-viliv-s5.git] / psb-kernel-source-4.41.1 / drm_edid.c
1 /*
2  * Copyright (c) 2007 Intel Corporation
3  *   Jesse Barnes <jesse.barnes@intel.com>
4  *
5  * DDC probing routines (drm_ddc_read & drm_do_probe_ddc_edid) originally from
6  * FB layer.
7  *   Copyright (C) 2006 Dennis Munsie <dmunsie@cecropia.com>
8  */
9 #include "drmP.h"
10 #include <linux/i2c-algo-bit.h>
11 #include "drm_edid.h"
12
13 #include <acpi/acpi_drivers.h>
14
15 /* Valid EDID header has these bytes */
16 static u8 edid_header[] = { 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 };
17
18 int drm_get_acpi_edid(char *method, char *edid, ssize_t length)
19 {
20         int status;
21         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
22         union acpi_object *obj;
23         union acpi_object arg0 = { ACPI_TYPE_INTEGER };
24         struct acpi_object_list args = { 1, &arg0 };
25
26         if (length == 128)
27                 arg0.integer.value = 1;
28         else if (length == 256)
29                 arg0.integer.value = 2;
30         else
31                 return -EINVAL;
32
33         status = acpi_evaluate_object(NULL, method, &args, &buffer);
34         if (ACPI_FAILURE(status))
35                 return -ENODEV;
36
37         obj = buffer.pointer;
38
39         if (obj && obj->type == ACPI_TYPE_BUFFER)
40                 memcpy(edid, obj->buffer.pointer, obj->buffer.length);
41         else {
42                 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
43                 status = -EFAULT;
44                 kfree(obj);
45         }
46
47         return status;
48 }
49 EXPORT_SYMBOL(drm_get_acpi_edid);
50
51 /**
52  * edid_valid - sanity check EDID data
53  * @edid: EDID data
54  *
55  * Sanity check the EDID block by looking at the header, the version number
56  * and the checksum.  Return 0 if the EDID doesn't check out, or 1 if it's
57  * valid.
58  */
59 static bool edid_valid(struct edid *edid)
60 {
61         int i;
62         u8 csum = 0;
63         u8 *raw_edid = (u8 *)edid;
64
65         if (memcmp(edid->header, edid_header, sizeof(edid_header)))
66                 goto bad;
67         if (edid->version != 1)
68                 goto bad;
69         if (edid->revision <= 0 || edid->revision > 3)
70                 goto bad;
71
72         for (i = 0; i < EDID_LENGTH; i++)
73                 csum += raw_edid[i];
74         if (csum)
75                 goto bad;
76
77         return 1;
78
79 bad:
80         return 0;
81 }
82
83 /**
84  * drm_mode_std - convert standard mode info (width, height, refresh) into mode
85  * @t: standard timing params
86  *
87  * Take the standard timing params (in this case width, aspect, and refresh)
88  * and convert them into a real mode using CVT.
89  *
90  * Punts for now, but should eventually use the FB layer's CVT based mode
91  * generation code.
92  */
93 struct drm_display_mode *drm_mode_std(struct drm_device *dev,
94                                       struct std_timing *t)
95 {
96 //      struct fb_videomode mode;
97
98 //      fb_find_mode_cvt(&mode, 0, 0);
99         /* JJJ:  convert to drm_display_mode */
100         struct drm_display_mode *mode;
101         int hsize = t->hsize * 8 + 248, vsize;
102
103         mode = drm_mode_create(dev);
104         if (!mode)
105                 return NULL;
106
107         if (t->aspect_ratio == 0)
108                 vsize = (hsize * 10) / 16;
109         else if (t->aspect_ratio == 1)
110                 vsize = (hsize * 3) / 4;
111         else if (t->aspect_ratio == 2)
112                 vsize = (hsize * 4) / 5;
113         else
114                 vsize = (hsize * 9) / 16;
115
116         drm_mode_set_name(mode);
117
118         return mode;
119 }
120
121 /**
122  * drm_mode_detailed - create a new mode from an EDID detailed timing section
123  * @timing: EDID detailed timing info
124  * @preferred: is this a preferred mode?
125  *
126  * An EDID detailed timing block contains enough info for us to create and
127  * return a new struct drm_display_mode.  The @preferred flag will be set
128  * if this is the display's preferred timing, and we'll use it to indicate
129  * to the other layers that this mode is desired.
130  */
131 struct drm_display_mode *drm_mode_detailed(struct drm_device *dev,
132                                            struct detailed_timing *timing)
133 {
134         struct drm_display_mode *mode;
135         struct detailed_pixel_timing *pt = &timing->data.pixel_data;
136
137         if (pt->stereo) {
138                 printk(KERN_WARNING "stereo mode not supported\n");
139                 return NULL;
140         }
141         if (!pt->separate_sync) {
142                 printk(KERN_WARNING "integrated sync not supported\n");
143                 return NULL;
144         }
145
146         mode = drm_mode_create(dev);
147         if (!mode)
148                 return NULL;
149
150         mode->type = DRM_MODE_TYPE_DRIVER;
151         mode->clock = timing->pixel_clock * 10;
152
153         mode->hdisplay = (pt->hactive_hi << 8) | pt->hactive_lo;
154         mode->hsync_start = mode->hdisplay + ((pt->hsync_offset_hi << 8) |
155                                               pt->hsync_offset_lo);
156         mode->hsync_end = mode->hsync_start +
157                 ((pt->hsync_pulse_width_hi << 8) |
158                  pt->hsync_pulse_width_lo);
159         mode->htotal = mode->hdisplay + ((pt->hblank_hi << 8) | pt->hblank_lo);
160
161         mode->vdisplay = (pt->vactive_hi << 8) | pt->vactive_lo;
162         mode->vsync_start = mode->vdisplay + ((pt->vsync_offset_hi << 8) |
163                                               pt->vsync_offset_lo);
164         mode->vsync_end = mode->vsync_start +
165                 ((pt->vsync_pulse_width_hi << 8) |
166                  pt->vsync_pulse_width_lo);
167         mode->vtotal = mode->vdisplay + ((pt->vblank_hi << 8) | pt->vblank_lo);
168
169         drm_mode_set_name(mode);
170
171         if (pt->interlaced)
172                 mode->flags |= V_INTERLACE;
173
174         mode->flags |= pt->hsync_positive ? V_PHSYNC : V_NHSYNC;
175         mode->flags |= pt->vsync_positive ? V_PVSYNC : V_NVSYNC;
176
177         return mode;
178 }
179
180 /*
181  * Detailed mode info for the EDID "established modes" data to use.
182  */
183 static struct drm_display_mode edid_est_modes[] = {
184         { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
185                    968, 1056, 0, 600, 601, 605, 628, 0,
186                    V_PHSYNC | V_PVSYNC) }, /* 800x600@60Hz */
187         { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 36000, 800, 824,
188                    896, 1024, 0, 600, 601, 603,  625, 0,
189                    V_PHSYNC | V_PVSYNC) }, /* 800x600@56Hz */
190         { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 31500, 640, 656,
191                    720, 840, 0, 480, 481, 484, 500, 0,
192                    V_NHSYNC | V_NVSYNC) }, /* 640x480@75Hz */
193         { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 31500, 640, 664,
194                    704,  832, 0, 480, 489, 491, 520, 0,
195                    V_NHSYNC | V_NVSYNC) }, /* 640x480@72Hz */
196         { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 30240, 640, 704,
197                    768,  864, 0, 480, 483, 486, 525, 0,
198                    V_NHSYNC | V_NVSYNC) }, /* 640x480@67Hz */
199         { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25200, 640, 656,
200                    752, 800, 0, 480, 490, 492, 525, 0,
201                    V_NHSYNC | V_NVSYNC) }, /* 640x480@60Hz */
202         { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 35500, 720, 738,
203                    846, 900, 0, 400, 421, 423,  449, 0,
204                    V_NHSYNC | V_NVSYNC) }, /* 720x400@88Hz */
205         { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 28320, 720, 738,
206                    846,  900, 0, 400, 412, 414, 449, 0,
207                    V_NHSYNC | V_PVSYNC) }, /* 720x400@70Hz */
208         { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 135000, 1280, 1296,
209                    1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
210                    V_PHSYNC | V_PVSYNC) }, /* 1280x1024@75Hz */
211         { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 78800, 1024, 1040,
212                    1136, 1312, 0,  768, 769, 772, 800, 0,
213                    V_PHSYNC | V_PVSYNC) }, /* 1024x768@75Hz */
214         { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 75000, 1024, 1048,
215                    1184, 1328, 0,  768, 771, 777, 806, 0,
216                    V_NHSYNC | V_NVSYNC) }, /* 1024x768@70Hz */
217         { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
218                    1184, 1344, 0,  768, 771, 777, 806, 0,
219                    V_NHSYNC | V_NVSYNC) }, /* 1024x768@60Hz */
220         { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER,44900, 1024, 1032,
221                    1208, 1264, 0, 768, 768, 776, 817, 0,
222                    V_PHSYNC | V_PVSYNC | V_INTERLACE) }, /* 1024x768@43Hz */
223         { DRM_MODE("832x624", DRM_MODE_TYPE_DRIVER, 57284, 832, 864,
224                    928, 1152, 0, 624, 625, 628, 667, 0,
225                    V_NHSYNC | V_NVSYNC) }, /* 832x624@75Hz */
226         { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 49500, 800, 816,
227                    896, 1056, 0, 600, 601, 604,  625, 0,
228                    V_PHSYNC | V_PVSYNC) }, /* 800x600@75Hz */
229         { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 50000, 800, 856,
230                    976, 1040, 0, 600, 637, 643, 666, 0,
231                    V_PHSYNC | V_PVSYNC) }, /* 800x600@72Hz */
232         { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
233                    1344, 1600, 0,  864, 865, 868, 900, 0,
234                    V_PHSYNC | V_PVSYNC) }, /* 1152x864@75Hz */
235 };
236
237 #define EDID_EST_TIMINGS 16
238 #define EDID_STD_TIMINGS 8
239 #define EDID_DETAILED_TIMINGS 4
240
241 /**
242  * add_established_modes - get est. modes from EDID and add them
243  * @edid: EDID block to scan
244  *
245  * Each EDID block contains a bitmap of the supported "established modes" list
246  * (defined above).  Tease them out and add them to the global modes list.
247  */
248 static int add_established_modes(struct drm_output *output, struct edid *edid)
249 {
250         struct drm_device *dev = output->dev;
251         unsigned long est_bits = edid->established_timings.t1 |
252                 (edid->established_timings.t2 << 8) |
253                 ((edid->established_timings.mfg_rsvd & 0x80) << 9);
254         int i, modes = 0;
255
256         for (i = 0; i <= EDID_EST_TIMINGS; i++)
257                 if (est_bits & (1<<i)) {
258                         struct drm_display_mode *newmode;
259                         newmode = drm_mode_duplicate(dev, &edid_est_modes[i]);
260                         drm_mode_probed_add(output, newmode);
261                         modes++;
262                 }
263
264         return modes;
265 }
266
267 /**
268  * add_standard_modes - get std. modes from EDID and add them
269  * @edid: EDID block to scan
270  *
271  * Standard modes can be calculated using the CVT standard.  Grab them from
272  * @edid, calculate them, and add them to the list.
273  */
274 static int add_standard_modes(struct drm_output *output, struct edid *edid)
275 {
276         struct drm_device *dev = output->dev;
277         int i, modes = 0;
278
279         for (i = 0; i < EDID_STD_TIMINGS; i++) {
280                 struct std_timing *t = &edid->standard_timings[i];
281                 struct drm_display_mode *newmode;
282
283                 /* If std timings bytes are 1, 1 it's empty */
284                 if (t->hsize == 1 && (t->aspect_ratio | t->vfreq) == 1)
285                         continue;
286
287                 newmode = drm_mode_std(dev, &edid->standard_timings[i]);
288                 drm_mode_probed_add(output, newmode);
289                 modes++;
290         }
291
292         return modes;
293 }
294
295 /**
296  * add_detailed_modes - get detailed mode info from EDID data
297  * @edid: EDID block to scan
298  *
299  * Some of the detailed timing sections may contain mode information.  Grab
300  * it and add it to the list.
301  */
302 static int add_detailed_info(struct drm_output *output, struct edid *edid)
303 {
304         struct drm_device *dev = output->dev;
305         int i, j, modes = 0;
306
307         for (i = 0; i < EDID_DETAILED_TIMINGS; i++) {
308                 struct detailed_timing *timing = &edid->detailed_timings[i];
309                 struct detailed_non_pixel *data = &timing->data.other_data;
310                 struct drm_display_mode *newmode;
311
312                 /* EDID up to and including 1.2 may put monitor info here */
313                 if (edid->version == 1 && edid->revision < 3)
314                         continue;
315
316                 /* Detailed mode timing */
317                 if (timing->pixel_clock) {
318                         newmode = drm_mode_detailed(dev, timing);
319                         /* First detailed mode is preferred */
320                         if (i == 0 && edid->preferred_timing)
321                                 newmode->type |= DRM_MODE_TYPE_PREFERRED;
322                         drm_mode_probed_add(output, newmode);
323                                      
324                         modes++;
325                         continue;
326                 }
327
328                 /* Other timing or info */
329                 switch (data->type) {
330                 case EDID_DETAIL_MONITOR_SERIAL:
331                         break;
332                 case EDID_DETAIL_MONITOR_STRING:
333                         break;
334                 case EDID_DETAIL_MONITOR_RANGE:
335                         /* Get monitor range data */
336                         break;
337                 case EDID_DETAIL_MONITOR_NAME:
338                         break;
339                 case EDID_DETAIL_MONITOR_CPDATA:
340                         break;
341                 case EDID_DETAIL_STD_MODES:
342                         /* Five modes per detailed section */
343                         for (j = 0; j < 5; i++) {
344                                 struct std_timing *std;
345                                 struct drm_display_mode *newmode;
346
347                                 std = &data->data.timings[j];
348                                 newmode = drm_mode_std(dev, std);
349                                 drm_mode_probed_add(output, newmode);
350                                 modes++;
351                         }
352                         break;
353                 default:
354                         break;
355                 }
356         }
357
358         return modes;
359 }
360
361 #define DDC_ADDR 0x50
362
363 static unsigned char *drm_do_probe_ddc_edid(struct i2c_adapter *adapter)
364 {
365         unsigned char start = 0x0;
366         unsigned char *buf = kmalloc(EDID_LENGTH, GFP_KERNEL);
367         struct i2c_msg msgs[] = {
368                 {
369                         .addr   = DDC_ADDR,
370                         .flags  = 0,
371                         .len    = 1,
372                         .buf    = &start,
373                 }, {
374                         .addr   = DDC_ADDR,
375                         .flags  = I2C_M_RD,
376                         .len    = EDID_LENGTH,
377                         .buf    = buf,
378                 }
379         };
380
381         if (!buf) {
382                 DRM_ERROR("unable to allocate memory for EDID block.\n");
383                 return NULL;
384         }
385
386         if (i2c_transfer(adapter, msgs, 2) == 2)
387                 return buf;
388
389         DRM_INFO("unable to read EDID block.\n");
390         kfree(buf);
391         return NULL;
392 }
393
394 unsigned char *drm_ddc_read(struct i2c_adapter *adapter)
395 {
396         struct i2c_algo_bit_data *algo_data = adapter->algo_data;
397         unsigned char *edid = NULL;
398         int i, j;
399
400         /*
401          * Startup the bus:
402          *   Set clock line high (but give it time to come up)
403          *   Then set clock & data low
404          */
405         algo_data->setscl(algo_data->data, 1);
406         udelay(550); /* startup delay */
407         algo_data->setscl(algo_data->data, 0);
408         algo_data->setsda(algo_data->data, 0);
409
410         for (i = 0; i < 3; i++) {
411                 /* For some old monitors we need the
412                  * following process to initialize/stop DDC
413                  */
414                 algo_data->setsda(algo_data->data, 0);
415                 msleep(13);
416
417                 algo_data->setscl(algo_data->data, 1);
418                 for (j = 0; j < 5; j++) {
419                         msleep(10);
420                         if (algo_data->getscl(algo_data->data))
421                                 break;
422                 }
423                 if (j == 5)
424                         continue;
425
426                 algo_data->setsda(algo_data->data, 0);
427                 msleep(15);
428                 algo_data->setscl(algo_data->data, 0);
429                 msleep(15);
430                 algo_data->setsda(algo_data->data, 1);
431                 msleep(15);
432
433                 /* Do the real work */
434                 edid = drm_do_probe_ddc_edid(adapter);
435                 algo_data->setsda(algo_data->data, 0);
436                 algo_data->setscl(algo_data->data, 0);
437                 msleep(15);
438
439                 algo_data->setscl(algo_data->data, 1);
440                 for (j = 0; j < 10; j++) {
441                         msleep(10);
442                         if (algo_data->getscl(algo_data->data))
443                                 break;
444                 }
445
446                 algo_data->setsda(algo_data->data, 1);
447                 msleep(15);
448                 algo_data->setscl(algo_data->data, 0);
449                 if (edid)
450                         break;
451         }
452         /* Release the DDC lines when done or the Apple Cinema HD display
453          * will switch off
454          */
455         algo_data->setsda(algo_data->data, 0);
456         algo_data->setscl(algo_data->data, 0);
457         algo_data->setscl(algo_data->data, 1);
458
459         return edid;
460 }
461 EXPORT_SYMBOL(drm_ddc_read);
462
463 /**
464  * drm_get_edid - get EDID data, if available
465  * @output: output we're probing
466  * @adapter: i2c adapter to use for DDC
467  *
468  * Poke the given output's i2c channel to grab EDID data if possible.
469  * 
470  * Return edid data or NULL if we couldn't find any.
471  */
472 struct edid *drm_get_edid(struct drm_output *output,
473                           struct i2c_adapter *adapter)
474 {
475         struct edid *edid;
476
477         edid = (struct edid *)drm_ddc_read(adapter);
478         if (!edid) {
479                 dev_warn(&output->dev->pdev->dev, "%s: no EDID data\n",
480                          output->name);
481                 return NULL;
482         }
483         if (!edid_valid(edid)) {
484                 dev_warn(&output->dev->pdev->dev, "%s: EDID invalid.\n",
485                          output->name);
486                 kfree(edid);
487                 return NULL;
488         }
489         return edid;
490 }
491 EXPORT_SYMBOL(drm_get_edid);
492
493 /**
494  * drm_add_edid_modes - add modes from EDID data, if available
495  * @output: output we're probing
496  * @edid: edid data
497  *
498  * Add the specified modes to the output's mode list.
499  *
500  * Return number of modes added or 0 if we couldn't find any.
501  */
502 int drm_add_edid_modes(struct drm_output *output, struct edid *edid)
503 {
504         int num_modes = 0;
505
506         if (edid == NULL) {
507                 return 0;
508         }
509         if (!edid_valid(edid)) {
510                 dev_warn(&output->dev->pdev->dev, "%s: EDID invalid.\n",
511                          output->name);
512                 return 0;
513         }
514         num_modes += add_established_modes(output, edid);
515         num_modes += add_standard_modes(output, edid);
516         num_modes += add_detailed_info(output, edid);
517         return num_modes;
518 }
519 EXPORT_SYMBOL(drm_add_edid_modes);