2 * National Semiconductor LM8322/8323 GPIO keyboard & PWM chips.
4 * Copyright (C) 2008 Nokia Corporation
5 * Written by Andrzej Zaborowski <andrew@openedhand.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 or
10 * (at your option) version 3 of the License.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, see <http://www.gnu.org/licenses/>.
23 #include "qemu-timer.h"
70 #define INT_KEYPAD (1 << 0)
71 #define INT_ERROR (1 << 3)
72 #define INT_NOINIT (1 << 4)
73 #define INT_PWMEND(n) (1 << (5 + n))
75 #define ERR_BADPAR (1 << 0)
76 #define ERR_CMDUNK (1 << 1)
77 #define ERR_KEYOVR (1 << 2)
78 #define ERR_FIFOOVR (1 << 6)
80 static void lm_kbd_irq_update(LM823KbdState
*s
)
82 qemu_set_irq(s
->nirq
, !s
->status
);
85 static void lm_kbd_gpio_update(LM823KbdState
*s
)
89 static void lm_kbd_reset(LM823KbdState
*s
)
92 s
->status
= INT_NOINIT
;
99 lm_kbd_gpio_update(s
);
102 static void lm_kbd_error(LM823KbdState
*s
, int err
)
105 s
->status
|= INT_ERROR
;
106 lm_kbd_irq_update(s
);
109 static void lm_kbd_pwm_tick(LM823KbdState
*s
, int line
)
113 static void lm_kbd_pwm_start(LM823KbdState
*s
, int line
)
115 lm_kbd_pwm_tick(s
, line
);
118 static void lm_kbd_pwm0_tick(void *opaque
)
120 lm_kbd_pwm_tick(opaque
, 0);
122 static void lm_kbd_pwm1_tick(void *opaque
)
124 lm_kbd_pwm_tick(opaque
, 1);
126 static void lm_kbd_pwm2_tick(void *opaque
)
128 lm_kbd_pwm_tick(opaque
, 2);
132 LM832x_CMD_READ_ID
= 0x80, /* Read chip ID. */
133 LM832x_CMD_WRITE_CFG
= 0x81, /* Set configuration item. */
134 LM832x_CMD_READ_INT
= 0x82, /* Get interrupt status. */
135 LM832x_CMD_RESET
= 0x83, /* Reset, same as external one */
136 LM823x_CMD_WRITE_PULL_DOWN
= 0x84, /* Select GPIO pull-up/down. */
137 LM832x_CMD_WRITE_PORT_SEL
= 0x85, /* Select GPIO in/out. */
138 LM832x_CMD_WRITE_PORT_STATE
= 0x86, /* Set GPIO pull-up/down. */
139 LM832x_CMD_READ_PORT_SEL
= 0x87, /* Get GPIO in/out. */
140 LM832x_CMD_READ_PORT_STATE
= 0x88, /* Get GPIO pull-up/down. */
141 LM832x_CMD_READ_FIFO
= 0x89, /* Read byte from FIFO. */
142 LM832x_CMD_RPT_READ_FIFO
= 0x8a, /* Read FIFO (no increment). */
143 LM832x_CMD_SET_ACTIVE
= 0x8b, /* Set active time. */
144 LM832x_CMD_READ_ERROR
= 0x8c, /* Get error status. */
145 LM832x_CMD_READ_ROTATOR
= 0x8e, /* Read rotator status. */
146 LM832x_CMD_SET_DEBOUNCE
= 0x8f, /* Set debouncing time. */
147 LM832x_CMD_SET_KEY_SIZE
= 0x90, /* Set keypad size. */
148 LM832x_CMD_READ_KEY_SIZE
= 0x91, /* Get keypad size. */
149 LM832x_CMD_READ_CFG
= 0x92, /* Get configuration item. */
150 LM832x_CMD_WRITE_CLOCK
= 0x93, /* Set clock config. */
151 LM832x_CMD_READ_CLOCK
= 0x94, /* Get clock config. */
152 LM832x_CMD_PWM_WRITE
= 0x95, /* Write PWM script. */
153 LM832x_CMD_PWM_START
= 0x96, /* Start PWM engine. */
154 LM832x_CMD_PWM_STOP
= 0x97, /* Stop PWM engine. */
155 LM832x_GENERAL_ERROR
= 0xff, /* There was one error.
156 Previously was represented by -1
157 This is not a command */
160 #define LM832x_MAX_KPX 8
161 #define LM832x_MAX_KPY 12
163 static uint8_t lm_kbd_read(LM823KbdState
*s
, int reg
, int byte
)
168 case LM832x_CMD_READ_ID
:
172 case LM832x_CMD_READ_INT
:
174 if (!(s
->status
& INT_NOINIT
)) {
176 lm_kbd_irq_update(s
);
180 case LM832x_CMD_READ_PORT_SEL
:
183 case LM832x_CMD_READ_PORT_STATE
:
187 case LM832x_CMD_READ_FIFO
:
191 /* Example response from the two commands after a INT_KEYPAD
192 * interrupt caused by the key 0x3c being pressed:
193 * RPT_READ_FIFO: 55 bc 00 4e ff 0a 50 08 00 29 d9 08 01 c9 01
194 * READ_FIFO: bc 00 00 4e ff 0a 50 08 00 29 d9 08 01 c9 01
195 * RPT_READ_FIFO: bc 00 00 4e ff 0a 50 08 00 29 d9 08 01 c9 01
197 * 55 is the code of the key release event serviced in the previous
198 * interrupt handling.
200 * TODO: find out whether the FIFO is advanced a single character
201 * before reading every byte or the whole size of the FIFO at the
202 * last LM832x_CMD_READ_FIFO. This affects LM832x_CMD_RPT_READ_FIFO
203 * output in cases where there are more than one event in the FIFO.
204 * Assume 0xbc and 0x3c events are in the FIFO:
205 * RPT_READ_FIFO: 55 bc 3c 00 4e ff 0a 50 08 00 29 d9 08 01 c9
206 * READ_FIFO: bc 3c 00 00 4e ff 0a 50 08 00 29 d9 08 01 c9
207 * Does RPT_READ_FIFO now return 0xbc and 0x3c or only 0x3c?
210 s
->kbd
.start
&= sizeof(s
->kbd
.fifo
) - 1;
213 return s
->kbd
.fifo
[s
->kbd
.start
];
214 case LM832x_CMD_RPT_READ_FIFO
:
215 if (byte
>= s
->kbd
.len
)
218 return s
->kbd
.fifo
[(s
->kbd
.start
+ byte
) & (sizeof(s
->kbd
.fifo
) - 1)];
220 case LM832x_CMD_READ_ERROR
:
223 case LM832x_CMD_READ_ROTATOR
:
226 case LM832x_CMD_READ_KEY_SIZE
:
229 case LM832x_CMD_READ_CFG
:
230 return s
->config
& 0xf;
232 case LM832x_CMD_READ_CLOCK
:
233 return (s
->clock
& 0xfc) | 2;
236 lm_kbd_error(s
, ERR_CMDUNK
);
237 fprintf(stderr
, "%s: unknown command %02x\n", __FUNCTION__
, reg
);
241 return ret
>> (byte
<< 3);
244 static void lm_kbd_write(LM823KbdState
*s
, int reg
, int byte
, uint8_t value
)
247 case LM832x_CMD_WRITE_CFG
:
249 /* This must be done whenever s->mux.in is updated (never). */
250 if ((s
->config
>> 1) & 1) /* MUX1EN */
251 qemu_set_irq(s
->mux
.out
[0], s
->mux
.in
[0][(s
->config
>> 0) & 1]);
252 if ((s
->config
>> 3) & 1) /* MUX2EN */
253 qemu_set_irq(s
->mux
.out
[0], s
->mux
.in
[0][(s
->config
>> 2) & 1]);
254 /* TODO: check that this is issued only following the chip reset
255 * and not in the middle of operation and that it is followed by
256 * the GPIO ports re-resablishing through WRITE_PORT_SEL and
257 * WRITE_PORT_STATE (using a timer perhaps) and otherwise output
260 lm_kbd_irq_update(s
);
263 s
->reg
= LM832x_GENERAL_ERROR
;
266 case LM832x_CMD_RESET
:
270 lm_kbd_error(s
, ERR_BADPAR
);
271 s
->reg
= LM832x_GENERAL_ERROR
;
274 case LM823x_CMD_WRITE_PULL_DOWN
:
276 s
->gpio
.pull
= value
;
278 s
->gpio
.pull
|= value
<< 8;
279 lm_kbd_gpio_update(s
);
280 s
->reg
= LM832x_GENERAL_ERROR
;
283 case LM832x_CMD_WRITE_PORT_SEL
:
287 s
->gpio
.dir
|= value
<< 8;
288 lm_kbd_gpio_update(s
);
289 s
->reg
= LM832x_GENERAL_ERROR
;
292 case LM832x_CMD_WRITE_PORT_STATE
:
294 s
->gpio
.mask
= value
;
296 s
->gpio
.mask
|= value
<< 8;
297 lm_kbd_gpio_update(s
);
298 s
->reg
= LM832x_GENERAL_ERROR
;
302 case LM832x_CMD_SET_ACTIVE
:
304 s
->reg
= LM832x_GENERAL_ERROR
;
307 case LM832x_CMD_SET_DEBOUNCE
:
308 s
->kbd
.dbnctime
= value
;
309 s
->reg
= LM832x_GENERAL_ERROR
;
311 lm_kbd_error(s
, ERR_BADPAR
);
314 case LM832x_CMD_SET_KEY_SIZE
:
316 s
->reg
= LM832x_GENERAL_ERROR
;
318 (value
& 0xf) < 3 || (value
& 0xf) > LM832x_MAX_KPY
||
319 (value
>> 4) < 3 || (value
>> 4) > LM832x_MAX_KPX
)
320 lm_kbd_error(s
, ERR_BADPAR
);
323 case LM832x_CMD_WRITE_CLOCK
:
325 s
->reg
= LM832x_GENERAL_ERROR
;
326 if ((value
& 3) && (value
& 3) != 3) {
327 lm_kbd_error(s
, ERR_BADPAR
);
328 fprintf(stderr
, "%s: invalid clock setting in RCPWM\n",
331 /* TODO: Validate that the command is only issued once */
334 case LM832x_CMD_PWM_WRITE
:
336 if (!(value
& 3) || (value
>> 2) > 59) {
337 lm_kbd_error(s
, ERR_BADPAR
);
338 s
->reg
= LM832x_GENERAL_ERROR
;
342 s
->pwm
.faddr
= value
;
343 s
->pwm
.file
[s
->pwm
.faddr
] = 0;
344 } else if (byte
== 1) {
345 s
->pwm
.file
[s
->pwm
.faddr
] |= value
<< 8;
346 } else if (byte
== 2) {
347 s
->pwm
.file
[s
->pwm
.faddr
] |= value
<< 0;
348 s
->reg
= LM832x_GENERAL_ERROR
;
351 case LM832x_CMD_PWM_START
:
352 s
->reg
= LM832x_GENERAL_ERROR
;
353 if (!(value
& 3) || (value
>> 2) > 59) {
354 lm_kbd_error(s
, ERR_BADPAR
);
358 s
->pwm
.addr
[(value
& 3) - 1] = value
>> 2;
359 lm_kbd_pwm_start(s
, (value
& 3) - 1);
361 case LM832x_CMD_PWM_STOP
:
362 s
->reg
= LM832x_GENERAL_ERROR
;
364 lm_kbd_error(s
, ERR_BADPAR
);
368 qemu_del_timer(s
->pwm
.tm
[(value
& 3) - 1]);
371 case LM832x_GENERAL_ERROR
:
372 lm_kbd_error(s
, ERR_BADPAR
);
375 lm_kbd_error(s
, ERR_CMDUNK
);
376 fprintf(stderr
, "%s: unknown command %02x\n", __FUNCTION__
, reg
);
381 static void lm_i2c_event(i2c_slave
*i2c
, enum i2c_event event
)
383 LM823KbdState
*s
= FROM_I2C_SLAVE(LM823KbdState
, i2c
);
389 s
->i2c_dir
= (event
== I2C_START_SEND
);
397 static int lm_i2c_rx(i2c_slave
*i2c
)
399 LM823KbdState
*s
= FROM_I2C_SLAVE(LM823KbdState
, i2c
);
401 return lm_kbd_read(s
, s
->reg
, s
->i2c_cycle
++);
404 static int lm_i2c_tx(i2c_slave
*i2c
, uint8_t data
)
406 LM823KbdState
*s
= (LM823KbdState
*) i2c
;
411 lm_kbd_write(s
, s
->reg
, s
->i2c_cycle
- 1, data
);
417 static int lm_kbd_post_load(void *opaque
, int version_id
)
419 LM823KbdState
*s
= opaque
;
421 lm_kbd_irq_update(s
);
422 lm_kbd_gpio_update(s
);
427 static const VMStateDescription vmstate_lm_kbd
= {
430 .minimum_version_id
= 0,
431 .minimum_version_id_old
= 0,
432 .post_load
= lm_kbd_post_load
,
433 .fields
= (VMStateField
[]) {
434 VMSTATE_I2C_SLAVE(i2c
, LM823KbdState
),
435 VMSTATE_UINT8(i2c_dir
, LM823KbdState
),
436 VMSTATE_UINT8(i2c_cycle
, LM823KbdState
),
437 VMSTATE_UINT8(reg
, LM823KbdState
),
438 VMSTATE_UINT8(config
, LM823KbdState
),
439 VMSTATE_UINT8(status
, LM823KbdState
),
440 VMSTATE_UINT8(acttime
, LM823KbdState
),
441 VMSTATE_UINT8(error
, LM823KbdState
),
442 VMSTATE_UINT8(clock
, LM823KbdState
),
443 VMSTATE_UINT16(gpio
.pull
, LM823KbdState
),
444 VMSTATE_UINT16(gpio
.mask
, LM823KbdState
),
445 VMSTATE_UINT16(gpio
.dir
, LM823KbdState
),
446 VMSTATE_UINT16(gpio
.level
, LM823KbdState
),
447 VMSTATE_UINT8(kbd
.dbnctime
, LM823KbdState
),
448 VMSTATE_UINT8(kbd
.size
, LM823KbdState
),
449 VMSTATE_UINT8(kbd
.start
, LM823KbdState
),
450 VMSTATE_UINT8(kbd
.len
, LM823KbdState
),
451 VMSTATE_BUFFER(kbd
.fifo
, LM823KbdState
),
452 VMSTATE_UINT16_ARRAY(pwm
.file
, LM823KbdState
, 256),
453 VMSTATE_UINT8(pwm
.faddr
, LM823KbdState
),
454 VMSTATE_BUFFER(pwm
.addr
, LM823KbdState
),
455 VMSTATE_TIMER_ARRAY(pwm
.tm
, LM823KbdState
, 3),
456 VMSTATE_END_OF_LIST()
461 static int lm8323_init(i2c_slave
*i2c
)
463 LM823KbdState
*s
= FROM_I2C_SLAVE(LM823KbdState
, i2c
);
466 s
->pwm
.tm
[0] = qemu_new_timer(vm_clock
, lm_kbd_pwm0_tick
, s
);
467 s
->pwm
.tm
[1] = qemu_new_timer(vm_clock
, lm_kbd_pwm1_tick
, s
);
468 s
->pwm
.tm
[2] = qemu_new_timer(vm_clock
, lm_kbd_pwm2_tick
, s
);
469 qdev_init_gpio_out(&i2c
->qdev
, &s
->nirq
, 1);
473 qemu_register_reset((void *) lm_kbd_reset
, s
);
477 void lm832x_key_event(struct i2c_slave
*i2c
, int key
, int state
)
479 LM823KbdState
*s
= (LM823KbdState
*) i2c
;
481 if ((s
->status
& INT_ERROR
) && (s
->error
& ERR_FIFOOVR
))
484 if (s
->kbd
.len
>= sizeof(s
->kbd
.fifo
)) {
485 lm_kbd_error(s
, ERR_FIFOOVR
);
489 s
->kbd
.fifo
[(s
->kbd
.start
+ s
->kbd
.len
++) & (sizeof(s
->kbd
.fifo
) - 1)] =
492 /* We never set ERR_KEYOVR because we support multiple keys fine. */
493 s
->status
|= INT_KEYPAD
;
494 lm_kbd_irq_update(s
);
497 static I2CSlaveInfo lm8323_info
= {
498 .qdev
.name
= "lm8323",
499 .qdev
.size
= sizeof(LM823KbdState
),
500 .qdev
.vmsd
= &vmstate_lm_kbd
,
502 .event
= lm_i2c_event
,
507 static void lm832x_register_devices(void)
509 i2c_register_slave(&lm8323_info
);
512 device_init(lm832x_register_devices
)