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"
15 #include "hw/misc/pca9552.h"
16 #include "hw/misc/pca9552_regs.h"
17 #include "migration/vmstate.h"
18 #include "qapi/error.h"
19 #include "qapi/visitor.h"
21 #define PCA9552_LED_ON 0x0
22 #define PCA9552_LED_OFF 0x1
23 #define PCA9552_LED_PWM0 0x2
24 #define PCA9552_LED_PWM1 0x3
26 static const char *led_state
[] = {"on", "off", "pwm0", "pwm1"};
28 static uint8_t pca9552_pin_get_config(PCA9552State
*s
, int pin
)
30 uint8_t reg
= PCA9552_LS0
+ (pin
/ 4);
31 uint8_t shift
= (pin
% 4) << 1;
33 return extract32(s
->regs
[reg
], shift
, 2);
36 static void pca9552_update_pin_input(PCA9552State
*s
)
40 for (i
= 0; i
< s
->nr_leds
; i
++) {
41 uint8_t input_reg
= PCA9552_INPUT0
+ (i
/ 8);
42 uint8_t input_shift
= (i
% 8);
43 uint8_t config
= pca9552_pin_get_config(s
, i
);
47 s
->regs
[input_reg
] |= 1 << input_shift
;
50 s
->regs
[input_reg
] &= ~(1 << input_shift
);
52 case PCA9552_LED_PWM0
:
53 case PCA9552_LED_PWM1
:
61 static uint8_t pca9552_read(PCA9552State
*s
, uint8_t reg
)
76 qemu_log_mask(LOG_GUEST_ERROR
, "%s: unexpected read to register %d\n",
82 static void pca9552_write(PCA9552State
*s
, uint8_t reg
, uint8_t data
)
97 pca9552_update_pin_input(s
);
103 qemu_log_mask(LOG_GUEST_ERROR
, "%s: unexpected write to register %d\n",
109 * When Auto-Increment is on, the register address is incremented
110 * after each byte is sent to or received by the device. The index
111 * rollovers to 0 when the maximum register address is reached.
113 static void pca9552_autoinc(PCA9552State
*s
)
115 if (s
->pointer
!= 0xFF && s
->pointer
& PCA9552_AUTOINC
) {
116 uint8_t reg
= s
->pointer
& 0xf;
118 reg
= (reg
+ 1) % (s
->max_reg
+ 1);
119 s
->pointer
= reg
| PCA9552_AUTOINC
;
123 static uint8_t pca9552_recv(I2CSlave
*i2c
)
125 PCA9552State
*s
= PCA9552(i2c
);
128 ret
= pca9552_read(s
, s
->pointer
& 0xf);
133 * Important Note: When a Read sequence is initiated and the
134 * AI bit is set to Logic Level 1, the Read Sequence MUST
135 * start by a register different from 0.
137 * I don't know what should be done in this case, so throw an
140 if (s
->pointer
== PCA9552_AUTOINC
) {
141 qemu_log_mask(LOG_GUEST_ERROR
,
142 "%s: Autoincrement read starting with register 0\n",
151 static int pca9552_send(I2CSlave
*i2c
, uint8_t data
)
153 PCA9552State
*s
= PCA9552(i2c
);
155 /* First byte sent by is the register address */
160 pca9552_write(s
, s
->pointer
& 0xf, data
);
168 static int pca9552_event(I2CSlave
*i2c
, enum i2c_event event
)
170 PCA9552State
*s
= PCA9552(i2c
);
176 static void pca9552_get_led(Object
*obj
, Visitor
*v
, const char *name
,
177 void *opaque
, Error
**errp
)
179 PCA9552State
*s
= PCA9552(obj
);
183 rc
= sscanf(name
, "led%2d", &led
);
185 error_setg(errp
, "%s: error reading %s", __func__
, name
);
188 if (led
< 0 || led
> s
->nr_leds
) {
189 error_setg(errp
, "%s invalid led %s", __func__
, name
);
193 * Get the LSx register as the qom interface should expose the device
194 * state, not the modeled 'input line' behaviour which would come from
195 * reading the INPUTx reg
197 reg
= PCA9552_LS0
+ led
/ 4;
198 state
= (pca9552_read(s
, reg
) >> (led
% 8)) & 0x3;
199 visit_type_str(v
, name
, (char **)&led_state
[state
], errp
);
203 * Return an LED selector register value based on an existing one, with
204 * the appropriate 2-bit state value set for the given LED number (0-3).
206 static inline uint8_t pca955x_ledsel(uint8_t oldval
, int led_num
, int state
)
208 return (oldval
& (~(0x3 << (led_num
<< 1)))) |
209 ((state
& 0x3) << (led_num
<< 1));
212 static void pca9552_set_led(Object
*obj
, Visitor
*v
, const char *name
,
213 void *opaque
, Error
**errp
)
215 PCA9552State
*s
= PCA9552(obj
);
216 Error
*local_err
= NULL
;
217 int led
, rc
, reg
, val
;
221 visit_type_str(v
, name
, &state_str
, &local_err
);
223 error_propagate(errp
, local_err
);
226 rc
= sscanf(name
, "led%2d", &led
);
228 error_setg(errp
, "%s: error reading %s", __func__
, name
);
231 if (led
< 0 || led
> s
->nr_leds
) {
232 error_setg(errp
, "%s invalid led %s", __func__
, name
);
236 for (state
= 0; state
< ARRAY_SIZE(led_state
); state
++) {
237 if (!strcmp(state_str
, led_state
[state
])) {
241 if (state
>= ARRAY_SIZE(led_state
)) {
242 error_setg(errp
, "%s invalid led state %s", __func__
, state_str
);
246 reg
= PCA9552_LS0
+ led
/ 4;
247 val
= pca9552_read(s
, reg
);
248 val
= pca955x_ledsel(val
, led
% 4, state
);
249 pca9552_write(s
, reg
, val
);
252 static const VMStateDescription pca9552_vmstate
= {
255 .minimum_version_id
= 0,
256 .fields
= (VMStateField
[]) {
257 VMSTATE_UINT8(len
, PCA9552State
),
258 VMSTATE_UINT8(pointer
, PCA9552State
),
259 VMSTATE_UINT8_ARRAY(regs
, PCA9552State
, PCA9552_NR_REGS
),
260 VMSTATE_I2C_SLAVE(i2c
, PCA9552State
),
261 VMSTATE_END_OF_LIST()
265 static void pca9552_reset(DeviceState
*dev
)
267 PCA9552State
*s
= PCA9552(dev
);
269 s
->regs
[PCA9552_PSC0
] = 0xFF;
270 s
->regs
[PCA9552_PWM0
] = 0x80;
271 s
->regs
[PCA9552_PSC1
] = 0xFF;
272 s
->regs
[PCA9552_PWM1
] = 0x80;
273 s
->regs
[PCA9552_LS0
] = 0x55; /* all OFF */
274 s
->regs
[PCA9552_LS1
] = 0x55;
275 s
->regs
[PCA9552_LS2
] = 0x55;
276 s
->regs
[PCA9552_LS3
] = 0x55;
278 pca9552_update_pin_input(s
);
284 static void pca9552_initfn(Object
*obj
)
286 PCA9552State
*s
= PCA9552(obj
);
289 /* If support for the other PCA955X devices are implemented, these
290 * constant values might be part of class structure describing the
293 s
->max_reg
= PCA9552_LS3
;
296 for (led
= 0; led
< s
->nr_leds
; led
++) {
299 name
= g_strdup_printf("led%d", led
);
300 object_property_add(obj
, name
, "bool", pca9552_get_led
, pca9552_set_led
,
306 static void pca9552_class_init(ObjectClass
*klass
, void *data
)
308 DeviceClass
*dc
= DEVICE_CLASS(klass
);
309 I2CSlaveClass
*k
= I2C_SLAVE_CLASS(klass
);
311 k
->event
= pca9552_event
;
312 k
->recv
= pca9552_recv
;
313 k
->send
= pca9552_send
;
314 dc
->reset
= pca9552_reset
;
315 dc
->vmsd
= &pca9552_vmstate
;
318 static const TypeInfo pca9552_info
= {
319 .name
= TYPE_PCA9552
,
320 .parent
= TYPE_I2C_SLAVE
,
321 .instance_init
= pca9552_initfn
,
322 .instance_size
= sizeof(PCA9552State
),
323 .class_init
= pca9552_class_init
,
326 static void pca9552_register_types(void)
328 type_register_static(&pca9552_info
);
331 type_init(pca9552_register_types
)