i2c-slave-eeprom.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * I2C slave mode EEPROM simulator
  3. *
  4. * Copyright (C) 2014 by Wolfram Sang, Sang Engineering <wsa@sang-engineering.com>
  5. * Copyright (C) 2014 by Renesas Electronics Corporation
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; version 2 of the License.
  10. *
  11. * Because most IP blocks can only detect one I2C slave address anyhow, this
  12. * driver does not support simulating EEPROM types which take more than one
  13. * address. It is prepared to simulate bigger EEPROMs with an internal 16 bit
  14. * pointer, yet implementation is deferred until the need actually arises.
  15. */
  16. #include <linux/i2c.h>
  17. #include <linux/init.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/slab.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/sysfs.h>
  23. struct eeprom_data {
  24. struct bin_attribute bin;
  25. bool first_write;
  26. spinlock_t buffer_lock;
  27. u8 buffer_idx;
  28. u8 buffer[];
  29. };
  30. static int i2c_slave_eeprom_slave_cb(struct i2c_client *client,
  31. enum i2c_slave_event event, u8 *val)
  32. {
  33. struct eeprom_data *eeprom = i2c_get_clientdata(client);
  34. switch (event) {
  35. case I2C_SLAVE_WRITE_RECEIVED:
  36. if (eeprom->first_write) {
  37. eeprom->buffer_idx = *val;
  38. eeprom->first_write = false;
  39. } else {
  40. spin_lock(&eeprom->buffer_lock);
  41. eeprom->buffer[eeprom->buffer_idx++] = *val;
  42. spin_unlock(&eeprom->buffer_lock);
  43. }
  44. break;
  45. case I2C_SLAVE_READ_PROCESSED:
  46. /* The previous byte made it to the bus, get next one */
  47. eeprom->buffer_idx++;
  48. /* fallthrough */
  49. case I2C_SLAVE_READ_REQUESTED:
  50. spin_lock(&eeprom->buffer_lock);
  51. *val = eeprom->buffer[eeprom->buffer_idx];
  52. spin_unlock(&eeprom->buffer_lock);
  53. /*
  54. * Do not increment buffer_idx here, because we don't know if
  55. * this byte will be actually used. Read Linux I2C slave docs
  56. * for details.
  57. */
  58. break;
  59. case I2C_SLAVE_STOP:
  60. case I2C_SLAVE_WRITE_REQUESTED:
  61. eeprom->first_write = true;
  62. break;
  63. default:
  64. break;
  65. }
  66. return 0;
  67. }
  68. static ssize_t i2c_slave_eeprom_bin_read(struct file *filp, struct kobject *kobj,
  69. struct bin_attribute *attr, char *buf, loff_t off, size_t count)
  70. {
  71. struct eeprom_data *eeprom;
  72. unsigned long flags;
  73. if (off + count > attr->size)
  74. return -EFBIG;
  75. eeprom = dev_get_drvdata(container_of(kobj, struct device, kobj));
  76. spin_lock_irqsave(&eeprom->buffer_lock, flags);
  77. memcpy(buf, &eeprom->buffer[off], count);
  78. spin_unlock_irqrestore(&eeprom->buffer_lock, flags);
  79. return count;
  80. }
  81. static ssize_t i2c_slave_eeprom_bin_write(struct file *filp, struct kobject *kobj,
  82. struct bin_attribute *attr, char *buf, loff_t off, size_t count)
  83. {
  84. struct eeprom_data *eeprom;
  85. unsigned long flags;
  86. if (off + count > attr->size)
  87. return -EFBIG;
  88. eeprom = dev_get_drvdata(container_of(kobj, struct device, kobj));
  89. spin_lock_irqsave(&eeprom->buffer_lock, flags);
  90. memcpy(&eeprom->buffer[off], buf, count);
  91. spin_unlock_irqrestore(&eeprom->buffer_lock, flags);
  92. return count;
  93. }
  94. static int i2c_slave_eeprom_probe(struct i2c_client *client, const struct i2c_device_id *id)
  95. {
  96. struct eeprom_data *eeprom;
  97. int ret;
  98. unsigned size = id->driver_data;
  99. eeprom = devm_kzalloc(&client->dev, sizeof(struct eeprom_data) + size, GFP_KERNEL);
  100. if (!eeprom)
  101. return -ENOMEM;
  102. eeprom->first_write = true;
  103. spin_lock_init(&eeprom->buffer_lock);
  104. i2c_set_clientdata(client, eeprom);
  105. sysfs_bin_attr_init(&eeprom->bin);
  106. eeprom->bin.attr.name = "slave-eeprom";
  107. eeprom->bin.attr.mode = S_IRUSR | S_IWUSR;
  108. eeprom->bin.read = i2c_slave_eeprom_bin_read;
  109. eeprom->bin.write = i2c_slave_eeprom_bin_write;
  110. eeprom->bin.size = size;
  111. ret = sysfs_create_bin_file(&client->dev.kobj, &eeprom->bin);
  112. if (ret)
  113. return ret;
  114. ret = i2c_slave_register(client, i2c_slave_eeprom_slave_cb);
  115. if (ret) {
  116. sysfs_remove_bin_file(&client->dev.kobj, &eeprom->bin);
  117. return ret;
  118. }
  119. return 0;
  120. };
  121. static int i2c_slave_eeprom_remove(struct i2c_client *client)
  122. {
  123. struct eeprom_data *eeprom = i2c_get_clientdata(client);
  124. i2c_slave_unregister(client);
  125. sysfs_remove_bin_file(&client->dev.kobj, &eeprom->bin);
  126. return 0;
  127. }
  128. static const struct i2c_device_id i2c_slave_eeprom_id[] = {
  129. { "slave-24c02", 2048 / 8 },
  130. { }
  131. };
  132. MODULE_DEVICE_TABLE(i2c, i2c_slave_eeprom_id);
  133. static struct i2c_driver i2c_slave_eeprom_driver = {
  134. .driver = {
  135. .name = "i2c-slave-eeprom",
  136. .owner = THIS_MODULE,
  137. },
  138. .probe = i2c_slave_eeprom_probe,
  139. .remove = i2c_slave_eeprom_remove,
  140. .id_table = i2c_slave_eeprom_id,
  141. };
  142. module_i2c_driver(i2c_slave_eeprom_driver);
  143. MODULE_AUTHOR("Wolfram Sang <wsa@sang-engineering.com>");
  144. MODULE_DESCRIPTION("I2C slave mode EEPROM simulator");
  145. MODULE_LICENSE("GPL v2");