2018-06-08 14:15:32 +02:00
|
|
|
/*
|
|
|
|
* PCA9552 I2C LED blinker
|
|
|
|
*
|
|
|
|
* https://www.nxp.com/docs/en/application-note/AN264.pdf
|
|
|
|
*
|
|
|
|
* Copyright (c) 2017-2018, IBM Corporation.
|
2020-06-23 09:27:18 +02:00
|
|
|
* Copyright (c) 2020 Philippe Mathieu-Daudé
|
2018-06-08 14:15:32 +02:00
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or
|
|
|
|
* later. See the COPYING file in the top-level directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
|
|
|
#include "qemu/log.h"
|
2019-05-23 16:35:07 +02:00
|
|
|
#include "qemu/module.h"
|
2018-06-08 14:15:32 +02:00
|
|
|
#include "hw/misc/pca9552.h"
|
|
|
|
#include "hw/misc/pca9552_regs.h"
|
2019-08-12 07:23:45 +02:00
|
|
|
#include "migration/vmstate.h"
|
2020-01-30 17:02:02 +01:00
|
|
|
#include "qapi/error.h"
|
|
|
|
#include "qapi/visitor.h"
|
2018-06-08 14:15:32 +02:00
|
|
|
|
2020-06-23 09:27:18 +02:00
|
|
|
typedef struct PCA955xClass {
|
|
|
|
/*< private >*/
|
|
|
|
I2CSlaveClass parent_class;
|
|
|
|
/*< public >*/
|
|
|
|
|
|
|
|
uint8_t pin_count;
|
|
|
|
uint8_t max_reg;
|
|
|
|
} PCA955xClass;
|
|
|
|
|
|
|
|
#define PCA955X_CLASS(klass) \
|
|
|
|
OBJECT_CLASS_CHECK(PCA955xClass, (klass), TYPE_PCA955X)
|
|
|
|
#define PCA955X_GET_CLASS(obj) \
|
|
|
|
OBJECT_GET_CLASS(PCA955xClass, (obj), TYPE_PCA955X)
|
|
|
|
|
2018-06-08 14:15:32 +02:00
|
|
|
#define PCA9552_LED_ON 0x0
|
|
|
|
#define PCA9552_LED_OFF 0x1
|
|
|
|
#define PCA9552_LED_PWM0 0x2
|
|
|
|
#define PCA9552_LED_PWM1 0x3
|
|
|
|
|
2020-01-30 17:02:02 +01:00
|
|
|
static const char *led_state[] = {"on", "off", "pwm0", "pwm1"};
|
|
|
|
|
2020-06-23 09:27:17 +02:00
|
|
|
static uint8_t pca955x_pin_get_config(PCA955xState *s, int pin)
|
2018-06-08 14:15:32 +02:00
|
|
|
{
|
|
|
|
uint8_t reg = PCA9552_LS0 + (pin / 4);
|
|
|
|
uint8_t shift = (pin % 4) << 1;
|
|
|
|
|
|
|
|
return extract32(s->regs[reg], shift, 2);
|
|
|
|
}
|
|
|
|
|
2020-06-23 09:27:17 +02:00
|
|
|
static void pca955x_update_pin_input(PCA955xState *s)
|
2018-06-08 14:15:32 +02:00
|
|
|
{
|
2020-06-23 09:27:18 +02:00
|
|
|
PCA955xClass *k = PCA955X_GET_CLASS(s);
|
2018-06-08 14:15:32 +02:00
|
|
|
int i;
|
|
|
|
|
2020-06-23 09:27:18 +02:00
|
|
|
for (i = 0; i < k->pin_count; i++) {
|
2018-06-08 14:15:32 +02:00
|
|
|
uint8_t input_reg = PCA9552_INPUT0 + (i / 8);
|
|
|
|
uint8_t input_shift = (i % 8);
|
2020-06-23 09:27:17 +02:00
|
|
|
uint8_t config = pca955x_pin_get_config(s, i);
|
2018-06-08 14:15:32 +02:00
|
|
|
|
|
|
|
switch (config) {
|
|
|
|
case PCA9552_LED_ON:
|
|
|
|
s->regs[input_reg] |= 1 << input_shift;
|
|
|
|
break;
|
|
|
|
case PCA9552_LED_OFF:
|
|
|
|
s->regs[input_reg] &= ~(1 << input_shift);
|
|
|
|
break;
|
|
|
|
case PCA9552_LED_PWM0:
|
|
|
|
case PCA9552_LED_PWM1:
|
|
|
|
/* TODO */
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 09:27:17 +02:00
|
|
|
static uint8_t pca955x_read(PCA955xState *s, uint8_t reg)
|
2018-06-08 14:15:32 +02:00
|
|
|
{
|
|
|
|
switch (reg) {
|
|
|
|
case PCA9552_INPUT0:
|
|
|
|
case PCA9552_INPUT1:
|
|
|
|
case PCA9552_PSC0:
|
|
|
|
case PCA9552_PWM0:
|
|
|
|
case PCA9552_PSC1:
|
|
|
|
case PCA9552_PWM1:
|
|
|
|
case PCA9552_LS0:
|
|
|
|
case PCA9552_LS1:
|
|
|
|
case PCA9552_LS2:
|
|
|
|
case PCA9552_LS3:
|
|
|
|
return s->regs[reg];
|
|
|
|
default:
|
|
|
|
qemu_log_mask(LOG_GUEST_ERROR, "%s: unexpected read to register %d\n",
|
|
|
|
__func__, reg);
|
|
|
|
return 0xFF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 09:27:17 +02:00
|
|
|
static void pca955x_write(PCA955xState *s, uint8_t reg, uint8_t data)
|
2018-06-08 14:15:32 +02:00
|
|
|
{
|
|
|
|
switch (reg) {
|
|
|
|
case PCA9552_PSC0:
|
|
|
|
case PCA9552_PWM0:
|
|
|
|
case PCA9552_PSC1:
|
|
|
|
case PCA9552_PWM1:
|
|
|
|
s->regs[reg] = data;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PCA9552_LS0:
|
|
|
|
case PCA9552_LS1:
|
|
|
|
case PCA9552_LS2:
|
|
|
|
case PCA9552_LS3:
|
|
|
|
s->regs[reg] = data;
|
2020-06-23 09:27:17 +02:00
|
|
|
pca955x_update_pin_input(s);
|
2018-06-08 14:15:32 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PCA9552_INPUT0:
|
|
|
|
case PCA9552_INPUT1:
|
|
|
|
default:
|
|
|
|
qemu_log_mask(LOG_GUEST_ERROR, "%s: unexpected write to register %d\n",
|
|
|
|
__func__, reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When Auto-Increment is on, the register address is incremented
|
|
|
|
* after each byte is sent to or received by the device. The index
|
|
|
|
* rollovers to 0 when the maximum register address is reached.
|
|
|
|
*/
|
2020-06-23 09:27:17 +02:00
|
|
|
static void pca955x_autoinc(PCA955xState *s)
|
2018-06-08 14:15:32 +02:00
|
|
|
{
|
2020-06-23 09:27:18 +02:00
|
|
|
PCA955xClass *k = PCA955X_GET_CLASS(s);
|
|
|
|
|
2018-06-08 14:15:32 +02:00
|
|
|
if (s->pointer != 0xFF && s->pointer & PCA9552_AUTOINC) {
|
|
|
|
uint8_t reg = s->pointer & 0xf;
|
|
|
|
|
2020-06-23 09:27:18 +02:00
|
|
|
reg = (reg + 1) % (k->max_reg + 1);
|
2018-06-08 14:15:32 +02:00
|
|
|
s->pointer = reg | PCA9552_AUTOINC;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 09:27:17 +02:00
|
|
|
static uint8_t pca955x_recv(I2CSlave *i2c)
|
2018-06-08 14:15:32 +02:00
|
|
|
{
|
2020-06-23 09:27:17 +02:00
|
|
|
PCA955xState *s = PCA955X(i2c);
|
2018-06-08 14:15:32 +02:00
|
|
|
uint8_t ret;
|
|
|
|
|
2020-06-23 09:27:17 +02:00
|
|
|
ret = pca955x_read(s, s->pointer & 0xf);
|
2018-06-08 14:15:32 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* From the Specs:
|
|
|
|
*
|
|
|
|
* Important Note: When a Read sequence is initiated and the
|
|
|
|
* AI bit is set to Logic Level 1, the Read Sequence MUST
|
|
|
|
* start by a register different from 0.
|
|
|
|
*
|
|
|
|
* I don't know what should be done in this case, so throw an
|
|
|
|
* error.
|
|
|
|
*/
|
|
|
|
if (s->pointer == PCA9552_AUTOINC) {
|
|
|
|
qemu_log_mask(LOG_GUEST_ERROR,
|
|
|
|
"%s: Autoincrement read starting with register 0\n",
|
|
|
|
__func__);
|
|
|
|
}
|
|
|
|
|
2020-06-23 09:27:17 +02:00
|
|
|
pca955x_autoinc(s);
|
2018-06-08 14:15:32 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-06-23 09:27:17 +02:00
|
|
|
static int pca955x_send(I2CSlave *i2c, uint8_t data)
|
2018-06-08 14:15:32 +02:00
|
|
|
{
|
2020-06-23 09:27:17 +02:00
|
|
|
PCA955xState *s = PCA955X(i2c);
|
2018-06-08 14:15:32 +02:00
|
|
|
|
|
|
|
/* First byte sent by is the register address */
|
|
|
|
if (s->len == 0) {
|
|
|
|
s->pointer = data;
|
|
|
|
s->len++;
|
|
|
|
} else {
|
2020-06-23 09:27:17 +02:00
|
|
|
pca955x_write(s, s->pointer & 0xf, data);
|
2018-06-08 14:15:32 +02:00
|
|
|
|
2020-06-23 09:27:17 +02:00
|
|
|
pca955x_autoinc(s);
|
2018-06-08 14:15:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-06-23 09:27:17 +02:00
|
|
|
static int pca955x_event(I2CSlave *i2c, enum i2c_event event)
|
2018-06-08 14:15:32 +02:00
|
|
|
{
|
2020-06-23 09:27:17 +02:00
|
|
|
PCA955xState *s = PCA955X(i2c);
|
2018-06-08 14:15:32 +02:00
|
|
|
|
|
|
|
s->len = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-06-23 09:27:17 +02:00
|
|
|
static void pca955x_get_led(Object *obj, Visitor *v, const char *name,
|
2020-01-30 17:02:02 +01:00
|
|
|
void *opaque, Error **errp)
|
|
|
|
{
|
2020-06-23 09:27:18 +02:00
|
|
|
PCA955xClass *k = PCA955X_GET_CLASS(obj);
|
2020-06-23 09:27:17 +02:00
|
|
|
PCA955xState *s = PCA955X(obj);
|
2020-01-30 17:02:02 +01:00
|
|
|
int led, rc, reg;
|
|
|
|
uint8_t state;
|
|
|
|
|
|
|
|
rc = sscanf(name, "led%2d", &led);
|
|
|
|
if (rc != 1) {
|
|
|
|
error_setg(errp, "%s: error reading %s", __func__, name);
|
|
|
|
return;
|
|
|
|
}
|
2020-06-23 09:27:18 +02:00
|
|
|
if (led < 0 || led > k->pin_count) {
|
2020-01-30 17:02:02 +01:00
|
|
|
error_setg(errp, "%s invalid led %s", __func__, name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Get the LSx register as the qom interface should expose the device
|
|
|
|
* state, not the modeled 'input line' behaviour which would come from
|
|
|
|
* reading the INPUTx reg
|
|
|
|
*/
|
|
|
|
reg = PCA9552_LS0 + led / 4;
|
2020-06-23 09:27:17 +02:00
|
|
|
state = (pca955x_read(s, reg) >> (led % 8)) & 0x3;
|
2020-01-30 17:02:02 +01:00
|
|
|
visit_type_str(v, name, (char **)&led_state[state], errp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return an LED selector register value based on an existing one, with
|
|
|
|
* the appropriate 2-bit state value set for the given LED number (0-3).
|
|
|
|
*/
|
|
|
|
static inline uint8_t pca955x_ledsel(uint8_t oldval, int led_num, int state)
|
|
|
|
{
|
|
|
|
return (oldval & (~(0x3 << (led_num << 1)))) |
|
|
|
|
((state & 0x3) << (led_num << 1));
|
|
|
|
}
|
|
|
|
|
2020-06-23 09:27:17 +02:00
|
|
|
static void pca955x_set_led(Object *obj, Visitor *v, const char *name,
|
2020-01-30 17:02:02 +01:00
|
|
|
void *opaque, Error **errp)
|
|
|
|
{
|
2020-06-23 09:27:18 +02:00
|
|
|
PCA955xClass *k = PCA955X_GET_CLASS(obj);
|
2020-06-23 09:27:17 +02:00
|
|
|
PCA955xState *s = PCA955X(obj);
|
2020-01-30 17:02:02 +01:00
|
|
|
Error *local_err = NULL;
|
|
|
|
int led, rc, reg, val;
|
|
|
|
uint8_t state;
|
|
|
|
char *state_str;
|
|
|
|
|
|
|
|
visit_type_str(v, name, &state_str, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
rc = sscanf(name, "led%2d", &led);
|
|
|
|
if (rc != 1) {
|
|
|
|
error_setg(errp, "%s: error reading %s", __func__, name);
|
|
|
|
return;
|
|
|
|
}
|
2020-06-23 09:27:18 +02:00
|
|
|
if (led < 0 || led > k->pin_count) {
|
2020-01-30 17:02:02 +01:00
|
|
|
error_setg(errp, "%s invalid led %s", __func__, name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (state = 0; state < ARRAY_SIZE(led_state); state++) {
|
|
|
|
if (!strcmp(state_str, led_state[state])) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (state >= ARRAY_SIZE(led_state)) {
|
|
|
|
error_setg(errp, "%s invalid led state %s", __func__, state_str);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
reg = PCA9552_LS0 + led / 4;
|
2020-06-23 09:27:17 +02:00
|
|
|
val = pca955x_read(s, reg);
|
2020-01-30 17:02:02 +01:00
|
|
|
val = pca955x_ledsel(val, led % 4, state);
|
2020-06-23 09:27:17 +02:00
|
|
|
pca955x_write(s, reg, val);
|
2020-01-30 17:02:02 +01:00
|
|
|
}
|
|
|
|
|
2018-06-08 14:15:32 +02:00
|
|
|
static const VMStateDescription pca9552_vmstate = {
|
|
|
|
.name = "PCA9552",
|
|
|
|
.version_id = 0,
|
|
|
|
.minimum_version_id = 0,
|
|
|
|
.fields = (VMStateField[]) {
|
2020-06-23 09:27:17 +02:00
|
|
|
VMSTATE_UINT8(len, PCA955xState),
|
|
|
|
VMSTATE_UINT8(pointer, PCA955xState),
|
|
|
|
VMSTATE_UINT8_ARRAY(regs, PCA955xState, PCA955X_NR_REGS),
|
|
|
|
VMSTATE_I2C_SLAVE(i2c, PCA955xState),
|
2018-06-08 14:15:32 +02:00
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static void pca9552_reset(DeviceState *dev)
|
|
|
|
{
|
2020-06-23 09:27:17 +02:00
|
|
|
PCA955xState *s = PCA955X(dev);
|
2018-06-08 14:15:32 +02:00
|
|
|
|
|
|
|
s->regs[PCA9552_PSC0] = 0xFF;
|
|
|
|
s->regs[PCA9552_PWM0] = 0x80;
|
|
|
|
s->regs[PCA9552_PSC1] = 0xFF;
|
|
|
|
s->regs[PCA9552_PWM1] = 0x80;
|
|
|
|
s->regs[PCA9552_LS0] = 0x55; /* all OFF */
|
|
|
|
s->regs[PCA9552_LS1] = 0x55;
|
|
|
|
s->regs[PCA9552_LS2] = 0x55;
|
|
|
|
s->regs[PCA9552_LS3] = 0x55;
|
|
|
|
|
2020-06-23 09:27:17 +02:00
|
|
|
pca955x_update_pin_input(s);
|
2018-06-08 14:15:32 +02:00
|
|
|
|
|
|
|
s->pointer = 0xFF;
|
|
|
|
s->len = 0;
|
|
|
|
}
|
|
|
|
|
2020-06-23 09:27:17 +02:00
|
|
|
static void pca955x_initfn(Object *obj)
|
2018-06-08 14:15:32 +02:00
|
|
|
{
|
2020-06-23 09:27:18 +02:00
|
|
|
PCA955xClass *k = PCA955X_GET_CLASS(obj);
|
2020-01-30 17:02:02 +01:00
|
|
|
int led;
|
2018-06-08 14:15:32 +02:00
|
|
|
|
2020-06-23 09:27:18 +02:00
|
|
|
assert(k->pin_count <= PCA955X_PIN_COUNT_MAX);
|
|
|
|
for (led = 0; led < k->pin_count; led++) {
|
2020-01-30 17:02:02 +01:00
|
|
|
char *name;
|
|
|
|
|
|
|
|
name = g_strdup_printf("led%d", led);
|
2020-06-23 09:27:17 +02:00
|
|
|
object_property_add(obj, name, "bool", pca955x_get_led, pca955x_set_led,
|
qom: Drop parameter @errp of object_property_add() & friends
The only way object_property_add() can fail is when a property with
the same name already exists. Since our property names are all
hardcoded, failure is a programming error, and the appropriate way to
handle it is passing &error_abort.
Same for its variants, except for object_property_add_child(), which
additionally fails when the child already has a parent. Parentage is
also under program control, so this is a programming error, too.
We have a bit over 500 callers. Almost half of them pass
&error_abort, slightly fewer ignore errors, one test case handles
errors, and the remaining few callers pass them to their own callers.
The previous few commits demonstrated once again that ignoring
programming errors is a bad idea.
Of the few ones that pass on errors, several violate the Error API.
The Error ** argument must be NULL, &error_abort, &error_fatal, or a
pointer to a variable containing NULL. Passing an argument of the
latter kind twice without clearing it in between is wrong: if the
first call sets an error, it no longer points to NULL for the second
call. ich9_pm_add_properties(), sparc32_ledma_realize(),
sparc32_dma_realize(), xilinx_axidma_realize(), xilinx_enet_realize()
are wrong that way.
When the one appropriate choice of argument is &error_abort, letting
users pick the argument is a bad idea.
Drop parameter @errp and assert the preconditions instead.
There's one exception to "duplicate property name is a programming
error": the way object_property_add() implements the magic (and
undocumented) "automatic arrayification". Don't drop @errp there.
Instead, rename object_property_add() to object_property_try_add(),
and add the obvious wrapper object_property_add().
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <20200505152926.18877-15-armbru@redhat.com>
[Two semantic rebase conflicts resolved]
2020-05-05 17:29:22 +02:00
|
|
|
NULL, NULL);
|
2020-01-30 17:02:02 +01:00
|
|
|
g_free(name);
|
|
|
|
}
|
2018-06-08 14:15:32 +02:00
|
|
|
}
|
|
|
|
|
2020-06-23 09:27:18 +02:00
|
|
|
static void pca955x_class_init(ObjectClass *klass, void *data)
|
2018-06-08 14:15:32 +02:00
|
|
|
{
|
|
|
|
I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
|
|
|
|
|
2020-06-23 09:27:17 +02:00
|
|
|
k->event = pca955x_event;
|
|
|
|
k->recv = pca955x_recv;
|
|
|
|
k->send = pca955x_send;
|
2020-06-23 09:27:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const TypeInfo pca955x_info = {
|
|
|
|
.name = TYPE_PCA955X,
|
|
|
|
.parent = TYPE_I2C_SLAVE,
|
|
|
|
.instance_init = pca955x_initfn,
|
|
|
|
.instance_size = sizeof(PCA955xState),
|
|
|
|
.class_init = pca955x_class_init,
|
|
|
|
.abstract = true,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void pca9552_class_init(ObjectClass *oc, void *data)
|
|
|
|
{
|
|
|
|
DeviceClass *dc = DEVICE_CLASS(oc);
|
|
|
|
PCA955xClass *pc = PCA955X_CLASS(oc);
|
|
|
|
|
2018-06-08 14:15:32 +02:00
|
|
|
dc->reset = pca9552_reset;
|
|
|
|
dc->vmsd = &pca9552_vmstate;
|
2020-06-23 09:27:18 +02:00
|
|
|
pc->max_reg = PCA9552_LS3;
|
|
|
|
pc->pin_count = 16;
|
2018-06-08 14:15:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const TypeInfo pca9552_info = {
|
|
|
|
.name = TYPE_PCA9552,
|
2020-06-23 09:27:18 +02:00
|
|
|
.parent = TYPE_PCA955X,
|
2018-06-08 14:15:32 +02:00
|
|
|
.class_init = pca9552_class_init,
|
|
|
|
};
|
|
|
|
|
2020-06-23 09:27:17 +02:00
|
|
|
static void pca955x_register_types(void)
|
2018-06-08 14:15:32 +02:00
|
|
|
{
|
2020-06-23 09:27:18 +02:00
|
|
|
type_register_static(&pca955x_info);
|
2018-06-08 14:15:32 +02:00
|
|
|
type_register_static(&pca9552_info);
|
|
|
|
}
|
|
|
|
|
2020-06-23 09:27:17 +02:00
|
|
|
type_init(pca955x_register_types)
|