xref: /linux/drivers/hwmon/asus_atk0110.c (revision 3fd6c59042dbba50391e30862beac979491145fe)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2007-2009 Luca Tettamanti <kronos.it@gmail.com>
4  *
5  * See COPYING in the top level directory of the kernel tree.
6  */
7 
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 
10 #include <linux/debugfs.h>
11 #include <linux/kernel.h>
12 #include <linux/hwmon.h>
13 #include <linux/list.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/dmi.h>
17 #include <linux/jiffies.h>
18 #include <linux/err.h>
19 #include <linux/acpi.h>
20 
21 #define ATK_HID "ATK0110"
22 
23 static bool new_if;
24 module_param(new_if, bool, 0);
25 MODULE_PARM_DESC(new_if, "Override detection heuristic and force the use of the new ATK0110 interface");
26 
27 static const struct dmi_system_id __initconst atk_force_new_if[] = {
28 	{
29 		/* Old interface has broken MCH temp monitoring */
30 		.ident = "Asus Sabertooth X58",
31 		.matches = {
32 			DMI_MATCH(DMI_BOARD_NAME, "SABERTOOTH X58")
33 		}
34 	}, {
35 		/* Old interface reads the same sensor for fan0 and fan1 */
36 		.ident = "Asus M5A78L",
37 		.matches = {
38 			DMI_MATCH(DMI_BOARD_NAME, "M5A78L")
39 		}
40 	},
41 	{ }
42 };
43 
44 /*
45  * Minimum time between readings, enforced in order to avoid
46  * hogging the CPU.
47  */
48 #define CACHE_TIME		HZ
49 
50 #define BOARD_ID		"MBIF"
51 #define METHOD_ENUMERATE	"GGRP"
52 #define METHOD_READ		"GITM"
53 #define METHOD_WRITE		"SITM"
54 #define METHOD_OLD_READ_TMP	"RTMP"
55 #define METHOD_OLD_READ_VLT	"RVLT"
56 #define METHOD_OLD_READ_FAN	"RFAN"
57 #define METHOD_OLD_ENUM_TMP	"TSIF"
58 #define METHOD_OLD_ENUM_VLT	"VSIF"
59 #define METHOD_OLD_ENUM_FAN	"FSIF"
60 
61 #define ATK_MUX_HWMON		0x00000006ULL
62 #define ATK_MUX_MGMT		0x00000011ULL
63 
64 #define ATK_CLASS_MASK		0xff000000ULL
65 #define ATK_CLASS_FREQ_CTL	0x03000000ULL
66 #define ATK_CLASS_FAN_CTL	0x04000000ULL
67 #define ATK_CLASS_HWMON		0x06000000ULL
68 #define ATK_CLASS_MGMT		0x11000000ULL
69 
70 #define ATK_TYPE_MASK		0x00ff0000ULL
71 #define HWMON_TYPE_VOLT		0x00020000ULL
72 #define HWMON_TYPE_TEMP		0x00030000ULL
73 #define HWMON_TYPE_FAN		0x00040000ULL
74 
75 #define ATK_ELEMENT_ID_MASK	0x0000ffffULL
76 
77 #define ATK_EC_ID		0x11060004ULL
78 
79 enum atk_pack_member {
80 	HWMON_PACK_FLAGS,
81 	HWMON_PACK_NAME,
82 	HWMON_PACK_LIMIT1,
83 	HWMON_PACK_LIMIT2,
84 	HWMON_PACK_ENABLE
85 };
86 
87 /* New package format */
88 #define _HWMON_NEW_PACK_SIZE	7
89 #define _HWMON_NEW_PACK_FLAGS	0
90 #define _HWMON_NEW_PACK_NAME	1
91 #define _HWMON_NEW_PACK_UNK1	2
92 #define _HWMON_NEW_PACK_UNK2	3
93 #define _HWMON_NEW_PACK_LIMIT1	4
94 #define _HWMON_NEW_PACK_LIMIT2	5
95 #define _HWMON_NEW_PACK_ENABLE	6
96 
97 /* Old package format */
98 #define _HWMON_OLD_PACK_SIZE	5
99 #define _HWMON_OLD_PACK_FLAGS	0
100 #define _HWMON_OLD_PACK_NAME	1
101 #define _HWMON_OLD_PACK_LIMIT1	2
102 #define _HWMON_OLD_PACK_LIMIT2	3
103 #define _HWMON_OLD_PACK_ENABLE	4
104 
105 
106 struct atk_data {
107 	struct device *hwmon_dev;
108 	acpi_handle atk_handle;
109 	struct acpi_device *acpi_dev;
110 
111 	bool old_interface;
112 
113 	/* old interface */
114 	acpi_handle rtmp_handle;
115 	acpi_handle rvlt_handle;
116 	acpi_handle rfan_handle;
117 	/* new interface */
118 	acpi_handle enumerate_handle;
119 	acpi_handle read_handle;
120 	acpi_handle write_handle;
121 
122 	bool disable_ec;
123 
124 	int voltage_count;
125 	int temperature_count;
126 	int fan_count;
127 	struct list_head sensor_list;
128 	struct attribute_group attr_group;
129 	const struct attribute_group *attr_groups[2];
130 
131 	struct {
132 		struct dentry *root;
133 		u32 id;
134 	} debugfs;
135 };
136 
137 
138 typedef ssize_t (*sysfs_show_func)(struct device *dev,
139 			struct device_attribute *attr, char *buf);
140 
141 static const struct acpi_device_id atk_ids[] = {
142 	{ATK_HID, 0},
143 	{"", 0},
144 };
145 MODULE_DEVICE_TABLE(acpi, atk_ids);
146 
147 #define ATTR_NAME_SIZE 16 /* Worst case is "tempN_input" */
148 
149 struct atk_sensor_data {
150 	struct list_head list;
151 	struct atk_data *data;
152 	struct device_attribute label_attr;
153 	struct device_attribute input_attr;
154 	struct device_attribute limit1_attr;
155 	struct device_attribute limit2_attr;
156 	char label_attr_name[ATTR_NAME_SIZE];
157 	char input_attr_name[ATTR_NAME_SIZE];
158 	char limit1_attr_name[ATTR_NAME_SIZE];
159 	char limit2_attr_name[ATTR_NAME_SIZE];
160 	u64 id;
161 	u64 type;
162 	u64 limit1;
163 	u64 limit2;
164 	u64 cached_value;
165 	unsigned long last_updated; /* in jiffies */
166 	bool is_valid;
167 	char const *acpi_name;
168 };
169 
170 /*
171  * Return buffer format:
172  * [0-3] "value" is valid flag
173  * [4-7] value
174  * [8- ] unknown stuff on newer mobos
175  */
176 struct atk_acpi_ret_buffer {
177 	u32 flags;
178 	u32 value;
179 	u8 data[];
180 };
181 
182 /* Input buffer used for GITM and SITM methods */
183 struct atk_acpi_input_buf {
184 	u32 id;
185 	u32 param1;
186 	u32 param2;
187 };
188 
189 static int atk_add(struct acpi_device *device);
190 static void atk_remove(struct acpi_device *device);
191 static void atk_print_sensor(struct atk_data *data, union acpi_object *obj);
192 static int atk_read_value(struct atk_sensor_data *sensor, u64 *value);
193 
194 static struct acpi_driver atk_driver = {
195 	.name	= ATK_HID,
196 	.class	= "hwmon",
197 	.ids	= atk_ids,
198 	.ops	= {
199 		.add	= atk_add,
200 		.remove	= atk_remove,
201 	},
202 };
203 
204 #define input_to_atk_sensor(attr) \
205 	container_of(attr, struct atk_sensor_data, input_attr)
206 
207 #define label_to_atk_sensor(attr) \
208 	container_of(attr, struct atk_sensor_data, label_attr)
209 
210 #define limit1_to_atk_sensor(attr) \
211 	container_of(attr, struct atk_sensor_data, limit1_attr)
212 
213 #define limit2_to_atk_sensor(attr) \
214 	container_of(attr, struct atk_sensor_data, limit2_attr)
215 
atk_input_show(struct device * dev,struct device_attribute * attr,char * buf)216 static ssize_t atk_input_show(struct device *dev,
217 		struct device_attribute *attr, char *buf)
218 {
219 	struct atk_sensor_data *s = input_to_atk_sensor(attr);
220 	u64 value;
221 	int err;
222 
223 	err = atk_read_value(s, &value);
224 	if (err)
225 		return err;
226 
227 	if (s->type == HWMON_TYPE_TEMP)
228 		/* ACPI returns decidegree */
229 		value *= 100;
230 
231 	return sprintf(buf, "%llu\n", value);
232 }
233 
atk_label_show(struct device * dev,struct device_attribute * attr,char * buf)234 static ssize_t atk_label_show(struct device *dev,
235 		struct device_attribute *attr, char *buf)
236 {
237 	struct atk_sensor_data *s = label_to_atk_sensor(attr);
238 
239 	return sprintf(buf, "%s\n", s->acpi_name);
240 }
241 
atk_limit1_show(struct device * dev,struct device_attribute * attr,char * buf)242 static ssize_t atk_limit1_show(struct device *dev,
243 		struct device_attribute *attr, char *buf)
244 {
245 	struct atk_sensor_data *s = limit1_to_atk_sensor(attr);
246 	u64 value = s->limit1;
247 
248 	if (s->type == HWMON_TYPE_TEMP)
249 		value *= 100;
250 
251 	return sprintf(buf, "%lld\n", value);
252 }
253 
atk_limit2_show(struct device * dev,struct device_attribute * attr,char * buf)254 static ssize_t atk_limit2_show(struct device *dev,
255 		struct device_attribute *attr, char *buf)
256 {
257 	struct atk_sensor_data *s = limit2_to_atk_sensor(attr);
258 	u64 value = s->limit2;
259 
260 	if (s->type == HWMON_TYPE_TEMP)
261 		value *= 100;
262 
263 	return sprintf(buf, "%lld\n", value);
264 }
265 
atk_init_attribute(struct device_attribute * attr,char * name,sysfs_show_func show)266 static void atk_init_attribute(struct device_attribute *attr, char *name,
267 		sysfs_show_func show)
268 {
269 	sysfs_attr_init(&attr->attr);
270 	attr->attr.name = name;
271 	attr->attr.mode = 0444;
272 	attr->show = show;
273 	attr->store = NULL;
274 }
275 
276 
atk_get_pack_member(struct atk_data * data,union acpi_object * pack,enum atk_pack_member m)277 static union acpi_object *atk_get_pack_member(struct atk_data *data,
278 						union acpi_object *pack,
279 						enum atk_pack_member m)
280 {
281 	bool old_if = data->old_interface;
282 	int offset;
283 
284 	switch (m) {
285 	case HWMON_PACK_FLAGS:
286 		offset = old_if ? _HWMON_OLD_PACK_FLAGS : _HWMON_NEW_PACK_FLAGS;
287 		break;
288 	case HWMON_PACK_NAME:
289 		offset = old_if ? _HWMON_OLD_PACK_NAME : _HWMON_NEW_PACK_NAME;
290 		break;
291 	case HWMON_PACK_LIMIT1:
292 		offset = old_if ? _HWMON_OLD_PACK_LIMIT1 :
293 				  _HWMON_NEW_PACK_LIMIT1;
294 		break;
295 	case HWMON_PACK_LIMIT2:
296 		offset = old_if ? _HWMON_OLD_PACK_LIMIT2 :
297 				  _HWMON_NEW_PACK_LIMIT2;
298 		break;
299 	case HWMON_PACK_ENABLE:
300 		offset = old_if ? _HWMON_OLD_PACK_ENABLE :
301 				  _HWMON_NEW_PACK_ENABLE;
302 		break;
303 	default:
304 		return NULL;
305 	}
306 
307 	return &pack->package.elements[offset];
308 }
309 
310 
311 /*
312  * New package format is:
313  * - flag (int)
314  *	class - used for de-muxing the request to the correct GITn
315  *	type (volt, temp, fan)
316  *	sensor id |
317  *	sensor id - used for de-muxing the request _inside_ the GITn
318  * - name (str)
319  * - unknown (int)
320  * - unknown (int)
321  * - limit1 (int)
322  * - limit2 (int)
323  * - enable (int)
324  *
325  * The old package has the same format but it's missing the two unknown fields.
326  */
validate_hwmon_pack(struct atk_data * data,union acpi_object * obj)327 static int validate_hwmon_pack(struct atk_data *data, union acpi_object *obj)
328 {
329 	struct device *dev = &data->acpi_dev->dev;
330 	union acpi_object *tmp;
331 	bool old_if = data->old_interface;
332 	int const expected_size = old_if ? _HWMON_OLD_PACK_SIZE :
333 					   _HWMON_NEW_PACK_SIZE;
334 
335 	if (obj->type != ACPI_TYPE_PACKAGE) {
336 		dev_warn(dev, "Invalid type: %d\n", obj->type);
337 		return -EINVAL;
338 	}
339 
340 	if (obj->package.count != expected_size) {
341 		dev_warn(dev, "Invalid package size: %d, expected: %d\n",
342 				obj->package.count, expected_size);
343 		return -EINVAL;
344 	}
345 
346 	tmp = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS);
347 	if (tmp->type != ACPI_TYPE_INTEGER) {
348 		dev_warn(dev, "Invalid type (flag): %d\n", tmp->type);
349 		return -EINVAL;
350 	}
351 
352 	tmp = atk_get_pack_member(data, obj, HWMON_PACK_NAME);
353 	if (tmp->type != ACPI_TYPE_STRING) {
354 		dev_warn(dev, "Invalid type (name): %d\n", tmp->type);
355 		return -EINVAL;
356 	}
357 
358 	/* Don't check... we don't know what they're useful for anyway */
359 #if 0
360 	tmp = &obj->package.elements[HWMON_PACK_UNK1];
361 	if (tmp->type != ACPI_TYPE_INTEGER) {
362 		dev_warn(dev, "Invalid type (unk1): %d\n", tmp->type);
363 		return -EINVAL;
364 	}
365 
366 	tmp = &obj->package.elements[HWMON_PACK_UNK2];
367 	if (tmp->type != ACPI_TYPE_INTEGER) {
368 		dev_warn(dev, "Invalid type (unk2): %d\n", tmp->type);
369 		return -EINVAL;
370 	}
371 #endif
372 
373 	tmp = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT1);
374 	if (tmp->type != ACPI_TYPE_INTEGER) {
375 		dev_warn(dev, "Invalid type (limit1): %d\n", tmp->type);
376 		return -EINVAL;
377 	}
378 
379 	tmp = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT2);
380 	if (tmp->type != ACPI_TYPE_INTEGER) {
381 		dev_warn(dev, "Invalid type (limit2): %d\n", tmp->type);
382 		return -EINVAL;
383 	}
384 
385 	tmp = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE);
386 	if (tmp->type != ACPI_TYPE_INTEGER) {
387 		dev_warn(dev, "Invalid type (enable): %d\n", tmp->type);
388 		return -EINVAL;
389 	}
390 
391 	atk_print_sensor(data, obj);
392 
393 	return 0;
394 }
395 
396 #ifdef DEBUG
atk_sensor_type(union acpi_object * flags)397 static char const *atk_sensor_type(union acpi_object *flags)
398 {
399 	u64 type = flags->integer.value & ATK_TYPE_MASK;
400 	char const *what;
401 
402 	switch (type) {
403 	case HWMON_TYPE_VOLT:
404 		what = "voltage";
405 		break;
406 	case HWMON_TYPE_TEMP:
407 		what = "temperature";
408 		break;
409 	case HWMON_TYPE_FAN:
410 		what = "fan";
411 		break;
412 	default:
413 		what = "unknown";
414 		break;
415 	}
416 
417 	return what;
418 }
419 #endif
420 
atk_print_sensor(struct atk_data * data,union acpi_object * obj)421 static void atk_print_sensor(struct atk_data *data, union acpi_object *obj)
422 {
423 #ifdef DEBUG
424 	struct device *dev = &data->acpi_dev->dev;
425 	union acpi_object *flags;
426 	union acpi_object *name;
427 	union acpi_object *limit1;
428 	union acpi_object *limit2;
429 	union acpi_object *enable;
430 	char const *what;
431 
432 	flags = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS);
433 	name = atk_get_pack_member(data, obj, HWMON_PACK_NAME);
434 	limit1 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT1);
435 	limit2 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT2);
436 	enable = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE);
437 
438 	what = atk_sensor_type(flags);
439 
440 	dev_dbg(dev, "%s: %#llx %s [%llu-%llu] %s\n", what,
441 			flags->integer.value,
442 			name->string.pointer,
443 			limit1->integer.value, limit2->integer.value,
444 			enable->integer.value ? "enabled" : "disabled");
445 #endif
446 }
447 
atk_read_value_old(struct atk_sensor_data * sensor,u64 * value)448 static int atk_read_value_old(struct atk_sensor_data *sensor, u64 *value)
449 {
450 	struct atk_data *data = sensor->data;
451 	struct device *dev = &data->acpi_dev->dev;
452 	struct acpi_object_list params;
453 	union acpi_object id;
454 	acpi_status status;
455 	acpi_handle method;
456 
457 	switch (sensor->type) {
458 	case HWMON_TYPE_VOLT:
459 		method = data->rvlt_handle;
460 		break;
461 	case HWMON_TYPE_TEMP:
462 		method = data->rtmp_handle;
463 		break;
464 	case HWMON_TYPE_FAN:
465 		method = data->rfan_handle;
466 		break;
467 	default:
468 		return -EINVAL;
469 	}
470 
471 	id.type = ACPI_TYPE_INTEGER;
472 	id.integer.value = sensor->id;
473 
474 	params.count = 1;
475 	params.pointer = &id;
476 
477 	status = acpi_evaluate_integer(method, NULL, &params, value);
478 	if (status != AE_OK) {
479 		dev_warn(dev, "%s: ACPI exception: %s\n", __func__,
480 				acpi_format_exception(status));
481 		return -EIO;
482 	}
483 
484 	return 0;
485 }
486 
atk_ggrp(struct atk_data * data,u16 mux)487 static union acpi_object *atk_ggrp(struct atk_data *data, u16 mux)
488 {
489 	struct device *dev = &data->acpi_dev->dev;
490 	struct acpi_buffer buf;
491 	acpi_status ret;
492 	struct acpi_object_list params;
493 	union acpi_object id;
494 	union acpi_object *pack;
495 
496 	id.type = ACPI_TYPE_INTEGER;
497 	id.integer.value = mux;
498 	params.count = 1;
499 	params.pointer = &id;
500 
501 	buf.length = ACPI_ALLOCATE_BUFFER;
502 	ret = acpi_evaluate_object(data->enumerate_handle, NULL, &params, &buf);
503 	if (ret != AE_OK) {
504 		dev_err(dev, "GGRP[%#x] ACPI exception: %s\n", mux,
505 				acpi_format_exception(ret));
506 		return ERR_PTR(-EIO);
507 	}
508 	pack = buf.pointer;
509 	if (pack->type != ACPI_TYPE_PACKAGE) {
510 		/* Execution was successful, but the id was not found */
511 		ACPI_FREE(pack);
512 		return ERR_PTR(-ENOENT);
513 	}
514 
515 	if (pack->package.count < 1) {
516 		dev_err(dev, "GGRP[%#x] package is too small\n", mux);
517 		ACPI_FREE(pack);
518 		return ERR_PTR(-EIO);
519 	}
520 	return pack;
521 }
522 
atk_gitm(struct atk_data * data,u64 id)523 static union acpi_object *atk_gitm(struct atk_data *data, u64 id)
524 {
525 	struct device *dev = &data->acpi_dev->dev;
526 	struct atk_acpi_input_buf buf;
527 	union acpi_object tmp;
528 	struct acpi_object_list params;
529 	struct acpi_buffer ret;
530 	union acpi_object *obj;
531 	acpi_status status;
532 
533 	buf.id = id;
534 	buf.param1 = 0;
535 	buf.param2 = 0;
536 
537 	tmp.type = ACPI_TYPE_BUFFER;
538 	tmp.buffer.pointer = (u8 *)&buf;
539 	tmp.buffer.length = sizeof(buf);
540 
541 	params.count = 1;
542 	params.pointer = (void *)&tmp;
543 
544 	ret.length = ACPI_ALLOCATE_BUFFER;
545 	status = acpi_evaluate_object_typed(data->read_handle, NULL, &params,
546 			&ret, ACPI_TYPE_BUFFER);
547 	if (status != AE_OK) {
548 		dev_warn(dev, "GITM[%#llx] ACPI exception: %s\n", id,
549 				acpi_format_exception(status));
550 		return ERR_PTR(-EIO);
551 	}
552 	obj = ret.pointer;
553 
554 	/* Sanity check */
555 	if (obj->buffer.length < 8) {
556 		dev_warn(dev, "Unexpected ASBF length: %u\n",
557 				obj->buffer.length);
558 		ACPI_FREE(obj);
559 		return ERR_PTR(-EIO);
560 	}
561 	return obj;
562 }
563 
atk_sitm(struct atk_data * data,struct atk_acpi_input_buf * buf)564 static union acpi_object *atk_sitm(struct atk_data *data,
565 		struct atk_acpi_input_buf *buf)
566 {
567 	struct device *dev = &data->acpi_dev->dev;
568 	struct acpi_object_list params;
569 	union acpi_object tmp;
570 	struct acpi_buffer ret;
571 	union acpi_object *obj;
572 	acpi_status status;
573 
574 	tmp.type = ACPI_TYPE_BUFFER;
575 	tmp.buffer.pointer = (u8 *)buf;
576 	tmp.buffer.length = sizeof(*buf);
577 
578 	params.count = 1;
579 	params.pointer = &tmp;
580 
581 	ret.length = ACPI_ALLOCATE_BUFFER;
582 	status = acpi_evaluate_object_typed(data->write_handle, NULL, &params,
583 			&ret, ACPI_TYPE_BUFFER);
584 	if (status != AE_OK) {
585 		dev_warn(dev, "SITM[%#x] ACPI exception: %s\n", buf->id,
586 				acpi_format_exception(status));
587 		return ERR_PTR(-EIO);
588 	}
589 	obj = ret.pointer;
590 
591 	/* Sanity check */
592 	if (obj->buffer.length < 8) {
593 		dev_warn(dev, "Unexpected ASBF length: %u\n",
594 				obj->buffer.length);
595 		ACPI_FREE(obj);
596 		return ERR_PTR(-EIO);
597 	}
598 	return obj;
599 }
600 
atk_read_value_new(struct atk_sensor_data * sensor,u64 * value)601 static int atk_read_value_new(struct atk_sensor_data *sensor, u64 *value)
602 {
603 	struct atk_data *data = sensor->data;
604 	struct device *dev = &data->acpi_dev->dev;
605 	union acpi_object *obj;
606 	struct atk_acpi_ret_buffer *buf;
607 	int err = 0;
608 
609 	obj = atk_gitm(data, sensor->id);
610 	if (IS_ERR(obj))
611 		return PTR_ERR(obj);
612 
613 	buf = (struct atk_acpi_ret_buffer *)obj->buffer.pointer;
614 	if (buf->flags == 0) {
615 		/*
616 		 * The reading is not valid, possible causes:
617 		 * - sensor failure
618 		 * - enumeration was FUBAR (and we didn't notice)
619 		 */
620 		dev_warn(dev, "Read failed, sensor = %#llx\n", sensor->id);
621 		err = -EIO;
622 		goto out;
623 	}
624 
625 	*value = buf->value;
626 out:
627 	ACPI_FREE(obj);
628 	return err;
629 }
630 
atk_read_value(struct atk_sensor_data * sensor,u64 * value)631 static int atk_read_value(struct atk_sensor_data *sensor, u64 *value)
632 {
633 	int err;
634 
635 	if (!sensor->is_valid ||
636 	    time_after(jiffies, sensor->last_updated + CACHE_TIME)) {
637 		if (sensor->data->old_interface)
638 			err = atk_read_value_old(sensor, value);
639 		else
640 			err = atk_read_value_new(sensor, value);
641 
642 		if (err)
643 			return err;
644 
645 		sensor->is_valid = true;
646 		sensor->last_updated = jiffies;
647 		sensor->cached_value = *value;
648 	} else {
649 		*value = sensor->cached_value;
650 		err = 0;
651 	}
652 
653 	return err;
654 }
655 
656 #ifdef CONFIG_DEBUG_FS
atk_debugfs_gitm_get(void * p,u64 * val)657 static int atk_debugfs_gitm_get(void *p, u64 *val)
658 {
659 	struct atk_data *data = p;
660 	union acpi_object *ret;
661 	struct atk_acpi_ret_buffer *buf;
662 	int err = 0;
663 
664 	if (!data->read_handle)
665 		return -ENODEV;
666 
667 	if (!data->debugfs.id)
668 		return -EINVAL;
669 
670 	ret = atk_gitm(data, data->debugfs.id);
671 	if (IS_ERR(ret))
672 		return PTR_ERR(ret);
673 
674 	buf = (struct atk_acpi_ret_buffer *)ret->buffer.pointer;
675 	if (buf->flags)
676 		*val = buf->value;
677 	else
678 		err = -EIO;
679 
680 	ACPI_FREE(ret);
681 	return err;
682 }
683 
684 DEFINE_DEBUGFS_ATTRIBUTE(atk_debugfs_gitm, atk_debugfs_gitm_get, NULL,
685 			 "0x%08llx\n");
686 
atk_acpi_print(char * buf,size_t sz,union acpi_object * obj)687 static int atk_acpi_print(char *buf, size_t sz, union acpi_object *obj)
688 {
689 	int ret = 0;
690 
691 	switch (obj->type) {
692 	case ACPI_TYPE_INTEGER:
693 		ret = snprintf(buf, sz, "0x%08llx\n", obj->integer.value);
694 		break;
695 	case ACPI_TYPE_STRING:
696 		ret = snprintf(buf, sz, "%s\n", obj->string.pointer);
697 		break;
698 	}
699 
700 	return ret;
701 }
702 
atk_pack_print(char * buf,size_t sz,union acpi_object * pack)703 static void atk_pack_print(char *buf, size_t sz, union acpi_object *pack)
704 {
705 	int ret;
706 	int i;
707 
708 	for (i = 0; i < pack->package.count; i++) {
709 		union acpi_object *obj = &pack->package.elements[i];
710 
711 		ret = atk_acpi_print(buf, sz, obj);
712 		if (ret >= sz)
713 			break;
714 		buf += ret;
715 		sz -= ret;
716 	}
717 }
718 
atk_debugfs_ggrp_open(struct inode * inode,struct file * file)719 static int atk_debugfs_ggrp_open(struct inode *inode, struct file *file)
720 {
721 	struct atk_data *data = inode->i_private;
722 	char *buf = NULL;
723 	union acpi_object *ret;
724 	u8 cls;
725 	int i;
726 
727 	if (!data->enumerate_handle)
728 		return -ENODEV;
729 	if (!data->debugfs.id)
730 		return -EINVAL;
731 
732 	cls = (data->debugfs.id & 0xff000000) >> 24;
733 	ret = atk_ggrp(data, cls);
734 	if (IS_ERR(ret))
735 		return PTR_ERR(ret);
736 
737 	for (i = 0; i < ret->package.count; i++) {
738 		union acpi_object *pack = &ret->package.elements[i];
739 		union acpi_object *id;
740 
741 		if (pack->type != ACPI_TYPE_PACKAGE)
742 			continue;
743 		if (!pack->package.count)
744 			continue;
745 		id = &pack->package.elements[0];
746 		if (id->integer.value == data->debugfs.id) {
747 			/* Print the package */
748 			buf = kzalloc(512, GFP_KERNEL);
749 			if (!buf) {
750 				ACPI_FREE(ret);
751 				return -ENOMEM;
752 			}
753 			atk_pack_print(buf, 512, pack);
754 			break;
755 		}
756 	}
757 	ACPI_FREE(ret);
758 
759 	if (!buf)
760 		return -EINVAL;
761 
762 	file->private_data = buf;
763 
764 	return nonseekable_open(inode, file);
765 }
766 
atk_debugfs_ggrp_read(struct file * file,char __user * buf,size_t count,loff_t * pos)767 static ssize_t atk_debugfs_ggrp_read(struct file *file, char __user *buf,
768 		size_t count, loff_t *pos)
769 {
770 	char *str = file->private_data;
771 	size_t len = strlen(str);
772 
773 	return simple_read_from_buffer(buf, count, pos, str, len);
774 }
775 
atk_debugfs_ggrp_release(struct inode * inode,struct file * file)776 static int atk_debugfs_ggrp_release(struct inode *inode, struct file *file)
777 {
778 	kfree(file->private_data);
779 	return 0;
780 }
781 
782 static const struct file_operations atk_debugfs_ggrp_fops = {
783 	.read		= atk_debugfs_ggrp_read,
784 	.open		= atk_debugfs_ggrp_open,
785 	.release	= atk_debugfs_ggrp_release,
786 };
787 
atk_debugfs_init(struct atk_data * data)788 static void atk_debugfs_init(struct atk_data *data)
789 {
790 	struct dentry *d;
791 
792 	data->debugfs.id = 0;
793 
794 	d = debugfs_create_dir("asus_atk0110", NULL);
795 
796 	debugfs_create_x32("id", 0600, d, &data->debugfs.id);
797 	debugfs_create_file_unsafe("gitm", 0400, d, data, &atk_debugfs_gitm);
798 	debugfs_create_file("ggrp", 0400, d, data, &atk_debugfs_ggrp_fops);
799 
800 	data->debugfs.root = d;
801 }
802 
atk_debugfs_cleanup(struct atk_data * data)803 static void atk_debugfs_cleanup(struct atk_data *data)
804 {
805 	debugfs_remove_recursive(data->debugfs.root);
806 }
807 
808 #else /* CONFIG_DEBUG_FS */
809 
atk_debugfs_init(struct atk_data * data)810 static void atk_debugfs_init(struct atk_data *data)
811 {
812 }
813 
atk_debugfs_cleanup(struct atk_data * data)814 static void atk_debugfs_cleanup(struct atk_data *data)
815 {
816 }
817 #endif
818 
atk_add_sensor(struct atk_data * data,union acpi_object * obj)819 static int atk_add_sensor(struct atk_data *data, union acpi_object *obj)
820 {
821 	struct device *dev = &data->acpi_dev->dev;
822 	union acpi_object *flags;
823 	union acpi_object *name;
824 	union acpi_object *limit1;
825 	union acpi_object *limit2;
826 	union acpi_object *enable;
827 	struct atk_sensor_data *sensor;
828 	char const *base_name;
829 	char const *limit1_name;
830 	char const *limit2_name;
831 	u64 type;
832 	int err;
833 	int *num;
834 	int start;
835 
836 	if (obj->type != ACPI_TYPE_PACKAGE) {
837 		/* wft is this? */
838 		dev_warn(dev, "Unknown type for ACPI object: (%d)\n",
839 				obj->type);
840 		return -EINVAL;
841 	}
842 
843 	err = validate_hwmon_pack(data, obj);
844 	if (err)
845 		return err;
846 
847 	/* Ok, we have a valid hwmon package */
848 	type = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS)->integer.value
849 	       & ATK_TYPE_MASK;
850 
851 	switch (type) {
852 	case HWMON_TYPE_VOLT:
853 		base_name = "in";
854 		limit1_name = "min";
855 		limit2_name = "max";
856 		num = &data->voltage_count;
857 		start = 0;
858 		break;
859 	case HWMON_TYPE_TEMP:
860 		base_name = "temp";
861 		limit1_name = "max";
862 		limit2_name = "crit";
863 		num = &data->temperature_count;
864 		start = 1;
865 		break;
866 	case HWMON_TYPE_FAN:
867 		base_name = "fan";
868 		limit1_name = "min";
869 		limit2_name = "max";
870 		num = &data->fan_count;
871 		start = 1;
872 		break;
873 	default:
874 		dev_warn(dev, "Unknown sensor type: %#llx\n", type);
875 		return -EINVAL;
876 	}
877 
878 	enable = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE);
879 	if (!enable->integer.value)
880 		/* sensor is disabled */
881 		return 0;
882 
883 	flags = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS);
884 	name = atk_get_pack_member(data, obj, HWMON_PACK_NAME);
885 	limit1 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT1);
886 	limit2 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT2);
887 
888 	sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
889 	if (!sensor)
890 		return -ENOMEM;
891 
892 	sensor->acpi_name = devm_kstrdup(dev, name->string.pointer, GFP_KERNEL);
893 	if (!sensor->acpi_name)
894 		return -ENOMEM;
895 
896 	INIT_LIST_HEAD(&sensor->list);
897 	sensor->type = type;
898 	sensor->data = data;
899 	sensor->id = flags->integer.value;
900 	sensor->limit1 = limit1->integer.value;
901 	if (data->old_interface)
902 		sensor->limit2 = limit2->integer.value;
903 	else
904 		/* The upper limit is expressed as delta from lower limit */
905 		sensor->limit2 = sensor->limit1 + limit2->integer.value;
906 
907 	snprintf(sensor->input_attr_name, ATTR_NAME_SIZE,
908 			"%s%d_input", base_name, start + *num);
909 	atk_init_attribute(&sensor->input_attr,
910 			sensor->input_attr_name,
911 			atk_input_show);
912 
913 	snprintf(sensor->label_attr_name, ATTR_NAME_SIZE,
914 			"%s%d_label", base_name, start + *num);
915 	atk_init_attribute(&sensor->label_attr,
916 			sensor->label_attr_name,
917 			atk_label_show);
918 
919 	snprintf(sensor->limit1_attr_name, ATTR_NAME_SIZE,
920 			"%s%d_%s", base_name, start + *num, limit1_name);
921 	atk_init_attribute(&sensor->limit1_attr,
922 			sensor->limit1_attr_name,
923 			atk_limit1_show);
924 
925 	snprintf(sensor->limit2_attr_name, ATTR_NAME_SIZE,
926 			"%s%d_%s", base_name, start + *num, limit2_name);
927 	atk_init_attribute(&sensor->limit2_attr,
928 			sensor->limit2_attr_name,
929 			atk_limit2_show);
930 
931 	list_add(&sensor->list, &data->sensor_list);
932 	(*num)++;
933 
934 	return 1;
935 }
936 
atk_enumerate_old_hwmon(struct atk_data * data)937 static int atk_enumerate_old_hwmon(struct atk_data *data)
938 {
939 	struct device *dev = &data->acpi_dev->dev;
940 	struct acpi_buffer buf;
941 	union acpi_object *pack;
942 	acpi_status status;
943 	int i, ret;
944 	int count = 0;
945 
946 	/* Voltages */
947 	buf.length = ACPI_ALLOCATE_BUFFER;
948 	status = acpi_evaluate_object_typed(data->atk_handle,
949 			METHOD_OLD_ENUM_VLT, NULL, &buf, ACPI_TYPE_PACKAGE);
950 	if (status != AE_OK) {
951 		dev_warn(dev, METHOD_OLD_ENUM_VLT ": ACPI exception: %s\n",
952 				acpi_format_exception(status));
953 
954 		return -ENODEV;
955 	}
956 
957 	pack = buf.pointer;
958 	for (i = 1; i < pack->package.count; i++) {
959 		union acpi_object *obj = &pack->package.elements[i];
960 
961 		ret = atk_add_sensor(data, obj);
962 		if (ret > 0)
963 			count++;
964 	}
965 	ACPI_FREE(buf.pointer);
966 
967 	/* Temperatures */
968 	buf.length = ACPI_ALLOCATE_BUFFER;
969 	status = acpi_evaluate_object_typed(data->atk_handle,
970 			METHOD_OLD_ENUM_TMP, NULL, &buf, ACPI_TYPE_PACKAGE);
971 	if (status != AE_OK) {
972 		dev_warn(dev, METHOD_OLD_ENUM_TMP ": ACPI exception: %s\n",
973 				acpi_format_exception(status));
974 
975 		return -ENODEV;
976 	}
977 
978 	pack = buf.pointer;
979 	for (i = 1; i < pack->package.count; i++) {
980 		union acpi_object *obj = &pack->package.elements[i];
981 
982 		ret = atk_add_sensor(data, obj);
983 		if (ret > 0)
984 			count++;
985 	}
986 	ACPI_FREE(buf.pointer);
987 
988 	/* Fans */
989 	buf.length = ACPI_ALLOCATE_BUFFER;
990 	status = acpi_evaluate_object_typed(data->atk_handle,
991 			METHOD_OLD_ENUM_FAN, NULL, &buf, ACPI_TYPE_PACKAGE);
992 	if (status != AE_OK) {
993 		dev_warn(dev, METHOD_OLD_ENUM_FAN ": ACPI exception: %s\n",
994 				acpi_format_exception(status));
995 
996 		return -ENODEV;
997 	}
998 
999 	pack = buf.pointer;
1000 	for (i = 1; i < pack->package.count; i++) {
1001 		union acpi_object *obj = &pack->package.elements[i];
1002 
1003 		ret = atk_add_sensor(data, obj);
1004 		if (ret > 0)
1005 			count++;
1006 	}
1007 	ACPI_FREE(buf.pointer);
1008 
1009 	return count;
1010 }
1011 
atk_ec_present(struct atk_data * data)1012 static int atk_ec_present(struct atk_data *data)
1013 {
1014 	struct device *dev = &data->acpi_dev->dev;
1015 	union acpi_object *pack;
1016 	union acpi_object *ec;
1017 	int ret;
1018 	int i;
1019 
1020 	pack = atk_ggrp(data, ATK_MUX_MGMT);
1021 	if (IS_ERR(pack)) {
1022 		if (PTR_ERR(pack) == -ENOENT) {
1023 			/* The MGMT class does not exists - that's ok */
1024 			dev_dbg(dev, "Class %#llx not found\n", ATK_MUX_MGMT);
1025 			return 0;
1026 		}
1027 		return PTR_ERR(pack);
1028 	}
1029 
1030 	/* Search the EC */
1031 	ec = NULL;
1032 	for (i = 0; i < pack->package.count; i++) {
1033 		union acpi_object *obj = &pack->package.elements[i];
1034 		union acpi_object *id;
1035 
1036 		if (obj->type != ACPI_TYPE_PACKAGE)
1037 			continue;
1038 
1039 		id = &obj->package.elements[0];
1040 		if (id->type != ACPI_TYPE_INTEGER)
1041 			continue;
1042 
1043 		if (id->integer.value == ATK_EC_ID) {
1044 			ec = obj;
1045 			break;
1046 		}
1047 	}
1048 
1049 	ret = (ec != NULL);
1050 	if (!ret)
1051 		/* The system has no EC */
1052 		dev_dbg(dev, "EC not found\n");
1053 
1054 	ACPI_FREE(pack);
1055 	return ret;
1056 }
1057 
atk_ec_enabled(struct atk_data * data)1058 static int atk_ec_enabled(struct atk_data *data)
1059 {
1060 	struct device *dev = &data->acpi_dev->dev;
1061 	union acpi_object *obj;
1062 	struct atk_acpi_ret_buffer *buf;
1063 	int err;
1064 
1065 	obj = atk_gitm(data, ATK_EC_ID);
1066 	if (IS_ERR(obj)) {
1067 		dev_err(dev, "Unable to query EC status\n");
1068 		return PTR_ERR(obj);
1069 	}
1070 	buf = (struct atk_acpi_ret_buffer *)obj->buffer.pointer;
1071 
1072 	if (buf->flags == 0) {
1073 		dev_err(dev, "Unable to query EC status\n");
1074 		err = -EIO;
1075 	} else {
1076 		err = (buf->value != 0);
1077 		dev_dbg(dev, "EC is %sabled\n",
1078 				err ? "en" : "dis");
1079 	}
1080 
1081 	ACPI_FREE(obj);
1082 	return err;
1083 }
1084 
atk_ec_ctl(struct atk_data * data,int enable)1085 static int atk_ec_ctl(struct atk_data *data, int enable)
1086 {
1087 	struct device *dev = &data->acpi_dev->dev;
1088 	union acpi_object *obj;
1089 	struct atk_acpi_input_buf sitm;
1090 	struct atk_acpi_ret_buffer *ec_ret;
1091 	int err = 0;
1092 
1093 	sitm.id = ATK_EC_ID;
1094 	sitm.param1 = enable;
1095 	sitm.param2 = 0;
1096 
1097 	obj = atk_sitm(data, &sitm);
1098 	if (IS_ERR(obj)) {
1099 		dev_err(dev, "Failed to %sable the EC\n",
1100 				enable ? "en" : "dis");
1101 		return PTR_ERR(obj);
1102 	}
1103 	ec_ret = (struct atk_acpi_ret_buffer *)obj->buffer.pointer;
1104 	if (ec_ret->flags == 0) {
1105 		dev_err(dev, "Failed to %sable the EC\n",
1106 				enable ? "en" : "dis");
1107 		err = -EIO;
1108 	} else {
1109 		dev_info(dev, "EC %sabled\n",
1110 				enable ? "en" : "dis");
1111 	}
1112 
1113 	ACPI_FREE(obj);
1114 	return err;
1115 }
1116 
atk_enumerate_new_hwmon(struct atk_data * data)1117 static int atk_enumerate_new_hwmon(struct atk_data *data)
1118 {
1119 	struct device *dev = &data->acpi_dev->dev;
1120 	union acpi_object *pack;
1121 	int err;
1122 	int i;
1123 
1124 	err = atk_ec_present(data);
1125 	if (err < 0)
1126 		return err;
1127 	if (err) {
1128 		err = atk_ec_enabled(data);
1129 		if (err < 0)
1130 			return err;
1131 		/* If the EC was disabled we will disable it again on unload */
1132 		data->disable_ec = err;
1133 
1134 		err = atk_ec_ctl(data, 1);
1135 		if (err) {
1136 			data->disable_ec = false;
1137 			return err;
1138 		}
1139 	}
1140 
1141 	dev_dbg(dev, "Enumerating hwmon sensors\n");
1142 
1143 	pack = atk_ggrp(data, ATK_MUX_HWMON);
1144 	if (IS_ERR(pack))
1145 		return PTR_ERR(pack);
1146 
1147 	for (i = 0; i < pack->package.count; i++) {
1148 		union acpi_object *obj = &pack->package.elements[i];
1149 
1150 		atk_add_sensor(data, obj);
1151 	}
1152 
1153 	err = data->voltage_count + data->temperature_count + data->fan_count;
1154 
1155 	ACPI_FREE(pack);
1156 	return err;
1157 }
1158 
atk_init_attribute_groups(struct atk_data * data)1159 static int atk_init_attribute_groups(struct atk_data *data)
1160 {
1161 	struct device *dev = &data->acpi_dev->dev;
1162 	struct atk_sensor_data *s;
1163 	struct attribute **attrs;
1164 	int i = 0;
1165 	int len = (data->voltage_count + data->temperature_count
1166 			+ data->fan_count) * 4 + 1;
1167 
1168 	attrs = devm_kcalloc(dev, len, sizeof(struct attribute *), GFP_KERNEL);
1169 	if (!attrs)
1170 		return -ENOMEM;
1171 
1172 	list_for_each_entry(s, &data->sensor_list, list) {
1173 		attrs[i++] = &s->input_attr.attr;
1174 		attrs[i++] = &s->label_attr.attr;
1175 		attrs[i++] = &s->limit1_attr.attr;
1176 		attrs[i++] = &s->limit2_attr.attr;
1177 	}
1178 
1179 	data->attr_group.attrs = attrs;
1180 	data->attr_groups[0] = &data->attr_group;
1181 
1182 	return 0;
1183 }
1184 
atk_register_hwmon(struct atk_data * data)1185 static int atk_register_hwmon(struct atk_data *data)
1186 {
1187 	struct device *dev = &data->acpi_dev->dev;
1188 
1189 	dev_dbg(dev, "registering hwmon device\n");
1190 	data->hwmon_dev = hwmon_device_register_with_groups(dev, "atk0110",
1191 							    data,
1192 							    data->attr_groups);
1193 
1194 	return PTR_ERR_OR_ZERO(data->hwmon_dev);
1195 }
1196 
atk_probe_if(struct atk_data * data)1197 static int atk_probe_if(struct atk_data *data)
1198 {
1199 	struct device *dev = &data->acpi_dev->dev;
1200 	acpi_handle ret;
1201 	acpi_status status;
1202 	int err = 0;
1203 
1204 	/* RTMP: read temperature */
1205 	status = acpi_get_handle(data->atk_handle, METHOD_OLD_READ_TMP, &ret);
1206 	if (ACPI_SUCCESS(status))
1207 		data->rtmp_handle = ret;
1208 	else
1209 		dev_dbg(dev, "method " METHOD_OLD_READ_TMP " not found: %s\n",
1210 				acpi_format_exception(status));
1211 
1212 	/* RVLT: read voltage */
1213 	status = acpi_get_handle(data->atk_handle, METHOD_OLD_READ_VLT, &ret);
1214 	if (ACPI_SUCCESS(status))
1215 		data->rvlt_handle = ret;
1216 	else
1217 		dev_dbg(dev, "method " METHOD_OLD_READ_VLT " not found: %s\n",
1218 				acpi_format_exception(status));
1219 
1220 	/* RFAN: read fan status */
1221 	status = acpi_get_handle(data->atk_handle, METHOD_OLD_READ_FAN, &ret);
1222 	if (ACPI_SUCCESS(status))
1223 		data->rfan_handle = ret;
1224 	else
1225 		dev_dbg(dev, "method " METHOD_OLD_READ_FAN " not found: %s\n",
1226 				acpi_format_exception(status));
1227 
1228 	/* Enumeration */
1229 	status = acpi_get_handle(data->atk_handle, METHOD_ENUMERATE, &ret);
1230 	if (ACPI_SUCCESS(status))
1231 		data->enumerate_handle = ret;
1232 	else
1233 		dev_dbg(dev, "method " METHOD_ENUMERATE " not found: %s\n",
1234 				acpi_format_exception(status));
1235 
1236 	/* De-multiplexer (read) */
1237 	status = acpi_get_handle(data->atk_handle, METHOD_READ, &ret);
1238 	if (ACPI_SUCCESS(status))
1239 		data->read_handle = ret;
1240 	else
1241 		dev_dbg(dev, "method " METHOD_READ " not found: %s\n",
1242 				acpi_format_exception(status));
1243 
1244 	/* De-multiplexer (write) */
1245 	status = acpi_get_handle(data->atk_handle, METHOD_WRITE, &ret);
1246 	if (ACPI_SUCCESS(status))
1247 		data->write_handle = ret;
1248 	else
1249 		dev_dbg(dev, "method " METHOD_WRITE " not found: %s\n",
1250 				 acpi_format_exception(status));
1251 
1252 	/*
1253 	 * Check for hwmon methods: first check "old" style methods; note that
1254 	 * both may be present: in this case we stick to the old interface;
1255 	 * analysis of multiple DSDTs indicates that when both interfaces
1256 	 * are present the new one (GGRP/GITM) is not functional.
1257 	 */
1258 	if (new_if)
1259 		dev_info(dev, "Overriding interface detection\n");
1260 	if (data->rtmp_handle &&
1261 			data->rvlt_handle && data->rfan_handle && !new_if)
1262 		data->old_interface = true;
1263 	else if (data->enumerate_handle && data->read_handle &&
1264 			data->write_handle)
1265 		data->old_interface = false;
1266 	else
1267 		err = -ENODEV;
1268 
1269 	return err;
1270 }
1271 
atk_add(struct acpi_device * device)1272 static int atk_add(struct acpi_device *device)
1273 {
1274 	acpi_status ret;
1275 	int err;
1276 	struct acpi_buffer buf;
1277 	union acpi_object *obj;
1278 	struct atk_data *data;
1279 
1280 	dev_dbg(&device->dev, "adding...\n");
1281 
1282 	data = devm_kzalloc(&device->dev, sizeof(*data), GFP_KERNEL);
1283 	if (!data)
1284 		return -ENOMEM;
1285 
1286 	data->acpi_dev = device;
1287 	data->atk_handle = device->handle;
1288 	INIT_LIST_HEAD(&data->sensor_list);
1289 	data->disable_ec = false;
1290 
1291 	buf.length = ACPI_ALLOCATE_BUFFER;
1292 	ret = acpi_evaluate_object_typed(data->atk_handle, BOARD_ID, NULL,
1293 			&buf, ACPI_TYPE_PACKAGE);
1294 	if (ret != AE_OK) {
1295 		dev_dbg(&device->dev, "atk: method MBIF not found\n");
1296 	} else {
1297 		obj = buf.pointer;
1298 		if (obj->package.count >= 2) {
1299 			union acpi_object *id = &obj->package.elements[1];
1300 			if (id->type == ACPI_TYPE_STRING)
1301 				dev_dbg(&device->dev, "board ID = %s\n",
1302 					id->string.pointer);
1303 		}
1304 		ACPI_FREE(buf.pointer);
1305 	}
1306 
1307 	err = atk_probe_if(data);
1308 	if (err) {
1309 		dev_err(&device->dev, "No usable hwmon interface detected\n");
1310 		goto out;
1311 	}
1312 
1313 	if (data->old_interface) {
1314 		dev_dbg(&device->dev, "Using old hwmon interface\n");
1315 		err = atk_enumerate_old_hwmon(data);
1316 	} else {
1317 		dev_dbg(&device->dev, "Using new hwmon interface\n");
1318 		err = atk_enumerate_new_hwmon(data);
1319 	}
1320 	if (err < 0)
1321 		goto out;
1322 	if (err == 0) {
1323 		dev_info(&device->dev,
1324 			 "No usable sensor detected, bailing out\n");
1325 		err = -ENODEV;
1326 		goto out;
1327 	}
1328 
1329 	err = atk_init_attribute_groups(data);
1330 	if (err)
1331 		goto out;
1332 	err = atk_register_hwmon(data);
1333 	if (err)
1334 		goto out;
1335 
1336 	atk_debugfs_init(data);
1337 
1338 	device->driver_data = data;
1339 	return 0;
1340 out:
1341 	if (data->disable_ec)
1342 		atk_ec_ctl(data, 0);
1343 	return err;
1344 }
1345 
atk_remove(struct acpi_device * device)1346 static void atk_remove(struct acpi_device *device)
1347 {
1348 	struct atk_data *data = device->driver_data;
1349 	dev_dbg(&device->dev, "removing...\n");
1350 
1351 	device->driver_data = NULL;
1352 
1353 	atk_debugfs_cleanup(data);
1354 
1355 	hwmon_device_unregister(data->hwmon_dev);
1356 
1357 	if (data->disable_ec) {
1358 		if (atk_ec_ctl(data, 0))
1359 			dev_err(&device->dev, "Failed to disable EC\n");
1360 	}
1361 }
1362 
atk0110_init(void)1363 static int __init atk0110_init(void)
1364 {
1365 	int ret;
1366 
1367 	/* Make sure it's safe to access the device through ACPI */
1368 	if (!acpi_resources_are_enforced()) {
1369 		pr_err("Resources not safely usable due to acpi_enforce_resources kernel parameter\n");
1370 		return -EBUSY;
1371 	}
1372 
1373 	if (dmi_check_system(atk_force_new_if))
1374 		new_if = true;
1375 
1376 	ret = acpi_bus_register_driver(&atk_driver);
1377 	if (ret)
1378 		pr_info("acpi_bus_register_driver failed: %d\n", ret);
1379 
1380 	return ret;
1381 }
1382 
atk0110_exit(void)1383 static void __exit atk0110_exit(void)
1384 {
1385 	acpi_bus_unregister_driver(&atk_driver);
1386 }
1387 
1388 module_init(atk0110_init);
1389 module_exit(atk0110_exit);
1390 
1391 MODULE_DESCRIPTION("ASUS ATK0110 driver");
1392 MODULE_LICENSE("GPL");
1393