blob: 0c770eabe85e15e3bec6c1bb662a635af8fedc23 [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 Delvare7a8d29c2006-08-13 23:46:44 +020032
Jean Delvare9d90c1f2007-10-13 23:56:31 +020033static unsigned short chip_addr[MAX_CHIPS];
34module_param_array(chip_addr, ushort, NULL, S_IRUGO);
35MODULE_PARM_DESC(chip_addr,
Jean Delvare85d69312008-04-29 23:11:37 +020036 "Chip addresses (up to 10, between 0x03 and 0x77)");
Jean Delvare9d90c1f2007-10-13 23:56:31 +020037
Jean Delvare38f41f22009-12-06 17:06:29 +010038static unsigned long functionality = ~0UL;
39module_param(functionality, ulong, S_IRUGO | S_IWUSR);
40MODULE_PARM_DESC(functionality, "Override functionality bitfield");
41
Jean Delvare9d90c1f2007-10-13 23:56:31 +020042struct stub_chip {
43 u8 pointer;
Jean Delvare569be442008-01-27 18:14:45 +010044 u16 words[256]; /* Byte operations use the LSB as per SMBus
45 specification */
Jean Delvare9d90c1f2007-10-13 23:56:31 +020046};
47
48static struct stub_chip *stub_chips;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
David Brownell97140342008-07-14 22:38:25 +020050/* Return negative errno on error. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051static s32 stub_xfer(struct i2c_adapter * adap, u16 addr, unsigned short flags,
52 char read_write, u8 command, int size, union i2c_smbus_data * data)
53{
54 s32 ret;
Jean Delvare47103172009-12-06 17:06:28 +010055 int i, len;
Jean Delvare9d90c1f2007-10-13 23:56:31 +020056 struct stub_chip *chip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Jean Delvare9d90c1f2007-10-13 23:56:31 +020058 /* Search for the right chip */
59 for (i = 0; i < MAX_CHIPS && chip_addr[i]; i++) {
60 if (addr == chip_addr[i]) {
61 chip = stub_chips + i;
62 break;
63 }
64 }
65 if (!chip)
Jean Delvare7a8d29c2006-08-13 23:46:44 +020066 return -ENODEV;
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 switch (size) {
69
70 case I2C_SMBUS_QUICK:
71 dev_dbg(&adap->dev, "smbus quick - addr 0x%02x\n", addr);
72 ret = 0;
73 break;
74
75 case I2C_SMBUS_BYTE:
76 if (read_write == I2C_SMBUS_WRITE) {
Jean Delvare9d90c1f2007-10-13 23:56:31 +020077 chip->pointer = command;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 dev_dbg(&adap->dev, "smbus byte - addr 0x%02x, "
79 "wrote 0x%02x.\n",
80 addr, command);
81 } else {
Jean Delvare569be442008-01-27 18:14:45 +010082 data->byte = chip->words[chip->pointer++] & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 dev_dbg(&adap->dev, "smbus byte - addr 0x%02x, "
84 "read 0x%02x.\n",
85 addr, data->byte);
86 }
87
88 ret = 0;
89 break;
90
91 case I2C_SMBUS_BYTE_DATA:
92 if (read_write == I2C_SMBUS_WRITE) {
Jean Delvare569be442008-01-27 18:14:45 +010093 chip->words[command] &= 0xff00;
94 chip->words[command] |= data->byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 dev_dbg(&adap->dev, "smbus byte data - addr 0x%02x, "
96 "wrote 0x%02x at 0x%02x.\n",
97 addr, data->byte, command);
98 } else {
Jean Delvare569be442008-01-27 18:14:45 +010099 data->byte = chip->words[command] & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 dev_dbg(&adap->dev, "smbus byte data - addr 0x%02x, "
101 "read 0x%02x at 0x%02x.\n",
102 addr, data->byte, command);
103 }
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200104 chip->pointer = command + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 ret = 0;
107 break;
108
109 case I2C_SMBUS_WORD_DATA:
110 if (read_write == I2C_SMBUS_WRITE) {
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200111 chip->words[command] = data->word;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 dev_dbg(&adap->dev, "smbus word data - addr 0x%02x, "
113 "wrote 0x%04x at 0x%02x.\n",
114 addr, data->word, command);
115 } else {
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200116 data->word = chip->words[command];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 dev_dbg(&adap->dev, "smbus word data - addr 0x%02x, "
118 "read 0x%04x at 0x%02x.\n",
119 addr, data->word, command);
120 }
121
122 ret = 0;
123 break;
124
Jean Delvare47103172009-12-06 17:06:28 +0100125 case I2C_SMBUS_I2C_BLOCK_DATA:
126 len = data->block[0];
127 if (read_write == I2C_SMBUS_WRITE) {
128 for (i = 0; i < len; i++) {
129 chip->words[command + i] &= 0xff00;
130 chip->words[command + i] |= data->block[1 + i];
131 }
132 dev_dbg(&adap->dev, "i2c block data - addr 0x%02x, "
133 "wrote %d bytes at 0x%02x.\n",
134 addr, len, command);
135 } else {
136 for (i = 0; i < len; i++) {
137 data->block[1 + i] =
138 chip->words[command + i] & 0xff;
139 }
140 dev_dbg(&adap->dev, "i2c block data - addr 0x%02x, "
141 "read %d bytes at 0x%02x.\n",
142 addr, len, command);
143 }
144
145 ret = 0;
146 break;
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 default:
149 dev_dbg(&adap->dev, "Unsupported I2C/SMBus command\n");
David Brownell97140342008-07-14 22:38:25 +0200150 ret = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 break;
152 } /* switch (size) */
153
154 return ret;
155}
156
157static u32 stub_func(struct i2c_adapter *adapter)
158{
Jean Delvare38f41f22009-12-06 17:06:29 +0100159 return (I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
Jean Delvare47103172009-12-06 17:06:28 +0100160 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
Jean Delvare38f41f22009-12-06 17:06:29 +0100161 I2C_FUNC_SMBUS_I2C_BLOCK) & functionality;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
Jean Delvare8f9082c2006-09-03 22:39:46 +0200164static const struct i2c_algorithm smbus_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 .functionality = stub_func,
166 .smbus_xfer = stub_xfer,
167};
168
169static struct i2c_adapter stub_adapter = {
170 .owner = THIS_MODULE,
Jean Delvare3401b2f2008-07-14 22:38:29 +0200171 .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 .algo = &smbus_algorithm,
173 .name = "SMBus stub driver",
174};
175
176static int __init i2c_stub_init(void)
177{
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200178 int i, ret;
179
180 if (!chip_addr[0]) {
Jean Delvare7a8d29c2006-08-13 23:46:44 +0200181 printk(KERN_ERR "i2c-stub: Please specify a chip address\n");
182 return -ENODEV;
183 }
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200184
185 for (i = 0; i < MAX_CHIPS && chip_addr[i]; i++) {
186 if (chip_addr[i] < 0x03 || chip_addr[i] > 0x77) {
187 printk(KERN_ERR "i2c-stub: Invalid chip address "
188 "0x%02x\n", chip_addr[i]);
189 return -EINVAL;
190 }
191
192 printk(KERN_INFO "i2c-stub: Virtual chip at 0x%02x\n",
193 chip_addr[i]);
Jean Delvare7a8d29c2006-08-13 23:46:44 +0200194 }
195
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200196 /* Allocate memory for all chips at once */
197 stub_chips = kzalloc(i * sizeof(struct stub_chip), GFP_KERNEL);
198 if (!stub_chips) {
199 printk(KERN_ERR "i2c-stub: Out of memory\n");
200 return -ENOMEM;
201 }
202
203 ret = i2c_add_adapter(&stub_adapter);
204 if (ret)
205 kfree(stub_chips);
206 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
209static void __exit i2c_stub_exit(void)
210{
211 i2c_del_adapter(&stub_adapter);
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200212 kfree(stub_chips);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
215MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
216MODULE_DESCRIPTION("I2C stub driver");
217MODULE_LICENSE("GPL");
218
219module_init(i2c_stub_init);
220module_exit(i2c_stub_exit);
221