Linux Audio

Check our new training course

Loading...
v6.8
  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_MAX_BUSSES		8
 35#define EE1004_ADDR_SET_PAGE		0x36
 36#define EE1004_NUM_PAGES		2
 37#define EE1004_PAGE_SIZE		256
 38#define EE1004_PAGE_SHIFT		8
 39#define EE1004_EEPROM_SIZE		(EE1004_PAGE_SIZE * EE1004_NUM_PAGES)
 40
 41/*
 42 * Mutex protects ee1004_set_page and ee1004_dev_count, and must be held
 43 * from page selection to end of read.
 44 */
 45static DEFINE_MUTEX(ee1004_bus_lock);
 46
 47static struct ee1004_bus_data {
 48	struct i2c_adapter *adap;
 49	struct i2c_client *set_page[EE1004_NUM_PAGES];
 50	unsigned int dev_count;
 51	int current_page;
 52} ee1004_bus_data[EE1004_MAX_BUSSES];
 53
 54static const struct i2c_device_id ee1004_ids[] = {
 55	{ "ee1004", 0 },
 56	{ }
 57};
 58MODULE_DEVICE_TABLE(i2c, ee1004_ids);
 59
 60/*-------------------------------------------------------------------------*/
 61
 62static struct ee1004_bus_data *ee1004_get_bus_data(struct i2c_adapter *adap)
 63{
 64	int i;
 65
 66	for (i = 0; i < EE1004_MAX_BUSSES; i++)
 67		if (ee1004_bus_data[i].adap == adap)
 68			return ee1004_bus_data + i;
 69
 70	/* If not existent yet, create new entry */
 71	for (i = 0; i < EE1004_MAX_BUSSES; i++)
 72		if (!ee1004_bus_data[i].adap) {
 73			ee1004_bus_data[i].adap = adap;
 74			return ee1004_bus_data + i;
 75		}
 76
 77	return NULL;
 78}
 79
 80static int ee1004_get_current_page(struct ee1004_bus_data *bd)
 81{
 82	int err;
 83
 84	err = i2c_smbus_read_byte(bd->set_page[0]);
 85	if (err == -ENXIO) {
 86		/* Nack means page 1 is selected */
 87		return 1;
 88	}
 89	if (err < 0) {
 90		/* Anything else is a real error, bail out */
 91		return err;
 92	}
 93
 94	/* Ack means page 0 is selected, returned value meaningless */
 95	return 0;
 96}
 97
 98static int ee1004_set_current_page(struct i2c_client *client, int page)
 99{
100	struct ee1004_bus_data *bd = i2c_get_clientdata(client);
101	int ret;
102
103	if (page == bd->current_page)
104		return 0;
105
106	/* Data is ignored */
107	ret = i2c_smbus_write_byte(bd->set_page[page], 0x00);
108	/*
109	 * Don't give up just yet. Some memory modules will select the page
110	 * but not ack the command. Check which page is selected now.
111	 */
112	if (ret == -ENXIO && ee1004_get_current_page(bd) == page)
113		ret = 0;
114	if (ret < 0) {
115		dev_err(&client->dev, "Failed to select page %d (%d)\n", page, ret);
116		return ret;
117	}
118
119	dev_dbg(&client->dev, "Selected page %d\n", page);
120	bd->current_page = page;
121
122	return 0;
123}
124
125static ssize_t ee1004_eeprom_read(struct i2c_client *client, char *buf,
126				  unsigned int offset, size_t count)
127{
128	int status, page;
129
130	page = offset >> EE1004_PAGE_SHIFT;
131	offset &= (1 << EE1004_PAGE_SHIFT) - 1;
132
133	status = ee1004_set_current_page(client, page);
134	if (status)
135		return status;
136
 
 
137	/* Can't cross page boundaries */
138	if (offset + count > EE1004_PAGE_SIZE)
139		count = EE1004_PAGE_SIZE - offset;
140
141	if (count > I2C_SMBUS_BLOCK_MAX)
142		count = I2C_SMBUS_BLOCK_MAX;
 
143
144	return i2c_smbus_read_i2c_block_data_or_emulated(client, offset, count, buf);
145}
146
147static ssize_t eeprom_read(struct file *filp, struct kobject *kobj,
148			   struct bin_attribute *bin_attr,
149			   char *buf, loff_t off, size_t count)
150{
151	struct i2c_client *client = kobj_to_i2c_client(kobj);
 
152	size_t requested = count;
153	int ret = 0;
 
 
 
 
 
 
 
 
154
155	/*
156	 * Read data from chip, protecting against concurrent access to
157	 * other EE1004 SPD EEPROMs on the same adapter.
158	 */
159	mutex_lock(&ee1004_bus_lock);
160
161	while (count) {
162		ret = ee1004_eeprom_read(client, buf, off, count);
163		if (ret < 0)
164			goto out;
165
166		buf += ret;
167		off += ret;
168		count -= ret;
169	}
170out:
171	mutex_unlock(&ee1004_bus_lock);
172
173	return ret < 0 ? ret : requested;
174}
175
176static BIN_ATTR_RO(eeprom, EE1004_EEPROM_SIZE);
177
178static struct bin_attribute *ee1004_attrs[] = {
179	&bin_attr_eeprom,
180	NULL
181};
182
183BIN_ATTRIBUTE_GROUPS(ee1004);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184
185static void ee1004_probe_temp_sensor(struct i2c_client *client)
186{
187	struct i2c_board_info info = { .type = "jc42" };
188	u8 byte14;
189	int ret;
190
191	/* byte 14, bit 7 is set if temp sensor is present */
192	ret = ee1004_eeprom_read(client, &byte14, 14, 1);
193	if (ret != 1 || !(byte14 & BIT(7)))
194		return;
 
 
 
 
195
196	info.addr = 0x18 | (client->addr & 7);
197
198	i2c_new_client_device(client->adapter, &info);
199}
200
201static void ee1004_cleanup(int idx, struct ee1004_bus_data *bd)
202{
203	if (--bd->dev_count == 0) {
204		while (--idx >= 0)
205			i2c_unregister_device(bd->set_page[idx]);
206		memset(bd, 0, sizeof(struct ee1004_bus_data));
207	}
208}
209
210static int ee1004_probe(struct i2c_client *client)
 
211{
212	struct ee1004_bus_data *bd;
213	int err, cnr = 0;
 
214
215	/* Make sure we can operate on this adapter */
216	if (!i2c_check_functionality(client->adapter,
217				     I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_READ_I2C_BLOCK) &&
218	    !i2c_check_functionality(client->adapter,
219				     I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_READ_BYTE_DATA))
220		return -EPFNOSUPPORT;
221
222	mutex_lock(&ee1004_bus_lock);
223
224	bd = ee1004_get_bus_data(client->adapter);
225	if (!bd) {
226		mutex_unlock(&ee1004_bus_lock);
227		return dev_err_probe(&client->dev, -ENOSPC,
228				     "Only %d busses supported", EE1004_MAX_BUSSES);
229	}
230
231	i2c_set_clientdata(client, bd);
232
233	if (++bd->dev_count == 1) {
234		/* Use 2 dummy devices for page select command */
235		for (cnr = 0; cnr < EE1004_NUM_PAGES; cnr++) {
236			struct i2c_client *cl;
237
238			cl = i2c_new_dummy_device(client->adapter, EE1004_ADDR_SET_PAGE + cnr);
239			if (IS_ERR(cl)) {
240				err = PTR_ERR(cl);
 
241				goto err_clients;
242			}
243			bd->set_page[cnr] = cl;
244		}
245
246		/* Remember current page to avoid unneeded page select */
247		err = ee1004_get_current_page(bd);
248		if (err < 0)
249			goto err_clients;
250		dev_dbg(&client->dev, "Currently selected page: %d\n", err);
251		bd->current_page = err;
252	}
253
254	ee1004_probe_temp_sensor(client);
255
 
 
 
 
 
256	mutex_unlock(&ee1004_bus_lock);
257
 
 
 
 
 
258	dev_info(&client->dev,
259		 "%u byte EE1004-compliant SPD EEPROM, read-only\n",
260		 EE1004_EEPROM_SIZE);
 
 
 
 
261
262	return 0;
263
 
 
264 err_clients:
265	ee1004_cleanup(cnr, bd);
 
 
 
 
 
266	mutex_unlock(&ee1004_bus_lock);
267
268	return err;
269}
270
271static void ee1004_remove(struct i2c_client *client)
272{
273	struct ee1004_bus_data *bd = i2c_get_clientdata(client);
 
 
274
275	/* Remove page select clients if this is the last device */
276	mutex_lock(&ee1004_bus_lock);
277	ee1004_cleanup(EE1004_NUM_PAGES, bd);
 
 
 
 
 
278	mutex_unlock(&ee1004_bus_lock);
 
 
279}
280
281/*-------------------------------------------------------------------------*/
282
283static struct i2c_driver ee1004_driver = {
284	.driver = {
285		.name = "ee1004",
286		.dev_groups = ee1004_groups,
287	},
288	.probe = ee1004_probe,
289	.remove = ee1004_remove,
290	.id_table = ee1004_ids,
291};
292module_i2c_driver(ee1004_driver);
 
 
 
 
 
 
 
 
 
 
 
293
294MODULE_DESCRIPTION("Driver for EE1004-compliant DDR4 SPD EEPROMs");
295MODULE_AUTHOR("Jean Delvare");
296MODULE_LICENSE("GPL");
v5.4
  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 */
 43static DEFINE_MUTEX(ee1004_bus_lock);
 44static struct i2c_client *ee1004_set_page[2];
 45static unsigned int ee1004_dev_count;
 46static int ee1004_current_page;
 
 
 
 
 47
 48static const struct i2c_device_id ee1004_ids[] = {
 49	{ "ee1004", 0 },
 50	{ }
 51};
 52MODULE_DEVICE_TABLE(i2c, ee1004_ids);
 53
 54/*-------------------------------------------------------------------------*/
 55
 56static 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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 74static 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	status = i2c_smbus_read_i2c_block_data_or_emulated(client, offset,
 86							   count, buf);
 87	dev_dbg(&client->dev, "read %zu@%d --> %d\n", count, offset, status);
 88
 89	return status;
 90}
 91
 92static ssize_t ee1004_read(struct file *filp, struct kobject *kobj,
 93			   struct bin_attribute *bin_attr,
 94			   char *buf, loff_t off, size_t count)
 95{
 96	struct device *dev = kobj_to_dev(kobj);
 97	struct i2c_client *client = to_i2c_client(dev);
 98	size_t requested = count;
 99	int page;
100
101	if (unlikely(!count))
102		return count;
103
104	page = off >> EE1004_PAGE_SHIFT;
105	if (unlikely(page > 1))
106		return 0;
107	off &= (1 << EE1004_PAGE_SHIFT) - 1;
108
109	/*
110	 * Read data from chip, protecting against concurrent access to
111	 * other EE1004 SPD EEPROMs on the same adapter.
112	 */
113	mutex_lock(&ee1004_bus_lock);
114
115	while (count) {
116		int status;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
118		/* Select page */
119		if (page != ee1004_current_page) {
120			/* Data is ignored */
121			status = i2c_smbus_write_byte(ee1004_set_page[page],
122						      0x00);
123			if (status == -ENXIO) {
124				/*
125				 * Don't give up just yet. Some memory
126				 * modules will select the page but not
127				 * ack the command. Check which page is
128				 * selected now.
129				 */
130				if (ee1004_get_current_page() == page)
131					status = 0;
132			}
133			if (status < 0) {
134				dev_err(dev, "Failed to select page %d (%d)\n",
135					page, status);
136				mutex_unlock(&ee1004_bus_lock);
137				return status;
138			}
139			dev_dbg(dev, "Selected page %d\n", page);
140			ee1004_current_page = page;
141		}
142
143		status = ee1004_eeprom_read(client, buf, off, count);
144		if (status < 0) {
145			mutex_unlock(&ee1004_bus_lock);
146			return status;
147		}
148		buf += status;
149		off += status;
150		count -= status;
151
152		if (off == EE1004_PAGE_SIZE) {
153			page++;
154			off = 0;
155		}
156	}
157
158	mutex_unlock(&ee1004_bus_lock);
159
160	return requested;
161}
162
163static const struct bin_attribute eeprom_attr = {
164	.attr = {
165		.name = "eeprom",
166		.mode = 0444,
167	},
168	.size = EE1004_EEPROM_SIZE,
169	.read = ee1004_read,
170};
171
172static int ee1004_probe(struct i2c_client *client,
173			const struct i2c_device_id *id)
174{
 
175	int err, cnr = 0;
176	const char *slow = NULL;
177
178	/* Make sure we can operate on this adapter */
179	if (!i2c_check_functionality(client->adapter,
180				     I2C_FUNC_SMBUS_READ_BYTE |
181				     I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
182		if (i2c_check_functionality(client->adapter,
183				     I2C_FUNC_SMBUS_READ_BYTE |
184				     I2C_FUNC_SMBUS_READ_WORD_DATA))
185			slow = "word";
186		else if (i2c_check_functionality(client->adapter,
187				     I2C_FUNC_SMBUS_READ_BYTE |
188				     I2C_FUNC_SMBUS_READ_BYTE_DATA))
189			slow = "byte";
190		else
191			return -EPFNOSUPPORT;
192	}
193
194	/* Use 2 dummy devices for page select command */
195	mutex_lock(&ee1004_bus_lock);
196	if (++ee1004_dev_count == 1) {
197		for (cnr = 0; cnr < 2; cnr++) {
198			ee1004_set_page[cnr] = i2c_new_dummy_device(client->adapter,
199						EE1004_ADDR_SET_PAGE + cnr);
200			if (IS_ERR(ee1004_set_page[cnr])) {
201				dev_err(&client->dev,
202					"address 0x%02x unavailable\n",
203					EE1004_ADDR_SET_PAGE + cnr);
204				err = PTR_ERR(ee1004_set_page[cnr]);
205				goto err_clients;
206			}
 
207		}
208	} else if (i2c_adapter_id(client->adapter) !=
209		   i2c_adapter_id(ee1004_set_page[0]->adapter)) {
210		dev_err(&client->dev,
211			"Driver only supports devices on a single I2C bus\n");
212		err = -EOPNOTSUPP;
213		goto err_clients;
 
214	}
215
216	/* Remember current page to avoid unneeded page select */
217	err = ee1004_get_current_page();
218	if (err < 0)
219		goto err_clients;
220	ee1004_current_page = err;
221	dev_dbg(&client->dev, "Currently selected page: %d\n",
222		ee1004_current_page);
223	mutex_unlock(&ee1004_bus_lock);
224
225	/* Create the sysfs eeprom file */
226	err = sysfs_create_bin_file(&client->dev.kobj, &eeprom_attr);
227	if (err)
228		goto err_clients_lock;
229
230	dev_info(&client->dev,
231		 "%u byte EE1004-compliant SPD EEPROM, read-only\n",
232		 EE1004_EEPROM_SIZE);
233	if (slow)
234		dev_notice(&client->dev,
235			   "Falling back to %s reads, performance will suffer\n",
236			   slow);
237
238	return 0;
239
240 err_clients_lock:
241	mutex_lock(&ee1004_bus_lock);
242 err_clients:
243	if (--ee1004_dev_count == 0) {
244		for (cnr--; cnr >= 0; cnr--) {
245			i2c_unregister_device(ee1004_set_page[cnr]);
246			ee1004_set_page[cnr] = NULL;
247		}
248	}
249	mutex_unlock(&ee1004_bus_lock);
250
251	return err;
252}
253
254static int ee1004_remove(struct i2c_client *client)
255{
256	int i;
257
258	sysfs_remove_bin_file(&client->dev.kobj, &eeprom_attr);
259
260	/* Remove page select clients if this is the last device */
261	mutex_lock(&ee1004_bus_lock);
262	if (--ee1004_dev_count == 0) {
263		for (i = 0; i < 2; i++) {
264			i2c_unregister_device(ee1004_set_page[i]);
265			ee1004_set_page[i] = NULL;
266		}
267	}
268	mutex_unlock(&ee1004_bus_lock);
269
270	return 0;
271}
272
273/*-------------------------------------------------------------------------*/
274
275static struct i2c_driver ee1004_driver = {
276	.driver = {
277		.name = "ee1004",
 
278	},
279	.probe = ee1004_probe,
280	.remove = ee1004_remove,
281	.id_table = ee1004_ids,
282};
283
284static int __init ee1004_init(void)
285{
286	return i2c_add_driver(&ee1004_driver);
287}
288module_init(ee1004_init);
289
290static void __exit ee1004_exit(void)
291{
292	i2c_del_driver(&ee1004_driver);
293}
294module_exit(ee1004_exit);
295
296MODULE_DESCRIPTION("Driver for EE1004-compliant DDR4 SPD EEPROMs");
297MODULE_AUTHOR("Jean Delvare");
298MODULE_LICENSE("GPL");