2
0

max7310.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * MAX7310 8-port GPIO expansion chip.
  3. *
  4. * Copyright (c) 2006 Openedhand Ltd.
  5. * Written by Andrzej Zaborowski <balrog@zabor.org>
  6. *
  7. * This file is licensed under GNU GPL.
  8. */
  9. #include "qemu/osdep.h"
  10. #include "hw/i2c/i2c.h"
  11. #include "hw/irq.h"
  12. #include "migration/vmstate.h"
  13. #include "qemu/log.h"
  14. #include "qemu/module.h"
  15. #include "qom/object.h"
  16. #define TYPE_MAX7310 "max7310"
  17. OBJECT_DECLARE_SIMPLE_TYPE(MAX7310State, MAX7310)
  18. struct MAX7310State {
  19. I2CSlave parent_obj;
  20. int i2c_command_byte;
  21. int len;
  22. uint8_t level;
  23. uint8_t direction;
  24. uint8_t polarity;
  25. uint8_t status;
  26. uint8_t command;
  27. qemu_irq handler[8];
  28. qemu_irq *gpio_in;
  29. };
  30. static void max7310_reset(DeviceState *dev)
  31. {
  32. MAX7310State *s = MAX7310(dev);
  33. s->level &= s->direction;
  34. s->direction = 0xff;
  35. s->polarity = 0xf0;
  36. s->status = 0x01;
  37. s->command = 0x00;
  38. }
  39. static uint8_t max7310_rx(I2CSlave *i2c)
  40. {
  41. MAX7310State *s = MAX7310(i2c);
  42. switch (s->command) {
  43. case 0x00: /* Input port */
  44. return s->level ^ s->polarity;
  45. case 0x01: /* Output port */
  46. return s->level & ~s->direction;
  47. case 0x02: /* Polarity inversion */
  48. return s->polarity;
  49. case 0x03: /* Configuration */
  50. return s->direction;
  51. case 0x04: /* Timeout */
  52. return s->status;
  53. case 0xff: /* Reserved */
  54. return 0xff;
  55. default:
  56. qemu_log_mask(LOG_UNIMP, "%s: Unsupported register 0x02%" PRIx8 "\n",
  57. __func__, s->command);
  58. break;
  59. }
  60. return 0xff;
  61. }
  62. static int max7310_tx(I2CSlave *i2c, uint8_t data)
  63. {
  64. MAX7310State *s = MAX7310(i2c);
  65. uint8_t diff;
  66. int line;
  67. if (s->len ++ > 1) {
  68. #ifdef VERBOSE
  69. printf("%s: message too long (%i bytes)\n", __func__, s->len);
  70. #endif
  71. return 1;
  72. }
  73. if (s->i2c_command_byte) {
  74. s->command = data;
  75. s->i2c_command_byte = 0;
  76. return 0;
  77. }
  78. switch (s->command) {
  79. case 0x01: /* Output port */
  80. for (diff = (data ^ s->level) & ~s->direction; diff;
  81. diff &= ~(1 << line)) {
  82. line = ctz32(diff);
  83. if (s->handler[line])
  84. qemu_set_irq(s->handler[line], (data >> line) & 1);
  85. }
  86. s->level = (s->level & s->direction) | (data & ~s->direction);
  87. break;
  88. case 0x02: /* Polarity inversion */
  89. s->polarity = data;
  90. break;
  91. case 0x03: /* Configuration */
  92. s->level &= ~(s->direction ^ data);
  93. s->direction = data;
  94. break;
  95. case 0x04: /* Timeout */
  96. s->status = data;
  97. break;
  98. case 0x00: /* Input port - ignore writes */
  99. break;
  100. default:
  101. qemu_log_mask(LOG_UNIMP, "%s: Unsupported register 0x02%" PRIx8 "\n",
  102. __func__, s->command);
  103. return 1;
  104. }
  105. return 0;
  106. }
  107. static int max7310_event(I2CSlave *i2c, enum i2c_event event)
  108. {
  109. MAX7310State *s = MAX7310(i2c);
  110. s->len = 0;
  111. switch (event) {
  112. case I2C_START_SEND:
  113. s->i2c_command_byte = 1;
  114. break;
  115. case I2C_FINISH:
  116. #ifdef VERBOSE
  117. if (s->len == 1)
  118. printf("%s: message too short (%i bytes)\n", __func__, s->len);
  119. #endif
  120. break;
  121. default:
  122. break;
  123. }
  124. return 0;
  125. }
  126. static const VMStateDescription vmstate_max7310 = {
  127. .name = "max7310",
  128. .version_id = 0,
  129. .minimum_version_id = 0,
  130. .fields = (VMStateField[]) {
  131. VMSTATE_INT32(i2c_command_byte, MAX7310State),
  132. VMSTATE_INT32(len, MAX7310State),
  133. VMSTATE_UINT8(level, MAX7310State),
  134. VMSTATE_UINT8(direction, MAX7310State),
  135. VMSTATE_UINT8(polarity, MAX7310State),
  136. VMSTATE_UINT8(status, MAX7310State),
  137. VMSTATE_UINT8(command, MAX7310State),
  138. VMSTATE_I2C_SLAVE(parent_obj, MAX7310State),
  139. VMSTATE_END_OF_LIST()
  140. }
  141. };
  142. static void max7310_gpio_set(void *opaque, int line, int level)
  143. {
  144. MAX7310State *s = (MAX7310State *) opaque;
  145. assert(line >= 0 && line < ARRAY_SIZE(s->handler));
  146. if (level)
  147. s->level |= s->direction & (1 << line);
  148. else
  149. s->level &= ~(s->direction & (1 << line));
  150. }
  151. /* MAX7310 is SMBus-compatible (can be used with only SMBus protocols),
  152. * but also accepts sequences that are not SMBus so return an I2C device. */
  153. static void max7310_realize(DeviceState *dev, Error **errp)
  154. {
  155. MAX7310State *s = MAX7310(dev);
  156. qdev_init_gpio_in(dev, max7310_gpio_set, ARRAY_SIZE(s->handler));
  157. qdev_init_gpio_out(dev, s->handler, ARRAY_SIZE(s->handler));
  158. }
  159. static void max7310_class_init(ObjectClass *klass, void *data)
  160. {
  161. DeviceClass *dc = DEVICE_CLASS(klass);
  162. I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
  163. dc->realize = max7310_realize;
  164. k->event = max7310_event;
  165. k->recv = max7310_rx;
  166. k->send = max7310_tx;
  167. dc->reset = max7310_reset;
  168. dc->vmsd = &vmstate_max7310;
  169. }
  170. static const TypeInfo max7310_info = {
  171. .name = TYPE_MAX7310,
  172. .parent = TYPE_I2C_SLAVE,
  173. .instance_size = sizeof(MAX7310State),
  174. .class_init = max7310_class_init,
  175. };
  176. static void max7310_register_types(void)
  177. {
  178. type_register_static(&max7310_info);
  179. }
  180. type_init(max7310_register_types)