1 /* A simple I2C slave for returning monitor EDID data via DDC. 2 * 3 * Copyright (c) 2011 Linaro Limited 4 * Written by Peter Maydell 5 * 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. 9 * 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. 14 * 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/>. 17 */ 18 19 #include "qemu/osdep.h" 20 #include "qemu-common.h" 21 #include "qemu/log.h" 22 #include "hw/i2c/i2c.h" 23 #include "hw/display/i2c-ddc.h" 24 25 #ifndef DEBUG_I2CDDC 26 #define DEBUG_I2CDDC 0 27 #endif 28 29 #define DPRINTF(fmt, ...) do { \ 30 if (DEBUG_I2CDDC) { \ 31 qemu_log("i2c-ddc: " fmt , ## __VA_ARGS__); \ 32 } \ 33 } while (0) 34 35 static void i2c_ddc_reset(DeviceState *ds) 36 { 37 I2CDDCState *s = I2CDDC(ds); 38 39 s->firstbyte = false; 40 s->reg = 0; 41 } 42 43 static int i2c_ddc_event(I2CSlave *i2c, enum i2c_event event) 44 { 45 I2CDDCState *s = I2CDDC(i2c); 46 47 if (event == I2C_START_SEND) { 48 s->firstbyte = true; 49 } 50 51 return 0; 52 } 53 54 static uint8_t i2c_ddc_rx(I2CSlave *i2c) 55 { 56 I2CDDCState *s = I2CDDC(i2c); 57 58 int value; 59 value = s->edid_blob[s->reg % sizeof(s->edid_blob)]; 60 s->reg++; 61 return value; 62 } 63 64 static int i2c_ddc_tx(I2CSlave *i2c, uint8_t data) 65 { 66 I2CDDCState *s = I2CDDC(i2c); 67 if (s->firstbyte) { 68 s->reg = data; 69 s->firstbyte = false; 70 DPRINTF("[EDID] Written new pointer: %u\n", data); 71 return 0; 72 } 73 74 /* Ignore all writes */ 75 s->reg++; 76 return 0; 77 } 78 79 static void i2c_ddc_init(Object *obj) 80 { 81 I2CDDCState *s = I2CDDC(obj); 82 83 qemu_edid_generate(s->edid_blob, sizeof(s->edid_blob), &s->edid_info); 84 } 85 86 static const VMStateDescription vmstate_i2c_ddc = { 87 .name = TYPE_I2CDDC, 88 .version_id = 1, 89 .fields = (VMStateField[]) { 90 VMSTATE_BOOL(firstbyte, I2CDDCState), 91 VMSTATE_UINT8(reg, I2CDDCState), 92 VMSTATE_END_OF_LIST() 93 } 94 }; 95 96 static Property i2c_ddc_properties[] = { 97 DEFINE_EDID_PROPERTIES(I2CDDCState, edid_info), 98 DEFINE_PROP_END_OF_LIST(), 99 }; 100 101 static void i2c_ddc_class_init(ObjectClass *oc, void *data) 102 { 103 DeviceClass *dc = DEVICE_CLASS(oc); 104 I2CSlaveClass *isc = I2C_SLAVE_CLASS(oc); 105 106 dc->reset = i2c_ddc_reset; 107 dc->vmsd = &vmstate_i2c_ddc; 108 dc->props = i2c_ddc_properties; 109 isc->event = i2c_ddc_event; 110 isc->recv = i2c_ddc_rx; 111 isc->send = i2c_ddc_tx; 112 } 113 114 static TypeInfo i2c_ddc_info = { 115 .name = TYPE_I2CDDC, 116 .parent = TYPE_I2C_SLAVE, 117 .instance_size = sizeof(I2CDDCState), 118 .instance_init = i2c_ddc_init, 119 .class_init = i2c_ddc_class_init 120 }; 121 122 static void ddc_register_devices(void) 123 { 124 type_register_static(&i2c_ddc_info); 125 } 126 127 type_init(ddc_register_devices); 128