Linux Audio

Check our new training course

Loading...
v4.6
 
  1/*
  2 * I2C slave mode EEPROM simulator
  3 *
  4 * Copyright (C) 2014 by Wolfram Sang, Sang Engineering <wsa@sang-engineering.com>
  5 * Copyright (C) 2014 by Renesas Electronics Corporation
  6 *
  7 * This program is free software; you can redistribute it and/or modify it
  8 * under the terms of the GNU General Public License as published by the
  9 * Free Software Foundation; version 2 of the License.
 10 *
 11 * Because most IP blocks can only detect one I2C slave address anyhow, this
 12 * driver does not support simulating EEPROM types which take more than one
 13 * address. It is prepared to simulate bigger EEPROMs with an internal 16 bit
 14 * pointer, yet implementation is deferred until the need actually arises.
 
 15 */
 16
 
 
 17#include <linux/i2c.h>
 18#include <linux/init.h>
 19#include <linux/module.h>
 20#include <linux/of.h>
 21#include <linux/slab.h>
 22#include <linux/spinlock.h>
 23#include <linux/sysfs.h>
 24
 25struct eeprom_data {
 26	struct bin_attribute bin;
 27	bool first_write;
 28	spinlock_t buffer_lock;
 29	u8 buffer_idx;
 
 
 
 
 30	u8 buffer[];
 31};
 32
 
 
 
 
 
 33static int i2c_slave_eeprom_slave_cb(struct i2c_client *client,
 34				     enum i2c_slave_event event, u8 *val)
 35{
 36	struct eeprom_data *eeprom = i2c_get_clientdata(client);
 37
 38	switch (event) {
 39	case I2C_SLAVE_WRITE_RECEIVED:
 40		if (eeprom->first_write) {
 41			eeprom->buffer_idx = *val;
 42			eeprom->first_write = false;
 
 
 43		} else {
 44			spin_lock(&eeprom->buffer_lock);
 45			eeprom->buffer[eeprom->buffer_idx++] = *val;
 46			spin_unlock(&eeprom->buffer_lock);
 
 
 47		}
 48		break;
 49
 50	case I2C_SLAVE_READ_PROCESSED:
 51		/* The previous byte made it to the bus, get next one */
 52		eeprom->buffer_idx++;
 53		/* fallthrough */
 54	case I2C_SLAVE_READ_REQUESTED:
 55		spin_lock(&eeprom->buffer_lock);
 56		*val = eeprom->buffer[eeprom->buffer_idx];
 57		spin_unlock(&eeprom->buffer_lock);
 58		/*
 59		 * Do not increment buffer_idx here, because we don't know if
 60		 * this byte will be actually used. Read Linux I2C slave docs
 61		 * for details.
 62		 */
 63		break;
 64
 65	case I2C_SLAVE_STOP:
 66	case I2C_SLAVE_WRITE_REQUESTED:
 67		eeprom->first_write = true;
 68		break;
 69
 70	default:
 71		break;
 72	}
 73
 74	return 0;
 75}
 76
 77static ssize_t i2c_slave_eeprom_bin_read(struct file *filp, struct kobject *kobj,
 78		struct bin_attribute *attr, char *buf, loff_t off, size_t count)
 79{
 80	struct eeprom_data *eeprom;
 81	unsigned long flags;
 82
 83	eeprom = dev_get_drvdata(container_of(kobj, struct device, kobj));
 84
 85	spin_lock_irqsave(&eeprom->buffer_lock, flags);
 86	memcpy(buf, &eeprom->buffer[off], count);
 87	spin_unlock_irqrestore(&eeprom->buffer_lock, flags);
 88
 89	return count;
 90}
 91
 92static ssize_t i2c_slave_eeprom_bin_write(struct file *filp, struct kobject *kobj,
 93		struct bin_attribute *attr, char *buf, loff_t off, size_t count)
 94{
 95	struct eeprom_data *eeprom;
 96	unsigned long flags;
 97
 98	eeprom = dev_get_drvdata(container_of(kobj, struct device, kobj));
 99
100	spin_lock_irqsave(&eeprom->buffer_lock, flags);
101	memcpy(&eeprom->buffer[off], buf, count);
102	spin_unlock_irqrestore(&eeprom->buffer_lock, flags);
103
104	return count;
105}
106
107static int i2c_slave_eeprom_probe(struct i2c_client *client, const struct i2c_device_id *id)
 
108{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109	struct eeprom_data *eeprom;
110	int ret;
111	unsigned size = id->driver_data;
 
112
113	eeprom = devm_kzalloc(&client->dev, sizeof(struct eeprom_data) + size, GFP_KERNEL);
114	if (!eeprom)
115		return -ENOMEM;
116
117	eeprom->first_write = true;
 
 
118	spin_lock_init(&eeprom->buffer_lock);
119	i2c_set_clientdata(client, eeprom);
120
 
 
 
 
121	sysfs_bin_attr_init(&eeprom->bin);
122	eeprom->bin.attr.name = "slave-eeprom";
123	eeprom->bin.attr.mode = S_IRUSR | S_IWUSR;
124	eeprom->bin.read = i2c_slave_eeprom_bin_read;
125	eeprom->bin.write = i2c_slave_eeprom_bin_write;
126	eeprom->bin.size = size;
127
128	ret = sysfs_create_bin_file(&client->dev.kobj, &eeprom->bin);
129	if (ret)
130		return ret;
131
132	ret = i2c_slave_register(client, i2c_slave_eeprom_slave_cb);
133	if (ret) {
134		sysfs_remove_bin_file(&client->dev.kobj, &eeprom->bin);
135		return ret;
136	}
137
138	return 0;
139};
140
141static int i2c_slave_eeprom_remove(struct i2c_client *client)
142{
143	struct eeprom_data *eeprom = i2c_get_clientdata(client);
144
145	i2c_slave_unregister(client);
146	sysfs_remove_bin_file(&client->dev.kobj, &eeprom->bin);
147
148	return 0;
149}
150
151static const struct i2c_device_id i2c_slave_eeprom_id[] = {
152	{ "slave-24c02", 2048 / 8 },
 
 
 
 
 
 
 
153	{ }
154};
155MODULE_DEVICE_TABLE(i2c, i2c_slave_eeprom_id);
156
157static struct i2c_driver i2c_slave_eeprom_driver = {
158	.driver = {
159		.name = "i2c-slave-eeprom",
160	},
161	.probe = i2c_slave_eeprom_probe,
162	.remove = i2c_slave_eeprom_remove,
163	.id_table = i2c_slave_eeprom_id,
164};
165module_i2c_driver(i2c_slave_eeprom_driver);
166
167MODULE_AUTHOR("Wolfram Sang <wsa@sang-engineering.com>");
168MODULE_DESCRIPTION("I2C slave mode EEPROM simulator");
169MODULE_LICENSE("GPL v2");
v6.2
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * I2C slave mode EEPROM simulator
  4 *
  5 * Copyright (C) 2014 by Wolfram Sang, Sang Engineering <wsa@sang-engineering.com>
  6 * Copyright (C) 2014 by Renesas Electronics Corporation
  7 *
  8 * Because most slave IP cores can only detect one I2C slave address anyhow,
  9 * this driver does not support simulating EEPROM types which take more than
 10 * one address.
 11 */
 12
 13/*
 14 * FIXME: What to do if only 8 bits of a 16 bit address are sent?
 15 * The ST-M24C64 sends only 0xff then. Needs verification with other
 16 * EEPROMs, though. We currently use the 8 bit as a valid address.
 17 */
 18
 19#include <linux/bitfield.h>
 20#include <linux/firmware.h>
 21#include <linux/i2c.h>
 22#include <linux/init.h>
 23#include <linux/module.h>
 24#include <linux/of.h>
 25#include <linux/slab.h>
 26#include <linux/spinlock.h>
 27#include <linux/sysfs.h>
 28
 29struct eeprom_data {
 30	struct bin_attribute bin;
 
 31	spinlock_t buffer_lock;
 32	u16 buffer_idx;
 33	u16 address_mask;
 34	u8 num_address_bytes;
 35	u8 idx_write_cnt;
 36	bool read_only;
 37	u8 buffer[];
 38};
 39
 40#define I2C_SLAVE_BYTELEN GENMASK(15, 0)
 41#define I2C_SLAVE_FLAG_ADDR16 BIT(16)
 42#define I2C_SLAVE_FLAG_RO BIT(17)
 43#define I2C_SLAVE_DEVICE_MAGIC(_len, _flags) ((_flags) | ((_len) - 1))
 44
 45static int i2c_slave_eeprom_slave_cb(struct i2c_client *client,
 46				     enum i2c_slave_event event, u8 *val)
 47{
 48	struct eeprom_data *eeprom = i2c_get_clientdata(client);
 49
 50	switch (event) {
 51	case I2C_SLAVE_WRITE_RECEIVED:
 52		if (eeprom->idx_write_cnt < eeprom->num_address_bytes) {
 53			if (eeprom->idx_write_cnt == 0)
 54				eeprom->buffer_idx = 0;
 55			eeprom->buffer_idx = *val | (eeprom->buffer_idx << 8);
 56			eeprom->idx_write_cnt++;
 57		} else {
 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			}
 63		}
 64		break;
 65
 66	case I2C_SLAVE_READ_PROCESSED:
 67		/* The previous byte made it to the bus, get next one */
 68		eeprom->buffer_idx++;
 69		fallthrough;
 70	case I2C_SLAVE_READ_REQUESTED:
 71		spin_lock(&eeprom->buffer_lock);
 72		*val = eeprom->buffer[eeprom->buffer_idx & eeprom->address_mask];
 73		spin_unlock(&eeprom->buffer_lock);
 74		/*
 75		 * Do not increment buffer_idx here, because we don't know if
 76		 * this byte will be actually used. Read Linux I2C slave docs
 77		 * for details.
 78		 */
 79		break;
 80
 81	case I2C_SLAVE_STOP:
 82	case I2C_SLAVE_WRITE_REQUESTED:
 83		eeprom->idx_write_cnt = 0;
 84		break;
 85
 86	default:
 87		break;
 88	}
 89
 90	return 0;
 91}
 92
 93static ssize_t i2c_slave_eeprom_bin_read(struct file *filp, struct kobject *kobj,
 94		struct bin_attribute *attr, char *buf, loff_t off, size_t count)
 95{
 96	struct eeprom_data *eeprom;
 97	unsigned long flags;
 98
 99	eeprom = dev_get_drvdata(kobj_to_dev(kobj));
100
101	spin_lock_irqsave(&eeprom->buffer_lock, flags);
102	memcpy(buf, &eeprom->buffer[off], count);
103	spin_unlock_irqrestore(&eeprom->buffer_lock, flags);
104
105	return count;
106}
107
108static ssize_t i2c_slave_eeprom_bin_write(struct file *filp, struct kobject *kobj,
109		struct bin_attribute *attr, char *buf, loff_t off, size_t count)
110{
111	struct eeprom_data *eeprom;
112	unsigned long flags;
113
114	eeprom = dev_get_drvdata(kobj_to_dev(kobj));
115
116	spin_lock_irqsave(&eeprom->buffer_lock, flags);
117	memcpy(&eeprom->buffer[off], buf, count);
118	spin_unlock_irqrestore(&eeprom->buffer_lock, flags);
119
120	return count;
121}
122
123static int i2c_slave_init_eeprom_data(struct eeprom_data *eeprom, struct i2c_client *client,
124				      unsigned int size)
125{
126	const struct firmware *fw;
127	const char *eeprom_data;
128	int ret = device_property_read_string(&client->dev, "firmware-name", &eeprom_data);
129
130	if (!ret) {
131		ret = request_firmware_into_buf(&fw, eeprom_data, &client->dev,
132						eeprom->buffer, size);
133		if (ret)
134			return ret;
135		release_firmware(fw);
136	} else {
137		/* An empty eeprom typically has all bits set to 1 */
138		memset(eeprom->buffer, 0xff, size);
139	}
140	return 0;
141}
142
143static int i2c_slave_eeprom_probe(struct i2c_client *client)
144{
145	const struct i2c_device_id *id = i2c_client_get_device_id(client);
146	struct eeprom_data *eeprom;
147	int ret;
148	unsigned int size = FIELD_GET(I2C_SLAVE_BYTELEN, id->driver_data) + 1;
149	unsigned int flag_addr16 = FIELD_GET(I2C_SLAVE_FLAG_ADDR16, id->driver_data);
150
151	eeprom = devm_kzalloc(&client->dev, sizeof(struct eeprom_data) + size, GFP_KERNEL);
152	if (!eeprom)
153		return -ENOMEM;
154
155	eeprom->num_address_bytes = flag_addr16 ? 2 : 1;
156	eeprom->address_mask = size - 1;
157	eeprom->read_only = FIELD_GET(I2C_SLAVE_FLAG_RO, id->driver_data);
158	spin_lock_init(&eeprom->buffer_lock);
159	i2c_set_clientdata(client, eeprom);
160
161	ret = i2c_slave_init_eeprom_data(eeprom, client, size);
162	if (ret)
163		return ret;
164
165	sysfs_bin_attr_init(&eeprom->bin);
166	eeprom->bin.attr.name = "slave-eeprom";
167	eeprom->bin.attr.mode = S_IRUSR | S_IWUSR;
168	eeprom->bin.read = i2c_slave_eeprom_bin_read;
169	eeprom->bin.write = i2c_slave_eeprom_bin_write;
170	eeprom->bin.size = size;
171
172	ret = sysfs_create_bin_file(&client->dev.kobj, &eeprom->bin);
173	if (ret)
174		return ret;
175
176	ret = i2c_slave_register(client, i2c_slave_eeprom_slave_cb);
177	if (ret) {
178		sysfs_remove_bin_file(&client->dev.kobj, &eeprom->bin);
179		return ret;
180	}
181
182	return 0;
183};
184
185static void i2c_slave_eeprom_remove(struct i2c_client *client)
186{
187	struct eeprom_data *eeprom = i2c_get_clientdata(client);
188
189	i2c_slave_unregister(client);
190	sysfs_remove_bin_file(&client->dev.kobj, &eeprom->bin);
 
 
191}
192
193static const struct i2c_device_id i2c_slave_eeprom_id[] = {
194	{ "slave-24c02", I2C_SLAVE_DEVICE_MAGIC(2048 / 8,  0) },
195	{ "slave-24c02ro", I2C_SLAVE_DEVICE_MAGIC(2048 / 8,  I2C_SLAVE_FLAG_RO) },
196	{ "slave-24c32", I2C_SLAVE_DEVICE_MAGIC(32768 / 8, I2C_SLAVE_FLAG_ADDR16) },
197	{ "slave-24c32ro", I2C_SLAVE_DEVICE_MAGIC(32768 / 8, I2C_SLAVE_FLAG_ADDR16 | I2C_SLAVE_FLAG_RO) },
198	{ "slave-24c64", I2C_SLAVE_DEVICE_MAGIC(65536 / 8, I2C_SLAVE_FLAG_ADDR16) },
199	{ "slave-24c64ro", I2C_SLAVE_DEVICE_MAGIC(65536 / 8, I2C_SLAVE_FLAG_ADDR16 | I2C_SLAVE_FLAG_RO) },
200	{ "slave-24c512", I2C_SLAVE_DEVICE_MAGIC(524288 / 8, I2C_SLAVE_FLAG_ADDR16) },
201	{ "slave-24c512ro", I2C_SLAVE_DEVICE_MAGIC(524288 / 8, I2C_SLAVE_FLAG_ADDR16 | I2C_SLAVE_FLAG_RO) },
202	{ }
203};
204MODULE_DEVICE_TABLE(i2c, i2c_slave_eeprom_id);
205
206static struct i2c_driver i2c_slave_eeprom_driver = {
207	.driver = {
208		.name = "i2c-slave-eeprom",
209	},
210	.probe_new = i2c_slave_eeprom_probe,
211	.remove = i2c_slave_eeprom_remove,
212	.id_table = i2c_slave_eeprom_id,
213};
214module_i2c_driver(i2c_slave_eeprom_driver);
215
216MODULE_AUTHOR("Wolfram Sang <wsa@sang-engineering.com>");
217MODULE_DESCRIPTION("I2C slave mode EEPROM simulator");
218MODULE_LICENSE("GPL v2");