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