blob: 84df29da1ddc1c2f79d3dec90984ee343696f387 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 i2c-stub.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4
5 Copyright (c) 2004 Mark M. Hoffman <mhoffman@lightlink.com>
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 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,
36 "Chip addresses (up to 10, between 0x03 and 0x77)\n");
37
38struct stub_chip {
39 u8 pointer;
40 u8 bytes[256];
41 u16 words[256];
42};
43
44static struct stub_chip *stub_chips;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46/* Return -1 on error. */
47static 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 Delvare9d90c1f2007-10-13 23:56:31 +020051 int i;
52 struct stub_chip *chip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Jean Delvare9d90c1f2007-10-13 23:56:31 +020054 /* 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 Delvare7a8d29c2006-08-13 23:46:44 +020062 return -ENODEV;
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 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 Delvare9d90c1f2007-10-13 23:56:31 +020073 chip->pointer = command;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 dev_dbg(&adap->dev, "smbus byte - addr 0x%02x, "
75 "wrote 0x%02x.\n",
76 addr, command);
77 } else {
Jean Delvare9d90c1f2007-10-13 23:56:31 +020078 data->byte = chip->bytes[chip->pointer++];
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 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 Delvare9d90c1f2007-10-13 23:56:31 +020089 chip->bytes[command] = data->byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 dev_dbg(&adap->dev, "smbus byte data - addr 0x%02x, "
91 "wrote 0x%02x at 0x%02x.\n",
92 addr, data->byte, command);
93 } else {
Jean Delvare9d90c1f2007-10-13 23:56:31 +020094 data->byte = chip->bytes[command];
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 dev_dbg(&adap->dev, "smbus byte data - addr 0x%02x, "
96 "read 0x%02x at 0x%02x.\n",
97 addr, data->byte, command);
98 }
Jean Delvare9d90c1f2007-10-13 23:56:31 +020099 chip->pointer = command + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 ret = 0;
102 break;
103
104 case I2C_SMBUS_WORD_DATA:
105 if (read_write == I2C_SMBUS_WRITE) {
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200106 chip->words[command] = data->word;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 dev_dbg(&adap->dev, "smbus word data - addr 0x%02x, "
108 "wrote 0x%04x at 0x%02x.\n",
109 addr, data->word, command);
110 } else {
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200111 data->word = chip->words[command];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 dev_dbg(&adap->dev, "smbus word data - addr 0x%02x, "
113 "read 0x%04x at 0x%02x.\n",
114 addr, data->word, command);
115 }
116
117 ret = 0;
118 break;
119
120 default:
121 dev_dbg(&adap->dev, "Unsupported I2C/SMBus command\n");
122 ret = -1;
123 break;
124 } /* switch (size) */
125
126 return ret;
127}
128
129static u32 stub_func(struct i2c_adapter *adapter)
130{
131 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
132 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA;
133}
134
Jean Delvare8f9082c2006-09-03 22:39:46 +0200135static const struct i2c_algorithm smbus_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 .functionality = stub_func,
137 .smbus_xfer = stub_xfer,
138};
139
140static struct i2c_adapter stub_adapter = {
141 .owner = THIS_MODULE,
142 .class = I2C_CLASS_HWMON,
143 .algo = &smbus_algorithm,
144 .name = "SMBus stub driver",
145};
146
147static int __init i2c_stub_init(void)
148{
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200149 int i, ret;
150
151 if (!chip_addr[0]) {
Jean Delvare7a8d29c2006-08-13 23:46:44 +0200152 printk(KERN_ERR "i2c-stub: Please specify a chip address\n");
153 return -ENODEV;
154 }
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200155
156 for (i = 0; i < MAX_CHIPS && chip_addr[i]; i++) {
157 if (chip_addr[i] < 0x03 || chip_addr[i] > 0x77) {
158 printk(KERN_ERR "i2c-stub: Invalid chip address "
159 "0x%02x\n", chip_addr[i]);
160 return -EINVAL;
161 }
162
163 printk(KERN_INFO "i2c-stub: Virtual chip at 0x%02x\n",
164 chip_addr[i]);
Jean Delvare7a8d29c2006-08-13 23:46:44 +0200165 }
166
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200167 /* Allocate memory for all chips at once */
168 stub_chips = kzalloc(i * sizeof(struct stub_chip), GFP_KERNEL);
169 if (!stub_chips) {
170 printk(KERN_ERR "i2c-stub: Out of memory\n");
171 return -ENOMEM;
172 }
173
174 ret = i2c_add_adapter(&stub_adapter);
175 if (ret)
176 kfree(stub_chips);
177 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
180static void __exit i2c_stub_exit(void)
181{
182 i2c_del_adapter(&stub_adapter);
Jean Delvare9d90c1f2007-10-13 23:56:31 +0200183 kfree(stub_chips);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
186MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
187MODULE_DESCRIPTION("I2C stub driver");
188MODULE_LICENSE("GPL");
189
190module_init(i2c_stub_init);
191module_exit(i2c_stub_exit);
192