max7310.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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/hw.h"
  11. #include "hw/i2c/i2c.h"
  12. #include "hw/hw.h"
  13. #include "hw/irq.h"
  14. #include "migration/vmstate.h"
  15. #include "qemu/module.h"
  16. #define TYPE_MAX7310 "max7310"
  17. #define MAX7310(obj) OBJECT_CHECK(MAX7310State, (obj), TYPE_MAX7310)
  18. typedef 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. } MAX7310State;
  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. break;
  46. case 0x01: /* Output port */
  47. return s->level & ~s->direction;
  48. break;
  49. case 0x02: /* Polarity inversion */
  50. return s->polarity;
  51. case 0x03: /* Configuration */
  52. return s->direction;
  53. case 0x04: /* Timeout */
  54. return s->status;
  55. break;
  56. case 0xff: /* Reserved */
  57. return 0xff;
  58. default:
  59. #ifdef VERBOSE
  60. printf("%s: unknown register %02x\n", __func__, s->command);
  61. #endif
  62. break;
  63. }
  64. return 0xff;
  65. }
  66. static int max7310_tx(I2CSlave *i2c, uint8_t data)
  67. {
  68. MAX7310State *s = MAX7310(i2c);
  69. uint8_t diff;
  70. int line;
  71. if (s->len ++ > 1) {
  72. #ifdef VERBOSE
  73. printf("%s: message too long (%i bytes)\n", __func__, s->len);
  74. #endif
  75. return 1;
  76. }
  77. if (s->i2c_command_byte) {
  78. s->command = data;
  79. s->i2c_command_byte = 0;
  80. return 0;
  81. }
  82. switch (s->command) {
  83. case 0x01: /* Output port */
  84. for (diff = (data ^ s->level) & ~s->direction; diff;
  85. diff &= ~(1 << line)) {
  86. line = ctz32(diff);
  87. if (s->handler[line])
  88. qemu_set_irq(s->handler[line], (data >> line) & 1);
  89. }
  90. s->level = (s->level & s->direction) | (data & ~s->direction);
  91. break;
  92. case 0x02: /* Polarity inversion */
  93. s->polarity = data;
  94. break;
  95. case 0x03: /* Configuration */
  96. s->level &= ~(s->direction ^ data);
  97. s->direction = data;
  98. break;
  99. case 0x04: /* Timeout */
  100. s->status = data;
  101. break;
  102. case 0x00: /* Input port - ignore writes */
  103. break;
  104. default:
  105. #ifdef VERBOSE
  106. printf("%s: unknown register %02x\n", __func__, s->command);
  107. #endif
  108. return 1;
  109. }
  110. return 0;
  111. }
  112. static int max7310_event(I2CSlave *i2c, enum i2c_event event)
  113. {
  114. MAX7310State *s = MAX7310(i2c);
  115. s->len = 0;
  116. switch (event) {
  117. case I2C_START_SEND:
  118. s->i2c_command_byte = 1;
  119. break;
  120. case I2C_FINISH:
  121. #ifdef VERBOSE
  122. if (s->len == 1)
  123. printf("%s: message too short (%i bytes)\n", __func__, s->len);
  124. #endif
  125. break;
  126. default:
  127. break;
  128. }
  129. return 0;
  130. }
  131. static const VMStateDescription vmstate_max7310 = {
  132. .name = "max7310",
  133. .version_id = 0,
  134. .minimum_version_id = 0,
  135. .fields = (VMStateField[]) {
  136. VMSTATE_INT32(i2c_command_byte, MAX7310State),
  137. VMSTATE_INT32(len, MAX7310State),
  138. VMSTATE_UINT8(level, MAX7310State),
  139. VMSTATE_UINT8(direction, MAX7310State),
  140. VMSTATE_UINT8(polarity, MAX7310State),
  141. VMSTATE_UINT8(status, MAX7310State),
  142. VMSTATE_UINT8(command, MAX7310State),
  143. VMSTATE_I2C_SLAVE(parent_obj, MAX7310State),
  144. VMSTATE_END_OF_LIST()
  145. }
  146. };
  147. static void max7310_gpio_set(void *opaque, int line, int level)
  148. {
  149. MAX7310State *s = (MAX7310State *) opaque;
  150. if (line >= ARRAY_SIZE(s->handler) || line < 0)
  151. hw_error("bad GPIO line");
  152. if (level)
  153. s->level |= s->direction & (1 << line);
  154. else
  155. s->level &= ~(s->direction & (1 << line));
  156. }
  157. /* MAX7310 is SMBus-compatible (can be used with only SMBus protocols),
  158. * but also accepts sequences that are not SMBus so return an I2C device. */
  159. static void max7310_realize(DeviceState *dev, Error **errp)
  160. {
  161. I2CSlave *i2c = I2C_SLAVE(dev);
  162. MAX7310State *s = MAX7310(dev);
  163. qdev_init_gpio_in(&i2c->qdev, max7310_gpio_set, 8);
  164. qdev_init_gpio_out(&i2c->qdev, s->handler, 8);
  165. }
  166. static void max7310_class_init(ObjectClass *klass, void *data)
  167. {
  168. DeviceClass *dc = DEVICE_CLASS(klass);
  169. I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
  170. dc->realize = max7310_realize;
  171. k->event = max7310_event;
  172. k->recv = max7310_rx;
  173. k->send = max7310_tx;
  174. dc->reset = max7310_reset;
  175. dc->vmsd = &vmstate_max7310;
  176. }
  177. static const TypeInfo max7310_info = {
  178. .name = TYPE_MAX7310,
  179. .parent = TYPE_I2C_SLAVE,
  180. .instance_size = sizeof(MAX7310State),
  181. .class_init = max7310_class_init,
  182. };
  183. static void max7310_register_types(void)
  184. {
  185. type_register_static(&max7310_info);
  186. }
  187. type_init(max7310_register_types)