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