i2c-slave-eeprom.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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_REQ_WRITE_END:
  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_REQ_READ_START:
  46. spin_lock(&eeprom->buffer_lock);
  47. *val = eeprom->buffer[eeprom->buffer_idx];
  48. spin_unlock(&eeprom->buffer_lock);
  49. break;
  50. case I2C_SLAVE_REQ_READ_END:
  51. eeprom->buffer_idx++;
  52. break;
  53. case I2C_SLAVE_STOP:
  54. eeprom->first_write = true;
  55. break;
  56. default:
  57. break;
  58. }
  59. return 0;
  60. }
  61. static ssize_t i2c_slave_eeprom_bin_read(struct file *filp, struct kobject *kobj,
  62. struct bin_attribute *attr, char *buf, loff_t off, size_t count)
  63. {
  64. struct eeprom_data *eeprom;
  65. unsigned long flags;
  66. if (off + count >= attr->size)
  67. return -EFBIG;
  68. eeprom = dev_get_drvdata(container_of(kobj, struct device, kobj));
  69. spin_lock_irqsave(&eeprom->buffer_lock, flags);
  70. memcpy(buf, &eeprom->buffer[off], count);
  71. spin_unlock_irqrestore(&eeprom->buffer_lock, flags);
  72. return count;
  73. }
  74. static ssize_t i2c_slave_eeprom_bin_write(struct file *filp, struct kobject *kobj,
  75. struct bin_attribute *attr, char *buf, loff_t off, size_t count)
  76. {
  77. struct eeprom_data *eeprom;
  78. unsigned long flags;
  79. if (off + count >= attr->size)
  80. return -EFBIG;
  81. eeprom = dev_get_drvdata(container_of(kobj, struct device, kobj));
  82. spin_lock_irqsave(&eeprom->buffer_lock, flags);
  83. memcpy(&eeprom->buffer[off], buf, count);
  84. spin_unlock_irqrestore(&eeprom->buffer_lock, flags);
  85. return count;
  86. }
  87. static int i2c_slave_eeprom_probe(struct i2c_client *client, const struct i2c_device_id *id)
  88. {
  89. struct eeprom_data *eeprom;
  90. int ret;
  91. unsigned size = id->driver_data;
  92. eeprom = devm_kzalloc(&client->dev, sizeof(struct eeprom_data) + size, GFP_KERNEL);
  93. if (!eeprom)
  94. return -ENOMEM;
  95. eeprom->first_write = true;
  96. spin_lock_init(&eeprom->buffer_lock);
  97. i2c_set_clientdata(client, eeprom);
  98. sysfs_bin_attr_init(&eeprom->bin);
  99. eeprom->bin.attr.name = "slave-eeprom";
  100. eeprom->bin.attr.mode = S_IRUSR | S_IWUSR;
  101. eeprom->bin.read = i2c_slave_eeprom_bin_read;
  102. eeprom->bin.write = i2c_slave_eeprom_bin_write;
  103. eeprom->bin.size = size;
  104. ret = sysfs_create_bin_file(&client->dev.kobj, &eeprom->bin);
  105. if (ret)
  106. return ret;
  107. ret = i2c_slave_register(client, i2c_slave_eeprom_slave_cb);
  108. if (ret) {
  109. sysfs_remove_bin_file(&client->dev.kobj, &eeprom->bin);
  110. return ret;
  111. }
  112. return 0;
  113. };
  114. static int i2c_slave_eeprom_remove(struct i2c_client *client)
  115. {
  116. struct eeprom_data *eeprom = i2c_get_clientdata(client);
  117. i2c_slave_unregister(client);
  118. sysfs_remove_bin_file(&client->dev.kobj, &eeprom->bin);
  119. return 0;
  120. }
  121. static const struct i2c_device_id i2c_slave_eeprom_id[] = {
  122. { "slave-24c02", 2048 / 8 },
  123. { }
  124. };
  125. MODULE_DEVICE_TABLE(i2c, i2c_slave_eeprom_id);
  126. static struct i2c_driver i2c_slave_eeprom_driver = {
  127. .driver = {
  128. .name = "i2c-slave-eeprom",
  129. .owner = THIS_MODULE,
  130. },
  131. .probe = i2c_slave_eeprom_probe,
  132. .remove = i2c_slave_eeprom_remove,
  133. .id_table = i2c_slave_eeprom_id,
  134. };
  135. module_i2c_driver(i2c_slave_eeprom_driver);
  136. MODULE_AUTHOR("Wolfram Sang <wsa@sang-engineering.com>");
  137. MODULE_DESCRIPTION("I2C slave mode EEPROM simulator");
  138. MODULE_LICENSE("GPL v2");