linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Add Silicom Platform Driver
@ 2023-07-18 16:01 Henry Shi
  2023-07-18 16:40 ` Guenter Roeck
                   ` (2 more replies)
  0 siblings, 3 replies; 32+ messages in thread
From: Henry Shi @ 2023-07-18 16:01 UTC (permalink / raw)
  To: hbshi69, tglx, mingo, bp, dave.hansen, x86, hpa, hdegoede,
	markgross, jdelvare, linux, linux-kernel, platform-driver-x86,
	linux-hwmon
  Cc: hb_shi2003, henrys, wenw

The Silicom platform (silicom-platform) Linux driver for Swisscom
Business Box (Swisscom BB) as well as Cordoba family products is a 
software solution designed to facilitate the efficient management
and control of devices through the integration of various Linux
frameworks. This platform driver provides seamless support for
device management via the Linux LED framework, GPIO framework,
Hardware Monitoring (HWMON), and device attributes. The Silicom
platform driver's compatibility with these Linux frameworks allows
applications to access and control Cordoba family devices using
existing software that is compatible with these frameworks. This
compatibility simplifies the development process, reduces
dependencies on proprietary solutions, and promotes
interoperability with other Linux-based systems and software.

Signed-off-by: Henry Shi <henryshi2018@gmail.com>
---
 drivers/platform/x86/Kconfig            |   12 +
 drivers/platform/x86/Makefile           |    1 +
 drivers/platform/x86/silicom-platform.c | 1123 +++++++++++++++++++++++
 3 files changed, 1136 insertions(+)
 create mode 100644 drivers/platform/x86/silicom-platform.c

diff --git a/drivers/platform/x86/Kconfig b/drivers/platform/x86/Kconfig
index 22052031c719..8c0988c2b4ce 100644
--- a/drivers/platform/x86/Kconfig
+++ b/drivers/platform/x86/Kconfig
@@ -188,6 +188,18 @@ config ACER_WMI
 	  If you have an ACPI-WMI compatible Acer/ Wistron laptop, say Y or M
 	  here.
 
+
+config SILICOM_PLATFORM
+	tristate "Silicom Edge Networking device support"
+	depends on DMI
+	select LEDS_CLASS_MULTICOLOR
+	select GPIOLIB
+	help
+	  This option enables support for the LEDs/GPIO/etc downstream of the
+	  embedded controller on Silicom "Cordoba" hardware and derivatives.
+
+	  If you have a Silicom network appliance, say Y or M here.
+
 source "drivers/platform/x86/amd/Kconfig"
 
 config ADV_SWBUTTON
diff --git a/drivers/platform/x86/Makefile b/drivers/platform/x86/Makefile
index 2cafe51ec4d8..f2f5743a9e54 100644
--- a/drivers/platform/x86/Makefile
+++ b/drivers/platform/x86/Makefile
@@ -113,6 +113,7 @@ obj-$(CONFIG_SERIAL_MULTI_INSTANTIATE)	+= serial-multi-instantiate.o
 obj-$(CONFIG_MLX_PLATFORM)		+= mlx-platform.o
 obj-$(CONFIG_TOUCHSCREEN_DMI)		+= touchscreen_dmi.o
 obj-$(CONFIG_WIRELESS_HOTKEY)		+= wireless-hotkey.o
+obj-$(CONFIG_SILICOM_PLATFORM)          += silicom-platform.o
 obj-$(CONFIG_X86_ANDROID_TABLETS)	+= x86-android-tablets/
 
 # Intel uncore drivers
diff --git a/drivers/platform/x86/silicom-platform.c b/drivers/platform/x86/silicom-platform.c
new file mode 100644
index 000000000000..90431f733682
--- /dev/null
+++ b/drivers/platform/x86/silicom-platform.c
@@ -0,0 +1,1123 @@
+// SPDX-License-Identifier: GPL-2.0+
+//
+// silicom-platform.c - Silicom MEC170x platform driver
+//
+// Copyright (C) 2023 Henry Shi <henrys@silicom-usa.com>
+
+#include <linux/dmi.h>
+#include <linux/gpio/driver.h>
+#include <linux/init.h>
+#include <linux/ioport.h>
+#include <linux/io.h>
+#include <linux/kernel.h>
+#include <linux/led-class-multicolor.h>
+#include <linux/module.h>
+#include <linux/hwmon.h>
+#include <linux/mutex.h>
+#include <linux/platform_device.h>
+#include <linux/string.h>
+#include <linux/thermal.h>
+#include <linux/kobject.h>
+#include <linux/sysfs.h>
+
+#define MEC_ADDR ((mec_io_base) + 0x02)
+#define MEC_DATA(byte) ((mec_io_base) + 0x04 + (byte))
+#define EC_ADDR_LSB MEC_ADDR
+#define EC_ADDR_MSB ((mec_io_base) + 0x03)
+#define SILICOM_MEC_MAGIC 0x5a
+#define OFFSET_BIT_TO_CHANNEL(off, bit) ((((off) + 0x014) << 3) | (bit))
+#define CHANNEL_TO_OFFSET(chan) (((chan) >> 3) - 0x14)
+#define CHANNEL_TO_BIT(chan) ((chan) & 0x07)
+
+static DEFINE_MUTEX(mec_io_mutex);
+static int mec_io_base, mec_io_len;
+
+struct silicom_fan_control_data {
+	struct   device *hdev;
+	int      temp;
+	int      fan_speed;
+};
+
+static const struct hwmon_channel_info *silicom_fan_control_info[] = {
+	HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT | HWMON_F_LABEL),
+	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT | HWMON_T_LABEL),
+	NULL
+};
+
+struct silicom_device_control_data {
+	struct   device *my_dev;
+	int      efuse_status;
+	int      uc_version;
+	int      power_cycle;
+};
+static struct silicom_device_control_data my_dev_ctl;
+
+struct silicom_platform_info {
+	int io_base;
+	int io_len;
+	struct led_classdev_mc *led_info;
+	struct gpio_chip *gpiochip;
+	u8 *gpio_channels;
+	u16 ngpio;
+};
+
+static const char * const plat_0222_gpio_names[] = {
+	"AUTOM0_SFP_TX_FAULT",
+	"SLOT2_LED_OUT",
+	"SIM_M2_SLOT2_B_DET",
+	"SIM_M2_SLOT2_A_DET",
+	"SLOT1_LED_OUT",
+	"SIM_M2_SLOT1_B_DET",
+	"SIM_M2_SLOT1_A_DET",
+	"SLOT0_LED_OUT",
+	"WAN_SFP0_RX_LOS",
+	"WAN_SFP0_PRSNT_N",
+	"WAN_SFP0_TX_FAULT",
+	"AUTOM1_SFP_RX_LOS",
+	"AUTOM1_SFP_PRSNT_N",
+	"AUTOM1_SFP_TX_FAULT",
+	"AUTOM0_SFP_RX_LOS",
+	"AUTOM0_SFP_PRSNT_N",
+	"WAN_SFP1_RX_LOS",
+	"WAN_SFP1_PRSNT_N",
+	"WAN_SFP1_TX_FAULT",
+	"SIM_M2_SLOT1_MUX_SEL",
+	"W_DISABLE_M2_SLOT1_N",
+	"W_DISABLE_MPCIE_SLOT0_N",
+	"W_DISABLE_M2_SLOT0_N",
+	"BT_COMMAND_MODE",
+	"WAN_SFP1_TX_DISABLE",
+	"WAN_SFP0_TX_DISABLE",
+	"AUTOM1_SFP_TX_DISABLE",
+	"AUTOM0_SFP_TX_DISABLE",
+	"SIM_M2_SLOT2_MUX_SEL",
+	"W_DISABLE_M2_SLOT2_N",
+	"RST_CTL_M2_SLOT_1_N",
+	"RST_CTL_M2_SLOT_2_N",
+	"PM_USB_PWR_EN_BOT",
+	"PM_USB_PWR_EN_TOP",
+};
+
+static u8 plat_0222_gpio_channels[] = {
+	OFFSET_BIT_TO_CHANNEL(0x00, 0),
+	OFFSET_BIT_TO_CHANNEL(0x00, 1),
+	OFFSET_BIT_TO_CHANNEL(0x00, 2),
+	OFFSET_BIT_TO_CHANNEL(0x00, 3),
+	OFFSET_BIT_TO_CHANNEL(0x00, 4),
+	OFFSET_BIT_TO_CHANNEL(0x00, 5),
+	OFFSET_BIT_TO_CHANNEL(0x00, 6),
+	OFFSET_BIT_TO_CHANNEL(0x00, 7),
+	OFFSET_BIT_TO_CHANNEL(0x01, 0),
+	OFFSET_BIT_TO_CHANNEL(0x01, 1),
+	OFFSET_BIT_TO_CHANNEL(0x01, 2),
+	OFFSET_BIT_TO_CHANNEL(0x01, 3),
+	OFFSET_BIT_TO_CHANNEL(0x01, 4),
+	OFFSET_BIT_TO_CHANNEL(0x01, 5),
+	OFFSET_BIT_TO_CHANNEL(0x01, 6),
+	OFFSET_BIT_TO_CHANNEL(0x01, 7),
+	OFFSET_BIT_TO_CHANNEL(0x02, 0),
+	OFFSET_BIT_TO_CHANNEL(0x02, 1),
+	OFFSET_BIT_TO_CHANNEL(0x02, 2),
+	OFFSET_BIT_TO_CHANNEL(0x09, 0),
+	OFFSET_BIT_TO_CHANNEL(0x09, 1),
+	OFFSET_BIT_TO_CHANNEL(0x09, 2),
+	OFFSET_BIT_TO_CHANNEL(0x09, 3),
+	OFFSET_BIT_TO_CHANNEL(0x0a, 0),
+	OFFSET_BIT_TO_CHANNEL(0x0a, 1),
+	OFFSET_BIT_TO_CHANNEL(0x0a, 2),
+	OFFSET_BIT_TO_CHANNEL(0x0a, 3),
+	OFFSET_BIT_TO_CHANNEL(0x0a, 4),
+	OFFSET_BIT_TO_CHANNEL(0x0a, 5),
+	OFFSET_BIT_TO_CHANNEL(0x0a, 6),
+	OFFSET_BIT_TO_CHANNEL(0x0b, 0),
+	OFFSET_BIT_TO_CHANNEL(0x0b, 1),
+	OFFSET_BIT_TO_CHANNEL(0x0b, 2),
+	OFFSET_BIT_TO_CHANNEL(0x0b, 3),
+};
+
+static int silicom_gpio_get_direction(struct gpio_chip *gc, unsigned int offset);
+static int silicom_gpio_direction_input(struct gpio_chip *gc, unsigned int offset);
+static int silicom_gpio_direction_output(struct gpio_chip *gc, unsigned int offset, int value);
+static int silicom_gpio_get(struct gpio_chip *gc, unsigned int offset);
+static void silicom_gpio_set(struct gpio_chip *gc, unsigned int offset, int value);
+static void silicom_mec_led_mc_brightness_set(struct led_classdev *led_cdev,
+					      enum led_brightness brightness);
+static enum led_brightness silicom_mec_led_mc_brightness_get(struct led_classdev *led_cdev);
+static struct platform_device *silicom_platform_dev;
+static struct led_classdev_mc *silicom_led_info __initdata;
+static struct gpio_chip *silicom_gpiochip __initdata;
+static u8 *silicom_gpio_channels __initdata;
+static struct gpio_chip silicom_gpio_chip = {
+	.label = "silicom-gpio",
+	.get_direction = silicom_gpio_get_direction,
+	.direction_input = silicom_gpio_direction_input,
+	.direction_output = silicom_gpio_direction_output,
+	.get = silicom_gpio_get,
+	.set = silicom_gpio_set,
+	.base = -1,
+	.ngpio = ARRAY_SIZE(plat_0222_gpio_channels),
+	.names = plat_0222_gpio_names,
+	/* We're using a mutex to protect the indirect access, so we can sleep if the lock blocks */
+	.can_sleep = true,
+};
+
+static struct mc_subled plat_0222_wan_mc_subled_info[] __initdata = {
+	{
+		.color_index = LED_COLOR_ID_WHITE,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 7),
+	},
+	{
+		.color_index = LED_COLOR_ID_YELLOW,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 6),
+	},
+	{
+		.color_index = LED_COLOR_ID_RED,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 5),
+	},
+};
+
+static struct mc_subled plat_0222_sys_mc_subled_info[] __initdata = {
+	{
+		.color_index = LED_COLOR_ID_WHITE,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 4),
+	},
+	{
+		.color_index = LED_COLOR_ID_AMBER,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 3),
+	},
+	{
+		.color_index = LED_COLOR_ID_RED,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 2),
+	},
+};
+
+static struct mc_subled plat_0222_stat1_mc_subled_info[] __initdata = {
+	{
+		.color_index = LED_COLOR_ID_RED,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 1),
+	},
+	{
+		.color_index = LED_COLOR_ID_GREEN,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 0),
+	},
+	{
+		.color_index = LED_COLOR_ID_BLUE,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 7),
+	},
+	{
+		.color_index = LED_COLOR_ID_YELLOW,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 6),
+	},
+};
+
+static struct mc_subled plat_0222_stat2_mc_subled_info[] __initdata = {
+	{
+		.color_index = LED_COLOR_ID_RED,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 5),
+	},
+	{
+		.color_index = LED_COLOR_ID_GREEN,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 4),
+	},
+	{
+		.color_index = LED_COLOR_ID_BLUE,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 3),
+	},
+	{
+		.color_index = LED_COLOR_ID_YELLOW,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 2),
+	},
+};
+
+static struct mc_subled plat_0222_stat3_mc_subled_info[] __initdata = {
+	{
+		.color_index = LED_COLOR_ID_RED,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 1),
+	},
+	{
+		.color_index = LED_COLOR_ID_GREEN,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 0),
+	},
+	{
+		.color_index = LED_COLOR_ID_BLUE,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0e, 1),
+	},
+	{
+		.color_index = LED_COLOR_ID_YELLOW,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0e, 0),
+	},
+};
+
+static struct led_classdev_mc plat_0222_mc_led_info[] __initdata = {
+	{
+		.led_cdev = {
+			.name = "multicolor:wan",
+			.brightness = 0,
+			.max_brightness = 1,
+			.brightness_set = silicom_mec_led_mc_brightness_set,
+			.brightness_get = silicom_mec_led_mc_brightness_get,
+		},
+		.num_colors = ARRAY_SIZE(plat_0222_wan_mc_subled_info),
+		.subled_info = plat_0222_wan_mc_subled_info,
+	},
+	{
+		.led_cdev = {
+			.name = "multicolor:sys",
+			.brightness = 0,
+			.max_brightness = 1,
+			.brightness_set = silicom_mec_led_mc_brightness_set,
+			.brightness_get = silicom_mec_led_mc_brightness_get,
+		},
+		.num_colors = ARRAY_SIZE(plat_0222_sys_mc_subled_info),
+		.subled_info = plat_0222_sys_mc_subled_info,
+	},
+	{
+		.led_cdev = {
+			.name = "multicolor:stat1",
+			.brightness = 0,
+			.max_brightness = 1,
+			.brightness_set = silicom_mec_led_mc_brightness_set,
+			.brightness_get = silicom_mec_led_mc_brightness_get,
+		},
+		.num_colors = ARRAY_SIZE(plat_0222_stat1_mc_subled_info),
+		.subled_info = plat_0222_stat1_mc_subled_info,
+	},
+	{
+		.led_cdev = {
+			.name = "multicolor:stat2",
+			.brightness = 0,
+			.max_brightness = 1,
+			.brightness_set = silicom_mec_led_mc_brightness_set,
+			.brightness_get = silicom_mec_led_mc_brightness_get,
+		},
+		.num_colors = ARRAY_SIZE(plat_0222_stat2_mc_subled_info),
+		.subled_info = plat_0222_stat2_mc_subled_info,
+	},
+	{
+		.led_cdev = {
+			.name = "multicolor:stat3",
+			.brightness = 0,
+			.max_brightness = 1,
+			.brightness_set = silicom_mec_led_mc_brightness_set,
+			.brightness_get = silicom_mec_led_mc_brightness_get,
+		},
+		.num_colors = ARRAY_SIZE(plat_0222_stat3_mc_subled_info),
+		.subled_info = plat_0222_stat3_mc_subled_info,
+	},
+	{ },
+};
+
+static struct silicom_platform_info silicom_plat_0222_cordoba_info __initdata = {
+	.io_base = 0x800,
+	.io_len = 8,
+	.led_info = plat_0222_mc_led_info,
+	.gpiochip = &silicom_gpio_chip,
+	.gpio_channels = plat_0222_gpio_channels,
+	/* The original generic cordoba does not have the last 4 outputs of the plat_0222 BB variant,
+	 * the rest are the same, so use the same longer list, but ignore the last entries here
+	 */
+	.ngpio = ARRAY_SIZE(plat_0222_gpio_channels),
+
+};
+
+static struct mc_subled cordoba_fp_left_mc_subled_info[] __initdata = {
+	{
+		.color_index = LED_COLOR_ID_RED,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 6),
+	},
+	{
+		.color_index = LED_COLOR_ID_GREEN,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 5),
+	},
+	{
+		.color_index = LED_COLOR_ID_BLUE,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x09, 7),
+	},
+	{
+		.color_index = LED_COLOR_ID_AMBER,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x09, 4),
+	},
+};
+
+static struct mc_subled cordoba_fp_center_mc_subled_info[] __initdata = {
+	{
+		.color_index = LED_COLOR_ID_RED,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 7),
+	},
+	{
+		.color_index = LED_COLOR_ID_GREEN,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 4),
+	},
+	{
+		.color_index = LED_COLOR_ID_BLUE,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 3),
+	},
+	{
+		.color_index = LED_COLOR_ID_AMBER,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x09, 6),
+	},
+};
+
+static struct mc_subled cordoba_fp_right_mc_subled_info[] __initdata = {
+	{
+		.color_index = LED_COLOR_ID_RED,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 2),
+	},
+	{
+		.color_index = LED_COLOR_ID_GREEN,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 1),
+	},
+	{
+		.color_index = LED_COLOR_ID_BLUE,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 0),
+	},
+	{
+		.color_index = LED_COLOR_ID_AMBER,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x09, 5),
+	},
+};
+
+static struct led_classdev_mc cordoba_mc_led_info[] __initdata = {
+	{
+		.led_cdev = {
+			.name = "multicolor:fp_left",
+			.brightness = 0,
+			.max_brightness = 1,
+			.brightness_set = silicom_mec_led_mc_brightness_set,
+			.brightness_get = silicom_mec_led_mc_brightness_get,
+		},
+		.num_colors = ARRAY_SIZE(cordoba_fp_left_mc_subled_info),
+		.subled_info = cordoba_fp_left_mc_subled_info,
+	},
+	{
+		.led_cdev = {
+			.name = "multicolor:fp_center",
+			.brightness = 0,
+			.max_brightness = 1,
+			.brightness_set = silicom_mec_led_mc_brightness_set,
+			.brightness_get = silicom_mec_led_mc_brightness_get,
+		},
+		.num_colors = ARRAY_SIZE(cordoba_fp_center_mc_subled_info),
+		.subled_info = cordoba_fp_center_mc_subled_info,
+	},
+	{
+		.led_cdev = {
+			.name = "multicolor:fp_right",
+			.brightness = 0,
+			.max_brightness = 1,
+			.brightness_set = silicom_mec_led_mc_brightness_set,
+			.brightness_get = silicom_mec_led_mc_brightness_get,
+		},
+		.num_colors = ARRAY_SIZE(cordoba_fp_right_mc_subled_info),
+		.subled_info = cordoba_fp_right_mc_subled_info,
+	},
+	{ },
+};
+
+static struct silicom_platform_info silicom_generic_cordoba_info __initdata = {
+	.io_base = 0x800,
+	.io_len = 8,
+	.led_info = cordoba_mc_led_info,
+	.gpiochip = &silicom_gpio_chip,
+	.gpio_channels = plat_0222_gpio_channels,
+	.ngpio = ARRAY_SIZE(plat_0222_gpio_channels),
+};
+
+static struct platform_driver silicom_platform_driver = {
+	.driver = {
+		.name = "silicom-platform",
+	},
+};
+
+void lock_io_modules(void)
+{
+	mutex_lock(&mec_io_mutex);
+}
+EXPORT_SYMBOL(lock_io_modules);
+
+void unlock_io_modules(void)
+{
+	mutex_unlock(&mec_io_mutex);
+}
+EXPORT_SYMBOL(unlock_io_modules);
+
+static ssize_t efuse_status_show(struct device *dev, struct device_attribute *attr,
+		      char *buf)
+{
+	u32 reg;
+	u32 bank = 0;
+	u32 offset = 0x28;
+	u32 byte_pos = 0;
+
+	mutex_lock(&mec_io_mutex);
+	/* Select memory region */
+	outb(bank, EC_ADDR_MSB);
+	outb(offset, EC_ADDR_LSB);
+
+	/* Get current date from the address */
+	reg = inl(MEC_DATA(byte_pos));
+	mutex_unlock(&mec_io_mutex);
+
+	my_dev_ctl.efuse_status = reg & 0x1;
+
+	return sprintf(buf, "%d\n", my_dev_ctl.efuse_status);
+}
+
+static ssize_t uc_version_show(struct device *dev,
+			       struct device_attribute *attr,
+			       char *buf)
+{
+	u32 reg;
+	u32 bank = 0;
+	u32 offset = 0x0;
+	u32 byte_pos = 0;
+	int uc_version;
+
+	mutex_lock(&mec_io_mutex);
+	outb(bank, EC_ADDR_MSB);
+	outb(offset, EC_ADDR_LSB);
+
+	reg = inl(MEC_DATA(byte_pos));
+	mutex_unlock(&mec_io_mutex);
+
+	uc_version = (reg >> 8) & 0xFF;
+	if (uc_version >= 64 && uc_version < 128) {
+		uc_version = uc_version - 64;
+		if (uc_version < 10)
+			uc_version = 100 + uc_version;
+		else
+			uc_version = 100 + 10 * (uc_version / 10) + uc_version % 10;
+	} else if (uc_version >= 128 && uc_version < 192) {
+		uc_version = uc_version - 128;
+		if (uc_version < 10)
+			uc_version = 200 + uc_version;
+		else
+			uc_version = 200 + 10 * (uc_version / 10) + uc_version % 10;
+	}
+	my_dev_ctl.uc_version = uc_version;
+	return sprintf(buf, "%d\n", my_dev_ctl.uc_version);
+}
+
+static ssize_t power_cycle_show(struct device *dev,
+				struct device_attribute *attr,
+				char *buf)
+{
+	return sprintf(buf, "%d\n", my_dev_ctl.power_cycle);
+}
+
+static void powercycle_uc(void)
+{
+	u32 bank = 0;
+	u32 offset = 0x24;
+	u32 byte_pos = 0;
+
+	mutex_lock(&mec_io_mutex);
+	/* Select memory region */
+	outb(bank, EC_ADDR_MSB);
+	outb(offset, EC_ADDR_LSB);
+
+	/* Set to 1 for current date from the address */
+	outb(1, MEC_DATA(byte_pos));
+	mutex_unlock(&mec_io_mutex);
+}
+
+static ssize_t power_cycle_store(struct device *dev, struct device_attribute *attr,
+				 const char *buf, size_t count)
+{
+	if (sscanf(buf, "%du", &my_dev_ctl.power_cycle) != 1) {
+		dev_err(dev, "Failed to read power_cycle\n");
+		return -EINVAL;
+	}
+	if (my_dev_ctl.power_cycle > 0)
+		powercycle_uc();
+
+	return count;
+}
+
+static struct device_attribute my_dev_attr[] = {
+	{
+		.attr = {.name = "efuse_status", .mode = 0644},
+		.show = efuse_status_show,
+		.store = NULL
+	},
+	{
+		.attr = {.name = "uc_version", .mode = 0644},
+		.show = uc_version_show,
+		.store = NULL
+	},
+	{
+		.attr = {.name = "power_cycle", .mode = 0644},
+		.show = power_cycle_show,
+		.store = power_cycle_store
+	},
+};
+
+static int silicom_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
+{
+	u8 *channels = gpiochip_get_data(gc);
+
+	/* Input registers have offsets between [0x00, 0x07] */
+	if (CHANNEL_TO_OFFSET(channels[offset]) < 0x08)
+		return GPIO_LINE_DIRECTION_IN;
+
+	return GPIO_LINE_DIRECTION_OUT;
+}
+
+static int silicom_gpio_direction_input(struct gpio_chip *gc, unsigned int offset)
+{
+	int direction = silicom_gpio_get_direction(gc, offset);
+
+	return direction == GPIO_LINE_DIRECTION_IN ? 0 : -EINVAL;
+}
+
+static void silicom_gpio_set(struct gpio_chip *gc, unsigned int offset, int value)
+{
+	u8 *channels = gpiochip_get_data(gc);
+	int direction = silicom_gpio_get_direction(gc, offset);
+	int channel = channels[offset];
+	u8 reg;
+
+	if (direction == GPIO_LINE_DIRECTION_IN)
+		return;
+
+	mutex_lock(&mec_io_mutex);
+	/* Get the dword offset from the channel */
+	outb((channel >> 3) & 0xfc, MEC_ADDR);
+
+	/* Get the current register */
+	reg = inb(MEC_DATA((channel >> 3) & 0x03));
+	if (value == 0)
+		reg &= ~(1 << (channel & 0x7));
+	else if (value > 0)
+		reg |= 1 << (channel & 0x7);
+	else
+		pr_err("Invalid GPIO value: %d\n", value);
+	outb(reg, MEC_DATA((channel >> 3) & 0x03));
+	mutex_unlock(&mec_io_mutex);
+}
+
+static int silicom_gpio_direction_output(struct gpio_chip *gc, unsigned int offset, int value)
+{
+	int direction = silicom_gpio_get_direction(gc, offset);
+
+	if (direction == GPIO_LINE_DIRECTION_IN)
+		return -EINVAL;
+
+	silicom_gpio_set(gc, offset, value);
+
+	return 0;
+}
+
+static int silicom_gpio_get(struct gpio_chip *gc, unsigned int offset)
+{
+	u8 *channels = gpiochip_get_data(gc);
+	int channel = channels[offset];
+	u8 reg;
+
+	mutex_lock(&mec_io_mutex);
+	/* Get the dword offset from the channel */
+	outb((channel >> 3) & 0xfc, MEC_ADDR);
+
+	/* Get the current register */
+	reg = inb(MEC_DATA((channel >> 3) & 0x03));
+	mutex_unlock(&mec_io_mutex);
+
+	return (reg >> (channel & 0x7)) & 0x01;
+}
+
+static int __init silicom_mc_leds_register(struct device *dev,
+					   const struct led_classdev_mc *mc_leds)
+{
+	struct led_classdev_mc *led;
+	int i, err;
+
+	for (i = 0; mc_leds[i].led_cdev.name; i++) {
+		/* allocate and copy data from the init constansts */
+		led = devm_kzalloc(dev, sizeof(struct led_classdev_mc), GFP_KERNEL);
+		if (IS_ERR_OR_NULL(led)) {
+			dev_err(dev, "Failed to alloc led_classdev_mc[%d]: %ld\n", i, PTR_ERR(led));
+			return -ENOMEM;
+		}
+		memcpy(led, &mc_leds[i], sizeof(*led));
+
+		led->subled_info = devm_kzalloc(dev, led->num_colors * sizeof(struct mc_subled),
+						GFP_KERNEL);
+		if (IS_ERR_OR_NULL(led->subled_info)) {
+			dev_err(dev, "Failed to alloc subled_info[%d]: %ld\n",
+				i, PTR_ERR(led->subled_info));
+			return -ENOMEM;
+		}
+		memcpy(led->subled_info, mc_leds[i].subled_info,
+			led->num_colors * sizeof(struct mc_subled));
+
+		err = devm_led_classdev_multicolor_register(dev, led);
+		if (err) {
+			dev_err(dev, "Failed to register[%d]: %d\n", i, err);
+			return err;
+		}
+	}
+
+	return 0;
+}
+
+static void silicom_mec_led_set(int channel, int on)
+{
+	u8 reg;
+
+	mutex_lock(&mec_io_mutex);
+	/* Get the dword offset from the channel */
+	outb((channel >> 3) & 0xfc, MEC_ADDR);
+
+	/* Get the current LED settings */
+	reg = inb(MEC_DATA((channel >> 3) & 0x03));
+
+	/* Outputs are active low, so clear the bit for on, or set it for off */
+	if (on)
+		reg &= ~(1 << (channel & 0x7));
+	else
+		reg |= 1 << (channel & 0x7);
+
+	/* Write back the updated register */
+	outb(reg, MEC_DATA((channel >> 3) & 0x03));
+
+	mutex_unlock(&mec_io_mutex);
+}
+
+static void silicom_mec_led_mc_brightness_set(struct led_classdev *led_cdev,
+					      enum led_brightness brightness)
+{
+	struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(led_cdev);
+	int i;
+
+	led_mc_calc_color_components(mc_cdev, brightness);
+
+	for (i = 0; i < mc_cdev->num_colors; i++) {
+		silicom_mec_led_set(mc_cdev->subled_info[i].channel,
+				    mc_cdev->subled_info[i].brightness);
+	}
+}
+
+static enum led_brightness silicom_mec_led_get(int channel)
+{
+	u8 reg;
+
+	mutex_lock(&mec_io_mutex);
+	/* Get the dword offset of the register for this LED from the channel */
+	outb((channel >> 3) & 0xfc, MEC_ADDR);
+	/* Get the current LED settings */
+	reg = inb(MEC_DATA((channel >> 3) & 0x03));
+	mutex_unlock(&mec_io_mutex);
+
+	/* Outputs are active low */
+	return reg & (1 << (channel & 0x7)) ? LED_OFF : LED_ON;
+}
+
+static enum led_brightness silicom_mec_led_mc_brightness_get(struct led_classdev *led_cdev)
+{
+	struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(led_cdev);
+	enum led_brightness brightness = LED_OFF;
+	int i;
+
+	for (i = 0; i < mc_cdev->num_colors; i++) {
+		mc_cdev->subled_info[i].brightness =
+			silicom_mec_led_get(mc_cdev->subled_info[i].channel);
+
+		/* Mark the overall brightness as LED_ON if any of the subleds are on */
+		if (mc_cdev->subled_info[i].brightness != LED_OFF)
+			brightness = LED_ON;
+	}
+
+	return brightness;
+}
+
+
+static u32 rpm_get(void)
+{
+	u32 reg;
+	u32 bank = 0;
+	u32 offset = 0xc;
+	u32 byte_pos = 0;
+
+	mutex_lock(&mec_io_mutex);
+	/* Select memory region */
+	outb(bank, EC_ADDR_MSB);
+	outb(offset, EC_ADDR_LSB);
+	/* Get current date from the address */
+	reg = inw(MEC_DATA(byte_pos));
+	mutex_unlock(&mec_io_mutex);
+
+	return reg;
+}
+
+static u32 temp_get(void)
+{
+	u32 reg;
+	u32 bank = 0;
+	u32 offset = 0xc;
+	u32 byte_pos = 0;
+
+	mutex_lock(&mec_io_mutex);
+	/* Select memory region */
+	outb(bank, EC_ADDR_MSB);
+	outb(offset, EC_ADDR_LSB);
+	/* Get current date from the address */
+	reg = inl(MEC_DATA(byte_pos));
+	mutex_unlock(&mec_io_mutex);
+
+	return (reg >> 16) / 10;
+}
+
+static umode_t silicom_fan_control_fan_is_visible(const u32 attr)
+{
+	switch (attr) {
+	case hwmon_fan_input:
+	case hwmon_fan_label:
+		return 0444;
+	default:
+		return 0;
+	}
+}
+
+static umode_t silicom_fan_control_temp_is_visible(const u32 attr)
+{
+	switch (attr) {
+	case hwmon_temp_input:
+	case hwmon_temp_label:
+		return 0444;
+	default:
+		return 0;
+	}
+}
+
+static int silicom_fan_control_read_fan(struct device *dev, u32 attr, long *val)
+{
+	struct silicom_fan_control_data *ctl = dev_get_drvdata(dev);
+
+	switch (attr) {
+	case hwmon_fan_input:
+		ctl->fan_speed = rpm_get();
+		*val = ctl->fan_speed;
+		return 0;
+	default:
+		return -EOPNOTSUPP;
+	}
+}
+
+static int silicom_fan_control_read_temp(struct device *dev, u32 attr, long *val)
+{
+	struct silicom_fan_control_data *ctl = dev_get_drvdata(dev);
+
+	switch (attr) {
+	case hwmon_temp_input:
+		ctl->temp = temp_get();
+		*val = ctl->temp;
+		return 0;
+	default:
+		return -EOPNOTSUPP;
+	}
+}
+
+static umode_t silicom_fan_control_is_visible(const void *data,
+					  enum hwmon_sensor_types type,
+					  u32 attr, int channel)
+{
+	switch (type) {
+	case hwmon_fan:
+		return silicom_fan_control_fan_is_visible(attr);
+	case hwmon_temp:
+		return silicom_fan_control_temp_is_visible(attr);
+	default:
+		return 0;
+	}
+}
+
+static int silicom_fan_control_read(struct device *dev, enum hwmon_sensor_types type,
+				    u32 attr, int channel, long *val)
+{
+	switch (type) {
+	case hwmon_fan:
+		return silicom_fan_control_read_fan(dev, attr, val);
+	case hwmon_temp:
+		return silicom_fan_control_read_temp(dev, attr, val);
+	default:
+		return -EOPNOTSUPP;
+	}
+}
+
+static int silicom_fan_control_read_labels(struct device *dev, enum hwmon_sensor_types type,
+					   u32 attr, int channel, const char **str)
+{
+	switch (type) {
+	case hwmon_fan:
+		*str = "Fan Speed (RPM)";
+		return 0;
+	case hwmon_temp:
+		*str = "Thermostat Sensor";
+		return 0;
+	default:
+		return -EOPNOTSUPP;
+	}
+}
+
+static int silicom_fan_control_write(struct device *dev, enum hwmon_sensor_types type,
+				     u32 attr, int channel, long val)
+{
+	return 0;
+}
+
+static const struct hwmon_ops silicom_fan_control_hwmon_ops = {
+	.is_visible = silicom_fan_control_is_visible,
+	.read = silicom_fan_control_read,
+	.write = silicom_fan_control_write,
+	.read_string = silicom_fan_control_read_labels,
+};
+
+static const struct hwmon_chip_info silicom_chip_info = {
+	.ops = &silicom_fan_control_hwmon_ops,
+	.info = silicom_fan_control_info,
+};
+
+static int __init silicom_platform_probe(struct platform_device *device)
+{
+	int i, err;
+	u8 magic, ver;
+	struct silicom_fan_control_data *ctl;
+	const char *name = "Silocom_Fan_Monitor";
+	const char *dev_name = "Silicom_platform";
+
+	mec_io_base = 0x0800;
+	mec_io_len = 8;
+	if (!devm_request_region(&device->dev, mec_io_base, mec_io_len, "mec")) {
+		dev_err(&device->dev, "couldn't reserve MEC io ports\n");
+		return -EBUSY;
+	}
+
+	/* Sanity check magic number read for EC */
+	outb(0x00, MEC_ADDR);
+	magic = inb(MEC_DATA(0));
+	ver = inb(MEC_DATA(1));
+	dev_dbg(&device->dev, "EC magic 0x%02x, version 0x%02x\n", magic, ver);
+
+	if (magic != SILICOM_MEC_MAGIC) {
+		dev_err(&device->dev, "Bad EC magic 0x%02x!\n", magic);
+		return -ENODEV;
+	}
+
+	if (silicom_led_info) {
+		err = silicom_mc_leds_register(&device->dev, silicom_led_info);
+		if (err) {
+			dev_err(&device->dev, "Failed to register LEDs\n");
+			return err;
+		}
+	}
+
+	if (silicom_gpiochip) {
+		err = devm_gpiochip_add_data(&device->dev, silicom_gpiochip, silicom_gpio_channels);
+		if (err) {
+			dev_err(&device->dev, "Failed to register gpiochip: %d\n", err);
+			return err;
+		}
+	}
+
+	ctl = devm_kzalloc(&device->dev, sizeof(*ctl), GFP_KERNEL);
+	if (!ctl)
+		return -ENOMEM;
+
+	ctl->hdev = devm_hwmon_device_register_with_info(&device->dev, name, ctl,
+				&silicom_chip_info, NULL);
+
+	my_dev_ctl.my_dev = root_device_register(dev_name);
+	for (i = 0; i < ARRAY_SIZE(my_dev_attr); i++) {
+		err = sysfs_create_file(&my_dev_ctl.my_dev->kobj, &my_dev_attr[i].attr);
+		if (err) {
+			pr_debug("failed to create the foo file in /sys/devices/Silicom_platform\n");
+			break;
+		}
+	}
+
+	return err;
+}
+
+static int __init silicom_platform_info_init(const struct dmi_system_id *id)
+{
+	struct silicom_platform_info *info = id->driver_data;
+
+	dev_info(&silicom_platform_dev->dev, "Detected %s\n", id->ident);
+
+	mec_io_base = info->io_base;
+	mec_io_len = info->io_len;
+	silicom_led_info = info->led_info;
+	silicom_gpio_channels = info->gpio_channels;
+	silicom_gpiochip = info->gpiochip;
+	if (silicom_gpiochip)
+		silicom_gpiochip->ngpio = info->ngpio;
+
+	return 1;
+}
+
+static const struct dmi_system_id silicom_dmi_ids[] __initconst = {
+	{
+		.callback = silicom_platform_info_init,
+		.ident = "Silicom Cordoba (Generic)",
+		.matches = {
+			DMI_MATCH(DMI_BOARD_VENDOR, "Silicom"),
+			DMI_MATCH(DMI_BOARD_NAME, "80300-0214-G"),
+		},
+		.driver_data = &silicom_generic_cordoba_info,
+	},
+	{
+		.callback = silicom_platform_info_init,
+		.ident = "Silicom Cordoba (Generic)",
+		.matches = {
+			DMI_MATCH(DMI_BOARD_VENDOR, "Silicom"),
+			DMI_MATCH(DMI_BOARD_NAME, "80500-0214-G"),
+		},
+		.driver_data = &silicom_generic_cordoba_info,
+	},
+	{
+		 .callback = silicom_platform_info_init,
+		 .ident = "Silicom Cordoba (plat_0222)",
+		 .matches = {
+		       DMI_MATCH(DMI_BOARD_VENDOR, "Silicom"),
+		       DMI_MATCH(DMI_BOARD_NAME, "80300-0222-G"),
+		 },
+		.driver_data = &silicom_plat_0222_cordoba_info,
+	},
+	{ },
+};
+
+static int __init silicom_platform_init(void)
+{
+	struct device *dev;
+	int err;
+
+	/* register a platform device to act as the parent for LEDS, etc. */
+	silicom_platform_dev = platform_device_register_simple("silicom-platform", -1, NULL, 0);
+	if (IS_ERR(silicom_platform_dev)) {
+		err = PTR_ERR(silicom_platform_dev);
+		pr_err("failed to register silicom-platform device: %d\n", err);
+		goto silicom_init_register_err;
+	}
+	dev = &silicom_platform_dev->dev;
+
+	err = dmi_check_system(silicom_dmi_ids);
+	if (err == 0) {
+		dev_err(dev, "No DMI match for this platform\n");
+		err = -ENODEV;
+		goto silicom_init_probe_err;
+	}
+
+	/* Directly probe the platform driver in init since this isn't a
+	 * hotpluggable device.  That means we don't need to register a driver
+	 * that needs to wait around in memory on the chance a matching device
+	 * would get added.  Instead run once in __init so that we can free all
+	 * those resources when the __init region is wiped
+	 */
+	err = platform_driver_probe(&silicom_platform_driver, silicom_platform_probe);
+	if (err) {
+		dev_err(dev, "Failed to probe platform driver %d\n", err);
+		goto silicom_init_probe_err;
+	}
+
+	return 0;
+
+silicom_init_probe_err:
+	if (silicom_platform_dev) {
+		platform_device_unregister(silicom_platform_dev);
+		silicom_platform_dev = NULL;
+	}
+	if (my_dev_ctl.my_dev) {
+		root_device_unregister(my_dev_ctl.my_dev);
+		my_dev_ctl.my_dev = NULL;
+	}
+
+silicom_init_register_err:
+	return err;
+}
+
+static void __exit silicom_platform_exit(void)
+{
+	int i;
+
+	if (silicom_platform_dev) {
+		platform_device_unregister(silicom_platform_dev);
+		platform_driver_unregister(&silicom_platform_driver);
+	}
+
+	if (my_dev_ctl.my_dev) {
+		for (i = 0; i < ARRAY_SIZE(my_dev_attr); i++)
+			sysfs_remove_file(&my_dev_ctl.my_dev->kobj, &my_dev_attr[i].attr);
+		root_device_unregister(my_dev_ctl.my_dev);
+	}
+	mutex_destroy(&mec_io_mutex);
+}
+
+module_init(silicom_platform_init);
+module_exit(silicom_platform_exit);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Henry Shi <henrys@silicom-usa.com>");
+MODULE_DESCRIPTION("Platform driver for Silicom network appliances");
+
+MODULE_DEVICE_TABLE(dmi, silicom_dmi_ids);
+
-- 
2.21.3


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

* Re: [PATCH] Add Silicom Platform Driver
  2023-07-18 16:01 [PATCH] Add Silicom Platform Driver Henry Shi
@ 2023-07-18 16:40 ` Guenter Roeck
  2023-07-25 20:29   ` Huibin Shi
  2023-07-19 12:13 ` Ilpo Järvinen
  2023-07-25 21:02 ` Christophe JAILLET
  2 siblings, 1 reply; 32+ messages in thread
From: Guenter Roeck @ 2023-07-18 16:40 UTC (permalink / raw)
  To: Henry Shi, hbshi69, tglx, mingo, bp, dave.hansen, x86, hpa,
	hdegoede, markgross, jdelvare, linux-kernel, platform-driver-x86,
	linux-hwmon
  Cc: hb_shi2003, henrys, wenw

On 7/18/23 09:01, Henry Shi wrote:
> The Silicom platform (silicom-platform) Linux driver for Swisscom
> Business Box (Swisscom BB) as well as Cordoba family products is a
> software solution designed to facilitate the efficient management
> and control of devices through the integration of various Linux
> frameworks. This platform driver provides seamless support for
> device management via the Linux LED framework, GPIO framework,
> Hardware Monitoring (HWMON), and device attributes. The Silicom
> platform driver's compatibility with these Linux frameworks allows
> applications to access and control Cordoba family devices using
> existing software that is compatible with these frameworks. This
> compatibility simplifies the development process, reduces
> dependencies on proprietary solutions, and promotes
> interoperability with other Linux-based systems and software.
> 
> Signed-off-by: Henry Shi <henryshi2018@gmail.com>
> ---
>   drivers/platform/x86/Kconfig            |   12 +
>   drivers/platform/x86/Makefile           |    1 +
>   drivers/platform/x86/silicom-platform.c | 1123 +++++++++++++++++++++++
>   3 files changed, 1136 insertions(+)
>   create mode 100644 drivers/platform/x86/silicom-platform.c
> 
> diff --git a/drivers/platform/x86/Kconfig b/drivers/platform/x86/Kconfig
> index 22052031c719..8c0988c2b4ce 100644
> --- a/drivers/platform/x86/Kconfig
> +++ b/drivers/platform/x86/Kconfig
> @@ -188,6 +188,18 @@ config ACER_WMI
>   	  If you have an ACPI-WMI compatible Acer/ Wistron laptop, say Y or M
>   	  here.
>   
> +
> +config SILICOM_PLATFORM
> +	tristate "Silicom Edge Networking device support"
> +	depends on DMI
> +	select LEDS_CLASS_MULTICOLOR
> +	select GPIOLIB
> +	help
> +	  This option enables support for the LEDs/GPIO/etc downstream of the
> +	  embedded controller on Silicom "Cordoba" hardware and derivatives.
> +
> +	  If you have a Silicom network appliance, say Y or M here.
> +
>   source "drivers/platform/x86/amd/Kconfig"
>   
>   config ADV_SWBUTTON
> diff --git a/drivers/platform/x86/Makefile b/drivers/platform/x86/Makefile
> index 2cafe51ec4d8..f2f5743a9e54 100644
> --- a/drivers/platform/x86/Makefile
> +++ b/drivers/platform/x86/Makefile
> @@ -113,6 +113,7 @@ obj-$(CONFIG_SERIAL_MULTI_INSTANTIATE)	+= serial-multi-instantiate.o
>   obj-$(CONFIG_MLX_PLATFORM)		+= mlx-platform.o
>   obj-$(CONFIG_TOUCHSCREEN_DMI)		+= touchscreen_dmi.o
>   obj-$(CONFIG_WIRELESS_HOTKEY)		+= wireless-hotkey.o
> +obj-$(CONFIG_SILICOM_PLATFORM)          += silicom-platform.o
>   obj-$(CONFIG_X86_ANDROID_TABLETS)	+= x86-android-tablets/
>   
>   # Intel uncore drivers
> diff --git a/drivers/platform/x86/silicom-platform.c b/drivers/platform/x86/silicom-platform.c
> new file mode 100644
> index 000000000000..90431f733682
> --- /dev/null
> +++ b/drivers/platform/x86/silicom-platform.c
> @@ -0,0 +1,1123 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +//
> +// silicom-platform.c - Silicom MEC170x platform driver
> +//
> +// Copyright (C) 2023 Henry Shi <henrys@silicom-usa.com>
> +
> +#include <linux/dmi.h>
> +#include <linux/gpio/driver.h>
> +#include <linux/init.h>
> +#include <linux/ioport.h>
> +#include <linux/io.h>
> +#include <linux/kernel.h>
> +#include <linux/led-class-multicolor.h>
> +#include <linux/module.h>
> +#include <linux/hwmon.h>
> +#include <linux/mutex.h>
> +#include <linux/platform_device.h>
> +#include <linux/string.h>
> +#include <linux/thermal.h>

What is this include for ?

> +#include <linux/kobject.h>
> +#include <linux/sysfs.h>
> +
> +#define MEC_ADDR ((mec_io_base) + 0x02)
> +#define MEC_DATA(byte) ((mec_io_base) + 0x04 + (byte))
> +#define EC_ADDR_LSB MEC_ADDR
> +#define EC_ADDR_MSB ((mec_io_base) + 0x03)
> +#define SILICOM_MEC_MAGIC 0x5a
> +#define OFFSET_BIT_TO_CHANNEL(off, bit) ((((off) + 0x014) << 3) | (bit))
> +#define CHANNEL_TO_OFFSET(chan) (((chan) >> 3) - 0x14)
> +#define CHANNEL_TO_BIT(chan) ((chan) & 0x07)
> +
> +static DEFINE_MUTEX(mec_io_mutex);
> +static int mec_io_base, mec_io_len;

mec_io_base is always 0x0800 and mec_io_len is always 0x8.
Why all that complexity with assigning values to those static
variables instead of just using defines ?

> +
> +struct silicom_fan_control_data {
> +	struct   device *hdev;
> +	int      temp;
> +	int      fan_speed;
> +};

I do not see the point of having this data structure. Values are
only written into it and never read.

> +
> +static const struct hwmon_channel_info *silicom_fan_control_info[] = {
> +	HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT | HWMON_F_LABEL),
> +	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT | HWMON_T_LABEL),
> +	NULL
> +};
> +
> +struct silicom_device_control_data {
> +	struct   device *my_dev;
> +	int      efuse_status;
> +	int      uc_version;
> +	int      power_cycle;
> +};
> +static struct silicom_device_control_data my_dev_ctl;
> +
> +struct silicom_platform_info {
> +	int io_base;
> +	int io_len;
> +	struct led_classdev_mc *led_info;
> +	struct gpio_chip *gpiochip;
> +	u8 *gpio_channels;
> +	u16 ngpio;
> +};
> +
> +static const char * const plat_0222_gpio_names[] = {
> +	"AUTOM0_SFP_TX_FAULT",
> +	"SLOT2_LED_OUT",
> +	"SIM_M2_SLOT2_B_DET",
> +	"SIM_M2_SLOT2_A_DET",
> +	"SLOT1_LED_OUT",
> +	"SIM_M2_SLOT1_B_DET",
> +	"SIM_M2_SLOT1_A_DET",
> +	"SLOT0_LED_OUT",
> +	"WAN_SFP0_RX_LOS",
> +	"WAN_SFP0_PRSNT_N",
> +	"WAN_SFP0_TX_FAULT",
> +	"AUTOM1_SFP_RX_LOS",
> +	"AUTOM1_SFP_PRSNT_N",
> +	"AUTOM1_SFP_TX_FAULT",
> +	"AUTOM0_SFP_RX_LOS",
> +	"AUTOM0_SFP_PRSNT_N",
> +	"WAN_SFP1_RX_LOS",
> +	"WAN_SFP1_PRSNT_N",
> +	"WAN_SFP1_TX_FAULT",
> +	"SIM_M2_SLOT1_MUX_SEL",
> +	"W_DISABLE_M2_SLOT1_N",
> +	"W_DISABLE_MPCIE_SLOT0_N",
> +	"W_DISABLE_M2_SLOT0_N",
> +	"BT_COMMAND_MODE",
> +	"WAN_SFP1_TX_DISABLE",
> +	"WAN_SFP0_TX_DISABLE",
> +	"AUTOM1_SFP_TX_DISABLE",
> +	"AUTOM0_SFP_TX_DISABLE",
> +	"SIM_M2_SLOT2_MUX_SEL",
> +	"W_DISABLE_M2_SLOT2_N",
> +	"RST_CTL_M2_SLOT_1_N",
> +	"RST_CTL_M2_SLOT_2_N",
> +	"PM_USB_PWR_EN_BOT",
> +	"PM_USB_PWR_EN_TOP",
> +};
> +
> +static u8 plat_0222_gpio_channels[] = {
> +	OFFSET_BIT_TO_CHANNEL(0x00, 0),
> +	OFFSET_BIT_TO_CHANNEL(0x00, 1),
> +	OFFSET_BIT_TO_CHANNEL(0x00, 2),
> +	OFFSET_BIT_TO_CHANNEL(0x00, 3),
> +	OFFSET_BIT_TO_CHANNEL(0x00, 4),
> +	OFFSET_BIT_TO_CHANNEL(0x00, 5),
> +	OFFSET_BIT_TO_CHANNEL(0x00, 6),
> +	OFFSET_BIT_TO_CHANNEL(0x00, 7),
> +	OFFSET_BIT_TO_CHANNEL(0x01, 0),
> +	OFFSET_BIT_TO_CHANNEL(0x01, 1),
> +	OFFSET_BIT_TO_CHANNEL(0x01, 2),
> +	OFFSET_BIT_TO_CHANNEL(0x01, 3),
> +	OFFSET_BIT_TO_CHANNEL(0x01, 4),
> +	OFFSET_BIT_TO_CHANNEL(0x01, 5),
> +	OFFSET_BIT_TO_CHANNEL(0x01, 6),
> +	OFFSET_BIT_TO_CHANNEL(0x01, 7),
> +	OFFSET_BIT_TO_CHANNEL(0x02, 0),
> +	OFFSET_BIT_TO_CHANNEL(0x02, 1),
> +	OFFSET_BIT_TO_CHANNEL(0x02, 2),
> +	OFFSET_BIT_TO_CHANNEL(0x09, 0),
> +	OFFSET_BIT_TO_CHANNEL(0x09, 1),
> +	OFFSET_BIT_TO_CHANNEL(0x09, 2),
> +	OFFSET_BIT_TO_CHANNEL(0x09, 3),
> +	OFFSET_BIT_TO_CHANNEL(0x0a, 0),
> +	OFFSET_BIT_TO_CHANNEL(0x0a, 1),
> +	OFFSET_BIT_TO_CHANNEL(0x0a, 2),
> +	OFFSET_BIT_TO_CHANNEL(0x0a, 3),
> +	OFFSET_BIT_TO_CHANNEL(0x0a, 4),
> +	OFFSET_BIT_TO_CHANNEL(0x0a, 5),
> +	OFFSET_BIT_TO_CHANNEL(0x0a, 6),
> +	OFFSET_BIT_TO_CHANNEL(0x0b, 0),
> +	OFFSET_BIT_TO_CHANNEL(0x0b, 1),
> +	OFFSET_BIT_TO_CHANNEL(0x0b, 2),
> +	OFFSET_BIT_TO_CHANNEL(0x0b, 3),
> +};
> +
> +static int silicom_gpio_get_direction(struct gpio_chip *gc, unsigned int offset);
> +static int silicom_gpio_direction_input(struct gpio_chip *gc, unsigned int offset);
> +static int silicom_gpio_direction_output(struct gpio_chip *gc, unsigned int offset, int value);
> +static int silicom_gpio_get(struct gpio_chip *gc, unsigned int offset);
> +static void silicom_gpio_set(struct gpio_chip *gc, unsigned int offset, int value);
> +static void silicom_mec_led_mc_brightness_set(struct led_classdev *led_cdev,
> +					      enum led_brightness brightness);
> +static enum led_brightness silicom_mec_led_mc_brightness_get(struct led_classdev *led_cdev);
> +static struct platform_device *silicom_platform_dev;
> +static struct led_classdev_mc *silicom_led_info __initdata;
> +static struct gpio_chip *silicom_gpiochip __initdata;
> +static u8 *silicom_gpio_channels __initdata;
> +static struct gpio_chip silicom_gpio_chip = {
> +	.label = "silicom-gpio",
> +	.get_direction = silicom_gpio_get_direction,
> +	.direction_input = silicom_gpio_direction_input,
> +	.direction_output = silicom_gpio_direction_output,
> +	.get = silicom_gpio_get,
> +	.set = silicom_gpio_set,
> +	.base = -1,
> +	.ngpio = ARRAY_SIZE(plat_0222_gpio_channels),
> +	.names = plat_0222_gpio_names,
> +	/* We're using a mutex to protect the indirect access, so we can sleep if the lock blocks */
> +	.can_sleep = true,
> +};
> +
> +static struct mc_subled plat_0222_wan_mc_subled_info[] __initdata = {
> +	{
> +		.color_index = LED_COLOR_ID_WHITE,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 7),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_YELLOW,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 6),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_RED,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 5),
> +	},
> +};
> +
> +static struct mc_subled plat_0222_sys_mc_subled_info[] __initdata = {
> +	{
> +		.color_index = LED_COLOR_ID_WHITE,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 4),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_AMBER,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 3),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_RED,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 2),
> +	},
> +};
> +
> +static struct mc_subled plat_0222_stat1_mc_subled_info[] __initdata = {
> +	{
> +		.color_index = LED_COLOR_ID_RED,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 1),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_GREEN,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 0),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_BLUE,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 7),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_YELLOW,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 6),
> +	},
> +};
> +
> +static struct mc_subled plat_0222_stat2_mc_subled_info[] __initdata = {
> +	{
> +		.color_index = LED_COLOR_ID_RED,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 5),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_GREEN,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 4),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_BLUE,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 3),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_YELLOW,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 2),
> +	},
> +};
> +
> +static struct mc_subled plat_0222_stat3_mc_subled_info[] __initdata = {
> +	{
> +		.color_index = LED_COLOR_ID_RED,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 1),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_GREEN,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 0),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_BLUE,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0e, 1),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_YELLOW,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0e, 0),
> +	},
> +};
> +
> +static struct led_classdev_mc plat_0222_mc_led_info[] __initdata = {
> +	{
> +		.led_cdev = {
> +			.name = "multicolor:wan",
> +			.brightness = 0,
> +			.max_brightness = 1,
> +			.brightness_set = silicom_mec_led_mc_brightness_set,
> +			.brightness_get = silicom_mec_led_mc_brightness_get,
> +		},
> +		.num_colors = ARRAY_SIZE(plat_0222_wan_mc_subled_info),
> +		.subled_info = plat_0222_wan_mc_subled_info,
> +	},
> +	{
> +		.led_cdev = {
> +			.name = "multicolor:sys",
> +			.brightness = 0,
> +			.max_brightness = 1,
> +			.brightness_set = silicom_mec_led_mc_brightness_set,
> +			.brightness_get = silicom_mec_led_mc_brightness_get,
> +		},
> +		.num_colors = ARRAY_SIZE(plat_0222_sys_mc_subled_info),
> +		.subled_info = plat_0222_sys_mc_subled_info,
> +	},
> +	{
> +		.led_cdev = {
> +			.name = "multicolor:stat1",
> +			.brightness = 0,
> +			.max_brightness = 1,
> +			.brightness_set = silicom_mec_led_mc_brightness_set,
> +			.brightness_get = silicom_mec_led_mc_brightness_get,
> +		},
> +		.num_colors = ARRAY_SIZE(plat_0222_stat1_mc_subled_info),
> +		.subled_info = plat_0222_stat1_mc_subled_info,
> +	},
> +	{
> +		.led_cdev = {
> +			.name = "multicolor:stat2",
> +			.brightness = 0,
> +			.max_brightness = 1,
> +			.brightness_set = silicom_mec_led_mc_brightness_set,
> +			.brightness_get = silicom_mec_led_mc_brightness_get,
> +		},
> +		.num_colors = ARRAY_SIZE(plat_0222_stat2_mc_subled_info),
> +		.subled_info = plat_0222_stat2_mc_subled_info,
> +	},
> +	{
> +		.led_cdev = {
> +			.name = "multicolor:stat3",
> +			.brightness = 0,
> +			.max_brightness = 1,
> +			.brightness_set = silicom_mec_led_mc_brightness_set,
> +			.brightness_get = silicom_mec_led_mc_brightness_get,
> +		},
> +		.num_colors = ARRAY_SIZE(plat_0222_stat3_mc_subled_info),
> +		.subled_info = plat_0222_stat3_mc_subled_info,
> +	},
> +	{ },
> +};
> +
> +static struct silicom_platform_info silicom_plat_0222_cordoba_info __initdata = {
> +	.io_base = 0x800,
> +	.io_len = 8,
> +	.led_info = plat_0222_mc_led_info,
> +	.gpiochip = &silicom_gpio_chip,
> +	.gpio_channels = plat_0222_gpio_channels,
> +	/* The original generic cordoba does not have the last 4 outputs of the plat_0222 BB variant,
> +	 * the rest are the same, so use the same longer list, but ignore the last entries here
> +	 */
> +	.ngpio = ARRAY_SIZE(plat_0222_gpio_channels),
> +
> +};
> +
> +static struct mc_subled cordoba_fp_left_mc_subled_info[] __initdata = {
> +	{
> +		.color_index = LED_COLOR_ID_RED,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 6),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_GREEN,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 5),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_BLUE,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x09, 7),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_AMBER,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x09, 4),
> +	},
> +};
> +
> +static struct mc_subled cordoba_fp_center_mc_subled_info[] __initdata = {
> +	{
> +		.color_index = LED_COLOR_ID_RED,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 7),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_GREEN,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 4),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_BLUE,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 3),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_AMBER,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x09, 6),
> +	},
> +};
> +
> +static struct mc_subled cordoba_fp_right_mc_subled_info[] __initdata = {
> +	{
> +		.color_index = LED_COLOR_ID_RED,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 2),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_GREEN,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 1),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_BLUE,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 0),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_AMBER,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x09, 5),
> +	},
> +};
> +
> +static struct led_classdev_mc cordoba_mc_led_info[] __initdata = {
> +	{
> +		.led_cdev = {
> +			.name = "multicolor:fp_left",
> +			.brightness = 0,
> +			.max_brightness = 1,
> +			.brightness_set = silicom_mec_led_mc_brightness_set,
> +			.brightness_get = silicom_mec_led_mc_brightness_get,
> +		},
> +		.num_colors = ARRAY_SIZE(cordoba_fp_left_mc_subled_info),
> +		.subled_info = cordoba_fp_left_mc_subled_info,
> +	},
> +	{
> +		.led_cdev = {
> +			.name = "multicolor:fp_center",
> +			.brightness = 0,
> +			.max_brightness = 1,
> +			.brightness_set = silicom_mec_led_mc_brightness_set,
> +			.brightness_get = silicom_mec_led_mc_brightness_get,
> +		},
> +		.num_colors = ARRAY_SIZE(cordoba_fp_center_mc_subled_info),
> +		.subled_info = cordoba_fp_center_mc_subled_info,
> +	},
> +	{
> +		.led_cdev = {
> +			.name = "multicolor:fp_right",
> +			.brightness = 0,
> +			.max_brightness = 1,
> +			.brightness_set = silicom_mec_led_mc_brightness_set,
> +			.brightness_get = silicom_mec_led_mc_brightness_get,
> +		},
> +		.num_colors = ARRAY_SIZE(cordoba_fp_right_mc_subled_info),
> +		.subled_info = cordoba_fp_right_mc_subled_info,
> +	},
> +	{ },
> +};
> +
> +static struct silicom_platform_info silicom_generic_cordoba_info __initdata = {
> +	.io_base = 0x800,
> +	.io_len = 8,
> +	.led_info = cordoba_mc_led_info,
> +	.gpiochip = &silicom_gpio_chip,
> +	.gpio_channels = plat_0222_gpio_channels,
> +	.ngpio = ARRAY_SIZE(plat_0222_gpio_channels),
> +};
> +
> +static struct platform_driver silicom_platform_driver = {
> +	.driver = {
> +		.name = "silicom-platform",
> +	},
> +};
> +
> +void lock_io_modules(void)
> +{
> +	mutex_lock(&mec_io_mutex);
> +}
> +EXPORT_SYMBOL(lock_io_modules);
> +
> +void unlock_io_modules(void)
> +{
> +	mutex_unlock(&mec_io_mutex);
> +}
> +EXPORT_SYMBOL(unlock_io_modules);
> +
> +static ssize_t efuse_status_show(struct device *dev, struct device_attribute *attr,
> +		      char *buf)
> +{
> +	u32 reg;
> +	u32 bank = 0;
> +	u32 offset = 0x28;
> +	u32 byte_pos = 0;
> +
> +	mutex_lock(&mec_io_mutex);
> +	/* Select memory region */
> +	outb(bank, EC_ADDR_MSB);
> +	outb(offset, EC_ADDR_LSB);
> +
> +	/* Get current date from the address */
> +	reg = inl(MEC_DATA(byte_pos));
> +	mutex_unlock(&mec_io_mutex);
> +
> +	my_dev_ctl.efuse_status = reg & 0x1;
> +
> +	return sprintf(buf, "%d\n", my_dev_ctl.efuse_status);
> +}
> +
> +static ssize_t uc_version_show(struct device *dev,
> +			       struct device_attribute *attr,
> +			       char *buf)
> +{
> +	u32 reg;
> +	u32 bank = 0;
> +	u32 offset = 0x0;
> +	u32 byte_pos = 0;
> +	int uc_version;
> +
> +	mutex_lock(&mec_io_mutex);
> +	outb(bank, EC_ADDR_MSB);
> +	outb(offset, EC_ADDR_LSB);
> +
> +	reg = inl(MEC_DATA(byte_pos));
> +	mutex_unlock(&mec_io_mutex);
> +
> +	uc_version = (reg >> 8) & 0xFF;
> +	if (uc_version >= 64 && uc_version < 128) {
> +		uc_version = uc_version - 64;
> +		if (uc_version < 10)
> +			uc_version = 100 + uc_version;
> +		else
> +			uc_version = 100 + 10 * (uc_version / 10) + uc_version % 10;
> +	} else if (uc_version >= 128 && uc_version < 192) {
> +		uc_version = uc_version - 128;
> +		if (uc_version < 10)
> +			uc_version = 200 + uc_version;
> +		else
> +			uc_version = 200 + 10 * (uc_version / 10) + uc_version % 10;
> +	}
> +	my_dev_ctl.uc_version = uc_version;
> +	return sprintf(buf, "%d\n", my_dev_ctl.uc_version);
> +}
> +
> +static ssize_t power_cycle_show(struct device *dev,
> +				struct device_attribute *attr,
> +				char *buf)
> +{
> +	return sprintf(buf, "%d\n", my_dev_ctl.power_cycle);
> +}
> +
> +static void powercycle_uc(void)
> +{
> +	u32 bank = 0;
> +	u32 offset = 0x24;
> +	u32 byte_pos = 0;
> +
> +	mutex_lock(&mec_io_mutex);
> +	/* Select memory region */
> +	outb(bank, EC_ADDR_MSB);
> +	outb(offset, EC_ADDR_LSB);
> +
> +	/* Set to 1 for current date from the address */
> +	outb(1, MEC_DATA(byte_pos));
> +	mutex_unlock(&mec_io_mutex);
> +}
> +
> +static ssize_t power_cycle_store(struct device *dev, struct device_attribute *attr,
> +				 const char *buf, size_t count)
> +{
> +	if (sscanf(buf, "%du", &my_dev_ctl.power_cycle) != 1) {
> +		dev_err(dev, "Failed to read power_cycle\n");
> +		return -EINVAL;
> +	}
> +	if (my_dev_ctl.power_cycle > 0)
> +		powercycle_uc();
> +
> +	return count;
> +}
> +
> +static struct device_attribute my_dev_attr[] = {
> +	{
> +		.attr = {.name = "efuse_status", .mode = 0644},
> +		.show = efuse_status_show,
> +		.store = NULL
> +	},
> +	{
> +		.attr = {.name = "uc_version", .mode = 0644},
> +		.show = uc_version_show,
> +		.store = NULL
> +	},
> +	{
> +		.attr = {.name = "power_cycle", .mode = 0644},
> +		.show = power_cycle_show,
> +		.store = power_cycle_store
> +	},
> +};
> +
> +static int silicom_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
> +{
> +	u8 *channels = gpiochip_get_data(gc);
> +
> +	/* Input registers have offsets between [0x00, 0x07] */
> +	if (CHANNEL_TO_OFFSET(channels[offset]) < 0x08)
> +		return GPIO_LINE_DIRECTION_IN;
> +
> +	return GPIO_LINE_DIRECTION_OUT;
> +}
> +
> +static int silicom_gpio_direction_input(struct gpio_chip *gc, unsigned int offset)
> +{
> +	int direction = silicom_gpio_get_direction(gc, offset);
> +
> +	return direction == GPIO_LINE_DIRECTION_IN ? 0 : -EINVAL;
> +}
> +
> +static void silicom_gpio_set(struct gpio_chip *gc, unsigned int offset, int value)
> +{
> +	u8 *channels = gpiochip_get_data(gc);
> +	int direction = silicom_gpio_get_direction(gc, offset);
> +	int channel = channels[offset];
> +	u8 reg;
> +
> +	if (direction == GPIO_LINE_DIRECTION_IN)
> +		return;
> +
> +	mutex_lock(&mec_io_mutex);
> +	/* Get the dword offset from the channel */
> +	outb((channel >> 3) & 0xfc, MEC_ADDR);
> +
> +	/* Get the current register */
> +	reg = inb(MEC_DATA((channel >> 3) & 0x03));
> +	if (value == 0)
> +		reg &= ~(1 << (channel & 0x7));
> +	else if (value > 0)
> +		reg |= 1 << (channel & 0x7);
> +	else
> +		pr_err("Invalid GPIO value: %d\n", value);
> +	outb(reg, MEC_DATA((channel >> 3) & 0x03));
> +	mutex_unlock(&mec_io_mutex);
> +}
> +
> +static int silicom_gpio_direction_output(struct gpio_chip *gc, unsigned int offset, int value)
> +{
> +	int direction = silicom_gpio_get_direction(gc, offset);
> +
> +	if (direction == GPIO_LINE_DIRECTION_IN)
> +		return -EINVAL;
> +
> +	silicom_gpio_set(gc, offset, value);
> +
> +	return 0;
> +}
> +
> +static int silicom_gpio_get(struct gpio_chip *gc, unsigned int offset)
> +{
> +	u8 *channels = gpiochip_get_data(gc);
> +	int channel = channels[offset];
> +	u8 reg;
> +
> +	mutex_lock(&mec_io_mutex);
> +	/* Get the dword offset from the channel */
> +	outb((channel >> 3) & 0xfc, MEC_ADDR);
> +
> +	/* Get the current register */
> +	reg = inb(MEC_DATA((channel >> 3) & 0x03));
> +	mutex_unlock(&mec_io_mutex);
> +
> +	return (reg >> (channel & 0x7)) & 0x01;
> +}
> +
> +static int __init silicom_mc_leds_register(struct device *dev,
> +					   const struct led_classdev_mc *mc_leds)
> +{
> +	struct led_classdev_mc *led;
> +	int i, err;
> +
> +	for (i = 0; mc_leds[i].led_cdev.name; i++) {
> +		/* allocate and copy data from the init constansts */
> +		led = devm_kzalloc(dev, sizeof(struct led_classdev_mc), GFP_KERNEL);
> +		if (IS_ERR_OR_NULL(led)) {
> +			dev_err(dev, "Failed to alloc led_classdev_mc[%d]: %ld\n", i, PTR_ERR(led));
> +			return -ENOMEM;
> +		}
> +		memcpy(led, &mc_leds[i], sizeof(*led));
> +
> +		led->subled_info = devm_kzalloc(dev, led->num_colors * sizeof(struct mc_subled),
> +						GFP_KERNEL);
> +		if (IS_ERR_OR_NULL(led->subled_info)) {
> +			dev_err(dev, "Failed to alloc subled_info[%d]: %ld\n",
> +				i, PTR_ERR(led->subled_info));
> +			return -ENOMEM;
> +		}
> +		memcpy(led->subled_info, mc_leds[i].subled_info,
> +			led->num_colors * sizeof(struct mc_subled));
> +
> +		err = devm_led_classdev_multicolor_register(dev, led);
> +		if (err) {
> +			dev_err(dev, "Failed to register[%d]: %d\n", i, err);
> +			return err;
> +		}
> +	}
> +
> +	return 0;
> +}
> +
> +static void silicom_mec_led_set(int channel, int on)
> +{
> +	u8 reg;
> +
> +	mutex_lock(&mec_io_mutex);
> +	/* Get the dword offset from the channel */
> +	outb((channel >> 3) & 0xfc, MEC_ADDR);
> +
> +	/* Get the current LED settings */
> +	reg = inb(MEC_DATA((channel >> 3) & 0x03));
> +
> +	/* Outputs are active low, so clear the bit for on, or set it for off */
> +	if (on)
> +		reg &= ~(1 << (channel & 0x7));
> +	else
> +		reg |= 1 << (channel & 0x7);
> +
> +	/* Write back the updated register */
> +	outb(reg, MEC_DATA((channel >> 3) & 0x03));
> +
> +	mutex_unlock(&mec_io_mutex);
> +}
> +
> +static void silicom_mec_led_mc_brightness_set(struct led_classdev *led_cdev,
> +					      enum led_brightness brightness)
> +{
> +	struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(led_cdev);
> +	int i;
> +
> +	led_mc_calc_color_components(mc_cdev, brightness);
> +
> +	for (i = 0; i < mc_cdev->num_colors; i++) {
> +		silicom_mec_led_set(mc_cdev->subled_info[i].channel,
> +				    mc_cdev->subled_info[i].brightness);
> +	}
> +}
> +
> +static enum led_brightness silicom_mec_led_get(int channel)
> +{
> +	u8 reg;
> +
> +	mutex_lock(&mec_io_mutex);
> +	/* Get the dword offset of the register for this LED from the channel */
> +	outb((channel >> 3) & 0xfc, MEC_ADDR);
> +	/* Get the current LED settings */
> +	reg = inb(MEC_DATA((channel >> 3) & 0x03));
> +	mutex_unlock(&mec_io_mutex);
> +
> +	/* Outputs are active low */
> +	return reg & (1 << (channel & 0x7)) ? LED_OFF : LED_ON;
> +}
> +
> +static enum led_brightness silicom_mec_led_mc_brightness_get(struct led_classdev *led_cdev)
> +{
> +	struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(led_cdev);
> +	enum led_brightness brightness = LED_OFF;
> +	int i;
> +
> +	for (i = 0; i < mc_cdev->num_colors; i++) {
> +		mc_cdev->subled_info[i].brightness =
> +			silicom_mec_led_get(mc_cdev->subled_info[i].channel);
> +
> +		/* Mark the overall brightness as LED_ON if any of the subleds are on */
> +		if (mc_cdev->subled_info[i].brightness != LED_OFF)
> +			brightness = LED_ON;
> +	}
> +
> +	return brightness;
> +}
> +
> +
> +static u32 rpm_get(void)
> +{
> +	u32 reg;
> +	u32 bank = 0;
> +	u32 offset = 0xc;
> +	u32 byte_pos = 0;

Pointless variables.

> +
> +	mutex_lock(&mec_io_mutex);
> +	/* Select memory region */
> +	outb(bank, EC_ADDR_MSB);
> +	outb(offset, EC_ADDR_LSB);
> +	/* Get current date from the address */

date or data ?

> +	reg = inw(MEC_DATA(byte_pos));
> +	mutex_unlock(&mec_io_mutex);

I think this should be handled in a function.

	return silicom_read_word(0xc);

with the above implemented in silicom_read_word().
Same for functions reading 8- or 32-bit data.

> +
> +	return reg;
> +}
> +
> +static u32 temp_get(void)
> +{
> +	u32 reg;
> +	u32 bank = 0;
> +	u32 offset = 0xc;
> +	u32 byte_pos = 0;
> +
> +	mutex_lock(&mec_io_mutex);
> +	/* Select memory region */
> +	outb(bank, EC_ADDR_MSB);
> +	outb(offset, EC_ADDR_LSB);
> +	/* Get current date from the address */

data ?

> +	reg = inl(MEC_DATA(byte_pos));
> +	mutex_unlock(&mec_io_mutex);
> +
> +	return (reg >> 16) / 10;

That doesn't look correct. Please confirm that the temperature is indeed reported
in 1/10000th degress C by the chip.

> +}
> +
> +static umode_t silicom_fan_control_fan_is_visible(const u32 attr)
> +{
> +	switch (attr) {
> +	case hwmon_fan_input:
> +	case hwmon_fan_label:
> +		return 0444;
> +	default:
> +		return 0;
> +	}
> +}
> +
> +static umode_t silicom_fan_control_temp_is_visible(const u32 attr)
> +{
> +	switch (attr) {
> +	case hwmon_temp_input:
> +	case hwmon_temp_label:
> +		return 0444;
> +	default:
> +		return 0;
> +	}
> +}
> +
> +static int silicom_fan_control_read_fan(struct device *dev, u32 attr, long *val)
> +{
> +	struct silicom_fan_control_data *ctl = dev_get_drvdata(dev);
> +
> +	switch (attr) {
> +	case hwmon_fan_input:
> +		ctl->fan_speed = rpm_get();
> +		*val = ctl->fan_speed;

		*val = rpm_get();

would do, and storing the value in ctl seems unnecessary / pointless.

> +		return 0;
> +	default:
> +		return -EOPNOTSUPP;
> +	}
> +}
> +
> +static int silicom_fan_control_read_temp(struct device *dev, u32 attr, long *val)
> +{
> +	struct silicom_fan_control_data *ctl = dev_get_drvdata(dev);
> +
> +	switch (attr) {
> +	case hwmon_temp_input:
> +		ctl->temp = temp_get();
> +		*val = ctl->temp;

Why not just
		*val = temp_get();
?

ctl is not really used except to store a value which is never read.
I don't see the point of doing that. Same for fan_speed.

> +		return 0;
> +	default:
> +		return -EOPNOTSUPP;
> +	}
> +}
> +
> +static umode_t silicom_fan_control_is_visible(const void *data,
> +					  enum hwmon_sensor_types type,
> +					  u32 attr, int channel)
> +{
> +	switch (type) {
> +	case hwmon_fan:
> +		return silicom_fan_control_fan_is_visible(attr);
> +	case hwmon_temp:
> +		return silicom_fan_control_temp_is_visible(attr);
> +	default:
> +		return 0;
> +	}
> +}
> +
> +static int silicom_fan_control_read(struct device *dev, enum hwmon_sensor_types type,
> +				    u32 attr, int channel, long *val)
> +{
> +	switch (type) {
> +	case hwmon_fan:
> +		return silicom_fan_control_read_fan(dev, attr, val);
> +	case hwmon_temp:
> +		return silicom_fan_control_read_temp(dev, attr, val);
> +	default:
> +		return -EOPNOTSUPP;
> +	}
> +}
> +
> +static int silicom_fan_control_read_labels(struct device *dev, enum hwmon_sensor_types type,
> +					   u32 attr, int channel, const char **str)
> +{
> +	switch (type) {
> +	case hwmon_fan:
> +		*str = "Fan Speed (RPM)";

RPM is redundant. Fan speed is always measured in RPM.
This would result in output such as

Fan Speed (RPM):                  1166 RPM

which really doesn't make sense. Also, and that is true for both
labels, the expected string is the location of the sensor (e.g.,
"CPU Fan", not something generic like "Fan Speed" or "Thermostat
Sensor".

> +		return 0;
> +	case hwmon_temp:
> +		*str = "Thermostat Sensor";
> +		return 0;
> +	default:
> +		return -EOPNOTSUPP;
> +	}
> +}
> +
> +static int silicom_fan_control_write(struct device *dev, enum hwmon_sensor_types type,
> +				     u32 attr, int channel, long val)
> +{
> +	return 0;
> +}

Unnecessary if there is no writeable attribute (and bad to claim that
there was no error if the function was somehow called).

> +
> +static const struct hwmon_ops silicom_fan_control_hwmon_ops = {
> +	.is_visible = silicom_fan_control_is_visible,
> +	.read = silicom_fan_control_read,
> +	.write = silicom_fan_control_write,
> +	.read_string = silicom_fan_control_read_labels,
> +};
> +
> +static const struct hwmon_chip_info silicom_chip_info = {
> +	.ops = &silicom_fan_control_hwmon_ops,
> +	.info = silicom_fan_control_info,
> +};
> +
> +static int __init silicom_platform_probe(struct platform_device *device)
> +{
> +	int i, err;
> +	u8 magic, ver;
> +	struct silicom_fan_control_data *ctl;
> +	const char *name = "Silocom_Fan_Monitor";
> +	const char *dev_name = "Silicom_platform";
> +
> +	mec_io_base = 0x0800;
> +	mec_io_len = 8;
> +	if (!devm_request_region(&device->dev, mec_io_base, mec_io_len, "mec")) {
> +		dev_err(&device->dev, "couldn't reserve MEC io ports\n");
> +		return -EBUSY;
> +	}
> +
> +	/* Sanity check magic number read for EC */
> +	outb(0x00, MEC_ADDR);
> +	magic = inb(MEC_DATA(0));
> +	ver = inb(MEC_DATA(1));
> +	dev_dbg(&device->dev, "EC magic 0x%02x, version 0x%02x\n", magic, ver);
> +
> +	if (magic != SILICOM_MEC_MAGIC) {
> +		dev_err(&device->dev, "Bad EC magic 0x%02x!\n", magic);
> +		return -ENODEV;
> +	}
> +
> +	if (silicom_led_info) {

I don't understand the conditional. silicom_led_info
is always set in silicom_platform_info_init(). The code makes it
look like it is optional, but each instance of silicom_platform_info
initializes it. That seems quite pointless to me.

> +		err = silicom_mc_leds_register(&device->dev, silicom_led_info);
> +		if (err) {
> +			dev_err(&device->dev, "Failed to register LEDs\n");
> +			return err;
> +		}
> +	}
> +
> +	if (silicom_gpiochip) {
> +		err = devm_gpiochip_add_data(&device->dev, silicom_gpiochip, silicom_gpio_channels);
> +		if (err) {
> +			dev_err(&device->dev, "Failed to register gpiochip: %d\n", err);
> +			return err;
> +		}
> +	}
> +
> +	ctl = devm_kzalloc(&device->dev, sizeof(*ctl), GFP_KERNEL);
> +	if (!ctl)
> +		return -ENOMEM;
> +
> +	ctl->hdev = devm_hwmon_device_register_with_info(&device->dev, name, ctl,
> +				&silicom_chip_info, NULL);
> +
ctl->hdev is not used anywhere and thus pointless.

> +	my_dev_ctl.my_dev = root_device_register(dev_name);
> +	for (i = 0; i < ARRAY_SIZE(my_dev_attr); i++) {
> +		err = sysfs_create_file(&my_dev_ctl.my_dev->kobj, &my_dev_attr[i].attr);
> +		if (err) {
> +			pr_debug("failed to create the foo file in /sys/devices/Silicom_platform\n");
> +			break;
> +		 > +	}
> +
> +	return err;
> +}
> +
> +static int __init silicom_platform_info_init(const struct dmi_system_id *id)
> +{
> +	struct silicom_platform_info *info = id->driver_data;
> +
> +	dev_info(&silicom_platform_dev->dev, "Detected %s\n", id->ident);
> +
> +	mec_io_base = info->io_base;
> +	mec_io_len = info->io_len;
> +	silicom_led_info = info->led_info;
> +	silicom_gpio_channels = info->gpio_channels;
> +	silicom_gpiochip = info->gpiochip;
> +	if (silicom_gpiochip)
> +		silicom_gpiochip->ngpio = info->ngpio;
> +
> +	return 1;
> +}
> +
> +static const struct dmi_system_id silicom_dmi_ids[] __initconst = {
> +	{
> +		.callback = silicom_platform_info_init,
> +		.ident = "Silicom Cordoba (Generic)",
> +		.matches = {
> +			DMI_MATCH(DMI_BOARD_VENDOR, "Silicom"),
> +			DMI_MATCH(DMI_BOARD_NAME, "80300-0214-G"),
> +		},
> +		.driver_data = &silicom_generic_cordoba_info,
> +	},
> +	{
> +		.callback = silicom_platform_info_init,
> +		.ident = "Silicom Cordoba (Generic)",
> +		.matches = {
> +			DMI_MATCH(DMI_BOARD_VENDOR, "Silicom"),
> +			DMI_MATCH(DMI_BOARD_NAME, "80500-0214-G"),
> +		},
> +		.driver_data = &silicom_generic_cordoba_info,
> +	},
> +	{
> +		 .callback = silicom_platform_info_init,
> +		 .ident = "Silicom Cordoba (plat_0222)",
> +		 .matches = {
> +		       DMI_MATCH(DMI_BOARD_VENDOR, "Silicom"),
> +		       DMI_MATCH(DMI_BOARD_NAME, "80300-0222-G"),
> +		 },
> +		.driver_data = &silicom_plat_0222_cordoba_info,
> +	},
> +	{ },
> +};
> +
> +static int __init silicom_platform_init(void)
> +{
> +	struct device *dev;
> +	int err;
> +
> +	/* register a platform device to act as the parent for LEDS, etc. */
> +	silicom_platform_dev = platform_device_register_simple("silicom-platform", -1, NULL, 0);
> +	if (IS_ERR(silicom_platform_dev)) {
> +		err = PTR_ERR(silicom_platform_dev);
> +		pr_err("failed to register silicom-platform device: %d\n", err);
> +		goto silicom_init_register_err;
> +	}
> +	dev = &silicom_platform_dev->dev;
> +
> +	err = dmi_check_system(silicom_dmi_ids);
> +	if (err == 0) {
> +		dev_err(dev, "No DMI match for this platform\n");
> +		err = -ENODEV;
> +		goto silicom_init_probe_err;
> +	}
> +
> +	/* Directly probe the platform driver in init since this isn't a
> +	 * hotpluggable device.  That means we don't need to register a driver
> +	 * that needs to wait around in memory on the chance a matching device
> +	 * would get added.  Instead run once in __init so that we can free all
> +	 * those resources when the __init region is wiped
> +	 */
> +	err = platform_driver_probe(&silicom_platform_driver, silicom_platform_probe);
> +	if (err) {
> +		dev_err(dev, "Failed to probe platform driver %d\n", err);
> +		goto silicom_init_probe_err;
> +	}
> +
> +	return 0;
> +
> +silicom_init_probe_err:
> +	if (silicom_platform_dev) {
> +		platform_device_unregister(silicom_platform_dev);
> +		silicom_platform_dev = NULL;
> +	}
> +	if (my_dev_ctl.my_dev) {
> +		root_device_unregister(my_dev_ctl.my_dev);
> +		my_dev_ctl.my_dev = NULL;
> +	}
> +
> +silicom_init_register_err:
> +	return err;
> +}
> +
> +static void __exit silicom_platform_exit(void)
> +{
> +	int i;
> +
> +	if (silicom_platform_dev) {
> +		platform_device_unregister(silicom_platform_dev);
> +		platform_driver_unregister(&silicom_platform_driver);
> +	}
> +
> +	if (my_dev_ctl.my_dev) {
> +		for (i = 0; i < ARRAY_SIZE(my_dev_attr); i++)
> +			sysfs_remove_file(&my_dev_ctl.my_dev->kobj, &my_dev_attr[i].attr);
> +		root_device_unregister(my_dev_ctl.my_dev);
> +	}
> +	mutex_destroy(&mec_io_mutex);
> +}
> +
> +module_init(silicom_platform_init);
> +module_exit(silicom_platform_exit);
> +
> +MODULE_LICENSE("GPL");
> +MODULE_AUTHOR("Henry Shi <henrys@silicom-usa.com>");
> +MODULE_DESCRIPTION("Platform driver for Silicom network appliances");
> +
> +MODULE_DEVICE_TABLE(dmi, silicom_dmi_ids);
> +


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

* Re: [PATCH] Add Silicom Platform Driver
  2023-07-18 16:01 [PATCH] Add Silicom Platform Driver Henry Shi
  2023-07-18 16:40 ` Guenter Roeck
@ 2023-07-19 12:13 ` Ilpo Järvinen
  2023-07-28 19:10   ` Huibin Shi
  2023-07-25 21:02 ` Christophe JAILLET
  2 siblings, 1 reply; 32+ messages in thread
From: Ilpo Järvinen @ 2023-07-19 12:13 UTC (permalink / raw)
  To: Henry Shi
  Cc: hbshi69, tglx, mingo, bp, dave.hansen, x86, hpa, hdegoede,
	markgross, jdelvare, linux, LKML, platform-driver-x86,
	linux-hwmon, hb_shi2003, henrys, wenw

On Tue, 18 Jul 2023, Henry Shi wrote:

> The Silicom platform (silicom-platform) Linux driver for Swisscom
> Business Box (Swisscom BB) as well as Cordoba family products is a 
> software solution designed to facilitate the efficient management
> and control of devices through the integration of various Linux
> frameworks. This platform driver provides seamless support for
> device management via the Linux LED framework, GPIO framework,
> Hardware Monitoring (HWMON), and device attributes. The Silicom
> platform driver's compatibility with these Linux frameworks allows
> applications to access and control Cordoba family devices using
> existing software that is compatible with these frameworks. This
> compatibility simplifies the development process, reduces
> dependencies on proprietary solutions, and promotes
> interoperability with other Linux-based systems and software.
> 
> Signed-off-by: Henry Shi <henryshi2018@gmail.com>
> ---
>  drivers/platform/x86/Kconfig            |   12 +
>  drivers/platform/x86/Makefile           |    1 +
>  drivers/platform/x86/silicom-platform.c | 1123 +++++++++++++++++++++++
>  3 files changed, 1136 insertions(+)
>  create mode 100644 drivers/platform/x86/silicom-platform.c
> 
> diff --git a/drivers/platform/x86/Kconfig b/drivers/platform/x86/Kconfig
> index 22052031c719..8c0988c2b4ce 100644
> --- a/drivers/platform/x86/Kconfig
> +++ b/drivers/platform/x86/Kconfig
> @@ -188,6 +188,18 @@ config ACER_WMI
>  	  If you have an ACPI-WMI compatible Acer/ Wistron laptop, say Y or M
>  	  here.
>  
> +

Extra newline

> +config SILICOM_PLATFORM
> +	tristate "Silicom Edge Networking device support"
> +	depends on DMI
> +	select LEDS_CLASS_MULTICOLOR
> +	select GPIOLIB
> +	help
> +	  This option enables support for the LEDs/GPIO/etc downstream of the
> +	  embedded controller on Silicom "Cordoba" hardware and derivatives.
> +
> +	  If you have a Silicom network appliance, say Y or M here.
> +
>  source "drivers/platform/x86/amd/Kconfig"
>  
>  config ADV_SWBUTTON
> diff --git a/drivers/platform/x86/Makefile b/drivers/platform/x86/Makefile
> index 2cafe51ec4d8..f2f5743a9e54 100644
> --- a/drivers/platform/x86/Makefile
> +++ b/drivers/platform/x86/Makefile
> @@ -113,6 +113,7 @@ obj-$(CONFIG_SERIAL_MULTI_INSTANTIATE)	+= serial-multi-instantiate.o
>  obj-$(CONFIG_MLX_PLATFORM)		+= mlx-platform.o
>  obj-$(CONFIG_TOUCHSCREEN_DMI)		+= touchscreen_dmi.o
>  obj-$(CONFIG_WIRELESS_HOTKEY)		+= wireless-hotkey.o
> +obj-$(CONFIG_SILICOM_PLATFORM)          += silicom-platform.o

Use tabs like the other lines.

>  obj-$(CONFIG_X86_ANDROID_TABLETS)	+= x86-android-tablets/
>  
>  # Intel uncore drivers
> diff --git a/drivers/platform/x86/silicom-platform.c b/drivers/platform/x86/silicom-platform.c
> new file mode 100644
> index 000000000000..90431f733682
> --- /dev/null
> +++ b/drivers/platform/x86/silicom-platform.c
> @@ -0,0 +1,1123 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +//
> +// silicom-platform.c - Silicom MEC170x platform driver
> +//
> +// Copyright (C) 2023 Henry Shi <henrys@silicom-usa.com>
> +
> +#include <linux/dmi.h>
> +#include <linux/gpio/driver.h>
> +#include <linux/init.h>
> +#include <linux/ioport.h>
> +#include <linux/io.h>
> +#include <linux/kernel.h>
> +#include <linux/led-class-multicolor.h>
> +#include <linux/module.h>
> +#include <linux/hwmon.h>
> +#include <linux/mutex.h>
> +#include <linux/platform_device.h>
> +#include <linux/string.h>
> +#include <linux/thermal.h>
> +#include <linux/kobject.h>
> +#include <linux/sysfs.h>
> +
> +#define MEC_ADDR ((mec_io_base) + 0x02)
> +#define MEC_DATA(byte) ((mec_io_base) + 0x04 + (byte))
> +#define EC_ADDR_LSB MEC_ADDR
> +#define EC_ADDR_MSB ((mec_io_base) + 0x03)
> +#define SILICOM_MEC_MAGIC 0x5a

> +#define OFFSET_BIT_TO_CHANNEL(off, bit) ((((off) + 0x014) << 3) | (bit))
> +#define CHANNEL_TO_OFFSET(chan) (((chan) >> 3) - 0x14)

So you have two fields, offset and "bit", whatever that is. It would seem
named define with GENMASK() for these two fields and FIELD_PREP() is 
appropriate here. And FIELD_GET() (and perhaps also FIELD_PREP()) used in 
the code below where appropriate.

0x14 should be named with a define.

> +#define CHANNEL_TO_BIT(chan) ((chan) & 0x07)

Unused.

> +
> +static DEFINE_MUTEX(mec_io_mutex);
> +static int mec_io_base, mec_io_len;
> +
> +struct silicom_fan_control_data {
> +	struct   device *hdev;
> +	int      temp;
> +	int      fan_speed;
> +};
> +
> +static const struct hwmon_channel_info *silicom_fan_control_info[] = {
> +	HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT | HWMON_F_LABEL),
> +	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT | HWMON_T_LABEL),
> +	NULL
> +};
> +
> +struct silicom_device_control_data {
> +	struct   device *my_dev;
> +	int      efuse_status;
> +	int      uc_version;
> +	int      power_cycle;
> +};
> +static struct silicom_device_control_data my_dev_ctl;
> +
> +struct silicom_platform_info {
> +	int io_base;
> +	int io_len;
> +	struct led_classdev_mc *led_info;
> +	struct gpio_chip *gpiochip;
> +	u8 *gpio_channels;
> +	u16 ngpio;
> +};
> +
> +static const char * const plat_0222_gpio_names[] = {
> +	"AUTOM0_SFP_TX_FAULT",
> +	"SLOT2_LED_OUT",
> +	"SIM_M2_SLOT2_B_DET",
> +	"SIM_M2_SLOT2_A_DET",
> +	"SLOT1_LED_OUT",
> +	"SIM_M2_SLOT1_B_DET",
> +	"SIM_M2_SLOT1_A_DET",
> +	"SLOT0_LED_OUT",
> +	"WAN_SFP0_RX_LOS",
> +	"WAN_SFP0_PRSNT_N",
> +	"WAN_SFP0_TX_FAULT",
> +	"AUTOM1_SFP_RX_LOS",
> +	"AUTOM1_SFP_PRSNT_N",
> +	"AUTOM1_SFP_TX_FAULT",
> +	"AUTOM0_SFP_RX_LOS",
> +	"AUTOM0_SFP_PRSNT_N",
> +	"WAN_SFP1_RX_LOS",
> +	"WAN_SFP1_PRSNT_N",
> +	"WAN_SFP1_TX_FAULT",
> +	"SIM_M2_SLOT1_MUX_SEL",
> +	"W_DISABLE_M2_SLOT1_N",
> +	"W_DISABLE_MPCIE_SLOT0_N",
> +	"W_DISABLE_M2_SLOT0_N",
> +	"BT_COMMAND_MODE",
> +	"WAN_SFP1_TX_DISABLE",
> +	"WAN_SFP0_TX_DISABLE",
> +	"AUTOM1_SFP_TX_DISABLE",
> +	"AUTOM0_SFP_TX_DISABLE",
> +	"SIM_M2_SLOT2_MUX_SEL",
> +	"W_DISABLE_M2_SLOT2_N",
> +	"RST_CTL_M2_SLOT_1_N",
> +	"RST_CTL_M2_SLOT_2_N",
> +	"PM_USB_PWR_EN_BOT",
> +	"PM_USB_PWR_EN_TOP",
> +};
> +
> +static u8 plat_0222_gpio_channels[] = {
> +	OFFSET_BIT_TO_CHANNEL(0x00, 0),
> +	OFFSET_BIT_TO_CHANNEL(0x00, 1),
> +	OFFSET_BIT_TO_CHANNEL(0x00, 2),
> +	OFFSET_BIT_TO_CHANNEL(0x00, 3),
> +	OFFSET_BIT_TO_CHANNEL(0x00, 4),
> +	OFFSET_BIT_TO_CHANNEL(0x00, 5),
> +	OFFSET_BIT_TO_CHANNEL(0x00, 6),
> +	OFFSET_BIT_TO_CHANNEL(0x00, 7),
> +	OFFSET_BIT_TO_CHANNEL(0x01, 0),
> +	OFFSET_BIT_TO_CHANNEL(0x01, 1),
> +	OFFSET_BIT_TO_CHANNEL(0x01, 2),
> +	OFFSET_BIT_TO_CHANNEL(0x01, 3),
> +	OFFSET_BIT_TO_CHANNEL(0x01, 4),
> +	OFFSET_BIT_TO_CHANNEL(0x01, 5),
> +	OFFSET_BIT_TO_CHANNEL(0x01, 6),
> +	OFFSET_BIT_TO_CHANNEL(0x01, 7),
> +	OFFSET_BIT_TO_CHANNEL(0x02, 0),
> +	OFFSET_BIT_TO_CHANNEL(0x02, 1),
> +	OFFSET_BIT_TO_CHANNEL(0x02, 2),
> +	OFFSET_BIT_TO_CHANNEL(0x09, 0),
> +	OFFSET_BIT_TO_CHANNEL(0x09, 1),
> +	OFFSET_BIT_TO_CHANNEL(0x09, 2),
> +	OFFSET_BIT_TO_CHANNEL(0x09, 3),
> +	OFFSET_BIT_TO_CHANNEL(0x0a, 0),
> +	OFFSET_BIT_TO_CHANNEL(0x0a, 1),
> +	OFFSET_BIT_TO_CHANNEL(0x0a, 2),
> +	OFFSET_BIT_TO_CHANNEL(0x0a, 3),
> +	OFFSET_BIT_TO_CHANNEL(0x0a, 4),
> +	OFFSET_BIT_TO_CHANNEL(0x0a, 5),
> +	OFFSET_BIT_TO_CHANNEL(0x0a, 6),
> +	OFFSET_BIT_TO_CHANNEL(0x0b, 0),
> +	OFFSET_BIT_TO_CHANNEL(0x0b, 1),
> +	OFFSET_BIT_TO_CHANNEL(0x0b, 2),
> +	OFFSET_BIT_TO_CHANNEL(0x0b, 3),
> +};
> +
> +static int silicom_gpio_get_direction(struct gpio_chip *gc, unsigned int offset);
> +static int silicom_gpio_direction_input(struct gpio_chip *gc, unsigned int offset);
> +static int silicom_gpio_direction_output(struct gpio_chip *gc, unsigned int offset, int value);
> +static int silicom_gpio_get(struct gpio_chip *gc, unsigned int offset);
> +static void silicom_gpio_set(struct gpio_chip *gc, unsigned int offset, int value);
> +static void silicom_mec_led_mc_brightness_set(struct led_classdev *led_cdev,
> +					      enum led_brightness brightness);
> +static enum led_brightness silicom_mec_led_mc_brightness_get(struct led_classdev *led_cdev);

Try to organize the code such that no forward declarations are necessary.

> +static struct platform_device *silicom_platform_dev;
> +static struct led_classdev_mc *silicom_led_info __initdata;
> +static struct gpio_chip *silicom_gpiochip __initdata;
> +static u8 *silicom_gpio_channels __initdata;
> +static struct gpio_chip silicom_gpio_chip = {
> +	.label = "silicom-gpio",
> +	.get_direction = silicom_gpio_get_direction,
> +	.direction_input = silicom_gpio_direction_input,
> +	.direction_output = silicom_gpio_direction_output,
> +	.get = silicom_gpio_get,
> +	.set = silicom_gpio_set,
> +	.base = -1,
> +	.ngpio = ARRAY_SIZE(plat_0222_gpio_channels),
> +	.names = plat_0222_gpio_names,
> +	/* We're using a mutex to protect the indirect access, so we can sleep if the lock blocks */
> +	.can_sleep = true,
> +};
> +
> +static struct mc_subled plat_0222_wan_mc_subled_info[] __initdata = {
> +	{
> +		.color_index = LED_COLOR_ID_WHITE,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 7),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_YELLOW,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 6),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_RED,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 5),
> +	},
> +};
> +
> +static struct mc_subled plat_0222_sys_mc_subled_info[] __initdata = {
> +	{
> +		.color_index = LED_COLOR_ID_WHITE,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 4),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_AMBER,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 3),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_RED,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 2),
> +	},
> +};
> +
> +static struct mc_subled plat_0222_stat1_mc_subled_info[] __initdata = {
> +	{
> +		.color_index = LED_COLOR_ID_RED,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 1),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_GREEN,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 0),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_BLUE,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 7),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_YELLOW,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 6),
> +	},
> +};
> +
> +static struct mc_subled plat_0222_stat2_mc_subled_info[] __initdata = {
> +	{
> +		.color_index = LED_COLOR_ID_RED,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 5),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_GREEN,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 4),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_BLUE,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 3),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_YELLOW,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 2),
> +	},
> +};
> +
> +static struct mc_subled plat_0222_stat3_mc_subled_info[] __initdata = {
> +	{
> +		.color_index = LED_COLOR_ID_RED,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 1),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_GREEN,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 0),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_BLUE,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0e, 1),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_YELLOW,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0e, 0),
> +	},
> +};
> +
> +static struct led_classdev_mc plat_0222_mc_led_info[] __initdata = {
> +	{
> +		.led_cdev = {
> +			.name = "multicolor:wan",
> +			.brightness = 0,
> +			.max_brightness = 1,
> +			.brightness_set = silicom_mec_led_mc_brightness_set,
> +			.brightness_get = silicom_mec_led_mc_brightness_get,
> +		},
> +		.num_colors = ARRAY_SIZE(plat_0222_wan_mc_subled_info),
> +		.subled_info = plat_0222_wan_mc_subled_info,
> +	},
> +	{
> +		.led_cdev = {
> +			.name = "multicolor:sys",
> +			.brightness = 0,
> +			.max_brightness = 1,
> +			.brightness_set = silicom_mec_led_mc_brightness_set,
> +			.brightness_get = silicom_mec_led_mc_brightness_get,
> +		},
> +		.num_colors = ARRAY_SIZE(plat_0222_sys_mc_subled_info),
> +		.subled_info = plat_0222_sys_mc_subled_info,
> +	},
> +	{
> +		.led_cdev = {
> +			.name = "multicolor:stat1",
> +			.brightness = 0,
> +			.max_brightness = 1,
> +			.brightness_set = silicom_mec_led_mc_brightness_set,
> +			.brightness_get = silicom_mec_led_mc_brightness_get,
> +		},
> +		.num_colors = ARRAY_SIZE(plat_0222_stat1_mc_subled_info),
> +		.subled_info = plat_0222_stat1_mc_subled_info,
> +	},
> +	{
> +		.led_cdev = {
> +			.name = "multicolor:stat2",
> +			.brightness = 0,
> +			.max_brightness = 1,
> +			.brightness_set = silicom_mec_led_mc_brightness_set,
> +			.brightness_get = silicom_mec_led_mc_brightness_get,
> +		},
> +		.num_colors = ARRAY_SIZE(plat_0222_stat2_mc_subled_info),
> +		.subled_info = plat_0222_stat2_mc_subled_info,
> +	},
> +	{
> +		.led_cdev = {
> +			.name = "multicolor:stat3",
> +			.brightness = 0,
> +			.max_brightness = 1,
> +			.brightness_set = silicom_mec_led_mc_brightness_set,
> +			.brightness_get = silicom_mec_led_mc_brightness_get,
> +		},
> +		.num_colors = ARRAY_SIZE(plat_0222_stat3_mc_subled_info),
> +		.subled_info = plat_0222_stat3_mc_subled_info,
> +	},
> +	{ },
> +};
> +
> +static struct silicom_platform_info silicom_plat_0222_cordoba_info __initdata = {
> +	.io_base = 0x800,
> +	.io_len = 8,
> +	.led_info = plat_0222_mc_led_info,
> +	.gpiochip = &silicom_gpio_chip,
> +	.gpio_channels = plat_0222_gpio_channels,
> +	/* The original generic cordoba does not have the last 4 outputs of the plat_0222 BB variant,
> +	 * the rest are the same, so use the same longer list, but ignore the last entries here
> +	 */
> +	.ngpio = ARRAY_SIZE(plat_0222_gpio_channels),
> +
> +};
> +
> +static struct mc_subled cordoba_fp_left_mc_subled_info[] __initdata = {
> +	{
> +		.color_index = LED_COLOR_ID_RED,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 6),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_GREEN,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 5),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_BLUE,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x09, 7),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_AMBER,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x09, 4),
> +	},
> +};
> +
> +static struct mc_subled cordoba_fp_center_mc_subled_info[] __initdata = {
> +	{
> +		.color_index = LED_COLOR_ID_RED,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 7),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_GREEN,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 4),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_BLUE,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 3),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_AMBER,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x09, 6),
> +	},
> +};
> +
> +static struct mc_subled cordoba_fp_right_mc_subled_info[] __initdata = {
> +	{
> +		.color_index = LED_COLOR_ID_RED,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 2),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_GREEN,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 1),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_BLUE,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 0),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_AMBER,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x09, 5),
> +	},
> +};
> +
> +static struct led_classdev_mc cordoba_mc_led_info[] __initdata = {
> +	{
> +		.led_cdev = {
> +			.name = "multicolor:fp_left",
> +			.brightness = 0,
> +			.max_brightness = 1,
> +			.brightness_set = silicom_mec_led_mc_brightness_set,
> +			.brightness_get = silicom_mec_led_mc_brightness_get,
> +		},
> +		.num_colors = ARRAY_SIZE(cordoba_fp_left_mc_subled_info),
> +		.subled_info = cordoba_fp_left_mc_subled_info,
> +	},
> +	{
> +		.led_cdev = {
> +			.name = "multicolor:fp_center",
> +			.brightness = 0,
> +			.max_brightness = 1,
> +			.brightness_set = silicom_mec_led_mc_brightness_set,
> +			.brightness_get = silicom_mec_led_mc_brightness_get,
> +		},
> +		.num_colors = ARRAY_SIZE(cordoba_fp_center_mc_subled_info),
> +		.subled_info = cordoba_fp_center_mc_subled_info,
> +	},
> +	{
> +		.led_cdev = {
> +			.name = "multicolor:fp_right",
> +			.brightness = 0,
> +			.max_brightness = 1,
> +			.brightness_set = silicom_mec_led_mc_brightness_set,
> +			.brightness_get = silicom_mec_led_mc_brightness_get,
> +		},
> +		.num_colors = ARRAY_SIZE(cordoba_fp_right_mc_subled_info),
> +		.subled_info = cordoba_fp_right_mc_subled_info,
> +	},
> +	{ },
> +};
> +
> +static struct silicom_platform_info silicom_generic_cordoba_info __initdata = {
> +	.io_base = 0x800,
> +	.io_len = 8,
> +	.led_info = cordoba_mc_led_info,
> +	.gpiochip = &silicom_gpio_chip,
> +	.gpio_channels = plat_0222_gpio_channels,
> +	.ngpio = ARRAY_SIZE(plat_0222_gpio_channels),
> +};
> +
> +static struct platform_driver silicom_platform_driver = {
> +	.driver = {
> +		.name = "silicom-platform",
> +	},
> +};
> +
> +void lock_io_modules(void)
> +{
> +	mutex_lock(&mec_io_mutex);
> +}
> +EXPORT_SYMBOL(lock_io_modules);
> +
> +void unlock_io_modules(void)
> +{
> +	mutex_unlock(&mec_io_mutex);
> +}
> +EXPORT_SYMBOL(unlock_io_modules);
> +
> +static ssize_t efuse_status_show(struct device *dev, struct device_attribute *attr,
> +		      char *buf)
> +{
> +	u32 reg;
> +	u32 bank = 0;
> +	u32 offset = 0x28;

Why is this not a named define?

No need for bank & offset variables.

> +	u32 byte_pos = 0;
> +
> +	mutex_lock(&mec_io_mutex);
> +	/* Select memory region */
> +	outb(bank, EC_ADDR_MSB);
> +	outb(offset, EC_ADDR_LSB);
> +
> +	/* Get current date from the address */
> +	reg = inl(MEC_DATA(byte_pos));
> +	mutex_unlock(&mec_io_mutex);
> +
> +	my_dev_ctl.efuse_status = reg & 0x1;
> +
> +	return sprintf(buf, "%d\n", my_dev_ctl.efuse_status);
> +}
> +
> +static ssize_t uc_version_show(struct device *dev,
> +			       struct device_attribute *attr,
> +			       char *buf)
> +{
> +	u32 reg;
> +	u32 bank = 0;
> +	u32 offset = 0x0;

Ditto.

> +	u32 byte_pos = 0;
> +	int uc_version;
> +
> +	mutex_lock(&mec_io_mutex);
> +	outb(bank, EC_ADDR_MSB);
> +	outb(offset, EC_ADDR_LSB);
> +
> +	reg = inl(MEC_DATA(byte_pos));
> +	mutex_unlock(&mec_io_mutex);
> +
> +	uc_version = (reg >> 8) & 0xFF;

Define a named mask with GENMASK() and use FIELD_GET().

> +	if (uc_version >= 64 && uc_version < 128) {
> +		uc_version = uc_version - 64;

Testing for a bit (or two bit field inside the version actually 
considering the other if too) in version and then handcrafting & ~THATBIT?

> +		if (uc_version < 10)
> +			uc_version = 100 + uc_version;
> +		else
> +			uc_version = 100 + 10 * (uc_version / 10) + uc_version % 10;

Why is this if necessary? Doesn't the latter yield the same value as the 
first one when uc_version < 10?

> +	} else if (uc_version >= 128 && uc_version < 192) {
> +		uc_version = uc_version - 128;
> +		if (uc_version < 10)
> +			uc_version = 200 + uc_version;
> +		else
> +			uc_version = 200 + 10 * (uc_version / 10) + uc_version % 10;

Similar comments to this block.

> +	}
> +	my_dev_ctl.uc_version = uc_version;
> +	return sprintf(buf, "%d\n", my_dev_ctl.uc_version);
> +}
> +
> +static ssize_t power_cycle_show(struct device *dev,
> +				struct device_attribute *attr,
> +				char *buf)
> +{
> +	return sprintf(buf, "%d\n", my_dev_ctl.power_cycle);
> +}
> +
> +static void powercycle_uc(void)
> +{
> +	u32 bank = 0;
> +	u32 offset = 0x24;

Named define, no need for these variables.

> +	u32 byte_pos = 0;
> +
> +	mutex_lock(&mec_io_mutex);
> +	/* Select memory region */
> +	outb(bank, EC_ADDR_MSB);
> +	outb(offset, EC_ADDR_LSB);
> +
> +	/* Set to 1 for current date from the address */
> +	outb(1, MEC_DATA(byte_pos));

Hmm, this is really misleading. MEC_DATA() seems to calculate _an offset_, 
not data? Name things accordingly.

> +	mutex_unlock(&mec_io_mutex);
> +}
> +
> +static ssize_t power_cycle_store(struct device *dev, struct device_attribute *attr,
> +				 const char *buf, size_t count)
> +{
> +	if (sscanf(buf, "%du", &my_dev_ctl.power_cycle) != 1) {
> +		dev_err(dev, "Failed to read power_cycle\n");
> +		return -EINVAL;
> +	}
> +	if (my_dev_ctl.power_cycle > 0)
> +		powercycle_uc();
> +
> +	return count;
> +}
> +
> +static struct device_attribute my_dev_attr[] = {
> +	{
> +		.attr = {.name = "efuse_status", .mode = 0644},
> +		.show = efuse_status_show,
> +		.store = NULL
> +	},
> +	{
> +		.attr = {.name = "uc_version", .mode = 0644},
> +		.show = uc_version_show,
> +		.store = NULL
> +	},
> +	{
> +		.attr = {.name = "power_cycle", .mode = 0644},
> +		.show = power_cycle_show,
> +		.store = power_cycle_store
> +	},
> +};
> +
> +static int silicom_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
> +{
> +	u8 *channels = gpiochip_get_data(gc);
> +
> +	/* Input registers have offsets between [0x00, 0x07] */
> +	if (CHANNEL_TO_OFFSET(channels[offset]) < 0x08)
> +		return GPIO_LINE_DIRECTION_IN;
> +
> +	return GPIO_LINE_DIRECTION_OUT;
> +}
> +
> +static int silicom_gpio_direction_input(struct gpio_chip *gc, unsigned int offset)
> +{
> +	int direction = silicom_gpio_get_direction(gc, offset);
> +
> +	return direction == GPIO_LINE_DIRECTION_IN ? 0 : -EINVAL;
> +}
> +
> +static void silicom_gpio_set(struct gpio_chip *gc, unsigned int offset, int value)
> +{
> +	u8 *channels = gpiochip_get_data(gc);
> +	int direction = silicom_gpio_get_direction(gc, offset);
> +	int channel = channels[offset];
> +	u8 reg;
> +
> +	if (direction == GPIO_LINE_DIRECTION_IN)
> +		return;
> +
> +	mutex_lock(&mec_io_mutex);
> +	/* Get the dword offset from the channel */
> +	outb((channel >> 3) & 0xfc, MEC_ADDR);
> +
> +	/* Get the current register */
> +	reg = inb(MEC_DATA((channel >> 3) & 0x03));
> +	if (value == 0)
> +		reg &= ~(1 << (channel & 0x7));
> +	else if (value > 0)
> +		reg |= 1 << (channel & 0x7);
> +	else
> +		pr_err("Invalid GPIO value: %d\n", value);
> +	outb(reg, MEC_DATA((channel >> 3) & 0x03));
> +	mutex_unlock(&mec_io_mutex);
> +}
> +
> +static int silicom_gpio_direction_output(struct gpio_chip *gc, unsigned int offset, int value)
> +{
> +	int direction = silicom_gpio_get_direction(gc, offset);
> +
> +	if (direction == GPIO_LINE_DIRECTION_IN)
> +		return -EINVAL;
> +
> +	silicom_gpio_set(gc, offset, value);
> +
> +	return 0;
> +}
> +
> +static int silicom_gpio_get(struct gpio_chip *gc, unsigned int offset)
> +{
> +	u8 *channels = gpiochip_get_data(gc);
> +	int channel = channels[offset];
> +	u8 reg;
> +
> +	mutex_lock(&mec_io_mutex);
> +	/* Get the dword offset from the channel */
> +	outb((channel >> 3) & 0xfc, MEC_ADDR);
> +
> +	/* Get the current register */
> +	reg = inb(MEC_DATA((channel >> 3) & 0x03));
> +	mutex_unlock(&mec_io_mutex);
> +
> +	return (reg >> (channel & 0x7)) & 0x01;
> +}
> +
> +static int __init silicom_mc_leds_register(struct device *dev,
> +					   const struct led_classdev_mc *mc_leds)
> +{
> +	struct led_classdev_mc *led;
> +	int i, err;
> +
> +	for (i = 0; mc_leds[i].led_cdev.name; i++) {
> +		/* allocate and copy data from the init constansts */

There's a typo in the constansts but the code seems obvious enough to not 
need a comment at all.

> +		led = devm_kzalloc(dev, sizeof(struct led_classdev_mc), GFP_KERNEL);
> +		if (IS_ERR_OR_NULL(led)) {
> +			dev_err(dev, "Failed to alloc led_classdev_mc[%d]: %ld\n", i, PTR_ERR(led));
> +			return -ENOMEM;
> +		}
> +		memcpy(led, &mc_leds[i], sizeof(*led));
> +
> +		led->subled_info = devm_kzalloc(dev, led->num_colors * sizeof(struct mc_subled),

array_size() from linux/overflow.h

> +						GFP_KERNEL);
> +		if (IS_ERR_OR_NULL(led->subled_info)) {
> +			dev_err(dev, "Failed to alloc subled_info[%d]: %ld\n",
> +				i, PTR_ERR(led->subled_info));
> +			return -ENOMEM;
> +		}
> +		memcpy(led->subled_info, mc_leds[i].subled_info,
> +			led->num_colors * sizeof(struct mc_subled));

array_size()

Although a local variable could be used to hold it as it's used for alloc 
and this line.

> +
> +		err = devm_led_classdev_multicolor_register(dev, led);
> +		if (err) {
> +			dev_err(dev, "Failed to register[%d]: %d\n", i, err);
> +			return err;
> +		}
> +	}
> +
> +	return 0;
> +}
> +
> +static void silicom_mec_led_set(int channel, int on)
> +{
> +	u8 reg;
> +
> +	mutex_lock(&mec_io_mutex);
> +	/* Get the dword offset from the channel */
> +	outb((channel >> 3) & 0xfc, MEC_ADDR);
> +	/* Get the current LED settings */
> +	reg = inb(MEC_DATA((channel >> 3) & 0x03));
> +
> +	/* Outputs are active low, so clear the bit for on, or set it for off */
> +	if (on)
> +		reg &= ~(1 << (channel & 0x7));
> +	else
> +		reg |= 1 << (channel & 0x7);
> +
> +	/* Write back the updated register */
> +	outb(reg, MEC_DATA((channel >> 3) & 0x03));
> +
> +	mutex_unlock(&mec_io_mutex);

There's an identical code block in silicom_gpio_set(). Why not simply call 
this from silicom_gpio_set()?

> +}
> +
> +static void silicom_mec_led_mc_brightness_set(struct led_classdev *led_cdev,
> +					      enum led_brightness brightness)
> +{
> +	struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(led_cdev);
> +	int i;
> +
> +	led_mc_calc_color_components(mc_cdev, brightness);
> +
> +	for (i = 0; i < mc_cdev->num_colors; i++) {
> +		silicom_mec_led_set(mc_cdev->subled_info[i].channel,
> +				    mc_cdev->subled_info[i].brightness);
> +	}
> +}
> +
> +static enum led_brightness silicom_mec_led_get(int channel)
> +{
> +	u8 reg;
> +
> +	mutex_lock(&mec_io_mutex);
> +	/* Get the dword offset of the register for this LED from the channel */
> +	outb((channel >> 3) & 0xfc, MEC_ADDR);
> +	/* Get the current LED settings */
> +	reg = inb(MEC_DATA((channel >> 3) & 0x03));
> +	mutex_unlock(&mec_io_mutex);
> +
> +	/* Outputs are active low */
> +	return reg & (1 << (channel & 0x7)) ? LED_OFF : LED_ON;

Looks identical to a part of silicom_gpio_get(). Perhaps it should call 
this function??

> +}
> +
> +static enum led_brightness silicom_mec_led_mc_brightness_get(struct led_classdev *led_cdev)
> +{
> +	struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(led_cdev);
> +	enum led_brightness brightness = LED_OFF;
> +	int i;
> +
> +	for (i = 0; i < mc_cdev->num_colors; i++) {
> +		mc_cdev->subled_info[i].brightness =
> +			silicom_mec_led_get(mc_cdev->subled_info[i].channel);
> +
> +		/* Mark the overall brightness as LED_ON if any of the subleds are on */
> +		if (mc_cdev->subled_info[i].brightness != LED_OFF)
> +			brightness = LED_ON;
> +	}
> +
> +	return brightness;
> +}
> +
> +
> +static u32 rpm_get(void)
> +{
> +	u32 reg;
> +	u32 bank = 0;
> +	u32 offset = 0xc;

Named with a define. Remove unnecessary local vars.

> +	u32 byte_pos = 0;
> +
> +	mutex_lock(&mec_io_mutex);
> +	/* Select memory region */
> +	outb(bank, EC_ADDR_MSB);
> +	outb(offset, EC_ADDR_LSB);
> +	/* Get current date from the address */
> +	reg = inw(MEC_DATA(byte_pos));
> +	mutex_unlock(&mec_io_mutex);
> +
> +	return reg;
> +}
> +
> +static u32 temp_get(void)
> +{
> +	u32 reg;
> +	u32 bank = 0;
> +	u32 offset = 0xc;
> +	u32 byte_pos = 0;

Ditto.

> +	mutex_lock(&mec_io_mutex);
> +	/* Select memory region */
> +	outb(bank, EC_ADDR_MSB);
> +	outb(offset, EC_ADDR_LSB);
> +	/* Get current date from the address */
> +	reg = inl(MEC_DATA(byte_pos));
> +	mutex_unlock(&mec_io_mutex);
> +
> +	return (reg >> 16) / 10;

Should that >> 16 do a FIELD_GET() for a named field, I suspect it should?

> +}
> +
> +static umode_t silicom_fan_control_fan_is_visible(const u32 attr)
> +{
> +	switch (attr) {
> +	case hwmon_fan_input:
> +	case hwmon_fan_label:
> +		return 0444;
> +	default:
> +		return 0;
> +	}
> +}
> +
> +static umode_t silicom_fan_control_temp_is_visible(const u32 attr)
> +{
> +	switch (attr) {
> +	case hwmon_temp_input:
> +	case hwmon_temp_label:
> +		return 0444;
> +	default:
> +		return 0;
> +	}
> +}
> +
> +static int silicom_fan_control_read_fan(struct device *dev, u32 attr, long *val)
> +{
> +	struct silicom_fan_control_data *ctl = dev_get_drvdata(dev);
> +
> +	switch (attr) {
> +	case hwmon_fan_input:
> +		ctl->fan_speed = rpm_get();
> +		*val = ctl->fan_speed;
> +		return 0;
> +	default:
> +		return -EOPNOTSUPP;
> +	}
> +}
> +
> +static int silicom_fan_control_read_temp(struct device *dev, u32 attr, long *val)
> +{
> +	struct silicom_fan_control_data *ctl = dev_get_drvdata(dev);
> +
> +	switch (attr) {
> +	case hwmon_temp_input:
> +		ctl->temp = temp_get();
> +		*val = ctl->temp;
> +		return 0;
> +	default:
> +		return -EOPNOTSUPP;
> +	}
> +}
> +
> +static umode_t silicom_fan_control_is_visible(const void *data,
> +					  enum hwmon_sensor_types type,
> +					  u32 attr, int channel)
> +{
> +	switch (type) {
> +	case hwmon_fan:
> +		return silicom_fan_control_fan_is_visible(attr);
> +	case hwmon_temp:
> +		return silicom_fan_control_temp_is_visible(attr);
> +	default:
> +		return 0;
> +	}
> +}
> +
> +static int silicom_fan_control_read(struct device *dev, enum hwmon_sensor_types type,
> +				    u32 attr, int channel, long *val)
> +{
> +	switch (type) {
> +	case hwmon_fan:
> +		return silicom_fan_control_read_fan(dev, attr, val);
> +	case hwmon_temp:
> +		return silicom_fan_control_read_temp(dev, attr, val);
> +	default:
> +		return -EOPNOTSUPP;
> +	}
> +}
> +
> +static int silicom_fan_control_read_labels(struct device *dev, enum hwmon_sensor_types type,
> +					   u32 attr, int channel, const char **str)
> +{
> +	switch (type) {
> +	case hwmon_fan:
> +		*str = "Fan Speed (RPM)";
> +		return 0;
> +	case hwmon_temp:
> +		*str = "Thermostat Sensor";
> +		return 0;
> +	default:
> +		return -EOPNOTSUPP;
> +	}
> +}
> +
> +static int silicom_fan_control_write(struct device *dev, enum hwmon_sensor_types type,
> +				     u32 attr, int channel, long val)
> +{
> +	return 0;
> +}
> +
> +static const struct hwmon_ops silicom_fan_control_hwmon_ops = {
> +	.is_visible = silicom_fan_control_is_visible,
> +	.read = silicom_fan_control_read,
> +	.write = silicom_fan_control_write,
> +	.read_string = silicom_fan_control_read_labels,
> +};
> +
> +static const struct hwmon_chip_info silicom_chip_info = {
> +	.ops = &silicom_fan_control_hwmon_ops,
> +	.info = silicom_fan_control_info,
> +};
> +
> +static int __init silicom_platform_probe(struct platform_device *device)
> +{
> +	int i, err;
> +	u8 magic, ver;
> +	struct silicom_fan_control_data *ctl;
> +	const char *name = "Silocom_Fan_Monitor";
> +	const char *dev_name = "Silicom_platform";
> +
> +	mec_io_base = 0x0800;
> +	mec_io_len = 8;
> +	if (!devm_request_region(&device->dev, mec_io_base, mec_io_len, "mec")) {
> +		dev_err(&device->dev, "couldn't reserve MEC io ports\n");
> +		return -EBUSY;
> +	}
> +
> +	/* Sanity check magic number read for EC */
> +	outb(0x00, MEC_ADDR);
> +	magic = inb(MEC_DATA(0));
> +	ver = inb(MEC_DATA(1));

0 and 1 should be named with defines.

> +	dev_dbg(&device->dev, "EC magic 0x%02x, version 0x%02x\n", magic, ver);
> +
> +	if (magic != SILICOM_MEC_MAGIC) {
> +		dev_err(&device->dev, "Bad EC magic 0x%02x!\n", magic);
> +		return -ENODEV;
> +	}
> +
> +	if (silicom_led_info) {

How can this be NULL?

> +		err = silicom_mc_leds_register(&device->dev, silicom_led_info);
> +		if (err) {
> +			dev_err(&device->dev, "Failed to register LEDs\n");
> +			return err;
> +		}
> +	}
> +
> +	if (silicom_gpiochip) {

How can this be NULL?

> +		err = devm_gpiochip_add_data(&device->dev, silicom_gpiochip, silicom_gpio_channels);
> +		if (err) {
> +			dev_err(&device->dev, "Failed to register gpiochip: %d\n", err);
> +			return err;
> +		}
> +	}
> +
> +	ctl = devm_kzalloc(&device->dev, sizeof(*ctl), GFP_KERNEL);
> +	if (!ctl)
> +		return -ENOMEM;
> +
> +	ctl->hdev = devm_hwmon_device_register_with_info(&device->dev, name, ctl,
> +				&silicom_chip_info, NULL);
> +
> +	my_dev_ctl.my_dev = root_device_register(dev_name);
> +	for (i = 0; i < ARRAY_SIZE(my_dev_attr); i++) {
> +		err = sysfs_create_file(&my_dev_ctl.my_dev->kobj, &my_dev_attr[i].attr);
> +		if (err) {
> +			pr_debug("failed to create the foo file in /sys/devices/Silicom_platform\n");
> +			break;
> +		}
> +	}
> +
> +	return err;
> +}
> +
> +static int __init silicom_platform_info_init(const struct dmi_system_id *id)
> +{
> +	struct silicom_platform_info *info = id->driver_data;
> +
> +	dev_info(&silicom_platform_dev->dev, "Detected %s\n", id->ident);
> +
> +	mec_io_base = info->io_base;
> +	mec_io_len = info->io_len;
> +	silicom_led_info = info->led_info;
> +	silicom_gpio_channels = info->gpio_channels;
> +	silicom_gpiochip = info->gpiochip;
> +	if (silicom_gpiochip)

How can this be NULL?

> +		silicom_gpiochip->ngpio = info->ngpio;
> +
> +	return 1;
> +}
> +
> +static const struct dmi_system_id silicom_dmi_ids[] __initconst = {
> +	{
> +		.callback = silicom_platform_info_init,
> +		.ident = "Silicom Cordoba (Generic)",
> +		.matches = {
> +			DMI_MATCH(DMI_BOARD_VENDOR, "Silicom"),
> +			DMI_MATCH(DMI_BOARD_NAME, "80300-0214-G"),
> +		},
> +		.driver_data = &silicom_generic_cordoba_info,
> +	},
> +	{
> +		.callback = silicom_platform_info_init,
> +		.ident = "Silicom Cordoba (Generic)",
> +		.matches = {
> +			DMI_MATCH(DMI_BOARD_VENDOR, "Silicom"),
> +			DMI_MATCH(DMI_BOARD_NAME, "80500-0214-G"),
> +		},
> +		.driver_data = &silicom_generic_cordoba_info,
> +	},
> +	{
> +		 .callback = silicom_platform_info_init,
> +		 .ident = "Silicom Cordoba (plat_0222)",
> +		 .matches = {
> +		       DMI_MATCH(DMI_BOARD_VENDOR, "Silicom"),
> +		       DMI_MATCH(DMI_BOARD_NAME, "80300-0222-G"),
> +		 },

Incorrect indentation x6.

> +		.driver_data = &silicom_plat_0222_cordoba_info,
> +	},
> +	{ },
> +};
> +
> +static int __init silicom_platform_init(void)
> +{
> +	struct device *dev;
> +	int err;
> +
> +	/* register a platform device to act as the parent for LEDS, etc. */
> +	silicom_platform_dev = platform_device_register_simple("silicom-platform", -1, NULL, 0);
> +	if (IS_ERR(silicom_platform_dev)) {
> +		err = PTR_ERR(silicom_platform_dev);
> +		pr_err("failed to register silicom-platform device: %d\n", err);
> +		goto silicom_init_register_err;
> +	}
> +	dev = &silicom_platform_dev->dev;
> +
> +	err = dmi_check_system(silicom_dmi_ids);
> +	if (err == 0) {
> +		dev_err(dev, "No DMI match for this platform\n");
> +		err = -ENODEV;
> +		goto silicom_init_probe_err;
> +	}
> +
> +	/* Directly probe the platform driver in init since this isn't a
> +	 * hotpluggable device.  That means we don't need to register a driver
> +	 * that needs to wait around in memory on the chance a matching device
> +	 * would get added.  Instead run once in __init so that we can free all
> +	 * those resources when the __init region is wiped
> +	 */
> +	err = platform_driver_probe(&silicom_platform_driver, silicom_platform_probe);
> +	if (err) {
> +		dev_err(dev, "Failed to probe platform driver %d\n", err);
> +		goto silicom_init_probe_err;
> +	}
> +
> +	return 0;
> +
> +silicom_init_probe_err:
> +	if (silicom_platform_dev) {

How can this by NULL?

> +		platform_device_unregister(silicom_platform_dev);
> +		silicom_platform_dev = NULL;

Seems unnecessary.

> +	}
> +	if (my_dev_ctl.my_dev) {
> +		root_device_unregister(my_dev_ctl.my_dev);
> +		my_dev_ctl.my_dev = NULL;

Ditto.

> +	}
> +
> +silicom_init_register_err:
> +	return err;
> +}
> +
> +static void __exit silicom_platform_exit(void)
> +{
> +	int i;
> +
> +	if (silicom_platform_dev) {
> +		platform_device_unregister(silicom_platform_dev);
> +		platform_driver_unregister(&silicom_platform_driver);
> +	}
> +
> +	if (my_dev_ctl.my_dev) {
> +		for (i = 0; i < ARRAY_SIZE(my_dev_attr); i++)
> +			sysfs_remove_file(&my_dev_ctl.my_dev->kobj, &my_dev_attr[i].attr);
> +		root_device_unregister(my_dev_ctl.my_dev);
> +	}
> +	mutex_destroy(&mec_io_mutex);
> +}
> +
> +module_init(silicom_platform_init);
> +module_exit(silicom_platform_exit);
> +
> +MODULE_LICENSE("GPL");
> +MODULE_AUTHOR("Henry Shi <henrys@silicom-usa.com>");
> +MODULE_DESCRIPTION("Platform driver for Silicom network appliances");
> +
> +MODULE_DEVICE_TABLE(dmi, silicom_dmi_ids);
> +

Extra newline in the end.

-- 
 i.


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

* RE: [PATCH] Add Silicom Platform Driver
  2023-07-18 16:40 ` Guenter Roeck
@ 2023-07-25 20:29   ` Huibin Shi
  0 siblings, 0 replies; 32+ messages in thread
From: Huibin Shi @ 2023-07-25 20:29 UTC (permalink / raw)
  To: Guenter Roeck, tglx, mingo, bp, dave.hansen, x86, hpa, hdegoede,
	markgross, jdelvare, linux-kernel, platform-driver-x86,
	linux-hwmon
  Cc: hb_shi2003, Wen Wang

Guenter,

See my comments below.

Updated patch will be sent out later after review comments from other people are also addressed. 

Thanks.
Henry
-----Original Message-----
From: Guenter Roeck <groeck7@gmail.com> On Behalf Of Guenter Roeck
Sent: Tuesday, July 18, 2023 12:41 PM
To: Henry Shi <henryshi2018@gmail.com>; hbshi69@hotmail.com; tglx@linutronix.de; mingo@redhat.com; bp@alien8.de; dave.hansen@linux.intel.com; x86@kernel.org; hpa@zytor.com; hdegoede@redhat.com; markgross@kernel.org; jdelvare@suse.com; linux-kernel@vger.kernel.org; platform-driver-x86@vger.kernel.org; linux-hwmon@vger.kernel.org
Cc: hb_shi2003@yahoo.com; Huibin Shi <henrys@silicom-usa.com>; Wen Wang <wenw@silicom-usa.com>
Subject: Re: [PATCH] Add Silicom Platform Driver

Caution: This is an external email. Please take care when clicking links or opening attachments.


On 7/18/23 09:01, Henry Shi wrote:
> The Silicom platform (silicom-platform) Linux driver for Swisscom 
> Business Box (Swisscom BB) as well as Cordoba family products is a 
> software solution designed to facilitate the efficient management and 
> control of devices through the integration of various Linux 
> frameworks. This platform driver provides seamless support for device 
> management via the Linux LED framework, GPIO framework, Hardware 
> Monitoring (HWMON), and device attributes. The Silicom platform 
> driver's compatibility with these Linux frameworks allows applications 
> to access and control Cordoba family devices using existing software 
> that is compatible with these frameworks. This compatibility 
> simplifies the development process, reduces dependencies on 
> proprietary solutions, and promotes interoperability with other 
> Linux-based systems and software.
>
> Signed-off-by: Henry Shi <henryshi2018@gmail.com>
> ---
>   drivers/platform/x86/Kconfig            |   12 +
>   drivers/platform/x86/Makefile           |    1 +
>   drivers/platform/x86/silicom-platform.c | 1123 +++++++++++++++++++++++
>   3 files changed, 1136 insertions(+)
>   create mode 100644 drivers/platform/x86/silicom-platform.c
>
> diff --git a/drivers/platform/x86/Kconfig 
> b/drivers/platform/x86/Kconfig index 22052031c719..8c0988c2b4ce 100644
> --- a/drivers/platform/x86/Kconfig
> +++ b/drivers/platform/x86/Kconfig
> @@ -188,6 +188,18 @@ config ACER_WMI
>         If you have an ACPI-WMI compatible Acer/ Wistron laptop, say Y or M
>         here.
>
> +
> +config SILICOM_PLATFORM
> +     tristate "Silicom Edge Networking device support"
> +     depends on DMI
> +     select LEDS_CLASS_MULTICOLOR
> +     select GPIOLIB
> +     help
> +       This option enables support for the LEDs/GPIO/etc downstream of the
> +       embedded controller on Silicom "Cordoba" hardware and derivatives.
> +
> +       If you have a Silicom network appliance, say Y or M here.
> +
>   source "drivers/platform/x86/amd/Kconfig"
>
>   config ADV_SWBUTTON
> diff --git a/drivers/platform/x86/Makefile 
> b/drivers/platform/x86/Makefile index 2cafe51ec4d8..f2f5743a9e54 
> 100644
> --- a/drivers/platform/x86/Makefile
> +++ b/drivers/platform/x86/Makefile
> @@ -113,6 +113,7 @@ obj-$(CONFIG_SERIAL_MULTI_INSTANTIATE)    += serial-multi-instantiate.o
>   obj-$(CONFIG_MLX_PLATFORM)          += mlx-platform.o
>   obj-$(CONFIG_TOUCHSCREEN_DMI)               += touchscreen_dmi.o
>   obj-$(CONFIG_WIRELESS_HOTKEY)               += wireless-hotkey.o
> +obj-$(CONFIG_SILICOM_PLATFORM)          += silicom-platform.o
>   obj-$(CONFIG_X86_ANDROID_TABLETS)   += x86-android-tablets/
>
>   # Intel uncore drivers
> diff --git a/drivers/platform/x86/silicom-platform.c 
> b/drivers/platform/x86/silicom-platform.c
> new file mode 100644
> index 000000000000..90431f733682
> --- /dev/null
> +++ b/drivers/platform/x86/silicom-platform.c
> @@ -0,0 +1,1123 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +//
> +// silicom-platform.c - Silicom MEC170x platform driver // // 
> +Copyright (C) 2023 Henry Shi <henrys@silicom-usa.com>
> +
> +#include <linux/dmi.h>
> +#include <linux/gpio/driver.h>
> +#include <linux/init.h>
> +#include <linux/ioport.h>
> +#include <linux/io.h>
> +#include <linux/kernel.h>
> +#include <linux/led-class-multicolor.h> #include <linux/module.h> 
> +#include <linux/hwmon.h> #include <linux/mutex.h> #include 
> +<linux/platform_device.h> #include <linux/string.h> #include 
> +<linux/thermal.h>

What is this include for ?

Henry: OK, removed.

> +#include <linux/kobject.h>
> +#include <linux/sysfs.h>
> +
> +#define MEC_ADDR ((mec_io_base) + 0x02) #define MEC_DATA(byte) 
> +((mec_io_base) + 0x04 + (byte)) #define EC_ADDR_LSB MEC_ADDR #define 
> +EC_ADDR_MSB ((mec_io_base) + 0x03) #define SILICOM_MEC_MAGIC 0x5a 
> +#define OFFSET_BIT_TO_CHANNEL(off, bit) ((((off) + 0x014) << 3) | 
> +(bit)) #define CHANNEL_TO_OFFSET(chan) (((chan) >> 3) - 0x14) #define 
> +CHANNEL_TO_BIT(chan) ((chan) & 0x07)
> +
> +static DEFINE_MUTEX(mec_io_mutex);
> +static int mec_io_base, mec_io_len;

mec_io_base is always 0x0800 and mec_io_len is always 0x8.
Why all that complexity with assigning values to those static variables instead of just using defines ?

> +
> +struct silicom_fan_control_data {
> +     struct   device *hdev;
> +     int      temp;
> +     int      fan_speed;
> +};

I do not see the point of having this data structure. Values are only written into it and never read.

Henry: The intention is to group all static variables for hwmon function together. 

> +
> +static const struct hwmon_channel_info *silicom_fan_control_info[] = {
> +     HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT | HWMON_F_LABEL),
> +     HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT | HWMON_T_LABEL),
> +     NULL
> +};
> +
> +struct silicom_device_control_data {
> +     struct   device *my_dev;
> +     int      efuse_status;
> +     int      uc_version;
> +     int      power_cycle;
> +};
> +static struct silicom_device_control_data my_dev_ctl;
> +
> +struct silicom_platform_info {
> +     int io_base;
> +     int io_len;
> +     struct led_classdev_mc *led_info;
> +     struct gpio_chip *gpiochip;
> +     u8 *gpio_channels;
> +     u16 ngpio;
> +};
> +
> +static const char * const plat_0222_gpio_names[] = {
> +     "AUTOM0_SFP_TX_FAULT",
> +     "SLOT2_LED_OUT",
> +     "SIM_M2_SLOT2_B_DET",
> +     "SIM_M2_SLOT2_A_DET",
> +     "SLOT1_LED_OUT",
> +     "SIM_M2_SLOT1_B_DET",
> +     "SIM_M2_SLOT1_A_DET",
> +     "SLOT0_LED_OUT",
> +     "WAN_SFP0_RX_LOS",
> +     "WAN_SFP0_PRSNT_N",
> +     "WAN_SFP0_TX_FAULT",
> +     "AUTOM1_SFP_RX_LOS",
> +     "AUTOM1_SFP_PRSNT_N",
> +     "AUTOM1_SFP_TX_FAULT",
> +     "AUTOM0_SFP_RX_LOS",
> +     "AUTOM0_SFP_PRSNT_N",
> +     "WAN_SFP1_RX_LOS",
> +     "WAN_SFP1_PRSNT_N",
> +     "WAN_SFP1_TX_FAULT",
> +     "SIM_M2_SLOT1_MUX_SEL",
> +     "W_DISABLE_M2_SLOT1_N",
> +     "W_DISABLE_MPCIE_SLOT0_N",
> +     "W_DISABLE_M2_SLOT0_N",
> +     "BT_COMMAND_MODE",
> +     "WAN_SFP1_TX_DISABLE",
> +     "WAN_SFP0_TX_DISABLE",
> +     "AUTOM1_SFP_TX_DISABLE",
> +     "AUTOM0_SFP_TX_DISABLE",
> +     "SIM_M2_SLOT2_MUX_SEL",
> +     "W_DISABLE_M2_SLOT2_N",
> +     "RST_CTL_M2_SLOT_1_N",
> +     "RST_CTL_M2_SLOT_2_N",
> +     "PM_USB_PWR_EN_BOT",
> +     "PM_USB_PWR_EN_TOP",
> +};
> +
> +static u8 plat_0222_gpio_channels[] = {
> +     OFFSET_BIT_TO_CHANNEL(0x00, 0),
> +     OFFSET_BIT_TO_CHANNEL(0x00, 1),
> +     OFFSET_BIT_TO_CHANNEL(0x00, 2),
> +     OFFSET_BIT_TO_CHANNEL(0x00, 3),
> +     OFFSET_BIT_TO_CHANNEL(0x00, 4),
> +     OFFSET_BIT_TO_CHANNEL(0x00, 5),
> +     OFFSET_BIT_TO_CHANNEL(0x00, 6),
> +     OFFSET_BIT_TO_CHANNEL(0x00, 7),
> +     OFFSET_BIT_TO_CHANNEL(0x01, 0),
> +     OFFSET_BIT_TO_CHANNEL(0x01, 1),
> +     OFFSET_BIT_TO_CHANNEL(0x01, 2),
> +     OFFSET_BIT_TO_CHANNEL(0x01, 3),
> +     OFFSET_BIT_TO_CHANNEL(0x01, 4),
> +     OFFSET_BIT_TO_CHANNEL(0x01, 5),
> +     OFFSET_BIT_TO_CHANNEL(0x01, 6),
> +     OFFSET_BIT_TO_CHANNEL(0x01, 7),
> +     OFFSET_BIT_TO_CHANNEL(0x02, 0),
> +     OFFSET_BIT_TO_CHANNEL(0x02, 1),
> +     OFFSET_BIT_TO_CHANNEL(0x02, 2),
> +     OFFSET_BIT_TO_CHANNEL(0x09, 0),
> +     OFFSET_BIT_TO_CHANNEL(0x09, 1),
> +     OFFSET_BIT_TO_CHANNEL(0x09, 2),
> +     OFFSET_BIT_TO_CHANNEL(0x09, 3),
> +     OFFSET_BIT_TO_CHANNEL(0x0a, 0),
> +     OFFSET_BIT_TO_CHANNEL(0x0a, 1),
> +     OFFSET_BIT_TO_CHANNEL(0x0a, 2),
> +     OFFSET_BIT_TO_CHANNEL(0x0a, 3),
> +     OFFSET_BIT_TO_CHANNEL(0x0a, 4),
> +     OFFSET_BIT_TO_CHANNEL(0x0a, 5),
> +     OFFSET_BIT_TO_CHANNEL(0x0a, 6),
> +     OFFSET_BIT_TO_CHANNEL(0x0b, 0),
> +     OFFSET_BIT_TO_CHANNEL(0x0b, 1),
> +     OFFSET_BIT_TO_CHANNEL(0x0b, 2),
> +     OFFSET_BIT_TO_CHANNEL(0x0b, 3),
> +};
> +
> +static int silicom_gpio_get_direction(struct gpio_chip *gc, unsigned 
> +int offset); static int silicom_gpio_direction_input(struct gpio_chip 
> +*gc, unsigned int offset); static int 
> +silicom_gpio_direction_output(struct gpio_chip *gc, unsigned int 
> +offset, int value); static int silicom_gpio_get(struct gpio_chip *gc, 
> +unsigned int offset); static void silicom_gpio_set(struct gpio_chip *gc, unsigned int offset, int value); static void silicom_mec_led_mc_brightness_set(struct led_classdev *led_cdev,
> +                                           enum led_brightness 
> +brightness); static enum led_brightness 
> +silicom_mec_led_mc_brightness_get(struct led_classdev *led_cdev); 
> +static struct platform_device *silicom_platform_dev; static struct 
> +led_classdev_mc *silicom_led_info __initdata; static struct gpio_chip 
> +*silicom_gpiochip __initdata; static u8 *silicom_gpio_channels 
> +__initdata; static struct gpio_chip silicom_gpio_chip = {
> +     .label = "silicom-gpio",
> +     .get_direction = silicom_gpio_get_direction,
> +     .direction_input = silicom_gpio_direction_input,
> +     .direction_output = silicom_gpio_direction_output,
> +     .get = silicom_gpio_get,
> +     .set = silicom_gpio_set,
> +     .base = -1,
> +     .ngpio = ARRAY_SIZE(plat_0222_gpio_channels),
> +     .names = plat_0222_gpio_names,
> +     /* We're using a mutex to protect the indirect access, so we can sleep if the lock blocks */
> +     .can_sleep = true,
> +};
> +
> +static struct mc_subled plat_0222_wan_mc_subled_info[] __initdata = {
> +     {
> +             .color_index = LED_COLOR_ID_WHITE,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0c, 7),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_YELLOW,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0c, 6),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_RED,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0c, 5),
> +     },
> +};
> +
> +static struct mc_subled plat_0222_sys_mc_subled_info[] __initdata = {
> +     {
> +             .color_index = LED_COLOR_ID_WHITE,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0c, 4),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_AMBER,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0c, 3),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_RED,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0c, 2),
> +     },
> +};
> +
> +static struct mc_subled plat_0222_stat1_mc_subled_info[] __initdata = {
> +     {
> +             .color_index = LED_COLOR_ID_RED,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0c, 1),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_GREEN,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0c, 0),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_BLUE,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0d, 7),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_YELLOW,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0d, 6),
> +     },
> +};
> +
> +static struct mc_subled plat_0222_stat2_mc_subled_info[] __initdata = {
> +     {
> +             .color_index = LED_COLOR_ID_RED,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0d, 5),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_GREEN,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0d, 4),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_BLUE,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0d, 3),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_YELLOW,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0d, 2),
> +     },
> +};
> +
> +static struct mc_subled plat_0222_stat3_mc_subled_info[] __initdata = {
> +     {
> +             .color_index = LED_COLOR_ID_RED,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0d, 1),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_GREEN,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0d, 0),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_BLUE,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0e, 1),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_YELLOW,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0e, 0),
> +     },
> +};
> +
> +static struct led_classdev_mc plat_0222_mc_led_info[] __initdata = {
> +     {
> +             .led_cdev = {
> +                     .name = "multicolor:wan",
> +                     .brightness = 0,
> +                     .max_brightness = 1,
> +                     .brightness_set = silicom_mec_led_mc_brightness_set,
> +                     .brightness_get = silicom_mec_led_mc_brightness_get,
> +             },
> +             .num_colors = ARRAY_SIZE(plat_0222_wan_mc_subled_info),
> +             .subled_info = plat_0222_wan_mc_subled_info,
> +     },
> +     {
> +             .led_cdev = {
> +                     .name = "multicolor:sys",
> +                     .brightness = 0,
> +                     .max_brightness = 1,
> +                     .brightness_set = silicom_mec_led_mc_brightness_set,
> +                     .brightness_get = silicom_mec_led_mc_brightness_get,
> +             },
> +             .num_colors = ARRAY_SIZE(plat_0222_sys_mc_subled_info),
> +             .subled_info = plat_0222_sys_mc_subled_info,
> +     },
> +     {
> +             .led_cdev = {
> +                     .name = "multicolor:stat1",
> +                     .brightness = 0,
> +                     .max_brightness = 1,
> +                     .brightness_set = silicom_mec_led_mc_brightness_set,
> +                     .brightness_get = silicom_mec_led_mc_brightness_get,
> +             },
> +             .num_colors = ARRAY_SIZE(plat_0222_stat1_mc_subled_info),
> +             .subled_info = plat_0222_stat1_mc_subled_info,
> +     },
> +     {
> +             .led_cdev = {
> +                     .name = "multicolor:stat2",
> +                     .brightness = 0,
> +                     .max_brightness = 1,
> +                     .brightness_set = silicom_mec_led_mc_brightness_set,
> +                     .brightness_get = silicom_mec_led_mc_brightness_get,
> +             },
> +             .num_colors = ARRAY_SIZE(plat_0222_stat2_mc_subled_info),
> +             .subled_info = plat_0222_stat2_mc_subled_info,
> +     },
> +     {
> +             .led_cdev = {
> +                     .name = "multicolor:stat3",
> +                     .brightness = 0,
> +                     .max_brightness = 1,
> +                     .brightness_set = silicom_mec_led_mc_brightness_set,
> +                     .brightness_get = silicom_mec_led_mc_brightness_get,
> +             },
> +             .num_colors = ARRAY_SIZE(plat_0222_stat3_mc_subled_info),
> +             .subled_info = plat_0222_stat3_mc_subled_info,
> +     },
> +     { },
> +};
> +
> +static struct silicom_platform_info silicom_plat_0222_cordoba_info __initdata = {
> +     .io_base = 0x800,
> +     .io_len = 8,
> +     .led_info = plat_0222_mc_led_info,
> +     .gpiochip = &silicom_gpio_chip,
> +     .gpio_channels = plat_0222_gpio_channels,
> +     /* The original generic cordoba does not have the last 4 outputs of the plat_0222 BB variant,
> +      * the rest are the same, so use the same longer list, but ignore the last entries here
> +      */
> +     .ngpio = ARRAY_SIZE(plat_0222_gpio_channels),
> +
> +};
> +
> +static struct mc_subled cordoba_fp_left_mc_subled_info[] __initdata = {
> +     {
> +             .color_index = LED_COLOR_ID_RED,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x08, 6),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_GREEN,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x08, 5),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_BLUE,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x09, 7),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_AMBER,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x09, 4),
> +     },
> +};
> +
> +static struct mc_subled cordoba_fp_center_mc_subled_info[] __initdata = {
> +     {
> +             .color_index = LED_COLOR_ID_RED,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x08, 7),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_GREEN,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x08, 4),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_BLUE,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x08, 3),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_AMBER,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x09, 6),
> +     },
> +};
> +
> +static struct mc_subled cordoba_fp_right_mc_subled_info[] __initdata = {
> +     {
> +             .color_index = LED_COLOR_ID_RED,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x08, 2),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_GREEN,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x08, 1),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_BLUE,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x08, 0),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_AMBER,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x09, 5),
> +     },
> +};
> +
> +static struct led_classdev_mc cordoba_mc_led_info[] __initdata = {
> +     {
> +             .led_cdev = {
> +                     .name = "multicolor:fp_left",
> +                     .brightness = 0,
> +                     .max_brightness = 1,
> +                     .brightness_set = silicom_mec_led_mc_brightness_set,
> +                     .brightness_get = silicom_mec_led_mc_brightness_get,
> +             },
> +             .num_colors = ARRAY_SIZE(cordoba_fp_left_mc_subled_info),
> +             .subled_info = cordoba_fp_left_mc_subled_info,
> +     },
> +     {
> +             .led_cdev = {
> +                     .name = "multicolor:fp_center",
> +                     .brightness = 0,
> +                     .max_brightness = 1,
> +                     .brightness_set = silicom_mec_led_mc_brightness_set,
> +                     .brightness_get = silicom_mec_led_mc_brightness_get,
> +             },
> +             .num_colors = ARRAY_SIZE(cordoba_fp_center_mc_subled_info),
> +             .subled_info = cordoba_fp_center_mc_subled_info,
> +     },
> +     {
> +             .led_cdev = {
> +                     .name = "multicolor:fp_right",
> +                     .brightness = 0,
> +                     .max_brightness = 1,
> +                     .brightness_set = silicom_mec_led_mc_brightness_set,
> +                     .brightness_get = silicom_mec_led_mc_brightness_get,
> +             },
> +             .num_colors = ARRAY_SIZE(cordoba_fp_right_mc_subled_info),
> +             .subled_info = cordoba_fp_right_mc_subled_info,
> +     },
> +     { },
> +};
> +
> +static struct silicom_platform_info silicom_generic_cordoba_info __initdata = {
> +     .io_base = 0x800,
> +     .io_len = 8,
> +     .led_info = cordoba_mc_led_info,
> +     .gpiochip = &silicom_gpio_chip,
> +     .gpio_channels = plat_0222_gpio_channels,
> +     .ngpio = ARRAY_SIZE(plat_0222_gpio_channels),
> +};
> +
> +static struct platform_driver silicom_platform_driver = {
> +     .driver = {
> +             .name = "silicom-platform",
> +     },
> +};
> +
> +void lock_io_modules(void)
> +{
> +     mutex_lock(&mec_io_mutex);
> +}
> +EXPORT_SYMBOL(lock_io_modules);
> +
> +void unlock_io_modules(void)
> +{
> +     mutex_unlock(&mec_io_mutex);
> +}
> +EXPORT_SYMBOL(unlock_io_modules);
> +
> +static ssize_t efuse_status_show(struct device *dev, struct device_attribute *attr,
> +                   char *buf)
> +{
> +     u32 reg;
> +     u32 bank = 0;
> +     u32 offset = 0x28;
> +     u32 byte_pos = 0;
> +
> +     mutex_lock(&mec_io_mutex);
> +     /* Select memory region */
> +     outb(bank, EC_ADDR_MSB);
> +     outb(offset, EC_ADDR_LSB);
> +
> +     /* Get current date from the address */
> +     reg = inl(MEC_DATA(byte_pos));
> +     mutex_unlock(&mec_io_mutex);
> +
> +     my_dev_ctl.efuse_status = reg & 0x1;
> +
> +     return sprintf(buf, "%d\n", my_dev_ctl.efuse_status); }
> +
> +static ssize_t uc_version_show(struct device *dev,
> +                            struct device_attribute *attr,
> +                            char *buf) {
> +     u32 reg;
> +     u32 bank = 0;
> +     u32 offset = 0x0;
> +     u32 byte_pos = 0;
> +     int uc_version;
> +
> +     mutex_lock(&mec_io_mutex);
> +     outb(bank, EC_ADDR_MSB);
> +     outb(offset, EC_ADDR_LSB);
> +
> +     reg = inl(MEC_DATA(byte_pos));
> +     mutex_unlock(&mec_io_mutex);
> +
> +     uc_version = (reg >> 8) & 0xFF;
> +     if (uc_version >= 64 && uc_version < 128) {
> +             uc_version = uc_version - 64;
> +             if (uc_version < 10)
> +                     uc_version = 100 + uc_version;
> +             else
> +                     uc_version = 100 + 10 * (uc_version / 10) + uc_version % 10;
> +     } else if (uc_version >= 128 && uc_version < 192) {
> +             uc_version = uc_version - 128;
> +             if (uc_version < 10)
> +                     uc_version = 200 + uc_version;
> +             else
> +                     uc_version = 200 + 10 * (uc_version / 10) + uc_version % 10;
> +     }
> +     my_dev_ctl.uc_version = uc_version;
> +     return sprintf(buf, "%d\n", my_dev_ctl.uc_version); }
> +
> +static ssize_t power_cycle_show(struct device *dev,
> +                             struct device_attribute *attr,
> +                             char *buf) {
> +     return sprintf(buf, "%d\n", my_dev_ctl.power_cycle); }
> +
> +static void powercycle_uc(void)
> +{
> +     u32 bank = 0;
> +     u32 offset = 0x24;
> +     u32 byte_pos = 0;
> +
> +     mutex_lock(&mec_io_mutex);
> +     /* Select memory region */
> +     outb(bank, EC_ADDR_MSB);
> +     outb(offset, EC_ADDR_LSB);
> +
> +     /* Set to 1 for current date from the address */
> +     outb(1, MEC_DATA(byte_pos));
> +     mutex_unlock(&mec_io_mutex);
> +}
> +
> +static ssize_t power_cycle_store(struct device *dev, struct device_attribute *attr,
> +                              const char *buf, size_t count) {
> +     if (sscanf(buf, "%du", &my_dev_ctl.power_cycle) != 1) {
> +             dev_err(dev, "Failed to read power_cycle\n");
> +             return -EINVAL;
> +     }
> +     if (my_dev_ctl.power_cycle > 0)
> +             powercycle_uc();
> +
> +     return count;
> +}
> +
> +static struct device_attribute my_dev_attr[] = {
> +     {
> +             .attr = {.name = "efuse_status", .mode = 0644},
> +             .show = efuse_status_show,
> +             .store = NULL
> +     },
> +     {
> +             .attr = {.name = "uc_version", .mode = 0644},
> +             .show = uc_version_show,
> +             .store = NULL
> +     },
> +     {
> +             .attr = {.name = "power_cycle", .mode = 0644},
> +             .show = power_cycle_show,
> +             .store = power_cycle_store
> +     },
> +};
> +
> +static int silicom_gpio_get_direction(struct gpio_chip *gc, unsigned 
> +int offset) {
> +     u8 *channels = gpiochip_get_data(gc);
> +
> +     /* Input registers have offsets between [0x00, 0x07] */
> +     if (CHANNEL_TO_OFFSET(channels[offset]) < 0x08)
> +             return GPIO_LINE_DIRECTION_IN;
> +
> +     return GPIO_LINE_DIRECTION_OUT;
> +}
> +
> +static int silicom_gpio_direction_input(struct gpio_chip *gc, 
> +unsigned int offset) {
> +     int direction = silicom_gpio_get_direction(gc, offset);
> +
> +     return direction == GPIO_LINE_DIRECTION_IN ? 0 : -EINVAL; }
> +
> +static void silicom_gpio_set(struct gpio_chip *gc, unsigned int 
> +offset, int value) {
> +     u8 *channels = gpiochip_get_data(gc);
> +     int direction = silicom_gpio_get_direction(gc, offset);
> +     int channel = channels[offset];
> +     u8 reg;
> +
> +     if (direction == GPIO_LINE_DIRECTION_IN)
> +             return;
> +
> +     mutex_lock(&mec_io_mutex);
> +     /* Get the dword offset from the channel */
> +     outb((channel >> 3) & 0xfc, MEC_ADDR);
> +
> +     /* Get the current register */
> +     reg = inb(MEC_DATA((channel >> 3) & 0x03));
> +     if (value == 0)
> +             reg &= ~(1 << (channel & 0x7));
> +     else if (value > 0)
> +             reg |= 1 << (channel & 0x7);
> +     else
> +             pr_err("Invalid GPIO value: %d\n", value);
> +     outb(reg, MEC_DATA((channel >> 3) & 0x03));
> +     mutex_unlock(&mec_io_mutex);
> +}
> +
> +static int silicom_gpio_direction_output(struct gpio_chip *gc, 
> +unsigned int offset, int value) {
> +     int direction = silicom_gpio_get_direction(gc, offset);
> +
> +     if (direction == GPIO_LINE_DIRECTION_IN)
> +             return -EINVAL;
> +
> +     silicom_gpio_set(gc, offset, value);
> +
> +     return 0;
> +}
> +
> +static int silicom_gpio_get(struct gpio_chip *gc, unsigned int 
> +offset) {
> +     u8 *channels = gpiochip_get_data(gc);
> +     int channel = channels[offset];
> +     u8 reg;
> +
> +     mutex_lock(&mec_io_mutex);
> +     /* Get the dword offset from the channel */
> +     outb((channel >> 3) & 0xfc, MEC_ADDR);
> +
> +     /* Get the current register */
> +     reg = inb(MEC_DATA((channel >> 3) & 0x03));
> +     mutex_unlock(&mec_io_mutex);
> +
> +     return (reg >> (channel & 0x7)) & 0x01; }
> +
> +static int __init silicom_mc_leds_register(struct device *dev,
> +                                        const struct led_classdev_mc 
> +*mc_leds) {
> +     struct led_classdev_mc *led;
> +     int i, err;
> +
> +     for (i = 0; mc_leds[i].led_cdev.name; i++) {
> +             /* allocate and copy data from the init constansts */
> +             led = devm_kzalloc(dev, sizeof(struct led_classdev_mc), GFP_KERNEL);
> +             if (IS_ERR_OR_NULL(led)) {
> +                     dev_err(dev, "Failed to alloc led_classdev_mc[%d]: %ld\n", i, PTR_ERR(led));
> +                     return -ENOMEM;
> +             }
> +             memcpy(led, &mc_leds[i], sizeof(*led));
> +
> +             led->subled_info = devm_kzalloc(dev, led->num_colors * sizeof(struct mc_subled),
> +                                             GFP_KERNEL);
> +             if (IS_ERR_OR_NULL(led->subled_info)) {
> +                     dev_err(dev, "Failed to alloc subled_info[%d]: %ld\n",
> +                             i, PTR_ERR(led->subled_info));
> +                     return -ENOMEM;
> +             }
> +             memcpy(led->subled_info, mc_leds[i].subled_info,
> +                     led->num_colors * sizeof(struct mc_subled));
> +
> +             err = devm_led_classdev_multicolor_register(dev, led);
> +             if (err) {
> +                     dev_err(dev, "Failed to register[%d]: %d\n", i, err);
> +                     return err;
> +             }
> +     }
> +
> +     return 0;
> +}
> +
> +static void silicom_mec_led_set(int channel, int on) {
> +     u8 reg;
> +
> +     mutex_lock(&mec_io_mutex);
> +     /* Get the dword offset from the channel */
> +     outb((channel >> 3) & 0xfc, MEC_ADDR);
> +
> +     /* Get the current LED settings */
> +     reg = inb(MEC_DATA((channel >> 3) & 0x03));
> +
> +     /* Outputs are active low, so clear the bit for on, or set it for off */
> +     if (on)
> +             reg &= ~(1 << (channel & 0x7));
> +     else
> +             reg |= 1 << (channel & 0x7);
> +
> +     /* Write back the updated register */
> +     outb(reg, MEC_DATA((channel >> 3) & 0x03));
> +
> +     mutex_unlock(&mec_io_mutex);
> +}
> +
> +static void silicom_mec_led_mc_brightness_set(struct led_classdev *led_cdev,
> +                                           enum led_brightness 
> +brightness) {
> +     struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(led_cdev);
> +     int i;
> +
> +     led_mc_calc_color_components(mc_cdev, brightness);
> +
> +     for (i = 0; i < mc_cdev->num_colors; i++) {
> +             silicom_mec_led_set(mc_cdev->subled_info[i].channel,
> +                                 mc_cdev->subled_info[i].brightness);
> +     }
> +}
> +
> +static enum led_brightness silicom_mec_led_get(int channel) {
> +     u8 reg;
> +
> +     mutex_lock(&mec_io_mutex);
> +     /* Get the dword offset of the register for this LED from the channel */
> +     outb((channel >> 3) & 0xfc, MEC_ADDR);
> +     /* Get the current LED settings */
> +     reg = inb(MEC_DATA((channel >> 3) & 0x03));
> +     mutex_unlock(&mec_io_mutex);
> +
> +     /* Outputs are active low */
> +     return reg & (1 << (channel & 0x7)) ? LED_OFF : LED_ON; }
> +
> +static enum led_brightness silicom_mec_led_mc_brightness_get(struct 
> +led_classdev *led_cdev) {
> +     struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(led_cdev);
> +     enum led_brightness brightness = LED_OFF;
> +     int i;
> +
> +     for (i = 0; i < mc_cdev->num_colors; i++) {
> +             mc_cdev->subled_info[i].brightness =
> +                     
> + silicom_mec_led_get(mc_cdev->subled_info[i].channel);
> +
> +             /* Mark the overall brightness as LED_ON if any of the subleds are on */
> +             if (mc_cdev->subled_info[i].brightness != LED_OFF)
> +                     brightness = LED_ON;
> +     }
> +
> +     return brightness;
> +}
> +
> +
> +static u32 rpm_get(void)
> +{
> +     u32 reg;
> +     u32 bank = 0;
> +     u32 offset = 0xc;
> +     u32 byte_pos = 0;

Pointless variables.

Henry: Ilpo also pointed out this. There variable will be removed.

> +
> +     mutex_lock(&mec_io_mutex);
> +     /* Select memory region */
> +     outb(bank, EC_ADDR_MSB);
> +     outb(offset, EC_ADDR_LSB);
> +     /* Get current date from the address */

date or data ?

Henry: corrected this typo here, and other three locations.

> +     reg = inw(MEC_DATA(byte_pos));
> +     mutex_unlock(&mec_io_mutex);

I think this should be handled in a function.

        return silicom_read_word(0xc);

with the above implemented in silicom_read_word().
Same for functions reading 8- or 32-bit data.

Henry: In my opinion, Linux already has inw(), inb function for io port access. There is no much value to wrap them with other functions.


> +
> +     return reg;
> +}
> +
> +static u32 temp_get(void)
> +{
> +     u32 reg;
> +     u32 bank = 0;
> +     u32 offset = 0xc;
> +     u32 byte_pos = 0;
> +
> +     mutex_lock(&mec_io_mutex);
> +     /* Select memory region */
> +     outb(bank, EC_ADDR_MSB);
> +     outb(offset, EC_ADDR_LSB);
> +     /* Get current date from the address */

data ?

Henry: typo will be corrected.

> +     reg = inl(MEC_DATA(byte_pos));
> +     mutex_unlock(&mec_io_mutex);
> +
> +     return (reg >> 16) / 10;

That doesn't look correct. Please confirm that the temperature is indeed reported in 1/10000th degress C by the chip.

Henry: This is to correct micro-controller firmware implementation: when firmware put temperature to the register, it put (real temperature * 10) as an integer to the register. (This is for internal debugging purpose, to get temperature with more precision). But the kernel driver need report real temperature.  

> +}
> +
> +static umode_t silicom_fan_control_fan_is_visible(const u32 attr) {
> +     switch (attr) {
> +     case hwmon_fan_input:
> +     case hwmon_fan_label:
> +             return 0444;
> +     default:
> +             return 0;
> +     }
> +}
> +
> +static umode_t silicom_fan_control_temp_is_visible(const u32 attr) {
> +     switch (attr) {
> +     case hwmon_temp_input:
> +     case hwmon_temp_label:
> +             return 0444;
> +     default:
> +             return 0;
> +     }
> +}
> +
> +static int silicom_fan_control_read_fan(struct device *dev, u32 attr, 
> +long *val) {
> +     struct silicom_fan_control_data *ctl = dev_get_drvdata(dev);
> +
> +     switch (attr) {
> +     case hwmon_fan_input:
> +             ctl->fan_speed = rpm_get();
> +             *val = ctl->fan_speed;

                *val = rpm_get();

would do, and storing the value in ctl seems unnecessary / pointless.

Henry: OK, will remove structure silicom_fan_control_data.

> +             return 0;
> +     default:
> +             return -EOPNOTSUPP;
> +     }
> +}
> +
> +static int silicom_fan_control_read_temp(struct device *dev, u32 
> +attr, long *val) {
> +     struct silicom_fan_control_data *ctl = dev_get_drvdata(dev);
> +
> +     switch (attr) {
> +     case hwmon_temp_input:
> +             ctl->temp = temp_get();
> +             *val = ctl->temp;

Why not just
                *val = temp_get();
?

Henry: OK 

ctl is not really used except to store a value which is never read.
I don't see the point of doing that. Same for fan_speed.

Henry: OK, will remove this structure. 

> +             return 0;
> +     default:
> +             return -EOPNOTSUPP;
> +     }
> +}
> +
> +static umode_t silicom_fan_control_is_visible(const void *data,
> +                                       enum hwmon_sensor_types type,
> +                                       u32 attr, int channel) {
> +     switch (type) {
> +     case hwmon_fan:
> +             return silicom_fan_control_fan_is_visible(attr);
> +     case hwmon_temp:
> +             return silicom_fan_control_temp_is_visible(attr);
> +     default:
> +             return 0;
> +     }
> +}
> +
> +static int silicom_fan_control_read(struct device *dev, enum hwmon_sensor_types type,
> +                                 u32 attr, int channel, long *val) {
> +     switch (type) {
> +     case hwmon_fan:
> +             return silicom_fan_control_read_fan(dev, attr, val);
> +     case hwmon_temp:
> +             return silicom_fan_control_read_temp(dev, attr, val);
> +     default:
> +             return -EOPNOTSUPP;
> +     }
> +}
> +
> +static int silicom_fan_control_read_labels(struct device *dev, enum hwmon_sensor_types type,
> +                                        u32 attr, int channel, const 
> +char **str) {
> +     switch (type) {
> +     case hwmon_fan:
> +             *str = "Fan Speed (RPM)";

RPM is redundant. Fan speed is always measured in RPM.
This would result in output such as

Fan Speed (RPM):                  1166 RPM

which really doesn't make sense. Also, and that is true for both labels, the expected string is the location of the sensor (e.g., "CPU Fan", not something generic like "Fan Speed" or "Thermostat Sensor".

Henry: OK, will change to "Fan Speed".

> +             return 0;
> +     case hwmon_temp:
> +             *str = "Thermostat Sensor";
> +             return 0;
> +     default:
> +             return -EOPNOTSUPP;
> +     }
> +}
> +
> +static int silicom_fan_control_write(struct device *dev, enum hwmon_sensor_types type,
> +                                  u32 attr, int channel, long val) {
> +     return 0;
> +}

Unnecessary if there is no writeable attribute (and bad to claim that there was no error if the function was somehow called).

Henry: OK, will remove this function. 

> +
> +static const struct hwmon_ops silicom_fan_control_hwmon_ops = {
> +     .is_visible = silicom_fan_control_is_visible,
> +     .read = silicom_fan_control_read,
> +     .write = silicom_fan_control_write,
> +     .read_string = silicom_fan_control_read_labels, };
> +
> +static const struct hwmon_chip_info silicom_chip_info = {
> +     .ops = &silicom_fan_control_hwmon_ops,
> +     .info = silicom_fan_control_info, };
> +
> +static int __init silicom_platform_probe(struct platform_device 
> +*device) {
> +     int i, err;
> +     u8 magic, ver;
> +     struct silicom_fan_control_data *ctl;
> +     const char *name = "Silocom_Fan_Monitor";
> +     const char *dev_name = "Silicom_platform";
> +
> +     mec_io_base = 0x0800;
> +     mec_io_len = 8;
> +     if (!devm_request_region(&device->dev, mec_io_base, mec_io_len, "mec")) {
> +             dev_err(&device->dev, "couldn't reserve MEC io ports\n");
> +             return -EBUSY;
> +     }
> +
> +     /* Sanity check magic number read for EC */
> +     outb(0x00, MEC_ADDR);
> +     magic = inb(MEC_DATA(0));
> +     ver = inb(MEC_DATA(1));
> +     dev_dbg(&device->dev, "EC magic 0x%02x, version 0x%02x\n", 
> + magic, ver);
> +
> +     if (magic != SILICOM_MEC_MAGIC) {
> +             dev_err(&device->dev, "Bad EC magic 0x%02x!\n", magic);
> +             return -ENODEV;
> +     }
> +
> +     if (silicom_led_info) {

I don't understand the conditional. silicom_led_info is always set in silicom_platform_info_init(). The code makes it look like it is optional, but each instance of silicom_platform_info initializes it. That seems quite pointless to me.

Henry: The check will be removed.

> +             err = silicom_mc_leds_register(&device->dev, silicom_led_info);
> +             if (err) {
> +                     dev_err(&device->dev, "Failed to register LEDs\n");
> +                     return err;
> +             }
> +     }
> +
> +     if (silicom_gpiochip) {
> +             err = devm_gpiochip_add_data(&device->dev, silicom_gpiochip, silicom_gpio_channels);
> +             if (err) {
> +                     dev_err(&device->dev, "Failed to register gpiochip: %d\n", err);
> +                     return err;
> +             }
> +     }
> +
> +     ctl = devm_kzalloc(&device->dev, sizeof(*ctl), GFP_KERNEL);
> +     if (!ctl)
> +             return -ENOMEM;
> +
> +     ctl->hdev = devm_hwmon_device_register_with_info(&device->dev, name, ctl,
> +                             &silicom_chip_info, NULL);
> +
ctl->hdev is not used anywhere and thus pointless.

Henry: the defining structure of ctl will be removed. 

> +     my_dev_ctl.my_dev = root_device_register(dev_name);
> +     for (i = 0; i < ARRAY_SIZE(my_dev_attr); i++) {
> +             err = sysfs_create_file(&my_dev_ctl.my_dev->kobj, &my_dev_attr[i].attr);
> +             if (err) {
> +                     pr_debug("failed to create the foo file in /sys/devices/Silicom_platform\n");
> +                     break;
> +              > +    }
> +
> +     return err;
> +}
> +
> +static int __init silicom_platform_info_init(const struct 
> +dmi_system_id *id) {
> +     struct silicom_platform_info *info = id->driver_data;
> +
> +     dev_info(&silicom_platform_dev->dev, "Detected %s\n", 
> + id->ident);
> +
> +     mec_io_base = info->io_base;
> +     mec_io_len = info->io_len;
> +     silicom_led_info = info->led_info;
> +     silicom_gpio_channels = info->gpio_channels;
> +     silicom_gpiochip = info->gpiochip;
> +     if (silicom_gpiochip)
> +             silicom_gpiochip->ngpio = info->ngpio;
> +
> +     return 1;
> +}
> +
> +static const struct dmi_system_id silicom_dmi_ids[] __initconst = {
> +     {
> +             .callback = silicom_platform_info_init,
> +             .ident = "Silicom Cordoba (Generic)",
> +             .matches = {
> +                     DMI_MATCH(DMI_BOARD_VENDOR, "Silicom"),
> +                     DMI_MATCH(DMI_BOARD_NAME, "80300-0214-G"),
> +             },
> +             .driver_data = &silicom_generic_cordoba_info,
> +     },
> +     {
> +             .callback = silicom_platform_info_init,
> +             .ident = "Silicom Cordoba (Generic)",
> +             .matches = {
> +                     DMI_MATCH(DMI_BOARD_VENDOR, "Silicom"),
> +                     DMI_MATCH(DMI_BOARD_NAME, "80500-0214-G"),
> +             },
> +             .driver_data = &silicom_generic_cordoba_info,
> +     },
> +     {
> +              .callback = silicom_platform_info_init,
> +              .ident = "Silicom Cordoba (plat_0222)",
> +              .matches = {
> +                    DMI_MATCH(DMI_BOARD_VENDOR, "Silicom"),
> +                    DMI_MATCH(DMI_BOARD_NAME, "80300-0222-G"),
> +              },
> +             .driver_data = &silicom_plat_0222_cordoba_info,
> +     },
> +     { },
> +};
> +
> +static int __init silicom_platform_init(void) {
> +     struct device *dev;
> +     int err;
> +
> +     /* register a platform device to act as the parent for LEDS, etc. */
> +     silicom_platform_dev = platform_device_register_simple("silicom-platform", -1, NULL, 0);
> +     if (IS_ERR(silicom_platform_dev)) {
> +             err = PTR_ERR(silicom_platform_dev);
> +             pr_err("failed to register silicom-platform device: %d\n", err);
> +             goto silicom_init_register_err;
> +     }
> +     dev = &silicom_platform_dev->dev;
> +
> +     err = dmi_check_system(silicom_dmi_ids);
> +     if (err == 0) {
> +             dev_err(dev, "No DMI match for this platform\n");
> +             err = -ENODEV;
> +             goto silicom_init_probe_err;
> +     }
> +
> +     /* Directly probe the platform driver in init since this isn't a
> +      * hotpluggable device.  That means we don't need to register a driver
> +      * that needs to wait around in memory on the chance a matching device
> +      * would get added.  Instead run once in __init so that we can free all
> +      * those resources when the __init region is wiped
> +      */
> +     err = platform_driver_probe(&silicom_platform_driver, silicom_platform_probe);
> +     if (err) {
> +             dev_err(dev, "Failed to probe platform driver %d\n", err);
> +             goto silicom_init_probe_err;
> +     }
> +
> +     return 0;
> +
> +silicom_init_probe_err:
> +     if (silicom_platform_dev) {
> +             platform_device_unregister(silicom_platform_dev);
> +             silicom_platform_dev = NULL;
> +     }
> +     if (my_dev_ctl.my_dev) {
> +             root_device_unregister(my_dev_ctl.my_dev);
> +             my_dev_ctl.my_dev = NULL;
> +     }
> +
> +silicom_init_register_err:
> +     return err;
> +}
> +
> +static void __exit silicom_platform_exit(void) {
> +     int i;
> +
> +     if (silicom_platform_dev) {
> +             platform_device_unregister(silicom_platform_dev);
> +             platform_driver_unregister(&silicom_platform_driver);
> +     }
> +
> +     if (my_dev_ctl.my_dev) {
> +             for (i = 0; i < ARRAY_SIZE(my_dev_attr); i++)
> +                     sysfs_remove_file(&my_dev_ctl.my_dev->kobj, &my_dev_attr[i].attr);
> +             root_device_unregister(my_dev_ctl.my_dev);
> +     }
> +     mutex_destroy(&mec_io_mutex);
> +}
> +
> +module_init(silicom_platform_init);
> +module_exit(silicom_platform_exit);
> +
> +MODULE_LICENSE("GPL");
> +MODULE_AUTHOR("Henry Shi <henrys@silicom-usa.com>"); 
> +MODULE_DESCRIPTION("Platform driver for Silicom network appliances");
> +
> +MODULE_DEVICE_TABLE(dmi, silicom_dmi_ids);
> +


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

* Re: [PATCH] Add Silicom Platform Driver
  2023-07-18 16:01 [PATCH] Add Silicom Platform Driver Henry Shi
  2023-07-18 16:40 ` Guenter Roeck
  2023-07-19 12:13 ` Ilpo Järvinen
@ 2023-07-25 21:02 ` Christophe JAILLET
  2023-07-28 12:59   ` Huibin Shi
  2 siblings, 1 reply; 32+ messages in thread
From: Christophe JAILLET @ 2023-07-25 21:02 UTC (permalink / raw)
  To: Henry Shi, hbshi69, tglx, mingo, bp, dave.hansen, x86, hpa,
	hdegoede, markgross, jdelvare, linux, linux-kernel,
	platform-driver-x86, linux-hwmon
  Cc: hb_shi2003, henrys, wenw

Le 18/07/2023 à 18:01, Henry Shi a écrit :
> The Silicom platform (silicom-platform) Linux driver for Swisscom
> Business Box (Swisscom BB) as well as Cordoba family products is a
> software solution designed to facilitate the efficient management
> and control of devices through the integration of various Linux
> frameworks. This platform driver provides seamless support for
> device management via the Linux LED framework, GPIO framework,
> Hardware Monitoring (HWMON), and device attributes. The Silicom
> platform driver's compatibility with these Linux frameworks allows
> applications to access and control Cordoba family devices using
> existing software that is compatible with these frameworks. This
> compatibility simplifies the development process, reduces
> dependencies on proprietary solutions, and promotes
> interoperability with other Linux-based systems and software.
> 
> Signed-off-by: Henry Shi <henryshi2018@gmail.com>
> ---

[...]

> +static int __init silicom_mc_leds_register(struct device *dev,
> +					   const struct led_classdev_mc *mc_leds)
> +{
> +	struct led_classdev_mc *led;
> +	int i, err;
> +
> +	for (i = 0; mc_leds[i].led_cdev.name; i++) {
> +		/* allocate and copy data from the init constansts */
> +		led = devm_kzalloc(dev, sizeof(struct led_classdev_mc), GFP_KERNEL);

sizeof(*led) is shorter.
Mostly a matter of taste.

Maybe even devm_kmemdup()?

> +		if (IS_ERR_OR_NULL(led)) {

if (!led)
is enough.

> +			dev_err(dev, "Failed to alloc led_classdev_mc[%d]: %ld\n", i, PTR_ERR(led));

This kind of message is useless and should be removed (checkpatch should 
warn about it)

> +			return -ENOMEM;
> +		}
> +		memcpy(led, &mc_leds[i], sizeof(*led));
> +
> +		led->subled_info = devm_kzalloc(dev, led->num_colors * sizeof(struct mc_subled),
> +						GFP_KERNEL);

Maybe even devm_kmemdup()?

> +		if (IS_ERR_OR_NULL(led->subled_info)) {

if (!led->subled_info)
is enough.

> +			dev_err(dev, "Failed to alloc subled_info[%d]: %ld\n",
> +				i, PTR_ERR(led->subled_info));

This kind of message is useless and should be removed (checkpatch should 
warn about it)

> +			return -ENOMEM;
> +		}
> +		memcpy(led->subled_info, mc_leds[i].subled_info,
> +			led->num_colors * sizeof(struct mc_subled));
> +
> +		err = devm_led_classdev_multicolor_register(dev, led);
> +		if (err) {
> +			dev_err(dev, "Failed to register[%d]: %d\n", i, err);
> +			return err;
> +		}
> +	}
> +
> +	return 0;
> +}

[...]


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

* RE: [PATCH] Add Silicom Platform Driver
  2023-07-25 21:02 ` Christophe JAILLET
@ 2023-07-28 12:59   ` Huibin Shi
  2023-07-28 14:28     ` Guenter Roeck
  2023-07-28 16:47     ` Christophe JAILLET
  0 siblings, 2 replies; 32+ messages in thread
From: Huibin Shi @ 2023-07-28 12:59 UTC (permalink / raw)
  To: Christophe JAILLET, Henry Shi, hbshi69, tglx, mingo, bp,
	dave.hansen, x86, hpa, hdegoede, markgross, jdelvare, linux,
	linux-kernel, platform-driver-x86, linux-hwmon
  Cc: hb_shi2003, Wen Wang

Christophe,

Thanks for the comments. See my comments below.

Updated patch will be sent out later after review comments from other reviewers are addressed. 

Henry
-----Original Message-----
From: Christophe JAILLET <christophe.jaillet@wanadoo.fr> 
Sent: Tuesday, July 25, 2023 5:03 PM
To: Henry Shi <henryshi2018@gmail.com>; hbshi69@hotmail.com; tglx@linutronix.de; mingo@redhat.com; bp@alien8.de; dave.hansen@linux.intel.com; x86@kernel.org; hpa@zytor.com; hdegoede@redhat.com; markgross@kernel.org; jdelvare@suse.com; linux@roeck-us.net; linux-kernel@vger.kernel.org; platform-driver-x86@vger.kernel.org; linux-hwmon@vger.kernel.org
Cc: hb_shi2003@yahoo.com; Huibin Shi <henrys@silicom-usa.com>; Wen Wang <wenw@silicom-usa.com>
Subject: Re: [PATCH] Add Silicom Platform Driver

Caution: This is an external email. Please take care when clicking links or opening attachments.


Le 18/07/2023 à 18:01, Henry Shi a écrit :
> The Silicom platform (silicom-platform) Linux driver for Swisscom 
> Business Box (Swisscom BB) as well as Cordoba family products is a 
> software solution designed to facilitate the efficient management and 
> control of devices through the integration of various Linux 
> frameworks. This platform driver provides seamless support for device 
> management via the Linux LED framework, GPIO framework, Hardware 
> Monitoring (HWMON), and device attributes. The Silicom platform 
> driver's compatibility with these Linux frameworks allows applications 
> to access and control Cordoba family devices using existing software 
> that is compatible with these frameworks. This compatibility 
> simplifies the development process, reduces dependencies on 
> proprietary solutions, and promotes interoperability with other 
> Linux-based systems and software.
>
> Signed-off-by: Henry Shi <henryshi2018@gmail.com>
> ---

[...]

> +static int __init silicom_mc_leds_register(struct device *dev,
> +                                        const struct led_classdev_mc 
> +*mc_leds) {
> +     struct led_classdev_mc *led;
> +     int i, err;
> +
> +     for (i = 0; mc_leds[i].led_cdev.name; i++) {
> +             /* allocate and copy data from the init constansts */
> +             led = devm_kzalloc(dev, sizeof(struct led_classdev_mc), 
> + GFP_KERNEL);

sizeof(*led) is shorter.
Mostly a matter of taste.

Maybe even devm_kmemdup()?

Henry: thanks. Devm_kmemdup() API requires additional argument that is not necessary of this driver. I prefer devm_kzalloc for now.

> +             if (IS_ERR_OR_NULL(led)) {

if (!led)
is enough.

Henry: OK, changed

> +                     dev_err(dev, "Failed to alloc 
> + led_classdev_mc[%d]: %ld\n", i, PTR_ERR(led));

This kind of message is useless and should be removed (checkpatch should warn about it)

Henry: OK, removed.

> +                     return -ENOMEM;
> +             }
> +             memcpy(led, &mc_leds[i], sizeof(*led));
> +
> +             led->subled_info = devm_kzalloc(dev, led->num_colors * sizeof(struct mc_subled),
> +                                             GFP_KERNEL);

Maybe even devm_kmemdup()?

> +             if (IS_ERR_OR_NULL(led->subled_info)) {

if (!led->subled_info)
is enough.

Henry: OK, changed.

> +                     dev_err(dev, "Failed to alloc subled_info[%d]: %ld\n",
> +                             i, PTR_ERR(led->subled_info));

This kind of message is useless and should be removed (checkpatch should warn about it)

Henry: OK, removed.

> +                     return -ENOMEM;
> +             }
> +             memcpy(led->subled_info, mc_leds[i].subled_info,
> +                     led->num_colors * sizeof(struct mc_subled));
> +
> +             err = devm_led_classdev_multicolor_register(dev, led);
> +             if (err) {
> +                     dev_err(dev, "Failed to register[%d]: %d\n", i, err);
> +                     return err;
> +             }
> +     }
> +
> +     return 0;
> +}

[...]


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

* Re: [PATCH] Add Silicom Platform Driver
  2023-07-28 12:59   ` Huibin Shi
@ 2023-07-28 14:28     ` Guenter Roeck
  2023-08-08 19:19       ` Huibin Shi
  2023-07-28 16:47     ` Christophe JAILLET
  1 sibling, 1 reply; 32+ messages in thread
From: Guenter Roeck @ 2023-07-28 14:28 UTC (permalink / raw)
  To: Huibin Shi, Christophe JAILLET, Henry Shi, hbshi69, tglx, mingo,
	bp, dave.hansen, x86, hpa, hdegoede, markgross, jdelvare,
	linux-kernel, platform-driver-x86, linux-hwmon
  Cc: hb_shi2003, Wen Wang

On 7/28/23 05:59, Huibin Shi wrote:
> Christophe,
> 
> Thanks for the comments. See my comments below.
> 
> Updated patch will be sent out later after review comments from other reviewers are addressed.
> 
> Henry
> -----Original Message-----
> From: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
> Sent: Tuesday, July 25, 2023 5:03 PM
> To: Henry Shi <henryshi2018@gmail.com>; hbshi69@hotmail.com; tglx@linutronix.de; mingo@redhat.com; bp@alien8.de; dave.hansen@linux.intel.com; x86@kernel.org; hpa@zytor.com; hdegoede@redhat.com; markgross@kernel.org; jdelvare@suse.com; linux@roeck-us.net; linux-kernel@vger.kernel.org; platform-driver-x86@vger.kernel.org; linux-hwmon@vger.kernel.org
> Cc: hb_shi2003@yahoo.com; Huibin Shi <henrys@silicom-usa.com>; Wen Wang <wenw@silicom-usa.com>
> Subject: Re: [PATCH] Add Silicom Platform Driver
> 
> Caution: This is an external email. Please take care when clicking links or opening attachments.
> 
> 
> Le 18/07/2023 à 18:01, Henry Shi a écrit :
>> The Silicom platform (silicom-platform) Linux driver for Swisscom
>> Business Box (Swisscom BB) as well as Cordoba family products is a
>> software solution designed to facilitate the efficient management and
>> control of devices through the integration of various Linux
>> frameworks. This platform driver provides seamless support for device
>> management via the Linux LED framework, GPIO framework, Hardware
>> Monitoring (HWMON), and device attributes. The Silicom platform
>> driver's compatibility with these Linux frameworks allows applications
>> to access and control Cordoba family devices using existing software
>> that is compatible with these frameworks. This compatibility
>> simplifies the development process, reduces dependencies on
>> proprietary solutions, and promotes interoperability with other
>> Linux-based systems and software.
>>
>> Signed-off-by: Henry Shi <henryshi2018@gmail.com>
>> ---
> 
> [...]
> 
>> +static int __init silicom_mc_leds_register(struct device *dev,
>> +                                        const struct led_classdev_mc
>> +*mc_leds) {
>> +     struct led_classdev_mc *led;
>> +     int i, err;
>> +
>> +     for (i = 0; mc_leds[i].led_cdev.name; i++) {
>> +             /* allocate and copy data from the init constansts */
>> +             led = devm_kzalloc(dev, sizeof(struct led_classdev_mc),
>> + GFP_KERNEL);
> 
> sizeof(*led) is shorter.
> Mostly a matter of taste.
> 
> Maybe even devm_kmemdup()?
> 
> Henry: thanks. Devm_kmemdup() API requires additional argument that is not necessary of this driver. I prefer devm_kzalloc for now.
> 

I am curious: What would that additional argument be ?

Guenter


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

* Re: RE: [PATCH] Add Silicom Platform Driver
  2023-07-28 12:59   ` Huibin Shi
  2023-07-28 14:28     ` Guenter Roeck
@ 2023-07-28 16:47     ` Christophe JAILLET
  2023-07-28 17:52       ` Guenter Roeck
  1 sibling, 1 reply; 32+ messages in thread
From: Christophe JAILLET @ 2023-07-28 16:47 UTC (permalink / raw)
  To: Huibin Shi, Henry Shi, hbshi69, tglx, mingo, bp, dave.hansen,
	x86, hpa, hdegoede, markgross, jdelvare, linux, linux-kernel,
	platform-driver-x86, linux-hwmon
  Cc: hb_shi2003, Wen Wang

Le 28/07/2023 à 14:59, Huibin Shi a écrit :
> Christophe,
> 
> Thanks for the comments. See my comments below.
> 
> Updated patch will be sent out later after review comments from other reviewers are addressed.
> 
> Henry
> -----Original Message-----
> From: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
> Sent: Tuesday, July 25, 2023 5:03 PM
> To: Henry Shi <henryshi2018@gmail.com>; hbshi69@hotmail.com; tglx@linutronix.de; mingo@redhat.com; bp@alien8.de; dave.hansen@linux.intel.com; x86@kernel.org; hpa@zytor.com; hdegoede@redhat.com; markgross@kernel.org; jdelvare@suse.com; linux@roeck-us.net; linux-kernel@vger.kernel.org; platform-driver-x86@vger.kernel.org; linux-hwmon@vger.kernel.org
> Cc: hb_shi2003@yahoo.com; Huibin Shi <henrys@silicom-usa.com>; Wen Wang <wenw@silicom-usa.com>
> Subject: Re: [PATCH] Add Silicom Platform Driver
> 
> Caution: This is an external email. Please take care when clicking links or opening attachments.
> 
> 
> Le 18/07/2023 à 18:01, Henry Shi a écrit :
>> The Silicom platform (silicom-platform) Linux driver for Swisscom
>> Business Box (Swisscom BB) as well as Cordoba family products is a
>> software solution designed to facilitate the efficient management and
>> control of devices through the integration of various Linux
>> frameworks. This platform driver provides seamless support for device
>> management via the Linux LED framework, GPIO framework, Hardware
>> Monitoring (HWMON), and device attributes. The Silicom platform
>> driver's compatibility with these Linux frameworks allows applications
>> to access and control Cordoba family devices using existing software
>> that is compatible with these frameworks. This compatibility
>> simplifies the development process, reduces dependencies on
>> proprietary solutions, and promotes interoperability with other
>> Linux-based systems and software.
>>
>> Signed-off-by: Henry Shi <henryshi2018@gmail.com>
>> ---
> 
> [...]
> 
>> +static int __init silicom_mc_leds_register(struct device *dev,
>> +                                        const struct led_classdev_mc
>> +*mc_leds) {
>> +     struct led_classdev_mc *led;
>> +     int i, err;
>> +
>> +     for (i = 0; mc_leds[i].led_cdev.name; i++) {
>> +             /* allocate and copy data from the init constansts */
>> +             led = devm_kzalloc(dev, sizeof(struct led_classdev_mc),
>> + GFP_KERNEL);
> 
> sizeof(*led) is shorter.
> Mostly a matter of taste.
> 
> Maybe even devm_kmemdup()?
> 
> Henry: thanks. Devm_kmemdup() API requires additional argument that is not necessary of this driver. I prefer devm_kzalloc for now.

CJ: The only additionnal parameter I can think of are the one of 
memcpy() ...

> 
>> +             if (IS_ERR_OR_NULL(led)) {
> 
> if (!led)
> is enough.
> 
> Henry: OK, changed
> 
>> +                     dev_err(dev, "Failed to alloc
>> + led_classdev_mc[%d]: %ld\n", i, PTR_ERR(led));
> 
> This kind of message is useless and should be removed (checkpatch should warn about it)
> 
> Henry: OK, removed.
> 
>> +                     return -ENOMEM;
>> +             }
>> +             memcpy(led, &mc_leds[i], sizeof(*led));

... here.

devm_kzalloc() + this memcpy() could be done with only devm_kmemdup().

This is mostly a matter of taste.

>> +
>> +             led->subled_info = devm_kzalloc(dev, led->num_colors * sizeof(struct mc_subled),
>> +                                             GFP_KERNEL);
> 
> Maybe even devm_kmemdup()?

Same...

> 
>> +             if (IS_ERR_OR_NULL(led->subled_info)) {
> 
> if (!led->subled_info)
> is enough.
> 
> Henry: OK, changed.
> 
>> +                     dev_err(dev, "Failed to alloc subled_info[%d]: %ld\n",
>> +                             i, PTR_ERR(led->subled_info));
> 
> This kind of message is useless and should be removed (checkpatch should warn about it)
> 
> Henry: OK, removed.
> 
>> +                     return -ENOMEM;
>> +             }
>> +             memcpy(led->subled_info, mc_leds[i].subled_info,
>> +                     led->num_colors * sizeof(struct mc_subled));

... here.

CJ



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

* Re: [PATCH] Add Silicom Platform Driver
  2023-07-28 16:47     ` Christophe JAILLET
@ 2023-07-28 17:52       ` Guenter Roeck
  2023-08-08 19:23         ` Huibin Shi
  0 siblings, 1 reply; 32+ messages in thread
From: Guenter Roeck @ 2023-07-28 17:52 UTC (permalink / raw)
  To: Christophe JAILLET, Huibin Shi, Henry Shi, hbshi69, tglx, mingo,
	bp, dave.hansen, x86, hpa, hdegoede, markgross, jdelvare,
	linux-kernel, platform-driver-x86, linux-hwmon
  Cc: hb_shi2003, Wen Wang

On 7/28/23 09:47, Christophe JAILLET wrote:
> Le 28/07/2023 à 14:59, Huibin Shi a écrit :
>> Christophe,
>>
>> Thanks for the comments. See my comments below.
>>
>> Updated patch will be sent out later after review comments from other reviewers are addressed.
>>
>> Henry
>> -----Original Message-----
>> From: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
>> Sent: Tuesday, July 25, 2023 5:03 PM
>> To: Henry Shi <henryshi2018@gmail.com>; hbshi69@hotmail.com; tglx@linutronix.de; mingo@redhat.com; bp@alien8.de; dave.hansen@linux.intel.com; x86@kernel.org; hpa@zytor.com; hdegoede@redhat.com; markgross@kernel.org; jdelvare@suse.com; linux@roeck-us.net; linux-kernel@vger.kernel.org; platform-driver-x86@vger.kernel.org; linux-hwmon@vger.kernel.org
>> Cc: hb_shi2003@yahoo.com; Huibin Shi <henrys@silicom-usa.com>; Wen Wang <wenw@silicom-usa.com>
>> Subject: Re: [PATCH] Add Silicom Platform Driver
>>
>> Caution: This is an external email. Please take care when clicking links or opening attachments.
>>
>>
>> Le 18/07/2023 à 18:01, Henry Shi a écrit :
>>> The Silicom platform (silicom-platform) Linux driver for Swisscom
>>> Business Box (Swisscom BB) as well as Cordoba family products is a
>>> software solution designed to facilitate the efficient management and
>>> control of devices through the integration of various Linux
>>> frameworks. This platform driver provides seamless support for device
>>> management via the Linux LED framework, GPIO framework, Hardware
>>> Monitoring (HWMON), and device attributes. The Silicom platform
>>> driver's compatibility with these Linux frameworks allows applications
>>> to access and control Cordoba family devices using existing software
>>> that is compatible with these frameworks. This compatibility
>>> simplifies the development process, reduces dependencies on
>>> proprietary solutions, and promotes interoperability with other
>>> Linux-based systems and software.
>>>
>>> Signed-off-by: Henry Shi <henryshi2018@gmail.com>
>>> ---
>>
>> [...]
>>
>>> +static int __init silicom_mc_leds_register(struct device *dev,
>>> +                                        const struct led_classdev_mc
>>> +*mc_leds) {
>>> +     struct led_classdev_mc *led;
>>> +     int i, err;
>>> +
>>> +     for (i = 0; mc_leds[i].led_cdev.name; i++) {
>>> +             /* allocate and copy data from the init constansts */
>>> +             led = devm_kzalloc(dev, sizeof(struct led_classdev_mc),
>>> + GFP_KERNEL);
>>
>> sizeof(*led) is shorter.
>> Mostly a matter of taste.
>>
>> Maybe even devm_kmemdup()?
>>
>> Henry: thanks. Devm_kmemdup() API requires additional argument that is not necessary of this driver. I prefer devm_kzalloc for now.
> 
> CJ: The only additionnal parameter I can think of are the one of memcpy() ...
> 
>>
>>> +             if (IS_ERR_OR_NULL(led)) {
>>
>> if (!led)
>> is enough.
>>
>> Henry: OK, changed
>>
>>> +                     dev_err(dev, "Failed to alloc
>>> + led_classdev_mc[%d]: %ld\n", i, PTR_ERR(led));
>>
>> This kind of message is useless and should be removed (checkpatch should warn about it)
>>
>> Henry: OK, removed.
>>
>>> +                     return -ENOMEM;
>>> +             }
>>> +             memcpy(led, &mc_leds[i], sizeof(*led));
> 
> ... here.
> 
> devm_kzalloc() + this memcpy() could be done with only devm_kmemdup().
> 
> This is mostly a matter of taste.
> 

I don't think that using (or not using) available API functions should
be a matter of taste, or the next step might be to re-implement memcpy().

Guenter


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

* RE: [PATCH] Add Silicom Platform Driver
  2023-07-19 12:13 ` Ilpo Järvinen
@ 2023-07-28 19:10   ` Huibin Shi
  2023-08-03 12:06     ` Ilpo Järvinen
  0 siblings, 1 reply; 32+ messages in thread
From: Huibin Shi @ 2023-07-28 19:10 UTC (permalink / raw)
  To: ilpo.jarvinen, tglx, mingo, bp, dave.hansen, x86, hpa, hdegoede,
	markgross, jdelvare, linux, linux-kernel, platform-driver-x86,
	linux-hwmon
  Cc: hb_shi2003, Wen Wang

Ilpo,

Thanks for the comments. See my comments below.

Updated patch will be sent out later after review comments from other reviewer are addressed. 

Henry
-----Original Message-----
From: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com> 
Sent: Wednesday, July 19, 2023 8:13 AM
To: Henry Shi <henryshi2018@gmail.com>
Cc: hbshi69@hotmail.com; tglx@linutronix.de; mingo@redhat.com; bp@alien8.de; dave.hansen@linux.intel.com; x86@kernel.org; hpa@zytor.com; hdegoede@redhat.com; markgross@kernel.org; jdelvare@suse.com; linux@roeck-us.net; LKML <linux-kernel@vger.kernel.org>; platform-driver-x86@vger.kernel.org; linux-hwmon@vger.kernel.org; hb_shi2003@yahoo.com; Huibin Shi <henrys@silicom-usa.com>; Wen Wang <wenw@silicom-usa.com>
Subject: Re: [PATCH] Add Silicom Platform Driver

Caution: This is an external email. Please take care when clicking links or opening attachments.


On Tue, 18 Jul 2023, Henry Shi wrote:

> The Silicom platform (silicom-platform) Linux driver for Swisscom 
> Business Box (Swisscom BB) as well as Cordoba family products is a 
> software solution designed to facilitate the efficient management and 
> control of devices through the integration of various Linux 
> frameworks. This platform driver provides seamless support for device 
> management via the Linux LED framework, GPIO framework, Hardware 
> Monitoring (HWMON), and device attributes. The Silicom platform 
> driver's compatibility with these Linux frameworks allows applications 
> to access and control Cordoba family devices using existing software 
> that is compatible with these frameworks. This compatibility 
> simplifies the development process, reduces dependencies on 
> proprietary solutions, and promotes interoperability with other 
> Linux-based systems and software.
>
> Signed-off-by: Henry Shi <henryshi2018@gmail.com>
> ---
>  drivers/platform/x86/Kconfig            |   12 +
>  drivers/platform/x86/Makefile           |    1 +
>  drivers/platform/x86/silicom-platform.c | 1123 
> +++++++++++++++++++++++
>  3 files changed, 1136 insertions(+)
>  create mode 100644 drivers/platform/x86/silicom-platform.c
>
> diff --git a/drivers/platform/x86/Kconfig 
> b/drivers/platform/x86/Kconfig index 22052031c719..8c0988c2b4ce 100644
> --- a/drivers/platform/x86/Kconfig
> +++ b/drivers/platform/x86/Kconfig
> @@ -188,6 +188,18 @@ config ACER_WMI
>         If you have an ACPI-WMI compatible Acer/ Wistron laptop, say Y or M
>         here.
>
> +

Extra newline

Henry: OK, removed.

> +config SILICOM_PLATFORM
> +     tristate "Silicom Edge Networking device support"
> +     depends on DMI
> +     select LEDS_CLASS_MULTICOLOR
> +     select GPIOLIB
> +     help
> +       This option enables support for the LEDs/GPIO/etc downstream of the
> +       embedded controller on Silicom "Cordoba" hardware and derivatives.
> +
> +       If you have a Silicom network appliance, say Y or M here.
> +
>  source "drivers/platform/x86/amd/Kconfig"
>
>  config ADV_SWBUTTON
> diff --git a/drivers/platform/x86/Makefile 
> b/drivers/platform/x86/Makefile index 2cafe51ec4d8..f2f5743a9e54 
> 100644
> --- a/drivers/platform/x86/Makefile
> +++ b/drivers/platform/x86/Makefile
> @@ -113,6 +113,7 @@ obj-$(CONFIG_SERIAL_MULTI_INSTANTIATE)    += serial-multi-instantiate.o
>  obj-$(CONFIG_MLX_PLATFORM)           += mlx-platform.o
>  obj-$(CONFIG_TOUCHSCREEN_DMI)                += touchscreen_dmi.o
>  obj-$(CONFIG_WIRELESS_HOTKEY)                += wireless-hotkey.o
> +obj-$(CONFIG_SILICOM_PLATFORM)          += silicom-platform.o

Use tabs like the other lines.

Henry: OK

>  obj-$(CONFIG_X86_ANDROID_TABLETS)    += x86-android-tablets/
>
>  # Intel uncore drivers
> diff --git a/drivers/platform/x86/silicom-platform.c 
> b/drivers/platform/x86/silicom-platform.c
> new file mode 100644
> index 000000000000..90431f733682
> --- /dev/null
> +++ b/drivers/platform/x86/silicom-platform.c
> @@ -0,0 +1,1123 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +//
> +// silicom-platform.c - Silicom MEC170x platform driver // // 
> +Copyright (C) 2023 Henry Shi <henrys@silicom-usa.com>
> +
> +#include <linux/dmi.h>
> +#include <linux/gpio/driver.h>
> +#include <linux/init.h>
> +#include <linux/ioport.h>
> +#include <linux/io.h>
> +#include <linux/kernel.h>
> +#include <linux/led-class-multicolor.h> #include <linux/module.h> 
> +#include <linux/hwmon.h> #include <linux/mutex.h> #include 
> +<linux/platform_device.h> #include <linux/string.h> #include 
> +<linux/thermal.h> #include <linux/kobject.h> #include <linux/sysfs.h>
> +
> +#define MEC_ADDR ((mec_io_base) + 0x02) #define MEC_DATA(byte) 
> +((mec_io_base) + 0x04 + (byte)) #define EC_ADDR_LSB MEC_ADDR #define 
> +EC_ADDR_MSB ((mec_io_base) + 0x03) #define SILICOM_MEC_MAGIC 0x5a

> +#define OFFSET_BIT_TO_CHANNEL(off, bit) ((((off) + 0x014) << 3) | 
> +(bit)) #define CHANNEL_TO_OFFSET(chan) (((chan) >> 3) - 0x14)

So you have two fields, offset and "bit", whatever that is. It would seem named define with GENMASK() for these two fields and FIELD_PREP() is appropriate here. And FIELD_GET() (and perhaps also FIELD_PREP()) used in the code below where appropriate.

0x14 should be named with a define.

> +#define CHANNEL_TO_BIT(chan) ((chan) & 0x07)

Unused.

Henry: OK, removed.

> +
> +static DEFINE_MUTEX(mec_io_mutex);
> +static int mec_io_base, mec_io_len;
> +
> +struct silicom_fan_control_data {
> +     struct   device *hdev;
> +     int      temp;
> +     int      fan_speed;
> +};
> +
> +static const struct hwmon_channel_info *silicom_fan_control_info[] = {
> +     HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT | HWMON_F_LABEL),
> +     HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT | HWMON_T_LABEL),
> +     NULL
> +};
> +
> +struct silicom_device_control_data {
> +     struct   device *my_dev;
> +     int      efuse_status;
> +     int      uc_version;
> +     int      power_cycle;
> +};
> +static struct silicom_device_control_data my_dev_ctl;
> +
> +struct silicom_platform_info {
> +     int io_base;
> +     int io_len;
> +     struct led_classdev_mc *led_info;
> +     struct gpio_chip *gpiochip;
> +     u8 *gpio_channels;
> +     u16 ngpio;
> +};
> +
> +static const char * const plat_0222_gpio_names[] = {
> +     "AUTOM0_SFP_TX_FAULT",
> +     "SLOT2_LED_OUT",
> +     "SIM_M2_SLOT2_B_DET",
> +     "SIM_M2_SLOT2_A_DET",
> +     "SLOT1_LED_OUT",
> +     "SIM_M2_SLOT1_B_DET",
> +     "SIM_M2_SLOT1_A_DET",
> +     "SLOT0_LED_OUT",
> +     "WAN_SFP0_RX_LOS",
> +     "WAN_SFP0_PRSNT_N",
> +     "WAN_SFP0_TX_FAULT",
> +     "AUTOM1_SFP_RX_LOS",
> +     "AUTOM1_SFP_PRSNT_N",
> +     "AUTOM1_SFP_TX_FAULT",
> +     "AUTOM0_SFP_RX_LOS",
> +     "AUTOM0_SFP_PRSNT_N",
> +     "WAN_SFP1_RX_LOS",
> +     "WAN_SFP1_PRSNT_N",
> +     "WAN_SFP1_TX_FAULT",
> +     "SIM_M2_SLOT1_MUX_SEL",
> +     "W_DISABLE_M2_SLOT1_N",
> +     "W_DISABLE_MPCIE_SLOT0_N",
> +     "W_DISABLE_M2_SLOT0_N",
> +     "BT_COMMAND_MODE",
> +     "WAN_SFP1_TX_DISABLE",
> +     "WAN_SFP0_TX_DISABLE",
> +     "AUTOM1_SFP_TX_DISABLE",
> +     "AUTOM0_SFP_TX_DISABLE",
> +     "SIM_M2_SLOT2_MUX_SEL",
> +     "W_DISABLE_M2_SLOT2_N",
> +     "RST_CTL_M2_SLOT_1_N",
> +     "RST_CTL_M2_SLOT_2_N",
> +     "PM_USB_PWR_EN_BOT",
> +     "PM_USB_PWR_EN_TOP",
> +};
> +
> +static u8 plat_0222_gpio_channels[] = {
> +     OFFSET_BIT_TO_CHANNEL(0x00, 0),
> +     OFFSET_BIT_TO_CHANNEL(0x00, 1),
> +     OFFSET_BIT_TO_CHANNEL(0x00, 2),
> +     OFFSET_BIT_TO_CHANNEL(0x00, 3),
> +     OFFSET_BIT_TO_CHANNEL(0x00, 4),
> +     OFFSET_BIT_TO_CHANNEL(0x00, 5),
> +     OFFSET_BIT_TO_CHANNEL(0x00, 6),
> +     OFFSET_BIT_TO_CHANNEL(0x00, 7),
> +     OFFSET_BIT_TO_CHANNEL(0x01, 0),
> +     OFFSET_BIT_TO_CHANNEL(0x01, 1),
> +     OFFSET_BIT_TO_CHANNEL(0x01, 2),
> +     OFFSET_BIT_TO_CHANNEL(0x01, 3),
> +     OFFSET_BIT_TO_CHANNEL(0x01, 4),
> +     OFFSET_BIT_TO_CHANNEL(0x01, 5),
> +     OFFSET_BIT_TO_CHANNEL(0x01, 6),
> +     OFFSET_BIT_TO_CHANNEL(0x01, 7),
> +     OFFSET_BIT_TO_CHANNEL(0x02, 0),
> +     OFFSET_BIT_TO_CHANNEL(0x02, 1),
> +     OFFSET_BIT_TO_CHANNEL(0x02, 2),
> +     OFFSET_BIT_TO_CHANNEL(0x09, 0),
> +     OFFSET_BIT_TO_CHANNEL(0x09, 1),
> +     OFFSET_BIT_TO_CHANNEL(0x09, 2),
> +     OFFSET_BIT_TO_CHANNEL(0x09, 3),
> +     OFFSET_BIT_TO_CHANNEL(0x0a, 0),
> +     OFFSET_BIT_TO_CHANNEL(0x0a, 1),
> +     OFFSET_BIT_TO_CHANNEL(0x0a, 2),
> +     OFFSET_BIT_TO_CHANNEL(0x0a, 3),
> +     OFFSET_BIT_TO_CHANNEL(0x0a, 4),
> +     OFFSET_BIT_TO_CHANNEL(0x0a, 5),
> +     OFFSET_BIT_TO_CHANNEL(0x0a, 6),
> +     OFFSET_BIT_TO_CHANNEL(0x0b, 0),
> +     OFFSET_BIT_TO_CHANNEL(0x0b, 1),
> +     OFFSET_BIT_TO_CHANNEL(0x0b, 2),
> +     OFFSET_BIT_TO_CHANNEL(0x0b, 3),
> +};
> +
> +static int silicom_gpio_get_direction(struct gpio_chip *gc, unsigned 
> +int offset); static int silicom_gpio_direction_input(struct gpio_chip 
> +*gc, unsigned int offset); static int 
> +silicom_gpio_direction_output(struct gpio_chip *gc, unsigned int 
> +offset, int value); static int silicom_gpio_get(struct gpio_chip *gc, 
> +unsigned int offset); static void silicom_gpio_set(struct gpio_chip *gc, unsigned int offset, int value); static void silicom_mec_led_mc_brightness_set(struct led_classdev *led_cdev,
> +                                           enum led_brightness 
> +brightness); static enum led_brightness 
> +silicom_mec_led_mc_brightness_get(struct led_classdev *led_cdev);

Try to organize the code such that no forward declarations are necessary.

Henry: OK.

> +static struct platform_device *silicom_platform_dev; static struct 
> +led_classdev_mc *silicom_led_info __initdata; static struct gpio_chip 
> +*silicom_gpiochip __initdata; static u8 *silicom_gpio_channels 
> +__initdata; static struct gpio_chip silicom_gpio_chip = {
> +     .label = "silicom-gpio",
> +     .get_direction = silicom_gpio_get_direction,
> +     .direction_input = silicom_gpio_direction_input,
> +     .direction_output = silicom_gpio_direction_output,
> +     .get = silicom_gpio_get,
> +     .set = silicom_gpio_set,
> +     .base = -1,
> +     .ngpio = ARRAY_SIZE(plat_0222_gpio_channels),
> +     .names = plat_0222_gpio_names,
> +     /* We're using a mutex to protect the indirect access, so we can sleep if the lock blocks */
> +     .can_sleep = true,
> +};
> +
> +static struct mc_subled plat_0222_wan_mc_subled_info[] __initdata = {
> +     {
> +             .color_index = LED_COLOR_ID_WHITE,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0c, 7),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_YELLOW,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0c, 6),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_RED,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0c, 5),
> +     },
> +};
> +
> +static struct mc_subled plat_0222_sys_mc_subled_info[] __initdata = {
> +     {
> +             .color_index = LED_COLOR_ID_WHITE,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0c, 4),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_AMBER,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0c, 3),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_RED,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0c, 2),
> +     },
> +};
> +
> +static struct mc_subled plat_0222_stat1_mc_subled_info[] __initdata = {
> +     {
> +             .color_index = LED_COLOR_ID_RED,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0c, 1),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_GREEN,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0c, 0),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_BLUE,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0d, 7),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_YELLOW,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0d, 6),
> +     },
> +};
> +
> +static struct mc_subled plat_0222_stat2_mc_subled_info[] __initdata = {
> +     {
> +             .color_index = LED_COLOR_ID_RED,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0d, 5),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_GREEN,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0d, 4),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_BLUE,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0d, 3),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_YELLOW,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0d, 2),
> +     },
> +};
> +
> +static struct mc_subled plat_0222_stat3_mc_subled_info[] __initdata = {
> +     {
> +             .color_index = LED_COLOR_ID_RED,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0d, 1),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_GREEN,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0d, 0),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_BLUE,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0e, 1),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_YELLOW,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0e, 0),
> +     },
> +};
> +
> +static struct led_classdev_mc plat_0222_mc_led_info[] __initdata = {
> +     {
> +             .led_cdev = {
> +                     .name = "multicolor:wan",
> +                     .brightness = 0,
> +                     .max_brightness = 1,
> +                     .brightness_set = silicom_mec_led_mc_brightness_set,
> +                     .brightness_get = silicom_mec_led_mc_brightness_get,
> +             },
> +             .num_colors = ARRAY_SIZE(plat_0222_wan_mc_subled_info),
> +             .subled_info = plat_0222_wan_mc_subled_info,
> +     },
> +     {
> +             .led_cdev = {
> +                     .name = "multicolor:sys",
> +                     .brightness = 0,
> +                     .max_brightness = 1,
> +                     .brightness_set = silicom_mec_led_mc_brightness_set,
> +                     .brightness_get = silicom_mec_led_mc_brightness_get,
> +             },
> +             .num_colors = ARRAY_SIZE(plat_0222_sys_mc_subled_info),
> +             .subled_info = plat_0222_sys_mc_subled_info,
> +     },
> +     {
> +             .led_cdev = {
> +                     .name = "multicolor:stat1",
> +                     .brightness = 0,
> +                     .max_brightness = 1,
> +                     .brightness_set = silicom_mec_led_mc_brightness_set,
> +                     .brightness_get = silicom_mec_led_mc_brightness_get,
> +             },
> +             .num_colors = ARRAY_SIZE(plat_0222_stat1_mc_subled_info),
> +             .subled_info = plat_0222_stat1_mc_subled_info,
> +     },
> +     {
> +             .led_cdev = {
> +                     .name = "multicolor:stat2",
> +                     .brightness = 0,
> +                     .max_brightness = 1,
> +                     .brightness_set = silicom_mec_led_mc_brightness_set,
> +                     .brightness_get = silicom_mec_led_mc_brightness_get,
> +             },
> +             .num_colors = ARRAY_SIZE(plat_0222_stat2_mc_subled_info),
> +             .subled_info = plat_0222_stat2_mc_subled_info,
> +     },
> +     {
> +             .led_cdev = {
> +                     .name = "multicolor:stat3",
> +                     .brightness = 0,
> +                     .max_brightness = 1,
> +                     .brightness_set = silicom_mec_led_mc_brightness_set,
> +                     .brightness_get = silicom_mec_led_mc_brightness_get,
> +             },
> +             .num_colors = ARRAY_SIZE(plat_0222_stat3_mc_subled_info),
> +             .subled_info = plat_0222_stat3_mc_subled_info,
> +     },
> +     { },
> +};
> +
> +static struct silicom_platform_info silicom_plat_0222_cordoba_info __initdata = {
> +     .io_base = 0x800,
> +     .io_len = 8,
> +     .led_info = plat_0222_mc_led_info,
> +     .gpiochip = &silicom_gpio_chip,
> +     .gpio_channels = plat_0222_gpio_channels,
> +     /* The original generic cordoba does not have the last 4 outputs of the plat_0222 BB variant,
> +      * the rest are the same, so use the same longer list, but ignore the last entries here
> +      */
> +     .ngpio = ARRAY_SIZE(plat_0222_gpio_channels),
> +
> +};
> +
> +static struct mc_subled cordoba_fp_left_mc_subled_info[] __initdata = {
> +     {
> +             .color_index = LED_COLOR_ID_RED,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x08, 6),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_GREEN,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x08, 5),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_BLUE,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x09, 7),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_AMBER,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x09, 4),
> +     },
> +};
> +
> +static struct mc_subled cordoba_fp_center_mc_subled_info[] __initdata = {
> +     {
> +             .color_index = LED_COLOR_ID_RED,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x08, 7),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_GREEN,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x08, 4),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_BLUE,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x08, 3),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_AMBER,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x09, 6),
> +     },
> +};
> +
> +static struct mc_subled cordoba_fp_right_mc_subled_info[] __initdata = {
> +     {
> +             .color_index = LED_COLOR_ID_RED,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x08, 2),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_GREEN,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x08, 1),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_BLUE,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x08, 0),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_AMBER,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x09, 5),
> +     },
> +};
> +
> +static struct led_classdev_mc cordoba_mc_led_info[] __initdata = {
> +     {
> +             .led_cdev = {
> +                     .name = "multicolor:fp_left",
> +                     .brightness = 0,
> +                     .max_brightness = 1,
> +                     .brightness_set = silicom_mec_led_mc_brightness_set,
> +                     .brightness_get = silicom_mec_led_mc_brightness_get,
> +             },
> +             .num_colors = ARRAY_SIZE(cordoba_fp_left_mc_subled_info),
> +             .subled_info = cordoba_fp_left_mc_subled_info,
> +     },
> +     {
> +             .led_cdev = {
> +                     .name = "multicolor:fp_center",
> +                     .brightness = 0,
> +                     .max_brightness = 1,
> +                     .brightness_set = silicom_mec_led_mc_brightness_set,
> +                     .brightness_get = silicom_mec_led_mc_brightness_get,
> +             },
> +             .num_colors = ARRAY_SIZE(cordoba_fp_center_mc_subled_info),
> +             .subled_info = cordoba_fp_center_mc_subled_info,
> +     },
> +     {
> +             .led_cdev = {
> +                     .name = "multicolor:fp_right",
> +                     .brightness = 0,
> +                     .max_brightness = 1,
> +                     .brightness_set = silicom_mec_led_mc_brightness_set,
> +                     .brightness_get = silicom_mec_led_mc_brightness_get,
> +             },
> +             .num_colors = ARRAY_SIZE(cordoba_fp_right_mc_subled_info),
> +             .subled_info = cordoba_fp_right_mc_subled_info,
> +     },
> +     { },
> +};
> +
> +static struct silicom_platform_info silicom_generic_cordoba_info __initdata = {
> +     .io_base = 0x800,
> +     .io_len = 8,
> +     .led_info = cordoba_mc_led_info,
> +     .gpiochip = &silicom_gpio_chip,
> +     .gpio_channels = plat_0222_gpio_channels,
> +     .ngpio = ARRAY_SIZE(plat_0222_gpio_channels),
> +};
> +
> +static struct platform_driver silicom_platform_driver = {
> +     .driver = {
> +             .name = "silicom-platform",
> +     },
> +};
> +
> +void lock_io_modules(void)
> +{
> +     mutex_lock(&mec_io_mutex);
> +}
> +EXPORT_SYMBOL(lock_io_modules);
> +
> +void unlock_io_modules(void)
> +{
> +     mutex_unlock(&mec_io_mutex);
> +}
> +EXPORT_SYMBOL(unlock_io_modules);
> +
> +static ssize_t efuse_status_show(struct device *dev, struct device_attribute *attr,
> +                   char *buf)
> +{
> +     u32 reg;
> +     u32 bank = 0;
> +     u32 offset = 0x28;

Why is this not a named define?

No need for bank & offset variables.

Henry: OK, added new named define and removed these three variables.

> +     u32 byte_pos = 0;
> +
> +     mutex_lock(&mec_io_mutex);
> +     /* Select memory region */
> +     outb(bank, EC_ADDR_MSB);
> +     outb(offset, EC_ADDR_LSB);
> +
> +     /* Get current date from the address */
> +     reg = inl(MEC_DATA(byte_pos));
> +     mutex_unlock(&mec_io_mutex);
> +
> +     my_dev_ctl.efuse_status = reg & 0x1;
> +
> +     return sprintf(buf, "%d\n", my_dev_ctl.efuse_status); }
> +
> +static ssize_t uc_version_show(struct device *dev,
> +                            struct device_attribute *attr,
> +                            char *buf) {
> +     u32 reg;
> +     u32 bank = 0;
> +     u32 offset = 0x0;

Ditto.

Henry: OK, added new named define and removed these three variables.

> +     u32 byte_pos = 0;
> +     int uc_version;
> +
> +     mutex_lock(&mec_io_mutex);
> +     outb(bank, EC_ADDR_MSB);
> +     outb(offset, EC_ADDR_LSB);
> +
> +     reg = inl(MEC_DATA(byte_pos));
> +     mutex_unlock(&mec_io_mutex);
> +
> +     uc_version = (reg >> 8) & 0xFF;

Define a named mask with GENMASK() and use FIELD_GET().

Henry: OK, changed code to use these two macro.

> +     if (uc_version >= 64 && uc_version < 128) {
> +             uc_version = uc_version - 64;

Testing for a bit (or two bit field inside the version actually considering the other if too) in version and then handcrafting & ~THATBIT?

Henry: OK, good point. Code was changed as suggested.

> +             if (uc_version < 10)
> +                     uc_version = 100 + uc_version;
> +             else
> +                     uc_version = 100 + 10 * (uc_version / 10) + 
> + uc_version % 10;

Why is this if necessary? Doesn't the latter yield the same value as the first one when uc_version < 10?

Henry: good point. Actually, the purpose of initial code is for uc version to return 1.x.y or 2.x.y, which is not an integer. I have changed the code to remove if check. 

> +     } else if (uc_version >= 128 && uc_version < 192) {
> +             uc_version = uc_version - 128;
> +             if (uc_version < 10)
> +                     uc_version = 200 + uc_version;
> +             else
> +                     uc_version = 200 + 10 * (uc_version / 10) + 
> + uc_version % 10;

Similar comments to this block.

Henry: good point. Actually, the purpose of initial code is for uc version to return 1.x.y or 2.x.y, which is not an integer. I have changed the code to remove if check.

> +     }
> +     my_dev_ctl.uc_version = uc_version;
> +     return sprintf(buf, "%d\n", my_dev_ctl.uc_version); }
> +
> +static ssize_t power_cycle_show(struct device *dev,
> +                             struct device_attribute *attr,
> +                             char *buf) {
> +     return sprintf(buf, "%d\n", my_dev_ctl.power_cycle); }
> +
> +static void powercycle_uc(void)
> +{
> +     u32 bank = 0;
> +     u32 offset = 0x24;

Named define, no need for these variables.

Henry: OK, added new named define and removed these three variables.

> +     u32 byte_pos = 0;
> +
> +     mutex_lock(&mec_io_mutex);
> +     /* Select memory region */
> +     outb(bank, EC_ADDR_MSB);
> +     outb(offset, EC_ADDR_LSB);
> +
> +     /* Set to 1 for current date from the address */
> +     outb(1, MEC_DATA(byte_pos));

Hmm, this is really misleading. MEC_DATA() seems to calculate _an offset_, not data? Name things accordingly.

Henry: OK, changed the defined to  "#define MEC_DATA(offset) ((mec_io_base) + 0x04 + (offset))"

> +     mutex_unlock(&mec_io_mutex);
> +}
> +
> +static ssize_t power_cycle_store(struct device *dev, struct device_attribute *attr,
> +                              const char *buf, size_t count) {
> +     if (sscanf(buf, "%du", &my_dev_ctl.power_cycle) != 1) {
> +             dev_err(dev, "Failed to read power_cycle\n");
> +             return -EINVAL;
> +     }
> +     if (my_dev_ctl.power_cycle > 0)
> +             powercycle_uc();
> +
> +     return count;
> +}
> +
> +static struct device_attribute my_dev_attr[] = {
> +     {
> +             .attr = {.name = "efuse_status", .mode = 0644},
> +             .show = efuse_status_show,
> +             .store = NULL
> +     },
> +     {
> +             .attr = {.name = "uc_version", .mode = 0644},
> +             .show = uc_version_show,
> +             .store = NULL
> +     },
> +     {
> +             .attr = {.name = "power_cycle", .mode = 0644},
> +             .show = power_cycle_show,
> +             .store = power_cycle_store
> +     },
> +};
> +
> +static int silicom_gpio_get_direction(struct gpio_chip *gc, unsigned 
> +int offset) {
> +     u8 *channels = gpiochip_get_data(gc);
> +
> +     /* Input registers have offsets between [0x00, 0x07] */
> +     if (CHANNEL_TO_OFFSET(channels[offset]) < 0x08)
> +             return GPIO_LINE_DIRECTION_IN;
> +
> +     return GPIO_LINE_DIRECTION_OUT;
> +}
> +
> +static int silicom_gpio_direction_input(struct gpio_chip *gc, 
> +unsigned int offset) {
> +     int direction = silicom_gpio_get_direction(gc, offset);
> +
> +     return direction == GPIO_LINE_DIRECTION_IN ? 0 : -EINVAL; }
> +
> +static void silicom_gpio_set(struct gpio_chip *gc, unsigned int 
> +offset, int value) {
> +     u8 *channels = gpiochip_get_data(gc);
> +     int direction = silicom_gpio_get_direction(gc, offset);
> +     int channel = channels[offset];
> +     u8 reg;
> +
> +     if (direction == GPIO_LINE_DIRECTION_IN)
> +             return;
> +
> +     mutex_lock(&mec_io_mutex);
> +     /* Get the dword offset from the channel */
> +     outb((channel >> 3) & 0xfc, MEC_ADDR);
> +
> +     /* Get the current register */
> +     reg = inb(MEC_DATA((channel >> 3) & 0x03));
> +     if (value == 0)
> +             reg &= ~(1 << (channel & 0x7));
> +     else if (value > 0)
> +             reg |= 1 << (channel & 0x7);
> +     else
> +             pr_err("Invalid GPIO value: %d\n", value);
> +     outb(reg, MEC_DATA((channel >> 3) & 0x03));
> +     mutex_unlock(&mec_io_mutex);
> +}
> +
> +static int silicom_gpio_direction_output(struct gpio_chip *gc, 
> +unsigned int offset, int value) {
> +     int direction = silicom_gpio_get_direction(gc, offset);
> +
> +     if (direction == GPIO_LINE_DIRECTION_IN)
> +             return -EINVAL;
> +
> +     silicom_gpio_set(gc, offset, value);
> +
> +     return 0;
> +}
> +
> +static int silicom_gpio_get(struct gpio_chip *gc, unsigned int 
> +offset) {
> +     u8 *channels = gpiochip_get_data(gc);
> +     int channel = channels[offset];
> +     u8 reg;
> +
> +     mutex_lock(&mec_io_mutex);
> +     /* Get the dword offset from the channel */
> +     outb((channel >> 3) & 0xfc, MEC_ADDR);
> +
> +     /* Get the current register */
> +     reg = inb(MEC_DATA((channel >> 3) & 0x03));
> +     mutex_unlock(&mec_io_mutex);
> +
> +     return (reg >> (channel & 0x7)) & 0x01; }
> +
> +static int __init silicom_mc_leds_register(struct device *dev,
> +                                        const struct led_classdev_mc 
> +*mc_leds) {
> +     struct led_classdev_mc *led;
> +     int i, err;
> +
> +     for (i = 0; mc_leds[i].led_cdev.name; i++) {
> +             /* allocate and copy data from the init constansts */

There's a typo in the constansts but the code seems obvious enough to not need a comment at all.

Henry: OK, removed the comment line.

> +             led = devm_kzalloc(dev, sizeof(struct led_classdev_mc), GFP_KERNEL);
> +             if (IS_ERR_OR_NULL(led)) {
> +                     dev_err(dev, "Failed to alloc led_classdev_mc[%d]: %ld\n", i, PTR_ERR(led));
> +                     return -ENOMEM;
> +             }
> +             memcpy(led, &mc_leds[i], sizeof(*led));
> +
> +             led->subled_info = devm_kzalloc(dev, led->num_colors * 
> + sizeof(struct mc_subled),

array_size() from linux/overflow.h

Henry: OK, changed to arary_size()

> +                                             GFP_KERNEL);
> +             if (IS_ERR_OR_NULL(led->subled_info)) {
> +                     dev_err(dev, "Failed to alloc subled_info[%d]: %ld\n",
> +                             i, PTR_ERR(led->subled_info));
> +                     return -ENOMEM;
> +             }
> +             memcpy(led->subled_info, mc_leds[i].subled_info,
> +                     led->num_colors * sizeof(struct mc_subled));

array_size()

Henry: OK, changed to arary_size()

Although a local variable could be used to hold it as it's used for alloc and this line.

Henry: Good point, allocated a local variable. 

> +
> +             err = devm_led_classdev_multicolor_register(dev, led);
> +             if (err) {
> +                     dev_err(dev, "Failed to register[%d]: %d\n", i, err);
> +                     return err;
> +             }
> +     }
> +
> +     return 0;
> +}
> +
> +static void silicom_mec_led_set(int channel, int on) {
> +     u8 reg;
> +
> +     mutex_lock(&mec_io_mutex);
> +     /* Get the dword offset from the channel */
> +     outb((channel >> 3) & 0xfc, MEC_ADDR);
> +     /* Get the current LED settings */
> +     reg = inb(MEC_DATA((channel >> 3) & 0x03));
> +
> +     /* Outputs are active low, so clear the bit for on, or set it for off */
> +     if (on)
> +             reg &= ~(1 << (channel & 0x7));
> +     else
> +             reg |= 1 << (channel & 0x7);
> +
> +     /* Write back the updated register */
> +     outb(reg, MEC_DATA((channel >> 3) & 0x03));
> +
> +     mutex_unlock(&mec_io_mutex);

There's an identical code block in silicom_gpio_set(). Why not simply call this from silicom_gpio_set()?

Henry: these two block of date are different: silicom_mec_led_set(xxx, int on), argument on, when on is 1, it means turn on LED; 0 means turn off LED. For  silicom_gpio_set(), the argument value, which is really the 0 or 1 bit value that passed to reg. 

> +}
> +
> +static void silicom_mec_led_mc_brightness_set(struct led_classdev *led_cdev,
> +                                           enum led_brightness 
> +brightness) {
> +     struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(led_cdev);
> +     int i;
> +
> +     led_mc_calc_color_components(mc_cdev, brightness);
> +
> +     for (i = 0; i < mc_cdev->num_colors; i++) {
> +             silicom_mec_led_set(mc_cdev->subled_info[i].channel,
> +                                 mc_cdev->subled_info[i].brightness);
> +     }
> +}
> +
> +static enum led_brightness silicom_mec_led_get(int channel) {
> +     u8 reg;
> +
> +     mutex_lock(&mec_io_mutex);
> +     /* Get the dword offset of the register for this LED from the channel */
> +     outb((channel >> 3) & 0xfc, MEC_ADDR);
> +     /* Get the current LED settings */
> +     reg = inb(MEC_DATA((channel >> 3) & 0x03));
> +     mutex_unlock(&mec_io_mutex);
> +
> +     /* Outputs are active low */
> +     return reg & (1 << (channel & 0x7)) ? LED_OFF : LED_ON;

Looks identical to a part of silicom_gpio_get(). Perhaps it should call this function??

Henry: some of the code block of these two functions are similar. But function return types are different. Gpio has status of 0 or 1, but LED has status of LED_OFF and LED_ON.

> +}
> +
> +static enum led_brightness silicom_mec_led_mc_brightness_get(struct 
> +led_classdev *led_cdev) {
> +     struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(led_cdev);
> +     enum led_brightness brightness = LED_OFF;
> +     int i;
> +
> +     for (i = 0; i < mc_cdev->num_colors; i++) {
> +             mc_cdev->subled_info[i].brightness =
> +                     
> + silicom_mec_led_get(mc_cdev->subled_info[i].channel);
> +
> +             /* Mark the overall brightness as LED_ON if any of the subleds are on */
> +             if (mc_cdev->subled_info[i].brightness != LED_OFF)
> +                     brightness = LED_ON;
> +     }
> +
> +     return brightness;
> +}
> +
> +
> +static u32 rpm_get(void)
> +{
> +     u32 reg;
> +     u32 bank = 0;
> +     u32 offset = 0xc;

Named with a define. Remove unnecessary local vars.

Henry: OK, added new named define and removed these three variables.

> +     u32 byte_pos = 0;
> +
> +     mutex_lock(&mec_io_mutex);
> +     /* Select memory region */
> +     outb(bank, EC_ADDR_MSB);
> +     outb(offset, EC_ADDR_LSB);
> +     /* Get current date from the address */
> +     reg = inw(MEC_DATA(byte_pos));
> +     mutex_unlock(&mec_io_mutex);
> +
> +     return reg;
> +}
> +
> +static u32 temp_get(void)
> +{
> +     u32 reg;
> +     u32 bank = 0;
> +     u32 offset = 0xc;
> +     u32 byte_pos = 0;

Ditto.

Henry: OK, added new named define and removed these three variables.

> +     mutex_lock(&mec_io_mutex);
> +     /* Select memory region */
> +     outb(bank, EC_ADDR_MSB);
> +     outb(offset, EC_ADDR_LSB);
> +     /* Get current date from the address */
> +     reg = inl(MEC_DATA(byte_pos));
> +     mutex_unlock(&mec_io_mutex);
> +
> +     return (reg >> 16) / 10;

Should that >> 16 do a FIELD_GET() for a named field, I suspect it should?

> +}
> +
> +static umode_t silicom_fan_control_fan_is_visible(const u32 attr) {
> +     switch (attr) {
> +     case hwmon_fan_input:
> +     case hwmon_fan_label:
> +             return 0444;
> +     default:
> +             return 0;
> +     }
> +}
> +
> +static umode_t silicom_fan_control_temp_is_visible(const u32 attr) {
> +     switch (attr) {
> +     case hwmon_temp_input:
> +     case hwmon_temp_label:
> +             return 0444;
> +     default:
> +             return 0;
> +     }
> +}
> +
> +static int silicom_fan_control_read_fan(struct device *dev, u32 attr, 
> +long *val) {
> +     struct silicom_fan_control_data *ctl = dev_get_drvdata(dev);
> +
> +     switch (attr) {
> +     case hwmon_fan_input:
> +             ctl->fan_speed = rpm_get();
> +             *val = ctl->fan_speed;
> +             return 0;
> +     default:
> +             return -EOPNOTSUPP;
> +     }
> +}
> +
> +static int silicom_fan_control_read_temp(struct device *dev, u32 
> +attr, long *val) {
> +     struct silicom_fan_control_data *ctl = dev_get_drvdata(dev);
> +
> +     switch (attr) {
> +     case hwmon_temp_input:
> +             ctl->temp = temp_get();
> +             *val = ctl->temp;
> +             return 0;
> +     default:
> +             return -EOPNOTSUPP;
> +     }
> +}
> +
> +static umode_t silicom_fan_control_is_visible(const void *data,
> +                                       enum hwmon_sensor_types type,
> +                                       u32 attr, int channel) {
> +     switch (type) {
> +     case hwmon_fan:
> +             return silicom_fan_control_fan_is_visible(attr);
> +     case hwmon_temp:
> +             return silicom_fan_control_temp_is_visible(attr);
> +     default:
> +             return 0;
> +     }
> +}
> +
> +static int silicom_fan_control_read(struct device *dev, enum hwmon_sensor_types type,
> +                                 u32 attr, int channel, long *val) {
> +     switch (type) {
> +     case hwmon_fan:
> +             return silicom_fan_control_read_fan(dev, attr, val);
> +     case hwmon_temp:
> +             return silicom_fan_control_read_temp(dev, attr, val);
> +     default:
> +             return -EOPNOTSUPP;
> +     }
> +}
> +
> +static int silicom_fan_control_read_labels(struct device *dev, enum hwmon_sensor_types type,
> +                                        u32 attr, int channel, const 
> +char **str) {
> +     switch (type) {
> +     case hwmon_fan:
> +             *str = "Fan Speed (RPM)";
> +             return 0;
> +     case hwmon_temp:
> +             *str = "Thermostat Sensor";
> +             return 0;
> +     default:
> +             return -EOPNOTSUPP;
> +     }
> +}
> +
> +static int silicom_fan_control_write(struct device *dev, enum hwmon_sensor_types type,
> +                                  u32 attr, int channel, long val) {
> +     return 0;
> +}
> +
> +static const struct hwmon_ops silicom_fan_control_hwmon_ops = {
> +     .is_visible = silicom_fan_control_is_visible,
> +     .read = silicom_fan_control_read,
> +     .write = silicom_fan_control_write,
> +     .read_string = silicom_fan_control_read_labels, };
> +
> +static const struct hwmon_chip_info silicom_chip_info = {
> +     .ops = &silicom_fan_control_hwmon_ops,
> +     .info = silicom_fan_control_info, };
> +
> +static int __init silicom_platform_probe(struct platform_device 
> +*device) {
> +     int i, err;
> +     u8 magic, ver;
> +     struct silicom_fan_control_data *ctl;
> +     const char *name = "Silocom_Fan_Monitor";
> +     const char *dev_name = "Silicom_platform";
> +
> +     mec_io_base = 0x0800;
> +     mec_io_len = 8;
> +     if (!devm_request_region(&device->dev, mec_io_base, mec_io_len, "mec")) {
> +             dev_err(&device->dev, "couldn't reserve MEC io ports\n");
> +             return -EBUSY;
> +     }
> +
> +     /* Sanity check magic number read for EC */
> +     outb(0x00, MEC_ADDR);
> +     magic = inb(MEC_DATA(0));
> +     ver = inb(MEC_DATA(1));

0 and 1 should be named with defines.

OK, added #define DEFAULT_CHAN_LO 0
#define DEFAULT_CHAN_HI 0

> +     dev_dbg(&device->dev, "EC magic 0x%02x, version 0x%02x\n", 
> + magic, ver);
> +
> +     if (magic != SILICOM_MEC_MAGIC) {
> +             dev_err(&device->dev, "Bad EC magic 0x%02x!\n", magic);
> +             return -ENODEV;
> +     }
> +
> +     if (silicom_led_info) {

How can this be NULL?

Henry: OK, no need for checking. Removed.

> +             err = silicom_mc_leds_register(&device->dev, silicom_led_info);
> +             if (err) {
> +                     dev_err(&device->dev, "Failed to register LEDs\n");
> +                     return err;
> +             }
> +     }
> +
> +     if (silicom_gpiochip) {

How can this be NULL?

Henry: OK, no need to check for NULL. Removed.

> +             err = devm_gpiochip_add_data(&device->dev, silicom_gpiochip, silicom_gpio_channels);
> +             if (err) {
> +                     dev_err(&device->dev, "Failed to register gpiochip: %d\n", err);
> +                     return err;
> +             }
> +     }
> +
> +     ctl = devm_kzalloc(&device->dev, sizeof(*ctl), GFP_KERNEL);
> +     if (!ctl)
> +             return -ENOMEM;
> +
> +     ctl->hdev = devm_hwmon_device_register_with_info(&device->dev, name, ctl,
> +                             &silicom_chip_info, NULL);
> +
> +     my_dev_ctl.my_dev = root_device_register(dev_name);
> +     for (i = 0; i < ARRAY_SIZE(my_dev_attr); i++) {
> +             err = sysfs_create_file(&my_dev_ctl.my_dev->kobj, &my_dev_attr[i].attr);
> +             if (err) {
> +                     pr_debug("failed to create the foo file in /sys/devices/Silicom_platform\n");
> +                     break;
> +             }
> +     }
> +
> +     return err;
> +}
> +
> +static int __init silicom_platform_info_init(const struct 
> +dmi_system_id *id) {
> +     struct silicom_platform_info *info = id->driver_data;
> +
> +     dev_info(&silicom_platform_dev->dev, "Detected %s\n", 
> + id->ident);
> +
> +     mec_io_base = info->io_base;
> +     mec_io_len = info->io_len;
> +     silicom_led_info = info->led_info;
> +     silicom_gpio_channels = info->gpio_channels;
> +     silicom_gpiochip = info->gpiochip;
> +     if (silicom_gpiochip)

How can this be NULL?

Henry: in case something wrong with info->gpiochip


> +             silicom_gpiochip->ngpio = info->ngpio;
> +
> +     return 1;
> +}
> +
> +static const struct dmi_system_id silicom_dmi_ids[] __initconst = {
> +     {
> +             .callback = silicom_platform_info_init,
> +             .ident = "Silicom Cordoba (Generic)",
> +             .matches = {
> +                     DMI_MATCH(DMI_BOARD_VENDOR, "Silicom"),
> +                     DMI_MATCH(DMI_BOARD_NAME, "80300-0214-G"),
> +             },
> +             .driver_data = &silicom_generic_cordoba_info,
> +     },
> +     {
> +             .callback = silicom_platform_info_init,
> +             .ident = "Silicom Cordoba (Generic)",
> +             .matches = {
> +                     DMI_MATCH(DMI_BOARD_VENDOR, "Silicom"),
> +                     DMI_MATCH(DMI_BOARD_NAME, "80500-0214-G"),
> +             },
> +             .driver_data = &silicom_generic_cordoba_info,
> +     },
> +     {
> +              .callback = silicom_platform_info_init,
> +              .ident = "Silicom Cordoba (plat_0222)",
> +              .matches = {
> +                    DMI_MATCH(DMI_BOARD_VENDOR, "Silicom"),
> +                    DMI_MATCH(DMI_BOARD_NAME, "80300-0222-G"),
> +              },

Incorrect indentation x6.

Henry: fixed

> +             .driver_data = &silicom_plat_0222_cordoba_info,
> +     },
> +     { },
> +};
> +
> +static int __init silicom_platform_init(void) {
> +     struct device *dev;
> +     int err;
> +
> +     /* register a platform device to act as the parent for LEDS, etc. */
> +     silicom_platform_dev = platform_device_register_simple("silicom-platform", -1, NULL, 0);
> +     if (IS_ERR(silicom_platform_dev)) {
> +             err = PTR_ERR(silicom_platform_dev);
> +             pr_err("failed to register silicom-platform device: %d\n", err);
> +             goto silicom_init_register_err;
> +     }
> +     dev = &silicom_platform_dev->dev;
> +
> +     err = dmi_check_system(silicom_dmi_ids);
> +     if (err == 0) {
> +             dev_err(dev, "No DMI match for this platform\n");
> +             err = -ENODEV;
> +             goto silicom_init_probe_err;
> +     }
> +
> +     /* Directly probe the platform driver in init since this isn't a
> +      * hotpluggable device.  That means we don't need to register a driver
> +      * that needs to wait around in memory on the chance a matching device
> +      * would get added.  Instead run once in __init so that we can free all
> +      * those resources when the __init region is wiped
> +      */
> +     err = platform_driver_probe(&silicom_platform_driver, silicom_platform_probe);
> +     if (err) {
> +             dev_err(dev, "Failed to probe platform driver %d\n", err);
> +             goto silicom_init_probe_err;
> +     }
> +
> +     return 0;
> +
> +silicom_init_probe_err:
> +     if (silicom_platform_dev) {

How can this by NULL?

Henry: if function platform_device_register_simple() failed to regiseter, silicom_platform_dev will be NULL,

> +             platform_device_unregister(silicom_platform_dev);
> +             silicom_platform_dev = NULL;

Seems unnecessary.

Henry: It is need. I saw when registering device failed, the driver crashes.

> +     }
> +     if (my_dev_ctl.my_dev) {
> +             root_device_unregister(my_dev_ctl.my_dev);
> +             my_dev_ctl.my_dev = NULL;

Ditto.

Henry: It is need. I saw when registering device failed, the driver crashes.


> +     }
> +
> +silicom_init_register_err:
> +     return err;
> +}
> +
> +static void __exit silicom_platform_exit(void) {
> +     int i;
> +
> +     if (silicom_platform_dev) {
> +             platform_device_unregister(silicom_platform_dev);
> +             platform_driver_unregister(&silicom_platform_driver);
> +     }
> +
> +     if (my_dev_ctl.my_dev) {
> +             for (i = 0; i < ARRAY_SIZE(my_dev_attr); i++)
> +                     sysfs_remove_file(&my_dev_ctl.my_dev->kobj, &my_dev_attr[i].attr);
> +             root_device_unregister(my_dev_ctl.my_dev);
> +     }
> +     mutex_destroy(&mec_io_mutex);
> +}
> +
> +module_init(silicom_platform_init);
> +module_exit(silicom_platform_exit);
> +
> +MODULE_LICENSE("GPL");
> +MODULE_AUTHOR("Henry Shi <henrys@silicom-usa.com>"); 
> +MODULE_DESCRIPTION("Platform driver for Silicom network appliances");
> +
> +MODULE_DEVICE_TABLE(dmi, silicom_dmi_ids);
> +

Extra newline in the end.

Henry: OK, removed.

--
 i.


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

* RE: [PATCH] Add Silicom Platform Driver
  2023-07-28 19:10   ` Huibin Shi
@ 2023-08-03 12:06     ` Ilpo Järvinen
  2023-08-10 17:48       ` Huibin Shi
  0 siblings, 1 reply; 32+ messages in thread
From: Ilpo Järvinen @ 2023-08-03 12:06 UTC (permalink / raw)
  To: Huibin Shi
  Cc: tglx, mingo, bp, dave.hansen, x86, hpa, hdegoede, markgross,
	jdelvare, linux, linux-kernel, platform-driver-x86, linux-hwmon,
	hb_shi2003, Wen Wang

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

On Fri, 28 Jul 2023, Huibin Shi wrote:

> Ilpo,
> 
> Thanks for the comments. See my comments below.
> 
> Updated patch will be sent out later after review comments from other reviewer are addressed. 
> 
> Henry
> -----Original Message-----
> From: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com> 
> Sent: Wednesday, July 19, 2023 8:13 AM
> To: Henry Shi <henryshi2018@gmail.com>
> Cc: hbshi69@hotmail.com; tglx@linutronix.de; mingo@redhat.com; bp@alien8.de; dave.hansen@linux.intel.com; x86@kernel.org; hpa@zytor.com; hdegoede@redhat.com; markgross@kernel.org; jdelvare@suse.com; linux@roeck-us.net; LKML <linux-kernel@vger.kernel.org>; platform-driver-x86@vger.kernel.org; linux-hwmon@vger.kernel.org; hb_shi2003@yahoo.com; Huibin Shi <henrys@silicom-usa.com>; Wen Wang <wenw@silicom-usa.com>
> Subject: Re: [PATCH] Add Silicom Platform Driver
> 
> Caution: This is an external email. Please take care when clicking links or opening attachments.
> 
> 
> On Tue, 18 Jul 2023, Henry Shi wrote:
> 
> > The Silicom platform (silicom-platform) Linux driver for Swisscom 
> > Business Box (Swisscom BB) as well as Cordoba family products is a 
> > software solution designed to facilitate the efficient management and 
> > control of devices through the integration of various Linux 
> > frameworks. This platform driver provides seamless support for device 
> > management via the Linux LED framework, GPIO framework, Hardware 
> > Monitoring (HWMON), and device attributes. The Silicom platform 
> > driver's compatibility with these Linux frameworks allows applications 
> > to access and control Cordoba family devices using existing software 
> > that is compatible with these frameworks. This compatibility 
> > simplifies the development process, reduces dependencies on 
> > proprietary solutions, and promotes interoperability with other 
> > Linux-based systems and software.
> >
> > Signed-off-by: Henry Shi <henryshi2018@gmail.com>
> > ---
> >  drivers/platform/x86/Kconfig            |   12 +
> >  drivers/platform/x86/Makefile           |    1 +
> >  drivers/platform/x86/silicom-platform.c | 1123 
> > +++++++++++++++++++++++
> >  3 files changed, 1136 insertions(+)
> >  create mode 100644 drivers/platform/x86/silicom-platform.c
> >

> > +void lock_io_modules(void)
> > +{
> > +     mutex_lock(&mec_io_mutex);
> > +}
> > +EXPORT_SYMBOL(lock_io_modules);
> > +
> > +void unlock_io_modules(void)
> > +{
> > +     mutex_unlock(&mec_io_mutex);
> > +}
> > +EXPORT_SYMBOL(unlock_io_modules);

These are unused.

> > +     u32 byte_pos = 0;
> > +
> > +     mutex_lock(&mec_io_mutex);
> > +     /* Select memory region */
> > +     outb(bank, EC_ADDR_MSB);
> > +     outb(offset, EC_ADDR_LSB);
> > +
> > +     /* Set to 1 for current date from the address */
> > +     outb(1, MEC_DATA(byte_pos));
> 
> Hmm, this is really misleading. MEC_DATA() seems to calculate _an offset_, not data? Name things accordingly.
> 
> Henry: OK, changed the defined to  "#define MEC_DATA(offset) ((mec_io_base) + 0x04 + (offset))"

Not much better, unfortunately. Call it MEC_OFFSET() or MEC_DATA_OFFSET() 
instead of MEC_DATA().

> > +static void silicom_gpio_set(struct gpio_chip *gc, unsigned int 
> > +offset, int value) {
> > +     u8 *channels = gpiochip_get_data(gc);
> > +     int direction = silicom_gpio_get_direction(gc, offset);
> > +     int channel = channels[offset];
> > +     u8 reg;
> > +
> > +     if (direction == GPIO_LINE_DIRECTION_IN)
> > +             return;
> > +
> > +     mutex_lock(&mec_io_mutex);
> > +     /* Get the dword offset from the channel */
> > +     outb((channel >> 3) & 0xfc, MEC_ADDR);
> > +
> > +     /* Get the current register */
> > +     reg = inb(MEC_DATA((channel >> 3) & 0x03));
> > +     if (value == 0)
> > +             reg &= ~(1 << (channel & 0x7));
> > +     else if (value > 0)
> > +             reg |= 1 << (channel & 0x7);
> > +     else
> > +             pr_err("Invalid GPIO value: %d\n", value);
> > +     outb(reg, MEC_DATA((channel >> 3) & 0x03));
> > +     mutex_unlock(&mec_io_mutex);
> > +}
> > +
> > +static int silicom_gpio_direction_output(struct gpio_chip *gc, 
> > +unsigned int offset, int value) {
> > +     int direction = silicom_gpio_get_direction(gc, offset);
> > +
> > +     if (direction == GPIO_LINE_DIRECTION_IN)
> > +             return -EINVAL;
> > +
> > +     silicom_gpio_set(gc, offset, value);
> > +
> > +     return 0;
> > +}
> > +
> > +static int silicom_gpio_get(struct gpio_chip *gc, unsigned int 
> > +offset) {
> > +     u8 *channels = gpiochip_get_data(gc);
> > +     int channel = channels[offset];
> > +     u8 reg;
> > +
> > +     mutex_lock(&mec_io_mutex);
> > +     /* Get the dword offset from the channel */
> > +     outb((channel >> 3) & 0xfc, MEC_ADDR);
> > +
> > +     /* Get the current register */
> > +     reg = inb(MEC_DATA((channel >> 3) & 0x03));
> > +     mutex_unlock(&mec_io_mutex);
> > +
> > +     return (reg >> (channel & 0x7)) & 0x01; }

> > +static void silicom_mec_led_set(int channel, int on) {
> > +     u8 reg;
> > +
> > +     mutex_lock(&mec_io_mutex);
> > +     /* Get the dword offset from the channel */
> > +     outb((channel >> 3) & 0xfc, MEC_ADDR);
> > +     /* Get the current LED settings */
> > +     reg = inb(MEC_DATA((channel >> 3) & 0x03));
> > +
> > +     /* Outputs are active low, so clear the bit for on, or set it for off */
> > +     if (on)
> > +             reg &= ~(1 << (channel & 0x7));
> > +     else
> > +             reg |= 1 << (channel & 0x7);
> > +
> > +     /* Write back the updated register */
> > +     outb(reg, MEC_DATA((channel >> 3) & 0x03));
> > +
> > +     mutex_unlock(&mec_io_mutex);
> 
> There's an identical code block in silicom_gpio_set(). Why not simply 
> call this from silicom_gpio_set()? 
> 
> Henry: these two block of date are different: silicom_mec_led_set(xxx, 
> int on), argument on, when on is 1, it means turn on LED; 0 means turn 
> off LED. For  silicom_gpio_set(), the argument value, which is really 
> the 0 or 1 bit value that passed to reg. 

The necessary conversion (and checks) can be made before/when calling the 
function from another. The code is so identical that I don't buy it 
cannot be done (I've diff'ed those functions against each other so I know 
for sure!).

> > +}
> > +
> > +static void silicom_mec_led_mc_brightness_set(struct led_classdev *led_cdev,
> > +                                           enum led_brightness 
> > +brightness) {
> > +     struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(led_cdev);
> > +     int i;
> > +
> > +     led_mc_calc_color_components(mc_cdev, brightness);
> > +
> > +     for (i = 0; i < mc_cdev->num_colors; i++) {
> > +             silicom_mec_led_set(mc_cdev->subled_info[i].channel,
> > +                                 mc_cdev->subled_info[i].brightness);
> > +     }
> > +}
> > +
> > +static enum led_brightness silicom_mec_led_get(int channel) {
> > +     u8 reg;
> > +
> > +     mutex_lock(&mec_io_mutex);
> > +     /* Get the dword offset of the register for this LED from the channel */
> > +     outb((channel >> 3) & 0xfc, MEC_ADDR);
> > +     /* Get the current LED settings */
> > +     reg = inb(MEC_DATA((channel >> 3) & 0x03));
> > +     mutex_unlock(&mec_io_mutex);
> > +
> > +     /* Outputs are active low */
> > +     return reg & (1 << (channel & 0x7)) ? LED_OFF : LED_ON;
> 
> Looks identical to a part of silicom_gpio_get(). Perhaps it should call this function??
> 
> Henry: some of the code block of these two functions are similar. But 
> function return types are different. Gpio has status of 0 or 1, but LED 
> has status of LED_OFF and LED_ON. 

The same comment as above for the set. I think the return types can be 
converted in the calling function before returning.

> > +static int __init silicom_platform_info_init(const struct 
> > +dmi_system_id *id) {
> > +     struct silicom_platform_info *info = id->driver_data;
> > +
> > +     dev_info(&silicom_platform_dev->dev, "Detected %s\n", 
> > + id->ident);
> > +
> > +     mec_io_base = info->io_base;
> > +     mec_io_len = info->io_len;
> > +     silicom_led_info = info->led_info;
> > +     silicom_gpio_channels = info->gpio_channels;
> > +     silicom_gpiochip = info->gpiochip;
> > +     if (silicom_gpiochip)
> 
> How can this be NULL?
> 
> Henry: in case something wrong with info->gpiochip

But how can that occur? Both struct defining silicom_platform_info provide
a non-NULL value. There's no need for such safety construct which cannot 
be every true within the kernel code.

> > +static int __init silicom_platform_init(void) {
> > +     struct device *dev;
> > +     int err;
> > +
> > +     /* register a platform device to act as the parent for LEDS, etc. */
> > +     silicom_platform_dev = platform_device_register_simple("silicom-platform", -1, NULL, 0);
> > +     if (IS_ERR(silicom_platform_dev)) {
> > +             err = PTR_ERR(silicom_platform_dev);
> > +             pr_err("failed to register silicom-platform device: %d\n", err);
> > +             goto silicom_init_register_err;

Instead of goto, return directly as there's nothing to rollback:

return err;

> > +     }
> > +     dev = &silicom_platform_dev->dev;
> > +
> > +     err = dmi_check_system(silicom_dmi_ids);
> > +     if (err == 0) {
> > +             dev_err(dev, "No DMI match for this platform\n");
> > +             err = -ENODEV;
> > +             goto silicom_init_probe_err;
> > +     }
> > +
> > +     /* Directly probe the platform driver in init since this isn't a
> > +      * hotpluggable device.  That means we don't need to register a driver
> > +      * that needs to wait around in memory on the chance a matching device
> > +      * would get added.  Instead run once in __init so that we can free all
> > +      * those resources when the __init region is wiped
> > +      */
> > +     err = platform_driver_probe(&silicom_platform_driver, silicom_platform_probe);
> > +     if (err) {
> > +             dev_err(dev, "Failed to probe platform driver %d\n", err);
> > +             goto silicom_init_probe_err;
> > +     }
> > +
> > +     return 0;
> > +
> > +silicom_init_probe_err:
> > +     if (silicom_platform_dev) {
> 
> How can this by NULL?
> 
> Henry: if function platform_device_register_simple() failed to regiseter, silicom_platform_dev will be NULL,

Ah, sorry. I should have mentioned that that branch should return error 
instead rather than gotoing here.

> > +             platform_device_unregister(silicom_platform_dev);
> > +             silicom_platform_dev = NULL;
> 
> Seems unnecessary.
> 
> Henry: It is need. I saw when registering device failed, the driver crashes.

From which access of silicom_platform_dev??

> > +     }
> > +     if (my_dev_ctl.my_dev) {
> > +             root_device_unregister(my_dev_ctl.my_dev);
> > +             my_dev_ctl.my_dev = NULL;
> 
> Ditto.
> 
> Henry: It is need. I saw when registering device failed, the driver crashes.

Ah, I didn't notice it before but this should be properly arranged into 
reverse order such that goto xx can target the correct amount of rollback 
(in reverse order than init). Add xx labels as necessary for each 
rollback step you need to jump to.

> > +     }
> > +
> > +silicom_init_register_err:
> > +     return err;
> > +}


-- 
 i.

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

* RE: [PATCH] Add Silicom Platform Driver
  2023-07-28 14:28     ` Guenter Roeck
@ 2023-08-08 19:19       ` Huibin Shi
  2023-08-08 19:35         ` Guenter Roeck
  0 siblings, 1 reply; 32+ messages in thread
From: Huibin Shi @ 2023-08-08 19:19 UTC (permalink / raw)
  To: Guenter Roeck, Christophe JAILLET, Henry Shi, hbshi69, tglx,
	mingo, bp, dave.hansen, x86, hpa, hdegoede, markgross, jdelvare,
	linux-kernel, platform-driver-x86, linux-hwmon
  Cc: hb_shi2003, Wen Wang

Guenter,

Here is the implementation of  devm_kmemdup(), *src is the extra argument

void *devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp)
{
	void *p;

	p = devm_kmalloc(dev, len, gfp);
	if (p)
		memcpy(p, src, len);

	return p;
}

Henry
-----Original Message-----
From: Guenter Roeck <groeck7@gmail.com> On Behalf Of Guenter Roeck
Sent: Friday, July 28, 2023 10:29 AM
To: Huibin Shi <henrys@silicom-usa.com>; Christophe JAILLET <christophe.jaillet@wanadoo.fr>; Henry Shi <henryshi2018@gmail.com>; hbshi69@hotmail.com; tglx@linutronix.de; mingo@redhat.com; bp@alien8.de; dave.hansen@linux.intel.com; x86@kernel.org; hpa@zytor.com; hdegoede@redhat.com; markgross@kernel.org; jdelvare@suse.com; linux-kernel@vger.kernel.org; platform-driver-x86@vger.kernel.org; linux-hwmon@vger.kernel.org
Cc: hb_shi2003@yahoo.com; Wen Wang <wenw@silicom-usa.com>
Subject: Re: [PATCH] Add Silicom Platform Driver

Caution: This is an external email. Please take care when clicking links or opening attachments.


On 7/28/23 05:59, Huibin Shi wrote:
> Christophe,
>
> Thanks for the comments. See my comments below.
>
> Updated patch will be sent out later after review comments from other reviewers are addressed.
>
> Henry
> -----Original Message-----
> From: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
> Sent: Tuesday, July 25, 2023 5:03 PM
> To: Henry Shi <henryshi2018@gmail.com>; hbshi69@hotmail.com; 
> tglx@linutronix.de; mingo@redhat.com; bp@alien8.de; 
> dave.hansen@linux.intel.com; x86@kernel.org; hpa@zytor.com; 
> hdegoede@redhat.com; markgross@kernel.org; jdelvare@suse.com; 
> linux@roeck-us.net; linux-kernel@vger.kernel.org; 
> platform-driver-x86@vger.kernel.org; linux-hwmon@vger.kernel.org
> Cc: hb_shi2003@yahoo.com; Huibin Shi <henrys@silicom-usa.com>; Wen 
> Wang <wenw@silicom-usa.com>
> Subject: Re: [PATCH] Add Silicom Platform Driver
>
> Caution: This is an external email. Please take care when clicking links or opening attachments.
>
>
> Le 18/07/2023 à 18:01, Henry Shi a écrit :
>> The Silicom platform (silicom-platform) Linux driver for Swisscom 
>> Business Box (Swisscom BB) as well as Cordoba family products is a 
>> software solution designed to facilitate the efficient management and 
>> control of devices through the integration of various Linux 
>> frameworks. This platform driver provides seamless support for device 
>> management via the Linux LED framework, GPIO framework, Hardware 
>> Monitoring (HWMON), and device attributes. The Silicom platform 
>> driver's compatibility with these Linux frameworks allows 
>> applications to access and control Cordoba family devices using 
>> existing software that is compatible with these frameworks. This 
>> compatibility simplifies the development process, reduces 
>> dependencies on proprietary solutions, and promotes interoperability 
>> with other Linux-based systems and software.
>>
>> Signed-off-by: Henry Shi <henryshi2018@gmail.com>
>> ---
>
> [...]
>
>> +static int __init silicom_mc_leds_register(struct device *dev,
>> +                                        const struct led_classdev_mc
>> +*mc_leds) {
>> +     struct led_classdev_mc *led;
>> +     int i, err;
>> +
>> +     for (i = 0; mc_leds[i].led_cdev.name; i++) {
>> +             /* allocate and copy data from the init constansts */
>> +             led = devm_kzalloc(dev, sizeof(struct led_classdev_mc), 
>> + GFP_KERNEL);
>
> sizeof(*led) is shorter.
> Mostly a matter of taste.
>
> Maybe even devm_kmemdup()?
>
> Henry: thanks. Devm_kmemdup() API requires additional argument that is not necessary of this driver. I prefer devm_kzalloc for now.
>

I am curious: What would that additional argument be ?

Guenter


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

* RE: [PATCH] Add Silicom Platform Driver
  2023-07-28 17:52       ` Guenter Roeck
@ 2023-08-08 19:23         ` Huibin Shi
  0 siblings, 0 replies; 32+ messages in thread
From: Huibin Shi @ 2023-08-08 19:23 UTC (permalink / raw)
  To: Guenter Roeck, Christophe JAILLET, Henry Shi, hbshi69, tglx,
	mingo, bp, dave.hansen, x86, hpa, hdegoede, markgross, jdelvare,
	linux-kernel, platform-driver-x86, linux-hwmon
  Cc: hb_shi2003, Wen Wang

See my comments below:

-----Original Message-----
From: Guenter Roeck <groeck7@gmail.com> On Behalf Of Guenter Roeck
Sent: Friday, July 28, 2023 1:53 PM
To: Christophe JAILLET <christophe.jaillet@wanadoo.fr>; Huibin Shi <henrys@silicom-usa.com>; Henry Shi <henryshi2018@gmail.com>; hbshi69@hotmail.com; tglx@linutronix.de; mingo@redhat.com; bp@alien8.de; dave.hansen@linux.intel.com; x86@kernel.org; hpa@zytor.com; hdegoede@redhat.com; markgross@kernel.org; jdelvare@suse.com; linux-kernel@vger.kernel.org; platform-driver-x86@vger.kernel.org; linux-hwmon@vger.kernel.org
Cc: hb_shi2003@yahoo.com; Wen Wang <wenw@silicom-usa.com>
Subject: Re: [PATCH] Add Silicom Platform Driver

Caution: This is an external email. Please take care when clicking links or opening attachments.


On 7/28/23 09:47, Christophe JAILLET wrote:
> Le 28/07/2023 à 14:59, Huibin Shi a écrit :
>> Christophe,
>>
>> Thanks for the comments. See my comments below.
>>
>> Updated patch will be sent out later after review comments from other reviewers are addressed.
>>
>> Henry
>> -----Original Message-----
>> From: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
>> Sent: Tuesday, July 25, 2023 5:03 PM
>> To: Henry Shi <henryshi2018@gmail.com>; hbshi69@hotmail.com; 
>> tglx@linutronix.de; mingo@redhat.com; bp@alien8.de; 
>> dave.hansen@linux.intel.com; x86@kernel.org; hpa@zytor.com; 
>> hdegoede@redhat.com; markgross@kernel.org; jdelvare@suse.com; 
>> linux@roeck-us.net; linux-kernel@vger.kernel.org; 
>> platform-driver-x86@vger.kernel.org; linux-hwmon@vger.kernel.org
>> Cc: hb_shi2003@yahoo.com; Huibin Shi <henrys@silicom-usa.com>; Wen 
>> Wang <wenw@silicom-usa.com>
>> Subject: Re: [PATCH] Add Silicom Platform Driver
>>
>> Caution: This is an external email. Please take care when clicking links or opening attachments.
>>
>>
>> Le 18/07/2023 à 18:01, Henry Shi a écrit :
>>> The Silicom platform (silicom-platform) Linux driver for Swisscom 
>>> Business Box (Swisscom BB) as well as Cordoba family products is a 
>>> software solution designed to facilitate the efficient management 
>>> and control of devices through the integration of various Linux 
>>> frameworks. This platform driver provides seamless support for 
>>> device management via the Linux LED framework, GPIO framework, 
>>> Hardware Monitoring (HWMON), and device attributes. The Silicom 
>>> platform driver's compatibility with these Linux frameworks allows 
>>> applications to access and control Cordoba family devices using 
>>> existing software that is compatible with these frameworks. This 
>>> compatibility simplifies the development process, reduces 
>>> dependencies on proprietary solutions, and promotes interoperability 
>>> with other Linux-based systems and software.
>>>
>>> Signed-off-by: Henry Shi <henryshi2018@gmail.com>
>>> ---
>>
>> [...]
>>
>>> +static int __init silicom_mc_leds_register(struct device *dev,
>>> +                                        const struct 
>>> +led_classdev_mc
>>> +*mc_leds) {
>>> +     struct led_classdev_mc *led;
>>> +     int i, err;
>>> +
>>> +     for (i = 0; mc_leds[i].led_cdev.name; i++) {
>>> +             /* allocate and copy data from the init constansts */
>>> +             led = devm_kzalloc(dev, sizeof(struct 
>>> + led_classdev_mc), GFP_KERNEL);
>>
>> sizeof(*led) is shorter.
>> Mostly a matter of taste.
>>
>> Maybe even devm_kmemdup()?
>>
>> Henry: thanks. Devm_kmemdup() API requires additional argument that is not necessary of this driver. I prefer devm_kzalloc for now.
>
> CJ: The only additionnal parameter I can think of are the one of memcpy() ...
>
>>
>>> +             if (IS_ERR_OR_NULL(led)) {
>>
>> if (!led)
>> is enough.
>>
>> Henry: OK, changed
>>
>>> +                     dev_err(dev, "Failed to alloc
>>> + led_classdev_mc[%d]: %ld\n", i, PTR_ERR(led));
>>
>> This kind of message is useless and should be removed (checkpatch 
>> should warn about it)
>>
>> Henry: OK, removed.
>>
>>> +                     return -ENOMEM;
>>> +             }
>>> +             memcpy(led, &mc_leds[i], sizeof(*led));
>
> ... here.
>
> devm_kzalloc() + this memcpy() could be done with only devm_kmemdup().
>
> This is mostly a matter of taste.
>

I don't think that using (or not using) available API functions should be a matter of taste, or the next step might be to re-implement memcpy().

Henry: Agree.

Guenter


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

* Re: [PATCH] Add Silicom Platform Driver
  2023-08-08 19:19       ` Huibin Shi
@ 2023-08-08 19:35         ` Guenter Roeck
  2023-08-08 20:39           ` Huibin Shi
  0 siblings, 1 reply; 32+ messages in thread
From: Guenter Roeck @ 2023-08-08 19:35 UTC (permalink / raw)
  To: Huibin Shi, Christophe JAILLET, Henry Shi, hbshi69, tglx, mingo,
	bp, dave.hansen, x86, hpa, hdegoede, markgross, jdelvare,
	linux-kernel, platform-driver-x86, linux-hwmon
  Cc: hb_shi2003, Wen Wang

On 8/8/23 12:19, Huibin Shi wrote:
> Guenter,
> 
> Here is the implementation of  devm_kmemdup(), *src is the extra argument
> 
> void *devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp)
> {
> 	void *p;
> 
> 	p = devm_kmalloc(dev, len, gfp);
> 	if (p)
> 		memcpy(p, src, len);
> 
> 	return p;
> }
> 

So you don't want to use devm_kmemdup() because of its 'src' argument, and instead
re-implement it locally by using devm_kzalloc() followed by memcpy() ? Really ?

Guenter


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

* RE: [PATCH] Add Silicom Platform Driver
  2023-08-08 19:35         ` Guenter Roeck
@ 2023-08-08 20:39           ` Huibin Shi
  0 siblings, 0 replies; 32+ messages in thread
From: Huibin Shi @ 2023-08-08 20:39 UTC (permalink / raw)
  To: Guenter Roeck, Christophe JAILLET, Henry Shi, hbshi69, tglx,
	mingo, bp, dave.hansen, x86, hpa, hdegoede, markgross, jdelvare,
	linux-kernel, platform-driver-x86, linux-hwmon
  Cc: hb_shi2003, Wen Wang

I mean this is the kernel implementation of devm_kmemdup() function in /driver/base/devres.c. 

Henry
-----Original Message-----
From: Guenter Roeck <groeck7@gmail.com> On Behalf Of Guenter Roeck
Sent: Tuesday, August 8, 2023 3:36 PM
To: Huibin Shi <henrys@silicom-usa.com>; Christophe JAILLET <christophe.jaillet@wanadoo.fr>; Henry Shi <henryshi2018@gmail.com>; hbshi69@hotmail.com; tglx@linutronix.de; mingo@redhat.com; bp@alien8.de; dave.hansen@linux.intel.com; x86@kernel.org; hpa@zytor.com; hdegoede@redhat.com; markgross@kernel.org; jdelvare@suse.com; linux-kernel@vger.kernel.org; platform-driver-x86@vger.kernel.org; linux-hwmon@vger.kernel.org
Cc: hb_shi2003@yahoo.com; Wen Wang <wenw@silicom-usa.com>
Subject: Re: [PATCH] Add Silicom Platform Driver

Caution: This is an external email. Please take care when clicking links or opening attachments.


On 8/8/23 12:19, Huibin Shi wrote:
> Guenter,
>
> Here is the implementation of  devm_kmemdup(), *src is the extra 
> argument
>
> void *devm_kmemdup(struct device *dev, const void *src, size_t len, 
> gfp_t gfp) {
>       void *p;
>
>       p = devm_kmalloc(dev, len, gfp);
>       if (p)
>               memcpy(p, src, len);
>
>       return p;
> }
>

So you don't want to use devm_kmemdup() because of its 'src' argument, and instead re-implement it locally by using devm_kzalloc() followed by memcpy() ? Really ?

Guenter


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

* RE: [PATCH] Add Silicom Platform Driver
  2023-08-03 12:06     ` Ilpo Järvinen
@ 2023-08-10 17:48       ` Huibin Shi
  2023-08-10 18:00         ` Guenter Roeck
  2023-08-10 18:01         ` Guenter Roeck
  0 siblings, 2 replies; 32+ messages in thread
From: Huibin Shi @ 2023-08-10 17:48 UTC (permalink / raw)
  To: Ilpo Järvinen
  Cc: tglx, mingo, bp, dave.hansen, x86, hpa, hdegoede, markgross,
	jdelvare, linux, linux-kernel, platform-driver-x86, linux-hwmon,
	hb_shi2003, Wen Wang

Ilpo, 

See my comments below.

Thanks.
Henry
----Original Message-----
From: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com> 
Sent: Thursday, August 3, 2023 8:07 AM
To: Huibin Shi <henrys@silicom-usa.com>
Cc: tglx@linutronix.de; mingo@redhat.com; bp@alien8.de; dave.hansen@linux.intel.com; x86@kernel.org; hpa@zytor.com; hdegoede@redhat.com; markgross@kernel.org; jdelvare@suse.com; linux@roeck-us.net; linux-kernel@vger.kernel.org; platform-driver-x86@vger.kernel.org; linux-hwmon@vger.kernel.org; hb_shi2003@yahoo.com; Wen Wang <wenw@silicom-usa.com>
Subject: RE: [PATCH] Add Silicom Platform Driver

Caution: This is an external email. Please take care when clicking links or opening attachments.


On Fri, 28 Jul 2023, Huibin Shi wrote:

> Ilpo,
>
> Thanks for the comments. See my comments below.
>
> Updated patch will be sent out later after review comments from other reviewer are addressed.
>
> Henry
> -----Original Message-----
> From: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
> Sent: Wednesday, July 19, 2023 8:13 AM
> To: Henry Shi <henryshi2018@gmail.com>
> Cc: hbshi69@hotmail.com; tglx@linutronix.de; mingo@redhat.com; 
> bp@alien8.de; dave.hansen@linux.intel.com; x86@kernel.org; 
> hpa@zytor.com; hdegoede@redhat.com; markgross@kernel.org; 
> jdelvare@suse.com; linux@roeck-us.net; LKML 
> <linux-kernel@vger.kernel.org>; platform-driver-x86@vger.kernel.org; 
> linux-hwmon@vger.kernel.org; hb_shi2003@yahoo.com; Huibin Shi 
> <henrys@silicom-usa.com>; Wen Wang <wenw@silicom-usa.com>
> Subject: Re: [PATCH] Add Silicom Platform Driver
>
> Caution: This is an external email. Please take care when clicking links or opening attachments.
>
>
> On Tue, 18 Jul 2023, Henry Shi wrote:
>
> > The Silicom platform (silicom-platform) Linux driver for Swisscom 
> > Business Box (Swisscom BB) as well as Cordoba family products is a 
> > software solution designed to facilitate the efficient management 
> > and control of devices through the integration of various Linux 
> > frameworks. This platform driver provides seamless support for 
> > device management via the Linux LED framework, GPIO framework, 
> > Hardware Monitoring (HWMON), and device attributes. The Silicom 
> > platform driver's compatibility with these Linux frameworks allows 
> > applications to access and control Cordoba family devices using 
> > existing software that is compatible with these frameworks. This 
> > compatibility simplifies the development process, reduces 
> > dependencies on proprietary solutions, and promotes interoperability 
> > with other Linux-based systems and software.
> >
> > Signed-off-by: Henry Shi <henryshi2018@gmail.com>
> > ---
> >  drivers/platform/x86/Kconfig            |   12 +
> >  drivers/platform/x86/Makefile           |    1 +
> >  drivers/platform/x86/silicom-platform.c | 1123
> > +++++++++++++++++++++++
> >  3 files changed, 1136 insertions(+)  create mode 100644 
> > drivers/platform/x86/silicom-platform.c
> >

> > +void lock_io_modules(void)
> > +{
> > +     mutex_lock(&mec_io_mutex);
> > +}
> > +EXPORT_SYMBOL(lock_io_modules);
> > +
> > +void unlock_io_modules(void)
> > +{
> > +     mutex_unlock(&mec_io_mutex);
> > +}
> > +EXPORT_SYMBOL(unlock_io_modules);

These are unused.

Henry: This will be used by another Silicom driver, which source code is not pushed to kernel repository. 


> > +     u32 byte_pos = 0;
> > +
> > +     mutex_lock(&mec_io_mutex);
> > +     /* Select memory region */
> > +     outb(bank, EC_ADDR_MSB);
> > +     outb(offset, EC_ADDR_LSB);
> > +
> > +     /* Set to 1 for current date from the address */
> > +     outb(1, MEC_DATA(byte_pos));
>
> Hmm, this is really misleading. MEC_DATA() seems to calculate _an offset_, not data? Name things accordingly.
>
> Henry: OK, changed the defined to  "#define MEC_DATA(offset) ((mec_io_base) + 0x04 + (offset))"

Not much better, unfortunately. Call it MEC_OFFSET() or MEC_DATA_OFFSET() instead of MEC_DATA().

Henry: Agree.

> > +static void silicom_gpio_set(struct gpio_chip *gc, unsigned int 
> > +offset, int value) {
> > +     u8 *channels = gpiochip_get_data(gc);
> > +     int direction = silicom_gpio_get_direction(gc, offset);
> > +     int channel = channels[offset];
> > +     u8 reg;
> > +
> > +     if (direction == GPIO_LINE_DIRECTION_IN)
> > +             return;
> > +
> > +     mutex_lock(&mec_io_mutex);
> > +     /* Get the dword offset from the channel */
> > +     outb((channel >> 3) & 0xfc, MEC_ADDR);
> > +
> > +     /* Get the current register */
> > +     reg = inb(MEC_DATA((channel >> 3) & 0x03));
> > +     if (value == 0)
> > +             reg &= ~(1 << (channel & 0x7));
> > +     else if (value > 0)
> > +             reg |= 1 << (channel & 0x7);
> > +     else
> > +             pr_err("Invalid GPIO value: %d\n", value);
> > +     outb(reg, MEC_DATA((channel >> 3) & 0x03));
> > +     mutex_unlock(&mec_io_mutex);
> > +}
> > +
> > +static int silicom_gpio_direction_output(struct gpio_chip *gc, 
> > +unsigned int offset, int value) {
> > +     int direction = silicom_gpio_get_direction(gc, offset);
> > +
> > +     if (direction == GPIO_LINE_DIRECTION_IN)
> > +             return -EINVAL;
> > +
> > +     silicom_gpio_set(gc, offset, value);
> > +
> > +     return 0;
> > +}
> > +
> > +static int silicom_gpio_get(struct gpio_chip *gc, unsigned int
> > +offset) {
> > +     u8 *channels = gpiochip_get_data(gc);
> > +     int channel = channels[offset];
> > +     u8 reg;
> > +
> > +     mutex_lock(&mec_io_mutex);
> > +     /* Get the dword offset from the channel */
> > +     outb((channel >> 3) & 0xfc, MEC_ADDR);
> > +
> > +     /* Get the current register */
> > +     reg = inb(MEC_DATA((channel >> 3) & 0x03));
> > +     mutex_unlock(&mec_io_mutex);
> > +
> > +     return (reg >> (channel & 0x7)) & 0x01; }

> > +static void silicom_mec_led_set(int channel, int on) {
> > +     u8 reg;
> > +
> > +     mutex_lock(&mec_io_mutex);
> > +     /* Get the dword offset from the channel */
> > +     outb((channel >> 3) & 0xfc, MEC_ADDR);
> > +     /* Get the current LED settings */
> > +     reg = inb(MEC_DATA((channel >> 3) & 0x03));
> > +
> > +     /* Outputs are active low, so clear the bit for on, or set it for off */
> > +     if (on)
> > +             reg &= ~(1 << (channel & 0x7));
> > +     else
> > +             reg |= 1 << (channel & 0x7);
> > +
> > +     /* Write back the updated register */
> > +     outb(reg, MEC_DATA((channel >> 3) & 0x03));
> > +
> > +     mutex_unlock(&mec_io_mutex);
>
> There's an identical code block in silicom_gpio_set(). Why not simply 
> call this from silicom_gpio_set()?
>
> Henry: these two block of date are different: silicom_mec_led_set(xxx, 
> int on), argument on, when on is 1, it means turn on LED; 0 means turn 
> off LED. For  silicom_gpio_set(), the argument value, which is really 
> the 0 or 1 bit value that passed to reg.

The necessary conversion (and checks) can be made before/when calling the function from another. The code is so identical that I don't buy it cannot be done (I've diff'ed those functions against each other so I know for sure!).

Henry: OK, I will change it in next submission.

> > +}
> > +
> > +static void silicom_mec_led_mc_brightness_set(struct led_classdev *led_cdev,
> > +                                           enum led_brightness
> > +brightness) {
> > +     struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(led_cdev);
> > +     int i;
> > +
> > +     led_mc_calc_color_components(mc_cdev, brightness);
> > +
> > +     for (i = 0; i < mc_cdev->num_colors; i++) {
> > +             silicom_mec_led_set(mc_cdev->subled_info[i].channel,
> > +                                 mc_cdev->subled_info[i].brightness);
> > +     }
> > +}
> > +
> > +static enum led_brightness silicom_mec_led_get(int channel) {
> > +     u8 reg;
> > +
> > +     mutex_lock(&mec_io_mutex);
> > +     /* Get the dword offset of the register for this LED from the channel */
> > +     outb((channel >> 3) & 0xfc, MEC_ADDR);
> > +     /* Get the current LED settings */
> > +     reg = inb(MEC_DATA((channel >> 3) & 0x03));
> > +     mutex_unlock(&mec_io_mutex);
> > +
> > +     /* Outputs are active low */
> > +     return reg & (1 << (channel & 0x7)) ? LED_OFF : LED_ON;
>
> Looks identical to a part of silicom_gpio_get(). Perhaps it should call this function??
>
> Henry: some of the code block of these two functions are similar. But 
> function return types are different. Gpio has status of 0 or 1, but 
> LED has status of LED_OFF and LED_ON.

The same comment as above for the set. I think the return types can be converted in the calling function before returning.

Henry: OK.

> > +static int __init silicom_platform_info_init(const struct 
> > +dmi_system_id *id) {
> > +     struct silicom_platform_info *info = id->driver_data;
> > +
> > +     dev_info(&silicom_platform_dev->dev, "Detected %s\n",
> > + id->ident);
> > +
> > +     mec_io_base = info->io_base;
> > +     mec_io_len = info->io_len;
> > +     silicom_led_info = info->led_info;
> > +     silicom_gpio_channels = info->gpio_channels;
> > +     silicom_gpiochip = info->gpiochip;
> > +     if (silicom_gpiochip)
>
> How can this be NULL?
>
> Henry: in case something wrong with info->gpiochip

But how can that occur? Both struct defining silicom_platform_info provide a non-NULL value. There's no need for such safety construct which cannot be every true within the kernel code.

Henry: OK, will remove checking. 

> > +static int __init silicom_platform_init(void) {
> > +     struct device *dev;
> > +     int err;
> > +
> > +     /* register a platform device to act as the parent for LEDS, etc. */
> > +     silicom_platform_dev = platform_device_register_simple("silicom-platform", -1, NULL, 0);
> > +     if (IS_ERR(silicom_platform_dev)) {
> > +             err = PTR_ERR(silicom_platform_dev);
> > +             pr_err("failed to register silicom-platform device: %d\n", err);
> > +             goto silicom_init_register_err;

Instead of goto, return directly as there's nothing to rollback:

return err;

Henry: OK.

> > +     }
> > +     dev = &silicom_platform_dev->dev;
> > +
> > +     err = dmi_check_system(silicom_dmi_ids);
> > +     if (err == 0) {
> > +             dev_err(dev, "No DMI match for this platform\n");
> > +             err = -ENODEV;
> > +             goto silicom_init_probe_err;
> > +     }
> > +
> > +     /* Directly probe the platform driver in init since this isn't a
> > +      * hotpluggable device.  That means we don't need to register a driver
> > +      * that needs to wait around in memory on the chance a matching device
> > +      * would get added.  Instead run once in __init so that we can free all
> > +      * those resources when the __init region is wiped
> > +      */
> > +     err = platform_driver_probe(&silicom_platform_driver, silicom_platform_probe);
> > +     if (err) {
> > +             dev_err(dev, "Failed to probe platform driver %d\n", err);
> > +             goto silicom_init_probe_err;
> > +     }
> > +
> > +     return 0;
> > +
> > +silicom_init_probe_err:
> > +     if (silicom_platform_dev) {
>
> How can this by NULL?
>
> Henry: if function platform_device_register_simple() failed to 
> regiseter, silicom_platform_dev will be NULL,

Ah, sorry. I should have mentioned that that branch should return error instead rather than gotoing here.

Henry: OK. 

> > +             platform_device_unregister(silicom_platform_dev);
> > +             silicom_platform_dev = NULL;
>
> Seems unnecessary.
>
> Henry: It is need. I saw when registering device failed, the driver crashes.

From which access of silicom_platform_dev??

> > +     }
> > +     if (my_dev_ctl.my_dev) {
> > +             root_device_unregister(my_dev_ctl.my_dev);
> > +             my_dev_ctl.my_dev = NULL;
>
> Ditto.
>
> Henry: It is need. I saw when registering device failed, the driver crashes.

Ah, I didn't notice it before but this should be properly arranged into reverse order such that goto xx can target the correct amount of rollback (in reverse order than init). Add xx labels as necessary for each rollback step you need to jump to.

Henry: OK.

> > +     }
> > +
> > +silicom_init_register_err:
> > +     return err;
> > +}


--
 i.

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

* Re: [PATCH] Add Silicom Platform Driver
  2023-08-10 17:48       ` Huibin Shi
@ 2023-08-10 18:00         ` Guenter Roeck
  2023-08-14 13:07           ` Huibin Shi
  2023-08-10 18:01         ` Guenter Roeck
  1 sibling, 1 reply; 32+ messages in thread
From: Guenter Roeck @ 2023-08-10 18:00 UTC (permalink / raw)
  To: Huibin Shi, Ilpo Järvinen
  Cc: tglx, mingo, bp, dave.hansen, x86, hpa, hdegoede, markgross,
	jdelvare, linux-kernel, platform-driver-x86, linux-hwmon,
	hb_shi2003, Wen Wang

On 8/10/23 10:48, Huibin Shi wrote:
> Ilpo,
> 
> See my comments below.
> 
> Thanks.
> Henry
> ----Original Message-----
> From: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
> Sent: Thursday, August 3, 2023 8:07 AM
> To: Huibin Shi <henrys@silicom-usa.com>
> Cc: tglx@linutronix.de; mingo@redhat.com; bp@alien8.de; dave.hansen@linux.intel.com; x86@kernel.org; hpa@zytor.com; hdegoede@redhat.com; markgross@kernel.org; jdelvare@suse.com; linux@roeck-us.net; linux-kernel@vger.kernel.org; platform-driver-x86@vger.kernel.org; linux-hwmon@vger.kernel.org; hb_shi2003@yahoo.com; Wen Wang <wenw@silicom-usa.com>
> Subject: RE: [PATCH] Add Silicom Platform Driver
> 
> Caution: This is an external email. Please take care when clicking links or opening attachments.
> 
> 
> On Fri, 28 Jul 2023, Huibin Shi wrote:
> 
>> Ilpo,
>>
>> Thanks for the comments. See my comments below.
>>
>> Updated patch will be sent out later after review comments from other reviewer are addressed.
>>
>> Henry
>> -----Original Message-----
>> From: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
>> Sent: Wednesday, July 19, 2023 8:13 AM
>> To: Henry Shi <henryshi2018@gmail.com>
>> Cc: hbshi69@hotmail.com; tglx@linutronix.de; mingo@redhat.com;
>> bp@alien8.de; dave.hansen@linux.intel.com; x86@kernel.org;
>> hpa@zytor.com; hdegoede@redhat.com; markgross@kernel.org;
>> jdelvare@suse.com; linux@roeck-us.net; LKML
>> <linux-kernel@vger.kernel.org>; platform-driver-x86@vger.kernel.org;
>> linux-hwmon@vger.kernel.org; hb_shi2003@yahoo.com; Huibin Shi
>> <henrys@silicom-usa.com>; Wen Wang <wenw@silicom-usa.com>
>> Subject: Re: [PATCH] Add Silicom Platform Driver
>>
>> Caution: This is an external email. Please take care when clicking links or opening attachments.
>>
>>
>> On Tue, 18 Jul 2023, Henry Shi wrote:
>>
>>> The Silicom platform (silicom-platform) Linux driver for Swisscom
>>> Business Box (Swisscom BB) as well as Cordoba family products is a
>>> software solution designed to facilitate the efficient management
>>> and control of devices through the integration of various Linux
>>> frameworks. This platform driver provides seamless support for
>>> device management via the Linux LED framework, GPIO framework,
>>> Hardware Monitoring (HWMON), and device attributes. The Silicom
>>> platform driver's compatibility with these Linux frameworks allows
>>> applications to access and control Cordoba family devices using
>>> existing software that is compatible with these frameworks. This
>>> compatibility simplifies the development process, reduces
>>> dependencies on proprietary solutions, and promotes interoperability
>>> with other Linux-based systems and software.
>>>
>>> Signed-off-by: Henry Shi <henryshi2018@gmail.com>
>>> ---
>>>   drivers/platform/x86/Kconfig            |   12 +
>>>   drivers/platform/x86/Makefile           |    1 +
>>>   drivers/platform/x86/silicom-platform.c | 1123
>>> +++++++++++++++++++++++
>>>   3 files changed, 1136 insertions(+)  create mode 100644
>>> drivers/platform/x86/silicom-platform.c
>>>
> 
>>> +void lock_io_modules(void)
>>> +{
>>> +     mutex_lock(&mec_io_mutex);
>>> +}
>>> +EXPORT_SYMBOL(lock_io_modules);
>>> +
>>> +void unlock_io_modules(void)
>>> +{
>>> +     mutex_unlock(&mec_io_mutex);
>>> +}
>>> +EXPORT_SYMBOL(unlock_io_modules);
> 
> These are unused.
> 
> Henry: This will be used by another Silicom driver, which source code is not pushed to kernel repository.
> 

Not an argument. Export it if and when needed. If that other driver won't be sent
upstream, the export should not be upstream either.

Guenter


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

* Re: [PATCH] Add Silicom Platform Driver
  2023-08-10 17:48       ` Huibin Shi
  2023-08-10 18:00         ` Guenter Roeck
@ 2023-08-10 18:01         ` Guenter Roeck
  2023-08-14 13:09           ` Huibin Shi
  1 sibling, 1 reply; 32+ messages in thread
From: Guenter Roeck @ 2023-08-10 18:01 UTC (permalink / raw)
  To: Huibin Shi, Ilpo Järvinen
  Cc: tglx, mingo, bp, dave.hansen, x86, hpa, hdegoede, markgross,
	jdelvare, linux-kernel, platform-driver-x86, linux-hwmon,
	hb_shi2003, Wen Wang

On 8/10/23 10:48, Huibin Shi wrote:
> Ilpo,
> 
> See my comments below.
> 
> Thanks.
> Henry
> ----Original Message-----
> From: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
> Sent: Thursday, August 3, 2023 8:07 AM
> To: Huibin Shi <henrys@silicom-usa.com>
> Cc: tglx@linutronix.de; mingo@redhat.com; bp@alien8.de; dave.hansen@linux.intel.com; x86@kernel.org; hpa@zytor.com; hdegoede@redhat.com; markgross@kernel.org; jdelvare@suse.com; linux@roeck-us.net; linux-kernel@vger.kernel.org; platform-driver-x86@vger.kernel.org; linux-hwmon@vger.kernel.org; hb_shi2003@yahoo.com; Wen Wang <wenw@silicom-usa.com>
> Subject: RE: [PATCH] Add Silicom Platform Driver
> 
> Caution: This is an external email. Please take care when clicking links or opening attachments.
> 
> 
> On Fri, 28 Jul 2023, Huibin Shi wrote:
> 
>> Ilpo,
>>
>> Thanks for the comments. See my comments below.
>>
>> Updated patch will be sent out later after review comments from other reviewer are addressed.
>>
>> Henry
>> -----Original Message-----
>> From: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
>> Sent: Wednesday, July 19, 2023 8:13 AM
>> To: Henry Shi <henryshi2018@gmail.com>
>> Cc: hbshi69@hotmail.com; tglx@linutronix.de; mingo@redhat.com;
>> bp@alien8.de; dave.hansen@linux.intel.com; x86@kernel.org;
>> hpa@zytor.com; hdegoede@redhat.com; markgross@kernel.org;
>> jdelvare@suse.com; linux@roeck-us.net; LKML
>> <linux-kernel@vger.kernel.org>; platform-driver-x86@vger.kernel.org;
>> linux-hwmon@vger.kernel.org; hb_shi2003@yahoo.com; Huibin Shi
>> <henrys@silicom-usa.com>; Wen Wang <wenw@silicom-usa.com>
>> Subject: Re: [PATCH] Add Silicom Platform Driver
>>
>> Caution: This is an external email. Please take care when clicking links or opening attachments.
>>
>>
>> On Tue, 18 Jul 2023, Henry Shi wrote:
>>
>>> The Silicom platform (silicom-platform) Linux driver for Swisscom
>>> Business Box (Swisscom BB) as well as Cordoba family products is a
>>> software solution designed to facilitate the efficient management
>>> and control of devices through the integration of various Linux
>>> frameworks. This platform driver provides seamless support for
>>> device management via the Linux LED framework, GPIO framework,
>>> Hardware Monitoring (HWMON), and device attributes. The Silicom
>>> platform driver's compatibility with these Linux frameworks allows
>>> applications to access and control Cordoba family devices using
>>> existing software that is compatible with these frameworks. This
>>> compatibility simplifies the development process, reduces
>>> dependencies on proprietary solutions, and promotes interoperability
>>> with other Linux-based systems and software.
>>>
>>> Signed-off-by: Henry Shi <henryshi2018@gmail.com>
>>> ---
>>>   drivers/platform/x86/Kconfig            |   12 +
>>>   drivers/platform/x86/Makefile           |    1 +
>>>   drivers/platform/x86/silicom-platform.c | 1123
>>> +++++++++++++++++++++++
>>>   3 files changed, 1136 insertions(+)  create mode 100644
>>> drivers/platform/x86/silicom-platform.c
>>>
> 
>>> +void lock_io_modules(void)
>>> +{
>>> +     mutex_lock(&mec_io_mutex);
>>> +}
>>> +EXPORT_SYMBOL(lock_io_modules);
>>> +
>>> +void unlock_io_modules(void)
>>> +{
>>> +     mutex_unlock(&mec_io_mutex);
>>> +}
>>> +EXPORT_SYMBOL(unlock_io_modules);
> 
> These are unused.
> 
> Henry: This will be used by another Silicom driver, which source code is not pushed to kernel repository.
> 

Besides, those function names are _way_ too generic for public symbols,
and even more so for exports.

Guenter


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

* RE: [PATCH] Add Silicom Platform Driver
  2023-08-10 18:00         ` Guenter Roeck
@ 2023-08-14 13:07           ` Huibin Shi
  0 siblings, 0 replies; 32+ messages in thread
From: Huibin Shi @ 2023-08-14 13:07 UTC (permalink / raw)
  To: Guenter Roeck, Ilpo Järvinen
  Cc: tglx, mingo, bp, dave.hansen, x86, hpa, hdegoede, markgross,
	jdelvare, linux-kernel, platform-driver-x86, linux-hwmon,
	hb_shi2003, Wen Wang



-----Original Message-----
From: Guenter Roeck <groeck7@gmail.com> On Behalf Of Guenter Roeck
Sent: Thursday, August 10, 2023 2:00 PM
To: Huibin Shi <henrys@silicom-usa.com>; Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Cc: tglx@linutronix.de; mingo@redhat.com; bp@alien8.de; dave.hansen@linux.intel.com; x86@kernel.org; hpa@zytor.com; hdegoede@redhat.com; markgross@kernel.org; jdelvare@suse.com; linux-kernel@vger.kernel.org; platform-driver-x86@vger.kernel.org; linux-hwmon@vger.kernel.org; hb_shi2003@yahoo.com; Wen Wang <wenw@silicom-usa.com>
Subject: Re: [PATCH] Add Silicom Platform Driver

Caution: This is an external email. Please take care when clicking links or opening attachments.


On 8/10/23 10:48, Huibin Shi wrote:
> Ilpo,
>
> See my comments below.
>
> Thanks.
> Henry
> ----Original Message-----
> From: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
> Sent: Thursday, August 3, 2023 8:07 AM
> To: Huibin Shi <henrys@silicom-usa.com>
> Cc: tglx@linutronix.de; mingo@redhat.com; bp@alien8.de; 
> dave.hansen@linux.intel.com; x86@kernel.org; hpa@zytor.com; 
> hdegoede@redhat.com; markgross@kernel.org; jdelvare@suse.com; 
> linux@roeck-us.net; linux-kernel@vger.kernel.org; 
> platform-driver-x86@vger.kernel.org; linux-hwmon@vger.kernel.org; 
> hb_shi2003@yahoo.com; Wen Wang <wenw@silicom-usa.com>
> Subject: RE: [PATCH] Add Silicom Platform Driver
>
> Caution: This is an external email. Please take care when clicking links or opening attachments.
>
>
> On Fri, 28 Jul 2023, Huibin Shi wrote:
>
>> Ilpo,
>>
>> Thanks for the comments. See my comments below.
>>
>> Updated patch will be sent out later after review comments from other reviewer are addressed.
>>
>> Henry
>> -----Original Message-----
>> From: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
>> Sent: Wednesday, July 19, 2023 8:13 AM
>> To: Henry Shi <henryshi2018@gmail.com>
>> Cc: hbshi69@hotmail.com; tglx@linutronix.de; mingo@redhat.com; 
>> bp@alien8.de; dave.hansen@linux.intel.com; x86@kernel.org; 
>> hpa@zytor.com; hdegoede@redhat.com; markgross@kernel.org; 
>> jdelvare@suse.com; linux@roeck-us.net; LKML 
>> <linux-kernel@vger.kernel.org>; platform-driver-x86@vger.kernel.org;
>> linux-hwmon@vger.kernel.org; hb_shi2003@yahoo.com; Huibin Shi 
>> <henrys@silicom-usa.com>; Wen Wang <wenw@silicom-usa.com>
>> Subject: Re: [PATCH] Add Silicom Platform Driver
>>
>> Caution: This is an external email. Please take care when clicking links or opening attachments.
>>
>>
>> On Tue, 18 Jul 2023, Henry Shi wrote:
>>
>>> The Silicom platform (silicom-platform) Linux driver for Swisscom 
>>> Business Box (Swisscom BB) as well as Cordoba family products is a 
>>> software solution designed to facilitate the efficient management 
>>> and control of devices through the integration of various Linux 
>>> frameworks. This platform driver provides seamless support for 
>>> device management via the Linux LED framework, GPIO framework, 
>>> Hardware Monitoring (HWMON), and device attributes. The Silicom 
>>> platform driver's compatibility with these Linux frameworks allows 
>>> applications to access and control Cordoba family devices using 
>>> existing software that is compatible with these frameworks. This 
>>> compatibility simplifies the development process, reduces 
>>> dependencies on proprietary solutions, and promotes interoperability 
>>> with other Linux-based systems and software.
>>>
>>> Signed-off-by: Henry Shi <henryshi2018@gmail.com>
>>> ---
>>>   drivers/platform/x86/Kconfig            |   12 +
>>>   drivers/platform/x86/Makefile           |    1 +
>>>   drivers/platform/x86/silicom-platform.c | 1123
>>> +++++++++++++++++++++++
>>>   3 files changed, 1136 insertions(+)  create mode 100644 
>>> drivers/platform/x86/silicom-platform.c
>>>
>
>>> +void lock_io_modules(void)
>>> +{
>>> +     mutex_lock(&mec_io_mutex);
>>> +}
>>> +EXPORT_SYMBOL(lock_io_modules);
>>> +
>>> +void unlock_io_modules(void)
>>> +{
>>> +     mutex_unlock(&mec_io_mutex);
>>> +}
>>> +EXPORT_SYMBOL(unlock_io_modules);
>
> These are unused.
>
> Henry: This will be used by another Silicom driver, which source code is not pushed to kernel repository.
>

Not an argument. Export it if and when needed. If that other driver won't be sent upstream, the export should not be upstream either.

Henry: OK, will remove these two EXPORT function

Guenter


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

* RE: [PATCH] Add Silicom Platform Driver
  2023-08-10 18:01         ` Guenter Roeck
@ 2023-08-14 13:09           ` Huibin Shi
  0 siblings, 0 replies; 32+ messages in thread
From: Huibin Shi @ 2023-08-14 13:09 UTC (permalink / raw)
  To: Guenter Roeck, Ilpo Järvinen
  Cc: tglx, mingo, bp, dave.hansen, x86, hpa, hdegoede, markgross,
	jdelvare, linux-kernel, platform-driver-x86, linux-hwmon,
	hb_shi2003, Wen Wang



-----Original Message-----
From: Guenter Roeck <groeck7@gmail.com> On Behalf Of Guenter Roeck
Sent: Thursday, August 10, 2023 2:01 PM
To: Huibin Shi <henrys@silicom-usa.com>; Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Cc: tglx@linutronix.de; mingo@redhat.com; bp@alien8.de; dave.hansen@linux.intel.com; x86@kernel.org; hpa@zytor.com; hdegoede@redhat.com; markgross@kernel.org; jdelvare@suse.com; linux-kernel@vger.kernel.org; platform-driver-x86@vger.kernel.org; linux-hwmon@vger.kernel.org; hb_shi2003@yahoo.com; Wen Wang <wenw@silicom-usa.com>
Subject: Re: [PATCH] Add Silicom Platform Driver

Caution: This is an external email. Please take care when clicking links or opening attachments.


On 8/10/23 10:48, Huibin Shi wrote:
> Ilpo,
>
> See my comments below.
>
> Thanks.
> Henry
> ----Original Message-----
> From: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
> Sent: Thursday, August 3, 2023 8:07 AM
> To: Huibin Shi <henrys@silicom-usa.com>
> Cc: tglx@linutronix.de; mingo@redhat.com; bp@alien8.de; 
> dave.hansen@linux.intel.com; x86@kernel.org; hpa@zytor.com; 
> hdegoede@redhat.com; markgross@kernel.org; jdelvare@suse.com; 
> linux@roeck-us.net; linux-kernel@vger.kernel.org; 
> platform-driver-x86@vger.kernel.org; linux-hwmon@vger.kernel.org; 
> hb_shi2003@yahoo.com; Wen Wang <wenw@silicom-usa.com>
> Subject: RE: [PATCH] Add Silicom Platform Driver
>
> Caution: This is an external email. Please take care when clicking links or opening attachments.
>
>
> On Fri, 28 Jul 2023, Huibin Shi wrote:
>
>> Ilpo,
>>
>> Thanks for the comments. See my comments below.
>>
>> Updated patch will be sent out later after review comments from other reviewer are addressed.
>>
>> Henry
>> -----Original Message-----
>> From: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
>> Sent: Wednesday, July 19, 2023 8:13 AM
>> To: Henry Shi <henryshi2018@gmail.com>
>> Cc: hbshi69@hotmail.com; tglx@linutronix.de; mingo@redhat.com; 
>> bp@alien8.de; dave.hansen@linux.intel.com; x86@kernel.org; 
>> hpa@zytor.com; hdegoede@redhat.com; markgross@kernel.org; 
>> jdelvare@suse.com; linux@roeck-us.net; LKML 
>> <linux-kernel@vger.kernel.org>; platform-driver-x86@vger.kernel.org;
>> linux-hwmon@vger.kernel.org; hb_shi2003@yahoo.com; Huibin Shi 
>> <henrys@silicom-usa.com>; Wen Wang <wenw@silicom-usa.com>
>> Subject: Re: [PATCH] Add Silicom Platform Driver
>>
>> Caution: This is an external email. Please take care when clicking links or opening attachments.
>>
>>
>> On Tue, 18 Jul 2023, Henry Shi wrote:
>>
>>> The Silicom platform (silicom-platform) Linux driver for Swisscom 
>>> Business Box (Swisscom BB) as well as Cordoba family products is a 
>>> software solution designed to facilitate the efficient management 
>>> and control of devices through the integration of various Linux 
>>> frameworks. This platform driver provides seamless support for 
>>> device management via the Linux LED framework, GPIO framework, 
>>> Hardware Monitoring (HWMON), and device attributes. The Silicom 
>>> platform driver's compatibility with these Linux frameworks allows 
>>> applications to access and control Cordoba family devices using 
>>> existing software that is compatible with these frameworks. This 
>>> compatibility simplifies the development process, reduces 
>>> dependencies on proprietary solutions, and promotes interoperability 
>>> with other Linux-based systems and software.
>>>
>>> Signed-off-by: Henry Shi <henryshi2018@gmail.com>
>>> ---
>>>   drivers/platform/x86/Kconfig            |   12 +
>>>   drivers/platform/x86/Makefile           |    1 +
>>>   drivers/platform/x86/silicom-platform.c | 1123
>>> +++++++++++++++++++++++
>>>   3 files changed, 1136 insertions(+)  create mode 100644 
>>> drivers/platform/x86/silicom-platform.c
>>>
>
>>> +void lock_io_modules(void)
>>> +{
>>> +     mutex_lock(&mec_io_mutex);
>>> +}
>>> +EXPORT_SYMBOL(lock_io_modules);
>>> +
>>> +void unlock_io_modules(void)
>>> +{
>>> +     mutex_unlock(&mec_io_mutex);
>>> +}
>>> +EXPORT_SYMBOL(unlock_io_modules);
>
> These are unused.
>
> Henry: This will be used by another Silicom driver, which source code is not pushed to kernel repository.
>

Besides, those function names are _way_ too generic for public symbols, and even more so for exports.

Henry: Good point, will change the function name to some special names when EXPORT them. 

Guenter


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

* RE: [PATCH] Add Silicom Platform Driver
  2023-08-21 16:09         ` Guenter Roeck
@ 2023-08-21 16:23           ` Huibin Shi
  0 siblings, 0 replies; 32+ messages in thread
From: Huibin Shi @ 2023-08-21 16:23 UTC (permalink / raw)
  To: Guenter Roeck
  Cc: Henry Shi, hbshi69, tglx, mingo, bp, dave.hansen, x86, hpa,
	hdegoede, markgross, jdelvare, linux-kernel, platform-driver-x86,
	linux-hwmon, hb_shi2003, Wen Wang

Hi Guenter,

OK, I see your point. I will make the change to let driver return milli-degrees return (reg >> 16) * 100.

The 32 bits register exposed by micro-controller has its upper 16 bits represent temperature.

Thanks
Henry

-----Original Message-----
From: Guenter Roeck <groeck7@gmail.com> On Behalf Of Guenter Roeck
Sent: Monday, August 21, 2023 12:09 PM
To: Huibin Shi <henrys@silicom-usa.com>
Cc: Henry Shi <henryshi2018@gmail.com>; hbshi69@hotmail.com; tglx@linutronix.de; mingo@redhat.com; bp@alien8.de; dave.hansen@linux.intel.com; x86@kernel.org; hpa@zytor.com; hdegoede@redhat.com; markgross@kernel.org; jdelvare@suse.com; linux-kernel@vger.kernel.org; platform-driver-x86@vger.kernel.org; linux-hwmon@vger.kernel.org; hb_shi2003@yahoo.com; Wen Wang <wenw@silicom-usa.com>
Subject: Re: [PATCH] Add Silicom Platform Driver

Caution: This is an external email. Please take care when clicking links or opening attachments.


On Mon, Aug 21, 2023 at 03:48:33PM +0000, Huibin Shi wrote:
> Guenter,
>
> See my comments below.
>
> Thanks
> Henry
>
> -----Original Message-----
> From: Guenter Roeck <groeck7@gmail.com> On Behalf Of Guenter Roeck
> Sent: Saturday, August 19, 2023 10:36 AM
> To: Huibin Shi <henrys@silicom-usa.com>
> Cc: Henry Shi <henryshi2018@gmail.com>; hbshi69@hotmail.com; 
> tglx@linutronix.de; mingo@redhat.com; bp@alien8.de; 
> dave.hansen@linux.intel.com; x86@kernel.org; hpa@zytor.com; 
> hdegoede@redhat.com; markgross@kernel.org; jdelvare@suse.com; 
> linux-kernel@vger.kernel.org; platform-driver-x86@vger.kernel.org; 
> linux-hwmon@vger.kernel.org; hb_shi2003@yahoo.com; Wen Wang 
> <wenw@silicom-usa.com>
> Subject: Re: [PATCH] Add Silicom Platform Driver
>
> Caution: This is an external email. Please take care when clicking links or opening attachments.
>
>
> On Sat, Aug 19, 2023 at 02:20:32PM +0000, Huibin Shi wrote:
> > Hi Guenter,
> >
> > Thanks for your comments. Probably, I should not resubmit patch too rushed. I will add version number to subject and change log in cover letter for next resubmission.
> >
> > See my comments below. Please let me know whether you accept my explanation.
> >
> > Henry
> > -----Original Message-----
> [ ... ]
>
> > > +
> > > +static u32 temp_get(void)
> > > +{
> > > +     u32 reg;
> > > +
> > > +     mutex_lock(&mec_io_mutex);
> > > +     /* Select memory region */
> > > +     outb(IO_REG_BANK, EC_ADDR_MSB);
> > > +     outb(0xc, EC_ADDR_LSB);
> > > +     /* Get current data from the address */
> > > +     reg = inl(MEC_DATA(DEFAULT_CHAN_LO));
> > > +     mutex_unlock(&mec_io_mutex);
> > > +
> > > +     return (reg >> 16) / 10;
> >
> > The hwmon ABI expects temperatures to be reported in milli-degrees C.
> > The above sets the maximum temperature to 65,535 / 10 = 6,553 milli-degrees or 6.553 degrees C. It is very unlikely that this is correct.
> >
> > Again, I commented on this before.
> >
> > Henry: this is due to an internal implementation of MIcor-controller firmware, instead of putting real temperature to the register, it put (real temperature * 10 ) to the register. So, in order to report correct temperature to user space application, the read value is divided by 10, then report to user space.
> >
> > Please let me know if you accept this. If not, I can change the code, but let user space application to do adjustment.
>
> No, I do not accept this. I do not believe that the maximum temperature reported by the microcontroller is 6.553 degrees C. I suspect it reports 10th of degrees C. In that case, the number reported should be multiplied by 100 to make it milli-degrees C as expected by the ABI.
>
> Henry: OK, I will remove "/10" in driver, and let user space application (or script) to the calculation.
>

Sorry, I completely fail to understand why you refuse to follow the ABI.
The temperature must be reported in milli-degrees C, not in some arbitrary unit, period.

FWIW, that really deserves a NACK now.



Guenter

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

* Re: [PATCH] Add Silicom Platform Driver
  2023-08-21 15:48       ` Huibin Shi
@ 2023-08-21 16:09         ` Guenter Roeck
  2023-08-21 16:23           ` Huibin Shi
  0 siblings, 1 reply; 32+ messages in thread
From: Guenter Roeck @ 2023-08-21 16:09 UTC (permalink / raw)
  To: Huibin Shi
  Cc: Henry Shi, hbshi69, tglx, mingo, bp, dave.hansen, x86, hpa,
	hdegoede, markgross, jdelvare, linux-kernel, platform-driver-x86,
	linux-hwmon, hb_shi2003, Wen Wang

On Mon, Aug 21, 2023 at 03:48:33PM +0000, Huibin Shi wrote:
> Guenter,
> 
> See my comments below.
> 
> Thanks
> Henry
> 
> -----Original Message-----
> From: Guenter Roeck <groeck7@gmail.com> On Behalf Of Guenter Roeck
> Sent: Saturday, August 19, 2023 10:36 AM
> To: Huibin Shi <henrys@silicom-usa.com>
> Cc: Henry Shi <henryshi2018@gmail.com>; hbshi69@hotmail.com; tglx@linutronix.de; mingo@redhat.com; bp@alien8.de; dave.hansen@linux.intel.com; x86@kernel.org; hpa@zytor.com; hdegoede@redhat.com; markgross@kernel.org; jdelvare@suse.com; linux-kernel@vger.kernel.org; platform-driver-x86@vger.kernel.org; linux-hwmon@vger.kernel.org; hb_shi2003@yahoo.com; Wen Wang <wenw@silicom-usa.com>
> Subject: Re: [PATCH] Add Silicom Platform Driver
> 
> Caution: This is an external email. Please take care when clicking links or opening attachments.
> 
> 
> On Sat, Aug 19, 2023 at 02:20:32PM +0000, Huibin Shi wrote:
> > Hi Guenter,
> >
> > Thanks for your comments. Probably, I should not resubmit patch too rushed. I will add version number to subject and change log in cover letter for next resubmission.
> >
> > See my comments below. Please let me know whether you accept my explanation.
> >
> > Henry
> > -----Original Message-----
> [ ... ]
> 
> > > +
> > > +static u32 temp_get(void)
> > > +{
> > > +     u32 reg;
> > > +
> > > +     mutex_lock(&mec_io_mutex);
> > > +     /* Select memory region */
> > > +     outb(IO_REG_BANK, EC_ADDR_MSB);
> > > +     outb(0xc, EC_ADDR_LSB);
> > > +     /* Get current data from the address */
> > > +     reg = inl(MEC_DATA(DEFAULT_CHAN_LO));
> > > +     mutex_unlock(&mec_io_mutex);
> > > +
> > > +     return (reg >> 16) / 10;
> >
> > The hwmon ABI expects temperatures to be reported in milli-degrees C.
> > The above sets the maximum temperature to 65,535 / 10 = 6,553 milli-degrees or 6.553 degrees C. It is very unlikely that this is correct.
> >
> > Again, I commented on this before.
> >
> > Henry: this is due to an internal implementation of MIcor-controller firmware, instead of putting real temperature to the register, it put (real temperature * 10 ) to the register. So, in order to report correct temperature to user space application, the read value is divided by 10, then report to user space.
> >
> > Please let me know if you accept this. If not, I can change the code, but let user space application to do adjustment.
> 
> No, I do not accept this. I do not believe that the maximum temperature reported by the microcontroller is 6.553 degrees C. I suspect it reports 10th of degrees C. In that case, the number reported should be multiplied by 100 to make it milli-degrees C as expected by the ABI.
> 
> Henry: OK, I will remove "/10" in driver, and let user space application (or script) to the calculation.
> 

Sorry, I completely fail to understand why you refuse to follow the ABI.
The temperature must be reported in milli-degrees C, not in some arbitrary
unit, period.

FWIW, that really deserves a NACK now.

Guenter

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

* RE: [PATCH] Add Silicom Platform Driver
  2023-08-21 10:39       ` Ilpo Järvinen
@ 2023-08-21 16:00         ` Huibin Shi
  0 siblings, 0 replies; 32+ messages in thread
From: Huibin Shi @ 2023-08-21 16:00 UTC (permalink / raw)
  To: Ilpo Järvinen
  Cc: Henry Shi, hbshi69, tglx, mingo, bp, dave.hansen, x86, hpa,
	hdegoede, markgross, jdelvare, linux-kernel, platform-driver-x86,
	linux-hwmon, hb_shi2003, Wen Wang, Guenter Roeck



-----Original Message-----
From: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com> 
Sent: Monday, August 21, 2023 6:40 AM
To: Huibin Shi <henrys@silicom-usa.com>
Cc: Henry Shi <henryshi2018@gmail.com>; hbshi69@hotmail.com; tglx@linutronix.de; mingo@redhat.com; bp@alien8.de; dave.hansen@linux.intel.com; x86@kernel.org; hpa@zytor.com; hdegoede@redhat.com; markgross@kernel.org; jdelvare@suse.com; linux-kernel@vger.kernel.org; platform-driver-x86@vger.kernel.org; linux-hwmon@vger.kernel.org; hb_shi2003@yahoo.com; Wen Wang <wenw@silicom-usa.com>; Guenter Roeck <linux@roeck-us.net>
Subject: Re: [PATCH] Add Silicom Platform Driver

Caution: This is an external email. Please take care when clicking links or opening attachments.


On Sat, 19 Aug 2023, Guenter Roeck wrote:

> On Sat, Aug 19, 2023 at 02:20:32PM +0000, Huibin Shi wrote:
> > Hi Guenter,
> >
> > Thanks for your comments. Probably, I should not resubmit patch too rushed. I will add version number to subject and change log in cover letter for next resubmission.
> >
> > See my comments below. Please let me know whether you accept my explanation.
> >
> > Henry
> > -----Original Message-----
> [ ... ]
>
> > > +
> > > +static u32 temp_get(void)
> > > +{
> > > +     u32 reg;
> > > +
> > > +     mutex_lock(&mec_io_mutex);
> > > +     /* Select memory region */
> > > +     outb(IO_REG_BANK, EC_ADDR_MSB);
> > > +     outb(0xc, EC_ADDR_LSB);
> > > +     /* Get current data from the address */
> > > +     reg = inl(MEC_DATA(DEFAULT_CHAN_LO));
> > > +     mutex_unlock(&mec_io_mutex);
> > > +
> > > +     return (reg >> 16) / 10;
> >
> > The hwmon ABI expects temperatures to be reported in milli-degrees C.
> > The above sets the maximum temperature to 65,535 / 10 = 6,553 milli-degrees or 6.553 degrees C. It is very unlikely that this is correct.
> >
> > Again, I commented on this before.
> >
> > Henry: this is due to an internal implementation of MIcor-controller firmware, instead of putting real temperature to the register, it put (real temperature * 10 ) to the register. So, in order to report correct temperature to user space application, the read value is divided by 10, then report to user space.
> >
> > Please let me know if you accept this. If not, I can change the code, but let user space application to do adjustment.
>
> No, I do not accept this. I do not believe that the maximum 
> temperature reported by the microcontroller is 6.553 degrees C. I 
> suspect it reports 10th of degrees C. In that case, the number 
> reported should be multiplied by 100 to make it milli-degrees C as expected by the ABI.

...And for that multiplication, please use the constant defined in include/linux/units.h instead of a literal.

Henry: will add constant define.


And please do version the patches properly, it's getting messy to track which version is which (if something is new or not). If you use git format-patch, it has an argument that can be used to add the version, no need to add it by hand.

Henry: OK I will do patch version and change log for next submission. I did not realize this before. 

--
 i.


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

* RE: [PATCH] Add Silicom Platform Driver
  2023-08-19 14:36     ` Guenter Roeck
  2023-08-21 10:39       ` Ilpo Järvinen
@ 2023-08-21 15:48       ` Huibin Shi
  2023-08-21 16:09         ` Guenter Roeck
  1 sibling, 1 reply; 32+ messages in thread
From: Huibin Shi @ 2023-08-21 15:48 UTC (permalink / raw)
  To: Guenter Roeck
  Cc: Henry Shi, hbshi69, tglx, mingo, bp, dave.hansen, x86, hpa,
	hdegoede, markgross, jdelvare, linux-kernel, platform-driver-x86,
	linux-hwmon, hb_shi2003, Wen Wang

Guenter,

See my comments below.

Thanks
Henry

-----Original Message-----
From: Guenter Roeck <groeck7@gmail.com> On Behalf Of Guenter Roeck
Sent: Saturday, August 19, 2023 10:36 AM
To: Huibin Shi <henrys@silicom-usa.com>
Cc: Henry Shi <henryshi2018@gmail.com>; hbshi69@hotmail.com; tglx@linutronix.de; mingo@redhat.com; bp@alien8.de; dave.hansen@linux.intel.com; x86@kernel.org; hpa@zytor.com; hdegoede@redhat.com; markgross@kernel.org; jdelvare@suse.com; linux-kernel@vger.kernel.org; platform-driver-x86@vger.kernel.org; linux-hwmon@vger.kernel.org; hb_shi2003@yahoo.com; Wen Wang <wenw@silicom-usa.com>
Subject: Re: [PATCH] Add Silicom Platform Driver

Caution: This is an external email. Please take care when clicking links or opening attachments.


On Sat, Aug 19, 2023 at 02:20:32PM +0000, Huibin Shi wrote:
> Hi Guenter,
>
> Thanks for your comments. Probably, I should not resubmit patch too rushed. I will add version number to subject and change log in cover letter for next resubmission.
>
> See my comments below. Please let me know whether you accept my explanation.
>
> Henry
> -----Original Message-----
[ ... ]

> > +
> > +static u32 temp_get(void)
> > +{
> > +     u32 reg;
> > +
> > +     mutex_lock(&mec_io_mutex);
> > +     /* Select memory region */
> > +     outb(IO_REG_BANK, EC_ADDR_MSB);
> > +     outb(0xc, EC_ADDR_LSB);
> > +     /* Get current data from the address */
> > +     reg = inl(MEC_DATA(DEFAULT_CHAN_LO));
> > +     mutex_unlock(&mec_io_mutex);
> > +
> > +     return (reg >> 16) / 10;
>
> The hwmon ABI expects temperatures to be reported in milli-degrees C.
> The above sets the maximum temperature to 65,535 / 10 = 6,553 milli-degrees or 6.553 degrees C. It is very unlikely that this is correct.
>
> Again, I commented on this before.
>
> Henry: this is due to an internal implementation of MIcor-controller firmware, instead of putting real temperature to the register, it put (real temperature * 10 ) to the register. So, in order to report correct temperature to user space application, the read value is divided by 10, then report to user space.
>
> Please let me know if you accept this. If not, I can change the code, but let user space application to do adjustment.

No, I do not accept this. I do not believe that the maximum temperature reported by the microcontroller is 6.553 degrees C. I suspect it reports 10th of degrees C. In that case, the number reported should be multiplied by 100 to make it milli-degrees C as expected by the ABI.

Henry: OK, I will remove "/10" in driver, and let user space application (or script) to the calculation.

[ ... ]

> > +static int silicom_fan_control_read_labels(struct device *dev, enum hwmon_sensor_types type,
> > +                                        u32 attr, int channel, 
> > +const char **str) {
> > +     switch (type) {
> > +     case hwmon_fan:
> > +             *str = "Fan Speed";
> > +             return 0;
> > +     case hwmon_temp:
> > +             *str = "Thermostat Sensor";
> > +             return 0;
>
> Those labels have no practical value.
>
> Henry: Those labels will be used by user space code to identify Silicom_platform driver.
>

The driver is identified by the driver name, not by the name of a temperature sensor or fan speed attribute. Any other driver could return "Fan Speed" or "Thermostat Sensor" here. Userspace relying on such values to identify the driver are simply broken.

Henry: Good point. 

When this driver installed, there is one directory created under /sys/class/hwmon/, called hwmon#. The exact # for the newly created directory can be different at run time. So the intention here is let user space software to identify the right directory for Silicom-platform driver. I can change the driver return to "Silicom-platform: Fan Speed" and "Silicom-platform: Thermostat Sensor" to identify silicom-platform driver. Any objection?

> > +     default:
> > +             return -EOPNOTSUPP;
> > +     }
> > +}
> > +
> > +static const struct hwmon_ops silicom_fan_control_hwmon_ops = {
> > +     .is_visible = silicom_fan_control_is_visible,
> > +     .read = silicom_fan_control_read,
> > +     .write = NULL,
>
> Unnecessary.
>
> Henry: OK, will be removed silicom_fan_control_is_visible.

The NULL pointer assignment is unnecessary. I have no idea what that has to do with silicom_fan_control_is_visible(), or why you would want to remove that function.

Henry: OK, will remove the NULL pointer assignment.

Guenter

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

* Re: [PATCH] Add Silicom Platform Driver
  2023-08-19 14:36     ` Guenter Roeck
@ 2023-08-21 10:39       ` Ilpo Järvinen
  2023-08-21 16:00         ` Huibin Shi
  2023-08-21 15:48       ` Huibin Shi
  1 sibling, 1 reply; 32+ messages in thread
From: Ilpo Järvinen @ 2023-08-21 10:39 UTC (permalink / raw)
  To: Huibin Shi
  Cc: Henry Shi, hbshi69, tglx, mingo, bp, dave.hansen, x86, hpa,
	hdegoede, markgross, jdelvare, linux-kernel, platform-driver-x86,
	linux-hwmon, hb_shi2003, Wen Wang, Guenter Roeck

On Sat, 19 Aug 2023, Guenter Roeck wrote:

> On Sat, Aug 19, 2023 at 02:20:32PM +0000, Huibin Shi wrote:
> > Hi Guenter,
> > 
> > Thanks for your comments. Probably, I should not resubmit patch too rushed. I will add version number to subject and change log in cover letter for next resubmission.
> > 
> > See my comments below. Please let me know whether you accept my explanation.
> > 
> > Henry
> > -----Original Message-----
> [ ... ]
> 
> > > +
> > > +static u32 temp_get(void)
> > > +{
> > > +     u32 reg;
> > > +
> > > +     mutex_lock(&mec_io_mutex);
> > > +     /* Select memory region */
> > > +     outb(IO_REG_BANK, EC_ADDR_MSB);
> > > +     outb(0xc, EC_ADDR_LSB);
> > > +     /* Get current data from the address */
> > > +     reg = inl(MEC_DATA(DEFAULT_CHAN_LO));
> > > +     mutex_unlock(&mec_io_mutex);
> > > +
> > > +     return (reg >> 16) / 10;
> > 
> > The hwmon ABI expects temperatures to be reported in milli-degrees C.
> > The above sets the maximum temperature to 65,535 / 10 = 6,553 milli-degrees or 6.553 degrees C. It is very unlikely that this is correct.
> > 
> > Again, I commented on this before.
> > 
> > Henry: this is due to an internal implementation of MIcor-controller firmware, instead of putting real temperature to the register, it put (real temperature * 10 ) to the register. So, in order to report correct temperature to user space application, the read value is divided by 10, then report to user space. 
> > 
> > Please let me know if you accept this. If not, I can change the code, but let user space application to do adjustment. 
> 
> No, I do not accept this. I do not believe that the maximum temperature
> reported by the microcontroller is 6.553 degrees C. I suspect it reports
> 10th of degrees C. In that case, the number reported should be multiplied
> by 100 to make it milli-degrees C as expected by the ABI.

...And for that multiplication, please use the constant defined in 
include/linux/units.h instead of a literal.


And please do version the patches properly, it's getting messy to track 
which version is which (if something is new or not). If you use git 
format-patch, it has an argument that can be used to add the version, no 
need to add it by hand.


-- 
 i.


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

* Re: [PATCH] Add Silicom Platform Driver
  2023-08-19 14:20   ` Huibin Shi
@ 2023-08-19 14:36     ` Guenter Roeck
  2023-08-21 10:39       ` Ilpo Järvinen
  2023-08-21 15:48       ` Huibin Shi
  0 siblings, 2 replies; 32+ messages in thread
From: Guenter Roeck @ 2023-08-19 14:36 UTC (permalink / raw)
  To: Huibin Shi
  Cc: Henry Shi, hbshi69, tglx, mingo, bp, dave.hansen, x86, hpa,
	hdegoede, markgross, jdelvare, linux-kernel, platform-driver-x86,
	linux-hwmon, hb_shi2003, Wen Wang

On Sat, Aug 19, 2023 at 02:20:32PM +0000, Huibin Shi wrote:
> Hi Guenter,
> 
> Thanks for your comments. Probably, I should not resubmit patch too rushed. I will add version number to subject and change log in cover letter for next resubmission.
> 
> See my comments below. Please let me know whether you accept my explanation.
> 
> Henry
> -----Original Message-----
[ ... ]

> > +
> > +static u32 temp_get(void)
> > +{
> > +     u32 reg;
> > +
> > +     mutex_lock(&mec_io_mutex);
> > +     /* Select memory region */
> > +     outb(IO_REG_BANK, EC_ADDR_MSB);
> > +     outb(0xc, EC_ADDR_LSB);
> > +     /* Get current data from the address */
> > +     reg = inl(MEC_DATA(DEFAULT_CHAN_LO));
> > +     mutex_unlock(&mec_io_mutex);
> > +
> > +     return (reg >> 16) / 10;
> 
> The hwmon ABI expects temperatures to be reported in milli-degrees C.
> The above sets the maximum temperature to 65,535 / 10 = 6,553 milli-degrees or 6.553 degrees C. It is very unlikely that this is correct.
> 
> Again, I commented on this before.
> 
> Henry: this is due to an internal implementation of MIcor-controller firmware, instead of putting real temperature to the register, it put (real temperature * 10 ) to the register. So, in order to report correct temperature to user space application, the read value is divided by 10, then report to user space. 
> 
> Please let me know if you accept this. If not, I can change the code, but let user space application to do adjustment. 

No, I do not accept this. I do not believe that the maximum temperature
reported by the microcontroller is 6.553 degrees C. I suspect it reports
10th of degrees C. In that case, the number reported should be multiplied
by 100 to make it milli-degrees C as expected by the ABI.

[ ... ]

> > +static int silicom_fan_control_read_labels(struct device *dev, enum hwmon_sensor_types type,
> > +                                        u32 attr, int channel, const 
> > +char **str) {
> > +     switch (type) {
> > +     case hwmon_fan:
> > +             *str = "Fan Speed";
> > +             return 0;
> > +     case hwmon_temp:
> > +             *str = "Thermostat Sensor";
> > +             return 0;
> 
> Those labels have no practical value.
> 
> Henry: Those labels will be used by user space code to identify Silicom_platform driver. 
> 

The driver is identified by the driver name, not by the name of
a temperature sensor or fan speed attribute. Any other driver
could return "Fan Speed" or "Thermostat Sensor" here. Userspace
relying on such values to identify the driver are simply broken.

> > +     default:
> > +             return -EOPNOTSUPP;
> > +     }
> > +}
> > +
> > +static const struct hwmon_ops silicom_fan_control_hwmon_ops = {
> > +     .is_visible = silicom_fan_control_is_visible,
> > +     .read = silicom_fan_control_read,
> > +     .write = NULL,
> 
> Unnecessary.
> 
> Henry: OK, will be removed silicom_fan_control_is_visible.

The NULL pointer assignment is unnecessary. I have no idea
what that has to do with silicom_fan_control_is_visible(),
or why you would want to remove that function.

Guenter

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

* RE: [PATCH] Add Silicom Platform Driver
  2023-08-19 12:41 ` Guenter Roeck
@ 2023-08-19 14:20   ` Huibin Shi
  2023-08-19 14:36     ` Guenter Roeck
  0 siblings, 1 reply; 32+ messages in thread
From: Huibin Shi @ 2023-08-19 14:20 UTC (permalink / raw)
  To: Guenter Roeck, Henry Shi
  Cc: hbshi69, tglx, mingo, bp, dave.hansen, x86, hpa, hdegoede,
	markgross, jdelvare, linux-kernel, platform-driver-x86,
	linux-hwmon, hb_shi2003, Wen Wang

Hi Guenter,

Thanks for your comments. Probably, I should not resubmit patch too rushed. I will add version number to subject and change log in cover letter for next resubmission.

See my comments below. Please let me know whether you accept my explanation.

Henry
-----Original Message-----
From: Guenter Roeck <groeck7@gmail.com> On Behalf Of Guenter Roeck
Sent: Saturday, August 19, 2023 8:41 AM
To: Henry Shi <henryshi2018@gmail.com>
Cc: hbshi69@hotmail.com; tglx@linutronix.de; mingo@redhat.com; bp@alien8.de; dave.hansen@linux.intel.com; x86@kernel.org; hpa@zytor.com; hdegoede@redhat.com; markgross@kernel.org; jdelvare@suse.com; linux-kernel@vger.kernel.org; platform-driver-x86@vger.kernel.org; linux-hwmon@vger.kernel.org; hb_shi2003@yahoo.com; Huibin Shi <henrys@silicom-usa.com>; Wen Wang <wenw@silicom-usa.com>
Subject: Re: [PATCH] Add Silicom Platform Driver

Caution: This is an external email. Please take care when clicking links or opening attachments.


On Fri, Aug 18, 2023 at 11:43:41AM -0400, Henry Shi wrote:

Description missing.

Henry: I forgot it for last submission, I realized it after I send out email.

This is at least v4 of this patch. No versioning, no change log.

Henry: Will add version and change log for next submission.

That was commented on before. This is the last time I am going to provide feedback, even more so since most of my previous feedback was ignored.

Note that my comments only refer to problems in hwmon code and do by no means suggest that the rest of the code may be acceptable.

> Signed-off-by: Henry Shi <henryshi2018@gmail.com>
> ---
>  drivers/platform/x86/Kconfig            |   11 +
>  drivers/platform/x86/Makefile           |    1 +
>  drivers/platform/x86/silicom-platform.c | 1046 
> +++++++++++++++++++++++
>  3 files changed, 1058 insertions(+)
>  create mode 100644 drivers/platform/x86/silicom-platform.c
>
> diff --git a/drivers/platform/x86/Kconfig 
> b/drivers/platform/x86/Kconfig index 22052031c719..7680c0dbcd8d 100644
> --- a/drivers/platform/x86/Kconfig
> +++ b/drivers/platform/x86/Kconfig
> @@ -188,6 +188,17 @@ config ACER_WMI
>         If you have an ACPI-WMI compatible Acer/ Wistron laptop, say Y or M
>         here.
>
> +config SILICOM_PLATFORM
> +     tristate "Silicom Edge Networking device support"
> +     depends on DMI
> +     select LEDS_CLASS_MULTICOLOR
> +     select GPIOLIB
> +     help
> +       This option enables support for the LEDs/GPIO/etc downstream of the
> +       embedded controller on Silicom "Cordoba" hardware and derivatives.
> +
> +       If you have a Silicom network appliance, say Y or M here.
> +
>  source "drivers/platform/x86/amd/Kconfig"
>
>  config ADV_SWBUTTON
> diff --git a/drivers/platform/x86/Makefile 
> b/drivers/platform/x86/Makefile index 2cafe51ec4d8..9355ebbc56ca 
> 100644
> --- a/drivers/platform/x86/Makefile
> +++ b/drivers/platform/x86/Makefile
> @@ -113,6 +113,7 @@ obj-$(CONFIG_SERIAL_MULTI_INSTANTIATE)    += serial-multi-instantiate.o
>  obj-$(CONFIG_MLX_PLATFORM)           += mlx-platform.o
>  obj-$(CONFIG_TOUCHSCREEN_DMI)                += touchscreen_dmi.o
>  obj-$(CONFIG_WIRELESS_HOTKEY)                += wireless-hotkey.o
> +obj-$(CONFIG_SILICOM_PLATFORM)               += silicom-platform.o
>  obj-$(CONFIG_X86_ANDROID_TABLETS)    += x86-android-tablets/
>
>  # Intel uncore drivers
> diff --git a/drivers/platform/x86/silicom-platform.c 
> b/drivers/platform/x86/silicom-platform.c
> new file mode 100644
> index 000000000000..ddf9961e2b9e
> --- /dev/null
> +++ b/drivers/platform/x86/silicom-platform.c
> @@ -0,0 +1,1046 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +//
> +// silicom-platform.c - Silicom MEC170x platform driver // // 
> +Copyright (C) 2023 Henry Shi <henrys@silicom-usa.com>
> +
> +#include <linux/dmi.h>
> +#include <linux/gpio/driver.h>
> +#include <linux/init.h>
> +#include <linux/ioport.h>
> +#include <linux/io.h>
> +#include <linux/kernel.h>
> +#include <linux/led-class-multicolor.h> #include <linux/module.h> 
> +#include <linux/hwmon.h> #include <linux/mutex.h> #include 
> +<linux/platform_device.h> #include <linux/string.h> #include 
> +<linux/kobject.h> #include <linux/sysfs.h> #include <linux/bits.h> 
> +#include <linux/bitfield.h>
> +
> +#define MEC_ADDR ((mec_io_base) + 0x02) #define MEC_DATA(offset) 
> +((mec_io_base) + 0x04 + (offset)) #define EC_ADDR_LSB MEC_ADDR 
> +#define EC_ADDR_MSB ((mec_io_base) + 0x03) #define SILICOM_MEC_MAGIC 
> +0x5a #define OFFSET_BIT_TO_CHANNEL(off, bit) ((((off) + 0x014) << 3) 
> +| (bit)) #define CHANNEL_TO_OFFSET(chan) (((chan) >> 3) - 0x14) 
> +#define MEC_VERSION_MAJOR GENMASK(15, 14) #define MEC_VERSION_MINOR 
> +GENMASK(13, 8) #define IO_REG_BANK 0 #define DEFAULT_CHAN_LO 0 
> +#define DEFAULT_CHAN_HI 0 #define MEC_EFUSE_LSB_ADDR 0x28 #define 
> +MEC_POWER_CYCLE_ADDR 0x24
> +
> +static DEFINE_MUTEX(mec_io_mutex);
> +static int mec_io_base, mec_io_len;
> +static struct device *my_dev;
> +static int efuse_status;
> +static int mec_uc_version;
> +static int power_cycle;
> +
> +struct silicom_fan_control_data {
> +     struct   device *hdev;

Unnecessary.

> +     int      temp;

Unused

Henry: OK, will remove it for next submission.

> +     int      fan_speed;

Unnecessary.

Henry: OK, will remove it for next submission.

> +};

This entire data structure is unnecessary. I did comment on that before.

> +
> +static const struct hwmon_channel_info *silicom_fan_control_info[] = {
> +     HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT | HWMON_F_LABEL),
> +     HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT | HWMON_T_LABEL),
> +     NULL
> +};
> +
> +struct silicom_platform_info {
> +     int io_base;
> +     int io_len;
> +     struct led_classdev_mc *led_info;
> +     struct gpio_chip *gpiochip;
> +     u8 *gpio_channels;
> +     u16 ngpio;
> +};
> +
> +static const char * const plat_0222_gpio_names[] = {
> +     "AUTOM0_SFP_TX_FAULT",
> +     "SLOT2_LED_OUT",
> +     "SIM_M2_SLOT2_B_DET",
> +     "SIM_M2_SLOT2_A_DET",
> +     "SLOT1_LED_OUT",
> +     "SIM_M2_SLOT1_B_DET",
> +     "SIM_M2_SLOT1_A_DET",
> +     "SLOT0_LED_OUT",
> +     "WAN_SFP0_RX_LOS",
> +     "WAN_SFP0_PRSNT_N",
> +     "WAN_SFP0_TX_FAULT",
> +     "AUTOM1_SFP_RX_LOS",
> +     "AUTOM1_SFP_PRSNT_N",
> +     "AUTOM1_SFP_TX_FAULT",
> +     "AUTOM0_SFP_RX_LOS",
> +     "AUTOM0_SFP_PRSNT_N",
> +     "WAN_SFP1_RX_LOS",
> +     "WAN_SFP1_PRSNT_N",
> +     "WAN_SFP1_TX_FAULT",
> +     "SIM_M2_SLOT1_MUX_SEL",
> +     "W_DISABLE_M2_SLOT1_N",
> +     "W_DISABLE_MPCIE_SLOT0_N",
> +     "W_DISABLE_M2_SLOT0_N",
> +     "BT_COMMAND_MODE",
> +     "WAN_SFP1_TX_DISABLE",
> +     "WAN_SFP0_TX_DISABLE",
> +     "AUTOM1_SFP_TX_DISABLE",
> +     "AUTOM0_SFP_TX_DISABLE",
> +     "SIM_M2_SLOT2_MUX_SEL",
> +     "W_DISABLE_M2_SLOT2_N",
> +     "RST_CTL_M2_SLOT_1_N",
> +     "RST_CTL_M2_SLOT_2_N",
> +     "PM_USB_PWR_EN_BOT",
> +     "PM_USB_PWR_EN_TOP",
> +};
> +
> +static u8 plat_0222_gpio_channels[] = {
> +     OFFSET_BIT_TO_CHANNEL(0x00, 0),
> +     OFFSET_BIT_TO_CHANNEL(0x00, 1),
> +     OFFSET_BIT_TO_CHANNEL(0x00, 2),
> +     OFFSET_BIT_TO_CHANNEL(0x00, 3),
> +     OFFSET_BIT_TO_CHANNEL(0x00, 4),
> +     OFFSET_BIT_TO_CHANNEL(0x00, 5),
> +     OFFSET_BIT_TO_CHANNEL(0x00, 6),
> +     OFFSET_BIT_TO_CHANNEL(0x00, 7),
> +     OFFSET_BIT_TO_CHANNEL(0x01, 0),
> +     OFFSET_BIT_TO_CHANNEL(0x01, 1),
> +     OFFSET_BIT_TO_CHANNEL(0x01, 2),
> +     OFFSET_BIT_TO_CHANNEL(0x01, 3),
> +     OFFSET_BIT_TO_CHANNEL(0x01, 4),
> +     OFFSET_BIT_TO_CHANNEL(0x01, 5),
> +     OFFSET_BIT_TO_CHANNEL(0x01, 6),
> +     OFFSET_BIT_TO_CHANNEL(0x01, 7),
> +     OFFSET_BIT_TO_CHANNEL(0x02, 0),
> +     OFFSET_BIT_TO_CHANNEL(0x02, 1),
> +     OFFSET_BIT_TO_CHANNEL(0x02, 2),
> +     OFFSET_BIT_TO_CHANNEL(0x09, 0),
> +     OFFSET_BIT_TO_CHANNEL(0x09, 1),
> +     OFFSET_BIT_TO_CHANNEL(0x09, 2),
> +     OFFSET_BIT_TO_CHANNEL(0x09, 3),
> +     OFFSET_BIT_TO_CHANNEL(0x0a, 0),
> +     OFFSET_BIT_TO_CHANNEL(0x0a, 1),
> +     OFFSET_BIT_TO_CHANNEL(0x0a, 2),
> +     OFFSET_BIT_TO_CHANNEL(0x0a, 3),
> +     OFFSET_BIT_TO_CHANNEL(0x0a, 4),
> +     OFFSET_BIT_TO_CHANNEL(0x0a, 5),
> +     OFFSET_BIT_TO_CHANNEL(0x0a, 6),
> +     OFFSET_BIT_TO_CHANNEL(0x0b, 0),
> +     OFFSET_BIT_TO_CHANNEL(0x0b, 1),
> +     OFFSET_BIT_TO_CHANNEL(0x0b, 2),
> +     OFFSET_BIT_TO_CHANNEL(0x0b, 3),
> +};
> +
> +static struct platform_device *silicom_platform_dev; static struct 
> +led_classdev_mc *silicom_led_info __initdata; static struct gpio_chip 
> +*silicom_gpiochip __initdata; static u8 *silicom_gpio_channels 
> +__initdata;
> +
> +static int silicom_mec_port_get(unsigned int offset) {
> +     u8 reg;
> +
> +     mutex_lock(&mec_io_mutex);
> +     /* Get the dword offset from the channel */
> +     outb((offset >> 3) & 0xfc, MEC_ADDR);
> +
> +     /* Get the current register */
> +     reg = inb(MEC_DATA((offset >> 3) & 0x03));
> +     mutex_unlock(&mec_io_mutex);
> +
> +     return (reg >> (offset & 0x7)) & 0x01; }
> +
> +static enum led_brightness silicom_mec_led_get(int channel) {
> +     /* Outputs are active low */
> +     return silicom_mec_port_get(channel) ? LED_OFF : LED_ON; }
> +
> +static void silicom_mec_port_set(int channel, int on) {
> +     u8 reg;
> +
> +     mutex_lock(&mec_io_mutex);
> +     /* Get the dword offset from the channel */
> +     outb((channel >> 3) & 0xfc, MEC_ADDR);
> +     /* Get the current port settings */
> +     reg = inb(MEC_DATA((channel >> 3) & 0x03));
> +     /* Outputs are active low, so clear the bit for on, or set it for off */
> +     if (on)
> +             reg &= ~(1 << (channel & 0x7));
> +     else
> +             reg |= 1 << (channel & 0x7);
> +     /* Write back the updated register */
> +     outb(reg, MEC_DATA((channel >> 3) & 0x03));
> +     mutex_unlock(&mec_io_mutex);
> +}
> +
> +static enum led_brightness silicom_mec_led_mc_brightness_get(struct 
> +led_classdev *led_cdev) {
> +     struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(led_cdev);
> +     enum led_brightness brightness = LED_OFF;
> +     int i;
> +
> +     for (i = 0; i < mc_cdev->num_colors; i++) {
> +             mc_cdev->subled_info[i].brightness =
> +                     silicom_mec_led_get(mc_cdev->subled_info[i].channel);
> +             /* Mark the overall brightness as LED_ON if any of the subleds are on */
> +             if (mc_cdev->subled_info[i].brightness != LED_OFF)
> +                     brightness = LED_ON;
> +     }
> +
> +     return brightness;
> +}
> +
> +static void silicom_mec_led_mc_brightness_set(struct led_classdev *led_cdev,
> +                                                                                     
> +enum led_brightness brightness) {
> +     struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(led_cdev);
> +     int i;
> +
> +     led_mc_calc_color_components(mc_cdev, brightness);
> +     for (i = 0; i < mc_cdev->num_colors; i++) {
> +             silicom_mec_port_set(mc_cdev->subled_info[i].channel,
> +             mc_cdev->subled_info[i].brightness);
> +     }
> +}
> +
> +static int silicom_gpio_get_direction(struct gpio_chip *gc, unsigned 
> +int offset) {
> +     u8 *channels = gpiochip_get_data(gc);
> +
> +     /* Input registers have offsets between [0x00, 0x07] */
> +     if (CHANNEL_TO_OFFSET(channels[offset]) < 0x08)
> +             return GPIO_LINE_DIRECTION_IN;
> +
> +     return GPIO_LINE_DIRECTION_OUT;
> +}
> +
> +static int silicom_gpio_direction_input(struct gpio_chip *gc, 
> +unsigned int offset) {
> +     int direction = silicom_gpio_get_direction(gc, offset);
> +
> +     return direction == GPIO_LINE_DIRECTION_IN ? 0 : -EINVAL; }
> +
> +static void silicom_gpio_set(struct gpio_chip *gc, unsigned int 
> +offset, int value) {
> +     u8 *channels = gpiochip_get_data(gc);
> +     int direction = silicom_gpio_get_direction(gc, offset);
> +     int channel = channels[offset];
> +
> +     if (direction == GPIO_LINE_DIRECTION_IN)
> +             return;
> +
> +     if (value)
> +             silicom_mec_port_set(channel, 0);
> +     else if (value == 0)
> +             silicom_mec_port_set(channel, 1);
> +     else
> +             pr_err("Wrong argument value: %d\n", value); }
> +
> +static int silicom_gpio_direction_output(struct gpio_chip *gc, 
> +unsigned int offset, int value) {
> +     int direction = silicom_gpio_get_direction(gc, offset);
> +
> +     if (direction == GPIO_LINE_DIRECTION_IN)
> +             return -EINVAL;
> +
> +     silicom_gpio_set(gc, offset, value);
> +
> +     return 0;
> +}
> +
> +static int silicom_gpio_get(struct gpio_chip *gc, unsigned int 
> +offset) {
> +     u8 *channels = gpiochip_get_data(gc);
> +     int channel = channels[offset];
> +
> +     return silicom_mec_port_get(channel); }
> +
> +
> +static struct mc_subled plat_0222_wan_mc_subled_info[] __initdata = {
> +     {
> +             .color_index = LED_COLOR_ID_WHITE,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0c, 7),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_YELLOW,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0c, 6),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_RED,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0c, 5),
> +     },
> +};
> +
> +static struct mc_subled plat_0222_sys_mc_subled_info[] __initdata = {
> +     {
> +             .color_index = LED_COLOR_ID_WHITE,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0c, 4),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_AMBER,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0c, 3),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_RED,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0c, 2),
> +     },
> +};
> +
> +static struct mc_subled plat_0222_stat1_mc_subled_info[] __initdata = {
> +     {
> +             .color_index = LED_COLOR_ID_RED,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0c, 1),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_GREEN,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0c, 0),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_BLUE,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0d, 7),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_YELLOW,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0d, 6),
> +     },
> +};
> +
> +static struct mc_subled plat_0222_stat2_mc_subled_info[] __initdata = {
> +     {
> +             .color_index = LED_COLOR_ID_RED,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0d, 5),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_GREEN,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0d, 4),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_BLUE,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0d, 3),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_YELLOW,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0d, 2),
> +     },
> +};
> +
> +static struct mc_subled plat_0222_stat3_mc_subled_info[] __initdata = {
> +     {
> +             .color_index = LED_COLOR_ID_RED,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0d, 1),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_GREEN,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0d, 0),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_BLUE,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0e, 1),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_YELLOW,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x0e, 0),
> +     },
> +};
> +
> +static struct led_classdev_mc plat_0222_mc_led_info[] __initdata = {
> +     {
> +             .led_cdev = {
> +                     .name = "multicolor:wan",
> +                     .brightness = 0,
> +                     .max_brightness = 1,
> +                     .brightness_set = silicom_mec_led_mc_brightness_set,
> +                     .brightness_get = silicom_mec_led_mc_brightness_get,
> +             },
> +             .num_colors = ARRAY_SIZE(plat_0222_wan_mc_subled_info),
> +             .subled_info = plat_0222_wan_mc_subled_info,
> +     },
> +     {
> +             .led_cdev = {
> +                     .name = "multicolor:sys",
> +                     .brightness = 0,
> +                     .max_brightness = 1,
> +                     .brightness_set = silicom_mec_led_mc_brightness_set,
> +                     .brightness_get = silicom_mec_led_mc_brightness_get,
> +             },
> +             .num_colors = ARRAY_SIZE(plat_0222_sys_mc_subled_info),
> +             .subled_info = plat_0222_sys_mc_subled_info,
> +     },
> +     {
> +             .led_cdev = {
> +                     .name = "multicolor:stat1",
> +                     .brightness = 0,
> +                     .max_brightness = 1,
> +                     .brightness_set = silicom_mec_led_mc_brightness_set,
> +                     .brightness_get = silicom_mec_led_mc_brightness_get,
> +             },
> +             .num_colors = ARRAY_SIZE(plat_0222_stat1_mc_subled_info),
> +             .subled_info = plat_0222_stat1_mc_subled_info,
> +     },
> +     {
> +             .led_cdev = {
> +                     .name = "multicolor:stat2",
> +                     .brightness = 0,
> +                     .max_brightness = 1,
> +                     .brightness_set = silicom_mec_led_mc_brightness_set,
> +                     .brightness_get = silicom_mec_led_mc_brightness_get,
> +             },
> +             .num_colors = ARRAY_SIZE(plat_0222_stat2_mc_subled_info),
> +             .subled_info = plat_0222_stat2_mc_subled_info,
> +     },
> +     {
> +             .led_cdev = {
> +                     .name = "multicolor:stat3",
> +                     .brightness = 0,
> +                     .max_brightness = 1,
> +                     .brightness_set = silicom_mec_led_mc_brightness_set,
> +                     .brightness_get = silicom_mec_led_mc_brightness_get,
> +             },
> +             .num_colors = ARRAY_SIZE(plat_0222_stat3_mc_subled_info),
> +             .subled_info = plat_0222_stat3_mc_subled_info,
> +     },
> +     { },
> +};
> +
> +static struct gpio_chip silicom_gpio_chip = {
> +     .label = "silicom-gpio",
> +     .get_direction = silicom_gpio_get_direction,
> +     .direction_input = silicom_gpio_direction_input,
> +     .direction_output = silicom_gpio_direction_output,
> +     .get = silicom_gpio_get,
> +     .set = silicom_gpio_set,
> +     .base = -1,
> +     .ngpio = ARRAY_SIZE(plat_0222_gpio_channels),
> +     .names = plat_0222_gpio_names,
> +     /* We're using a mutex to protect the indirect access, so we can sleep if the
> +      * lock blocks
> +      */
> +     .can_sleep = true,
> +};
> +
> +static struct silicom_platform_info silicom_plat_0222_cordoba_info __initdata = {
> +     .io_base = 0x800,
> +     .io_len = 8,
> +     .led_info = plat_0222_mc_led_info,
> +     .gpiochip = &silicom_gpio_chip,
> +     .gpio_channels = plat_0222_gpio_channels,
> +     /* The original generic cordoba does not have the last 4 outputs of the plat_0222
> +      * BB variant, the rest are the same, so use the same longer list, but ignore the
> +      * last entries here
> +      */
> +     .ngpio = ARRAY_SIZE(plat_0222_gpio_channels),
> +
> +};
> +
> +static struct mc_subled cordoba_fp_left_mc_subled_info[] __initdata = {
> +     {
> +             .color_index = LED_COLOR_ID_RED,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x08, 6),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_GREEN,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x08, 5),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_BLUE,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x09, 7),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_AMBER,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x09, 4),
> +     },
> +};
> +
> +static struct mc_subled cordoba_fp_center_mc_subled_info[] __initdata = {
> +     {
> +             .color_index = LED_COLOR_ID_RED,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x08, 7),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_GREEN,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x08, 4),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_BLUE,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x08, 3),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_AMBER,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x09, 6),
> +     },
> +};
> +
> +static struct mc_subled cordoba_fp_right_mc_subled_info[] __initdata = {
> +     {
> +             .color_index = LED_COLOR_ID_RED,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x08, 2),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_GREEN,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x08, 1),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_BLUE,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x08, 0),
> +     },
> +     {
> +             .color_index = LED_COLOR_ID_AMBER,
> +             .brightness = 1,
> +             .intensity = 0,
> +             .channel = OFFSET_BIT_TO_CHANNEL(0x09, 5),
> +     },
> +};
> +
> +static struct led_classdev_mc cordoba_mc_led_info[] __initdata = {
> +     {
> +             .led_cdev = {
> +                     .name = "multicolor:fp_left",
> +                     .brightness = 0,
> +                     .max_brightness = 1,
> +                     .brightness_set = silicom_mec_led_mc_brightness_set,
> +                     .brightness_get = silicom_mec_led_mc_brightness_get,
> +             },
> +             .num_colors = ARRAY_SIZE(cordoba_fp_left_mc_subled_info),
> +             .subled_info = cordoba_fp_left_mc_subled_info,
> +     },
> +     {
> +             .led_cdev = {
> +                     .name = "multicolor:fp_center",
> +                     .brightness = 0,
> +                     .max_brightness = 1,
> +                     .brightness_set = silicom_mec_led_mc_brightness_set,
> +                     .brightness_get = silicom_mec_led_mc_brightness_get,
> +             },
> +             .num_colors = ARRAY_SIZE(cordoba_fp_center_mc_subled_info),
> +             .subled_info = cordoba_fp_center_mc_subled_info,
> +     },
> +     {
> +             .led_cdev = {
> +                     .name = "multicolor:fp_right",
> +                     .brightness = 0,
> +                     .max_brightness = 1,
> +                     .brightness_set = silicom_mec_led_mc_brightness_set,
> +                     .brightness_get = silicom_mec_led_mc_brightness_get,
> +             },
> +             .num_colors = ARRAY_SIZE(cordoba_fp_right_mc_subled_info),
> +             .subled_info = cordoba_fp_right_mc_subled_info,
> +     },
> +     { },
> +};
> +
> +static struct silicom_platform_info silicom_generic_cordoba_info __initdata = {
> +     .io_base = 0x800,
> +     .io_len = 8,
> +     .led_info = cordoba_mc_led_info,
> +     .gpiochip = &silicom_gpio_chip,
> +     .gpio_channels = plat_0222_gpio_channels,
> +     .ngpio = ARRAY_SIZE(plat_0222_gpio_channels),
> +};
> +
> +static struct platform_driver silicom_platform_driver = {
> +     .driver = {
> +             .name = "silicom-platform",
> +     },
> +};
> +
> +static ssize_t efuse_status_show(struct device *dev, struct device_attribute *attr,
> +                                                             char 
> +*buf) {
> +     u32 reg;
> +
> +     mutex_lock(&mec_io_mutex);
> +     /* Select memory region */
> +     outb(IO_REG_BANK, EC_ADDR_MSB);
> +     outb(MEC_EFUSE_LSB_ADDR, EC_ADDR_LSB);
> +
> +     /* Get current data from the address */
> +     reg = inl(MEC_DATA(DEFAULT_CHAN_LO));
> +     mutex_unlock(&mec_io_mutex);
> +
> +     efuse_status = reg & 0x1;
> +
> +     return sprintf(buf, "%d\n", efuse_status); }
> +
> +static ssize_t uc_version_show(struct device *dev,
> +                                     struct device_attribute *attr,
> +                                     char *buf) {
> +     u32 reg;
> +     int uc_version;
> +
> +     mutex_lock(&mec_io_mutex);
> +     outb(IO_REG_BANK, EC_ADDR_MSB);
> +     outb(DEFAULT_CHAN_LO, EC_ADDR_LSB);
> +
> +     reg = inl(MEC_DATA(DEFAULT_CHAN_LO));
> +     mutex_unlock(&mec_io_mutex);
> +     uc_version = FIELD_GET(GENMASK(15, 8), reg);
> +     if (uc_version >= 192)
> +             pr_err("uc version not supported\n");
> +
> +     uc_version = FIELD_GET(MEC_VERSION_MAJOR, reg) * 100 +
> +                             FIELD_GET(MEC_VERSION_MINOR, reg);
> +
> +     mec_uc_version = uc_version;
> +     return sprintf(buf, "%d\n", mec_uc_version); }
> +
> +static ssize_t power_cycle_show(struct device *dev,
> +                             struct device_attribute *attr,
> +                             char *buf) {
> +     return sprintf(buf, "%d\n", power_cycle); }
> +
> +static void powercycle_uc(void)
> +{
> +     mutex_lock(&mec_io_mutex);
> +     /* Select memory region */
> +     outb(IO_REG_BANK, EC_ADDR_MSB);
> +     outb(MEC_POWER_CYCLE_ADDR, EC_ADDR_LSB);
> +
> +     /* Set to 1 for current data from the address */
> +     outb(1, MEC_DATA(DEFAULT_CHAN_LO));
> +     mutex_unlock(&mec_io_mutex);
> +}
> +
> +static ssize_t power_cycle_store(struct device *dev, struct device_attribute *attr,
> +                              const char *buf, size_t count) {
> +     if (sscanf(buf, "%du", &power_cycle) != 1) {
> +             dev_err(dev, "Failed to read power_cycle\n");
> +             return -EINVAL;
> +     }
> +     if (power_cycle > 0)
> +             powercycle_uc();
> +
> +     return count;
> +}
> +
> +static struct device_attribute my_dev_attr[] = {
> +     {
> +             .attr = {.name = "efuse_status", .mode = 0644},
> +             .show = efuse_status_show,
> +             .store = NULL
> +     },
> +     {
> +             .attr = {.name = "uc_version", .mode = 0644},
> +             .show = uc_version_show,
> +             .store = NULL
> +     },
> +     {
> +             .attr = {.name = "power_cycle", .mode = 0644},
> +             .show = power_cycle_show,
> +             .store = power_cycle_store
> +     },
> +};
> +
> +static int __init silicom_mc_leds_register(struct device *dev,
> +                                        const struct led_classdev_mc 
> +*mc_leds) {
> +     struct led_classdev_mc *led;
> +     int i, err;
> +
> +     for (i = 0; mc_leds[i].led_cdev.name; i++) {
> +             led = devm_kzalloc(dev, sizeof(struct led_classdev_mc), GFP_KERNEL);
> +             if (!led)
> +                     return -ENOMEM;
> +             memcpy(led, &mc_leds[i], sizeof(*led));
> +
> +             led->subled_info = devm_kzalloc(dev, led->num_colors * sizeof(struct mc_subled), GFP_KERNEL);
> +             if (!led->subled_info)
> +                     return -ENOMEM;
> +             memcpy(led->subled_info, mc_leds[i].subled_info, 
> + led->num_colors * sizeof(struct mc_subled));
> +
> +             err = devm_led_classdev_multicolor_register(dev, led);
> +             if (err) {
> +                     dev_err(dev, "Failed to register[%d]: %d\n", i, err);
> +                     return err;
> +             }
> +     }
> +
> +     return 0;
> +}
> +
> +static u32 rpm_get(void)
> +{
> +     u32 reg;
> +
> +     mutex_lock(&mec_io_mutex);
> +     /* Select memory region */
> +     outb(IO_REG_BANK, EC_ADDR_MSB);
> +     outb(0xc, EC_ADDR_LSB);
> +     /* Get current data from the address */
> +     reg = inw(MEC_DATA(DEFAULT_CHAN_LO));
> +     mutex_unlock(&mec_io_mutex);
> +
> +     return reg;
> +}
> +
> +static u32 temp_get(void)
> +{
> +     u32 reg;
> +
> +     mutex_lock(&mec_io_mutex);
> +     /* Select memory region */
> +     outb(IO_REG_BANK, EC_ADDR_MSB);
> +     outb(0xc, EC_ADDR_LSB);
> +     /* Get current data from the address */
> +     reg = inl(MEC_DATA(DEFAULT_CHAN_LO));
> +     mutex_unlock(&mec_io_mutex);
> +
> +     return (reg >> 16) / 10;

The hwmon ABI expects temperatures to be reported in milli-degrees C.
The above sets the maximum temperature to 65,535 / 10 = 6,553 milli-degrees or 6.553 degrees C. It is very unlikely that this is correct.

Again, I commented on this before.

Henry: this is due to an internal implementation of MIcor-controller firmware, instead of putting real temperature to the register, it put (real temperature * 10 ) to the register. So, in order to report correct temperature to user space application, the read value is divided by 10, then report to user space. 

Please let me know if you accept this. If not, I can change the code, but let user space application to do adjustment. 

> +}
> +
> +static umode_t silicom_fan_control_fan_is_visible(const u32 attr) {
> +     switch (attr) {
> +     case hwmon_fan_input:
> +     case hwmon_fan_label:
> +             return 0444;
> +     default:
> +             return 0;
> +     }
> +}
> +
> +static umode_t silicom_fan_control_temp_is_visible(const u32 attr) {
> +     switch (attr) {
> +     case hwmon_temp_input:
> +     case hwmon_temp_label:
> +             return 0444;
> +     default:
> +             return 0;
> +     }
> +}
> +
> +static int silicom_fan_control_read_fan(struct device *dev, u32 attr, 
> +long *val) {
> +     struct silicom_fan_control_data *ctl = dev_get_drvdata(dev);
> +
> +     switch (attr) {
> +     case hwmon_fan_input:
> +             ctl->fan_speed = rpm_get();
> +             *val = ctl->fan_speed;

Storing data in fan_speed is unnecessary.

Henry: OK, it will be removed.

> +             return 0;
> +     default:
> +             return -EOPNOTSUPP;
> +     }
> +}
> +
> +static int silicom_fan_control_read_temp(struct device *dev, u32 
> +attr, long *val) {
> +     switch (attr) {
> +     case hwmon_temp_input:
> +             *val = temp_get();
> +             return 0;
> +     default:
> +             return -EOPNOTSUPP;
> +     }
> +}
> +
> +static umode_t silicom_fan_control_is_visible(const void *data,
> +                                       enum hwmon_sensor_types type,
> +                                       u32 attr, int channel) {
> +     switch (type) {
> +     case hwmon_fan:
> +             return silicom_fan_control_fan_is_visible(attr);
> +     case hwmon_temp:
> +             return silicom_fan_control_temp_is_visible(attr);
> +     default:
> +             return 0;
> +     }
> +}
> +
> +static int silicom_fan_control_read(struct device *dev, enum hwmon_sensor_types type,
> +                                 u32 attr, int channel, long *val) {
> +     switch (type) {
> +     case hwmon_fan:
> +             return silicom_fan_control_read_fan(dev, attr, val);
> +     case hwmon_temp:
> +             return silicom_fan_control_read_temp(dev, attr, val);
> +     default:
> +             return -EOPNOTSUPP;
> +     }
> +}
> +
> +static int silicom_fan_control_read_labels(struct device *dev, enum hwmon_sensor_types type,
> +                                        u32 attr, int channel, const 
> +char **str) {
> +     switch (type) {
> +     case hwmon_fan:
> +             *str = "Fan Speed";
> +             return 0;
> +     case hwmon_temp:
> +             *str = "Thermostat Sensor";
> +             return 0;

Those labels have no practical value.

Henry: Those labels will be used by user space code to identify Silicom_platform driver. 

> +     default:
> +             return -EOPNOTSUPP;
> +     }
> +}
> +
> +static const struct hwmon_ops silicom_fan_control_hwmon_ops = {
> +     .is_visible = silicom_fan_control_is_visible,
> +     .read = silicom_fan_control_read,
> +     .write = NULL,

Unnecessary.

Henry: OK, will be removed silicom_fan_control_is_visible.

> +     .read_string = silicom_fan_control_read_labels, };
> +
> +static const struct hwmon_chip_info silicom_chip_info = {
> +     .ops = &silicom_fan_control_hwmon_ops,
> +     .info = silicom_fan_control_info, };
> +
> +static int __init silicom_platform_probe(struct platform_device 
> +*device) {
> +     int i, err;
> +     u8 magic, ver;
> +     struct silicom_fan_control_data *ctl;
> +     const char *name = "Silocom_Fan_Monitor";
> +     const char *dev_name = "Silicom_platform";
> +
> +     mec_io_base = 0x0800;
> +     mec_io_len = 8;
> +     if (!devm_request_region(&device->dev, mec_io_base, mec_io_len, "mec")) {
> +             dev_err(&device->dev, "couldn't reserve MEC io ports\n");
> +             return -EBUSY;
> +     }
> +
> +     /* Sanity check magic number read for EC */
> +     outb(0x00, MEC_ADDR);
> +     magic = inb(MEC_DATA(DEFAULT_CHAN_LO));
> +     ver = inb(MEC_DATA(DEFAULT_CHAN_HI));
> +     dev_dbg(&device->dev, "EC magic 0x%02x, version 0x%02x\n", 
> + magic, ver);
> +
> +     if (magic != SILICOM_MEC_MAGIC) {
> +             dev_err(&device->dev, "Bad EC magic 0x%02x!\n", magic);
> +             return -ENODEV;
> +     }
> +
> +     err = silicom_mc_leds_register(&device->dev, silicom_led_info);
> +     if (err) {
> +             dev_err(&device->dev, "Failed to register LEDs\n");
> +             return err;
> +     }
> +
> +     err = devm_gpiochip_add_data(&device->dev, silicom_gpiochip, silicom_gpio_channels);
> +     if (err) {
> +             dev_err(&device->dev, "Failed to register gpiochip: %d\n", err);
> +             return err;
> +     }
> +
> +     ctl = devm_kzalloc(&device->dev, sizeof(*ctl), GFP_KERNEL);
> +     if (!ctl)
> +             return -ENOMEM;
> +
> +     ctl->hdev = devm_hwmon_device_register_with_info(&device->dev, name, ctl,
> +                             &silicom_chip_info, NULL);
> +
> +     my_dev = root_device_register(dev_name);
> +     for (i = 0; i < ARRAY_SIZE(my_dev_attr); i++) {
> +             err = sysfs_create_file(&my_dev->kobj, &my_dev_attr[i].attr);
> +             if (err) {
> +                     pr_debug("failed to create the foo file in /sys/devices/Silicom_platform\n");
> +                     break;
> +             }
> +     }
> +
> +     return err;
> +}
> +
> +static int __init silicom_platform_info_init(const struct 
> +dmi_system_id *id) {
> +     struct silicom_platform_info *info = id->driver_data;
> +
> +     dev_info(&silicom_platform_dev->dev, "Detected %s\n", 
> + id->ident);
> +
> +     mec_io_base = info->io_base;
> +     mec_io_len = info->io_len;
> +     silicom_led_info = info->led_info;
> +     silicom_gpio_channels = info->gpio_channels;
> +     silicom_gpiochip = info->gpiochip;
> +     if (silicom_gpiochip)
> +             silicom_gpiochip->ngpio = info->ngpio;
> +
> +     return 1;
> +}
> +
> +static const struct dmi_system_id silicom_dmi_ids[] __initconst = {
> +     {
> +             .callback = silicom_platform_info_init,
> +             .ident = "Silicom Cordoba (Generic)",
> +             .matches = {
> +                     DMI_MATCH(DMI_BOARD_VENDOR, "Silicom"),
> +                     DMI_MATCH(DMI_BOARD_NAME, "80300-0214-G"),
> +             },
> +             .driver_data = &silicom_generic_cordoba_info,
> +     },
> +     {
> +             .callback = silicom_platform_info_init,
> +             .ident = "Silicom Cordoba (Generic)",
> +             .matches = {
> +                     DMI_MATCH(DMI_BOARD_VENDOR, "Silicom"),
> +                     DMI_MATCH(DMI_BOARD_NAME, "80500-0214-G"),
> +             },
> +             .driver_data = &silicom_generic_cordoba_info,
> +     },
> +     {
> +              .callback = silicom_platform_info_init,
> +              .ident = "Silicom Cordoba (plat_0222)",
> +              .matches = {
> +                     DMI_MATCH(DMI_BOARD_VENDOR, "Silicom"),
> +                     DMI_MATCH(DMI_BOARD_NAME, "80300-0222-G"),
> +              },
> +             .driver_data = &silicom_plat_0222_cordoba_info,
> +     },
> +     { },
> +};
> +
> +static int __init silicom_platform_init(void) {
> +     struct device *dev;
> +     int err;
> +
> +     /* register a platform device to act as the parent for LEDS, etc. */
> +     silicom_platform_dev = platform_device_register_simple("silicom-platform", -1, NULL, 0);
> +     if (IS_ERR(silicom_platform_dev)) {
> +             err = PTR_ERR(silicom_platform_dev);
> +             pr_err("failed to register silicom-platform device: %d\n", err);
> +             goto silicom_init_register_err;
> +     }
> +     dev = &silicom_platform_dev->dev;
> +
> +     err = dmi_check_system(silicom_dmi_ids);
> +     if (err == 0) {
> +             dev_err(dev, "No DMI match for this platform\n");
> +             err = -ENODEV;
> +             goto silicom_init_probe_err;
> +     }
> +
> +     /* Directly probe the platform driver in init since this isn't a
> +      * hotpluggable device.  That means we don't need to register a driver
> +      * that needs to wait around in memory on the chance a matching device
> +      * would get added.  Instead run once in __init so that we can free all
> +      * those resources when the __init region is wiped
> +      */
> +     err = platform_driver_probe(&silicom_platform_driver, silicom_platform_probe);
> +     if (err) {
> +             dev_err(dev, "Failed to probe platform driver %d\n", err);
> +             goto silicom_init_probe_err;
> +     }
> +
> +     return 0;
> +
> +silicom_init_probe_err:
> +     if (silicom_platform_dev) {
> +             platform_device_unregister(silicom_platform_dev);
> +             silicom_platform_dev = NULL;
> +     }
> +     if (my_dev) {
> +             root_device_unregister(my_dev);
> +             my_dev = NULL;
> +     }
> +
> +silicom_init_register_err:
> +     return err;
> +}
> +
> +static void __exit silicom_platform_exit(void) {
> +     int i;
> +
> +     if (silicom_platform_dev) {
> +             platform_device_unregister(silicom_platform_dev);
> +             platform_driver_unregister(&silicom_platform_driver);
> +     }
> +
> +     if (my_dev) {
> +             for (i = 0; i < ARRAY_SIZE(my_dev_attr); i++)
> +                     sysfs_remove_file(&my_dev->kobj, &my_dev_attr[i].attr);
> +             root_device_unregister(my_dev);
> +     }
> +     mutex_destroy(&mec_io_mutex);
> +}
> +
> +module_init(silicom_platform_init);
> +module_exit(silicom_platform_exit);
> +
> +MODULE_LICENSE("GPL");
> +MODULE_AUTHOR("Henry Shi <henrys@silicom-usa.com>"); 
> +MODULE_DESCRIPTION("Platform driver for Silicom network appliances");
> +
> +MODULE_DEVICE_TABLE(dmi, silicom_dmi_ids);

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

* Re: [PATCH] Add Silicom Platform Driver
  2023-08-18 15:43 Henry Shi
@ 2023-08-19 12:41 ` Guenter Roeck
  2023-08-19 14:20   ` Huibin Shi
  0 siblings, 1 reply; 32+ messages in thread
From: Guenter Roeck @ 2023-08-19 12:41 UTC (permalink / raw)
  To: Henry Shi
  Cc: hbshi69, tglx, mingo, bp, dave.hansen, x86, hpa, hdegoede,
	markgross, jdelvare, linux-kernel, platform-driver-x86,
	linux-hwmon, hb_shi2003, henrys, wenw

On Fri, Aug 18, 2023 at 11:43:41AM -0400, Henry Shi wrote:

Description missing.

This is at least v4 of this patch. No versioning, no change log.
That was commented on before. This is the last time I am going
to provide feedback, even more so since most of my previous
feedback was ignored.

Note that my comments only refer to problems in hwmon code and
do by no means suggest that the rest of the code may be acceptable.
 
> Signed-off-by: Henry Shi <henryshi2018@gmail.com>
> ---
>  drivers/platform/x86/Kconfig            |   11 +
>  drivers/platform/x86/Makefile           |    1 +
>  drivers/platform/x86/silicom-platform.c | 1046 +++++++++++++++++++++++
>  3 files changed, 1058 insertions(+)
>  create mode 100644 drivers/platform/x86/silicom-platform.c
> 
> diff --git a/drivers/platform/x86/Kconfig b/drivers/platform/x86/Kconfig
> index 22052031c719..7680c0dbcd8d 100644
> --- a/drivers/platform/x86/Kconfig
> +++ b/drivers/platform/x86/Kconfig
> @@ -188,6 +188,17 @@ config ACER_WMI
>  	  If you have an ACPI-WMI compatible Acer/ Wistron laptop, say Y or M
>  	  here.
>  
> +config SILICOM_PLATFORM
> +	tristate "Silicom Edge Networking device support"
> +	depends on DMI
> +	select LEDS_CLASS_MULTICOLOR
> +	select GPIOLIB
> +	help
> +	  This option enables support for the LEDs/GPIO/etc downstream of the
> +	  embedded controller on Silicom "Cordoba" hardware and derivatives.
> +
> +	  If you have a Silicom network appliance, say Y or M here.
> +
>  source "drivers/platform/x86/amd/Kconfig"
>  
>  config ADV_SWBUTTON
> diff --git a/drivers/platform/x86/Makefile b/drivers/platform/x86/Makefile
> index 2cafe51ec4d8..9355ebbc56ca 100644
> --- a/drivers/platform/x86/Makefile
> +++ b/drivers/platform/x86/Makefile
> @@ -113,6 +113,7 @@ obj-$(CONFIG_SERIAL_MULTI_INSTANTIATE)	+= serial-multi-instantiate.o
>  obj-$(CONFIG_MLX_PLATFORM)		+= mlx-platform.o
>  obj-$(CONFIG_TOUCHSCREEN_DMI)		+= touchscreen_dmi.o
>  obj-$(CONFIG_WIRELESS_HOTKEY)		+= wireless-hotkey.o
> +obj-$(CONFIG_SILICOM_PLATFORM)		+= silicom-platform.o
>  obj-$(CONFIG_X86_ANDROID_TABLETS)	+= x86-android-tablets/
>  
>  # Intel uncore drivers
> diff --git a/drivers/platform/x86/silicom-platform.c b/drivers/platform/x86/silicom-platform.c
> new file mode 100644
> index 000000000000..ddf9961e2b9e
> --- /dev/null
> +++ b/drivers/platform/x86/silicom-platform.c
> @@ -0,0 +1,1046 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +//
> +// silicom-platform.c - Silicom MEC170x platform driver
> +//
> +// Copyright (C) 2023 Henry Shi <henrys@silicom-usa.com>
> +
> +#include <linux/dmi.h>
> +#include <linux/gpio/driver.h>
> +#include <linux/init.h>
> +#include <linux/ioport.h>
> +#include <linux/io.h>
> +#include <linux/kernel.h>
> +#include <linux/led-class-multicolor.h>
> +#include <linux/module.h>
> +#include <linux/hwmon.h>
> +#include <linux/mutex.h>
> +#include <linux/platform_device.h>
> +#include <linux/string.h>
> +#include <linux/kobject.h>
> +#include <linux/sysfs.h>
> +#include <linux/bits.h>
> +#include <linux/bitfield.h>
> +
> +#define MEC_ADDR ((mec_io_base) + 0x02)
> +#define MEC_DATA(offset) ((mec_io_base) + 0x04 + (offset))
> +#define EC_ADDR_LSB MEC_ADDR
> +#define EC_ADDR_MSB ((mec_io_base) + 0x03)
> +#define SILICOM_MEC_MAGIC 0x5a
> +#define OFFSET_BIT_TO_CHANNEL(off, bit) ((((off) + 0x014) << 3) | (bit))
> +#define CHANNEL_TO_OFFSET(chan) (((chan) >> 3) - 0x14)
> +#define MEC_VERSION_MAJOR GENMASK(15, 14)
> +#define MEC_VERSION_MINOR GENMASK(13, 8)
> +#define IO_REG_BANK 0
> +#define DEFAULT_CHAN_LO 0
> +#define DEFAULT_CHAN_HI 0
> +#define MEC_EFUSE_LSB_ADDR 0x28
> +#define MEC_POWER_CYCLE_ADDR 0x24
> +
> +static DEFINE_MUTEX(mec_io_mutex);
> +static int mec_io_base, mec_io_len;
> +static struct device *my_dev;
> +static int efuse_status;
> +static int mec_uc_version;
> +static int power_cycle;
> +
> +struct silicom_fan_control_data {
> +	struct   device *hdev;

Unnecessary.

> +	int      temp;

Unused

> +	int      fan_speed;

Unnecessary.

> +};

This entire data structure is unnecessary. I did comment on that before.

> +
> +static const struct hwmon_channel_info *silicom_fan_control_info[] = {
> +	HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT | HWMON_F_LABEL),
> +	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT | HWMON_T_LABEL),
> +	NULL
> +};
> +
> +struct silicom_platform_info {
> +	int io_base;
> +	int io_len;
> +	struct led_classdev_mc *led_info;
> +	struct gpio_chip *gpiochip;
> +	u8 *gpio_channels;
> +	u16 ngpio;
> +};
> +
> +static const char * const plat_0222_gpio_names[] = {
> +	"AUTOM0_SFP_TX_FAULT",
> +	"SLOT2_LED_OUT",
> +	"SIM_M2_SLOT2_B_DET",
> +	"SIM_M2_SLOT2_A_DET",
> +	"SLOT1_LED_OUT",
> +	"SIM_M2_SLOT1_B_DET",
> +	"SIM_M2_SLOT1_A_DET",
> +	"SLOT0_LED_OUT",
> +	"WAN_SFP0_RX_LOS",
> +	"WAN_SFP0_PRSNT_N",
> +	"WAN_SFP0_TX_FAULT",
> +	"AUTOM1_SFP_RX_LOS",
> +	"AUTOM1_SFP_PRSNT_N",
> +	"AUTOM1_SFP_TX_FAULT",
> +	"AUTOM0_SFP_RX_LOS",
> +	"AUTOM0_SFP_PRSNT_N",
> +	"WAN_SFP1_RX_LOS",
> +	"WAN_SFP1_PRSNT_N",
> +	"WAN_SFP1_TX_FAULT",
> +	"SIM_M2_SLOT1_MUX_SEL",
> +	"W_DISABLE_M2_SLOT1_N",
> +	"W_DISABLE_MPCIE_SLOT0_N",
> +	"W_DISABLE_M2_SLOT0_N",
> +	"BT_COMMAND_MODE",
> +	"WAN_SFP1_TX_DISABLE",
> +	"WAN_SFP0_TX_DISABLE",
> +	"AUTOM1_SFP_TX_DISABLE",
> +	"AUTOM0_SFP_TX_DISABLE",
> +	"SIM_M2_SLOT2_MUX_SEL",
> +	"W_DISABLE_M2_SLOT2_N",
> +	"RST_CTL_M2_SLOT_1_N",
> +	"RST_CTL_M2_SLOT_2_N",
> +	"PM_USB_PWR_EN_BOT",
> +	"PM_USB_PWR_EN_TOP",
> +};
> +
> +static u8 plat_0222_gpio_channels[] = {
> +	OFFSET_BIT_TO_CHANNEL(0x00, 0),
> +	OFFSET_BIT_TO_CHANNEL(0x00, 1),
> +	OFFSET_BIT_TO_CHANNEL(0x00, 2),
> +	OFFSET_BIT_TO_CHANNEL(0x00, 3),
> +	OFFSET_BIT_TO_CHANNEL(0x00, 4),
> +	OFFSET_BIT_TO_CHANNEL(0x00, 5),
> +	OFFSET_BIT_TO_CHANNEL(0x00, 6),
> +	OFFSET_BIT_TO_CHANNEL(0x00, 7),
> +	OFFSET_BIT_TO_CHANNEL(0x01, 0),
> +	OFFSET_BIT_TO_CHANNEL(0x01, 1),
> +	OFFSET_BIT_TO_CHANNEL(0x01, 2),
> +	OFFSET_BIT_TO_CHANNEL(0x01, 3),
> +	OFFSET_BIT_TO_CHANNEL(0x01, 4),
> +	OFFSET_BIT_TO_CHANNEL(0x01, 5),
> +	OFFSET_BIT_TO_CHANNEL(0x01, 6),
> +	OFFSET_BIT_TO_CHANNEL(0x01, 7),
> +	OFFSET_BIT_TO_CHANNEL(0x02, 0),
> +	OFFSET_BIT_TO_CHANNEL(0x02, 1),
> +	OFFSET_BIT_TO_CHANNEL(0x02, 2),
> +	OFFSET_BIT_TO_CHANNEL(0x09, 0),
> +	OFFSET_BIT_TO_CHANNEL(0x09, 1),
> +	OFFSET_BIT_TO_CHANNEL(0x09, 2),
> +	OFFSET_BIT_TO_CHANNEL(0x09, 3),
> +	OFFSET_BIT_TO_CHANNEL(0x0a, 0),
> +	OFFSET_BIT_TO_CHANNEL(0x0a, 1),
> +	OFFSET_BIT_TO_CHANNEL(0x0a, 2),
> +	OFFSET_BIT_TO_CHANNEL(0x0a, 3),
> +	OFFSET_BIT_TO_CHANNEL(0x0a, 4),
> +	OFFSET_BIT_TO_CHANNEL(0x0a, 5),
> +	OFFSET_BIT_TO_CHANNEL(0x0a, 6),
> +	OFFSET_BIT_TO_CHANNEL(0x0b, 0),
> +	OFFSET_BIT_TO_CHANNEL(0x0b, 1),
> +	OFFSET_BIT_TO_CHANNEL(0x0b, 2),
> +	OFFSET_BIT_TO_CHANNEL(0x0b, 3),
> +};
> +
> +static struct platform_device *silicom_platform_dev;
> +static struct led_classdev_mc *silicom_led_info __initdata;
> +static struct gpio_chip *silicom_gpiochip __initdata;
> +static u8 *silicom_gpio_channels __initdata;
> +
> +static int silicom_mec_port_get(unsigned int offset)
> +{
> +	u8 reg;
> +
> +	mutex_lock(&mec_io_mutex);
> +	/* Get the dword offset from the channel */
> +	outb((offset >> 3) & 0xfc, MEC_ADDR);
> +
> +	/* Get the current register */
> +	reg = inb(MEC_DATA((offset >> 3) & 0x03));
> +	mutex_unlock(&mec_io_mutex);
> +
> +	return (reg >> (offset & 0x7)) & 0x01;
> +}
> +
> +static enum led_brightness silicom_mec_led_get(int channel)
> +{
> +	/* Outputs are active low */
> +	return silicom_mec_port_get(channel) ? LED_OFF : LED_ON;
> +}
> +
> +static void silicom_mec_port_set(int channel, int on)
> +{
> +	u8 reg;
> +
> +	mutex_lock(&mec_io_mutex);
> +	/* Get the dword offset from the channel */
> +	outb((channel >> 3) & 0xfc, MEC_ADDR);
> +	/* Get the current port settings */
> +	reg = inb(MEC_DATA((channel >> 3) & 0x03));
> +	/* Outputs are active low, so clear the bit for on, or set it for off */
> +	if (on)
> +		reg &= ~(1 << (channel & 0x7));
> +	else
> +		reg |= 1 << (channel & 0x7);
> +	/* Write back the updated register */
> +	outb(reg, MEC_DATA((channel >> 3) & 0x03));
> +	mutex_unlock(&mec_io_mutex);
> +}
> +
> +static enum led_brightness silicom_mec_led_mc_brightness_get(struct led_classdev *led_cdev)
> +{
> +	struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(led_cdev);
> +	enum led_brightness brightness = LED_OFF;
> +	int i;
> +
> +	for (i = 0; i < mc_cdev->num_colors; i++) {
> +		mc_cdev->subled_info[i].brightness =
> +			silicom_mec_led_get(mc_cdev->subled_info[i].channel);
> +		/* Mark the overall brightness as LED_ON if any of the subleds are on */
> +		if (mc_cdev->subled_info[i].brightness != LED_OFF)
> +			brightness = LED_ON;
> +	}
> +
> +	return brightness;
> +}
> +
> +static void silicom_mec_led_mc_brightness_set(struct led_classdev *led_cdev,
> +											enum led_brightness brightness)
> +{
> +	struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(led_cdev);
> +	int i;
> +
> +	led_mc_calc_color_components(mc_cdev, brightness);
> +	for (i = 0; i < mc_cdev->num_colors; i++) {
> +		silicom_mec_port_set(mc_cdev->subled_info[i].channel,
> +		mc_cdev->subled_info[i].brightness);
> +	}
> +}
> +
> +static int silicom_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
> +{
> +	u8 *channels = gpiochip_get_data(gc);
> +
> +	/* Input registers have offsets between [0x00, 0x07] */
> +	if (CHANNEL_TO_OFFSET(channels[offset]) < 0x08)
> +		return GPIO_LINE_DIRECTION_IN;
> +
> +	return GPIO_LINE_DIRECTION_OUT;
> +}
> +
> +static int silicom_gpio_direction_input(struct gpio_chip *gc, unsigned int offset)
> +{
> +	int direction = silicom_gpio_get_direction(gc, offset);
> +
> +	return direction == GPIO_LINE_DIRECTION_IN ? 0 : -EINVAL;
> +}
> +
> +static void silicom_gpio_set(struct gpio_chip *gc, unsigned int offset, int value)
> +{
> +	u8 *channels = gpiochip_get_data(gc);
> +	int direction = silicom_gpio_get_direction(gc, offset);
> +	int channel = channels[offset];
> +
> +	if (direction == GPIO_LINE_DIRECTION_IN)
> +		return;
> +
> +	if (value)
> +		silicom_mec_port_set(channel, 0);
> +	else if (value == 0)
> +		silicom_mec_port_set(channel, 1);
> +	else
> +		pr_err("Wrong argument value: %d\n", value);
> +}
> +
> +static int silicom_gpio_direction_output(struct gpio_chip *gc, unsigned int offset, int value)
> +{
> +	int direction = silicom_gpio_get_direction(gc, offset);
> +
> +	if (direction == GPIO_LINE_DIRECTION_IN)
> +		return -EINVAL;
> +
> +	silicom_gpio_set(gc, offset, value);
> +
> +	return 0;
> +}
> +
> +static int silicom_gpio_get(struct gpio_chip *gc, unsigned int offset)
> +{
> +	u8 *channels = gpiochip_get_data(gc);
> +	int channel = channels[offset];
> +
> +	return silicom_mec_port_get(channel);
> +}
> +
> +
> +static struct mc_subled plat_0222_wan_mc_subled_info[] __initdata = {
> +	{
> +		.color_index = LED_COLOR_ID_WHITE,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 7),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_YELLOW,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 6),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_RED,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 5),
> +	},
> +};
> +
> +static struct mc_subled plat_0222_sys_mc_subled_info[] __initdata = {
> +	{
> +		.color_index = LED_COLOR_ID_WHITE,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 4),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_AMBER,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 3),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_RED,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 2),
> +	},
> +};
> +
> +static struct mc_subled plat_0222_stat1_mc_subled_info[] __initdata = {
> +	{
> +		.color_index = LED_COLOR_ID_RED,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 1),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_GREEN,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 0),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_BLUE,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 7),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_YELLOW,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 6),
> +	},
> +};
> +
> +static struct mc_subled plat_0222_stat2_mc_subled_info[] __initdata = {
> +	{
> +		.color_index = LED_COLOR_ID_RED,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 5),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_GREEN,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 4),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_BLUE,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 3),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_YELLOW,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 2),
> +	},
> +};
> +
> +static struct mc_subled plat_0222_stat3_mc_subled_info[] __initdata = {
> +	{
> +		.color_index = LED_COLOR_ID_RED,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 1),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_GREEN,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 0),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_BLUE,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0e, 1),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_YELLOW,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x0e, 0),
> +	},
> +};
> +
> +static struct led_classdev_mc plat_0222_mc_led_info[] __initdata = {
> +	{
> +		.led_cdev = {
> +			.name = "multicolor:wan",
> +			.brightness = 0,
> +			.max_brightness = 1,
> +			.brightness_set = silicom_mec_led_mc_brightness_set,
> +			.brightness_get = silicom_mec_led_mc_brightness_get,
> +		},
> +		.num_colors = ARRAY_SIZE(plat_0222_wan_mc_subled_info),
> +		.subled_info = plat_0222_wan_mc_subled_info,
> +	},
> +	{
> +		.led_cdev = {
> +			.name = "multicolor:sys",
> +			.brightness = 0,
> +			.max_brightness = 1,
> +			.brightness_set = silicom_mec_led_mc_brightness_set,
> +			.brightness_get = silicom_mec_led_mc_brightness_get,
> +		},
> +		.num_colors = ARRAY_SIZE(plat_0222_sys_mc_subled_info),
> +		.subled_info = plat_0222_sys_mc_subled_info,
> +	},
> +	{
> +		.led_cdev = {
> +			.name = "multicolor:stat1",
> +			.brightness = 0,
> +			.max_brightness = 1,
> +			.brightness_set = silicom_mec_led_mc_brightness_set,
> +			.brightness_get = silicom_mec_led_mc_brightness_get,
> +		},
> +		.num_colors = ARRAY_SIZE(plat_0222_stat1_mc_subled_info),
> +		.subled_info = plat_0222_stat1_mc_subled_info,
> +	},
> +	{
> +		.led_cdev = {
> +			.name = "multicolor:stat2",
> +			.brightness = 0,
> +			.max_brightness = 1,
> +			.brightness_set = silicom_mec_led_mc_brightness_set,
> +			.brightness_get = silicom_mec_led_mc_brightness_get,
> +		},
> +		.num_colors = ARRAY_SIZE(plat_0222_stat2_mc_subled_info),
> +		.subled_info = plat_0222_stat2_mc_subled_info,
> +	},
> +	{
> +		.led_cdev = {
> +			.name = "multicolor:stat3",
> +			.brightness = 0,
> +			.max_brightness = 1,
> +			.brightness_set = silicom_mec_led_mc_brightness_set,
> +			.brightness_get = silicom_mec_led_mc_brightness_get,
> +		},
> +		.num_colors = ARRAY_SIZE(plat_0222_stat3_mc_subled_info),
> +		.subled_info = plat_0222_stat3_mc_subled_info,
> +	},
> +	{ },
> +};
> +
> +static struct gpio_chip silicom_gpio_chip = {
> +	.label = "silicom-gpio",
> +	.get_direction = silicom_gpio_get_direction,
> +	.direction_input = silicom_gpio_direction_input,
> +	.direction_output = silicom_gpio_direction_output,
> +	.get = silicom_gpio_get,
> +	.set = silicom_gpio_set,
> +	.base = -1,
> +	.ngpio = ARRAY_SIZE(plat_0222_gpio_channels),
> +	.names = plat_0222_gpio_names,
> +	/* We're using a mutex to protect the indirect access, so we can sleep if the
> +	 * lock blocks
> +	 */
> +	.can_sleep = true,
> +};
> +
> +static struct silicom_platform_info silicom_plat_0222_cordoba_info __initdata = {
> +	.io_base = 0x800,
> +	.io_len = 8,
> +	.led_info = plat_0222_mc_led_info,
> +	.gpiochip = &silicom_gpio_chip,
> +	.gpio_channels = plat_0222_gpio_channels,
> +	/* The original generic cordoba does not have the last 4 outputs of the plat_0222
> +	 * BB variant, the rest are the same, so use the same longer list, but ignore the
> +	 * last entries here
> +	 */
> +	.ngpio = ARRAY_SIZE(plat_0222_gpio_channels),
> +
> +};
> +
> +static struct mc_subled cordoba_fp_left_mc_subled_info[] __initdata = {
> +	{
> +		.color_index = LED_COLOR_ID_RED,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 6),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_GREEN,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 5),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_BLUE,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x09, 7),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_AMBER,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x09, 4),
> +	},
> +};
> +
> +static struct mc_subled cordoba_fp_center_mc_subled_info[] __initdata = {
> +	{
> +		.color_index = LED_COLOR_ID_RED,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 7),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_GREEN,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 4),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_BLUE,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 3),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_AMBER,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x09, 6),
> +	},
> +};
> +
> +static struct mc_subled cordoba_fp_right_mc_subled_info[] __initdata = {
> +	{
> +		.color_index = LED_COLOR_ID_RED,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 2),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_GREEN,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 1),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_BLUE,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 0),
> +	},
> +	{
> +		.color_index = LED_COLOR_ID_AMBER,
> +		.brightness = 1,
> +		.intensity = 0,
> +		.channel = OFFSET_BIT_TO_CHANNEL(0x09, 5),
> +	},
> +};
> +
> +static struct led_classdev_mc cordoba_mc_led_info[] __initdata = {
> +	{
> +		.led_cdev = {
> +			.name = "multicolor:fp_left",
> +			.brightness = 0,
> +			.max_brightness = 1,
> +			.brightness_set = silicom_mec_led_mc_brightness_set,
> +			.brightness_get = silicom_mec_led_mc_brightness_get,
> +		},
> +		.num_colors = ARRAY_SIZE(cordoba_fp_left_mc_subled_info),
> +		.subled_info = cordoba_fp_left_mc_subled_info,
> +	},
> +	{
> +		.led_cdev = {
> +			.name = "multicolor:fp_center",
> +			.brightness = 0,
> +			.max_brightness = 1,
> +			.brightness_set = silicom_mec_led_mc_brightness_set,
> +			.brightness_get = silicom_mec_led_mc_brightness_get,
> +		},
> +		.num_colors = ARRAY_SIZE(cordoba_fp_center_mc_subled_info),
> +		.subled_info = cordoba_fp_center_mc_subled_info,
> +	},
> +	{
> +		.led_cdev = {
> +			.name = "multicolor:fp_right",
> +			.brightness = 0,
> +			.max_brightness = 1,
> +			.brightness_set = silicom_mec_led_mc_brightness_set,
> +			.brightness_get = silicom_mec_led_mc_brightness_get,
> +		},
> +		.num_colors = ARRAY_SIZE(cordoba_fp_right_mc_subled_info),
> +		.subled_info = cordoba_fp_right_mc_subled_info,
> +	},
> +	{ },
> +};
> +
> +static struct silicom_platform_info silicom_generic_cordoba_info __initdata = {
> +	.io_base = 0x800,
> +	.io_len = 8,
> +	.led_info = cordoba_mc_led_info,
> +	.gpiochip = &silicom_gpio_chip,
> +	.gpio_channels = plat_0222_gpio_channels,
> +	.ngpio = ARRAY_SIZE(plat_0222_gpio_channels),
> +};
> +
> +static struct platform_driver silicom_platform_driver = {
> +	.driver = {
> +		.name = "silicom-platform",
> +	},
> +};
> +
> +static ssize_t efuse_status_show(struct device *dev, struct device_attribute *attr,
> +								char *buf)
> +{
> +	u32 reg;
> +
> +	mutex_lock(&mec_io_mutex);
> +	/* Select memory region */
> +	outb(IO_REG_BANK, EC_ADDR_MSB);
> +	outb(MEC_EFUSE_LSB_ADDR, EC_ADDR_LSB);
> +
> +	/* Get current data from the address */
> +	reg = inl(MEC_DATA(DEFAULT_CHAN_LO));
> +	mutex_unlock(&mec_io_mutex);
> +
> +	efuse_status = reg & 0x1;
> +
> +	return sprintf(buf, "%d\n", efuse_status);
> +}
> +
> +static ssize_t uc_version_show(struct device *dev,
> +					struct device_attribute *attr,
> +					char *buf)
> +{
> +	u32 reg;
> +	int uc_version;
> +
> +	mutex_lock(&mec_io_mutex);
> +	outb(IO_REG_BANK, EC_ADDR_MSB);
> +	outb(DEFAULT_CHAN_LO, EC_ADDR_LSB);
> +
> +	reg = inl(MEC_DATA(DEFAULT_CHAN_LO));
> +	mutex_unlock(&mec_io_mutex);
> +	uc_version = FIELD_GET(GENMASK(15, 8), reg);
> +	if (uc_version >= 192)
> +		pr_err("uc version not supported\n");
> +
> +	uc_version = FIELD_GET(MEC_VERSION_MAJOR, reg) * 100 +
> +				FIELD_GET(MEC_VERSION_MINOR, reg);
> +
> +	mec_uc_version = uc_version;
> +	return sprintf(buf, "%d\n", mec_uc_version);
> +}
> +
> +static ssize_t power_cycle_show(struct device *dev,
> +				struct device_attribute *attr,
> +				char *buf)
> +{
> +	return sprintf(buf, "%d\n", power_cycle);
> +}
> +
> +static void powercycle_uc(void)
> +{
> +	mutex_lock(&mec_io_mutex);
> +	/* Select memory region */
> +	outb(IO_REG_BANK, EC_ADDR_MSB);
> +	outb(MEC_POWER_CYCLE_ADDR, EC_ADDR_LSB);
> +
> +	/* Set to 1 for current data from the address */
> +	outb(1, MEC_DATA(DEFAULT_CHAN_LO));
> +	mutex_unlock(&mec_io_mutex);
> +}
> +
> +static ssize_t power_cycle_store(struct device *dev, struct device_attribute *attr,
> +				 const char *buf, size_t count)
> +{
> +	if (sscanf(buf, "%du", &power_cycle) != 1) {
> +		dev_err(dev, "Failed to read power_cycle\n");
> +		return -EINVAL;
> +	}
> +	if (power_cycle > 0)
> +		powercycle_uc();
> +
> +	return count;
> +}
> +
> +static struct device_attribute my_dev_attr[] = {
> +	{
> +		.attr = {.name = "efuse_status", .mode = 0644},
> +		.show = efuse_status_show,
> +		.store = NULL
> +	},
> +	{
> +		.attr = {.name = "uc_version", .mode = 0644},
> +		.show = uc_version_show,
> +		.store = NULL
> +	},
> +	{
> +		.attr = {.name = "power_cycle", .mode = 0644},
> +		.show = power_cycle_show,
> +		.store = power_cycle_store
> +	},
> +};
> +
> +static int __init silicom_mc_leds_register(struct device *dev,
> +					   const struct led_classdev_mc *mc_leds)
> +{
> +	struct led_classdev_mc *led;
> +	int i, err;
> +
> +	for (i = 0; mc_leds[i].led_cdev.name; i++) {
> +		led = devm_kzalloc(dev, sizeof(struct led_classdev_mc), GFP_KERNEL);
> +		if (!led)
> +			return -ENOMEM;
> +		memcpy(led, &mc_leds[i], sizeof(*led));
> +
> +		led->subled_info = devm_kzalloc(dev, led->num_colors * sizeof(struct mc_subled), GFP_KERNEL);
> +		if (!led->subled_info)
> +			return -ENOMEM;
> +		memcpy(led->subled_info, mc_leds[i].subled_info, led->num_colors * sizeof(struct mc_subled));
> +
> +		err = devm_led_classdev_multicolor_register(dev, led);
> +		if (err) {
> +			dev_err(dev, "Failed to register[%d]: %d\n", i, err);
> +			return err;
> +		}
> +	}
> +
> +	return 0;
> +}
> +
> +static u32 rpm_get(void)
> +{
> +	u32 reg;
> +
> +	mutex_lock(&mec_io_mutex);
> +	/* Select memory region */
> +	outb(IO_REG_BANK, EC_ADDR_MSB);
> +	outb(0xc, EC_ADDR_LSB);
> +	/* Get current data from the address */
> +	reg = inw(MEC_DATA(DEFAULT_CHAN_LO));
> +	mutex_unlock(&mec_io_mutex);
> +
> +	return reg;
> +}
> +
> +static u32 temp_get(void)
> +{
> +	u32 reg;
> +
> +	mutex_lock(&mec_io_mutex);
> +	/* Select memory region */
> +	outb(IO_REG_BANK, EC_ADDR_MSB);
> +	outb(0xc, EC_ADDR_LSB);
> +	/* Get current data from the address */
> +	reg = inl(MEC_DATA(DEFAULT_CHAN_LO));
> +	mutex_unlock(&mec_io_mutex);
> +
> +	return (reg >> 16) / 10;

The hwmon ABI expects temperatures to be reported in milli-degrees C.
The above sets the maximum temperature to 65,535 / 10 = 6,553
milli-degrees or 6.553 degrees C. It is very unlikely that this
is correct.

Again, I commented on this before.

> +}
> +
> +static umode_t silicom_fan_control_fan_is_visible(const u32 attr)
> +{
> +	switch (attr) {
> +	case hwmon_fan_input:
> +	case hwmon_fan_label:
> +		return 0444;
> +	default:
> +		return 0;
> +	}
> +}
> +
> +static umode_t silicom_fan_control_temp_is_visible(const u32 attr)
> +{
> +	switch (attr) {
> +	case hwmon_temp_input:
> +	case hwmon_temp_label:
> +		return 0444;
> +	default:
> +		return 0;
> +	}
> +}
> +
> +static int silicom_fan_control_read_fan(struct device *dev, u32 attr, long *val)
> +{
> +	struct silicom_fan_control_data *ctl = dev_get_drvdata(dev);
> +
> +	switch (attr) {
> +	case hwmon_fan_input:
> +		ctl->fan_speed = rpm_get();
> +		*val = ctl->fan_speed;

Storing data in fan_speed is unnecessary.

> +		return 0;
> +	default:
> +		return -EOPNOTSUPP;
> +	}
> +}
> +
> +static int silicom_fan_control_read_temp(struct device *dev, u32 attr, long *val)
> +{
> +	switch (attr) {
> +	case hwmon_temp_input:
> +		*val = temp_get();
> +		return 0;
> +	default:
> +		return -EOPNOTSUPP;
> +	}
> +}
> +
> +static umode_t silicom_fan_control_is_visible(const void *data,
> +					  enum hwmon_sensor_types type,
> +					  u32 attr, int channel)
> +{
> +	switch (type) {
> +	case hwmon_fan:
> +		return silicom_fan_control_fan_is_visible(attr);
> +	case hwmon_temp:
> +		return silicom_fan_control_temp_is_visible(attr);
> +	default:
> +		return 0;
> +	}
> +}
> +
> +static int silicom_fan_control_read(struct device *dev, enum hwmon_sensor_types type,
> +				    u32 attr, int channel, long *val)
> +{
> +	switch (type) {
> +	case hwmon_fan:
> +		return silicom_fan_control_read_fan(dev, attr, val);
> +	case hwmon_temp:
> +		return silicom_fan_control_read_temp(dev, attr, val);
> +	default:
> +		return -EOPNOTSUPP;
> +	}
> +}
> +
> +static int silicom_fan_control_read_labels(struct device *dev, enum hwmon_sensor_types type,
> +					   u32 attr, int channel, const char **str)
> +{
> +	switch (type) {
> +	case hwmon_fan:
> +		*str = "Fan Speed";
> +		return 0;
> +	case hwmon_temp:
> +		*str = "Thermostat Sensor";
> +		return 0;

Those labels have no practical value.

> +	default:
> +		return -EOPNOTSUPP;
> +	}
> +}
> +
> +static const struct hwmon_ops silicom_fan_control_hwmon_ops = {
> +	.is_visible = silicom_fan_control_is_visible,
> +	.read = silicom_fan_control_read,
> +	.write = NULL,

Unnecessary.

> +	.read_string = silicom_fan_control_read_labels,
> +};
> +
> +static const struct hwmon_chip_info silicom_chip_info = {
> +	.ops = &silicom_fan_control_hwmon_ops,
> +	.info = silicom_fan_control_info,
> +};
> +
> +static int __init silicom_platform_probe(struct platform_device *device)
> +{
> +	int i, err;
> +	u8 magic, ver;
> +	struct silicom_fan_control_data *ctl;
> +	const char *name = "Silocom_Fan_Monitor";
> +	const char *dev_name = "Silicom_platform";
> +
> +	mec_io_base = 0x0800;
> +	mec_io_len = 8;
> +	if (!devm_request_region(&device->dev, mec_io_base, mec_io_len, "mec")) {
> +		dev_err(&device->dev, "couldn't reserve MEC io ports\n");
> +		return -EBUSY;
> +	}
> +
> +	/* Sanity check magic number read for EC */
> +	outb(0x00, MEC_ADDR);
> +	magic = inb(MEC_DATA(DEFAULT_CHAN_LO));
> +	ver = inb(MEC_DATA(DEFAULT_CHAN_HI));
> +	dev_dbg(&device->dev, "EC magic 0x%02x, version 0x%02x\n", magic, ver);
> +
> +	if (magic != SILICOM_MEC_MAGIC) {
> +		dev_err(&device->dev, "Bad EC magic 0x%02x!\n", magic);
> +		return -ENODEV;
> +	}
> +
> +	err = silicom_mc_leds_register(&device->dev, silicom_led_info);
> +	if (err) {
> +		dev_err(&device->dev, "Failed to register LEDs\n");
> +		return err;
> +	}
> +
> +	err = devm_gpiochip_add_data(&device->dev, silicom_gpiochip, silicom_gpio_channels);
> +	if (err) {
> +		dev_err(&device->dev, "Failed to register gpiochip: %d\n", err);
> +		return err;
> +	}
> +
> +	ctl = devm_kzalloc(&device->dev, sizeof(*ctl), GFP_KERNEL);
> +	if (!ctl)
> +		return -ENOMEM;
> +
> +	ctl->hdev = devm_hwmon_device_register_with_info(&device->dev, name, ctl,
> +				&silicom_chip_info, NULL);
> +
> +	my_dev = root_device_register(dev_name);
> +	for (i = 0; i < ARRAY_SIZE(my_dev_attr); i++) {
> +		err = sysfs_create_file(&my_dev->kobj, &my_dev_attr[i].attr);
> +		if (err) {
> +			pr_debug("failed to create the foo file in /sys/devices/Silicom_platform\n");
> +			break;
> +		}
> +	}
> +
> +	return err;
> +}
> +
> +static int __init silicom_platform_info_init(const struct dmi_system_id *id)
> +{
> +	struct silicom_platform_info *info = id->driver_data;
> +
> +	dev_info(&silicom_platform_dev->dev, "Detected %s\n", id->ident);
> +
> +	mec_io_base = info->io_base;
> +	mec_io_len = info->io_len;
> +	silicom_led_info = info->led_info;
> +	silicom_gpio_channels = info->gpio_channels;
> +	silicom_gpiochip = info->gpiochip;
> +	if (silicom_gpiochip)
> +		silicom_gpiochip->ngpio = info->ngpio;
> +
> +	return 1;
> +}
> +
> +static const struct dmi_system_id silicom_dmi_ids[] __initconst = {
> +	{
> +		.callback = silicom_platform_info_init,
> +		.ident = "Silicom Cordoba (Generic)",
> +		.matches = {
> +			DMI_MATCH(DMI_BOARD_VENDOR, "Silicom"),
> +			DMI_MATCH(DMI_BOARD_NAME, "80300-0214-G"),
> +		},
> +		.driver_data = &silicom_generic_cordoba_info,
> +	},
> +	{
> +		.callback = silicom_platform_info_init,
> +		.ident = "Silicom Cordoba (Generic)",
> +		.matches = {
> +			DMI_MATCH(DMI_BOARD_VENDOR, "Silicom"),
> +			DMI_MATCH(DMI_BOARD_NAME, "80500-0214-G"),
> +		},
> +		.driver_data = &silicom_generic_cordoba_info,
> +	},
> +	{
> +		 .callback = silicom_platform_info_init,
> +		 .ident = "Silicom Cordoba (plat_0222)",
> +		 .matches = {
> +			DMI_MATCH(DMI_BOARD_VENDOR, "Silicom"),
> +			DMI_MATCH(DMI_BOARD_NAME, "80300-0222-G"),
> +		 },
> +		.driver_data = &silicom_plat_0222_cordoba_info,
> +	},
> +	{ },
> +};
> +
> +static int __init silicom_platform_init(void)
> +{
> +	struct device *dev;
> +	int err;
> +
> +	/* register a platform device to act as the parent for LEDS, etc. */
> +	silicom_platform_dev = platform_device_register_simple("silicom-platform", -1, NULL, 0);
> +	if (IS_ERR(silicom_platform_dev)) {
> +		err = PTR_ERR(silicom_platform_dev);
> +		pr_err("failed to register silicom-platform device: %d\n", err);
> +		goto silicom_init_register_err;
> +	}
> +	dev = &silicom_platform_dev->dev;
> +
> +	err = dmi_check_system(silicom_dmi_ids);
> +	if (err == 0) {
> +		dev_err(dev, "No DMI match for this platform\n");
> +		err = -ENODEV;
> +		goto silicom_init_probe_err;
> +	}
> +
> +	/* Directly probe the platform driver in init since this isn't a
> +	 * hotpluggable device.  That means we don't need to register a driver
> +	 * that needs to wait around in memory on the chance a matching device
> +	 * would get added.  Instead run once in __init so that we can free all
> +	 * those resources when the __init region is wiped
> +	 */
> +	err = platform_driver_probe(&silicom_platform_driver, silicom_platform_probe);
> +	if (err) {
> +		dev_err(dev, "Failed to probe platform driver %d\n", err);
> +		goto silicom_init_probe_err;
> +	}
> +
> +	return 0;
> +
> +silicom_init_probe_err:
> +	if (silicom_platform_dev) {
> +		platform_device_unregister(silicom_platform_dev);
> +		silicom_platform_dev = NULL;
> +	}
> +	if (my_dev) {
> +		root_device_unregister(my_dev);
> +		my_dev = NULL;
> +	}
> +
> +silicom_init_register_err:
> +	return err;
> +}
> +
> +static void __exit silicom_platform_exit(void)
> +{
> +	int i;
> +
> +	if (silicom_platform_dev) {
> +		platform_device_unregister(silicom_platform_dev);
> +		platform_driver_unregister(&silicom_platform_driver);
> +	}
> +
> +	if (my_dev) {
> +		for (i = 0; i < ARRAY_SIZE(my_dev_attr); i++)
> +			sysfs_remove_file(&my_dev->kobj, &my_dev_attr[i].attr);
> +		root_device_unregister(my_dev);
> +	}
> +	mutex_destroy(&mec_io_mutex);
> +}
> +
> +module_init(silicom_platform_init);
> +module_exit(silicom_platform_exit);
> +
> +MODULE_LICENSE("GPL");
> +MODULE_AUTHOR("Henry Shi <henrys@silicom-usa.com>");
> +MODULE_DESCRIPTION("Platform driver for Silicom network appliances");
> +
> +MODULE_DEVICE_TABLE(dmi, silicom_dmi_ids);

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

* [PATCH] Add Silicom Platform Driver
@ 2023-08-18 15:43 Henry Shi
  2023-08-19 12:41 ` Guenter Roeck
  0 siblings, 1 reply; 32+ messages in thread
From: Henry Shi @ 2023-08-18 15:43 UTC (permalink / raw)
  To: hbshi69, tglx, mingo, bp, dave.hansen, x86, hpa, hdegoede,
	markgross, jdelvare, linux, linux-kernel, platform-driver-x86,
	linux-hwmon
  Cc: hb_shi2003, henrys, wenw

Signed-off-by: Henry Shi <henryshi2018@gmail.com>
---
 drivers/platform/x86/Kconfig            |   11 +
 drivers/platform/x86/Makefile           |    1 +
 drivers/platform/x86/silicom-platform.c | 1046 +++++++++++++++++++++++
 3 files changed, 1058 insertions(+)
 create mode 100644 drivers/platform/x86/silicom-platform.c

diff --git a/drivers/platform/x86/Kconfig b/drivers/platform/x86/Kconfig
index 22052031c719..7680c0dbcd8d 100644
--- a/drivers/platform/x86/Kconfig
+++ b/drivers/platform/x86/Kconfig
@@ -188,6 +188,17 @@ config ACER_WMI
 	  If you have an ACPI-WMI compatible Acer/ Wistron laptop, say Y or M
 	  here.
 
+config SILICOM_PLATFORM
+	tristate "Silicom Edge Networking device support"
+	depends on DMI
+	select LEDS_CLASS_MULTICOLOR
+	select GPIOLIB
+	help
+	  This option enables support for the LEDs/GPIO/etc downstream of the
+	  embedded controller on Silicom "Cordoba" hardware and derivatives.
+
+	  If you have a Silicom network appliance, say Y or M here.
+
 source "drivers/platform/x86/amd/Kconfig"
 
 config ADV_SWBUTTON
diff --git a/drivers/platform/x86/Makefile b/drivers/platform/x86/Makefile
index 2cafe51ec4d8..9355ebbc56ca 100644
--- a/drivers/platform/x86/Makefile
+++ b/drivers/platform/x86/Makefile
@@ -113,6 +113,7 @@ obj-$(CONFIG_SERIAL_MULTI_INSTANTIATE)	+= serial-multi-instantiate.o
 obj-$(CONFIG_MLX_PLATFORM)		+= mlx-platform.o
 obj-$(CONFIG_TOUCHSCREEN_DMI)		+= touchscreen_dmi.o
 obj-$(CONFIG_WIRELESS_HOTKEY)		+= wireless-hotkey.o
+obj-$(CONFIG_SILICOM_PLATFORM)		+= silicom-platform.o
 obj-$(CONFIG_X86_ANDROID_TABLETS)	+= x86-android-tablets/
 
 # Intel uncore drivers
diff --git a/drivers/platform/x86/silicom-platform.c b/drivers/platform/x86/silicom-platform.c
new file mode 100644
index 000000000000..ddf9961e2b9e
--- /dev/null
+++ b/drivers/platform/x86/silicom-platform.c
@@ -0,0 +1,1046 @@
+// SPDX-License-Identifier: GPL-2.0+
+//
+// silicom-platform.c - Silicom MEC170x platform driver
+//
+// Copyright (C) 2023 Henry Shi <henrys@silicom-usa.com>
+
+#include <linux/dmi.h>
+#include <linux/gpio/driver.h>
+#include <linux/init.h>
+#include <linux/ioport.h>
+#include <linux/io.h>
+#include <linux/kernel.h>
+#include <linux/led-class-multicolor.h>
+#include <linux/module.h>
+#include <linux/hwmon.h>
+#include <linux/mutex.h>
+#include <linux/platform_device.h>
+#include <linux/string.h>
+#include <linux/kobject.h>
+#include <linux/sysfs.h>
+#include <linux/bits.h>
+#include <linux/bitfield.h>
+
+#define MEC_ADDR ((mec_io_base) + 0x02)
+#define MEC_DATA(offset) ((mec_io_base) + 0x04 + (offset))
+#define EC_ADDR_LSB MEC_ADDR
+#define EC_ADDR_MSB ((mec_io_base) + 0x03)
+#define SILICOM_MEC_MAGIC 0x5a
+#define OFFSET_BIT_TO_CHANNEL(off, bit) ((((off) + 0x014) << 3) | (bit))
+#define CHANNEL_TO_OFFSET(chan) (((chan) >> 3) - 0x14)
+#define MEC_VERSION_MAJOR GENMASK(15, 14)
+#define MEC_VERSION_MINOR GENMASK(13, 8)
+#define IO_REG_BANK 0
+#define DEFAULT_CHAN_LO 0
+#define DEFAULT_CHAN_HI 0
+#define MEC_EFUSE_LSB_ADDR 0x28
+#define MEC_POWER_CYCLE_ADDR 0x24
+
+static DEFINE_MUTEX(mec_io_mutex);
+static int mec_io_base, mec_io_len;
+static struct device *my_dev;
+static int efuse_status;
+static int mec_uc_version;
+static int power_cycle;
+
+struct silicom_fan_control_data {
+	struct   device *hdev;
+	int      temp;
+	int      fan_speed;
+};
+
+static const struct hwmon_channel_info *silicom_fan_control_info[] = {
+	HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT | HWMON_F_LABEL),
+	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT | HWMON_T_LABEL),
+	NULL
+};
+
+struct silicom_platform_info {
+	int io_base;
+	int io_len;
+	struct led_classdev_mc *led_info;
+	struct gpio_chip *gpiochip;
+	u8 *gpio_channels;
+	u16 ngpio;
+};
+
+static const char * const plat_0222_gpio_names[] = {
+	"AUTOM0_SFP_TX_FAULT",
+	"SLOT2_LED_OUT",
+	"SIM_M2_SLOT2_B_DET",
+	"SIM_M2_SLOT2_A_DET",
+	"SLOT1_LED_OUT",
+	"SIM_M2_SLOT1_B_DET",
+	"SIM_M2_SLOT1_A_DET",
+	"SLOT0_LED_OUT",
+	"WAN_SFP0_RX_LOS",
+	"WAN_SFP0_PRSNT_N",
+	"WAN_SFP0_TX_FAULT",
+	"AUTOM1_SFP_RX_LOS",
+	"AUTOM1_SFP_PRSNT_N",
+	"AUTOM1_SFP_TX_FAULT",
+	"AUTOM0_SFP_RX_LOS",
+	"AUTOM0_SFP_PRSNT_N",
+	"WAN_SFP1_RX_LOS",
+	"WAN_SFP1_PRSNT_N",
+	"WAN_SFP1_TX_FAULT",
+	"SIM_M2_SLOT1_MUX_SEL",
+	"W_DISABLE_M2_SLOT1_N",
+	"W_DISABLE_MPCIE_SLOT0_N",
+	"W_DISABLE_M2_SLOT0_N",
+	"BT_COMMAND_MODE",
+	"WAN_SFP1_TX_DISABLE",
+	"WAN_SFP0_TX_DISABLE",
+	"AUTOM1_SFP_TX_DISABLE",
+	"AUTOM0_SFP_TX_DISABLE",
+	"SIM_M2_SLOT2_MUX_SEL",
+	"W_DISABLE_M2_SLOT2_N",
+	"RST_CTL_M2_SLOT_1_N",
+	"RST_CTL_M2_SLOT_2_N",
+	"PM_USB_PWR_EN_BOT",
+	"PM_USB_PWR_EN_TOP",
+};
+
+static u8 plat_0222_gpio_channels[] = {
+	OFFSET_BIT_TO_CHANNEL(0x00, 0),
+	OFFSET_BIT_TO_CHANNEL(0x00, 1),
+	OFFSET_BIT_TO_CHANNEL(0x00, 2),
+	OFFSET_BIT_TO_CHANNEL(0x00, 3),
+	OFFSET_BIT_TO_CHANNEL(0x00, 4),
+	OFFSET_BIT_TO_CHANNEL(0x00, 5),
+	OFFSET_BIT_TO_CHANNEL(0x00, 6),
+	OFFSET_BIT_TO_CHANNEL(0x00, 7),
+	OFFSET_BIT_TO_CHANNEL(0x01, 0),
+	OFFSET_BIT_TO_CHANNEL(0x01, 1),
+	OFFSET_BIT_TO_CHANNEL(0x01, 2),
+	OFFSET_BIT_TO_CHANNEL(0x01, 3),
+	OFFSET_BIT_TO_CHANNEL(0x01, 4),
+	OFFSET_BIT_TO_CHANNEL(0x01, 5),
+	OFFSET_BIT_TO_CHANNEL(0x01, 6),
+	OFFSET_BIT_TO_CHANNEL(0x01, 7),
+	OFFSET_BIT_TO_CHANNEL(0x02, 0),
+	OFFSET_BIT_TO_CHANNEL(0x02, 1),
+	OFFSET_BIT_TO_CHANNEL(0x02, 2),
+	OFFSET_BIT_TO_CHANNEL(0x09, 0),
+	OFFSET_BIT_TO_CHANNEL(0x09, 1),
+	OFFSET_BIT_TO_CHANNEL(0x09, 2),
+	OFFSET_BIT_TO_CHANNEL(0x09, 3),
+	OFFSET_BIT_TO_CHANNEL(0x0a, 0),
+	OFFSET_BIT_TO_CHANNEL(0x0a, 1),
+	OFFSET_BIT_TO_CHANNEL(0x0a, 2),
+	OFFSET_BIT_TO_CHANNEL(0x0a, 3),
+	OFFSET_BIT_TO_CHANNEL(0x0a, 4),
+	OFFSET_BIT_TO_CHANNEL(0x0a, 5),
+	OFFSET_BIT_TO_CHANNEL(0x0a, 6),
+	OFFSET_BIT_TO_CHANNEL(0x0b, 0),
+	OFFSET_BIT_TO_CHANNEL(0x0b, 1),
+	OFFSET_BIT_TO_CHANNEL(0x0b, 2),
+	OFFSET_BIT_TO_CHANNEL(0x0b, 3),
+};
+
+static struct platform_device *silicom_platform_dev;
+static struct led_classdev_mc *silicom_led_info __initdata;
+static struct gpio_chip *silicom_gpiochip __initdata;
+static u8 *silicom_gpio_channels __initdata;
+
+static int silicom_mec_port_get(unsigned int offset)
+{
+	u8 reg;
+
+	mutex_lock(&mec_io_mutex);
+	/* Get the dword offset from the channel */
+	outb((offset >> 3) & 0xfc, MEC_ADDR);
+
+	/* Get the current register */
+	reg = inb(MEC_DATA((offset >> 3) & 0x03));
+	mutex_unlock(&mec_io_mutex);
+
+	return (reg >> (offset & 0x7)) & 0x01;
+}
+
+static enum led_brightness silicom_mec_led_get(int channel)
+{
+	/* Outputs are active low */
+	return silicom_mec_port_get(channel) ? LED_OFF : LED_ON;
+}
+
+static void silicom_mec_port_set(int channel, int on)
+{
+	u8 reg;
+
+	mutex_lock(&mec_io_mutex);
+	/* Get the dword offset from the channel */
+	outb((channel >> 3) & 0xfc, MEC_ADDR);
+	/* Get the current port settings */
+	reg = inb(MEC_DATA((channel >> 3) & 0x03));
+	/* Outputs are active low, so clear the bit for on, or set it for off */
+	if (on)
+		reg &= ~(1 << (channel & 0x7));
+	else
+		reg |= 1 << (channel & 0x7);
+	/* Write back the updated register */
+	outb(reg, MEC_DATA((channel >> 3) & 0x03));
+	mutex_unlock(&mec_io_mutex);
+}
+
+static enum led_brightness silicom_mec_led_mc_brightness_get(struct led_classdev *led_cdev)
+{
+	struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(led_cdev);
+	enum led_brightness brightness = LED_OFF;
+	int i;
+
+	for (i = 0; i < mc_cdev->num_colors; i++) {
+		mc_cdev->subled_info[i].brightness =
+			silicom_mec_led_get(mc_cdev->subled_info[i].channel);
+		/* Mark the overall brightness as LED_ON if any of the subleds are on */
+		if (mc_cdev->subled_info[i].brightness != LED_OFF)
+			brightness = LED_ON;
+	}
+
+	return brightness;
+}
+
+static void silicom_mec_led_mc_brightness_set(struct led_classdev *led_cdev,
+											enum led_brightness brightness)
+{
+	struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(led_cdev);
+	int i;
+
+	led_mc_calc_color_components(mc_cdev, brightness);
+	for (i = 0; i < mc_cdev->num_colors; i++) {
+		silicom_mec_port_set(mc_cdev->subled_info[i].channel,
+		mc_cdev->subled_info[i].brightness);
+	}
+}
+
+static int silicom_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
+{
+	u8 *channels = gpiochip_get_data(gc);
+
+	/* Input registers have offsets between [0x00, 0x07] */
+	if (CHANNEL_TO_OFFSET(channels[offset]) < 0x08)
+		return GPIO_LINE_DIRECTION_IN;
+
+	return GPIO_LINE_DIRECTION_OUT;
+}
+
+static int silicom_gpio_direction_input(struct gpio_chip *gc, unsigned int offset)
+{
+	int direction = silicom_gpio_get_direction(gc, offset);
+
+	return direction == GPIO_LINE_DIRECTION_IN ? 0 : -EINVAL;
+}
+
+static void silicom_gpio_set(struct gpio_chip *gc, unsigned int offset, int value)
+{
+	u8 *channels = gpiochip_get_data(gc);
+	int direction = silicom_gpio_get_direction(gc, offset);
+	int channel = channels[offset];
+
+	if (direction == GPIO_LINE_DIRECTION_IN)
+		return;
+
+	if (value)
+		silicom_mec_port_set(channel, 0);
+	else if (value == 0)
+		silicom_mec_port_set(channel, 1);
+	else
+		pr_err("Wrong argument value: %d\n", value);
+}
+
+static int silicom_gpio_direction_output(struct gpio_chip *gc, unsigned int offset, int value)
+{
+	int direction = silicom_gpio_get_direction(gc, offset);
+
+	if (direction == GPIO_LINE_DIRECTION_IN)
+		return -EINVAL;
+
+	silicom_gpio_set(gc, offset, value);
+
+	return 0;
+}
+
+static int silicom_gpio_get(struct gpio_chip *gc, unsigned int offset)
+{
+	u8 *channels = gpiochip_get_data(gc);
+	int channel = channels[offset];
+
+	return silicom_mec_port_get(channel);
+}
+
+
+static struct mc_subled plat_0222_wan_mc_subled_info[] __initdata = {
+	{
+		.color_index = LED_COLOR_ID_WHITE,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 7),
+	},
+	{
+		.color_index = LED_COLOR_ID_YELLOW,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 6),
+	},
+	{
+		.color_index = LED_COLOR_ID_RED,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 5),
+	},
+};
+
+static struct mc_subled plat_0222_sys_mc_subled_info[] __initdata = {
+	{
+		.color_index = LED_COLOR_ID_WHITE,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 4),
+	},
+	{
+		.color_index = LED_COLOR_ID_AMBER,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 3),
+	},
+	{
+		.color_index = LED_COLOR_ID_RED,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 2),
+	},
+};
+
+static struct mc_subled plat_0222_stat1_mc_subled_info[] __initdata = {
+	{
+		.color_index = LED_COLOR_ID_RED,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 1),
+	},
+	{
+		.color_index = LED_COLOR_ID_GREEN,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 0),
+	},
+	{
+		.color_index = LED_COLOR_ID_BLUE,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 7),
+	},
+	{
+		.color_index = LED_COLOR_ID_YELLOW,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 6),
+	},
+};
+
+static struct mc_subled plat_0222_stat2_mc_subled_info[] __initdata = {
+	{
+		.color_index = LED_COLOR_ID_RED,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 5),
+	},
+	{
+		.color_index = LED_COLOR_ID_GREEN,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 4),
+	},
+	{
+		.color_index = LED_COLOR_ID_BLUE,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 3),
+	},
+	{
+		.color_index = LED_COLOR_ID_YELLOW,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 2),
+	},
+};
+
+static struct mc_subled plat_0222_stat3_mc_subled_info[] __initdata = {
+	{
+		.color_index = LED_COLOR_ID_RED,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 1),
+	},
+	{
+		.color_index = LED_COLOR_ID_GREEN,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 0),
+	},
+	{
+		.color_index = LED_COLOR_ID_BLUE,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0e, 1),
+	},
+	{
+		.color_index = LED_COLOR_ID_YELLOW,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0e, 0),
+	},
+};
+
+static struct led_classdev_mc plat_0222_mc_led_info[] __initdata = {
+	{
+		.led_cdev = {
+			.name = "multicolor:wan",
+			.brightness = 0,
+			.max_brightness = 1,
+			.brightness_set = silicom_mec_led_mc_brightness_set,
+			.brightness_get = silicom_mec_led_mc_brightness_get,
+		},
+		.num_colors = ARRAY_SIZE(plat_0222_wan_mc_subled_info),
+		.subled_info = plat_0222_wan_mc_subled_info,
+	},
+	{
+		.led_cdev = {
+			.name = "multicolor:sys",
+			.brightness = 0,
+			.max_brightness = 1,
+			.brightness_set = silicom_mec_led_mc_brightness_set,
+			.brightness_get = silicom_mec_led_mc_brightness_get,
+		},
+		.num_colors = ARRAY_SIZE(plat_0222_sys_mc_subled_info),
+		.subled_info = plat_0222_sys_mc_subled_info,
+	},
+	{
+		.led_cdev = {
+			.name = "multicolor:stat1",
+			.brightness = 0,
+			.max_brightness = 1,
+			.brightness_set = silicom_mec_led_mc_brightness_set,
+			.brightness_get = silicom_mec_led_mc_brightness_get,
+		},
+		.num_colors = ARRAY_SIZE(plat_0222_stat1_mc_subled_info),
+		.subled_info = plat_0222_stat1_mc_subled_info,
+	},
+	{
+		.led_cdev = {
+			.name = "multicolor:stat2",
+			.brightness = 0,
+			.max_brightness = 1,
+			.brightness_set = silicom_mec_led_mc_brightness_set,
+			.brightness_get = silicom_mec_led_mc_brightness_get,
+		},
+		.num_colors = ARRAY_SIZE(plat_0222_stat2_mc_subled_info),
+		.subled_info = plat_0222_stat2_mc_subled_info,
+	},
+	{
+		.led_cdev = {
+			.name = "multicolor:stat3",
+			.brightness = 0,
+			.max_brightness = 1,
+			.brightness_set = silicom_mec_led_mc_brightness_set,
+			.brightness_get = silicom_mec_led_mc_brightness_get,
+		},
+		.num_colors = ARRAY_SIZE(plat_0222_stat3_mc_subled_info),
+		.subled_info = plat_0222_stat3_mc_subled_info,
+	},
+	{ },
+};
+
+static struct gpio_chip silicom_gpio_chip = {
+	.label = "silicom-gpio",
+	.get_direction = silicom_gpio_get_direction,
+	.direction_input = silicom_gpio_direction_input,
+	.direction_output = silicom_gpio_direction_output,
+	.get = silicom_gpio_get,
+	.set = silicom_gpio_set,
+	.base = -1,
+	.ngpio = ARRAY_SIZE(plat_0222_gpio_channels),
+	.names = plat_0222_gpio_names,
+	/* We're using a mutex to protect the indirect access, so we can sleep if the
+	 * lock blocks
+	 */
+	.can_sleep = true,
+};
+
+static struct silicom_platform_info silicom_plat_0222_cordoba_info __initdata = {
+	.io_base = 0x800,
+	.io_len = 8,
+	.led_info = plat_0222_mc_led_info,
+	.gpiochip = &silicom_gpio_chip,
+	.gpio_channels = plat_0222_gpio_channels,
+	/* The original generic cordoba does not have the last 4 outputs of the plat_0222
+	 * BB variant, the rest are the same, so use the same longer list, but ignore the
+	 * last entries here
+	 */
+	.ngpio = ARRAY_SIZE(plat_0222_gpio_channels),
+
+};
+
+static struct mc_subled cordoba_fp_left_mc_subled_info[] __initdata = {
+	{
+		.color_index = LED_COLOR_ID_RED,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 6),
+	},
+	{
+		.color_index = LED_COLOR_ID_GREEN,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 5),
+	},
+	{
+		.color_index = LED_COLOR_ID_BLUE,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x09, 7),
+	},
+	{
+		.color_index = LED_COLOR_ID_AMBER,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x09, 4),
+	},
+};
+
+static struct mc_subled cordoba_fp_center_mc_subled_info[] __initdata = {
+	{
+		.color_index = LED_COLOR_ID_RED,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 7),
+	},
+	{
+		.color_index = LED_COLOR_ID_GREEN,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 4),
+	},
+	{
+		.color_index = LED_COLOR_ID_BLUE,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 3),
+	},
+	{
+		.color_index = LED_COLOR_ID_AMBER,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x09, 6),
+	},
+};
+
+static struct mc_subled cordoba_fp_right_mc_subled_info[] __initdata = {
+	{
+		.color_index = LED_COLOR_ID_RED,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 2),
+	},
+	{
+		.color_index = LED_COLOR_ID_GREEN,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 1),
+	},
+	{
+		.color_index = LED_COLOR_ID_BLUE,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 0),
+	},
+	{
+		.color_index = LED_COLOR_ID_AMBER,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x09, 5),
+	},
+};
+
+static struct led_classdev_mc cordoba_mc_led_info[] __initdata = {
+	{
+		.led_cdev = {
+			.name = "multicolor:fp_left",
+			.brightness = 0,
+			.max_brightness = 1,
+			.brightness_set = silicom_mec_led_mc_brightness_set,
+			.brightness_get = silicom_mec_led_mc_brightness_get,
+		},
+		.num_colors = ARRAY_SIZE(cordoba_fp_left_mc_subled_info),
+		.subled_info = cordoba_fp_left_mc_subled_info,
+	},
+	{
+		.led_cdev = {
+			.name = "multicolor:fp_center",
+			.brightness = 0,
+			.max_brightness = 1,
+			.brightness_set = silicom_mec_led_mc_brightness_set,
+			.brightness_get = silicom_mec_led_mc_brightness_get,
+		},
+		.num_colors = ARRAY_SIZE(cordoba_fp_center_mc_subled_info),
+		.subled_info = cordoba_fp_center_mc_subled_info,
+	},
+	{
+		.led_cdev = {
+			.name = "multicolor:fp_right",
+			.brightness = 0,
+			.max_brightness = 1,
+			.brightness_set = silicom_mec_led_mc_brightness_set,
+			.brightness_get = silicom_mec_led_mc_brightness_get,
+		},
+		.num_colors = ARRAY_SIZE(cordoba_fp_right_mc_subled_info),
+		.subled_info = cordoba_fp_right_mc_subled_info,
+	},
+	{ },
+};
+
+static struct silicom_platform_info silicom_generic_cordoba_info __initdata = {
+	.io_base = 0x800,
+	.io_len = 8,
+	.led_info = cordoba_mc_led_info,
+	.gpiochip = &silicom_gpio_chip,
+	.gpio_channels = plat_0222_gpio_channels,
+	.ngpio = ARRAY_SIZE(plat_0222_gpio_channels),
+};
+
+static struct platform_driver silicom_platform_driver = {
+	.driver = {
+		.name = "silicom-platform",
+	},
+};
+
+static ssize_t efuse_status_show(struct device *dev, struct device_attribute *attr,
+								char *buf)
+{
+	u32 reg;
+
+	mutex_lock(&mec_io_mutex);
+	/* Select memory region */
+	outb(IO_REG_BANK, EC_ADDR_MSB);
+	outb(MEC_EFUSE_LSB_ADDR, EC_ADDR_LSB);
+
+	/* Get current data from the address */
+	reg = inl(MEC_DATA(DEFAULT_CHAN_LO));
+	mutex_unlock(&mec_io_mutex);
+
+	efuse_status = reg & 0x1;
+
+	return sprintf(buf, "%d\n", efuse_status);
+}
+
+static ssize_t uc_version_show(struct device *dev,
+					struct device_attribute *attr,
+					char *buf)
+{
+	u32 reg;
+	int uc_version;
+
+	mutex_lock(&mec_io_mutex);
+	outb(IO_REG_BANK, EC_ADDR_MSB);
+	outb(DEFAULT_CHAN_LO, EC_ADDR_LSB);
+
+	reg = inl(MEC_DATA(DEFAULT_CHAN_LO));
+	mutex_unlock(&mec_io_mutex);
+	uc_version = FIELD_GET(GENMASK(15, 8), reg);
+	if (uc_version >= 192)
+		pr_err("uc version not supported\n");
+
+	uc_version = FIELD_GET(MEC_VERSION_MAJOR, reg) * 100 +
+				FIELD_GET(MEC_VERSION_MINOR, reg);
+
+	mec_uc_version = uc_version;
+	return sprintf(buf, "%d\n", mec_uc_version);
+}
+
+static ssize_t power_cycle_show(struct device *dev,
+				struct device_attribute *attr,
+				char *buf)
+{
+	return sprintf(buf, "%d\n", power_cycle);
+}
+
+static void powercycle_uc(void)
+{
+	mutex_lock(&mec_io_mutex);
+	/* Select memory region */
+	outb(IO_REG_BANK, EC_ADDR_MSB);
+	outb(MEC_POWER_CYCLE_ADDR, EC_ADDR_LSB);
+
+	/* Set to 1 for current data from the address */
+	outb(1, MEC_DATA(DEFAULT_CHAN_LO));
+	mutex_unlock(&mec_io_mutex);
+}
+
+static ssize_t power_cycle_store(struct device *dev, struct device_attribute *attr,
+				 const char *buf, size_t count)
+{
+	if (sscanf(buf, "%du", &power_cycle) != 1) {
+		dev_err(dev, "Failed to read power_cycle\n");
+		return -EINVAL;
+	}
+	if (power_cycle > 0)
+		powercycle_uc();
+
+	return count;
+}
+
+static struct device_attribute my_dev_attr[] = {
+	{
+		.attr = {.name = "efuse_status", .mode = 0644},
+		.show = efuse_status_show,
+		.store = NULL
+	},
+	{
+		.attr = {.name = "uc_version", .mode = 0644},
+		.show = uc_version_show,
+		.store = NULL
+	},
+	{
+		.attr = {.name = "power_cycle", .mode = 0644},
+		.show = power_cycle_show,
+		.store = power_cycle_store
+	},
+};
+
+static int __init silicom_mc_leds_register(struct device *dev,
+					   const struct led_classdev_mc *mc_leds)
+{
+	struct led_classdev_mc *led;
+	int i, err;
+
+	for (i = 0; mc_leds[i].led_cdev.name; i++) {
+		led = devm_kzalloc(dev, sizeof(struct led_classdev_mc), GFP_KERNEL);
+		if (!led)
+			return -ENOMEM;
+		memcpy(led, &mc_leds[i], sizeof(*led));
+
+		led->subled_info = devm_kzalloc(dev, led->num_colors * sizeof(struct mc_subled), GFP_KERNEL);
+		if (!led->subled_info)
+			return -ENOMEM;
+		memcpy(led->subled_info, mc_leds[i].subled_info, led->num_colors * sizeof(struct mc_subled));
+
+		err = devm_led_classdev_multicolor_register(dev, led);
+		if (err) {
+			dev_err(dev, "Failed to register[%d]: %d\n", i, err);
+			return err;
+		}
+	}
+
+	return 0;
+}
+
+static u32 rpm_get(void)
+{
+	u32 reg;
+
+	mutex_lock(&mec_io_mutex);
+	/* Select memory region */
+	outb(IO_REG_BANK, EC_ADDR_MSB);
+	outb(0xc, EC_ADDR_LSB);
+	/* Get current data from the address */
+	reg = inw(MEC_DATA(DEFAULT_CHAN_LO));
+	mutex_unlock(&mec_io_mutex);
+
+	return reg;
+}
+
+static u32 temp_get(void)
+{
+	u32 reg;
+
+	mutex_lock(&mec_io_mutex);
+	/* Select memory region */
+	outb(IO_REG_BANK, EC_ADDR_MSB);
+	outb(0xc, EC_ADDR_LSB);
+	/* Get current data from the address */
+	reg = inl(MEC_DATA(DEFAULT_CHAN_LO));
+	mutex_unlock(&mec_io_mutex);
+
+	return (reg >> 16) / 10;
+}
+
+static umode_t silicom_fan_control_fan_is_visible(const u32 attr)
+{
+	switch (attr) {
+	case hwmon_fan_input:
+	case hwmon_fan_label:
+		return 0444;
+	default:
+		return 0;
+	}
+}
+
+static umode_t silicom_fan_control_temp_is_visible(const u32 attr)
+{
+	switch (attr) {
+	case hwmon_temp_input:
+	case hwmon_temp_label:
+		return 0444;
+	default:
+		return 0;
+	}
+}
+
+static int silicom_fan_control_read_fan(struct device *dev, u32 attr, long *val)
+{
+	struct silicom_fan_control_data *ctl = dev_get_drvdata(dev);
+
+	switch (attr) {
+	case hwmon_fan_input:
+		ctl->fan_speed = rpm_get();
+		*val = ctl->fan_speed;
+		return 0;
+	default:
+		return -EOPNOTSUPP;
+	}
+}
+
+static int silicom_fan_control_read_temp(struct device *dev, u32 attr, long *val)
+{
+	switch (attr) {
+	case hwmon_temp_input:
+		*val = temp_get();
+		return 0;
+	default:
+		return -EOPNOTSUPP;
+	}
+}
+
+static umode_t silicom_fan_control_is_visible(const void *data,
+					  enum hwmon_sensor_types type,
+					  u32 attr, int channel)
+{
+	switch (type) {
+	case hwmon_fan:
+		return silicom_fan_control_fan_is_visible(attr);
+	case hwmon_temp:
+		return silicom_fan_control_temp_is_visible(attr);
+	default:
+		return 0;
+	}
+}
+
+static int silicom_fan_control_read(struct device *dev, enum hwmon_sensor_types type,
+				    u32 attr, int channel, long *val)
+{
+	switch (type) {
+	case hwmon_fan:
+		return silicom_fan_control_read_fan(dev, attr, val);
+	case hwmon_temp:
+		return silicom_fan_control_read_temp(dev, attr, val);
+	default:
+		return -EOPNOTSUPP;
+	}
+}
+
+static int silicom_fan_control_read_labels(struct device *dev, enum hwmon_sensor_types type,
+					   u32 attr, int channel, const char **str)
+{
+	switch (type) {
+	case hwmon_fan:
+		*str = "Fan Speed";
+		return 0;
+	case hwmon_temp:
+		*str = "Thermostat Sensor";
+		return 0;
+	default:
+		return -EOPNOTSUPP;
+	}
+}
+
+static const struct hwmon_ops silicom_fan_control_hwmon_ops = {
+	.is_visible = silicom_fan_control_is_visible,
+	.read = silicom_fan_control_read,
+	.write = NULL,
+	.read_string = silicom_fan_control_read_labels,
+};
+
+static const struct hwmon_chip_info silicom_chip_info = {
+	.ops = &silicom_fan_control_hwmon_ops,
+	.info = silicom_fan_control_info,
+};
+
+static int __init silicom_platform_probe(struct platform_device *device)
+{
+	int i, err;
+	u8 magic, ver;
+	struct silicom_fan_control_data *ctl;
+	const char *name = "Silocom_Fan_Monitor";
+	const char *dev_name = "Silicom_platform";
+
+	mec_io_base = 0x0800;
+	mec_io_len = 8;
+	if (!devm_request_region(&device->dev, mec_io_base, mec_io_len, "mec")) {
+		dev_err(&device->dev, "couldn't reserve MEC io ports\n");
+		return -EBUSY;
+	}
+
+	/* Sanity check magic number read for EC */
+	outb(0x00, MEC_ADDR);
+	magic = inb(MEC_DATA(DEFAULT_CHAN_LO));
+	ver = inb(MEC_DATA(DEFAULT_CHAN_HI));
+	dev_dbg(&device->dev, "EC magic 0x%02x, version 0x%02x\n", magic, ver);
+
+	if (magic != SILICOM_MEC_MAGIC) {
+		dev_err(&device->dev, "Bad EC magic 0x%02x!\n", magic);
+		return -ENODEV;
+	}
+
+	err = silicom_mc_leds_register(&device->dev, silicom_led_info);
+	if (err) {
+		dev_err(&device->dev, "Failed to register LEDs\n");
+		return err;
+	}
+
+	err = devm_gpiochip_add_data(&device->dev, silicom_gpiochip, silicom_gpio_channels);
+	if (err) {
+		dev_err(&device->dev, "Failed to register gpiochip: %d\n", err);
+		return err;
+	}
+
+	ctl = devm_kzalloc(&device->dev, sizeof(*ctl), GFP_KERNEL);
+	if (!ctl)
+		return -ENOMEM;
+
+	ctl->hdev = devm_hwmon_device_register_with_info(&device->dev, name, ctl,
+				&silicom_chip_info, NULL);
+
+	my_dev = root_device_register(dev_name);
+	for (i = 0; i < ARRAY_SIZE(my_dev_attr); i++) {
+		err = sysfs_create_file(&my_dev->kobj, &my_dev_attr[i].attr);
+		if (err) {
+			pr_debug("failed to create the foo file in /sys/devices/Silicom_platform\n");
+			break;
+		}
+	}
+
+	return err;
+}
+
+static int __init silicom_platform_info_init(const struct dmi_system_id *id)
+{
+	struct silicom_platform_info *info = id->driver_data;
+
+	dev_info(&silicom_platform_dev->dev, "Detected %s\n", id->ident);
+
+	mec_io_base = info->io_base;
+	mec_io_len = info->io_len;
+	silicom_led_info = info->led_info;
+	silicom_gpio_channels = info->gpio_channels;
+	silicom_gpiochip = info->gpiochip;
+	if (silicom_gpiochip)
+		silicom_gpiochip->ngpio = info->ngpio;
+
+	return 1;
+}
+
+static const struct dmi_system_id silicom_dmi_ids[] __initconst = {
+	{
+		.callback = silicom_platform_info_init,
+		.ident = "Silicom Cordoba (Generic)",
+		.matches = {
+			DMI_MATCH(DMI_BOARD_VENDOR, "Silicom"),
+			DMI_MATCH(DMI_BOARD_NAME, "80300-0214-G"),
+		},
+		.driver_data = &silicom_generic_cordoba_info,
+	},
+	{
+		.callback = silicom_platform_info_init,
+		.ident = "Silicom Cordoba (Generic)",
+		.matches = {
+			DMI_MATCH(DMI_BOARD_VENDOR, "Silicom"),
+			DMI_MATCH(DMI_BOARD_NAME, "80500-0214-G"),
+		},
+		.driver_data = &silicom_generic_cordoba_info,
+	},
+	{
+		 .callback = silicom_platform_info_init,
+		 .ident = "Silicom Cordoba (plat_0222)",
+		 .matches = {
+			DMI_MATCH(DMI_BOARD_VENDOR, "Silicom"),
+			DMI_MATCH(DMI_BOARD_NAME, "80300-0222-G"),
+		 },
+		.driver_data = &silicom_plat_0222_cordoba_info,
+	},
+	{ },
+};
+
+static int __init silicom_platform_init(void)
+{
+	struct device *dev;
+	int err;
+
+	/* register a platform device to act as the parent for LEDS, etc. */
+	silicom_platform_dev = platform_device_register_simple("silicom-platform", -1, NULL, 0);
+	if (IS_ERR(silicom_platform_dev)) {
+		err = PTR_ERR(silicom_platform_dev);
+		pr_err("failed to register silicom-platform device: %d\n", err);
+		goto silicom_init_register_err;
+	}
+	dev = &silicom_platform_dev->dev;
+
+	err = dmi_check_system(silicom_dmi_ids);
+	if (err == 0) {
+		dev_err(dev, "No DMI match for this platform\n");
+		err = -ENODEV;
+		goto silicom_init_probe_err;
+	}
+
+	/* Directly probe the platform driver in init since this isn't a
+	 * hotpluggable device.  That means we don't need to register a driver
+	 * that needs to wait around in memory on the chance a matching device
+	 * would get added.  Instead run once in __init so that we can free all
+	 * those resources when the __init region is wiped
+	 */
+	err = platform_driver_probe(&silicom_platform_driver, silicom_platform_probe);
+	if (err) {
+		dev_err(dev, "Failed to probe platform driver %d\n", err);
+		goto silicom_init_probe_err;
+	}
+
+	return 0;
+
+silicom_init_probe_err:
+	if (silicom_platform_dev) {
+		platform_device_unregister(silicom_platform_dev);
+		silicom_platform_dev = NULL;
+	}
+	if (my_dev) {
+		root_device_unregister(my_dev);
+		my_dev = NULL;
+	}
+
+silicom_init_register_err:
+	return err;
+}
+
+static void __exit silicom_platform_exit(void)
+{
+	int i;
+
+	if (silicom_platform_dev) {
+		platform_device_unregister(silicom_platform_dev);
+		platform_driver_unregister(&silicom_platform_driver);
+	}
+
+	if (my_dev) {
+		for (i = 0; i < ARRAY_SIZE(my_dev_attr); i++)
+			sysfs_remove_file(&my_dev->kobj, &my_dev_attr[i].attr);
+		root_device_unregister(my_dev);
+	}
+	mutex_destroy(&mec_io_mutex);
+}
+
+module_init(silicom_platform_init);
+module_exit(silicom_platform_exit);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Henry Shi <henrys@silicom-usa.com>");
+MODULE_DESCRIPTION("Platform driver for Silicom network appliances");
+
+MODULE_DEVICE_TABLE(dmi, silicom_dmi_ids);
-- 
2.21.3


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

* RE: [PATCH] Add Silicom Platform Driver
  2023-08-17  8:06 ` Ilpo Järvinen
@ 2023-08-18 14:31   ` Huibin Shi
  0 siblings, 0 replies; 32+ messages in thread
From: Huibin Shi @ 2023-08-18 14:31 UTC (permalink / raw)
  To: Ilpo Järvinen, Henry Shi
  Cc: hbshi69, tglx, mingo, bp, dave.hansen, x86, hpa, hdegoede,
	markgross, jdelvare, linux, LKML, platform-driver-x86,
	linux-hwmon, hb_shi2003, Wen Wang

Ilpo,

See my comments below.

Thanks.
Henry

-----Original Message-----
From: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com> 
Sent: Thursday, August 17, 2023 4:07 AM
To: Henry Shi <henryshi2018@gmail.com>
Cc: hbshi69@hotmail.com; tglx@linutronix.de; mingo@redhat.com; bp@alien8.de; dave.hansen@linux.intel.com; x86@kernel.org; hpa@zytor.com; hdegoede@redhat.com; markgross@kernel.org; jdelvare@suse.com; linux@roeck-us.net; LKML <linux-kernel@vger.kernel.org>; platform-driver-x86@vger.kernel.org; linux-hwmon@vger.kernel.org; hb_shi2003@yahoo.com; Huibin Shi <henrys@silicom-usa.com>; Wen Wang <wenw@silicom-usa.com>
Subject: Re: [PATCH] Add Silicom Platform Driver

Caution: This is an external email. Please take care when clicking links or opening attachments.


On Tue, 15 Aug 2023, Henry Shi wrote:

> The Silicom platform (silicom-platform) Linux driver for Swisscom 
> Business Box (Swisscom BB) as well as Cordoba family products is a 
> software solution designed to facilitate the efficient management and 
> control of devices through the integration of various Linux 
> frameworks. This platform driver provides seamless support for device 
> management via the Linux LED framework, GPIO framework, Hardware 
> Monitoring (HWMON), and device attributes. The Silicom platform 
> driver's compatibility with these Linux frameworks allows applications 
> to access and control Cordoba family devices using existing software 
> that is compatible with these frameworks. This compatibility 
> simplifies the development process, reduces dependencies on 
> proprietary solutions, and promotes interoperability with other 
> Linux-based systems and software.
>
> Signed-off-by: Henry Shi <henryshi2018@gmail.com>
> ---

You should use version the submission (vXX should appear already in the
subject) and provide the version history in a list (listing version to version changes).

Henry: OK.

>  drivers/platform/x86/Kconfig            |   11 +
>  drivers/platform/x86/Makefile           |    1 +
>  drivers/platform/x86/silicom-platform.c | 1053 
> +++++++++++++++++++++++
>  3 files changed, 1065 insertions(+)
>  create mode 100644 drivers/platform/x86/silicom-platform.c
>
> diff --git a/drivers/platform/x86/Kconfig 
> b/drivers/platform/x86/Kconfig index 22052031c719..7680c0dbcd8d 100644
> --- a/drivers/platform/x86/Kconfig
> +++ b/drivers/platform/x86/Kconfig
> @@ -188,6 +188,17 @@ config ACER_WMI
>         If you have an ACPI-WMI compatible Acer/ Wistron laptop, say Y or M
>         here.
>
> +config SILICOM_PLATFORM
> +     tristate "Silicom Edge Networking device support"
> +     depends on DMI
> +     select LEDS_CLASS_MULTICOLOR
> +     select GPIOLIB
> +     help
> +       This option enables support for the LEDs/GPIO/etc downstream of the
> +       embedded controller on Silicom "Cordoba" hardware and derivatives.
> +
> +       If you have a Silicom network appliance, say Y or M here.
> +
>  source "drivers/platform/x86/amd/Kconfig"
>
>  config ADV_SWBUTTON
> diff --git a/drivers/platform/x86/Makefile 
> b/drivers/platform/x86/Makefile index 2cafe51ec4d8..9355ebbc56ca 
> 100644
> --- a/drivers/platform/x86/Makefile
> +++ b/drivers/platform/x86/Makefile
> @@ -113,6 +113,7 @@ obj-$(CONFIG_SERIAL_MULTI_INSTANTIATE)    += serial-multi-instantiate.o
>  obj-$(CONFIG_MLX_PLATFORM)           += mlx-platform.o
>  obj-$(CONFIG_TOUCHSCREEN_DMI)                += touchscreen_dmi.o
>  obj-$(CONFIG_WIRELESS_HOTKEY)                += wireless-hotkey.o
> +obj-$(CONFIG_SILICOM_PLATFORM)               += silicom-platform.o
>  obj-$(CONFIG_X86_ANDROID_TABLETS)    += x86-android-tablets/
>
>  # Intel uncore drivers
> diff --git a/drivers/platform/x86/silicom-platform.c 
> b/drivers/platform/x86/silicom-platform.c
> new file mode 100644
> index 000000000000..f8d1eb68b105
> --- /dev/null
> +++ b/drivers/platform/x86/silicom-platform.c
> @@ -0,0 +1,1053 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +//
> +// silicom-platform.c - Silicom MEC170x platform driver // // 
> +Copyright (C) 2023 Henry Shi <henrys@silicom-usa.com>
> +
> +#include <linux/dmi.h>
> +#include <linux/gpio/driver.h>
> +#include <linux/init.h>
> +#include <linux/ioport.h>
> +#include <linux/io.h>
> +#include <linux/kernel.h>
> +#include <linux/led-class-multicolor.h> #include <linux/module.h> 
> +#include <linux/hwmon.h> #include <linux/mutex.h> #include 
> +<linux/platform_device.h> #include <linux/string.h> #include 
> +<linux/kobject.h> #include <linux/sysfs.h> #include <linux/bits.h> 
> +#include <linux/bitfield.h>
> +
> +#define MEC_ADDR ((mec_io_base) + 0x02) #define MEC_DATA(offset) 
> +((mec_io_base) + 0x04 + (offset)) #define EC_ADDR_LSB MEC_ADDR 
> +#define EC_ADDR_MSB ((mec_io_base) + 0x03) #define SILICOM_MEC_MAGIC 
> +0x5a #define OFFSET_BIT_TO_CHANNEL(off, bit) ((((off) + 0x014) << 3) 
> +| (bit)) #define CHANNEL_TO_OFFSET(chan) (((chan) >> 3) - 0x14) 
> +#define IO_REG_BANK 0 #define DEFAULT_CHAN_LO 0 #define 
> +DEFAULT_CHAN_HI 0
> +
> +static DEFINE_MUTEX(mec_io_mutex);
> +static int mec_io_base, mec_io_len;
> +static struct device *my_dev;
> +static int efuse_status;
> +static int mec_uc_version;
> +static int power_cycle;
> +
> +struct silicom_fan_control_data {
> +     struct   device *hdev;
> +     int      temp;
> +     int      fan_speed;
> +};
> +
> +static const struct hwmon_channel_info *silicom_fan_control_info[] = {
> +     HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT | HWMON_F_LABEL),
> +     HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT | HWMON_T_LABEL),
> +     NULL
> +};
> +
> +struct silicom_platform_info {
> +     int io_base;
> +     int io_len;
> +     struct led_classdev_mc *led_info;
> +     struct gpio_chip *gpiochip;
> +     u8 *gpio_channels;
> +     u16 ngpio;
> +};
> +
> +static const char * const plat_0222_gpio_names[] = {
> +     "AUTOM0_SFP_TX_FAULT",
> +     "SLOT2_LED_OUT",
> +     "SIM_M2_SLOT2_B_DET",
> +     "SIM_M2_SLOT2_A_DET",
> +     "SLOT1_LED_OUT",
> +     "SIM_M2_SLOT1_B_DET",
> +     "SIM_M2_SLOT1_A_DET",
> +     "SLOT0_LED_OUT",
> +     "WAN_SFP0_RX_LOS",
> +     "WAN_SFP0_PRSNT_N",
> +     "WAN_SFP0_TX_FAULT",
> +     "AUTOM1_SFP_RX_LOS",
> +     "AUTOM1_SFP_PRSNT_N",
> +     "AUTOM1_SFP_TX_FAULT",
> +     "AUTOM0_SFP_RX_LOS",
> +     "AUTOM0_SFP_PRSNT_N",
> +     "WAN_SFP1_RX_LOS",
> +     "WAN_SFP1_PRSNT_N",
> +     "WAN_SFP1_TX_FAULT",
> +     "SIM_M2_SLOT1_MUX_SEL",
> +     "W_DISABLE_M2_SLOT1_N",
> +     "W_DISABLE_MPCIE_SLOT0_N",
> +     "W_DISABLE_M2_SLOT0_N",
> +     "BT_COMMAND_MODE",
> +     "WAN_SFP1_TX_DISABLE",
> +     "WAN_SFP0_TX_DISABLE",
> +     "AUTOM1_SFP_TX_DISABLE",
> +     "AUTOM0_SFP_TX_DISABLE",
> +     "SIM_M2_SLOT2_MUX_SEL",
> +     "W_DISABLE_M2_SLOT2_N",
> +     "RST_CTL_M2_SLOT_1_N",
> +     "RST_CTL_M2_SLOT_2_N",
> +     "PM_USB_PWR_EN_BOT",
> +     "PM_USB_PWR_EN_TOP",
> +};
> +
> +static u8 plat_0222_gpio_channels[] = {
> +     OFFSET_BIT_TO_CHANNEL(0x00, 0),
> +     OFFSET_BIT_TO_CHANNEL(0x00, 1),
> +     OFFSET_BIT_TO_CHANNEL(0x00, 2),
> +     OFFSET_BIT_TO_CHANNEL(0x00, 3),
> +     OFFSET_BIT_TO_CHANNEL(0x00, 4),
> +     OFFSET_BIT_TO_CHANNEL(0x00, 5),
> +     OFFSET_BIT_TO_CHANNEL(0x00, 6),
> +     OFFSET_BIT_TO_CHANNEL(0x00, 7),
> +     OFFSET_BIT_TO_CHANNEL(0x01, 0),
> +     OFFSET_BIT_TO_CHANNEL(0x01, 1),
> +     OFFSET_BIT_TO_CHANNEL(0x01, 2),
> +     OFFSET_BIT_TO_CHANNEL(0x01, 3),
> +     OFFSET_BIT_TO_CHANNEL(0x01, 4),
> +     OFFSET_BIT_TO_CHANNEL(0x01, 5),
> +     OFFSET_BIT_TO_CHANNEL(0x01, 6),
> +     OFFSET_BIT_TO_CHANNEL(0x01, 7),
> +     OFFSET_BIT_TO_CHANNEL(0x02, 0),
> +     OFFSET_BIT_TO_CHANNEL(0x02, 1),
> +     OFFSET_BIT_TO_CHANNEL(0x02, 2),
> +     OFFSET_BIT_TO_CHANNEL(0x09, 0),
> +     OFFSET_BIT_TO_CHANNEL(0x09, 1),
> +     OFFSET_BIT_TO_CHANNEL(0x09, 2),
> +     OFFSET_BIT_TO_CHANNEL(0x09, 3),
> +     OFFSET_BIT_TO_CHANNEL(0x0a, 0),
> +     OFFSET_BIT_TO_CHANNEL(0x0a, 1),
> +     OFFSET_BIT_TO_CHANNEL(0x0a, 2),
> +     OFFSET_BIT_TO_CHANNEL(0x0a, 3),
> +     OFFSET_BIT_TO_CHANNEL(0x0a, 4),
> +     OFFSET_BIT_TO_CHANNEL(0x0a, 5),
> +     OFFSET_BIT_TO_CHANNEL(0x0a, 6),
> +     OFFSET_BIT_TO_CHANNEL(0x0b, 0),
> +     OFFSET_BIT_TO_CHANNEL(0x0b, 1),
> +     OFFSET_BIT_TO_CHANNEL(0x0b, 2),
> +     OFFSET_BIT_TO_CHANNEL(0x0b, 3),
> +};
> +
> +static struct platform_device *silicom_platform_dev; static struct 
> +led_classdev_mc *silicom_led_info __initdata; static struct gpio_chip 
> +*silicom_gpiochip __initdata; static u8 *silicom_gpio_channels 
> +__initdata;
> +
> +static int silicom_mec_port_get(unsigned int offset) {
> +     u8 reg;
> +
> +     mutex_lock(&mec_io_mutex);
> +     /* Get the dword offset from the channel */
> +     outb((offset >> 3) & 0xfc, MEC_ADDR);
> +
> +     /* Get the current register */
> +     reg = inb(MEC_DATA((offset >> 3) & 0x03));
> +     mutex_unlock(&mec_io_mutex);
> +
> +     return (reg >> (offset & 0x7)) & 0x01; }
> +
> +static enum led_brightness silicom_mec_led_get(int channel) {
> +     u8 reg;
> +
> +     mutex_lock(&mec_io_mutex);
> +     /* Get the dword offset of the register for this LED from the channel */
> +     outb((channel >> 3) & 0xfc, MEC_ADDR);
> +     /* Get the current LED settings */
> +     reg = inb(MEC_DATA((channel >> 3) & 0x03));
> +     mutex_unlock(&mec_io_mutex);
> +
> +     /* Outputs are active low */
> +     return silicom_mec_port_get(channel) ? LED_OFF : LED_ON;

Why is code now done twice, first in this function and then it calls
silicom_mec_port_get() which does the same thing?? Perhaps you forgot to remove it from this function while you added the call.

Henry: Yes, those lines should be removed. 

> +}
> +
> +static void silicom_mec_port_set(int channel, int on) {
> +     u8 reg;
> +
> +     mutex_lock(&mec_io_mutex);
> +     /* Get the dword offset from the channel */
> +     outb((channel >> 3) & 0xfc, MEC_ADDR);
> +     /* Get the current port settings */
> +     reg = inb(MEC_DATA((channel >> 3) & 0x03));
> +     /* Outputs are active low, so clear the bit for on, or set it for off */
> +     if (on)
> +             reg &= ~(1 << (channel & 0x7));
> +     else
> +             reg |= 1 << (channel & 0x7);
> +     /* Write back the updated register */
> +     outb(reg, MEC_DATA((channel >> 3) & 0x03));
> +     mutex_unlock(&mec_io_mutex);
> +}
> +
> +static enum led_brightness silicom_mec_led_mc_brightness_get(struct 
> +led_classdev *led_cdev) {
> +     struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(led_cdev);
> +     enum led_brightness brightness = LED_OFF;
> +     int i;
> +
> +     for (i = 0; i < mc_cdev->num_colors; i++) {
> +             mc_cdev->subled_info[i].brightness =
> +                     silicom_mec_led_get(mc_cdev->subled_info[i].channel);
> +             /* Mark the overall brightness as LED_ON if any of the subleds are on */
> +             if (mc_cdev->subled_info[i].brightness != LED_OFF)
> +                     brightness = LED_ON;
> +     }
> +
> +     return brightness;
> +}
> +
> +static void silicom_mec_led_mc_brightness_set(struct led_classdev *led_cdev,
> +                                                                                     
> +enum led_brightness brightness) {
> +     struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(led_cdev);
> +     int i;
> +
> +     led_mc_calc_color_components(mc_cdev, brightness);
> +     for (i = 0; i < mc_cdev->num_colors; i++) {
> +             silicom_mec_port_set(mc_cdev->subled_info[i].channel,
> +                                     
> + mc_cdev->subled_info[i].brightness);

Align the arguments to the same column please.

Henry: OK

> +     }
> +}
> +
> +static int silicom_gpio_get_direction(struct gpio_chip *gc, unsigned 
> +int offset) {
> +     u8 *channels = gpiochip_get_data(gc);
> +
> +     /* Input registers have offsets between [0x00, 0x07] */
> +     if (CHANNEL_TO_OFFSET(channels[offset]) < 0x08)
> +             return GPIO_LINE_DIRECTION_IN;
> +
> +     return GPIO_LINE_DIRECTION_OUT;
> +}
> +
> +static int silicom_gpio_direction_input(struct gpio_chip *gc, 
> +unsigned int offset) {
> +     int direction = silicom_gpio_get_direction(gc, offset);
> +
> +     return direction == GPIO_LINE_DIRECTION_IN ? 0 : -EINVAL; }
> +
> +static void silicom_gpio_set(struct gpio_chip *gc, unsigned int 
> +offset, int value) {
> +     u8 *channels = gpiochip_get_data(gc);
> +     int direction = silicom_gpio_get_direction(gc, offset);
> +     int channel = channels[offset];
> +
> +     if (direction == GPIO_LINE_DIRECTION_IN)
> +             return;
> +
> +     if (value)
> +             silicom_mec_port_set(channel, 0);
> +     else if (value == 0)
> +             silicom_mec_port_set(channel, 1);
> +     else
> +             pr_err("Wrong argument value: %d\n", value); }
> +
> +static int silicom_gpio_direction_output(struct gpio_chip *gc, 
> +unsigned int offset, int value) {
> +     int direction = silicom_gpio_get_direction(gc, offset);
> +
> +     if (direction == GPIO_LINE_DIRECTION_IN)
> +             return -EINVAL;
> +
> +     silicom_gpio_set(gc, offset, value);
> +
> +     return 0;
> +}
> +
> +static int silicom_gpio_get(struct gpio_chip *gc, unsigned int 
> +offset) {
> +     u8 *channels = gpiochip_get_data(gc);
> +     int channel = channels[offset];
> +
> +     return silicom_mec_port_get(channel); }
> +
> +


> +static ssize_t efuse_status_show(struct device *dev, struct device_attribute *attr,
> +                                                             char 
> +*buf) {
> +     u32 reg;
> +
> +     mutex_lock(&mec_io_mutex);
> +     /* Select memory region */
> +     outb(IO_REG_BANK, EC_ADDR_MSB);
> +     outb(0x28, EC_ADDR_LSB);

That 0x28 is some HW offset right? It should be named to what is found at that address with a define. Fiven the function name, perhaps something
along the lines of #define MEC_EFUSE_STATUS     0x28

Henry: OK

> +
> +     /* Get current data from the address */
> +     reg = inl(MEC_DATA(DEFAULT_CHAN_LO));
> +     mutex_unlock(&mec_io_mutex);
> +
> +     efuse_status = reg & 0x1;
> +
> +     return sprintf(buf, "%d\n", efuse_status); }
> +
> +static ssize_t uc_version_show(struct device *dev,
> +                                     struct device_attribute *attr,
> +                                     char *buf) {
> +     u32 reg;
> +     int uc_version;
> +
> +     mutex_lock(&mec_io_mutex);
> +     outb(IO_REG_BANK, EC_ADDR_MSB);
> +     outb(0x0, EC_ADDR_LSB);

Named define for 0x0.

Henry: OK.

> +
> +     reg = inl(MEC_DATA(DEFAULT_CHAN_LO));
> +     mutex_unlock(&mec_io_mutex);
> +     uc_version = FIELD_GET(GENMASK(15, 8), reg);

In general, it's more useful to have #define with name for GENMASK() like this, but see below...

> +     if (uc_version >= 64 && uc_version < 128) {
> +             uc_version &= ~(1 << 6);
> +             uc_version = 100 + uc_version;
> +     } else if (uc_version >= 128 && uc_version < 192) {
> +             uc_version &= ~(1 << 7);
> +             uc_version = 200 + uc_version;
> +     }

I see you probably missed what I tried to say earlier. Does this version field have two distinct fields? How about this:

#define MEC_VERSION_MAJOR       GENMASK(15, 14)
#define MEC_VERSION_MINOR       GENMASK(13, 8)

        uc_version = FIELD_GET(MEC_VERSION_MAJOR, reg) * 100 +
                     FIELD_GET(MEC_VERSION_MINOR, reg);

...you might want to add something for >= 192 values (or accept they'll be in 300..3xx range if that's okay, I don't know the internals of this fields so I cannot tell which is preferred here).

I think the results are identical to what the above code does but doesn't require any if()s (sans >= 192 that might need additional check).

Henry: Good suggestion. 

> +     mec_uc_version = uc_version;
> +     return sprintf(buf, "%d\n", mec_uc_version); }
> +
> +static ssize_t power_cycle_show(struct device *dev,
> +                             struct device_attribute *attr,
> +                             char *buf) {
> +     return sprintf(buf, "%d\n", power_cycle); }
> +
> +static void powercycle_uc(void)
> +{
> +     mutex_lock(&mec_io_mutex);
> +     /* Select memory region */
> +     outb(IO_REG_BANK, EC_ADDR_MSB);
> +     outb(0x24, EC_ADDR_LSB);

Named define for 0x24.

Henry: OK

--
 i.



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

* Re: [PATCH] Add Silicom Platform Driver
  2023-08-15 13:37 Henry Shi
@ 2023-08-17  8:06 ` Ilpo Järvinen
  2023-08-18 14:31   ` Huibin Shi
  0 siblings, 1 reply; 32+ messages in thread
From: Ilpo Järvinen @ 2023-08-17  8:06 UTC (permalink / raw)
  To: Henry Shi
  Cc: hbshi69, tglx, mingo, bp, dave.hansen, x86, hpa, hdegoede,
	markgross, jdelvare, linux, LKML, platform-driver-x86,
	linux-hwmon, hb_shi2003, henrys, wenw

On Tue, 15 Aug 2023, Henry Shi wrote:

> The Silicom platform (silicom-platform) Linux driver for Swisscom
> Business Box (Swisscom BB) as well as Cordoba family products is a 
> software solution designed to facilitate the efficient management
> and control of devices through the integration of various Linux
> frameworks. This platform driver provides seamless support for
> device management via the Linux LED framework, GPIO framework,
> Hardware Monitoring (HWMON), and device attributes. The Silicom
> platform driver's compatibility with these Linux frameworks allows
> applications to access and control Cordoba family devices using
> existing software that is compatible with these frameworks. This
> compatibility simplifies the development process, reduces
> dependencies on proprietary solutions, and promotes
> interoperability with other Linux-based systems and software.
> 
> Signed-off-by: Henry Shi <henryshi2018@gmail.com>
> ---

You should use version the submission (vXX should appear already in the 
subject) and provide the version history in a list (listing version to 
version changes).

>  drivers/platform/x86/Kconfig            |   11 +
>  drivers/platform/x86/Makefile           |    1 +
>  drivers/platform/x86/silicom-platform.c | 1053 +++++++++++++++++++++++
>  3 files changed, 1065 insertions(+)
>  create mode 100644 drivers/platform/x86/silicom-platform.c
> 
> diff --git a/drivers/platform/x86/Kconfig b/drivers/platform/x86/Kconfig
> index 22052031c719..7680c0dbcd8d 100644
> --- a/drivers/platform/x86/Kconfig
> +++ b/drivers/platform/x86/Kconfig
> @@ -188,6 +188,17 @@ config ACER_WMI
>  	  If you have an ACPI-WMI compatible Acer/ Wistron laptop, say Y or M
>  	  here.
>  
> +config SILICOM_PLATFORM
> +	tristate "Silicom Edge Networking device support"
> +	depends on DMI
> +	select LEDS_CLASS_MULTICOLOR
> +	select GPIOLIB
> +	help
> +	  This option enables support for the LEDs/GPIO/etc downstream of the
> +	  embedded controller on Silicom "Cordoba" hardware and derivatives.
> +
> +	  If you have a Silicom network appliance, say Y or M here.
> +
>  source "drivers/platform/x86/amd/Kconfig"
>  
>  config ADV_SWBUTTON
> diff --git a/drivers/platform/x86/Makefile b/drivers/platform/x86/Makefile
> index 2cafe51ec4d8..9355ebbc56ca 100644
> --- a/drivers/platform/x86/Makefile
> +++ b/drivers/platform/x86/Makefile
> @@ -113,6 +113,7 @@ obj-$(CONFIG_SERIAL_MULTI_INSTANTIATE)	+= serial-multi-instantiate.o
>  obj-$(CONFIG_MLX_PLATFORM)		+= mlx-platform.o
>  obj-$(CONFIG_TOUCHSCREEN_DMI)		+= touchscreen_dmi.o
>  obj-$(CONFIG_WIRELESS_HOTKEY)		+= wireless-hotkey.o
> +obj-$(CONFIG_SILICOM_PLATFORM)		+= silicom-platform.o
>  obj-$(CONFIG_X86_ANDROID_TABLETS)	+= x86-android-tablets/
>  
>  # Intel uncore drivers
> diff --git a/drivers/platform/x86/silicom-platform.c b/drivers/platform/x86/silicom-platform.c
> new file mode 100644
> index 000000000000..f8d1eb68b105
> --- /dev/null
> +++ b/drivers/platform/x86/silicom-platform.c
> @@ -0,0 +1,1053 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +//
> +// silicom-platform.c - Silicom MEC170x platform driver
> +//
> +// Copyright (C) 2023 Henry Shi <henrys@silicom-usa.com>
> +
> +#include <linux/dmi.h>
> +#include <linux/gpio/driver.h>
> +#include <linux/init.h>
> +#include <linux/ioport.h>
> +#include <linux/io.h>
> +#include <linux/kernel.h>
> +#include <linux/led-class-multicolor.h>
> +#include <linux/module.h>
> +#include <linux/hwmon.h>
> +#include <linux/mutex.h>
> +#include <linux/platform_device.h>
> +#include <linux/string.h>
> +#include <linux/kobject.h>
> +#include <linux/sysfs.h>
> +#include <linux/bits.h>
> +#include <linux/bitfield.h>
> +
> +#define MEC_ADDR ((mec_io_base) + 0x02)
> +#define MEC_DATA(offset) ((mec_io_base) + 0x04 + (offset))
> +#define EC_ADDR_LSB MEC_ADDR
> +#define EC_ADDR_MSB ((mec_io_base) + 0x03)
> +#define SILICOM_MEC_MAGIC 0x5a
> +#define OFFSET_BIT_TO_CHANNEL(off, bit) ((((off) + 0x014) << 3) | (bit))
> +#define CHANNEL_TO_OFFSET(chan) (((chan) >> 3) - 0x14)
> +#define IO_REG_BANK 0
> +#define DEFAULT_CHAN_LO 0
> +#define DEFAULT_CHAN_HI 0
> +
> +static DEFINE_MUTEX(mec_io_mutex);
> +static int mec_io_base, mec_io_len;
> +static struct device *my_dev;
> +static int efuse_status;
> +static int mec_uc_version;
> +static int power_cycle;
> +
> +struct silicom_fan_control_data {
> +	struct   device *hdev;
> +	int      temp;
> +	int      fan_speed;
> +};
> +
> +static const struct hwmon_channel_info *silicom_fan_control_info[] = {
> +	HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT | HWMON_F_LABEL),
> +	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT | HWMON_T_LABEL),
> +	NULL
> +};
> +
> +struct silicom_platform_info {
> +	int io_base;
> +	int io_len;
> +	struct led_classdev_mc *led_info;
> +	struct gpio_chip *gpiochip;
> +	u8 *gpio_channels;
> +	u16 ngpio;
> +};
> +
> +static const char * const plat_0222_gpio_names[] = {
> +	"AUTOM0_SFP_TX_FAULT",
> +	"SLOT2_LED_OUT",
> +	"SIM_M2_SLOT2_B_DET",
> +	"SIM_M2_SLOT2_A_DET",
> +	"SLOT1_LED_OUT",
> +	"SIM_M2_SLOT1_B_DET",
> +	"SIM_M2_SLOT1_A_DET",
> +	"SLOT0_LED_OUT",
> +	"WAN_SFP0_RX_LOS",
> +	"WAN_SFP0_PRSNT_N",
> +	"WAN_SFP0_TX_FAULT",
> +	"AUTOM1_SFP_RX_LOS",
> +	"AUTOM1_SFP_PRSNT_N",
> +	"AUTOM1_SFP_TX_FAULT",
> +	"AUTOM0_SFP_RX_LOS",
> +	"AUTOM0_SFP_PRSNT_N",
> +	"WAN_SFP1_RX_LOS",
> +	"WAN_SFP1_PRSNT_N",
> +	"WAN_SFP1_TX_FAULT",
> +	"SIM_M2_SLOT1_MUX_SEL",
> +	"W_DISABLE_M2_SLOT1_N",
> +	"W_DISABLE_MPCIE_SLOT0_N",
> +	"W_DISABLE_M2_SLOT0_N",
> +	"BT_COMMAND_MODE",
> +	"WAN_SFP1_TX_DISABLE",
> +	"WAN_SFP0_TX_DISABLE",
> +	"AUTOM1_SFP_TX_DISABLE",
> +	"AUTOM0_SFP_TX_DISABLE",
> +	"SIM_M2_SLOT2_MUX_SEL",
> +	"W_DISABLE_M2_SLOT2_N",
> +	"RST_CTL_M2_SLOT_1_N",
> +	"RST_CTL_M2_SLOT_2_N",
> +	"PM_USB_PWR_EN_BOT",
> +	"PM_USB_PWR_EN_TOP",
> +};
> +
> +static u8 plat_0222_gpio_channels[] = {
> +	OFFSET_BIT_TO_CHANNEL(0x00, 0),
> +	OFFSET_BIT_TO_CHANNEL(0x00, 1),
> +	OFFSET_BIT_TO_CHANNEL(0x00, 2),
> +	OFFSET_BIT_TO_CHANNEL(0x00, 3),
> +	OFFSET_BIT_TO_CHANNEL(0x00, 4),
> +	OFFSET_BIT_TO_CHANNEL(0x00, 5),
> +	OFFSET_BIT_TO_CHANNEL(0x00, 6),
> +	OFFSET_BIT_TO_CHANNEL(0x00, 7),
> +	OFFSET_BIT_TO_CHANNEL(0x01, 0),
> +	OFFSET_BIT_TO_CHANNEL(0x01, 1),
> +	OFFSET_BIT_TO_CHANNEL(0x01, 2),
> +	OFFSET_BIT_TO_CHANNEL(0x01, 3),
> +	OFFSET_BIT_TO_CHANNEL(0x01, 4),
> +	OFFSET_BIT_TO_CHANNEL(0x01, 5),
> +	OFFSET_BIT_TO_CHANNEL(0x01, 6),
> +	OFFSET_BIT_TO_CHANNEL(0x01, 7),
> +	OFFSET_BIT_TO_CHANNEL(0x02, 0),
> +	OFFSET_BIT_TO_CHANNEL(0x02, 1),
> +	OFFSET_BIT_TO_CHANNEL(0x02, 2),
> +	OFFSET_BIT_TO_CHANNEL(0x09, 0),
> +	OFFSET_BIT_TO_CHANNEL(0x09, 1),
> +	OFFSET_BIT_TO_CHANNEL(0x09, 2),
> +	OFFSET_BIT_TO_CHANNEL(0x09, 3),
> +	OFFSET_BIT_TO_CHANNEL(0x0a, 0),
> +	OFFSET_BIT_TO_CHANNEL(0x0a, 1),
> +	OFFSET_BIT_TO_CHANNEL(0x0a, 2),
> +	OFFSET_BIT_TO_CHANNEL(0x0a, 3),
> +	OFFSET_BIT_TO_CHANNEL(0x0a, 4),
> +	OFFSET_BIT_TO_CHANNEL(0x0a, 5),
> +	OFFSET_BIT_TO_CHANNEL(0x0a, 6),
> +	OFFSET_BIT_TO_CHANNEL(0x0b, 0),
> +	OFFSET_BIT_TO_CHANNEL(0x0b, 1),
> +	OFFSET_BIT_TO_CHANNEL(0x0b, 2),
> +	OFFSET_BIT_TO_CHANNEL(0x0b, 3),
> +};
> +
> +static struct platform_device *silicom_platform_dev;
> +static struct led_classdev_mc *silicom_led_info __initdata;
> +static struct gpio_chip *silicom_gpiochip __initdata;
> +static u8 *silicom_gpio_channels __initdata;
> +
> +static int silicom_mec_port_get(unsigned int offset)
> +{
> +	u8 reg;
> +
> +	mutex_lock(&mec_io_mutex);
> +	/* Get the dword offset from the channel */
> +	outb((offset >> 3) & 0xfc, MEC_ADDR);
> +
> +	/* Get the current register */
> +	reg = inb(MEC_DATA((offset >> 3) & 0x03));
> +	mutex_unlock(&mec_io_mutex);
> +
> +	return (reg >> (offset & 0x7)) & 0x01;
> +}
> +
> +static enum led_brightness silicom_mec_led_get(int channel)
> +{
> +	u8 reg;
> +
> +	mutex_lock(&mec_io_mutex);
> +	/* Get the dword offset of the register for this LED from the channel */
> +	outb((channel >> 3) & 0xfc, MEC_ADDR);
> +	/* Get the current LED settings */
> +	reg = inb(MEC_DATA((channel >> 3) & 0x03));
> +	mutex_unlock(&mec_io_mutex);
> +
> +	/* Outputs are active low */
> +	return silicom_mec_port_get(channel) ? LED_OFF : LED_ON;

Why is code now done twice, first in this function and then it calls 
silicom_mec_port_get() which does the same thing?? Perhaps you forgot to 
remove it from this function while you added the call.

> +}
> +
> +static void silicom_mec_port_set(int channel, int on)
> +{
> +	u8 reg;
> +
> +	mutex_lock(&mec_io_mutex);
> +	/* Get the dword offset from the channel */
> +	outb((channel >> 3) & 0xfc, MEC_ADDR);
> +	/* Get the current port settings */
> +	reg = inb(MEC_DATA((channel >> 3) & 0x03));
> +	/* Outputs are active low, so clear the bit for on, or set it for off */
> +	if (on)
> +		reg &= ~(1 << (channel & 0x7));
> +	else
> +		reg |= 1 << (channel & 0x7);
> +	/* Write back the updated register */
> +	outb(reg, MEC_DATA((channel >> 3) & 0x03));
> +	mutex_unlock(&mec_io_mutex);
> +}
> +
> +static enum led_brightness silicom_mec_led_mc_brightness_get(struct led_classdev *led_cdev)
> +{
> +	struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(led_cdev);
> +	enum led_brightness brightness = LED_OFF;
> +	int i;
> +
> +	for (i = 0; i < mc_cdev->num_colors; i++) {
> +		mc_cdev->subled_info[i].brightness =
> +			silicom_mec_led_get(mc_cdev->subled_info[i].channel);
> +		/* Mark the overall brightness as LED_ON if any of the subleds are on */
> +		if (mc_cdev->subled_info[i].brightness != LED_OFF)
> +			brightness = LED_ON;
> +	}
> +
> +	return brightness;
> +}
> +
> +static void silicom_mec_led_mc_brightness_set(struct led_classdev *led_cdev,
> +											enum led_brightness brightness)
> +{
> +	struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(led_cdev);
> +	int i;
> +
> +	led_mc_calc_color_components(mc_cdev, brightness);
> +	for (i = 0; i < mc_cdev->num_colors; i++) {
> +		silicom_mec_port_set(mc_cdev->subled_info[i].channel,
> +					mc_cdev->subled_info[i].brightness);

Align the arguments to the same column please.

> +	}
> +}
> +
> +static int silicom_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
> +{
> +	u8 *channels = gpiochip_get_data(gc);
> +
> +	/* Input registers have offsets between [0x00, 0x07] */
> +	if (CHANNEL_TO_OFFSET(channels[offset]) < 0x08)
> +		return GPIO_LINE_DIRECTION_IN;
> +
> +	return GPIO_LINE_DIRECTION_OUT;
> +}
> +
> +static int silicom_gpio_direction_input(struct gpio_chip *gc, unsigned int offset)
> +{
> +	int direction = silicom_gpio_get_direction(gc, offset);
> +
> +	return direction == GPIO_LINE_DIRECTION_IN ? 0 : -EINVAL;
> +}
> +
> +static void silicom_gpio_set(struct gpio_chip *gc, unsigned int offset, int value)
> +{
> +	u8 *channels = gpiochip_get_data(gc);
> +	int direction = silicom_gpio_get_direction(gc, offset);
> +	int channel = channels[offset];
> +
> +	if (direction == GPIO_LINE_DIRECTION_IN)
> +		return;
> +
> +	if (value)
> +		silicom_mec_port_set(channel, 0);
> +	else if (value == 0)
> +		silicom_mec_port_set(channel, 1);
> +	else
> +		pr_err("Wrong argument value: %d\n", value);
> +}
> +
> +static int silicom_gpio_direction_output(struct gpio_chip *gc, unsigned int offset, int value)
> +{
> +	int direction = silicom_gpio_get_direction(gc, offset);
> +
> +	if (direction == GPIO_LINE_DIRECTION_IN)
> +		return -EINVAL;
> +
> +	silicom_gpio_set(gc, offset, value);
> +
> +	return 0;
> +}
> +
> +static int silicom_gpio_get(struct gpio_chip *gc, unsigned int offset)
> +{
> +	u8 *channels = gpiochip_get_data(gc);
> +	int channel = channels[offset];
> +
> +	return silicom_mec_port_get(channel);
> +}
> +
> +


> +static ssize_t efuse_status_show(struct device *dev, struct device_attribute *attr,
> +								char *buf)
> +{
> +	u32 reg;
> +
> +	mutex_lock(&mec_io_mutex);
> +	/* Select memory region */
> +	outb(IO_REG_BANK, EC_ADDR_MSB);
> +	outb(0x28, EC_ADDR_LSB);

That 0x28 is some HW offset right? It should be named to what is found at 
that address with a define. Fiven the function name, perhaps something 
along the lines of #define MEC_EFUSE_STATUS	0x28

> +
> +	/* Get current data from the address */
> +	reg = inl(MEC_DATA(DEFAULT_CHAN_LO));
> +	mutex_unlock(&mec_io_mutex);
> +
> +	efuse_status = reg & 0x1;
> +
> +	return sprintf(buf, "%d\n", efuse_status);
> +}
> +
> +static ssize_t uc_version_show(struct device *dev,
> +					struct device_attribute *attr,
> +					char *buf)
> +{
> +	u32 reg;
> +	int uc_version;
> +
> +	mutex_lock(&mec_io_mutex);
> +	outb(IO_REG_BANK, EC_ADDR_MSB);
> +	outb(0x0, EC_ADDR_LSB);

Named define for 0x0.

> +
> +	reg = inl(MEC_DATA(DEFAULT_CHAN_LO));
> +	mutex_unlock(&mec_io_mutex);
> +	uc_version = FIELD_GET(GENMASK(15, 8), reg);

In general, it's more useful to have #define with name for GENMASK() like 
this, but see below...

> +	if (uc_version >= 64 && uc_version < 128) {
> +		uc_version &= ~(1 << 6);
> +		uc_version = 100 + uc_version;
> +	} else if (uc_version >= 128 && uc_version < 192) {
> +		uc_version &= ~(1 << 7);
> +		uc_version = 200 + uc_version;
> +	}

I see you probably missed what I tried to say earlier. Does this version 
field have two distinct fields? How about this:

#define	MEC_VERSION_MAJOR	GENMASK(15, 14)
#define MEC_VERSION_MINOR	GENMASK(13, 8)

	uc_version = FIELD_GET(MEC_VERSION_MAJOR, reg) * 100 +
		     FIELD_GET(MEC_VERSION_MINOR, reg);

...you might want to add something for >= 192 values (or accept they'll be 
in 300..3xx range if that's okay, I don't know the internals of this 
fields so I cannot tell which is preferred here).

I think the results are identical to what the above code does but doesn't
require any if()s (sans >= 192 that might need additional check).

> +	mec_uc_version = uc_version;
> +	return sprintf(buf, "%d\n", mec_uc_version);
> +}
> +
> +static ssize_t power_cycle_show(struct device *dev,
> +				struct device_attribute *attr,
> +				char *buf)
> +{
> +	return sprintf(buf, "%d\n", power_cycle);
> +}
> +
> +static void powercycle_uc(void)
> +{
> +	mutex_lock(&mec_io_mutex);
> +	/* Select memory region */
> +	outb(IO_REG_BANK, EC_ADDR_MSB);
> +	outb(0x24, EC_ADDR_LSB);

Named define for 0x24.

-- 
 i.



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

* [PATCH] Add Silicom Platform Driver
@ 2023-08-15 13:37 Henry Shi
  2023-08-17  8:06 ` Ilpo Järvinen
  0 siblings, 1 reply; 32+ messages in thread
From: Henry Shi @ 2023-08-15 13:37 UTC (permalink / raw)
  To: hbshi69, tglx, mingo, bp, dave.hansen, x86, hpa, hdegoede,
	markgross, jdelvare, linux, linux-kernel, platform-driver-x86,
	linux-hwmon
  Cc: hb_shi2003, henrys, wenw

The Silicom platform (silicom-platform) Linux driver for Swisscom
Business Box (Swisscom BB) as well as Cordoba family products is a 
software solution designed to facilitate the efficient management
and control of devices through the integration of various Linux
frameworks. This platform driver provides seamless support for
device management via the Linux LED framework, GPIO framework,
Hardware Monitoring (HWMON), and device attributes. The Silicom
platform driver's compatibility with these Linux frameworks allows
applications to access and control Cordoba family devices using
existing software that is compatible with these frameworks. This
compatibility simplifies the development process, reduces
dependencies on proprietary solutions, and promotes
interoperability with other Linux-based systems and software.

Signed-off-by: Henry Shi <henryshi2018@gmail.com>
---
 drivers/platform/x86/Kconfig            |   11 +
 drivers/platform/x86/Makefile           |    1 +
 drivers/platform/x86/silicom-platform.c | 1053 +++++++++++++++++++++++
 3 files changed, 1065 insertions(+)
 create mode 100644 drivers/platform/x86/silicom-platform.c

diff --git a/drivers/platform/x86/Kconfig b/drivers/platform/x86/Kconfig
index 22052031c719..7680c0dbcd8d 100644
--- a/drivers/platform/x86/Kconfig
+++ b/drivers/platform/x86/Kconfig
@@ -188,6 +188,17 @@ config ACER_WMI
 	  If you have an ACPI-WMI compatible Acer/ Wistron laptop, say Y or M
 	  here.
 
+config SILICOM_PLATFORM
+	tristate "Silicom Edge Networking device support"
+	depends on DMI
+	select LEDS_CLASS_MULTICOLOR
+	select GPIOLIB
+	help
+	  This option enables support for the LEDs/GPIO/etc downstream of the
+	  embedded controller on Silicom "Cordoba" hardware and derivatives.
+
+	  If you have a Silicom network appliance, say Y or M here.
+
 source "drivers/platform/x86/amd/Kconfig"
 
 config ADV_SWBUTTON
diff --git a/drivers/platform/x86/Makefile b/drivers/platform/x86/Makefile
index 2cafe51ec4d8..9355ebbc56ca 100644
--- a/drivers/platform/x86/Makefile
+++ b/drivers/platform/x86/Makefile
@@ -113,6 +113,7 @@ obj-$(CONFIG_SERIAL_MULTI_INSTANTIATE)	+= serial-multi-instantiate.o
 obj-$(CONFIG_MLX_PLATFORM)		+= mlx-platform.o
 obj-$(CONFIG_TOUCHSCREEN_DMI)		+= touchscreen_dmi.o
 obj-$(CONFIG_WIRELESS_HOTKEY)		+= wireless-hotkey.o
+obj-$(CONFIG_SILICOM_PLATFORM)		+= silicom-platform.o
 obj-$(CONFIG_X86_ANDROID_TABLETS)	+= x86-android-tablets/
 
 # Intel uncore drivers
diff --git a/drivers/platform/x86/silicom-platform.c b/drivers/platform/x86/silicom-platform.c
new file mode 100644
index 000000000000..f8d1eb68b105
--- /dev/null
+++ b/drivers/platform/x86/silicom-platform.c
@@ -0,0 +1,1053 @@
+// SPDX-License-Identifier: GPL-2.0+
+//
+// silicom-platform.c - Silicom MEC170x platform driver
+//
+// Copyright (C) 2023 Henry Shi <henrys@silicom-usa.com>
+
+#include <linux/dmi.h>
+#include <linux/gpio/driver.h>
+#include <linux/init.h>
+#include <linux/ioport.h>
+#include <linux/io.h>
+#include <linux/kernel.h>
+#include <linux/led-class-multicolor.h>
+#include <linux/module.h>
+#include <linux/hwmon.h>
+#include <linux/mutex.h>
+#include <linux/platform_device.h>
+#include <linux/string.h>
+#include <linux/kobject.h>
+#include <linux/sysfs.h>
+#include <linux/bits.h>
+#include <linux/bitfield.h>
+
+#define MEC_ADDR ((mec_io_base) + 0x02)
+#define MEC_DATA(offset) ((mec_io_base) + 0x04 + (offset))
+#define EC_ADDR_LSB MEC_ADDR
+#define EC_ADDR_MSB ((mec_io_base) + 0x03)
+#define SILICOM_MEC_MAGIC 0x5a
+#define OFFSET_BIT_TO_CHANNEL(off, bit) ((((off) + 0x014) << 3) | (bit))
+#define CHANNEL_TO_OFFSET(chan) (((chan) >> 3) - 0x14)
+#define IO_REG_BANK 0
+#define DEFAULT_CHAN_LO 0
+#define DEFAULT_CHAN_HI 0
+
+static DEFINE_MUTEX(mec_io_mutex);
+static int mec_io_base, mec_io_len;
+static struct device *my_dev;
+static int efuse_status;
+static int mec_uc_version;
+static int power_cycle;
+
+struct silicom_fan_control_data {
+	struct   device *hdev;
+	int      temp;
+	int      fan_speed;
+};
+
+static const struct hwmon_channel_info *silicom_fan_control_info[] = {
+	HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT | HWMON_F_LABEL),
+	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT | HWMON_T_LABEL),
+	NULL
+};
+
+struct silicom_platform_info {
+	int io_base;
+	int io_len;
+	struct led_classdev_mc *led_info;
+	struct gpio_chip *gpiochip;
+	u8 *gpio_channels;
+	u16 ngpio;
+};
+
+static const char * const plat_0222_gpio_names[] = {
+	"AUTOM0_SFP_TX_FAULT",
+	"SLOT2_LED_OUT",
+	"SIM_M2_SLOT2_B_DET",
+	"SIM_M2_SLOT2_A_DET",
+	"SLOT1_LED_OUT",
+	"SIM_M2_SLOT1_B_DET",
+	"SIM_M2_SLOT1_A_DET",
+	"SLOT0_LED_OUT",
+	"WAN_SFP0_RX_LOS",
+	"WAN_SFP0_PRSNT_N",
+	"WAN_SFP0_TX_FAULT",
+	"AUTOM1_SFP_RX_LOS",
+	"AUTOM1_SFP_PRSNT_N",
+	"AUTOM1_SFP_TX_FAULT",
+	"AUTOM0_SFP_RX_LOS",
+	"AUTOM0_SFP_PRSNT_N",
+	"WAN_SFP1_RX_LOS",
+	"WAN_SFP1_PRSNT_N",
+	"WAN_SFP1_TX_FAULT",
+	"SIM_M2_SLOT1_MUX_SEL",
+	"W_DISABLE_M2_SLOT1_N",
+	"W_DISABLE_MPCIE_SLOT0_N",
+	"W_DISABLE_M2_SLOT0_N",
+	"BT_COMMAND_MODE",
+	"WAN_SFP1_TX_DISABLE",
+	"WAN_SFP0_TX_DISABLE",
+	"AUTOM1_SFP_TX_DISABLE",
+	"AUTOM0_SFP_TX_DISABLE",
+	"SIM_M2_SLOT2_MUX_SEL",
+	"W_DISABLE_M2_SLOT2_N",
+	"RST_CTL_M2_SLOT_1_N",
+	"RST_CTL_M2_SLOT_2_N",
+	"PM_USB_PWR_EN_BOT",
+	"PM_USB_PWR_EN_TOP",
+};
+
+static u8 plat_0222_gpio_channels[] = {
+	OFFSET_BIT_TO_CHANNEL(0x00, 0),
+	OFFSET_BIT_TO_CHANNEL(0x00, 1),
+	OFFSET_BIT_TO_CHANNEL(0x00, 2),
+	OFFSET_BIT_TO_CHANNEL(0x00, 3),
+	OFFSET_BIT_TO_CHANNEL(0x00, 4),
+	OFFSET_BIT_TO_CHANNEL(0x00, 5),
+	OFFSET_BIT_TO_CHANNEL(0x00, 6),
+	OFFSET_BIT_TO_CHANNEL(0x00, 7),
+	OFFSET_BIT_TO_CHANNEL(0x01, 0),
+	OFFSET_BIT_TO_CHANNEL(0x01, 1),
+	OFFSET_BIT_TO_CHANNEL(0x01, 2),
+	OFFSET_BIT_TO_CHANNEL(0x01, 3),
+	OFFSET_BIT_TO_CHANNEL(0x01, 4),
+	OFFSET_BIT_TO_CHANNEL(0x01, 5),
+	OFFSET_BIT_TO_CHANNEL(0x01, 6),
+	OFFSET_BIT_TO_CHANNEL(0x01, 7),
+	OFFSET_BIT_TO_CHANNEL(0x02, 0),
+	OFFSET_BIT_TO_CHANNEL(0x02, 1),
+	OFFSET_BIT_TO_CHANNEL(0x02, 2),
+	OFFSET_BIT_TO_CHANNEL(0x09, 0),
+	OFFSET_BIT_TO_CHANNEL(0x09, 1),
+	OFFSET_BIT_TO_CHANNEL(0x09, 2),
+	OFFSET_BIT_TO_CHANNEL(0x09, 3),
+	OFFSET_BIT_TO_CHANNEL(0x0a, 0),
+	OFFSET_BIT_TO_CHANNEL(0x0a, 1),
+	OFFSET_BIT_TO_CHANNEL(0x0a, 2),
+	OFFSET_BIT_TO_CHANNEL(0x0a, 3),
+	OFFSET_BIT_TO_CHANNEL(0x0a, 4),
+	OFFSET_BIT_TO_CHANNEL(0x0a, 5),
+	OFFSET_BIT_TO_CHANNEL(0x0a, 6),
+	OFFSET_BIT_TO_CHANNEL(0x0b, 0),
+	OFFSET_BIT_TO_CHANNEL(0x0b, 1),
+	OFFSET_BIT_TO_CHANNEL(0x0b, 2),
+	OFFSET_BIT_TO_CHANNEL(0x0b, 3),
+};
+
+static struct platform_device *silicom_platform_dev;
+static struct led_classdev_mc *silicom_led_info __initdata;
+static struct gpio_chip *silicom_gpiochip __initdata;
+static u8 *silicom_gpio_channels __initdata;
+
+static int silicom_mec_port_get(unsigned int offset)
+{
+	u8 reg;
+
+	mutex_lock(&mec_io_mutex);
+	/* Get the dword offset from the channel */
+	outb((offset >> 3) & 0xfc, MEC_ADDR);
+
+	/* Get the current register */
+	reg = inb(MEC_DATA((offset >> 3) & 0x03));
+	mutex_unlock(&mec_io_mutex);
+
+	return (reg >> (offset & 0x7)) & 0x01;
+}
+
+static enum led_brightness silicom_mec_led_get(int channel)
+{
+	u8 reg;
+
+	mutex_lock(&mec_io_mutex);
+	/* Get the dword offset of the register for this LED from the channel */
+	outb((channel >> 3) & 0xfc, MEC_ADDR);
+	/* Get the current LED settings */
+	reg = inb(MEC_DATA((channel >> 3) & 0x03));
+	mutex_unlock(&mec_io_mutex);
+
+	/* Outputs are active low */
+	return silicom_mec_port_get(channel) ? LED_OFF : LED_ON;
+}
+
+static void silicom_mec_port_set(int channel, int on)
+{
+	u8 reg;
+
+	mutex_lock(&mec_io_mutex);
+	/* Get the dword offset from the channel */
+	outb((channel >> 3) & 0xfc, MEC_ADDR);
+	/* Get the current port settings */
+	reg = inb(MEC_DATA((channel >> 3) & 0x03));
+	/* Outputs are active low, so clear the bit for on, or set it for off */
+	if (on)
+		reg &= ~(1 << (channel & 0x7));
+	else
+		reg |= 1 << (channel & 0x7);
+	/* Write back the updated register */
+	outb(reg, MEC_DATA((channel >> 3) & 0x03));
+	mutex_unlock(&mec_io_mutex);
+}
+
+static enum led_brightness silicom_mec_led_mc_brightness_get(struct led_classdev *led_cdev)
+{
+	struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(led_cdev);
+	enum led_brightness brightness = LED_OFF;
+	int i;
+
+	for (i = 0; i < mc_cdev->num_colors; i++) {
+		mc_cdev->subled_info[i].brightness =
+			silicom_mec_led_get(mc_cdev->subled_info[i].channel);
+		/* Mark the overall brightness as LED_ON if any of the subleds are on */
+		if (mc_cdev->subled_info[i].brightness != LED_OFF)
+			brightness = LED_ON;
+	}
+
+	return brightness;
+}
+
+static void silicom_mec_led_mc_brightness_set(struct led_classdev *led_cdev,
+											enum led_brightness brightness)
+{
+	struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(led_cdev);
+	int i;
+
+	led_mc_calc_color_components(mc_cdev, brightness);
+	for (i = 0; i < mc_cdev->num_colors; i++) {
+		silicom_mec_port_set(mc_cdev->subled_info[i].channel,
+					mc_cdev->subled_info[i].brightness);
+	}
+}
+
+static int silicom_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
+{
+	u8 *channels = gpiochip_get_data(gc);
+
+	/* Input registers have offsets between [0x00, 0x07] */
+	if (CHANNEL_TO_OFFSET(channels[offset]) < 0x08)
+		return GPIO_LINE_DIRECTION_IN;
+
+	return GPIO_LINE_DIRECTION_OUT;
+}
+
+static int silicom_gpio_direction_input(struct gpio_chip *gc, unsigned int offset)
+{
+	int direction = silicom_gpio_get_direction(gc, offset);
+
+	return direction == GPIO_LINE_DIRECTION_IN ? 0 : -EINVAL;
+}
+
+static void silicom_gpio_set(struct gpio_chip *gc, unsigned int offset, int value)
+{
+	u8 *channels = gpiochip_get_data(gc);
+	int direction = silicom_gpio_get_direction(gc, offset);
+	int channel = channels[offset];
+
+	if (direction == GPIO_LINE_DIRECTION_IN)
+		return;
+
+	if (value)
+		silicom_mec_port_set(channel, 0);
+	else if (value == 0)
+		silicom_mec_port_set(channel, 1);
+	else
+		pr_err("Wrong argument value: %d\n", value);
+}
+
+static int silicom_gpio_direction_output(struct gpio_chip *gc, unsigned int offset, int value)
+{
+	int direction = silicom_gpio_get_direction(gc, offset);
+
+	if (direction == GPIO_LINE_DIRECTION_IN)
+		return -EINVAL;
+
+	silicom_gpio_set(gc, offset, value);
+
+	return 0;
+}
+
+static int silicom_gpio_get(struct gpio_chip *gc, unsigned int offset)
+{
+	u8 *channels = gpiochip_get_data(gc);
+	int channel = channels[offset];
+
+	return silicom_mec_port_get(channel);
+}
+
+
+static struct mc_subled plat_0222_wan_mc_subled_info[] __initdata = {
+	{
+		.color_index = LED_COLOR_ID_WHITE,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 7),
+	},
+	{
+		.color_index = LED_COLOR_ID_YELLOW,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 6),
+	},
+	{
+		.color_index = LED_COLOR_ID_RED,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 5),
+	},
+};
+
+static struct mc_subled plat_0222_sys_mc_subled_info[] __initdata = {
+	{
+		.color_index = LED_COLOR_ID_WHITE,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 4),
+	},
+	{
+		.color_index = LED_COLOR_ID_AMBER,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 3),
+	},
+	{
+		.color_index = LED_COLOR_ID_RED,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 2),
+	},
+};
+
+static struct mc_subled plat_0222_stat1_mc_subled_info[] __initdata = {
+	{
+		.color_index = LED_COLOR_ID_RED,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 1),
+	},
+	{
+		.color_index = LED_COLOR_ID_GREEN,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0c, 0),
+	},
+	{
+		.color_index = LED_COLOR_ID_BLUE,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 7),
+	},
+	{
+		.color_index = LED_COLOR_ID_YELLOW,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 6),
+	},
+};
+
+static struct mc_subled plat_0222_stat2_mc_subled_info[] __initdata = {
+	{
+		.color_index = LED_COLOR_ID_RED,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 5),
+	},
+	{
+		.color_index = LED_COLOR_ID_GREEN,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 4),
+	},
+	{
+		.color_index = LED_COLOR_ID_BLUE,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 3),
+	},
+	{
+		.color_index = LED_COLOR_ID_YELLOW,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 2),
+	},
+};
+
+static struct mc_subled plat_0222_stat3_mc_subled_info[] __initdata = {
+	{
+		.color_index = LED_COLOR_ID_RED,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 1),
+	},
+	{
+		.color_index = LED_COLOR_ID_GREEN,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0d, 0),
+	},
+	{
+		.color_index = LED_COLOR_ID_BLUE,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0e, 1),
+	},
+	{
+		.color_index = LED_COLOR_ID_YELLOW,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x0e, 0),
+	},
+};
+
+static struct led_classdev_mc plat_0222_mc_led_info[] __initdata = {
+	{
+		.led_cdev = {
+			.name = "multicolor:wan",
+			.brightness = 0,
+			.max_brightness = 1,
+			.brightness_set = silicom_mec_led_mc_brightness_set,
+			.brightness_get = silicom_mec_led_mc_brightness_get,
+		},
+		.num_colors = ARRAY_SIZE(plat_0222_wan_mc_subled_info),
+		.subled_info = plat_0222_wan_mc_subled_info,
+	},
+	{
+		.led_cdev = {
+			.name = "multicolor:sys",
+			.brightness = 0,
+			.max_brightness = 1,
+			.brightness_set = silicom_mec_led_mc_brightness_set,
+			.brightness_get = silicom_mec_led_mc_brightness_get,
+		},
+		.num_colors = ARRAY_SIZE(plat_0222_sys_mc_subled_info),
+		.subled_info = plat_0222_sys_mc_subled_info,
+	},
+	{
+		.led_cdev = {
+			.name = "multicolor:stat1",
+			.brightness = 0,
+			.max_brightness = 1,
+			.brightness_set = silicom_mec_led_mc_brightness_set,
+			.brightness_get = silicom_mec_led_mc_brightness_get,
+		},
+		.num_colors = ARRAY_SIZE(plat_0222_stat1_mc_subled_info),
+		.subled_info = plat_0222_stat1_mc_subled_info,
+	},
+	{
+		.led_cdev = {
+			.name = "multicolor:stat2",
+			.brightness = 0,
+			.max_brightness = 1,
+			.brightness_set = silicom_mec_led_mc_brightness_set,
+			.brightness_get = silicom_mec_led_mc_brightness_get,
+		},
+		.num_colors = ARRAY_SIZE(plat_0222_stat2_mc_subled_info),
+		.subled_info = plat_0222_stat2_mc_subled_info,
+	},
+	{
+		.led_cdev = {
+			.name = "multicolor:stat3",
+			.brightness = 0,
+			.max_brightness = 1,
+			.brightness_set = silicom_mec_led_mc_brightness_set,
+			.brightness_get = silicom_mec_led_mc_brightness_get,
+		},
+		.num_colors = ARRAY_SIZE(plat_0222_stat3_mc_subled_info),
+		.subled_info = plat_0222_stat3_mc_subled_info,
+	},
+	{ },
+};
+
+static struct gpio_chip silicom_gpio_chip = {
+	.label = "silicom-gpio",
+	.get_direction = silicom_gpio_get_direction,
+	.direction_input = silicom_gpio_direction_input,
+	.direction_output = silicom_gpio_direction_output,
+	.get = silicom_gpio_get,
+	.set = silicom_gpio_set,
+	.base = -1,
+	.ngpio = ARRAY_SIZE(plat_0222_gpio_channels),
+	.names = plat_0222_gpio_names,
+	/* We're using a mutex to protect the indirect access, so we can sleep if the
+	 * lock blocks
+	 */
+	.can_sleep = true,
+};
+
+static struct silicom_platform_info silicom_plat_0222_cordoba_info __initdata = {
+	.io_base = 0x800,
+	.io_len = 8,
+	.led_info = plat_0222_mc_led_info,
+	.gpiochip = &silicom_gpio_chip,
+	.gpio_channels = plat_0222_gpio_channels,
+	/* The original generic cordoba does not have the last 4 outputs of the plat_0222
+	 * BB variant, the rest are the same, so use the same longer list, but ignore the
+	 * last entries here
+	 */
+	.ngpio = ARRAY_SIZE(plat_0222_gpio_channels),
+
+};
+
+static struct mc_subled cordoba_fp_left_mc_subled_info[] __initdata = {
+	{
+		.color_index = LED_COLOR_ID_RED,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 6),
+	},
+	{
+		.color_index = LED_COLOR_ID_GREEN,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 5),
+	},
+	{
+		.color_index = LED_COLOR_ID_BLUE,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x09, 7),
+	},
+	{
+		.color_index = LED_COLOR_ID_AMBER,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x09, 4),
+	},
+};
+
+static struct mc_subled cordoba_fp_center_mc_subled_info[] __initdata = {
+	{
+		.color_index = LED_COLOR_ID_RED,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 7),
+	},
+	{
+		.color_index = LED_COLOR_ID_GREEN,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 4),
+	},
+	{
+		.color_index = LED_COLOR_ID_BLUE,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 3),
+	},
+	{
+		.color_index = LED_COLOR_ID_AMBER,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x09, 6),
+	},
+};
+
+static struct mc_subled cordoba_fp_right_mc_subled_info[] __initdata = {
+	{
+		.color_index = LED_COLOR_ID_RED,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 2),
+	},
+	{
+		.color_index = LED_COLOR_ID_GREEN,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 1),
+	},
+	{
+		.color_index = LED_COLOR_ID_BLUE,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x08, 0),
+	},
+	{
+		.color_index = LED_COLOR_ID_AMBER,
+		.brightness = 1,
+		.intensity = 0,
+		.channel = OFFSET_BIT_TO_CHANNEL(0x09, 5),
+	},
+};
+
+static struct led_classdev_mc cordoba_mc_led_info[] __initdata = {
+	{
+		.led_cdev = {
+			.name = "multicolor:fp_left",
+			.brightness = 0,
+			.max_brightness = 1,
+			.brightness_set = silicom_mec_led_mc_brightness_set,
+			.brightness_get = silicom_mec_led_mc_brightness_get,
+		},
+		.num_colors = ARRAY_SIZE(cordoba_fp_left_mc_subled_info),
+		.subled_info = cordoba_fp_left_mc_subled_info,
+	},
+	{
+		.led_cdev = {
+			.name = "multicolor:fp_center",
+			.brightness = 0,
+			.max_brightness = 1,
+			.brightness_set = silicom_mec_led_mc_brightness_set,
+			.brightness_get = silicom_mec_led_mc_brightness_get,
+		},
+		.num_colors = ARRAY_SIZE(cordoba_fp_center_mc_subled_info),
+		.subled_info = cordoba_fp_center_mc_subled_info,
+	},
+	{
+		.led_cdev = {
+			.name = "multicolor:fp_right",
+			.brightness = 0,
+			.max_brightness = 1,
+			.brightness_set = silicom_mec_led_mc_brightness_set,
+			.brightness_get = silicom_mec_led_mc_brightness_get,
+		},
+		.num_colors = ARRAY_SIZE(cordoba_fp_right_mc_subled_info),
+		.subled_info = cordoba_fp_right_mc_subled_info,
+	},
+	{ },
+};
+
+static struct silicom_platform_info silicom_generic_cordoba_info __initdata = {
+	.io_base = 0x800,
+	.io_len = 8,
+	.led_info = cordoba_mc_led_info,
+	.gpiochip = &silicom_gpio_chip,
+	.gpio_channels = plat_0222_gpio_channels,
+	.ngpio = ARRAY_SIZE(plat_0222_gpio_channels),
+};
+
+static struct platform_driver silicom_platform_driver = {
+	.driver = {
+		.name = "silicom-platform",
+	},
+};
+
+static ssize_t efuse_status_show(struct device *dev, struct device_attribute *attr,
+								char *buf)
+{
+	u32 reg;
+
+	mutex_lock(&mec_io_mutex);
+	/* Select memory region */
+	outb(IO_REG_BANK, EC_ADDR_MSB);
+	outb(0x28, EC_ADDR_LSB);
+
+	/* Get current data from the address */
+	reg = inl(MEC_DATA(DEFAULT_CHAN_LO));
+	mutex_unlock(&mec_io_mutex);
+
+	efuse_status = reg & 0x1;
+
+	return sprintf(buf, "%d\n", efuse_status);
+}
+
+static ssize_t uc_version_show(struct device *dev,
+					struct device_attribute *attr,
+					char *buf)
+{
+	u32 reg;
+	int uc_version;
+
+	mutex_lock(&mec_io_mutex);
+	outb(IO_REG_BANK, EC_ADDR_MSB);
+	outb(0x0, EC_ADDR_LSB);
+
+	reg = inl(MEC_DATA(DEFAULT_CHAN_LO));
+	mutex_unlock(&mec_io_mutex);
+	uc_version = FIELD_GET(GENMASK(15, 8), reg);
+	if (uc_version >= 64 && uc_version < 128) {
+		uc_version &= ~(1 << 6);
+		uc_version = 100 + uc_version;
+	} else if (uc_version >= 128 && uc_version < 192) {
+		uc_version &= ~(1 << 7);
+		uc_version = 200 + uc_version;
+	}
+	mec_uc_version = uc_version;
+	return sprintf(buf, "%d\n", mec_uc_version);
+}
+
+static ssize_t power_cycle_show(struct device *dev,
+				struct device_attribute *attr,
+				char *buf)
+{
+	return sprintf(buf, "%d\n", power_cycle);
+}
+
+static void powercycle_uc(void)
+{
+	mutex_lock(&mec_io_mutex);
+	/* Select memory region */
+	outb(IO_REG_BANK, EC_ADDR_MSB);
+	outb(0x24, EC_ADDR_LSB);
+
+	/* Set to 1 for current data from the address */
+	outb(1, MEC_DATA(DEFAULT_CHAN_LO));
+	mutex_unlock(&mec_io_mutex);
+}
+
+static ssize_t power_cycle_store(struct device *dev, struct device_attribute *attr,
+				 const char *buf, size_t count)
+{
+	if (sscanf(buf, "%du", &power_cycle) != 1) {
+		dev_err(dev, "Failed to read power_cycle\n");
+		return -EINVAL;
+	}
+	if (power_cycle > 0)
+		powercycle_uc();
+
+	return count;
+}
+
+static struct device_attribute my_dev_attr[] = {
+	{
+		.attr = {.name = "efuse_status", .mode = 0644},
+		.show = efuse_status_show,
+		.store = NULL
+	},
+	{
+		.attr = {.name = "uc_version", .mode = 0644},
+		.show = uc_version_show,
+		.store = NULL
+	},
+	{
+		.attr = {.name = "power_cycle", .mode = 0644},
+		.show = power_cycle_show,
+		.store = power_cycle_store
+	},
+};
+
+static int __init silicom_mc_leds_register(struct device *dev,
+					   const struct led_classdev_mc *mc_leds)
+{
+	struct led_classdev_mc *led;
+	int i, err;
+
+	for (i = 0; mc_leds[i].led_cdev.name; i++) {
+		led = devm_kzalloc(dev, sizeof(struct led_classdev_mc), GFP_KERNEL);
+		if (!led)
+			return -ENOMEM;
+		memcpy(led, &mc_leds[i], sizeof(*led));
+
+		led->subled_info = devm_kzalloc(dev, led->num_colors * sizeof(struct mc_subled), GFP_KERNEL);
+		if (!led->subled_info)
+			return -ENOMEM;
+		memcpy(led->subled_info, mc_leds[i].subled_info, led->num_colors * sizeof(struct mc_subled));
+
+		err = devm_led_classdev_multicolor_register(dev, led);
+		if (err) {
+			dev_err(dev, "Failed to register[%d]: %d\n", i, err);
+			return err;
+		}
+	}
+
+	return 0;
+}
+
+static u32 rpm_get(void)
+{
+	u32 reg;
+
+	mutex_lock(&mec_io_mutex);
+	/* Select memory region */
+	outb(IO_REG_BANK, EC_ADDR_MSB);
+	outb(0xc, EC_ADDR_LSB);
+	/* Get current data from the address */
+	reg = inw(MEC_DATA(DEFAULT_CHAN_LO));
+	mutex_unlock(&mec_io_mutex);
+
+	return reg;
+}
+
+static u32 temp_get(void)
+{
+	u32 reg;
+
+	mutex_lock(&mec_io_mutex);
+	/* Select memory region */
+	outb(IO_REG_BANK, EC_ADDR_MSB);
+	outb(0xc, EC_ADDR_LSB);
+	/* Get current data from the address */
+	reg = inl(MEC_DATA(DEFAULT_CHAN_LO));
+	mutex_unlock(&mec_io_mutex);
+
+	return (reg >> 16) / 10;
+}
+
+static umode_t silicom_fan_control_fan_is_visible(const u32 attr)
+{
+	switch (attr) {
+	case hwmon_fan_input:
+	case hwmon_fan_label:
+		return 0444;
+	default:
+		return 0;
+	}
+}
+
+static umode_t silicom_fan_control_temp_is_visible(const u32 attr)
+{
+	switch (attr) {
+	case hwmon_temp_input:
+	case hwmon_temp_label:
+		return 0444;
+	default:
+		return 0;
+	}
+}
+
+static int silicom_fan_control_read_fan(struct device *dev, u32 attr, long *val)
+{
+	struct silicom_fan_control_data *ctl = dev_get_drvdata(dev);
+
+	switch (attr) {
+	case hwmon_fan_input:
+		ctl->fan_speed = rpm_get();
+		*val = ctl->fan_speed;
+		return 0;
+	default:
+		return -EOPNOTSUPP;
+	}
+}
+
+static int silicom_fan_control_read_temp(struct device *dev, u32 attr, long *val)
+{
+	switch (attr) {
+	case hwmon_temp_input:
+		*val = temp_get();
+		return 0;
+	default:
+		return -EOPNOTSUPP;
+	}
+}
+
+static umode_t silicom_fan_control_is_visible(const void *data,
+					  enum hwmon_sensor_types type,
+					  u32 attr, int channel)
+{
+	switch (type) {
+	case hwmon_fan:
+		return silicom_fan_control_fan_is_visible(attr);
+	case hwmon_temp:
+		return silicom_fan_control_temp_is_visible(attr);
+	default:
+		return 0;
+	}
+}
+
+static int silicom_fan_control_read(struct device *dev, enum hwmon_sensor_types type,
+				    u32 attr, int channel, long *val)
+{
+	switch (type) {
+	case hwmon_fan:
+		return silicom_fan_control_read_fan(dev, attr, val);
+	case hwmon_temp:
+		return silicom_fan_control_read_temp(dev, attr, val);
+	default:
+		return -EOPNOTSUPP;
+	}
+}
+
+static int silicom_fan_control_read_labels(struct device *dev, enum hwmon_sensor_types type,
+					   u32 attr, int channel, const char **str)
+{
+	switch (type) {
+	case hwmon_fan:
+		*str = "Fan Speed";
+		return 0;
+	case hwmon_temp:
+		*str = "Thermostat Sensor";
+		return 0;
+	default:
+		return -EOPNOTSUPP;
+	}
+}
+
+static const struct hwmon_ops silicom_fan_control_hwmon_ops = {
+	.is_visible = silicom_fan_control_is_visible,
+	.read = silicom_fan_control_read,
+	.write = NULL,
+	.read_string = silicom_fan_control_read_labels,
+};
+
+static const struct hwmon_chip_info silicom_chip_info = {
+	.ops = &silicom_fan_control_hwmon_ops,
+	.info = silicom_fan_control_info,
+};
+
+static int __init silicom_platform_probe(struct platform_device *device)
+{
+	int i, err;
+	u8 magic, ver;
+	struct silicom_fan_control_data *ctl;
+	const char *name = "Silocom_Fan_Monitor";
+	const char *dev_name = "Silicom_platform";
+
+	mec_io_base = 0x0800;
+	mec_io_len = 8;
+	if (!devm_request_region(&device->dev, mec_io_base, mec_io_len, "mec")) {
+		dev_err(&device->dev, "couldn't reserve MEC io ports\n");
+		return -EBUSY;
+	}
+
+	/* Sanity check magic number read for EC */
+	outb(0x00, MEC_ADDR);
+	magic = inb(MEC_DATA(DEFAULT_CHAN_LO));
+	ver = inb(MEC_DATA(DEFAULT_CHAN_HI));
+	dev_dbg(&device->dev, "EC magic 0x%02x, version 0x%02x\n", magic, ver);
+
+	if (magic != SILICOM_MEC_MAGIC) {
+		dev_err(&device->dev, "Bad EC magic 0x%02x!\n", magic);
+		return -ENODEV;
+	}
+
+	err = silicom_mc_leds_register(&device->dev, silicom_led_info);
+	if (err) {
+		dev_err(&device->dev, "Failed to register LEDs\n");
+		return err;
+	}
+
+	err = devm_gpiochip_add_data(&device->dev, silicom_gpiochip, silicom_gpio_channels);
+	if (err) {
+		dev_err(&device->dev, "Failed to register gpiochip: %d\n", err);
+		return err;
+	}
+
+	ctl = devm_kzalloc(&device->dev, sizeof(*ctl), GFP_KERNEL);
+	if (!ctl)
+		return -ENOMEM;
+
+	ctl->hdev = devm_hwmon_device_register_with_info(&device->dev, name, ctl,
+				&silicom_chip_info, NULL);
+
+	my_dev = root_device_register(dev_name);
+	for (i = 0; i < ARRAY_SIZE(my_dev_attr); i++) {
+		err = sysfs_create_file(&my_dev->kobj, &my_dev_attr[i].attr);
+		if (err) {
+			pr_debug("failed to create the foo file in /sys/devices/Silicom_platform\n");
+			break;
+		}
+	}
+
+	return err;
+}
+
+static int __init silicom_platform_info_init(const struct dmi_system_id *id)
+{
+	struct silicom_platform_info *info = id->driver_data;
+
+	dev_info(&silicom_platform_dev->dev, "Detected %s\n", id->ident);
+
+	mec_io_base = info->io_base;
+	mec_io_len = info->io_len;
+	silicom_led_info = info->led_info;
+	silicom_gpio_channels = info->gpio_channels;
+	silicom_gpiochip = info->gpiochip;
+	if (silicom_gpiochip)
+		silicom_gpiochip->ngpio = info->ngpio;
+
+	return 1;
+}
+
+static const struct dmi_system_id silicom_dmi_ids[] __initconst = {
+	{
+		.callback = silicom_platform_info_init,
+		.ident = "Silicom Cordoba (Generic)",
+		.matches = {
+			DMI_MATCH(DMI_BOARD_VENDOR, "Silicom"),
+			DMI_MATCH(DMI_BOARD_NAME, "80300-0214-G"),
+		},
+		.driver_data = &silicom_generic_cordoba_info,
+	},
+	{
+		.callback = silicom_platform_info_init,
+		.ident = "Silicom Cordoba (Generic)",
+		.matches = {
+			DMI_MATCH(DMI_BOARD_VENDOR, "Silicom"),
+			DMI_MATCH(DMI_BOARD_NAME, "80500-0214-G"),
+		},
+		.driver_data = &silicom_generic_cordoba_info,
+	},
+	{
+		 .callback = silicom_platform_info_init,
+		 .ident = "Silicom Cordoba (plat_0222)",
+		 .matches = {
+			DMI_MATCH(DMI_BOARD_VENDOR, "Silicom"),
+			DMI_MATCH(DMI_BOARD_NAME, "80300-0222-G"),
+		 },
+		.driver_data = &silicom_plat_0222_cordoba_info,
+	},
+	{ },
+};
+
+static int __init silicom_platform_init(void)
+{
+	struct device *dev;
+	int err;
+
+	/* register a platform device to act as the parent for LEDS, etc. */
+	silicom_platform_dev = platform_device_register_simple("silicom-platform", -1, NULL, 0);
+	if (IS_ERR(silicom_platform_dev)) {
+		err = PTR_ERR(silicom_platform_dev);
+		pr_err("failed to register silicom-platform device: %d\n", err);
+		goto silicom_init_register_err;
+	}
+	dev = &silicom_platform_dev->dev;
+
+	err = dmi_check_system(silicom_dmi_ids);
+	if (err == 0) {
+		dev_err(dev, "No DMI match for this platform\n");
+		err = -ENODEV;
+		goto silicom_init_probe_err;
+	}
+
+	/* Directly probe the platform driver in init since this isn't a
+	 * hotpluggable device.  That means we don't need to register a driver
+	 * that needs to wait around in memory on the chance a matching device
+	 * would get added.  Instead run once in __init so that we can free all
+	 * those resources when the __init region is wiped
+	 */
+	err = platform_driver_probe(&silicom_platform_driver, silicom_platform_probe);
+	if (err) {
+		dev_err(dev, "Failed to probe platform driver %d\n", err);
+		goto silicom_init_probe_err;
+	}
+
+	return 0;
+
+silicom_init_probe_err:
+	if (silicom_platform_dev) {
+		platform_device_unregister(silicom_platform_dev);
+		silicom_platform_dev = NULL;
+	}
+	if (my_dev) {
+		root_device_unregister(my_dev);
+		my_dev = NULL;
+	}
+
+silicom_init_register_err:
+	return err;
+}
+
+static void __exit silicom_platform_exit(void)
+{
+	int i;
+
+	if (silicom_platform_dev) {
+		platform_device_unregister(silicom_platform_dev);
+		platform_driver_unregister(&silicom_platform_driver);
+	}
+
+	if (my_dev) {
+		for (i = 0; i < ARRAY_SIZE(my_dev_attr); i++)
+			sysfs_remove_file(&my_dev->kobj, &my_dev_attr[i].attr);
+		root_device_unregister(my_dev);
+	}
+	mutex_destroy(&mec_io_mutex);
+}
+
+module_init(silicom_platform_init);
+module_exit(silicom_platform_exit);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Henry Shi <henrys@silicom-usa.com>");
+MODULE_DESCRIPTION("Platform driver for Silicom network appliances");
+
+MODULE_DEVICE_TABLE(dmi, silicom_dmi_ids);
+
-- 
2.21.3


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

end of thread, other threads:[~2023-08-21 16:23 UTC | newest]

Thread overview: 32+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-18 16:01 [PATCH] Add Silicom Platform Driver Henry Shi
2023-07-18 16:40 ` Guenter Roeck
2023-07-25 20:29   ` Huibin Shi
2023-07-19 12:13 ` Ilpo Järvinen
2023-07-28 19:10   ` Huibin Shi
2023-08-03 12:06     ` Ilpo Järvinen
2023-08-10 17:48       ` Huibin Shi
2023-08-10 18:00         ` Guenter Roeck
2023-08-14 13:07           ` Huibin Shi
2023-08-10 18:01         ` Guenter Roeck
2023-08-14 13:09           ` Huibin Shi
2023-07-25 21:02 ` Christophe JAILLET
2023-07-28 12:59   ` Huibin Shi
2023-07-28 14:28     ` Guenter Roeck
2023-08-08 19:19       ` Huibin Shi
2023-08-08 19:35         ` Guenter Roeck
2023-08-08 20:39           ` Huibin Shi
2023-07-28 16:47     ` Christophe JAILLET
2023-07-28 17:52       ` Guenter Roeck
2023-08-08 19:23         ` Huibin Shi
2023-08-15 13:37 Henry Shi
2023-08-17  8:06 ` Ilpo Järvinen
2023-08-18 14:31   ` Huibin Shi
2023-08-18 15:43 Henry Shi
2023-08-19 12:41 ` Guenter Roeck
2023-08-19 14:20   ` Huibin Shi
2023-08-19 14:36     ` Guenter Roeck
2023-08-21 10:39       ` Ilpo Järvinen
2023-08-21 16:00         ` Huibin Shi
2023-08-21 15:48       ` Huibin Shi
2023-08-21 16:09         ` Guenter Roeck
2023-08-21 16:23           ` Huibin Shi

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).