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.
10 #include "qemu/osdep.h"
11 #include "hw/i2c/i2c.h"
13 #define TYPE_MAX7310 "max7310"
14 #define MAX7310(obj) OBJECT_CHECK(MAX7310State, (obj), TYPE_MAX7310)
16 typedef struct MAX7310State
{
31 static void max7310_reset(DeviceState
*dev
)
33 MAX7310State
*s
= MAX7310(dev
);
35 s
->level
&= s
->direction
;
42 static int max7310_rx(I2CSlave
*i2c
)
44 MAX7310State
*s
= MAX7310(i2c
);
47 case 0x00: /* Input port */
48 return s
->level
^ s
->polarity
;
51 case 0x01: /* Output port */
52 return s
->level
& ~s
->direction
;
55 case 0x02: /* Polarity inversion */
58 case 0x03: /* Configuration */
61 case 0x04: /* Timeout */
65 case 0xff: /* Reserved */
70 printf("%s: unknown register %02x\n", __FUNCTION__
, s
->command
);
77 static int max7310_tx(I2CSlave
*i2c
, uint8_t data
)
79 MAX7310State
*s
= MAX7310(i2c
);
85 printf("%s: message too long (%i bytes)\n", __FUNCTION__
, s
->len
);
90 if (s
->i2c_command_byte
) {
92 s
->i2c_command_byte
= 0;
97 case 0x01: /* Output port */
98 for (diff
= (data
^ s
->level
) & ~s
->direction
; diff
;
99 diff
&= ~(1 << line
)) {
101 if (s
->handler
[line
])
102 qemu_set_irq(s
->handler
[line
], (data
>> line
) & 1);
104 s
->level
= (s
->level
& s
->direction
) | (data
& ~s
->direction
);
107 case 0x02: /* Polarity inversion */
111 case 0x03: /* Configuration */
112 s
->level
&= ~(s
->direction
^ data
);
116 case 0x04: /* Timeout */
120 case 0x00: /* Input port - ignore writes */
124 printf("%s: unknown register %02x\n", __FUNCTION__
, s
->command
);
132 static void max7310_event(I2CSlave
*i2c
, enum i2c_event event
)
134 MAX7310State
*s
= MAX7310(i2c
);
139 s
->i2c_command_byte
= 1;
144 printf("%s: message too short (%i bytes)\n", __FUNCTION__
, s
->len
);
152 static const VMStateDescription vmstate_max7310
= {
155 .minimum_version_id
= 0,
156 .fields
= (VMStateField
[]) {
157 VMSTATE_INT32(i2c_command_byte
, MAX7310State
),
158 VMSTATE_INT32(len
, MAX7310State
),
159 VMSTATE_UINT8(level
, MAX7310State
),
160 VMSTATE_UINT8(direction
, MAX7310State
),
161 VMSTATE_UINT8(polarity
, MAX7310State
),
162 VMSTATE_UINT8(status
, MAX7310State
),
163 VMSTATE_UINT8(command
, MAX7310State
),
164 VMSTATE_I2C_SLAVE(parent_obj
, MAX7310State
),
165 VMSTATE_END_OF_LIST()
169 static void max7310_gpio_set(void *opaque
, int line
, int level
)
171 MAX7310State
*s
= (MAX7310State
*) opaque
;
172 if (line
>= ARRAY_SIZE(s
->handler
) || line
< 0)
173 hw_error("bad GPIO line");
176 s
->level
|= s
->direction
& (1 << line
);
178 s
->level
&= ~(s
->direction
& (1 << line
));
181 /* MAX7310 is SMBus-compatible (can be used with only SMBus protocols),
182 * but also accepts sequences that are not SMBus so return an I2C device. */
183 static int max7310_init(I2CSlave
*i2c
)
185 MAX7310State
*s
= MAX7310(i2c
);
187 qdev_init_gpio_in(&i2c
->qdev
, max7310_gpio_set
, 8);
188 qdev_init_gpio_out(&i2c
->qdev
, s
->handler
, 8);
193 static void max7310_class_init(ObjectClass
*klass
, void *data
)
195 DeviceClass
*dc
= DEVICE_CLASS(klass
);
196 I2CSlaveClass
*k
= I2C_SLAVE_CLASS(klass
);
198 k
->init
= max7310_init
;
199 k
->event
= max7310_event
;
200 k
->recv
= max7310_rx
;
201 k
->send
= max7310_tx
;
202 dc
->reset
= max7310_reset
;
203 dc
->vmsd
= &vmstate_max7310
;
206 static const TypeInfo max7310_info
= {
207 .name
= TYPE_MAX7310
,
208 .parent
= TYPE_I2C_SLAVE
,
209 .instance_size
= sizeof(MAX7310State
),
210 .class_init
= max7310_class_init
,
213 static void max7310_register_types(void)
215 type_register_static(&max7310_info
);
218 type_init(max7310_register_types
)