Skip to contentSkip to conversation

Add OneXPlayer mini AMD board driver

PATCH Superseded Joaquín Ignacio Aramendía 4 participants 19 messages Oct 29, 2022 – Oct 31, 2022

Subsystems (2)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add OneXPlayer mini AMD board driver

Platform driver for OXP Handhelds that expose fan reading and control via hwmon sysfs.

As far as I could gather all OXP boards have the same DMI strings and they are told appart by the boot cpu vendor (Intel/AMD). Currently only AMD boards are supported but the code is made to be simple to add other handheld boards in the future.

Fan control is provided via pwm interface in the range [0-255]. AMD boards have [0-100] as range in the EC, the written value is scaled to accomodate for that.

PWM control is disabled by default, can be enabled via module parameter fan_control=1.

---
 drivers/platform/x86/Kconfig        |   8 +
 drivers/platform/x86/Makefile       |   3 +
 drivers/platform/x86/oxp-platform.c | 393 ++++++++++++++++++++++++++++
 3 files changed, 404 insertions(+)
 create mode 100644 drivers/platform/x86/oxp-platform.c
drivers/platform/x86/Kconfig +8 -0
index f5312f51de19..8fe3ca1dd808 100644
@@ -738,6 +738,14 @@ config XO1_RFKILL
 	  Support for enabling/disabling the WLAN interface on the OLPC XO-1
 	  laptop.
 
+config OXP_DEVICE
+	tristate "OneXPlayer EC fan control"
+	depends on ACPI
+	depends on HWMON
+	help
+		Support for OneXPlayer device board EC fan control. Only AMD boards
+		are supported.
+
 config PCENGINES_APU2
 	tristate "PC Engines APUv2/3 front button and LEDs driver"
 	depends on INPUT && INPUT_KEYBOARD && GPIOLIB
drivers/platform/x86/Makefile +3 -0
index 5a428caa654a..fa6f5c68ec45 100644
@@ -80,6 +80,9 @@ obj-$(CONFIG_MSI_WMI)		+= msi-wmi.o
 obj-$(CONFIG_XO15_EBOOK)	+= xo15-ebook.o
 obj-$(CONFIG_XO1_RFKILL)	+= xo1-rfkill.o
 
+# OneXPlayer
+obj-$(CONFIG_OXP_DEVICE) += oxp-platform.o
+
 # PC Engines
 obj-$(CONFIG_PCENGINES_APU2)	+= pcengines-apuv2.o
 
drivers/platform/x86/oxp-platform.c new file +393 -0

393 lines not on this page · raw message

--
2.38.1

maintainer
#1 lore raw
Quoted 80 lines · Joaquín Ignacio Aramendía

Platform driver for OXP Handhelds that expose fan reading and control via hwmon sysfs.

As far as I could gather all OXP boards have the same DMI strings and they are told appart by the boot cpu vendor (Intel/AMD). Currently only AMD boards are supported but the code is made to be simple to add other handheld boards in the future.

Fan control is provided via pwm interface in the range [0-255]. AMD boards have [0-100] as range in the EC, the written value is scaled to accomodate for that.

PWM control is disabled by default, can be enabled via module parameter fan_control=1.

---
  drivers/platform/x86/Kconfig        |   8 +
  drivers/platform/x86/Makefile       |   3 +
  drivers/platform/x86/oxp-platform.c | 393 ++++++++++++++++++++++++++++
  3 files changed, 404 insertions(+)

create mode 100644 drivers/platform/x86/oxp-platform.c

drivers/platform/x86/Kconfig +8 -0
index f5312f51de19..8fe3ca1dd808 100644
@@ -738,6 +738,14 @@ config XO1_RFKILL
  	  Support for enabling/disabling the WLAN interface on the OLPC XO-1
  	  laptop.
 
+config OXP_DEVICE
+	tristate "OneXPlayer EC fan control"
+	depends on ACPI
+	depends on HWMON
+	help
+		Support for OneXPlayer device board EC fan control. Only AMD boards
+		are supported.
+
  config PCENGINES_APU2
  	tristate "PC Engines APUv2/3 front button and LEDs driver"
  	depends on INPUT && INPUT_KEYBOARD && GPIOLIB
drivers/platform/x86/Makefile +3 -0
index 5a428caa654a..fa6f5c68ec45 100644
@@ -80,6 +80,9 @@ obj-$(CONFIG_MSI_WMI)		+= msi-wmi.o
  obj-$(CONFIG_XO15_EBOOK)	+= xo15-ebook.o
  obj-$(CONFIG_XO1_RFKILL)	+= xo1-rfkill.o
 
+# OneXPlayer
+obj-$(CONFIG_OXP_DEVICE) += oxp-platform.o
+
  # PC Engines
  obj-$(CONFIG_PCENGINES_APU2)	+= pcengines-apuv2.o
 
drivers/platform/x86/oxp-platform.c new file +20 -0
new file mode 100644
index 000000000000..4fb13e7450ff
@@ -0,0 +1,393 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Platform driver for OXP Handhelds that expose fan reading and control
+ * via hwmon sysfs.
+ *
+ * All boards have the same DMI strings and they are told appart by the
+ * boot cpu vendor (Intel/AMD). Currently only AMD boards are supported
+ * but the code is made to be simple to add other handheld boards in the
+ * future.
+ * Fan control is provided via pwm interface in the range [0-255]. AMD
+ * boards use [0-100] as range in the EC, the written value is scaled to
+ * accomodate for that.
+ *
+ * PWM control is disabled by default, can be enabled via module parameter.
+ *
+ * Copyright (C) 2022 Joaquín I. Aramendía <[email protected]>
+ */
+
+#include <linux/acpi.h>
+#include <linux/dev_printk.h>

drivers/platform/x86/oxp-platform.c line 30

Quoted 10 lines
drivers/platform/x86/oxp-platform.c new file +10 -0
+#include <linux/dmi.h>
+#include <linux/hwmon.h>
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <asm/processor.h>
+
+static bool fan_control;
+module_param_hw(fan_control, bool, other, 0644);

Runtime writeable parameter is unacceptable. Why would this be needed anyway ? What is it supposed to accomplish that can not be done with a sysfs attribute ?

Quoted 42 lines
drivers/platform/x86/oxp-platform.c new file +42 -0
+MODULE_PARM_DESC(fan_control, "Enable fan control");
+
+#define ACPI_LOCK_DELAY_MS	500
+
+/* Handle ACPI lock mechanism */
+struct lock_data {
+	u32 mutex;
+	bool (*lock)(struct lock_data *data);
+	bool (*unlock)(struct lock_data *data);
+};
+
+static bool lock_global_acpi_lock(struct lock_data *data)
+{
+	return ACPI_SUCCESS(acpi_acquire_global_lock(ACPI_LOCK_DELAY_MS,
+								 &data->mutex));
+}
+
+static bool unlock_global_acpi_lock(struct lock_data *data)
+{
+	return ACPI_SUCCESS(acpi_release_global_lock(data->mutex));
+}
+
+#define MAX_IDENTICAL_BOARD_VARIATIONS	2
+
+enum board_family {
+	family_unknown,
+	family_mini_amd,
+};
+
+enum oxp_sensor_type {
+	oxp_sensor_fan = 0,
+	oxp_sensor_pwm,
+	oxp_sensor_number,
+};
+
+struct oxp_ec_sensor_addr {
+	enum hwmon_sensor_types type;
+	u8 reg;
+	short size;
+	union {
+		struct {
+			u8 enable;

drivers/platform/x86/oxp-platform.c line 82

Quoted 10 lines
drivers/platform/x86/oxp-platform.c new file +10 -0
+			u8 val_enable;
+			u8 val_disable;
+		};
+		struct {
+		  int max_speed;
+		};
+	};
+};
+
+

Extra empty line.

Quoted 6 lines
drivers/platform/x86/oxp-platform.c new file +6 -0
+/* AMD board EC addresses */
+static const struct oxp_ec_sensor_addr amd_sensors[] = {
+	[oxp_sensor_fan] = {
+		.type = hwmon_fan,
+		.reg = 0x76,
+		.size = 2,

drivers/platform/x86/oxp-platform.c line 98

Quoted 10 lines
drivers/platform/x86/oxp-platform.c new file +10 -0
+		.max_speed = 5000,
+	},
+	[oxp_sensor_pwm] = {
+		.type = hwmon_pwm,
+		.reg = 0x4B,
+		.size = 1,
+		.enable = 0x4A,
+		.val_enable = 0x01,
+		.val_disable = 0x00,
+	},

I don't see the point of this data structure. There is just one entry. Why not use defines ?

Quoted 5 lines
drivers/platform/x86/oxp-platform.c new file +5 -0
+	{},
+};
+
+struct ec_board_info {
+	const char *board_names[MAX_IDENTICAL_BOARD_VARIATIONS];

drivers/platform/x86/oxp-platform.c line 113

Quoted 10 lines
drivers/platform/x86/oxp-platform.c new file +10 -0
+	enum board_family family;
+	const struct oxp_ec_sensor_addr *sensors;
+};
+
+static const struct ec_board_info board_info[] = {
+	{
+		.board_names = {"ONE XPLAYER", "ONEXPLAYER mini A07"},
+		.family = family_mini_amd,
+		.sensors = amd_sensors,
+	},

There is just one family. What is the point of this data structure ?

Quoted 11 lines
drivers/platform/x86/oxp-platform.c new file +11 -0
+	{}
+};
+
+struct oxp_status {
+	struct ec_board_info board;
+	struct lock_data lock_data;
+};
+
+/* Helper functions */
+static int read_from_ec(u8 reg, int size, long *val)
+{

drivers/platform/x86/oxp-platform.c line 134

Quoted 10 lines
drivers/platform/x86/oxp-platform.c new file +10 -0
+	int i;
+	int ret;
+	u8 buffer;
+
+	*val = 0;
+	for (i = 0; i < size; i++) {
+		ret = ec_read(reg + i, &buffer);
+		if (ret)
+			return ret;
+		(*val) <<= i*8;

space between i and 8

drivers/platform/x86/oxp-platform.c line 137

Quoted 3 lines
drivers/platform/x86/oxp-platform.c new file +3 -0
+		*val += buffer;
+	}
+	return ret;
	return 0;

drivers/platform/x86/oxp-platform.c line 143

Quoted 6 lines
drivers/platform/x86/oxp-platform.c new file +6 -0
+}
+
+static int write_to_ec(const struct device *dev, u8 reg, u8 value)
+{
+	struct oxp_status *state = dev_get_drvdata(dev);
+	int ret = -1;

unnecessary (and bad) variable initialization

Quoted 13 lines
drivers/platform/x86/oxp-platform.c new file +13 -0
+
+	if (!state->lock_data.lock(&state->lock_data)) {
+		dev_warn(dev, "Failed to acquire mutex");
+		return -EBUSY;
+	}
+
+	ret = ec_write(reg, value);
+
+	if (!state->lock_data.unlock(&state->lock_data))
+		dev_err(dev, "Failed to release mutex");
+
+	return ret;
+}

drivers/platform/x86/oxp-platform.c line 166

Quoted 10 lines
drivers/platform/x86/oxp-platform.c new file +10 -0
+
+/* Callbacks for hwmon interface */
+static umode_t oxp_ec_hwmon_is_visible(const void *drvdata,
+					enum hwmon_sensor_types type, u32 attr, int channel)
+{
+	switch (type) {
+		case hwmon_fan:
+			return S_IRUGO;
+		case hwmon_pwm:
+			return S_IRUGO | S_IWUSR;

Please use 0444 and 0644 directly. Checkpatch will tell.

drivers/platform/x86/oxp-platform.c line 174

Quoted 8 lines
drivers/platform/x86/oxp-platform.c new file +8 -0
+		default:
+			return 0;
+	}
+	return 0;
+}
+
+static int oxp_platform_read(struct device *dev, enum hwmon_sensor_types type,
+		u32 attr, int channel, long *val)

Align continuation line with '('. Checkpatch will tell.

Quoted 10 lines
drivers/platform/x86/oxp-platform.c new file +10 -0
+{
+	int ret = -1;
+	const struct oxp_status *state = dev_get_drvdata(dev);
+	const struct ec_board_info *board = &state->board;
+
+	switch(type) {
+		case hwmon_fan:
+			switch(attr) {
+				case hwmon_fan_input:
+					ret = read_from_ec(board->sensors[oxp_sensor_fan].reg,

drivers/platform/x86/oxp-platform.c line 194

Quoted 10 lines
drivers/platform/x86/oxp-platform.c new file +10 -0
+							board->sensors[oxp_sensor_fan].size, val);
+					break;
+				case hwmon_fan_max:
+					ret = 0;
+					*val = board->sensors[oxp_sensor_fan].max_speed;
+					break;
+				case hwmon_fan_min:
+					ret = 0;
+					*val = 0;
+					break;

If fan_max and fan_min are not sent to/from the EC, do not provide the attributes.

drivers/platform/x86/oxp-platform.c line 196

Quoted 2 lines
drivers/platform/x86/oxp-platform.c new file +2 -0
+				default:
+					dev_dbg(dev, "Unknown attribute for type %d: %d\n", type, attr);

missing break;

Quoted 8 lines
drivers/platform/x86/oxp-platform.c new file +8 -0
+			}
+			return ret;
+		case hwmon_pwm:
+			switch(attr) {
+				case hwmon_pwm_input:
+					ret = read_from_ec(board->sensors[oxp_sensor_pwm].reg,
+							board->sensors[oxp_sensor_pwm].size, val);
+					if (board->family == family_mini_amd) {

drivers/platform/x86/oxp-platform.c line 214

Quoted 10 lines
drivers/platform/x86/oxp-platform.c new file +10 -0
+						*val = (*val * 255) / 100;
+					}
+					break;
+				case hwmon_pwm_enable:
+					ret = read_from_ec(board->sensors[oxp_sensor_pwm].enable, 1, val);
+					if (*val == board->sensors[oxp_sensor_pwm].val_enable) {
+						*val = 1;
+					} else {
+						*val = 0;
+					}

Unnecessary { }. Checkpatch would tell.

I don't see the point of this code. Why have board->sensors[oxp_sensor_pwm].val_enable to start with ? It is 1. Can the EC return something else ? If so, what is the rationale to map it to 0 ?

drivers/platform/x86/oxp-platform.c line 196

Quoted 3 lines
drivers/platform/x86/oxp-platform.c new file +3 -0
+					break;
+				default:
+					dev_dbg(dev, "Unknown attribute for type %d: %d\n", type, attr);

missing break;

drivers/platform/x86/oxp-platform.c line 222

Quoted 5 lines
drivers/platform/x86/oxp-platform.c new file +5 -0
+			}
+			return ret;
+		default:
+			dev_dbg(dev, "Unknown sensor type %d.\n", type);
+			return -1;

bad error code.

drivers/platform/x86/oxp-platform.c line 228

Quoted 6 lines
drivers/platform/x86/oxp-platform.c new file +6 -0
+	}
+}
+
+static int oxp_pwm_enable(const struct device *dev)
+{
+	int ret = -1;

Unnecessary (and bad) variable initialization.

drivers/platform/x86/oxp-platform.c line 240

Quoted 12 lines
drivers/platform/x86/oxp-platform.c new file +12 -0
+	struct oxp_status *state = dev_get_drvdata(dev);
+	const struct ec_board_info *board = &state->board;
+
+	if (!fan_control) {
+		dev_info(dev, "Can't enable pwm, fan_control=0");
+		return -EPERM;
+	}
+
+	ret = write_to_ec(dev, board->sensors[oxp_sensor_pwm].enable,
+		board->sensors[oxp_sensor_pwm].val_enable);
+
+	return ret;

... and unnecessary variable.

drivers/platform/x86/oxp-platform.c line 245

Quoted 5 lines
drivers/platform/x86/oxp-platform.c new file +5 -0
+}
+
+static int oxp_pwm_disable(const struct device *dev)
+{
+	int ret = -1;

Unnecessary initialization, and bad error code.

drivers/platform/x86/oxp-platform.c line 252

Quoted 7 lines
drivers/platform/x86/oxp-platform.c new file +7 -0
+	struct oxp_status *state = dev_get_drvdata(dev);
+	const struct ec_board_info *board = &state->board;
+
+	if (!fan_control) {
+		dev_info(dev, "Can't disable pwm, fan_control=0");
+		return -EPERM;
+	}

I really don't see the point of the "fan_control" module parameter. One can set it to 1 (or true) to enable the pwm_enable attribute, or set it to 0 to disable it. It is effectively the same as just another attribute, forcing users to write two attributes instead of one. That really doesn't make sense.

drivers/platform/x86/oxp-platform.c line 257

Quoted 5 lines
drivers/platform/x86/oxp-platform.c new file +5 -0
+
+	ret = write_to_ec(dev, board->sensors[oxp_sensor_pwm].enable,
+		board->sensors[oxp_sensor_pwm].val_disable);
+
+	return ret;

Just

	return write_to_ec(...);

would do. There is no need for the ret variable. Same elsewhere.

drivers/platform/x86/oxp-platform.c line 263

Quoted 6 lines
drivers/platform/x86/oxp-platform.c new file +6 -0
+}
+
+static int oxp_platform_write(struct device *dev, enum hwmon_sensor_types type,
+		u32 attr, int channel, long val)
+{
+	int ret = -1;

bad error code.

Quoted 8 lines
drivers/platform/x86/oxp-platform.c new file +8 -0
+	struct oxp_status *state = dev_get_drvdata(dev);
+	const struct ec_board_info *board = &state->board;
+
+	switch(type) {
+		case hwmon_pwm:
+			if (!fan_control) {
+				dev_info(dev, "Can't control fans, fan_control=0");
+				return -EPERM;

drivers/platform/x86/oxp-platform.c line 281

Quoted 10 lines
drivers/platform/x86/oxp-platform.c new file +10 -0
+			}
+			switch(attr) {
+				case hwmon_pwm_enable:
+					if (val == 1) {
+						ret = oxp_pwm_enable(dev);
+					} else if (val == 0) {
+						ret = oxp_pwm_disable(dev);
+					} else {
+						return -EINVAL;
+					}

Unnecessary { }, and the single return on error instead of

						ret = -EINVAL;

is inconsistent.

Quoted 4 lines
drivers/platform/x86/oxp-platform.c new file +4 -0
+					return ret;
+				case hwmon_pwm_input:
+					if (val < 0 || val > 255)
+						return -EINVAL;

drivers/platform/x86/oxp-platform.c line 295

Quoted 10 lines
drivers/platform/x86/oxp-platform.c new file +10 -0
+					if (board->family == family_mini_amd)
+						val = (val * 100) / 255;
+					ret = write_to_ec(dev, board->sensors[oxp_sensor_pwm].reg, val);
+					return ret;
+				default:
+					dev_dbg(dev, "Unknown attribute for type %d: %d", type, attr);
+					return ret;
+			}
+		default:
+			dev_dbg(dev, "Unknown sensor type: %d", type);

break missing

drivers/platform/x86/oxp-platform.c line 305

Quoted 10 lines
drivers/platform/x86/oxp-platform.c new file +10 -0
+	}
+	return ret;
+}
+
+/* Known sensors in the OXP EC controllers */
+static const struct hwmon_channel_info *oxp_platform_sensors[] = {
+	HWMON_CHANNEL_INFO(fan,
+		HWMON_F_INPUT | HWMON_F_MAX | HWMON_F_MIN),
+	HWMON_CHANNEL_INFO(pwm,
+		HWMON_PWM_INPUT | HWMON_PWM_ENABLE),

bad alignment. Please use checkpatch --strict and fix what it reports.

Quoted 29 lines
drivers/platform/x86/oxp-platform.c new file +29 -0
+	NULL
+};
+
+static const struct hwmon_ops oxp_ec_hwmon_ops = {
+	.is_visible = oxp_ec_hwmon_is_visible,
+	.read = oxp_platform_read,
+	.write = oxp_platform_write,
+};
+
+static const struct hwmon_chip_info oxp_ec_chip_info = {
+	.ops = &oxp_ec_hwmon_ops,
+	.info = oxp_platform_sensors,
+};
+
+/* Initialization logic */
+static const struct ec_board_info * __init get_board_info(struct device *dev)
+{
+	const char *dmi_board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
+	const char *dmi_board_name = dmi_get_system_info(DMI_BOARD_NAME);
+	const struct ec_board_info *board;
+
+	if (!dmi_board_vendor || !dmi_board_name ||
+	    (strcasecmp(dmi_board_vendor, "ONE-NETBOOK TECHNOLOGY CO., LTD.") &&
+	     strcasecmp(dmi_board_vendor, "ONE-NETBOOK")))
+		return NULL;
+
+	/* Match our known boards */
+	/* Need to check for AMD/Intel here */
+	for (board = board_info; board->sensors; board++) {

drivers/platform/x86/oxp-platform.c line 344

Quoted 10 lines
drivers/platform/x86/oxp-platform.c new file +10 -0
+		if (match_string(board->board_names,
+				 MAX_IDENTICAL_BOARD_VARIATIONS,
+				 dmi_board_name) >= 0) {
+			if (board->family == family_mini_amd &&
+					boot_cpu_data.x86_vendor == X86_VENDOR_AMD) {
+				return board;
+			}
+		}
+	}
+	return NULL;

Why not use a dmi match table for the above code ?

Quoted 17 lines
drivers/platform/x86/oxp-platform.c new file +17 -0
+}
+
+static int __init oxp_platform_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct device *hwdev;
+	const struct ec_board_info *pboard_info;
+	struct oxp_status *state;
+
+	pboard_info = get_board_info(dev);
+	if (!pboard_info)
+		return -ENODEV;
+
+	state = devm_kzalloc(dev, sizeof(struct oxp_status), GFP_KERNEL);
+	if (!state)
+		return -ENOMEM;
+

drivers/platform/x86/oxp-platform.c line 371

Quoted 10 lines
drivers/platform/x86/oxp-platform.c new file +10 -0
+	state->board = *pboard_info;
+
+	state->lock_data.mutex = 0;
+	state->lock_data.lock = lock_global_acpi_lock;
+	state->lock_data.unlock = unlock_global_acpi_lock;
+
+	hwdev = devm_hwmon_device_register_with_info(dev, "oxpec", state,
+							&oxp_ec_chip_info, NULL);
+
+	return PTR_ERR_OR_ZERO(hwdev);

This only configures a hwmon driver and thus should reside in the hwmon subsystem/directory.

drivers/platform/x86/oxp-platform.c line 378

Quoted 7 lines
drivers/platform/x86/oxp-platform.c new file +7 -0
+}
+
+static const struct acpi_device_id acpi_ec_ids[] = {
+	/* Embedded Controller Device */
+	{ "PNP0C09", 0 },
+	{}
+};

I am not sure if this works. There are other drivers mapping to the same ACPI ID; those may refuse to load if this driver is in the system. We had problems with this before, so I am very concerned about side effects.

Quoted 18 lines
drivers/platform/x86/oxp-platform.c new file +15 -1
+
+static struct platform_driver oxp_platform_driver = {
+	.driver = {
+		.name	= "oxp-platform",
+		.acpi_match_table = acpi_ec_ids,
+	},
+};
+
+MODULE_DEVICE_TABLE(acpi, acpi_ec_ids);
+module_platform_driver_probe(oxp_platform_driver, oxp_platform_probe);
+
+MODULE_AUTHOR("Joaquín Ignacio Aramendía <[email protected]>");
+MODULE_DESCRIPTION(
+	"Platform driver that handles ACPI EC of OneXPlayer devices");
+MODULE_LICENSE("GPL");
--

2.38.1

#2 lore raw

Hello, thanks so much to take the time for the feedback! Maybe this one needs some clarification as for some design choices in regards to some of the structures you see. I know there is only one board supported at the moment, but those structures are meant to be the boilerplate for possibly more boards. I'm aware that these devices are a mess and the EC registers are all over the place. I wanted to make it easier for myself. That said I'll try to address some of the concerns and redo the patcha according to standards :)

Quoted 8 lines · Guenter Roeck

drivers/platform/x86/oxp-platform.c line 30

Quoted 3 lines · Joaquín Ignacio Aramendía
drivers/platform/x86/oxp-platform.c new file +3 -0
+
+static bool fan_control;
+module_param_hw(fan_control, bool, other, 0644);

Runtime writeable parameter is unacceptable. Why would this be needed anyway ? What is it supposed to accomplish that can not be done with a sysfs attribute ?

Is meant for safety, I suppose, but you are right that it is better to have the parameter non-writable at runtime.
The goal is to be able to use the fan readings without allowing the pwm to be used unless you really know what you are doing, but I guess there is no point in having this if already is the pwm_enable attribute.

Quoted 11 lines
Quoted 11 lines
drivers/platform/x86/oxp-platform.c new file +11 -0
+struct oxp_ec_sensor_addr {
+     enum hwmon_sensor_types type;
+     u8 reg;
+     short size;
+     union {
+             struct {
+                     u8 enable;
+                     u8 val_enable;
+                     u8 val_disable;
+             };
+             struct {
Quoted 8 lines

drivers/platform/x86/oxp-platform.c line 82

Quoted 6 lines
drivers/platform/x86/oxp-platform.c new file +6 -0
+               int max_speed;
+             };
+     };
+};
+
+

Extra empty line.

Removed. Thanks.

Quoted 11 lines
Quoted 11 lines
drivers/platform/x86/oxp-platform.c new file +11 -0
+/* AMD board EC addresses */
+static const struct oxp_ec_sensor_addr amd_sensors[] = {
+     [oxp_sensor_fan] = {
+             .type = hwmon_fan,
+             .reg = 0x76,
+             .size = 2,
+             .max_speed = 5000,
+     },
+     [oxp_sensor_pwm] = {
+             .type = hwmon_pwm,
+             .reg = 0x4B,
Quoted 8 lines

drivers/platform/x86/oxp-platform.c line 98

Quoted 5 lines
drivers/platform/x86/oxp-platform.c new file +5 -0
+             .size = 1,
+             .enable = 0x4A,
+             .val_enable = 0x01,
+             .val_disable = 0x00,
+     },

I don't see the point of this data structure. There is just one entry. Why not use defines ?

It is one entry now, but i figured in a while there will be other boards to support with different values. Having this structure seems easier for future updates.
I can remove it and only use defines for now.

Quoted 9 lines
Quoted 9 lines
drivers/platform/x86/oxp-platform.c new file +9 -0
+     {},
+};
+
+struct ec_board_info {
+     const char *board_names[MAX_IDENTICAL_BOARD_VARIATIONS];
+     enum board_family family;
+     const struct oxp_ec_sensor_addr *sensors;
+};
+
Quoted 8 lines

drivers/platform/x86/oxp-platform.c line 113

Quoted 6 lines
drivers/platform/x86/oxp-platform.c new file +6 -0
+static const struct ec_board_info board_info[] = {
+     {
+             .board_names = {"ONE XPLAYER", "ONEXPLAYER mini A07"},
+             .family = family_mini_amd,
+             .sensors = amd_sensors,
+     },

There is just one family. What is the point of this data structure ?

It is meant for expansion on other boards. I only own a OXP mini AMD, but others exist with their own quirks. For example, the same device but with Intel CPU has completely different EC registers, values and ranges.
I guess i can remove the whole "family" thing and bring it back when it is appropriate.

Quoted 15 lines
Quoted 15 lines
drivers/platform/x86/oxp-platform.c new file +15 -0
+     {}
+};
+
+struct oxp_status {
+     struct ec_board_info board;
+     struct lock_data lock_data;
+};
+
+/* Helper functions */
+static int read_from_ec(u8 reg, int size, long *val)
+{
+     int i;
+     int ret;
+     u8 buffer;
+
Quoted 8 lines

drivers/platform/x86/oxp-platform.c line 134

Quoted 6 lines
drivers/platform/x86/oxp-platform.c new file +6 -0
+     *val = 0;
+     for (i = 0; i < size; i++) {
+             ret = ec_read(reg + i, &buffer);
+             if (ret)
+                     return ret;
+             (*val) <<= i*8;

space between i and 8

Ok. Will remove.

Quoted 6 lines

drivers/platform/x86/oxp-platform.c line 137

Quoted 3 lines
drivers/platform/x86/oxp-platform.c new file +3 -0
+             *val += buffer;
+     }
+     return ret;
        return 0;
Quoted 8 lines

drivers/platform/x86/oxp-platform.c line 143

Quoted 6 lines
drivers/platform/x86/oxp-platform.c new file +6 -0
+}
+
+static int write_to_ec(const struct device *dev, u8 reg, u8 value)
+{
+     struct oxp_status *state = dev_get_drvdata(dev);
+     int ret = -1;

unnecessary (and bad) variable initialization

Ok. Will improve on this.

Quoted 17 lines
Quoted 17 lines
drivers/platform/x86/oxp-platform.c new file +17 -0
+
+     if (!state->lock_data.lock(&state->lock_data)) {
+             dev_warn(dev, "Failed to acquire mutex");
+             return -EBUSY;
+     }
+
+     ret = ec_write(reg, value);
+
+     if (!state->lock_data.unlock(&state->lock_data))
+             dev_err(dev, "Failed to release mutex");
+
+     return ret;
+}
+
+/* Callbacks for hwmon interface */
+static umode_t oxp_ec_hwmon_is_visible(const void *drvdata,
+                                     enum hwmon_sensor_types type, u32 attr, int channel)
Quoted 8 lines

drivers/platform/x86/oxp-platform.c line 166

Quoted 6 lines
drivers/platform/x86/oxp-platform.c new file +6 -0
+{
+     switch (type) {
+             case hwmon_fan:
+                     return S_IRUGO;
+             case hwmon_pwm:
+                     return S_IRUGO | S_IWUSR;

Please use 0444 and 0644 directly. Checkpatch will tell.

Oh. I did as the documentation suggested. I must confess I didn't run checkpatch, will don in the next submission.

Quoted 10 lines

drivers/platform/x86/oxp-platform.c line 174

Quoted 8 lines
drivers/platform/x86/oxp-platform.c new file +8 -0
+             default:
+                     return 0;
+     }
+     return 0;
+}
+
+static int oxp_platform_read(struct device *dev, enum hwmon_sensor_types type,
+             u32 attr, int channel, long *val)

Align continuation line with '('. Checkpatch will tell.

Will do. I guess my vim settings are messed up.

Quoted 14 lines
Quoted 14 lines
drivers/platform/x86/oxp-platform.c new file +14 -0
+{
+     int ret = -1;
+     const struct oxp_status *state = dev_get_drvdata(dev);
+     const struct ec_board_info *board = &state->board;
+
+     switch(type) {
+             case hwmon_fan:
+                     switch(attr) {
+                             case hwmon_fan_input:
+                                     ret = read_from_ec(board->sensors[oxp_sensor_fan].reg,
+                                                     board->sensors[oxp_sensor_fan].size, val);
+                                     break;
+                             case hwmon_fan_max:
+                                     ret = 0;
Quoted 8 lines

drivers/platform/x86/oxp-platform.c line 194

Quoted 6 lines
drivers/platform/x86/oxp-platform.c new file +6 -0
+                                     *val = board->sensors[oxp_sensor_fan].max_speed;
+                                     break;
+                             case hwmon_fan_min:
+                                     ret = 0;
+                                     *val = 0;
+                                     break;

If fan_max and fan_min are not sent to/from the EC, do not provide the attributes.

They are not, but they are in the spec of the fan (in the attached sticker, that is). Should I keep it if it's not read directly but has a known value?

Quoted 4 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+                             default:
+                                     dev_dbg(dev, "Unknown attribute for type %d: %d\n", type, attr);

missing break;

Ooops, sorry. Will improve on this one.

Quoted 19 lines
Quoted 8 lines
drivers/platform/x86/oxp-platform.c new file +8 -0
+                     }
+                     return ret;
+             case hwmon_pwm:
+                     switch(attr) {
+                             case hwmon_pwm_input:
+                                     ret = read_from_ec(board->sensors[oxp_sensor_pwm].reg,
+                                                     board->sensors[oxp_sensor_pwm].size, val);
+                                     if (board->family == family_mini_amd) {

drivers/platform/x86/oxp-platform.c line 214

Quoted 10 lines
drivers/platform/x86/oxp-platform.c new file +10 -0
+                                             *val = (*val * 255) / 100;
+                                     }
+                                     break;
+                             case hwmon_pwm_enable:
+                                     ret = read_from_ec(board->sensors[oxp_sensor_pwm].enable, 1, val);
+                                     if (*val == board->sensors[oxp_sensor_pwm].val_enable) {
+                                             *val = 1;
+                                     } else {
+                                             *val = 0;
+                                     }
Quoted 6 lines

Unnecessary { }. Checkpatch would tell.

I don't see the point of this code. Why have board->sensors[oxp_sensor_pwm].val_enable to start with ? It is 1. Can the EC return something else ? If so, what is the rationale to map it to 0 ?

The enable/disable values are configurable, since they can vary from board to board. AMD happens to be just 1 and 0, so it fits in this case. My goal is to map them to 1 and 0 to be exposed in the same way across all devices. That way userspace tools like fancontrold can use this interface as is.

Quoted 5 lines

drivers/platform/x86/oxp-platform.c line 196

Quoted 3 lines
drivers/platform/x86/oxp-platform.c new file +3 -0
+                                     break;
+                             default:
+                                     dev_dbg(dev, "Unknown attribute for type %d: %d\n", type, attr);

missing break;

Oops, will correct. Thanks

Quoted 7 lines

drivers/platform/x86/oxp-platform.c line 222

Quoted 5 lines
drivers/platform/x86/oxp-platform.c new file +5 -0
+                     }
+                     return ret;
+             default:
+                     dev_dbg(dev, "Unknown sensor type %d.\n", type);
+                     return -1;

bad error code.

Should I return EINVAL in this case? Seems appropriate reading error code headers.

Quoted 8 lines

drivers/platform/x86/oxp-platform.c line 228

Quoted 6 lines
drivers/platform/x86/oxp-platform.c new file +6 -0
+     }
+}
+
+static int oxp_pwm_enable(const struct device *dev)
+{
+     int ret = -1;

Unnecessary (and bad) variable initialization.

Ok. Will improve.

Quoted 6 lines
Quoted 6 lines
drivers/platform/x86/oxp-platform.c new file +6 -0
+     struct oxp_status *state = dev_get_drvdata(dev);
+     const struct ec_board_info *board = &state->board;
+
+     if (!fan_control) {
+             dev_info(dev, "Can't enable pwm, fan_control=0");
+             return -EPERM;
Quoted 8 lines

drivers/platform/x86/oxp-platform.c line 240

Quoted 6 lines
drivers/platform/x86/oxp-platform.c new file +6 -0
+     }
+
+     ret = write_to_ec(dev, board->sensors[oxp_sensor_pwm].enable,
+             board->sensors[oxp_sensor_pwm].val_enable);
+
+     return ret;

... and unnecessary variable.

Then I would just do
return write_to_ec():
as stated below.

Quoted 7 lines

drivers/platform/x86/oxp-platform.c line 245

Quoted 5 lines
drivers/platform/x86/oxp-platform.c new file +5 -0
+}
+
+static int oxp_pwm_disable(const struct device *dev)
+{
+     int ret = -1;

Unnecessary initialization, and bad error code.

Seems like I really like to mess this up, really... Sorry.

Quoted 8 lines

drivers/platform/x86/oxp-platform.c line 252

Quoted 7 lines
drivers/platform/x86/oxp-platform.c new file +7 -0
+     struct oxp_status *state = dev_get_drvdata(dev);
+     const struct ec_board_info *board = &state->board;
+
+     if (!fan_control) {
+             dev_info(dev, "Can't disable pwm, fan_control=0");
+             return -EPERM;
+     }
Quoted 6 lines

I really don't see the point of the "fan_control" module parameter. One can set it to 1 (or true) to enable the pwm_enable attribute, or set it to 0 to disable it. It is effectively the same as just another attribute, forcing users to write two attributes instead of one. That really doesn't make sense.

Ok. I can remove the fan_control parameter entirely and just leave it to the userspace to handle pwm_enable without any other safeguards.

Quoted 11 lines

drivers/platform/x86/oxp-platform.c line 257

Quoted 5 lines
drivers/platform/x86/oxp-platform.c new file +5 -0
+
+     ret = write_to_ec(dev, board->sensors[oxp_sensor_pwm].enable,
+             board->sensors[oxp_sensor_pwm].val_disable);
+
+     return ret;

Just

        return write_to_ec(...);

would do. There is no need for the ret variable. Same elsewhere.

Same as above, so I'll just do this.

Quoted 8 lines

drivers/platform/x86/oxp-platform.c line 263

Quoted 6 lines
drivers/platform/x86/oxp-platform.c new file +6 -0
+}
+
+static int oxp_platform_write(struct device *dev, enum hwmon_sensor_types type,
+             u32 attr, int channel, long val)
+{
+     int ret = -1;

bad error code.

Ok. Will improve on this.

Quoted 19 lines
Quoted 8 lines
drivers/platform/x86/oxp-platform.c new file +8 -0
+     struct oxp_status *state = dev_get_drvdata(dev);
+     const struct ec_board_info *board = &state->board;
+
+     switch(type) {
+             case hwmon_pwm:
+                     if (!fan_control) {
+                             dev_info(dev, "Can't control fans, fan_control=0");
+                             return -EPERM;

drivers/platform/x86/oxp-platform.c line 281

Quoted 10 lines
drivers/platform/x86/oxp-platform.c new file +10 -0
+                     }
+                     switch(attr) {
+                             case hwmon_pwm_enable:
+                                     if (val == 1) {
+                                             ret = oxp_pwm_enable(dev);
+                                     } else if (val == 0) {
+                                             ret = oxp_pwm_disable(dev);
+                                     } else {
+                                             return -EINVAL;
+                                     }
Quoted 3 lines

Unnecessary { }, and the single return on error instead of

                                                ret = -EINVAL;

is inconsistent.

Ok. Will improve on this one.

Quoted 15 lines
Quoted 4 lines
drivers/platform/x86/oxp-platform.c new file +4 -0
+                                     return ret;
+                             case hwmon_pwm_input:
+                                     if (val < 0 || val > 255)
+                                             return -EINVAL;

drivers/platform/x86/oxp-platform.c line 295

Quoted 10 lines
drivers/platform/x86/oxp-platform.c new file +10 -0
+                                     if (board->family == family_mini_amd)
+                                             val = (val * 100) / 255;
+                                     ret = write_to_ec(dev, board->sensors[oxp_sensor_pwm].reg, val);
+                                     return ret;
+                             default:
+                                     dev_dbg(dev, "Unknown attribute for type %d: %d", type, attr);
+                                     return ret;
+                     }
+             default:
+                     dev_dbg(dev, "Unknown sensor type: %d", type);
Quoted 1 line

break missing

Oops... noted.

Quoted 4 lines
Quoted 4 lines
drivers/platform/x86/oxp-platform.c new file +4 -0
+     }
+     return ret;
+}
+
Quoted 8 lines

drivers/platform/x86/oxp-platform.c line 305

Quoted 6 lines
drivers/platform/x86/oxp-platform.c new file +6 -0
+/* Known sensors in the OXP EC controllers */
+static const struct hwmon_channel_info *oxp_platform_sensors[] = {
+     HWMON_CHANNEL_INFO(fan,
+             HWMON_F_INPUT | HWMON_F_MAX | HWMON_F_MIN),
+     HWMON_CHANNEL_INFO(pwm,
+             HWMON_PWM_INPUT | HWMON_PWM_ENABLE),

bad alignment. Please use checkpatch --strict and fix what it reports.

Will do, sorry.

Quoted 33 lines
Quoted 33 lines
drivers/platform/x86/oxp-platform.c new file +33 -0
+     NULL
+};
+
+static const struct hwmon_ops oxp_ec_hwmon_ops = {
+     .is_visible = oxp_ec_hwmon_is_visible,
+     .read = oxp_platform_read,
+     .write = oxp_platform_write,
+};
+
+static const struct hwmon_chip_info oxp_ec_chip_info = {
+     .ops = &oxp_ec_hwmon_ops,
+     .info = oxp_platform_sensors,
+};
+
+/* Initialization logic */
+static const struct ec_board_info * __init get_board_info(struct device *dev)
+{
+     const char *dmi_board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
+     const char *dmi_board_name = dmi_get_system_info(DMI_BOARD_NAME);
+     const struct ec_board_info *board;
+
+     if (!dmi_board_vendor || !dmi_board_name ||
+         (strcasecmp(dmi_board_vendor, "ONE-NETBOOK TECHNOLOGY CO., LTD.") &&
+          strcasecmp(dmi_board_vendor, "ONE-NETBOOK")))
+             return NULL;
+
+     /* Match our known boards */
+     /* Need to check for AMD/Intel here */
+     for (board = board_info; board->sensors; board++) {
+             if (match_string(board->board_names,
+                              MAX_IDENTICAL_BOARD_VARIATIONS,
+                              dmi_board_name) >= 0) {
+                     if (board->family == family_mini_amd &&
Quoted 8 lines

drivers/platform/x86/oxp-platform.c line 344

Quoted 6 lines
drivers/platform/x86/oxp-platform.c new file +6 -0
+                                     boot_cpu_data.x86_vendor == X86_VENDOR_AMD) {
+                             return board;
+                     }
+             }
+     }
+     return NULL;

Why not use a dmi match table for the above code ?

I could use a dmi match table.

Quoted 22 lines
Quoted 22 lines
drivers/platform/x86/oxp-platform.c new file +22 -0
+}
+
+static int __init oxp_platform_probe(struct platform_device *pdev)
+{
+     struct device *dev = &pdev->dev;
+     struct device *hwdev;
+     const struct ec_board_info *pboard_info;
+     struct oxp_status *state;
+
+     pboard_info = get_board_info(dev);
+     if (!pboard_info)
+             return -ENODEV;
+
+     state = devm_kzalloc(dev, sizeof(struct oxp_status), GFP_KERNEL);
+     if (!state)
+             return -ENOMEM;
+
+     state->board = *pboard_info;
+
+     state->lock_data.mutex = 0;
+     state->lock_data.lock = lock_global_acpi_lock;
+     state->lock_data.unlock = unlock_global_acpi_lock;
Quoted 8 lines

drivers/platform/x86/oxp-platform.c line 371

Quoted 5 lines
drivers/platform/x86/oxp-platform.c new file +5 -0
+
+     hwdev = devm_hwmon_device_register_with_info(dev, "oxpec", state,
+                                                     &oxp_ec_chip_info, NULL);
+
+     return PTR_ERR_OR_ZERO(hwdev);

This only configures a hwmon driver and thus should reside in the hwmon subsystem/directory.

For now yes, it only provides hwmon. I'll move to it then.

Quoted 11 lines

drivers/platform/x86/oxp-platform.c line 378

Quoted 7 lines
drivers/platform/x86/oxp-platform.c new file +7 -0
+}
+
+static const struct acpi_device_id acpi_ec_ids[] = {
+     /* Embedded Controller Device */
+     { "PNP0C09", 0 },
+     {}
+};

I am not sure if this works. There are other drivers mapping to the same ACPI ID; those may refuse to load if this driver is in the system. We had problems with this before, so I am very concerned about side effects.

So should I just remove this and go for the DMI match table instead?

Quoted 10 lines
Quoted 10 lines
drivers/platform/x86/oxp-platform.c new file +10 -0
+
+static struct platform_driver oxp_platform_driver = {
+     .driver = {
+             .name   = "oxp-platform",
+             .acpi_match_table = acpi_ec_ids,
+     },
+};
+
+MODULE_DEVICE_TABLE(acpi, acpi_ec_ids);
+module_platform_driver_probe(oxp_platform_driver, oxp_platform_probe);
Quoted 9 lines
Quoted 8 lines
drivers/platform/x86/oxp-platform.c new file +5 -1
+
+MODULE_AUTHOR("Joaquín Ignacio Aramendía <[email protected]>");
+MODULE_DESCRIPTION(
+     "Platform driver that handles ACPI EC of OneXPlayer devices");
+MODULE_LICENSE("GPL");
--

2.38.1

Thanks very much for taking the time.

Signature · 1 line
Joaquín I. Aramendía
maintainer
#3 lore raw
Quoted 9 lines · Joaquin Aramendia

Hello, thanks so much to take the time for the feedback! Maybe this one needs some clarification as for some design choices in regards to some of the structures you see. I know there is only one board supported at the moment, but those structures are meant to be the boilerplate for possibly more boards. I'm aware that these devices are a mess and the EC registers are all over the place. I wanted to make it easier for myself. That said I'll try to address some of the concerns and redo the patcha according to standards :)

Please introduce such details when they are needed, not for a possible which may never happen.

Quoted 10 lines · Guenter Roeck
Quoted 8 lines · Joaquín Ignacio Aramendía

drivers/platform/x86/oxp-platform.c line 30

Quoted 3 lines
drivers/platform/x86/oxp-platform.c new file +3 -0
+
+static bool fan_control;
+module_param_hw(fan_control, bool, other, 0644);

Runtime writeable parameter is unacceptable. Why would this be needed anyway ? What is it supposed to accomplish that can not be done with a sysfs attribute ?

Quoted 7 lines

Is meant for safety, I suppose, but you are right that it is better to have the parameter non-writable at runtime.
The goal is to be able to use the fan readings without allowing the pwm to be used unless you really know what you are doing, but I guess there is no point in having this if already is the pwm_enable attribute.

Exactly. I do not see the pont of the attribute in the first place since it just adds complexity and duplicates pwm_enable.

Quoted 42 lines
Quoted 11 lines
Quoted 11 lines
drivers/platform/x86/oxp-platform.c new file +11 -0
+struct oxp_ec_sensor_addr {
+     enum hwmon_sensor_types type;
+     u8 reg;
+     short size;
+     union {
+             struct {
+                     u8 enable;
+                     u8 val_enable;
+                     u8 val_disable;
+             };
+             struct {
Quoted 8 lines

drivers/platform/x86/oxp-platform.c line 82

Quoted 6 lines
drivers/platform/x86/oxp-platform.c new file +6 -0
+               int max_speed;
+             };
+     };
+};
+
+

Extra empty line.

Removed. Thanks.

Quoted 11 lines
Quoted 11 lines
drivers/platform/x86/oxp-platform.c new file +11 -0
+/* AMD board EC addresses */
+static const struct oxp_ec_sensor_addr amd_sensors[] = {
+     [oxp_sensor_fan] = {
+             .type = hwmon_fan,
+             .reg = 0x76,
+             .size = 2,
+             .max_speed = 5000,
+     },
+     [oxp_sensor_pwm] = {
+             .type = hwmon_pwm,
+             .reg = 0x4B,
Quoted 8 lines

drivers/platform/x86/oxp-platform.c line 98

Quoted 5 lines
drivers/platform/x86/oxp-platform.c new file +5 -0
+             .size = 1,
+             .enable = 0x4A,
+             .val_enable = 0x01,
+             .val_disable = 0x00,
+     },

I don't see the point of this data structure. There is just one entry. Why not use defines ?

Quoted 5 lines

It is one entry now, but i figured in a while there will be other boards to support with different values. Having this structure seems easier for future updates.
I can remove it and only use defines for now.

Please do.

Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
drivers/platform/x86/oxp-platform.c new file +9 -0
+     {},
+};
+
+struct ec_board_info {
+     const char *board_names[MAX_IDENTICAL_BOARD_VARIATIONS];
+     enum board_family family;
+     const struct oxp_ec_sensor_addr *sensors;
+};
+
Quoted 8 lines

drivers/platform/x86/oxp-platform.c line 113

Quoted 6 lines
drivers/platform/x86/oxp-platform.c new file +6 -0
+static const struct ec_board_info board_info[] = {
+     {
+             .board_names = {"ONE XPLAYER", "ONEXPLAYER mini A07"},
+             .family = family_mini_amd,
+             .sensors = amd_sensors,
+     },

There is just one family. What is the point of this data structure ?

Quoted 7 lines

It is meant for expansion on other boards. I only own a OXP mini AMD, but others exist with their own quirks. For example, the same device but with Intel CPU has completely different EC registers, values and ranges.
I guess i can remove the whole "family" thing and bring it back when it is appropriate.

Without knowing those boards it may well be that a separate driver would be appropriate, and/or that the here introduced flexibility is insufficient. It does not make sense to introduce such complexity unless it is known that it is needed, and that it meets the requirements of additional boards.

Quoted 69 lines
Quoted 15 lines
Quoted 15 lines
drivers/platform/x86/oxp-platform.c new file +15 -0
+     {}
+};
+
+struct oxp_status {
+     struct ec_board_info board;
+     struct lock_data lock_data;
+};
+
+/* Helper functions */
+static int read_from_ec(u8 reg, int size, long *val)
+{
+     int i;
+     int ret;
+     u8 buffer;
+
Quoted 8 lines

drivers/platform/x86/oxp-platform.c line 134

Quoted 6 lines
drivers/platform/x86/oxp-platform.c new file +6 -0
+     *val = 0;
+     for (i = 0; i < size; i++) {
+             ret = ec_read(reg + i, &buffer);
+             if (ret)
+                     return ret;
+             (*val) <<= i*8;

space between i and 8

Ok. Will remove.

Quoted 6 lines

drivers/platform/x86/oxp-platform.c line 137

Quoted 3 lines
drivers/platform/x86/oxp-platform.c new file +3 -0
+             *val += buffer;
+     }
+     return ret;
         return 0;
Quoted 8 lines

drivers/platform/x86/oxp-platform.c line 143

Quoted 6 lines
drivers/platform/x86/oxp-platform.c new file +6 -0
+}
+
+static int write_to_ec(const struct device *dev, u8 reg, u8 value)
+{
+     struct oxp_status *state = dev_get_drvdata(dev);
+     int ret = -1;

unnecessary (and bad) variable initialization

Ok. Will improve on this.

Quoted 17 lines
Quoted 17 lines
drivers/platform/x86/oxp-platform.c new file +17 -0
+
+     if (!state->lock_data.lock(&state->lock_data)) {
+             dev_warn(dev, "Failed to acquire mutex");
+             return -EBUSY;
+     }
+
+     ret = ec_write(reg, value);
+
+     if (!state->lock_data.unlock(&state->lock_data))
+             dev_err(dev, "Failed to release mutex");
+
+     return ret;
+}
+
+/* Callbacks for hwmon interface */
+static umode_t oxp_ec_hwmon_is_visible(const void *drvdata,
+                                     enum hwmon_sensor_types type, u32 attr, int channel)
Quoted 8 lines

drivers/platform/x86/oxp-platform.c line 166

Quoted 6 lines
drivers/platform/x86/oxp-platform.c new file +6 -0
+{
+     switch (type) {
+             case hwmon_fan:
+                     return S_IRUGO;
+             case hwmon_pwm:
+                     return S_IRUGO | S_IWUSR;

Please use 0444 and 0644 directly. Checkpatch will tell.

Quoted 3 lines

Oh. I did as the documentation suggested. I must confess I didn't run checkpatch, will don in the next submission.

That is long ago. Octal values are and have been preferred for several years.

Quoted 36 lines
Quoted 10 lines

drivers/platform/x86/oxp-platform.c line 174

Quoted 8 lines
drivers/platform/x86/oxp-platform.c new file +8 -0
+             default:
+                     return 0;
+     }
+     return 0;
+}
+
+static int oxp_platform_read(struct device *dev, enum hwmon_sensor_types type,
+             u32 attr, int channel, long *val)

Align continuation line with '('. Checkpatch will tell.

Will do. I guess my vim settings are messed up.

Quoted 14 lines
Quoted 14 lines
drivers/platform/x86/oxp-platform.c new file +14 -0
+{
+     int ret = -1;
+     const struct oxp_status *state = dev_get_drvdata(dev);
+     const struct ec_board_info *board = &state->board;
+
+     switch(type) {
+             case hwmon_fan:
+                     switch(attr) {
+                             case hwmon_fan_input:
+                                     ret = read_from_ec(board->sensors[oxp_sensor_fan].reg,
+                                                     board->sensors[oxp_sensor_fan].size, val);
+                                     break;
+                             case hwmon_fan_max:
+                                     ret = 0;
Quoted 8 lines

drivers/platform/x86/oxp-platform.c line 194

Quoted 6 lines
drivers/platform/x86/oxp-platform.c new file +6 -0
+                                     *val = board->sensors[oxp_sensor_fan].max_speed;
+                                     break;
+                             case hwmon_fan_min:
+                                     ret = 0;
+                                     *val = 0;
+                                     break;

If fan_max and fan_min are not sent to/from the EC, do not provide the attributes.

Quoted 4 lines

They are not, but they are in the spec of the fan (in the attached sticker, that is). Should I keep it if it's not read directly but has a known value?

No. That is not the purpose of hwmon sysfs attributes.

Quoted 31 lines
Quoted 4 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+                             default:
+                                     dev_dbg(dev, "Unknown attribute for type %d: %d\n", type, attr);

missing break;

Ooops, sorry. Will improve on this one.

Quoted 19 lines
Quoted 8 lines
drivers/platform/x86/oxp-platform.c new file +8 -0
+                     }
+                     return ret;
+             case hwmon_pwm:
+                     switch(attr) {
+                             case hwmon_pwm_input:
+                                     ret = read_from_ec(board->sensors[oxp_sensor_pwm].reg,
+                                                     board->sensors[oxp_sensor_pwm].size, val);
+                                     if (board->family == family_mini_amd) {

drivers/platform/x86/oxp-platform.c line 214

Quoted 10 lines
drivers/platform/x86/oxp-platform.c new file +10 -0
+                                             *val = (*val * 255) / 100;
+                                     }
+                                     break;
+                             case hwmon_pwm_enable:
+                                     ret = read_from_ec(board->sensors[oxp_sensor_pwm].enable, 1, val);
+                                     if (*val == board->sensors[oxp_sensor_pwm].val_enable) {
+                                             *val = 1;
+                                     } else {
+                                             *val = 0;
+                                     }
Quoted 6 lines

Unnecessary { }. Checkpatch would tell.

I don't see the point of this code. Why have board->sensors[oxp_sensor_pwm].val_enable to start with ? It is 1. Can the EC return something else ? If so, what is the rationale to map it to 0 ?

Quoted 6 lines

The enable/disable values are configurable, since they can vary from board to board. AMD happens to be just 1 and 0, so it fits in this case. My goal is to map them to 1 and 0 to be exposed in the same way across all devices. That way userspace tools like fancontrold can use this interface as is.

Again, that is not known at this time. Unless there are such boards it is impossible to predict how they are programmed, and if that programming even remotely fits into the scheme used by this driver.

Quoted 13 lines
Quoted 5 lines

drivers/platform/x86/oxp-platform.c line 196

Quoted 3 lines
drivers/platform/x86/oxp-platform.c new file +3 -0
+                                     break;
+                             default:
+                                     dev_dbg(dev, "Unknown attribute for type %d: %d\n", type, attr);

missing break;

Oops, will correct. Thanks

Quoted 6 lines
Quoted 5 lines
drivers/platform/x86/oxp-platform.c new file +5 -0
+                     }
+                     return ret;
+             default:
+                     dev_dbg(dev, "Unknown sensor type %d.\n", type);
+                     return -1;
Quoted 4 lines
Quoted 1 line

bad error code.

Should I return EINVAL in this case? Seems appropriate reading error code headers.

-EOPNOTSUPP

Quoted 176 lines
Quoted 8 lines

drivers/platform/x86/oxp-platform.c line 228

Quoted 6 lines
drivers/platform/x86/oxp-platform.c new file +6 -0
+     }
+}
+
+static int oxp_pwm_enable(const struct device *dev)
+{
+     int ret = -1;

Unnecessary (and bad) variable initialization.

Ok. Will improve.

Quoted 6 lines
Quoted 6 lines
drivers/platform/x86/oxp-platform.c new file +6 -0
+     struct oxp_status *state = dev_get_drvdata(dev);
+     const struct ec_board_info *board = &state->board;
+
+     if (!fan_control) {
+             dev_info(dev, "Can't enable pwm, fan_control=0");
+             return -EPERM;
Quoted 8 lines

drivers/platform/x86/oxp-platform.c line 240

Quoted 6 lines
drivers/platform/x86/oxp-platform.c new file +6 -0
+     }
+
+     ret = write_to_ec(dev, board->sensors[oxp_sensor_pwm].enable,
+             board->sensors[oxp_sensor_pwm].val_enable);
+
+     return ret;

... and unnecessary variable.

Then I would just do
return write_to_ec():
as stated below.

Quoted 7 lines

drivers/platform/x86/oxp-platform.c line 245

Quoted 5 lines
drivers/platform/x86/oxp-platform.c new file +5 -0
+}
+
+static int oxp_pwm_disable(const struct device *dev)
+{
+     int ret = -1;

Unnecessary initialization, and bad error code.

Seems like I really like to mess this up, really... Sorry.

Quoted 8 lines

drivers/platform/x86/oxp-platform.c line 252

Quoted 7 lines
drivers/platform/x86/oxp-platform.c new file +7 -0
+     struct oxp_status *state = dev_get_drvdata(dev);
+     const struct ec_board_info *board = &state->board;
+
+     if (!fan_control) {
+             dev_info(dev, "Can't disable pwm, fan_control=0");
+             return -EPERM;
+     }
Quoted 6 lines

I really don't see the point of the "fan_control" module parameter. One can set it to 1 (or true) to enable the pwm_enable attribute, or set it to 0 to disable it. It is effectively the same as just another attribute, forcing users to write two attributes instead of one. That really doesn't make sense.

Ok. I can remove the fan_control parameter entirely and just leave it to the userspace to handle pwm_enable without any other safeguards.

Quoted 11 lines

drivers/platform/x86/oxp-platform.c line 257

Quoted 5 lines
drivers/platform/x86/oxp-platform.c new file +5 -0
+
+     ret = write_to_ec(dev, board->sensors[oxp_sensor_pwm].enable,
+             board->sensors[oxp_sensor_pwm].val_disable);
+
+     return ret;

Just

         return write_to_ec(...);

would do. There is no need for the ret variable. Same elsewhere.

Same as above, so I'll just do this.

Quoted 8 lines

drivers/platform/x86/oxp-platform.c line 263

Quoted 6 lines
drivers/platform/x86/oxp-platform.c new file +6 -0
+}
+
+static int oxp_platform_write(struct device *dev, enum hwmon_sensor_types type,
+             u32 attr, int channel, long val)
+{
+     int ret = -1;

bad error code.

Ok. Will improve on this.

Quoted 19 lines
Quoted 8 lines
drivers/platform/x86/oxp-platform.c new file +8 -0
+     struct oxp_status *state = dev_get_drvdata(dev);
+     const struct ec_board_info *board = &state->board;
+
+     switch(type) {
+             case hwmon_pwm:
+                     if (!fan_control) {
+                             dev_info(dev, "Can't control fans, fan_control=0");
+                             return -EPERM;

drivers/platform/x86/oxp-platform.c line 281

Quoted 10 lines
drivers/platform/x86/oxp-platform.c new file +10 -0
+                     }
+                     switch(attr) {
+                             case hwmon_pwm_enable:
+                                     if (val == 1) {
+                                             ret = oxp_pwm_enable(dev);
+                                     } else if (val == 0) {
+                                             ret = oxp_pwm_disable(dev);
+                                     } else {
+                                             return -EINVAL;
+                                     }
Quoted 3 lines

Unnecessary { }, and the single return on error instead of

                                                 ret = -EINVAL;

is inconsistent.

Ok. Will improve on this one.

Quoted 15 lines
Quoted 4 lines
drivers/platform/x86/oxp-platform.c new file +4 -0
+                                     return ret;
+                             case hwmon_pwm_input:
+                                     if (val < 0 || val > 255)
+                                             return -EINVAL;

drivers/platform/x86/oxp-platform.c line 295

Quoted 10 lines
drivers/platform/x86/oxp-platform.c new file +10 -0
+                                     if (board->family == family_mini_amd)
+                                             val = (val * 100) / 255;
+                                     ret = write_to_ec(dev, board->sensors[oxp_sensor_pwm].reg, val);
+                                     return ret;
+                             default:
+                                     dev_dbg(dev, "Unknown attribute for type %d: %d", type, attr);
+                                     return ret;
+                     }
+             default:
+                     dev_dbg(dev, "Unknown sensor type: %d", type);
Quoted 1 line

break missing

Oops... noted.

Quoted 4 lines
Quoted 4 lines
drivers/platform/x86/oxp-platform.c new file +4 -0
+     }
+     return ret;
+}
+
Quoted 8 lines

drivers/platform/x86/oxp-platform.c line 305

Quoted 6 lines
drivers/platform/x86/oxp-platform.c new file +6 -0
+/* Known sensors in the OXP EC controllers */
+static const struct hwmon_channel_info *oxp_platform_sensors[] = {
+     HWMON_CHANNEL_INFO(fan,
+             HWMON_F_INPUT | HWMON_F_MAX | HWMON_F_MIN),
+     HWMON_CHANNEL_INFO(pwm,
+             HWMON_PWM_INPUT | HWMON_PWM_ENABLE),

bad alignment. Please use checkpatch --strict and fix what it reports.

Will do, sorry.

Quoted 33 lines
Quoted 33 lines
drivers/platform/x86/oxp-platform.c new file +33 -0
+     NULL
+};
+
+static const struct hwmon_ops oxp_ec_hwmon_ops = {
+     .is_visible = oxp_ec_hwmon_is_visible,
+     .read = oxp_platform_read,
+     .write = oxp_platform_write,
+};
+
+static const struct hwmon_chip_info oxp_ec_chip_info = {
+     .ops = &oxp_ec_hwmon_ops,
+     .info = oxp_platform_sensors,
+};
+
+/* Initialization logic */
+static const struct ec_board_info * __init get_board_info(struct device *dev)
+{
+     const char *dmi_board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
+     const char *dmi_board_name = dmi_get_system_info(DMI_BOARD_NAME);
+     const struct ec_board_info *board;
+
+     if (!dmi_board_vendor || !dmi_board_name ||
+         (strcasecmp(dmi_board_vendor, "ONE-NETBOOK TECHNOLOGY CO., LTD.") &&
+          strcasecmp(dmi_board_vendor, "ONE-NETBOOK")))
+             return NULL;
+
+     /* Match our known boards */
+     /* Need to check for AMD/Intel here */
+     for (board = board_info; board->sensors; board++) {
+             if (match_string(board->board_names,
+                              MAX_IDENTICAL_BOARD_VARIATIONS,
+                              dmi_board_name) >= 0) {
+                     if (board->family == family_mini_amd &&
Quoted 8 lines

drivers/platform/x86/oxp-platform.c line 344

Quoted 6 lines
drivers/platform/x86/oxp-platform.c new file +6 -0
+                                     boot_cpu_data.x86_vendor == X86_VENDOR_AMD) {
+                             return board;
+                     }
+             }
+     }
+     return NULL;

Why not use a dmi match table for the above code ?

Quoted 2 lines

I could use a dmi match table.

Please do.

Quoted 45 lines
Quoted 22 lines
Quoted 22 lines
drivers/platform/x86/oxp-platform.c new file +22 -0
+}
+
+static int __init oxp_platform_probe(struct platform_device *pdev)
+{
+     struct device *dev = &pdev->dev;
+     struct device *hwdev;
+     const struct ec_board_info *pboard_info;
+     struct oxp_status *state;
+
+     pboard_info = get_board_info(dev);
+     if (!pboard_info)
+             return -ENODEV;
+
+     state = devm_kzalloc(dev, sizeof(struct oxp_status), GFP_KERNEL);
+     if (!state)
+             return -ENOMEM;
+
+     state->board = *pboard_info;
+
+     state->lock_data.mutex = 0;
+     state->lock_data.lock = lock_global_acpi_lock;
+     state->lock_data.unlock = unlock_global_acpi_lock;
Quoted 8 lines

drivers/platform/x86/oxp-platform.c line 371

Quoted 5 lines
drivers/platform/x86/oxp-platform.c new file +5 -0
+
+     hwdev = devm_hwmon_device_register_with_info(dev, "oxpec", state,
+                                                     &oxp_ec_chip_info, NULL);
+
+     return PTR_ERR_OR_ZERO(hwdev);

This only configures a hwmon driver and thus should reside in the hwmon subsystem/directory.

For now yes, it only provides hwmon. I'll move to it then.

Quoted 11 lines

drivers/platform/x86/oxp-platform.c line 378

Quoted 7 lines
drivers/platform/x86/oxp-platform.c new file +7 -0
+}
+
+static const struct acpi_device_id acpi_ec_ids[] = {
+     /* Embedded Controller Device */
+     { "PNP0C09", 0 },
+     {}
+};

I am not sure if this works. There are other drivers mapping to the same ACPI ID; those may refuse to load if this driver is in the system. We had problems with this before, so I am very concerned about side effects.

Quoted 2 lines

So should I just remove this and go for the DMI match table instead?

Yes, and a platform driver. Look at drivers/hwmon/asus-ec-sensors.c and its history (specifically commit 88700d1396b) for problems with PNP0C09.

Thanks,
Guenter

#4 lore raw

PATCH v2 1/1 Add OneXPlayer mini AMD sensors driver

Sensors driver for OXP Handhelds from One-Netbook that expose fan reading and control via hwmon sysfs.

As far as I could gather all OXP boards have the same DMI strings and they are told appart by the boot cpu vendor (Intel/AMD). Currently only AMD boards are supported.

Fan control is provided via pwm interface in the range [0-255]. AMD boards have [0-100] as range in the EC, the written value is scaled to accommodate for that.

---
Rewritten the driver according to feedback, checkpatch passes, removed unnecessary complexity and moved the driver to hwmon

---
 drivers/hwmon/Kconfig       |  13 +-
 drivers/hwmon/Makefile      |   1 +
 drivers/hwmon/oxp-sensors.c | 278 ++++++++++++++++++++++++++++++++++++
 3 files changed, 291 insertions(+), 1 deletion(-)
 create mode 100644 drivers/hwmon/oxp-sensors.c
drivers/hwmon/Kconfig +12 -1
index 7ac3daaf59ce..baa3e43b52b6 100644
@@ -1607,6 +1607,17 @@ config SENSORS_NZXT_SMART2
 
 source "drivers/hwmon/occ/Kconfig"
 
+config SENSORS_OXP
+	tristate "OneXPlayer EC fan control"
+	depends on ACPI
+	depends on X86
+	help
+		If you say yes here you get support for fan readings and control over
+		OneXPlayer handheld devices. Only OneXPlayer mini AMD handheld variant
+		boards are supported.
+
+		Can also be built as a module. In that case it will be called oxp_sensors.
+
 config SENSORS_PCF8591
 	tristate "Philips PCF8591 ADC/DAC"
 	depends on I2C
@@ -1957,7 +1968,7 @@ config SENSORS_ADS7871
 
 config SENSORS_AMC6821
 	tristate "Texas Instruments AMC6821"
-	depends on I2C
+	depends on I2C
 	help
 	  If you say yes here you get support for the Texas Instruments
 	  AMC6821 hardware monitoring chips.
drivers/hwmon/Makefile +1 -0
index 11d076cad8a2..35824f8be455 100644
@@ -167,6 +167,7 @@ obj-$(CONFIG_SENSORS_NSA320)	+= nsa320-hwmon.o
 obj-$(CONFIG_SENSORS_NTC_THERMISTOR)	+= ntc_thermistor.o
 obj-$(CONFIG_SENSORS_NZXT_KRAKEN2) += nzxt-kraken2.o
 obj-$(CONFIG_SENSORS_NZXT_SMART2) += nzxt-smart2.o
+obj-$(CONFIG_SENSORS_OXP) += oxp-sensors.o
 obj-$(CONFIG_SENSORS_PC87360)	+= pc87360.o
 obj-$(CONFIG_SENSORS_PC87427)	+= pc87427.o
 obj-$(CONFIG_SENSORS_PCF8591)	+= pcf8591.o
drivers/hwmon/oxp-sensors.c new file +278 -0

278 lines not on this page · raw message

--
2.38.1

#5 lore raw

Re: [PATCH v2] Add OneXPlayer mini AMD sensors driver

Hi

2022. október 30., vasárnap 21:32 keltezéssel, Joaquín Ignacio Aramendía írta:

Quoted 33 lines

Sensors driver for OXP Handhelds from One-Netbook that expose fan reading and control via hwmon sysfs.

As far as I could gather all OXP boards have the same DMI strings and they are told appart by the boot cpu vendor (Intel/AMD). Currently only AMD boards are supported.

Fan control is provided via pwm interface in the range [0-255]. AMD boards have [0-100] as range in the EC, the written value is scaled to accommodate for that.

---
Rewritten the driver according to feedback, checkpatch passes, removed unnecessary complexity and moved the driver to hwmon

---
 drivers/hwmon/Kconfig       |  13 +-
 drivers/hwmon/Makefile      |   1 +
 drivers/hwmon/oxp-sensors.c | 278 ++++++++++++++++++++++++++++++++++++
 3 files changed, 291 insertions(+), 1 deletion(-)
 create mode 100644 drivers/hwmon/oxp-sensors.c

[...]

drivers/hwmon/oxp-sensors.c new file +4 -0
new file mode 100644
index 000000000000..128fdf4c46e2
@@ -0,0 +1,278 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Platform driver for OXP Handhelds that expose fan reading and control
+ * via hwmon sysfs.

drivers/hwmon/oxp-sensors.c line 14

Quoted 10 lines
drivers/hwmon/oxp-sensors.c new file +10 -0
+ *
+ * All boards have the same DMI strings and they are told appart by the
+ * boot cpu vendor (Intel/AMD). Currently only AMD boards are supported
+ * but the code is made to be simple to add other handheld boards in the
+ * future.
+ * Fan control is provided via pwm interface in the range [0-255]. AMD
+ * boards use [0-100] as range in the EC, the written value is scaled to
+ * accommodate for that.
+ *
+ * PWM control is disabled by default, can be enabled via module parameter.

As far as I can see this is not true anymore.

Also, have you checked if there is maybe a WMI interface for this?

Quoted 1 line

[...]

Regards,
Barnabás Pőcze

#6 lore raw

Re: [PATCH v2] Add OneXPlayer mini AMD sensors driver

Quoted 43 lines · Barnabás Pőcze

Hi

2022. október 30., vasárnap 21:32 keltezéssel, Joaquín Ignacio Aramendía írta:

Quoted 37 lines

Sensors driver for OXP Handhelds from One-Netbook that expose fan reading and control via hwmon sysfs.

As far as I could gather all OXP boards have the same DMI strings and they are told appart by the boot cpu vendor (Intel/AMD). Currently only AMD boards are supported.

Fan control is provided via pwm interface in the range [0-255]. AMD boards have [0-100] as range in the EC, the written value is scaled to accommodate for that.

---
Rewritten the driver according to feedback, checkpatch passes, removed unnecessary complexity and moved the driver to hwmon

---
 drivers/hwmon/Kconfig       |  13 +-
 drivers/hwmon/Makefile      |   1 +
 drivers/hwmon/oxp-sensors.c | 278 ++++++++++++++++++++++++++++++++++++
 3 files changed, 291 insertions(+), 1 deletion(-)
 create mode 100644 drivers/hwmon/oxp-sensors.c

[...]

drivers/hwmon/oxp-sensors.c new file +8 -0
new file mode 100644
index 000000000000..128fdf4c46e2
@@ -0,0 +1,278 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Platform driver for OXP Handhelds that expose fan reading and control
+ * via hwmon sysfs.
+ *
+ * All boards have the same DMI strings and they are told appart by the
+ * boot cpu vendor (Intel/AMD). Currently only AMD boards are supported
+ * but the code is made to be simple to add other handheld boards in the
Quoted 8 lines

drivers/hwmon/oxp-sensors.c line 14

Quoted 6 lines
drivers/hwmon/oxp-sensors.c new file +6 -0
+ * future.
+ * Fan control is provided via pwm interface in the range [0-255]. AMD
+ * boards use [0-100] as range in the EC, the written value is scaled to
+ * accommodate for that.
+ *
+ * PWM control is disabled by default, can be enabled via module parameter.

As far as I can see this is not true anymore.

True. Will remove that. Also I found a typo on my registers that needs to be fixed

Quoted 2 lines

Also, have you checked if there is maybe a WMI interface for this?

There is not as far as I can tell. Even on Windows the fan control app uses EC registers directly.

Signature · 1 line
Joaquín I. Aramendía
#7 lore raw

PATCH v3 1/1 Add OneXPlayer mini AMD sensors driver

In reply to Barnabás Pőcze

Sensors driver for OXP Handhelds from One-Netbook that expose fan reading and control via hwmon sysfs.

As far as I could gather all OXP boards have the same DMI strings and they are told appart by the boot cpu vendor (Intel/AMD). Currently only AMD boards are supported.

Fan control is provided via pwm interface in the range [0-255]. AMD boards have [0-100] as range in the EC, the written value is scaled to accommodate for that.

---
Removed fan_control reference in comment.
Bugfix MIX/MIN reporting not available
Bugfix pwm_enable register set wrong

---
 drivers/hwmon/Kconfig       |  13 +-
 drivers/hwmon/Makefile      |   1 +
 drivers/hwmon/oxp-sensors.c | 277 ++++++++++++++++++++++++++++++++++++
 3 files changed, 290 insertions(+), 1 deletion(-)
 create mode 100644 drivers/hwmon/oxp-sensors.c
drivers/hwmon/Kconfig +12 -1
index 7ac3daaf59ce..a1cdb03b4d13 100644
@@ -1607,6 +1607,17 @@ config SENSORS_NZXT_SMART2
 
 source "drivers/hwmon/occ/Kconfig"
 
+config SENSORS_OXP
+	tristate "OneXPlayer EC fan control"
+	depends on ACPI
+	depends on X86
+	help
+		If you say yes here you get support for fan readings and control over
+		OneXPlayer handheld devices. Only OneXPlayer mini AMD handheld variant
+		boards are supported.
+
+		Can also be built as a module. In that case it will be called oxp-sensors.
+
 config SENSORS_PCF8591
 	tristate "Philips PCF8591 ADC/DAC"
 	depends on I2C
@@ -1957,7 +1968,7 @@ config SENSORS_ADS7871
 
 config SENSORS_AMC6821
 	tristate "Texas Instruments AMC6821"
-	depends on I2C
+	depends on I2C
 	help
 	  If you say yes here you get support for the Texas Instruments
 	  AMC6821 hardware monitoring chips.
drivers/hwmon/Makefile +1 -0
index 11d076cad8a2..35824f8be455 100644
@@ -167,6 +167,7 @@ obj-$(CONFIG_SENSORS_NSA320)	+= nsa320-hwmon.o
 obj-$(CONFIG_SENSORS_NTC_THERMISTOR)	+= ntc_thermistor.o
 obj-$(CONFIG_SENSORS_NZXT_KRAKEN2) += nzxt-kraken2.o
 obj-$(CONFIG_SENSORS_NZXT_SMART2) += nzxt-smart2.o
+obj-$(CONFIG_SENSORS_OXP) += oxp-sensors.o
 obj-$(CONFIG_SENSORS_PC87360)	+= pc87360.o
 obj-$(CONFIG_SENSORS_PC87427)	+= pc87427.o
 obj-$(CONFIG_SENSORS_PCF8591)	+= pcf8591.o
drivers/hwmon/oxp-sensors.c new file +277 -0

277 lines not on this page · raw message

--
2.38.1

maintainer
#8 lore raw

Re: [PATCH v3] Add OneXPlayer mini AMD sensors driver

Quoted 7 lines · Joaquín Ignacio Aramendía

Sensors driver for OXP Handhelds from One-Netbook that expose fan reading and control via hwmon sysfs.

As far as I could gather all OXP boards have the same DMI strings and they are told appart by the boot cpu vendor (Intel/AMD). Currently only AMD boards are supported.

Quoted 5 lines

Fan control is provided via pwm interface in the range [0-255]. AMD boards have [0-100] as range in the EC, the written value is scaled to accommodate for that.

I didn't have time to look at your patch, but _pease_ stop sending new versions of your patches in response to previous versions.

Guenter

Quoted 340 lines

---
Removed fan_control reference in comment.
Bugfix MIX/MIN reporting not available
Bugfix pwm_enable register set wrong

---
 drivers/hwmon/Kconfig       |  13 +-
 drivers/hwmon/Makefile      |   1 +
 drivers/hwmon/oxp-sensors.c | 277 ++++++++++++++++++++++++++++++++++++
 3 files changed, 290 insertions(+), 1 deletion(-)
 create mode 100644 drivers/hwmon/oxp-sensors.c
drivers/hwmon/Kconfig +12 -1
index 7ac3daaf59ce..a1cdb03b4d13 100644
@@ -1607,6 +1607,17 @@ config SENSORS_NZXT_SMART2
 
 source "drivers/hwmon/occ/Kconfig"
 
+config SENSORS_OXP
+	tristate "OneXPlayer EC fan control"
+	depends on ACPI
+	depends on X86
+	help
+		If you say yes here you get support for fan readings and control over
+		OneXPlayer handheld devices. Only OneXPlayer mini AMD handheld variant
+		boards are supported.
+
+		Can also be built as a module. In that case it will be called oxp-sensors.
+
 config SENSORS_PCF8591
 	tristate "Philips PCF8591 ADC/DAC"
 	depends on I2C
@@ -1957,7 +1968,7 @@ config SENSORS_ADS7871
 
 config SENSORS_AMC6821
 	tristate "Texas Instruments AMC6821"
-	depends on I2C
+	depends on I2C
 	help
 	  If you say yes here you get support for the Texas Instruments
 	  AMC6821 hardware monitoring chips.
drivers/hwmon/Makefile +1 -0
index 11d076cad8a2..35824f8be455 100644
@@ -167,6 +167,7 @@ obj-$(CONFIG_SENSORS_NSA320)	+= nsa320-hwmon.o
 obj-$(CONFIG_SENSORS_NTC_THERMISTOR)	+= ntc_thermistor.o
 obj-$(CONFIG_SENSORS_NZXT_KRAKEN2) += nzxt-kraken2.o
 obj-$(CONFIG_SENSORS_NZXT_SMART2) += nzxt-smart2.o
+obj-$(CONFIG_SENSORS_OXP) += oxp-sensors.o
 obj-$(CONFIG_SENSORS_PC87360)	+= pc87360.o
 obj-$(CONFIG_SENSORS_PC87427)	+= pc87427.o
 obj-$(CONFIG_SENSORS_PCF8591)	+= pcf8591.o
drivers/hwmon/oxp-sensors.c new file +277 -1
new file mode 100644
index 000000000000..f5895dc11094
@@ -0,0 +1,277 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Platform driver for OXP Handhelds that expose fan reading and control
+ * via hwmon sysfs.
+ *
+ * All boards have the same DMI strings and they are told appart by the
+ * boot cpu vendor (Intel/AMD). Currently only AMD boards are supported
+ * but the code is made to be simple to add other handheld boards in the
+ * future.
+ * Fan control is provided via pwm interface in the range [0-255]. AMD
+ * boards use [0-100] as range in the EC, the written value is scaled to
+ * accommodate for that.
+ *
+ * Copyright (C) 2022 Joaquín I. Aramendía <[email protected]>
+ */
+
+#include <linux/acpi.h>
+#include <linux/dev_printk.h>
+#include <linux/dmi.h>
+#include <linux/hwmon.h>
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/processor.h>
+
+#define ACPI_LOCK_DELAY_MS	500
+
+/* Handle ACPI lock mechanism */
+struct lock_data {
+	u32 mutex;
+	bool (*lock)(struct lock_data *data);
+	bool (*unlock)(struct lock_data *data);
+};
+
+static bool lock_global_acpi_lock(struct lock_data *data)
+{
+	return ACPI_SUCCESS(acpi_acquire_global_lock(ACPI_LOCK_DELAY_MS,
+								 &data->mutex));
+}
+
+static bool unlock_global_acpi_lock(struct lock_data *data)
+{
+	return ACPI_SUCCESS(acpi_release_global_lock(data->mutex));
+}
+
+#define OXP_SENSOR_FAN_REG		0x76 /* Fan reading is 2 registers long */
+#define OXP_SENSOR_PWM_ENABLE_REG	0x4A /* PWM enable is 1 register long */
+#define OXP_SENSOR_PWM_REG		0x4B /* PWM reading is 1 register long */
+
+static const struct dmi_system_id dmi_table[] = {
+	{
+		.matches = {
+			DMI_EXACT_MATCH(DMI_BOARD_VENDOR,
+					"ONE-NETBOOK TECHNOLOGY CO., LTD."),
+		},
+	},
+	{
+		.matches = {
+			DMI_EXACT_MATCH(DMI_BOARD_VENDOR,
+					"ONE-NETBOOK"),
+		},
+	},
+	{},
+};
+
+struct oxp_status {
+	struct lock_data lock_data;
+};
+
+/* Helper functions to handle EC read/write */
+static int read_from_ec(u8 reg, int size, long *val)
+{
+	int i;
+	int ret;
+	u8 buffer;
+
+	*val = 0;
+	for (i = 0; i < size; i++) {
+		ret = ec_read(reg + i, &buffer);
+		if (ret)
+			return ret;
+		(*val) <<= i * 8;
+		*val += buffer;
+	}
+	return ret;
+}
+
+static int write_to_ec(const struct device *dev, u8 reg, u8 value)
+{
+	struct oxp_status *state = dev_get_drvdata(dev);
+	int ret;
+
+	if (!state->lock_data.lock(&state->lock_data)) {
+		dev_warn(dev, "Failed to acquire mutex");
+		return -EBUSY;
+	}
+
+	ret = ec_write(reg, value);
+
+	if (!state->lock_data.unlock(&state->lock_data))
+		dev_err(dev, "Failed to release mutex");
+
+	return ret;
+}
+
+static int oxp_pwm_enable(const struct device *dev)
+{
+	return write_to_ec(dev, OXP_SENSOR_PWM_ENABLE_REG, 0x01);
+}
+
+static int oxp_pwm_disable(const struct device *dev)
+{
+	return write_to_ec(dev, OXP_SENSOR_PWM_ENABLE_REG, 0x00);
+}
+
+/* Callbacks for hwmon interface */
+static umode_t oxp_ec_hwmon_is_visible(const void *drvdata,
+					enum hwmon_sensor_types type, u32 attr, int channel)
+{
+	switch (type) {
+	case hwmon_fan:
+		return 0444;
+	case hwmon_pwm:
+		return 0644;
+	default:
+		return 0;
+	}
+	return 0;
+}
+
+static int oxp_platform_read(struct device *dev, enum hwmon_sensor_types type,
+			     u32 attr, int channel, long *val)
+{
+	int ret;
+
+	switch (type) {
+	case hwmon_fan:
+		switch (attr) {
+		case hwmon_fan_input:
+			return read_from_ec(OXP_SENSOR_FAN_REG,
+					   2,
+					   val);
+		default:
+			dev_dbg(dev, "Unknown attribute for type %d: %d\n", type, attr);
+			return -EOPNOTSUPP;
+		}
+	case hwmon_pwm:
+		switch (attr) {
+		case hwmon_pwm_input:
+			ret = read_from_ec(OXP_SENSOR_PWM_REG,
+					   2, val);
+			*val = (*val * 255) / 100;
+			return ret;
+		case hwmon_pwm_enable:
+			return read_from_ec(OXP_SENSOR_PWM_ENABLE_REG, 1, val);
+		default:
+			dev_dbg(dev, "Unknown attribute for type %d: %d\n", type, attr);
+			return -EOPNOTSUPP;
+		}
+	default:
+		dev_dbg(dev, "Unknown sensor type %d.\n", type);
+		return -EOPNOTSUPP;
+	}
+}
+
+static int oxp_platform_write(struct device *dev, enum hwmon_sensor_types type,
+		u32 attr, int channel, long val)
+{
+	switch (type) {
+	case hwmon_pwm:
+		switch (attr) {
+		case hwmon_pwm_enable:
+			if (val == 1)
+				return oxp_pwm_enable(dev);
+			else if (val == 0)
+				return oxp_pwm_disable(dev);
+			else
+				return -EINVAL;
+		case hwmon_pwm_input:
+			if (val < 0 || val > 255)
+				return -EINVAL;
+			val = (val * 100) / 255;
+			return write_to_ec(dev, OXP_SENSOR_PWM_REG, val);
+		default:
+			dev_dbg(dev, "Unknown attribute for type %d: %d", type, attr);
+			return -EOPNOTSUPP;
+		}
+	default:
+		dev_dbg(dev, "Unknown sensor type: %d", type);
+		return -EOPNOTSUPP;
+	}
+	return -EINVAL;
+}
+
+/* Known sensors in the OXP EC controllers */
+static const struct hwmon_channel_info *oxp_platform_sensors[] = {
+	HWMON_CHANNEL_INFO(fan,
+		HWMON_F_INPUT),
+	HWMON_CHANNEL_INFO(pwm,
+		HWMON_PWM_INPUT | HWMON_PWM_ENABLE),
+	NULL,
+};
+
+static const struct hwmon_ops oxp_ec_hwmon_ops = {
+	.is_visible = oxp_ec_hwmon_is_visible,
+	.read = oxp_platform_read,
+	.write = oxp_platform_write,
+};
+
+static const struct hwmon_chip_info oxp_ec_chip_info = {
+	.ops = &oxp_ec_hwmon_ops,
+	.info = oxp_platform_sensors,
+};
+
+/* Initialization logic */
+static int oxp_platform_probe(struct platform_device *pdev)
+{
+	const struct dmi_system_id *dmi_entry;
+	struct device *dev = &pdev->dev;
+	struct device *hwdev;
+	struct oxp_status *state;
+
+	/* Have to check for AMD processor here */
+	dmi_entry = dmi_first_match(dmi_table);
+	if (!dmi_entry || boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
+		return -ENODEV;
+
+	state = devm_kzalloc(dev, sizeof(struct oxp_status), GFP_KERNEL);
+	if (!state)
+		return -ENOMEM;
+
+	state->lock_data.mutex = 0;
+	state->lock_data.lock = lock_global_acpi_lock;
+	state->lock_data.unlock = unlock_global_acpi_lock;
+
+	hwdev = devm_hwmon_device_register_with_info(dev, "oxpec", state,
+							&oxp_ec_chip_info, NULL);
+
+	return PTR_ERR_OR_ZERO(hwdev);
+}
+
+static struct platform_driver oxp_platform_driver = {
+	.driver = {
+		.name = "oxp-platform",
+	},
+	.probe = oxp_platform_probe,
+};
+
+static struct platform_device *oxp_platform_device;
+
+static int __init oxp_platform_init(void)
+{
+	oxp_platform_device =
+		platform_create_bundle(&oxp_platform_driver,
+				       oxp_platform_probe, NULL, 0, NULL, 0);
+
+	if (IS_ERR(oxp_platform_device))
+		return PTR_ERR(oxp_platform_device);
+
+	return 0;
+}
+
+static void __exit oxp_platform_exit(void)
+{
+	platform_device_unregister(oxp_platform_device);
+	platform_driver_unregister(&oxp_platform_driver);
+}
+
+MODULE_DEVICE_TABLE(dmi, dmi_table);
+module_init(oxp_platform_init);
+module_exit(oxp_platform_exit);
+
+MODULE_AUTHOR("Joaquín Ignacio Aramendía <[email protected]>");
+MODULE_DESCRIPTION(
+	"Platform driver that handles ACPI EC of OneXPlayer devices");
+MODULE_LICENSE("GPL");
--

2.38.1

#9 lore raw

Re: [PATCH v3] Add OneXPlayer mini AMD sensors driver

Quoted 5 lines · Guenter Roeck

I didn't have time to look at your patch, but _pease_ stop sending new versions of your patches in response to previous versions.

Guenter

Sorry, new to this, not entirely sure about the etiquette of new versions. The v3 is the last one and hopefully the definitive one.

Signature · 1 line
Joaquín I. Aramendía
#10 lore raw

RE: [PATCH v3] Add OneXPlayer mini AMD sensors driver

In reply to Joaquín Ignacio Aramendía

[Public]

Quoted 90 lines

-----Original Message-----
From: Joaquín Ignacio Aramendía <[email protected]>
Sent: Monday, October 31, 2022 09:53
To: [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; [email protected]; [email protected]; [email protected]; Joaquín Ignacio Aramendía <[email protected]>
Subject: [PATCH v3] Add OneXPlayer mini AMD sensors driver

Sensors driver for OXP Handhelds from One-Netbook that expose fan reading
and control via hwmon sysfs.

As far as I could gather all OXP boards have the same DMI strings and they are told appart by the boot cpu vendor (Intel/AMD). Currently only AMD boards are supported.

Fan control is provided via pwm interface in the range [0-255]. AMD boards have [0-100] as range in the EC, the written value is scaled to accommodate for that.

---
Removed fan_control reference in comment.
Bugfix MIX/MIN reporting not available
Bugfix pwm_enable register set wrong

---
 drivers/hwmon/Kconfig       |  13 +-
 drivers/hwmon/Makefile      |   1 +
 drivers/hwmon/oxp-sensors.c | 277
++++++++++++++++++++++++++++++++++++
 3 files changed, 290 insertions(+), 1 deletion(-)
 create mode 100644 drivers/hwmon/oxp-sensors.c
drivers/hwmon/Kconfig +6 -0
index 7ac3daaf59ce..a1cdb03b4d13 100644
@@ -1607,6 +1607,17 @@ config SENSORS_NZXT_SMART2
 
 source "drivers/hwmon/occ/Kconfig"
 
+config SENSORS_OXP
+	tristate "OneXPlayer EC fan control"
+	depends on ACPI
+	depends on X86
+	help
+		If you say yes here you get support for fan readings and

control over

drivers/hwmon/Kconfig +1 -0
+		OneXPlayer handheld devices. Only OneXPlayer mini AMD

handheld variant

drivers/hwmon/Kconfig +3 -0
+		boards are supported.
+
+		Can also be built as a module. In that case it will be called oxp-

sensors.

drivers/hwmon/Kconfig +2 -1
+
 config SENSORS_PCF8591
 	tristate "Philips PCF8591 ADC/DAC"
 	depends on I2C
@@ -1957,7 +1968,7 @@ config SENSORS_ADS7871
 
 config SENSORS_AMC6821
 	tristate "Texas Instruments AMC6821"
-	depends on I2C
+	depends on I2C
 	help
 	  If you say yes here you get support for the Texas Instruments
 	  AMC6821 hardware monitoring chips.
drivers/hwmon/Makefile +0 -0
index 11d076cad8a2..35824f8be455 100644
@@ -167,6 +167,7 @@ obj-$(CONFIG_SENSORS_NSA320)	+= nsa320-

hwmon.o
obj-$(CONFIG_SENSORS_NTC_THERMISTOR) += ntc_thermistor.o
obj-$(CONFIG_SENSORS_NZXT_KRAKEN2) += nzxt-kraken2.o obj-$(CONFIG_SENSORS_NZXT_SMART2) += nzxt-smart2.o

drivers/hwmon/Makefile +1 -0
+obj-$(CONFIG_SENSORS_OXP) += oxp-sensors.o
 obj-$(CONFIG_SENSORS_PC87360)	+= pc87360.o
 obj-$(CONFIG_SENSORS_PC87427)	+= pc87427.o
 obj-$(CONFIG_SENSORS_PCF8591)	+= pcf8591.o
drivers/hwmon/oxp-sensors.c new file +2 -0
new file mode 100644
index 000000000000..f5895dc11094
@@ -0,0 +1,277 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*

drivers/hwmon/oxp-sensors.c line 12

Quoted 10 lines
drivers/hwmon/oxp-sensors.c new file +10 -0
+ * Platform driver for OXP Handhelds that expose fan reading and control
+ * via hwmon sysfs.
+ *
+ * All boards have the same DMI strings and they are told appart by the
+ * boot cpu vendor (Intel/AMD). Currently only AMD boards are supported
+ * but the code is made to be simple to add other handheld boards in the
+ * future.
+ * Fan control is provided via pwm interface in the range [0-255]. AMD
+ * boards use [0-100] as range in the EC, the written value is scaled to
+ * accommodate for that.

What happens on the Intel variant with this code? Are they not the same EC? Why doesn't it work there? If you keep the AMD check in the code, I think it would be good to document the problems with the Intel one at least.

Quoted 70 lines
drivers/hwmon/oxp-sensors.c new file +26 -0
+ *
+ * Copyright (C) 2022 Joaquín I. Aramendía <[email protected]>
+ */
+
+#include <linux/acpi.h>
+#include <linux/dev_printk.h>
+#include <linux/dmi.h>
+#include <linux/hwmon.h>
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/processor.h>
+
+#define ACPI_LOCK_DELAY_MS	500
+
+/* Handle ACPI lock mechanism */
+struct lock_data {
+	u32 mutex;
+	bool (*lock)(struct lock_data *data);
+	bool (*unlock)(struct lock_data *data);
+};
+
+static bool lock_global_acpi_lock(struct lock_data *data)
+{
+	return

ACPI_SUCCESS(acpi_acquire_global_lock(ACPI_LOCK_DELAY_MS,

drivers/hwmon/oxp-sensors.c new file +1 -0
+								 &data-
Quoted 1 line

mutex));

drivers/hwmon/oxp-sensors.c new file +8 -0
+}
+
+static bool unlock_global_acpi_lock(struct lock_data *data)
+{
+	return ACPI_SUCCESS(acpi_release_global_lock(data->mutex));
+}
+
+#define OXP_SENSOR_FAN_REG		0x76 /* Fan reading is 2

registers long */
+#define OXP_SENSOR_PWM_ENABLE_REG 0x4A /* PWM enable is 1
register long */
+#define OXP_SENSOR_PWM_REG 0x4B /* PWM reading is 1
register long */

drivers/hwmon/oxp-sensors.c new file +6 -0
+
+static const struct dmi_system_id dmi_table[] = {
+	{
+		.matches = {
+			DMI_EXACT_MATCH(DMI_BOARD_VENDOR,
+					"ONE-NETBOOK TECHNOLOGY CO.,

LTD."),

drivers/hwmon/oxp-sensors.c new file +21 -0
+		},
+	},
+	{
+		.matches = {
+			DMI_EXACT_MATCH(DMI_BOARD_VENDOR,
+					"ONE-NETBOOK"),
+		},
+	},
+	{},
+};
+
+struct oxp_status {
+	struct lock_data lock_data;
+};
+
+/* Helper functions to handle EC read/write */
+static int read_from_ec(u8 reg, int size, long *val)
+{
+	int i;
+	int ret;
+	u8 buffer;

drivers/hwmon/oxp-sensors.c line 86

Quoted 10 lines
drivers/hwmon/oxp-sensors.c new file +10 -0
+
+	*val = 0;
+	for (i = 0; i < size; i++) {
+		ret = ec_read(reg + i, &buffer);
+		if (ret)
+			return ret;
+		(*val) <<= i * 8;
+		*val += buffer;
+	}
+	return ret;

Don't you need to acquire your mutex for reading too? Otherwise you could potentially have userspace trying to read and write another at the same time and get indeterminate results.

Quoted 108 lines
drivers/hwmon/oxp-sensors.c new file +33 -0
+}
+
+static int write_to_ec(const struct device *dev, u8 reg, u8 value)
+{
+	struct oxp_status *state = dev_get_drvdata(dev);
+	int ret;
+
+	if (!state->lock_data.lock(&state->lock_data)) {
+		dev_warn(dev, "Failed to acquire mutex");
+		return -EBUSY;
+	}
+
+	ret = ec_write(reg, value);
+
+	if (!state->lock_data.unlock(&state->lock_data))
+		dev_err(dev, "Failed to release mutex");
+
+	return ret;
+}
+
+static int oxp_pwm_enable(const struct device *dev)
+{
+	return write_to_ec(dev, OXP_SENSOR_PWM_ENABLE_REG, 0x01);
+}
+
+static int oxp_pwm_disable(const struct device *dev)
+{
+	return write_to_ec(dev, OXP_SENSOR_PWM_ENABLE_REG, 0x00);
+}
+
+/* Callbacks for hwmon interface */
+static umode_t oxp_ec_hwmon_is_visible(const void *drvdata,
+					enum hwmon_sensor_types type,

u32 attr, int channel)

drivers/hwmon/oxp-sensors.c new file +13 -0
+{
+	switch (type) {
+	case hwmon_fan:
+		return 0444;
+	case hwmon_pwm:
+		return 0644;
+	default:
+		return 0;
+	}
+	return 0;
+}
+
+static int oxp_platform_read(struct device *dev, enum

hwmon_sensor_types type,

drivers/hwmon/oxp-sensors.c new file +13 -0
+			     u32 attr, int channel, long *val)
+{
+	int ret;
+
+	switch (type) {
+	case hwmon_fan:
+		switch (attr) {
+		case hwmon_fan_input:
+			return read_from_ec(OXP_SENSOR_FAN_REG,
+					   2,
+					   val);
+		default:
+			dev_dbg(dev, "Unknown attribute for type %d:

%d\n", type, attr);

drivers/hwmon/oxp-sensors.c new file +11 -0
+			return -EOPNOTSUPP;
+		}
+	case hwmon_pwm:
+		switch (attr) {
+		case hwmon_pwm_input:
+			ret = read_from_ec(OXP_SENSOR_PWM_REG,
+					   2, val);
+			*val = (*val * 255) / 100;
+			return ret;
+		case hwmon_pwm_enable:
+			return

read_from_ec(OXP_SENSOR_PWM_ENABLE_REG, 1, val);

drivers/hwmon/oxp-sensors.c new file +2 -0
+		default:
+			dev_dbg(dev, "Unknown attribute for type %d:

%d\n", type, attr);

drivers/hwmon/oxp-sensors.c new file +9 -0
+			return -EOPNOTSUPP;
+		}
+	default:
+		dev_dbg(dev, "Unknown sensor type %d.\n", type);
+		return -EOPNOTSUPP;
+	}
+}
+
+static int oxp_platform_write(struct device *dev, enum

hwmon_sensor_types type,

drivers/hwmon/oxp-sensors.c new file +17 -0
+		u32 attr, int channel, long val)
+{
+	switch (type) {
+	case hwmon_pwm:
+		switch (attr) {
+		case hwmon_pwm_enable:
+			if (val == 1)
+				return oxp_pwm_enable(dev);
+			else if (val == 0)
+				return oxp_pwm_disable(dev);
+			else
+				return -EINVAL;
+		case hwmon_pwm_input:
+			if (val < 0 || val > 255)
+				return -EINVAL;
+			val = (val * 100) / 255;
+			return write_to_ec(dev, OXP_SENSOR_PWM_REG,

val);

drivers/hwmon/oxp-sensors.c new file +2 -0
+		default:
+			dev_dbg(dev, "Unknown attribute for type %d: %d",

type, attr);

drivers/hwmon/oxp-sensors.c line 193

Quoted 7 lines
drivers/hwmon/oxp-sensors.c new file +7 -0
+			return -EOPNOTSUPP;
+		}
+	default:
+		dev_dbg(dev, "Unknown sensor type: %d", type);
+		return -EOPNOTSUPP;
+	}
+	return -EINVAL;

Can you actually hit this scenario? I would think not; you'll hit "default" and return -EOPNOTSUPP. Maybe it's better to just drop the default label and then outside the switch/case do this:

	dev_dbg(dev, "Unknown sensor type: %d", type);
	return -EOPNOTSUPP;
Quoted 24 lines
drivers/hwmon/oxp-sensors.c new file +24 -0
+}
+
+/* Known sensors in the OXP EC controllers */
+static const struct hwmon_channel_info *oxp_platform_sensors[] = {
+	HWMON_CHANNEL_INFO(fan,
+		HWMON_F_INPUT),
+	HWMON_CHANNEL_INFO(pwm,
+		HWMON_PWM_INPUT | HWMON_PWM_ENABLE),
+	NULL,
+};
+
+static const struct hwmon_ops oxp_ec_hwmon_ops = {
+	.is_visible = oxp_ec_hwmon_is_visible,
+	.read = oxp_platform_read,
+	.write = oxp_platform_write,
+};
+
+static const struct hwmon_chip_info oxp_ec_chip_info = {
+	.ops = &oxp_ec_hwmon_ops,
+	.info = oxp_platform_sensors,
+};
+
+/* Initialization logic */
+static int oxp_platform_probe(struct platform_device *pdev)

drivers/hwmon/oxp-sensors.c line 227

Quoted 10 lines
drivers/hwmon/oxp-sensors.c new file +10 -0
+{
+	const struct dmi_system_id *dmi_entry;
+	struct device *dev = &pdev->dev;
+	struct device *hwdev;
+	struct oxp_status *state;
+
+	/* Have to check for AMD processor here */
+	dmi_entry = dmi_first_match(dmi_table);
+	if (!dmi_entry || boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
+		return -ENODEV;

So it's shared DMI data values for the Intel and AMD variants of this platform? What happens if you run all this code on the Intel one?

Quoted 54 lines
drivers/hwmon/oxp-sensors.c new file +10 -0
+
+	state = devm_kzalloc(dev, sizeof(struct oxp_status), GFP_KERNEL);
+	if (!state)
+		return -ENOMEM;
+
+	state->lock_data.mutex = 0;
+	state->lock_data.lock = lock_global_acpi_lock;
+	state->lock_data.unlock = unlock_global_acpi_lock;
+
+	hwdev = devm_hwmon_device_register_with_info(dev, "oxpec",

state,

drivers/hwmon/oxp-sensors.c new file +1 -0
+							&oxp_ec_chip_info,

NULL);

drivers/hwmon/oxp-sensors.c new file +39 -1
+
+	return PTR_ERR_OR_ZERO(hwdev);
+}
+
+static struct platform_driver oxp_platform_driver = {
+	.driver = {
+		.name = "oxp-platform",
+	},
+	.probe = oxp_platform_probe,
+};
+
+static struct platform_device *oxp_platform_device;
+
+static int __init oxp_platform_init(void)
+{
+	oxp_platform_device =
+		platform_create_bundle(&oxp_platform_driver,
+				       oxp_platform_probe, NULL, 0, NULL, 0);
+
+	if (IS_ERR(oxp_platform_device))
+		return PTR_ERR(oxp_platform_device);
+
+	return 0;
+}
+
+static void __exit oxp_platform_exit(void)
+{
+	platform_device_unregister(oxp_platform_device);
+	platform_driver_unregister(&oxp_platform_driver);
+}
+
+MODULE_DEVICE_TABLE(dmi, dmi_table);
+module_init(oxp_platform_init);
+module_exit(oxp_platform_exit);
+
+MODULE_AUTHOR("Joaquín Ignacio Aramendía <[email protected]>");
+MODULE_DESCRIPTION(
+	"Platform driver that handles ACPI EC of OneXPlayer devices");
+MODULE_LICENSE("GPL");
--

2.38.1

#11 lore raw

Re: [PATCH v3] Add OneXPlayer mini AMD sensors driver

Quoted 101 lines · Mario

[Public]

Quoted 96 lines

-----Original Message-----
From: Joaquín Ignacio Aramendía <[email protected]>
Sent: Monday, October 31, 2022 09:53
To: [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; [email protected]; [email protected]; [email protected]; Joaquín Ignacio Aramendía <[email protected]>
Subject: [PATCH v3] Add OneXPlayer mini AMD sensors driver

Sensors driver for OXP Handhelds from One-Netbook that expose fan reading
and control via hwmon sysfs.

As far as I could gather all OXP boards have the same DMI strings and they are told appart by the boot cpu vendor (Intel/AMD). Currently only AMD boards are supported.

Fan control is provided via pwm interface in the range [0-255]. AMD boards have [0-100] as range in the EC, the written value is scaled to accommodate for that.

---
Removed fan_control reference in comment.
Bugfix MIX/MIN reporting not available
Bugfix pwm_enable register set wrong

---
 drivers/hwmon/Kconfig       |  13 +-
 drivers/hwmon/Makefile      |   1 +
 drivers/hwmon/oxp-sensors.c | 277
++++++++++++++++++++++++++++++++++++
 3 files changed, 290 insertions(+), 1 deletion(-)
 create mode 100644 drivers/hwmon/oxp-sensors.c
drivers/hwmon/Kconfig +6 -0
index 7ac3daaf59ce..a1cdb03b4d13 100644
@@ -1607,6 +1607,17 @@ config SENSORS_NZXT_SMART2
 
 source "drivers/hwmon/occ/Kconfig"
 
+config SENSORS_OXP
+     tristate "OneXPlayer EC fan control"
+     depends on ACPI
+     depends on X86
+     help
+             If you say yes here you get support for fan readings and

control over
+ OneXPlayer handheld devices. Only OneXPlayer mini AMD
handheld variant

drivers/hwmon/Kconfig +3 -0
+             boards are supported.
+
+             Can also be built as a module. In that case it will be called oxp-

sensors.

drivers/hwmon/Kconfig +2 -1
+
 config SENSORS_PCF8591
      tristate "Philips PCF8591 ADC/DAC"
      depends on I2C
@@ -1957,7 +1968,7 @@ config SENSORS_ADS7871
 
 config SENSORS_AMC6821
      tristate "Texas Instruments AMC6821"
-     depends on I2C
+     depends on I2C
      help
        If you say yes here you get support for the Texas Instruments
        AMC6821 hardware monitoring chips.
drivers/hwmon/Makefile +0 -0
index 11d076cad8a2..35824f8be455 100644
@@ -167,6 +167,7 @@ obj-$(CONFIG_SENSORS_NSA320)      += nsa320-

hwmon.o
obj-$(CONFIG_SENSORS_NTC_THERMISTOR) += ntc_thermistor.o obj-$(CONFIG_SENSORS_NZXT_KRAKEN2) += nzxt-kraken2.o obj-$(CONFIG_SENSORS_NZXT_SMART2) += nzxt-smart2.o

drivers/hwmon/Makefile +1 -0
+obj-$(CONFIG_SENSORS_OXP) += oxp-sensors.o
 obj-$(CONFIG_SENSORS_PC87360)        += pc87360.o
 obj-$(CONFIG_SENSORS_PC87427)        += pc87427.o
 obj-$(CONFIG_SENSORS_PCF8591)        += pcf8591.o
drivers/hwmon/oxp-sensors.c new file +8 -0
new file mode 100644
index 000000000000..f5895dc11094
@@ -0,0 +1,277 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Platform driver for OXP Handhelds that expose fan reading and control
+ * via hwmon sysfs.
+ *
+ * All boards have the same DMI strings and they are told appart by the
+ * boot cpu vendor (Intel/AMD). Currently only AMD boards are supported
+ * but the code is made to be simple to add other handheld boards in the
Quoted 8 lines

drivers/hwmon/oxp-sensors.c line 12

Quoted 4 lines
drivers/hwmon/oxp-sensors.c new file +4 -0
+ * future.
+ * Fan control is provided via pwm interface in the range [0-255]. AMD
+ * boards use [0-100] as range in the EC, the written value is scaled to
+ * accommodate for that.

What happens on the Intel variant with this code? Are they not the same EC? Why doesn't it work there? If you keep the AMD check in the code, I think it would be good to document the problems with the Intel one at least.

I don't own an intel board but a friend of mine does. It won't work. The EC registers are different, even though they have the same DMI strings for board manufacturer and board name. There is also a variant for the board vendor that is programmed here "ONE-NETBOOK" and "ONE-NETBOOK TECHNOLOGY CO., LTD." for the same device.
The explanation for the Intel issue is I couldn't figure out the EC registers and values to read/write. I have a version of this on my repo with a non-functional Intel case. I can document it in a code comment over the amd cpu check.

Quoted 76 lines
Quoted 76 lines
drivers/hwmon/oxp-sensors.c new file +26 -0
+ *
+ * Copyright (C) 2022 Joaquín I. Aramendía <[email protected]>
+ */
+
+#include <linux/acpi.h>
+#include <linux/dev_printk.h>
+#include <linux/dmi.h>
+#include <linux/hwmon.h>
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/processor.h>
+
+#define ACPI_LOCK_DELAY_MS   500
+
+/* Handle ACPI lock mechanism */
+struct lock_data {
+     u32 mutex;
+     bool (*lock)(struct lock_data *data);
+     bool (*unlock)(struct lock_data *data);
+};
+
+static bool lock_global_acpi_lock(struct lock_data *data)
+{
+     return

ACPI_SUCCESS(acpi_acquire_global_lock(ACPI_LOCK_DELAY_MS,
+ &data-

Quoted 1 line

mutex));

drivers/hwmon/oxp-sensors.c new file +8 -0
+}
+
+static bool unlock_global_acpi_lock(struct lock_data *data)
+{
+     return ACPI_SUCCESS(acpi_release_global_lock(data->mutex));
+}
+
+#define OXP_SENSOR_FAN_REG           0x76 /* Fan reading is 2

registers long */
+#define OXP_SENSOR_PWM_ENABLE_REG 0x4A /* PWM enable is 1
register long */
+#define OXP_SENSOR_PWM_REG 0x4B /* PWM reading is 1
register long */

drivers/hwmon/oxp-sensors.c new file +6 -0
+
+static const struct dmi_system_id dmi_table[] = {
+     {
+             .matches = {
+                     DMI_EXACT_MATCH(DMI_BOARD_VENDOR,
+                                     "ONE-NETBOOK TECHNOLOGY CO.,

LTD."),

drivers/hwmon/oxp-sensors.c new file +27 -0
+             },
+     },
+     {
+             .matches = {
+                     DMI_EXACT_MATCH(DMI_BOARD_VENDOR,
+                                     "ONE-NETBOOK"),
+             },
+     },
+     {},
+};
+
+struct oxp_status {
+     struct lock_data lock_data;
+};
+
+/* Helper functions to handle EC read/write */
+static int read_from_ec(u8 reg, int size, long *val)
+{
+     int i;
+     int ret;
+     u8 buffer;
+
+     *val = 0;
+     for (i = 0; i < size; i++) {
+             ret = ec_read(reg + i, &buffer);
+             if (ret)
+                     return ret;
Quoted 8 lines

drivers/hwmon/oxp-sensors.c line 86

Quoted 4 lines
drivers/hwmon/oxp-sensors.c new file +4 -0
+             (*val) <<= i * 8;
+             *val += buffer;
+     }
+     return ret;

Don't you need to acquire your mutex for reading too? Otherwise you could potentially have userspace trying to read and write another at the same time and get indeterminate results.

Will add since it doesn't seem to hurt. I added the mutex to the write case only since it can indeed present an issue.

Quoted 119 lines
Quoted 108 lines
drivers/hwmon/oxp-sensors.c new file +33 -0
+}
+
+static int write_to_ec(const struct device *dev, u8 reg, u8 value)
+{
+     struct oxp_status *state = dev_get_drvdata(dev);
+     int ret;
+
+     if (!state->lock_data.lock(&state->lock_data)) {
+             dev_warn(dev, "Failed to acquire mutex");
+             return -EBUSY;
+     }
+
+     ret = ec_write(reg, value);
+
+     if (!state->lock_data.unlock(&state->lock_data))
+             dev_err(dev, "Failed to release mutex");
+
+     return ret;
+}
+
+static int oxp_pwm_enable(const struct device *dev)
+{
+     return write_to_ec(dev, OXP_SENSOR_PWM_ENABLE_REG, 0x01);
+}
+
+static int oxp_pwm_disable(const struct device *dev)
+{
+     return write_to_ec(dev, OXP_SENSOR_PWM_ENABLE_REG, 0x00);
+}
+
+/* Callbacks for hwmon interface */
+static umode_t oxp_ec_hwmon_is_visible(const void *drvdata,
+                                     enum hwmon_sensor_types type,

u32 attr, int channel)

drivers/hwmon/oxp-sensors.c new file +13 -0
+{
+     switch (type) {
+     case hwmon_fan:
+             return 0444;
+     case hwmon_pwm:
+             return 0644;
+     default:
+             return 0;
+     }
+     return 0;
+}
+
+static int oxp_platform_read(struct device *dev, enum

hwmon_sensor_types type,

drivers/hwmon/oxp-sensors.c new file +13 -0
+                          u32 attr, int channel, long *val)
+{
+     int ret;
+
+     switch (type) {
+     case hwmon_fan:
+             switch (attr) {
+             case hwmon_fan_input:
+                     return read_from_ec(OXP_SENSOR_FAN_REG,
+                                        2,
+                                        val);
+             default:
+                     dev_dbg(dev, "Unknown attribute for type %d:

%d\n", type, attr);

drivers/hwmon/oxp-sensors.c new file +11 -0
+                     return -EOPNOTSUPP;
+             }
+     case hwmon_pwm:
+             switch (attr) {
+             case hwmon_pwm_input:
+                     ret = read_from_ec(OXP_SENSOR_PWM_REG,
+                                        2, val);
+                     *val = (*val * 255) / 100;
+                     return ret;
+             case hwmon_pwm_enable:
+                     return

read_from_ec(OXP_SENSOR_PWM_ENABLE_REG, 1, val);

drivers/hwmon/oxp-sensors.c new file +2 -0
+             default:
+                     dev_dbg(dev, "Unknown attribute for type %d:

%d\n", type, attr);

drivers/hwmon/oxp-sensors.c new file +9 -0
+                     return -EOPNOTSUPP;
+             }
+     default:
+             dev_dbg(dev, "Unknown sensor type %d.\n", type);
+             return -EOPNOTSUPP;
+     }
+}
+
+static int oxp_platform_write(struct device *dev, enum

hwmon_sensor_types type,

drivers/hwmon/oxp-sensors.c new file +17 -0
+             u32 attr, int channel, long val)
+{
+     switch (type) {
+     case hwmon_pwm:
+             switch (attr) {
+             case hwmon_pwm_enable:
+                     if (val == 1)
+                             return oxp_pwm_enable(dev);
+                     else if (val == 0)
+                             return oxp_pwm_disable(dev);
+                     else
+                             return -EINVAL;
+             case hwmon_pwm_input:
+                     if (val < 0 || val > 255)
+                             return -EINVAL;
+                     val = (val * 100) / 255;
+                     return write_to_ec(dev, OXP_SENSOR_PWM_REG,

val);

drivers/hwmon/oxp-sensors.c new file +2 -0
+             default:
+                     dev_dbg(dev, "Unknown attribute for type %d: %d",

type, attr);

drivers/hwmon/oxp-sensors.c line 193

Quoted 7 lines
drivers/hwmon/oxp-sensors.c new file +7 -0
+                     return -EOPNOTSUPP;
+             }
+     default:
+             dev_dbg(dev, "Unknown sensor type: %d", type);
+             return -EOPNOTSUPP;
+     }
+     return -EINVAL;

Can you actually hit this scenario? I would think not; you'll hit "default" and return -EOPNOTSUPP. Maybe it's better to just drop the default label and then outside the switch/case do this:

Quoted 3 lines
        dev_dbg(dev, "Unknown sensor type: %d", type);
        return -EOPNOTSUPP;

It shouldn't since there are no other attributes present. I can simplify this logic by a catch all case as you stated.

Quoted 29 lines
Quoted 29 lines
drivers/hwmon/oxp-sensors.c new file +29 -0
+}
+
+/* Known sensors in the OXP EC controllers */
+static const struct hwmon_channel_info *oxp_platform_sensors[] = {
+     HWMON_CHANNEL_INFO(fan,
+             HWMON_F_INPUT),
+     HWMON_CHANNEL_INFO(pwm,
+             HWMON_PWM_INPUT | HWMON_PWM_ENABLE),
+     NULL,
+};
+
+static const struct hwmon_ops oxp_ec_hwmon_ops = {
+     .is_visible = oxp_ec_hwmon_is_visible,
+     .read = oxp_platform_read,
+     .write = oxp_platform_write,
+};
+
+static const struct hwmon_chip_info oxp_ec_chip_info = {
+     .ops = &oxp_ec_hwmon_ops,
+     .info = oxp_platform_sensors,
+};
+
+/* Initialization logic */
+static int oxp_platform_probe(struct platform_device *pdev)
+{
+     const struct dmi_system_id *dmi_entry;
+     struct device *dev = &pdev->dev;
+     struct device *hwdev;
+     struct oxp_status *state;
Quoted 9 lines

drivers/hwmon/oxp-sensors.c line 227

Quoted 5 lines
drivers/hwmon/oxp-sensors.c new file +5 -0
+
+     /* Have to check for AMD processor here */
+     dmi_entry = dmi_first_match(dmi_table);
+     if (!dmi_entry || boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
+             return -ENODEV;

So it's shared DMI data values for the Intel and AMD variants of this platform? What happens if you run all this code on the Intel one?

Yeah... These devices have all the same DMI strings. In facto all the OneXPlayers have the same DMI model name also "ONEXPLAYER". Running this code on Intel won't work, the EC registers and values seem to differ.

Quoted 54 lines
Quoted 54 lines
drivers/hwmon/oxp-sensors.c new file +10 -0
+
+     state = devm_kzalloc(dev, sizeof(struct oxp_status), GFP_KERNEL);
+     if (!state)
+             return -ENOMEM;
+
+     state->lock_data.mutex = 0;
+     state->lock_data.lock = lock_global_acpi_lock;
+     state->lock_data.unlock = unlock_global_acpi_lock;
+
+     hwdev = devm_hwmon_device_register_with_info(dev, "oxpec",

state,
+ &oxp_ec_chip_info,
NULL);

drivers/hwmon/oxp-sensors.c new file +39 -1
+
+     return PTR_ERR_OR_ZERO(hwdev);
+}
+
+static struct platform_driver oxp_platform_driver = {
+     .driver = {
+             .name = "oxp-platform",
+     },
+     .probe = oxp_platform_probe,
+};
+
+static struct platform_device *oxp_platform_device;
+
+static int __init oxp_platform_init(void)
+{
+     oxp_platform_device =
+             platform_create_bundle(&oxp_platform_driver,
+                                    oxp_platform_probe, NULL, 0, NULL, 0);
+
+     if (IS_ERR(oxp_platform_device))
+             return PTR_ERR(oxp_platform_device);
+
+     return 0;
+}
+
+static void __exit oxp_platform_exit(void)
+{
+     platform_device_unregister(oxp_platform_device);
+     platform_driver_unregister(&oxp_platform_driver);
+}
+
+MODULE_DEVICE_TABLE(dmi, dmi_table);
+module_init(oxp_platform_init);
+module_exit(oxp_platform_exit);
+
+MODULE_AUTHOR("Joaquín Ignacio Aramendía <[email protected]>");
+MODULE_DESCRIPTION(
+     "Platform driver that handles ACPI EC of OneXPlayer devices");
+MODULE_LICENSE("GPL");
--

2.38.1

Signature · 1 line
Joaquín I. Aramendía
#12 lore raw

RE: [PATCH v3] Add OneXPlayer mini AMD sensors driver

[Public]

Quoted 127 lines

-----Original Message-----
From: Joaquin Aramendia <[email protected]>
Sent: Monday, October 31, 2022 13:58
To: Limonciello, Mario <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v3] Add OneXPlayer mini AMD sensors driver

Quoted 80 lines · Mario

[Public]

Quoted 75 lines

-----Original Message-----
From: Joaquín Ignacio Aramendía <[email protected]>
Sent: Monday, October 31, 2022 09:53
To: [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; [email protected]; [email protected]; [email protected]; Joaquín Ignacio Aramendía <[email protected]>
Subject: [PATCH v3] Add OneXPlayer mini AMD sensors driver

Sensors driver for OXP Handhelds from One-Netbook that expose fan reading
and control via hwmon sysfs.

As far as I could gather all OXP boards have the same DMI strings and they are told appart by the boot cpu vendor (Intel/AMD). Currently only AMD boards are supported.

Fan control is provided via pwm interface in the range [0-255]. AMD boards have [0-100] as range in the EC, the written value is scaled to accommodate for that.

---
Removed fan_control reference in comment.
Bugfix MIX/MIN reporting not available
Bugfix pwm_enable register set wrong

---
 drivers/hwmon/Kconfig       |  13 +-
 drivers/hwmon/Makefile      |   1 +
 drivers/hwmon/oxp-sensors.c | 277
++++++++++++++++++++++++++++++++++++
 3 files changed, 290 insertions(+), 1 deletion(-)
 create mode 100644 drivers/hwmon/oxp-sensors.c
drivers/hwmon/Kconfig +6 -0
index 7ac3daaf59ce..a1cdb03b4d13 100644
@@ -1607,6 +1607,17 @@ config SENSORS_NZXT_SMART2
 
 source "drivers/hwmon/occ/Kconfig"
 
+config SENSORS_OXP
+     tristate "OneXPlayer EC fan control"
+     depends on ACPI
+     depends on X86
+     help
+             If you say yes here you get support for fan readings and

control over
+ OneXPlayer handheld devices. Only OneXPlayer mini AMD
handheld variant

drivers/hwmon/Kconfig +3 -0
+             boards are supported.
+
+             Can also be built as a module. In that case it will be called oxp-

sensors.

drivers/hwmon/Kconfig +2 -1
+
 config SENSORS_PCF8591
      tristate "Philips PCF8591 ADC/DAC"
      depends on I2C
@@ -1957,7 +1968,7 @@ config SENSORS_ADS7871
 
 config SENSORS_AMC6821
      tristate "Texas Instruments AMC6821"
-     depends on I2C
+     depends on I2C
      help
        If you say yes here you get support for the Texas Instruments
        AMC6821 hardware monitoring chips.
drivers/hwmon/Makefile +0 -0
index 11d076cad8a2..35824f8be455 100644
@@ -167,6 +167,7 @@ obj-$(CONFIG_SENSORS_NSA320)      += nsa320-

hwmon.o

Quoted 8 lines

drivers/hwmon/oxp-

Quoted 8 lines

obj-$(CONFIG_SENSORS_NTC_THERMISTOR) += ntc_thermistor.o obj-$(CONFIG_SENSORS_NZXT_KRAKEN2) += nzxt-kraken2.o obj-$(CONFIG_SENSORS_NZXT_SMART2) += nzxt-smart2.o

drivers/hwmon/Makefile +1 -0
+obj-$(CONFIG_SENSORS_OXP) += oxp-sensors.o
 obj-$(CONFIG_SENSORS_PC87360)        += pc87360.o
 obj-$(CONFIG_SENSORS_PC87427)        += pc87427.o
 obj-$(CONFIG_SENSORS_PCF8591)        += pcf8591.o
drivers/hwmon/oxp- renamed from drivers/hwmon/oxp-sensors.c +0 -0

sensors.c

Quoted 8 lines

drivers/hwmon/oxp-sensors.c line 3

Quoted 8 lines

new file mode 100644
index 000000000000..f5895dc11094

drivers/hwmon/oxp-sensors.c new file +3 -0
@@ -0,0 +1,277 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Platform driver for OXP Handhelds that expose fan reading and

control

Quoted 4 lines

drivers/hwmon/oxp-sensors.c

Quoted 4 lines
drivers/hwmon/oxp-sensors.c new file +4 -0
+ * via hwmon sysfs.
+ *
+ * All boards have the same DMI strings and they are told appart by the
+ * boot cpu vendor (Intel/AMD). Currently only AMD boards are

supported

Quoted 1 line

drivers/hwmon/oxp-sensors.c line 8

Quoted 1 line
drivers/hwmon/oxp-sensors.c new file +1 -0
+ * but the code is made to be simple to add other handheld boards in

the

Quoted 6 lines

drivers/hwmon/oxp-sensors.c line 12

Quoted 4 lines
drivers/hwmon/oxp-sensors.c new file +4 -0
+ * future.
+ * Fan control is provided via pwm interface in the range [0-255]. AMD
+ * boards use [0-100] as range in the EC, the written value is scaled to
+ * accommodate for that.

What happens on the Intel variant with this code? Are they not the same

EC?

Quoted 1 line

Why doesn't it work there? If you keep the AMD check in the code, I think

it

Quoted 1 line

would be good to document the problems with the Intel one at least.

Quoted 10 lines

I don't own an intel board but a friend of mine does. It won't work. The EC registers are different, even though they have the same DMI strings for board manufacturer and board name. There is also a variant for the board vendor that is programmed here "ONE-NETBOOK" and "ONE-NETBOOK TECHNOLOGY CO., LTD." for the same device.
The explanation for the Intel issue is I couldn't figure out the EC registers and values to read/write. I have a version of this on my repo with a non-functional Intel case. I can document it in a code comment over the amd cpu check.

Have you tried comparing all of the DMI strings? I'm really surprised that they would all be the same. My suspicion would be that at least the SKU string should be different. If it is, you should be able to drop the CPU match and use that instead.

Quoted 319 lines
Quoted 76 lines
Quoted 76 lines
drivers/hwmon/oxp-sensors.c new file +26 -0
+ *
+ * Copyright (C) 2022 Joaquín I. Aramendía <[email protected]>
+ */
+
+#include <linux/acpi.h>
+#include <linux/dev_printk.h>
+#include <linux/dmi.h>
+#include <linux/hwmon.h>
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/processor.h>
+
+#define ACPI_LOCK_DELAY_MS   500
+
+/* Handle ACPI lock mechanism */
+struct lock_data {
+     u32 mutex;
+     bool (*lock)(struct lock_data *data);
+     bool (*unlock)(struct lock_data *data);
+};
+
+static bool lock_global_acpi_lock(struct lock_data *data)
+{
+     return

ACPI_SUCCESS(acpi_acquire_global_lock(ACPI_LOCK_DELAY_MS,
+ &data-

Quoted 1 line

mutex));

drivers/hwmon/oxp-sensors.c new file +8 -0
+}
+
+static bool unlock_global_acpi_lock(struct lock_data *data)
+{
+     return ACPI_SUCCESS(acpi_release_global_lock(data->mutex));
+}
+
+#define OXP_SENSOR_FAN_REG           0x76 /* Fan reading is 2

registers long */
+#define OXP_SENSOR_PWM_ENABLE_REG 0x4A /* PWM enable is 1
register long */
+#define OXP_SENSOR_PWM_REG 0x4B /* PWM reading is 1
register long */

drivers/hwmon/oxp-sensors.c new file +6 -0
+
+static const struct dmi_system_id dmi_table[] = {
+     {
+             .matches = {
+                     DMI_EXACT_MATCH(DMI_BOARD_VENDOR,
+                                     "ONE-NETBOOK TECHNOLOGY CO.,

LTD."),

drivers/hwmon/oxp-sensors.c new file +27 -0
+             },
+     },
+     {
+             .matches = {
+                     DMI_EXACT_MATCH(DMI_BOARD_VENDOR,
+                                     "ONE-NETBOOK"),
+             },
+     },
+     {},
+};
+
+struct oxp_status {
+     struct lock_data lock_data;
+};
+
+/* Helper functions to handle EC read/write */
+static int read_from_ec(u8 reg, int size, long *val)
+{
+     int i;
+     int ret;
+     u8 buffer;
+
+     *val = 0;
+     for (i = 0; i < size; i++) {
+             ret = ec_read(reg + i, &buffer);
+             if (ret)
+                     return ret;
Quoted 8 lines

drivers/hwmon/oxp-sensors.c line 86

Quoted 4 lines
drivers/hwmon/oxp-sensors.c new file +4 -0
+             (*val) <<= i * 8;
+             *val += buffer;
+     }
+     return ret;

Don't you need to acquire your mutex for reading too? Otherwise you could potentially have userspace trying to read and write another at the same time and get indeterminate results.

Will add since it doesn't seem to hurt. I added the mutex to the write case only since it can indeed present an issue.

Quoted 109 lines
Quoted 109 lines
drivers/hwmon/oxp-sensors.c new file +33 -0
+}
+
+static int write_to_ec(const struct device *dev, u8 reg, u8 value)
+{
+     struct oxp_status *state = dev_get_drvdata(dev);
+     int ret;
+
+     if (!state->lock_data.lock(&state->lock_data)) {
+             dev_warn(dev, "Failed to acquire mutex");
+             return -EBUSY;
+     }
+
+     ret = ec_write(reg, value);
+
+     if (!state->lock_data.unlock(&state->lock_data))
+             dev_err(dev, "Failed to release mutex");
+
+     return ret;
+}
+
+static int oxp_pwm_enable(const struct device *dev)
+{
+     return write_to_ec(dev, OXP_SENSOR_PWM_ENABLE_REG, 0x01);
+}
+
+static int oxp_pwm_disable(const struct device *dev)
+{
+     return write_to_ec(dev, OXP_SENSOR_PWM_ENABLE_REG, 0x00);
+}
+
+/* Callbacks for hwmon interface */
+static umode_t oxp_ec_hwmon_is_visible(const void *drvdata,
+                                     enum hwmon_sensor_types type,

u32 attr, int channel)

drivers/hwmon/oxp-sensors.c new file +13 -0
+{
+     switch (type) {
+     case hwmon_fan:
+             return 0444;
+     case hwmon_pwm:
+             return 0644;
+     default:
+             return 0;
+     }
+     return 0;
+}
+
+static int oxp_platform_read(struct device *dev, enum

hwmon_sensor_types type,

drivers/hwmon/oxp-sensors.c new file +13 -0
+                          u32 attr, int channel, long *val)
+{
+     int ret;
+
+     switch (type) {
+     case hwmon_fan:
+             switch (attr) {
+             case hwmon_fan_input:
+                     return read_from_ec(OXP_SENSOR_FAN_REG,
+                                        2,
+                                        val);
+             default:
+                     dev_dbg(dev, "Unknown attribute for type %d:

%d\n", type, attr);

drivers/hwmon/oxp-sensors.c new file +11 -0
+                     return -EOPNOTSUPP;
+             }
+     case hwmon_pwm:
+             switch (attr) {
+             case hwmon_pwm_input:
+                     ret = read_from_ec(OXP_SENSOR_PWM_REG,
+                                        2, val);
+                     *val = (*val * 255) / 100;
+                     return ret;
+             case hwmon_pwm_enable:
+                     return

read_from_ec(OXP_SENSOR_PWM_ENABLE_REG, 1, val);

drivers/hwmon/oxp-sensors.c new file +2 -0
+             default:
+                     dev_dbg(dev, "Unknown attribute for type %d:

%d\n", type, attr);

drivers/hwmon/oxp-sensors.c new file +9 -0
+                     return -EOPNOTSUPP;
+             }
+     default:
+             dev_dbg(dev, "Unknown sensor type %d.\n", type);
+             return -EOPNOTSUPP;
+     }
+}
+
+static int oxp_platform_write(struct device *dev, enum

hwmon_sensor_types type,

drivers/hwmon/oxp-sensors.c new file +17 -0
+             u32 attr, int channel, long val)
+{
+     switch (type) {
+     case hwmon_pwm:
+             switch (attr) {
+             case hwmon_pwm_enable:
+                     if (val == 1)
+                             return oxp_pwm_enable(dev);
+                     else if (val == 0)
+                             return oxp_pwm_disable(dev);
+                     else
+                             return -EINVAL;
+             case hwmon_pwm_input:
+                     if (val < 0 || val > 255)
+                             return -EINVAL;
+                     val = (val * 100) / 255;
+                     return write_to_ec(dev, OXP_SENSOR_PWM_REG,

val);

drivers/hwmon/oxp-sensors.c new file +2 -0
+             default:
+                     dev_dbg(dev, "Unknown attribute for type %d: %d",

type, attr);
+ return -EOPNOTSUPP;

Quoted 8 lines

drivers/hwmon/oxp-sensors.c line 193

Quoted 6 lines
drivers/hwmon/oxp-sensors.c new file +6 -0
+             }
+     default:
+             dev_dbg(dev, "Unknown sensor type: %d", type);
+             return -EOPNOTSUPP;
+     }
+     return -EINVAL;

Can you actually hit this scenario? I would think not; you'll hit "default" and

return -EOPNOTSUPP.

Quoted 1 line

Maybe it's better to just drop the default label and then outside the

switch/case do this:

Quoted 4 lines
        dev_dbg(dev, "Unknown sensor type: %d", type);
        return -EOPNOTSUPP;

It shouldn't since there are no other attributes present. I can simplify this logic by a catch all case as you stated.

Quoted 28 lines
Quoted 28 lines
drivers/hwmon/oxp-sensors.c new file +28 -0
+}
+
+/* Known sensors in the OXP EC controllers */
+static const struct hwmon_channel_info *oxp_platform_sensors[] = {
+     HWMON_CHANNEL_INFO(fan,
+             HWMON_F_INPUT),
+     HWMON_CHANNEL_INFO(pwm,
+             HWMON_PWM_INPUT | HWMON_PWM_ENABLE),
+     NULL,
+};
+
+static const struct hwmon_ops oxp_ec_hwmon_ops = {
+     .is_visible = oxp_ec_hwmon_is_visible,
+     .read = oxp_platform_read,
+     .write = oxp_platform_write,
+};
+
+static const struct hwmon_chip_info oxp_ec_chip_info = {
+     .ops = &oxp_ec_hwmon_ops,
+     .info = oxp_platform_sensors,
+};
+
+/* Initialization logic */
+static int oxp_platform_probe(struct platform_device *pdev)
+{
+     const struct dmi_system_id *dmi_entry;
+     struct device *dev = &pdev->dev;
+     struct device *hwdev;
Quoted 8 lines

drivers/hwmon/oxp-sensors.c line 227

Quoted 6 lines
drivers/hwmon/oxp-sensors.c new file +6 -0
+     struct oxp_status *state;
+
+     /* Have to check for AMD processor here */
+     dmi_entry = dmi_first_match(dmi_table);
+     if (!dmi_entry || boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
+             return -ENODEV;

So it's shared DMI data values for the Intel and AMD variants of this

platform? What

Quoted 2 lines

happens if you run all this code on the Intel one?

Yeah... These devices have all the same DMI strings. In facto all the OneXPlayers have the same DMI model name also "ONEXPLAYER". Running this code on Intel won't work, the EC registers and values seem to differ.

Quoted 41 lines
Quoted 41 lines
drivers/hwmon/oxp-sensors.c new file +10 -0
+
+     state = devm_kzalloc(dev, sizeof(struct oxp_status), GFP_KERNEL);
+     if (!state)
+             return -ENOMEM;
+
+     state->lock_data.mutex = 0;
+     state->lock_data.lock = lock_global_acpi_lock;
+     state->lock_data.unlock = unlock_global_acpi_lock;
+
+     hwdev = devm_hwmon_device_register_with_info(dev, "oxpec",

state,
+ &oxp_ec_chip_info,
NULL);

drivers/hwmon/oxp-sensors.c new file +28 -0
+
+     return PTR_ERR_OR_ZERO(hwdev);
+}
+
+static struct platform_driver oxp_platform_driver = {
+     .driver = {
+             .name = "oxp-platform",
+     },
+     .probe = oxp_platform_probe,
+};
+
+static struct platform_device *oxp_platform_device;
+
+static int __init oxp_platform_init(void)
+{
+     oxp_platform_device =
+             platform_create_bundle(&oxp_platform_driver,
+                                    oxp_platform_probe, NULL, 0, NULL, 0);
+
+     if (IS_ERR(oxp_platform_device))
+             return PTR_ERR(oxp_platform_device);
+
+     return 0;
+}
+
+static void __exit oxp_platform_exit(void)
+{
+     platform_device_unregister(oxp_platform_device);
Quoted 8 lines

drivers/hwmon/oxp-sensors.c

Quoted 8 lines
drivers/hwmon/oxp-sensors.c new file +8 -0
+     platform_driver_unregister(&oxp_platform_driver);
+}
+
+MODULE_DEVICE_TABLE(dmi, dmi_table);
+module_init(oxp_platform_init);
+module_exit(oxp_platform_exit);
+
+MODULE_AUTHOR("Joaquín Ignacio Aramendía

<[email protected]>");

Quoted 5 lines
Quoted 5 lines
drivers/hwmon/oxp-sensors.c new file +3 -1
+MODULE_DESCRIPTION(
+     "Platform driver that handles ACPI EC of OneXPlayer devices");
+MODULE_LICENSE("GPL");
--

2.38.1

--
Joaquín I. Aramendía

#13 lore raw

Re: [PATCH v3] Add OneXPlayer mini AMD sensors driver

Hi again, Mario. Sad but true

Quoted 5 lines · Mario

Have you tried comparing all of the DMI strings? I'm really surprised that they would all be the same. My suspicion would be that at least the SKU string should be different. If it is, you should be able to drop the CPU match and use that instead.

I tried all DMI strings: Here is the SKU in particular:

# cat  /sys/class/dmi/id/product_sku
ONE XPLAYER

Is the same as product_name and board_name. product_family is empty, product version is "Default string", same as chassis_version and board_asset_tag. Yeah. These devices are a mess... Hope the next ones can be better and eventually they release a firmware upgrade to change the strings.

maintainer
#14 lore raw

Re: [PATCH v3] Add OneXPlayer mini AMD sensors driver

In reply to Joaquín Ignacio Aramendía
Quoted 7 lines · Joaquín Ignacio Aramendía

Sensors driver for OXP Handhelds from One-Netbook that expose fan reading and control via hwmon sysfs.

As far as I could gather all OXP boards have the same DMI strings and they are told appart by the boot cpu vendor (Intel/AMD). Currently only AMD boards are supported.

Quoted 5 lines

Fan control is provided via pwm interface in the range [0-255]. AMD boards have [0-100] as range in the EC, the written value is scaled to accommodate for that.

Please run "checkpatch --strict" on your patch and fix the CHECK messages. Also see Documentation/hwmon/submitting-patches.rst.

Quoted 8 lines

---
Removed fan_control reference in comment.
Bugfix MIX/MIN reporting not available
Bugfix pwm_enable register set wrong

---
 drivers/hwmon/Kconfig       |  13 +-
 drivers/hwmon/Makefile      |   1 +
 drivers/hwmon/oxp-sensors.c | 277 ++++++++++++++++++++++++++++++++++++

Also needs Documentation/hwmon/oxp-sensors.rst

Quoted 26 lines
 3 files changed, 290 insertions(+), 1 deletion(-)
 create mode 100644 drivers/hwmon/oxp-sensors.c
drivers/hwmon/Kconfig +11 -0
index 7ac3daaf59ce..a1cdb03b4d13 100644
@@ -1607,6 +1607,17 @@ config SENSORS_NZXT_SMART2
 
 source "drivers/hwmon/occ/Kconfig"
 
+config SENSORS_OXP
+	tristate "OneXPlayer EC fan control"
+	depends on ACPI
+	depends on X86
+	help
+		If you say yes here you get support for fan readings and control over
+		OneXPlayer handheld devices. Only OneXPlayer mini AMD handheld variant
+		boards are supported.
+
+		Can also be built as a module. In that case it will be called oxp-sensors.
+
 config SENSORS_PCF8591
 	tristate "Philips PCF8591 ADC/DAC"
 	depends on I2C

drivers/hwmon/Kconfig config SENSORS_ADS7871 · line 1971

Quoted 5 lines
drivers/hwmon/Kconfig +1 -1
@@ -1957,7 +1968,7 @@ config SENSORS_ADS7871
 
 config SENSORS_AMC6821
 	tristate "Texas Instruments AMC6821"
-	depends on I2C
+	depends on I2C

Please refrain from making unrelated changes. If you want to fix the extra space, submit a separate patch.

Quoted 72 lines
drivers/hwmon/Kconfig +0 -0
 	help
 	  If you say yes here you get support for the Texas Instruments
 	  AMC6821 hardware monitoring chips.
drivers/hwmon/Makefile +1 -0
index 11d076cad8a2..35824f8be455 100644
@@ -167,6 +167,7 @@ obj-$(CONFIG_SENSORS_NSA320)	+= nsa320-hwmon.o
 obj-$(CONFIG_SENSORS_NTC_THERMISTOR)	+= ntc_thermistor.o
 obj-$(CONFIG_SENSORS_NZXT_KRAKEN2) += nzxt-kraken2.o
 obj-$(CONFIG_SENSORS_NZXT_SMART2) += nzxt-smart2.o
+obj-$(CONFIG_SENSORS_OXP) += oxp-sensors.o
 obj-$(CONFIG_SENSORS_PC87360)	+= pc87360.o
 obj-$(CONFIG_SENSORS_PC87427)	+= pc87427.o
 obj-$(CONFIG_SENSORS_PCF8591)	+= pcf8591.o
drivers/hwmon/oxp-sensors.c new file +51 -0
new file mode 100644
index 000000000000..f5895dc11094
@@ -0,0 +1,277 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Platform driver for OXP Handhelds that expose fan reading and control
+ * via hwmon sysfs.
+ *
+ * All boards have the same DMI strings and they are told appart by the
+ * boot cpu vendor (Intel/AMD). Currently only AMD boards are supported
+ * but the code is made to be simple to add other handheld boards in the
+ * future.
+ * Fan control is provided via pwm interface in the range [0-255]. AMD
+ * boards use [0-100] as range in the EC, the written value is scaled to
+ * accommodate for that.
+ *
+ * Copyright (C) 2022 Joaquín I. Aramendía <[email protected]>
+ */
+
+#include <linux/acpi.h>
+#include <linux/dev_printk.h>
+#include <linux/dmi.h>
+#include <linux/hwmon.h>
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/processor.h>
+
+#define ACPI_LOCK_DELAY_MS	500
+
+/* Handle ACPI lock mechanism */
+struct lock_data {
+	u32 mutex;
+	bool (*lock)(struct lock_data *data);
+	bool (*unlock)(struct lock_data *data);
+};
+
+static bool lock_global_acpi_lock(struct lock_data *data)
+{
+	return ACPI_SUCCESS(acpi_acquire_global_lock(ACPI_LOCK_DELAY_MS,
+								 &data->mutex));
+}
+
+static bool unlock_global_acpi_lock(struct lock_data *data)
+{
+	return ACPI_SUCCESS(acpi_release_global_lock(data->mutex));
+}
+
+#define OXP_SENSOR_FAN_REG		0x76 /* Fan reading is 2 registers long */
+#define OXP_SENSOR_PWM_ENABLE_REG	0x4A /* PWM enable is 1 register long */
+#define OXP_SENSOR_PWM_REG		0x4B /* PWM reading is 1 register long */
+
+static const struct dmi_system_id dmi_table[] = {

drivers/hwmon/oxp-sensors.c line 61

Quoted 10 lines
drivers/hwmon/oxp-sensors.c new file +10 -0
+	{
+		.matches = {
+			DMI_EXACT_MATCH(DMI_BOARD_VENDOR,
+					"ONE-NETBOOK TECHNOLOGY CO., LTD."),
+		},
+	},
+	{
+		.matches = {
+			DMI_EXACT_MATCH(DMI_BOARD_VENDOR,
+					"ONE-NETBOOK"),

Are there any others devices which start with "ONE-NETBOOK" but are not supported ? If not a single entry with DMI_MATCH() woud be sufficient. Either case I would like to see some additional entry or entries here. Just matching on the vendor name seems risky. At the very least there should also be a match for the "ONE XPLAYER" sku.

Quoted 12 lines
drivers/hwmon/oxp-sensors.c new file +12 -0
+		},
+	},
+	{},
+};
+
+struct oxp_status {
+	struct lock_data lock_data;
+};
+
+/* Helper functions to handle EC read/write */
+static int read_from_ec(u8 reg, int size, long *val)
+{

drivers/hwmon/oxp-sensors.c line 83

Quoted 10 lines
drivers/hwmon/oxp-sensors.c new file +10 -0
+	int i;
+	int ret;
+	u8 buffer;
+
+	*val = 0;
+	for (i = 0; i < size; i++) {
+		ret = ec_read(reg + i, &buffer);
+		if (ret)
+			return ret;
+		(*val) <<= i * 8;

Unnecessary ( )

drivers/hwmon/oxp-sensors.c line 95

Quoted 12 lines
drivers/hwmon/oxp-sensors.c new file +12 -0
+		*val += buffer;
+	}
+	return ret;
+}
+
+static int write_to_ec(const struct device *dev, u8 reg, u8 value)
+{
+	struct oxp_status *state = dev_get_drvdata(dev);
+	int ret;
+
+	if (!state->lock_data.lock(&state->lock_data)) {
+		dev_warn(dev, "Failed to acquire mutex");

Is that message necessary ? If so it should be dev_err(). If it is expected, ie if acquiring the lock is observed to fail sometimes, there should be no log message.

drivers/hwmon/oxp-sensors.c line 102

Quoted 7 lines
drivers/hwmon/oxp-sensors.c new file +7 -0
+		return -EBUSY;
+	}
+
+	ret = ec_write(reg, value);
+
+	if (!state->lock_data.unlock(&state->lock_data))
+		dev_err(dev, "Failed to release mutex");

No error return ? Then it is not an error and should not be logged as one.

I am a bit concerned about those error messages. If they are seen the errors are either sporadic and there should be no log, or they are persistent and would clog the kernel log. If you think that will indeed happen, is not normal operation, and that the message is essential enough to be logged, please at least consider using _once variants of the message to avoid clogging the kernel log.

Quoted 31 lines
drivers/hwmon/oxp-sensors.c new file +31 -0
+
+	return ret;
+}
+
+static int oxp_pwm_enable(const struct device *dev)
+{
+	return write_to_ec(dev, OXP_SENSOR_PWM_ENABLE_REG, 0x01);
+}
+
+static int oxp_pwm_disable(const struct device *dev)
+{
+	return write_to_ec(dev, OXP_SENSOR_PWM_ENABLE_REG, 0x00);
+}
+
+/* Callbacks for hwmon interface */
+static umode_t oxp_ec_hwmon_is_visible(const void *drvdata,
+					enum hwmon_sensor_types type, u32 attr, int channel)
+{
+	switch (type) {
+	case hwmon_fan:
+		return 0444;
+	case hwmon_pwm:
+		return 0644;
+	default:
+		return 0;
+	}
+	return 0;
+}
+
+static int oxp_platform_read(struct device *dev, enum hwmon_sensor_types type,
+			     u32 attr, int channel, long *val)

drivers/hwmon/oxp-sensors.c line 143

Quoted 10 lines
drivers/hwmon/oxp-sensors.c new file +10 -0
+{
+	int ret;
+
+	switch (type) {
+	case hwmon_fan:
+		switch (attr) {
+		case hwmon_fan_input:
+			return read_from_ec(OXP_SENSOR_FAN_REG,
+					   2,
+					   val);

Unnecessary continuation lines

drivers/hwmon/oxp-sensors.c line 152

Quoted 9 lines
drivers/hwmon/oxp-sensors.c new file +9 -0
+		default:
+			dev_dbg(dev, "Unknown attribute for type %d: %d\n", type, attr);
+			return -EOPNOTSUPP;
+		}
+	case hwmon_pwm:
+		switch (attr) {
+		case hwmon_pwm_input:
+			ret = read_from_ec(OXP_SENSOR_PWM_REG,
+					   2, val);

Please, no unnecessary continuation lines, and make sure that continuation lines match with '(' in the preceding line.

Quoted 71 lines
drivers/hwmon/oxp-sensors.c new file +71 -0
+			*val = (*val * 255) / 100;
+			return ret;
+		case hwmon_pwm_enable:
+			return read_from_ec(OXP_SENSOR_PWM_ENABLE_REG, 1, val);
+		default:
+			dev_dbg(dev, "Unknown attribute for type %d: %d\n", type, attr);
+			return -EOPNOTSUPP;
+		}
+	default:
+		dev_dbg(dev, "Unknown sensor type %d.\n", type);
+		return -EOPNOTSUPP;
+	}
+}
+
+static int oxp_platform_write(struct device *dev, enum hwmon_sensor_types type,
+		u32 attr, int channel, long val)
+{
+	switch (type) {
+	case hwmon_pwm:
+		switch (attr) {
+		case hwmon_pwm_enable:
+			if (val == 1)
+				return oxp_pwm_enable(dev);
+			else if (val == 0)
+				return oxp_pwm_disable(dev);
+			else
+				return -EINVAL;
+		case hwmon_pwm_input:
+			if (val < 0 || val > 255)
+				return -EINVAL;
+			val = (val * 100) / 255;
+			return write_to_ec(dev, OXP_SENSOR_PWM_REG, val);
+		default:
+			dev_dbg(dev, "Unknown attribute for type %d: %d", type, attr);
+			return -EOPNOTSUPP;
+		}
+	default:
+		dev_dbg(dev, "Unknown sensor type: %d", type);
+		return -EOPNOTSUPP;
+	}
+	return -EINVAL;
+}
+
+/* Known sensors in the OXP EC controllers */
+static const struct hwmon_channel_info *oxp_platform_sensors[] = {
+	HWMON_CHANNEL_INFO(fan,
+		HWMON_F_INPUT),
+	HWMON_CHANNEL_INFO(pwm,
+		HWMON_PWM_INPUT | HWMON_PWM_ENABLE),
+	NULL,
+};
+
+static const struct hwmon_ops oxp_ec_hwmon_ops = {
+	.is_visible = oxp_ec_hwmon_is_visible,
+	.read = oxp_platform_read,
+	.write = oxp_platform_write,
+};
+
+static const struct hwmon_chip_info oxp_ec_chip_info = {
+	.ops = &oxp_ec_hwmon_ops,
+	.info = oxp_platform_sensors,
+};
+
+/* Initialization logic */
+static int oxp_platform_probe(struct platform_device *pdev)
+{
+	const struct dmi_system_id *dmi_entry;
+	struct device *dev = &pdev->dev;
+	struct device *hwdev;
+	struct oxp_status *state;
+

drivers/hwmon/oxp-sensors.c line 233

Quoted 10 lines
drivers/hwmon/oxp-sensors.c new file +10 -0
+	/* Have to check for AMD processor here */
+	dmi_entry = dmi_first_match(dmi_table);
+	if (!dmi_entry || boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
+		return -ENODEV;
+
+	state = devm_kzalloc(dev, sizeof(struct oxp_status), GFP_KERNEL);
+	if (!state)
+		return -ENOMEM;
+
+	state->lock_data.mutex = 0;

Unnecessary initialization. The data structure is initialized with devm_kzalloc().

drivers/hwmon/oxp-sensors.c line 235

Quoted 2 lines
drivers/hwmon/oxp-sensors.c new file +2 -0
+	state->lock_data.lock = lock_global_acpi_lock;
+	state->lock_data.unlock = unlock_global_acpi_lock;

What is the purpose of this callback function ? Why not just call the lock and unlock functions directly ?

Quoted 45 lines
drivers/hwmon/oxp-sensors.c new file +42 -1
+
+	hwdev = devm_hwmon_device_register_with_info(dev, "oxpec", state,
+							&oxp_ec_chip_info, NULL);
+
+	return PTR_ERR_OR_ZERO(hwdev);
+}
+
+static struct platform_driver oxp_platform_driver = {
+	.driver = {
+		.name = "oxp-platform",
+	},
+	.probe = oxp_platform_probe,
+};
+
+static struct platform_device *oxp_platform_device;
+
+static int __init oxp_platform_init(void)
+{
+	oxp_platform_device =
+		platform_create_bundle(&oxp_platform_driver,
+				       oxp_platform_probe, NULL, 0, NULL, 0);
+
+	if (IS_ERR(oxp_platform_device))
+		return PTR_ERR(oxp_platform_device);
+
+	return 0;
+}
+
+static void __exit oxp_platform_exit(void)
+{
+	platform_device_unregister(oxp_platform_device);
+	platform_driver_unregister(&oxp_platform_driver);
+}
+
+MODULE_DEVICE_TABLE(dmi, dmi_table);
+module_init(oxp_platform_init);
+module_exit(oxp_platform_exit);
+
+MODULE_AUTHOR("Joaquín Ignacio Aramendía <[email protected]>");
+MODULE_DESCRIPTION(
+	"Platform driver that handles ACPI EC of OneXPlayer devices");
+MODULE_LICENSE("GPL");
--

2.38.1

#15 lore raw

Re: [PATCH v3] Add OneXPlayer mini AMD sensors driver

Thanks for your time and review. I'll follow your advice and corrections. Should I send the next patch version in a separate thread? Or should I answer to this one?

Quoted 9 lines · Guenter Roeck
Quoted 7 lines · Joaquín Ignacio Aramendía

Sensors driver for OXP Handhelds from One-Netbook that expose fan reading and control via hwmon sysfs.

As far as I could gather all OXP boards have the same DMI strings and they are told appart by the boot cpu vendor (Intel/AMD). Currently only AMD boards are supported.

Quoted 8 lines
Quoted 5 lines

Fan control is provided via pwm interface in the range [0-255]. AMD boards have [0-100] as range in the EC, the written value is scaled to accommodate for that.

Please run "checkpatch --strict" on your patch and fix the CHECK messages. Also see Documentation/hwmon/submitting-patches.rst.

There is a Warning about MAINTAINERS to be updated. Should I add myself to it? If yes, Should it be under a new header?

Quoted 10 lines
Quoted 8 lines

---
Removed fan_control reference in comment.
Bugfix MIX/MIN reporting not available
Bugfix pwm_enable register set wrong

---
 drivers/hwmon/Kconfig       |  13 +-
 drivers/hwmon/Makefile      |   1 +
 drivers/hwmon/oxp-sensors.c | 277 ++++++++++++++++++++++++++++++++++++

Also needs Documentation/hwmon/oxp-sensors.rst

Will add.

Quoted 27 lines
Quoted 27 lines
 3 files changed, 290 insertions(+), 1 deletion(-)
 create mode 100644 drivers/hwmon/oxp-sensors.c
drivers/hwmon/Kconfig +11 -0
index 7ac3daaf59ce..a1cdb03b4d13 100644
@@ -1607,6 +1607,17 @@ config SENSORS_NZXT_SMART2
 
 source "drivers/hwmon/occ/Kconfig"
 
+config SENSORS_OXP
+     tristate "OneXPlayer EC fan control"
+     depends on ACPI
+     depends on X86
+     help
+             If you say yes here you get support for fan readings and control over
+             OneXPlayer handheld devices. Only OneXPlayer mini AMD handheld variant
+             boards are supported.
+
+             Can also be built as a module. In that case it will be called oxp-sensors.
+
 config SENSORS_PCF8591
      tristate "Philips PCF8591 ADC/DAC"
      depends on I2C
@@ -1957,7 +1968,7 @@ config SENSORS_ADS7871
 
Quoted 7 lines

drivers/hwmon/Kconfig config SENSORS_ADS7871 · line 1971

Quoted 4 lines
drivers/hwmon/Kconfig +1 -1
 config SENSORS_AMC6821
      tristate "Texas Instruments AMC6821"
-     depends on I2C
+     depends on I2C

Please refrain from making unrelated changes. If you want to fix the extra space, submit a separate patch.

Sorry this must have been vim removing trailing spaces. Will remove this chunk from the patch.

Quoted 80 lines
Quoted 80 lines
drivers/hwmon/Kconfig +0 -0
      help
        If you say yes here you get support for the Texas Instruments
        AMC6821 hardware monitoring chips.
drivers/hwmon/Makefile +1 -0
index 11d076cad8a2..35824f8be455 100644
@@ -167,6 +167,7 @@ obj-$(CONFIG_SENSORS_NSA320)      += nsa320-hwmon.o
 obj-$(CONFIG_SENSORS_NTC_THERMISTOR) += ntc_thermistor.o
 obj-$(CONFIG_SENSORS_NZXT_KRAKEN2) += nzxt-kraken2.o
 obj-$(CONFIG_SENSORS_NZXT_SMART2) += nzxt-smart2.o
+obj-$(CONFIG_SENSORS_OXP) += oxp-sensors.o
 obj-$(CONFIG_SENSORS_PC87360)        += pc87360.o
 obj-$(CONFIG_SENSORS_PC87427)        += pc87427.o
 obj-$(CONFIG_SENSORS_PCF8591)        += pcf8591.o
drivers/hwmon/oxp-sensors.c new file +59 -0
new file mode 100644
index 000000000000..f5895dc11094
@@ -0,0 +1,277 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Platform driver for OXP Handhelds that expose fan reading and control
+ * via hwmon sysfs.
+ *
+ * All boards have the same DMI strings and they are told appart by the
+ * boot cpu vendor (Intel/AMD). Currently only AMD boards are supported
+ * but the code is made to be simple to add other handheld boards in the
+ * future.
+ * Fan control is provided via pwm interface in the range [0-255]. AMD
+ * boards use [0-100] as range in the EC, the written value is scaled to
+ * accommodate for that.
+ *
+ * Copyright (C) 2022 Joaquín I. Aramendía <[email protected]>
+ */
+
+#include <linux/acpi.h>
+#include <linux/dev_printk.h>
+#include <linux/dmi.h>
+#include <linux/hwmon.h>
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/processor.h>
+
+#define ACPI_LOCK_DELAY_MS   500
+
+/* Handle ACPI lock mechanism */
+struct lock_data {
+     u32 mutex;
+     bool (*lock)(struct lock_data *data);
+     bool (*unlock)(struct lock_data *data);
+};
+
+static bool lock_global_acpi_lock(struct lock_data *data)
+{
+     return ACPI_SUCCESS(acpi_acquire_global_lock(ACPI_LOCK_DELAY_MS,
+                                                              &data->mutex));
+}
+
+static bool unlock_global_acpi_lock(struct lock_data *data)
+{
+     return ACPI_SUCCESS(acpi_release_global_lock(data->mutex));
+}
+
+#define OXP_SENSOR_FAN_REG           0x76 /* Fan reading is 2 registers long */
+#define OXP_SENSOR_PWM_ENABLE_REG    0x4A /* PWM enable is 1 register long */
+#define OXP_SENSOR_PWM_REG           0x4B /* PWM reading is 1 register long */
+
+static const struct dmi_system_id dmi_table[] = {
+     {
+             .matches = {
+                     DMI_EXACT_MATCH(DMI_BOARD_VENDOR,
+                                     "ONE-NETBOOK TECHNOLOGY CO., LTD."),
+             },
+     },
+     {
+             .matches = {
Quoted 8 lines

drivers/hwmon/oxp-sensors.c line 61

Quoted 2 lines
drivers/hwmon/oxp-sensors.c new file +2 -0
+                     DMI_EXACT_MATCH(DMI_BOARD_VENDOR,
+                                     "ONE-NETBOOK"),

Are there any others devices which start with "ONE-NETBOOK" but are not supported ? If not a single entry with DMI_MATCH() woud be sufficient. Either case I would like to see some additional entry or entries here. Just matching on the vendor name seems risky. At the very least there should also be a match for the "ONE XPLAYER" sku.

I would add a match for the board name instead of the sku if that is ok. The rest will be added.

Quoted 16 lines
Quoted 16 lines
drivers/hwmon/oxp-sensors.c new file +16 -0
+             },
+     },
+     {},
+};
+
+struct oxp_status {
+     struct lock_data lock_data;
+};
+
+/* Helper functions to handle EC read/write */
+static int read_from_ec(u8 reg, int size, long *val)
+{
+     int i;
+     int ret;
+     u8 buffer;
+
Quoted 8 lines

drivers/hwmon/oxp-sensors.c line 83

Quoted 6 lines
drivers/hwmon/oxp-sensors.c new file +6 -0
+     *val = 0;
+     for (i = 0; i < size; i++) {
+             ret = ec_read(reg + i, &buffer);
+             if (ret)
+                     return ret;
+             (*val) <<= i * 8;

Unnecessary ( )

Will remove.

Quoted 8 lines
Quoted 8 lines
drivers/hwmon/oxp-sensors.c new file +8 -0
+             *val += buffer;
+     }
+     return ret;
+}
+
+static int write_to_ec(const struct device *dev, u8 reg, u8 value)
+{
+     struct oxp_status *state = dev_get_drvdata(dev);
Quoted 8 lines

drivers/hwmon/oxp-sensors.c line 95

Quoted 4 lines
drivers/hwmon/oxp-sensors.c new file +4 -0
+     int ret;
+
+     if (!state->lock_data.lock(&state->lock_data)) {
+             dev_warn(dev, "Failed to acquire mutex");

Is that message necessary ? If so it should be dev_err(). If it is expected, ie if acquiring the lock is observed to fail sometimes, there should be no log message.

The messages are there in case this fails, never failed on me, honestly, but I've seen it in other ec-sensors drivers and adopted it as a "good practice", I guess? Anyway I'll add a _once error message and return error if it fails.

Quoted 11 lines

drivers/hwmon/oxp-sensors.c line 102

Quoted 7 lines
drivers/hwmon/oxp-sensors.c new file +7 -0
+             return -EBUSY;
+     }
+
+     ret = ec_write(reg, value);
+
+     if (!state->lock_data.unlock(&state->lock_data))
+             dev_err(dev, "Failed to release mutex");

No error return ? Then it is not an error and should not be logged as one.

Quoted 7 lines

I am a bit concerned about those error messages. If they are seen the errors are either sporadic and there should be no log, or they are persistent and would clog the kernel log. If you think that will indeed happen, is not normal operation, and that the message is essential enough to be logged, please at least consider using _once variants of the message to avoid clogging the kernel log.

Never saw those errors in about a month I used this driver on my own device. As said, I saw the practice in other drivers. I think the best way is to check for it and return an error while reporting it with the _once variant if that is ok.

Quoted 35 lines
Quoted 35 lines
drivers/hwmon/oxp-sensors.c new file +35 -0
+
+     return ret;
+}
+
+static int oxp_pwm_enable(const struct device *dev)
+{
+     return write_to_ec(dev, OXP_SENSOR_PWM_ENABLE_REG, 0x01);
+}
+
+static int oxp_pwm_disable(const struct device *dev)
+{
+     return write_to_ec(dev, OXP_SENSOR_PWM_ENABLE_REG, 0x00);
+}
+
+/* Callbacks for hwmon interface */
+static umode_t oxp_ec_hwmon_is_visible(const void *drvdata,
+                                     enum hwmon_sensor_types type, u32 attr, int channel)
+{
+     switch (type) {
+     case hwmon_fan:
+             return 0444;
+     case hwmon_pwm:
+             return 0644;
+     default:
+             return 0;
+     }
+     return 0;
+}
+
+static int oxp_platform_read(struct device *dev, enum hwmon_sensor_types type,
+                          u32 attr, int channel, long *val)
+{
+     int ret;
+
+     switch (type) {
Quoted 8 lines

drivers/hwmon/oxp-sensors.c line 143

Quoted 6 lines
drivers/hwmon/oxp-sensors.c new file +6 -0
+     case hwmon_fan:
+             switch (attr) {
+             case hwmon_fan_input:
+                     return read_from_ec(OXP_SENSOR_FAN_REG,
+                                        2,
+                                        val);

Unnecessary continuation lines

Ok. Will correct.

Quoted 10 lines

drivers/hwmon/oxp-sensors.c line 152

Quoted 9 lines
drivers/hwmon/oxp-sensors.c new file +9 -0
+             default:
+                     dev_dbg(dev, "Unknown attribute for type %d: %d\n", type, attr);
+                     return -EOPNOTSUPP;
+             }
+     case hwmon_pwm:
+             switch (attr) {
+             case hwmon_pwm_input:
+                     ret = read_from_ec(OXP_SENSOR_PWM_REG,
+                                        2, val);
Quoted 2 lines

Please, no unnecessary continuation lines, and make sure that continuation lines match with '(' in the preceding line.

Ok. Will correct.

Quoted 76 lines
Quoted 76 lines
drivers/hwmon/oxp-sensors.c new file +76 -0
+                     *val = (*val * 255) / 100;
+                     return ret;
+             case hwmon_pwm_enable:
+                     return read_from_ec(OXP_SENSOR_PWM_ENABLE_REG, 1, val);
+             default:
+                     dev_dbg(dev, "Unknown attribute for type %d: %d\n", type, attr);
+                     return -EOPNOTSUPP;
+             }
+     default:
+             dev_dbg(dev, "Unknown sensor type %d.\n", type);
+             return -EOPNOTSUPP;
+     }
+}
+
+static int oxp_platform_write(struct device *dev, enum hwmon_sensor_types type,
+             u32 attr, int channel, long val)
+{
+     switch (type) {
+     case hwmon_pwm:
+             switch (attr) {
+             case hwmon_pwm_enable:
+                     if (val == 1)
+                             return oxp_pwm_enable(dev);
+                     else if (val == 0)
+                             return oxp_pwm_disable(dev);
+                     else
+                             return -EINVAL;
+             case hwmon_pwm_input:
+                     if (val < 0 || val > 255)
+                             return -EINVAL;
+                     val = (val * 100) / 255;
+                     return write_to_ec(dev, OXP_SENSOR_PWM_REG, val);
+             default:
+                     dev_dbg(dev, "Unknown attribute for type %d: %d", type, attr);
+                     return -EOPNOTSUPP;
+             }
+     default:
+             dev_dbg(dev, "Unknown sensor type: %d", type);
+             return -EOPNOTSUPP;
+     }
+     return -EINVAL;
+}
+
+/* Known sensors in the OXP EC controllers */
+static const struct hwmon_channel_info *oxp_platform_sensors[] = {
+     HWMON_CHANNEL_INFO(fan,
+             HWMON_F_INPUT),
+     HWMON_CHANNEL_INFO(pwm,
+             HWMON_PWM_INPUT | HWMON_PWM_ENABLE),
+     NULL,
+};
+
+static const struct hwmon_ops oxp_ec_hwmon_ops = {
+     .is_visible = oxp_ec_hwmon_is_visible,
+     .read = oxp_platform_read,
+     .write = oxp_platform_write,
+};
+
+static const struct hwmon_chip_info oxp_ec_chip_info = {
+     .ops = &oxp_ec_hwmon_ops,
+     .info = oxp_platform_sensors,
+};
+
+/* Initialization logic */
+static int oxp_platform_probe(struct platform_device *pdev)
+{
+     const struct dmi_system_id *dmi_entry;
+     struct device *dev = &pdev->dev;
+     struct device *hwdev;
+     struct oxp_status *state;
+
+     /* Have to check for AMD processor here */
+     dmi_entry = dmi_first_match(dmi_table);
+     if (!dmi_entry || boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
+             return -ENODEV;
+
Quoted 8 lines

drivers/hwmon/oxp-sensors.c line 233

Quoted 5 lines
drivers/hwmon/oxp-sensors.c new file +5 -0
+     state = devm_kzalloc(dev, sizeof(struct oxp_status), GFP_KERNEL);
+     if (!state)
+             return -ENOMEM;
+
+     state->lock_data.mutex = 0;

Unnecessary initialization. The data structure is initialized with devm_kzalloc().

Ok. Will remove.

Quoted 5 lines

drivers/hwmon/oxp-sensors.c line 235

Quoted 2 lines
drivers/hwmon/oxp-sensors.c new file +2 -0
+     state->lock_data.lock = lock_global_acpi_lock;
+     state->lock_data.unlock = unlock_global_acpi_lock;

What is the purpose of this callback function ? Why not just call the lock and unlock functions directly ?

Can be called directly and simplify the structure a little. Will change it.

Quoted 37 lines
Quoted 37 lines
drivers/hwmon/oxp-sensors.c new file +37 -0
+
+     hwdev = devm_hwmon_device_register_with_info(dev, "oxpec", state,
+                                                     &oxp_ec_chip_info, NULL);
+
+     return PTR_ERR_OR_ZERO(hwdev);
+}
+
+static struct platform_driver oxp_platform_driver = {
+     .driver = {
+             .name = "oxp-platform",
+     },
+     .probe = oxp_platform_probe,
+};
+
+static struct platform_device *oxp_platform_device;
+
+static int __init oxp_platform_init(void)
+{
+     oxp_platform_device =
+             platform_create_bundle(&oxp_platform_driver,
+                                    oxp_platform_probe, NULL, 0, NULL, 0);
+
+     if (IS_ERR(oxp_platform_device))
+             return PTR_ERR(oxp_platform_device);
+
+     return 0;
+}
+
+static void __exit oxp_platform_exit(void)
+{
+     platform_device_unregister(oxp_platform_device);
+     platform_driver_unregister(&oxp_platform_driver);
+}
+
+MODULE_DEVICE_TABLE(dmi, dmi_table);
+module_init(oxp_platform_init);
+module_exit(oxp_platform_exit);
Quoted 8 lines
Quoted 8 lines
drivers/hwmon/oxp-sensors.c new file +5 -1
+
+MODULE_AUTHOR("Joaquín Ignacio Aramendía <[email protected]>");
+MODULE_DESCRIPTION(
+     "Platform driver that handles ACPI EC of OneXPlayer devices");
+MODULE_LICENSE("GPL");
--

2.38.1

Thanks again :)

Signature · 1 line
Joaquín I. Aramendía
maintainer
#16 lore raw

Re: [PATCH v3] Add OneXPlayer mini AMD sensors driver

Quoted 18 lines · Joaquin Aramendia

Thanks for your time and review. I'll follow your advice and corrections. Should I send the next patch version in a separate thread? Or should I answer to this one?

Quoted 13 lines · Guenter Roeck
Quoted 11 lines · Joaquín Ignacio Aramendía

Sensors driver for OXP Handhelds from One-Netbook that expose fan reading and control via hwmon sysfs.

As far as I could gather all OXP boards have the same DMI strings and they are told appart by the boot cpu vendor (Intel/AMD). Currently only AMD boards are supported.

Fan control is provided via pwm interface in the range [0-255]. AMD boards have [0-100] as range in the EC, the written value is scaled to accommodate for that.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Please run "checkpatch --strict" on your patch and fix the CHECK messages. Also see Documentation/hwmon/submitting-patches.rst.

There is a Warning about MAINTAINERS to be updated. Should I add myself to it? If yes, Should it be under a new header?

Feel free to do so if you like, but that one isn't mandatory.

Quoted 140 lines
Quoted 10 lines
Quoted 8 lines

---
Removed fan_control reference in comment.
Bugfix MIX/MIN reporting not available
Bugfix pwm_enable register set wrong

---
 drivers/hwmon/Kconfig       |  13 +-
 drivers/hwmon/Makefile      |   1 +
 drivers/hwmon/oxp-sensors.c | 277 ++++++++++++++++++++++++++++++++++++

Also needs Documentation/hwmon/oxp-sensors.rst

Will add.

Quoted 27 lines
Quoted 27 lines
 3 files changed, 290 insertions(+), 1 deletion(-)
 create mode 100644 drivers/hwmon/oxp-sensors.c
drivers/hwmon/Kconfig +11 -0
index 7ac3daaf59ce..a1cdb03b4d13 100644
@@ -1607,6 +1607,17 @@ config SENSORS_NZXT_SMART2
 
 source "drivers/hwmon/occ/Kconfig"
 
+config SENSORS_OXP
+     tristate "OneXPlayer EC fan control"
+     depends on ACPI
+     depends on X86
+     help
+             If you say yes here you get support for fan readings and control over
+             OneXPlayer handheld devices. Only OneXPlayer mini AMD handheld variant
+             boards are supported.
+
+             Can also be built as a module. In that case it will be called oxp-sensors.
+
 config SENSORS_PCF8591
      tristate "Philips PCF8591 ADC/DAC"
      depends on I2C
@@ -1957,7 +1968,7 @@ config SENSORS_ADS7871
 
Quoted 7 lines

drivers/hwmon/Kconfig config SENSORS_ADS7871 · line 1971

Quoted 4 lines
drivers/hwmon/Kconfig +1 -1
 config SENSORS_AMC6821
      tristate "Texas Instruments AMC6821"
-     depends on I2C
+     depends on I2C

Please refrain from making unrelated changes. If you want to fix the extra space, submit a separate patch.

Sorry this must have been vim removing trailing spaces. Will remove this chunk from the patch.

Quoted 80 lines
Quoted 80 lines
drivers/hwmon/Kconfig +0 -0
      help
        If you say yes here you get support for the Texas Instruments
        AMC6821 hardware monitoring chips.
drivers/hwmon/Makefile +1 -0
index 11d076cad8a2..35824f8be455 100644
@@ -167,6 +167,7 @@ obj-$(CONFIG_SENSORS_NSA320)      += nsa320-hwmon.o
 obj-$(CONFIG_SENSORS_NTC_THERMISTOR) += ntc_thermistor.o
 obj-$(CONFIG_SENSORS_NZXT_KRAKEN2) += nzxt-kraken2.o
 obj-$(CONFIG_SENSORS_NZXT_SMART2) += nzxt-smart2.o
+obj-$(CONFIG_SENSORS_OXP) += oxp-sensors.o
 obj-$(CONFIG_SENSORS_PC87360)        += pc87360.o
 obj-$(CONFIG_SENSORS_PC87427)        += pc87427.o
 obj-$(CONFIG_SENSORS_PCF8591)        += pcf8591.o
drivers/hwmon/oxp-sensors.c new file +59 -0
new file mode 100644
index 000000000000..f5895dc11094
@@ -0,0 +1,277 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Platform driver for OXP Handhelds that expose fan reading and control
+ * via hwmon sysfs.
+ *
+ * All boards have the same DMI strings and they are told appart by the
+ * boot cpu vendor (Intel/AMD). Currently only AMD boards are supported
+ * but the code is made to be simple to add other handheld boards in the
+ * future.
+ * Fan control is provided via pwm interface in the range [0-255]. AMD
+ * boards use [0-100] as range in the EC, the written value is scaled to
+ * accommodate for that.
+ *
+ * Copyright (C) 2022 Joaquín I. Aramendía <[email protected]>
+ */
+
+#include <linux/acpi.h>
+#include <linux/dev_printk.h>
+#include <linux/dmi.h>
+#include <linux/hwmon.h>
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/processor.h>
+
+#define ACPI_LOCK_DELAY_MS   500
+
+/* Handle ACPI lock mechanism */
+struct lock_data {
+     u32 mutex;
+     bool (*lock)(struct lock_data *data);
+     bool (*unlock)(struct lock_data *data);
+};
+
+static bool lock_global_acpi_lock(struct lock_data *data)
+{
+     return ACPI_SUCCESS(acpi_acquire_global_lock(ACPI_LOCK_DELAY_MS,
+                                                              &data->mutex));
+}
+
+static bool unlock_global_acpi_lock(struct lock_data *data)
+{
+     return ACPI_SUCCESS(acpi_release_global_lock(data->mutex));
+}
+
+#define OXP_SENSOR_FAN_REG           0x76 /* Fan reading is 2 registers long */
+#define OXP_SENSOR_PWM_ENABLE_REG    0x4A /* PWM enable is 1 register long */
+#define OXP_SENSOR_PWM_REG           0x4B /* PWM reading is 1 register long */
+
+static const struct dmi_system_id dmi_table[] = {
+     {
+             .matches = {
+                     DMI_EXACT_MATCH(DMI_BOARD_VENDOR,
+                                     "ONE-NETBOOK TECHNOLOGY CO., LTD."),
+             },
+     },
+     {
+             .matches = {
Quoted 8 lines

drivers/hwmon/oxp-sensors.c line 61

Quoted 2 lines
drivers/hwmon/oxp-sensors.c new file +2 -0
+                     DMI_EXACT_MATCH(DMI_BOARD_VENDOR,
+                                     "ONE-NETBOOK"),

Are there any others devices which start with "ONE-NETBOOK" but are not supported ? If not a single entry with DMI_MATCH() woud be sufficient. Either case I would like to see some additional entry or entries here. Just matching on the vendor name seems risky. At the very least there should also be a match for the "ONE XPLAYER" sku.

Quoted 3 lines

I would add a match for the board name instead of the sku if that is ok. The rest will be added.

Sure, if it contains useful information.

Quoted 44 lines
Quoted 16 lines
Quoted 16 lines
drivers/hwmon/oxp-sensors.c new file +16 -0
+             },
+     },
+     {},
+};
+
+struct oxp_status {
+     struct lock_data lock_data;
+};
+
+/* Helper functions to handle EC read/write */
+static int read_from_ec(u8 reg, int size, long *val)
+{
+     int i;
+     int ret;
+     u8 buffer;
+
Quoted 8 lines

drivers/hwmon/oxp-sensors.c line 83

Quoted 6 lines
drivers/hwmon/oxp-sensors.c new file +6 -0
+     *val = 0;
+     for (i = 0; i < size; i++) {
+             ret = ec_read(reg + i, &buffer);
+             if (ret)
+                     return ret;
+             (*val) <<= i * 8;

Unnecessary ( )

Will remove.

Quoted 8 lines
Quoted 8 lines
drivers/hwmon/oxp-sensors.c new file +8 -0
+             *val += buffer;
+     }
+     return ret;
+}
+
+static int write_to_ec(const struct device *dev, u8 reg, u8 value)
+{
+     struct oxp_status *state = dev_get_drvdata(dev);
Quoted 8 lines

drivers/hwmon/oxp-sensors.c line 95

Quoted 4 lines
drivers/hwmon/oxp-sensors.c new file +4 -0
+     int ret;
+
+     if (!state->lock_data.lock(&state->lock_data)) {
+             dev_warn(dev, "Failed to acquire mutex");

Is that message necessary ? If so it should be dev_err(). If it is expected, ie if acquiring the lock is observed to fail sometimes, there should be no log message.

Quoted 4 lines

The messages are there in case this fails, never failed on me, honestly, but I've seen it in other ec-sensors drivers and adopted it as a "good practice", I guess? Anyway I'll add a _once error message and return error if it fails.

It is not a good practice, it is developers inisisting to add noise. It makes me cringe each time I see it, but I often let it go because arguing about it is not worth wasting my time.

Quoted 20 lines
Quoted 12 lines

drivers/hwmon/oxp-sensors.c line 102

Quoted 8 lines
drivers/hwmon/oxp-sensors.c new file +7 -0
+             return -EBUSY;
+     }
+
+     ret = ec_write(reg, value);
+
+     if (!state->lock_data.unlock(&state->lock_data))
+             dev_err(dev, "Failed to release mutex");

No error return ? Then it is not an error and should not be logged as one.

Quoted 7 lines

I am a bit concerned about those error messages. If they are seen the errors are either sporadic and there should be no log, or they are persistent and would clog the kernel log. If you think that will indeed happen, is not normal operation, and that the message is essential enough to be logged, please at least consider using _once variants of the message to avoid clogging the kernel log.

Quoted 5 lines

Never saw those errors in about a month I used this driver on my own device. As said, I saw the practice in other drivers. I think the best way is to check for it and return an error while reporting it with the _once variant if that is ok.

I'd prefer to not have a message at all, but then again it is not worth wasting my time over it.

Thanks,
Guenter

#17 lore raw
In reply to Guenter Roeck

Hello again and thanks for the review. I submitted the patch again and needs some polish before it can be accepted. I had one question left:

Quoted 13 lines · Guenter Roeck
Quoted 13 lines
Quoted 12 lines

drivers/platform/x86/oxp-platform.c line 166

Quoted 10 lines
drivers/platform/x86/oxp-platform.c new file +9 -0
+/* Callbacks for hwmon interface */
+static umode_t oxp_ec_hwmon_is_visible(const void *drvdata,
+                                     enum hwmon_sensor_types type, u32 attr, int channel)
+{
+     switch (type) {
+             case hwmon_fan:
+                     return S_IRUGO;
+             case hwmon_pwm:
+                     return S_IRUGO | S_IWUSR;

Please use 0444 and 0644 directly. Checkpatch will tell.

Quoted 6 lines
Quoted 3 lines

Oh. I did as the documentation suggested. I must confess I didn't run checkpatch, will don in the next submission.

That is long ago. Octal values are and have been preferred for several years.

I've read this form here[1]. Should I send a patch to the Documentation to reflect the preference?

[1]https://www.kernel.org/doc/html/latest/hwmon/hwmon-kernel-api.html#driver-callback-functions

maintainer
#18 lore raw
Quoted 21 lines · Joaquin Aramendia

Hello again and thanks for the review. I submitted the patch again and needs some polish before it can be accepted. I had one question left:

Quoted 17 lines · Guenter Roeck
Quoted 16 lines
Quoted 12 lines

drivers/platform/x86/oxp-platform.c line 166

Quoted 10 lines
drivers/platform/x86/oxp-platform.c new file +9 -0
+/* Callbacks for hwmon interface */
+static umode_t oxp_ec_hwmon_is_visible(const void *drvdata,
+                                     enum hwmon_sensor_types type, u32 attr, int channel)
+{
+     switch (type) {
+             case hwmon_fan:
+                     return S_IRUGO;
+             case hwmon_pwm:
+                     return S_IRUGO | S_IWUSR;

Please use 0444 and 0644 directly. Checkpatch will tell.

Oh. I did as the documentation suggested. I must confess I didn't run checkpatch, will don in the next submission.

Quoted 6 lines
Quoted 2 lines

That is long ago. Octal values are and have been preferred for several years.

I've read this form here[1]. Should I send a patch to the Documentation to reflect the preference?

Sure, patches are always welcome.

Guenter