blob: b1b3447942c9efd42c30ed7a9e271f00268db127 [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 Delvare569be442008-01-27 18:14:45 +01005 Copyright (C) 2007 Jean Delvare <khali@linux-fr.org>
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>
30
Jean Delvare9d90c1f2007-10-13 23:56:31 +020031#define MAX_CHIPS 10
Jean Delvarec5aa6922010-05-21 18:40:56 +020032#define STUB_FUNC (I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE | \
33 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA | \
34 I2C_FUNC_SMBUS_I2C_BLOCK)
Jean Delvare7a8d29c2006-08-13 23:46:44 +020035
Jean Delvare9d90c1f2007-10-13 23:56:31 +020036static unsigned short chip_addr[MAX_CHIPS];
37module_param_array(chip_addr, ushort, NULL, S_IRUGO);
38MODULE_PARM_DESC(chip_addr,
Jean Delvare85d69312008-04-29 23:11:37 +020039 "Chip addresses (up to 10, between 0x03 and 0x77)");
Jean Delvare9d90c1f2007-10-13 23:56:31 +020040
Jean Delvarec5aa6922010-05-21 18:40:56 +020041static unsigned long functionality = STUB_FUNC;
Jean Delvare38f41f22009-12-06 17:06:29 +010042module_param(functionality, ulong, S_IRUGO | S_IWUSR);
43MODULE_PARM_DESC(functionality, "Override functionality bitfield");
44
Jean Delvare9d90c1f2007-10-13 23:56:31 +020045struct stub_chip {
46 u8 pointer;
Jean Delvare569be442008-01-27 18:14:45 +010047 u16 words[256]; /* Byte operations use the LSB as per SMBus
48 specification */
Jean Delvare9d90c1f2007-10-13 23:56:31 +020049};
50
51static struct stub_chip *stub_chips;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
David Brownell97140342008-07-14 22:38:25 +020053/* Return negative errno on error. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static s32 stub_xfer(struct i2c_adapter * adap, u16 addr, unsigned short flags,
55 char read_write, u8 command, int size, union i2c_smbus_data * data)
56{
57 s32 ret;
Jean Delvare47103172009-12-06 17:06:28 +010058 int i, len;
Jean Delvare9d90c1f2007-10-13 23:56:31 +020059 struct stub_chip *chip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Jean Delvare9d90c1f2007-10-13 23:56:31 +020061 /* Search for the right chip */
62 for (i = 0; i < MAX_CHIPS && chip_addr[i]; i++) {
63 if (addr == chip_addr[i]) {
64 chip = stub_chips + i;
65 break;
66 }
67 }
68 if (!chip)
Jean Delvare7a8d29c2006-08-13 23:46:44 +020069 return -ENODEV;
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 switch (size) {
72
73 case I2C_SMBUS_QUICK:
74 dev_dbg(&adap->dev, "smbus quick - addr 0x%02x\n", addr);
75 ret = 0;
76 break;
77
78 case I2C_SMBUS_BYTE:
79 if (read_write == I2C_SMBUS_WRITE) {
Jean Delvare9d90c1f2007-10-13 23:56:31 +020080 chip->pointer = command;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 dev_dbg(&adap->dev, "smbus byte - addr 0x%02x, "
82 "wrote 0x%02x.\n",
83 addr, command);
84 } else {
Jean Delvare569be442008-01-27 18:14:45 +010085 data->byte = chip->words[chip->pointer++] & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 dev_dbg(&adap->dev, "smbus byte - addr 0x%02x, "
87 "read 0x%02x.\n",
88 addr, data->byte);
89 }
90
91 ret = 0;
92 break;
93
94 case I2C_SMBUS_BYTE_DATA:
95 if (read_write == I2C_SMBUS_WRITE) {
Jean Delvare569be442008-01-27 18:14:45 +010096 chip->words[command] &= 0xff00;
97 chip->words[command] |= data->byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 dev_dbg(&adap->dev, "smbus byte data - addr 0x%02x, "
99 "wrote 0x%02x at 0x%02x.\n",
100 addr, data->byte, command);
101 } else {
Jean Delvare569be442008-01-27 18:14:45 +0100102 data->byte = chip->words[command] & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 dev_dbg(&adap->dev, "smbus byte data - addr 0x%02x, "
104 "read 0x%02x at 0x%02x.\n",
105 addr, data->byte, command);
106 }
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200107 chip->pointer = command + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 ret = 0;
110 break;
111
112 case I2C_SMBUS_WORD_DATA:
113 if (read_write == I2C_SMBUS_WRITE) {
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200114 chip->words[command] = data->word;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 dev_dbg(&adap->dev, "smbus word data - addr 0x%02x, "
116 "wrote 0x%04x at 0x%02x.\n",
117 addr, data->word, command);
118 } else {
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200119 data->word = chip->words[command];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 dev_dbg(&adap->dev, "smbus word data - addr 0x%02x, "
121 "read 0x%04x at 0x%02x.\n",
122 addr, data->word, command);
123 }
124
125 ret = 0;
126 break;
127
Jean Delvare47103172009-12-06 17:06:28 +0100128 case I2C_SMBUS_I2C_BLOCK_DATA:
129 len = data->block[0];
130 if (read_write == I2C_SMBUS_WRITE) {
131 for (i = 0; i < len; i++) {
132 chip->words[command + i] &= 0xff00;
133 chip->words[command + i] |= data->block[1 + i];
134 }
135 dev_dbg(&adap->dev, "i2c block data - addr 0x%02x, "
136 "wrote %d bytes at 0x%02x.\n",
137 addr, len, command);
138 } else {
139 for (i = 0; i < len; i++) {
140 data->block[1 + i] =
141 chip->words[command + i] & 0xff;
142 }
143 dev_dbg(&adap->dev, "i2c block data - addr 0x%02x, "
144 "read %d bytes at 0x%02x.\n",
145 addr, len, command);
146 }
147
148 ret = 0;
149 break;
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 default:
152 dev_dbg(&adap->dev, "Unsupported I2C/SMBus command\n");
David Brownell97140342008-07-14 22:38:25 +0200153 ret = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 break;
155 } /* switch (size) */
156
157 return ret;
158}
159
160static u32 stub_func(struct i2c_adapter *adapter)
161{
Jean Delvarec5aa6922010-05-21 18:40:56 +0200162 return STUB_FUNC & functionality;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
Jean Delvare8f9082c2006-09-03 22:39:46 +0200165static const struct i2c_algorithm smbus_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 .functionality = stub_func,
167 .smbus_xfer = stub_xfer,
168};
169
170static struct i2c_adapter stub_adapter = {
171 .owner = THIS_MODULE,
Jean Delvare3401b2f2008-07-14 22:38:29 +0200172 .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 .algo = &smbus_algorithm,
174 .name = "SMBus stub driver",
175};
176
177static int __init i2c_stub_init(void)
178{
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200179 int i, ret;
180
181 if (!chip_addr[0]) {
Jean Delvare7a8d29c2006-08-13 23:46:44 +0200182 printk(KERN_ERR "i2c-stub: Please specify a chip address\n");
183 return -ENODEV;
184 }
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200185
186 for (i = 0; i < MAX_CHIPS && chip_addr[i]; i++) {
187 if (chip_addr[i] < 0x03 || chip_addr[i] > 0x77) {
188 printk(KERN_ERR "i2c-stub: Invalid chip address "
189 "0x%02x\n", chip_addr[i]);
190 return -EINVAL;
191 }
192
193 printk(KERN_INFO "i2c-stub: Virtual chip at 0x%02x\n",
194 chip_addr[i]);
Jean Delvare7a8d29c2006-08-13 23:46:44 +0200195 }
196
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200197 /* Allocate memory for all chips at once */
198 stub_chips = kzalloc(i * sizeof(struct stub_chip), GFP_KERNEL);
199 if (!stub_chips) {
200 printk(KERN_ERR "i2c-stub: Out of memory\n");
201 return -ENOMEM;
202 }
203
204 ret = i2c_add_adapter(&stub_adapter);
205 if (ret)
206 kfree(stub_chips);
207 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
210static void __exit i2c_stub_exit(void)
211{
212 i2c_del_adapter(&stub_adapter);
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200213 kfree(stub_chips);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
216MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
217MODULE_DESCRIPTION("I2C stub driver");
218MODULE_LICENSE("GPL");
219
220module_init(i2c_stub_init);
221module_exit(i2c_stub_exit);
222