2 * MAX7310 8-port GPIO expansion chip.
4 * Copyright (c) 2006 Openedhand Ltd.
5 * Written by Andrzej Zaborowski <balrog@zabor.org>
7 * This file is licensed under GNU GPL.
26 void max7310_reset(i2c_slave *i2c)
28 struct max7310_s *s = (struct max7310_s *) i2c;
29 s->level &= s->direction;
36 static int max7310_rx(i2c_slave *i2c)
38 struct max7310_s *s = (struct max7310_s *) 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 struct max7310_s *s = (struct max7310_s *) 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 struct max7310_s *s = (struct max7310_s *) i2c;
133 s->i2c_command_byte = 1;
138 printf("%s: message too short (%i bytes)\n", __FUNCTION__, s->len);
146 static void max7310_gpio_set(void *opaque, int line, int level)
148 struct max7310_s *s = (struct max7310_s *) opaque;
149 if (line >= sizeof(s->handler) / sizeof(*s->handler) || line < 0)
150 cpu_abort(cpu_single_env, "bad GPIO line");
153 s->level |= s->direction & (1 << line);
155 s->level &= ~(s->direction & (1 << line));
158 /* MAX7310 is SMBus-compatible (can be used with only SMBus protocols),
159 * but also accepts sequences that are not SMBus so return an I2C device. */
160 struct i2c_slave *max7310_init(i2c_bus *bus)
162 struct max7310_s *s = (struct max7310_s *)
163 i2c_slave_init(bus, 0, sizeof(struct max7310_s));
164 s->i2c.event = max7310_event;
165 s->i2c.recv = max7310_rx;
166 s->i2c.send = max7310_tx;
167 s->gpio_in = qemu_allocate_irqs(max7310_gpio_set, s,
168 sizeof(s->handler) / sizeof(*s->handler));
170 max7310_reset(&s->i2c);
175 qemu_irq *max7310_gpio_in_get(i2c_slave *i2c)
177 struct max7310_s *s = (struct max7310_s *) i2c;
181 void max7310_gpio_out_set(i2c_slave *i2c, int line, qemu_irq handler)
183 struct max7310_s *s = (struct max7310_s *) i2c;
184 if (line >= sizeof(s->handler) / sizeof(*s->handler) || line < 0)
185 cpu_abort(cpu_single_env, "bad GPIO line");
187 s->handler[line] = handler;