13
#include "qemu/osdep.h"
15
#include "qemu/module.h"
16
#include "qemu/bitops.h"
17
#include "hw/qdev-properties.h"
18
#include "hw/gpio/pca9552.h"
19
#include "hw/gpio/pca9552_regs.h"
21
#include "migration/vmstate.h"
22
#include "qapi/error.h"
23
#include "qapi/visitor.h"
25
#include "qom/object.h"
29
I2CSlaveClass parent_class;
35
typedef struct PCA955xClass PCA955xClass;
37
DECLARE_CLASS_CHECKERS(PCA955xClass, PCA955X,
43
#define PCA9552_LED_ON 0x0
44
#define PCA9552_LED_OFF 0x1
45
#define PCA9552_LED_PWM0 0x2
46
#define PCA9552_LED_PWM1 0x3
47
#define PCA9552_PIN_LOW 0x0
48
#define PCA9552_PIN_HIZ 0x1
50
static const char *led_state[] = {"on", "off", "pwm0", "pwm1"};
52
static uint8_t pca955x_pin_get_config(PCA955xState *s, int pin)
54
uint8_t reg = PCA9552_LS0 + (pin / 4);
55
uint8_t shift = (pin % 4) << 1;
57
return extract32(s->regs[reg], shift, 2);
61
static uint16_t pca955x_pins_get_status(PCA955xState *s)
63
return (s->regs[PCA9552_INPUT1] << 8) | s->regs[PCA9552_INPUT0];
66
static void pca955x_display_pins_status(PCA955xState *s,
67
uint16_t previous_pins_status)
69
PCA955xClass *k = PCA955X_GET_CLASS(s);
70
uint16_t pins_status, pins_changed;
73
pins_status = pca955x_pins_get_status(s);
74
pins_changed = previous_pins_status ^ pins_status;
78
if (trace_event_get_state_backends(TRACE_PCA955X_GPIO_STATUS)) {
79
char *buf = g_newa(char, k->pin_count + 1);
81
for (i = 0; i < k->pin_count; i++) {
82
if (extract32(pins_status, i, 1)) {
89
trace_pca955x_gpio_status(s->description, buf);
91
if (trace_event_get_state_backends(TRACE_PCA955X_GPIO_CHANGE)) {
92
for (i = 0; i < k->pin_count; i++) {
93
if (extract32(pins_changed, i, 1)) {
94
unsigned new_state = extract32(pins_status, i, 1);
101
trace_pca955x_gpio_change(s->description, i,
102
!new_state, new_state);
108
static void pca955x_update_pin_input(PCA955xState *s)
110
PCA955xClass *k = PCA955X_GET_CLASS(s);
113
for (i = 0; i < k->pin_count; i++) {
114
uint8_t input_reg = PCA9552_INPUT0 + (i / 8);
115
uint8_t bit_mask = 1 << (i % 8);
116
uint8_t config = pca955x_pin_get_config(s, i);
117
uint8_t old_value = s->regs[input_reg] & bit_mask;
123
s->regs[input_reg] &= ~bit_mask;
125
case PCA9552_LED_OFF:
131
if (s->ext_state[i] == PCA9552_PIN_LOW) {
132
s->regs[input_reg] &= ~bit_mask;
134
s->regs[input_reg] |= bit_mask;
137
case PCA9552_LED_PWM0:
138
case PCA9552_LED_PWM1:
145
new_value = s->regs[input_reg] & bit_mask;
146
if (new_value != old_value) {
147
qemu_set_irq(s->gpio_out[i], !!new_value);
152
static uint8_t pca955x_read(PCA955xState *s, uint8_t reg)
167
qemu_log_mask(LOG_GUEST_ERROR, "%s: unexpected read to register %d\n",
173
static void pca955x_write(PCA955xState *s, uint8_t reg, uint8_t data)
175
uint16_t pins_status;
189
pins_status = pca955x_pins_get_status(s);
191
pca955x_update_pin_input(s);
192
pca955x_display_pins_status(s, pins_status);
198
qemu_log_mask(LOG_GUEST_ERROR, "%s: unexpected write to register %d\n",
208
static void pca955x_autoinc(PCA955xState *s)
210
PCA955xClass *k = PCA955X_GET_CLASS(s);
212
if (s->pointer != 0xFF && s->pointer & PCA9552_AUTOINC) {
213
uint8_t reg = s->pointer & 0xf;
215
reg = (reg + 1) % (k->max_reg + 1);
216
s->pointer = reg | PCA9552_AUTOINC;
220
static uint8_t pca955x_recv(I2CSlave *i2c)
222
PCA955xState *s = PCA955X(i2c);
225
ret = pca955x_read(s, s->pointer & 0xf);
237
if (s->pointer == PCA9552_AUTOINC) {
238
qemu_log_mask(LOG_GUEST_ERROR,
239
"%s: Autoincrement read starting with register 0\n",
248
static int pca955x_send(I2CSlave *i2c, uint8_t data)
250
PCA955xState *s = PCA955X(i2c);
257
pca955x_write(s, s->pointer & 0xf, data);
265
static int pca955x_event(I2CSlave *i2c, enum i2c_event event)
267
PCA955xState *s = PCA955X(i2c);
273
static void pca955x_get_led(Object *obj, Visitor *v, const char *name,
274
void *opaque, Error **errp)
276
PCA955xClass *k = PCA955X_GET_CLASS(obj);
277
PCA955xState *s = PCA955X(obj);
281
rc = sscanf(name, "led%2d", &led);
283
error_setg(errp, "%s: error reading %s", __func__, name);
286
if (led < 0 || led > k->pin_count) {
287
error_setg(errp, "%s invalid led %s", __func__, name);
295
reg = PCA9552_LS0 + led / 4;
296
state = (pca955x_read(s, reg) >> ((led % 4) * 2)) & 0x3;
297
visit_type_str(v, name, (char **)&led_state[state], errp);
304
static inline uint8_t pca955x_ledsel(uint8_t oldval, int led_num, int state)
306
return (oldval & (~(0x3 << (led_num << 1)))) |
307
((state & 0x3) << (led_num << 1));
310
static void pca955x_set_led(Object *obj, Visitor *v, const char *name,
311
void *opaque, Error **errp)
313
PCA955xClass *k = PCA955X_GET_CLASS(obj);
314
PCA955xState *s = PCA955X(obj);
315
int led, rc, reg, val;
319
if (!visit_type_str(v, name, &state_str, errp)) {
322
rc = sscanf(name, "led%2d", &led);
324
error_setg(errp, "%s: error reading %s", __func__, name);
327
if (led < 0 || led > k->pin_count) {
328
error_setg(errp, "%s invalid led %s", __func__, name);
332
for (state = 0; state < ARRAY_SIZE(led_state); state++) {
333
if (!strcmp(state_str, led_state[state])) {
337
if (state >= ARRAY_SIZE(led_state)) {
338
error_setg(errp, "%s invalid led state %s", __func__, state_str);
342
reg = PCA9552_LS0 + led / 4;
343
val = pca955x_read(s, reg);
344
val = pca955x_ledsel(val, led % 4, state);
345
pca955x_write(s, reg, val);
348
static const VMStateDescription pca9552_vmstate = {
351
.minimum_version_id = 0,
352
.fields = (const VMStateField[]) {
353
VMSTATE_UINT8(len, PCA955xState),
354
VMSTATE_UINT8(pointer, PCA955xState),
355
VMSTATE_UINT8_ARRAY(regs, PCA955xState, PCA955X_NR_REGS),
356
VMSTATE_UINT8_ARRAY(ext_state, PCA955xState, PCA955X_PIN_COUNT_MAX),
357
VMSTATE_I2C_SLAVE(i2c, PCA955xState),
358
VMSTATE_END_OF_LIST()
362
static void pca9552_reset(DeviceState *dev)
364
PCA955xState *s = PCA955X(dev);
366
s->regs[PCA9552_PSC0] = 0xFF;
367
s->regs[PCA9552_PWM0] = 0x80;
368
s->regs[PCA9552_PSC1] = 0xFF;
369
s->regs[PCA9552_PWM1] = 0x80;
370
s->regs[PCA9552_LS0] = 0x55;
371
s->regs[PCA9552_LS1] = 0x55;
372
s->regs[PCA9552_LS2] = 0x55;
373
s->regs[PCA9552_LS3] = 0x55;
375
memset(s->ext_state, PCA9552_PIN_HIZ, PCA955X_PIN_COUNT_MAX);
376
pca955x_update_pin_input(s);
382
static void pca955x_initfn(Object *obj)
384
PCA955xClass *k = PCA955X_GET_CLASS(obj);
387
assert(k->pin_count <= PCA955X_PIN_COUNT_MAX);
388
for (led = 0; led < k->pin_count; led++) {
391
name = g_strdup_printf("led%d", led);
392
object_property_add(obj, name, "bool", pca955x_get_led, pca955x_set_led,
398
static void pca955x_set_ext_state(PCA955xState *s, int pin, int level)
400
if (s->ext_state[pin] != level) {
401
uint16_t pins_status = pca955x_pins_get_status(s);
402
s->ext_state[pin] = level;
403
pca955x_update_pin_input(s);
404
pca955x_display_pins_status(s, pins_status);
408
static void pca955x_gpio_in_handler(void *opaque, int pin, int level)
411
PCA955xState *s = PCA955X(opaque);
412
PCA955xClass *k = PCA955X_GET_CLASS(s);
414
assert((pin >= 0) && (pin < k->pin_count));
415
pca955x_set_ext_state(s, pin, level);
418
static void pca955x_realize(DeviceState *dev, Error **errp)
420
PCA955xClass *k = PCA955X_GET_CLASS(dev);
421
PCA955xState *s = PCA955X(dev);
423
if (!s->description) {
424
s->description = g_strdup("pca-unspecified");
427
qdev_init_gpio_out(dev, s->gpio_out, k->pin_count);
428
qdev_init_gpio_in(dev, pca955x_gpio_in_handler, k->pin_count);
431
static Property pca955x_properties[] = {
432
DEFINE_PROP_STRING("description", PCA955xState, description),
433
DEFINE_PROP_END_OF_LIST(),
436
static void pca955x_class_init(ObjectClass *klass, void *data)
438
DeviceClass *dc = DEVICE_CLASS(klass);
439
I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
441
k->event = pca955x_event;
442
k->recv = pca955x_recv;
443
k->send = pca955x_send;
444
dc->realize = pca955x_realize;
445
device_class_set_props(dc, pca955x_properties);
448
static const TypeInfo pca955x_info = {
449
.name = TYPE_PCA955X,
450
.parent = TYPE_I2C_SLAVE,
451
.instance_init = pca955x_initfn,
452
.instance_size = sizeof(PCA955xState),
453
.class_init = pca955x_class_init,
454
.class_size = sizeof(PCA955xClass),
458
static void pca9552_class_init(ObjectClass *oc, void *data)
460
DeviceClass *dc = DEVICE_CLASS(oc);
461
PCA955xClass *pc = PCA955X_CLASS(oc);
463
dc->reset = pca9552_reset;
464
dc->vmsd = &pca9552_vmstate;
465
pc->max_reg = PCA9552_LS3;
469
static const TypeInfo pca9552_info = {
470
.name = TYPE_PCA9552,
471
.parent = TYPE_PCA955X,
472
.class_init = pca9552_class_init,
475
static void pca955x_register_types(void)
477
type_register_static(&pca955x_info);
478
type_register_static(&pca9552_info);
481
type_init(pca955x_register_types)