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.
14 #include <linux/bitfield.h>
15 #include <linux/i2c.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/spinlock.h>
21 #include <linux/sysfs.h>
24 struct bin_attribute bin;
25 spinlock_t buffer_lock;
33 #define I2C_SLAVE_BYTELEN GENMASK(15, 0)
34 #define I2C_SLAVE_FLAG_ADDR16 BIT(16)
35 #define I2C_SLAVE_DEVICE_MAGIC(_len, _flags) ((_flags) | (_len))
37 static int i2c_slave_eeprom_slave_cb(struct i2c_client *client,
38 enum i2c_slave_event event, u8 *val)
40 struct eeprom_data *eeprom = i2c_get_clientdata(client);
43 case I2C_SLAVE_WRITE_RECEIVED:
44 if (eeprom->idx_write_cnt < eeprom->num_address_bytes) {
45 if (eeprom->idx_write_cnt == 0)
46 eeprom->buffer_idx = 0;
47 eeprom->buffer_idx = *val | (eeprom->buffer_idx << 8);
48 eeprom->idx_write_cnt++;
50 spin_lock(&eeprom->buffer_lock);
51 eeprom->buffer[eeprom->buffer_idx++ & eeprom->address_mask] = *val;
52 spin_unlock(&eeprom->buffer_lock);
56 case I2C_SLAVE_READ_PROCESSED:
57 /* The previous byte made it to the bus, get next one */
60 case I2C_SLAVE_READ_REQUESTED:
61 spin_lock(&eeprom->buffer_lock);
62 *val = eeprom->buffer[eeprom->buffer_idx & eeprom->address_mask];
63 spin_unlock(&eeprom->buffer_lock);
65 * Do not increment buffer_idx here, because we don't know if
66 * this byte will be actually used. Read Linux I2C slave docs
72 case I2C_SLAVE_WRITE_REQUESTED:
73 eeprom->idx_write_cnt = 0;
83 static ssize_t i2c_slave_eeprom_bin_read(struct file *filp, struct kobject *kobj,
84 struct bin_attribute *attr, char *buf, loff_t off, size_t count)
86 struct eeprom_data *eeprom;
89 eeprom = dev_get_drvdata(container_of(kobj, struct device, kobj));
91 spin_lock_irqsave(&eeprom->buffer_lock, flags);
92 memcpy(buf, &eeprom->buffer[off], count);
93 spin_unlock_irqrestore(&eeprom->buffer_lock, flags);
98 static ssize_t i2c_slave_eeprom_bin_write(struct file *filp, struct kobject *kobj,
99 struct bin_attribute *attr, char *buf, loff_t off, size_t count)
101 struct eeprom_data *eeprom;
104 eeprom = dev_get_drvdata(container_of(kobj, struct device, kobj));
106 spin_lock_irqsave(&eeprom->buffer_lock, flags);
107 memcpy(&eeprom->buffer[off], buf, count);
108 spin_unlock_irqrestore(&eeprom->buffer_lock, flags);
113 static int i2c_slave_eeprom_probe(struct i2c_client *client, const struct i2c_device_id *id)
115 struct eeprom_data *eeprom;
117 unsigned int size = FIELD_GET(I2C_SLAVE_BYTELEN, id->driver_data);
118 unsigned int flag_addr16 = FIELD_GET(I2C_SLAVE_FLAG_ADDR16, id->driver_data);
120 eeprom = devm_kzalloc(&client->dev, sizeof(struct eeprom_data) + size, GFP_KERNEL);
124 eeprom->idx_write_cnt = 0;
125 eeprom->num_address_bytes = flag_addr16 ? 2 : 1;
126 eeprom->address_mask = size - 1;
127 spin_lock_init(&eeprom->buffer_lock);
128 i2c_set_clientdata(client, eeprom);
130 sysfs_bin_attr_init(&eeprom->bin);
131 eeprom->bin.attr.name = "slave-eeprom";
132 eeprom->bin.attr.mode = S_IRUSR | S_IWUSR;
133 eeprom->bin.read = i2c_slave_eeprom_bin_read;
134 eeprom->bin.write = i2c_slave_eeprom_bin_write;
135 eeprom->bin.size = size;
137 ret = sysfs_create_bin_file(&client->dev.kobj, &eeprom->bin);
141 ret = i2c_slave_register(client, i2c_slave_eeprom_slave_cb);
143 sysfs_remove_bin_file(&client->dev.kobj, &eeprom->bin);
150 static int i2c_slave_eeprom_remove(struct i2c_client *client)
152 struct eeprom_data *eeprom = i2c_get_clientdata(client);
154 i2c_slave_unregister(client);
155 sysfs_remove_bin_file(&client->dev.kobj, &eeprom->bin);
160 static const struct i2c_device_id i2c_slave_eeprom_id[] = {
161 { "slave-24c02", I2C_SLAVE_DEVICE_MAGIC(2048 / 8, 0) },
162 { "slave-24c32", I2C_SLAVE_DEVICE_MAGIC(32768 / 8, I2C_SLAVE_FLAG_ADDR16) },
163 { "slave-24c64", I2C_SLAVE_DEVICE_MAGIC(65536 / 8, I2C_SLAVE_FLAG_ADDR16) },
166 MODULE_DEVICE_TABLE(i2c, i2c_slave_eeprom_id);
168 static struct i2c_driver i2c_slave_eeprom_driver = {
170 .name = "i2c-slave-eeprom",
172 .probe = i2c_slave_eeprom_probe,
173 .remove = i2c_slave_eeprom_remove,
174 .id_table = i2c_slave_eeprom_id,
176 module_i2c_driver(i2c_slave_eeprom_driver);
178 MODULE_AUTHOR("Wolfram Sang <wsa@sang-engineering.com>");
179 MODULE_DESCRIPTION("I2C slave mode EEPROM simulator");
180 MODULE_LICENSE("GPL v2");