i2c-ddc.c 3.2 KB

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