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"
12 #include "hw/i2c/i2c.h"
15 #include "migration/vmstate.h"
17 #include "qemu/module.h"
18 #include "qom/object.h"
20 #define TYPE_MAX7310 "max7310"
21 typedef struct MAX7310State MAX7310State
;
22 DECLARE_INSTANCE_CHECKER(MAX7310State
, MAX7310
,
40 static void max7310_reset(DeviceState
*dev
)
42 MAX7310State
*s
= MAX7310(dev
);
44 s
->level
&= s
->direction
;
51 static uint8_t max7310_rx(I2CSlave
*i2c
)
53 MAX7310State
*s
= MAX7310(i2c
);
56 case 0x00: /* Input port */
57 return s
->level
^ s
->polarity
;
59 case 0x01: /* Output port */
60 return s
->level
& ~s
->direction
;
62 case 0x02: /* Polarity inversion */
65 case 0x03: /* Configuration */
68 case 0x04: /* Timeout */
71 case 0xff: /* Reserved */
75 qemu_log_mask(LOG_UNIMP
, "%s: Unsupported register 0x02%" PRIx8
"\n",
76 __func__
, s
->command
);
82 static int max7310_tx(I2CSlave
*i2c
, uint8_t data
)
84 MAX7310State
*s
= MAX7310(i2c
);
90 printf("%s: message too long (%i bytes)\n", __func__
, s
->len
);
95 if (s
->i2c_command_byte
) {
97 s
->i2c_command_byte
= 0;
101 switch (s
->command
) {
102 case 0x01: /* Output port */
103 for (diff
= (data
^ s
->level
) & ~s
->direction
; diff
;
104 diff
&= ~(1 << line
)) {
106 if (s
->handler
[line
])
107 qemu_set_irq(s
->handler
[line
], (data
>> line
) & 1);
109 s
->level
= (s
->level
& s
->direction
) | (data
& ~s
->direction
);
112 case 0x02: /* Polarity inversion */
116 case 0x03: /* Configuration */
117 s
->level
&= ~(s
->direction
^ data
);
121 case 0x04: /* Timeout */
125 case 0x00: /* Input port - ignore writes */
128 qemu_log_mask(LOG_UNIMP
, "%s: Unsupported register 0x02%" PRIx8
"\n",
129 __func__
, s
->command
);
136 static int max7310_event(I2CSlave
*i2c
, enum i2c_event event
)
138 MAX7310State
*s
= MAX7310(i2c
);
143 s
->i2c_command_byte
= 1;
148 printf("%s: message too short (%i bytes)\n", __func__
, s
->len
);
158 static const VMStateDescription vmstate_max7310
= {
161 .minimum_version_id
= 0,
162 .fields
= (VMStateField
[]) {
163 VMSTATE_INT32(i2c_command_byte
, MAX7310State
),
164 VMSTATE_INT32(len
, MAX7310State
),
165 VMSTATE_UINT8(level
, MAX7310State
),
166 VMSTATE_UINT8(direction
, MAX7310State
),
167 VMSTATE_UINT8(polarity
, MAX7310State
),
168 VMSTATE_UINT8(status
, MAX7310State
),
169 VMSTATE_UINT8(command
, MAX7310State
),
170 VMSTATE_I2C_SLAVE(parent_obj
, MAX7310State
),
171 VMSTATE_END_OF_LIST()
175 static void max7310_gpio_set(void *opaque
, int line
, int level
)
177 MAX7310State
*s
= (MAX7310State
*) opaque
;
178 if (line
>= ARRAY_SIZE(s
->handler
) || line
< 0)
179 hw_error("bad GPIO line");
182 s
->level
|= s
->direction
& (1 << line
);
184 s
->level
&= ~(s
->direction
& (1 << line
));
187 /* MAX7310 is SMBus-compatible (can be used with only SMBus protocols),
188 * but also accepts sequences that are not SMBus so return an I2C device. */
189 static void max7310_realize(DeviceState
*dev
, Error
**errp
)
191 I2CSlave
*i2c
= I2C_SLAVE(dev
);
192 MAX7310State
*s
= MAX7310(dev
);
194 qdev_init_gpio_in(&i2c
->qdev
, max7310_gpio_set
, 8);
195 qdev_init_gpio_out(&i2c
->qdev
, s
->handler
, 8);
198 static void max7310_class_init(ObjectClass
*klass
, void *data
)
200 DeviceClass
*dc
= DEVICE_CLASS(klass
);
201 I2CSlaveClass
*k
= I2C_SLAVE_CLASS(klass
);
203 dc
->realize
= max7310_realize
;
204 k
->event
= max7310_event
;
205 k
->recv
= max7310_rx
;
206 k
->send
= max7310_tx
;
207 dc
->reset
= max7310_reset
;
208 dc
->vmsd
= &vmstate_max7310
;
211 static const TypeInfo max7310_info
= {
212 .name
= TYPE_MAX7310
,
213 .parent
= TYPE_I2C_SLAVE
,
214 .instance_size
= sizeof(MAX7310State
),
215 .class_init
= max7310_class_init
,
218 static void max7310_register_types(void)
220 type_register_static(&max7310_info
);
223 type_init(max7310_register_types
)