]>
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" |
1c3bd33a | 16 | #include "qemu/log.h" |
0b8fa32f | 17 | #include "qemu/module.h" |
db1015e9 | 18 | #include "qom/object.h" |
adb86c37 | 19 | |
b8bcf811 | 20 | #define TYPE_MAX7310 "max7310" |
db1015e9 | 21 | typedef struct MAX7310State MAX7310State; |
8110fa1d EH |
22 | DECLARE_INSTANCE_CHECKER(MAX7310State, MAX7310, |
23 | TYPE_MAX7310) | |
b8bcf811 | 24 | |
db1015e9 | 25 | struct MAX7310State { |
b8bcf811 AF |
26 | I2CSlave parent_obj; |
27 | ||
adb86c37 AZ |
28 | int i2c_command_byte; |
29 | int len; | |
30 | ||
31 | uint8_t level; | |
32 | uint8_t direction; | |
33 | uint8_t polarity; | |
34 | uint8_t status; | |
35 | uint8_t command; | |
36 | qemu_irq handler[8]; | |
37 | qemu_irq *gpio_in; | |
db1015e9 | 38 | }; |
adb86c37 | 39 | |
987e8b3b | 40 | static void max7310_reset(DeviceState *dev) |
adb86c37 | 41 | { |
b8bcf811 AF |
42 | MAX7310State *s = MAX7310(dev); |
43 | ||
adb86c37 AZ |
44 | s->level &= s->direction; |
45 | s->direction = 0xff; | |
46 | s->polarity = 0xf0; | |
47 | s->status = 0x01; | |
48 | s->command = 0x00; | |
49 | } | |
50 | ||
2ac4c5f4 | 51 | static uint8_t max7310_rx(I2CSlave *i2c) |
adb86c37 | 52 | { |
b8bcf811 | 53 | MAX7310State *s = MAX7310(i2c); |
adb86c37 AZ |
54 | |
55 | switch (s->command) { | |
56 | case 0x00: /* Input port */ | |
57 | return s->level ^ s->polarity; | |
adb86c37 AZ |
58 | |
59 | case 0x01: /* Output port */ | |
60 | return s->level & ~s->direction; | |
adb86c37 AZ |
61 | |
62 | case 0x02: /* Polarity inversion */ | |
63 | return s->polarity; | |
64 | ||
65 | case 0x03: /* Configuration */ | |
66 | return s->direction; | |
67 | ||
68 | case 0x04: /* Timeout */ | |
69 | return s->status; | |
adb86c37 AZ |
70 | |
71 | case 0xff: /* Reserved */ | |
72 | return 0xff; | |
73 | ||
74 | default: | |
1c3bd33a PMD |
75 | qemu_log_mask(LOG_UNIMP, "%s: Unsupported register 0x02%" PRIx8 "\n", |
76 | __func__, s->command); | |
adb86c37 AZ |
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 | 127 | default: |
1c3bd33a PMD |
128 | qemu_log_mask(LOG_UNIMP, "%s: Unsupported register 0x02%" PRIx8 "\n", |
129 | __func__, s->command); | |
adb86c37 AZ |
130 | return 1; |
131 | } | |
132 | ||
133 | return 0; | |
134 | } | |
135 | ||
d307c28c | 136 | static int max7310_event(I2CSlave *i2c, enum i2c_event event) |
adb86c37 | 137 | { |
b8bcf811 | 138 | MAX7310State *s = MAX7310(i2c); |
adb86c37 AZ |
139 | s->len = 0; |
140 | ||
141 | switch (event) { | |
142 | case I2C_START_SEND: | |
143 | s->i2c_command_byte = 1; | |
144 | break; | |
145 | case I2C_FINISH: | |
adb86c37 | 146 | #ifdef VERBOSE |
e62ab7a1 | 147 | if (s->len == 1) |
a89f364a | 148 | printf("%s: message too short (%i bytes)\n", __func__, s->len); |
adb86c37 AZ |
149 | #endif |
150 | break; | |
151 | default: | |
152 | break; | |
153 | } | |
d307c28c CM |
154 | |
155 | return 0; | |
adb86c37 AZ |
156 | } |
157 | ||
7cb45faa JQ |
158 | static const VMStateDescription vmstate_max7310 = { |
159 | .name = "max7310", | |
160 | .version_id = 0, | |
161 | .minimum_version_id = 0, | |
8f1e884b | 162 | .fields = (VMStateField[]) { |
7cb45faa JQ |
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), | |
b8bcf811 | 170 | VMSTATE_I2C_SLAVE(parent_obj, MAX7310State), |
7cb45faa JQ |
171 | VMSTATE_END_OF_LIST() |
172 | } | |
173 | }; | |
aa941b94 | 174 | |
adb86c37 AZ |
175 | static void max7310_gpio_set(void *opaque, int line, int level) |
176 | { | |
bc24a225 | 177 | MAX7310State *s = (MAX7310State *) opaque; |
b1503cda | 178 | if (line >= ARRAY_SIZE(s->handler) || line < 0) |
87ecb68b | 179 | hw_error("bad GPIO line"); |
adb86c37 AZ |
180 | |
181 | if (level) | |
182 | s->level |= s->direction & (1 << line); | |
183 | else | |
184 | s->level &= ~(s->direction & (1 << line)); | |
185 | } | |
186 | ||
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. */ | |
c8c9e103 | 189 | static void max7310_realize(DeviceState *dev, Error **errp) |
adb86c37 | 190 | { |
c8c9e103 PMD |
191 | I2CSlave *i2c = I2C_SLAVE(dev); |
192 | MAX7310State *s = MAX7310(dev); | |
6c0bd6bd | 193 | |
987e8b3b DES |
194 | qdev_init_gpio_in(&i2c->qdev, max7310_gpio_set, 8); |
195 | qdev_init_gpio_out(&i2c->qdev, s->handler, 8); | |
adb86c37 AZ |
196 | } |
197 | ||
b5ea9327 AL |
198 | static void max7310_class_init(ObjectClass *klass, void *data) |
199 | { | |
39bffca2 | 200 | DeviceClass *dc = DEVICE_CLASS(klass); |
b5ea9327 AL |
201 | I2CSlaveClass *k = I2C_SLAVE_CLASS(klass); |
202 | ||
c8c9e103 | 203 | dc->realize = max7310_realize; |
b5ea9327 AL |
204 | k->event = max7310_event; |
205 | k->recv = max7310_rx; | |
206 | k->send = max7310_tx; | |
39bffca2 AL |
207 | dc->reset = max7310_reset; |
208 | dc->vmsd = &vmstate_max7310; | |
b5ea9327 AL |
209 | } |
210 | ||
8c43a6f0 | 211 | static const TypeInfo max7310_info = { |
b8bcf811 | 212 | .name = TYPE_MAX7310, |
39bffca2 AL |
213 | .parent = TYPE_I2C_SLAVE, |
214 | .instance_size = sizeof(MAX7310State), | |
215 | .class_init = max7310_class_init, | |
6c0bd6bd PB |
216 | }; |
217 | ||
83f7d43a | 218 | static void max7310_register_types(void) |
6c0bd6bd | 219 | { |
39bffca2 | 220 | type_register_static(&max7310_info); |
6c0bd6bd PB |
221 | } |
222 | ||
83f7d43a | 223 | type_init(max7310_register_types) |