i2c-stub.c 10 KB

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