2008-05-10 02:16:11 +04:00
|
|
|
/*
|
|
|
|
* National Semiconductor LM8322/8323 GPIO keyboard & PWM chips.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 Nokia Corporation
|
|
|
|
* Written by Andrzej Zaborowski <andrew@openedhand.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 or
|
|
|
|
* (at your option) version 3 of the License.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
2009-01-05 01:05:52 +03:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
2009-07-17 00:47:01 +04:00
|
|
|
* with this program; if not, see <http://www.gnu.org/licenses/>.
|
2008-05-10 02:16:11 +04:00
|
|
|
*/
|
|
|
|
|
2016-01-26 21:17:30 +03:00
|
|
|
#include "qemu/osdep.h"
|
2013-02-05 20:06:20 +04:00
|
|
|
#include "hw/i2c/i2c.h"
|
2019-08-12 08:23:42 +03:00
|
|
|
#include "hw/irq.h"
|
2019-08-12 08:23:45 +03:00
|
|
|
#include "migration/vmstate.h"
|
2019-05-23 17:35:07 +03:00
|
|
|
#include "qemu/module.h"
|
2012-12-17 21:20:00 +04:00
|
|
|
#include "qemu/timer.h"
|
2012-11-28 15:06:30 +04:00
|
|
|
#include "ui/console.h"
|
2020-09-03 23:43:22 +03:00
|
|
|
#include "qom/object.h"
|
2008-05-10 02:16:11 +04:00
|
|
|
|
2013-12-20 01:30:16 +04:00
|
|
|
#define TYPE_LM8323 "lm8323"
|
2020-09-16 21:25:19 +03:00
|
|
|
OBJECT_DECLARE_SIMPLE_TYPE(LM823KbdState, LM8323)
|
2013-12-20 01:30:16 +04:00
|
|
|
|
2020-09-03 23:43:22 +03:00
|
|
|
struct LM823KbdState {
|
2013-12-20 01:30:16 +04:00
|
|
|
I2CSlave parent_obj;
|
|
|
|
|
2009-09-30 00:48:42 +04:00
|
|
|
uint8_t i2c_dir;
|
|
|
|
uint8_t i2c_cycle;
|
|
|
|
uint8_t reg;
|
2008-05-10 02:16:11 +04:00
|
|
|
|
|
|
|
qemu_irq nirq;
|
|
|
|
uint16_t model;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
qemu_irq out[2];
|
|
|
|
int in[2][2];
|
|
|
|
} mux;
|
|
|
|
|
|
|
|
uint8_t config;
|
|
|
|
uint8_t status;
|
|
|
|
uint8_t acttime;
|
|
|
|
uint8_t error;
|
|
|
|
uint8_t clock;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
uint16_t pull;
|
|
|
|
uint16_t mask;
|
|
|
|
uint16_t dir;
|
|
|
|
uint16_t level;
|
|
|
|
qemu_irq out[16];
|
|
|
|
} gpio;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
uint8_t dbnctime;
|
|
|
|
uint8_t size;
|
2009-09-30 00:48:42 +04:00
|
|
|
uint8_t start;
|
|
|
|
uint8_t len;
|
2008-05-10 02:16:11 +04:00
|
|
|
uint8_t fifo[16];
|
|
|
|
} kbd;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
uint16_t file[256];
|
2018-12-14 01:37:37 +03:00
|
|
|
uint8_t faddr;
|
2008-05-10 02:16:11 +04:00
|
|
|
uint8_t addr[3];
|
|
|
|
QEMUTimer *tm[3];
|
|
|
|
} pwm;
|
2020-09-03 23:43:22 +03:00
|
|
|
};
|
2008-05-10 02:16:11 +04:00
|
|
|
|
|
|
|
#define INT_KEYPAD (1 << 0)
|
|
|
|
#define INT_ERROR (1 << 3)
|
|
|
|
#define INT_NOINIT (1 << 4)
|
|
|
|
#define INT_PWMEND(n) (1 << (5 + n))
|
|
|
|
|
|
|
|
#define ERR_BADPAR (1 << 0)
|
|
|
|
#define ERR_CMDUNK (1 << 1)
|
|
|
|
#define ERR_KEYOVR (1 << 2)
|
|
|
|
#define ERR_FIFOOVR (1 << 6)
|
|
|
|
|
2009-05-10 04:44:56 +04:00
|
|
|
static void lm_kbd_irq_update(LM823KbdState *s)
|
2008-05-10 02:16:11 +04:00
|
|
|
{
|
|
|
|
qemu_set_irq(s->nirq, !s->status);
|
|
|
|
}
|
|
|
|
|
2009-05-10 04:44:56 +04:00
|
|
|
static void lm_kbd_gpio_update(LM823KbdState *s)
|
2008-05-10 02:16:11 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-10-10 16:15:26 +03:00
|
|
|
static void lm_kbd_reset(DeviceState *dev)
|
2008-05-10 02:16:11 +04:00
|
|
|
{
|
2019-10-10 16:15:26 +03:00
|
|
|
LM823KbdState *s = LM8323(dev);
|
|
|
|
|
2008-05-10 02:16:11 +04:00
|
|
|
s->config = 0x80;
|
|
|
|
s->status = INT_NOINIT;
|
|
|
|
s->acttime = 125;
|
|
|
|
s->kbd.dbnctime = 3;
|
|
|
|
s->kbd.size = 0x33;
|
|
|
|
s->clock = 0x08;
|
|
|
|
|
|
|
|
lm_kbd_irq_update(s);
|
|
|
|
lm_kbd_gpio_update(s);
|
|
|
|
}
|
|
|
|
|
2009-05-10 04:44:56 +04:00
|
|
|
static void lm_kbd_error(LM823KbdState *s, int err)
|
2008-05-10 02:16:11 +04:00
|
|
|
{
|
|
|
|
s->error |= err;
|
|
|
|
s->status |= INT_ERROR;
|
|
|
|
lm_kbd_irq_update(s);
|
|
|
|
}
|
|
|
|
|
2009-05-10 04:44:56 +04:00
|
|
|
static void lm_kbd_pwm_tick(LM823KbdState *s, int line)
|
2008-05-10 02:16:11 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-05-10 04:44:56 +04:00
|
|
|
static void lm_kbd_pwm_start(LM823KbdState *s, int line)
|
2008-05-10 02:16:11 +04:00
|
|
|
{
|
|
|
|
lm_kbd_pwm_tick(s, line);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void lm_kbd_pwm0_tick(void *opaque)
|
|
|
|
{
|
|
|
|
lm_kbd_pwm_tick(opaque, 0);
|
|
|
|
}
|
|
|
|
static void lm_kbd_pwm1_tick(void *opaque)
|
|
|
|
{
|
|
|
|
lm_kbd_pwm_tick(opaque, 1);
|
|
|
|
}
|
|
|
|
static void lm_kbd_pwm2_tick(void *opaque)
|
|
|
|
{
|
|
|
|
lm_kbd_pwm_tick(opaque, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
enum {
|
|
|
|
LM832x_CMD_READ_ID = 0x80, /* Read chip ID. */
|
|
|
|
LM832x_CMD_WRITE_CFG = 0x81, /* Set configuration item. */
|
|
|
|
LM832x_CMD_READ_INT = 0x82, /* Get interrupt status. */
|
|
|
|
LM832x_CMD_RESET = 0x83, /* Reset, same as external one */
|
|
|
|
LM823x_CMD_WRITE_PULL_DOWN = 0x84, /* Select GPIO pull-up/down. */
|
|
|
|
LM832x_CMD_WRITE_PORT_SEL = 0x85, /* Select GPIO in/out. */
|
|
|
|
LM832x_CMD_WRITE_PORT_STATE = 0x86, /* Set GPIO pull-up/down. */
|
|
|
|
LM832x_CMD_READ_PORT_SEL = 0x87, /* Get GPIO in/out. */
|
|
|
|
LM832x_CMD_READ_PORT_STATE = 0x88, /* Get GPIO pull-up/down. */
|
|
|
|
LM832x_CMD_READ_FIFO = 0x89, /* Read byte from FIFO. */
|
|
|
|
LM832x_CMD_RPT_READ_FIFO = 0x8a, /* Read FIFO (no increment). */
|
|
|
|
LM832x_CMD_SET_ACTIVE = 0x8b, /* Set active time. */
|
|
|
|
LM832x_CMD_READ_ERROR = 0x8c, /* Get error status. */
|
|
|
|
LM832x_CMD_READ_ROTATOR = 0x8e, /* Read rotator status. */
|
|
|
|
LM832x_CMD_SET_DEBOUNCE = 0x8f, /* Set debouncing time. */
|
|
|
|
LM832x_CMD_SET_KEY_SIZE = 0x90, /* Set keypad size. */
|
|
|
|
LM832x_CMD_READ_KEY_SIZE = 0x91, /* Get keypad size. */
|
|
|
|
LM832x_CMD_READ_CFG = 0x92, /* Get configuration item. */
|
|
|
|
LM832x_CMD_WRITE_CLOCK = 0x93, /* Set clock config. */
|
|
|
|
LM832x_CMD_READ_CLOCK = 0x94, /* Get clock config. */
|
|
|
|
LM832x_CMD_PWM_WRITE = 0x95, /* Write PWM script. */
|
|
|
|
LM832x_CMD_PWM_START = 0x96, /* Start PWM engine. */
|
|
|
|
LM832x_CMD_PWM_STOP = 0x97, /* Stop PWM engine. */
|
2009-09-30 00:48:42 +04:00
|
|
|
LM832x_GENERAL_ERROR = 0xff, /* There was one error.
|
|
|
|
Previously was represented by -1
|
|
|
|
This is not a command */
|
2008-05-10 02:16:11 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
#define LM832x_MAX_KPX 8
|
|
|
|
#define LM832x_MAX_KPY 12
|
|
|
|
|
2009-05-10 04:44:56 +04:00
|
|
|
static uint8_t lm_kbd_read(LM823KbdState *s, int reg, int byte)
|
2008-05-10 02:16:11 +04:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
switch (reg) {
|
|
|
|
case LM832x_CMD_READ_ID:
|
|
|
|
ret = 0x0400;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LM832x_CMD_READ_INT:
|
|
|
|
ret = s->status;
|
|
|
|
if (!(s->status & INT_NOINIT)) {
|
|
|
|
s->status = 0;
|
|
|
|
lm_kbd_irq_update(s);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LM832x_CMD_READ_PORT_SEL:
|
|
|
|
ret = s->gpio.dir;
|
|
|
|
break;
|
|
|
|
case LM832x_CMD_READ_PORT_STATE:
|
|
|
|
ret = s->gpio.mask;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LM832x_CMD_READ_FIFO:
|
|
|
|
if (s->kbd.len <= 1)
|
|
|
|
return 0x00;
|
|
|
|
|
|
|
|
/* Example response from the two commands after a INT_KEYPAD
|
|
|
|
* interrupt caused by the key 0x3c being pressed:
|
|
|
|
* RPT_READ_FIFO: 55 bc 00 4e ff 0a 50 08 00 29 d9 08 01 c9 01
|
|
|
|
* READ_FIFO: bc 00 00 4e ff 0a 50 08 00 29 d9 08 01 c9 01
|
|
|
|
* RPT_READ_FIFO: bc 00 00 4e ff 0a 50 08 00 29 d9 08 01 c9 01
|
|
|
|
*
|
|
|
|
* 55 is the code of the key release event serviced in the previous
|
|
|
|
* interrupt handling.
|
|
|
|
*
|
|
|
|
* TODO: find out whether the FIFO is advanced a single character
|
|
|
|
* before reading every byte or the whole size of the FIFO at the
|
|
|
|
* last LM832x_CMD_READ_FIFO. This affects LM832x_CMD_RPT_READ_FIFO
|
|
|
|
* output in cases where there are more than one event in the FIFO.
|
|
|
|
* Assume 0xbc and 0x3c events are in the FIFO:
|
|
|
|
* RPT_READ_FIFO: 55 bc 3c 00 4e ff 0a 50 08 00 29 d9 08 01 c9
|
|
|
|
* READ_FIFO: bc 3c 00 00 4e ff 0a 50 08 00 29 d9 08 01 c9
|
|
|
|
* Does RPT_READ_FIFO now return 0xbc and 0x3c or only 0x3c?
|
|
|
|
*/
|
|
|
|
s->kbd.start ++;
|
|
|
|
s->kbd.start &= sizeof(s->kbd.fifo) - 1;
|
|
|
|
s->kbd.len --;
|
|
|
|
|
|
|
|
return s->kbd.fifo[s->kbd.start];
|
|
|
|
case LM832x_CMD_RPT_READ_FIFO:
|
|
|
|
if (byte >= s->kbd.len)
|
|
|
|
return 0x00;
|
|
|
|
|
|
|
|
return s->kbd.fifo[(s->kbd.start + byte) & (sizeof(s->kbd.fifo) - 1)];
|
|
|
|
|
|
|
|
case LM832x_CMD_READ_ERROR:
|
|
|
|
return s->error;
|
|
|
|
|
|
|
|
case LM832x_CMD_READ_ROTATOR:
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case LM832x_CMD_READ_KEY_SIZE:
|
|
|
|
return s->kbd.size;
|
|
|
|
|
|
|
|
case LM832x_CMD_READ_CFG:
|
|
|
|
return s->config & 0xf;
|
|
|
|
|
|
|
|
case LM832x_CMD_READ_CLOCK:
|
|
|
|
return (s->clock & 0xfc) | 2;
|
|
|
|
|
|
|
|
default:
|
|
|
|
lm_kbd_error(s, ERR_CMDUNK);
|
2017-11-09 01:56:31 +03:00
|
|
|
fprintf(stderr, "%s: unknown command %02x\n", __func__, reg);
|
2008-05-10 02:16:11 +04:00
|
|
|
return 0x00;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret >> (byte << 3);
|
|
|
|
}
|
|
|
|
|
2009-05-10 04:44:56 +04:00
|
|
|
static void lm_kbd_write(LM823KbdState *s, int reg, int byte, uint8_t value)
|
2008-05-10 02:16:11 +04:00
|
|
|
{
|
|
|
|
switch (reg) {
|
|
|
|
case LM832x_CMD_WRITE_CFG:
|
|
|
|
s->config = value;
|
|
|
|
/* This must be done whenever s->mux.in is updated (never). */
|
|
|
|
if ((s->config >> 1) & 1) /* MUX1EN */
|
|
|
|
qemu_set_irq(s->mux.out[0], s->mux.in[0][(s->config >> 0) & 1]);
|
|
|
|
if ((s->config >> 3) & 1) /* MUX2EN */
|
|
|
|
qemu_set_irq(s->mux.out[0], s->mux.in[0][(s->config >> 2) & 1]);
|
|
|
|
/* TODO: check that this is issued only following the chip reset
|
|
|
|
* and not in the middle of operation and that it is followed by
|
|
|
|
* the GPIO ports re-resablishing through WRITE_PORT_SEL and
|
|
|
|
* WRITE_PORT_STATE (using a timer perhaps) and otherwise output
|
|
|
|
* warnings. */
|
|
|
|
s->status = 0;
|
|
|
|
lm_kbd_irq_update(s);
|
|
|
|
s->kbd.len = 0;
|
|
|
|
s->kbd.start = 0;
|
2009-09-30 00:48:42 +04:00
|
|
|
s->reg = LM832x_GENERAL_ERROR;
|
2008-05-10 02:16:11 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LM832x_CMD_RESET:
|
|
|
|
if (value == 0xaa)
|
2019-10-10 16:15:26 +03:00
|
|
|
lm_kbd_reset(DEVICE(s));
|
2008-05-10 02:16:11 +04:00
|
|
|
else
|
|
|
|
lm_kbd_error(s, ERR_BADPAR);
|
2009-09-30 00:48:42 +04:00
|
|
|
s->reg = LM832x_GENERAL_ERROR;
|
2008-05-10 02:16:11 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LM823x_CMD_WRITE_PULL_DOWN:
|
|
|
|
if (!byte)
|
|
|
|
s->gpio.pull = value;
|
|
|
|
else {
|
|
|
|
s->gpio.pull |= value << 8;
|
|
|
|
lm_kbd_gpio_update(s);
|
2009-09-30 00:48:42 +04:00
|
|
|
s->reg = LM832x_GENERAL_ERROR;
|
2008-05-10 02:16:11 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case LM832x_CMD_WRITE_PORT_SEL:
|
|
|
|
if (!byte)
|
|
|
|
s->gpio.dir = value;
|
|
|
|
else {
|
|
|
|
s->gpio.dir |= value << 8;
|
|
|
|
lm_kbd_gpio_update(s);
|
2009-09-30 00:48:42 +04:00
|
|
|
s->reg = LM832x_GENERAL_ERROR;
|
2008-05-10 02:16:11 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case LM832x_CMD_WRITE_PORT_STATE:
|
|
|
|
if (!byte)
|
|
|
|
s->gpio.mask = value;
|
|
|
|
else {
|
|
|
|
s->gpio.mask |= value << 8;
|
|
|
|
lm_kbd_gpio_update(s);
|
2009-09-30 00:48:42 +04:00
|
|
|
s->reg = LM832x_GENERAL_ERROR;
|
2008-05-10 02:16:11 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LM832x_CMD_SET_ACTIVE:
|
|
|
|
s->acttime = value;
|
2009-09-30 00:48:42 +04:00
|
|
|
s->reg = LM832x_GENERAL_ERROR;
|
2008-05-10 02:16:11 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LM832x_CMD_SET_DEBOUNCE:
|
|
|
|
s->kbd.dbnctime = value;
|
2009-09-30 00:48:42 +04:00
|
|
|
s->reg = LM832x_GENERAL_ERROR;
|
2008-05-10 02:16:11 +04:00
|
|
|
if (!value)
|
|
|
|
lm_kbd_error(s, ERR_BADPAR);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LM832x_CMD_SET_KEY_SIZE:
|
|
|
|
s->kbd.size = value;
|
2009-09-30 00:48:42 +04:00
|
|
|
s->reg = LM832x_GENERAL_ERROR;
|
2008-05-10 02:16:11 +04:00
|
|
|
if (
|
|
|
|
(value & 0xf) < 3 || (value & 0xf) > LM832x_MAX_KPY ||
|
|
|
|
(value >> 4) < 3 || (value >> 4) > LM832x_MAX_KPX)
|
|
|
|
lm_kbd_error(s, ERR_BADPAR);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LM832x_CMD_WRITE_CLOCK:
|
|
|
|
s->clock = value;
|
2009-09-30 00:48:42 +04:00
|
|
|
s->reg = LM832x_GENERAL_ERROR;
|
2008-05-10 02:16:11 +04:00
|
|
|
if ((value & 3) && (value & 3) != 3) {
|
|
|
|
lm_kbd_error(s, ERR_BADPAR);
|
|
|
|
fprintf(stderr, "%s: invalid clock setting in RCPWM\n",
|
2017-11-09 01:56:31 +03:00
|
|
|
__func__);
|
2008-05-10 02:16:11 +04:00
|
|
|
}
|
|
|
|
/* TODO: Validate that the command is only issued once */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LM832x_CMD_PWM_WRITE:
|
|
|
|
if (byte == 0) {
|
|
|
|
if (!(value & 3) || (value >> 2) > 59) {
|
|
|
|
lm_kbd_error(s, ERR_BADPAR);
|
2009-09-30 00:48:42 +04:00
|
|
|
s->reg = LM832x_GENERAL_ERROR;
|
2008-05-10 02:16:11 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->pwm.faddr = value;
|
|
|
|
s->pwm.file[s->pwm.faddr] = 0;
|
|
|
|
} else if (byte == 1) {
|
|
|
|
s->pwm.file[s->pwm.faddr] |= value << 8;
|
|
|
|
} else if (byte == 2) {
|
|
|
|
s->pwm.file[s->pwm.faddr] |= value << 0;
|
2009-09-30 00:48:42 +04:00
|
|
|
s->reg = LM832x_GENERAL_ERROR;
|
2008-05-10 02:16:11 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case LM832x_CMD_PWM_START:
|
2009-09-30 00:48:42 +04:00
|
|
|
s->reg = LM832x_GENERAL_ERROR;
|
2008-05-10 02:16:11 +04:00
|
|
|
if (!(value & 3) || (value >> 2) > 59) {
|
|
|
|
lm_kbd_error(s, ERR_BADPAR);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->pwm.addr[(value & 3) - 1] = value >> 2;
|
|
|
|
lm_kbd_pwm_start(s, (value & 3) - 1);
|
|
|
|
break;
|
|
|
|
case LM832x_CMD_PWM_STOP:
|
2009-09-30 00:48:42 +04:00
|
|
|
s->reg = LM832x_GENERAL_ERROR;
|
2008-05-10 02:16:11 +04:00
|
|
|
if (!(value & 3)) {
|
|
|
|
lm_kbd_error(s, ERR_BADPAR);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-08-21 19:03:08 +04:00
|
|
|
timer_del(s->pwm.tm[(value & 3) - 1]);
|
2008-05-10 02:16:11 +04:00
|
|
|
break;
|
|
|
|
|
2009-09-30 00:48:42 +04:00
|
|
|
case LM832x_GENERAL_ERROR:
|
2008-05-10 02:16:11 +04:00
|
|
|
lm_kbd_error(s, ERR_BADPAR);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
lm_kbd_error(s, ERR_CMDUNK);
|
2017-11-09 01:56:31 +03:00
|
|
|
fprintf(stderr, "%s: unknown command %02x\n", __func__, reg);
|
2008-05-10 02:16:11 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-09 14:40:20 +03:00
|
|
|
static int lm_i2c_event(I2CSlave *i2c, enum i2c_event event)
|
2008-05-10 02:16:11 +04:00
|
|
|
{
|
2013-12-20 01:30:16 +04:00
|
|
|
LM823KbdState *s = LM8323(i2c);
|
2008-05-10 02:16:11 +04:00
|
|
|
|
|
|
|
switch (event) {
|
|
|
|
case I2C_START_RECV:
|
|
|
|
case I2C_START_SEND:
|
|
|
|
s->i2c_cycle = 0;
|
|
|
|
s->i2c_dir = (event == I2C_START_SEND);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-01-09 14:40:20 +03:00
|
|
|
|
|
|
|
return 0;
|
2008-05-10 02:16:11 +04:00
|
|
|
}
|
|
|
|
|
2018-11-14 20:50:50 +03:00
|
|
|
static uint8_t lm_i2c_rx(I2CSlave *i2c)
|
2008-05-10 02:16:11 +04:00
|
|
|
{
|
2013-12-20 01:30:16 +04:00
|
|
|
LM823KbdState *s = LM8323(i2c);
|
2008-05-10 02:16:11 +04:00
|
|
|
|
|
|
|
return lm_kbd_read(s, s->reg, s->i2c_cycle ++);
|
|
|
|
}
|
|
|
|
|
2011-12-05 06:28:27 +04:00
|
|
|
static int lm_i2c_tx(I2CSlave *i2c, uint8_t data)
|
2008-05-10 02:16:11 +04:00
|
|
|
{
|
2013-12-20 01:30:16 +04:00
|
|
|
LM823KbdState *s = LM8323(i2c);
|
2008-05-10 02:16:11 +04:00
|
|
|
|
|
|
|
if (!s->i2c_cycle)
|
|
|
|
s->reg = data;
|
|
|
|
else
|
|
|
|
lm_kbd_write(s, s->reg, s->i2c_cycle - 1, data);
|
|
|
|
s->i2c_cycle ++;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-09-30 00:48:43 +04:00
|
|
|
static int lm_kbd_post_load(void *opaque, int version_id)
|
2008-05-10 02:16:11 +04:00
|
|
|
{
|
2009-09-30 00:48:43 +04:00
|
|
|
LM823KbdState *s = opaque;
|
2008-05-10 02:16:11 +04:00
|
|
|
|
|
|
|
lm_kbd_irq_update(s);
|
|
|
|
lm_kbd_gpio_update(s);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-09-30 00:48:43 +04:00
|
|
|
static const VMStateDescription vmstate_lm_kbd = {
|
|
|
|
.name = "LM8323",
|
|
|
|
.version_id = 0,
|
|
|
|
.minimum_version_id = 0,
|
|
|
|
.post_load = lm_kbd_post_load,
|
2014-05-13 19:09:35 +04:00
|
|
|
.fields = (VMStateField[]) {
|
2013-12-20 01:30:16 +04:00
|
|
|
VMSTATE_I2C_SLAVE(parent_obj, LM823KbdState),
|
2009-09-30 00:48:43 +04:00
|
|
|
VMSTATE_UINT8(i2c_dir, LM823KbdState),
|
|
|
|
VMSTATE_UINT8(i2c_cycle, LM823KbdState),
|
|
|
|
VMSTATE_UINT8(reg, LM823KbdState),
|
|
|
|
VMSTATE_UINT8(config, LM823KbdState),
|
|
|
|
VMSTATE_UINT8(status, LM823KbdState),
|
|
|
|
VMSTATE_UINT8(acttime, LM823KbdState),
|
|
|
|
VMSTATE_UINT8(error, LM823KbdState),
|
|
|
|
VMSTATE_UINT8(clock, LM823KbdState),
|
|
|
|
VMSTATE_UINT16(gpio.pull, LM823KbdState),
|
|
|
|
VMSTATE_UINT16(gpio.mask, LM823KbdState),
|
|
|
|
VMSTATE_UINT16(gpio.dir, LM823KbdState),
|
|
|
|
VMSTATE_UINT16(gpio.level, LM823KbdState),
|
|
|
|
VMSTATE_UINT8(kbd.dbnctime, LM823KbdState),
|
|
|
|
VMSTATE_UINT8(kbd.size, LM823KbdState),
|
|
|
|
VMSTATE_UINT8(kbd.start, LM823KbdState),
|
|
|
|
VMSTATE_UINT8(kbd.len, LM823KbdState),
|
|
|
|
VMSTATE_BUFFER(kbd.fifo, LM823KbdState),
|
|
|
|
VMSTATE_UINT16_ARRAY(pwm.file, LM823KbdState, 256),
|
|
|
|
VMSTATE_UINT8(pwm.faddr, LM823KbdState),
|
|
|
|
VMSTATE_BUFFER(pwm.addr, LM823KbdState),
|
2015-01-08 12:18:59 +03:00
|
|
|
VMSTATE_TIMER_PTR_ARRAY(pwm.tm, LM823KbdState, 3),
|
2009-09-30 00:48:43 +04:00
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-05-28 17:45:07 +03:00
|
|
|
static void lm8323_realize(DeviceState *dev, Error **errp)
|
2008-05-10 02:16:11 +04:00
|
|
|
{
|
2018-05-28 17:45:07 +03:00
|
|
|
LM823KbdState *s = LM8323(dev);
|
2008-05-10 02:16:11 +04:00
|
|
|
|
|
|
|
s->model = 0x8323;
|
2013-08-21 19:03:08 +04:00
|
|
|
s->pwm.tm[0] = timer_new_ns(QEMU_CLOCK_VIRTUAL, lm_kbd_pwm0_tick, s);
|
|
|
|
s->pwm.tm[1] = timer_new_ns(QEMU_CLOCK_VIRTUAL, lm_kbd_pwm1_tick, s);
|
|
|
|
s->pwm.tm[2] = timer_new_ns(QEMU_CLOCK_VIRTUAL, lm_kbd_pwm2_tick, s);
|
2018-05-28 17:45:07 +03:00
|
|
|
qdev_init_gpio_out(dev, &s->nirq, 1);
|
2008-05-10 02:16:11 +04:00
|
|
|
}
|
|
|
|
|
2011-07-29 19:35:18 +04:00
|
|
|
void lm832x_key_event(DeviceState *dev, int key, int state)
|
2008-05-10 02:16:11 +04:00
|
|
|
{
|
2013-12-20 01:30:16 +04:00
|
|
|
LM823KbdState *s = LM8323(dev);
|
2008-05-10 02:16:11 +04:00
|
|
|
|
|
|
|
if ((s->status & INT_ERROR) && (s->error & ERR_FIFOOVR))
|
|
|
|
return;
|
|
|
|
|
2009-04-07 22:22:35 +04:00
|
|
|
if (s->kbd.len >= sizeof(s->kbd.fifo)) {
|
|
|
|
lm_kbd_error(s, ERR_FIFOOVR);
|
|
|
|
return;
|
|
|
|
}
|
2008-05-10 02:16:11 +04:00
|
|
|
|
|
|
|
s->kbd.fifo[(s->kbd.start + s->kbd.len ++) & (sizeof(s->kbd.fifo) - 1)] =
|
|
|
|
key | (state << 7);
|
|
|
|
|
|
|
|
/* We never set ERR_KEYOVR because we support multiple keys fine. */
|
|
|
|
s->status |= INT_KEYPAD;
|
|
|
|
lm_kbd_irq_update(s);
|
|
|
|
}
|
2009-05-15 01:35:08 +04:00
|
|
|
|
2011-12-05 06:39:20 +04:00
|
|
|
static void lm8323_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
2011-12-08 07:34:16 +04:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2011-12-05 06:39:20 +04:00
|
|
|
I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
|
|
|
|
|
2019-10-10 16:15:26 +03:00
|
|
|
dc->reset = lm_kbd_reset;
|
2018-05-28 17:45:07 +03:00
|
|
|
dc->realize = lm8323_realize;
|
2011-12-05 06:39:20 +04:00
|
|
|
k->event = lm_i2c_event;
|
|
|
|
k->recv = lm_i2c_rx;
|
|
|
|
k->send = lm_i2c_tx;
|
2011-12-08 07:34:16 +04:00
|
|
|
dc->vmsd = &vmstate_lm_kbd;
|
2011-12-05 06:39:20 +04:00
|
|
|
}
|
|
|
|
|
2013-01-10 19:19:07 +04:00
|
|
|
static const TypeInfo lm8323_info = {
|
2013-12-20 01:30:16 +04:00
|
|
|
.name = TYPE_LM8323,
|
2011-12-08 07:34:16 +04:00
|
|
|
.parent = TYPE_I2C_SLAVE,
|
|
|
|
.instance_size = sizeof(LM823KbdState),
|
|
|
|
.class_init = lm8323_class_init,
|
2009-05-15 01:35:08 +04:00
|
|
|
};
|
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
static void lm832x_register_types(void)
|
2009-05-15 01:35:08 +04:00
|
|
|
{
|
2011-12-08 07:34:16 +04:00
|
|
|
type_register_static(&lm8323_info);
|
2009-05-15 01:35:08 +04:00
|
|
|
}
|
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
type_init(lm832x_register_types)
|