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 "hw/i2c/i2c.h"
12 #define TYPE_MAX7310 "max7310"
13 #define MAX7310(obj) OBJECT_CHECK(MAX7310State, (obj), TYPE_MAX7310)
15 typedef struct MAX7310State
{
30 static void max7310_reset(DeviceState
*dev
)
32 MAX7310State
*s
= MAX7310(dev
);
34 s
->level
&= s
->direction
;
41 static int max7310_rx(I2CSlave
*i2c
)
43 MAX7310State
*s
= MAX7310(i2c
);
46 case 0x00: /* Input port */
47 return s
->level
^ s
->polarity
;
50 case 0x01: /* Output port */
51 return s
->level
& ~s
->direction
;
54 case 0x02: /* Polarity inversion */
57 case 0x03: /* Configuration */
60 case 0x04: /* Timeout */
64 case 0xff: /* Reserved */
69 printf("%s: unknown register %02x\n", __FUNCTION__
, s
->command
);
76 static int max7310_tx(I2CSlave
*i2c
, uint8_t data
)
78 MAX7310State
*s
= MAX7310(i2c
);
84 printf("%s: message too long (%i bytes)\n", __FUNCTION__
, s
->len
);
89 if (s
->i2c_command_byte
) {
91 s
->i2c_command_byte
= 0;
96 case 0x01: /* Output port */
97 for (diff
= (data
^ s
->level
) & ~s
->direction
; diff
;
98 diff
&= ~(1 << line
)) {
100 if (s
->handler
[line
])
101 qemu_set_irq(s
->handler
[line
], (data
>> line
) & 1);
103 s
->level
= (s
->level
& s
->direction
) | (data
& ~s
->direction
);
106 case 0x02: /* Polarity inversion */
110 case 0x03: /* Configuration */
111 s
->level
&= ~(s
->direction
^ data
);
115 case 0x04: /* Timeout */
119 case 0x00: /* Input port - ignore writes */
123 printf("%s: unknown register %02x\n", __FUNCTION__
, s
->command
);
131 static void max7310_event(I2CSlave
*i2c
, enum i2c_event event
)
133 MAX7310State
*s
= MAX7310(i2c
);
138 s
->i2c_command_byte
= 1;
143 printf("%s: message too short (%i bytes)\n", __FUNCTION__
, s
->len
);
151 static const VMStateDescription vmstate_max7310
= {
154 .minimum_version_id
= 0,
155 .fields
= (VMStateField
[]) {
156 VMSTATE_INT32(i2c_command_byte
, MAX7310State
),
157 VMSTATE_INT32(len
, MAX7310State
),
158 VMSTATE_UINT8(level
, MAX7310State
),
159 VMSTATE_UINT8(direction
, MAX7310State
),
160 VMSTATE_UINT8(polarity
, MAX7310State
),
161 VMSTATE_UINT8(status
, MAX7310State
),
162 VMSTATE_UINT8(command
, MAX7310State
),
163 VMSTATE_I2C_SLAVE(parent_obj
, MAX7310State
),
164 VMSTATE_END_OF_LIST()
168 static void max7310_gpio_set(void *opaque
, int line
, int level
)
170 MAX7310State
*s
= (MAX7310State
*) opaque
;
171 if (line
>= ARRAY_SIZE(s
->handler
) || line
< 0)
172 hw_error("bad GPIO line");
175 s
->level
|= s
->direction
& (1 << line
);
177 s
->level
&= ~(s
->direction
& (1 << line
));
180 /* MAX7310 is SMBus-compatible (can be used with only SMBus protocols),
181 * but also accepts sequences that are not SMBus so return an I2C device. */
182 static int max7310_init(I2CSlave
*i2c
)
184 MAX7310State
*s
= MAX7310(i2c
);
186 qdev_init_gpio_in(&i2c
->qdev
, max7310_gpio_set
, 8);
187 qdev_init_gpio_out(&i2c
->qdev
, s
->handler
, 8);
192 static void max7310_class_init(ObjectClass
*klass
, void *data
)
194 DeviceClass
*dc
= DEVICE_CLASS(klass
);
195 I2CSlaveClass
*k
= I2C_SLAVE_CLASS(klass
);
197 k
->init
= max7310_init
;
198 k
->event
= max7310_event
;
199 k
->recv
= max7310_rx
;
200 k
->send
= max7310_tx
;
201 dc
->reset
= max7310_reset
;
202 dc
->vmsd
= &vmstate_max7310
;
205 static const TypeInfo max7310_info
= {
206 .name
= TYPE_MAX7310
,
207 .parent
= TYPE_I2C_SLAVE
,
208 .instance_size
= sizeof(MAX7310State
),
209 .class_init
= max7310_class_init
,
212 static void max7310_register_types(void)
214 type_register_static(&max7310_info
);
217 type_init(max7310_register_types
)