Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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