30 #include <linux/module.h>
31 #include <linux/pci.h>
32 #include <linux/slab.h>
33 #include <video/edid.h>
36 #include <asm/pci-bridge.h>
47 #define DPRINTK(fmt, args...) printk(fmt,## args)
49 #define DPRINTK(fmt, args...)
52 #define FBMON_FIX_HEADER 1
53 #define FBMON_FIX_INPUT 2
54 #define FBMON_FIX_TIMINGS 3
56 #ifdef CONFIG_FB_MODE_HELPERS
63 static const struct broken_edid brokendb[] = {
66 .manufacturer =
"DEC",
72 .manufacturer =
"VSC",
78 .manufacturer =
"SHP",
84 static const unsigned char edid_v1_header[] = { 0x00, 0xff, 0xff, 0xff,
85 0xff, 0xff, 0xff, 0x00
88 static void copy_string(
unsigned char *
c,
unsigned char *
s)
92 for (i = 0; (i < 13 && *c != 0x0A); i++)
95 while (i-- && (*--s == 0x20)) *s = 0;
98 static int edid_is_serial_block(
unsigned char *
block)
100 if ((block[0] == 0x00) && (block[1] == 0x00) &&
101 (block[2] == 0x00) && (block[3] == 0xff) &&
108 static int edid_is_ascii_block(
unsigned char *block)
110 if ((block[0] == 0x00) && (block[1] == 0x00) &&
111 (block[2] == 0x00) && (block[3] == 0xfe) &&
118 static int edid_is_limits_block(
unsigned char *block)
120 if ((block[0] == 0x00) && (block[1] == 0x00) &&
121 (block[2] == 0x00) && (block[3] == 0xfd) &&
128 static int edid_is_monitor_block(
unsigned char *block)
130 if ((block[0] == 0x00) && (block[1] == 0x00) &&
131 (block[2] == 0x00) && (block[3] == 0xfc) &&
138 static int edid_is_timing_block(
unsigned char *block)
140 if ((block[0] != 0x00) || (block[1] != 0x00) ||
141 (block[2] != 0x00) || (block[4] != 0x00))
147 static int check_edid(
unsigned char *
edid)
152 int i, fix = 0,
ret = 0;
154 manufacturer[0] = ((block[0] & 0x7c) >> 2) +
'@';
155 manufacturer[1] = ((block[0] & 0x03) << 3) +
156 ((block[1] & 0xe0) >> 5) +
'@';
157 manufacturer[2] = (block[1] & 0x1f) +
'@';
159 model = block[2] + (block[3] << 8);
162 if (!
strncmp(manufacturer, brokendb[i].manufacturer, 4) &&
163 brokendb[
i].model == model) {
164 fix = brokendb[
i].fix;
171 for (i = 0; i < 8; i++) {
172 if (edid[i] != edid_v1_header[i]) {
182 if (b[4] & 0x01 && b[0] & 0x80)
189 for (i = 0; i < 4; i++) {
190 if (edid_is_limits_block(b)) {
202 printk(
"fbmon: The EDID Block of "
203 "Manufacturer: %s Model: 0x%x is known to "
204 "be broken,\n", manufacturer, model);
209 static void fix_edid(
unsigned char *edid,
int fix)
212 unsigned char *
b,
csum = 0;
216 printk(
"fbmon: trying a header reconstruct\n");
217 memcpy(edid, edid_v1_header, 8);
220 printk(
"fbmon: trying to fix input type\n");
226 printk(
"fbmon: trying to fix monitor timings\n");
228 for (i = 0; i < 4; i++) {
229 if (!(edid_is_serial_block(b) ||
230 edid_is_ascii_block(b) ||
231 edid_is_monitor_block(b) ||
232 edid_is_timing_block(b))) {
253 edid[127] = 256 -
csum;
258 static int edid_checksum(
unsigned char *edid)
260 unsigned char csum = 0, all_null = 0;
261 int i,
err = 0, fix = check_edid(edid);
271 if (csum == 0x00 && all_null) {
279 static int edid_check_header(
unsigned char *edid)
281 int i, err = 1, fix = check_edid(edid);
286 for (i = 0; i < 8; i++) {
287 if (edid[i] != edid_v1_header[i])
294 static void parse_vendor_block(
unsigned char *block,
struct fb_monspecs *specs)
296 specs->
manufacturer[0] = ((block[0] & 0x7c) >> 2) +
'@';
298 ((block[1] & 0xe0) >> 5) +
'@';
301 specs->
model = block[2] + (block[3] << 8);
302 specs->
serial = block[4] + (block[5] << 8) +
303 (block[6] << 16) + (block[7] << 24);
304 specs->
year = block[9] + 1990;
305 specs->
week = block[8];
312 static void get_dpms_capabilities(
unsigned char flags,
322 DPRINTK(
" DPMS: Active %s, Suspend %s, Standby %s\n",
323 (flags & DPMS_ACTIVE_OFF) ?
"yes" :
"no",
324 (flags & DPMS_SUSPEND) ?
"yes" :
"no",
325 (flags & DPMS_STANDBY) ?
"yes" :
"no");
328 static void get_chroma(
unsigned char *block,
struct fb_monspecs *specs)
334 tmp = ((block[5] & (3 << 6)) >> 6) | (block[0x7] << 2);
337 specs->
chroma.redx = tmp/1024;
340 tmp = ((block[5] & (3 << 4)) >> 4) | (block[0x8] << 2);
343 specs->
chroma.redy = tmp/1024;
346 tmp = ((block[5] & (3 << 2)) >> 2) | (block[0x9] << 2);
349 specs->
chroma.greenx = tmp/1024;
352 tmp = (block[5] & 3) | (block[0xa] << 2);
355 specs->
chroma.greeny = tmp/1024;
358 tmp = ((block[6] & (3 << 6)) >> 6) | (block[0
xb] << 2);
361 specs->
chroma.bluex = tmp/1024;
364 tmp = ((block[6] & (3 << 4)) >> 4) | (block[0
xc] << 2);
367 specs->
chroma.bluey = tmp/1024;
370 tmp = ((block[6] & (3 << 2)) >> 2) | (block[0xd] << 2);
373 specs->
chroma.whitex = tmp/1024;
376 tmp = (block[6] & 3) | (block[0
xe] << 2);
379 specs->
chroma.whitey = tmp/1024;
383 static void calc_mode_timings(
int xres,
int yres,
int refresh,
411 static int get_est_timing(
unsigned char *block,
struct fb_videomode *mode)
418 calc_mode_timings(720, 400, 70, &mode[num]);
423 calc_mode_timings(720, 400, 88, &mode[num]);
432 calc_mode_timings(640, 480, 67, &mode[num]);
463 calc_mode_timings(832, 624, 75, &mode[num]);
469 DPRINTK(
" 1024x768@87Hz Interlaced\n");
492 DPRINTK(
" Manufacturer's mask: %x\n",c&0x7F);
496 static int get_std_timing(
unsigned char *block,
struct fb_videomode *mode,
501 xres = (block[0] + 31) * 8;
505 ratio = (block[1] & 0xc0) >> 6;
509 if (ver < 1 || (ver == 1 && rev < 3))
512 yres = (xres * 10)/16;
521 yres = (xres * 9)/16;
524 refresh = (block[1] & 0x3f) + 60;
526 DPRINTK(
" %dx%d@%dHz\n", xres, yres, refresh);
528 if (vesa_modes[i].xres == xres &&
529 vesa_modes[i].yres == yres &&
530 vesa_modes[i].refresh == refresh) {
536 calc_mode_timings(xres, yres, refresh, mode);
540 static int get_dst_timing(
unsigned char *block,
546 num += get_std_timing(block, &mode[num], ver, rev);
551 static void get_detailed_timing(
unsigned char *block,
602 static struct fb_videomode *fb_create_modedb(
unsigned char *edid,
int *dbsize)
605 unsigned char *
block;
606 int num = 0,
i,
first = 1;
616 if (edid ==
NULL || !edid_checksum(edid) ||
617 !edid_check_header(edid)) {
624 DPRINTK(
" Detailed Timings\n");
627 if (!(block[0] == 0x00 && block[1] == 0x00)) {
628 get_detailed_timing(block, &mode[num]);
637 DPRINTK(
" Supported VESA Modes\n");
639 num += get_est_timing(block, &mode[num]);
641 DPRINTK(
" Standard Timings\n");
644 num += get_std_timing(block, &mode[num], ver, rev);
648 if (block[0] == 0x00 && block[1] == 0x00 && block[3] == 0xfa)
649 num += get_dst_timing(block + 5, &mode[num], ver, rev);
679 static int fb_get_monitor_limits(
unsigned char *edid,
struct fb_monspecs *specs)
682 unsigned char *
block;
686 DPRINTK(
" Monitor Operating Limits: ");
689 if (edid_is_limits_block(block)) {
708 modes = fb_create_modedb(edid, &num_modes);
715 for (i = 0; i < num_modes; i++) {
717 pixclock =
PICOS2KHZ(modes[i].pixclock) * 1000;
729 hscan = (pixclock + htotal / 2) / htotal;
730 hscan = (hscan + 500) / 1000 * 1000;
731 hz = (hscan + vtotal / 2) / vtotal;
739 if (specs->
hfmax == 0 || specs->
hfmax < hscan)
740 specs->
hfmax = hscan;
742 if (specs->
hfmin == 0 || specs->
hfmin > hscan)
743 specs->
hfmin = hscan;
754 DPRINTK(
" H: %d-%dKHz V: %d-%dHz DCLK: %dMHz\n",
760 static void get_monspecs(
unsigned char *edid,
struct fb_monspecs *specs)
766 fb_get_monitor_limits(edid, specs);
772 DPRINTK(
" Digital Display Input");
774 DPRINTK(
" Analog Display Input: Input Voltage - ");
775 switch ((block[0] & 0x60) >> 5) {
797 DPRINTK(
" Configurable signal level\n");
821 specs->
max_x = block[1];
822 specs->
max_y = block[2];
823 DPRINTK(
" Max H-size in cm: ");
828 DPRINTK(
" Max V-size in cm: ");
835 specs->
gamma = c+100;
839 get_dpms_capabilities(block[4], specs);
841 switch ((block[4] & 0x18) >> 3) {
843 DPRINTK(
" Monochrome/Grayscale\n");
847 DPRINTK(
" RGB Color Display\n");
851 DPRINTK(
" Non-RGB Multicolor Display\n");
860 get_chroma(block, specs);
865 DPRINTK(
" Default color format is primary\n");
869 DPRINTK(
" First DETAILED Timing is preferred\n");
873 printk(
" Display is GTF capable\n");
881 unsigned char *
block;
886 if (!(edid_checksum(edid)))
889 if (!(edid_check_header(edid)))
895 if (edid_is_timing_block(block)) {
923 unsigned char *
block;
929 if (!(edid_checksum(edid)))
932 if (!(edid_check_header(edid)))
940 DPRINTK(
"========================================\n");
941 DPRINTK(
"Display Information (EDID)\n");
942 DPRINTK(
"========================================\n");
946 parse_vendor_block(edid + ID_MANUFACTURER_NAME, specs);
950 if (edid_is_serial_block(block)) {
953 }
else if (edid_is_ascii_block(block)) {
954 copy_string(block, specs->
ascii);
956 }
else if (edid_is_monitor_block(block)) {
957 copy_string(block, specs->
monitor);
962 DPRINTK(
" Display Characteristics:\n");
963 get_monspecs(edid, specs);
982 DPRINTK(
"========================================\n");
992 unsigned char *
block;
996 u8 pos = 4, svd_n = 0;
1001 if (!edid_checksum(edid))
1004 if (edid[0] != 0x2 ||
1008 DPRINTK(
" Short Video Descriptors\n");
1010 while (pos < edid[2]) {
1012 pr_debug(
"Data block %u of %u bytes\n", type, len);
1014 for (i = pos; i < pos + len; i++) {
1015 u8 idx = edid[pos +
i] & 0x7f;
1018 edid[pos + i] & 0x80 ?
"" :
"on-n", idx);
1023 block = edid + edid[2];
1025 DPRINTK(
" Extended Detailed Timings\n");
1030 edt[num++] = block - edid;
1036 m = kzalloc((specs->
modedb_len + num + svd_n) *
1044 for (i = specs->
modedb_len; i < specs->modedb_len + num; i++) {
1045 get_detailed_timing(edid + edt[i - specs->
modedb_len], &m[i]);
1048 pr_debug(
"Adding %ux%u@%u\n", m[i].xres, m[i].yres, m[i].refresh);
1051 for (i = specs->
modedb_len + num; i < specs->modedb_len + num + svd_n; i++) {
1053 if (!idx || idx > 63) {
1055 }
else if (idx >
ARRAY_SIZE(cea_modes) || !cea_modes[idx].xres) {
1056 pr_warning(
"Unimplemented SVD code %d\n", idx);
1058 memcpy(&m[i], cea_modes + idx,
sizeof(m[i]));
1059 pr_debug(
"Adding SVD #%d: %ux%u@%u\n", idx,
1060 m[i].xres, m[i].yres, m[i].refresh);
1074 #define V_FRONTPORCH 1
1076 #define H_SCALEFACTOR 20
1077 #define H_BLANKSCALE 128
1078 #define H_GRADIENT 600
1082 struct __fb_timings {
1109 static u32 fb_get_vblank(
u32 hfreq)
1113 vblank = (hfreq * FLYBACK)/1000;
1114 vblank = (vblank + 500)/1000;
1115 return (vblank + V_FRONTPORCH);
1138 static u32 fb_get_hblank_by_hfreq(
u32 hfreq,
u32 xres)
1140 u32 c_val, m_val, duty_cycle, hblank;
1142 c_val = (((H_OFFSET - H_SCALEFACTOR) * H_BLANKSCALE)/256 +
1143 H_SCALEFACTOR) * 1000;
1144 m_val = (H_BLANKSCALE * H_GRADIENT)/256;
1145 m_val = (m_val * 1000000)/hfreq;
1146 duty_cycle = c_val - m_val;
1147 hblank = (xres * duty_cycle)/(100000 - duty_cycle);
1174 u32 duty_cycle, h_period, hblank;
1177 h_period = 100 - C_VAL;
1178 h_period *= h_period;
1179 h_period += (M_VAL * xres * 2 * 1000)/(5 * dclk);
1183 h_period -= (100 - C_VAL) * 100;
1185 h_period /= 2 * M_VAL;
1187 duty_cycle = C_VAL * 1000 - (M_VAL * h_period)/100;
1188 hblank = (xres * duty_cycle)/(100000 - duty_cycle) + 8;
1210 divisor = (1000000 - (vfreq * FLYBACK))/1000;
1211 hfreq = (yres + V_FRONTPORCH) * vfreq * 1000;
1212 return (hfreq/divisor);
1215 static void fb_timings_vfreq(
struct __fb_timings *timings)
1217 timings->hfreq = fb_get_hfreq(timings->vfreq, timings->vactive);
1218 timings->vblank = fb_get_vblank(timings->hfreq);
1219 timings->vtotal = timings->vactive + timings->vblank;
1220 timings->hblank = fb_get_hblank_by_hfreq(timings->hfreq,
1222 timings->htotal = timings->hactive + timings->hblank;
1223 timings->dclk = timings->htotal * timings->hfreq;
1226 static void fb_timings_hfreq(
struct __fb_timings *timings)
1228 timings->vblank = fb_get_vblank(timings->hfreq);
1229 timings->vtotal = timings->vactive + timings->vblank;
1230 timings->vfreq = timings->hfreq/timings->vtotal;
1231 timings->hblank = fb_get_hblank_by_hfreq(timings->hfreq,
1233 timings->htotal = timings->hactive + timings->hblank;
1234 timings->dclk = timings->htotal * timings->hfreq;
1237 static void fb_timings_dclk(
struct __fb_timings *timings)
1239 timings->hblank = fb_get_hblank_by_dclk(timings->dclk,
1241 timings->htotal = timings->hactive + timings->hblank;
1242 timings->hfreq = timings->dclk/timings->htotal;
1243 timings->vblank = fb_get_vblank(timings->hfreq);
1244 timings->vtotal = timings->vactive + timings->vblank;
1245 timings->vfreq = timings->hfreq/timings->vtotal;
1286 u32 hfmin, hfmax, vfmin, vfmax, dclkmin, dclkmax, err = 0;
1289 timings = kzalloc(
sizeof(
struct __fb_timings),
GFP_KERNEL);
1303 hfmin = 29000; hfmax = 30000;
1304 vfmin = 60; vfmax = 60;
1305 dclkmin = 0; dclkmax = 25000000;
1315 timings->hactive = var->
xres;
1316 timings->vactive = var->
yres;
1318 timings->vactive /= 2;
1322 timings->vactive *= 2;
1328 timings->hfreq = hfmax;
1329 fb_timings_hfreq(timings);
1330 if (timings->vfreq > vfmax) {
1331 timings->vfreq = vfmax;
1332 fb_timings_vfreq(timings);
1334 if (timings->dclk > dclkmax) {
1335 timings->dclk = dclkmax;
1336 fb_timings_dclk(timings);
1340 timings->vfreq =
val;
1341 fb_timings_vfreq(timings);
1344 timings->hfreq =
val;
1345 fb_timings_hfreq(timings);
1349 fb_timings_dclk(timings);
1357 (timings->vfreq < vfmin || timings->vfreq > vfmax ||
1358 timings->hfreq < hfmin || timings->hfreq > hfmax ||
1359 timings->dclk < dclkmin || timings->dclk > dclkmax))) {
1363 var->
hsync_len = (timings->htotal * 8)/100;
1412 u32 hfreq,
vfreq, htotal, vtotal, pixclock;
1413 u32 hfmin, hfmax, vfmin, vfmax, dclkmin, dclkmax;
1424 hfmin = 29000; hfmax = 30000;
1425 vfmin = 60; vfmax = 60;
1426 dclkmin = 0; dclkmax = 25000000;
1450 hfreq = pixclock/htotal;
1451 hfreq = (hfreq + 500) / 1000 * 1000;
1453 vfreq = hfreq/vtotal;
1455 return (vfreq < vfmin || vfreq > vfmax ||
1456 hfreq < hfmin || hfreq > hfmax ||
1457 pixclock < dclkmin || pixclock > dclkmax) ?
1461 #if defined(CONFIG_FIRMWARE_EDID) && defined(CONFIG_X86)
1472 unsigned char *edid =
NULL;