Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Jean Delvare | 569be44 | 2008-01-27 18:14:45 +0100 | [diff] [blame] | 2 | i2c-stub.c - I2C/SMBus chip emulator |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | |
| 4 | Copyright (c) 2004 Mark M. Hoffman <mhoffman@lightlink.com> |
Jean Delvare | 569be44 | 2008-01-27 18:14:45 +0100 | [diff] [blame] | 5 | Copyright (C) 2007 Jean Delvare <khali@linux-fr.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 2 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program; if not, write to the Free Software |
| 19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | */ |
| 21 | |
| 22 | #define DEBUG 1 |
| 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/init.h> |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/kernel.h> |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 27 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/errno.h> |
| 29 | #include <linux/i2c.h> |
| 30 | |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 31 | #define MAX_CHIPS 10 |
Jean Delvare | 7a8d29c | 2006-08-13 23:46:44 +0200 | [diff] [blame] | 32 | |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 33 | static unsigned short chip_addr[MAX_CHIPS]; |
| 34 | module_param_array(chip_addr, ushort, NULL, S_IRUGO); |
| 35 | MODULE_PARM_DESC(chip_addr, |
Jean Delvare | 85d6931 | 2008-04-29 23:11:37 +0200 | [diff] [blame] | 36 | "Chip addresses (up to 10, between 0x03 and 0x77)"); |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 37 | |
| 38 | struct stub_chip { |
| 39 | u8 pointer; |
Jean Delvare | 569be44 | 2008-01-27 18:14:45 +0100 | [diff] [blame] | 40 | u16 words[256]; /* Byte operations use the LSB as per SMBus |
| 41 | specification */ |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | static struct stub_chip *stub_chips; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
David Brownell | 9714034 | 2008-07-14 22:38:25 +0200 | [diff] [blame] | 46 | /* Return negative errno on error. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | static s32 stub_xfer(struct i2c_adapter * adap, u16 addr, unsigned short flags, |
| 48 | char read_write, u8 command, int size, union i2c_smbus_data * data) |
| 49 | { |
| 50 | s32 ret; |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 51 | int i; |
| 52 | struct stub_chip *chip = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 54 | /* Search for the right chip */ |
| 55 | for (i = 0; i < MAX_CHIPS && chip_addr[i]; i++) { |
| 56 | if (addr == chip_addr[i]) { |
| 57 | chip = stub_chips + i; |
| 58 | break; |
| 59 | } |
| 60 | } |
| 61 | if (!chip) |
Jean Delvare | 7a8d29c | 2006-08-13 23:46:44 +0200 | [diff] [blame] | 62 | return -ENODEV; |
| 63 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | switch (size) { |
| 65 | |
| 66 | case I2C_SMBUS_QUICK: |
| 67 | dev_dbg(&adap->dev, "smbus quick - addr 0x%02x\n", addr); |
| 68 | ret = 0; |
| 69 | break; |
| 70 | |
| 71 | case I2C_SMBUS_BYTE: |
| 72 | if (read_write == I2C_SMBUS_WRITE) { |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 73 | chip->pointer = command; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | dev_dbg(&adap->dev, "smbus byte - addr 0x%02x, " |
| 75 | "wrote 0x%02x.\n", |
| 76 | addr, command); |
| 77 | } else { |
Jean Delvare | 569be44 | 2008-01-27 18:14:45 +0100 | [diff] [blame] | 78 | data->byte = chip->words[chip->pointer++] & 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | dev_dbg(&adap->dev, "smbus byte - addr 0x%02x, " |
| 80 | "read 0x%02x.\n", |
| 81 | addr, data->byte); |
| 82 | } |
| 83 | |
| 84 | ret = 0; |
| 85 | break; |
| 86 | |
| 87 | case I2C_SMBUS_BYTE_DATA: |
| 88 | if (read_write == I2C_SMBUS_WRITE) { |
Jean Delvare | 569be44 | 2008-01-27 18:14:45 +0100 | [diff] [blame] | 89 | chip->words[command] &= 0xff00; |
| 90 | chip->words[command] |= data->byte; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | dev_dbg(&adap->dev, "smbus byte data - addr 0x%02x, " |
| 92 | "wrote 0x%02x at 0x%02x.\n", |
| 93 | addr, data->byte, command); |
| 94 | } else { |
Jean Delvare | 569be44 | 2008-01-27 18:14:45 +0100 | [diff] [blame] | 95 | data->byte = chip->words[command] & 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | dev_dbg(&adap->dev, "smbus byte data - addr 0x%02x, " |
| 97 | "read 0x%02x at 0x%02x.\n", |
| 98 | addr, data->byte, command); |
| 99 | } |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 100 | chip->pointer = command + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
| 102 | ret = 0; |
| 103 | break; |
| 104 | |
| 105 | case I2C_SMBUS_WORD_DATA: |
| 106 | if (read_write == I2C_SMBUS_WRITE) { |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 107 | chip->words[command] = data->word; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | dev_dbg(&adap->dev, "smbus word data - addr 0x%02x, " |
| 109 | "wrote 0x%04x at 0x%02x.\n", |
| 110 | addr, data->word, command); |
| 111 | } else { |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 112 | data->word = chip->words[command]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | dev_dbg(&adap->dev, "smbus word data - addr 0x%02x, " |
| 114 | "read 0x%04x at 0x%02x.\n", |
| 115 | addr, data->word, command); |
| 116 | } |
| 117 | |
| 118 | ret = 0; |
| 119 | break; |
| 120 | |
| 121 | default: |
| 122 | dev_dbg(&adap->dev, "Unsupported I2C/SMBus command\n"); |
David Brownell | 9714034 | 2008-07-14 22:38:25 +0200 | [diff] [blame] | 123 | ret = -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | break; |
| 125 | } /* switch (size) */ |
| 126 | |
| 127 | return ret; |
| 128 | } |
| 129 | |
| 130 | static u32 stub_func(struct i2c_adapter *adapter) |
| 131 | { |
| 132 | return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE | |
| 133 | I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA; |
| 134 | } |
| 135 | |
Jean Delvare | 8f9082c | 2006-09-03 22:39:46 +0200 | [diff] [blame] | 136 | static const struct i2c_algorithm smbus_algorithm = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | .functionality = stub_func, |
| 138 | .smbus_xfer = stub_xfer, |
| 139 | }; |
| 140 | |
| 141 | static struct i2c_adapter stub_adapter = { |
| 142 | .owner = THIS_MODULE, |
Jean Delvare | 3401b2f | 2008-07-14 22:38:29 +0200 | [diff] [blame] | 143 | .class = I2C_CLASS_HWMON | I2C_CLASS_SPD, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | .algo = &smbus_algorithm, |
| 145 | .name = "SMBus stub driver", |
| 146 | }; |
| 147 | |
| 148 | static int __init i2c_stub_init(void) |
| 149 | { |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 150 | int i, ret; |
| 151 | |
| 152 | if (!chip_addr[0]) { |
Jean Delvare | 7a8d29c | 2006-08-13 23:46:44 +0200 | [diff] [blame] | 153 | printk(KERN_ERR "i2c-stub: Please specify a chip address\n"); |
| 154 | return -ENODEV; |
| 155 | } |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 156 | |
| 157 | for (i = 0; i < MAX_CHIPS && chip_addr[i]; i++) { |
| 158 | if (chip_addr[i] < 0x03 || chip_addr[i] > 0x77) { |
| 159 | printk(KERN_ERR "i2c-stub: Invalid chip address " |
| 160 | "0x%02x\n", chip_addr[i]); |
| 161 | return -EINVAL; |
| 162 | } |
| 163 | |
| 164 | printk(KERN_INFO "i2c-stub: Virtual chip at 0x%02x\n", |
| 165 | chip_addr[i]); |
Jean Delvare | 7a8d29c | 2006-08-13 23:46:44 +0200 | [diff] [blame] | 166 | } |
| 167 | |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 168 | /* Allocate memory for all chips at once */ |
| 169 | stub_chips = kzalloc(i * sizeof(struct stub_chip), GFP_KERNEL); |
| 170 | if (!stub_chips) { |
| 171 | printk(KERN_ERR "i2c-stub: Out of memory\n"); |
| 172 | return -ENOMEM; |
| 173 | } |
| 174 | |
| 175 | ret = i2c_add_adapter(&stub_adapter); |
| 176 | if (ret) |
| 177 | kfree(stub_chips); |
| 178 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | static void __exit i2c_stub_exit(void) |
| 182 | { |
| 183 | i2c_del_adapter(&stub_adapter); |
Jean Delvare | 9d90c1f | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 184 | kfree(stub_chips); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>"); |
| 188 | MODULE_DESCRIPTION("I2C stub driver"); |
| 189 | MODULE_LICENSE("GPL"); |
| 190 | |
| 191 | module_init(i2c_stub_init); |
| 192 | module_exit(i2c_stub_exit); |
| 193 | |