Commit 11af27f4 authored by Björn Ardö's avatar Björn Ardö Committed by Wolfram Sang

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: default avatarBjörn Ardö <bjorn.ardo@axis.com>
Signed-off-by: default avatarWolfram Sang <wsa@the-dreams.de>
parent fd4b204a
...@@ -33,11 +33,13 @@ struct eeprom_data { ...@@ -33,11 +33,13 @@ struct eeprom_data {
u16 address_mask; u16 address_mask;
u8 num_address_bytes; u8 num_address_bytes;
u8 idx_write_cnt; u8 idx_write_cnt;
bool read_only;
u8 buffer[]; u8 buffer[];
}; };
#define I2C_SLAVE_BYTELEN GENMASK(15, 0) #define I2C_SLAVE_BYTELEN GENMASK(15, 0)
#define I2C_SLAVE_FLAG_ADDR16 BIT(16) #define I2C_SLAVE_FLAG_ADDR16 BIT(16)
#define I2C_SLAVE_FLAG_RO BIT(17)
#define I2C_SLAVE_DEVICE_MAGIC(_len, _flags) ((_flags) | (_len)) #define I2C_SLAVE_DEVICE_MAGIC(_len, _flags) ((_flags) | (_len))
static int i2c_slave_eeprom_slave_cb(struct i2c_client *client, 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, ...@@ -53,9 +55,11 @@ static int i2c_slave_eeprom_slave_cb(struct i2c_client *client,
eeprom->buffer_idx = *val | (eeprom->buffer_idx << 8); eeprom->buffer_idx = *val | (eeprom->buffer_idx << 8);
eeprom->idx_write_cnt++; eeprom->idx_write_cnt++;
} else { } else {
spin_lock(&eeprom->buffer_lock); if (!eeprom->read_only) {
eeprom->buffer[eeprom->buffer_idx++ & eeprom->address_mask] = *val; spin_lock(&eeprom->buffer_lock);
spin_unlock(&eeprom->buffer_lock); eeprom->buffer[eeprom->buffer_idx++ & eeprom->address_mask] = *val;
spin_unlock(&eeprom->buffer_lock);
}
} }
break; break;
...@@ -130,6 +134,7 @@ static int i2c_slave_eeprom_probe(struct i2c_client *client, const struct i2c_de ...@@ -130,6 +134,7 @@ static int i2c_slave_eeprom_probe(struct i2c_client *client, const struct i2c_de
eeprom->idx_write_cnt = 0; eeprom->idx_write_cnt = 0;
eeprom->num_address_bytes = flag_addr16 ? 2 : 1; eeprom->num_address_bytes = flag_addr16 ? 2 : 1;
eeprom->address_mask = size - 1; eeprom->address_mask = size - 1;
eeprom->read_only = FIELD_GET(I2C_SLAVE_FLAG_RO, id->driver_data);
spin_lock_init(&eeprom->buffer_lock); spin_lock_init(&eeprom->buffer_lock);
i2c_set_clientdata(client, eeprom); i2c_set_clientdata(client, eeprom);
...@@ -165,8 +170,11 @@ static int i2c_slave_eeprom_remove(struct i2c_client *client) ...@@ -165,8 +170,11 @@ static int i2c_slave_eeprom_remove(struct i2c_client *client)
static const struct i2c_device_id i2c_slave_eeprom_id[] = { static const struct i2c_device_id i2c_slave_eeprom_id[] = {
{ "slave-24c02", I2C_SLAVE_DEVICE_MAGIC(2048 / 8, 0) }, { "slave-24c02", I2C_SLAVE_DEVICE_MAGIC(2048 / 8, 0) },
{ "slave-24c02ro", I2C_SLAVE_DEVICE_MAGIC(2048 / 8, I2C_SLAVE_FLAG_RO) },
{ "slave-24c32", I2C_SLAVE_DEVICE_MAGIC(32768 / 8, I2C_SLAVE_FLAG_ADDR16) }, { "slave-24c32", I2C_SLAVE_DEVICE_MAGIC(32768 / 8, I2C_SLAVE_FLAG_ADDR16) },
{ "slave-24c32ro", I2C_SLAVE_DEVICE_MAGIC(32768 / 8, I2C_SLAVE_FLAG_ADDR16 | I2C_SLAVE_FLAG_RO) },
{ "slave-24c64", I2C_SLAVE_DEVICE_MAGIC(65536 / 8, I2C_SLAVE_FLAG_ADDR16) }, { "slave-24c64", I2C_SLAVE_DEVICE_MAGIC(65536 / 8, I2C_SLAVE_FLAG_ADDR16) },
{ "slave-24c64ro", I2C_SLAVE_DEVICE_MAGIC(65536 / 8, I2C_SLAVE_FLAG_ADDR16 | I2C_SLAVE_FLAG_RO) },
{ } { }
}; };
MODULE_DEVICE_TABLE(i2c, i2c_slave_eeprom_id); MODULE_DEVICE_TABLE(i2c, i2c_slave_eeprom_id);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment