Merge tag 'v3.14.25' into backport/v3.14.24-ltsi-rc1+v3.14.25/snapshot-merge.wip
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / i2c / i2c-stub.c
1 /*
2     i2c-stub.c - I2C/SMBus chip emulator
3
4     Copyright (c) 2004 Mark M. Hoffman <mhoffman@lightlink.com>
5     Copyright (C) 2007, 2012 Jean Delvare <jdelvare@suse.de>
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
18 #define DEBUG 1
19
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/slab.h>
24 #include <linux/errno.h>
25 #include <linux/i2c.h>
26
27 #define MAX_CHIPS 10
28 #define STUB_FUNC (I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE | \
29                    I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA | \
30                    I2C_FUNC_SMBUS_I2C_BLOCK)
31
32 static unsigned short chip_addr[MAX_CHIPS];
33 module_param_array(chip_addr, ushort, NULL, S_IRUGO);
34 MODULE_PARM_DESC(chip_addr,
35                  "Chip addresses (up to 10, between 0x03 and 0x77)");
36
37 static unsigned long functionality = STUB_FUNC;
38 module_param(functionality, ulong, S_IRUGO | S_IWUSR);
39 MODULE_PARM_DESC(functionality, "Override functionality bitfield");
40
41 struct stub_chip {
42         u8 pointer;
43         u16 words[256];         /* Byte operations use the LSB as per SMBus
44                                    specification */
45 };
46
47 static struct stub_chip *stub_chips;
48
49 /* Return negative errno on error. */
50 static s32 stub_xfer(struct i2c_adapter *adap, u16 addr, unsigned short flags,
51         char read_write, u8 command, int size, union i2c_smbus_data *data)
52 {
53         s32 ret;
54         int i, len;
55         struct stub_chip *chip = NULL;
56
57         /* Search for the right chip */
58         for (i = 0; i < MAX_CHIPS && chip_addr[i]; i++) {
59                 if (addr == chip_addr[i]) {
60                         chip = stub_chips + i;
61                         break;
62                 }
63         }
64         if (!chip)
65                 return -ENODEV;
66
67         switch (size) {
68
69         case I2C_SMBUS_QUICK:
70                 dev_dbg(&adap->dev, "smbus quick - addr 0x%02x\n", addr);
71                 ret = 0;
72                 break;
73
74         case I2C_SMBUS_BYTE:
75                 if (read_write == I2C_SMBUS_WRITE) {
76                         chip->pointer = command;
77                         dev_dbg(&adap->dev,
78                                 "smbus byte - addr 0x%02x, wrote 0x%02x.\n",
79                                 addr, command);
80                 } else {
81                         data->byte = chip->words[chip->pointer++] & 0xff;
82                         dev_dbg(&adap->dev,
83                                 "smbus byte - addr 0x%02x, read  0x%02x.\n",
84                                 addr, data->byte);
85                 }
86
87                 ret = 0;
88                 break;
89
90         case I2C_SMBUS_BYTE_DATA:
91                 if (read_write == I2C_SMBUS_WRITE) {
92                         chip->words[command] &= 0xff00;
93                         chip->words[command] |= data->byte;
94                         dev_dbg(&adap->dev,
95                                 "smbus byte data - addr 0x%02x, wrote 0x%02x at 0x%02x.\n",
96                                 addr, data->byte, command);
97                 } else {
98                         data->byte = chip->words[command] & 0xff;
99                         dev_dbg(&adap->dev,
100                                 "smbus byte data - addr 0x%02x, read  0x%02x at 0x%02x.\n",
101                                 addr, data->byte, command);
102                 }
103                 chip->pointer = command + 1;
104
105                 ret = 0;
106                 break;
107
108         case I2C_SMBUS_WORD_DATA:
109                 if (read_write == I2C_SMBUS_WRITE) {
110                         chip->words[command] = data->word;
111                         dev_dbg(&adap->dev,
112                                 "smbus word data - addr 0x%02x, wrote 0x%04x at 0x%02x.\n",
113                                 addr, data->word, command);
114                 } else {
115                         data->word = chip->words[command];
116                         dev_dbg(&adap->dev,
117                                 "smbus word data - addr 0x%02x, read  0x%04x at 0x%02x.\n",
118                                 addr, data->word, command);
119                 }
120
121                 ret = 0;
122                 break;
123
124         case I2C_SMBUS_I2C_BLOCK_DATA:
125                 len = data->block[0];
126                 if (read_write == I2C_SMBUS_WRITE) {
127                         for (i = 0; i < len; i++) {
128                                 chip->words[command + i] &= 0xff00;
129                                 chip->words[command + i] |= data->block[1 + i];
130                         }
131                         dev_dbg(&adap->dev,
132                                 "i2c block data - addr 0x%02x, wrote %d bytes at 0x%02x.\n",
133                                 addr, len, command);
134                 } else {
135                         for (i = 0; i < len; i++) {
136                                 data->block[1 + i] =
137                                         chip->words[command + i] & 0xff;
138                         }
139                         dev_dbg(&adap->dev,
140                                 "i2c block data - addr 0x%02x, read  %d bytes at 0x%02x.\n",
141                                 addr, len, command);
142                 }
143
144                 ret = 0;
145                 break;
146
147         default:
148                 dev_dbg(&adap->dev, "Unsupported I2C/SMBus command\n");
149                 ret = -EOPNOTSUPP;
150                 break;
151         } /* switch (size) */
152
153         return ret;
154 }
155
156 static u32 stub_func(struct i2c_adapter *adapter)
157 {
158         return STUB_FUNC & functionality;
159 }
160
161 static const struct i2c_algorithm smbus_algorithm = {
162         .functionality  = stub_func,
163         .smbus_xfer     = stub_xfer,
164 };
165
166 static struct i2c_adapter stub_adapter = {
167         .owner          = THIS_MODULE,
168         .class          = I2C_CLASS_HWMON | I2C_CLASS_SPD,
169         .algo           = &smbus_algorithm,
170         .name           = "SMBus stub driver",
171 };
172
173 static int __init i2c_stub_init(void)
174 {
175         int i, ret;
176
177         if (!chip_addr[0]) {
178                 pr_err("i2c-stub: Please specify a chip address\n");
179                 return -ENODEV;
180         }
181
182         for (i = 0; i < MAX_CHIPS && chip_addr[i]; i++) {
183                 if (chip_addr[i] < 0x03 || chip_addr[i] > 0x77) {
184                         pr_err("i2c-stub: Invalid chip address 0x%02x\n",
185                                chip_addr[i]);
186                         return -EINVAL;
187                 }
188
189                 pr_info("i2c-stub: Virtual chip at 0x%02x\n", chip_addr[i]);
190         }
191
192         /* Allocate memory for all chips at once */
193         stub_chips = kzalloc(i * sizeof(struct stub_chip), GFP_KERNEL);
194         if (!stub_chips) {
195                 pr_err("i2c-stub: Out of memory\n");
196                 return -ENOMEM;
197         }
198
199         ret = i2c_add_adapter(&stub_adapter);
200         if (ret)
201                 kfree(stub_chips);
202         return ret;
203 }
204
205 static void __exit i2c_stub_exit(void)
206 {
207         i2c_del_adapter(&stub_adapter);
208         kfree(stub_chips);
209 }
210
211 MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
212 MODULE_DESCRIPTION("I2C stub driver");
213 MODULE_LICENSE("GPL");
214
215 module_init(i2c_stub_init);
216 module_exit(i2c_stub_exit);