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.
27 void max7310_reset(i2c_slave
*i2c
)
29 MAX7310State
*s
= (MAX7310State
*) i2c
;
30 s
->level
&= s
->direction
;
37 static int max7310_rx(i2c_slave
*i2c
)
39 MAX7310State
*s
= (MAX7310State
*) i2c
;
42 case 0x00: /* Input port */
43 return s
->level
^ s
->polarity
;
46 case 0x01: /* Output port */
47 return s
->level
& ~s
->direction
;
50 case 0x02: /* Polarity inversion */
53 case 0x03: /* Configuration */
56 case 0x04: /* Timeout */
60 case 0xff: /* Reserved */
65 printf("%s: unknown register %02x\n", __FUNCTION__
, s
->command
);
72 static int max7310_tx(i2c_slave
*i2c
, uint8_t data
)
74 MAX7310State
*s
= (MAX7310State
*) i2c
;
80 printf("%s: message too long (%i bytes)\n", __FUNCTION__
, s
->len
);
85 if (s
->i2c_command_byte
) {
87 s
->i2c_command_byte
= 0;
92 case 0x01: /* Output port */
93 for (diff
= (data
^ s
->level
) & ~s
->direction
; diff
;
94 diff
&= ~(1 << line
)) {
97 qemu_set_irq(s
->handler
[line
], (data
>> line
) & 1);
99 s
->level
= (s
->level
& s
->direction
) | (data
& ~s
->direction
);
102 case 0x02: /* Polarity inversion */
106 case 0x03: /* Configuration */
107 s
->level
&= ~(s
->direction
^ data
);
111 case 0x04: /* Timeout */
115 case 0x00: /* Input port - ignore writes */
119 printf("%s: unknown register %02x\n", __FUNCTION__
, s
->command
);
127 static void max7310_event(i2c_slave
*i2c
, enum i2c_event event
)
129 MAX7310State
*s
= (MAX7310State
*) i2c
;
134 s
->i2c_command_byte
= 1;
139 printf("%s: message too short (%i bytes)\n", __FUNCTION__
, s
->len
);
147 static void max7310_save(QEMUFile
*f
, void *opaque
)
149 MAX7310State
*s
= (MAX7310State
*) opaque
;
151 qemu_put_be32(f
, s
->i2c_command_byte
);
152 qemu_put_be32(f
, s
->len
);
154 qemu_put_8s(f
, &s
->level
);
155 qemu_put_8s(f
, &s
->direction
);
156 qemu_put_8s(f
, &s
->polarity
);
157 qemu_put_8s(f
, &s
->status
);
158 qemu_put_8s(f
, &s
->command
);
160 i2c_slave_save(f
, &s
->i2c
);
163 static int max7310_load(QEMUFile
*f
, void *opaque
, int version_id
)
165 MAX7310State
*s
= (MAX7310State
*) opaque
;
167 s
->i2c_command_byte
= qemu_get_be32(f
);
168 s
->len
= qemu_get_be32(f
);
170 qemu_get_8s(f
, &s
->level
);
171 qemu_get_8s(f
, &s
->direction
);
172 qemu_get_8s(f
, &s
->polarity
);
173 qemu_get_8s(f
, &s
->status
);
174 qemu_get_8s(f
, &s
->command
);
176 i2c_slave_load(f
, &s
->i2c
);
180 static void max7310_gpio_set(void *opaque
, int line
, int level
)
182 MAX7310State
*s
= (MAX7310State
*) opaque
;
183 if (line
>= ARRAY_SIZE(s
->handler
) || line
< 0)
184 hw_error("bad GPIO line");
187 s
->level
|= s
->direction
& (1 << line
);
189 s
->level
&= ~(s
->direction
& (1 << line
));
192 /* MAX7310 is SMBus-compatible (can be used with only SMBus protocols),
193 * but also accepts sequences that are not SMBus so return an I2C device. */
194 i2c_slave
*max7310_init(i2c_bus
*bus
)
196 MAX7310State
*s
= (MAX7310State
*)
197 i2c_slave_init(bus
, 0, sizeof(MAX7310State
));
198 s
->i2c
.event
= max7310_event
;
199 s
->i2c
.recv
= max7310_rx
;
200 s
->i2c
.send
= max7310_tx
;
201 s
->gpio_in
= qemu_allocate_irqs(max7310_gpio_set
, s
,
202 ARRAY_SIZE(s
->handler
));
204 max7310_reset(&s
->i2c
);
206 register_savevm("max7310", -1, 0, max7310_save
, max7310_load
, s
);
211 qemu_irq
*max7310_gpio_in_get(i2c_slave
*i2c
)
213 MAX7310State
*s
= (MAX7310State
*) i2c
;
217 void max7310_gpio_out_set(i2c_slave
*i2c
, int line
, qemu_irq handler
)
219 MAX7310State
*s
= (MAX7310State
*) i2c
;
220 if (line
>= ARRAY_SIZE(s
->handler
) || line
< 0)
221 hw_error("bad GPIO line");
223 s
->handler
[line
] = handler
;