xref: /openbmc/linux/drivers/hwmon/oxp-sensors.c (revision ae6385af)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Platform driver for OneXPlayer, AOK ZOE, and Aya Neo Handhelds that expose
4  * fan reading and control via hwmon sysfs.
5  *
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.
14  *
15  * Copyright (C) 2022 Joaquín I. Aramendía <samsagax@gmail.com>
16  */
17 
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>
27 
28 /* Handle ACPI lock mechanism */
29 static u32 oxp_mutex;
30 
31 #define ACPI_LOCK_DELAY_MS	500
32 
33 static bool lock_global_acpi_lock(void)
34 {
35 	return ACPI_SUCCESS(acpi_acquire_global_lock(ACPI_LOCK_DELAY_MS, &oxp_mutex));
36 }
37 
38 static bool unlock_global_acpi_lock(void)
39 {
40 	return ACPI_SUCCESS(acpi_release_global_lock(oxp_mutex));
41 }
42 
43 enum oxp_board {
44 	aok_zoe_a1 = 1,
45 	aya_neo_air,
46 	aya_neo_air_pro,
47 	oxp_mini_amd,
48 	oxp_mini_amd_pro,
49 };
50 
51 static enum oxp_board board;
52 
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 */
56 
57 static const struct dmi_system_id dmi_table[] = {
58 	{
59 		.matches = {
60 			DMI_MATCH(DMI_BOARD_VENDOR, "AOKZOE"),
61 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "AOKZOE A1 AR07"),
62 		},
63 		.driver_data = (void *) &(enum oxp_board) {aok_zoe_a1},
64 	},
65 	{
66 		.matches = {
67 			DMI_MATCH(DMI_BOARD_VENDOR, "AYANEO"),
68 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "AIR"),
69 		},
70 		.driver_data = (void *) &(enum oxp_board) {aya_neo_air},
71 	},
72 	{
73 		.matches = {
74 			DMI_MATCH(DMI_BOARD_VENDOR, "AYANEO"),
75 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "AIR Pro"),
76 		},
77 		.driver_data = (void *) &(enum oxp_board) {aya_neo_air_pro},
78 	},
79 	{
80 		.matches = {
81 			DMI_MATCH(DMI_BOARD_VENDOR, "ONE-NETBOOK"),
82 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "ONE XPLAYER"),
83 		},
84 		.driver_data = (void *) &(enum oxp_board) {oxp_mini_amd},
85 	},
86 	{
87 		.matches = {
88 			DMI_MATCH(DMI_BOARD_VENDOR, "ONE-NETBOOK"),
89 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "ONEXPLAYER Mini Pro"),
90 		},
91 		.driver_data = (void *) &(enum oxp_board) {oxp_mini_amd_pro},
92 	},
93 	{},
94 };
95 
96 /* Helper functions to handle EC read/write */
97 static int read_from_ec(u8 reg, int size, long *val)
98 {
99 	int i;
100 	int ret;
101 	u8 buffer;
102 
103 	if (!lock_global_acpi_lock())
104 		return -EBUSY;
105 
106 	*val = 0;
107 	for (i = 0; i < size; i++) {
108 		ret = ec_read(reg + i, &buffer);
109 		if (ret)
110 			return ret;
111 		*val <<= i * 8;
112 		*val += buffer;
113 	}
114 
115 	if (!unlock_global_acpi_lock())
116 		return -EBUSY;
117 
118 	return 0;
119 }
120 
121 static int write_to_ec(const struct device *dev, u8 reg, u8 value)
122 {
123 	int ret;
124 
125 	if (!lock_global_acpi_lock())
126 		return -EBUSY;
127 
128 	ret = ec_write(reg, value);
129 
130 	if (!unlock_global_acpi_lock())
131 		return -EBUSY;
132 
133 	return ret;
134 }
135 
136 static int oxp_pwm_enable(const struct device *dev)
137 {
138 	return write_to_ec(dev, OXP_SENSOR_PWM_ENABLE_REG, 0x01);
139 }
140 
141 static int oxp_pwm_disable(const struct device *dev)
142 {
143 	return write_to_ec(dev, OXP_SENSOR_PWM_ENABLE_REG, 0x00);
144 }
145 
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)
149 {
150 	switch (type) {
151 	case hwmon_fan:
152 		return 0444;
153 	case hwmon_pwm:
154 		return 0644;
155 	default:
156 		return 0;
157 	}
158 }
159 
160 static int oxp_platform_read(struct device *dev, enum hwmon_sensor_types type,
161 			     u32 attr, int channel, long *val)
162 {
163 	int ret;
164 
165 	switch (type) {
166 	case hwmon_fan:
167 		switch (attr) {
168 		case hwmon_fan_input:
169 			return read_from_ec(OXP_SENSOR_FAN_REG, 2, val);
170 		default:
171 			break;
172 		}
173 		break;
174 	case hwmon_pwm:
175 		switch (attr) {
176 		case hwmon_pwm_input:
177 			ret = read_from_ec(OXP_SENSOR_PWM_REG, 1, val);
178 			if (ret)
179 				return ret;
180 			switch (board) {
181 			case aya_neo_air:
182 			case aya_neo_air_pro:
183 			case oxp_mini_amd:
184 				*val = (*val * 255) / 100;
185 				break;
186 			case oxp_mini_amd_pro:
187 			case aok_zoe_a1:
188 			default:
189 				break;
190 			}
191 			return 0;
192 		case hwmon_pwm_enable:
193 			return read_from_ec(OXP_SENSOR_PWM_ENABLE_REG, 1, val);
194 		default:
195 			break;
196 		}
197 		break;
198 	default:
199 		break;
200 	}
201 	return -EOPNOTSUPP;
202 }
203 
204 static int oxp_platform_write(struct device *dev, enum hwmon_sensor_types type,
205 			      u32 attr, int channel, long val)
206 {
207 	switch (type) {
208 	case hwmon_pwm:
209 		switch (attr) {
210 		case hwmon_pwm_enable:
211 			if (val == 1)
212 				return oxp_pwm_enable(dev);
213 			else if (val == 0)
214 				return oxp_pwm_disable(dev);
215 			return -EINVAL;
216 		case hwmon_pwm_input:
217 			if (val < 0 || val > 255)
218 				return -EINVAL;
219 			switch (board) {
220 			case aya_neo_air:
221 			case aya_neo_air_pro:
222 			case oxp_mini_amd:
223 				val = (val * 100) / 255;
224 				break;
225 			case aok_zoe_a1:
226 			case oxp_mini_amd_pro:
227 			default:
228 				break;
229 			}
230 			return write_to_ec(dev, OXP_SENSOR_PWM_REG, val);
231 		default:
232 			break;
233 		}
234 		break;
235 	default:
236 		break;
237 	}
238 	return -EOPNOTSUPP;
239 }
240 
241 /* Known sensors in the OXP EC controllers */
242 static const struct hwmon_channel_info *oxp_platform_sensors[] = {
243 	HWMON_CHANNEL_INFO(fan,
244 			   HWMON_F_INPUT),
245 	HWMON_CHANNEL_INFO(pwm,
246 			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE),
247 	NULL,
248 };
249 
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,
254 };
255 
256 static const struct hwmon_chip_info oxp_ec_chip_info = {
257 	.ops = &oxp_ec_hwmon_ops,
258 	.info = oxp_platform_sensors,
259 };
260 
261 /* Initialization logic */
262 static int oxp_platform_probe(struct platform_device *pdev)
263 {
264 	const struct dmi_system_id *dmi_entry;
265 	struct device *dev = &pdev->dev;
266 	struct device *hwdev;
267 
268 	/*
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
271 	 * is the CPU.
272 	 * Intel boards seem to have different EC registers and values to
273 	 * read/write.
274 	 */
275 	dmi_entry = dmi_first_match(dmi_table);
276 	if (!dmi_entry || boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
277 		return -ENODEV;
278 
279 	board = *((enum oxp_board *) dmi_entry->driver_data);
280 
281 	hwdev = devm_hwmon_device_register_with_info(dev, "oxpec", NULL,
282 						     &oxp_ec_chip_info, NULL);
283 
284 	return PTR_ERR_OR_ZERO(hwdev);
285 }
286 
287 static struct platform_driver oxp_platform_driver = {
288 	.driver = {
289 		.name = "oxp-platform",
290 	},
291 	.probe = oxp_platform_probe,
292 };
293 
294 static struct platform_device *oxp_platform_device;
295 
296 static int __init oxp_platform_init(void)
297 {
298 	oxp_platform_device =
299 		platform_create_bundle(&oxp_platform_driver,
300 				       oxp_platform_probe, NULL, 0, NULL, 0);
301 
302 	return PTR_ERR_OR_ZERO(oxp_platform_device);
303 }
304 
305 static void __exit oxp_platform_exit(void)
306 {
307 	platform_device_unregister(oxp_platform_device);
308 	platform_driver_unregister(&oxp_platform_driver);
309 }
310 
311 MODULE_DEVICE_TABLE(dmi, dmi_table);
312 
313 module_init(oxp_platform_init);
314 module_exit(oxp_platform_exit);
315 
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");
319