xref: /linux/drivers/platform/x86/lenovo/wmi-other.c (revision 9e947d96915af63e28896caae85a81b74855621b)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Lenovo Other Mode WMI interface driver.
4  *
5  * This driver uses the fw_attributes class to expose the various WMI functions
6  * provided by the "Other Mode" WMI interface. This enables CPU and GPU power
7  * limit as well as various other attributes for devices that fall under the
8  * "Gaming Series" of Lenovo laptop devices. Each attribute exposed by the
9  * "Other Mode" interface has a corresponding Capability Data struct that
10  * allows the driver to probe details about the attribute such as if it is
11  * supported by the hardware, the default_value, max_value, min_value, and step
12  * increment.
13  *
14  * These attributes typically don't fit anywhere else in the sysfs and are set
15  * in Windows using one of Lenovo's multiple user applications.
16  *
17  * Additionally, this driver also exports tunable fan speed RPM to HWMON.
18  * Min/max RPM are also provided for reference.
19  *
20  * Copyright (C) 2025 Derek J. Clark <derekjohn.clark@gmail.com>
21  *   - fw_attributes
22  *   - binding to Capability Data 01
23  *
24  * Copyright (C) 2025 Rong Zhang <i@rong.moe>
25  *   - HWMON
26  *   - binding to Capability Data 00 and Fan
27  */
28 
29 #include <linux/acpi.h>
30 #include <linux/bitfield.h>
31 #include <linux/cleanup.h>
32 #include <linux/component.h>
33 #include <linux/container_of.h>
34 #include <linux/device.h>
35 #include <linux/export.h>
36 #include <linux/firmware_attributes.h>
37 #include <linux/gfp_types.h>
38 #include <linux/hwmon.h>
39 #include <linux/idr.h>
40 #include <linux/kdev_t.h>
41 #include <linux/kobject.h>
42 #include <linux/limits.h>
43 #include <linux/module.h>
44 #include <linux/platform_profile.h>
45 #include <linux/power_supply.h>
46 #include <linux/types.h>
47 #include <linux/wmi.h>
48 
49 #include <acpi/battery.h>
50 
51 #include "wmi-capdata.h"
52 #include "wmi-events.h"
53 #include "wmi-helpers.h"
54 
55 #define LENOVO_OTHER_MODE_GUID "DC2A8805-3A8C-41BA-A6F7-092E0089CD3B"
56 
57 enum lwmi_feature_id_cpu {
58 	LWMI_FEATURE_ID_CPU_SPPT =	0x01,
59 	LWMI_FEATURE_ID_CPU_SPL =	0x02,
60 	LWMI_FEATURE_ID_CPU_FPPT =	0x03,
61 	LWMI_FEATURE_ID_CPU_TEMP =	0x04,
62 	LWMI_FEATURE_ID_CPU_APU =	0x05,
63 	LWMI_FEATURE_ID_CPU_CL =	0x06,
64 	LWMI_FEATURE_ID_CPU_TAU =	0x07,
65 	LWMI_FEATURE_ID_CPU_IPL =	0x09,
66 };
67 
68 enum lwmi_feature_id_gpu {
69 	LWMI_FEATURE_ID_GPU_NV_PPAB =		0x01,
70 	LWMI_FEATURE_ID_GPU_NV_CTGP =		0x02,
71 	LWMI_FEATURE_ID_GPU_TEMP =		0x03,
72 	LWMI_FEATURE_ID_GPU_AC_OFFSET =		0x04,
73 	LWMI_FEATURE_ID_DGPU_BOOST_CLK =	0x06,
74 	LWMI_FEATURE_ID_DGPU_EN =		0x07,
75 	LWMI_FEATURE_ID_GPU_MODE =		0x08,
76 	LWMI_FEATURE_ID_DGPU_DIDVID =		0x09,
77 	LWMI_FEATURE_ID_GPU_NV_BPL =		0x0a,
78 	LWMI_FEATURE_ID_GPU_NV_CPU_BOOST =	0x0b,
79 };
80 
81 enum lwmi_feature_id_psu {
82 	LWMI_FEATURE_ID_PSU_CHARGE_TYPES =	0x01,
83 	LWMI_FEATURE_ID_PSU_CHARGE_BEHAVIOUR =	0x02,
84 };
85 
86 #define LWMI_FEATURE_ID_FAN_RPM 0x03
87 
88 #define LWMI_TYPE_ID_CROSSLOAD	0x01
89 #define LWMI_TYPE_ID_PSU_AC	0x01
90 
91 #define LWMI_FEATURE_VALUE_GET 17
92 #define LWMI_FEATURE_VALUE_SET 18
93 
94 #define LWMI_FAN_ID_BASE 1
95 #define LWMI_FAN_NR 4
96 #define LWMI_FAN_ID(x) ((x) + LWMI_FAN_ID_BASE)
97 
98 #define LWMI_FAN_DIV 100
99 
100 #define LWMI_CHARGE_BEHAVIOR_DISCHARGE	0x00
101 #define LWMI_CHARGE_BEHAVIOR_AUTO	0x01
102 #define LWMI_CHARGE_TYPE_STANDARD	0x00
103 #define LWMI_CHARGE_TYPE_LONGLIFE	0x01
104 
105 #define LWMI_ATTR_ID_FAN_RPM(x)                                   \
106 	lwmi_attr_id(LWMI_DEVICE_ID_FAN, LWMI_FEATURE_ID_FAN_RPM, \
107 		     LWMI_GZ_THERMAL_MODE_NONE, LWMI_FAN_ID(x))
108 
109 #define LWMI_ATTR_ID_PSU(feat, type)			\
110 	lwmi_attr_id(LWMI_DEVICE_ID_PSU, feat,		\
111 		     LWMI_GZ_THERMAL_MODE_NONE, type)
112 
113 #define LWMI_OM_SYSFS_NAME "lenovo-wmi-other"
114 #define LWMI_OM_HWMON_NAME "lenovo_wmi_other"
115 
116 static DEFINE_IDA(lwmi_om_ida);
117 
118 enum attribute_property {
119 	DEFAULT_VAL,
120 	MAX_VAL,
121 	MIN_VAL,
122 	STEP_VAL,
123 	SUPPORTED,
124 };
125 
126 struct lwmi_fan_info {
127 	u32 supported;
128 	u32 last_target;
129 	long min_rpm;
130 	long max_rpm;
131 };
132 
133 struct lwmi_om_priv {
134 	struct component_master_ops *ops;
135 
136 	/* only valid after capdata bind */
137 	struct cd_list *cd00_list;
138 	struct cd_list *cd01_list;
139 
140 	struct device *hwmon_dev;
141 	struct device *fw_attr_dev;
142 	struct kset *fw_attr_kset;
143 	struct wmi_device *wdev;
144 	int ida_id;
145 
146 	struct lwmi_fan_info fan_info[LWMI_FAN_NR];
147 
148 	struct {
149 		bool capdata00_collected : 1;
150 		bool capdata_fan_collected : 1;
151 	} fan_flags;
152 
153 	enum power_supply_charge_behaviour charge_behaviour;
154 	const struct power_supply_ext *battery_ext;
155 	struct acpi_battery_hook battery_hook;
156 	bool bh_registered;
157 };
158 
159 /*
160  * Visibility of fan channels:
161  *
162  * +-------------------+---------+------------------+-----------------------+------------+
163  * |                   | default | +expose_all_fans | +relax_fan_constraint | +both      |
164  * +-------------------+---------+------------------+-----------------------+------------+
165  * | canonical         | RW      | RW               | RW+relaxed            | RW+relaxed |
166  * +-------------------+---------+------------------+-----------------------+------------+
167  * | -capdata_fan[idx] | N       | RO               | N                     | RW+relaxed |
168  * +-------------------+---------+------------------+-----------------------+------------+
169  *
170  * Note:
171  * 1. LWMI_ATTR_ID_FAN_RPM[idx].supported is always checked before exposing a channel.
172  * 2. -capdata_fan implies -capdata_fan[idx].
173  */
174 static bool expose_all_fans;
175 module_param(expose_all_fans, bool, 0444);
176 MODULE_PARM_DESC(expose_all_fans,
177 	"This option skips some capability checks and solely relies on per-channel ones "
178 	"to expose fan attributes. Use with caution.");
179 
180 static bool relax_fan_constraint;
181 module_param(relax_fan_constraint, bool, 0444);
182 MODULE_PARM_DESC(relax_fan_constraint,
183 	"Do not enforce fan RPM constraint (div/min/max) "
184 	"and enables fan tuning when such data is missing. "
185 	"Enabling this may results in HWMON attributes being out-of-sync, "
186 	"and setting a too low RPM stops the fan. Use with caution.");
187 
188 /* Visibility of power supply extensions */
189 static bool force_load_psy_ext;
190 module_param(force_load_psy_ext, bool, 0444);
191 MODULE_PARM_DESC(force_load_psy_ext,
192 	"This option will skip checking if the ideapad_laptop driver will conflict "
193 	"with adding an extension to set the battery charge behavior and battery charge "
194 	"control end threshold. It will also skip checking if the BIOS reports that "
195 	"those features are fully supported. It is recommended to blacklist the ideapad "
196 	"driver before using this option.");
197 
198 /* ======== HWMON (component: lenovo-wmi-capdata 00 & fan) ======== */
199 
200 /**
201  * lwmi_om_fan_get_set() - Get or set fan RPM value of specified fan
202  * @priv: Driver private data structure
203  * @channel: Fan channel index (0-based)
204  * @val: Pointer to value (input for set, output for get)
205  * @set: True to set value, false to get value
206  *
207  * Communicates with WMI interface to either retrieve current fan RPM
208  * or set target fan RPM.
209  *
210  * Return: 0 on success, or an error code.
211  */
lwmi_om_fan_get_set(struct lwmi_om_priv * priv,int channel,u32 * val,bool set)212 static int lwmi_om_fan_get_set(struct lwmi_om_priv *priv, int channel, u32 *val, bool set)
213 {
214 	struct wmi_method_args_32 args = {};
215 	u32 method_id, retval;
216 	int err;
217 
218 	method_id = set ? LWMI_FEATURE_VALUE_SET : LWMI_FEATURE_VALUE_GET;
219 	args.arg0 = LWMI_ATTR_ID_FAN_RPM(channel);
220 	args.arg1 = set ? *val : 0;
221 
222 	err = lwmi_dev_evaluate_int(priv->wdev, 0x0, method_id,
223 				    (unsigned char *)&args, sizeof(args), &retval);
224 	if (err)
225 		return err;
226 
227 	if (!set) {
228 		*val = retval;
229 		return 0;
230 	}
231 
232 	/*
233 	 * It seems that 0 means "no error" and 1 means "done". Apparently
234 	 * different firmware teams have different thoughts on indicating
235 	 * success, so we accepts both.
236 	 */
237 	return (retval == 0 || retval == 1) ? 0 : -EIO;
238 }
239 
240 /**
241  * lwmi_om_hwmon_is_visible() - Determine visibility of HWMON attributes
242  * @drvdata: Driver private data
243  * @type: Sensor type
244  * @attr: Attribute identifier
245  * @channel: Channel index
246  *
247  * Determines whether an HWMON attribute should be visible in sysfs
248  * based on hardware capabilities and current configuration.
249  *
250  * Return: permission mode, or 0 if invisible.
251  */
lwmi_om_hwmon_is_visible(const void * drvdata,enum hwmon_sensor_types type,u32 attr,int channel)252 static umode_t lwmi_om_hwmon_is_visible(const void *drvdata, enum hwmon_sensor_types type,
253 					u32 attr, int channel)
254 {
255 	struct lwmi_om_priv *priv = (struct lwmi_om_priv *)drvdata;
256 	bool visible = false;
257 
258 	if (type == hwmon_fan) {
259 		if (!(priv->fan_info[channel].supported & LWMI_SUPP_VALID))
260 			return 0;
261 
262 		switch (attr) {
263 		case hwmon_fan_target:
264 			if (!(priv->fan_info[channel].supported & LWMI_SUPP_SET))
265 				return 0;
266 
267 			if (relax_fan_constraint ||
268 			    (priv->fan_info[channel].min_rpm >= 0 &&
269 			     priv->fan_info[channel].max_rpm >= 0))
270 				return 0644;
271 
272 			/*
273 			 * Reaching here implies expose_all_fans is set.
274 			 * See lwmi_om_hwmon_add().
275 			 */
276 			dev_warn_once(&priv->wdev->dev,
277 				      "fan tuning disabled due to missing RPM constraint\n");
278 			return 0;
279 		case hwmon_fan_div:
280 		case hwmon_fan_input:
281 			visible = priv->fan_info[channel].supported & LWMI_SUPP_GET;
282 			break;
283 		case hwmon_fan_min:
284 			visible = priv->fan_info[channel].min_rpm >= 0;
285 			break;
286 		case hwmon_fan_max:
287 			visible = priv->fan_info[channel].max_rpm >= 0;
288 			break;
289 		}
290 	}
291 
292 	return visible ? 0444 : 0;
293 }
294 
295 /**
296  * lwmi_om_hwmon_read() - Read HWMON sensor data
297  * @dev: Device pointer
298  * @type: Sensor type
299  * @attr: Attribute identifier
300  * @channel: Channel index
301  * @val: Pointer to store value
302  *
303  * Reads current sensor values from hardware through WMI interface.
304  *
305  * Return: 0 on success, or an error code.
306  */
lwmi_om_hwmon_read(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long * val)307 static int lwmi_om_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
308 			      u32 attr, int channel, long *val)
309 {
310 	struct lwmi_om_priv *priv = dev_get_drvdata(dev);
311 	u32 retval = 0;
312 	int err;
313 
314 	if (type == hwmon_fan) {
315 		switch (attr) {
316 		/*
317 		 * The EC has an internal RPM divisor (i.e., the raw register value is
318 		 * RPM / fanY_div). For fanY_input, the WMI method reads the register
319 		 * value and returns raw * fanY_div. For fanY_target, the WMI method
320 		 * divides the written value by fanY_div before writing it to the EC.
321 		 *
322 		 * As a result, reading fanY_input always returns a multiple of fanY_div,
323 		 * while writing to fanY_target loses the remainder.
324 		 */
325 		case hwmon_fan_div:
326 			*val = LWMI_FAN_DIV;
327 			return 0;
328 		case hwmon_fan_input:
329 			err = lwmi_om_fan_get_set(priv, channel, &retval, false);
330 			if (err)
331 				return err;
332 
333 			*val = retval;
334 			return 0;
335 		case hwmon_fan_target:
336 			*val = priv->fan_info[channel].last_target;
337 			return 0;
338 		case hwmon_fan_min:
339 			*val = priv->fan_info[channel].min_rpm;
340 			return 0;
341 		case hwmon_fan_max:
342 			*val = priv->fan_info[channel].max_rpm;
343 			return 0;
344 		}
345 	}
346 
347 	return -EOPNOTSUPP;
348 }
349 
350 /**
351  * lwmi_om_hwmon_write() - Write HWMON sensor data
352  * @dev: Device pointer
353  * @type: Sensor type
354  * @attr: Attribute identifier
355  * @channel: Channel index
356  * @val: Value to write
357  *
358  * Writes configuration values to hardware through WMI interface.
359  *
360  * Return: 0 on success, or an error code.
361  */
lwmi_om_hwmon_write(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long val)362 static int lwmi_om_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
363 			       u32 attr, int channel, long val)
364 {
365 	struct lwmi_om_priv *priv = dev_get_drvdata(dev);
366 	u32 raw, min_rpm, max_rpm;
367 	int err;
368 
369 	if (type == hwmon_fan) {
370 		switch (attr) {
371 		case hwmon_fan_target:
372 			if (relax_fan_constraint) {
373 				min_rpm = 1;
374 				max_rpm = U16_MAX;
375 			} else {
376 				min_rpm = priv->fan_info[channel].min_rpm;
377 				max_rpm = priv->fan_info[channel].max_rpm;
378 			}
379 
380 			/* 0 means "auto". */
381 			if (val != 0 && (val < min_rpm || val > max_rpm))
382 				return -EINVAL;
383 
384 			/*
385 			 * The effective fanY_target is always a multiple of fanY_div
386 			 * due to the EC's internal RPM divisor (see lwmi_om_hwmon_read).
387 			 *
388 			 * Round down the written value to the nearest multiple of fanY_div
389 			 * to prevent mismatch between the effective value and last_target.
390 			 *
391 			 * For relax_fan_constraint, skip this conversion as setting a
392 			 * sub-fanY_div value is necessary to completely stop the fan on
393 			 * some devices.
394 			 */
395 			if (!relax_fan_constraint)
396 				raw = val / LWMI_FAN_DIV * LWMI_FAN_DIV;
397 			else
398 				raw = val;
399 
400 			err = lwmi_om_fan_get_set(priv, channel, &raw, true);
401 			if (err)
402 				return err;
403 
404 			priv->fan_info[channel].last_target = raw;
405 			return 0;
406 		}
407 	}
408 
409 	return -EOPNOTSUPP;
410 }
411 
412 static const struct hwmon_channel_info * const lwmi_om_hwmon_info[] = {
413 	/* Must match LWMI_FAN_NR. */
414 	HWMON_CHANNEL_INFO(fan,
415 			   HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_DIV |
416 			   HWMON_F_MIN | HWMON_F_MAX,
417 			   HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_DIV |
418 			   HWMON_F_MIN | HWMON_F_MAX,
419 			   HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_DIV |
420 			   HWMON_F_MIN | HWMON_F_MAX,
421 			   HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_DIV |
422 			   HWMON_F_MIN | HWMON_F_MAX),
423 	NULL
424 };
425 
426 static const struct hwmon_ops lwmi_om_hwmon_ops = {
427 	.is_visible = lwmi_om_hwmon_is_visible,
428 	.read = lwmi_om_hwmon_read,
429 	.write = lwmi_om_hwmon_write,
430 };
431 
432 static const struct hwmon_chip_info lwmi_om_hwmon_chip_info = {
433 	.ops = &lwmi_om_hwmon_ops,
434 	.info = lwmi_om_hwmon_info,
435 };
436 
437 /**
438  * lwmi_om_hwmon_add() - Register HWMON device if all info is collected
439  * @priv: Driver private data
440  */
lwmi_om_hwmon_add(struct lwmi_om_priv * priv)441 static void lwmi_om_hwmon_add(struct lwmi_om_priv *priv)
442 {
443 	int i, valid;
444 
445 	if (WARN_ON(priv->hwmon_dev))
446 		return;
447 
448 	if (!priv->fan_flags.capdata00_collected || !priv->fan_flags.capdata_fan_collected) {
449 		dev_dbg(&priv->wdev->dev, "HWMON registration pending (00: %d, fan: %d)\n",
450 			priv->fan_flags.capdata00_collected,
451 			priv->fan_flags.capdata_fan_collected);
452 		return;
453 	}
454 
455 	if (expose_all_fans)
456 		dev_warn(&priv->wdev->dev, "all fans exposed. Use with caution\n");
457 
458 	if (relax_fan_constraint)
459 		dev_warn(&priv->wdev->dev, "fan RPM constraint relaxed. Use with caution\n");
460 
461 	valid = 0;
462 	for (i = 0; i < LWMI_FAN_NR; i++) {
463 		if (!(priv->fan_info[i].supported & LWMI_SUPP_VALID))
464 			continue;
465 
466 		valid++;
467 
468 		if (!expose_all_fans &&
469 		    (priv->fan_info[i].min_rpm < 0 || priv->fan_info[i].max_rpm < 0)) {
470 			dev_dbg(&priv->wdev->dev, "missing RPM constraint for fan%d, hiding\n",
471 				LWMI_FAN_ID(i));
472 			priv->fan_info[i].supported = 0;
473 			valid--;
474 		}
475 	}
476 
477 	if (valid == 0) {
478 		dev_warn(&priv->wdev->dev,
479 			 "fan reporting/tuning is unsupported on this device\n");
480 		return;
481 	}
482 
483 	priv->hwmon_dev = hwmon_device_register_with_info(&priv->wdev->dev,
484 							  LWMI_OM_HWMON_NAME, priv,
485 							  &lwmi_om_hwmon_chip_info,
486 							  NULL);
487 	if (IS_ERR(priv->hwmon_dev)) {
488 		dev_warn(&priv->wdev->dev, "failed to register HWMON device: %ld\n",
489 			 PTR_ERR(priv->hwmon_dev));
490 		priv->hwmon_dev = NULL;
491 		return;
492 	}
493 
494 	dev_dbg(&priv->wdev->dev, "registered HWMON device\n");
495 }
496 
497 /**
498  * lwmi_om_hwmon_remove() - Unregister HWMON device
499  * @priv: Driver private data
500  *
501  * Unregisters the HWMON device if applicable.
502  */
lwmi_om_hwmon_remove(struct lwmi_om_priv * priv)503 static void lwmi_om_hwmon_remove(struct lwmi_om_priv *priv)
504 {
505 	if (!priv->hwmon_dev)
506 		return;
507 
508 	hwmon_device_unregister(priv->hwmon_dev);
509 	priv->hwmon_dev = NULL;
510 }
511 
512 /**
513  * lwmi_om_fan_info_init() - Initialzie fan info
514  * @priv: Driver private data
515  *
516  * lwmi_om_fan_info_collect_cd00() and lwmi_om_fan_info_collect_cd_fan() may be
517  * called in an arbitrary order. Hence, initializion must be done before.
518  */
lwmi_om_fan_info_init(struct lwmi_om_priv * priv)519 static void lwmi_om_fan_info_init(struct lwmi_om_priv *priv)
520 {
521 	int i;
522 
523 	for (i = 0; i < LWMI_FAN_NR; i++) {
524 		priv->fan_info[i] = (struct lwmi_fan_info) {
525 			.supported = 0,
526 			/*
527 			 * Assume 0 on probe as the EC resets all fans to auto mode on (re)boot.
528 			 *
529 			 * Note that S0ix (s2idle) preserves the RPM target, so we don't need
530 			 * suspend/resume callbacks. This behavior has not been tested on S3-
531 			 * capable devices, but I doubt if such devices even have this interface.
532 			 */
533 			.last_target = 0,
534 			.min_rpm = -ENODATA,
535 			.max_rpm = -ENODATA,
536 		};
537 	}
538 
539 	priv->fan_flags.capdata00_collected = false;
540 	priv->fan_flags.capdata_fan_collected = false;
541 }
542 
543 /**
544  * lwmi_om_fan_info_collect_cd00() - Collect fan info from capdata 00
545  * @priv: Driver private data
546  */
lwmi_om_fan_info_collect_cd00(struct lwmi_om_priv * priv)547 static void lwmi_om_fan_info_collect_cd00(struct lwmi_om_priv *priv)
548 {
549 	struct capdata00 capdata00;
550 	int i, err;
551 
552 	dev_dbg(&priv->wdev->dev, "Collecting fan info from capdata00\n");
553 
554 	for (i = 0; i < LWMI_FAN_NR; i++) {
555 		err = lwmi_cd00_get_data(priv->cd00_list, LWMI_ATTR_ID_FAN_RPM(i), &capdata00);
556 		priv->fan_info[i].supported = err ? 0 : capdata00.supported;
557 	}
558 
559 	priv->fan_flags.capdata00_collected = true;
560 	lwmi_om_hwmon_add(priv);
561 }
562 
563 /**
564  * lwmi_om_fan_info_collect_cd_fan() - Collect fan info from capdata fan
565  * @dev: Pointer to the lenovo-wmi-other device
566  * @cd_fan_list: Pointer to the capdata fan list
567  */
lwmi_om_fan_info_collect_cd_fan(struct device * dev,struct cd_list * cd_fan_list)568 static void lwmi_om_fan_info_collect_cd_fan(struct device *dev, struct cd_list *cd_fan_list)
569 {
570 	struct lwmi_om_priv *priv = dev_get_drvdata(dev);
571 	struct capdata_fan capdata_fan;
572 	int i, err;
573 
574 	dev_dbg(dev, "Collecting fan info from capdata_fan\n");
575 
576 	if (!cd_fan_list)
577 		goto out;
578 
579 	for (i = 0; i < LWMI_FAN_NR; i++) {
580 		err = lwmi_cd_fan_get_data(cd_fan_list, LWMI_FAN_ID(i), &capdata_fan);
581 		if (err)
582 			continue;
583 
584 		priv->fan_info[i].min_rpm = capdata_fan.min_rpm;
585 		priv->fan_info[i].max_rpm = capdata_fan.max_rpm;
586 	}
587 
588 out:
589 	priv->fan_flags.capdata_fan_collected = true;
590 	lwmi_om_hwmon_add(priv);
591 }
592 
593 /* ======== Power Supply Extension (component: lenovo-wmi-capdata 00) ======== */
594 
595 /**
596  * lwmi_psy_ext_get_prop() - Get a power_supply_ext property
597  * @ps: The battery that was extended
598  * @ext: The extension
599  * @ext_data: Pointer to the lwmi_om_priv drvdata
600  * @prop: The property to read
601  * @val: The value to return
602  *
603  * Reads the given value from the power_supply_ext property
604  *
605  * Return: 0 on success, or an error
606  */
lwmi_psy_ext_get_prop(struct power_supply * ps,const struct power_supply_ext * ext,void * ext_data,enum power_supply_property prop,union power_supply_propval * val)607 static int lwmi_psy_ext_get_prop(struct power_supply *ps,
608 				 const struct power_supply_ext *ext,
609 				 void *ext_data,
610 				 enum power_supply_property prop,
611 				 union power_supply_propval *val)
612 {
613 	struct lwmi_om_priv *priv = ext_data;
614 	struct wmi_method_args_32 args = {};
615 	u32 retval;
616 	int ret;
617 
618 	switch (prop) {
619 	case POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR:
620 		/* Reading from BIOS reads the wrong bit. Use cached value */
621 		val->intval = priv->charge_behaviour;
622 		return 0;
623 	case POWER_SUPPLY_PROP_CHARGE_TYPES:
624 		args.arg0 = LWMI_ATTR_ID_PSU(LWMI_FEATURE_ID_PSU_CHARGE_TYPES,
625 					     LWMI_TYPE_ID_PSU_AC);
626 		break;
627 	default:
628 		return -EINVAL;
629 	}
630 
631 	ret = lwmi_dev_evaluate_int(priv->wdev, 0x0, LWMI_FEATURE_VALUE_GET,
632 				    (u8 *)&args, sizeof(args),
633 				    &retval);
634 	if (ret)
635 		return ret;
636 
637 	dev_dbg(&priv->wdev->dev, "Got return value %#x for property %#x\n", retval, prop);
638 
639 	switch (retval) {
640 	case LWMI_CHARGE_TYPE_LONGLIFE:
641 		val->intval = POWER_SUPPLY_CHARGE_TYPE_LONGLIFE;
642 		break;
643 	case LWMI_CHARGE_TYPE_STANDARD:
644 		val->intval = POWER_SUPPLY_CHARGE_TYPE_STANDARD;
645 		break;
646 	default:
647 		dev_err(&priv->wdev->dev, "Got invalid charge types value: %#x\n", retval);
648 		return -EINVAL;
649 	}
650 
651 	return 0;
652 }
653 
654 /**
655  * lwmi_psy_ext_set_prop() - Set a power_supply_ext property
656  * @ps: The battery that was extended
657  * @ext: The extension
658  * @ext_data: Pointer to the lwmi_om_priv drvdata
659  * @prop: The property to write
660  * @val: The value to write
661  *
662  * Writes the given value to the power_supply_ext property
663  *
664  * Return: 0 on success, or an error
665  */
lwmi_psy_ext_set_prop(struct power_supply * ps,const struct power_supply_ext * ext,void * ext_data,enum power_supply_property prop,const union power_supply_propval * val)666 static int lwmi_psy_ext_set_prop(struct power_supply *ps,
667 				 const struct power_supply_ext *ext,
668 				 void *ext_data,
669 				 enum power_supply_property prop,
670 				 const union power_supply_propval *val)
671 {
672 	struct lwmi_om_priv *priv = ext_data;
673 	struct wmi_method_args_32 args = {};
674 
675 	switch (prop) {
676 	case POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR:
677 		args.arg0 = LWMI_ATTR_ID_PSU(LWMI_FEATURE_ID_PSU_CHARGE_BEHAVIOUR,
678 					     LWMI_TYPE_ID_NONE);
679 		switch (val->intval) {
680 		case POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO:
681 			args.arg1 = LWMI_CHARGE_BEHAVIOR_AUTO;
682 			break;
683 		case POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE:
684 			args.arg1 = LWMI_CHARGE_BEHAVIOR_DISCHARGE;
685 			break;
686 		default:
687 			dev_err(&priv->wdev->dev, "Got invalid charge behavior value: %#x\n",
688 				val->intval);
689 			return -EINVAL;
690 		}
691 		priv->charge_behaviour = val->intval;
692 		break;
693 	case POWER_SUPPLY_PROP_CHARGE_TYPES:
694 		args.arg0 = LWMI_ATTR_ID_PSU(LWMI_FEATURE_ID_PSU_CHARGE_TYPES,
695 					     LWMI_TYPE_ID_PSU_AC);
696 		switch (val->intval) {
697 		case POWER_SUPPLY_CHARGE_TYPE_LONGLIFE:
698 			args.arg1 = LWMI_CHARGE_TYPE_LONGLIFE;
699 			break;
700 		case POWER_SUPPLY_CHARGE_TYPE_STANDARD:
701 			args.arg1 = LWMI_CHARGE_TYPE_STANDARD;
702 			break;
703 		default:
704 			dev_err(&priv->wdev->dev, "Got invalid charge types value: %#x\n",
705 				val->intval);
706 			return -EINVAL;
707 		}
708 		break;
709 	default:
710 		return -EINVAL;
711 	}
712 
713 	dev_dbg(&priv->wdev->dev, "Attempting to set %#010x for property %#x to %#x\n",
714 		args.arg0, prop, args.arg1);
715 
716 	return lwmi_dev_evaluate_int(priv->wdev, 0x0, LWMI_FEATURE_VALUE_SET,
717 				     (u8 *)&args, sizeof(args), NULL);
718 }
719 
720 /** lwmi_psy_prop_get_supported() - Gets the support level from capdata for a given property
721  * @priv: Pointer to the lwmi_om_priv drvdata
722  * @prop: The power supply property to be evaluated
723  *
724  * Return: bitmapped capability data support level
725  */
lwmi_psy_prop_get_supported(struct lwmi_om_priv * priv,enum power_supply_property prop)726 static u32 lwmi_psy_prop_get_supported(struct lwmi_om_priv *priv, enum power_supply_property prop)
727 {
728 	struct capdata00 capdata;
729 	u32 attribute_id;
730 	int ret;
731 
732 	switch (prop) {
733 	case POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR:
734 		attribute_id = LWMI_ATTR_ID_PSU(LWMI_FEATURE_ID_PSU_CHARGE_BEHAVIOUR,
735 						LWMI_TYPE_ID_NONE);
736 		break;
737 	case POWER_SUPPLY_PROP_CHARGE_TYPES:
738 		attribute_id = LWMI_ATTR_ID_PSU(LWMI_FEATURE_ID_PSU_CHARGE_TYPES,
739 						LWMI_TYPE_ID_PSU_AC);
740 		break;
741 	default:
742 		return 0;
743 	}
744 
745 	ret = lwmi_cd00_get_data(priv->cd00_list, attribute_id, &capdata);
746 	if (ret)
747 		return 0;
748 
749 	dev_dbg(&priv->wdev->dev, "Battery charge feature (%#010x) support level: %#x\n",
750 		attribute_id, capdata.supported);
751 
752 	return capdata.supported;
753 }
754 
755 /**
756  * lwmi_psy_prop_is_supported() - Determine if the property is supported
757  * @priv: Pointer to the lwmi_om_priv drvdata
758  * @prop: The power supply property to be evaluated
759  *
760  * Checks capdata 00 to determine if the property is supported.
761  *
762  * Return: true if readable, or false
763  */
lwmi_psy_prop_is_supported(struct lwmi_om_priv * priv,enum power_supply_property prop)764 static bool lwmi_psy_prop_is_supported(struct lwmi_om_priv *priv, enum power_supply_property prop)
765 {
766 	int ret;
767 
768 	if (force_load_psy_ext)
769 		return true;
770 
771 	ret = lwmi_psy_prop_get_supported(priv, prop);
772 	if (ret < 0)
773 		return false;
774 
775 	return (ret & LWMI_SUPP_VALID) && (ret & LWMI_SUPP_GET);
776 }
777 
778 /**
779  * lwmi_psy_prop_is_writeable() - Determine if the property is writeable
780  * @ps: The battery that was extended
781  * @ext: The extension
782  * @ext_data: Pointer the lwmi_om_priv drvdata
783  * @prop: The property to check
784  *
785  * Checks capdata 00 to determine if the property is writable.
786  *
787  * Return: true if writable, or false
788  */
lwmi_psy_prop_is_writeable(struct power_supply * ps,const struct power_supply_ext * ext,void * ext_data,enum power_supply_property prop)789 static int lwmi_psy_prop_is_writeable(struct power_supply *ps,
790 				      const struct power_supply_ext *ext,
791 				      void *ext_data,
792 				      enum power_supply_property prop)
793 {
794 	struct lwmi_om_priv *priv = ext_data;
795 	int ret;
796 
797 	if (force_load_psy_ext)
798 		return true;
799 
800 	ret = lwmi_psy_prop_get_supported(priv, prop);
801 	if (ret < 0)
802 		return false;
803 
804 	return !!(ret & LWMI_SUPP_SET);
805 }
806 
807 #define DEFINE_LWMI_POWER_SUPPLY_EXTENSION(_name, _props, _behaviours, _types)	\
808 	static const struct power_supply_ext _name = {				\
809 		.name			= LWMI_OM_SYSFS_NAME,			\
810 		.properties		= _props,				\
811 		.num_properties		= ARRAY_SIZE(_props),			\
812 		.charge_behaviours	= _behaviours,				\
813 		.charge_types		= _types,				\
814 		.get_property		= lwmi_psy_ext_get_prop,		\
815 		.set_property		= lwmi_psy_ext_set_prop,		\
816 		.property_is_writeable	= lwmi_psy_prop_is_writeable,		\
817 	}
818 
819 static const enum power_supply_property lwmi_psy_ext_props_all[] = {
820 	POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR,
821 	POWER_SUPPLY_PROP_CHARGE_TYPES,
822 };
823 
824 static const enum power_supply_property lwmi_psy_ext_props_types[] = {
825 	POWER_SUPPLY_PROP_CHARGE_TYPES,
826 };
827 
828 static const enum power_supply_property lwmi_psy_ext_props_behaviour[] = {
829 	POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR,
830 };
831 
832 #define LWMI_CHARGE_BEHAVIOURS (BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO) | \
833 				BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE))
834 
835 #define LWMI_CHARGE_TYPES (BIT(POWER_SUPPLY_CHARGE_TYPE_STANDARD) | \
836 			   BIT(POWER_SUPPLY_CHARGE_TYPE_LONGLIFE))
837 
838 DEFINE_LWMI_POWER_SUPPLY_EXTENSION(lwmi_psy_ext_all, lwmi_psy_ext_props_all,
839 				   LWMI_CHARGE_BEHAVIOURS, LWMI_CHARGE_TYPES);
840 DEFINE_LWMI_POWER_SUPPLY_EXTENSION(lwmi_psy_ext_types, lwmi_psy_ext_props_types,
841 				   0, LWMI_CHARGE_TYPES);
842 DEFINE_LWMI_POWER_SUPPLY_EXTENSION(lwmi_psy_ext_behaviour, lwmi_psy_ext_props_behaviour,
843 				   LWMI_CHARGE_BEHAVIOURS, 0);
844 
845 #define LWMI_PSY_PROP_BEHAVIOUR BIT(0)
846 #define LWMI_PSY_PROP_TYPES BIT(1)
847 
848 static const struct power_supply_ext *lwmi_psy_exts[] = {
849 	[LWMI_PSY_PROP_BEHAVIOUR] =				&lwmi_psy_ext_behaviour,
850 	[LWMI_PSY_PROP_TYPES] =					&lwmi_psy_ext_types,
851 	[LWMI_PSY_PROP_BEHAVIOUR | LWMI_PSY_PROP_TYPES] =	&lwmi_psy_ext_all,
852 };
853 
854 /**
855  * lwmi_add_battery() - Connect the power_supply_ext
856  * @battery: The battery to extend
857  * @hook: The driver hook used to extend the battery
858  *
859  * Return: 0 on success, or an error.
860  */
lwmi_add_battery(struct power_supply * battery,struct acpi_battery_hook * hook)861 static int lwmi_add_battery(struct power_supply *battery, struct acpi_battery_hook *hook)
862 {
863 	struct lwmi_om_priv *priv = container_of(hook, struct lwmi_om_priv, battery_hook);
864 
865 	return power_supply_register_extension(battery, priv->battery_ext, &priv->wdev->dev, priv);
866 }
867 
868 /**
869  * lwmi_remove_battery() - Disconnect the power_supply_ext
870  * @battery: The battery that was extended
871  * @hook: The driver hook used to extend the battery
872  *
873  * Return: 0 on success, or an error.
874  */
lwmi_remove_battery(struct power_supply * battery,struct acpi_battery_hook * hook)875 static int lwmi_remove_battery(struct power_supply *battery, struct acpi_battery_hook *hook)
876 {
877 	struct lwmi_om_priv *priv = container_of(hook, struct lwmi_om_priv, battery_hook);
878 
879 	power_supply_unregister_extension(battery, priv->battery_ext);
880 	return 0;
881 }
882 
883 /**
884  * lwmi_acpi_match() - Attempts to return the ideapad acpi handle
885  * @handle: The ACPI handle that manages battery charging
886  * @lvl: Unused
887  * @context: Void pointer to the acpi_handle object to return
888  * @retval: Unused
889  *
890  * Checks if the ideapad_laptop driver is going to manage charge_type first,
891  * then if not, hooks the battery to our WMI methods.
892  *
893  * Return: AE_CTRL_TERMINATE if found, AE_OK if not found.
894  */
lwmi_acpi_match(acpi_handle handle,u32 lvl,void * context,void ** retval)895 static acpi_status lwmi_acpi_match(acpi_handle handle, u32 lvl,
896 				   void *context, void **retval)
897 {
898 	acpi_handle *ahand = context;
899 
900 	if (!handle)
901 		return AE_OK;
902 
903 	*ahand = handle;
904 
905 	return AE_CTRL_TERMINATE;
906 }
907 
908 /**
909  * lwmi_om_psy_ext_init() - Hooks power supply extension to device battery
910  * @priv: Pointer to the lwmi_om_priv drvdata.
911  *
912  * Checks if the ideapad_laptop driver is going to manage charge attributes first,
913  * then if not, hooks the battery to our WMI methods if they are supported.
914  */
lwmi_om_psy_ext_init(struct lwmi_om_priv * priv)915 static void lwmi_om_psy_ext_init(struct lwmi_om_priv *priv)
916 {
917 	static const char * const ideapad_hid = "VPC2004";
918 	acpi_handle handle = NULL;
919 	unsigned int props = 0;
920 	int ret;
921 
922 	if (lwmi_psy_prop_is_supported(priv, POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR))
923 		props |= LWMI_PSY_PROP_BEHAVIOUR;
924 	if (lwmi_psy_prop_is_supported(priv, POWER_SUPPLY_PROP_CHARGE_TYPES))
925 		props |= LWMI_PSY_PROP_TYPES;
926 	if (!props)
927 		return;
928 	if (force_load_psy_ext)
929 		goto load_psy_ext;
930 
931 	/* Deconflict ideapad_laptop driver */
932 	ret = acpi_get_devices(ideapad_hid, lwmi_acpi_match, &handle, NULL);
933 	if (ret)
934 		return;
935 
936 	if (handle && acpi_has_method(handle, "GBMD") && acpi_has_method(handle, "SBMC")) {
937 		dev_dbg(&priv->wdev->dev, "ideapad_laptop driver manages battery for device\n");
938 		return;
939 	}
940 
941 load_psy_ext:
942 	/* Add battery hooks */
943 	priv->battery_ext = lwmi_psy_exts[props];
944 	priv->battery_hook.add_battery = lwmi_add_battery;
945 	priv->battery_hook.remove_battery = lwmi_remove_battery;
946 	priv->battery_hook.name = "Lenovo WMI Other Battery Extension";
947 	priv->bh_registered = true;
948 
949 	battery_hook_register(&priv->battery_hook);
950 }
951 
952 /**
953  * lwmi_om_psy_remove() - Unregister battery hook
954  * @priv: Driver private data
955  *
956  * Unregisters the battery hook if applicable.
957  */
lwmi_om_psy_remove(struct lwmi_om_priv * priv)958 static void lwmi_om_psy_remove(struct lwmi_om_priv *priv)
959 {
960 	if (!priv->bh_registered)
961 		return;
962 
963 	battery_hook_unregister(&priv->battery_hook);
964 	priv->bh_registered = false;
965 }
966 
967 /* ======== fw_attributes (component: lenovo-wmi-capdata 01) ======== */
968 
969 struct tunable_attr_01 {
970 	struct device *dev;
971 	u8 feature_id;
972 	u8 device_id;
973 	u8 type_id;
974 	u8 cd_mode_id; /* mode arg for searching capdata */
975 	u8 cv_mode_id; /* mode arg for set/get current_value */
976 };
977 
978 /**
979  * tunable_attr_01_id() - Formats a tunable_attr_01 to a capdata attribute ID
980  * @attr: The tunable_attr_01 to format.
981  * @mode: The u8 corresponding to the wmi-gamezone mode for set/get.
982  *
983  * Return: encoded capability data attribute ID.
984  */
tunable_attr_01_id(struct tunable_attr_01 * attr,u8 mode)985 static u32 tunable_attr_01_id(struct tunable_attr_01 *attr, u8 mode)
986 {
987 	return lwmi_attr_id(attr->device_id, attr->feature_id, mode, attr->type_id);
988 }
989 
990 static struct tunable_attr_01 ppt_pl1_spl = {
991 	.device_id = LWMI_DEVICE_ID_CPU,
992 	.feature_id = LWMI_FEATURE_ID_CPU_SPL,
993 	.type_id = LWMI_TYPE_ID_NONE,
994 };
995 
996 static struct tunable_attr_01 ppt_pl1_spl_cl = {
997 	.device_id = LWMI_DEVICE_ID_CPU,
998 	.feature_id = LWMI_FEATURE_ID_CPU_SPL,
999 	.type_id = LWMI_TYPE_ID_CROSSLOAD,
1000 };
1001 
1002 static struct tunable_attr_01 ppt_pl2_sppt = {
1003 	.device_id = LWMI_DEVICE_ID_CPU,
1004 	.feature_id = LWMI_FEATURE_ID_CPU_SPPT,
1005 	.type_id = LWMI_TYPE_ID_NONE,
1006 };
1007 
1008 static struct tunable_attr_01 ppt_pl2_sppt_cl = {
1009 	.device_id = LWMI_DEVICE_ID_CPU,
1010 	.feature_id = LWMI_FEATURE_ID_CPU_SPPT,
1011 	.type_id = LWMI_TYPE_ID_CROSSLOAD,
1012 };
1013 
1014 static struct tunable_attr_01 ppt_pl3_fppt = {
1015 	.device_id = LWMI_DEVICE_ID_CPU,
1016 	.feature_id = LWMI_FEATURE_ID_CPU_FPPT,
1017 	.type_id = LWMI_TYPE_ID_NONE,
1018 };
1019 
1020 static struct tunable_attr_01 ppt_pl3_fppt_cl = {
1021 	.device_id = LWMI_DEVICE_ID_CPU,
1022 	.feature_id = LWMI_FEATURE_ID_CPU_FPPT,
1023 	.type_id = LWMI_TYPE_ID_CROSSLOAD,
1024 };
1025 
1026 static struct tunable_attr_01 cpu_temp = {
1027 	.device_id = LWMI_DEVICE_ID_CPU,
1028 	.feature_id = LWMI_FEATURE_ID_CPU_TEMP,
1029 	.type_id = LWMI_TYPE_ID_NONE,
1030 };
1031 
1032 static struct tunable_attr_01 ppt_pl1_apu_spl = {
1033 	.device_id = LWMI_DEVICE_ID_CPU,
1034 	.feature_id = LWMI_FEATURE_ID_CPU_APU,
1035 	.type_id = LWMI_TYPE_ID_NONE,
1036 };
1037 
1038 static struct tunable_attr_01 ppt_cpu_cl = {
1039 	.device_id = LWMI_DEVICE_ID_CPU,
1040 	.feature_id = LWMI_FEATURE_ID_CPU_CL,
1041 	.type_id = LWMI_TYPE_ID_NONE,
1042 };
1043 
1044 static struct tunable_attr_01 ppt_pl1_tau = {
1045 	.device_id = LWMI_DEVICE_ID_CPU,
1046 	.feature_id = LWMI_FEATURE_ID_CPU_TAU,
1047 	.type_id = LWMI_TYPE_ID_NONE,
1048 };
1049 
1050 static struct tunable_attr_01 ppt_pl4_ipl = {
1051 	.device_id = LWMI_DEVICE_ID_CPU,
1052 	.feature_id = LWMI_FEATURE_ID_CPU_IPL,
1053 	.type_id = LWMI_TYPE_ID_NONE,
1054 };
1055 
1056 static struct tunable_attr_01 ppt_pl4_ipl_cl = {
1057 	.device_id = LWMI_DEVICE_ID_CPU,
1058 	.feature_id = LWMI_FEATURE_ID_CPU_IPL,
1059 	.type_id = LWMI_TYPE_ID_CROSSLOAD,
1060 };
1061 
1062 static struct tunable_attr_01 gpu_nv_ppab = {
1063 	.device_id = LWMI_DEVICE_ID_GPU,
1064 	.feature_id = LWMI_FEATURE_ID_GPU_NV_PPAB,
1065 	.type_id = LWMI_TYPE_ID_NONE,
1066 };
1067 
1068 static struct tunable_attr_01 gpu_nv_ctgp = {
1069 	.device_id = LWMI_DEVICE_ID_GPU,
1070 	.feature_id = LWMI_FEATURE_ID_GPU_NV_CTGP,
1071 	.type_id = LWMI_TYPE_ID_NONE,
1072 };
1073 
1074 static struct tunable_attr_01 gpu_temp = {
1075 	.device_id = LWMI_DEVICE_ID_GPU,
1076 	.feature_id = LWMI_FEATURE_ID_GPU_TEMP,
1077 	.type_id = LWMI_TYPE_ID_NONE,
1078 };
1079 
1080 static struct tunable_attr_01 gpu_nv_ac_offset = {
1081 	.device_id = LWMI_DEVICE_ID_GPU,
1082 	.feature_id = LWMI_FEATURE_ID_GPU_AC_OFFSET,
1083 	.type_id = LWMI_TYPE_ID_NONE,
1084 };
1085 
1086 static struct tunable_attr_01 dgpu_boost_clk = {
1087 	.device_id = LWMI_DEVICE_ID_GPU,
1088 	.feature_id = LWMI_FEATURE_ID_DGPU_BOOST_CLK,
1089 	.type_id = LWMI_TYPE_ID_NONE,
1090 };
1091 
1092 static struct tunable_attr_01 dgpu_enable = {
1093 	.device_id = LWMI_DEVICE_ID_GPU,
1094 	.feature_id = LWMI_FEATURE_ID_DGPU_EN,
1095 	.type_id = LWMI_TYPE_ID_NONE,
1096 };
1097 
1098 static struct tunable_attr_01 gpu_mode = {
1099 	.device_id = LWMI_DEVICE_ID_GPU,
1100 	.feature_id = LWMI_FEATURE_ID_GPU_MODE,
1101 	.type_id = LWMI_TYPE_ID_NONE,
1102 };
1103 
1104 static struct tunable_attr_01 dgpu_didvid = {
1105 	.device_id = LWMI_DEVICE_ID_GPU,
1106 	.feature_id = LWMI_FEATURE_ID_DGPU_DIDVID,
1107 	.type_id = LWMI_TYPE_ID_NONE,
1108 };
1109 
1110 static struct tunable_attr_01 gpu_nv_bpl = {
1111 	.device_id = LWMI_DEVICE_ID_GPU,
1112 	.feature_id = LWMI_FEATURE_ID_GPU_NV_BPL,
1113 	.type_id = LWMI_TYPE_ID_NONE,
1114 };
1115 
1116 static struct tunable_attr_01 gpu_nv_cpu_boost = {
1117 	.device_id = LWMI_DEVICE_ID_GPU,
1118 	.feature_id = LWMI_FEATURE_ID_GPU_NV_CPU_BOOST,
1119 	.type_id = LWMI_TYPE_ID_NONE,
1120 };
1121 
1122 struct capdata01_attr_group {
1123 	const struct attribute_group *attr_group;
1124 	struct tunable_attr_01 *tunable_attr;
1125 };
1126 
1127 /* Attribute Methods */
1128 
1129 /**
1130  * int_type_show() - Emit the data type for an integer attribute
1131  * @kobj: Pointer to the driver object.
1132  * @kattr: Pointer to the attribute calling this function.
1133  * @buf: The buffer to write to.
1134  *
1135  * Return: Number of characters written to buf.
1136  */
int_type_show(struct kobject * kobj,struct kobj_attribute * kattr,char * buf)1137 static ssize_t int_type_show(struct kobject *kobj, struct kobj_attribute *kattr,
1138 			     char *buf)
1139 {
1140 	return sysfs_emit(buf, "integer\n");
1141 }
1142 
1143 /**
1144  * attr_capdata01_show() - Get the value of the specified attribute property
1145  *
1146  * @kobj: Pointer to the driver object.
1147  * @kattr: Pointer to the attribute calling this function.
1148  * @buf: The buffer to write to.
1149  * @tunable_attr: The attribute to be read.
1150  * @prop: The property of this attribute to be read.
1151  *
1152  * Retrieves the given property from the capability data 01 struct for the
1153  * specified attribute's "custom" thermal mode. This function is intended
1154  * to be generic so it can be called from any integer attributes "_show"
1155  * function.
1156  *
1157  * If the WMI is success the sysfs attribute is notified.
1158  *
1159  * Return: Either number of characters written to buf, or an error code.
1160  */
attr_capdata01_show(struct kobject * kobj,struct kobj_attribute * kattr,char * buf,struct tunable_attr_01 * tunable_attr,enum attribute_property prop)1161 static ssize_t attr_capdata01_show(struct kobject *kobj,
1162 				   struct kobj_attribute *kattr, char *buf,
1163 				   struct tunable_attr_01 *tunable_attr,
1164 				   enum attribute_property prop)
1165 {
1166 	struct lwmi_om_priv *priv = dev_get_drvdata(tunable_attr->dev);
1167 	struct capdata01 capdata;
1168 	u32 attribute_id;
1169 	int value, ret;
1170 
1171 	attribute_id = tunable_attr_01_id(tunable_attr, tunable_attr->cd_mode_id);
1172 
1173 	ret = lwmi_cd01_get_data(priv->cd01_list, attribute_id, &capdata);
1174 	if (ret)
1175 		return ret;
1176 
1177 	switch (prop) {
1178 	case DEFAULT_VAL:
1179 		value = capdata.default_value;
1180 		break;
1181 	case MAX_VAL:
1182 		value = capdata.max_value;
1183 		break;
1184 	case MIN_VAL:
1185 		value = capdata.min_value;
1186 		break;
1187 	case STEP_VAL:
1188 		value = capdata.step;
1189 		break;
1190 	default:
1191 		return -EINVAL;
1192 	}
1193 
1194 	return sysfs_emit(buf, "%d\n", value);
1195 }
1196 
1197 /**
1198  * attr_current_value_store() - Set the current value of the given attribute
1199  * @kobj: Pointer to the driver object.
1200  * @kattr: Pointer to the attribute calling this function.
1201  * @buf: The buffer to read from, this is parsed to `int` type.
1202  * @count: Required by sysfs attribute macros, pass in from the callee attr.
1203  * @tunable_attr: The attribute to be stored.
1204  *
1205  * Sets the value of the given attribute when operating under the "custom"
1206  * smartfan profile. The current smartfan profile is retrieved from the
1207  * lenovo-wmi-gamezone driver and error is returned if the result is not
1208  * "custom". This function is intended to be generic so it can be called from
1209  * any integer attribute's "_store" function. The integer to be sent to the WMI
1210  * method is range checked and an error code is returned if out of range.
1211  *
1212  * If the value is valid and WMI is success, then the sysfs attribute is
1213  * notified.
1214  *
1215  * Return: Either count, or an error code.
1216  */
attr_current_value_store(struct kobject * kobj,struct kobj_attribute * kattr,const char * buf,size_t count,struct tunable_attr_01 * tunable_attr)1217 static ssize_t attr_current_value_store(struct kobject *kobj,
1218 					struct kobj_attribute *kattr,
1219 					const char *buf, size_t count,
1220 					struct tunable_attr_01 *tunable_attr)
1221 {
1222 	struct lwmi_om_priv *priv = dev_get_drvdata(tunable_attr->dev);
1223 	struct wmi_method_args_32 args = {};
1224 	struct capdata01 capdata;
1225 	enum thermal_mode mode;
1226 	u32 value;
1227 	int ret;
1228 
1229 	ret = lwmi_tm_notifier_call(&mode);
1230 	if (ret)
1231 		return ret;
1232 
1233 	if (mode != LWMI_GZ_THERMAL_MODE_CUSTOM)
1234 		return -EBUSY;
1235 
1236 	args.arg0 = tunable_attr_01_id(tunable_attr, tunable_attr->cd_mode_id);
1237 
1238 	ret = lwmi_cd01_get_data(priv->cd01_list, args.arg0, &capdata);
1239 	if (ret)
1240 		return ret;
1241 
1242 	ret = kstrtouint(buf, 10, &value);
1243 	if (ret)
1244 		return ret;
1245 
1246 	if (value < capdata.min_value || value > capdata.max_value)
1247 		return -EINVAL;
1248 
1249 	args.arg0 = tunable_attr_01_id(tunable_attr, tunable_attr->cv_mode_id);
1250 	args.arg1 = value;
1251 
1252 	ret = lwmi_dev_evaluate_int(priv->wdev, 0x0, LWMI_FEATURE_VALUE_SET,
1253 				    (unsigned char *)&args, sizeof(args), NULL);
1254 	if (ret)
1255 		return ret;
1256 
1257 	return count;
1258 };
1259 
1260 /**
1261  * attr_current_value_show() - Get the current value of the given attribute
1262  * @kobj: Pointer to the driver object.
1263  * @kattr: Pointer to the attribute calling this function.
1264  * @buf: The buffer to write to.
1265  * @tunable_attr: The attribute to be read.
1266  *
1267  * Retrieves the value of the given attribute for the current smartfan profile.
1268  * The current smartfan profile is retrieved from the lenovo-wmi-gamezone driver.
1269  * This function is intended to be generic so it can be called from any integer
1270  * attribute's "_show" function.
1271  *
1272  * If the WMI is success the sysfs attribute is notified.
1273  *
1274  * Return: Either number of characters written to buf, or an error code.
1275  */
attr_current_value_show(struct kobject * kobj,struct kobj_attribute * kattr,char * buf,struct tunable_attr_01 * tunable_attr)1276 static ssize_t attr_current_value_show(struct kobject *kobj,
1277 				       struct kobj_attribute *kattr, char *buf,
1278 				       struct tunable_attr_01 *tunable_attr)
1279 {
1280 	struct lwmi_om_priv *priv = dev_get_drvdata(tunable_attr->dev);
1281 	struct wmi_method_args_32 args = {};
1282 	enum thermal_mode mode;
1283 	int retval;
1284 	int ret;
1285 
1286 	ret = lwmi_tm_notifier_call(&mode);
1287 	if (ret)
1288 		return ret;
1289 
1290 	/* If "no-mode" is the supported mode, ensure we never send current mode */
1291 	if (tunable_attr->cv_mode_id == LWMI_GZ_THERMAL_MODE_NONE)
1292 		mode = tunable_attr->cv_mode_id;
1293 
1294 	args.arg0 = tunable_attr_01_id(tunable_attr, mode);
1295 
1296 	ret = lwmi_dev_evaluate_int(priv->wdev, 0x0, LWMI_FEATURE_VALUE_GET,
1297 				    (unsigned char *)&args, sizeof(args),
1298 				    &retval);
1299 	if (ret)
1300 		return ret;
1301 
1302 	return sysfs_emit(buf, "%d\n", retval);
1303 }
1304 
1305 /**
1306  * lwmi_attr_01_is_supported() - Determine if the given attribute is supported.
1307  * @tunable_attr: The attribute to verify.
1308  *
1309  * For an attribute to be supported it must have a functional get/set method,
1310  * as well as associated capability data stored in the capdata01 table.
1311  *
1312  * First check if the attribute has a corresponding data table under custom mode
1313  * (0xff), then under no mode (0x00). If either of those passes, check if the
1314  * supported field of the capdata struct is > 0. If it is supported, store the
1315  * successful mode in the cd_mode_id field of tunable_attr.
1316  *
1317  * If the attribute capdata shows it is supported, attempt to determine the mode
1318  * for the current value property get/set methods using a similar pattern to the
1319  * capdata table check. If the value returned by either mode is 0 or an error,
1320  * assume that mode is not supported. Otherwise, store the successful mode in the
1321  * cv_mode_id field of tunable_attr.
1322  *
1323  * If any of the above checks fail then the attribute is not fully supported.
1324  *
1325  * Return: true if capdata and set/get modes are found, otherwise false.
1326  */
lwmi_attr_01_is_supported(struct tunable_attr_01 * tunable_attr)1327 static bool lwmi_attr_01_is_supported(struct tunable_attr_01 *tunable_attr)
1328 {
1329 	u8 modes[2] = { LWMI_GZ_THERMAL_MODE_CUSTOM, LWMI_GZ_THERMAL_MODE_NONE };
1330 	struct lwmi_om_priv *priv = dev_get_drvdata(tunable_attr->dev);
1331 	struct wmi_method_args_32 args = {};
1332 	bool cd_mode_found = false;
1333 	bool cv_mode_found = false;
1334 	struct capdata01 capdata;
1335 	int retval, ret, i;
1336 
1337 	/* Determine tunable_attr->cd_mode_id */
1338 	for (i = 0; i < ARRAY_SIZE(modes); i++) {
1339 		args.arg0 = tunable_attr_01_id(tunable_attr, modes[i]);
1340 
1341 		ret = lwmi_cd01_get_data(priv->cd01_list, args.arg0, &capdata);
1342 		if (ret || !capdata.supported)
1343 			continue;
1344 
1345 		tunable_attr->cd_mode_id = modes[i];
1346 		cd_mode_found = true;
1347 		break;
1348 	}
1349 
1350 	if (!cd_mode_found)
1351 		return cd_mode_found;
1352 
1353 	dev_dbg(tunable_attr->dev,
1354 		"cd_mode_id: %#010x\n", args.arg0);
1355 
1356 	/* Determine tunable_attr->cv_mode_id, returns 1 if supported */
1357 	for (i = 0; i < ARRAY_SIZE(modes); i++) {
1358 		args.arg0 = tunable_attr_01_id(tunable_attr, modes[i]);
1359 
1360 		ret = lwmi_dev_evaluate_int(priv->wdev, 0x0, LWMI_FEATURE_VALUE_GET,
1361 					    (u8 *)&args, sizeof(args),
1362 					    &retval);
1363 		if (ret || !retval)
1364 			continue;
1365 
1366 		tunable_attr->cv_mode_id = modes[i];
1367 		cv_mode_found = true;
1368 		break;
1369 	}
1370 
1371 	if (!cv_mode_found)
1372 		return cv_mode_found;
1373 
1374 	dev_dbg(tunable_attr->dev, "cv_mode_id: %#010x, attribute support level: %#010x\n",
1375 		args.arg0, capdata.supported);
1376 
1377 	return capdata.supported > 0;
1378 }
1379 
1380 /* Lenovo WMI Other Mode Attribute macros */
1381 #define __LWMI_ATTR_RO(_func, _name)                                  \
1382 	{                                                             \
1383 		.attr = { .name = __stringify(_name), .mode = 0444 }, \
1384 		.show = _func##_##_name##_show,                       \
1385 	}
1386 
1387 #define __LWMI_ATTR_RO_AS(_name, _show)                               \
1388 	{                                                             \
1389 		.attr = { .name = __stringify(_name), .mode = 0444 }, \
1390 		.show = _show,                                        \
1391 	}
1392 
1393 #define __LWMI_ATTR_RW(_func, _name) \
1394 	__ATTR(_name, 0644, _func##_##_name##_show, _func##_##_name##_store)
1395 
1396 /* Shows a formatted static variable */
1397 #define __LWMI_ATTR_SHOW_FMT(_prop, _attrname, _fmt, _val)                     \
1398 	static ssize_t _attrname##_##_prop##_show(                             \
1399 		struct kobject *kobj, struct kobj_attribute *kattr, char *buf) \
1400 	{                                                                      \
1401 		return sysfs_emit(buf, _fmt, _val);                            \
1402 	}                                                                      \
1403 	static struct kobj_attribute attr_##_attrname##_##_prop =              \
1404 		__LWMI_ATTR_RO(_attrname, _prop)
1405 
1406 /* Attribute current value read/write */
1407 #define __LWMI_TUNABLE_CURRENT_VALUE_CAP01(_attrname)                          \
1408 	static ssize_t _attrname##_current_value_store(                        \
1409 		struct kobject *kobj, struct kobj_attribute *kattr,            \
1410 		const char *buf, size_t count)                                 \
1411 	{                                                                      \
1412 		return attr_current_value_store(kobj, kattr, buf, count,       \
1413 						&_attrname);                   \
1414 	}                                                                      \
1415 	static ssize_t _attrname##_current_value_show(                         \
1416 		struct kobject *kobj, struct kobj_attribute *kattr, char *buf) \
1417 	{                                                                      \
1418 		return attr_current_value_show(kobj, kattr, buf, &_attrname);  \
1419 	}                                                                      \
1420 	static struct kobj_attribute attr_##_attrname##_current_value =        \
1421 		__LWMI_ATTR_RW(_attrname, current_value)
1422 
1423 /* Attribute property read only */
1424 #define __LWMI_TUNABLE_RO_CAP01(_prop, _attrname, _prop_type)                  \
1425 	static ssize_t _attrname##_##_prop##_show(                             \
1426 		struct kobject *kobj, struct kobj_attribute *kattr, char *buf) \
1427 	{                                                                      \
1428 		return attr_capdata01_show(kobj, kattr, buf, &_attrname,       \
1429 					   _prop_type);                        \
1430 	}                                                                      \
1431 	static struct kobj_attribute attr_##_attrname##_##_prop =              \
1432 		__LWMI_ATTR_RO(_attrname, _prop)
1433 
1434 #define LWMI_ATTR_GROUP_TUNABLE_CAP01(_attrname, _fsname, _dispname)      \
1435 	__LWMI_TUNABLE_CURRENT_VALUE_CAP01(_attrname);                    \
1436 	__LWMI_TUNABLE_RO_CAP01(default_value, _attrname, DEFAULT_VAL);   \
1437 	__LWMI_ATTR_SHOW_FMT(display_name, _attrname, "%s\n", _dispname); \
1438 	__LWMI_TUNABLE_RO_CAP01(max_value, _attrname, MAX_VAL);           \
1439 	__LWMI_TUNABLE_RO_CAP01(min_value, _attrname, MIN_VAL);           \
1440 	__LWMI_TUNABLE_RO_CAP01(scalar_increment, _attrname, STEP_VAL);   \
1441 	static struct kobj_attribute attr_##_attrname##_type =            \
1442 		__LWMI_ATTR_RO_AS(type, int_type_show);                   \
1443 	static struct attribute *_attrname##_attrs[] = {                  \
1444 		&attr_##_attrname##_current_value.attr,                   \
1445 		&attr_##_attrname##_default_value.attr,                   \
1446 		&attr_##_attrname##_display_name.attr,                    \
1447 		&attr_##_attrname##_max_value.attr,                       \
1448 		&attr_##_attrname##_min_value.attr,                       \
1449 		&attr_##_attrname##_scalar_increment.attr,                \
1450 		&attr_##_attrname##_type.attr,                            \
1451 		NULL,                                                     \
1452 	};                                                                \
1453 	static const struct attribute_group _attrname##_attr_group = {    \
1454 		.name = _fsname, .attrs = _attrname##_attrs               \
1455 	}
1456 
1457 /* CPU tunable attributes */
1458 LWMI_ATTR_GROUP_TUNABLE_CAP01(cpu_temp, "cpu_temp",
1459 			      "Set the CPU thermal load limit");
1460 LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_cpu_cl, "ppt_cpu_cl",
1461 			      "Set the CPU cross loading power limit");
1462 LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl1_apu_spl, "ppt_pl1_apu_spl",
1463 			      "Set the APU sustained power limit");
1464 LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl1_spl, "ppt_pl1_spl",
1465 			      "Set the CPU sustained power limit");
1466 LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl1_spl_cl, "ppt_pl1_spl_cl",
1467 			      "Set the CPU cross loading sustained power limit");
1468 LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl2_sppt, "ppt_pl2_sppt",
1469 			      "Set the CPU slow package power tracking limit");
1470 LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl2_sppt_cl, "ppt_pl2_sppt_cl",
1471 			      "Set the CPU cross loading slow package power tracking limit");
1472 LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl3_fppt, "ppt_pl3_fppt",
1473 			      "Set the CPU fast package power tracking limit");
1474 LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl3_fppt_cl, "ppt_pl3_fppt_cl",
1475 			      "Set the CPU cross loading fast package power tracking limit");
1476 LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl1_tau, "ppt_pl1_tau",
1477 			      "Set the CPU sustained power limit exceed duration");
1478 LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl4_ipl, "ppt_pl4_ipl",
1479 			      "Set the CPU instantaneous power limit");
1480 LWMI_ATTR_GROUP_TUNABLE_CAP01(ppt_pl4_ipl_cl, "ppt_pl4_ipl_cl",
1481 			      "Set the CPU cross loading instantaneous power limit");
1482 
1483 /* GPU tunable attributes */
1484 LWMI_ATTR_GROUP_TUNABLE_CAP01(dgpu_boost_clk, "dgpu_boost_clk",
1485 			      "Set the dedicated GPU boost clock");
1486 LWMI_ATTR_GROUP_TUNABLE_CAP01(dgpu_didvid, "dgpu_didvid",
1487 			      "Get the GPU device identifier and vendor identifier");
1488 LWMI_ATTR_GROUP_TUNABLE_CAP01(dgpu_enable, "dgpu_enable",
1489 			      "Set the dedicated Nvidia GPU enabled status");
1490 LWMI_ATTR_GROUP_TUNABLE_CAP01(gpu_mode, "gpu_mode",
1491 			      "Set the GPU mode by power limit");
1492 LWMI_ATTR_GROUP_TUNABLE_CAP01(gpu_nv_ac_offset, "gpu_nv_ac_offset",
1493 			      "Set the Nvidia GPU AC total processing power baseline offset");
1494 LWMI_ATTR_GROUP_TUNABLE_CAP01(gpu_nv_bpl, "gpu_nv_bpl",
1495 			      "Set the Nvidia GPU base power limit");
1496 LWMI_ATTR_GROUP_TUNABLE_CAP01(gpu_nv_cpu_boost, "gpu_nv_cpu_boost",
1497 			      "Set the Nvidia GPU to CPU dynamic boost limit");
1498 LWMI_ATTR_GROUP_TUNABLE_CAP01(gpu_nv_ctgp, "gpu_nv_ctgp",
1499 			      "Set the GPU configurable total graphics power");
1500 LWMI_ATTR_GROUP_TUNABLE_CAP01(gpu_nv_ppab, "gpu_nv_ppab",
1501 			      "Set the Nvidia GPU power performance aware boost limit");
1502 LWMI_ATTR_GROUP_TUNABLE_CAP01(gpu_temp, "gpu_temp",
1503 			      "Set the GPU thermal load limit");
1504 
1505 static struct capdata01_attr_group cd01_attr_groups[] = {
1506 	{ &cpu_temp_attr_group, &cpu_temp },
1507 	{ &dgpu_boost_clk_attr_group, &dgpu_boost_clk },
1508 	{ &dgpu_didvid_attr_group, &dgpu_didvid },
1509 	{ &dgpu_enable_attr_group, &dgpu_enable },
1510 	{ &gpu_mode_attr_group, &gpu_mode },
1511 	{ &gpu_nv_ac_offset_attr_group, &gpu_nv_ac_offset },
1512 	{ &gpu_nv_bpl_attr_group, &gpu_nv_bpl },
1513 	{ &gpu_nv_cpu_boost_attr_group, &gpu_nv_cpu_boost },
1514 	{ &gpu_nv_ctgp_attr_group, &gpu_nv_ctgp },
1515 	{ &gpu_nv_ppab_attr_group, &gpu_nv_ppab },
1516 	{ &gpu_temp_attr_group, &gpu_temp },
1517 	{ &ppt_cpu_cl_attr_group, &ppt_cpu_cl },
1518 	{ &ppt_pl1_apu_spl_attr_group, &ppt_pl1_apu_spl },
1519 	{ &ppt_pl1_spl_attr_group, &ppt_pl1_spl },
1520 	{ &ppt_pl1_spl_cl_attr_group, &ppt_pl1_spl_cl },
1521 	{ &ppt_pl1_tau_attr_group, &ppt_pl1_tau },
1522 	{ &ppt_pl2_sppt_attr_group, &ppt_pl2_sppt },
1523 	{ &ppt_pl2_sppt_cl_attr_group, &ppt_pl2_sppt_cl },
1524 	{ &ppt_pl3_fppt_attr_group, &ppt_pl3_fppt },
1525 	{ &ppt_pl3_fppt_cl_attr_group, &ppt_pl3_fppt_cl },
1526 	{ &ppt_pl4_ipl_attr_group, &ppt_pl4_ipl },
1527 	{ &ppt_pl4_ipl_cl_attr_group, &ppt_pl4_ipl_cl },
1528 	{},
1529 };
1530 
1531 /**
1532  * lwmi_om_fw_attr_add() - Register all firmware_attributes_class members
1533  * @priv: The Other Mode driver data.
1534  */
lwmi_om_fw_attr_add(struct lwmi_om_priv * priv)1535 static void lwmi_om_fw_attr_add(struct lwmi_om_priv *priv)
1536 {
1537 	unsigned int i;
1538 	int err;
1539 
1540 	err = ida_alloc(&lwmi_om_ida, GFP_KERNEL);
1541 	if (err < 0)
1542 		goto err_no_ida;
1543 
1544 	priv->ida_id = err;
1545 
1546 	priv->fw_attr_dev = device_create(&firmware_attributes_class, NULL,
1547 					  MKDEV(0, 0), NULL, "%s-%u",
1548 					  LWMI_OM_SYSFS_NAME, priv->ida_id);
1549 	if (IS_ERR(priv->fw_attr_dev)) {
1550 		err = PTR_ERR(priv->fw_attr_dev);
1551 		goto err_free_ida;
1552 	}
1553 
1554 	priv->fw_attr_kset = kset_create_and_add("attributes", NULL,
1555 						 &priv->fw_attr_dev->kobj);
1556 	if (!priv->fw_attr_kset) {
1557 		err = -ENOMEM;
1558 		goto err_destroy_classdev;
1559 	}
1560 
1561 	for (i = 0; i < ARRAY_SIZE(cd01_attr_groups) - 1; i++) {
1562 		cd01_attr_groups[i].tunable_attr->dev = &priv->wdev->dev;
1563 		if (!lwmi_attr_01_is_supported(cd01_attr_groups[i].tunable_attr))
1564 			continue;
1565 
1566 		err = sysfs_create_group(&priv->fw_attr_kset->kobj,
1567 					 cd01_attr_groups[i].attr_group);
1568 		if (err)
1569 			goto err_remove_groups;
1570 	}
1571 	return;
1572 
1573 err_remove_groups:
1574 	while (i--)
1575 		sysfs_remove_group(&priv->fw_attr_kset->kobj,
1576 				   cd01_attr_groups[i].attr_group);
1577 
1578 	kset_unregister(priv->fw_attr_kset);
1579 
1580 err_destroy_classdev:
1581 	device_unregister(priv->fw_attr_dev);
1582 
1583 err_free_ida:
1584 	ida_free(&lwmi_om_ida, priv->ida_id);
1585 
1586 err_no_ida:
1587 	priv->ida_id = -EIDRM;
1588 
1589 	dev_warn(&priv->wdev->dev,
1590 		 "failed to register firmware-attributes device: %d\n", err);
1591 }
1592 
1593 /**
1594  * lwmi_om_fw_attr_remove() - Unregister all capability data attribute groups
1595  * @priv: the lenovo-wmi-other driver data.
1596  */
lwmi_om_fw_attr_remove(struct lwmi_om_priv * priv)1597 static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv)
1598 {
1599 	if (priv->ida_id < 0)
1600 		return;
1601 
1602 	for (unsigned int i = 0; i < ARRAY_SIZE(cd01_attr_groups) - 1; i++)
1603 		sysfs_remove_group(&priv->fw_attr_kset->kobj,
1604 				   cd01_attr_groups[i].attr_group);
1605 
1606 	kset_unregister(priv->fw_attr_kset);
1607 	device_unregister(priv->fw_attr_dev);
1608 	ida_free(&lwmi_om_ida, priv->ida_id);
1609 	priv->ida_id = -EIDRM;
1610 }
1611 
1612 /* ======== Self (master: lenovo-wmi-other) ======== */
1613 
1614 /**
1615  * lwmi_om_master_bind() - Bind all components of the other mode driver
1616  * @dev: The lenovo-wmi-other driver basic device.
1617  *
1618  * Call component_bind_all to bind the lenovo-wmi-capdata devices to the
1619  * lenovo-wmi-other master driver, with a callback to collect fan info from
1620  * capdata_fan. On success, assign the capability data list pointers to the
1621  * driver data struct for later access. These pointers are only valid while the
1622  * capdata interfaces exist. Finally, collect fan info from capdata00 and
1623  * register all firmware attribute groups. Note that the HWMON device is
1624  * registered only if all fan info is collected. Hence, it is not registered
1625  * here. See lwmi_om_fan_info_collect_cd00() and
1626  * lwmi_om_fan_info_collect_cd_fan().
1627  *
1628  * Return: 0 on success, or an error code.
1629  */
lwmi_om_master_bind(struct device * dev)1630 static int lwmi_om_master_bind(struct device *dev)
1631 {
1632 	struct lwmi_om_priv *priv = dev_get_drvdata(dev);
1633 	struct lwmi_cd_binder binder = {
1634 		.cd_fan_list_cb = lwmi_om_fan_info_collect_cd_fan,
1635 	};
1636 	int ret;
1637 
1638 	lwmi_om_fan_info_init(priv);
1639 
1640 	ret = component_bind_all(dev, &binder);
1641 	if (ret)
1642 		return ret;
1643 
1644 	priv->cd00_list = binder.cd00_list;
1645 	priv->cd01_list = binder.cd01_list;
1646 	if (!priv->cd00_list || !priv->cd01_list) {
1647 		component_unbind_all(dev, NULL);
1648 
1649 		return -ENODEV;
1650 	}
1651 
1652 	lwmi_om_fan_info_collect_cd00(priv);
1653 	lwmi_om_psy_ext_init(priv);
1654 
1655 	lwmi_om_fw_attr_add(priv);
1656 
1657 	return 0;
1658 }
1659 
1660 /**
1661  * lwmi_om_master_unbind() - Unbind all components of the other mode driver
1662  * @dev: The lenovo-wmi-other driver basic device
1663  *
1664  * Unregister all firmware attribute groups and the HWMON device. Then call
1665  * component_unbind_all to unbind lenovo-wmi-capdata devices from the
1666  * lenovo-wmi-other master driver.
1667  */
lwmi_om_master_unbind(struct device * dev)1668 static void lwmi_om_master_unbind(struct device *dev)
1669 {
1670 	struct lwmi_om_priv *priv = dev_get_drvdata(dev);
1671 
1672 	lwmi_om_fw_attr_remove(priv);
1673 
1674 	lwmi_om_hwmon_remove(priv);
1675 
1676 	lwmi_om_psy_remove(priv);
1677 
1678 	component_unbind_all(dev, NULL);
1679 }
1680 
1681 static const struct component_master_ops lwmi_om_master_ops = {
1682 	.bind = lwmi_om_master_bind,
1683 	.unbind = lwmi_om_master_unbind,
1684 };
1685 
lwmi_other_probe(struct wmi_device * wdev,const void * context)1686 static int lwmi_other_probe(struct wmi_device *wdev, const void *context)
1687 {
1688 	struct component_match *master_match = NULL;
1689 	struct lwmi_om_priv *priv;
1690 
1691 	priv = devm_kzalloc(&wdev->dev, sizeof(*priv), GFP_KERNEL);
1692 	if (!priv)
1693 		return -ENOMEM;
1694 
1695 	/* Sentinel for on-demand ida_free(). */
1696 	priv->ida_id = -EIDRM;
1697 
1698 	priv->wdev = wdev;
1699 	dev_set_drvdata(&wdev->dev, priv);
1700 
1701 	lwmi_cd_match_add_all(&wdev->dev, &master_match);
1702 	if (IS_ERR(master_match))
1703 		return PTR_ERR(master_match);
1704 
1705 	return component_master_add_with_match(&wdev->dev, &lwmi_om_master_ops,
1706 					       master_match);
1707 }
1708 
lwmi_other_remove(struct wmi_device * wdev)1709 static void lwmi_other_remove(struct wmi_device *wdev)
1710 {
1711 	component_master_del(&wdev->dev, &lwmi_om_master_ops);
1712 }
1713 
1714 static const struct wmi_device_id lwmi_other_id_table[] = {
1715 	{ LENOVO_OTHER_MODE_GUID, NULL },
1716 	{}
1717 };
1718 
1719 static struct wmi_driver lwmi_other_driver = {
1720 	.driver = {
1721 		.name = "lenovo_wmi_other",
1722 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
1723 	},
1724 	.id_table = lwmi_other_id_table,
1725 	.probe = lwmi_other_probe,
1726 	.remove = lwmi_other_remove,
1727 	.no_singleton = true,
1728 };
1729 
1730 module_wmi_driver(lwmi_other_driver);
1731 
1732 MODULE_IMPORT_NS("LENOVO_WMI_CAPDATA");
1733 MODULE_IMPORT_NS("LENOVO_WMI_HELPERS");
1734 MODULE_DEVICE_TABLE(wmi, lwmi_other_id_table);
1735 MODULE_AUTHOR("Derek J. Clark <derekjohn.clark@gmail.com>");
1736 MODULE_AUTHOR("Rong Zhang <i@rong.moe>");
1737 MODULE_DESCRIPTION("Lenovo Other Mode WMI Driver");
1738 MODULE_LICENSE("GPL");
1739