blob: cb415b10642f7dfed29c08c68fae9c854ae84797 [file] [log] [blame]
Thomas Gleixnerb886d83c2019-06-01 10:08:55 +02001// SPDX-License-Identifier: GPL-2.0-only
Wolfram Sang389be322014-11-18 17:04:54 +01002/*
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 *
Wolfram Sang389be322014-11-18 17:04:54 +01008 * Because most IP blocks can only detect one I2C slave address anyhow, this
9 * driver does not support simulating EEPROM types which take more than one
10 * address. It is prepared to simulate bigger EEPROMs with an internal 16 bit
11 * pointer, yet implementation is deferred until the need actually arises.
12 */
13
Björn Ardöfe050f92019-09-05 16:50:26 +020014/*
15 * FIXME: What to do if only 8 bits of a 16 bit address are sent?
16 * The ST-M24C64 sends only 0xff then. Needs verification with other
17 * EEPROMs, though. We currently use the 8 bit as a valid address.
18 */
19
Björn Ardö82d51482019-09-04 11:29:53 +020020#include <linux/bitfield.h>
Wolfram Sang389be322014-11-18 17:04:54 +010021#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;
Wolfram Sang389be322014-11-18 17:04:54 +010031 spinlock_t buffer_lock;
Björn Ardö82d51482019-09-04 11:29:53 +020032 u16 buffer_idx;
33 u16 address_mask;
34 u8 num_address_bytes;
35 u8 idx_write_cnt;
Björn Ardö11af27f2019-09-06 16:06:09 +020036 bool read_only;
Wolfram Sang389be322014-11-18 17:04:54 +010037 u8 buffer[];
38};
39
Björn Ardö82d51482019-09-04 11:29:53 +020040#define I2C_SLAVE_BYTELEN GENMASK(15, 0)
41#define I2C_SLAVE_FLAG_ADDR16 BIT(16)
Björn Ardö11af27f2019-09-06 16:06:09 +020042#define I2C_SLAVE_FLAG_RO BIT(17)
Björn Ardö82d51482019-09-04 11:29:53 +020043#define I2C_SLAVE_DEVICE_MAGIC(_len, _flags) ((_flags) | (_len))
44
Wolfram Sang389be322014-11-18 17:04:54 +010045static 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) {
Wolfram Sang5b77d162015-03-23 09:26:36 +010051 case I2C_SLAVE_WRITE_RECEIVED:
Björn Ardö82d51482019-09-04 11:29:53 +020052 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++;
Wolfram Sang389be322014-11-18 17:04:54 +010057 } else {
Björn Ardö11af27f2019-09-06 16:06:09 +020058 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 }
Wolfram Sang389be322014-11-18 17:04:54 +010063 }
64 break;
65
Wolfram Sang5b77d162015-03-23 09:26:36 +010066 case I2C_SLAVE_READ_PROCESSED:
Wolfram Sang98e982b3a2015-03-23 09:26:39 +010067 /* The previous byte made it to the bus, get next one */
Wolfram Sang5b77d162015-03-23 09:26:36 +010068 eeprom->buffer_idx++;
69 /* fallthrough */
70 case I2C_SLAVE_READ_REQUESTED:
Wolfram Sang389be322014-11-18 17:04:54 +010071 spin_lock(&eeprom->buffer_lock);
Björn Ardö82d51482019-09-04 11:29:53 +020072 *val = eeprom->buffer[eeprom->buffer_idx & eeprom->address_mask];
Wolfram Sang389be322014-11-18 17:04:54 +010073 spin_unlock(&eeprom->buffer_lock);
Wolfram Sang98e982b3a2015-03-23 09:26:39 +010074 /*
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 */
Wolfram Sang389be322014-11-18 17:04:54 +010079 break;
80
Wolfram Sang389be322014-11-18 17:04:54 +010081 case I2C_SLAVE_STOP:
Wolfram Sang5b77d162015-03-23 09:26:36 +010082 case I2C_SLAVE_WRITE_REQUESTED:
Björn Ardö82d51482019-09-04 11:29:53 +020083 eeprom->idx_write_cnt = 0;
Wolfram Sang389be322014-11-18 17:04:54 +010084 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
chenqiwu6b060d82020-02-14 20:56:37 +080099 eeprom = dev_get_drvdata(kobj_to_dev(kobj));
Wolfram Sang389be322014-11-18 17:04:54 +0100100
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
chenqiwu6b060d82020-02-14 20:56:37 +0800114 eeprom = dev_get_drvdata(kobj_to_dev(kobj));
Wolfram Sang389be322014-11-18 17:04:54 +0100115
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_eeprom_probe(struct i2c_client *client, const struct i2c_device_id *id)
124{
125 struct eeprom_data *eeprom;
126 int ret;
Björn Ardö82d51482019-09-04 11:29:53 +0200127 unsigned int size = FIELD_GET(I2C_SLAVE_BYTELEN, id->driver_data);
128 unsigned int flag_addr16 = FIELD_GET(I2C_SLAVE_FLAG_ADDR16, id->driver_data);
Wolfram Sang389be322014-11-18 17:04:54 +0100129
130 eeprom = devm_kzalloc(&client->dev, sizeof(struct eeprom_data) + size, GFP_KERNEL);
131 if (!eeprom)
132 return -ENOMEM;
133
Björn Ardö82d51482019-09-04 11:29:53 +0200134 eeprom->idx_write_cnt = 0;
135 eeprom->num_address_bytes = flag_addr16 ? 2 : 1;
136 eeprom->address_mask = size - 1;
Björn Ardö11af27f2019-09-06 16:06:09 +0200137 eeprom->read_only = FIELD_GET(I2C_SLAVE_FLAG_RO, id->driver_data);
Wolfram Sang389be322014-11-18 17:04:54 +0100138 spin_lock_init(&eeprom->buffer_lock);
139 i2c_set_clientdata(client, eeprom);
140
141 sysfs_bin_attr_init(&eeprom->bin);
142 eeprom->bin.attr.name = "slave-eeprom";
143 eeprom->bin.attr.mode = S_IRUSR | S_IWUSR;
144 eeprom->bin.read = i2c_slave_eeprom_bin_read;
145 eeprom->bin.write = i2c_slave_eeprom_bin_write;
146 eeprom->bin.size = size;
147
148 ret = sysfs_create_bin_file(&client->dev.kobj, &eeprom->bin);
149 if (ret)
150 return ret;
151
152 ret = i2c_slave_register(client, i2c_slave_eeprom_slave_cb);
153 if (ret) {
154 sysfs_remove_bin_file(&client->dev.kobj, &eeprom->bin);
155 return ret;
156 }
157
158 return 0;
159};
160
161static int i2c_slave_eeprom_remove(struct i2c_client *client)
162{
163 struct eeprom_data *eeprom = i2c_get_clientdata(client);
164
165 i2c_slave_unregister(client);
166 sysfs_remove_bin_file(&client->dev.kobj, &eeprom->bin);
167
168 return 0;
169}
170
171static const struct i2c_device_id i2c_slave_eeprom_id[] = {
Björn Ardö82d51482019-09-04 11:29:53 +0200172 { "slave-24c02", I2C_SLAVE_DEVICE_MAGIC(2048 / 8, 0) },
Björn Ardö11af27f2019-09-06 16:06:09 +0200173 { "slave-24c02ro", I2C_SLAVE_DEVICE_MAGIC(2048 / 8, I2C_SLAVE_FLAG_RO) },
Björn Ardö82d51482019-09-04 11:29:53 +0200174 { "slave-24c32", I2C_SLAVE_DEVICE_MAGIC(32768 / 8, I2C_SLAVE_FLAG_ADDR16) },
Björn Ardö11af27f2019-09-06 16:06:09 +0200175 { "slave-24c32ro", I2C_SLAVE_DEVICE_MAGIC(32768 / 8, I2C_SLAVE_FLAG_ADDR16 | I2C_SLAVE_FLAG_RO) },
Björn Ardö82d51482019-09-04 11:29:53 +0200176 { "slave-24c64", I2C_SLAVE_DEVICE_MAGIC(65536 / 8, I2C_SLAVE_FLAG_ADDR16) },
Björn Ardö11af27f2019-09-06 16:06:09 +0200177 { "slave-24c64ro", I2C_SLAVE_DEVICE_MAGIC(65536 / 8, I2C_SLAVE_FLAG_ADDR16 | I2C_SLAVE_FLAG_RO) },
Wolfram Sang389be322014-11-18 17:04:54 +0100178 { }
179};
180MODULE_DEVICE_TABLE(i2c, i2c_slave_eeprom_id);
181
182static struct i2c_driver i2c_slave_eeprom_driver = {
183 .driver = {
184 .name = "i2c-slave-eeprom",
Wolfram Sang389be322014-11-18 17:04:54 +0100185 },
186 .probe = i2c_slave_eeprom_probe,
187 .remove = i2c_slave_eeprom_remove,
188 .id_table = i2c_slave_eeprom_id,
189};
190module_i2c_driver(i2c_slave_eeprom_driver);
191
192MODULE_AUTHOR("Wolfram Sang <wsa@sang-engineering.com>");
193MODULE_DESCRIPTION("I2C slave mode EEPROM simulator");
194MODULE_LICENSE("GPL v2");