blob: d642cad219d9e65b561659cfb895af11c6cba41b [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Jean Delvare569be442008-01-27 18:14:45 +01003 i2c-stub.c - I2C/SMBus chip emulator
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
5 Copyright (c) 2004 Mark M. Hoffman <mhoffman@lightlink.com>
Jean Delvare2408c172014-07-10 12:56:59 +02006 Copyright (C) 2007-2014 Jean Delvare <jdelvare@suse.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007
Linus Torvalds1da177e2005-04-16 15:20:36 -07008*/
9
Wolfram Sang6c427782017-05-16 13:21:05 +020010#define pr_fmt(fmt) "i2c-stub: " fmt
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/errno.h>
13#include <linux/i2c.h>
Wolfram Sang9f8f53a2016-02-20 23:33:39 +010014#include <linux/init.h>
15#include <linux/kernel.h>
Guenter Roeck6f16b752014-07-17 09:56:03 -070016#include <linux/list.h>
Wolfram Sang9f8f53a2016-02-20 23:33:39 +010017#include <linux/module.h>
18#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Jean Delvare9d90c1f2007-10-13 23:56:31 +020020#define MAX_CHIPS 10
Guenter Roeck6f16b752014-07-17 09:56:03 -070021
22/*
23 * Support for I2C_FUNC_SMBUS_BLOCK_DATA is disabled by default and must
24 * be enabled explicitly by setting the I2C_FUNC_SMBUS_BLOCK_DATA bits
25 * in the 'functionality' module parameter.
26 */
27#define STUB_FUNC_DEFAULT \
28 (I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE | \
29 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA | \
30 I2C_FUNC_SMBUS_I2C_BLOCK)
31
32#define STUB_FUNC_ALL \
33 (STUB_FUNC_DEFAULT | I2C_FUNC_SMBUS_BLOCK_DATA)
Jean Delvare7a8d29c2006-08-13 23:46:44 +020034
Jean Delvare9d90c1f2007-10-13 23:56:31 +020035static unsigned short chip_addr[MAX_CHIPS];
36module_param_array(chip_addr, ushort, NULL, S_IRUGO);
37MODULE_PARM_DESC(chip_addr,
Jean Delvare85d69312008-04-29 23:11:37 +020038 "Chip addresses (up to 10, between 0x03 and 0x77)");
Jean Delvare9d90c1f2007-10-13 23:56:31 +020039
Guenter Roeck6f16b752014-07-17 09:56:03 -070040static unsigned long functionality = STUB_FUNC_DEFAULT;
Jean Delvare38f41f22009-12-06 17:06:29 +010041module_param(functionality, ulong, S_IRUGO | S_IWUSR);
42MODULE_PARM_DESC(functionality, "Override functionality bitfield");
43
Jean Delvare2408c172014-07-10 12:56:59 +020044/* Some chips have banked register ranges */
45
46static u8 bank_reg[MAX_CHIPS];
47module_param_array(bank_reg, byte, NULL, S_IRUGO);
48MODULE_PARM_DESC(bank_reg, "Bank register");
49
50static u8 bank_mask[MAX_CHIPS];
51module_param_array(bank_mask, byte, NULL, S_IRUGO);
52MODULE_PARM_DESC(bank_mask, "Bank value mask");
53
54static u8 bank_start[MAX_CHIPS];
55module_param_array(bank_start, byte, NULL, S_IRUGO);
56MODULE_PARM_DESC(bank_start, "First banked register");
57
58static u8 bank_end[MAX_CHIPS];
59module_param_array(bank_end, byte, NULL, S_IRUGO);
60MODULE_PARM_DESC(bank_end, "Last banked register");
61
Guenter Roeck6f16b752014-07-17 09:56:03 -070062struct smbus_block_data {
63 struct list_head node;
64 u8 command;
65 u8 len;
66 u8 block[I2C_SMBUS_BLOCK_MAX];
67};
68
Jean Delvare9d90c1f2007-10-13 23:56:31 +020069struct stub_chip {
70 u8 pointer;
Jean Delvare569be442008-01-27 18:14:45 +010071 u16 words[256]; /* Byte operations use the LSB as per SMBus
72 specification */
Guenter Roeck6f16b752014-07-17 09:56:03 -070073 struct list_head smbus_blocks;
Jean Delvare2408c172014-07-10 12:56:59 +020074
75 /* For chips with banks, extra registers are allocated dynamically */
76 u8 bank_reg;
77 u8 bank_shift;
78 u8 bank_mask;
79 u8 bank_sel; /* Currently selected bank */
80 u8 bank_start;
81 u8 bank_end;
82 u16 bank_size;
83 u16 *bank_words; /* Room for bank_mask * bank_size registers */
Jean Delvare9d90c1f2007-10-13 23:56:31 +020084};
85
86static struct stub_chip *stub_chips;
Jean Delvare1dff5982014-07-10 12:45:11 +020087static int stub_chips_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Guenter Roeck6f16b752014-07-17 09:56:03 -070089static struct smbus_block_data *stub_find_block(struct device *dev,
90 struct stub_chip *chip,
91 u8 command, bool create)
92{
93 struct smbus_block_data *b, *rb = NULL;
94
95 list_for_each_entry(b, &chip->smbus_blocks, node) {
96 if (b->command == command) {
97 rb = b;
98 break;
99 }
100 }
101 if (rb == NULL && create) {
102 rb = devm_kzalloc(dev, sizeof(*rb), GFP_KERNEL);
103 if (rb == NULL)
104 return rb;
105 rb->command = command;
106 list_add(&rb->node, &chip->smbus_blocks);
107 }
108 return rb;
109}
110
Jean Delvare2408c172014-07-10 12:56:59 +0200111static u16 *stub_get_wordp(struct stub_chip *chip, u8 offset)
112{
113 if (chip->bank_sel &&
114 offset >= chip->bank_start && offset <= chip->bank_end)
115 return chip->bank_words +
116 (chip->bank_sel - 1) * chip->bank_size +
117 offset - chip->bank_start;
118 else
119 return chip->words + offset;
120}
121
David Brownell97140342008-07-14 22:38:25 +0200122/* Return negative errno on error. */
Jean Delvare31d178b2012-10-28 21:37:00 +0100123static s32 stub_xfer(struct i2c_adapter *adap, u16 addr, unsigned short flags,
124 char read_write, u8 command, int size, union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 s32 ret;
Jean Delvare47103172009-12-06 17:06:28 +0100127 int i, len;
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200128 struct stub_chip *chip = NULL;
Guenter Roeck6f16b752014-07-17 09:56:03 -0700129 struct smbus_block_data *b;
Jean Delvare2408c172014-07-10 12:56:59 +0200130 u16 *wordp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200132 /* Search for the right chip */
Jean Delvare1dff5982014-07-10 12:45:11 +0200133 for (i = 0; i < stub_chips_nr; i++) {
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200134 if (addr == chip_addr[i]) {
135 chip = stub_chips + i;
136 break;
137 }
138 }
139 if (!chip)
Jean Delvare7a8d29c2006-08-13 23:46:44 +0200140 return -ENODEV;
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 switch (size) {
143
144 case I2C_SMBUS_QUICK:
145 dev_dbg(&adap->dev, "smbus quick - addr 0x%02x\n", addr);
146 ret = 0;
147 break;
148
149 case I2C_SMBUS_BYTE:
150 if (read_write == I2C_SMBUS_WRITE) {
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200151 chip->pointer = command;
Jean Delvare31d178b2012-10-28 21:37:00 +0100152 dev_dbg(&adap->dev,
153 "smbus byte - addr 0x%02x, wrote 0x%02x.\n",
154 addr, command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 } else {
Jean Delvare2408c172014-07-10 12:56:59 +0200156 wordp = stub_get_wordp(chip, chip->pointer++);
157 data->byte = *wordp & 0xff;
Jean Delvare31d178b2012-10-28 21:37:00 +0100158 dev_dbg(&adap->dev,
159 "smbus byte - addr 0x%02x, read 0x%02x.\n",
160 addr, data->byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
162
163 ret = 0;
164 break;
165
166 case I2C_SMBUS_BYTE_DATA:
Jean Delvare2408c172014-07-10 12:56:59 +0200167 wordp = stub_get_wordp(chip, command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 if (read_write == I2C_SMBUS_WRITE) {
Jean Delvare2408c172014-07-10 12:56:59 +0200169 *wordp &= 0xff00;
170 *wordp |= data->byte;
Jean Delvare31d178b2012-10-28 21:37:00 +0100171 dev_dbg(&adap->dev,
172 "smbus byte data - addr 0x%02x, wrote 0x%02x at 0x%02x.\n",
173 addr, data->byte, command);
Jean Delvare2408c172014-07-10 12:56:59 +0200174
175 /* Set the bank as needed */
176 if (chip->bank_words && command == chip->bank_reg) {
177 chip->bank_sel =
178 (data->byte >> chip->bank_shift)
179 & chip->bank_mask;
180 dev_dbg(&adap->dev,
181 "switching to bank %u.\n",
182 chip->bank_sel);
183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 } else {
Jean Delvare2408c172014-07-10 12:56:59 +0200185 data->byte = *wordp & 0xff;
Jean Delvare31d178b2012-10-28 21:37:00 +0100186 dev_dbg(&adap->dev,
187 "smbus byte data - addr 0x%02x, read 0x%02x at 0x%02x.\n",
188 addr, data->byte, command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200190 chip->pointer = command + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 ret = 0;
193 break;
194
195 case I2C_SMBUS_WORD_DATA:
Jean Delvare2408c172014-07-10 12:56:59 +0200196 wordp = stub_get_wordp(chip, command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 if (read_write == I2C_SMBUS_WRITE) {
Jean Delvare2408c172014-07-10 12:56:59 +0200198 *wordp = data->word;
Jean Delvare31d178b2012-10-28 21:37:00 +0100199 dev_dbg(&adap->dev,
200 "smbus word data - addr 0x%02x, wrote 0x%04x at 0x%02x.\n",
201 addr, data->word, command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 } else {
Jean Delvare2408c172014-07-10 12:56:59 +0200203 data->word = *wordp;
Jean Delvare31d178b2012-10-28 21:37:00 +0100204 dev_dbg(&adap->dev,
205 "smbus word data - addr 0x%02x, read 0x%04x at 0x%02x.\n",
206 addr, data->word, command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
208
209 ret = 0;
210 break;
211
Jean Delvare47103172009-12-06 17:06:28 +0100212 case I2C_SMBUS_I2C_BLOCK_DATA:
Jean Delvare2408c172014-07-10 12:56:59 +0200213 /*
214 * We ignore banks here, because banked chips don't use I2C
215 * block transfers
216 */
Jean Delvare0f6ba0d2014-07-13 17:17:17 +0200217 if (data->block[0] > 256 - command) /* Avoid overrun */
218 data->block[0] = 256 - command;
Jean Delvare47103172009-12-06 17:06:28 +0100219 len = data->block[0];
220 if (read_write == I2C_SMBUS_WRITE) {
221 for (i = 0; i < len; i++) {
222 chip->words[command + i] &= 0xff00;
223 chip->words[command + i] |= data->block[1 + i];
224 }
Jean Delvare31d178b2012-10-28 21:37:00 +0100225 dev_dbg(&adap->dev,
226 "i2c block data - addr 0x%02x, wrote %d bytes at 0x%02x.\n",
227 addr, len, command);
Jean Delvare47103172009-12-06 17:06:28 +0100228 } else {
229 for (i = 0; i < len; i++) {
230 data->block[1 + i] =
231 chip->words[command + i] & 0xff;
232 }
Jean Delvare31d178b2012-10-28 21:37:00 +0100233 dev_dbg(&adap->dev,
234 "i2c block data - addr 0x%02x, read %d bytes at 0x%02x.\n",
235 addr, len, command);
Jean Delvare47103172009-12-06 17:06:28 +0100236 }
237
238 ret = 0;
239 break;
240
Guenter Roeck6f16b752014-07-17 09:56:03 -0700241 case I2C_SMBUS_BLOCK_DATA:
Jean Delvare2408c172014-07-10 12:56:59 +0200242 /*
243 * We ignore banks here, because chips typically don't use both
244 * banks and SMBus block transfers
245 */
Guenter Roeck6f16b752014-07-17 09:56:03 -0700246 b = stub_find_block(&adap->dev, chip, command, false);
247 if (read_write == I2C_SMBUS_WRITE) {
248 len = data->block[0];
249 if (len == 0 || len > I2C_SMBUS_BLOCK_MAX) {
250 ret = -EINVAL;
251 break;
252 }
253 if (b == NULL) {
254 b = stub_find_block(&adap->dev, chip, command,
255 true);
256 if (b == NULL) {
257 ret = -ENOMEM;
258 break;
259 }
260 }
261 /* Largest write sets read block length */
262 if (len > b->len)
263 b->len = len;
264 for (i = 0; i < len; i++)
265 b->block[i] = data->block[i + 1];
266 /* update for byte and word commands */
267 chip->words[command] = (b->block[0] << 8) | b->len;
268 dev_dbg(&adap->dev,
269 "smbus block data - addr 0x%02x, wrote %d bytes at 0x%02x.\n",
270 addr, len, command);
271 } else {
272 if (b == NULL) {
273 dev_dbg(&adap->dev,
274 "SMBus block read command without prior block write not supported\n");
275 ret = -EOPNOTSUPP;
276 break;
277 }
278 len = b->len;
279 data->block[0] = len;
280 for (i = 0; i < len; i++)
281 data->block[i + 1] = b->block[i];
282 dev_dbg(&adap->dev,
283 "smbus block data - addr 0x%02x, read %d bytes at 0x%02x.\n",
284 addr, len, command);
285 }
286
287 ret = 0;
288 break;
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 default:
291 dev_dbg(&adap->dev, "Unsupported I2C/SMBus command\n");
David Brownell97140342008-07-14 22:38:25 +0200292 ret = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 break;
294 } /* switch (size) */
295
296 return ret;
297}
298
299static u32 stub_func(struct i2c_adapter *adapter)
300{
Guenter Roeck6f16b752014-07-17 09:56:03 -0700301 return STUB_FUNC_ALL & functionality;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
Jean Delvare8f9082c2006-09-03 22:39:46 +0200304static const struct i2c_algorithm smbus_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 .functionality = stub_func,
306 .smbus_xfer = stub_xfer,
307};
308
309static struct i2c_adapter stub_adapter = {
310 .owner = THIS_MODULE,
Jean Delvare3401b2f2008-07-14 22:38:29 +0200311 .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 .algo = &smbus_algorithm,
313 .name = "SMBus stub driver",
314};
315
Jean Delvare2408c172014-07-10 12:56:59 +0200316static int __init i2c_stub_allocate_banks(int i)
317{
318 struct stub_chip *chip = stub_chips + i;
319
320 chip->bank_reg = bank_reg[i];
321 chip->bank_start = bank_start[i];
322 chip->bank_end = bank_end[i];
323 chip->bank_size = bank_end[i] - bank_start[i] + 1;
324
325 /* We assume that all bits in the mask are contiguous */
326 chip->bank_mask = bank_mask[i];
327 while (!(chip->bank_mask & 1)) {
328 chip->bank_shift++;
329 chip->bank_mask >>= 1;
330 }
331
Kees Cook6396bb22018-06-12 14:03:40 -0700332 chip->bank_words = kcalloc(chip->bank_mask * chip->bank_size,
333 sizeof(u16),
334 GFP_KERNEL);
Jean Delvare2408c172014-07-10 12:56:59 +0200335 if (!chip->bank_words)
336 return -ENOMEM;
337
Wolfram Sang6c427782017-05-16 13:21:05 +0200338 pr_debug("Allocated %u banks of %u words each (registers 0x%02x to 0x%02x)\n",
Jean Delvare2408c172014-07-10 12:56:59 +0200339 chip->bank_mask, chip->bank_size, chip->bank_start,
340 chip->bank_end);
341
342 return 0;
343}
344
345static void i2c_stub_free(void)
346{
347 int i;
348
349 for (i = 0; i < stub_chips_nr; i++)
350 kfree(stub_chips[i].bank_words);
351 kfree(stub_chips);
352}
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354static int __init i2c_stub_init(void)
355{
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200356 int i, ret;
357
358 if (!chip_addr[0]) {
Wolfram Sang6c427782017-05-16 13:21:05 +0200359 pr_err("Please specify a chip address\n");
Jean Delvare7a8d29c2006-08-13 23:46:44 +0200360 return -ENODEV;
361 }
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200362
363 for (i = 0; i < MAX_CHIPS && chip_addr[i]; i++) {
364 if (chip_addr[i] < 0x03 || chip_addr[i] > 0x77) {
Wolfram Sang6c427782017-05-16 13:21:05 +0200365 pr_err("Invalid chip address 0x%02x\n",
Jean Delvare31d178b2012-10-28 21:37:00 +0100366 chip_addr[i]);
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200367 return -EINVAL;
368 }
369
Wolfram Sang6c427782017-05-16 13:21:05 +0200370 pr_info("Virtual chip at 0x%02x\n", chip_addr[i]);
Jean Delvare7a8d29c2006-08-13 23:46:44 +0200371 }
372
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200373 /* Allocate memory for all chips at once */
Jean Delvare1dff5982014-07-10 12:45:11 +0200374 stub_chips_nr = i;
375 stub_chips = kcalloc(stub_chips_nr, sizeof(struct stub_chip),
376 GFP_KERNEL);
Wolfram Sang6c427782017-05-16 13:21:05 +0200377 if (!stub_chips)
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200378 return -ENOMEM;
Wolfram Sang6c427782017-05-16 13:21:05 +0200379
Jean Delvare2408c172014-07-10 12:56:59 +0200380 for (i = 0; i < stub_chips_nr; i++) {
Guenter Roeck6f16b752014-07-17 09:56:03 -0700381 INIT_LIST_HEAD(&stub_chips[i].smbus_blocks);
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200382
Jean Delvare2408c172014-07-10 12:56:59 +0200383 /* Allocate extra memory for banked register ranges */
384 if (bank_mask[i]) {
385 ret = i2c_stub_allocate_banks(i);
386 if (ret)
387 goto fail_free;
388 }
389 }
390
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200391 ret = i2c_add_adapter(&stub_adapter);
392 if (ret)
Jean Delvare2408c172014-07-10 12:56:59 +0200393 goto fail_free;
394
395 return 0;
396
397 fail_free:
398 i2c_stub_free();
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200399 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
402static void __exit i2c_stub_exit(void)
403{
404 i2c_del_adapter(&stub_adapter);
Jean Delvare2408c172014-07-10 12:56:59 +0200405 i2c_stub_free();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
408MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
409MODULE_DESCRIPTION("I2C stub driver");
410MODULE_LICENSE("GPL");
411
412module_init(i2c_stub_init);
413module_exit(i2c_stub_exit);