Linux Audio

Check our new training course

Loading...
v6.13.7
  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/device.h>
 13#include <linux/i2c.h>
 14#include <linux/init.h>
 15#include <linux/kernel.h>
 16#include <linux/mod_devicetable.h>
 17#include <linux/module.h>
 18#include <linux/mutex.h>
 19#include <linux/nvmem-provider.h>
 20
 21/*
 22 * DDR4 memory modules use special EEPROMs following the Jedec EE1004
 23 * specification. These are 512-byte EEPROMs using a single I2C address
 24 * in the 0x50-0x57 range for data. One of two 256-byte page is selected
 25 * by writing a command to I2C address 0x36 or 0x37 on the same I2C bus.
 26 *
 27 * Therefore we need to request these 2 additional addresses, and serialize
 28 * access to all such EEPROMs with a single mutex.
 29 *
 30 * We assume it is safe to read up to 32 bytes at once from these EEPROMs.
 31 * We use SMBus access even if I2C is available, these EEPROMs are small
 32 * enough, and reading from them infrequent enough, that we favor simplicity
 33 * over performance.
 34 */
 35
 36#define EE1004_MAX_BUSSES		8
 37#define EE1004_ADDR_SET_PAGE		0x36
 38#define EE1004_NUM_PAGES		2
 39#define EE1004_PAGE_SIZE		256
 40#define EE1004_PAGE_SHIFT		8
 41#define EE1004_EEPROM_SIZE		(EE1004_PAGE_SIZE * EE1004_NUM_PAGES)
 42
 43/*
 44 * Mutex protects ee1004_set_page and ee1004_dev_count, and must be held
 45 * from page selection to end of read.
 46 */
 47static DEFINE_MUTEX(ee1004_bus_lock);
 48
 49static struct ee1004_bus_data {
 50	struct i2c_adapter *adap;
 51	struct i2c_client *set_page[EE1004_NUM_PAGES];
 52	unsigned int dev_count;
 53	int current_page;
 54} ee1004_bus_data[EE1004_MAX_BUSSES];
 55
 56static const struct i2c_device_id ee1004_ids[] = {
 57	{ "ee1004" },
 58	{ }
 59};
 60MODULE_DEVICE_TABLE(i2c, ee1004_ids);
 61
 62/*-------------------------------------------------------------------------*/
 63
 64static struct ee1004_bus_data *ee1004_get_bus_data(struct i2c_adapter *adap)
 65{
 66	int i;
 67
 68	for (i = 0; i < EE1004_MAX_BUSSES; i++)
 69		if (ee1004_bus_data[i].adap == adap)
 70			return ee1004_bus_data + i;
 71
 72	/* If not existent yet, create new entry */
 73	for (i = 0; i < EE1004_MAX_BUSSES; i++)
 74		if (!ee1004_bus_data[i].adap) {
 75			ee1004_bus_data[i].adap = adap;
 76			return ee1004_bus_data + i;
 77		}
 78
 79	return NULL;
 80}
 81
 82static int ee1004_get_current_page(struct ee1004_bus_data *bd)
 83{
 84	int err;
 85
 86	err = i2c_smbus_read_byte(bd->set_page[0]);
 87	if (err == -ENXIO) {
 88		/* Nack means page 1 is selected */
 89		return 1;
 90	}
 91	if (err < 0) {
 92		/* Anything else is a real error, bail out */
 93		return err;
 94	}
 95
 96	/* Ack means page 0 is selected, returned value meaningless */
 97	return 0;
 98}
 99
100static int ee1004_set_current_page(struct i2c_client *client, int page)
101{
102	struct ee1004_bus_data *bd = i2c_get_clientdata(client);
103	int ret;
104
105	if (page == bd->current_page)
106		return 0;
107
108	/* Data is ignored */
109	ret = i2c_smbus_write_byte(bd->set_page[page], 0x00);
110	/*
111	 * Don't give up just yet. Some memory modules will select the page
112	 * but not ack the command. Check which page is selected now.
113	 */
114	if (ret == -ENXIO && ee1004_get_current_page(bd) == page)
115		ret = 0;
116	if (ret < 0) {
117		dev_err(&client->dev, "Failed to select page %d (%d)\n", page, ret);
118		return ret;
119	}
120
121	dev_dbg(&client->dev, "Selected page %d\n", page);
122	bd->current_page = page;
123
124	return 0;
125}
126
127static ssize_t ee1004_eeprom_read(struct i2c_client *client, char *buf,
128				  unsigned int offset, size_t count)
129{
130	int status, page;
131
132	page = offset >> EE1004_PAGE_SHIFT;
133	offset &= (1 << EE1004_PAGE_SHIFT) - 1;
134
135	status = ee1004_set_current_page(client, page);
136	if (status)
137		return status;
138
139	/* Can't cross page boundaries */
140	if (offset + count > EE1004_PAGE_SIZE)
141		count = EE1004_PAGE_SIZE - offset;
142
143	if (count > I2C_SMBUS_BLOCK_MAX)
144		count = I2C_SMBUS_BLOCK_MAX;
145
146	return i2c_smbus_read_i2c_block_data_or_emulated(client, offset, count, buf);
147}
148
149static int ee1004_read(void *priv, unsigned int off, void *val, size_t count)
150{
151	struct i2c_client *client = priv;
152	char *buf = val;
153	int ret;
154
155	if (unlikely(!count))
156		return count;
157
158	if (off + count > EE1004_EEPROM_SIZE)
159		return -EINVAL;
160
161	/*
162	 * Read data from chip, protecting against concurrent access to
163	 * other EE1004 SPD EEPROMs on the same adapter.
164	 */
165	mutex_lock(&ee1004_bus_lock);
166
167	while (count) {
168		ret = ee1004_eeprom_read(client, buf, off, count);
169		if (ret < 0) {
170			mutex_unlock(&ee1004_bus_lock);
171			return ret;
172		}
173
174		buf += ret;
175		off += ret;
176		count -= ret;
177	}
178
179	mutex_unlock(&ee1004_bus_lock);
180
181	return 0;
182}
183
184static void ee1004_probe_temp_sensor(struct i2c_client *client)
185{
186	struct i2c_board_info info = { .type = "jc42" };
187	unsigned short addr = 0x18 | (client->addr & 7);
188	unsigned short addr_list[] = { addr, I2C_CLIENT_END };
189	u8 data[2];
190	int ret;
191
192	/* byte 14, bit 7 is set if temp sensor is present */
193	ret = ee1004_eeprom_read(client, data, 14, 1);
194	if (ret != 1)
195		return;
196
197	if (!(data[0] & BIT(7))) {
198		/*
199		 * If the SPD data suggests that there is no temperature
200		 * sensor, it may still be there for SPD revision 1.0.
201		 * See SPD Annex L, Revision 1 and 2, for details.
202		 * Check DIMM type and SPD revision; if it is a DDR4
203		 * with SPD revision 1.0, check the thermal sensor address
204		 * and instantiate the jc42 driver if a chip is found at
205		 * that address.
206		 * It is not necessary to check if there is a chip at the
207		 * temperature sensor address since i2c_new_scanned_device()
208		 * will do that and return silently if no chip is found.
209		 */
210		ret = ee1004_eeprom_read(client, data, 1, 2);
211		if (ret != 2 || data[0] != 0x10 || data[1] != 0x0c)
212			return;
213	}
214	i2c_new_scanned_device(client->adapter, &info, addr_list, NULL);
215}
216
217static void ee1004_cleanup(int idx, struct ee1004_bus_data *bd)
218{
219	if (--bd->dev_count == 0) {
220		while (--idx >= 0)
221			i2c_unregister_device(bd->set_page[idx]);
222		memset(bd, 0, sizeof(struct ee1004_bus_data));
223	}
224}
225
226static void ee1004_cleanup_bus_data(void *data)
227{
228	struct ee1004_bus_data *bd = data;
229
230	/* Remove page select clients if this is the last device */
231	mutex_lock(&ee1004_bus_lock);
232	ee1004_cleanup(EE1004_NUM_PAGES, bd);
233	mutex_unlock(&ee1004_bus_lock);
234}
235
236static int ee1004_init_bus_data(struct i2c_client *client)
237{
238	struct ee1004_bus_data *bd;
239	int err, cnr = 0;
240
241	bd = ee1004_get_bus_data(client->adapter);
242	if (!bd)
243		return dev_err_probe(&client->dev, -ENOSPC, "Only %d busses supported",
244				     EE1004_MAX_BUSSES);
245
246	i2c_set_clientdata(client, bd);
247
248	if (++bd->dev_count == 1) {
249		/* Use 2 dummy devices for page select command */
 
250		for (cnr = 0; cnr < EE1004_NUM_PAGES; cnr++) {
251			struct i2c_client *cl;
252
253			cl = i2c_new_dummy_device(client->adapter, EE1004_ADDR_SET_PAGE + cnr);
254			if (IS_ERR(cl)) {
255				err = PTR_ERR(cl);
256				goto err_out;
257			}
258
259			bd->set_page[cnr] = cl;
260		}
261
262		/* Remember current page to avoid unneeded page select */
263		err = ee1004_get_current_page(bd);
264		if (err < 0)
265			goto err_out;
266
267		dev_dbg(&client->dev, "Currently selected page: %d\n", err);
268		bd->current_page = err;
 
 
 
 
 
269	}
 
 
 
 
 
270
271	return 0;
272
273err_out:
274	ee1004_cleanup(cnr, bd);
 
275
276	return err;
277}
278
279static int ee1004_probe(struct i2c_client *client)
280{
281	struct nvmem_config config = {
282		.dev = &client->dev,
283		.name = dev_name(&client->dev),
284		.id = NVMEM_DEVID_NONE,
285		.owner = THIS_MODULE,
286		.type = NVMEM_TYPE_EEPROM,
287		.read_only = true,
288		.root_only = false,
289		.reg_read = ee1004_read,
290		.size = EE1004_EEPROM_SIZE,
291		.word_size = 1,
292		.stride = 1,
293		.priv = client,
294		.compat = true,
295		.base_dev = &client->dev,
296	};
297	struct nvmem_device *ndev;
298	int err;
299
300	/* Make sure we can operate on this adapter */
301	if (!i2c_check_functionality(client->adapter,
302				     I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_READ_I2C_BLOCK) &&
303	    !i2c_check_functionality(client->adapter,
304				     I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_READ_BYTE_DATA))
305		return -EPFNOSUPPORT;
306
307	mutex_lock(&ee1004_bus_lock);
308
309	err = ee1004_init_bus_data(client);
310	if (err < 0) {
311		mutex_unlock(&ee1004_bus_lock);
312		return err;
313	}
314
315	ee1004_probe_temp_sensor(client);
316
317	mutex_unlock(&ee1004_bus_lock);
318
319	err = devm_add_action_or_reset(&client->dev, ee1004_cleanup_bus_data,
320				       i2c_get_clientdata(client));
321	if (err < 0)
322		return err;
323
324	ndev = devm_nvmem_register(&client->dev, &config);
325	if (IS_ERR(ndev))
326		return PTR_ERR(ndev);
327
328	dev_info(&client->dev,
329		 "%u byte EE1004-compliant SPD EEPROM, read-only\n",
330		 EE1004_EEPROM_SIZE);
331
332	return 0;
333}
334
335/*-------------------------------------------------------------------------*/
336
337static struct i2c_driver ee1004_driver = {
338	.driver = {
339		.name = "ee1004",
 
340	},
341	.probe = ee1004_probe,
 
342	.id_table = ee1004_ids,
343};
344module_i2c_driver(ee1004_driver);
345
346MODULE_DESCRIPTION("Driver for EE1004-compliant DDR4 SPD EEPROMs");
347MODULE_AUTHOR("Jean Delvare");
348MODULE_LICENSE("GPL");
v5.14.15
  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_NUM_PAGES		2
 36#define EE1004_PAGE_SIZE		256
 37#define EE1004_PAGE_SHIFT		8
 38#define EE1004_EEPROM_SIZE		(EE1004_PAGE_SIZE * EE1004_NUM_PAGES)
 39
 40/*
 41 * Mutex protects ee1004_set_page and ee1004_dev_count, and must be held
 42 * from page selection to end of read.
 43 */
 44static DEFINE_MUTEX(ee1004_bus_lock);
 45static struct i2c_client *ee1004_set_page[EE1004_NUM_PAGES];
 46static unsigned int ee1004_dev_count;
 47static int ee1004_current_page;
 
 
 
 
 48
 49static const struct i2c_device_id ee1004_ids[] = {
 50	{ "ee1004", 0 },
 51	{ }
 52};
 53MODULE_DEVICE_TABLE(i2c, ee1004_ids);
 54
 55/*-------------------------------------------------------------------------*/
 56
 57static int ee1004_get_current_page(void)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 58{
 59	int err;
 60
 61	err = i2c_smbus_read_byte(ee1004_set_page[0]);
 62	if (err == -ENXIO) {
 63		/* Nack means page 1 is selected */
 64		return 1;
 65	}
 66	if (err < 0) {
 67		/* Anything else is a real error, bail out */
 68		return err;
 69	}
 70
 71	/* Ack means page 0 is selected, returned value meaningless */
 72	return 0;
 73}
 74
 75static int ee1004_set_current_page(struct device *dev, int page)
 76{
 
 77	int ret;
 78
 79	if (page == ee1004_current_page)
 80		return 0;
 81
 82	/* Data is ignored */
 83	ret = i2c_smbus_write_byte(ee1004_set_page[page], 0x00);
 84	/*
 85	 * Don't give up just yet. Some memory modules will select the page
 86	 * but not ack the command. Check which page is selected now.
 87	 */
 88	if (ret == -ENXIO && ee1004_get_current_page() == page)
 89		ret = 0;
 90	if (ret < 0) {
 91		dev_err(dev, "Failed to select page %d (%d)\n", page, ret);
 92		return ret;
 93	}
 94
 95	dev_dbg(dev, "Selected page %d\n", page);
 96	ee1004_current_page = page;
 97
 98	return 0;
 99}
100
101static ssize_t ee1004_eeprom_read(struct i2c_client *client, char *buf,
102				  unsigned int offset, size_t count)
103{
104	int status, page;
105
106	page = offset >> EE1004_PAGE_SHIFT;
107	offset &= (1 << EE1004_PAGE_SHIFT) - 1;
108
109	status = ee1004_set_current_page(&client->dev, page);
110	if (status)
111		return status;
112
113	/* Can't cross page boundaries */
114	if (offset + count > EE1004_PAGE_SIZE)
115		count = EE1004_PAGE_SIZE - offset;
116
 
 
 
117	return i2c_smbus_read_i2c_block_data_or_emulated(client, offset, count, buf);
118}
119
120static ssize_t eeprom_read(struct file *filp, struct kobject *kobj,
121			   struct bin_attribute *bin_attr,
122			   char *buf, loff_t off, size_t count)
123{
124	struct i2c_client *client = kobj_to_i2c_client(kobj);
125	size_t requested = count;
126	int ret = 0;
 
 
 
 
127
128	/*
129	 * Read data from chip, protecting against concurrent access to
130	 * other EE1004 SPD EEPROMs on the same adapter.
131	 */
132	mutex_lock(&ee1004_bus_lock);
133
134	while (count) {
135		ret = ee1004_eeprom_read(client, buf, off, count);
136		if (ret < 0)
137			goto out;
 
 
138
139		buf += ret;
140		off += ret;
141		count -= ret;
142	}
143out:
144	mutex_unlock(&ee1004_bus_lock);
145
146	return ret < 0 ? ret : requested;
147}
148
149static BIN_ATTR_RO(eeprom, EE1004_EEPROM_SIZE);
 
 
 
 
 
 
150
151static struct bin_attribute *ee1004_attrs[] = {
152	&bin_attr_eeprom,
153	NULL
154};
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155
156BIN_ATTRIBUTE_GROUPS(ee1004);
 
 
 
 
 
 
 
157
158static void ee1004_cleanup(int idx)
159{
160	if (--ee1004_dev_count == 0)
161		while (--idx >= 0) {
162			i2c_unregister_device(ee1004_set_page[idx]);
163			ee1004_set_page[idx] = NULL;
164		}
 
165}
166
167static int ee1004_probe(struct i2c_client *client)
168{
 
169	int err, cnr = 0;
170
171	/* Make sure we can operate on this adapter */
172	if (!i2c_check_functionality(client->adapter,
173				     I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_READ_I2C_BLOCK) &&
174	    !i2c_check_functionality(client->adapter,
175				     I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_READ_BYTE_DATA))
176		return -EPFNOSUPPORT;
177
178	/* Use 2 dummy devices for page select command */
179	mutex_lock(&ee1004_bus_lock);
180	if (++ee1004_dev_count == 1) {
181		for (cnr = 0; cnr < EE1004_NUM_PAGES; cnr++) {
182			struct i2c_client *cl;
183
184			cl = i2c_new_dummy_device(client->adapter, EE1004_ADDR_SET_PAGE + cnr);
185			if (IS_ERR(cl)) {
186				err = PTR_ERR(cl);
187				goto err_clients;
188			}
189			ee1004_set_page[cnr] = cl;
 
190		}
191
192		/* Remember current page to avoid unneeded page select */
193		err = ee1004_get_current_page();
194		if (err < 0)
195			goto err_clients;
 
196		dev_dbg(&client->dev, "Currently selected page: %d\n", err);
197		ee1004_current_page = err;
198	} else if (client->adapter != ee1004_set_page[0]->adapter) {
199		dev_err(&client->dev,
200			"Driver only supports devices on a single I2C bus\n");
201		err = -EOPNOTSUPP;
202		goto err_clients;
203	}
204	mutex_unlock(&ee1004_bus_lock);
205
206	dev_info(&client->dev,
207		 "%u byte EE1004-compliant SPD EEPROM, read-only\n",
208		 EE1004_EEPROM_SIZE);
209
210	return 0;
211
212 err_clients:
213	ee1004_cleanup(cnr);
214	mutex_unlock(&ee1004_bus_lock);
215
216	return err;
217}
218
219static int ee1004_remove(struct i2c_client *client)
220{
221	/* Remove page select clients if this is the last device */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
222	mutex_lock(&ee1004_bus_lock);
223	ee1004_cleanup(EE1004_NUM_PAGES);
 
 
 
 
 
 
 
 
224	mutex_unlock(&ee1004_bus_lock);
225
 
 
 
 
 
 
 
 
 
 
 
 
 
226	return 0;
227}
228
229/*-------------------------------------------------------------------------*/
230
231static struct i2c_driver ee1004_driver = {
232	.driver = {
233		.name = "ee1004",
234		.dev_groups = ee1004_groups,
235	},
236	.probe_new = ee1004_probe,
237	.remove = ee1004_remove,
238	.id_table = ee1004_ids,
239};
240module_i2c_driver(ee1004_driver);
241
242MODULE_DESCRIPTION("Driver for EE1004-compliant DDR4 SPD EEPROMs");
243MODULE_AUTHOR("Jean Delvare");
244MODULE_LICENSE("GPL");