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 #include "qemu/module.h"
15 #define TYPE_MAX7310 "max7310"
16 #define MAX7310(obj) OBJECT_CHECK(MAX7310State, (obj), TYPE_MAX7310)
18 typedef struct MAX7310State
{
33 static void max7310_reset(DeviceState
*dev
)
35 MAX7310State
*s
= MAX7310(dev
);
37 s
->level
&= s
->direction
;
44 static uint8_t max7310_rx(I2CSlave
*i2c
)
46 MAX7310State
*s
= MAX7310(i2c
);
49 case 0x00: /* Input port */
50 return s
->level
^ s
->polarity
;
53 case 0x01: /* Output port */
54 return s
->level
& ~s
->direction
;
57 case 0x02: /* Polarity inversion */
60 case 0x03: /* Configuration */
63 case 0x04: /* Timeout */
67 case 0xff: /* Reserved */
72 printf("%s: unknown register %02x\n", __func__
, s
->command
);
79 static int max7310_tx(I2CSlave
*i2c
, uint8_t data
)
81 MAX7310State
*s
= MAX7310(i2c
);
87 printf("%s: message too long (%i bytes)\n", __func__
, s
->len
);
92 if (s
->i2c_command_byte
) {
94 s
->i2c_command_byte
= 0;
99 case 0x01: /* Output port */
100 for (diff
= (data
^ s
->level
) & ~s
->direction
; diff
;
101 diff
&= ~(1 << line
)) {
103 if (s
->handler
[line
])
104 qemu_set_irq(s
->handler
[line
], (data
>> line
) & 1);
106 s
->level
= (s
->level
& s
->direction
) | (data
& ~s
->direction
);
109 case 0x02: /* Polarity inversion */
113 case 0x03: /* Configuration */
114 s
->level
&= ~(s
->direction
^ data
);
118 case 0x04: /* Timeout */
122 case 0x00: /* Input port - ignore writes */
126 printf("%s: unknown register %02x\n", __func__
, s
->command
);
134 static int max7310_event(I2CSlave
*i2c
, enum i2c_event event
)
136 MAX7310State
*s
= MAX7310(i2c
);
141 s
->i2c_command_byte
= 1;
146 printf("%s: message too short (%i bytes)\n", __func__
, s
->len
);
156 static const VMStateDescription vmstate_max7310
= {
159 .minimum_version_id
= 0,
160 .fields
= (VMStateField
[]) {
161 VMSTATE_INT32(i2c_command_byte
, MAX7310State
),
162 VMSTATE_INT32(len
, MAX7310State
),
163 VMSTATE_UINT8(level
, MAX7310State
),
164 VMSTATE_UINT8(direction
, MAX7310State
),
165 VMSTATE_UINT8(polarity
, MAX7310State
),
166 VMSTATE_UINT8(status
, MAX7310State
),
167 VMSTATE_UINT8(command
, MAX7310State
),
168 VMSTATE_I2C_SLAVE(parent_obj
, MAX7310State
),
169 VMSTATE_END_OF_LIST()
173 static void max7310_gpio_set(void *opaque
, int line
, int level
)
175 MAX7310State
*s
= (MAX7310State
*) opaque
;
176 if (line
>= ARRAY_SIZE(s
->handler
) || line
< 0)
177 hw_error("bad GPIO line");
180 s
->level
|= s
->direction
& (1 << line
);
182 s
->level
&= ~(s
->direction
& (1 << line
));
185 /* MAX7310 is SMBus-compatible (can be used with only SMBus protocols),
186 * but also accepts sequences that are not SMBus so return an I2C device. */
187 static void max7310_realize(DeviceState
*dev
, Error
**errp
)
189 I2CSlave
*i2c
= I2C_SLAVE(dev
);
190 MAX7310State
*s
= MAX7310(dev
);
192 qdev_init_gpio_in(&i2c
->qdev
, max7310_gpio_set
, 8);
193 qdev_init_gpio_out(&i2c
->qdev
, s
->handler
, 8);
196 static void max7310_class_init(ObjectClass
*klass
, void *data
)
198 DeviceClass
*dc
= DEVICE_CLASS(klass
);
199 I2CSlaveClass
*k
= I2C_SLAVE_CLASS(klass
);
201 dc
->realize
= max7310_realize
;
202 k
->event
= max7310_event
;
203 k
->recv
= max7310_rx
;
204 k
->send
= max7310_tx
;
205 dc
->reset
= max7310_reset
;
206 dc
->vmsd
= &vmstate_max7310
;
209 static const TypeInfo max7310_info
= {
210 .name
= TYPE_MAX7310
,
211 .parent
= TYPE_I2C_SLAVE
,
212 .instance_size
= sizeof(MAX7310State
),
213 .class_init
= max7310_class_init
,
216 static void max7310_register_types(void)
218 type_register_static(&max7310_info
);
221 type_init(max7310_register_types
)