i2c-slave-eeprom.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * I2C slave mode EEPROM simulator
  4. *
  5. * Copyright (C) 2014 by Wolfram Sang, Sang Engineering <[email protected]>
  6. * Copyright (C) 2014 by Renesas Electronics Corporation
  7. *
  8. * Because most slave IP cores can only detect one I2C slave address anyhow,
  9. * this driver does not support simulating EEPROM types which take more than
  10. * one address.
  11. */
  12. /*
  13. * FIXME: What to do if only 8 bits of a 16 bit address are sent?
  14. * The ST-M24C64 sends only 0xff then. Needs verification with other
  15. * EEPROMs, though. We currently use the 8 bit as a valid address.
  16. */
  17. #include <linux/bitfield.h>
  18. #include <linux/firmware.h>
  19. #include <linux/i2c.h>
  20. #include <linux/init.h>
  21. #include <linux/module.h>
  22. #include <linux/of.h>
  23. #include <linux/slab.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/sysfs.h>
  26. struct eeprom_data {
  27. struct bin_attribute bin;
  28. spinlock_t buffer_lock;
  29. u16 buffer_idx;
  30. u16 address_mask;
  31. u8 num_address_bytes;
  32. u8 idx_write_cnt;
  33. bool read_only;
  34. u8 buffer[];
  35. };
  36. #define I2C_SLAVE_BYTELEN GENMASK(15, 0)
  37. #define I2C_SLAVE_FLAG_ADDR16 BIT(16)
  38. #define I2C_SLAVE_FLAG_RO BIT(17)
  39. #define I2C_SLAVE_DEVICE_MAGIC(_len, _flags) ((_flags) | ((_len) - 1))
  40. static int i2c_slave_eeprom_slave_cb(struct i2c_client *client,
  41. enum i2c_slave_event event, u8 *val)
  42. {
  43. struct eeprom_data *eeprom = i2c_get_clientdata(client);
  44. switch (event) {
  45. case I2C_SLAVE_WRITE_RECEIVED:
  46. if (eeprom->idx_write_cnt < eeprom->num_address_bytes) {
  47. if (eeprom->idx_write_cnt == 0)
  48. eeprom->buffer_idx = 0;
  49. eeprom->buffer_idx = *val | (eeprom->buffer_idx << 8);
  50. eeprom->idx_write_cnt++;
  51. } else {
  52. if (!eeprom->read_only) {
  53. spin_lock(&eeprom->buffer_lock);
  54. eeprom->buffer[eeprom->buffer_idx++ & eeprom->address_mask] = *val;
  55. spin_unlock(&eeprom->buffer_lock);
  56. }
  57. }
  58. break;
  59. case I2C_SLAVE_READ_PROCESSED:
  60. /* The previous byte made it to the bus, get next one */
  61. eeprom->buffer_idx++;
  62. fallthrough;
  63. case I2C_SLAVE_READ_REQUESTED:
  64. spin_lock(&eeprom->buffer_lock);
  65. *val = eeprom->buffer[eeprom->buffer_idx & eeprom->address_mask];
  66. spin_unlock(&eeprom->buffer_lock);
  67. /*
  68. * Do not increment buffer_idx here, because we don't know if
  69. * this byte will be actually used. Read Linux I2C slave docs
  70. * for details.
  71. */
  72. break;
  73. case I2C_SLAVE_STOP:
  74. case I2C_SLAVE_WRITE_REQUESTED:
  75. eeprom->idx_write_cnt = 0;
  76. break;
  77. default:
  78. break;
  79. }
  80. return 0;
  81. }
  82. static ssize_t i2c_slave_eeprom_bin_read(struct file *filp, struct kobject *kobj,
  83. struct bin_attribute *attr, char *buf, loff_t off, size_t count)
  84. {
  85. struct eeprom_data *eeprom;
  86. unsigned long flags;
  87. eeprom = dev_get_drvdata(kobj_to_dev(kobj));
  88. spin_lock_irqsave(&eeprom->buffer_lock, flags);
  89. memcpy(buf, &eeprom->buffer[off], count);
  90. spin_unlock_irqrestore(&eeprom->buffer_lock, flags);
  91. return count;
  92. }
  93. static ssize_t i2c_slave_eeprom_bin_write(struct file *filp, struct kobject *kobj,
  94. struct bin_attribute *attr, char *buf, loff_t off, size_t count)
  95. {
  96. struct eeprom_data *eeprom;
  97. unsigned long flags;
  98. eeprom = dev_get_drvdata(kobj_to_dev(kobj));
  99. spin_lock_irqsave(&eeprom->buffer_lock, flags);
  100. memcpy(&eeprom->buffer[off], buf, count);
  101. spin_unlock_irqrestore(&eeprom->buffer_lock, flags);
  102. return count;
  103. }
  104. static int i2c_slave_init_eeprom_data(struct eeprom_data *eeprom, struct i2c_client *client,
  105. unsigned int size)
  106. {
  107. const struct firmware *fw;
  108. const char *eeprom_data;
  109. int ret = device_property_read_string(&client->dev, "firmware-name", &eeprom_data);
  110. if (!ret) {
  111. ret = request_firmware_into_buf(&fw, eeprom_data, &client->dev,
  112. eeprom->buffer, size);
  113. if (ret)
  114. return ret;
  115. release_firmware(fw);
  116. } else {
  117. /* An empty eeprom typically has all bits set to 1 */
  118. memset(eeprom->buffer, 0xff, size);
  119. }
  120. return 0;
  121. }
  122. static int i2c_slave_eeprom_probe(struct i2c_client *client, const struct i2c_device_id *id)
  123. {
  124. struct eeprom_data *eeprom;
  125. int ret;
  126. unsigned int size = FIELD_GET(I2C_SLAVE_BYTELEN, id->driver_data) + 1;
  127. unsigned int flag_addr16 = FIELD_GET(I2C_SLAVE_FLAG_ADDR16, id->driver_data);
  128. eeprom = devm_kzalloc(&client->dev, sizeof(struct eeprom_data) + size, GFP_KERNEL);
  129. if (!eeprom)
  130. return -ENOMEM;
  131. eeprom->num_address_bytes = flag_addr16 ? 2 : 1;
  132. eeprom->address_mask = size - 1;
  133. eeprom->read_only = FIELD_GET(I2C_SLAVE_FLAG_RO, id->driver_data);
  134. spin_lock_init(&eeprom->buffer_lock);
  135. i2c_set_clientdata(client, eeprom);
  136. ret = i2c_slave_init_eeprom_data(eeprom, client, size);
  137. if (ret)
  138. return ret;
  139. sysfs_bin_attr_init(&eeprom->bin);
  140. eeprom->bin.attr.name = "slave-eeprom";
  141. eeprom->bin.attr.mode = S_IRUSR | S_IWUSR;
  142. eeprom->bin.read = i2c_slave_eeprom_bin_read;
  143. eeprom->bin.write = i2c_slave_eeprom_bin_write;
  144. eeprom->bin.size = size;
  145. ret = sysfs_create_bin_file(&client->dev.kobj, &eeprom->bin);
  146. if (ret)
  147. return ret;
  148. ret = i2c_slave_register(client, i2c_slave_eeprom_slave_cb);
  149. if (ret) {
  150. sysfs_remove_bin_file(&client->dev.kobj, &eeprom->bin);
  151. return ret;
  152. }
  153. return 0;
  154. };
  155. static void i2c_slave_eeprom_remove(struct i2c_client *client)
  156. {
  157. struct eeprom_data *eeprom = i2c_get_clientdata(client);
  158. i2c_slave_unregister(client);
  159. sysfs_remove_bin_file(&client->dev.kobj, &eeprom->bin);
  160. }
  161. static const struct i2c_device_id i2c_slave_eeprom_id[] = {
  162. { "slave-24c02", I2C_SLAVE_DEVICE_MAGIC(2048 / 8, 0) },
  163. { "slave-24c02ro", I2C_SLAVE_DEVICE_MAGIC(2048 / 8, I2C_SLAVE_FLAG_RO) },
  164. { "slave-24c32", I2C_SLAVE_DEVICE_MAGIC(32768 / 8, I2C_SLAVE_FLAG_ADDR16) },
  165. { "slave-24c32ro", I2C_SLAVE_DEVICE_MAGIC(32768 / 8, I2C_SLAVE_FLAG_ADDR16 | I2C_SLAVE_FLAG_RO) },
  166. { "slave-24c64", I2C_SLAVE_DEVICE_MAGIC(65536 / 8, I2C_SLAVE_FLAG_ADDR16) },
  167. { "slave-24c64ro", I2C_SLAVE_DEVICE_MAGIC(65536 / 8, I2C_SLAVE_FLAG_ADDR16 | I2C_SLAVE_FLAG_RO) },
  168. { "slave-24c512", I2C_SLAVE_DEVICE_MAGIC(524288 / 8, I2C_SLAVE_FLAG_ADDR16) },
  169. { "slave-24c512ro", I2C_SLAVE_DEVICE_MAGIC(524288 / 8, I2C_SLAVE_FLAG_ADDR16 | I2C_SLAVE_FLAG_RO) },
  170. { }
  171. };
  172. MODULE_DEVICE_TABLE(i2c, i2c_slave_eeprom_id);
  173. static struct i2c_driver i2c_slave_eeprom_driver = {
  174. .driver = {
  175. .name = "i2c-slave-eeprom",
  176. },
  177. .probe = i2c_slave_eeprom_probe,
  178. .remove = i2c_slave_eeprom_remove,
  179. .id_table = i2c_slave_eeprom_id,
  180. };
  181. module_i2c_driver(i2c_slave_eeprom_driver);
  182. MODULE_AUTHOR("Wolfram Sang <[email protected]>");
  183. MODULE_DESCRIPTION("I2C slave mode EEPROM simulator");
  184. MODULE_LICENSE("GPL v2");