2 * PCA9552 I2C LED blinker
4 * https://www.nxp.com/docs/en/application-note/AN264.pdf
6 * Copyright (c) 2017-2018, IBM Corporation.
8 * This work is licensed under the terms of the GNU GPL, version 2 or
9 * later. See the COPYING file in the top-level directory.
12 #include "qemu/osdep.h"
14 #include "qemu/module.h"
16 #include "hw/misc/pca9552.h"
17 #include "hw/misc/pca9552_regs.h"
18 #include "migration/vmstate.h"
20 #define PCA9552_LED_ON 0x0
21 #define PCA9552_LED_OFF 0x1
22 #define PCA9552_LED_PWM0 0x2
23 #define PCA9552_LED_PWM1 0x3
25 static uint8_t pca9552_pin_get_config(PCA9552State
*s
, int pin
)
27 uint8_t reg
= PCA9552_LS0
+ (pin
/ 4);
28 uint8_t shift
= (pin
% 4) << 1;
30 return extract32(s
->regs
[reg
], shift
, 2);
33 static void pca9552_update_pin_input(PCA9552State
*s
)
37 for (i
= 0; i
< s
->nr_leds
; i
++) {
38 uint8_t input_reg
= PCA9552_INPUT0
+ (i
/ 8);
39 uint8_t input_shift
= (i
% 8);
40 uint8_t config
= pca9552_pin_get_config(s
, i
);
44 s
->regs
[input_reg
] |= 1 << input_shift
;
47 s
->regs
[input_reg
] &= ~(1 << input_shift
);
49 case PCA9552_LED_PWM0
:
50 case PCA9552_LED_PWM1
:
58 static uint8_t pca9552_read(PCA9552State
*s
, uint8_t reg
)
73 qemu_log_mask(LOG_GUEST_ERROR
, "%s: unexpected read to register %d\n",
79 static void pca9552_write(PCA9552State
*s
, uint8_t reg
, uint8_t data
)
94 pca9552_update_pin_input(s
);
100 qemu_log_mask(LOG_GUEST_ERROR
, "%s: unexpected write to register %d\n",
106 * When Auto-Increment is on, the register address is incremented
107 * after each byte is sent to or received by the device. The index
108 * rollovers to 0 when the maximum register address is reached.
110 static void pca9552_autoinc(PCA9552State
*s
)
112 if (s
->pointer
!= 0xFF && s
->pointer
& PCA9552_AUTOINC
) {
113 uint8_t reg
= s
->pointer
& 0xf;
115 reg
= (reg
+ 1) % (s
->max_reg
+ 1);
116 s
->pointer
= reg
| PCA9552_AUTOINC
;
120 static uint8_t pca9552_recv(I2CSlave
*i2c
)
122 PCA9552State
*s
= PCA9552(i2c
);
125 ret
= pca9552_read(s
, s
->pointer
& 0xf);
130 * Important Note: When a Read sequence is initiated and the
131 * AI bit is set to Logic Level 1, the Read Sequence MUST
132 * start by a register different from 0.
134 * I don't know what should be done in this case, so throw an
137 if (s
->pointer
== PCA9552_AUTOINC
) {
138 qemu_log_mask(LOG_GUEST_ERROR
,
139 "%s: Autoincrement read starting with register 0\n",
148 static int pca9552_send(I2CSlave
*i2c
, uint8_t data
)
150 PCA9552State
*s
= PCA9552(i2c
);
152 /* First byte sent by is the register address */
157 pca9552_write(s
, s
->pointer
& 0xf, data
);
165 static int pca9552_event(I2CSlave
*i2c
, enum i2c_event event
)
167 PCA9552State
*s
= PCA9552(i2c
);
173 static const VMStateDescription pca9552_vmstate
= {
176 .minimum_version_id
= 0,
177 .fields
= (VMStateField
[]) {
178 VMSTATE_UINT8(len
, PCA9552State
),
179 VMSTATE_UINT8(pointer
, PCA9552State
),
180 VMSTATE_UINT8_ARRAY(regs
, PCA9552State
, PCA9552_NR_REGS
),
181 VMSTATE_I2C_SLAVE(i2c
, PCA9552State
),
182 VMSTATE_END_OF_LIST()
186 static void pca9552_reset(DeviceState
*dev
)
188 PCA9552State
*s
= PCA9552(dev
);
190 s
->regs
[PCA9552_PSC0
] = 0xFF;
191 s
->regs
[PCA9552_PWM0
] = 0x80;
192 s
->regs
[PCA9552_PSC1
] = 0xFF;
193 s
->regs
[PCA9552_PWM1
] = 0x80;
194 s
->regs
[PCA9552_LS0
] = 0x55; /* all OFF */
195 s
->regs
[PCA9552_LS1
] = 0x55;
196 s
->regs
[PCA9552_LS2
] = 0x55;
197 s
->regs
[PCA9552_LS3
] = 0x55;
199 pca9552_update_pin_input(s
);
205 static void pca9552_initfn(Object
*obj
)
207 PCA9552State
*s
= PCA9552(obj
);
209 /* If support for the other PCA955X devices are implemented, these
210 * constant values might be part of class structure describing the
213 s
->max_reg
= PCA9552_LS3
;
217 static void pca9552_class_init(ObjectClass
*klass
, void *data
)
219 DeviceClass
*dc
= DEVICE_CLASS(klass
);
220 I2CSlaveClass
*k
= I2C_SLAVE_CLASS(klass
);
222 k
->event
= pca9552_event
;
223 k
->recv
= pca9552_recv
;
224 k
->send
= pca9552_send
;
225 dc
->reset
= pca9552_reset
;
226 dc
->vmsd
= &pca9552_vmstate
;
229 static const TypeInfo pca9552_info
= {
230 .name
= TYPE_PCA9552
,
231 .parent
= TYPE_I2C_SLAVE
,
232 .instance_init
= pca9552_initfn
,
233 .instance_size
= sizeof(PCA9552State
),
234 .class_init
= pca9552_class_init
,
237 static void pca9552_register_types(void)
239 type_register_static(&pca9552_info
);
242 type_init(pca9552_register_types
)