2 i2c-stub.c - I2C/SMBus chip emulator
4 Copyright (c) 2004 Mark M. Hoffman <mhoffman@lightlink.com>
5 Copyright (C) 2007 Jean Delvare <khali@linux-fr.org>
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.
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.
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.
24 #include <linux/init.h>
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/errno.h>
29 #include <linux/i2c.h>
32 #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)
36 static unsigned short chip_addr
[MAX_CHIPS
];
37 module_param_array(chip_addr
, ushort
, NULL
, S_IRUGO
);
38 MODULE_PARM_DESC(chip_addr
,
39 "Chip addresses (up to 10, between 0x03 and 0x77)");
41 static unsigned long functionality
= STUB_FUNC
;
42 module_param(functionality
, ulong
, S_IRUGO
| S_IWUSR
);
43 MODULE_PARM_DESC(functionality
, "Override functionality bitfield");
47 u16 words
[256]; /* Byte operations use the LSB as per SMBus
51 static struct stub_chip
*stub_chips
;
53 /* Return negative errno on error. */
54 static 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
)
59 struct stub_chip
*chip
= NULL
;
61 /* 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
;
74 dev_dbg(&adap
->dev
, "smbus quick - addr 0x%02x\n", addr
);
79 if (read_write
== I2C_SMBUS_WRITE
) {
80 chip
->pointer
= command
;
81 dev_dbg(&adap
->dev
, "smbus byte - addr 0x%02x, "
85 data
->byte
= chip
->words
[chip
->pointer
++] & 0xff;
86 dev_dbg(&adap
->dev
, "smbus byte - addr 0x%02x, "
94 case I2C_SMBUS_BYTE_DATA
:
95 if (read_write
== I2C_SMBUS_WRITE
) {
96 chip
->words
[command
] &= 0xff00;
97 chip
->words
[command
] |= data
->byte
;
98 dev_dbg(&adap
->dev
, "smbus byte data - addr 0x%02x, "
99 "wrote 0x%02x at 0x%02x.\n",
100 addr
, data
->byte
, command
);
102 data
->byte
= chip
->words
[command
] & 0xff;
103 dev_dbg(&adap
->dev
, "smbus byte data - addr 0x%02x, "
104 "read 0x%02x at 0x%02x.\n",
105 addr
, data
->byte
, command
);
107 chip
->pointer
= command
+ 1;
112 case I2C_SMBUS_WORD_DATA
:
113 if (read_write
== I2C_SMBUS_WRITE
) {
114 chip
->words
[command
] = data
->word
;
115 dev_dbg(&adap
->dev
, "smbus word data - addr 0x%02x, "
116 "wrote 0x%04x at 0x%02x.\n",
117 addr
, data
->word
, command
);
119 data
->word
= chip
->words
[command
];
120 dev_dbg(&adap
->dev
, "smbus word data - addr 0x%02x, "
121 "read 0x%04x at 0x%02x.\n",
122 addr
, data
->word
, command
);
128 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
];
135 dev_dbg(&adap
->dev
, "i2c block data - addr 0x%02x, "
136 "wrote %d bytes at 0x%02x.\n",
139 for (i
= 0; i
< len
; i
++) {
141 chip
->words
[command
+ i
] & 0xff;
143 dev_dbg(&adap
->dev
, "i2c block data - addr 0x%02x, "
144 "read %d bytes at 0x%02x.\n",
152 dev_dbg(&adap
->dev
, "Unsupported I2C/SMBus command\n");
155 } /* switch (size) */
160 static u32
stub_func(struct i2c_adapter
*adapter
)
162 return STUB_FUNC
& functionality
;
165 static const struct i2c_algorithm smbus_algorithm
= {
166 .functionality
= stub_func
,
167 .smbus_xfer
= stub_xfer
,
170 static struct i2c_adapter stub_adapter
= {
171 .owner
= THIS_MODULE
,
172 .class = I2C_CLASS_HWMON
| I2C_CLASS_SPD
,
173 .algo
= &smbus_algorithm
,
174 .name
= "SMBus stub driver",
177 static int __init
i2c_stub_init(void)
182 printk(KERN_ERR
"i2c-stub: Please specify a chip address\n");
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
]);
193 printk(KERN_INFO
"i2c-stub: Virtual chip at 0x%02x\n",
197 /* Allocate memory for all chips at once */
198 stub_chips
= kzalloc(i
* sizeof(struct stub_chip
), GFP_KERNEL
);
200 printk(KERN_ERR
"i2c-stub: Out of memory\n");
204 ret
= i2c_add_adapter(&stub_adapter
);
210 static void __exit
i2c_stub_exit(void)
212 i2c_del_adapter(&stub_adapter
);
216 MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
217 MODULE_DESCRIPTION("I2C stub driver");
218 MODULE_LICENSE("GPL");
220 module_init(i2c_stub_init
);
221 module_exit(i2c_stub_exit
);