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.
18 #include <linux/acpi.h>
19 #include <linux/dmi.h>
20 #include <linux/hwmon.h>
21 #include <linux/init.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/platform_device.h>
25 #include <linux/processor.h>
27 /* Handle ACPI lock mechanism */
30 #define ACPI_LOCK_DELAY_MS 500
32 static bool lock_global_acpi_lock(void)
34 return ACPI_SUCCESS(acpi_acquire_global_lock(ACPI_LOCK_DELAY_MS, &oxp_mutex));
37 static bool unlock_global_acpi_lock(void)
39 return ACPI_SUCCESS(acpi_release_global_lock(oxp_mutex));
46 aya_neo_air_plus_mendo,
54 static enum oxp_board board;
56 /* Fan reading and PWM */
57 #define OXP_SENSOR_FAN_REG 0x76 /* Fan reading is 2 registers long */
58 #define OXP_SENSOR_PWM_ENABLE_REG 0x4A /* PWM enable is 1 register long */
59 #define OXP_SENSOR_PWM_REG 0x4B /* PWM reading is 1 register long */
61 /* Turbo button takeover function
62 * Older boards have different values and EC registers
63 * for the same function
65 #define OXP_OLD_TURBO_SWITCH_REG 0x1E
66 #define OXP_OLD_TURBO_TAKE_VAL 0x01
67 #define OXP_OLD_TURBO_RETURN_VAL 0x00
69 #define OXP_TURBO_SWITCH_REG 0xF1
70 #define OXP_TURBO_TAKE_VAL 0x40
71 #define OXP_TURBO_RETURN_VAL 0x00
73 static const struct dmi_system_id dmi_table[] = {
76 DMI_MATCH(DMI_BOARD_VENDOR, "AOKZOE"),
77 DMI_EXACT_MATCH(DMI_BOARD_NAME, "AOKZOE A1 AR07"),
79 .driver_data = (void *)aok_zoe_a1,
83 DMI_MATCH(DMI_BOARD_VENDOR, "AOKZOE"),
84 DMI_EXACT_MATCH(DMI_BOARD_NAME, "AOKZOE A1 Pro"),
86 .driver_data = (void *)aok_zoe_a1,
90 DMI_MATCH(DMI_BOARD_VENDOR, "AYANEO"),
91 DMI_EXACT_MATCH(DMI_BOARD_NAME, "AYANEO 2"),
93 .driver_data = (void *)aya_neo_2,
97 DMI_MATCH(DMI_BOARD_VENDOR, "AYANEO"),
98 DMI_EXACT_MATCH(DMI_BOARD_NAME, "AIR"),
100 .driver_data = (void *)aya_neo_air,
104 DMI_MATCH(DMI_BOARD_VENDOR, "AYANEO"),
105 DMI_EXACT_MATCH(DMI_BOARD_NAME, "AB05-Mendocino"),
107 .driver_data = (void *)aya_neo_air_plus_mendo,
111 DMI_MATCH(DMI_BOARD_VENDOR, "AYANEO"),
112 DMI_EXACT_MATCH(DMI_BOARD_NAME, "AIR Pro"),
114 .driver_data = (void *)aya_neo_air_pro,
118 DMI_MATCH(DMI_BOARD_VENDOR, "AYANEO"),
119 DMI_EXACT_MATCH(DMI_BOARD_NAME, "GEEK"),
121 .driver_data = (void *)aya_neo_geek,
125 DMI_MATCH(DMI_BOARD_VENDOR, "ONE-NETBOOK"),
126 DMI_EXACT_MATCH(DMI_BOARD_NAME, "ONE XPLAYER"),
128 .driver_data = (void *)oxp_mini_amd,
132 DMI_MATCH(DMI_BOARD_VENDOR, "ONE-NETBOOK"),
133 DMI_EXACT_MATCH(DMI_BOARD_NAME, "ONEXPLAYER mini A07"),
135 .driver_data = (void *)oxp_mini_amd_a07,
139 DMI_MATCH(DMI_BOARD_VENDOR, "ONE-NETBOOK"),
140 DMI_EXACT_MATCH(DMI_BOARD_NAME, "ONEXPLAYER Mini Pro"),
142 .driver_data = (void *)oxp_mini_amd_pro,
147 /* Helper functions to handle EC read/write */
148 static int read_from_ec(u8 reg, int size, long *val)
154 if (!lock_global_acpi_lock())
158 for (i = 0; i < size; i++) {
159 ret = ec_read(reg + i, &buffer);
166 if (!unlock_global_acpi_lock())
172 static int write_to_ec(u8 reg, u8 value)
176 if (!lock_global_acpi_lock())
179 ret = ec_write(reg, value);
181 if (!unlock_global_acpi_lock())
187 /* Turbo button toggle functions */
188 static int tt_toggle_enable(void)
194 case oxp_mini_amd_a07:
195 reg = OXP_OLD_TURBO_SWITCH_REG;
196 val = OXP_OLD_TURBO_TAKE_VAL;
198 case oxp_mini_amd_pro:
200 reg = OXP_TURBO_SWITCH_REG;
201 val = OXP_TURBO_TAKE_VAL;
206 return write_to_ec(reg, val);
209 static int tt_toggle_disable(void)
215 case oxp_mini_amd_a07:
216 reg = OXP_OLD_TURBO_SWITCH_REG;
217 val = OXP_OLD_TURBO_RETURN_VAL;
219 case oxp_mini_amd_pro:
221 reg = OXP_TURBO_SWITCH_REG;
222 val = OXP_TURBO_RETURN_VAL;
227 return write_to_ec(reg, val);
230 /* Callbacks for turbo toggle attribute */
231 static umode_t tt_toggle_is_visible(struct kobject *kobj,
232 struct attribute *attr, int n)
236 case oxp_mini_amd_a07:
237 case oxp_mini_amd_pro:
245 static ssize_t tt_toggle_store(struct device *dev,
246 struct device_attribute *attr, const char *buf,
252 rval = kstrtobool(buf, &value);
257 rval = tt_toggle_enable();
259 rval = tt_toggle_disable();
267 static ssize_t tt_toggle_show(struct device *dev,
268 struct device_attribute *attr, char *buf)
275 case oxp_mini_amd_a07:
276 reg = OXP_OLD_TURBO_SWITCH_REG;
278 case oxp_mini_amd_pro:
280 reg = OXP_TURBO_SWITCH_REG;
286 retval = read_from_ec(reg, 1, &val);
290 return sysfs_emit(buf, "%d\n", !!val);
293 static DEVICE_ATTR_RW(tt_toggle);
295 /* PWM enable/disable functions */
296 static int oxp_pwm_enable(void)
298 return write_to_ec(OXP_SENSOR_PWM_ENABLE_REG, 0x01);
301 static int oxp_pwm_disable(void)
303 return write_to_ec(OXP_SENSOR_PWM_ENABLE_REG, 0x00);
306 /* Callbacks for hwmon interface */
307 static umode_t oxp_ec_hwmon_is_visible(const void *drvdata,
308 enum hwmon_sensor_types type, u32 attr, int channel)
320 static int oxp_platform_read(struct device *dev, enum hwmon_sensor_types type,
321 u32 attr, int channel, long *val)
328 case hwmon_fan_input:
329 return read_from_ec(OXP_SENSOR_FAN_REG, 2, val);
336 case hwmon_pwm_input:
337 ret = read_from_ec(OXP_SENSOR_PWM_REG, 1, val);
343 case aya_neo_air_plus_mendo:
344 case aya_neo_air_pro:
347 case oxp_mini_amd_a07:
348 *val = (*val * 255) / 100;
350 case oxp_mini_amd_pro:
356 case hwmon_pwm_enable:
357 return read_from_ec(OXP_SENSOR_PWM_ENABLE_REG, 1, val);
368 static int oxp_platform_write(struct device *dev, enum hwmon_sensor_types type,
369 u32 attr, int channel, long val)
374 case hwmon_pwm_enable:
376 return oxp_pwm_enable();
378 return oxp_pwm_disable();
380 case hwmon_pwm_input:
381 if (val < 0 || val > 255)
386 case aya_neo_air_plus_mendo:
387 case aya_neo_air_pro:
390 case oxp_mini_amd_a07:
391 val = (val * 100) / 255;
394 case oxp_mini_amd_pro:
398 return write_to_ec(OXP_SENSOR_PWM_REG, val);
409 /* Known sensors in the OXP EC controllers */
410 static const struct hwmon_channel_info * const oxp_platform_sensors[] = {
411 HWMON_CHANNEL_INFO(fan,
413 HWMON_CHANNEL_INFO(pwm,
414 HWMON_PWM_INPUT | HWMON_PWM_ENABLE),
418 static struct attribute *oxp_ec_attrs[] = {
419 &dev_attr_tt_toggle.attr,
423 static struct attribute_group oxp_ec_attribute_group = {
424 .is_visible = tt_toggle_is_visible,
425 .attrs = oxp_ec_attrs,
428 static const struct attribute_group *oxp_ec_groups[] = {
429 &oxp_ec_attribute_group,
433 static const struct hwmon_ops oxp_ec_hwmon_ops = {
434 .is_visible = oxp_ec_hwmon_is_visible,
435 .read = oxp_platform_read,
436 .write = oxp_platform_write,
439 static const struct hwmon_chip_info oxp_ec_chip_info = {
440 .ops = &oxp_ec_hwmon_ops,
441 .info = oxp_platform_sensors,
444 /* Initialization logic */
445 static int oxp_platform_probe(struct platform_device *pdev)
447 struct device *dev = &pdev->dev;
448 struct device *hwdev;
450 hwdev = devm_hwmon_device_register_with_info(dev, "oxpec", NULL,
451 &oxp_ec_chip_info, NULL);
453 return PTR_ERR_OR_ZERO(hwdev);
456 static struct platform_driver oxp_platform_driver = {
458 .name = "oxp-platform",
459 .dev_groups = oxp_ec_groups,
461 .probe = oxp_platform_probe,
464 static struct platform_device *oxp_platform_device;
466 static int __init oxp_platform_init(void)
468 const struct dmi_system_id *dmi_entry;
471 * Have to check for AMD processor here because DMI strings are the
472 * same between Intel and AMD boards, the only way to tell them apart
474 * Intel boards seem to have different EC registers and values to
477 dmi_entry = dmi_first_match(dmi_table);
478 if (!dmi_entry || boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
481 board = (enum oxp_board)(unsigned long)dmi_entry->driver_data;
483 oxp_platform_device =
484 platform_create_bundle(&oxp_platform_driver,
485 oxp_platform_probe, NULL, 0, NULL, 0);
487 return PTR_ERR_OR_ZERO(oxp_platform_device);
490 static void __exit oxp_platform_exit(void)
492 platform_device_unregister(oxp_platform_device);
493 platform_driver_unregister(&oxp_platform_driver);
496 MODULE_DEVICE_TABLE(dmi, dmi_table);
498 module_init(oxp_platform_init);
499 module_exit(oxp_platform_exit);
502 MODULE_DESCRIPTION("Platform driver that handles EC sensors of OneXPlayer devices");
503 MODULE_LICENSE("GPL");