]>
Commit | Line | Data |
---|---|---|
adb86c37 AZ |
1 | /* |
2 | * MAX7310 8-port GPIO expansion chip. | |
3 | * | |
4 | * Copyright (c) 2006 Openedhand Ltd. | |
5 | * Written by Andrzej Zaborowski <balrog@zabor.org> | |
6 | * | |
7 | * This file is licensed under GNU GPL. | |
8 | */ | |
9 | ||
0430891c | 10 | #include "qemu/osdep.h" |
650d103d | 11 | #include "hw/hw.h" |
0d09e41a | 12 | #include "hw/i2c/i2c.h" |
a27bd6c7 | 13 | #include "hw/hw.h" |
64552b6b | 14 | #include "hw/irq.h" |
d6454270 | 15 | #include "migration/vmstate.h" |
0b8fa32f | 16 | #include "qemu/module.h" |
adb86c37 | 17 | |
b8bcf811 AF |
18 | #define TYPE_MAX7310 "max7310" |
19 | #define MAX7310(obj) OBJECT_CHECK(MAX7310State, (obj), TYPE_MAX7310) | |
20 | ||
21 | typedef struct MAX7310State { | |
22 | I2CSlave parent_obj; | |
23 | ||
adb86c37 AZ |
24 | int i2c_command_byte; |
25 | int len; | |
26 | ||
27 | uint8_t level; | |
28 | uint8_t direction; | |
29 | uint8_t polarity; | |
30 | uint8_t status; | |
31 | uint8_t command; | |
32 | qemu_irq handler[8]; | |
33 | qemu_irq *gpio_in; | |
bc24a225 | 34 | } MAX7310State; |
adb86c37 | 35 | |
987e8b3b | 36 | static void max7310_reset(DeviceState *dev) |
adb86c37 | 37 | { |
b8bcf811 AF |
38 | MAX7310State *s = MAX7310(dev); |
39 | ||
adb86c37 AZ |
40 | s->level &= s->direction; |
41 | s->direction = 0xff; | |
42 | s->polarity = 0xf0; | |
43 | s->status = 0x01; | |
44 | s->command = 0x00; | |
45 | } | |
46 | ||
2ac4c5f4 | 47 | static uint8_t max7310_rx(I2CSlave *i2c) |
adb86c37 | 48 | { |
b8bcf811 | 49 | MAX7310State *s = MAX7310(i2c); |
adb86c37 AZ |
50 | |
51 | switch (s->command) { | |
52 | case 0x00: /* Input port */ | |
53 | return s->level ^ s->polarity; | |
54 | break; | |
55 | ||
56 | case 0x01: /* Output port */ | |
57 | return s->level & ~s->direction; | |
58 | break; | |
59 | ||
60 | case 0x02: /* Polarity inversion */ | |
61 | return s->polarity; | |
62 | ||
63 | case 0x03: /* Configuration */ | |
64 | return s->direction; | |
65 | ||
66 | case 0x04: /* Timeout */ | |
67 | return s->status; | |
68 | break; | |
69 | ||
70 | case 0xff: /* Reserved */ | |
71 | return 0xff; | |
72 | ||
73 | default: | |
74 | #ifdef VERBOSE | |
a89f364a | 75 | printf("%s: unknown register %02x\n", __func__, s->command); |
adb86c37 AZ |
76 | #endif |
77 | break; | |
78 | } | |
79 | return 0xff; | |
80 | } | |
81 | ||
9e07bdf8 | 82 | static int max7310_tx(I2CSlave *i2c, uint8_t data) |
adb86c37 | 83 | { |
b8bcf811 | 84 | MAX7310State *s = MAX7310(i2c); |
adb86c37 AZ |
85 | uint8_t diff; |
86 | int line; | |
87 | ||
88 | if (s->len ++ > 1) { | |
89 | #ifdef VERBOSE | |
a89f364a | 90 | printf("%s: message too long (%i bytes)\n", __func__, s->len); |
adb86c37 AZ |
91 | #endif |
92 | return 1; | |
93 | } | |
94 | ||
95 | if (s->i2c_command_byte) { | |
96 | s->command = data; | |
97 | s->i2c_command_byte = 0; | |
98 | return 0; | |
99 | } | |
100 | ||
101 | switch (s->command) { | |
102 | case 0x01: /* Output port */ | |
103 | for (diff = (data ^ s->level) & ~s->direction; diff; | |
104 | diff &= ~(1 << line)) { | |
786a4ea8 | 105 | line = ctz32(diff); |
adb86c37 AZ |
106 | if (s->handler[line]) |
107 | qemu_set_irq(s->handler[line], (data >> line) & 1); | |
108 | } | |
109 | s->level = (s->level & s->direction) | (data & ~s->direction); | |
110 | break; | |
111 | ||
112 | case 0x02: /* Polarity inversion */ | |
113 | s->polarity = data; | |
114 | break; | |
115 | ||
116 | case 0x03: /* Configuration */ | |
117 | s->level &= ~(s->direction ^ data); | |
118 | s->direction = data; | |
119 | break; | |
120 | ||
121 | case 0x04: /* Timeout */ | |
122 | s->status = data; | |
123 | break; | |
124 | ||
125 | case 0x00: /* Input port - ignore writes */ | |
7d37435b | 126 | break; |
adb86c37 AZ |
127 | default: |
128 | #ifdef VERBOSE | |
a89f364a | 129 | printf("%s: unknown register %02x\n", __func__, s->command); |
adb86c37 AZ |
130 | #endif |
131 | return 1; | |
132 | } | |
133 | ||
134 | return 0; | |
135 | } | |
136 | ||
d307c28c | 137 | static int max7310_event(I2CSlave *i2c, enum i2c_event event) |
adb86c37 | 138 | { |
b8bcf811 | 139 | MAX7310State *s = MAX7310(i2c); |
adb86c37 AZ |
140 | s->len = 0; |
141 | ||
142 | switch (event) { | |
143 | case I2C_START_SEND: | |
144 | s->i2c_command_byte = 1; | |
145 | break; | |
146 | case I2C_FINISH: | |
adb86c37 | 147 | #ifdef VERBOSE |
e62ab7a1 | 148 | if (s->len == 1) |
a89f364a | 149 | printf("%s: message too short (%i bytes)\n", __func__, s->len); |
adb86c37 AZ |
150 | #endif |
151 | break; | |
152 | default: | |
153 | break; | |
154 | } | |
d307c28c CM |
155 | |
156 | return 0; | |
adb86c37 AZ |
157 | } |
158 | ||
7cb45faa JQ |
159 | static const VMStateDescription vmstate_max7310 = { |
160 | .name = "max7310", | |
161 | .version_id = 0, | |
162 | .minimum_version_id = 0, | |
8f1e884b | 163 | .fields = (VMStateField[]) { |
7cb45faa JQ |
164 | VMSTATE_INT32(i2c_command_byte, MAX7310State), |
165 | VMSTATE_INT32(len, MAX7310State), | |
166 | VMSTATE_UINT8(level, MAX7310State), | |
167 | VMSTATE_UINT8(direction, MAX7310State), | |
168 | VMSTATE_UINT8(polarity, MAX7310State), | |
169 | VMSTATE_UINT8(status, MAX7310State), | |
170 | VMSTATE_UINT8(command, MAX7310State), | |
b8bcf811 | 171 | VMSTATE_I2C_SLAVE(parent_obj, MAX7310State), |
7cb45faa JQ |
172 | VMSTATE_END_OF_LIST() |
173 | } | |
174 | }; | |
aa941b94 | 175 | |
adb86c37 AZ |
176 | static void max7310_gpio_set(void *opaque, int line, int level) |
177 | { | |
bc24a225 | 178 | MAX7310State *s = (MAX7310State *) opaque; |
b1503cda | 179 | if (line >= ARRAY_SIZE(s->handler) || line < 0) |
87ecb68b | 180 | hw_error("bad GPIO line"); |
adb86c37 AZ |
181 | |
182 | if (level) | |
183 | s->level |= s->direction & (1 << line); | |
184 | else | |
185 | s->level &= ~(s->direction & (1 << line)); | |
186 | } | |
187 | ||
188 | /* MAX7310 is SMBus-compatible (can be used with only SMBus protocols), | |
189 | * but also accepts sequences that are not SMBus so return an I2C device. */ | |
c8c9e103 | 190 | static void max7310_realize(DeviceState *dev, Error **errp) |
adb86c37 | 191 | { |
c8c9e103 PMD |
192 | I2CSlave *i2c = I2C_SLAVE(dev); |
193 | MAX7310State *s = MAX7310(dev); | |
6c0bd6bd | 194 | |
987e8b3b DES |
195 | qdev_init_gpio_in(&i2c->qdev, max7310_gpio_set, 8); |
196 | qdev_init_gpio_out(&i2c->qdev, s->handler, 8); | |
adb86c37 AZ |
197 | } |
198 | ||
b5ea9327 AL |
199 | static void max7310_class_init(ObjectClass *klass, void *data) |
200 | { | |
39bffca2 | 201 | DeviceClass *dc = DEVICE_CLASS(klass); |
b5ea9327 AL |
202 | I2CSlaveClass *k = I2C_SLAVE_CLASS(klass); |
203 | ||
c8c9e103 | 204 | dc->realize = max7310_realize; |
b5ea9327 AL |
205 | k->event = max7310_event; |
206 | k->recv = max7310_rx; | |
207 | k->send = max7310_tx; | |
39bffca2 AL |
208 | dc->reset = max7310_reset; |
209 | dc->vmsd = &vmstate_max7310; | |
b5ea9327 AL |
210 | } |
211 | ||
8c43a6f0 | 212 | static const TypeInfo max7310_info = { |
b8bcf811 | 213 | .name = TYPE_MAX7310, |
39bffca2 AL |
214 | .parent = TYPE_I2C_SLAVE, |
215 | .instance_size = sizeof(MAX7310State), | |
216 | .class_init = max7310_class_init, | |
6c0bd6bd PB |
217 | }; |
218 | ||
83f7d43a | 219 | static void max7310_register_types(void) |
6c0bd6bd | 220 | { |
39bffca2 | 221 | type_register_static(&max7310_info); |
6c0bd6bd PB |
222 | } |
223 | ||
83f7d43a | 224 | type_init(max7310_register_types) |