i2c-stub.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. i2c-stub.c - I2C/SMBus chip emulator
  3. Copyright (c) 2004 Mark M. Hoffman <mhoffman@lightlink.com>
  4. Copyright (C) 2007-2014 Jean Delvare <jdelvare@suse.de>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. #define DEBUG 1
  18. #include <linux/init.h>
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/slab.h>
  22. #include <linux/errno.h>
  23. #include <linux/i2c.h>
  24. #include <linux/list.h>
  25. #define MAX_CHIPS 10
  26. /*
  27. * Support for I2C_FUNC_SMBUS_BLOCK_DATA is disabled by default and must
  28. * be enabled explicitly by setting the I2C_FUNC_SMBUS_BLOCK_DATA bits
  29. * in the 'functionality' module parameter.
  30. */
  31. #define STUB_FUNC_DEFAULT \
  32. (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)
  35. #define STUB_FUNC_ALL \
  36. (STUB_FUNC_DEFAULT | I2C_FUNC_SMBUS_BLOCK_DATA)
  37. static unsigned short chip_addr[MAX_CHIPS];
  38. module_param_array(chip_addr, ushort, NULL, S_IRUGO);
  39. MODULE_PARM_DESC(chip_addr,
  40. "Chip addresses (up to 10, between 0x03 and 0x77)");
  41. static unsigned long functionality = STUB_FUNC_DEFAULT;
  42. module_param(functionality, ulong, S_IRUGO | S_IWUSR);
  43. MODULE_PARM_DESC(functionality, "Override functionality bitfield");
  44. /* Some chips have banked register ranges */
  45. static u8 bank_reg[MAX_CHIPS];
  46. module_param_array(bank_reg, byte, NULL, S_IRUGO);
  47. MODULE_PARM_DESC(bank_reg, "Bank register");
  48. static u8 bank_mask[MAX_CHIPS];
  49. module_param_array(bank_mask, byte, NULL, S_IRUGO);
  50. MODULE_PARM_DESC(bank_mask, "Bank value mask");
  51. static u8 bank_start[MAX_CHIPS];
  52. module_param_array(bank_start, byte, NULL, S_IRUGO);
  53. MODULE_PARM_DESC(bank_start, "First banked register");
  54. static u8 bank_end[MAX_CHIPS];
  55. module_param_array(bank_end, byte, NULL, S_IRUGO);
  56. MODULE_PARM_DESC(bank_end, "Last banked register");
  57. struct smbus_block_data {
  58. struct list_head node;
  59. u8 command;
  60. u8 len;
  61. u8 block[I2C_SMBUS_BLOCK_MAX];
  62. };
  63. struct stub_chip {
  64. u8 pointer;
  65. u16 words[256]; /* Byte operations use the LSB as per SMBus
  66. specification */
  67. struct list_head smbus_blocks;
  68. /* For chips with banks, extra registers are allocated dynamically */
  69. u8 bank_reg;
  70. u8 bank_shift;
  71. u8 bank_mask;
  72. u8 bank_sel; /* Currently selected bank */
  73. u8 bank_start;
  74. u8 bank_end;
  75. u16 bank_size;
  76. u16 *bank_words; /* Room for bank_mask * bank_size registers */
  77. };
  78. static struct stub_chip *stub_chips;
  79. static int stub_chips_nr;
  80. static struct smbus_block_data *stub_find_block(struct device *dev,
  81. struct stub_chip *chip,
  82. u8 command, bool create)
  83. {
  84. struct smbus_block_data *b, *rb = NULL;
  85. list_for_each_entry(b, &chip->smbus_blocks, node) {
  86. if (b->command == command) {
  87. rb = b;
  88. break;
  89. }
  90. }
  91. if (rb == NULL && create) {
  92. rb = devm_kzalloc(dev, sizeof(*rb), GFP_KERNEL);
  93. if (rb == NULL)
  94. return rb;
  95. rb->command = command;
  96. list_add(&rb->node, &chip->smbus_blocks);
  97. }
  98. return rb;
  99. }
  100. static u16 *stub_get_wordp(struct stub_chip *chip, u8 offset)
  101. {
  102. if (chip->bank_sel &&
  103. offset >= chip->bank_start && offset <= chip->bank_end)
  104. return chip->bank_words +
  105. (chip->bank_sel - 1) * chip->bank_size +
  106. offset - chip->bank_start;
  107. else
  108. return chip->words + offset;
  109. }
  110. /* Return negative errno on error. */
  111. static s32 stub_xfer(struct i2c_adapter *adap, u16 addr, unsigned short flags,
  112. char read_write, u8 command, int size, union i2c_smbus_data *data)
  113. {
  114. s32 ret;
  115. int i, len;
  116. struct stub_chip *chip = NULL;
  117. struct smbus_block_data *b;
  118. u16 *wordp;
  119. /* Search for the right chip */
  120. for (i = 0; i < stub_chips_nr; i++) {
  121. if (addr == chip_addr[i]) {
  122. chip = stub_chips + i;
  123. break;
  124. }
  125. }
  126. if (!chip)
  127. return -ENODEV;
  128. switch (size) {
  129. case I2C_SMBUS_QUICK:
  130. dev_dbg(&adap->dev, "smbus quick - addr 0x%02x\n", addr);
  131. ret = 0;
  132. break;
  133. case I2C_SMBUS_BYTE:
  134. if (read_write == I2C_SMBUS_WRITE) {
  135. chip->pointer = command;
  136. dev_dbg(&adap->dev,
  137. "smbus byte - addr 0x%02x, wrote 0x%02x.\n",
  138. addr, command);
  139. } else {
  140. wordp = stub_get_wordp(chip, chip->pointer++);
  141. data->byte = *wordp & 0xff;
  142. dev_dbg(&adap->dev,
  143. "smbus byte - addr 0x%02x, read 0x%02x.\n",
  144. addr, data->byte);
  145. }
  146. ret = 0;
  147. break;
  148. case I2C_SMBUS_BYTE_DATA:
  149. wordp = stub_get_wordp(chip, command);
  150. if (read_write == I2C_SMBUS_WRITE) {
  151. *wordp &= 0xff00;
  152. *wordp |= data->byte;
  153. dev_dbg(&adap->dev,
  154. "smbus byte data - addr 0x%02x, wrote 0x%02x at 0x%02x.\n",
  155. addr, data->byte, command);
  156. /* Set the bank as needed */
  157. if (chip->bank_words && command == chip->bank_reg) {
  158. chip->bank_sel =
  159. (data->byte >> chip->bank_shift)
  160. & chip->bank_mask;
  161. dev_dbg(&adap->dev,
  162. "switching to bank %u.\n",
  163. chip->bank_sel);
  164. }
  165. } else {
  166. data->byte = *wordp & 0xff;
  167. dev_dbg(&adap->dev,
  168. "smbus byte data - addr 0x%02x, read 0x%02x at 0x%02x.\n",
  169. addr, data->byte, command);
  170. }
  171. chip->pointer = command + 1;
  172. ret = 0;
  173. break;
  174. case I2C_SMBUS_WORD_DATA:
  175. wordp = stub_get_wordp(chip, command);
  176. if (read_write == I2C_SMBUS_WRITE) {
  177. *wordp = data->word;
  178. dev_dbg(&adap->dev,
  179. "smbus word data - addr 0x%02x, wrote 0x%04x at 0x%02x.\n",
  180. addr, data->word, command);
  181. } else {
  182. data->word = *wordp;
  183. dev_dbg(&adap->dev,
  184. "smbus word data - addr 0x%02x, read 0x%04x at 0x%02x.\n",
  185. addr, data->word, command);
  186. }
  187. ret = 0;
  188. break;
  189. case I2C_SMBUS_I2C_BLOCK_DATA:
  190. /*
  191. * We ignore banks here, because banked chips don't use I2C
  192. * block transfers
  193. */
  194. if (data->block[0] > 256 - command) /* Avoid overrun */
  195. data->block[0] = 256 - command;
  196. len = data->block[0];
  197. if (read_write == I2C_SMBUS_WRITE) {
  198. for (i = 0; i < len; i++) {
  199. chip->words[command + i] &= 0xff00;
  200. chip->words[command + i] |= data->block[1 + i];
  201. }
  202. dev_dbg(&adap->dev,
  203. "i2c block data - addr 0x%02x, wrote %d bytes at 0x%02x.\n",
  204. addr, len, command);
  205. } else {
  206. for (i = 0; i < len; i++) {
  207. data->block[1 + i] =
  208. chip->words[command + i] & 0xff;
  209. }
  210. dev_dbg(&adap->dev,
  211. "i2c block data - addr 0x%02x, read %d bytes at 0x%02x.\n",
  212. addr, len, command);
  213. }
  214. ret = 0;
  215. break;
  216. case I2C_SMBUS_BLOCK_DATA:
  217. /*
  218. * We ignore banks here, because chips typically don't use both
  219. * banks and SMBus block transfers
  220. */
  221. b = stub_find_block(&adap->dev, chip, command, false);
  222. if (read_write == I2C_SMBUS_WRITE) {
  223. len = data->block[0];
  224. if (len == 0 || len > I2C_SMBUS_BLOCK_MAX) {
  225. ret = -EINVAL;
  226. break;
  227. }
  228. if (b == NULL) {
  229. b = stub_find_block(&adap->dev, chip, command,
  230. true);
  231. if (b == NULL) {
  232. ret = -ENOMEM;
  233. break;
  234. }
  235. }
  236. /* Largest write sets read block length */
  237. if (len > b->len)
  238. b->len = len;
  239. for (i = 0; i < len; i++)
  240. b->block[i] = data->block[i + 1];
  241. /* update for byte and word commands */
  242. chip->words[command] = (b->block[0] << 8) | b->len;
  243. dev_dbg(&adap->dev,
  244. "smbus block data - addr 0x%02x, wrote %d bytes at 0x%02x.\n",
  245. addr, len, command);
  246. } else {
  247. if (b == NULL) {
  248. dev_dbg(&adap->dev,
  249. "SMBus block read command without prior block write not supported\n");
  250. ret = -EOPNOTSUPP;
  251. break;
  252. }
  253. len = b->len;
  254. data->block[0] = len;
  255. for (i = 0; i < len; i++)
  256. data->block[i + 1] = b->block[i];
  257. dev_dbg(&adap->dev,
  258. "smbus block data - addr 0x%02x, read %d bytes at 0x%02x.\n",
  259. addr, len, command);
  260. }
  261. ret = 0;
  262. break;
  263. default:
  264. dev_dbg(&adap->dev, "Unsupported I2C/SMBus command\n");
  265. ret = -EOPNOTSUPP;
  266. break;
  267. } /* switch (size) */
  268. return ret;
  269. }
  270. static u32 stub_func(struct i2c_adapter *adapter)
  271. {
  272. return STUB_FUNC_ALL & functionality;
  273. }
  274. static const struct i2c_algorithm smbus_algorithm = {
  275. .functionality = stub_func,
  276. .smbus_xfer = stub_xfer,
  277. };
  278. static struct i2c_adapter stub_adapter = {
  279. .owner = THIS_MODULE,
  280. .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
  281. .algo = &smbus_algorithm,
  282. .name = "SMBus stub driver",
  283. };
  284. static int __init i2c_stub_allocate_banks(int i)
  285. {
  286. struct stub_chip *chip = stub_chips + i;
  287. chip->bank_reg = bank_reg[i];
  288. chip->bank_start = bank_start[i];
  289. chip->bank_end = bank_end[i];
  290. chip->bank_size = bank_end[i] - bank_start[i] + 1;
  291. /* We assume that all bits in the mask are contiguous */
  292. chip->bank_mask = bank_mask[i];
  293. while (!(chip->bank_mask & 1)) {
  294. chip->bank_shift++;
  295. chip->bank_mask >>= 1;
  296. }
  297. chip->bank_words = kzalloc(chip->bank_mask * chip->bank_size *
  298. sizeof(u16), GFP_KERNEL);
  299. if (!chip->bank_words)
  300. return -ENOMEM;
  301. pr_debug("i2c-stub: Allocated %u banks of %u words each (registers 0x%02x to 0x%02x)\n",
  302. chip->bank_mask, chip->bank_size, chip->bank_start,
  303. chip->bank_end);
  304. return 0;
  305. }
  306. static void i2c_stub_free(void)
  307. {
  308. int i;
  309. for (i = 0; i < stub_chips_nr; i++)
  310. kfree(stub_chips[i].bank_words);
  311. kfree(stub_chips);
  312. }
  313. static int __init i2c_stub_init(void)
  314. {
  315. int i, ret;
  316. if (!chip_addr[0]) {
  317. pr_err("i2c-stub: Please specify a chip address\n");
  318. return -ENODEV;
  319. }
  320. for (i = 0; i < MAX_CHIPS && chip_addr[i]; i++) {
  321. if (chip_addr[i] < 0x03 || chip_addr[i] > 0x77) {
  322. pr_err("i2c-stub: Invalid chip address 0x%02x\n",
  323. chip_addr[i]);
  324. return -EINVAL;
  325. }
  326. pr_info("i2c-stub: Virtual chip at 0x%02x\n", chip_addr[i]);
  327. }
  328. /* Allocate memory for all chips at once */
  329. stub_chips_nr = i;
  330. stub_chips = kcalloc(stub_chips_nr, sizeof(struct stub_chip),
  331. GFP_KERNEL);
  332. if (!stub_chips) {
  333. pr_err("i2c-stub: Out of memory\n");
  334. return -ENOMEM;
  335. }
  336. for (i = 0; i < stub_chips_nr; i++) {
  337. INIT_LIST_HEAD(&stub_chips[i].smbus_blocks);
  338. /* Allocate extra memory for banked register ranges */
  339. if (bank_mask[i]) {
  340. ret = i2c_stub_allocate_banks(i);
  341. if (ret)
  342. goto fail_free;
  343. }
  344. }
  345. ret = i2c_add_adapter(&stub_adapter);
  346. if (ret)
  347. goto fail_free;
  348. return 0;
  349. fail_free:
  350. i2c_stub_free();
  351. return ret;
  352. }
  353. static void __exit i2c_stub_exit(void)
  354. {
  355. i2c_del_adapter(&stub_adapter);
  356. i2c_stub_free();
  357. }
  358. MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
  359. MODULE_DESCRIPTION("I2C stub driver");
  360. MODULE_LICENSE("GPL");
  361. module_init(i2c_stub_init);
  362. module_exit(i2c_stub_exit);