xref: /qemu/hw/gpio/pca9552.c (revision e8c5503a)
1 /*
2  * PCA9552 I2C LED blinker
3  *
4  *     https://www.nxp.com/docs/en/application-note/AN264.pdf
5  *
6  * Copyright (c) 2017-2018, IBM Corporation.
7  * Copyright (c) 2020 Philippe Mathieu-Daudé
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2 or
10  * later. See the COPYING file in the top-level directory.
11  */
12 
13 #include "qemu/osdep.h"
14 #include "qemu/log.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"
20 #include "hw/irq.h"
21 #include "migration/vmstate.h"
22 #include "qapi/error.h"
23 #include "qapi/visitor.h"
24 #include "trace.h"
25 #include "qom/object.h"
26 
27 struct PCA955xClass {
28     /*< private >*/
29     I2CSlaveClass parent_class;
30     /*< public >*/
31 
32     uint8_t pin_count;
33     uint8_t max_reg;
34 };
35 typedef struct PCA955xClass PCA955xClass;
36 
37 DECLARE_CLASS_CHECKERS(PCA955xClass, PCA955X,
38                        TYPE_PCA955X)
39 /*
40  * Note:  The LED_ON and LED_OFF configuration values for the PCA955X
41  *        chips are the reverse of the PCA953X family of chips.
42  */
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
49 
50 static const char *led_state[] = {"on", "off", "pwm0", "pwm1"};
51 
52 static uint8_t pca955x_pin_get_config(PCA955xState *s, int pin)
53 {
54     uint8_t reg   = PCA9552_LS0 + (pin / 4);
55     uint8_t shift = (pin % 4) << 1;
56 
57     return extract32(s->regs[reg], shift, 2);
58 }
59 
60 /* Return INPUT status (bit #N belongs to GPIO #N) */
61 static uint16_t pca955x_pins_get_status(PCA955xState *s)
62 {
63     return (s->regs[PCA9552_INPUT1] << 8) | s->regs[PCA9552_INPUT0];
64 }
65 
66 static void pca955x_display_pins_status(PCA955xState *s,
67                                         uint16_t previous_pins_status)
68 {
69     PCA955xClass *k = PCA955X_GET_CLASS(s);
70     uint16_t pins_status, pins_changed;
71     int i;
72 
73     pins_status = pca955x_pins_get_status(s);
74     pins_changed = previous_pins_status ^ pins_status;
75     if (!pins_changed) {
76         return;
77     }
78     if (trace_event_get_state_backends(TRACE_PCA955X_GPIO_STATUS)) {
79         char *buf = g_newa(char, k->pin_count + 1);
80 
81         for (i = 0; i < k->pin_count; i++) {
82             if (extract32(pins_status, i, 1)) {
83                 buf[i] = '*';
84             } else {
85                 buf[i] = '.';
86             }
87         }
88         buf[i] = '\0';
89         trace_pca955x_gpio_status(s->description, buf);
90     }
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);
95 
96                 /*
97                  * We display the state using the PCA logic ("active-high").
98                  * This is not the state of the LED, which signal might be
99                  * wired "active-low" on the board.
100                  */
101                 trace_pca955x_gpio_change(s->description, i,
102                                           !new_state, new_state);
103             }
104         }
105     }
106 }
107 
108 static void pca955x_update_pin_input(PCA955xState *s)
109 {
110     PCA955xClass *k = PCA955X_GET_CLASS(s);
111     int i;
112 
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;
118         uint8_t new_value;
119 
120         switch (config) {
121         case PCA9552_LED_ON:
122             /* Pin is set to 0V to turn on LED */
123             s->regs[input_reg] &= ~bit_mask;
124             break;
125         case PCA9552_LED_OFF:
126             /*
127              * Pin is set to Hi-Z to turn off LED and
128              * pullup sets it to a logical 1 unless
129              * external device drives it low.
130              */
131             if (s->ext_state[i] == PCA9552_PIN_LOW) {
132                 s->regs[input_reg] &= ~bit_mask;
133             } else {
134                 s->regs[input_reg] |=  bit_mask;
135             }
136             break;
137         case PCA9552_LED_PWM0:
138         case PCA9552_LED_PWM1:
139             /* TODO */
140         default:
141             break;
142         }
143 
144         /* update irq state only if pin state changed */
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);
148         }
149     }
150 }
151 
152 static uint8_t pca955x_read(PCA955xState *s, uint8_t reg)
153 {
154     switch (reg) {
155     case PCA9552_INPUT0:
156     case PCA9552_INPUT1:
157     case PCA9552_PSC0:
158     case PCA9552_PWM0:
159     case PCA9552_PSC1:
160     case PCA9552_PWM1:
161     case PCA9552_LS0:
162     case PCA9552_LS1:
163     case PCA9552_LS2:
164     case PCA9552_LS3:
165         return s->regs[reg];
166     default:
167         qemu_log_mask(LOG_GUEST_ERROR, "%s: unexpected read to register %d\n",
168                       __func__, reg);
169         return 0xFF;
170     }
171 }
172 
173 static void pca955x_write(PCA955xState *s, uint8_t reg, uint8_t data)
174 {
175     uint16_t pins_status;
176 
177     switch (reg) {
178     case PCA9552_PSC0:
179     case PCA9552_PWM0:
180     case PCA9552_PSC1:
181     case PCA9552_PWM1:
182         s->regs[reg] = data;
183         break;
184 
185     case PCA9552_LS0:
186     case PCA9552_LS1:
187     case PCA9552_LS2:
188     case PCA9552_LS3:
189         pins_status = pca955x_pins_get_status(s);
190         s->regs[reg] = data;
191         pca955x_update_pin_input(s);
192         pca955x_display_pins_status(s, pins_status);
193         break;
194 
195     case PCA9552_INPUT0:
196     case PCA9552_INPUT1:
197     default:
198         qemu_log_mask(LOG_GUEST_ERROR, "%s: unexpected write to register %d\n",
199                       __func__, reg);
200     }
201 }
202 
203 /*
204  * When Auto-Increment is on, the register address is incremented
205  * after each byte is sent to or received by the device. The index
206  * rollovers to 0 when the maximum register address is reached.
207  */
208 static void pca955x_autoinc(PCA955xState *s)
209 {
210     PCA955xClass *k = PCA955X_GET_CLASS(s);
211 
212     if (s->pointer != 0xFF && s->pointer & PCA9552_AUTOINC) {
213         uint8_t reg = s->pointer & 0xf;
214 
215         reg = (reg + 1) % (k->max_reg + 1);
216         s->pointer = reg | PCA9552_AUTOINC;
217     }
218 }
219 
220 static uint8_t pca955x_recv(I2CSlave *i2c)
221 {
222     PCA955xState *s = PCA955X(i2c);
223     uint8_t ret;
224 
225     ret = pca955x_read(s, s->pointer & 0xf);
226 
227     /*
228      * From the Specs:
229      *
230      *     Important Note: When a Read sequence is initiated and the
231      *     AI bit is set to Logic Level 1, the Read Sequence MUST
232      *     start by a register different from 0.
233      *
234      * I don't know what should be done in this case, so throw an
235      * error.
236      */
237     if (s->pointer == PCA9552_AUTOINC) {
238         qemu_log_mask(LOG_GUEST_ERROR,
239                       "%s: Autoincrement read starting with register 0\n",
240                       __func__);
241     }
242 
243     pca955x_autoinc(s);
244 
245     return ret;
246 }
247 
248 static int pca955x_send(I2CSlave *i2c, uint8_t data)
249 {
250     PCA955xState *s = PCA955X(i2c);
251 
252     /* First byte sent by is the register address */
253     if (s->len == 0) {
254         s->pointer = data;
255         s->len++;
256     } else {
257         pca955x_write(s, s->pointer & 0xf, data);
258 
259         pca955x_autoinc(s);
260     }
261 
262     return 0;
263 }
264 
265 static int pca955x_event(I2CSlave *i2c, enum i2c_event event)
266 {
267     PCA955xState *s = PCA955X(i2c);
268 
269     s->len = 0;
270     return 0;
271 }
272 
273 static void pca955x_get_led(Object *obj, Visitor *v, const char *name,
274                             void *opaque, Error **errp)
275 {
276     PCA955xClass *k = PCA955X_GET_CLASS(obj);
277     PCA955xState *s = PCA955X(obj);
278     int led, rc, reg;
279     uint8_t state;
280 
281     rc = sscanf(name, "led%2d", &led);
282     if (rc != 1) {
283         error_setg(errp, "%s: error reading %s", __func__, name);
284         return;
285     }
286     if (led < 0 || led > k->pin_count) {
287         error_setg(errp, "%s invalid led %s", __func__, name);
288         return;
289     }
290     /*
291      * Get the LSx register as the qom interface should expose the device
292      * state, not the modeled 'input line' behaviour which would come from
293      * reading the INPUTx reg
294      */
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);
298 }
299 
300 /*
301  * Return an LED selector register value based on an existing one, with
302  * the appropriate 2-bit state value set for the given LED number (0-3).
303  */
304 static inline uint8_t pca955x_ledsel(uint8_t oldval, int led_num, int state)
305 {
306         return (oldval & (~(0x3 << (led_num << 1)))) |
307                 ((state & 0x3) << (led_num << 1));
308 }
309 
310 static void pca955x_set_led(Object *obj, Visitor *v, const char *name,
311                             void *opaque, Error **errp)
312 {
313     PCA955xClass *k = PCA955X_GET_CLASS(obj);
314     PCA955xState *s = PCA955X(obj);
315     int led, rc, reg, val;
316     uint8_t state;
317     char *state_str;
318 
319     if (!visit_type_str(v, name, &state_str, errp)) {
320         return;
321     }
322     rc = sscanf(name, "led%2d", &led);
323     if (rc != 1) {
324         error_setg(errp, "%s: error reading %s", __func__, name);
325         return;
326     }
327     if (led < 0 || led > k->pin_count) {
328         error_setg(errp, "%s invalid led %s", __func__, name);
329         return;
330     }
331 
332     for (state = 0; state < ARRAY_SIZE(led_state); state++) {
333         if (!strcmp(state_str, led_state[state])) {
334             break;
335         }
336     }
337     if (state >= ARRAY_SIZE(led_state)) {
338         error_setg(errp, "%s invalid led state %s", __func__, state_str);
339         return;
340     }
341 
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);
346 }
347 
348 static const VMStateDescription pca9552_vmstate = {
349     .name = "PCA9552",
350     .version_id = 0,
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()
359     }
360 };
361 
362 static void pca9552_reset(DeviceState *dev)
363 {
364     PCA955xState *s = PCA955X(dev);
365 
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; /* all OFF */
371     s->regs[PCA9552_LS1] = 0x55;
372     s->regs[PCA9552_LS2] = 0x55;
373     s->regs[PCA9552_LS3] = 0x55;
374 
375     memset(s->ext_state, PCA9552_PIN_HIZ, PCA955X_PIN_COUNT_MAX);
376     pca955x_update_pin_input(s);
377 
378     s->pointer = 0xFF;
379     s->len = 0;
380 }
381 
382 static void pca955x_initfn(Object *obj)
383 {
384     PCA955xClass *k = PCA955X_GET_CLASS(obj);
385     int led;
386 
387     assert(k->pin_count <= PCA955X_PIN_COUNT_MAX);
388     for (led = 0; led < k->pin_count; led++) {
389         char *name;
390 
391         name = g_strdup_printf("led%d", led);
392         object_property_add(obj, name, "bool", pca955x_get_led, pca955x_set_led,
393                             NULL, NULL);
394         g_free(name);
395     }
396 }
397 
398 static void pca955x_set_ext_state(PCA955xState *s, int pin, int level)
399 {
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);
405     }
406 }
407 
408 static void pca955x_gpio_in_handler(void *opaque, int pin, int level)
409 {
410 
411     PCA955xState *s = PCA955X(opaque);
412     PCA955xClass *k = PCA955X_GET_CLASS(s);
413 
414     assert((pin >= 0) && (pin < k->pin_count));
415     pca955x_set_ext_state(s, pin, level);
416 }
417 
418 static void pca955x_realize(DeviceState *dev, Error **errp)
419 {
420     PCA955xClass *k = PCA955X_GET_CLASS(dev);
421     PCA955xState *s = PCA955X(dev);
422 
423     if (!s->description) {
424         s->description = g_strdup("pca-unspecified");
425     }
426 
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);
429 }
430 
431 static Property pca955x_properties[] = {
432     DEFINE_PROP_STRING("description", PCA955xState, description),
433     DEFINE_PROP_END_OF_LIST(),
434 };
435 
436 static void pca955x_class_init(ObjectClass *klass, void *data)
437 {
438     DeviceClass *dc = DEVICE_CLASS(klass);
439     I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
440 
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);
446 }
447 
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),
455     .abstract      = true,
456 };
457 
458 static void pca9552_class_init(ObjectClass *oc, void *data)
459 {
460     DeviceClass *dc = DEVICE_CLASS(oc);
461     PCA955xClass *pc = PCA955X_CLASS(oc);
462 
463     dc->reset = pca9552_reset;
464     dc->vmsd = &pca9552_vmstate;
465     pc->max_reg = PCA9552_LS3;
466     pc->pin_count = 16;
467 }
468 
469 static const TypeInfo pca9552_info = {
470     .name          = TYPE_PCA9552,
471     .parent        = TYPE_PCA955X,
472     .class_init    = pca9552_class_init,
473 };
474 
475 static void pca955x_register_types(void)
476 {
477     type_register_static(&pca955x_info);
478     type_register_static(&pca9552_info);
479 }
480 
481 type_init(pca955x_register_types)
482