18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * I2C slave mode EEPROM simulator
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2014 by Wolfram Sang, Sang Engineering <wsa@sang-engineering.com>
68c2ecf20Sopenharmony_ci * Copyright (C) 2014 by Renesas Electronics Corporation
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Because most slave IP cores can only detect one I2C slave address anyhow,
98c2ecf20Sopenharmony_ci * this driver does not support simulating EEPROM types which take more than
108c2ecf20Sopenharmony_ci * one address.
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci/*
148c2ecf20Sopenharmony_ci * FIXME: What to do if only 8 bits of a 16 bit address are sent?
158c2ecf20Sopenharmony_ci * The ST-M24C64 sends only 0xff then. Needs verification with other
168c2ecf20Sopenharmony_ci * EEPROMs, though. We currently use the 8 bit as a valid address.
178c2ecf20Sopenharmony_ci */
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#include <linux/bitfield.h>
208c2ecf20Sopenharmony_ci#include <linux/firmware.h>
218c2ecf20Sopenharmony_ci#include <linux/i2c.h>
228c2ecf20Sopenharmony_ci#include <linux/init.h>
238c2ecf20Sopenharmony_ci#include <linux/module.h>
248c2ecf20Sopenharmony_ci#include <linux/of.h>
258c2ecf20Sopenharmony_ci#include <linux/slab.h>
268c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
278c2ecf20Sopenharmony_ci#include <linux/sysfs.h>
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_cistruct eeprom_data {
308c2ecf20Sopenharmony_ci	struct bin_attribute bin;
318c2ecf20Sopenharmony_ci	spinlock_t buffer_lock;
328c2ecf20Sopenharmony_ci	u16 buffer_idx;
338c2ecf20Sopenharmony_ci	u16 address_mask;
348c2ecf20Sopenharmony_ci	u8 num_address_bytes;
358c2ecf20Sopenharmony_ci	u8 idx_write_cnt;
368c2ecf20Sopenharmony_ci	bool read_only;
378c2ecf20Sopenharmony_ci	u8 buffer[];
388c2ecf20Sopenharmony_ci};
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci#define I2C_SLAVE_BYTELEN GENMASK(15, 0)
418c2ecf20Sopenharmony_ci#define I2C_SLAVE_FLAG_ADDR16 BIT(16)
428c2ecf20Sopenharmony_ci#define I2C_SLAVE_FLAG_RO BIT(17)
438c2ecf20Sopenharmony_ci#define I2C_SLAVE_DEVICE_MAGIC(_len, _flags) ((_flags) | ((_len) - 1))
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistatic int i2c_slave_eeprom_slave_cb(struct i2c_client *client,
468c2ecf20Sopenharmony_ci				     enum i2c_slave_event event, u8 *val)
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	struct eeprom_data *eeprom = i2c_get_clientdata(client);
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	switch (event) {
518c2ecf20Sopenharmony_ci	case I2C_SLAVE_WRITE_RECEIVED:
528c2ecf20Sopenharmony_ci		if (eeprom->idx_write_cnt < eeprom->num_address_bytes) {
538c2ecf20Sopenharmony_ci			if (eeprom->idx_write_cnt == 0)
548c2ecf20Sopenharmony_ci				eeprom->buffer_idx = 0;
558c2ecf20Sopenharmony_ci			eeprom->buffer_idx = *val | (eeprom->buffer_idx << 8);
568c2ecf20Sopenharmony_ci			eeprom->idx_write_cnt++;
578c2ecf20Sopenharmony_ci		} else {
588c2ecf20Sopenharmony_ci			if (!eeprom->read_only) {
598c2ecf20Sopenharmony_ci				spin_lock(&eeprom->buffer_lock);
608c2ecf20Sopenharmony_ci				eeprom->buffer[eeprom->buffer_idx++ & eeprom->address_mask] = *val;
618c2ecf20Sopenharmony_ci				spin_unlock(&eeprom->buffer_lock);
628c2ecf20Sopenharmony_ci			}
638c2ecf20Sopenharmony_ci		}
648c2ecf20Sopenharmony_ci		break;
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	case I2C_SLAVE_READ_PROCESSED:
678c2ecf20Sopenharmony_ci		/* The previous byte made it to the bus, get next one */
688c2ecf20Sopenharmony_ci		eeprom->buffer_idx++;
698c2ecf20Sopenharmony_ci		fallthrough;
708c2ecf20Sopenharmony_ci	case I2C_SLAVE_READ_REQUESTED:
718c2ecf20Sopenharmony_ci		spin_lock(&eeprom->buffer_lock);
728c2ecf20Sopenharmony_ci		*val = eeprom->buffer[eeprom->buffer_idx & eeprom->address_mask];
738c2ecf20Sopenharmony_ci		spin_unlock(&eeprom->buffer_lock);
748c2ecf20Sopenharmony_ci		/*
758c2ecf20Sopenharmony_ci		 * Do not increment buffer_idx here, because we don't know if
768c2ecf20Sopenharmony_ci		 * this byte will be actually used. Read Linux I2C slave docs
778c2ecf20Sopenharmony_ci		 * for details.
788c2ecf20Sopenharmony_ci		 */
798c2ecf20Sopenharmony_ci		break;
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	case I2C_SLAVE_STOP:
828c2ecf20Sopenharmony_ci	case I2C_SLAVE_WRITE_REQUESTED:
838c2ecf20Sopenharmony_ci		eeprom->idx_write_cnt = 0;
848c2ecf20Sopenharmony_ci		break;
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	default:
878c2ecf20Sopenharmony_ci		break;
888c2ecf20Sopenharmony_ci	}
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	return 0;
918c2ecf20Sopenharmony_ci}
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cistatic ssize_t i2c_slave_eeprom_bin_read(struct file *filp, struct kobject *kobj,
948c2ecf20Sopenharmony_ci		struct bin_attribute *attr, char *buf, loff_t off, size_t count)
958c2ecf20Sopenharmony_ci{
968c2ecf20Sopenharmony_ci	struct eeprom_data *eeprom;
978c2ecf20Sopenharmony_ci	unsigned long flags;
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	eeprom = dev_get_drvdata(kobj_to_dev(kobj));
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	spin_lock_irqsave(&eeprom->buffer_lock, flags);
1028c2ecf20Sopenharmony_ci	memcpy(buf, &eeprom->buffer[off], count);
1038c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&eeprom->buffer_lock, flags);
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	return count;
1068c2ecf20Sopenharmony_ci}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistatic ssize_t i2c_slave_eeprom_bin_write(struct file *filp, struct kobject *kobj,
1098c2ecf20Sopenharmony_ci		struct bin_attribute *attr, char *buf, loff_t off, size_t count)
1108c2ecf20Sopenharmony_ci{
1118c2ecf20Sopenharmony_ci	struct eeprom_data *eeprom;
1128c2ecf20Sopenharmony_ci	unsigned long flags;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	eeprom = dev_get_drvdata(kobj_to_dev(kobj));
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	spin_lock_irqsave(&eeprom->buffer_lock, flags);
1178c2ecf20Sopenharmony_ci	memcpy(&eeprom->buffer[off], buf, count);
1188c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&eeprom->buffer_lock, flags);
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	return count;
1218c2ecf20Sopenharmony_ci}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cistatic int i2c_slave_init_eeprom_data(struct eeprom_data *eeprom, struct i2c_client *client,
1248c2ecf20Sopenharmony_ci				      unsigned int size)
1258c2ecf20Sopenharmony_ci{
1268c2ecf20Sopenharmony_ci	const struct firmware *fw;
1278c2ecf20Sopenharmony_ci	const char *eeprom_data;
1288c2ecf20Sopenharmony_ci	int ret = device_property_read_string(&client->dev, "firmware-name", &eeprom_data);
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	if (!ret) {
1318c2ecf20Sopenharmony_ci		ret = request_firmware_into_buf(&fw, eeprom_data, &client->dev,
1328c2ecf20Sopenharmony_ci						eeprom->buffer, size);
1338c2ecf20Sopenharmony_ci		if (ret)
1348c2ecf20Sopenharmony_ci			return ret;
1358c2ecf20Sopenharmony_ci		release_firmware(fw);
1368c2ecf20Sopenharmony_ci	} else {
1378c2ecf20Sopenharmony_ci		/* An empty eeprom typically has all bits set to 1 */
1388c2ecf20Sopenharmony_ci		memset(eeprom->buffer, 0xff, size);
1398c2ecf20Sopenharmony_ci	}
1408c2ecf20Sopenharmony_ci	return 0;
1418c2ecf20Sopenharmony_ci}
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_cistatic int i2c_slave_eeprom_probe(struct i2c_client *client, const struct i2c_device_id *id)
1448c2ecf20Sopenharmony_ci{
1458c2ecf20Sopenharmony_ci	struct eeprom_data *eeprom;
1468c2ecf20Sopenharmony_ci	int ret;
1478c2ecf20Sopenharmony_ci	unsigned int size = FIELD_GET(I2C_SLAVE_BYTELEN, id->driver_data) + 1;
1488c2ecf20Sopenharmony_ci	unsigned int flag_addr16 = FIELD_GET(I2C_SLAVE_FLAG_ADDR16, id->driver_data);
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	eeprom = devm_kzalloc(&client->dev, sizeof(struct eeprom_data) + size, GFP_KERNEL);
1518c2ecf20Sopenharmony_ci	if (!eeprom)
1528c2ecf20Sopenharmony_ci		return -ENOMEM;
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	eeprom->num_address_bytes = flag_addr16 ? 2 : 1;
1558c2ecf20Sopenharmony_ci	eeprom->address_mask = size - 1;
1568c2ecf20Sopenharmony_ci	eeprom->read_only = FIELD_GET(I2C_SLAVE_FLAG_RO, id->driver_data);
1578c2ecf20Sopenharmony_ci	spin_lock_init(&eeprom->buffer_lock);
1588c2ecf20Sopenharmony_ci	i2c_set_clientdata(client, eeprom);
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	ret = i2c_slave_init_eeprom_data(eeprom, client, size);
1618c2ecf20Sopenharmony_ci	if (ret)
1628c2ecf20Sopenharmony_ci		return ret;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	sysfs_bin_attr_init(&eeprom->bin);
1658c2ecf20Sopenharmony_ci	eeprom->bin.attr.name = "slave-eeprom";
1668c2ecf20Sopenharmony_ci	eeprom->bin.attr.mode = S_IRUSR | S_IWUSR;
1678c2ecf20Sopenharmony_ci	eeprom->bin.read = i2c_slave_eeprom_bin_read;
1688c2ecf20Sopenharmony_ci	eeprom->bin.write = i2c_slave_eeprom_bin_write;
1698c2ecf20Sopenharmony_ci	eeprom->bin.size = size;
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	ret = sysfs_create_bin_file(&client->dev.kobj, &eeprom->bin);
1728c2ecf20Sopenharmony_ci	if (ret)
1738c2ecf20Sopenharmony_ci		return ret;
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	ret = i2c_slave_register(client, i2c_slave_eeprom_slave_cb);
1768c2ecf20Sopenharmony_ci	if (ret) {
1778c2ecf20Sopenharmony_ci		sysfs_remove_bin_file(&client->dev.kobj, &eeprom->bin);
1788c2ecf20Sopenharmony_ci		return ret;
1798c2ecf20Sopenharmony_ci	}
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	return 0;
1828c2ecf20Sopenharmony_ci};
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_cistatic int i2c_slave_eeprom_remove(struct i2c_client *client)
1858c2ecf20Sopenharmony_ci{
1868c2ecf20Sopenharmony_ci	struct eeprom_data *eeprom = i2c_get_clientdata(client);
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	i2c_slave_unregister(client);
1898c2ecf20Sopenharmony_ci	sysfs_remove_bin_file(&client->dev.kobj, &eeprom->bin);
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	return 0;
1928c2ecf20Sopenharmony_ci}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_cistatic const struct i2c_device_id i2c_slave_eeprom_id[] = {
1958c2ecf20Sopenharmony_ci	{ "slave-24c02", I2C_SLAVE_DEVICE_MAGIC(2048 / 8,  0) },
1968c2ecf20Sopenharmony_ci	{ "slave-24c02ro", I2C_SLAVE_DEVICE_MAGIC(2048 / 8,  I2C_SLAVE_FLAG_RO) },
1978c2ecf20Sopenharmony_ci	{ "slave-24c32", I2C_SLAVE_DEVICE_MAGIC(32768 / 8, I2C_SLAVE_FLAG_ADDR16) },
1988c2ecf20Sopenharmony_ci	{ "slave-24c32ro", I2C_SLAVE_DEVICE_MAGIC(32768 / 8, I2C_SLAVE_FLAG_ADDR16 | I2C_SLAVE_FLAG_RO) },
1998c2ecf20Sopenharmony_ci	{ "slave-24c64", I2C_SLAVE_DEVICE_MAGIC(65536 / 8, I2C_SLAVE_FLAG_ADDR16) },
2008c2ecf20Sopenharmony_ci	{ "slave-24c64ro", I2C_SLAVE_DEVICE_MAGIC(65536 / 8, I2C_SLAVE_FLAG_ADDR16 | I2C_SLAVE_FLAG_RO) },
2018c2ecf20Sopenharmony_ci	{ "slave-24c512", I2C_SLAVE_DEVICE_MAGIC(524288 / 8, I2C_SLAVE_FLAG_ADDR16) },
2028c2ecf20Sopenharmony_ci	{ "slave-24c512ro", I2C_SLAVE_DEVICE_MAGIC(524288 / 8, I2C_SLAVE_FLAG_ADDR16 | I2C_SLAVE_FLAG_RO) },
2038c2ecf20Sopenharmony_ci	{ }
2048c2ecf20Sopenharmony_ci};
2058c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, i2c_slave_eeprom_id);
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_cistatic struct i2c_driver i2c_slave_eeprom_driver = {
2088c2ecf20Sopenharmony_ci	.driver = {
2098c2ecf20Sopenharmony_ci		.name = "i2c-slave-eeprom",
2108c2ecf20Sopenharmony_ci	},
2118c2ecf20Sopenharmony_ci	.probe = i2c_slave_eeprom_probe,
2128c2ecf20Sopenharmony_ci	.remove = i2c_slave_eeprom_remove,
2138c2ecf20Sopenharmony_ci	.id_table = i2c_slave_eeprom_id,
2148c2ecf20Sopenharmony_ci};
2158c2ecf20Sopenharmony_cimodule_i2c_driver(i2c_slave_eeprom_driver);
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ciMODULE_AUTHOR("Wolfram Sang <wsa@sang-engineering.com>");
2188c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("I2C slave mode EEPROM simulator");
2198c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
220