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