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 static void max7310_reset(DeviceState
*dev
)
28 MAX7310State
*s
= FROM_I2C_SLAVE(MAX7310State
, I2C_SLAVE_FROM_QDEV(dev
));
29 s
->level
&= s
->direction
;
36 static int max7310_rx(I2CSlave
*i2c
)
38 MAX7310State
*s
= (MAX7310State
*) 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(I2CSlave
*i2c
, uint8_t data
)
73 MAX7310State
*s
= (MAX7310State
*) 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(I2CSlave
*i2c
, enum i2c_event event
)
128 MAX7310State
*s
= (MAX7310State
*) i2c
;
133 s
->i2c_command_byte
= 1;
138 printf("%s: message too short (%i bytes)\n", __FUNCTION__
, s
->len
);
146 static const VMStateDescription vmstate_max7310
= {
149 .minimum_version_id
= 0,
150 .minimum_version_id_old
= 0,
151 .fields
= (VMStateField
[]) {
152 VMSTATE_INT32(i2c_command_byte
, MAX7310State
),
153 VMSTATE_INT32(len
, MAX7310State
),
154 VMSTATE_UINT8(level
, MAX7310State
),
155 VMSTATE_UINT8(direction
, MAX7310State
),
156 VMSTATE_UINT8(polarity
, MAX7310State
),
157 VMSTATE_UINT8(status
, MAX7310State
),
158 VMSTATE_UINT8(command
, MAX7310State
),
159 VMSTATE_I2C_SLAVE(i2c
, MAX7310State
),
160 VMSTATE_END_OF_LIST()
164 static void max7310_gpio_set(void *opaque
, int line
, int level
)
166 MAX7310State
*s
= (MAX7310State
*) opaque
;
167 if (line
>= ARRAY_SIZE(s
->handler
) || line
< 0)
168 hw_error("bad GPIO line");
171 s
->level
|= s
->direction
& (1 << line
);
173 s
->level
&= ~(s
->direction
& (1 << line
));
176 /* MAX7310 is SMBus-compatible (can be used with only SMBus protocols),
177 * but also accepts sequences that are not SMBus so return an I2C device. */
178 static int max7310_init(I2CSlave
*i2c
)
180 MAX7310State
*s
= FROM_I2C_SLAVE(MAX7310State
, i2c
);
182 qdev_init_gpio_in(&i2c
->qdev
, max7310_gpio_set
, 8);
183 qdev_init_gpio_out(&i2c
->qdev
, s
->handler
, 8);
188 static void max7310_class_init(ObjectClass
*klass
, void *data
)
190 DeviceClass
*dc
= DEVICE_CLASS(klass
);
191 I2CSlaveClass
*k
= I2C_SLAVE_CLASS(klass
);
193 k
->init
= max7310_init
;
194 k
->event
= max7310_event
;
195 k
->recv
= max7310_rx
;
196 k
->send
= max7310_tx
;
197 dc
->reset
= max7310_reset
;
198 dc
->vmsd
= &vmstate_max7310
;
201 static const TypeInfo max7310_info
= {
203 .parent
= TYPE_I2C_SLAVE
,
204 .instance_size
= sizeof(MAX7310State
),
205 .class_init
= max7310_class_init
,
208 static void max7310_register_types(void)
210 type_register_static(&max7310_info
);
213 type_init(max7310_register_types
)