2 * Copyright (c) 2007 Intel Corporation
3 * Jesse Barnes <jesse.barnes@intel.com>
5 * DDC probing routines (drm_ddc_read & drm_do_probe_ddc_edid) originally from
7 * Copyright (C) 2006 Dennis Munsie <dmunsie@cecropia.com>
10 #include <linux/i2c-algo-bit.h>
13 #include <acpi/acpi_drivers.h>
15 /* Valid EDID header has these bytes */
16 static u8 edid_header[] = { 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 };
18 int drm_get_acpi_edid(char *method, char *edid, ssize_t length)
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 };
27 arg0.integer.value = 1;
28 else if (length == 256)
29 arg0.integer.value = 2;
33 status = acpi_evaluate_object(NULL, method, &args, &buffer);
34 if (ACPI_FAILURE(status))
39 if (obj && obj->type == ACPI_TYPE_BUFFER)
40 memcpy(edid, obj->buffer.pointer, obj->buffer.length);
42 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
49 EXPORT_SYMBOL(drm_get_acpi_edid);
52 * edid_valid - sanity check EDID data
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
59 static bool edid_valid(struct edid *edid)
63 u8 *raw_edid = (u8 *)edid;
65 if (memcmp(edid->header, edid_header, sizeof(edid_header)))
67 if (edid->version != 1)
69 if (edid->revision <= 0 || edid->revision > 3)
72 for (i = 0; i < EDID_LENGTH; i++)
84 * drm_mode_std - convert standard mode info (width, height, refresh) into mode
85 * @t: standard timing params
87 * Take the standard timing params (in this case width, aspect, and refresh)
88 * and convert them into a real mode using CVT.
90 * Punts for now, but should eventually use the FB layer's CVT based mode
93 struct drm_display_mode *drm_mode_std(struct drm_device *dev,
96 // struct fb_videomode mode;
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;
103 mode = drm_mode_create(dev);
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;
114 vsize = (hsize * 9) / 16;
116 drm_mode_set_name(mode);
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?
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.
131 struct drm_display_mode *drm_mode_detailed(struct drm_device *dev,
132 struct detailed_timing *timing)
134 struct drm_display_mode *mode;
135 struct detailed_pixel_timing *pt = &timing->data.pixel_data;
138 printk(KERN_WARNING "stereo mode not supported\n");
141 if (!pt->separate_sync) {
142 printk(KERN_WARNING "integrated sync not supported\n");
146 mode = drm_mode_create(dev);
150 mode->type = DRM_MODE_TYPE_DRIVER;
151 mode->clock = timing->pixel_clock * 10;
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);
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);
169 drm_mode_set_name(mode);
172 mode->flags |= V_INTERLACE;
174 mode->flags |= pt->hsync_positive ? V_PHSYNC : V_NHSYNC;
175 mode->flags |= pt->vsync_positive ? V_PVSYNC : V_NVSYNC;
181 * Detailed mode info for the EDID "established modes" data to use.
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 */
237 #define EDID_EST_TIMINGS 16
238 #define EDID_STD_TIMINGS 8
239 #define EDID_DETAILED_TIMINGS 4
242 * add_established_modes - get est. modes from EDID and add them
243 * @edid: EDID block to scan
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.
248 static int add_established_modes(struct drm_output *output, struct edid *edid)
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);
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);
268 * add_standard_modes - get std. modes from EDID and add them
269 * @edid: EDID block to scan
271 * Standard modes can be calculated using the CVT standard. Grab them from
272 * @edid, calculate them, and add them to the list.
274 static int add_standard_modes(struct drm_output *output, struct edid *edid)
276 struct drm_device *dev = output->dev;
279 for (i = 0; i < EDID_STD_TIMINGS; i++) {
280 struct std_timing *t = &edid->standard_timings[i];
281 struct drm_display_mode *newmode;
283 /* If std timings bytes are 1, 1 it's empty */
284 if (t->hsize == 1 && (t->aspect_ratio | t->vfreq) == 1)
287 newmode = drm_mode_std(dev, &edid->standard_timings[i]);
288 drm_mode_probed_add(output, newmode);
296 * add_detailed_modes - get detailed mode info from EDID data
297 * @edid: EDID block to scan
299 * Some of the detailed timing sections may contain mode information. Grab
300 * it and add it to the list.
302 static int add_detailed_info(struct drm_output *output, struct edid *edid)
304 struct drm_device *dev = output->dev;
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;
312 /* EDID up to and including 1.2 may put monitor info here */
313 if (edid->version == 1 && edid->revision < 3)
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);
328 /* Other timing or info */
329 switch (data->type) {
330 case EDID_DETAIL_MONITOR_SERIAL:
332 case EDID_DETAIL_MONITOR_STRING:
334 case EDID_DETAIL_MONITOR_RANGE:
335 /* Get monitor range data */
337 case EDID_DETAIL_MONITOR_NAME:
339 case EDID_DETAIL_MONITOR_CPDATA:
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;
347 std = &data->data.timings[j];
348 newmode = drm_mode_std(dev, std);
349 drm_mode_probed_add(output, newmode);
361 #define DDC_ADDR 0x50
363 static unsigned char *drm_do_probe_ddc_edid(struct i2c_adapter *adapter)
365 unsigned char start = 0x0;
366 unsigned char *buf = kmalloc(EDID_LENGTH, GFP_KERNEL);
367 struct i2c_msg msgs[] = {
382 DRM_ERROR("unable to allocate memory for EDID block.\n");
386 if (i2c_transfer(adapter, msgs, 2) == 2)
389 DRM_INFO("unable to read EDID block.\n");
394 unsigned char *drm_ddc_read(struct i2c_adapter *adapter)
396 struct i2c_algo_bit_data *algo_data = adapter->algo_data;
397 unsigned char *edid = NULL;
402 * Set clock line high (but give it time to come up)
403 * Then set clock & data low
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);
410 for (i = 0; i < 3; i++) {
411 /* For some old monitors we need the
412 * following process to initialize/stop DDC
414 algo_data->setsda(algo_data->data, 0);
417 algo_data->setscl(algo_data->data, 1);
418 for (j = 0; j < 5; j++) {
420 if (algo_data->getscl(algo_data->data))
426 algo_data->setsda(algo_data->data, 0);
428 algo_data->setscl(algo_data->data, 0);
430 algo_data->setsda(algo_data->data, 1);
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);
439 algo_data->setscl(algo_data->data, 1);
440 for (j = 0; j < 10; j++) {
442 if (algo_data->getscl(algo_data->data))
446 algo_data->setsda(algo_data->data, 1);
448 algo_data->setscl(algo_data->data, 0);
452 /* Release the DDC lines when done or the Apple Cinema HD display
455 algo_data->setsda(algo_data->data, 0);
456 algo_data->setscl(algo_data->data, 0);
457 algo_data->setscl(algo_data->data, 1);
461 EXPORT_SYMBOL(drm_ddc_read);
464 * drm_get_edid - get EDID data, if available
465 * @output: output we're probing
466 * @adapter: i2c adapter to use for DDC
468 * Poke the given output's i2c channel to grab EDID data if possible.
470 * Return edid data or NULL if we couldn't find any.
472 struct edid *drm_get_edid(struct drm_output *output,
473 struct i2c_adapter *adapter)
477 edid = (struct edid *)drm_ddc_read(adapter);
479 dev_warn(&output->dev->pdev->dev, "%s: no EDID data\n",
483 if (!edid_valid(edid)) {
484 dev_warn(&output->dev->pdev->dev, "%s: EDID invalid.\n",
491 EXPORT_SYMBOL(drm_get_edid);
494 * drm_add_edid_modes - add modes from EDID data, if available
495 * @output: output we're probing
498 * Add the specified modes to the output's mode list.
500 * Return number of modes added or 0 if we couldn't find any.
502 int drm_add_edid_modes(struct drm_output *output, struct edid *edid)
509 if (!edid_valid(edid)) {
510 dev_warn(&output->dev->pdev->dev, "%s: EDID invalid.\n",
514 num_modes += add_established_modes(output, edid);
515 num_modes += add_standard_modes(output, edid);
516 num_modes += add_detailed_info(output, edid);
519 EXPORT_SYMBOL(drm_add_edid_modes);