root / hw / max7310.c @ e9b43ea3
History | View | Annotate | Download (5.2 kB)
1 | adb86c37 | balrog | /*
|
---|---|---|---|
2 | adb86c37 | balrog | * MAX7310 8-port GPIO expansion chip.
|
3 | adb86c37 | balrog | *
|
4 | adb86c37 | balrog | * Copyright (c) 2006 Openedhand Ltd.
|
5 | adb86c37 | balrog | * Written by Andrzej Zaborowski <balrog@zabor.org>
|
6 | adb86c37 | balrog | *
|
7 | adb86c37 | balrog | * This file is licensed under GNU GPL.
|
8 | adb86c37 | balrog | */
|
9 | adb86c37 | balrog | |
10 | 87ecb68b | pbrook | #include "i2c.h" |
11 | adb86c37 | balrog | |
12 | bc24a225 | Paul Brook | typedef struct { |
13 | adb86c37 | balrog | i2c_slave i2c; |
14 | adb86c37 | balrog | int i2c_command_byte;
|
15 | adb86c37 | balrog | int len;
|
16 | adb86c37 | balrog | |
17 | adb86c37 | balrog | uint8_t level; |
18 | adb86c37 | balrog | uint8_t direction; |
19 | adb86c37 | balrog | uint8_t polarity; |
20 | adb86c37 | balrog | uint8_t status; |
21 | adb86c37 | balrog | uint8_t command; |
22 | adb86c37 | balrog | qemu_irq handler[8];
|
23 | adb86c37 | balrog | qemu_irq *gpio_in; |
24 | bc24a225 | Paul Brook | } MAX7310State; |
25 | adb86c37 | balrog | |
26 | adb86c37 | balrog | void max7310_reset(i2c_slave *i2c)
|
27 | adb86c37 | balrog | { |
28 | bc24a225 | Paul Brook | MAX7310State *s = (MAX7310State *) i2c; |
29 | adb86c37 | balrog | s->level &= s->direction; |
30 | adb86c37 | balrog | s->direction = 0xff;
|
31 | adb86c37 | balrog | s->polarity = 0xf0;
|
32 | adb86c37 | balrog | s->status = 0x01;
|
33 | adb86c37 | balrog | s->command = 0x00;
|
34 | adb86c37 | balrog | } |
35 | adb86c37 | balrog | |
36 | adb86c37 | balrog | static int max7310_rx(i2c_slave *i2c) |
37 | adb86c37 | balrog | { |
38 | bc24a225 | Paul Brook | MAX7310State *s = (MAX7310State *) i2c; |
39 | adb86c37 | balrog | |
40 | adb86c37 | balrog | switch (s->command) {
|
41 | adb86c37 | balrog | case 0x00: /* Input port */ |
42 | adb86c37 | balrog | return s->level ^ s->polarity;
|
43 | adb86c37 | balrog | break;
|
44 | adb86c37 | balrog | |
45 | adb86c37 | balrog | case 0x01: /* Output port */ |
46 | adb86c37 | balrog | return s->level & ~s->direction;
|
47 | adb86c37 | balrog | break;
|
48 | adb86c37 | balrog | |
49 | adb86c37 | balrog | case 0x02: /* Polarity inversion */ |
50 | adb86c37 | balrog | return s->polarity;
|
51 | adb86c37 | balrog | |
52 | adb86c37 | balrog | case 0x03: /* Configuration */ |
53 | adb86c37 | balrog | return s->direction;
|
54 | adb86c37 | balrog | |
55 | adb86c37 | balrog | case 0x04: /* Timeout */ |
56 | adb86c37 | balrog | return s->status;
|
57 | adb86c37 | balrog | break;
|
58 | adb86c37 | balrog | |
59 | adb86c37 | balrog | case 0xff: /* Reserved */ |
60 | adb86c37 | balrog | return 0xff; |
61 | adb86c37 | balrog | |
62 | adb86c37 | balrog | default:
|
63 | adb86c37 | balrog | #ifdef VERBOSE
|
64 | adb86c37 | balrog | printf("%s: unknown register %02x\n", __FUNCTION__, s->command);
|
65 | adb86c37 | balrog | #endif
|
66 | adb86c37 | balrog | break;
|
67 | adb86c37 | balrog | } |
68 | adb86c37 | balrog | return 0xff; |
69 | adb86c37 | balrog | } |
70 | adb86c37 | balrog | |
71 | adb86c37 | balrog | static int max7310_tx(i2c_slave *i2c, uint8_t data) |
72 | adb86c37 | balrog | { |
73 | bc24a225 | Paul Brook | MAX7310State *s = (MAX7310State *) i2c; |
74 | adb86c37 | balrog | uint8_t diff; |
75 | adb86c37 | balrog | int line;
|
76 | adb86c37 | balrog | |
77 | adb86c37 | balrog | if (s->len ++ > 1) { |
78 | adb86c37 | balrog | #ifdef VERBOSE
|
79 | adb86c37 | balrog | printf("%s: message too long (%i bytes)\n", __FUNCTION__, s->len);
|
80 | adb86c37 | balrog | #endif
|
81 | adb86c37 | balrog | return 1; |
82 | adb86c37 | balrog | } |
83 | adb86c37 | balrog | |
84 | adb86c37 | balrog | if (s->i2c_command_byte) {
|
85 | adb86c37 | balrog | s->command = data; |
86 | adb86c37 | balrog | s->i2c_command_byte = 0;
|
87 | adb86c37 | balrog | return 0; |
88 | adb86c37 | balrog | } |
89 | adb86c37 | balrog | |
90 | adb86c37 | balrog | switch (s->command) {
|
91 | adb86c37 | balrog | case 0x01: /* Output port */ |
92 | adb86c37 | balrog | for (diff = (data ^ s->level) & ~s->direction; diff;
|
93 | adb86c37 | balrog | diff &= ~(1 << line)) {
|
94 | adb86c37 | balrog | line = ffs(diff) - 1;
|
95 | adb86c37 | balrog | if (s->handler[line])
|
96 | adb86c37 | balrog | qemu_set_irq(s->handler[line], (data >> line) & 1);
|
97 | adb86c37 | balrog | } |
98 | adb86c37 | balrog | s->level = (s->level & s->direction) | (data & ~s->direction); |
99 | adb86c37 | balrog | break;
|
100 | adb86c37 | balrog | |
101 | adb86c37 | balrog | case 0x02: /* Polarity inversion */ |
102 | adb86c37 | balrog | s->polarity = data; |
103 | adb86c37 | balrog | break;
|
104 | adb86c37 | balrog | |
105 | adb86c37 | balrog | case 0x03: /* Configuration */ |
106 | adb86c37 | balrog | s->level &= ~(s->direction ^ data); |
107 | adb86c37 | balrog | s->direction = data; |
108 | adb86c37 | balrog | break;
|
109 | adb86c37 | balrog | |
110 | adb86c37 | balrog | case 0x04: /* Timeout */ |
111 | adb86c37 | balrog | s->status = data; |
112 | adb86c37 | balrog | break;
|
113 | adb86c37 | balrog | |
114 | adb86c37 | balrog | case 0x00: /* Input port - ignore writes */ |
115 | adb86c37 | balrog | break;
|
116 | adb86c37 | balrog | default:
|
117 | adb86c37 | balrog | #ifdef VERBOSE
|
118 | adb86c37 | balrog | printf("%s: unknown register %02x\n", __FUNCTION__, s->command);
|
119 | adb86c37 | balrog | #endif
|
120 | adb86c37 | balrog | return 1; |
121 | adb86c37 | balrog | } |
122 | adb86c37 | balrog | |
123 | adb86c37 | balrog | return 0; |
124 | adb86c37 | balrog | } |
125 | adb86c37 | balrog | |
126 | adb86c37 | balrog | static void max7310_event(i2c_slave *i2c, enum i2c_event event) |
127 | adb86c37 | balrog | { |
128 | bc24a225 | Paul Brook | MAX7310State *s = (MAX7310State *) i2c; |
129 | adb86c37 | balrog | s->len = 0;
|
130 | adb86c37 | balrog | |
131 | adb86c37 | balrog | switch (event) {
|
132 | adb86c37 | balrog | case I2C_START_SEND:
|
133 | adb86c37 | balrog | s->i2c_command_byte = 1;
|
134 | adb86c37 | balrog | break;
|
135 | adb86c37 | balrog | case I2C_FINISH:
|
136 | adb86c37 | balrog | #ifdef VERBOSE
|
137 | e62ab7a1 | balrog | if (s->len == 1) |
138 | adb86c37 | balrog | printf("%s: message too short (%i bytes)\n", __FUNCTION__, s->len);
|
139 | adb86c37 | balrog | #endif
|
140 | adb86c37 | balrog | break;
|
141 | adb86c37 | balrog | default:
|
142 | adb86c37 | balrog | break;
|
143 | adb86c37 | balrog | } |
144 | adb86c37 | balrog | } |
145 | adb86c37 | balrog | |
146 | aa941b94 | balrog | static void max7310_save(QEMUFile *f, void *opaque) |
147 | aa941b94 | balrog | { |
148 | bc24a225 | Paul Brook | MAX7310State *s = (MAX7310State *) opaque; |
149 | aa941b94 | balrog | |
150 | aa941b94 | balrog | qemu_put_be32(f, s->i2c_command_byte); |
151 | aa941b94 | balrog | qemu_put_be32(f, s->len); |
152 | aa941b94 | balrog | |
153 | aa941b94 | balrog | qemu_put_8s(f, &s->level); |
154 | aa941b94 | balrog | qemu_put_8s(f, &s->direction); |
155 | aa941b94 | balrog | qemu_put_8s(f, &s->polarity); |
156 | aa941b94 | balrog | qemu_put_8s(f, &s->status); |
157 | aa941b94 | balrog | qemu_put_8s(f, &s->command); |
158 | aa941b94 | balrog | |
159 | aa941b94 | balrog | i2c_slave_save(f, &s->i2c); |
160 | aa941b94 | balrog | } |
161 | aa941b94 | balrog | |
162 | aa941b94 | balrog | static int max7310_load(QEMUFile *f, void *opaque, int version_id) |
163 | aa941b94 | balrog | { |
164 | bc24a225 | Paul Brook | MAX7310State *s = (MAX7310State *) opaque; |
165 | aa941b94 | balrog | |
166 | aa941b94 | balrog | s->i2c_command_byte = qemu_get_be32(f); |
167 | aa941b94 | balrog | s->len = qemu_get_be32(f); |
168 | aa941b94 | balrog | |
169 | aa941b94 | balrog | qemu_get_8s(f, &s->level); |
170 | aa941b94 | balrog | qemu_get_8s(f, &s->direction); |
171 | aa941b94 | balrog | qemu_get_8s(f, &s->polarity); |
172 | aa941b94 | balrog | qemu_get_8s(f, &s->status); |
173 | aa941b94 | balrog | qemu_get_8s(f, &s->command); |
174 | aa941b94 | balrog | |
175 | aa941b94 | balrog | i2c_slave_load(f, &s->i2c); |
176 | aa941b94 | balrog | return 0; |
177 | aa941b94 | balrog | } |
178 | aa941b94 | balrog | |
179 | adb86c37 | balrog | static void max7310_gpio_set(void *opaque, int line, int level) |
180 | adb86c37 | balrog | { |
181 | bc24a225 | Paul Brook | MAX7310State *s = (MAX7310State *) opaque; |
182 | b1503cda | malc | if (line >= ARRAY_SIZE(s->handler) || line < 0) |
183 | 87ecb68b | pbrook | hw_error("bad GPIO line");
|
184 | adb86c37 | balrog | |
185 | adb86c37 | balrog | if (level)
|
186 | adb86c37 | balrog | s->level |= s->direction & (1 << line);
|
187 | adb86c37 | balrog | else
|
188 | adb86c37 | balrog | s->level &= ~(s->direction & (1 << line));
|
189 | adb86c37 | balrog | } |
190 | adb86c37 | balrog | |
191 | adb86c37 | balrog | /* MAX7310 is SMBus-compatible (can be used with only SMBus protocols),
|
192 | adb86c37 | balrog | * but also accepts sequences that are not SMBus so return an I2C device. */
|
193 | 81a322d4 | Gerd Hoffmann | static int max7310_init(i2c_slave *i2c) |
194 | adb86c37 | balrog | { |
195 | 6c0bd6bd | Paul Brook | MAX7310State *s = FROM_I2C_SLAVE(MAX7310State, i2c); |
196 | 6c0bd6bd | Paul Brook | |
197 | adb86c37 | balrog | s->gpio_in = qemu_allocate_irqs(max7310_gpio_set, s, |
198 | b1503cda | malc | ARRAY_SIZE(s->handler)); |
199 | adb86c37 | balrog | |
200 | adb86c37 | balrog | max7310_reset(&s->i2c); |
201 | adb86c37 | balrog | |
202 | 18be5187 | pbrook | register_savevm("max7310", -1, 0, max7310_save, max7310_load, s); |
203 | 81a322d4 | Gerd Hoffmann | return 0; |
204 | adb86c37 | balrog | } |
205 | adb86c37 | balrog | |
206 | adb86c37 | balrog | qemu_irq *max7310_gpio_in_get(i2c_slave *i2c) |
207 | adb86c37 | balrog | { |
208 | bc24a225 | Paul Brook | MAX7310State *s = (MAX7310State *) i2c; |
209 | adb86c37 | balrog | return s->gpio_in;
|
210 | adb86c37 | balrog | } |
211 | adb86c37 | balrog | |
212 | adb86c37 | balrog | void max7310_gpio_out_set(i2c_slave *i2c, int line, qemu_irq handler) |
213 | adb86c37 | balrog | { |
214 | bc24a225 | Paul Brook | MAX7310State *s = (MAX7310State *) i2c; |
215 | b1503cda | malc | if (line >= ARRAY_SIZE(s->handler) || line < 0) |
216 | 87ecb68b | pbrook | hw_error("bad GPIO line");
|
217 | adb86c37 | balrog | |
218 | adb86c37 | balrog | s->handler[line] = handler; |
219 | adb86c37 | balrog | } |
220 | 6c0bd6bd | Paul Brook | |
221 | 6c0bd6bd | Paul Brook | static I2CSlaveInfo max7310_info = {
|
222 | 074f2fff | Gerd Hoffmann | .qdev.name = "max7310",
|
223 | 074f2fff | Gerd Hoffmann | .qdev.size = sizeof(MAX7310State),
|
224 | 6c0bd6bd | Paul Brook | .init = max7310_init, |
225 | 6c0bd6bd | Paul Brook | .event = max7310_event, |
226 | 6c0bd6bd | Paul Brook | .recv = max7310_rx, |
227 | 6c0bd6bd | Paul Brook | .send = max7310_tx |
228 | 6c0bd6bd | Paul Brook | }; |
229 | 6c0bd6bd | Paul Brook | |
230 | 6c0bd6bd | Paul Brook | static void max7310_register_devices(void) |
231 | 6c0bd6bd | Paul Brook | { |
232 | 074f2fff | Gerd Hoffmann | i2c_register_slave(&max7310_info); |
233 | 6c0bd6bd | Paul Brook | } |
234 | 6c0bd6bd | Paul Brook | |
235 | 6c0bd6bd | Paul Brook | device_init(max7310_register_devices) |