i2c-slave-eeprom.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. eeprom = dev_get_drvdata(container_of(kobj, struct device, kobj));
  74. spin_lock_irqsave(&eeprom->buffer_lock, flags);
  75. memcpy(buf, &eeprom->buffer[off], count);
  76. spin_unlock_irqrestore(&eeprom->buffer_lock, flags);
  77. return count;
  78. }
  79. static ssize_t i2c_slave_eeprom_bin_write(struct file *filp, struct kobject *kobj,
  80. struct bin_attribute *attr, char *buf, loff_t off, size_t count)
  81. {
  82. struct eeprom_data *eeprom;
  83. unsigned long flags;
  84. eeprom = dev_get_drvdata(container_of(kobj, struct device, kobj));
  85. spin_lock_irqsave(&eeprom->buffer_lock, flags);
  86. memcpy(&eeprom->buffer[off], buf, count);
  87. spin_unlock_irqrestore(&eeprom->buffer_lock, flags);
  88. return count;
  89. }
  90. static int i2c_slave_eeprom_probe(struct i2c_client *client, const struct i2c_device_id *id)
  91. {
  92. struct eeprom_data *eeprom;
  93. int ret;
  94. unsigned size = id->driver_data;
  95. eeprom = devm_kzalloc(&client->dev, sizeof(struct eeprom_data) + size, GFP_KERNEL);
  96. if (!eeprom)
  97. return -ENOMEM;
  98. eeprom->first_write = true;
  99. spin_lock_init(&eeprom->buffer_lock);
  100. i2c_set_clientdata(client, eeprom);
  101. sysfs_bin_attr_init(&eeprom->bin);
  102. eeprom->bin.attr.name = "slave-eeprom";
  103. eeprom->bin.attr.mode = S_IRUSR | S_IWUSR;
  104. eeprom->bin.read = i2c_slave_eeprom_bin_read;
  105. eeprom->bin.write = i2c_slave_eeprom_bin_write;
  106. eeprom->bin.size = size;
  107. ret = sysfs_create_bin_file(&client->dev.kobj, &eeprom->bin);
  108. if (ret)
  109. return ret;
  110. ret = i2c_slave_register(client, i2c_slave_eeprom_slave_cb);
  111. if (ret) {
  112. sysfs_remove_bin_file(&client->dev.kobj, &eeprom->bin);
  113. return ret;
  114. }
  115. return 0;
  116. };
  117. static int i2c_slave_eeprom_remove(struct i2c_client *client)
  118. {
  119. struct eeprom_data *eeprom = i2c_get_clientdata(client);
  120. i2c_slave_unregister(client);
  121. sysfs_remove_bin_file(&client->dev.kobj, &eeprom->bin);
  122. return 0;
  123. }
  124. static const struct i2c_device_id i2c_slave_eeprom_id[] = {
  125. { "slave-24c02", 2048 / 8 },
  126. { }
  127. };
  128. MODULE_DEVICE_TABLE(i2c, i2c_slave_eeprom_id);
  129. static struct i2c_driver i2c_slave_eeprom_driver = {
  130. .driver = {
  131. .name = "i2c-slave-eeprom",
  132. },
  133. .probe = i2c_slave_eeprom_probe,
  134. .remove = i2c_slave_eeprom_remove,
  135. .id_table = i2c_slave_eeprom_id,
  136. };
  137. module_i2c_driver(i2c_slave_eeprom_driver);
  138. MODULE_AUTHOR("Wolfram Sang <wsa@sang-engineering.com>");
  139. MODULE_DESCRIPTION("I2C slave mode EEPROM simulator");
  140. MODULE_LICENSE("GPL v2");