Commit 2408c17f authored by Jean Delvare's avatar Jean Delvare Committed by Wolfram Sang

i2c: stub: Add support for banked register ranges

Some chips implement banked register ranges. This allows implementing
more registers than the limited 8-bit address space originally allows.
In order to access a register on these chips, you must first select
the proper bank. Add support for this mechanism to the i2c-stub driver
so that such chips can be emulated. All the bank settings are passed
as module parameters.
Signed-off-by: default avatarJean Delvare <jdelvare@suse.de>
Tested-by: default avatarGuenter Roeck <linux@roeck-us.net>
Signed-off-by: default avatarWolfram Sang <wsa@the-dreams.de>
parent 1dff5983
...@@ -47,15 +47,18 @@ unsigned long functionality: ...@@ -47,15 +47,18 @@ unsigned long functionality:
value 0x1f0000 would only enable the quick, byte and byte data value 0x1f0000 would only enable the quick, byte and byte data
commands. commands.
u8 bank_reg[10]
u8 bank_mask[10]
u8 bank_start[10]
u8 bank_end[10]:
Optional bank settings. They tell which bits in which register
select the active bank, as well as the range of banked registers.
CAVEATS: CAVEATS:
If your target driver polls some byte or word waiting for it to change, the If your target driver polls some byte or word waiting for it to change, the
stub could lock it up. Use i2cset to unlock it. stub could lock it up. Use i2cset to unlock it.
If the hardware for your driver has banked registers (e.g. Winbond sensors
chips) this module will not work well - although it could be extended to
support that pretty easily.
If you spam it hard enough, printk can be lossy. This module really wants If you spam it hard enough, printk can be lossy. This module really wants
something like relayfs. something like relayfs.
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
i2c-stub.c - I2C/SMBus chip emulator i2c-stub.c - I2C/SMBus chip emulator
Copyright (c) 2004 Mark M. Hoffman <mhoffman@lightlink.com> Copyright (c) 2004 Mark M. Hoffman <mhoffman@lightlink.com>
Copyright (C) 2007, 2012 Jean Delvare <jdelvare@suse.de> Copyright (C) 2007-2014 Jean Delvare <jdelvare@suse.de>
This program is free software; you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
...@@ -53,6 +53,24 @@ static unsigned long functionality = STUB_FUNC_DEFAULT; ...@@ -53,6 +53,24 @@ static unsigned long functionality = STUB_FUNC_DEFAULT;
module_param(functionality, ulong, S_IRUGO | S_IWUSR); module_param(functionality, ulong, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(functionality, "Override functionality bitfield"); MODULE_PARM_DESC(functionality, "Override functionality bitfield");
/* 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");
struct smbus_block_data { struct smbus_block_data {
struct list_head node; struct list_head node;
u8 command; u8 command;
...@@ -65,6 +83,16 @@ struct stub_chip { ...@@ -65,6 +83,16 @@ struct stub_chip {
u16 words[256]; /* Byte operations use the LSB as per SMBus u16 words[256]; /* Byte operations use the LSB as per SMBus
specification */ specification */
struct list_head smbus_blocks; struct list_head smbus_blocks;
/* 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 */
}; };
static struct stub_chip *stub_chips; static struct stub_chip *stub_chips;
...@@ -92,6 +120,17 @@ static struct smbus_block_data *stub_find_block(struct device *dev, ...@@ -92,6 +120,17 @@ static struct smbus_block_data *stub_find_block(struct device *dev,
return rb; return rb;
} }
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;
}
/* Return negative errno on error. */ /* Return negative errno on error. */
static s32 stub_xfer(struct i2c_adapter *adap, u16 addr, unsigned short flags, 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) char read_write, u8 command, int size, union i2c_smbus_data *data)
...@@ -100,6 +139,7 @@ static s32 stub_xfer(struct i2c_adapter *adap, u16 addr, unsigned short flags, ...@@ -100,6 +139,7 @@ static s32 stub_xfer(struct i2c_adapter *adap, u16 addr, unsigned short flags,
int i, len; int i, len;
struct stub_chip *chip = NULL; struct stub_chip *chip = NULL;
struct smbus_block_data *b; struct smbus_block_data *b;
u16 *wordp;
/* Search for the right chip */ /* Search for the right chip */
for (i = 0; i < stub_chips_nr; i++) { for (i = 0; i < stub_chips_nr; i++) {
...@@ -125,7 +165,8 @@ static s32 stub_xfer(struct i2c_adapter *adap, u16 addr, unsigned short flags, ...@@ -125,7 +165,8 @@ static s32 stub_xfer(struct i2c_adapter *adap, u16 addr, unsigned short flags,
"smbus byte - addr 0x%02x, wrote 0x%02x.\n", "smbus byte - addr 0x%02x, wrote 0x%02x.\n",
addr, command); addr, command);
} else { } else {
data->byte = chip->words[chip->pointer++] & 0xff; wordp = stub_get_wordp(chip, chip->pointer++);
data->byte = *wordp & 0xff;
dev_dbg(&adap->dev, dev_dbg(&adap->dev,
"smbus byte - addr 0x%02x, read 0x%02x.\n", "smbus byte - addr 0x%02x, read 0x%02x.\n",
addr, data->byte); addr, data->byte);
...@@ -135,14 +176,25 @@ static s32 stub_xfer(struct i2c_adapter *adap, u16 addr, unsigned short flags, ...@@ -135,14 +176,25 @@ static s32 stub_xfer(struct i2c_adapter *adap, u16 addr, unsigned short flags,
break; break;
case I2C_SMBUS_BYTE_DATA: case I2C_SMBUS_BYTE_DATA:
wordp = stub_get_wordp(chip, command);
if (read_write == I2C_SMBUS_WRITE) { if (read_write == I2C_SMBUS_WRITE) {
chip->words[command] &= 0xff00; *wordp &= 0xff00;
chip->words[command] |= data->byte; *wordp |= data->byte;
dev_dbg(&adap->dev, dev_dbg(&adap->dev,
"smbus byte data - addr 0x%02x, wrote 0x%02x at 0x%02x.\n", "smbus byte data - addr 0x%02x, wrote 0x%02x at 0x%02x.\n",
addr, data->byte, command); addr, data->byte, command);
/* 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);
}
} else { } else {
data->byte = chip->words[command] & 0xff; data->byte = *wordp & 0xff;
dev_dbg(&adap->dev, dev_dbg(&adap->dev,
"smbus byte data - addr 0x%02x, read 0x%02x at 0x%02x.\n", "smbus byte data - addr 0x%02x, read 0x%02x at 0x%02x.\n",
addr, data->byte, command); addr, data->byte, command);
...@@ -153,13 +205,14 @@ static s32 stub_xfer(struct i2c_adapter *adap, u16 addr, unsigned short flags, ...@@ -153,13 +205,14 @@ static s32 stub_xfer(struct i2c_adapter *adap, u16 addr, unsigned short flags,
break; break;
case I2C_SMBUS_WORD_DATA: case I2C_SMBUS_WORD_DATA:
wordp = stub_get_wordp(chip, command);
if (read_write == I2C_SMBUS_WRITE) { if (read_write == I2C_SMBUS_WRITE) {
chip->words[command] = data->word; *wordp = data->word;
dev_dbg(&adap->dev, dev_dbg(&adap->dev,
"smbus word data - addr 0x%02x, wrote 0x%04x at 0x%02x.\n", "smbus word data - addr 0x%02x, wrote 0x%04x at 0x%02x.\n",
addr, data->word, command); addr, data->word, command);
} else { } else {
data->word = chip->words[command]; data->word = *wordp;
dev_dbg(&adap->dev, dev_dbg(&adap->dev,
"smbus word data - addr 0x%02x, read 0x%04x at 0x%02x.\n", "smbus word data - addr 0x%02x, read 0x%04x at 0x%02x.\n",
addr, data->word, command); addr, data->word, command);
...@@ -169,6 +222,10 @@ static s32 stub_xfer(struct i2c_adapter *adap, u16 addr, unsigned short flags, ...@@ -169,6 +222,10 @@ static s32 stub_xfer(struct i2c_adapter *adap, u16 addr, unsigned short flags,
break; break;
case I2C_SMBUS_I2C_BLOCK_DATA: case I2C_SMBUS_I2C_BLOCK_DATA:
/*
* We ignore banks here, because banked chips don't use I2C
* block transfers
*/
len = data->block[0]; len = data->block[0];
if (read_write == I2C_SMBUS_WRITE) { if (read_write == I2C_SMBUS_WRITE) {
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
...@@ -192,6 +249,10 @@ static s32 stub_xfer(struct i2c_adapter *adap, u16 addr, unsigned short flags, ...@@ -192,6 +249,10 @@ static s32 stub_xfer(struct i2c_adapter *adap, u16 addr, unsigned short flags,
break; break;
case I2C_SMBUS_BLOCK_DATA: case I2C_SMBUS_BLOCK_DATA:
/*
* We ignore banks here, because chips typically don't use both
* banks and SMBus block transfers
*/
b = stub_find_block(&adap->dev, chip, command, false); b = stub_find_block(&adap->dev, chip, command, false);
if (read_write == I2C_SMBUS_WRITE) { if (read_write == I2C_SMBUS_WRITE) {
len = data->block[0]; len = data->block[0];
...@@ -262,6 +323,43 @@ static struct i2c_adapter stub_adapter = { ...@@ -262,6 +323,43 @@ static struct i2c_adapter stub_adapter = {
.name = "SMBus stub driver", .name = "SMBus stub driver",
}; };
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;
}
chip->bank_words = kzalloc(chip->bank_mask * chip->bank_size *
sizeof(u16), GFP_KERNEL);
if (!chip->bank_words)
return -ENOMEM;
pr_debug("i2c-stub: Allocated %u banks of %u words each (registers 0x%02x to 0x%02x)\n",
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);
}
static int __init i2c_stub_init(void) static int __init i2c_stub_init(void)
{ {
int i, ret; int i, ret;
...@@ -289,19 +387,32 @@ static int __init i2c_stub_init(void) ...@@ -289,19 +387,32 @@ static int __init i2c_stub_init(void)
pr_err("i2c-stub: Out of memory\n"); pr_err("i2c-stub: Out of memory\n");
return -ENOMEM; return -ENOMEM;
} }
for (i = 0; i < stub_chips_nr; i++) for (i = 0; i < stub_chips_nr; i++) {
INIT_LIST_HEAD(&stub_chips[i].smbus_blocks); INIT_LIST_HEAD(&stub_chips[i].smbus_blocks);
/* Allocate extra memory for banked register ranges */
if (bank_mask[i]) {
ret = i2c_stub_allocate_banks(i);
if (ret)
goto fail_free;
}
}
ret = i2c_add_adapter(&stub_adapter); ret = i2c_add_adapter(&stub_adapter);
if (ret) if (ret)
kfree(stub_chips); goto fail_free;
return 0;
fail_free:
i2c_stub_free();
return ret; return ret;
} }
static void __exit i2c_stub_exit(void) static void __exit i2c_stub_exit(void)
{ {
i2c_del_adapter(&stub_adapter); i2c_del_adapter(&stub_adapter);
kfree(stub_chips); i2c_stub_free();
} }
MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>"); MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment