]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/i2c/i2c-slave-eeprom.c
i2c-eeprom_slave: Add support for more eeprom models
[linux.git] / drivers / i2c / i2c-slave-eeprom.c
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 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
14 #include <linux/bitfield.h>
15 #include <linux/i2c.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/of.h>
19 #include <linux/slab.h>
20 #include <linux/spinlock.h>
21 #include <linux/sysfs.h>
22
23 struct eeprom_data {
24         struct bin_attribute bin;
25         spinlock_t buffer_lock;
26         u16 buffer_idx;
27         u16 address_mask;
28         u8 num_address_bytes;
29         u8 idx_write_cnt;
30         u8 buffer[];
31 };
32
33 #define I2C_SLAVE_BYTELEN GENMASK(15, 0)
34 #define I2C_SLAVE_FLAG_ADDR16 BIT(16)
35 #define I2C_SLAVE_DEVICE_MAGIC(_len, _flags) ((_flags) | (_len))
36
37 static int i2c_slave_eeprom_slave_cb(struct i2c_client *client,
38                                      enum i2c_slave_event event, u8 *val)
39 {
40         struct eeprom_data *eeprom = i2c_get_clientdata(client);
41
42         switch (event) {
43         case I2C_SLAVE_WRITE_RECEIVED:
44                 if (eeprom->idx_write_cnt < eeprom->num_address_bytes) {
45                         if (eeprom->idx_write_cnt == 0)
46                                 eeprom->buffer_idx = 0;
47                         eeprom->buffer_idx = *val | (eeprom->buffer_idx << 8);
48                         eeprom->idx_write_cnt++;
49                 } else {
50                         spin_lock(&eeprom->buffer_lock);
51                         eeprom->buffer[eeprom->buffer_idx++ & eeprom->address_mask] = *val;
52                         spin_unlock(&eeprom->buffer_lock);
53                 }
54                 break;
55
56         case I2C_SLAVE_READ_PROCESSED:
57                 /* The previous byte made it to the bus, get next one */
58                 eeprom->buffer_idx++;
59                 /* fallthrough */
60         case I2C_SLAVE_READ_REQUESTED:
61                 spin_lock(&eeprom->buffer_lock);
62                 *val = eeprom->buffer[eeprom->buffer_idx & eeprom->address_mask];
63                 spin_unlock(&eeprom->buffer_lock);
64                 /*
65                  * Do not increment buffer_idx here, because we don't know if
66                  * this byte will be actually used. Read Linux I2C slave docs
67                  * for details.
68                  */
69                 break;
70
71         case I2C_SLAVE_STOP:
72         case I2C_SLAVE_WRITE_REQUESTED:
73                 eeprom->idx_write_cnt = 0;
74                 break;
75
76         default:
77                 break;
78         }
79
80         return 0;
81 }
82
83 static ssize_t i2c_slave_eeprom_bin_read(struct file *filp, struct kobject *kobj,
84                 struct bin_attribute *attr, char *buf, loff_t off, size_t count)
85 {
86         struct eeprom_data *eeprom;
87         unsigned long flags;
88
89         eeprom = dev_get_drvdata(container_of(kobj, struct device, kobj));
90
91         spin_lock_irqsave(&eeprom->buffer_lock, flags);
92         memcpy(buf, &eeprom->buffer[off], count);
93         spin_unlock_irqrestore(&eeprom->buffer_lock, flags);
94
95         return count;
96 }
97
98 static ssize_t i2c_slave_eeprom_bin_write(struct file *filp, struct kobject *kobj,
99                 struct bin_attribute *attr, char *buf, loff_t off, size_t count)
100 {
101         struct eeprom_data *eeprom;
102         unsigned long flags;
103
104         eeprom = dev_get_drvdata(container_of(kobj, struct device, kobj));
105
106         spin_lock_irqsave(&eeprom->buffer_lock, flags);
107         memcpy(&eeprom->buffer[off], buf, count);
108         spin_unlock_irqrestore(&eeprom->buffer_lock, flags);
109
110         return count;
111 }
112
113 static int i2c_slave_eeprom_probe(struct i2c_client *client, const struct i2c_device_id *id)
114 {
115         struct eeprom_data *eeprom;
116         int ret;
117         unsigned int size = FIELD_GET(I2C_SLAVE_BYTELEN, id->driver_data);
118         unsigned int flag_addr16 = FIELD_GET(I2C_SLAVE_FLAG_ADDR16, id->driver_data);
119
120         eeprom = devm_kzalloc(&client->dev, sizeof(struct eeprom_data) + size, GFP_KERNEL);
121         if (!eeprom)
122                 return -ENOMEM;
123
124         eeprom->idx_write_cnt = 0;
125         eeprom->num_address_bytes = flag_addr16 ? 2 : 1;
126         eeprom->address_mask = size - 1;
127         spin_lock_init(&eeprom->buffer_lock);
128         i2c_set_clientdata(client, eeprom);
129
130         sysfs_bin_attr_init(&eeprom->bin);
131         eeprom->bin.attr.name = "slave-eeprom";
132         eeprom->bin.attr.mode = S_IRUSR | S_IWUSR;
133         eeprom->bin.read = i2c_slave_eeprom_bin_read;
134         eeprom->bin.write = i2c_slave_eeprom_bin_write;
135         eeprom->bin.size = size;
136
137         ret = sysfs_create_bin_file(&client->dev.kobj, &eeprom->bin);
138         if (ret)
139                 return ret;
140
141         ret = i2c_slave_register(client, i2c_slave_eeprom_slave_cb);
142         if (ret) {
143                 sysfs_remove_bin_file(&client->dev.kobj, &eeprom->bin);
144                 return ret;
145         }
146
147         return 0;
148 };
149
150 static int i2c_slave_eeprom_remove(struct i2c_client *client)
151 {
152         struct eeprom_data *eeprom = i2c_get_clientdata(client);
153
154         i2c_slave_unregister(client);
155         sysfs_remove_bin_file(&client->dev.kobj, &eeprom->bin);
156
157         return 0;
158 }
159
160 static const struct i2c_device_id i2c_slave_eeprom_id[] = {
161         { "slave-24c02", I2C_SLAVE_DEVICE_MAGIC(2048 / 8,  0) },
162         { "slave-24c32", I2C_SLAVE_DEVICE_MAGIC(32768 / 8, I2C_SLAVE_FLAG_ADDR16) },
163         { "slave-24c64", I2C_SLAVE_DEVICE_MAGIC(65536 / 8, I2C_SLAVE_FLAG_ADDR16) },
164         { }
165 };
166 MODULE_DEVICE_TABLE(i2c, i2c_slave_eeprom_id);
167
168 static struct i2c_driver i2c_slave_eeprom_driver = {
169         .driver = {
170                 .name = "i2c-slave-eeprom",
171         },
172         .probe = i2c_slave_eeprom_probe,
173         .remove = i2c_slave_eeprom_remove,
174         .id_table = i2c_slave_eeprom_id,
175 };
176 module_i2c_driver(i2c_slave_eeprom_driver);
177
178 MODULE_AUTHOR("Wolfram Sang <wsa@sang-engineering.com>");
179 MODULE_DESCRIPTION("I2C slave mode EEPROM simulator");
180 MODULE_LICENSE("GPL v2");