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" 23065177eeSPatrick Venture #include "hw/sysbus.h" 24065177eeSPatrick Venture #include "qemu/log.h" 25065177eeSPatrick Venture #include "qemu/module.h" 26065177eeSPatrick Venture #include "qemu/queue.h" 27065177eeSPatrick Venture #include "qom/object.h" 28065177eeSPatrick Venture #include "trace.h" 29065177eeSPatrick Venture 30065177eeSPatrick Venture #define PCA9548_CHANNEL_COUNT 8 31065177eeSPatrick Venture #define PCA9546_CHANNEL_COUNT 4 32065177eeSPatrick Venture 33065177eeSPatrick Venture /* 34065177eeSPatrick Venture * struct Pca954xState - The pca954x state object. 35065177eeSPatrick Venture * @control: The value written to the mux control. 36065177eeSPatrick Venture * @channel: The set of i2c channel buses that act as channels which own the 37065177eeSPatrick Venture * i2c children. 38065177eeSPatrick Venture */ 39065177eeSPatrick Venture typedef struct Pca954xState { 40065177eeSPatrick Venture SMBusDevice parent; 41065177eeSPatrick Venture 42065177eeSPatrick Venture uint8_t control; 43065177eeSPatrick Venture 44d8bdf979SPatrick Venture bool enabled[PCA9548_CHANNEL_COUNT]; 45d8bdf979SPatrick Venture I2CBus *bus[PCA9548_CHANNEL_COUNT]; 46065177eeSPatrick Venture } Pca954xState; 47065177eeSPatrick Venture 48065177eeSPatrick Venture /* 49065177eeSPatrick Venture * struct Pca954xClass - The pca954x class object. 50065177eeSPatrick Venture * @nchans: The number of i2c channels this device has. 51065177eeSPatrick Venture */ 52065177eeSPatrick Venture typedef struct Pca954xClass { 53065177eeSPatrick Venture SMBusDeviceClass parent; 54065177eeSPatrick Venture 55065177eeSPatrick Venture uint8_t nchans; 56065177eeSPatrick Venture } Pca954xClass; 57065177eeSPatrick Venture 58065177eeSPatrick Venture #define TYPE_PCA954X "pca954x" 59065177eeSPatrick Venture OBJECT_DECLARE_TYPE(Pca954xState, Pca954xClass, PCA954X) 60065177eeSPatrick Venture 61065177eeSPatrick Venture /* 62065177eeSPatrick Venture * For each channel, if it's enabled, recursively call match on those children. 63065177eeSPatrick Venture */ 64065177eeSPatrick Venture static bool pca954x_match(I2CSlave *candidate, uint8_t address, 65065177eeSPatrick Venture bool broadcast, 66065177eeSPatrick Venture I2CNodeList *current_devs) 67065177eeSPatrick Venture { 68065177eeSPatrick Venture Pca954xState *mux = PCA954X(candidate); 69065177eeSPatrick Venture Pca954xClass *mc = PCA954X_GET_CLASS(mux); 70065177eeSPatrick Venture int i; 71065177eeSPatrick Venture 72065177eeSPatrick Venture /* They are talking to the mux itself (or all devices enabled). */ 73065177eeSPatrick Venture if ((candidate->address == address) || broadcast) { 74*b21e2380SMarkus Armbruster I2CNode *node = g_new(struct I2CNode, 1); 75065177eeSPatrick Venture node->elt = candidate; 76065177eeSPatrick Venture QLIST_INSERT_HEAD(current_devs, node, next); 77065177eeSPatrick Venture if (!broadcast) { 78065177eeSPatrick Venture return true; 79065177eeSPatrick Venture } 80065177eeSPatrick Venture } 81065177eeSPatrick Venture 82065177eeSPatrick Venture for (i = 0; i < mc->nchans; i++) { 83d8bdf979SPatrick Venture if (!mux->enabled[i]) { 84065177eeSPatrick Venture continue; 85065177eeSPatrick Venture } 86065177eeSPatrick Venture 87d8bdf979SPatrick Venture if (i2c_scan_bus(mux->bus[i], address, broadcast, 88065177eeSPatrick Venture current_devs)) { 89065177eeSPatrick Venture if (!broadcast) { 90065177eeSPatrick Venture return true; 91065177eeSPatrick Venture } 92065177eeSPatrick Venture } 93065177eeSPatrick Venture } 94065177eeSPatrick Venture 95065177eeSPatrick Venture /* If we arrived here we didn't find a match, return broadcast. */ 96065177eeSPatrick Venture return broadcast; 97065177eeSPatrick Venture } 98065177eeSPatrick Venture 99065177eeSPatrick Venture static void pca954x_enable_channel(Pca954xState *s, uint8_t enable_mask) 100065177eeSPatrick Venture { 101065177eeSPatrick Venture Pca954xClass *mc = PCA954X_GET_CLASS(s); 102065177eeSPatrick Venture int i; 103065177eeSPatrick Venture 104065177eeSPatrick Venture /* 105065177eeSPatrick Venture * For each channel, check if their bit is set in enable_mask and if yes, 106065177eeSPatrick Venture * enable it, otherwise disable, hide it. 107065177eeSPatrick Venture */ 108065177eeSPatrick Venture for (i = 0; i < mc->nchans; i++) { 109065177eeSPatrick Venture if (enable_mask & (1 << i)) { 110d8bdf979SPatrick Venture s->enabled[i] = true; 111065177eeSPatrick Venture } else { 112d8bdf979SPatrick Venture s->enabled[i] = false; 113065177eeSPatrick Venture } 114065177eeSPatrick Venture } 115065177eeSPatrick Venture } 116065177eeSPatrick Venture 117065177eeSPatrick Venture static void pca954x_write(Pca954xState *s, uint8_t data) 118065177eeSPatrick Venture { 119065177eeSPatrick Venture s->control = data; 120065177eeSPatrick Venture pca954x_enable_channel(s, data); 121065177eeSPatrick Venture 122065177eeSPatrick Venture trace_pca954x_write_bytes(data); 123065177eeSPatrick Venture } 124065177eeSPatrick Venture 125065177eeSPatrick Venture static int pca954x_write_data(SMBusDevice *d, uint8_t *buf, uint8_t len) 126065177eeSPatrick Venture { 127065177eeSPatrick Venture Pca954xState *s = PCA954X(d); 128065177eeSPatrick Venture 129065177eeSPatrick Venture if (len == 0) { 130065177eeSPatrick Venture qemu_log_mask(LOG_GUEST_ERROR, "%s: writing empty data\n", __func__); 131065177eeSPatrick Venture return -1; 132065177eeSPatrick Venture } 133065177eeSPatrick Venture 134065177eeSPatrick Venture /* 135065177eeSPatrick Venture * len should be 1, because they write one byte to enable/disable channels. 136065177eeSPatrick Venture */ 137065177eeSPatrick Venture if (len > 1) { 138065177eeSPatrick Venture qemu_log_mask(LOG_GUEST_ERROR, 139065177eeSPatrick Venture "%s: extra data after channel selection mask\n", 140065177eeSPatrick Venture __func__); 141065177eeSPatrick Venture return -1; 142065177eeSPatrick Venture } 143065177eeSPatrick Venture 144065177eeSPatrick Venture pca954x_write(s, buf[0]); 145065177eeSPatrick Venture return 0; 146065177eeSPatrick Venture } 147065177eeSPatrick Venture 148065177eeSPatrick Venture static uint8_t pca954x_read_byte(SMBusDevice *d) 149065177eeSPatrick Venture { 150065177eeSPatrick Venture Pca954xState *s = PCA954X(d); 151065177eeSPatrick Venture uint8_t data = s->control; 152065177eeSPatrick Venture trace_pca954x_read_data(data); 153065177eeSPatrick Venture return data; 154065177eeSPatrick Venture } 155065177eeSPatrick Venture 156065177eeSPatrick Venture static void pca954x_enter_reset(Object *obj, ResetType type) 157065177eeSPatrick Venture { 158065177eeSPatrick Venture Pca954xState *s = PCA954X(obj); 159065177eeSPatrick Venture /* Reset will disable all channels. */ 160065177eeSPatrick Venture pca954x_write(s, 0); 161065177eeSPatrick Venture } 162065177eeSPatrick Venture 163065177eeSPatrick Venture I2CBus *pca954x_i2c_get_bus(I2CSlave *mux, uint8_t channel) 164065177eeSPatrick Venture { 165065177eeSPatrick Venture Pca954xClass *pc = PCA954X_GET_CLASS(mux); 166065177eeSPatrick Venture Pca954xState *pca954x = PCA954X(mux); 167065177eeSPatrick Venture 168065177eeSPatrick Venture g_assert(channel < pc->nchans); 169d8bdf979SPatrick Venture return pca954x->bus[channel]; 170065177eeSPatrick Venture } 171065177eeSPatrick Venture 172065177eeSPatrick Venture static void pca9546_class_init(ObjectClass *klass, void *data) 173065177eeSPatrick Venture { 174065177eeSPatrick Venture Pca954xClass *s = PCA954X_CLASS(klass); 175065177eeSPatrick Venture s->nchans = PCA9546_CHANNEL_COUNT; 176065177eeSPatrick Venture } 177065177eeSPatrick Venture 178065177eeSPatrick Venture static void pca9548_class_init(ObjectClass *klass, void *data) 179065177eeSPatrick Venture { 180065177eeSPatrick Venture Pca954xClass *s = PCA954X_CLASS(klass); 181065177eeSPatrick Venture s->nchans = PCA9548_CHANNEL_COUNT; 182065177eeSPatrick Venture } 183065177eeSPatrick Venture 184065177eeSPatrick Venture static void pca954x_init(Object *obj) 185065177eeSPatrick Venture { 186065177eeSPatrick Venture Pca954xState *s = PCA954X(obj); 187065177eeSPatrick Venture Pca954xClass *c = PCA954X_GET_CLASS(obj); 188065177eeSPatrick Venture int i; 189065177eeSPatrick Venture 190d8bdf979SPatrick Venture /* SMBus modules. Cannot fail. */ 191065177eeSPatrick Venture for (i = 0; i < c->nchans; i++) { 192d8bdf979SPatrick Venture g_autofree gchar *bus_name = g_strdup_printf("i2c.%d", i); 193d8bdf979SPatrick Venture 194d8bdf979SPatrick Venture /* start all channels as disabled. */ 195d8bdf979SPatrick Venture s->enabled[i] = false; 196d8bdf979SPatrick Venture s->bus[i] = i2c_init_bus(DEVICE(s), bus_name); 197065177eeSPatrick Venture } 198065177eeSPatrick Venture } 199065177eeSPatrick Venture 200065177eeSPatrick Venture static void pca954x_class_init(ObjectClass *klass, void *data) 201065177eeSPatrick Venture { 202065177eeSPatrick Venture I2CSlaveClass *sc = I2C_SLAVE_CLASS(klass); 203065177eeSPatrick Venture ResettableClass *rc = RESETTABLE_CLASS(klass); 204065177eeSPatrick Venture DeviceClass *dc = DEVICE_CLASS(klass); 205065177eeSPatrick Venture SMBusDeviceClass *k = SMBUS_DEVICE_CLASS(klass); 206065177eeSPatrick Venture 207065177eeSPatrick Venture sc->match_and_add = pca954x_match; 208065177eeSPatrick Venture 209065177eeSPatrick Venture rc->phases.enter = pca954x_enter_reset; 210065177eeSPatrick Venture 211065177eeSPatrick Venture dc->desc = "Pca954x i2c-mux"; 212065177eeSPatrick Venture 213065177eeSPatrick Venture k->write_data = pca954x_write_data; 214065177eeSPatrick Venture k->receive_byte = pca954x_read_byte; 215065177eeSPatrick Venture } 216065177eeSPatrick Venture 217065177eeSPatrick Venture static const TypeInfo pca954x_info[] = { 218065177eeSPatrick Venture { 219065177eeSPatrick Venture .name = TYPE_PCA954X, 220065177eeSPatrick Venture .parent = TYPE_SMBUS_DEVICE, 221065177eeSPatrick Venture .instance_size = sizeof(Pca954xState), 222065177eeSPatrick Venture .instance_init = pca954x_init, 223065177eeSPatrick Venture .class_size = sizeof(Pca954xClass), 224065177eeSPatrick Venture .class_init = pca954x_class_init, 225065177eeSPatrick Venture .abstract = true, 226065177eeSPatrick Venture }, 227065177eeSPatrick Venture { 228065177eeSPatrick Venture .name = TYPE_PCA9546, 229065177eeSPatrick Venture .parent = TYPE_PCA954X, 230065177eeSPatrick Venture .class_init = pca9546_class_init, 231065177eeSPatrick Venture }, 232065177eeSPatrick Venture { 233065177eeSPatrick Venture .name = TYPE_PCA9548, 234065177eeSPatrick Venture .parent = TYPE_PCA954X, 235065177eeSPatrick Venture .class_init = pca9548_class_init, 236065177eeSPatrick Venture }, 237065177eeSPatrick Venture }; 238065177eeSPatrick Venture 239065177eeSPatrick Venture DEFINE_TYPES(pca954x_info) 240