root / hw / lm832x.c @ a6307b08
History | View | Annotate | Download (14.6 kB)
1 | 1d4e547b | balrog | /*
|
---|---|---|---|
2 | 1d4e547b | balrog | * National Semiconductor LM8322/8323 GPIO keyboard & PWM chips.
|
3 | 1d4e547b | balrog | *
|
4 | 1d4e547b | balrog | * Copyright (C) 2008 Nokia Corporation
|
5 | 1d4e547b | balrog | * Written by Andrzej Zaborowski <andrew@openedhand.com>
|
6 | 1d4e547b | balrog | *
|
7 | 1d4e547b | balrog | * This program is free software; you can redistribute it and/or
|
8 | 1d4e547b | balrog | * modify it under the terms of the GNU General Public License as
|
9 | 1d4e547b | balrog | * published by the Free Software Foundation; either version 2 or
|
10 | 1d4e547b | balrog | * (at your option) version 3 of the License.
|
11 | 1d4e547b | balrog | *
|
12 | 1d4e547b | balrog | * This program is distributed in the hope that it will be useful,
|
13 | 1d4e547b | balrog | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14 | 1d4e547b | balrog | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15 | 1d4e547b | balrog | * GNU General Public License for more details.
|
16 | 1d4e547b | balrog | *
|
17 | fad6cb1a | aurel32 | * You should have received a copy of the GNU General Public License along
|
18 | 8167ee88 | Blue Swirl | * with this program; if not, see <http://www.gnu.org/licenses/>.
|
19 | 1d4e547b | balrog | */
|
20 | 1d4e547b | balrog | |
21 | 1d4e547b | balrog | #include "hw.h" |
22 | 1d4e547b | balrog | #include "i2c.h" |
23 | 1d4e547b | balrog | #include "qemu-timer.h" |
24 | 1d4e547b | balrog | #include "console.h" |
25 | 1d4e547b | balrog | |
26 | bc24a225 | Paul Brook | typedef struct { |
27 | 1d4e547b | balrog | i2c_slave i2c; |
28 | 1d4e547b | balrog | int i2c_dir;
|
29 | 1d4e547b | balrog | int i2c_cycle;
|
30 | 1d4e547b | balrog | int reg;
|
31 | 1d4e547b | balrog | |
32 | 1d4e547b | balrog | qemu_irq nirq; |
33 | 1d4e547b | balrog | uint16_t model; |
34 | 1d4e547b | balrog | |
35 | 1d4e547b | balrog | struct {
|
36 | 1d4e547b | balrog | qemu_irq out[2];
|
37 | 1d4e547b | balrog | int in[2][2]; |
38 | 1d4e547b | balrog | } mux; |
39 | 1d4e547b | balrog | |
40 | 1d4e547b | balrog | uint8_t config; |
41 | 1d4e547b | balrog | uint8_t status; |
42 | 1d4e547b | balrog | uint8_t acttime; |
43 | 1d4e547b | balrog | uint8_t error; |
44 | 1d4e547b | balrog | uint8_t clock; |
45 | 1d4e547b | balrog | |
46 | 1d4e547b | balrog | struct {
|
47 | 1d4e547b | balrog | uint16_t pull; |
48 | 1d4e547b | balrog | uint16_t mask; |
49 | 1d4e547b | balrog | uint16_t dir; |
50 | 1d4e547b | balrog | uint16_t level; |
51 | 1d4e547b | balrog | qemu_irq out[16];
|
52 | 1d4e547b | balrog | } gpio; |
53 | 1d4e547b | balrog | |
54 | 1d4e547b | balrog | struct {
|
55 | 1d4e547b | balrog | uint8_t dbnctime; |
56 | 1d4e547b | balrog | uint8_t size; |
57 | 1d4e547b | balrog | int start;
|
58 | 1d4e547b | balrog | int len;
|
59 | 1d4e547b | balrog | uint8_t fifo[16];
|
60 | 1d4e547b | balrog | } kbd; |
61 | 1d4e547b | balrog | |
62 | 1d4e547b | balrog | struct {
|
63 | 1d4e547b | balrog | uint16_t file[256];
|
64 | 1d4e547b | balrog | uint8_t faddr; |
65 | 1d4e547b | balrog | uint8_t addr[3];
|
66 | 1d4e547b | balrog | QEMUTimer *tm[3];
|
67 | 1d4e547b | balrog | } pwm; |
68 | bc24a225 | Paul Brook | } LM823KbdState; |
69 | 1d4e547b | balrog | |
70 | 1d4e547b | balrog | #define INT_KEYPAD (1 << 0) |
71 | 1d4e547b | balrog | #define INT_ERROR (1 << 3) |
72 | 1d4e547b | balrog | #define INT_NOINIT (1 << 4) |
73 | 1d4e547b | balrog | #define INT_PWMEND(n) (1 << (5 + n)) |
74 | 1d4e547b | balrog | |
75 | 1d4e547b | balrog | #define ERR_BADPAR (1 << 0) |
76 | 1d4e547b | balrog | #define ERR_CMDUNK (1 << 1) |
77 | 1d4e547b | balrog | #define ERR_KEYOVR (1 << 2) |
78 | 1d4e547b | balrog | #define ERR_FIFOOVR (1 << 6) |
79 | 1d4e547b | balrog | |
80 | bc24a225 | Paul Brook | static void lm_kbd_irq_update(LM823KbdState *s) |
81 | 1d4e547b | balrog | { |
82 | 1d4e547b | balrog | qemu_set_irq(s->nirq, !s->status); |
83 | 1d4e547b | balrog | } |
84 | 1d4e547b | balrog | |
85 | bc24a225 | Paul Brook | static void lm_kbd_gpio_update(LM823KbdState *s) |
86 | 1d4e547b | balrog | { |
87 | 1d4e547b | balrog | } |
88 | 1d4e547b | balrog | |
89 | bc24a225 | Paul Brook | static void lm_kbd_reset(LM823KbdState *s) |
90 | 1d4e547b | balrog | { |
91 | 1d4e547b | balrog | s->config = 0x80;
|
92 | 1d4e547b | balrog | s->status = INT_NOINIT; |
93 | 1d4e547b | balrog | s->acttime = 125;
|
94 | 1d4e547b | balrog | s->kbd.dbnctime = 3;
|
95 | 1d4e547b | balrog | s->kbd.size = 0x33;
|
96 | 1d4e547b | balrog | s->clock = 0x08;
|
97 | 1d4e547b | balrog | |
98 | 1d4e547b | balrog | lm_kbd_irq_update(s); |
99 | 1d4e547b | balrog | lm_kbd_gpio_update(s); |
100 | 1d4e547b | balrog | } |
101 | 1d4e547b | balrog | |
102 | bc24a225 | Paul Brook | static void lm_kbd_error(LM823KbdState *s, int err) |
103 | 1d4e547b | balrog | { |
104 | 1d4e547b | balrog | s->error |= err; |
105 | 1d4e547b | balrog | s->status |= INT_ERROR; |
106 | 1d4e547b | balrog | lm_kbd_irq_update(s); |
107 | 1d4e547b | balrog | } |
108 | 1d4e547b | balrog | |
109 | bc24a225 | Paul Brook | static void lm_kbd_pwm_tick(LM823KbdState *s, int line) |
110 | 1d4e547b | balrog | { |
111 | 1d4e547b | balrog | } |
112 | 1d4e547b | balrog | |
113 | bc24a225 | Paul Brook | static void lm_kbd_pwm_start(LM823KbdState *s, int line) |
114 | 1d4e547b | balrog | { |
115 | 1d4e547b | balrog | lm_kbd_pwm_tick(s, line); |
116 | 1d4e547b | balrog | } |
117 | 1d4e547b | balrog | |
118 | 1d4e547b | balrog | static void lm_kbd_pwm0_tick(void *opaque) |
119 | 1d4e547b | balrog | { |
120 | 1d4e547b | balrog | lm_kbd_pwm_tick(opaque, 0);
|
121 | 1d4e547b | balrog | } |
122 | 1d4e547b | balrog | static void lm_kbd_pwm1_tick(void *opaque) |
123 | 1d4e547b | balrog | { |
124 | 1d4e547b | balrog | lm_kbd_pwm_tick(opaque, 1);
|
125 | 1d4e547b | balrog | } |
126 | 1d4e547b | balrog | static void lm_kbd_pwm2_tick(void *opaque) |
127 | 1d4e547b | balrog | { |
128 | 1d4e547b | balrog | lm_kbd_pwm_tick(opaque, 2);
|
129 | 1d4e547b | balrog | } |
130 | 1d4e547b | balrog | |
131 | 1d4e547b | balrog | enum {
|
132 | 1d4e547b | balrog | LM832x_CMD_READ_ID = 0x80, /* Read chip ID. */ |
133 | 1d4e547b | balrog | LM832x_CMD_WRITE_CFG = 0x81, /* Set configuration item. */ |
134 | 1d4e547b | balrog | LM832x_CMD_READ_INT = 0x82, /* Get interrupt status. */ |
135 | 1d4e547b | balrog | LM832x_CMD_RESET = 0x83, /* Reset, same as external one */ |
136 | 1d4e547b | balrog | LM823x_CMD_WRITE_PULL_DOWN = 0x84, /* Select GPIO pull-up/down. */ |
137 | 1d4e547b | balrog | LM832x_CMD_WRITE_PORT_SEL = 0x85, /* Select GPIO in/out. */ |
138 | 1d4e547b | balrog | LM832x_CMD_WRITE_PORT_STATE = 0x86, /* Set GPIO pull-up/down. */ |
139 | 1d4e547b | balrog | LM832x_CMD_READ_PORT_SEL = 0x87, /* Get GPIO in/out. */ |
140 | 1d4e547b | balrog | LM832x_CMD_READ_PORT_STATE = 0x88, /* Get GPIO pull-up/down. */ |
141 | 1d4e547b | balrog | LM832x_CMD_READ_FIFO = 0x89, /* Read byte from FIFO. */ |
142 | 1d4e547b | balrog | LM832x_CMD_RPT_READ_FIFO = 0x8a, /* Read FIFO (no increment). */ |
143 | 1d4e547b | balrog | LM832x_CMD_SET_ACTIVE = 0x8b, /* Set active time. */ |
144 | 1d4e547b | balrog | LM832x_CMD_READ_ERROR = 0x8c, /* Get error status. */ |
145 | 1d4e547b | balrog | LM832x_CMD_READ_ROTATOR = 0x8e, /* Read rotator status. */ |
146 | 1d4e547b | balrog | LM832x_CMD_SET_DEBOUNCE = 0x8f, /* Set debouncing time. */ |
147 | 1d4e547b | balrog | LM832x_CMD_SET_KEY_SIZE = 0x90, /* Set keypad size. */ |
148 | 1d4e547b | balrog | LM832x_CMD_READ_KEY_SIZE = 0x91, /* Get keypad size. */ |
149 | 1d4e547b | balrog | LM832x_CMD_READ_CFG = 0x92, /* Get configuration item. */ |
150 | 1d4e547b | balrog | LM832x_CMD_WRITE_CLOCK = 0x93, /* Set clock config. */ |
151 | 1d4e547b | balrog | LM832x_CMD_READ_CLOCK = 0x94, /* Get clock config. */ |
152 | 1d4e547b | balrog | LM832x_CMD_PWM_WRITE = 0x95, /* Write PWM script. */ |
153 | 1d4e547b | balrog | LM832x_CMD_PWM_START = 0x96, /* Start PWM engine. */ |
154 | 1d4e547b | balrog | LM832x_CMD_PWM_STOP = 0x97, /* Stop PWM engine. */ |
155 | 1d4e547b | balrog | }; |
156 | 1d4e547b | balrog | |
157 | 1d4e547b | balrog | #define LM832x_MAX_KPX 8 |
158 | 1d4e547b | balrog | #define LM832x_MAX_KPY 12 |
159 | 1d4e547b | balrog | |
160 | bc24a225 | Paul Brook | static uint8_t lm_kbd_read(LM823KbdState *s, int reg, int byte) |
161 | 1d4e547b | balrog | { |
162 | 1d4e547b | balrog | int ret;
|
163 | 1d4e547b | balrog | |
164 | 1d4e547b | balrog | switch (reg) {
|
165 | 1d4e547b | balrog | case LM832x_CMD_READ_ID:
|
166 | 1d4e547b | balrog | ret = 0x0400;
|
167 | 1d4e547b | balrog | break;
|
168 | 1d4e547b | balrog | |
169 | 1d4e547b | balrog | case LM832x_CMD_READ_INT:
|
170 | 1d4e547b | balrog | ret = s->status; |
171 | 1d4e547b | balrog | if (!(s->status & INT_NOINIT)) {
|
172 | 1d4e547b | balrog | s->status = 0;
|
173 | 1d4e547b | balrog | lm_kbd_irq_update(s); |
174 | 1d4e547b | balrog | } |
175 | 1d4e547b | balrog | break;
|
176 | 1d4e547b | balrog | |
177 | 1d4e547b | balrog | case LM832x_CMD_READ_PORT_SEL:
|
178 | 1d4e547b | balrog | ret = s->gpio.dir; |
179 | 1d4e547b | balrog | break;
|
180 | 1d4e547b | balrog | case LM832x_CMD_READ_PORT_STATE:
|
181 | 1d4e547b | balrog | ret = s->gpio.mask; |
182 | 1d4e547b | balrog | break;
|
183 | 1d4e547b | balrog | |
184 | 1d4e547b | balrog | case LM832x_CMD_READ_FIFO:
|
185 | 1d4e547b | balrog | if (s->kbd.len <= 1) |
186 | 1d4e547b | balrog | return 0x00; |
187 | 1d4e547b | balrog | |
188 | 1d4e547b | balrog | /* Example response from the two commands after a INT_KEYPAD
|
189 | 1d4e547b | balrog | * interrupt caused by the key 0x3c being pressed:
|
190 | 1d4e547b | balrog | * RPT_READ_FIFO: 55 bc 00 4e ff 0a 50 08 00 29 d9 08 01 c9 01
|
191 | 1d4e547b | balrog | * READ_FIFO: bc 00 00 4e ff 0a 50 08 00 29 d9 08 01 c9 01
|
192 | 1d4e547b | balrog | * RPT_READ_FIFO: bc 00 00 4e ff 0a 50 08 00 29 d9 08 01 c9 01
|
193 | 1d4e547b | balrog | *
|
194 | 1d4e547b | balrog | * 55 is the code of the key release event serviced in the previous
|
195 | 1d4e547b | balrog | * interrupt handling.
|
196 | 1d4e547b | balrog | *
|
197 | 1d4e547b | balrog | * TODO: find out whether the FIFO is advanced a single character
|
198 | 1d4e547b | balrog | * before reading every byte or the whole size of the FIFO at the
|
199 | 1d4e547b | balrog | * last LM832x_CMD_READ_FIFO. This affects LM832x_CMD_RPT_READ_FIFO
|
200 | 1d4e547b | balrog | * output in cases where there are more than one event in the FIFO.
|
201 | 1d4e547b | balrog | * Assume 0xbc and 0x3c events are in the FIFO:
|
202 | 1d4e547b | balrog | * RPT_READ_FIFO: 55 bc 3c 00 4e ff 0a 50 08 00 29 d9 08 01 c9
|
203 | 1d4e547b | balrog | * READ_FIFO: bc 3c 00 00 4e ff 0a 50 08 00 29 d9 08 01 c9
|
204 | 1d4e547b | balrog | * Does RPT_READ_FIFO now return 0xbc and 0x3c or only 0x3c?
|
205 | 1d4e547b | balrog | */
|
206 | 1d4e547b | balrog | s->kbd.start ++; |
207 | 1d4e547b | balrog | s->kbd.start &= sizeof(s->kbd.fifo) - 1; |
208 | 1d4e547b | balrog | s->kbd.len --; |
209 | 1d4e547b | balrog | |
210 | 1d4e547b | balrog | return s->kbd.fifo[s->kbd.start];
|
211 | 1d4e547b | balrog | case LM832x_CMD_RPT_READ_FIFO:
|
212 | 1d4e547b | balrog | if (byte >= s->kbd.len)
|
213 | 1d4e547b | balrog | return 0x00; |
214 | 1d4e547b | balrog | |
215 | 1d4e547b | balrog | return s->kbd.fifo[(s->kbd.start + byte) & (sizeof(s->kbd.fifo) - 1)]; |
216 | 1d4e547b | balrog | |
217 | 1d4e547b | balrog | case LM832x_CMD_READ_ERROR:
|
218 | 1d4e547b | balrog | return s->error;
|
219 | 1d4e547b | balrog | |
220 | 1d4e547b | balrog | case LM832x_CMD_READ_ROTATOR:
|
221 | 1d4e547b | balrog | return 0; |
222 | 1d4e547b | balrog | |
223 | 1d4e547b | balrog | case LM832x_CMD_READ_KEY_SIZE:
|
224 | 1d4e547b | balrog | return s->kbd.size;
|
225 | 1d4e547b | balrog | |
226 | 1d4e547b | balrog | case LM832x_CMD_READ_CFG:
|
227 | 1d4e547b | balrog | return s->config & 0xf; |
228 | 1d4e547b | balrog | |
229 | 1d4e547b | balrog | case LM832x_CMD_READ_CLOCK:
|
230 | 1d4e547b | balrog | return (s->clock & 0xfc) | 2; |
231 | 1d4e547b | balrog | |
232 | 1d4e547b | balrog | default:
|
233 | 1d4e547b | balrog | lm_kbd_error(s, ERR_CMDUNK); |
234 | 1d4e547b | balrog | fprintf(stderr, "%s: unknown command %02x\n", __FUNCTION__, reg);
|
235 | 1d4e547b | balrog | return 0x00; |
236 | 1d4e547b | balrog | } |
237 | 1d4e547b | balrog | |
238 | 1d4e547b | balrog | return ret >> (byte << 3); |
239 | 1d4e547b | balrog | } |
240 | 1d4e547b | balrog | |
241 | bc24a225 | Paul Brook | static void lm_kbd_write(LM823KbdState *s, int reg, int byte, uint8_t value) |
242 | 1d4e547b | balrog | { |
243 | 1d4e547b | balrog | switch (reg) {
|
244 | 1d4e547b | balrog | case LM832x_CMD_WRITE_CFG:
|
245 | 1d4e547b | balrog | s->config = value; |
246 | 1d4e547b | balrog | /* This must be done whenever s->mux.in is updated (never). */
|
247 | 1d4e547b | balrog | if ((s->config >> 1) & 1) /* MUX1EN */ |
248 | 1d4e547b | balrog | qemu_set_irq(s->mux.out[0], s->mux.in[0][(s->config >> 0) & 1]); |
249 | 1d4e547b | balrog | if ((s->config >> 3) & 1) /* MUX2EN */ |
250 | 1d4e547b | balrog | qemu_set_irq(s->mux.out[0], s->mux.in[0][(s->config >> 2) & 1]); |
251 | 1d4e547b | balrog | /* TODO: check that this is issued only following the chip reset
|
252 | 1d4e547b | balrog | * and not in the middle of operation and that it is followed by
|
253 | 1d4e547b | balrog | * the GPIO ports re-resablishing through WRITE_PORT_SEL and
|
254 | 1d4e547b | balrog | * WRITE_PORT_STATE (using a timer perhaps) and otherwise output
|
255 | 1d4e547b | balrog | * warnings. */
|
256 | 1d4e547b | balrog | s->status = 0;
|
257 | 1d4e547b | balrog | lm_kbd_irq_update(s); |
258 | 1d4e547b | balrog | s->kbd.len = 0;
|
259 | 1d4e547b | balrog | s->kbd.start = 0;
|
260 | 1d4e547b | balrog | s->reg = -1;
|
261 | 1d4e547b | balrog | break;
|
262 | 1d4e547b | balrog | |
263 | 1d4e547b | balrog | case LM832x_CMD_RESET:
|
264 | 1d4e547b | balrog | if (value == 0xaa) |
265 | 1d4e547b | balrog | lm_kbd_reset(s); |
266 | 1d4e547b | balrog | else
|
267 | 1d4e547b | balrog | lm_kbd_error(s, ERR_BADPAR); |
268 | 1d4e547b | balrog | s->reg = -1;
|
269 | 1d4e547b | balrog | break;
|
270 | 1d4e547b | balrog | |
271 | 1d4e547b | balrog | case LM823x_CMD_WRITE_PULL_DOWN:
|
272 | 1d4e547b | balrog | if (!byte)
|
273 | 1d4e547b | balrog | s->gpio.pull = value; |
274 | 1d4e547b | balrog | else {
|
275 | 1d4e547b | balrog | s->gpio.pull |= value << 8;
|
276 | 1d4e547b | balrog | lm_kbd_gpio_update(s); |
277 | 1d4e547b | balrog | s->reg = -1;
|
278 | 1d4e547b | balrog | } |
279 | 1d4e547b | balrog | break;
|
280 | 1d4e547b | balrog | case LM832x_CMD_WRITE_PORT_SEL:
|
281 | 1d4e547b | balrog | if (!byte)
|
282 | 1d4e547b | balrog | s->gpio.dir = value; |
283 | 1d4e547b | balrog | else {
|
284 | 1d4e547b | balrog | s->gpio.dir |= value << 8;
|
285 | 1d4e547b | balrog | lm_kbd_gpio_update(s); |
286 | 1d4e547b | balrog | s->reg = -1;
|
287 | 1d4e547b | balrog | } |
288 | 1d4e547b | balrog | break;
|
289 | 1d4e547b | balrog | case LM832x_CMD_WRITE_PORT_STATE:
|
290 | 1d4e547b | balrog | if (!byte)
|
291 | 1d4e547b | balrog | s->gpio.mask = value; |
292 | 1d4e547b | balrog | else {
|
293 | 1d4e547b | balrog | s->gpio.mask |= value << 8;
|
294 | 1d4e547b | balrog | lm_kbd_gpio_update(s); |
295 | 1d4e547b | balrog | s->reg = -1;
|
296 | 1d4e547b | balrog | } |
297 | 1d4e547b | balrog | break;
|
298 | 1d4e547b | balrog | |
299 | 1d4e547b | balrog | case LM832x_CMD_SET_ACTIVE:
|
300 | 1d4e547b | balrog | s->acttime = value; |
301 | 1d4e547b | balrog | s->reg = -1;
|
302 | 1d4e547b | balrog | break;
|
303 | 1d4e547b | balrog | |
304 | 1d4e547b | balrog | case LM832x_CMD_SET_DEBOUNCE:
|
305 | 1d4e547b | balrog | s->kbd.dbnctime = value; |
306 | 1d4e547b | balrog | s->reg = -1;
|
307 | 1d4e547b | balrog | if (!value)
|
308 | 1d4e547b | balrog | lm_kbd_error(s, ERR_BADPAR); |
309 | 1d4e547b | balrog | break;
|
310 | 1d4e547b | balrog | |
311 | 1d4e547b | balrog | case LM832x_CMD_SET_KEY_SIZE:
|
312 | 1d4e547b | balrog | s->kbd.size = value; |
313 | 1d4e547b | balrog | s->reg = -1;
|
314 | 1d4e547b | balrog | if (
|
315 | 1d4e547b | balrog | (value & 0xf) < 3 || (value & 0xf) > LM832x_MAX_KPY || |
316 | 1d4e547b | balrog | (value >> 4) < 3 || (value >> 4) > LM832x_MAX_KPX) |
317 | 1d4e547b | balrog | lm_kbd_error(s, ERR_BADPAR); |
318 | 1d4e547b | balrog | break;
|
319 | 1d4e547b | balrog | |
320 | 1d4e547b | balrog | case LM832x_CMD_WRITE_CLOCK:
|
321 | 1d4e547b | balrog | s->clock = value; |
322 | 1d4e547b | balrog | s->reg = -1;
|
323 | 1d4e547b | balrog | if ((value & 3) && (value & 3) != 3) { |
324 | 1d4e547b | balrog | lm_kbd_error(s, ERR_BADPAR); |
325 | 1d4e547b | balrog | fprintf(stderr, "%s: invalid clock setting in RCPWM\n",
|
326 | 1d4e547b | balrog | __FUNCTION__); |
327 | 1d4e547b | balrog | } |
328 | 1d4e547b | balrog | /* TODO: Validate that the command is only issued once */
|
329 | 1d4e547b | balrog | break;
|
330 | 1d4e547b | balrog | |
331 | 1d4e547b | balrog | case LM832x_CMD_PWM_WRITE:
|
332 | 1d4e547b | balrog | if (byte == 0) { |
333 | 1d4e547b | balrog | if (!(value & 3) || (value >> 2) > 59) { |
334 | 1d4e547b | balrog | lm_kbd_error(s, ERR_BADPAR); |
335 | 1d4e547b | balrog | s->reg = -1;
|
336 | 1d4e547b | balrog | break;
|
337 | 1d4e547b | balrog | } |
338 | 1d4e547b | balrog | |
339 | 1d4e547b | balrog | s->pwm.faddr = value; |
340 | 1d4e547b | balrog | s->pwm.file[s->pwm.faddr] = 0;
|
341 | 1d4e547b | balrog | } else if (byte == 1) { |
342 | 1d4e547b | balrog | s->pwm.file[s->pwm.faddr] |= value << 8;
|
343 | 1d4e547b | balrog | } else if (byte == 2) { |
344 | 1d4e547b | balrog | s->pwm.file[s->pwm.faddr] |= value << 0;
|
345 | 1d4e547b | balrog | s->reg = -1;
|
346 | 1d4e547b | balrog | } |
347 | 1d4e547b | balrog | break;
|
348 | 1d4e547b | balrog | case LM832x_CMD_PWM_START:
|
349 | 1d4e547b | balrog | s->reg = -1;
|
350 | 1d4e547b | balrog | if (!(value & 3) || (value >> 2) > 59) { |
351 | 1d4e547b | balrog | lm_kbd_error(s, ERR_BADPAR); |
352 | 1d4e547b | balrog | break;
|
353 | 1d4e547b | balrog | } |
354 | 1d4e547b | balrog | |
355 | 1d4e547b | balrog | s->pwm.addr[(value & 3) - 1] = value >> 2; |
356 | 1d4e547b | balrog | lm_kbd_pwm_start(s, (value & 3) - 1); |
357 | 1d4e547b | balrog | break;
|
358 | 1d4e547b | balrog | case LM832x_CMD_PWM_STOP:
|
359 | 1d4e547b | balrog | s->reg = -1;
|
360 | 1d4e547b | balrog | if (!(value & 3)) { |
361 | 1d4e547b | balrog | lm_kbd_error(s, ERR_BADPAR); |
362 | 1d4e547b | balrog | break;
|
363 | 1d4e547b | balrog | } |
364 | 1d4e547b | balrog | |
365 | 1d4e547b | balrog | qemu_del_timer(s->pwm.tm[(value & 3) - 1]); |
366 | 1d4e547b | balrog | break;
|
367 | 1d4e547b | balrog | |
368 | 1d4e547b | balrog | case -1: |
369 | 1d4e547b | balrog | lm_kbd_error(s, ERR_BADPAR); |
370 | 1d4e547b | balrog | break;
|
371 | 1d4e547b | balrog | default:
|
372 | 1d4e547b | balrog | lm_kbd_error(s, ERR_CMDUNK); |
373 | 1d4e547b | balrog | fprintf(stderr, "%s: unknown command %02x\n", __FUNCTION__, reg);
|
374 | 1d4e547b | balrog | break;
|
375 | 1d4e547b | balrog | } |
376 | 1d4e547b | balrog | } |
377 | 1d4e547b | balrog | |
378 | 1d4e547b | balrog | static void lm_i2c_event(i2c_slave *i2c, enum i2c_event event) |
379 | 1d4e547b | balrog | { |
380 | 2d9401aa | Paul Brook | LM823KbdState *s = FROM_I2C_SLAVE(LM823KbdState, i2c); |
381 | 1d4e547b | balrog | |
382 | 1d4e547b | balrog | switch (event) {
|
383 | 1d4e547b | balrog | case I2C_START_RECV:
|
384 | 1d4e547b | balrog | case I2C_START_SEND:
|
385 | 1d4e547b | balrog | s->i2c_cycle = 0;
|
386 | 1d4e547b | balrog | s->i2c_dir = (event == I2C_START_SEND); |
387 | 1d4e547b | balrog | break;
|
388 | 1d4e547b | balrog | |
389 | 1d4e547b | balrog | default:
|
390 | 1d4e547b | balrog | break;
|
391 | 1d4e547b | balrog | } |
392 | 1d4e547b | balrog | } |
393 | 1d4e547b | balrog | |
394 | 1d4e547b | balrog | static int lm_i2c_rx(i2c_slave *i2c) |
395 | 1d4e547b | balrog | { |
396 | 2d9401aa | Paul Brook | LM823KbdState *s = FROM_I2C_SLAVE(LM823KbdState, i2c); |
397 | 1d4e547b | balrog | |
398 | 1d4e547b | balrog | return lm_kbd_read(s, s->reg, s->i2c_cycle ++);
|
399 | 1d4e547b | balrog | } |
400 | 1d4e547b | balrog | |
401 | 1d4e547b | balrog | static int lm_i2c_tx(i2c_slave *i2c, uint8_t data) |
402 | 1d4e547b | balrog | { |
403 | bc24a225 | Paul Brook | LM823KbdState *s = (LM823KbdState *) i2c; |
404 | 1d4e547b | balrog | |
405 | 1d4e547b | balrog | if (!s->i2c_cycle)
|
406 | 1d4e547b | balrog | s->reg = data; |
407 | 1d4e547b | balrog | else
|
408 | 1d4e547b | balrog | lm_kbd_write(s, s->reg, s->i2c_cycle - 1, data);
|
409 | 1d4e547b | balrog | s->i2c_cycle ++; |
410 | 1d4e547b | balrog | |
411 | 1d4e547b | balrog | return 0; |
412 | 1d4e547b | balrog | } |
413 | 1d4e547b | balrog | |
414 | 1d4e547b | balrog | static void lm_kbd_save(QEMUFile *f, void *opaque) |
415 | 1d4e547b | balrog | { |
416 | bc24a225 | Paul Brook | LM823KbdState *s = (LM823KbdState *) opaque; |
417 | 1d4e547b | balrog | int i;
|
418 | 1d4e547b | balrog | |
419 | 1d4e547b | balrog | i2c_slave_save(f, &s->i2c); |
420 | 1d4e547b | balrog | qemu_put_byte(f, s->i2c_dir); |
421 | 1d4e547b | balrog | qemu_put_byte(f, s->i2c_cycle); |
422 | 1d4e547b | balrog | qemu_put_byte(f, (uint8_t) s->reg); |
423 | 1d4e547b | balrog | |
424 | 1d4e547b | balrog | qemu_put_8s(f, &s->config); |
425 | 1d4e547b | balrog | qemu_put_8s(f, &s->status); |
426 | 1d4e547b | balrog | qemu_put_8s(f, &s->acttime); |
427 | 1d4e547b | balrog | qemu_put_8s(f, &s->error); |
428 | 1d4e547b | balrog | qemu_put_8s(f, &s->clock); |
429 | 1d4e547b | balrog | |
430 | 1d4e547b | balrog | qemu_put_be16s(f, &s->gpio.pull); |
431 | 1d4e547b | balrog | qemu_put_be16s(f, &s->gpio.mask); |
432 | 1d4e547b | balrog | qemu_put_be16s(f, &s->gpio.dir); |
433 | 1d4e547b | balrog | qemu_put_be16s(f, &s->gpio.level); |
434 | 1d4e547b | balrog | |
435 | 1d4e547b | balrog | qemu_put_byte(f, s->kbd.dbnctime); |
436 | 1d4e547b | balrog | qemu_put_byte(f, s->kbd.size); |
437 | 1d4e547b | balrog | qemu_put_byte(f, s->kbd.start); |
438 | 1d4e547b | balrog | qemu_put_byte(f, s->kbd.len); |
439 | 1d4e547b | balrog | qemu_put_buffer(f, s->kbd.fifo, sizeof(s->kbd.fifo));
|
440 | 1d4e547b | balrog | |
441 | 1d4e547b | balrog | for (i = 0; i < sizeof(s->pwm.file); i ++) |
442 | 1d4e547b | balrog | qemu_put_be16s(f, &s->pwm.file[i]); |
443 | 1d4e547b | balrog | qemu_put_8s(f, &s->pwm.faddr); |
444 | 1d4e547b | balrog | qemu_put_buffer(f, s->pwm.addr, sizeof(s->pwm.addr));
|
445 | 1d4e547b | balrog | qemu_put_timer(f, s->pwm.tm[0]);
|
446 | 1d4e547b | balrog | qemu_put_timer(f, s->pwm.tm[1]);
|
447 | 1d4e547b | balrog | qemu_put_timer(f, s->pwm.tm[2]);
|
448 | 1d4e547b | balrog | } |
449 | 1d4e547b | balrog | |
450 | 1d4e547b | balrog | static int lm_kbd_load(QEMUFile *f, void *opaque, int version_id) |
451 | 1d4e547b | balrog | { |
452 | bc24a225 | Paul Brook | LM823KbdState *s = (LM823KbdState *) opaque; |
453 | 1d4e547b | balrog | int i;
|
454 | 1d4e547b | balrog | |
455 | 1d4e547b | balrog | i2c_slave_load(f, &s->i2c); |
456 | 1d4e547b | balrog | s->i2c_dir = qemu_get_byte(f); |
457 | 1d4e547b | balrog | s->i2c_cycle = qemu_get_byte(f); |
458 | 1d4e547b | balrog | s->reg = (int8_t) qemu_get_byte(f); |
459 | 1d4e547b | balrog | |
460 | 1d4e547b | balrog | qemu_get_8s(f, &s->config); |
461 | 1d4e547b | balrog | qemu_get_8s(f, &s->status); |
462 | 1d4e547b | balrog | qemu_get_8s(f, &s->acttime); |
463 | 1d4e547b | balrog | qemu_get_8s(f, &s->error); |
464 | 1d4e547b | balrog | qemu_get_8s(f, &s->clock); |
465 | 1d4e547b | balrog | |
466 | 1d4e547b | balrog | qemu_get_be16s(f, &s->gpio.pull); |
467 | 1d4e547b | balrog | qemu_get_be16s(f, &s->gpio.mask); |
468 | 1d4e547b | balrog | qemu_get_be16s(f, &s->gpio.dir); |
469 | 1d4e547b | balrog | qemu_get_be16s(f, &s->gpio.level); |
470 | 1d4e547b | balrog | |
471 | 1d4e547b | balrog | s->kbd.dbnctime = qemu_get_byte(f); |
472 | 1d4e547b | balrog | s->kbd.size = qemu_get_byte(f); |
473 | 1d4e547b | balrog | s->kbd.start = qemu_get_byte(f); |
474 | 1d4e547b | balrog | s->kbd.len = qemu_get_byte(f); |
475 | 1d4e547b | balrog | qemu_get_buffer(f, s->kbd.fifo, sizeof(s->kbd.fifo));
|
476 | 1d4e547b | balrog | |
477 | 1d4e547b | balrog | for (i = 0; i < sizeof(s->pwm.file); i ++) |
478 | 1d4e547b | balrog | qemu_get_be16s(f, &s->pwm.file[i]); |
479 | 1d4e547b | balrog | qemu_get_8s(f, &s->pwm.faddr); |
480 | 1d4e547b | balrog | qemu_get_buffer(f, s->pwm.addr, sizeof(s->pwm.addr));
|
481 | 1d4e547b | balrog | qemu_get_timer(f, s->pwm.tm[0]);
|
482 | 1d4e547b | balrog | qemu_get_timer(f, s->pwm.tm[1]);
|
483 | 1d4e547b | balrog | qemu_get_timer(f, s->pwm.tm[2]);
|
484 | 1d4e547b | balrog | |
485 | 1d4e547b | balrog | lm_kbd_irq_update(s); |
486 | 1d4e547b | balrog | lm_kbd_gpio_update(s); |
487 | 1d4e547b | balrog | |
488 | 1d4e547b | balrog | return 0; |
489 | 1d4e547b | balrog | } |
490 | 1d4e547b | balrog | |
491 | 2d9401aa | Paul Brook | static void lm8323_init(i2c_slave *i2c) |
492 | 1d4e547b | balrog | { |
493 | 2d9401aa | Paul Brook | LM823KbdState *s = FROM_I2C_SLAVE(LM823KbdState, i2c); |
494 | 1d4e547b | balrog | |
495 | 1d4e547b | balrog | s->model = 0x8323;
|
496 | 1d4e547b | balrog | s->pwm.tm[0] = qemu_new_timer(vm_clock, lm_kbd_pwm0_tick, s);
|
497 | 1d4e547b | balrog | s->pwm.tm[1] = qemu_new_timer(vm_clock, lm_kbd_pwm1_tick, s);
|
498 | 1d4e547b | balrog | s->pwm.tm[2] = qemu_new_timer(vm_clock, lm_kbd_pwm2_tick, s);
|
499 | 2d9401aa | Paul Brook | qdev_init_gpio_out(&i2c->qdev, &s->nirq, 1);
|
500 | 1d4e547b | balrog | |
501 | 1d4e547b | balrog | lm_kbd_reset(s); |
502 | 1d4e547b | balrog | |
503 | a08d4367 | Jan Kiszka | qemu_register_reset((void *) lm_kbd_reset, s);
|
504 | 18be5187 | pbrook | register_savevm("LM8323", -1, 0, lm_kbd_save, lm_kbd_load, s); |
505 | 1d4e547b | balrog | } |
506 | 1d4e547b | balrog | |
507 | 1d4e547b | balrog | void lm832x_key_event(struct i2c_slave *i2c, int key, int state) |
508 | 1d4e547b | balrog | { |
509 | bc24a225 | Paul Brook | LM823KbdState *s = (LM823KbdState *) i2c; |
510 | 1d4e547b | balrog | |
511 | 1d4e547b | balrog | if ((s->status & INT_ERROR) && (s->error & ERR_FIFOOVR))
|
512 | 1d4e547b | balrog | return;
|
513 | 1d4e547b | balrog | |
514 | 7442511c | blueswir1 | if (s->kbd.len >= sizeof(s->kbd.fifo)) { |
515 | 7442511c | blueswir1 | lm_kbd_error(s, ERR_FIFOOVR); |
516 | 7442511c | blueswir1 | return;
|
517 | 7442511c | blueswir1 | } |
518 | 1d4e547b | balrog | |
519 | 1d4e547b | balrog | s->kbd.fifo[(s->kbd.start + s->kbd.len ++) & (sizeof(s->kbd.fifo) - 1)] = |
520 | 1d4e547b | balrog | key | (state << 7);
|
521 | 1d4e547b | balrog | |
522 | 1d4e547b | balrog | /* We never set ERR_KEYOVR because we support multiple keys fine. */
|
523 | 1d4e547b | balrog | s->status |= INT_KEYPAD; |
524 | 1d4e547b | balrog | lm_kbd_irq_update(s); |
525 | 1d4e547b | balrog | } |
526 | 2d9401aa | Paul Brook | |
527 | 2d9401aa | Paul Brook | static I2CSlaveInfo lm8323_info = {
|
528 | 074f2fff | Gerd Hoffmann | .qdev.name = "lm8323",
|
529 | 074f2fff | Gerd Hoffmann | .qdev.size = sizeof(LM823KbdState),
|
530 | 2d9401aa | Paul Brook | .init = lm8323_init, |
531 | 2d9401aa | Paul Brook | .event = lm_i2c_event, |
532 | 2d9401aa | Paul Brook | .recv = lm_i2c_rx, |
533 | 2d9401aa | Paul Brook | .send = lm_i2c_tx |
534 | 2d9401aa | Paul Brook | }; |
535 | 2d9401aa | Paul Brook | |
536 | 2d9401aa | Paul Brook | static void lm832x_register_devices(void) |
537 | 2d9401aa | Paul Brook | { |
538 | 074f2fff | Gerd Hoffmann | i2c_register_slave(&lm8323_info); |
539 | 2d9401aa | Paul Brook | } |
540 | 2d9401aa | Paul Brook | |
541 | 2d9401aa | Paul Brook | device_init(lm832x_register_devices) |