qemu

Форк
0
/
i2c_mux_pca954x.c 
261 строка · 6.7 Кб
1
/*
2
 * I2C multiplexer for PCA954x series of I2C multiplexer/switch chips.
3
 *
4
 * Copyright 2021 Google LLC
5
 *
6
 * This program is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU General Public License as published by the
8
 * Free Software Foundation; either version 2 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
 * for more details.
15
 */
16

17
#include "qemu/osdep.h"
18
#include "qapi/error.h"
19
#include "hw/i2c/i2c.h"
20
#include "hw/i2c/i2c_mux_pca954x.h"
21
#include "hw/i2c/smbus_slave.h"
22
#include "hw/qdev-core.h"
23
#include "hw/qdev-properties.h"
24
#include "hw/sysbus.h"
25
#include "qemu/log.h"
26
#include "qemu/module.h"
27
#include "qemu/queue.h"
28
#include "qom/object.h"
29
#include "trace.h"
30

31
#define PCA9548_CHANNEL_COUNT 8
32
#define PCA9546_CHANNEL_COUNT 4
33

34
/*
35
 * struct Pca954xState - The pca954x state object.
36
 * @control: The value written to the mux control.
37
 * @channel: The set of i2c channel buses that act as channels which own the
38
 * i2c children.
39
 */
40
typedef struct Pca954xState {
41
    SMBusDevice parent;
42

43
    uint8_t control;
44

45
    bool enabled[PCA9548_CHANNEL_COUNT];
46
    I2CBus *bus[PCA9548_CHANNEL_COUNT];
47

48
    char *name;
49
} Pca954xState;
50

51
/*
52
 * struct Pca954xClass - The pca954x class object.
53
 * @nchans: The number of i2c channels this device has.
54
 */
55
typedef struct Pca954xClass {
56
    SMBusDeviceClass parent;
57

58
    uint8_t nchans;
59
} Pca954xClass;
60

61
#define TYPE_PCA954X "pca954x"
62
OBJECT_DECLARE_TYPE(Pca954xState, Pca954xClass, PCA954X)
63

64
/*
65
 * For each channel, if it's enabled, recursively call match on those children.
66
 */
67
static bool pca954x_match(I2CSlave *candidate, uint8_t address,
68
                          bool broadcast,
69
                          I2CNodeList *current_devs)
70
{
71
    Pca954xState *mux = PCA954X(candidate);
72
    Pca954xClass *mc = PCA954X_GET_CLASS(mux);
73
    int i;
74

75
    /* They are talking to the mux itself (or all devices enabled). */
76
    if ((candidate->address == address) || broadcast) {
77
        I2CNode *node = g_new(struct I2CNode, 1);
78
        node->elt = candidate;
79
        QLIST_INSERT_HEAD(current_devs, node, next);
80
        if (!broadcast) {
81
            return true;
82
        }
83
    }
84

85
    for (i = 0; i < mc->nchans; i++) {
86
        if (!mux->enabled[i]) {
87
            continue;
88
        }
89

90
        if (i2c_scan_bus(mux->bus[i], address, broadcast,
91
                         current_devs)) {
92
            if (!broadcast) {
93
                return true;
94
            }
95
        }
96
    }
97

98
    /* If we arrived here we didn't find a match, return broadcast. */
99
    return broadcast;
100
}
101

102
static void pca954x_enable_channel(Pca954xState *s, uint8_t enable_mask)
103
{
104
    Pca954xClass *mc = PCA954X_GET_CLASS(s);
105
    int i;
106

107
    /*
108
     * For each channel, check if their bit is set in enable_mask and if yes,
109
     * enable it, otherwise disable, hide it.
110
     */
111
    for (i = 0; i < mc->nchans; i++) {
112
        if (enable_mask & (1 << i)) {
113
            s->enabled[i] = true;
114
        } else {
115
            s->enabled[i] = false;
116
        }
117
    }
118
}
119

120
static void pca954x_write(Pca954xState *s, uint8_t data)
121
{
122
    s->control = data;
123
    pca954x_enable_channel(s, data);
124

125
    trace_pca954x_write_bytes(data);
126
}
127

128
static int pca954x_write_data(SMBusDevice *d, uint8_t *buf, uint8_t len)
129
{
130
    Pca954xState *s = PCA954X(d);
131

132
    if (len == 0) {
133
        qemu_log_mask(LOG_GUEST_ERROR, "%s: writing empty data\n", __func__);
134
        return -1;
135
    }
136

137
    /*
138
     * len should be 1, because they write one byte to enable/disable channels.
139
     */
140
    if (len > 1) {
141
        qemu_log_mask(LOG_GUEST_ERROR,
142
            "%s: extra data after channel selection mask\n",
143
            __func__);
144
        return -1;
145
    }
146

147
    pca954x_write(s, buf[0]);
148
    return 0;
149
}
150

151
static uint8_t pca954x_read_byte(SMBusDevice *d)
152
{
153
    Pca954xState *s = PCA954X(d);
154
    uint8_t data = s->control;
155
    trace_pca954x_read_data(data);
156
    return data;
157
}
158

159
static void pca954x_enter_reset(Object *obj, ResetType type)
160
{
161
    Pca954xState *s = PCA954X(obj);
162
    /* Reset will disable all channels. */
163
    pca954x_write(s, 0);
164
}
165

166
I2CBus *pca954x_i2c_get_bus(I2CSlave *mux, uint8_t channel)
167
{
168
    Pca954xClass *pc = PCA954X_GET_CLASS(mux);
169
    Pca954xState *pca954x = PCA954X(mux);
170

171
    g_assert(channel < pc->nchans);
172
    return pca954x->bus[channel];
173
}
174

175
static void pca9546_class_init(ObjectClass *klass, void *data)
176
{
177
    Pca954xClass *s = PCA954X_CLASS(klass);
178
    s->nchans = PCA9546_CHANNEL_COUNT;
179
}
180

181
static void pca9548_class_init(ObjectClass *klass, void *data)
182
{
183
    Pca954xClass *s = PCA954X_CLASS(klass);
184
    s->nchans = PCA9548_CHANNEL_COUNT;
185
}
186

187
static void pca954x_realize(DeviceState *dev, Error **errp)
188
{
189
    Pca954xState *s = PCA954X(dev);
190
    DeviceState *d = DEVICE(s);
191
    if (s->name) {
192
        d->id = g_strdup(s->name);
193
    } else {
194
        d->id = g_strdup_printf("pca954x[%x]", s->parent.i2c.address);
195
    }
196
}
197

198
static void pca954x_init(Object *obj)
199
{
200
    Pca954xState *s = PCA954X(obj);
201
    Pca954xClass *c = PCA954X_GET_CLASS(obj);
202
    int i;
203

204
    /* SMBus modules. Cannot fail. */
205
    for (i = 0; i < c->nchans; i++) {
206
        g_autofree gchar *bus_name = g_strdup_printf("i2c.%d", i);
207

208
        /* start all channels as disabled. */
209
        s->enabled[i] = false;
210
        s->bus[i] = i2c_init_bus(DEVICE(s), bus_name);
211
    }
212
}
213

214
static Property pca954x_props[] = {
215
    DEFINE_PROP_STRING("name", Pca954xState, name),
216
    DEFINE_PROP_END_OF_LIST()
217
};
218

219
static void pca954x_class_init(ObjectClass *klass, void *data)
220
{
221
    I2CSlaveClass *sc = I2C_SLAVE_CLASS(klass);
222
    ResettableClass *rc = RESETTABLE_CLASS(klass);
223
    DeviceClass *dc = DEVICE_CLASS(klass);
224
    SMBusDeviceClass *k = SMBUS_DEVICE_CLASS(klass);
225

226
    sc->match_and_add = pca954x_match;
227

228
    rc->phases.enter = pca954x_enter_reset;
229

230
    dc->desc = "Pca954x i2c-mux";
231
    dc->realize = pca954x_realize;
232

233
    k->write_data = pca954x_write_data;
234
    k->receive_byte = pca954x_read_byte;
235

236
    device_class_set_props(dc, pca954x_props);
237
}
238

239
static const TypeInfo pca954x_info[] = {
240
    {
241
        .name          = TYPE_PCA954X,
242
        .parent        = TYPE_SMBUS_DEVICE,
243
        .instance_size = sizeof(Pca954xState),
244
        .instance_init = pca954x_init,
245
        .class_size    = sizeof(Pca954xClass),
246
        .class_init    = pca954x_class_init,
247
        .abstract      = true,
248
    },
249
    {
250
        .name          = TYPE_PCA9546,
251
        .parent        = TYPE_PCA954X,
252
        .class_init    = pca9546_class_init,
253
    },
254
    {
255
        .name          = TYPE_PCA9548,
256
        .parent        = TYPE_PCA954X,
257
        .class_init    = pca9548_class_init,
258
    },
259
};
260

261
DEFINE_TYPES(pca954x_info)
262

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.