Merge remote-tracking branch 'remotes/juanquintela/tags/migration/20151203' into...
[qemu/ar7.git] / hw / gpio / max7310.c
blob2f59b134ee5b579af9790f30cf077548854faa9e
1 /*
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.
8 */
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 {
16 I2CSlave parent_obj;
18 int i2c_command_byte;
19 int len;
21 uint8_t level;
22 uint8_t direction;
23 uint8_t polarity;
24 uint8_t status;
25 uint8_t command;
26 qemu_irq handler[8];
27 qemu_irq *gpio_in;
28 } MAX7310State;
30 static void max7310_reset(DeviceState *dev)
32 MAX7310State *s = MAX7310(dev);
34 s->level &= s->direction;
35 s->direction = 0xff;
36 s->polarity = 0xf0;
37 s->status = 0x01;
38 s->command = 0x00;
41 static int max7310_rx(I2CSlave *i2c)
43 MAX7310State *s = MAX7310(i2c);
45 switch (s->command) {
46 case 0x00: /* Input port */
47 return s->level ^ s->polarity;
48 break;
50 case 0x01: /* Output port */
51 return s->level & ~s->direction;
52 break;
54 case 0x02: /* Polarity inversion */
55 return s->polarity;
57 case 0x03: /* Configuration */
58 return s->direction;
60 case 0x04: /* Timeout */
61 return s->status;
62 break;
64 case 0xff: /* Reserved */
65 return 0xff;
67 default:
68 #ifdef VERBOSE
69 printf("%s: unknown register %02x\n", __FUNCTION__, s->command);
70 #endif
71 break;
73 return 0xff;
76 static int max7310_tx(I2CSlave *i2c, uint8_t data)
78 MAX7310State *s = MAX7310(i2c);
79 uint8_t diff;
80 int line;
82 if (s->len ++ > 1) {
83 #ifdef VERBOSE
84 printf("%s: message too long (%i bytes)\n", __FUNCTION__, s->len);
85 #endif
86 return 1;
89 if (s->i2c_command_byte) {
90 s->command = data;
91 s->i2c_command_byte = 0;
92 return 0;
95 switch (s->command) {
96 case 0x01: /* Output port */
97 for (diff = (data ^ s->level) & ~s->direction; diff;
98 diff &= ~(1 << line)) {
99 line = ctz32(diff);
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);
104 break;
106 case 0x02: /* Polarity inversion */
107 s->polarity = data;
108 break;
110 case 0x03: /* Configuration */
111 s->level &= ~(s->direction ^ data);
112 s->direction = data;
113 break;
115 case 0x04: /* Timeout */
116 s->status = data;
117 break;
119 case 0x00: /* Input port - ignore writes */
120 break;
121 default:
122 #ifdef VERBOSE
123 printf("%s: unknown register %02x\n", __FUNCTION__, s->command);
124 #endif
125 return 1;
128 return 0;
131 static void max7310_event(I2CSlave *i2c, enum i2c_event event)
133 MAX7310State *s = MAX7310(i2c);
134 s->len = 0;
136 switch (event) {
137 case I2C_START_SEND:
138 s->i2c_command_byte = 1;
139 break;
140 case I2C_FINISH:
141 #ifdef VERBOSE
142 if (s->len == 1)
143 printf("%s: message too short (%i bytes)\n", __FUNCTION__, s->len);
144 #endif
145 break;
146 default:
147 break;
151 static const VMStateDescription vmstate_max7310 = {
152 .name = "max7310",
153 .version_id = 0,
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");
174 if (level)
175 s->level |= s->direction & (1 << line);
176 else
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);
189 return 0;
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)