1 // SPDX-License-Identifier: GPL-2.0+
3 * Platform driver for OXP Handhelds that expose fan reading and control
6 * Old boards have the same DMI strings and they are told appart by the
7 * boot cpu vendor (Intel/AMD). Currently only AMD boards are supported
8 * but the code is made to be simple to add other handheld boards in the
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/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));
49 static enum oxp_board board;
51 #define OXP_SENSOR_FAN_REG 0x76 /* Fan reading is 2 registers long */
52 #define OXP_SENSOR_PWM_ENABLE_REG 0x4A /* PWM enable is 1 register long */
53 #define OXP_SENSOR_PWM_REG 0x4B /* PWM reading is 1 register long */
55 static const struct dmi_system_id dmi_table[] = {
58 DMI_MATCH(DMI_BOARD_VENDOR, "AOKZOE"),
59 DMI_EXACT_MATCH(DMI_BOARD_NAME, "AOKZOE A1 AR07"),
61 .driver_data = (void *) &(enum oxp_board) {aok_zoe_a1},
65 DMI_MATCH(DMI_BOARD_VENDOR, "ONE-NETBOOK"),
66 DMI_EXACT_MATCH(DMI_BOARD_NAME, "ONE XPLAYER"),
68 .driver_data = (void *) &(enum oxp_board) {oxp_mini_amd},
72 DMI_MATCH(DMI_BOARD_VENDOR, "ONE-NETBOOK"),
73 DMI_EXACT_MATCH(DMI_BOARD_NAME, "ONEXPLAYER Mini Pro"),
75 .driver_data = (void *) &(enum oxp_board) {oxp_mini_amd_pro},
80 /* Helper functions to handle EC read/write */
81 static int read_from_ec(u8 reg, int size, long *val)
87 if (!lock_global_acpi_lock())
91 for (i = 0; i < size; i++) {
92 ret = ec_read(reg + i, &buffer);
99 if (!unlock_global_acpi_lock())
105 static int write_to_ec(const struct device *dev, u8 reg, u8 value)
109 if (!lock_global_acpi_lock())
112 ret = ec_write(reg, value);
114 if (!unlock_global_acpi_lock())
120 static int oxp_pwm_enable(const struct device *dev)
122 return write_to_ec(dev, OXP_SENSOR_PWM_ENABLE_REG, 0x01);
125 static int oxp_pwm_disable(const struct device *dev)
127 return write_to_ec(dev, OXP_SENSOR_PWM_ENABLE_REG, 0x00);
130 /* Callbacks for hwmon interface */
131 static umode_t oxp_ec_hwmon_is_visible(const void *drvdata,
132 enum hwmon_sensor_types type, u32 attr, int channel)
144 static int oxp_platform_read(struct device *dev, enum hwmon_sensor_types type,
145 u32 attr, int channel, long *val)
152 case hwmon_fan_input:
153 return read_from_ec(OXP_SENSOR_FAN_REG, 2, val);
160 case hwmon_pwm_input:
161 ret = read_from_ec(OXP_SENSOR_PWM_REG, 1, val);
164 if (board == oxp_mini_amd)
165 *val = (*val * 255) / 100;
167 case hwmon_pwm_enable:
168 return read_from_ec(OXP_SENSOR_PWM_ENABLE_REG, 1, val);
179 static int oxp_platform_write(struct device *dev, enum hwmon_sensor_types type,
180 u32 attr, int channel, long val)
185 case hwmon_pwm_enable:
187 return oxp_pwm_enable(dev);
189 return oxp_pwm_disable(dev);
191 case hwmon_pwm_input:
192 if (val < 0 || val > 255)
194 if (board == oxp_mini_amd)
195 val = (val * 100) / 255;
196 return write_to_ec(dev, OXP_SENSOR_PWM_REG, val);
207 /* Known sensors in the OXP EC controllers */
208 static const struct hwmon_channel_info *oxp_platform_sensors[] = {
209 HWMON_CHANNEL_INFO(fan,
211 HWMON_CHANNEL_INFO(pwm,
212 HWMON_PWM_INPUT | HWMON_PWM_ENABLE),
216 static const struct hwmon_ops oxp_ec_hwmon_ops = {
217 .is_visible = oxp_ec_hwmon_is_visible,
218 .read = oxp_platform_read,
219 .write = oxp_platform_write,
222 static const struct hwmon_chip_info oxp_ec_chip_info = {
223 .ops = &oxp_ec_hwmon_ops,
224 .info = oxp_platform_sensors,
227 /* Initialization logic */
228 static int oxp_platform_probe(struct platform_device *pdev)
230 const struct dmi_system_id *dmi_entry;
231 struct device *dev = &pdev->dev;
232 struct device *hwdev;
235 * Have to check for AMD processor here because DMI strings are the
236 * same between Intel and AMD boards, the only way to tell them appart
238 * Intel boards seem to have different EC registers and values to
241 dmi_entry = dmi_first_match(dmi_table);
242 if (!dmi_entry || boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
245 board = *((enum oxp_board *) dmi_entry->driver_data);
247 hwdev = devm_hwmon_device_register_with_info(dev, "oxpec", NULL,
248 &oxp_ec_chip_info, NULL);
250 return PTR_ERR_OR_ZERO(hwdev);
253 static struct platform_driver oxp_platform_driver = {
255 .name = "oxp-platform",
257 .probe = oxp_platform_probe,
260 static struct platform_device *oxp_platform_device;
262 static int __init oxp_platform_init(void)
264 oxp_platform_device =
265 platform_create_bundle(&oxp_platform_driver,
266 oxp_platform_probe, NULL, 0, NULL, 0);
268 return PTR_ERR_OR_ZERO(oxp_platform_device);
271 static void __exit oxp_platform_exit(void)
273 platform_device_unregister(oxp_platform_device);
274 platform_driver_unregister(&oxp_platform_driver);
277 MODULE_DEVICE_TABLE(dmi, dmi_table);
279 module_init(oxp_platform_init);
280 module_exit(oxp_platform_exit);
283 MODULE_DESCRIPTION("Platform driver that handles EC sensors of OneXPlayer devices");
284 MODULE_LICENSE("GPL");