1 // SPDX-License-Identifier: GPL-2.0+
3 * Platform driver for OneXPlayer, AOK ZOE, and Aya Neo Handhelds that expose
4 * fan reading and control via hwmon sysfs.
6 * Old OXP boards have the same DMI strings and they are told apart by
7 * the boot cpu vendor (Intel/AMD). Currently only AMD boards are
8 * supported but the code is made to be simple to add other handheld
9 * boards in the future.
10 * Fan control is provided via pwm interface in the range [0-255].
11 * Old AMD boards use [0-100] as range in the EC, the written value is
12 * scaled to accommodate for that. Newer boards like the mini PRO and
13 * AOK ZOE are not scaled but have the same EC layout.
15 * Copyright (C) 2022 Joaquín I. Aramendía <samsagax@gmail.com>
18 #include <linux/acpi.h>
19 #include <linux/dev_printk.h>
20 #include <linux/dmi.h>
21 #include <linux/hwmon.h>
22 #include <linux/init.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/processor.h>
28 /* Handle ACPI lock mechanism */
31 #define ACPI_LOCK_DELAY_MS 500
33 static bool lock_global_acpi_lock(void)
35 return ACPI_SUCCESS(acpi_acquire_global_lock(ACPI_LOCK_DELAY_MS, &oxp_mutex));
38 static bool unlock_global_acpi_lock(void)
40 return ACPI_SUCCESS(acpi_release_global_lock(oxp_mutex));
51 static enum oxp_board board;
53 #define OXP_SENSOR_FAN_REG 0x76 /* Fan reading is 2 registers long */
54 #define OXP_SENSOR_PWM_ENABLE_REG 0x4A /* PWM enable is 1 register long */
55 #define OXP_SENSOR_PWM_REG 0x4B /* PWM reading is 1 register long */
57 static const struct dmi_system_id dmi_table[] = {
60 DMI_MATCH(DMI_BOARD_VENDOR, "AOKZOE"),
61 DMI_EXACT_MATCH(DMI_BOARD_NAME, "AOKZOE A1 AR07"),
63 .driver_data = (void *) &(enum oxp_board) {aok_zoe_a1},
67 DMI_MATCH(DMI_BOARD_VENDOR, "AYANEO"),
68 DMI_EXACT_MATCH(DMI_BOARD_NAME, "AIR"),
70 .driver_data = (void *) &(enum oxp_board) {aya_neo_air},
74 DMI_MATCH(DMI_BOARD_VENDOR, "AYANEO"),
75 DMI_EXACT_MATCH(DMI_BOARD_NAME, "AIR Pro"),
77 .driver_data = (void *) &(enum oxp_board) {aya_neo_air_pro},
81 DMI_MATCH(DMI_BOARD_VENDOR, "ONE-NETBOOK"),
82 DMI_EXACT_MATCH(DMI_BOARD_NAME, "ONE XPLAYER"),
84 .driver_data = (void *) &(enum oxp_board) {oxp_mini_amd},
88 DMI_MATCH(DMI_BOARD_VENDOR, "ONE-NETBOOK"),
89 DMI_EXACT_MATCH(DMI_BOARD_NAME, "ONEXPLAYER Mini Pro"),
91 .driver_data = (void *) &(enum oxp_board) {oxp_mini_amd_pro},
96 /* Helper functions to handle EC read/write */
97 static int read_from_ec(u8 reg, int size, long *val)
103 if (!lock_global_acpi_lock())
107 for (i = 0; i < size; i++) {
108 ret = ec_read(reg + i, &buffer);
115 if (!unlock_global_acpi_lock())
121 static int write_to_ec(const struct device *dev, u8 reg, u8 value)
125 if (!lock_global_acpi_lock())
128 ret = ec_write(reg, value);
130 if (!unlock_global_acpi_lock())
136 static int oxp_pwm_enable(const struct device *dev)
138 return write_to_ec(dev, OXP_SENSOR_PWM_ENABLE_REG, 0x01);
141 static int oxp_pwm_disable(const struct device *dev)
143 return write_to_ec(dev, OXP_SENSOR_PWM_ENABLE_REG, 0x00);
146 /* Callbacks for hwmon interface */
147 static umode_t oxp_ec_hwmon_is_visible(const void *drvdata,
148 enum hwmon_sensor_types type, u32 attr, int channel)
160 static int oxp_platform_read(struct device *dev, enum hwmon_sensor_types type,
161 u32 attr, int channel, long *val)
168 case hwmon_fan_input:
169 return read_from_ec(OXP_SENSOR_FAN_REG, 2, val);
176 case hwmon_pwm_input:
177 ret = read_from_ec(OXP_SENSOR_PWM_REG, 1, val);
182 case aya_neo_air_pro:
184 *val = (*val * 255) / 100;
186 case oxp_mini_amd_pro:
192 case hwmon_pwm_enable:
193 return read_from_ec(OXP_SENSOR_PWM_ENABLE_REG, 1, val);
204 static int oxp_platform_write(struct device *dev, enum hwmon_sensor_types type,
205 u32 attr, int channel, long val)
210 case hwmon_pwm_enable:
212 return oxp_pwm_enable(dev);
214 return oxp_pwm_disable(dev);
216 case hwmon_pwm_input:
217 if (val < 0 || val > 255)
221 case aya_neo_air_pro:
223 val = (val * 100) / 255;
226 case oxp_mini_amd_pro:
230 return write_to_ec(dev, OXP_SENSOR_PWM_REG, val);
241 /* Known sensors in the OXP EC controllers */
242 static const struct hwmon_channel_info * const oxp_platform_sensors[] = {
243 HWMON_CHANNEL_INFO(fan,
245 HWMON_CHANNEL_INFO(pwm,
246 HWMON_PWM_INPUT | HWMON_PWM_ENABLE),
250 static const struct hwmon_ops oxp_ec_hwmon_ops = {
251 .is_visible = oxp_ec_hwmon_is_visible,
252 .read = oxp_platform_read,
253 .write = oxp_platform_write,
256 static const struct hwmon_chip_info oxp_ec_chip_info = {
257 .ops = &oxp_ec_hwmon_ops,
258 .info = oxp_platform_sensors,
261 /* Initialization logic */
262 static int oxp_platform_probe(struct platform_device *pdev)
264 const struct dmi_system_id *dmi_entry;
265 struct device *dev = &pdev->dev;
266 struct device *hwdev;
269 * Have to check for AMD processor here because DMI strings are the
270 * same between Intel and AMD boards, the only way to tell them apart
272 * Intel boards seem to have different EC registers and values to
275 dmi_entry = dmi_first_match(dmi_table);
276 if (!dmi_entry || boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
279 board = *((enum oxp_board *) dmi_entry->driver_data);
281 hwdev = devm_hwmon_device_register_with_info(dev, "oxpec", NULL,
282 &oxp_ec_chip_info, NULL);
284 return PTR_ERR_OR_ZERO(hwdev);
287 static struct platform_driver oxp_platform_driver = {
289 .name = "oxp-platform",
291 .probe = oxp_platform_probe,
294 static struct platform_device *oxp_platform_device;
296 static int __init oxp_platform_init(void)
298 oxp_platform_device =
299 platform_create_bundle(&oxp_platform_driver,
300 oxp_platform_probe, NULL, 0, NULL, 0);
302 return PTR_ERR_OR_ZERO(oxp_platform_device);
305 static void __exit oxp_platform_exit(void)
307 platform_device_unregister(oxp_platform_device);
308 platform_driver_unregister(&oxp_platform_driver);
311 MODULE_DEVICE_TABLE(dmi, dmi_table);
313 module_init(oxp_platform_init);
314 module_exit(oxp_platform_exit);
316 MODULE_AUTHOR("Joaquín Ignacio Aramendía <samsagax@gmail.com>");
317 MODULE_DESCRIPTION("Platform driver that handles EC sensors of OneXPlayer devices");
318 MODULE_LICENSE("GPL");