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