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