i2c-eeprom_slave: Add support for more eeprom models
Add a 32 and a 64 kbit memory. These needs 16 bit address so added support for that as well. Signed-off-by: Björn Ardö <bjorn.ardo@axis.com> Signed-off-by: Wolfram Sang <wsa@the-dreams.de>
This commit is contained in:
Родитель
0a321b9736
Коммит
82d5148154
|
@ -11,6 +11,7 @@
|
||||||
* pointer, yet implementation is deferred until the need actually arises.
|
* pointer, yet implementation is deferred until the need actually arises.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <linux/bitfield.h>
|
||||||
#include <linux/i2c.h>
|
#include <linux/i2c.h>
|
||||||
#include <linux/init.h>
|
#include <linux/init.h>
|
||||||
#include <linux/module.h>
|
#include <linux/module.h>
|
||||||
|
@ -21,12 +22,18 @@
|
||||||
|
|
||||||
struct eeprom_data {
|
struct eeprom_data {
|
||||||
struct bin_attribute bin;
|
struct bin_attribute bin;
|
||||||
bool first_write;
|
|
||||||
spinlock_t buffer_lock;
|
spinlock_t buffer_lock;
|
||||||
u8 buffer_idx;
|
u16 buffer_idx;
|
||||||
|
u16 address_mask;
|
||||||
|
u8 num_address_bytes;
|
||||||
|
u8 idx_write_cnt;
|
||||||
u8 buffer[];
|
u8 buffer[];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#define I2C_SLAVE_BYTELEN GENMASK(15, 0)
|
||||||
|
#define I2C_SLAVE_FLAG_ADDR16 BIT(16)
|
||||||
|
#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,
|
||||||
enum i2c_slave_event event, u8 *val)
|
enum i2c_slave_event event, u8 *val)
|
||||||
{
|
{
|
||||||
|
@ -34,12 +41,14 @@ static int i2c_slave_eeprom_slave_cb(struct i2c_client *client,
|
||||||
|
|
||||||
switch (event) {
|
switch (event) {
|
||||||
case I2C_SLAVE_WRITE_RECEIVED:
|
case I2C_SLAVE_WRITE_RECEIVED:
|
||||||
if (eeprom->first_write) {
|
if (eeprom->idx_write_cnt < eeprom->num_address_bytes) {
|
||||||
eeprom->buffer_idx = *val;
|
if (eeprom->idx_write_cnt == 0)
|
||||||
eeprom->first_write = false;
|
eeprom->buffer_idx = 0;
|
||||||
|
eeprom->buffer_idx = *val | (eeprom->buffer_idx << 8);
|
||||||
|
eeprom->idx_write_cnt++;
|
||||||
} else {
|
} else {
|
||||||
spin_lock(&eeprom->buffer_lock);
|
spin_lock(&eeprom->buffer_lock);
|
||||||
eeprom->buffer[eeprom->buffer_idx++] = *val;
|
eeprom->buffer[eeprom->buffer_idx++ & eeprom->address_mask] = *val;
|
||||||
spin_unlock(&eeprom->buffer_lock);
|
spin_unlock(&eeprom->buffer_lock);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -50,7 +59,7 @@ static int i2c_slave_eeprom_slave_cb(struct i2c_client *client,
|
||||||
/* fallthrough */
|
/* fallthrough */
|
||||||
case I2C_SLAVE_READ_REQUESTED:
|
case I2C_SLAVE_READ_REQUESTED:
|
||||||
spin_lock(&eeprom->buffer_lock);
|
spin_lock(&eeprom->buffer_lock);
|
||||||
*val = eeprom->buffer[eeprom->buffer_idx];
|
*val = eeprom->buffer[eeprom->buffer_idx & eeprom->address_mask];
|
||||||
spin_unlock(&eeprom->buffer_lock);
|
spin_unlock(&eeprom->buffer_lock);
|
||||||
/*
|
/*
|
||||||
* Do not increment buffer_idx here, because we don't know if
|
* Do not increment buffer_idx here, because we don't know if
|
||||||
|
@ -61,7 +70,7 @@ static int i2c_slave_eeprom_slave_cb(struct i2c_client *client,
|
||||||
|
|
||||||
case I2C_SLAVE_STOP:
|
case I2C_SLAVE_STOP:
|
||||||
case I2C_SLAVE_WRITE_REQUESTED:
|
case I2C_SLAVE_WRITE_REQUESTED:
|
||||||
eeprom->first_write = true;
|
eeprom->idx_write_cnt = 0;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
@ -105,13 +114,16 @@ static int i2c_slave_eeprom_probe(struct i2c_client *client, const struct i2c_de
|
||||||
{
|
{
|
||||||
struct eeprom_data *eeprom;
|
struct eeprom_data *eeprom;
|
||||||
int ret;
|
int ret;
|
||||||
unsigned size = id->driver_data;
|
unsigned int size = FIELD_GET(I2C_SLAVE_BYTELEN, id->driver_data);
|
||||||
|
unsigned int flag_addr16 = FIELD_GET(I2C_SLAVE_FLAG_ADDR16, id->driver_data);
|
||||||
|
|
||||||
eeprom = devm_kzalloc(&client->dev, sizeof(struct eeprom_data) + size, GFP_KERNEL);
|
eeprom = devm_kzalloc(&client->dev, sizeof(struct eeprom_data) + size, GFP_KERNEL);
|
||||||
if (!eeprom)
|
if (!eeprom)
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
|
|
||||||
eeprom->first_write = true;
|
eeprom->idx_write_cnt = 0;
|
||||||
|
eeprom->num_address_bytes = flag_addr16 ? 2 : 1;
|
||||||
|
eeprom->address_mask = size - 1;
|
||||||
spin_lock_init(&eeprom->buffer_lock);
|
spin_lock_init(&eeprom->buffer_lock);
|
||||||
i2c_set_clientdata(client, eeprom);
|
i2c_set_clientdata(client, eeprom);
|
||||||
|
|
||||||
|
@ -146,7 +158,9 @@ 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", 2048 / 8 },
|
{ "slave-24c02", I2C_SLAVE_DEVICE_MAGIC(2048 / 8, 0) },
|
||||||
|
{ "slave-24c32", I2C_SLAVE_DEVICE_MAGIC(32768 / 8, I2C_SLAVE_FLAG_ADDR16) },
|
||||||
|
{ "slave-24c64", I2C_SLAVE_DEVICE_MAGIC(65536 / 8, I2C_SLAVE_FLAG_ADDR16) },
|
||||||
{ }
|
{ }
|
||||||
};
|
};
|
||||||
MODULE_DEVICE_TABLE(i2c, i2c_slave_eeprom_id);
|
MODULE_DEVICE_TABLE(i2c, i2c_slave_eeprom_id);
|
||||||
|
|
Загрузка…
Ссылка в новой задаче