blob: e0bb4655661d600a2c3e7519b0ad758e71a203f8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jean Delvare569be442008-01-27 18:14:45 +01002 i2c-stub.c - I2C/SMBus chip emulator
Linus Torvalds1da177e2005-04-16 15:20:36 -07003
4 Copyright (c) 2004 Mark M. Hoffman <mhoffman@lightlink.com>
Jean Delvare7c81c602014-01-29 20:40:08 +01005 Copyright (C) 2007, 2012 Jean Delvare <jdelvare@suse.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006
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 Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/init.h>
25#include <linux/module.h>
26#include <linux/kernel.h>
Jean Delvare9d90c1f2007-10-13 23:56:31 +020027#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/errno.h>
29#include <linux/i2c.h>
Guenter Roeck6f16b752014-07-17 09:56:03 -070030#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Jean Delvare9d90c1f2007-10-13 23:56:31 +020032#define MAX_CHIPS 10
Guenter Roeck6f16b752014-07-17 09:56:03 -070033
34/*
35 * Support for I2C_FUNC_SMBUS_BLOCK_DATA is disabled by default and must
36 * be enabled explicitly by setting the I2C_FUNC_SMBUS_BLOCK_DATA bits
37 * in the 'functionality' module parameter.
38 */
39#define STUB_FUNC_DEFAULT \
40 (I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE | \
41 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA | \
42 I2C_FUNC_SMBUS_I2C_BLOCK)
43
44#define STUB_FUNC_ALL \
45 (STUB_FUNC_DEFAULT | I2C_FUNC_SMBUS_BLOCK_DATA)
Jean Delvare7a8d29c2006-08-13 23:46:44 +020046
Jean Delvare9d90c1f2007-10-13 23:56:31 +020047static unsigned short chip_addr[MAX_CHIPS];
48module_param_array(chip_addr, ushort, NULL, S_IRUGO);
49MODULE_PARM_DESC(chip_addr,
Jean Delvare85d69312008-04-29 23:11:37 +020050 "Chip addresses (up to 10, between 0x03 and 0x77)");
Jean Delvare9d90c1f2007-10-13 23:56:31 +020051
Guenter Roeck6f16b752014-07-17 09:56:03 -070052static unsigned long functionality = STUB_FUNC_DEFAULT;
Jean Delvare38f41f22009-12-06 17:06:29 +010053module_param(functionality, ulong, S_IRUGO | S_IWUSR);
54MODULE_PARM_DESC(functionality, "Override functionality bitfield");
55
Guenter Roeck6f16b752014-07-17 09:56:03 -070056struct smbus_block_data {
57 struct list_head node;
58 u8 command;
59 u8 len;
60 u8 block[I2C_SMBUS_BLOCK_MAX];
61};
62
Jean Delvare9d90c1f2007-10-13 23:56:31 +020063struct stub_chip {
64 u8 pointer;
Jean Delvare569be442008-01-27 18:14:45 +010065 u16 words[256]; /* Byte operations use the LSB as per SMBus
66 specification */
Guenter Roeck6f16b752014-07-17 09:56:03 -070067 struct list_head smbus_blocks;
Jean Delvare9d90c1f2007-10-13 23:56:31 +020068};
69
70static struct stub_chip *stub_chips;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Guenter Roeck6f16b752014-07-17 09:56:03 -070072static struct smbus_block_data *stub_find_block(struct device *dev,
73 struct stub_chip *chip,
74 u8 command, bool create)
75{
76 struct smbus_block_data *b, *rb = NULL;
77
78 list_for_each_entry(b, &chip->smbus_blocks, node) {
79 if (b->command == command) {
80 rb = b;
81 break;
82 }
83 }
84 if (rb == NULL && create) {
85 rb = devm_kzalloc(dev, sizeof(*rb), GFP_KERNEL);
86 if (rb == NULL)
87 return rb;
88 rb->command = command;
89 list_add(&rb->node, &chip->smbus_blocks);
90 }
91 return rb;
92}
93
David Brownell97140342008-07-14 22:38:25 +020094/* Return negative errno on error. */
Jean Delvare31d178b2012-10-28 21:37:00 +010095static s32 stub_xfer(struct i2c_adapter *adap, u16 addr, unsigned short flags,
96 char read_write, u8 command, int size, union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
98 s32 ret;
Jean Delvare47103172009-12-06 17:06:28 +010099 int i, len;
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200100 struct stub_chip *chip = NULL;
Guenter Roeck6f16b752014-07-17 09:56:03 -0700101 struct smbus_block_data *b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200103 /* Search for the right chip */
104 for (i = 0; i < MAX_CHIPS && chip_addr[i]; i++) {
105 if (addr == chip_addr[i]) {
106 chip = stub_chips + i;
107 break;
108 }
109 }
110 if (!chip)
Jean Delvare7a8d29c2006-08-13 23:46:44 +0200111 return -ENODEV;
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 switch (size) {
114
115 case I2C_SMBUS_QUICK:
116 dev_dbg(&adap->dev, "smbus quick - addr 0x%02x\n", addr);
117 ret = 0;
118 break;
119
120 case I2C_SMBUS_BYTE:
121 if (read_write == I2C_SMBUS_WRITE) {
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200122 chip->pointer = command;
Jean Delvare31d178b2012-10-28 21:37:00 +0100123 dev_dbg(&adap->dev,
124 "smbus byte - addr 0x%02x, wrote 0x%02x.\n",
125 addr, command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 } else {
Jean Delvare569be442008-01-27 18:14:45 +0100127 data->byte = chip->words[chip->pointer++] & 0xff;
Jean Delvare31d178b2012-10-28 21:37:00 +0100128 dev_dbg(&adap->dev,
129 "smbus byte - addr 0x%02x, read 0x%02x.\n",
130 addr, data->byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 }
132
133 ret = 0;
134 break;
135
136 case I2C_SMBUS_BYTE_DATA:
137 if (read_write == I2C_SMBUS_WRITE) {
Jean Delvare569be442008-01-27 18:14:45 +0100138 chip->words[command] &= 0xff00;
139 chip->words[command] |= data->byte;
Jean Delvare31d178b2012-10-28 21:37:00 +0100140 dev_dbg(&adap->dev,
141 "smbus byte data - addr 0x%02x, wrote 0x%02x at 0x%02x.\n",
142 addr, data->byte, command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 } else {
Jean Delvare569be442008-01-27 18:14:45 +0100144 data->byte = chip->words[command] & 0xff;
Jean Delvare31d178b2012-10-28 21:37:00 +0100145 dev_dbg(&adap->dev,
146 "smbus byte data - addr 0x%02x, read 0x%02x at 0x%02x.\n",
147 addr, data->byte, command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 }
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200149 chip->pointer = command + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 ret = 0;
152 break;
153
154 case I2C_SMBUS_WORD_DATA:
155 if (read_write == I2C_SMBUS_WRITE) {
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200156 chip->words[command] = data->word;
Jean Delvare31d178b2012-10-28 21:37:00 +0100157 dev_dbg(&adap->dev,
158 "smbus word data - addr 0x%02x, wrote 0x%04x at 0x%02x.\n",
159 addr, data->word, command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 } else {
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200161 data->word = chip->words[command];
Jean Delvare31d178b2012-10-28 21:37:00 +0100162 dev_dbg(&adap->dev,
163 "smbus word data - addr 0x%02x, read 0x%04x at 0x%02x.\n",
164 addr, data->word, command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
166
167 ret = 0;
168 break;
169
Jean Delvare47103172009-12-06 17:06:28 +0100170 case I2C_SMBUS_I2C_BLOCK_DATA:
171 len = data->block[0];
172 if (read_write == I2C_SMBUS_WRITE) {
173 for (i = 0; i < len; i++) {
174 chip->words[command + i] &= 0xff00;
175 chip->words[command + i] |= data->block[1 + i];
176 }
Jean Delvare31d178b2012-10-28 21:37:00 +0100177 dev_dbg(&adap->dev,
178 "i2c block data - addr 0x%02x, wrote %d bytes at 0x%02x.\n",
179 addr, len, command);
Jean Delvare47103172009-12-06 17:06:28 +0100180 } else {
181 for (i = 0; i < len; i++) {
182 data->block[1 + i] =
183 chip->words[command + i] & 0xff;
184 }
Jean Delvare31d178b2012-10-28 21:37:00 +0100185 dev_dbg(&adap->dev,
186 "i2c block data - addr 0x%02x, read %d bytes at 0x%02x.\n",
187 addr, len, command);
Jean Delvare47103172009-12-06 17:06:28 +0100188 }
189
190 ret = 0;
191 break;
192
Guenter Roeck6f16b752014-07-17 09:56:03 -0700193 case I2C_SMBUS_BLOCK_DATA:
194 b = stub_find_block(&adap->dev, chip, command, false);
195 if (read_write == I2C_SMBUS_WRITE) {
196 len = data->block[0];
197 if (len == 0 || len > I2C_SMBUS_BLOCK_MAX) {
198 ret = -EINVAL;
199 break;
200 }
201 if (b == NULL) {
202 b = stub_find_block(&adap->dev, chip, command,
203 true);
204 if (b == NULL) {
205 ret = -ENOMEM;
206 break;
207 }
208 }
209 /* Largest write sets read block length */
210 if (len > b->len)
211 b->len = len;
212 for (i = 0; i < len; i++)
213 b->block[i] = data->block[i + 1];
214 /* update for byte and word commands */
215 chip->words[command] = (b->block[0] << 8) | b->len;
216 dev_dbg(&adap->dev,
217 "smbus block data - addr 0x%02x, wrote %d bytes at 0x%02x.\n",
218 addr, len, command);
219 } else {
220 if (b == NULL) {
221 dev_dbg(&adap->dev,
222 "SMBus block read command without prior block write not supported\n");
223 ret = -EOPNOTSUPP;
224 break;
225 }
226 len = b->len;
227 data->block[0] = len;
228 for (i = 0; i < len; i++)
229 data->block[i + 1] = b->block[i];
230 dev_dbg(&adap->dev,
231 "smbus block data - addr 0x%02x, read %d bytes at 0x%02x.\n",
232 addr, len, command);
233 }
234
235 ret = 0;
236 break;
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 default:
239 dev_dbg(&adap->dev, "Unsupported I2C/SMBus command\n");
David Brownell97140342008-07-14 22:38:25 +0200240 ret = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 break;
242 } /* switch (size) */
243
244 return ret;
245}
246
247static u32 stub_func(struct i2c_adapter *adapter)
248{
Guenter Roeck6f16b752014-07-17 09:56:03 -0700249 return STUB_FUNC_ALL & functionality;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
Jean Delvare8f9082c2006-09-03 22:39:46 +0200252static const struct i2c_algorithm smbus_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 .functionality = stub_func,
254 .smbus_xfer = stub_xfer,
255};
256
257static struct i2c_adapter stub_adapter = {
258 .owner = THIS_MODULE,
Jean Delvare3401b2f2008-07-14 22:38:29 +0200259 .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 .algo = &smbus_algorithm,
261 .name = "SMBus stub driver",
262};
263
264static int __init i2c_stub_init(void)
265{
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200266 int i, ret;
267
268 if (!chip_addr[0]) {
Jean Delvare31d178b2012-10-28 21:37:00 +0100269 pr_err("i2c-stub: Please specify a chip address\n");
Jean Delvare7a8d29c2006-08-13 23:46:44 +0200270 return -ENODEV;
271 }
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200272
273 for (i = 0; i < MAX_CHIPS && chip_addr[i]; i++) {
274 if (chip_addr[i] < 0x03 || chip_addr[i] > 0x77) {
Jean Delvare31d178b2012-10-28 21:37:00 +0100275 pr_err("i2c-stub: Invalid chip address 0x%02x\n",
276 chip_addr[i]);
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200277 return -EINVAL;
278 }
279
Jean Delvare31d178b2012-10-28 21:37:00 +0100280 pr_info("i2c-stub: Virtual chip at 0x%02x\n", chip_addr[i]);
Jean Delvare7a8d29c2006-08-13 23:46:44 +0200281 }
282
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200283 /* Allocate memory for all chips at once */
284 stub_chips = kzalloc(i * sizeof(struct stub_chip), GFP_KERNEL);
285 if (!stub_chips) {
Jean Delvare31d178b2012-10-28 21:37:00 +0100286 pr_err("i2c-stub: Out of memory\n");
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200287 return -ENOMEM;
288 }
Guenter Roeck6f16b752014-07-17 09:56:03 -0700289 for (i--; i >= 0; i--)
290 INIT_LIST_HEAD(&stub_chips[i].smbus_blocks);
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200291
292 ret = i2c_add_adapter(&stub_adapter);
293 if (ret)
294 kfree(stub_chips);
295 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
298static void __exit i2c_stub_exit(void)
299{
300 i2c_del_adapter(&stub_adapter);
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200301 kfree(stub_chips);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
304MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
305MODULE_DESCRIPTION("I2C stub driver");
306MODULE_LICENSE("GPL");
307
308module_init(i2c_stub_init);
309module_exit(i2c_stub_exit);