2 * MAX7310 8-port GPIO expansion chip.
4 * Copyright (c) 2006 Openedhand Ltd.
7 * This file is licensed under GNU GPL.
26 void max7310_reset(i2c_slave *i2c)
28 MAX7310State *s = (MAX7310State *) i2c;
29 s->level &= s->direction;
36 static int max7310_rx(i2c_slave *i2c)
38 MAX7310State *s = (MAX7310State *) i2c;
41 case 0x00: /* Input port */
42 return s->level ^ s->polarity;
45 case 0x01: /* Output port */
46 return s->level & ~s->direction;
49 case 0x02: /* Polarity inversion */
52 case 0x03: /* Configuration */
55 case 0x04: /* Timeout */
59 case 0xff: /* Reserved */
64 printf("%s: unknown register %02x\n", __FUNCTION__, s->command);
71 static int max7310_tx(i2c_slave *i2c, uint8_t data)
73 MAX7310State *s = (MAX7310State *) i2c;
79 printf("%s: message too long (%i bytes)\n", __FUNCTION__, s->len);
84 if (s->i2c_command_byte) {
86 s->i2c_command_byte = 0;
91 case 0x01: /* Output port */
92 for (diff = (data ^ s->level) & ~s->direction; diff;
93 diff &= ~(1 << line)) {
96 qemu_set_irq(s->handler[line], (data >> line) & 1);
98 s->level = (s->level & s->direction) | (data & ~s->direction);
101 case 0x02: /* Polarity inversion */
105 case 0x03: /* Configuration */
106 s->level &= ~(s->direction ^ data);
110 case 0x04: /* Timeout */
114 case 0x00: /* Input port - ignore writes */
118 printf("%s: unknown register %02x\n", __FUNCTION__, s->command);
126 static void max7310_event(i2c_slave *i2c, enum i2c_event event)
128 MAX7310State *s = (MAX7310State *) i2c;
133 s->i2c_command_byte = 1;
138 printf("%s: message too short (%i bytes)\n", __FUNCTION__, s->len);
146 static void max7310_save(QEMUFile *f, void *opaque)
148 MAX7310State *s = (MAX7310State *) opaque;
150 qemu_put_be32(f, s->i2c_command_byte);
151 qemu_put_be32(f, s->len);
153 qemu_put_8s(f, &s->level);
154 qemu_put_8s(f, &s->direction);
155 qemu_put_8s(f, &s->polarity);
156 qemu_put_8s(f, &s->status);
157 qemu_put_8s(f, &s->command);
159 i2c_slave_save(f, &s->i2c);
162 static int max7310_load(QEMUFile *f, void *opaque, int version_id)
164 MAX7310State *s = (MAX7310State *) opaque;
166 s->i2c_command_byte = qemu_get_be32(f);
167 s->len = qemu_get_be32(f);
169 qemu_get_8s(f, &s->level);
170 qemu_get_8s(f, &s->direction);
171 qemu_get_8s(f, &s->polarity);
172 qemu_get_8s(f, &s->status);
173 qemu_get_8s(f, &s->command);
175 i2c_slave_load(f, &s->i2c);
179 static void max7310_gpio_set(void *opaque, int line, int level)
181 MAX7310State *s = (MAX7310State *) opaque;
182 if (line >= ARRAY_SIZE(s->handler) || line < 0)
183 hw_error("bad GPIO line");
186 s->level |= s->direction & (1 << line);
188 s->level &= ~(s->direction & (1 << line));
191 /* MAX7310 is SMBus-compatible (can be used with only SMBus protocols),
192 * but also accepts sequences that are not SMBus so return an I2C device. */
193 static int max7310_init(i2c_slave *i2c)
195 MAX7310State *s = FROM_I2C_SLAVE(MAX7310State, i2c);
197 s->gpio_in = qemu_allocate_irqs(max7310_gpio_set, s,
198 ARRAY_SIZE(s->handler));
200 max7310_reset(&s->i2c);
202 register_savevm("max7310", -1, 0, max7310_save, max7310_load, s);
206 qemu_irq *max7310_gpio_in_get(i2c_slave *i2c)
208 MAX7310State *s = (MAX7310State *) i2c;
212 void max7310_gpio_out_set(i2c_slave *i2c, int line, qemu_irq handler)
214 MAX7310State *s = (MAX7310State *) i2c;
215 if (line >= ARRAY_SIZE(s->handler) || line < 0)
216 hw_error("bad GPIO line");
218 s->handler[line] = handler;
221 static I2CSlaveInfo max7310_info = {
222 .qdev.name = "max7310",
223 .qdev.size = sizeof(MAX7310State),
224 .init = max7310_init,
225 .event = max7310_event,
230 static void max7310_register_devices(void)
232 i2c_register_slave(&max7310_info);
235 device_init(max7310_register_devices)