root / hw / max7310.c @ 3ced9f7a
History | View | Annotate | Download (5 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 | 7cb45faa | Juan Quintela | static const VMStateDescription vmstate_max7310 = { |
147 | 7cb45faa | Juan Quintela | .name = "max7310",
|
148 | 7cb45faa | Juan Quintela | .version_id = 0,
|
149 | 7cb45faa | Juan Quintela | .minimum_version_id = 0,
|
150 | 7cb45faa | Juan Quintela | .minimum_version_id_old = 0,
|
151 | 7cb45faa | Juan Quintela | .fields = (VMStateField []) { |
152 | 7cb45faa | Juan Quintela | VMSTATE_INT32(i2c_command_byte, MAX7310State), |
153 | 7cb45faa | Juan Quintela | VMSTATE_INT32(len, MAX7310State), |
154 | 7cb45faa | Juan Quintela | VMSTATE_UINT8(level, MAX7310State), |
155 | 7cb45faa | Juan Quintela | VMSTATE_UINT8(direction, MAX7310State), |
156 | 7cb45faa | Juan Quintela | VMSTATE_UINT8(polarity, MAX7310State), |
157 | 7cb45faa | Juan Quintela | VMSTATE_UINT8(status, MAX7310State), |
158 | 7cb45faa | Juan Quintela | VMSTATE_UINT8(command, MAX7310State), |
159 | 7cb45faa | Juan Quintela | VMSTATE_I2C_SLAVE(i2c, MAX7310State), |
160 | 7cb45faa | Juan Quintela | VMSTATE_END_OF_LIST() |
161 | 7cb45faa | Juan Quintela | } |
162 | 7cb45faa | Juan Quintela | }; |
163 | aa941b94 | balrog | |
164 | adb86c37 | balrog | static void max7310_gpio_set(void *opaque, int line, int level) |
165 | adb86c37 | balrog | { |
166 | bc24a225 | Paul Brook | MAX7310State *s = (MAX7310State *) opaque; |
167 | b1503cda | malc | if (line >= ARRAY_SIZE(s->handler) || line < 0) |
168 | 87ecb68b | pbrook | hw_error("bad GPIO line");
|
169 | adb86c37 | balrog | |
170 | adb86c37 | balrog | if (level)
|
171 | adb86c37 | balrog | s->level |= s->direction & (1 << line);
|
172 | adb86c37 | balrog | else
|
173 | adb86c37 | balrog | s->level &= ~(s->direction & (1 << line));
|
174 | adb86c37 | balrog | } |
175 | adb86c37 | balrog | |
176 | adb86c37 | balrog | /* MAX7310 is SMBus-compatible (can be used with only SMBus protocols),
|
177 | adb86c37 | balrog | * but also accepts sequences that are not SMBus so return an I2C device. */
|
178 | 81a322d4 | Gerd Hoffmann | static int max7310_init(i2c_slave *i2c) |
179 | adb86c37 | balrog | { |
180 | 6c0bd6bd | Paul Brook | MAX7310State *s = FROM_I2C_SLAVE(MAX7310State, i2c); |
181 | 6c0bd6bd | Paul Brook | |
182 | adb86c37 | balrog | s->gpio_in = qemu_allocate_irqs(max7310_gpio_set, s, |
183 | b1503cda | malc | ARRAY_SIZE(s->handler)); |
184 | adb86c37 | balrog | |
185 | adb86c37 | balrog | max7310_reset(&s->i2c); |
186 | adb86c37 | balrog | |
187 | 7cb45faa | Juan Quintela | vmstate_register(-1, &vmstate_max7310, s);
|
188 | 81a322d4 | Gerd Hoffmann | return 0; |
189 | adb86c37 | balrog | } |
190 | adb86c37 | balrog | |
191 | adb86c37 | balrog | qemu_irq *max7310_gpio_in_get(i2c_slave *i2c) |
192 | adb86c37 | balrog | { |
193 | bc24a225 | Paul Brook | MAX7310State *s = (MAX7310State *) i2c; |
194 | adb86c37 | balrog | return s->gpio_in;
|
195 | adb86c37 | balrog | } |
196 | adb86c37 | balrog | |
197 | adb86c37 | balrog | void max7310_gpio_out_set(i2c_slave *i2c, int line, qemu_irq handler) |
198 | adb86c37 | balrog | { |
199 | bc24a225 | Paul Brook | MAX7310State *s = (MAX7310State *) i2c; |
200 | b1503cda | malc | if (line >= ARRAY_SIZE(s->handler) || line < 0) |
201 | 87ecb68b | pbrook | hw_error("bad GPIO line");
|
202 | adb86c37 | balrog | |
203 | adb86c37 | balrog | s->handler[line] = handler; |
204 | adb86c37 | balrog | } |
205 | 6c0bd6bd | Paul Brook | |
206 | 6c0bd6bd | Paul Brook | static I2CSlaveInfo max7310_info = {
|
207 | 074f2fff | Gerd Hoffmann | .qdev.name = "max7310",
|
208 | 074f2fff | Gerd Hoffmann | .qdev.size = sizeof(MAX7310State),
|
209 | 6c0bd6bd | Paul Brook | .init = max7310_init, |
210 | 6c0bd6bd | Paul Brook | .event = max7310_event, |
211 | 6c0bd6bd | Paul Brook | .recv = max7310_rx, |
212 | 6c0bd6bd | Paul Brook | .send = max7310_tx |
213 | 6c0bd6bd | Paul Brook | }; |
214 | 6c0bd6bd | Paul Brook | |
215 | 6c0bd6bd | Paul Brook | static void max7310_register_devices(void) |
216 | 6c0bd6bd | Paul Brook | { |
217 | 074f2fff | Gerd Hoffmann | i2c_register_slave(&max7310_info); |
218 | 6c0bd6bd | Paul Brook | } |
219 | 6c0bd6bd | Paul Brook | |
220 | 6c0bd6bd | Paul Brook | device_init(max7310_register_devices) |