2
0

max7310.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 "hw/i2c/i2c.h"
  10. #define TYPE_MAX7310 "max7310"
  11. #define MAX7310(obj) OBJECT_CHECK(MAX7310State, (obj), TYPE_MAX7310)
  12. typedef struct MAX7310State {
  13. I2CSlave parent_obj;
  14. int i2c_command_byte;
  15. int len;
  16. uint8_t level;
  17. uint8_t direction;
  18. uint8_t polarity;
  19. uint8_t status;
  20. uint8_t command;
  21. qemu_irq handler[8];
  22. qemu_irq *gpio_in;
  23. } MAX7310State;
  24. static void max7310_reset(DeviceState *dev)
  25. {
  26. MAX7310State *s = MAX7310(dev);
  27. s->level &= s->direction;
  28. s->direction = 0xff;
  29. s->polarity = 0xf0;
  30. s->status = 0x01;
  31. s->command = 0x00;
  32. }
  33. static int max7310_rx(I2CSlave *i2c)
  34. {
  35. MAX7310State *s = MAX7310(i2c);
  36. switch (s->command) {
  37. case 0x00: /* Input port */
  38. return s->level ^ s->polarity;
  39. break;
  40. case 0x01: /* Output port */
  41. return s->level & ~s->direction;
  42. break;
  43. case 0x02: /* Polarity inversion */
  44. return s->polarity;
  45. case 0x03: /* Configuration */
  46. return s->direction;
  47. case 0x04: /* Timeout */
  48. return s->status;
  49. break;
  50. case 0xff: /* Reserved */
  51. return 0xff;
  52. default:
  53. #ifdef VERBOSE
  54. printf("%s: unknown register %02x\n", __FUNCTION__, s->command);
  55. #endif
  56. break;
  57. }
  58. return 0xff;
  59. }
  60. static int max7310_tx(I2CSlave *i2c, uint8_t data)
  61. {
  62. MAX7310State *s = MAX7310(i2c);
  63. uint8_t diff;
  64. int line;
  65. if (s->len ++ > 1) {
  66. #ifdef VERBOSE
  67. printf("%s: message too long (%i bytes)\n", __FUNCTION__, s->len);
  68. #endif
  69. return 1;
  70. }
  71. if (s->i2c_command_byte) {
  72. s->command = data;
  73. s->i2c_command_byte = 0;
  74. return 0;
  75. }
  76. switch (s->command) {
  77. case 0x01: /* Output port */
  78. for (diff = (data ^ s->level) & ~s->direction; diff;
  79. diff &= ~(1 << line)) {
  80. line = ffs(diff) - 1;
  81. if (s->handler[line])
  82. qemu_set_irq(s->handler[line], (data >> line) & 1);
  83. }
  84. s->level = (s->level & s->direction) | (data & ~s->direction);
  85. break;
  86. case 0x02: /* Polarity inversion */
  87. s->polarity = data;
  88. break;
  89. case 0x03: /* Configuration */
  90. s->level &= ~(s->direction ^ data);
  91. s->direction = data;
  92. break;
  93. case 0x04: /* Timeout */
  94. s->status = data;
  95. break;
  96. case 0x00: /* Input port - ignore writes */
  97. break;
  98. default:
  99. #ifdef VERBOSE
  100. printf("%s: unknown register %02x\n", __FUNCTION__, s->command);
  101. #endif
  102. return 1;
  103. }
  104. return 0;
  105. }
  106. static void max7310_event(I2CSlave *i2c, enum i2c_event event)
  107. {
  108. MAX7310State *s = MAX7310(i2c);
  109. s->len = 0;
  110. switch (event) {
  111. case I2C_START_SEND:
  112. s->i2c_command_byte = 1;
  113. break;
  114. case I2C_FINISH:
  115. #ifdef VERBOSE
  116. if (s->len == 1)
  117. printf("%s: message too short (%i bytes)\n", __FUNCTION__, s->len);
  118. #endif
  119. break;
  120. default:
  121. break;
  122. }
  123. }
  124. static const VMStateDescription vmstate_max7310 = {
  125. .name = "max7310",
  126. .version_id = 0,
  127. .minimum_version_id = 0,
  128. .minimum_version_id_old = 0,
  129. .fields = (VMStateField []) {
  130. VMSTATE_INT32(i2c_command_byte, MAX7310State),
  131. VMSTATE_INT32(len, MAX7310State),
  132. VMSTATE_UINT8(level, MAX7310State),
  133. VMSTATE_UINT8(direction, MAX7310State),
  134. VMSTATE_UINT8(polarity, MAX7310State),
  135. VMSTATE_UINT8(status, MAX7310State),
  136. VMSTATE_UINT8(command, MAX7310State),
  137. VMSTATE_I2C_SLAVE(parent_obj, MAX7310State),
  138. VMSTATE_END_OF_LIST()
  139. }
  140. };
  141. static void max7310_gpio_set(void *opaque, int line, int level)
  142. {
  143. MAX7310State *s = (MAX7310State *) opaque;
  144. if (line >= ARRAY_SIZE(s->handler) || line < 0)
  145. hw_error("bad GPIO line");
  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 int max7310_init(I2CSlave *i2c)
  154. {
  155. MAX7310State *s = MAX7310(i2c);
  156. qdev_init_gpio_in(&i2c->qdev, max7310_gpio_set, 8);
  157. qdev_init_gpio_out(&i2c->qdev, s->handler, 8);
  158. return 0;
  159. }
  160. static void max7310_class_init(ObjectClass *klass, void *data)
  161. {
  162. DeviceClass *dc = DEVICE_CLASS(klass);
  163. I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
  164. k->init = max7310_init;
  165. k->event = max7310_event;
  166. k->recv = max7310_rx;
  167. k->send = max7310_tx;
  168. dc->reset = max7310_reset;
  169. dc->vmsd = &vmstate_max7310;
  170. }
  171. static const TypeInfo max7310_info = {
  172. .name = TYPE_MAX7310,
  173. .parent = TYPE_I2C_SLAVE,
  174. .instance_size = sizeof(MAX7310State),
  175. .class_init = max7310_class_init,
  176. };
  177. static void max7310_register_types(void)
  178. {
  179. type_register_static(&max7310_info);
  180. }
  181. type_init(max7310_register_types)