Skip to content

Commit 11af27f

Browse files
bjornaxisWolfram Sang
authored andcommitted
i2c: slave-eeprom: Add read only mode
Add read-only versions of all EEPROMs. These versions are read-only on the i2c side, but can be written from the sysfs side. Signed-off-by: Björn Ardö <[email protected]> Signed-off-by: Wolfram Sang <[email protected]>
1 parent fd4b204 commit 11af27f

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

drivers/i2c/i2c-slave-eeprom.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@ struct eeprom_data {
3333
u16 address_mask;
3434
u8 num_address_bytes;
3535
u8 idx_write_cnt;
36+
bool read_only;
3637
u8 buffer[];
3738
};
3839

3940
#define I2C_SLAVE_BYTELEN GENMASK(15, 0)
4041
#define I2C_SLAVE_FLAG_ADDR16 BIT(16)
42+
#define I2C_SLAVE_FLAG_RO BIT(17)
4143
#define I2C_SLAVE_DEVICE_MAGIC(_len, _flags) ((_flags) | (_len))
4244

4345
static int i2c_slave_eeprom_slave_cb(struct i2c_client *client,
@@ -53,9 +55,11 @@ static int i2c_slave_eeprom_slave_cb(struct i2c_client *client,
5355
eeprom->buffer_idx = *val | (eeprom->buffer_idx << 8);
5456
eeprom->idx_write_cnt++;
5557
} else {
56-
spin_lock(&eeprom->buffer_lock);
57-
eeprom->buffer[eeprom->buffer_idx++ & eeprom->address_mask] = *val;
58-
spin_unlock(&eeprom->buffer_lock);
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);
62+
}
5963
}
6064
break;
6165

@@ -130,6 +134,7 @@ static int i2c_slave_eeprom_probe(struct i2c_client *client, const struct i2c_de
130134
eeprom->idx_write_cnt = 0;
131135
eeprom->num_address_bytes = flag_addr16 ? 2 : 1;
132136
eeprom->address_mask = size - 1;
137+
eeprom->read_only = FIELD_GET(I2C_SLAVE_FLAG_RO, id->driver_data);
133138
spin_lock_init(&eeprom->buffer_lock);
134139
i2c_set_clientdata(client, eeprom);
135140

@@ -165,8 +170,11 @@ static int i2c_slave_eeprom_remove(struct i2c_client *client)
165170

166171
static const struct i2c_device_id i2c_slave_eeprom_id[] = {
167172
{ "slave-24c02", I2C_SLAVE_DEVICE_MAGIC(2048 / 8, 0) },
173+
{ "slave-24c02ro", I2C_SLAVE_DEVICE_MAGIC(2048 / 8, I2C_SLAVE_FLAG_RO) },
168174
{ "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) },
169176
{ "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) },
170178
{ }
171179
};
172180
MODULE_DEVICE_TABLE(i2c, i2c_slave_eeprom_id);

0 commit comments

Comments
 (0)