linux-user, mips64: add syscall table generation support
[qemu/ar7.git] / hw / misc / pca9552.c
blobefd961e04148b442b841ce674be283f31db08bcd
1 /*
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"
13 #include "qemu/log.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)
38 int i;
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);
45 switch (config) {
46 case PCA9552_LED_ON:
47 s->regs[input_reg] |= 1 << input_shift;
48 break;
49 case PCA9552_LED_OFF:
50 s->regs[input_reg] &= ~(1 << input_shift);
51 break;
52 case PCA9552_LED_PWM0:
53 case PCA9552_LED_PWM1:
54 /* TODO */
55 default:
56 break;
61 static uint8_t pca9552_read(PCA9552State *s, uint8_t reg)
63 switch (reg) {
64 case PCA9552_INPUT0:
65 case PCA9552_INPUT1:
66 case PCA9552_PSC0:
67 case PCA9552_PWM0:
68 case PCA9552_PSC1:
69 case PCA9552_PWM1:
70 case PCA9552_LS0:
71 case PCA9552_LS1:
72 case PCA9552_LS2:
73 case PCA9552_LS3:
74 return s->regs[reg];
75 default:
76 qemu_log_mask(LOG_GUEST_ERROR, "%s: unexpected read to register %d\n",
77 __func__, reg);
78 return 0xFF;
82 static void pca9552_write(PCA9552State *s, uint8_t reg, uint8_t data)
84 switch (reg) {
85 case PCA9552_PSC0:
86 case PCA9552_PWM0:
87 case PCA9552_PSC1:
88 case PCA9552_PWM1:
89 s->regs[reg] = data;
90 break;
92 case PCA9552_LS0:
93 case PCA9552_LS1:
94 case PCA9552_LS2:
95 case PCA9552_LS3:
96 s->regs[reg] = data;
97 pca9552_update_pin_input(s);
98 break;
100 case PCA9552_INPUT0:
101 case PCA9552_INPUT1:
102 default:
103 qemu_log_mask(LOG_GUEST_ERROR, "%s: unexpected write to register %d\n",
104 __func__, reg);
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);
126 uint8_t ret;
128 ret = pca9552_read(s, s->pointer & 0xf);
131 * From the Specs:
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
138 * error.
140 if (s->pointer == PCA9552_AUTOINC) {
141 qemu_log_mask(LOG_GUEST_ERROR,
142 "%s: Autoincrement read starting with register 0\n",
143 __func__);
146 pca9552_autoinc(s);
148 return ret;
151 static int pca9552_send(I2CSlave *i2c, uint8_t data)
153 PCA9552State *s = PCA9552(i2c);
155 /* First byte sent by is the register address */
156 if (s->len == 0) {
157 s->pointer = data;
158 s->len++;
159 } else {
160 pca9552_write(s, s->pointer & 0xf, data);
162 pca9552_autoinc(s);
165 return 0;
168 static int pca9552_event(I2CSlave *i2c, enum i2c_event event)
170 PCA9552State *s = PCA9552(i2c);
172 s->len = 0;
173 return 0;
176 static void pca9552_get_led(Object *obj, Visitor *v, const char *name,
177 void *opaque, Error **errp)
179 PCA9552State *s = PCA9552(obj);
180 int led, rc, reg;
181 uint8_t state;
183 rc = sscanf(name, "led%2d", &led);
184 if (rc != 1) {
185 error_setg(errp, "%s: error reading %s", __func__, name);
186 return;
188 if (led < 0 || led > s->nr_leds) {
189 error_setg(errp, "%s invalid led %s", __func__, name);
190 return;
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;
218 uint8_t state;
219 char *state_str;
221 visit_type_str(v, name, &state_str, &local_err);
222 if (local_err) {
223 error_propagate(errp, local_err);
224 return;
226 rc = sscanf(name, "led%2d", &led);
227 if (rc != 1) {
228 error_setg(errp, "%s: error reading %s", __func__, name);
229 return;
231 if (led < 0 || led > s->nr_leds) {
232 error_setg(errp, "%s invalid led %s", __func__, name);
233 return;
236 for (state = 0; state < ARRAY_SIZE(led_state); state++) {
237 if (!strcmp(state_str, led_state[state])) {
238 break;
241 if (state >= ARRAY_SIZE(led_state)) {
242 error_setg(errp, "%s invalid led state %s", __func__, state_str);
243 return;
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 = {
253 .name = "PCA9552",
254 .version_id = 0,
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);
280 s->pointer = 0xFF;
281 s->len = 0;
284 static void pca9552_initfn(Object *obj)
286 PCA9552State *s = PCA9552(obj);
287 int led;
289 /* If support for the other PCA955X devices are implemented, these
290 * constant values might be part of class structure describing the
291 * PCA955X device
293 s->max_reg = PCA9552_LS3;
294 s->nr_leds = 16;
296 for (led = 0; led < s->nr_leds; led++) {
297 char *name;
299 name = g_strdup_printf("led%d", led);
300 object_property_add(obj, name, "bool", pca9552_get_led, pca9552_set_led,
301 NULL, NULL, NULL);
302 g_free(name);
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)