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