1 /* A simple I2C slave for returning monitor EDID data via DDC.
3 * Copyright (c) 2011 Linaro Limited
4 * Written by Peter Maydell
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, see <http://www.gnu.org/licenses/>.
19 #include "qemu/osdep.h"
20 #include "qemu-common.h"
22 #include "hw/i2c/i2c.h"
23 #include "hw/i2c/i2c-ddc.h"
26 #define DEBUG_I2CDDC 0
29 #define DPRINTF(fmt, ...) do { \
31 qemu_log("i2c-ddc: " fmt , ## __VA_ARGS__); \
35 /* Structure defining a monitor's characteristics in a
36 * readable format: this should be passed to build_edid_blob()
37 * to convert it into the 128 byte binary EDID blob.
38 * Not all bits of the EDID are customisable here.
41 char manuf_id
[3]; /* three upper case letters */
49 char monitor_name
[14];
50 char serial_no_string
[14];
52 uint8_t vmin
; /* Hz */
53 uint8_t vmax
; /* Hz */
54 uint8_t hmin
; /* kHz */
55 uint8_t hmax
; /* kHz */
56 uint8_t pixclock
; /* MHz / 10 */
57 uint8_t timing_data
[18];
60 typedef struct EDIDData EDIDData
;
62 /* EDID data for a simple LCD monitor */
63 static const EDIDData lcd_edid
= {
64 /* The manuf_id ought really to be an assigned EISA ID */
73 .monitor_name
= "QEMU monitor",
74 .serial_no_string
= "1",
81 /* Borrowed from a 21" LCD */
82 0x48, 0x3f, 0x40, 0x30, 0x62, 0xb0, 0x32, 0x40, 0x40,
83 0xc0, 0x13, 0x00, 0x98, 0x32, 0x11, 0x00, 0x00, 0x1e
87 static uint8_t manuf_char_to_int(char c
)
89 return (c
- 'A') & 0x1f;
92 static void write_ascii_descriptor_block(uint8_t *descblob
, uint8_t blocktype
,
95 /* Write an EDID Descriptor Block of the "ascii string" type */
97 descblob
[0] = descblob
[1] = descblob
[2] = descblob
[4] = 0;
98 descblob
[3] = blocktype
;
99 /* The rest is 13 bytes of ASCII; if less then the rest must
100 * be filled with newline then spaces
102 for (i
= 5; i
< 19; i
++) {
103 descblob
[i
] = string
[i
- 5];
109 descblob
[i
++] = '\n';
111 for ( ; i
< 19; i
++) {
116 static void write_range_limits_descriptor(const EDIDData
*edid
,
120 descblob
[0] = descblob
[1] = descblob
[2] = descblob
[4] = 0;
122 descblob
[5] = edid
->vmin
;
123 descblob
[6] = edid
->vmax
;
124 descblob
[7] = edid
->hmin
;
125 descblob
[8] = edid
->hmax
;
126 descblob
[9] = edid
->pixclock
;
129 for (i
= 12; i
< 19; i
++) {
134 static void build_edid_blob(const EDIDData
*edid
, uint8_t *blob
)
136 /* Write an EDID 1.3 format blob (128 bytes) based
137 * on the EDIDData structure.
143 blob
[0] = blob
[7] = 0;
144 for (i
= 1 ; i
< 7; i
++) {
147 /* 08-09 : manufacturer ID */
148 blob
[8] = (manuf_char_to_int(edid
->manuf_id
[0]) << 2)
149 | (manuf_char_to_int(edid
->manuf_id
[1]) >> 3);
150 blob
[9] = (manuf_char_to_int(edid
->manuf_id
[1]) << 5)
151 | manuf_char_to_int(edid
->manuf_id
[2]);
152 /* 10-11 : product ID code */
153 blob
[10] = edid
->product_id
;
154 blob
[11] = edid
->product_id
>> 8;
155 blob
[12] = edid
->serial_no
;
156 blob
[13] = edid
->serial_no
>> 8;
157 blob
[14] = edid
->serial_no
>> 16;
158 blob
[15] = edid
->serial_no
>> 24;
159 /* 16 : week of manufacture */
160 blob
[16] = edid
->manuf_week
;
161 /* 17 : year of manufacture - 1990 */
162 blob
[17] = edid
->manuf_year
- 1990;
163 /* 18, 19 : EDID version and revision */
166 /* 20 - 24 : basic display parameters */
167 /* We are always a digital display */
169 /* 21, 22 : max h/v size in cm */
170 blob
[21] = edid
->h_cm
;
171 blob
[22] = edid
->v_cm
;
172 /* 23 : gamma (divide by 100 then add 1 for actual value) */
173 blob
[23] = edid
->gamma
;
174 /* 24 feature support: no power management, RGB, preferred timing mode,
175 * standard colour space
178 /* 25 - 34 : chromaticity coordinates. These are the
179 * standard sRGB chromaticity values
191 /* 35, 36 : Established timings: claim to support everything */
192 blob
[35] = blob
[36] = 0xff;
193 /* 37 : manufacturer's reserved timing: none */
195 /* 38 - 53 : standard timing identification
196 * don't claim anything beyond what the 'established timings'
197 * already provide. Unused slots must be (0x1, 0x1)
199 for (i
= 38; i
< 54; i
++) {
202 /* 54 - 71 : descriptor block 1 : must be preferred timing data */
203 memcpy(blob
+ 54, edid
->timing_data
, 18);
204 /* 72 - 89, 90 - 107, 108 - 125 : descriptor block 2, 3, 4
205 * Order not important, but we must have a monitor name and a
206 * range limits descriptor.
208 write_range_limits_descriptor(edid
, blob
+ 72);
209 write_ascii_descriptor_block(blob
+ 90, 0xfc, edid
->monitor_name
);
210 write_ascii_descriptor_block(blob
+ 108, 0xff, edid
->serial_no_string
);
212 /* 126 : extension flag */
216 for (i
= 0; i
< 127; i
++) {
222 qemu_hexdump((char *)blob
, stdout
, "", 128);
226 static void i2c_ddc_reset(DeviceState
*ds
)
228 I2CDDCState
*s
= I2CDDC(ds
);
230 s
->firstbyte
= false;
234 static int i2c_ddc_event(I2CSlave
*i2c
, enum i2c_event event
)
236 I2CDDCState
*s
= I2CDDC(i2c
);
238 if (event
== I2C_START_SEND
) {
245 static int i2c_ddc_rx(I2CSlave
*i2c
)
247 I2CDDCState
*s
= I2CDDC(i2c
);
250 value
= s
->edid_blob
[s
->reg
];
255 static int i2c_ddc_tx(I2CSlave
*i2c
, uint8_t data
)
257 I2CDDCState
*s
= I2CDDC(i2c
);
260 s
->firstbyte
= false;
261 DPRINTF("[EDID] Written new pointer: %u\n", data
);
265 /* Ignore all writes */
270 static void i2c_ddc_init(Object
*obj
)
272 I2CDDCState
*s
= I2CDDC(obj
);
273 build_edid_blob(&lcd_edid
, s
->edid_blob
);
276 static const VMStateDescription vmstate_i2c_ddc
= {
279 .fields
= (VMStateField
[]) {
280 VMSTATE_BOOL(firstbyte
, I2CDDCState
),
281 VMSTATE_UINT8(reg
, I2CDDCState
),
282 VMSTATE_END_OF_LIST()
286 static void i2c_ddc_class_init(ObjectClass
*oc
, void *data
)
288 DeviceClass
*dc
= DEVICE_CLASS(oc
);
289 I2CSlaveClass
*isc
= I2C_SLAVE_CLASS(oc
);
291 dc
->reset
= i2c_ddc_reset
;
292 dc
->vmsd
= &vmstate_i2c_ddc
;
293 isc
->event
= i2c_ddc_event
;
294 isc
->recv
= i2c_ddc_rx
;
295 isc
->send
= i2c_ddc_tx
;
298 static TypeInfo i2c_ddc_info
= {
300 .parent
= TYPE_I2C_SLAVE
,
301 .instance_size
= sizeof(I2CDDCState
),
302 .instance_init
= i2c_ddc_init
,
303 .class_init
= i2c_ddc_class_init
306 static void ddc_register_devices(void)
308 type_register_static(&i2c_ddc_info
);
311 type_init(ddc_register_devices
);