xref: /openbmc/qemu/hw/i2c/i2c_mux_pca954x.c (revision be5e8563f737582276068c01f4dc4abfe484d0c3)
1065177eeSPatrick Venture /*
2065177eeSPatrick Venture  * I2C multiplexer for PCA954x series of I2C multiplexer/switch chips.
3065177eeSPatrick Venture  *
4065177eeSPatrick Venture  * Copyright 2021 Google LLC
5065177eeSPatrick Venture  *
6065177eeSPatrick Venture  * This program is free software; you can redistribute it and/or modify it
7065177eeSPatrick Venture  * under the terms of the GNU General Public License as published by the
8065177eeSPatrick Venture  * Free Software Foundation; either version 2 of the License, or
9065177eeSPatrick Venture  * (at your option) any later version.
10065177eeSPatrick Venture  *
11065177eeSPatrick Venture  * This program is distributed in the hope that it will be useful, but WITHOUT
12065177eeSPatrick Venture  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13065177eeSPatrick Venture  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14065177eeSPatrick Venture  * for more details.
15065177eeSPatrick Venture  */
16065177eeSPatrick Venture 
17065177eeSPatrick Venture #include "qemu/osdep.h"
18065177eeSPatrick Venture #include "qapi/error.h"
19065177eeSPatrick Venture #include "hw/i2c/i2c.h"
20065177eeSPatrick Venture #include "hw/i2c/i2c_mux_pca954x.h"
21065177eeSPatrick Venture #include "hw/i2c/smbus_slave.h"
22065177eeSPatrick Venture #include "hw/qdev-core.h"
23*29770e09SPatrick Venture #include "hw/qdev-properties.h"
24065177eeSPatrick Venture #include "hw/sysbus.h"
25065177eeSPatrick Venture #include "qemu/log.h"
26065177eeSPatrick Venture #include "qemu/module.h"
27065177eeSPatrick Venture #include "qemu/queue.h"
28065177eeSPatrick Venture #include "qom/object.h"
29065177eeSPatrick Venture #include "trace.h"
30065177eeSPatrick Venture 
31065177eeSPatrick Venture #define PCA9548_CHANNEL_COUNT 8
32065177eeSPatrick Venture #define PCA9546_CHANNEL_COUNT 4
33065177eeSPatrick Venture 
34065177eeSPatrick Venture /*
35065177eeSPatrick Venture  * struct Pca954xState - The pca954x state object.
36065177eeSPatrick Venture  * @control: The value written to the mux control.
37065177eeSPatrick Venture  * @channel: The set of i2c channel buses that act as channels which own the
38065177eeSPatrick Venture  * i2c children.
39065177eeSPatrick Venture  */
40065177eeSPatrick Venture typedef struct Pca954xState {
41065177eeSPatrick Venture     SMBusDevice parent;
42065177eeSPatrick Venture 
43065177eeSPatrick Venture     uint8_t control;
44065177eeSPatrick Venture 
45d8bdf979SPatrick Venture     bool enabled[PCA9548_CHANNEL_COUNT];
46d8bdf979SPatrick Venture     I2CBus *bus[PCA9548_CHANNEL_COUNT];
47*29770e09SPatrick Venture 
48*29770e09SPatrick Venture     char *name;
49065177eeSPatrick Venture } Pca954xState;
50065177eeSPatrick Venture 
51065177eeSPatrick Venture /*
52065177eeSPatrick Venture  * struct Pca954xClass - The pca954x class object.
53065177eeSPatrick Venture  * @nchans: The number of i2c channels this device has.
54065177eeSPatrick Venture  */
55065177eeSPatrick Venture typedef struct Pca954xClass {
56065177eeSPatrick Venture     SMBusDeviceClass parent;
57065177eeSPatrick Venture 
58065177eeSPatrick Venture     uint8_t nchans;
59065177eeSPatrick Venture } Pca954xClass;
60065177eeSPatrick Venture 
61065177eeSPatrick Venture #define TYPE_PCA954X "pca954x"
OBJECT_DECLARE_TYPE(Pca954xState,Pca954xClass,PCA954X)62065177eeSPatrick Venture OBJECT_DECLARE_TYPE(Pca954xState, Pca954xClass, PCA954X)
63065177eeSPatrick Venture 
64065177eeSPatrick Venture /*
65065177eeSPatrick Venture  * For each channel, if it's enabled, recursively call match on those children.
66065177eeSPatrick Venture  */
67065177eeSPatrick Venture static bool pca954x_match(I2CSlave *candidate, uint8_t address,
68065177eeSPatrick Venture                           bool broadcast,
69065177eeSPatrick Venture                           I2CNodeList *current_devs)
70065177eeSPatrick Venture {
71065177eeSPatrick Venture     Pca954xState *mux = PCA954X(candidate);
72065177eeSPatrick Venture     Pca954xClass *mc = PCA954X_GET_CLASS(mux);
73065177eeSPatrick Venture     int i;
74065177eeSPatrick Venture 
75065177eeSPatrick Venture     /* They are talking to the mux itself (or all devices enabled). */
76065177eeSPatrick Venture     if ((candidate->address == address) || broadcast) {
77b21e2380SMarkus Armbruster         I2CNode *node = g_new(struct I2CNode, 1);
78065177eeSPatrick Venture         node->elt = candidate;
79065177eeSPatrick Venture         QLIST_INSERT_HEAD(current_devs, node, next);
80065177eeSPatrick Venture         if (!broadcast) {
81065177eeSPatrick Venture             return true;
82065177eeSPatrick Venture         }
83065177eeSPatrick Venture     }
84065177eeSPatrick Venture 
85065177eeSPatrick Venture     for (i = 0; i < mc->nchans; i++) {
86d8bdf979SPatrick Venture         if (!mux->enabled[i]) {
87065177eeSPatrick Venture             continue;
88065177eeSPatrick Venture         }
89065177eeSPatrick Venture 
90d8bdf979SPatrick Venture         if (i2c_scan_bus(mux->bus[i], address, broadcast,
91065177eeSPatrick Venture                          current_devs)) {
92065177eeSPatrick Venture             if (!broadcast) {
93065177eeSPatrick Venture                 return true;
94065177eeSPatrick Venture             }
95065177eeSPatrick Venture         }
96065177eeSPatrick Venture     }
97065177eeSPatrick Venture 
98065177eeSPatrick Venture     /* If we arrived here we didn't find a match, return broadcast. */
99065177eeSPatrick Venture     return broadcast;
100065177eeSPatrick Venture }
101065177eeSPatrick Venture 
pca954x_enable_channel(Pca954xState * s,uint8_t enable_mask)102065177eeSPatrick Venture static void pca954x_enable_channel(Pca954xState *s, uint8_t enable_mask)
103065177eeSPatrick Venture {
104065177eeSPatrick Venture     Pca954xClass *mc = PCA954X_GET_CLASS(s);
105065177eeSPatrick Venture     int i;
106065177eeSPatrick Venture 
107065177eeSPatrick Venture     /*
108065177eeSPatrick Venture      * For each channel, check if their bit is set in enable_mask and if yes,
109065177eeSPatrick Venture      * enable it, otherwise disable, hide it.
110065177eeSPatrick Venture      */
111065177eeSPatrick Venture     for (i = 0; i < mc->nchans; i++) {
112065177eeSPatrick Venture         if (enable_mask & (1 << i)) {
113d8bdf979SPatrick Venture             s->enabled[i] = true;
114065177eeSPatrick Venture         } else {
115d8bdf979SPatrick Venture             s->enabled[i] = false;
116065177eeSPatrick Venture         }
117065177eeSPatrick Venture     }
118065177eeSPatrick Venture }
119065177eeSPatrick Venture 
pca954x_write(Pca954xState * s,uint8_t data)120065177eeSPatrick Venture static void pca954x_write(Pca954xState *s, uint8_t data)
121065177eeSPatrick Venture {
122065177eeSPatrick Venture     s->control = data;
123065177eeSPatrick Venture     pca954x_enable_channel(s, data);
124065177eeSPatrick Venture 
125065177eeSPatrick Venture     trace_pca954x_write_bytes(data);
126065177eeSPatrick Venture }
127065177eeSPatrick Venture 
pca954x_write_data(SMBusDevice * d,uint8_t * buf,uint8_t len)128065177eeSPatrick Venture static int pca954x_write_data(SMBusDevice *d, uint8_t *buf, uint8_t len)
129065177eeSPatrick Venture {
130065177eeSPatrick Venture     Pca954xState *s = PCA954X(d);
131065177eeSPatrick Venture 
132065177eeSPatrick Venture     if (len == 0) {
133065177eeSPatrick Venture         qemu_log_mask(LOG_GUEST_ERROR, "%s: writing empty data\n", __func__);
134065177eeSPatrick Venture         return -1;
135065177eeSPatrick Venture     }
136065177eeSPatrick Venture 
137065177eeSPatrick Venture     /*
138065177eeSPatrick Venture      * len should be 1, because they write one byte to enable/disable channels.
139065177eeSPatrick Venture      */
140065177eeSPatrick Venture     if (len > 1) {
141065177eeSPatrick Venture         qemu_log_mask(LOG_GUEST_ERROR,
142065177eeSPatrick Venture             "%s: extra data after channel selection mask\n",
143065177eeSPatrick Venture             __func__);
144065177eeSPatrick Venture         return -1;
145065177eeSPatrick Venture     }
146065177eeSPatrick Venture 
147065177eeSPatrick Venture     pca954x_write(s, buf[0]);
148065177eeSPatrick Venture     return 0;
149065177eeSPatrick Venture }
150065177eeSPatrick Venture 
pca954x_read_byte(SMBusDevice * d)151065177eeSPatrick Venture static uint8_t pca954x_read_byte(SMBusDevice *d)
152065177eeSPatrick Venture {
153065177eeSPatrick Venture     Pca954xState *s = PCA954X(d);
154065177eeSPatrick Venture     uint8_t data = s->control;
155065177eeSPatrick Venture     trace_pca954x_read_data(data);
156065177eeSPatrick Venture     return data;
157065177eeSPatrick Venture }
158065177eeSPatrick Venture 
pca954x_enter_reset(Object * obj,ResetType type)159065177eeSPatrick Venture static void pca954x_enter_reset(Object *obj, ResetType type)
160065177eeSPatrick Venture {
161065177eeSPatrick Venture     Pca954xState *s = PCA954X(obj);
162065177eeSPatrick Venture     /* Reset will disable all channels. */
163065177eeSPatrick Venture     pca954x_write(s, 0);
164065177eeSPatrick Venture }
165065177eeSPatrick Venture 
pca954x_i2c_get_bus(I2CSlave * mux,uint8_t channel)166065177eeSPatrick Venture I2CBus *pca954x_i2c_get_bus(I2CSlave *mux, uint8_t channel)
167065177eeSPatrick Venture {
168065177eeSPatrick Venture     Pca954xClass *pc = PCA954X_GET_CLASS(mux);
169065177eeSPatrick Venture     Pca954xState *pca954x = PCA954X(mux);
170065177eeSPatrick Venture 
171065177eeSPatrick Venture     g_assert(channel < pc->nchans);
172d8bdf979SPatrick Venture     return pca954x->bus[channel];
173065177eeSPatrick Venture }
174065177eeSPatrick Venture 
pca9546_class_init(ObjectClass * klass,void * data)175065177eeSPatrick Venture static void pca9546_class_init(ObjectClass *klass, void *data)
176065177eeSPatrick Venture {
177065177eeSPatrick Venture     Pca954xClass *s = PCA954X_CLASS(klass);
178065177eeSPatrick Venture     s->nchans = PCA9546_CHANNEL_COUNT;
179065177eeSPatrick Venture }
180065177eeSPatrick Venture 
pca9548_class_init(ObjectClass * klass,void * data)181065177eeSPatrick Venture static void pca9548_class_init(ObjectClass *klass, void *data)
182065177eeSPatrick Venture {
183065177eeSPatrick Venture     Pca954xClass *s = PCA954X_CLASS(klass);
184065177eeSPatrick Venture     s->nchans = PCA9548_CHANNEL_COUNT;
185065177eeSPatrick Venture }
186065177eeSPatrick Venture 
pca954x_realize(DeviceState * dev,Error ** errp)187*29770e09SPatrick Venture static void pca954x_realize(DeviceState *dev, Error **errp)
188*29770e09SPatrick Venture {
189*29770e09SPatrick Venture     Pca954xState *s = PCA954X(dev);
190*29770e09SPatrick Venture     DeviceState *d = DEVICE(s);
191*29770e09SPatrick Venture     if (s->name) {
192*29770e09SPatrick Venture         d->id = g_strdup(s->name);
193*29770e09SPatrick Venture     } else {
194*29770e09SPatrick Venture         d->id = g_strdup_printf("pca954x[%x]", s->parent.i2c.address);
195*29770e09SPatrick Venture     }
196*29770e09SPatrick Venture }
197*29770e09SPatrick Venture 
pca954x_init(Object * obj)198065177eeSPatrick Venture static void pca954x_init(Object *obj)
199065177eeSPatrick Venture {
200065177eeSPatrick Venture     Pca954xState *s = PCA954X(obj);
201065177eeSPatrick Venture     Pca954xClass *c = PCA954X_GET_CLASS(obj);
202065177eeSPatrick Venture     int i;
203065177eeSPatrick Venture 
204d8bdf979SPatrick Venture     /* SMBus modules. Cannot fail. */
205065177eeSPatrick Venture     for (i = 0; i < c->nchans; i++) {
206d8bdf979SPatrick Venture         g_autofree gchar *bus_name = g_strdup_printf("i2c.%d", i);
207d8bdf979SPatrick Venture 
208d8bdf979SPatrick Venture         /* start all channels as disabled. */
209d8bdf979SPatrick Venture         s->enabled[i] = false;
210d8bdf979SPatrick Venture         s->bus[i] = i2c_init_bus(DEVICE(s), bus_name);
211065177eeSPatrick Venture     }
212065177eeSPatrick Venture }
213065177eeSPatrick Venture 
214*29770e09SPatrick Venture static Property pca954x_props[] = {
215*29770e09SPatrick Venture     DEFINE_PROP_STRING("name", Pca954xState, name),
216*29770e09SPatrick Venture     DEFINE_PROP_END_OF_LIST()
217*29770e09SPatrick Venture };
218*29770e09SPatrick Venture 
pca954x_class_init(ObjectClass * klass,void * data)219065177eeSPatrick Venture static void pca954x_class_init(ObjectClass *klass, void *data)
220065177eeSPatrick Venture {
221065177eeSPatrick Venture     I2CSlaveClass *sc = I2C_SLAVE_CLASS(klass);
222065177eeSPatrick Venture     ResettableClass *rc = RESETTABLE_CLASS(klass);
223065177eeSPatrick Venture     DeviceClass *dc = DEVICE_CLASS(klass);
224065177eeSPatrick Venture     SMBusDeviceClass *k = SMBUS_DEVICE_CLASS(klass);
225065177eeSPatrick Venture 
226065177eeSPatrick Venture     sc->match_and_add = pca954x_match;
227065177eeSPatrick Venture 
228065177eeSPatrick Venture     rc->phases.enter = pca954x_enter_reset;
229065177eeSPatrick Venture 
230065177eeSPatrick Venture     dc->desc = "Pca954x i2c-mux";
231*29770e09SPatrick Venture     dc->realize = pca954x_realize;
232065177eeSPatrick Venture 
233065177eeSPatrick Venture     k->write_data = pca954x_write_data;
234065177eeSPatrick Venture     k->receive_byte = pca954x_read_byte;
235*29770e09SPatrick Venture 
236*29770e09SPatrick Venture     device_class_set_props(dc, pca954x_props);
237065177eeSPatrick Venture }
238065177eeSPatrick Venture 
239065177eeSPatrick Venture static const TypeInfo pca954x_info[] = {
240065177eeSPatrick Venture     {
241065177eeSPatrick Venture         .name          = TYPE_PCA954X,
242065177eeSPatrick Venture         .parent        = TYPE_SMBUS_DEVICE,
243065177eeSPatrick Venture         .instance_size = sizeof(Pca954xState),
244065177eeSPatrick Venture         .instance_init = pca954x_init,
245065177eeSPatrick Venture         .class_size    = sizeof(Pca954xClass),
246065177eeSPatrick Venture         .class_init    = pca954x_class_init,
247065177eeSPatrick Venture         .abstract      = true,
248065177eeSPatrick Venture     },
249065177eeSPatrick Venture     {
250065177eeSPatrick Venture         .name          = TYPE_PCA9546,
251065177eeSPatrick Venture         .parent        = TYPE_PCA954X,
252065177eeSPatrick Venture         .class_init    = pca9546_class_init,
253065177eeSPatrick Venture     },
254065177eeSPatrick Venture     {
255065177eeSPatrick Venture         .name          = TYPE_PCA9548,
256065177eeSPatrick Venture         .parent        = TYPE_PCA954X,
257065177eeSPatrick Venture         .class_init    = pca9548_class_init,
258065177eeSPatrick Venture     },
259065177eeSPatrick Venture };
260065177eeSPatrick Venture 
261065177eeSPatrick Venture DEFINE_TYPES(pca954x_info)
262