i2c-stub.c 10.3 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
/*
2
    i2c-stub.c - I2C/SMBus chip emulator
Linus Torvalds's avatar
Linus Torvalds committed
3 4

    Copyright (c) 2004 Mark M. Hoffman <mhoffman@lightlink.com>
5
    Copyright (C) 2007-2014 Jean Delvare <jdelvare@suse.de>
Linus Torvalds's avatar
Linus Torvalds committed
6 7 8 9 10 11 12 13 14 15 16 17

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
*/

18
#define DEBUG 1
Wolfram Sang's avatar
Wolfram Sang committed
19
#define pr_fmt(fmt) "i2c-stub: " fmt
Linus Torvalds's avatar
Linus Torvalds committed
20 21 22

#include <linux/errno.h>
#include <linux/i2c.h>
Wolfram Sang's avatar
Wolfram Sang committed
23 24
#include <linux/init.h>
#include <linux/kernel.h>
25
#include <linux/list.h>
Wolfram Sang's avatar
Wolfram Sang committed
26 27
#include <linux/module.h>
#include <linux/slab.h>
Linus Torvalds's avatar
Linus Torvalds committed
28

29
#define MAX_CHIPS 10
30 31 32 33 34 35 36 37 38 39 40 41 42

/*
 * Support for I2C_FUNC_SMBUS_BLOCK_DATA is disabled by default and must
 * be enabled explicitly by setting the I2C_FUNC_SMBUS_BLOCK_DATA bits
 * in the 'functionality' module parameter.
 */
#define STUB_FUNC_DEFAULT \
		(I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE | \
		 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA | \
		 I2C_FUNC_SMBUS_I2C_BLOCK)

#define STUB_FUNC_ALL \
		(STUB_FUNC_DEFAULT | I2C_FUNC_SMBUS_BLOCK_DATA)
43

44 45 46
static unsigned short chip_addr[MAX_CHIPS];
module_param_array(chip_addr, ushort, NULL, S_IRUGO);
MODULE_PARM_DESC(chip_addr,
47
		 "Chip addresses (up to 10, between 0x03 and 0x77)");
48

49
static unsigned long functionality = STUB_FUNC_DEFAULT;
50 51 52
module_param(functionality, ulong, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(functionality, "Override functionality bitfield");

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
/* Some chips have banked register ranges */

static u8 bank_reg[MAX_CHIPS];
module_param_array(bank_reg, byte, NULL, S_IRUGO);
MODULE_PARM_DESC(bank_reg, "Bank register");

static u8 bank_mask[MAX_CHIPS];
module_param_array(bank_mask, byte, NULL, S_IRUGO);
MODULE_PARM_DESC(bank_mask, "Bank value mask");

static u8 bank_start[MAX_CHIPS];
module_param_array(bank_start, byte, NULL, S_IRUGO);
MODULE_PARM_DESC(bank_start, "First banked register");

static u8 bank_end[MAX_CHIPS];
module_param_array(bank_end, byte, NULL, S_IRUGO);
MODULE_PARM_DESC(bank_end, "Last banked register");

71 72 73 74 75 76 77
struct smbus_block_data {
	struct list_head node;
	u8 command;
	u8 len;
	u8 block[I2C_SMBUS_BLOCK_MAX];
};

78 79
struct stub_chip {
	u8 pointer;
80 81
	u16 words[256];		/* Byte operations use the LSB as per SMBus
				   specification */
82
	struct list_head smbus_blocks;
83 84 85 86 87 88 89 90 91 92

	/* For chips with banks, extra registers are allocated dynamically */
	u8 bank_reg;
	u8 bank_shift;
	u8 bank_mask;
	u8 bank_sel;		/* Currently selected bank */
	u8 bank_start;
	u8 bank_end;
	u16 bank_size;
	u16 *bank_words;	/* Room for bank_mask * bank_size registers */
93 94 95
};

static struct stub_chip *stub_chips;
96
static int stub_chips_nr;
Linus Torvalds's avatar
Linus Torvalds committed
97

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
static struct smbus_block_data *stub_find_block(struct device *dev,
						struct stub_chip *chip,
						u8 command, bool create)
{
	struct smbus_block_data *b, *rb = NULL;

	list_for_each_entry(b, &chip->smbus_blocks, node) {
		if (b->command == command) {
			rb = b;
			break;
		}
	}
	if (rb == NULL && create) {
		rb = devm_kzalloc(dev, sizeof(*rb), GFP_KERNEL);
		if (rb == NULL)
			return rb;
		rb->command = command;
		list_add(&rb->node, &chip->smbus_blocks);
	}
	return rb;
}

120 121 122 123 124 125 126 127 128 129 130
static u16 *stub_get_wordp(struct stub_chip *chip, u8 offset)
{
	if (chip->bank_sel &&
	    offset >= chip->bank_start && offset <= chip->bank_end)
		return chip->bank_words +
		       (chip->bank_sel - 1) * chip->bank_size +
		       offset - chip->bank_start;
	else
		return chip->words + offset;
}

131
/* Return negative errno on error. */
Jean Delvare's avatar
Jean Delvare committed
132 133
static s32 stub_xfer(struct i2c_adapter *adap, u16 addr, unsigned short flags,
	char read_write, u8 command, int size, union i2c_smbus_data *data)
Linus Torvalds's avatar
Linus Torvalds committed
134 135
{
	s32 ret;
136
	int i, len;
137
	struct stub_chip *chip = NULL;
138
	struct smbus_block_data *b;
139
	u16 *wordp;
140 141

	/* Search for the right chip */
142
	for (i = 0; i < stub_chips_nr; i++) {
143 144 145 146 147 148
		if (addr == chip_addr[i]) {
			chip = stub_chips + i;
			break;
		}
	}
	if (!chip)
149 150
		return -ENODEV;

Linus Torvalds's avatar
Linus Torvalds committed
151 152 153 154 155 156 157 158 159
	switch (size) {

	case I2C_SMBUS_QUICK:
		dev_dbg(&adap->dev, "smbus quick - addr 0x%02x\n", addr);
		ret = 0;
		break;

	case I2C_SMBUS_BYTE:
		if (read_write == I2C_SMBUS_WRITE) {
160
			chip->pointer = command;
Jean Delvare's avatar
Jean Delvare committed
161 162 163
			dev_dbg(&adap->dev,
				"smbus byte - addr 0x%02x, wrote 0x%02x.\n",
				addr, command);
Linus Torvalds's avatar
Linus Torvalds committed
164
		} else {
165 166
			wordp = stub_get_wordp(chip, chip->pointer++);
			data->byte = *wordp & 0xff;
Jean Delvare's avatar
Jean Delvare committed
167 168 169
			dev_dbg(&adap->dev,
				"smbus byte - addr 0x%02x, read  0x%02x.\n",
				addr, data->byte);
Linus Torvalds's avatar
Linus Torvalds committed
170 171 172 173 174 175
		}

		ret = 0;
		break;

	case I2C_SMBUS_BYTE_DATA:
176
		wordp = stub_get_wordp(chip, command);
Linus Torvalds's avatar
Linus Torvalds committed
177
		if (read_write == I2C_SMBUS_WRITE) {
178 179
			*wordp &= 0xff00;
			*wordp |= data->byte;
Jean Delvare's avatar
Jean Delvare committed
180 181 182
			dev_dbg(&adap->dev,
				"smbus byte data - addr 0x%02x, wrote 0x%02x at 0x%02x.\n",
				addr, data->byte, command);
183 184 185 186 187 188 189 190 191 192

			/* Set the bank as needed */
			if (chip->bank_words && command == chip->bank_reg) {
				chip->bank_sel =
					(data->byte >> chip->bank_shift)
					& chip->bank_mask;
				dev_dbg(&adap->dev,
					"switching to bank %u.\n",
					chip->bank_sel);
			}
Linus Torvalds's avatar
Linus Torvalds committed
193
		} else {
194
			data->byte = *wordp & 0xff;
Jean Delvare's avatar
Jean Delvare committed
195 196 197
			dev_dbg(&adap->dev,
				"smbus byte data - addr 0x%02x, read  0x%02x at 0x%02x.\n",
				addr, data->byte, command);
Linus Torvalds's avatar
Linus Torvalds committed
198
		}
199
		chip->pointer = command + 1;
Linus Torvalds's avatar
Linus Torvalds committed
200 201 202 203 204

		ret = 0;
		break;

	case I2C_SMBUS_WORD_DATA:
205
		wordp = stub_get_wordp(chip, command);
Linus Torvalds's avatar
Linus Torvalds committed
206
		if (read_write == I2C_SMBUS_WRITE) {
207
			*wordp = data->word;
Jean Delvare's avatar
Jean Delvare committed
208 209 210
			dev_dbg(&adap->dev,
				"smbus word data - addr 0x%02x, wrote 0x%04x at 0x%02x.\n",
				addr, data->word, command);
Linus Torvalds's avatar
Linus Torvalds committed
211
		} else {
212
			data->word = *wordp;
Jean Delvare's avatar
Jean Delvare committed
213 214 215
			dev_dbg(&adap->dev,
				"smbus word data - addr 0x%02x, read  0x%04x at 0x%02x.\n",
				addr, data->word, command);
Linus Torvalds's avatar
Linus Torvalds committed
216 217 218 219 220
		}

		ret = 0;
		break;

221
	case I2C_SMBUS_I2C_BLOCK_DATA:
222 223 224 225
		/*
		 * We ignore banks here, because banked chips don't use I2C
		 * block transfers
		 */
226 227
		if (data->block[0] > 256 - command)	/* Avoid overrun */
			data->block[0] = 256 - command;
228 229 230 231 232 233
		len = data->block[0];
		if (read_write == I2C_SMBUS_WRITE) {
			for (i = 0; i < len; i++) {
				chip->words[command + i] &= 0xff00;
				chip->words[command + i] |= data->block[1 + i];
			}
Jean Delvare's avatar
Jean Delvare committed
234 235 236
			dev_dbg(&adap->dev,
				"i2c block data - addr 0x%02x, wrote %d bytes at 0x%02x.\n",
				addr, len, command);
237 238 239 240 241
		} else {
			for (i = 0; i < len; i++) {
				data->block[1 + i] =
					chip->words[command + i] & 0xff;
			}
Jean Delvare's avatar
Jean Delvare committed
242 243 244
			dev_dbg(&adap->dev,
				"i2c block data - addr 0x%02x, read  %d bytes at 0x%02x.\n",
				addr, len, command);
245 246 247 248 249
		}

		ret = 0;
		break;

250
	case I2C_SMBUS_BLOCK_DATA:
251 252 253 254
		/*
		 * We ignore banks here, because chips typically don't use both
		 * banks and SMBus block transfers
		 */
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
		b = stub_find_block(&adap->dev, chip, command, false);
		if (read_write == I2C_SMBUS_WRITE) {
			len = data->block[0];
			if (len == 0 || len > I2C_SMBUS_BLOCK_MAX) {
				ret = -EINVAL;
				break;
			}
			if (b == NULL) {
				b = stub_find_block(&adap->dev, chip, command,
						    true);
				if (b == NULL) {
					ret = -ENOMEM;
					break;
				}
			}
			/* Largest write sets read block length */
			if (len > b->len)
				b->len = len;
			for (i = 0; i < len; i++)
				b->block[i] = data->block[i + 1];
			/* update for byte and word commands */
			chip->words[command] = (b->block[0] << 8) | b->len;
			dev_dbg(&adap->dev,
				"smbus block data - addr 0x%02x, wrote %d bytes at 0x%02x.\n",
				addr, len, command);
		} else {
			if (b == NULL) {
				dev_dbg(&adap->dev,
					"SMBus block read command without prior block write not supported\n");
				ret = -EOPNOTSUPP;
				break;
			}
			len = b->len;
			data->block[0] = len;
			for (i = 0; i < len; i++)
				data->block[i + 1] = b->block[i];
			dev_dbg(&adap->dev,
				"smbus block data - addr 0x%02x, read  %d bytes at 0x%02x.\n",
				addr, len, command);
		}

		ret = 0;
		break;

Linus Torvalds's avatar
Linus Torvalds committed
299 300
	default:
		dev_dbg(&adap->dev, "Unsupported I2C/SMBus command\n");
301
		ret = -EOPNOTSUPP;
Linus Torvalds's avatar
Linus Torvalds committed
302 303 304 305 306 307 308 309
		break;
	} /* switch (size) */

	return ret;
}

static u32 stub_func(struct i2c_adapter *adapter)
{
310
	return STUB_FUNC_ALL & functionality;
Linus Torvalds's avatar
Linus Torvalds committed
311 312
}

313
static const struct i2c_algorithm smbus_algorithm = {
Linus Torvalds's avatar
Linus Torvalds committed
314 315 316 317 318 319
	.functionality	= stub_func,
	.smbus_xfer	= stub_xfer,
};

static struct i2c_adapter stub_adapter = {
	.owner		= THIS_MODULE,
320
	.class		= I2C_CLASS_HWMON | I2C_CLASS_SPD,
Linus Torvalds's avatar
Linus Torvalds committed
321 322 323 324
	.algo		= &smbus_algorithm,
	.name		= "SMBus stub driver",
};

325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
static int __init i2c_stub_allocate_banks(int i)
{
	struct stub_chip *chip = stub_chips + i;

	chip->bank_reg = bank_reg[i];
	chip->bank_start = bank_start[i];
	chip->bank_end = bank_end[i];
	chip->bank_size = bank_end[i] - bank_start[i] + 1;

	/* We assume that all bits in the mask are contiguous */
	chip->bank_mask = bank_mask[i];
	while (!(chip->bank_mask & 1)) {
		chip->bank_shift++;
		chip->bank_mask >>= 1;
	}

Kees Cook's avatar
Kees Cook committed
341 342 343
	chip->bank_words = kcalloc(chip->bank_mask * chip->bank_size,
				   sizeof(u16),
				   GFP_KERNEL);
344 345 346
	if (!chip->bank_words)
		return -ENOMEM;

Wolfram Sang's avatar
Wolfram Sang committed
347
	pr_debug("Allocated %u banks of %u words each (registers 0x%02x to 0x%02x)\n",
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
		 chip->bank_mask, chip->bank_size, chip->bank_start,
		 chip->bank_end);

	return 0;
}

static void i2c_stub_free(void)
{
	int i;

	for (i = 0; i < stub_chips_nr; i++)
		kfree(stub_chips[i].bank_words);
	kfree(stub_chips);
}

Linus Torvalds's avatar
Linus Torvalds committed
363 364
static int __init i2c_stub_init(void)
{
365 366 367
	int i, ret;

	if (!chip_addr[0]) {
Wolfram Sang's avatar
Wolfram Sang committed
368
		pr_err("Please specify a chip address\n");
369 370
		return -ENODEV;
	}
371 372 373

	for (i = 0; i < MAX_CHIPS && chip_addr[i]; i++) {
		if (chip_addr[i] < 0x03 || chip_addr[i] > 0x77) {
Wolfram Sang's avatar
Wolfram Sang committed
374
			pr_err("Invalid chip address 0x%02x\n",
Jean Delvare's avatar
Jean Delvare committed
375
			       chip_addr[i]);
376 377 378
			return -EINVAL;
		}

Wolfram Sang's avatar
Wolfram Sang committed
379
		pr_info("Virtual chip at 0x%02x\n", chip_addr[i]);
380 381
	}

382
	/* Allocate memory for all chips at once */
383 384 385
	stub_chips_nr = i;
	stub_chips = kcalloc(stub_chips_nr, sizeof(struct stub_chip),
			     GFP_KERNEL);
Wolfram Sang's avatar
Wolfram Sang committed
386
	if (!stub_chips)
387
		return -ENOMEM;
Wolfram Sang's avatar
Wolfram Sang committed
388

389
	for (i = 0; i < stub_chips_nr; i++) {
390
		INIT_LIST_HEAD(&stub_chips[i].smbus_blocks);
391

392 393 394 395 396 397 398 399
		/* Allocate extra memory for banked register ranges */
		if (bank_mask[i]) {
			ret = i2c_stub_allocate_banks(i);
			if (ret)
				goto fail_free;
		}
	}

400 401
	ret = i2c_add_adapter(&stub_adapter);
	if (ret)
402 403 404 405 406 407
		goto fail_free;

	return 0;

 fail_free:
	i2c_stub_free();
408
	return ret;
Linus Torvalds's avatar
Linus Torvalds committed
409 410 411 412 413
}

static void __exit i2c_stub_exit(void)
{
	i2c_del_adapter(&stub_adapter);
414
	i2c_stub_free();
Linus Torvalds's avatar
Linus Torvalds committed
415 416 417 418 419 420 421 422
}

MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
MODULE_DESCRIPTION("I2C stub driver");
MODULE_LICENSE("GPL");

module_init(i2c_stub_init);
module_exit(i2c_stub_exit);