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
;
40 #define I2C_SLAVE_BYTELEN GENMASK(15, 0)
41 #define I2C_SLAVE_FLAG_ADDR16 BIT(16)
42 #define I2C_SLAVE_FLAG_RO BIT(17)
43 #define I2C_SLAVE_DEVICE_MAGIC(_len, _flags) ((_flags) | (_len))
45 static int i2c_slave_eeprom_slave_cb(struct i2c_client
*client
,
46 enum i2c_slave_event event
, u8
*val
)
48 struct eeprom_data
*eeprom
= i2c_get_clientdata(client
);
51 case I2C_SLAVE_WRITE_RECEIVED
:
52 if (eeprom
->idx_write_cnt
< eeprom
->num_address_bytes
) {
53 if (eeprom
->idx_write_cnt
== 0)
54 eeprom
->buffer_idx
= 0;
55 eeprom
->buffer_idx
= *val
| (eeprom
->buffer_idx
<< 8);
56 eeprom
->idx_write_cnt
++;
58 if (!eeprom
->read_only
) {
59 spin_lock(&eeprom
->buffer_lock
);
60 eeprom
->buffer
[eeprom
->buffer_idx
++ & eeprom
->address_mask
] = *val
;
61 spin_unlock(&eeprom
->buffer_lock
);
66 case I2C_SLAVE_READ_PROCESSED
:
67 /* The previous byte made it to the bus, get next one */
70 case I2C_SLAVE_READ_REQUESTED
:
71 spin_lock(&eeprom
->buffer_lock
);
72 *val
= eeprom
->buffer
[eeprom
->buffer_idx
& eeprom
->address_mask
];
73 spin_unlock(&eeprom
->buffer_lock
);
75 * Do not increment buffer_idx here, because we don't know if
76 * this byte will be actually used. Read Linux I2C slave docs
82 case I2C_SLAVE_WRITE_REQUESTED
:
83 eeprom
->idx_write_cnt
= 0;
93 static ssize_t
i2c_slave_eeprom_bin_read(struct file
*filp
, struct kobject
*kobj
,
94 struct bin_attribute
*attr
, char *buf
, loff_t off
, size_t count
)
96 struct eeprom_data
*eeprom
;
99 eeprom
= dev_get_drvdata(kobj_to_dev(kobj
));
101 spin_lock_irqsave(&eeprom
->buffer_lock
, flags
);
102 memcpy(buf
, &eeprom
->buffer
[off
], count
);
103 spin_unlock_irqrestore(&eeprom
->buffer_lock
, flags
);
108 static ssize_t
i2c_slave_eeprom_bin_write(struct file
*filp
, struct kobject
*kobj
,
109 struct bin_attribute
*attr
, char *buf
, loff_t off
, size_t count
)
111 struct eeprom_data
*eeprom
;
114 eeprom
= dev_get_drvdata(kobj_to_dev(kobj
));
116 spin_lock_irqsave(&eeprom
->buffer_lock
, flags
);
117 memcpy(&eeprom
->buffer
[off
], buf
, count
);
118 spin_unlock_irqrestore(&eeprom
->buffer_lock
, flags
);
123 static int i2c_slave_eeprom_probe(struct i2c_client
*client
, const struct i2c_device_id
*id
)
125 struct eeprom_data
*eeprom
;
127 unsigned int size
= FIELD_GET(I2C_SLAVE_BYTELEN
, id
->driver_data
);
128 unsigned int flag_addr16
= FIELD_GET(I2C_SLAVE_FLAG_ADDR16
, id
->driver_data
);
130 eeprom
= devm_kzalloc(&client
->dev
, sizeof(struct eeprom_data
) + size
, GFP_KERNEL
);
134 eeprom
->idx_write_cnt
= 0;
135 eeprom
->num_address_bytes
= flag_addr16
? 2 : 1;
136 eeprom
->address_mask
= size
- 1;
137 eeprom
->read_only
= FIELD_GET(I2C_SLAVE_FLAG_RO
, id
->driver_data
);
138 spin_lock_init(&eeprom
->buffer_lock
);
139 i2c_set_clientdata(client
, eeprom
);
141 sysfs_bin_attr_init(&eeprom
->bin
);
142 eeprom
->bin
.attr
.name
= "slave-eeprom";
143 eeprom
->bin
.attr
.mode
= S_IRUSR
| S_IWUSR
;
144 eeprom
->bin
.read
= i2c_slave_eeprom_bin_read
;
145 eeprom
->bin
.write
= i2c_slave_eeprom_bin_write
;
146 eeprom
->bin
.size
= size
;
148 ret
= sysfs_create_bin_file(&client
->dev
.kobj
, &eeprom
->bin
);
152 ret
= i2c_slave_register(client
, i2c_slave_eeprom_slave_cb
);
154 sysfs_remove_bin_file(&client
->dev
.kobj
, &eeprom
->bin
);
161 static int i2c_slave_eeprom_remove(struct i2c_client
*client
)
163 struct eeprom_data
*eeprom
= i2c_get_clientdata(client
);
165 i2c_slave_unregister(client
);
166 sysfs_remove_bin_file(&client
->dev
.kobj
, &eeprom
->bin
);
171 static const struct i2c_device_id i2c_slave_eeprom_id
[] = {
172 { "slave-24c02", I2C_SLAVE_DEVICE_MAGIC(2048 / 8, 0) },
173 { "slave-24c02ro", I2C_SLAVE_DEVICE_MAGIC(2048 / 8, I2C_SLAVE_FLAG_RO
) },
174 { "slave-24c32", I2C_SLAVE_DEVICE_MAGIC(32768 / 8, I2C_SLAVE_FLAG_ADDR16
) },
175 { "slave-24c32ro", I2C_SLAVE_DEVICE_MAGIC(32768 / 8, I2C_SLAVE_FLAG_ADDR16
| I2C_SLAVE_FLAG_RO
) },
176 { "slave-24c64", I2C_SLAVE_DEVICE_MAGIC(65536 / 8, I2C_SLAVE_FLAG_ADDR16
) },
177 { "slave-24c64ro", I2C_SLAVE_DEVICE_MAGIC(65536 / 8, I2C_SLAVE_FLAG_ADDR16
| I2C_SLAVE_FLAG_RO
) },
180 MODULE_DEVICE_TABLE(i2c
, i2c_slave_eeprom_id
);
182 static struct i2c_driver i2c_slave_eeprom_driver
= {
184 .name
= "i2c-slave-eeprom",
186 .probe
= i2c_slave_eeprom_probe
,
187 .remove
= i2c_slave_eeprom_remove
,
188 .id_table
= i2c_slave_eeprom_id
,
190 module_i2c_driver(i2c_slave_eeprom_driver
);
192 MODULE_AUTHOR("Wolfram Sang <wsa@sang-engineering.com>");
193 MODULE_DESCRIPTION("I2C slave mode EEPROM simulator");
194 MODULE_LICENSE("GPL v2");