2
0

pcf8574.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * NXP PCF8574 8-port I2C GPIO expansion chip.
  4. * Copyright (c) 2024 KNS Group (YADRO).
  5. * Written by Dmitrii Sharikhin <d.sharikhin@yadro.com>
  6. */
  7. #include "qemu/osdep.h"
  8. #include "hw/i2c/i2c.h"
  9. #include "hw/gpio/pcf8574.h"
  10. #include "hw/irq.h"
  11. #include "migration/vmstate.h"
  12. #include "qemu/log.h"
  13. #include "qemu/module.h"
  14. #include "qom/object.h"
  15. /*
  16. * PCF8574 and compatible chips incorporate quasi-bidirectional
  17. * IO. Electrically it means that device sustain pull-up to line
  18. * unless IO port is configured as output _and_ driven low.
  19. *
  20. * IO access is implemented as simple I2C single-byte read
  21. * or write operation. So, to configure line to input user write 1
  22. * to corresponding bit. To configure line to output and drive it low
  23. * user write 0 to corresponding bit.
  24. *
  25. * In essence, user can think of quasi-bidirectional IO as
  26. * open-drain line, except presence of builtin rising edge acceleration
  27. * embedded in PCF8574 IC
  28. *
  29. * PCF8574 has interrupt request line, which is being pulled down when
  30. * port line state differs from last read. Port read operation clears
  31. * state and INT line returns to high state via pullup.
  32. */
  33. OBJECT_DECLARE_SIMPLE_TYPE(PCF8574State, PCF8574)
  34. #define PORTS_COUNT (8)
  35. struct PCF8574State {
  36. I2CSlave parent_obj;
  37. uint8_t lastrq; /* Last requested state. If changed - assert irq */
  38. uint8_t input; /* external electrical line state */
  39. uint8_t output; /* Pull-up (1) or drive low (0) on bit */
  40. qemu_irq handler[PORTS_COUNT];
  41. qemu_irq intrq; /* External irq request */
  42. };
  43. static void pcf8574_reset(DeviceState *dev)
  44. {
  45. PCF8574State *s = PCF8574(dev);
  46. s->lastrq = MAKE_64BIT_MASK(0, PORTS_COUNT);
  47. s->input = MAKE_64BIT_MASK(0, PORTS_COUNT);
  48. s->output = MAKE_64BIT_MASK(0, PORTS_COUNT);
  49. }
  50. static inline uint8_t pcf8574_line_state(PCF8574State *s)
  51. {
  52. /* we driving line low or external circuit does that */
  53. return s->input & s->output;
  54. }
  55. static uint8_t pcf8574_rx(I2CSlave *i2c)
  56. {
  57. PCF8574State *s = PCF8574(i2c);
  58. uint8_t linestate = pcf8574_line_state(s);
  59. if (s->lastrq != linestate) {
  60. s->lastrq = linestate;
  61. if (s->intrq) {
  62. qemu_set_irq(s->intrq, 1);
  63. }
  64. }
  65. return linestate;
  66. }
  67. static int pcf8574_tx(I2CSlave *i2c, uint8_t data)
  68. {
  69. PCF8574State *s = PCF8574(i2c);
  70. uint8_t prev;
  71. uint8_t diff;
  72. uint8_t actual;
  73. int line = 0;
  74. prev = pcf8574_line_state(s);
  75. s->output = data;
  76. actual = pcf8574_line_state(s);
  77. for (diff = (actual ^ prev); diff; diff &= ~(1 << line)) {
  78. line = ctz32(diff);
  79. if (s->handler[line]) {
  80. qemu_set_irq(s->handler[line], (actual >> line) & 1);
  81. }
  82. }
  83. if (s->intrq) {
  84. qemu_set_irq(s->intrq, actual == s->lastrq);
  85. }
  86. return 0;
  87. }
  88. static const VMStateDescription vmstate_pcf8574 = {
  89. .name = "pcf8574",
  90. .version_id = 0,
  91. .minimum_version_id = 0,
  92. .fields = (VMStateField[]) {
  93. VMSTATE_I2C_SLAVE(parent_obj, PCF8574State),
  94. VMSTATE_UINT8(lastrq, PCF8574State),
  95. VMSTATE_UINT8(input, PCF8574State),
  96. VMSTATE_UINT8(output, PCF8574State),
  97. VMSTATE_END_OF_LIST()
  98. }
  99. };
  100. static void pcf8574_gpio_set(void *opaque, int line, int level)
  101. {
  102. PCF8574State *s = (PCF8574State *) opaque;
  103. assert(line >= 0 && line < ARRAY_SIZE(s->handler));
  104. if (level) {
  105. s->input |= (1 << line);
  106. } else {
  107. s->input &= ~(1 << line);
  108. }
  109. if (pcf8574_line_state(s) != s->lastrq && s->intrq) {
  110. qemu_set_irq(s->intrq, 0);
  111. }
  112. }
  113. static void pcf8574_realize(DeviceState *dev, Error **errp)
  114. {
  115. PCF8574State *s = PCF8574(dev);
  116. qdev_init_gpio_in(dev, pcf8574_gpio_set, ARRAY_SIZE(s->handler));
  117. qdev_init_gpio_out(dev, s->handler, ARRAY_SIZE(s->handler));
  118. qdev_init_gpio_out_named(dev, &s->intrq, "nINT", 1);
  119. }
  120. static void pcf8574_class_init(ObjectClass *klass, void *data)
  121. {
  122. DeviceClass *dc = DEVICE_CLASS(klass);
  123. I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
  124. k->recv = pcf8574_rx;
  125. k->send = pcf8574_tx;
  126. dc->realize = pcf8574_realize;
  127. device_class_set_legacy_reset(dc, pcf8574_reset);
  128. dc->vmsd = &vmstate_pcf8574;
  129. }
  130. static const TypeInfo pcf8574_infos[] = {
  131. {
  132. .name = TYPE_PCF8574,
  133. .parent = TYPE_I2C_SLAVE,
  134. .instance_size = sizeof(PCF8574State),
  135. .class_init = pcf8574_class_init,
  136. }
  137. };
  138. DEFINE_TYPES(pcf8574_infos);