2019-06-01 15:08:55 +07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2014-11-18 23:04:54 +07:00
|
|
|
/*
|
|
|
|
* I2C slave mode EEPROM simulator
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 by Wolfram Sang, Sang Engineering <wsa@sang-engineering.com>
|
|
|
|
* Copyright (C) 2014 by Renesas Electronics Corporation
|
|
|
|
*
|
|
|
|
* Because most IP blocks can only detect one I2C slave address anyhow, this
|
|
|
|
* driver does not support simulating EEPROM types which take more than one
|
|
|
|
* address. It is prepared to simulate bigger EEPROMs with an internal 16 bit
|
|
|
|
* pointer, yet implementation is deferred until the need actually arises.
|
|
|
|
*/
|
|
|
|
|
2019-09-05 21:50:26 +07:00
|
|
|
/*
|
|
|
|
* FIXME: What to do if only 8 bits of a 16 bit address are sent?
|
|
|
|
* The ST-M24C64 sends only 0xff then. Needs verification with other
|
|
|
|
* EEPROMs, though. We currently use the 8 bit as a valid address.
|
|
|
|
*/
|
|
|
|
|
2019-09-04 16:29:53 +07:00
|
|
|
#include <linux/bitfield.h>
|
2014-11-18 23:04:54 +07:00
|
|
|
#include <linux/i2c.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/of.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <linux/sysfs.h>
|
|
|
|
|
|
|
|
struct eeprom_data {
|
|
|
|
struct bin_attribute bin;
|
|
|
|
spinlock_t buffer_lock;
|
2019-09-04 16:29:53 +07:00
|
|
|
u16 buffer_idx;
|
|
|
|
u16 address_mask;
|
|
|
|
u8 num_address_bytes;
|
|
|
|
u8 idx_write_cnt;
|
2014-11-18 23:04:54 +07:00
|
|
|
u8 buffer[];
|
|
|
|
};
|
|
|
|
|
2019-09-04 16:29:53 +07:00
|
|
|
#define I2C_SLAVE_BYTELEN GENMASK(15, 0)
|
|
|
|
#define I2C_SLAVE_FLAG_ADDR16 BIT(16)
|
|
|
|
#define I2C_SLAVE_DEVICE_MAGIC(_len, _flags) ((_flags) | (_len))
|
|
|
|
|
2014-11-18 23:04:54 +07:00
|
|
|
static int i2c_slave_eeprom_slave_cb(struct i2c_client *client,
|
|
|
|
enum i2c_slave_event event, u8 *val)
|
|
|
|
{
|
|
|
|
struct eeprom_data *eeprom = i2c_get_clientdata(client);
|
|
|
|
|
|
|
|
switch (event) {
|
2015-03-23 15:26:36 +07:00
|
|
|
case I2C_SLAVE_WRITE_RECEIVED:
|
2019-09-04 16:29:53 +07:00
|
|
|
if (eeprom->idx_write_cnt < eeprom->num_address_bytes) {
|
|
|
|
if (eeprom->idx_write_cnt == 0)
|
|
|
|
eeprom->buffer_idx = 0;
|
|
|
|
eeprom->buffer_idx = *val | (eeprom->buffer_idx << 8);
|
|
|
|
eeprom->idx_write_cnt++;
|
2014-11-18 23:04:54 +07:00
|
|
|
} else {
|
|
|
|
spin_lock(&eeprom->buffer_lock);
|
2019-09-04 16:29:53 +07:00
|
|
|
eeprom->buffer[eeprom->buffer_idx++ & eeprom->address_mask] = *val;
|
2014-11-18 23:04:54 +07:00
|
|
|
spin_unlock(&eeprom->buffer_lock);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2015-03-23 15:26:36 +07:00
|
|
|
case I2C_SLAVE_READ_PROCESSED:
|
2015-03-23 15:26:39 +07:00
|
|
|
/* The previous byte made it to the bus, get next one */
|
2015-03-23 15:26:36 +07:00
|
|
|
eeprom->buffer_idx++;
|
|
|
|
/* fallthrough */
|
|
|
|
case I2C_SLAVE_READ_REQUESTED:
|
2014-11-18 23:04:54 +07:00
|
|
|
spin_lock(&eeprom->buffer_lock);
|
2019-09-04 16:29:53 +07:00
|
|
|
*val = eeprom->buffer[eeprom->buffer_idx & eeprom->address_mask];
|
2014-11-18 23:04:54 +07:00
|
|
|
spin_unlock(&eeprom->buffer_lock);
|
2015-03-23 15:26:39 +07:00
|
|
|
/*
|
|
|
|
* Do not increment buffer_idx here, because we don't know if
|
|
|
|
* this byte will be actually used. Read Linux I2C slave docs
|
|
|
|
* for details.
|
|
|
|
*/
|
2014-11-18 23:04:54 +07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case I2C_SLAVE_STOP:
|
2015-03-23 15:26:36 +07:00
|
|
|
case I2C_SLAVE_WRITE_REQUESTED:
|
2019-09-04 16:29:53 +07:00
|
|
|
eeprom->idx_write_cnt = 0;
|
2014-11-18 23:04:54 +07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t i2c_slave_eeprom_bin_read(struct file *filp, struct kobject *kobj,
|
|
|
|
struct bin_attribute *attr, char *buf, loff_t off, size_t count)
|
|
|
|
{
|
|
|
|
struct eeprom_data *eeprom;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
eeprom = dev_get_drvdata(container_of(kobj, struct device, kobj));
|
|
|
|
|
|
|
|
spin_lock_irqsave(&eeprom->buffer_lock, flags);
|
|
|
|
memcpy(buf, &eeprom->buffer[off], count);
|
|
|
|
spin_unlock_irqrestore(&eeprom->buffer_lock, flags);
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t i2c_slave_eeprom_bin_write(struct file *filp, struct kobject *kobj,
|
|
|
|
struct bin_attribute *attr, char *buf, loff_t off, size_t count)
|
|
|
|
{
|
|
|
|
struct eeprom_data *eeprom;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
eeprom = dev_get_drvdata(container_of(kobj, struct device, kobj));
|
|
|
|
|
|
|
|
spin_lock_irqsave(&eeprom->buffer_lock, flags);
|
|
|
|
memcpy(&eeprom->buffer[off], buf, count);
|
|
|
|
spin_unlock_irqrestore(&eeprom->buffer_lock, flags);
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int i2c_slave_eeprom_probe(struct i2c_client *client, const struct i2c_device_id *id)
|
|
|
|
{
|
|
|
|
struct eeprom_data *eeprom;
|
|
|
|
int ret;
|
2019-09-04 16:29:53 +07:00
|
|
|
unsigned int size = FIELD_GET(I2C_SLAVE_BYTELEN, id->driver_data);
|
|
|
|
unsigned int flag_addr16 = FIELD_GET(I2C_SLAVE_FLAG_ADDR16, id->driver_data);
|
2014-11-18 23:04:54 +07:00
|
|
|
|
|
|
|
eeprom = devm_kzalloc(&client->dev, sizeof(struct eeprom_data) + size, GFP_KERNEL);
|
|
|
|
if (!eeprom)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2019-09-04 16:29:53 +07:00
|
|
|
eeprom->idx_write_cnt = 0;
|
|
|
|
eeprom->num_address_bytes = flag_addr16 ? 2 : 1;
|
|
|
|
eeprom->address_mask = size - 1;
|
2014-11-18 23:04:54 +07:00
|
|
|
spin_lock_init(&eeprom->buffer_lock);
|
|
|
|
i2c_set_clientdata(client, eeprom);
|
|
|
|
|
|
|
|
sysfs_bin_attr_init(&eeprom->bin);
|
|
|
|
eeprom->bin.attr.name = "slave-eeprom";
|
|
|
|
eeprom->bin.attr.mode = S_IRUSR | S_IWUSR;
|
|
|
|
eeprom->bin.read = i2c_slave_eeprom_bin_read;
|
|
|
|
eeprom->bin.write = i2c_slave_eeprom_bin_write;
|
|
|
|
eeprom->bin.size = size;
|
|
|
|
|
|
|
|
ret = sysfs_create_bin_file(&client->dev.kobj, &eeprom->bin);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = i2c_slave_register(client, i2c_slave_eeprom_slave_cb);
|
|
|
|
if (ret) {
|
|
|
|
sysfs_remove_bin_file(&client->dev.kobj, &eeprom->bin);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int i2c_slave_eeprom_remove(struct i2c_client *client)
|
|
|
|
{
|
|
|
|
struct eeprom_data *eeprom = i2c_get_clientdata(client);
|
|
|
|
|
|
|
|
i2c_slave_unregister(client);
|
|
|
|
sysfs_remove_bin_file(&client->dev.kobj, &eeprom->bin);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct i2c_device_id i2c_slave_eeprom_id[] = {
|
2019-09-04 16:29:53 +07:00
|
|
|
{ "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) },
|
2014-11-18 23:04:54 +07:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(i2c, i2c_slave_eeprom_id);
|
|
|
|
|
|
|
|
static struct i2c_driver i2c_slave_eeprom_driver = {
|
|
|
|
.driver = {
|
|
|
|
.name = "i2c-slave-eeprom",
|
|
|
|
},
|
|
|
|
.probe = i2c_slave_eeprom_probe,
|
|
|
|
.remove = i2c_slave_eeprom_remove,
|
|
|
|
.id_table = i2c_slave_eeprom_id,
|
|
|
|
};
|
|
|
|
module_i2c_driver(i2c_slave_eeprom_driver);
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Wolfram Sang <wsa@sang-engineering.com>");
|
|
|
|
MODULE_DESCRIPTION("I2C slave mode EEPROM simulator");
|
|
|
|
MODULE_LICENSE("GPL v2");
|