xref: /openbmc/qemu/hw/gpio/pca9554.c (revision 6328d8ff)
1 /*
2  * PCA9554 I/O port
3  *
4  * Copyright (c) 2023, IBM Corporation.
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  */
8 
9 #include "qemu/osdep.h"
10 #include "qemu/log.h"
11 #include "qemu/module.h"
12 #include "qemu/bitops.h"
13 #include "hw/qdev-properties.h"
14 #include "hw/gpio/pca9554.h"
15 #include "hw/gpio/pca9554_regs.h"
16 #include "hw/irq.h"
17 #include "migration/vmstate.h"
18 #include "qapi/error.h"
19 #include "qapi/visitor.h"
20 #include "trace.h"
21 #include "qom/object.h"
22 
23 struct PCA9554Class {
24     /*< private >*/
25     I2CSlaveClass parent_class;
26     /*< public >*/
27 };
28 typedef struct PCA9554Class PCA9554Class;
29 
30 DECLARE_CLASS_CHECKERS(PCA9554Class, PCA9554,
31                        TYPE_PCA9554)
32 
33 #define PCA9554_PIN_LOW  0x0
34 #define PCA9554_PIN_HIZ  0x1
35 
36 static const char *pin_state[] = {"low", "high"};
37 
pca9554_update_pin_input(PCA9554State * s)38 static void pca9554_update_pin_input(PCA9554State *s)
39 {
40     int i;
41     uint8_t config = s->regs[PCA9554_CONFIG];
42     uint8_t output = s->regs[PCA9554_OUTPUT];
43     uint8_t internal_state = config | output;
44 
45     for (i = 0; i < PCA9554_PIN_COUNT; i++) {
46         uint8_t bit_mask = 1 << i;
47         uint8_t internal_pin_state = (internal_state >> i) & 0x1;
48         uint8_t old_value = s->regs[PCA9554_INPUT] & bit_mask;
49         uint8_t new_value;
50 
51         switch (internal_pin_state) {
52         case PCA9554_PIN_LOW:
53             s->regs[PCA9554_INPUT] &= ~bit_mask;
54             break;
55         case PCA9554_PIN_HIZ:
56             /*
57              * pullup sets it to a logical 1 unless
58              * external device drives it low.
59              */
60             if (s->ext_state[i] == PCA9554_PIN_LOW) {
61                 s->regs[PCA9554_INPUT] &= ~bit_mask;
62             } else {
63                 s->regs[PCA9554_INPUT] |=  bit_mask;
64             }
65             break;
66         default:
67             break;
68         }
69 
70         /* update irq state only if pin state changed */
71         new_value = s->regs[PCA9554_INPUT] & bit_mask;
72         if (new_value != old_value) {
73             if (new_value) {
74                 /* changed from 0 to 1 */
75                 qemu_set_irq(s->gpio_out[i], 1);
76             } else {
77                 /* changed from 1 to 0 */
78                 qemu_set_irq(s->gpio_out[i], 0);
79             }
80         }
81     }
82 }
83 
pca9554_read(PCA9554State * s,uint8_t reg)84 static uint8_t pca9554_read(PCA9554State *s, uint8_t reg)
85 {
86     switch (reg) {
87     case PCA9554_INPUT:
88         return s->regs[PCA9554_INPUT] ^ s->regs[PCA9554_POLARITY];
89     case PCA9554_OUTPUT:
90     case PCA9554_POLARITY:
91     case PCA9554_CONFIG:
92         return s->regs[reg];
93     default:
94         qemu_log_mask(LOG_GUEST_ERROR, "%s: unexpected read to register %d\n",
95                       __func__, reg);
96         return 0xFF;
97     }
98 }
99 
pca9554_write(PCA9554State * s,uint8_t reg,uint8_t data)100 static void pca9554_write(PCA9554State *s, uint8_t reg, uint8_t data)
101 {
102     switch (reg) {
103     case PCA9554_OUTPUT:
104     case PCA9554_CONFIG:
105         s->regs[reg] = data;
106         pca9554_update_pin_input(s);
107         break;
108     case PCA9554_POLARITY:
109         s->regs[reg] = data;
110         break;
111     case PCA9554_INPUT:
112     default:
113         qemu_log_mask(LOG_GUEST_ERROR, "%s: unexpected write to register %d\n",
114                       __func__, reg);
115     }
116 }
117 
pca9554_recv(I2CSlave * i2c)118 static uint8_t pca9554_recv(I2CSlave *i2c)
119 {
120     PCA9554State *s = PCA9554(i2c);
121     uint8_t ret;
122 
123     ret = pca9554_read(s, s->pointer & 0x3);
124 
125     return ret;
126 }
127 
pca9554_send(I2CSlave * i2c,uint8_t data)128 static int pca9554_send(I2CSlave *i2c, uint8_t data)
129 {
130     PCA9554State *s = PCA9554(i2c);
131 
132     /* First byte sent by is the register address */
133     if (s->len == 0) {
134         s->pointer = data;
135         s->len++;
136     } else {
137         pca9554_write(s, s->pointer & 0x3, data);
138     }
139 
140     return 0;
141 }
142 
pca9554_event(I2CSlave * i2c,enum i2c_event event)143 static int pca9554_event(I2CSlave *i2c, enum i2c_event event)
144 {
145     PCA9554State *s = PCA9554(i2c);
146 
147     s->len = 0;
148     return 0;
149 }
150 
pca9554_get_pin(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)151 static void pca9554_get_pin(Object *obj, Visitor *v, const char *name,
152                             void *opaque, Error **errp)
153 {
154     PCA9554State *s = PCA9554(obj);
155     int pin, rc;
156     uint8_t state;
157 
158     rc = sscanf(name, "pin%2d", &pin);
159     if (rc != 1) {
160         error_setg(errp, "%s: error reading %s", __func__, name);
161         return;
162     }
163     if (pin < 0 || pin >= PCA9554_PIN_COUNT) {
164         error_setg(errp, "%s invalid pin %s", __func__, name);
165         return;
166     }
167 
168     state = pca9554_read(s, PCA9554_CONFIG);
169     state |= pca9554_read(s, PCA9554_OUTPUT);
170     state = (state >> pin) & 0x1;
171     visit_type_str(v, name, (char **)&pin_state[state], errp);
172 }
173 
pca9554_set_pin(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)174 static void pca9554_set_pin(Object *obj, Visitor *v, const char *name,
175                             void *opaque, Error **errp)
176 {
177     PCA9554State *s = PCA9554(obj);
178     int pin, rc, val;
179     uint8_t state, mask;
180     char *state_str;
181 
182     if (!visit_type_str(v, name, &state_str, errp)) {
183         return;
184     }
185     rc = sscanf(name, "pin%2d", &pin);
186     if (rc != 1) {
187         error_setg(errp, "%s: error reading %s", __func__, name);
188         return;
189     }
190     if (pin < 0 || pin >= PCA9554_PIN_COUNT) {
191         error_setg(errp, "%s invalid pin %s", __func__, name);
192         return;
193     }
194 
195     for (state = 0; state < ARRAY_SIZE(pin_state); state++) {
196         if (!strcmp(state_str, pin_state[state])) {
197             break;
198         }
199     }
200     if (state >= ARRAY_SIZE(pin_state)) {
201         error_setg(errp, "%s invalid pin state %s", __func__, state_str);
202         return;
203     }
204 
205     /* First, modify the output register bit */
206     val = pca9554_read(s, PCA9554_OUTPUT);
207     mask = 0x1 << pin;
208     if (state == PCA9554_PIN_LOW) {
209         val &= ~(mask);
210     } else {
211         val |= mask;
212     }
213     pca9554_write(s, PCA9554_OUTPUT, val);
214 
215     /* Then, clear the config register bit for output mode */
216     val = pca9554_read(s, PCA9554_CONFIG);
217     val &= ~mask;
218     pca9554_write(s, PCA9554_CONFIG, val);
219 }
220 
221 static const VMStateDescription pca9554_vmstate = {
222     .name = "PCA9554",
223     .version_id = 0,
224     .minimum_version_id = 0,
225     .fields = (VMStateField[]) {
226         VMSTATE_UINT8(len, PCA9554State),
227         VMSTATE_UINT8(pointer, PCA9554State),
228         VMSTATE_UINT8_ARRAY(regs, PCA9554State, PCA9554_NR_REGS),
229         VMSTATE_UINT8_ARRAY(ext_state, PCA9554State, PCA9554_PIN_COUNT),
230         VMSTATE_I2C_SLAVE(i2c, PCA9554State),
231         VMSTATE_END_OF_LIST()
232     }
233 };
234 
pca9554_reset(DeviceState * dev)235 static void pca9554_reset(DeviceState *dev)
236 {
237     PCA9554State *s = PCA9554(dev);
238 
239     s->regs[PCA9554_INPUT] = 0xFF;
240     s->regs[PCA9554_OUTPUT] = 0xFF;
241     s->regs[PCA9554_POLARITY] = 0x0; /* No pins are inverted */
242     s->regs[PCA9554_CONFIG] = 0xFF; /* All pins are inputs */
243 
244     memset(s->ext_state, PCA9554_PIN_HIZ, PCA9554_PIN_COUNT);
245     pca9554_update_pin_input(s);
246 
247     s->pointer = 0x0;
248     s->len = 0;
249 }
250 
pca9554_initfn(Object * obj)251 static void pca9554_initfn(Object *obj)
252 {
253     int pin;
254 
255     for (pin = 0; pin < PCA9554_PIN_COUNT; pin++) {
256         char *name;
257 
258         name = g_strdup_printf("pin%d", pin);
259         object_property_add(obj, name, "bool", pca9554_get_pin, pca9554_set_pin,
260                             NULL, NULL);
261         g_free(name);
262     }
263 }
264 
pca9554_set_ext_state(PCA9554State * s,int pin,int level)265 static void pca9554_set_ext_state(PCA9554State *s, int pin, int level)
266 {
267     if (s->ext_state[pin] != level) {
268         s->ext_state[pin] = level;
269         pca9554_update_pin_input(s);
270     }
271 }
272 
pca9554_gpio_in_handler(void * opaque,int pin,int level)273 static void pca9554_gpio_in_handler(void *opaque, int pin, int level)
274 {
275 
276     PCA9554State *s = PCA9554(opaque);
277 
278     assert((pin >= 0) && (pin < PCA9554_PIN_COUNT));
279     pca9554_set_ext_state(s, pin, level);
280 }
281 
pca9554_realize(DeviceState * dev,Error ** errp)282 static void pca9554_realize(DeviceState *dev, Error **errp)
283 {
284     PCA9554State *s = PCA9554(dev);
285 
286     if (!s->description) {
287         s->description = g_strdup("pca9554");
288     }
289 
290     qdev_init_gpio_out(dev, s->gpio_out, PCA9554_PIN_COUNT);
291     qdev_init_gpio_in(dev, pca9554_gpio_in_handler, PCA9554_PIN_COUNT);
292 }
293 
294 static Property pca9554_properties[] = {
295     DEFINE_PROP_STRING("description", PCA9554State, description),
296     DEFINE_PROP_END_OF_LIST(),
297 };
298 
pca9554_class_init(ObjectClass * klass,void * data)299 static void pca9554_class_init(ObjectClass *klass, void *data)
300 {
301     DeviceClass *dc = DEVICE_CLASS(klass);
302     I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
303 
304     k->event = pca9554_event;
305     k->recv = pca9554_recv;
306     k->send = pca9554_send;
307     dc->realize = pca9554_realize;
308     dc->reset = pca9554_reset;
309     dc->vmsd = &pca9554_vmstate;
310     device_class_set_props(dc, pca9554_properties);
311 }
312 
313 static const TypeInfo pca9554_info = {
314     .name          = TYPE_PCA9554,
315     .parent        = TYPE_I2C_SLAVE,
316     .instance_init = pca9554_initfn,
317     .instance_size = sizeof(PCA9554State),
318     .class_init    = pca9554_class_init,
319     .class_size    = sizeof(PCA9554Class),
320     .abstract      = false,
321 };
322 
pca9554_register_types(void)323 static void pca9554_register_types(void)
324 {
325     type_register_static(&pca9554_info);
326 }
327 
328 type_init(pca9554_register_types)
329