1 // SPDX-License-Identifier: GPL-2.0-only
3 * I2C slave mode EEPROM simulator
5 * Copyright (C) 2014 by Wolfram Sang, Sang Engineering <wsa@sang-engineering.com>
6 * Copyright (C) 2014 by Renesas Electronics Corporation
8 * Because most IP blocks can only detect one I2C slave address anyhow, this
9 * driver does not support simulating EEPROM types which take more than one
10 * address. It is prepared to simulate bigger EEPROMs with an internal 16 bit
11 * pointer, yet implementation is deferred until the need actually arises.
15 * FIXME: What to do if only 8 bits of a 16 bit address are sent?
16 * The ST-M24C64 sends only 0xff then. Needs verification with other
17 * EEPROMs, though. We currently use the 8 bit as a valid address.
20 #include <linux/bitfield.h>
21 #include <linux/i2c.h>
22 #include <linux/init.h>
23 #include <linux/module.h>
25 #include <linux/slab.h>
26 #include <linux/spinlock.h>
27 #include <linux/sysfs.h>
30 struct bin_attribute bin;
31 spinlock_t buffer_lock;
39 #define I2C_SLAVE_BYTELEN GENMASK(15, 0)
40 #define I2C_SLAVE_FLAG_ADDR16 BIT(16)
41 #define I2C_SLAVE_DEVICE_MAGIC(_len, _flags) ((_flags) | (_len))
43 static int i2c_slave_eeprom_slave_cb(struct i2c_client *client,
44 enum i2c_slave_event event, u8 *val)
46 struct eeprom_data *eeprom = i2c_get_clientdata(client);
49 case I2C_SLAVE_WRITE_RECEIVED:
50 if (eeprom->idx_write_cnt < eeprom->num_address_bytes) {
51 if (eeprom->idx_write_cnt == 0)
52 eeprom->buffer_idx = 0;
53 eeprom->buffer_idx = *val | (eeprom->buffer_idx << 8);
54 eeprom->idx_write_cnt++;
56 spin_lock(&eeprom->buffer_lock);
57 eeprom->buffer[eeprom->buffer_idx++ & eeprom->address_mask] = *val;
58 spin_unlock(&eeprom->buffer_lock);
62 case I2C_SLAVE_READ_PROCESSED:
63 /* The previous byte made it to the bus, get next one */
66 case I2C_SLAVE_READ_REQUESTED:
67 spin_lock(&eeprom->buffer_lock);
68 *val = eeprom->buffer[eeprom->buffer_idx & eeprom->address_mask];
69 spin_unlock(&eeprom->buffer_lock);
71 * Do not increment buffer_idx here, because we don't know if
72 * this byte will be actually used. Read Linux I2C slave docs
78 case I2C_SLAVE_WRITE_REQUESTED:
79 eeprom->idx_write_cnt = 0;
89 static ssize_t i2c_slave_eeprom_bin_read(struct file *filp, struct kobject *kobj,
90 struct bin_attribute *attr, char *buf, loff_t off, size_t count)
92 struct eeprom_data *eeprom;
95 eeprom = dev_get_drvdata(container_of(kobj, struct device, kobj));
97 spin_lock_irqsave(&eeprom->buffer_lock, flags);
98 memcpy(buf, &eeprom->buffer[off], count);
99 spin_unlock_irqrestore(&eeprom->buffer_lock, flags);
104 static ssize_t i2c_slave_eeprom_bin_write(struct file *filp, struct kobject *kobj,
105 struct bin_attribute *attr, char *buf, loff_t off, size_t count)
107 struct eeprom_data *eeprom;
110 eeprom = dev_get_drvdata(container_of(kobj, struct device, kobj));
112 spin_lock_irqsave(&eeprom->buffer_lock, flags);
113 memcpy(&eeprom->buffer[off], buf, count);
114 spin_unlock_irqrestore(&eeprom->buffer_lock, flags);
119 static int i2c_slave_eeprom_probe(struct i2c_client *client, const struct i2c_device_id *id)
121 struct eeprom_data *eeprom;
123 unsigned int size = FIELD_GET(I2C_SLAVE_BYTELEN, id->driver_data);
124 unsigned int flag_addr16 = FIELD_GET(I2C_SLAVE_FLAG_ADDR16, id->driver_data);
126 eeprom = devm_kzalloc(&client->dev, sizeof(struct eeprom_data) + size, GFP_KERNEL);
130 eeprom->idx_write_cnt = 0;
131 eeprom->num_address_bytes = flag_addr16 ? 2 : 1;
132 eeprom->address_mask = size - 1;
133 spin_lock_init(&eeprom->buffer_lock);
134 i2c_set_clientdata(client, eeprom);
136 sysfs_bin_attr_init(&eeprom->bin);
137 eeprom->bin.attr.name = "slave-eeprom";
138 eeprom->bin.attr.mode = S_IRUSR | S_IWUSR;
139 eeprom->bin.read = i2c_slave_eeprom_bin_read;
140 eeprom->bin.write = i2c_slave_eeprom_bin_write;
141 eeprom->bin.size = size;
143 ret = sysfs_create_bin_file(&client->dev.kobj, &eeprom->bin);
147 ret = i2c_slave_register(client, i2c_slave_eeprom_slave_cb);
149 sysfs_remove_bin_file(&client->dev.kobj, &eeprom->bin);
156 static int i2c_slave_eeprom_remove(struct i2c_client *client)
158 struct eeprom_data *eeprom = i2c_get_clientdata(client);
160 i2c_slave_unregister(client);
161 sysfs_remove_bin_file(&client->dev.kobj, &eeprom->bin);
166 static const struct i2c_device_id i2c_slave_eeprom_id[] = {
167 { "slave-24c02", I2C_SLAVE_DEVICE_MAGIC(2048 / 8, 0) },
168 { "slave-24c32", I2C_SLAVE_DEVICE_MAGIC(32768 / 8, I2C_SLAVE_FLAG_ADDR16) },
169 { "slave-24c64", I2C_SLAVE_DEVICE_MAGIC(65536 / 8, I2C_SLAVE_FLAG_ADDR16) },
172 MODULE_DEVICE_TABLE(i2c, i2c_slave_eeprom_id);
174 static struct i2c_driver i2c_slave_eeprom_driver = {
176 .name = "i2c-slave-eeprom",
178 .probe = i2c_slave_eeprom_probe,
179 .remove = i2c_slave_eeprom_remove,
180 .id_table = i2c_slave_eeprom_id,
182 module_i2c_driver(i2c_slave_eeprom_driver);
184 MODULE_AUTHOR("Wolfram Sang <wsa@sang-engineering.com>");
185 MODULE_DESCRIPTION("I2C slave mode EEPROM simulator");
186 MODULE_LICENSE("GPL v2");