hw/arm/iotkit: Wire up the watchdogs
[qemu/ar7.git] / hw / i2c / i2c-ddc.c
blobbec0c91e2dd0d95b9bfa8166432042e63f7eaab7
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"
21 #include "qemu/log.h"
22 #include "hw/i2c/i2c.h"
23 #include "hw/i2c/i2c-ddc.h"
25 #ifndef DEBUG_I2CDDC
26 #define DEBUG_I2CDDC 0
27 #endif
29 #define DPRINTF(fmt, ...) do { \
30 if (DEBUG_I2CDDC) { \
31 qemu_log("i2c-ddc: " fmt , ## __VA_ARGS__); \
32 } \
33 } while (0)
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.
40 struct EDIDData {
41 char manuf_id[3]; /* three upper case letters */
42 uint16_t product_id;
43 uint32_t serial_no;
44 uint8_t manuf_week;
45 int manuf_year;
46 uint8_t h_cm;
47 uint8_t v_cm;
48 uint8_t gamma;
49 char monitor_name[14];
50 char serial_no_string[14];
51 /* Range limits */
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 */
65 .manuf_id = "QMU",
66 .product_id = 0,
67 .serial_no = 1,
68 .manuf_week = 1,
69 .manuf_year = 2011,
70 .h_cm = 40,
71 .v_cm = 30,
72 .gamma = 0x78,
73 .monitor_name = "QEMU monitor",
74 .serial_no_string = "1",
75 .vmin = 40,
76 .vmax = 120,
77 .hmin = 30,
78 .hmax = 100,
79 .pixclock = 18,
80 .timing_data = {
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,
93 const char *string)
95 /* Write an EDID Descriptor Block of the "ascii string" type */
96 int i;
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];
104 if (!descblob[i]) {
105 break;
108 if (i < 19) {
109 descblob[i++] = '\n';
111 for ( ; i < 19; i++) {
112 descblob[i] = ' ';
116 static void write_range_limits_descriptor(const EDIDData *edid,
117 uint8_t *descblob)
119 int i;
120 descblob[0] = descblob[1] = descblob[2] = descblob[4] = 0;
121 descblob[3] = 0xfd;
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;
127 descblob[10] = 0;
128 descblob[11] = 0xa;
129 for (i = 12; i < 19; i++) {
130 descblob[i] = 0x20;
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.
139 int i;
140 uint8_t cksum;
142 /* 00-07 : header */
143 blob[0] = blob[7] = 0;
144 for (i = 1 ; i < 7; i++) {
145 blob[i] = 0xff;
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 */
164 blob[18] = 1;
165 blob[19] = 3;
166 /* 20 - 24 : basic display parameters */
167 /* We are always a digital display */
168 blob[20] = 0x80;
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
177 blob[24] = 0x0e;
178 /* 25 - 34 : chromaticity coordinates. These are the
179 * standard sRGB chromaticity values
181 blob[25] = 0xee;
182 blob[26] = 0x91;
183 blob[27] = 0xa3;
184 blob[28] = 0x54;
185 blob[29] = 0x4c;
186 blob[30] = 0x99;
187 blob[31] = 0x26;
188 blob[32] = 0x0f;
189 blob[33] = 0x50;
190 blob[34] = 0x54;
191 /* 35, 36 : Established timings: claim to support everything */
192 blob[35] = blob[36] = 0xff;
193 /* 37 : manufacturer's reserved timing: none */
194 blob[37] = 0;
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++) {
200 blob[i] = 0x1;
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 */
213 blob[126] = 0;
215 cksum = 0;
216 for (i = 0; i < 127; i++) {
217 cksum += blob[i];
219 /* 127 : checksum */
220 blob[127] = -cksum;
221 if (DEBUG_I2CDDC) {
222 qemu_hexdump((char *)blob, stdout, "", 128);
226 static void i2c_ddc_reset(DeviceState *ds)
228 I2CDDCState *s = I2CDDC(ds);
230 s->firstbyte = false;
231 s->reg = 0;
234 static int i2c_ddc_event(I2CSlave *i2c, enum i2c_event event)
236 I2CDDCState *s = I2CDDC(i2c);
238 if (event == I2C_START_SEND) {
239 s->firstbyte = true;
242 return 0;
245 static int i2c_ddc_rx(I2CSlave *i2c)
247 I2CDDCState *s = I2CDDC(i2c);
249 int value;
250 value = s->edid_blob[s->reg];
251 s->reg++;
252 return value;
255 static int i2c_ddc_tx(I2CSlave *i2c, uint8_t data)
257 I2CDDCState *s = I2CDDC(i2c);
258 if (s->firstbyte) {
259 s->reg = data;
260 s->firstbyte = false;
261 DPRINTF("[EDID] Written new pointer: %u\n", data);
262 return 0;
265 /* Ignore all writes */
266 s->reg++;
267 return 0;
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 = {
277 .name = TYPE_I2CDDC,
278 .version_id = 1,
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 = {
299 .name = TYPE_I2CDDC,
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);