GNU Linux-libre 5.10.215-gnu1
[releases.git] / drivers / misc / eeprom / ee1004.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * ee1004 - driver for DDR4 SPD EEPROMs
4  *
5  * Copyright (C) 2017-2019 Jean Delvare
6  *
7  * Based on the at24 driver:
8  * Copyright (C) 2005-2007 David Brownell
9  * Copyright (C) 2008 Wolfram Sang, Pengutronix
10  */
11
12 #include <linux/i2c.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/mod_devicetable.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18
19 /*
20  * DDR4 memory modules use special EEPROMs following the Jedec EE1004
21  * specification. These are 512-byte EEPROMs using a single I2C address
22  * in the 0x50-0x57 range for data. One of two 256-byte page is selected
23  * by writing a command to I2C address 0x36 or 0x37 on the same I2C bus.
24  *
25  * Therefore we need to request these 2 additional addresses, and serialize
26  * access to all such EEPROMs with a single mutex.
27  *
28  * We assume it is safe to read up to 32 bytes at once from these EEPROMs.
29  * We use SMBus access even if I2C is available, these EEPROMs are small
30  * enough, and reading from them infrequent enough, that we favor simplicity
31  * over performance.
32  */
33
34 #define EE1004_ADDR_SET_PAGE            0x36
35 #define EE1004_EEPROM_SIZE              512
36 #define EE1004_PAGE_SIZE                256
37 #define EE1004_PAGE_SHIFT               8
38
39 /*
40  * Mutex protects ee1004_set_page and ee1004_dev_count, and must be held
41  * from page selection to end of read.
42  */
43 static DEFINE_MUTEX(ee1004_bus_lock);
44 static struct i2c_client *ee1004_set_page[2];
45 static unsigned int ee1004_dev_count;
46 static int ee1004_current_page;
47
48 static const struct i2c_device_id ee1004_ids[] = {
49         { "ee1004", 0 },
50         { }
51 };
52 MODULE_DEVICE_TABLE(i2c, ee1004_ids);
53
54 /*-------------------------------------------------------------------------*/
55
56 static int ee1004_get_current_page(void)
57 {
58         int err;
59
60         err = i2c_smbus_read_byte(ee1004_set_page[0]);
61         if (err == -ENXIO) {
62                 /* Nack means page 1 is selected */
63                 return 1;
64         }
65         if (err < 0) {
66                 /* Anything else is a real error, bail out */
67                 return err;
68         }
69
70         /* Ack means page 0 is selected, returned value meaningless */
71         return 0;
72 }
73
74 static ssize_t ee1004_eeprom_read(struct i2c_client *client, char *buf,
75                                   unsigned int offset, size_t count)
76 {
77         int status;
78
79         if (count > I2C_SMBUS_BLOCK_MAX)
80                 count = I2C_SMBUS_BLOCK_MAX;
81         /* Can't cross page boundaries */
82         if (unlikely(offset + count > EE1004_PAGE_SIZE))
83                 count = EE1004_PAGE_SIZE - offset;
84
85         if (count > I2C_SMBUS_BLOCK_MAX)
86                 count = I2C_SMBUS_BLOCK_MAX;
87
88         status = i2c_smbus_read_i2c_block_data_or_emulated(client, offset,
89                                                            count, buf);
90         dev_dbg(&client->dev, "read %zu@%d --> %d\n", count, offset, status);
91
92         return status;
93 }
94
95 static ssize_t ee1004_read(struct file *filp, struct kobject *kobj,
96                            struct bin_attribute *bin_attr,
97                            char *buf, loff_t off, size_t count)
98 {
99         struct device *dev = kobj_to_dev(kobj);
100         struct i2c_client *client = to_i2c_client(dev);
101         size_t requested = count;
102         int page;
103
104         if (unlikely(!count))
105                 return count;
106
107         page = off >> EE1004_PAGE_SHIFT;
108         if (unlikely(page > 1))
109                 return 0;
110         off &= (1 << EE1004_PAGE_SHIFT) - 1;
111
112         /*
113          * Read data from chip, protecting against concurrent access to
114          * other EE1004 SPD EEPROMs on the same adapter.
115          */
116         mutex_lock(&ee1004_bus_lock);
117
118         while (count) {
119                 int status;
120
121                 /* Select page */
122                 if (page != ee1004_current_page) {
123                         /* Data is ignored */
124                         status = i2c_smbus_write_byte(ee1004_set_page[page],
125                                                       0x00);
126                         if (status == -ENXIO) {
127                                 /*
128                                  * Don't give up just yet. Some memory
129                                  * modules will select the page but not
130                                  * ack the command. Check which page is
131                                  * selected now.
132                                  */
133                                 if (ee1004_get_current_page() == page)
134                                         status = 0;
135                         }
136                         if (status < 0) {
137                                 dev_err(dev, "Failed to select page %d (%d)\n",
138                                         page, status);
139                                 mutex_unlock(&ee1004_bus_lock);
140                                 return status;
141                         }
142                         dev_dbg(dev, "Selected page %d\n", page);
143                         ee1004_current_page = page;
144                 }
145
146                 status = ee1004_eeprom_read(client, buf, off, count);
147                 if (status < 0) {
148                         mutex_unlock(&ee1004_bus_lock);
149                         return status;
150                 }
151                 buf += status;
152                 off += status;
153                 count -= status;
154
155                 if (off == EE1004_PAGE_SIZE) {
156                         page++;
157                         off = 0;
158                 }
159         }
160
161         mutex_unlock(&ee1004_bus_lock);
162
163         return requested;
164 }
165
166 static const struct bin_attribute eeprom_attr = {
167         .attr = {
168                 .name = "eeprom",
169                 .mode = 0444,
170         },
171         .size = EE1004_EEPROM_SIZE,
172         .read = ee1004_read,
173 };
174
175 static int ee1004_probe(struct i2c_client *client,
176                         const struct i2c_device_id *id)
177 {
178         int err, cnr = 0;
179         const char *slow = NULL;
180
181         /* Make sure we can operate on this adapter */
182         if (!i2c_check_functionality(client->adapter,
183                                      I2C_FUNC_SMBUS_READ_BYTE |
184                                      I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
185                 if (i2c_check_functionality(client->adapter,
186                                      I2C_FUNC_SMBUS_READ_BYTE |
187                                      I2C_FUNC_SMBUS_READ_WORD_DATA))
188                         slow = "word";
189                 else if (i2c_check_functionality(client->adapter,
190                                      I2C_FUNC_SMBUS_READ_BYTE |
191                                      I2C_FUNC_SMBUS_READ_BYTE_DATA))
192                         slow = "byte";
193                 else
194                         return -EPFNOSUPPORT;
195         }
196
197         /* Use 2 dummy devices for page select command */
198         mutex_lock(&ee1004_bus_lock);
199         if (++ee1004_dev_count == 1) {
200                 for (cnr = 0; cnr < 2; cnr++) {
201                         ee1004_set_page[cnr] = i2c_new_dummy_device(client->adapter,
202                                                 EE1004_ADDR_SET_PAGE + cnr);
203                         if (IS_ERR(ee1004_set_page[cnr])) {
204                                 dev_err(&client->dev,
205                                         "address 0x%02x unavailable\n",
206                                         EE1004_ADDR_SET_PAGE + cnr);
207                                 err = PTR_ERR(ee1004_set_page[cnr]);
208                                 goto err_clients;
209                         }
210                 }
211         } else if (i2c_adapter_id(client->adapter) !=
212                    i2c_adapter_id(ee1004_set_page[0]->adapter)) {
213                 dev_err(&client->dev,
214                         "Driver only supports devices on a single I2C bus\n");
215                 err = -EOPNOTSUPP;
216                 goto err_clients;
217         }
218
219         /* Remember current page to avoid unneeded page select */
220         err = ee1004_get_current_page();
221         if (err < 0)
222                 goto err_clients;
223         ee1004_current_page = err;
224         dev_dbg(&client->dev, "Currently selected page: %d\n",
225                 ee1004_current_page);
226         mutex_unlock(&ee1004_bus_lock);
227
228         /* Create the sysfs eeprom file */
229         err = sysfs_create_bin_file(&client->dev.kobj, &eeprom_attr);
230         if (err)
231                 goto err_clients_lock;
232
233         dev_info(&client->dev,
234                  "%u byte EE1004-compliant SPD EEPROM, read-only\n",
235                  EE1004_EEPROM_SIZE);
236         if (slow)
237                 dev_notice(&client->dev,
238                            "Falling back to %s reads, performance will suffer\n",
239                            slow);
240
241         return 0;
242
243  err_clients_lock:
244         mutex_lock(&ee1004_bus_lock);
245  err_clients:
246         if (--ee1004_dev_count == 0) {
247                 for (cnr--; cnr >= 0; cnr--) {
248                         i2c_unregister_device(ee1004_set_page[cnr]);
249                         ee1004_set_page[cnr] = NULL;
250                 }
251         }
252         mutex_unlock(&ee1004_bus_lock);
253
254         return err;
255 }
256
257 static int ee1004_remove(struct i2c_client *client)
258 {
259         int i;
260
261         sysfs_remove_bin_file(&client->dev.kobj, &eeprom_attr);
262
263         /* Remove page select clients if this is the last device */
264         mutex_lock(&ee1004_bus_lock);
265         if (--ee1004_dev_count == 0) {
266                 for (i = 0; i < 2; i++) {
267                         i2c_unregister_device(ee1004_set_page[i]);
268                         ee1004_set_page[i] = NULL;
269                 }
270         }
271         mutex_unlock(&ee1004_bus_lock);
272
273         return 0;
274 }
275
276 /*-------------------------------------------------------------------------*/
277
278 static struct i2c_driver ee1004_driver = {
279         .driver = {
280                 .name = "ee1004",
281         },
282         .probe = ee1004_probe,
283         .remove = ee1004_remove,
284         .id_table = ee1004_ids,
285 };
286 module_i2c_driver(ee1004_driver);
287
288 MODULE_DESCRIPTION("Driver for EE1004-compliant DDR4 SPD EEPROMs");
289 MODULE_AUTHOR("Jean Delvare");
290 MODULE_LICENSE("GPL");