1 // SPDX-License-Identifier: GPL-2.0-or-later
3 i2c-stub.c - I2C/SMBus chip emulator
5 Copyright (c) 2004 Mark M. Hoffman <mhoffman@lightlink.com>
6 Copyright (C) 2007-2014 Jean Delvare <jdelvare@suse.de>
11 #define pr_fmt(fmt) "i2c-stub: " fmt
13 #include <linux/errno.h>
14 #include <linux/i2c.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/list.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
24 * Support for I2C_FUNC_SMBUS_BLOCK_DATA is disabled by default and must
25 * be enabled explicitly by setting the I2C_FUNC_SMBUS_BLOCK_DATA bits
26 * in the 'functionality' module parameter.
28 #define STUB_FUNC_DEFAULT \
29 (I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE | \
30 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA | \
31 I2C_FUNC_SMBUS_I2C_BLOCK)
33 #define STUB_FUNC_ALL \
34 (STUB_FUNC_DEFAULT | I2C_FUNC_SMBUS_BLOCK_DATA)
36 static unsigned short chip_addr[MAX_CHIPS];
37 module_param_array(chip_addr, ushort, NULL, S_IRUGO);
38 MODULE_PARM_DESC(chip_addr,
39 "Chip addresses (up to 10, between 0x03 and 0x77)");
41 static unsigned long functionality = STUB_FUNC_DEFAULT;
42 module_param(functionality, ulong, S_IRUGO | S_IWUSR);
43 MODULE_PARM_DESC(functionality, "Override functionality bitfield");
45 /* Some chips have banked register ranges */
47 static u8 bank_reg[MAX_CHIPS];
48 module_param_array(bank_reg, byte, NULL, S_IRUGO);
49 MODULE_PARM_DESC(bank_reg, "Bank register");
51 static u8 bank_mask[MAX_CHIPS];
52 module_param_array(bank_mask, byte, NULL, S_IRUGO);
53 MODULE_PARM_DESC(bank_mask, "Bank value mask");
55 static u8 bank_start[MAX_CHIPS];
56 module_param_array(bank_start, byte, NULL, S_IRUGO);
57 MODULE_PARM_DESC(bank_start, "First banked register");
59 static u8 bank_end[MAX_CHIPS];
60 module_param_array(bank_end, byte, NULL, S_IRUGO);
61 MODULE_PARM_DESC(bank_end, "Last banked register");
63 struct smbus_block_data {
64 struct list_head node;
67 u8 block[I2C_SMBUS_BLOCK_MAX];
72 u16 words[256]; /* Byte operations use the LSB as per SMBus
74 struct list_head smbus_blocks;
76 /* For chips with banks, extra registers are allocated dynamically */
80 u8 bank_sel; /* Currently selected bank */
84 u16 *bank_words; /* Room for bank_mask * bank_size registers */
87 static struct stub_chip *stub_chips;
88 static int stub_chips_nr;
90 static struct smbus_block_data *stub_find_block(struct device *dev,
91 struct stub_chip *chip,
92 u8 command, bool create)
94 struct smbus_block_data *b, *rb = NULL;
96 list_for_each_entry(b, &chip->smbus_blocks, node) {
97 if (b->command == command) {
102 if (rb == NULL && create) {
103 rb = devm_kzalloc(dev, sizeof(*rb), GFP_KERNEL);
106 rb->command = command;
107 list_add(&rb->node, &chip->smbus_blocks);
112 static u16 *stub_get_wordp(struct stub_chip *chip, u8 offset)
114 if (chip->bank_sel &&
115 offset >= chip->bank_start && offset <= chip->bank_end)
116 return chip->bank_words +
117 (chip->bank_sel - 1) * chip->bank_size +
118 offset - chip->bank_start;
120 return chip->words + offset;
123 /* Return negative errno on error. */
124 static s32 stub_xfer(struct i2c_adapter *adap, u16 addr, unsigned short flags,
125 char read_write, u8 command, int size, union i2c_smbus_data *data)
129 struct stub_chip *chip = NULL;
130 struct smbus_block_data *b;
133 /* Search for the right chip */
134 for (i = 0; i < stub_chips_nr; i++) {
135 if (addr == chip_addr[i]) {
136 chip = stub_chips + i;
145 case I2C_SMBUS_QUICK:
146 dev_dbg(&adap->dev, "smbus quick - addr 0x%02x\n", addr);
151 if (read_write == I2C_SMBUS_WRITE) {
152 chip->pointer = command;
154 "smbus byte - addr 0x%02x, wrote 0x%02x.\n",
157 wordp = stub_get_wordp(chip, chip->pointer++);
158 data->byte = *wordp & 0xff;
160 "smbus byte - addr 0x%02x, read 0x%02x.\n",
167 case I2C_SMBUS_BYTE_DATA:
168 wordp = stub_get_wordp(chip, command);
169 if (read_write == I2C_SMBUS_WRITE) {
171 *wordp |= data->byte;
173 "smbus byte data - addr 0x%02x, wrote 0x%02x at 0x%02x.\n",
174 addr, data->byte, command);
176 /* Set the bank as needed */
177 if (chip->bank_words && command == chip->bank_reg) {
179 (data->byte >> chip->bank_shift)
182 "switching to bank %u.\n",
186 data->byte = *wordp & 0xff;
188 "smbus byte data - addr 0x%02x, read 0x%02x at 0x%02x.\n",
189 addr, data->byte, command);
191 chip->pointer = command + 1;
196 case I2C_SMBUS_WORD_DATA:
197 wordp = stub_get_wordp(chip, command);
198 if (read_write == I2C_SMBUS_WRITE) {
201 "smbus word data - addr 0x%02x, wrote 0x%04x at 0x%02x.\n",
202 addr, data->word, command);
206 "smbus word data - addr 0x%02x, read 0x%04x at 0x%02x.\n",
207 addr, data->word, command);
213 case I2C_SMBUS_I2C_BLOCK_DATA:
215 * We ignore banks here, because banked chips don't use I2C
218 if (data->block[0] > 256 - command) /* Avoid overrun */
219 data->block[0] = 256 - command;
220 len = data->block[0];
221 if (read_write == I2C_SMBUS_WRITE) {
222 for (i = 0; i < len; i++) {
223 chip->words[command + i] &= 0xff00;
224 chip->words[command + i] |= data->block[1 + i];
227 "i2c block data - addr 0x%02x, wrote %d bytes at 0x%02x.\n",
230 for (i = 0; i < len; i++) {
232 chip->words[command + i] & 0xff;
235 "i2c block data - addr 0x%02x, read %d bytes at 0x%02x.\n",
242 case I2C_SMBUS_BLOCK_DATA:
244 * We ignore banks here, because chips typically don't use both
245 * banks and SMBus block transfers
247 b = stub_find_block(&adap->dev, chip, command, false);
248 if (read_write == I2C_SMBUS_WRITE) {
249 len = data->block[0];
250 if (len == 0 || len > I2C_SMBUS_BLOCK_MAX) {
255 b = stub_find_block(&adap->dev, chip, command,
262 /* Largest write sets read block length */
265 for (i = 0; i < len; i++)
266 b->block[i] = data->block[i + 1];
267 /* update for byte and word commands */
268 chip->words[command] = (b->block[0] << 8) | b->len;
270 "smbus block data - addr 0x%02x, wrote %d bytes at 0x%02x.\n",
275 "SMBus block read command without prior block write not supported\n");
280 data->block[0] = len;
281 for (i = 0; i < len; i++)
282 data->block[i + 1] = b->block[i];
284 "smbus block data - addr 0x%02x, read %d bytes at 0x%02x.\n",
292 dev_dbg(&adap->dev, "Unsupported I2C/SMBus command\n");
295 } /* switch (size) */
300 static u32 stub_func(struct i2c_adapter *adapter)
302 return STUB_FUNC_ALL & functionality;
305 static const struct i2c_algorithm smbus_algorithm = {
306 .functionality = stub_func,
307 .smbus_xfer = stub_xfer,
310 static struct i2c_adapter stub_adapter = {
311 .owner = THIS_MODULE,
312 .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
313 .algo = &smbus_algorithm,
314 .name = "SMBus stub driver",
317 static int __init i2c_stub_allocate_banks(int i)
319 struct stub_chip *chip = stub_chips + i;
321 chip->bank_reg = bank_reg[i];
322 chip->bank_start = bank_start[i];
323 chip->bank_end = bank_end[i];
324 chip->bank_size = bank_end[i] - bank_start[i] + 1;
326 /* We assume that all bits in the mask are contiguous */
327 chip->bank_mask = bank_mask[i];
328 while (!(chip->bank_mask & 1)) {
330 chip->bank_mask >>= 1;
333 chip->bank_words = kcalloc(chip->bank_mask * chip->bank_size,
336 if (!chip->bank_words)
339 pr_debug("Allocated %u banks of %u words each (registers 0x%02x to 0x%02x)\n",
340 chip->bank_mask, chip->bank_size, chip->bank_start,
346 static void i2c_stub_free(void)
350 for (i = 0; i < stub_chips_nr; i++)
351 kfree(stub_chips[i].bank_words);
355 static int __init i2c_stub_init(void)
360 pr_err("Please specify a chip address\n");
364 for (i = 0; i < MAX_CHIPS && chip_addr[i]; i++) {
365 if (chip_addr[i] < 0x03 || chip_addr[i] > 0x77) {
366 pr_err("Invalid chip address 0x%02x\n",
371 pr_info("Virtual chip at 0x%02x\n", chip_addr[i]);
374 /* Allocate memory for all chips at once */
376 stub_chips = kcalloc(stub_chips_nr, sizeof(struct stub_chip),
381 for (i = 0; i < stub_chips_nr; i++) {
382 INIT_LIST_HEAD(&stub_chips[i].smbus_blocks);
384 /* Allocate extra memory for banked register ranges */
386 ret = i2c_stub_allocate_banks(i);
392 ret = i2c_add_adapter(&stub_adapter);
403 static void __exit i2c_stub_exit(void)
405 i2c_del_adapter(&stub_adapter);
409 MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
410 MODULE_DESCRIPTION("I2C stub driver");
411 MODULE_LICENSE("GPL");
413 module_init(i2c_stub_init);
414 module_exit(i2c_stub_exit);