linux-i2c.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] i2c: smbus: Add a way to instantiate SPD EEPROMs automatically
@ 2020-03-16 10:22 Jean Delvare
  2020-03-16 10:24 ` [PATCH 2/2] i2c: i801: Instantiate " Jean Delvare
  2020-05-29 10:58 ` [PATCH 1/2] i2c: smbus: Add a way to instantiate " Wolfram Sang
  0 siblings, 2 replies; 4+ messages in thread
From: Jean Delvare @ 2020-03-16 10:22 UTC (permalink / raw)
  To: Linux I2C; +Cc: Wolfram Sang

In simple cases we can instantiate SPD EEPROMs on the SMBus
automatically. Start with just DDR2, DDR3 and DDR4 on x86 for now,
and only for systems with no more than 4 memory slots. These
limitations may be lifted later.

Signed-off-by: Jean Delvare <jdelvare@suse.de>
Cc: Wolfram Sang <wsa@the-dreams.de>
---
The dependencies have been merged into the dmi subsystem in kernel v5.5
so I think we are good to merge this now.

 drivers/i2c/i2c-smbus.c   |  104 +++++++++++++++++++++++++++++++++++++++++++++-
 include/linux/i2c-smbus.h |    8 +++
 2 files changed, 110 insertions(+), 2 deletions(-)

--- linux-5.5.orig/drivers/i2c/i2c-smbus.c	2020-01-27 01:23:03.000000000 +0100
+++ linux-5.5/drivers/i2c/i2c-smbus.c	2020-02-11 12:54:11.588866579 +0100
@@ -3,10 +3,11 @@
  * i2c-smbus.c - SMBus extensions to the I2C protocol
  *
  * Copyright (C) 2008 David Brownell
- * Copyright (C) 2010 Jean Delvare <jdelvare@suse.de>
+ * Copyright (C) 2010-2019 Jean Delvare <jdelvare@suse.de>
  */
 
 #include <linux/device.h>
+#include <linux/dmi.h>
 #include <linux/i2c.h>
 #include <linux/i2c-smbus.h>
 #include <linux/interrupt.h>
@@ -196,6 +197,107 @@ EXPORT_SYMBOL_GPL(i2c_handle_smbus_alert
 
 module_i2c_driver(smbalert_driver);
 
+/*
+ * SPD is not part of SMBus but we include it here for convenience as the
+ * target systems are the same.
+ * Restrictions to automatic SPD instantiation:
+ *  - Only works if all filled slots have the same memory type
+ *  - Only works for DDR2, DDR3 and DDR4 for now
+ *  - Only works on systems with 1 to 4 memory slots
+ */
+#if IS_ENABLED(CONFIG_DMI)
+void i2c_register_spd(struct i2c_adapter *adap)
+{
+	int n, slot_count = 0, dimm_count = 0;
+	u16 handle;
+	u8 common_mem_type = 0x0, mem_type;
+	u64 mem_size;
+	const char *name;
+
+	while ((handle = dmi_memdev_handle(slot_count)) != 0xffff) {
+		slot_count++;
+
+		/* Skip empty slots */
+		mem_size = dmi_memdev_size(handle);
+		if (!mem_size)
+			continue;
+
+		/* Skip undefined memory type */
+		mem_type = dmi_memdev_type(handle);
+		if (mem_type <= 0x02)		/* Invalid, Other, Unknown */
+			continue;
+
+		if (!common_mem_type) {
+			/* First filled slot */
+			common_mem_type = mem_type;
+		} else {
+			/* Check that all filled slots have the same type */
+			if (mem_type != common_mem_type) {
+				dev_warn(&adap->dev,
+					 "Different memory types mixed, not instantiating SPD\n");
+				return;
+			}
+		}
+		dimm_count++;
+	}
+
+	/* No useful DMI data, bail out */
+	if (!dimm_count)
+		return;
+
+	dev_info(&adap->dev, "%d/%d memory slots populated (from DMI)\n",
+		 dimm_count, slot_count);
+
+	if (slot_count > 4) {
+		dev_warn(&adap->dev,
+			 "Systems with more than 4 memory slots not supported yet, not instantiating SPD\n");
+		return;
+	}
+
+	switch (common_mem_type) {
+	case 0x13:	/* DDR2 */
+	case 0x18:	/* DDR3 */
+	case 0x1C:	/* LPDDR2 */
+	case 0x1D:	/* LPDDR3 */
+		name = "spd";
+		break;
+	case 0x1A:	/* DDR4 */
+	case 0x1E:	/* LPDDR4 */
+		name = "ee1004";
+		break;
+	default:
+		dev_info(&adap->dev,
+			 "Memory type 0x%02x not supported yet, not instantiating SPD\n",
+			 common_mem_type);
+		return;
+	}
+
+	/*
+	 * We don't know in which slots the memory modules are. We could
+	 * try to guess from the slot names, but that would be rather complex
+	 * and unreliable, so better probe all possible addresses until we
+	 * have found all memory modules.
+	 */
+	for (n = 0; n < slot_count && dimm_count; n++) {
+		struct i2c_board_info info;
+		unsigned short addr_list[2];
+
+		memset(&info, 0, sizeof(struct i2c_board_info));
+		strlcpy(info.type, name, I2C_NAME_SIZE);
+		addr_list[0] = 0x50 + n;
+		addr_list[1] = I2C_CLIENT_END;
+
+		if (i2c_new_probed_device(adap, &info, addr_list, NULL)) {
+			dev_info(&adap->dev,
+				 "Successfully instantiated SPD at 0x%hx\n",
+				 addr_list[0]);
+			dimm_count--;
+		}
+	}
+}
+EXPORT_SYMBOL_GPL(i2c_register_spd);
+#endif
+
 MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
 MODULE_DESCRIPTION("SMBus protocol extensions support");
 MODULE_LICENSE("GPL");
--- linux-5.5.orig/include/linux/i2c-smbus.h	2020-01-27 01:23:03.000000000 +0100
+++ linux-5.5/include/linux/i2c-smbus.h	2020-02-11 12:54:11.588866579 +0100
@@ -2,7 +2,7 @@
 /*
  * i2c-smbus.h - SMBus extensions to the I2C protocol
  *
- * Copyright (C) 2010 Jean Delvare <jdelvare@suse.de>
+ * Copyright (C) 2010-2019 Jean Delvare <jdelvare@suse.de>
  */
 
 #ifndef _LINUX_I2C_SMBUS_H
@@ -44,4 +44,10 @@ static inline int of_i2c_setup_smbus_ale
 }
 #endif
 
+#if IS_ENABLED(CONFIG_I2C_SMBUS) && IS_ENABLED(CONFIG_DMI)
+void i2c_register_spd(struct i2c_adapter *adap);
+#else
+static inline void i2c_register_spd(struct i2c_adapter *adap) { }
+#endif
+
 #endif /* _LINUX_I2C_SMBUS_H */


-- 
Jean Delvare
SUSE L3 Support

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 2/2] i2c: i801: Instantiate SPD EEPROMs automatically
  2020-03-16 10:22 [PATCH 1/2] i2c: smbus: Add a way to instantiate SPD EEPROMs automatically Jean Delvare
@ 2020-03-16 10:24 ` Jean Delvare
  2020-05-29 10:58   ` Wolfram Sang
  2020-05-29 10:58 ` [PATCH 1/2] i2c: smbus: Add a way to instantiate " Wolfram Sang
  1 sibling, 1 reply; 4+ messages in thread
From: Jean Delvare @ 2020-03-16 10:24 UTC (permalink / raw)
  To: Linux I2C; +Cc: Wolfram Sang

Call the function to instantiate SPD EEPROMs automatically on the
main SMBus controller.

Multiplexed SMBus systems are excluded for now as they are more
complex to handle.

Signed-off-by: Jean Delvare <jdelvare@suse.de>
Cc: Wolfram Sang <wsa@the-dreams.de> 
---
 drivers/i2c/busses/i2c-i801.c |    6 ++++++
 1 file changed, 6 insertions(+)

--- linux-5.5.orig/drivers/i2c/busses/i2c-i801.c	2020-03-16 11:11:57.463497502 +0100
+++ linux-5.5/drivers/i2c/busses/i2c-i801.c	2020-03-16 11:14:33.279262494 +0100
@@ -1318,6 +1318,12 @@ static void i801_probe_optional_slaves(s
 
 	if (is_dell_system_with_lis3lv02d())
 		register_dell_lis3lv02d_i2c_device(priv);
+
+	/* Instantiate SPD EEPROMs unless the SMBus is multiplexed */
+#if IS_ENABLED(CONFIG_I2C_MUX_GPIO)
+	if (!priv->mux_drvdata)
+#endif
+		i2c_register_spd(&priv->adapter);
 }
 #else
 static void __init input_apanel_init(void) {}

-- 
Jean Delvare
SUSE L3 Support

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/2] i2c: smbus: Add a way to instantiate SPD EEPROMs automatically
  2020-03-16 10:22 [PATCH 1/2] i2c: smbus: Add a way to instantiate SPD EEPROMs automatically Jean Delvare
  2020-03-16 10:24 ` [PATCH 2/2] i2c: i801: Instantiate " Jean Delvare
@ 2020-05-29 10:58 ` Wolfram Sang
  1 sibling, 0 replies; 4+ messages in thread
From: Wolfram Sang @ 2020-05-29 10:58 UTC (permalink / raw)
  To: Jean Delvare; +Cc: Linux I2C

[-- Attachment #1: Type: text/plain, Size: 424 bytes --]

On Mon, Mar 16, 2020 at 11:22:24AM +0100, Jean Delvare wrote:
> In simple cases we can instantiate SPD EEPROMs on the SMBus
> automatically. Start with just DDR2, DDR3 and DDR4 on x86 for now,
> and only for systems with no more than 4 memory slots. These
> limitations may be lifted later.
> 
> Signed-off-by: Jean Delvare <jdelvare@suse.de>
> Cc: Wolfram Sang <wsa@the-dreams.de>

Applied to for-next, thanks!


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 2/2] i2c: i801: Instantiate SPD EEPROMs automatically
  2020-03-16 10:24 ` [PATCH 2/2] i2c: i801: Instantiate " Jean Delvare
@ 2020-05-29 10:58   ` Wolfram Sang
  0 siblings, 0 replies; 4+ messages in thread
From: Wolfram Sang @ 2020-05-29 10:58 UTC (permalink / raw)
  To: Jean Delvare; +Cc: Linux I2C

[-- Attachment #1: Type: text/plain, Size: 380 bytes --]

On Mon, Mar 16, 2020 at 11:24:48AM +0100, Jean Delvare wrote:
> Call the function to instantiate SPD EEPROMs automatically on the
> main SMBus controller.
> 
> Multiplexed SMBus systems are excluded for now as they are more
> complex to handle.
> 
> Signed-off-by: Jean Delvare <jdelvare@suse.de>
> Cc: Wolfram Sang <wsa@the-dreams.de> 

Applied to for-next, thanks!


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-05-29 10:58 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-16 10:22 [PATCH 1/2] i2c: smbus: Add a way to instantiate SPD EEPROMs automatically Jean Delvare
2020-03-16 10:24 ` [PATCH 2/2] i2c: i801: Instantiate " Jean Delvare
2020-05-29 10:58   ` Wolfram Sang
2020-05-29 10:58 ` [PATCH 1/2] i2c: smbus: Add a way to instantiate " Wolfram Sang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).