• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
18 #include <acpi/acpi.h>
19 #include <acpi/acpixf.h>
20 #include <acpi/acpi_drivers.h>
21 #include <acpi/acpi_bus.h>
22 
23 
24 #define ATK_HID "ATK0110"
25 
26 static bool new_if;
27 module_param(new_if, bool, 0);
28 MODULE_PARM_DESC(new_if, "Override detection heuristic and force the use of the new ATK0110 interface");
29 
30 static const struct dmi_system_id __initconst atk_force_new_if[] = {
31 	{
32 		/* Old interface has broken MCH temp monitoring */
33 		.ident = "Asus Sabertooth X58",
34 		.matches = {
35 			DMI_MATCH(DMI_BOARD_NAME, "SABERTOOTH X58")
36 		}
37 	}, {
38 		/* Old interface reads the same sensor for fan0 and fan1 */
39 		.ident = "Asus M5A78L",
40 		.matches = {
41 			DMI_MATCH(DMI_BOARD_NAME, "M5A78L")
42 		}
43 	},
44 	{ }
45 };
46 
47 /*
48  * Minimum time between readings, enforced in order to avoid
49  * hogging the CPU.
50  */
51 #define CACHE_TIME		HZ
52 
53 #define BOARD_ID		"MBIF"
54 #define METHOD_ENUMERATE	"GGRP"
55 #define METHOD_READ		"GITM"
56 #define METHOD_WRITE		"SITM"
57 #define METHOD_OLD_READ_TMP	"RTMP"
58 #define METHOD_OLD_READ_VLT	"RVLT"
59 #define METHOD_OLD_READ_FAN	"RFAN"
60 #define METHOD_OLD_ENUM_TMP	"TSIF"
61 #define METHOD_OLD_ENUM_VLT	"VSIF"
62 #define METHOD_OLD_ENUM_FAN	"FSIF"
63 
64 #define ATK_MUX_HWMON		0x00000006ULL
65 #define ATK_MUX_MGMT		0x00000011ULL
66 
67 #define ATK_CLASS_MASK		0xff000000ULL
68 #define ATK_CLASS_FREQ_CTL	0x03000000ULL
69 #define ATK_CLASS_FAN_CTL	0x04000000ULL
70 #define ATK_CLASS_HWMON		0x06000000ULL
71 #define ATK_CLASS_MGMT		0x11000000ULL
72 
73 #define ATK_TYPE_MASK		0x00ff0000ULL
74 #define HWMON_TYPE_VOLT		0x00020000ULL
75 #define HWMON_TYPE_TEMP		0x00030000ULL
76 #define HWMON_TYPE_FAN		0x00040000ULL
77 
78 #define ATK_ELEMENT_ID_MASK	0x0000ffffULL
79 
80 #define ATK_EC_ID		0x11060004ULL
81 
82 enum atk_pack_member {
83 	HWMON_PACK_FLAGS,
84 	HWMON_PACK_NAME,
85 	HWMON_PACK_LIMIT1,
86 	HWMON_PACK_LIMIT2,
87 	HWMON_PACK_ENABLE
88 };
89 
90 /* New package format */
91 #define _HWMON_NEW_PACK_SIZE	7
92 #define _HWMON_NEW_PACK_FLAGS	0
93 #define _HWMON_NEW_PACK_NAME	1
94 #define _HWMON_NEW_PACK_UNK1	2
95 #define _HWMON_NEW_PACK_UNK2	3
96 #define _HWMON_NEW_PACK_LIMIT1	4
97 #define _HWMON_NEW_PACK_LIMIT2	5
98 #define _HWMON_NEW_PACK_ENABLE	6
99 
100 /* Old package format */
101 #define _HWMON_OLD_PACK_SIZE	5
102 #define _HWMON_OLD_PACK_FLAGS	0
103 #define _HWMON_OLD_PACK_NAME	1
104 #define _HWMON_OLD_PACK_LIMIT1	2
105 #define _HWMON_OLD_PACK_LIMIT2	3
106 #define _HWMON_OLD_PACK_ENABLE	4
107 
108 
109 struct atk_data {
110 	struct device *hwmon_dev;
111 	acpi_handle atk_handle;
112 	struct acpi_device *acpi_dev;
113 
114 	bool old_interface;
115 
116 	/* old interface */
117 	acpi_handle rtmp_handle;
118 	acpi_handle rvlt_handle;
119 	acpi_handle rfan_handle;
120 	/* new inteface */
121 	acpi_handle enumerate_handle;
122 	acpi_handle read_handle;
123 	acpi_handle write_handle;
124 
125 	bool disable_ec;
126 
127 	int voltage_count;
128 	int temperature_count;
129 	int fan_count;
130 	struct list_head sensor_list;
131 
132 	struct {
133 		struct dentry *root;
134 		u32 id;
135 	} debugfs;
136 };
137 
138 
139 typedef ssize_t (*sysfs_show_func)(struct device *dev,
140 			struct device_attribute *attr, char *buf);
141 
142 static const struct acpi_device_id atk_ids[] = {
143 	{ATK_HID, 0},
144 	{"", 0},
145 };
146 MODULE_DEVICE_TABLE(acpi, atk_ids);
147 
148 #define ATTR_NAME_SIZE 16 /* Worst case is "tempN_input" */
149 
150 struct atk_sensor_data {
151 	struct list_head list;
152 	struct atk_data *data;
153 	struct device_attribute label_attr;
154 	struct device_attribute input_attr;
155 	struct device_attribute limit1_attr;
156 	struct device_attribute limit2_attr;
157 	char label_attr_name[ATTR_NAME_SIZE];
158 	char input_attr_name[ATTR_NAME_SIZE];
159 	char limit1_attr_name[ATTR_NAME_SIZE];
160 	char limit2_attr_name[ATTR_NAME_SIZE];
161 	u64 id;
162 	u64 type;
163 	u64 limit1;
164 	u64 limit2;
165 	u64 cached_value;
166 	unsigned long last_updated; /* in jiffies */
167 	bool is_valid;
168 	char const *acpi_name;
169 };
170 
171 /*
172  * Return buffer format:
173  * [0-3] "value" is valid flag
174  * [4-7] value
175  * [8- ] unknown stuff on newer mobos
176  */
177 struct atk_acpi_ret_buffer {
178 	u32 flags;
179 	u32 value;
180 	u8 data[];
181 };
182 
183 /* Input buffer used for GITM and SITM methods */
184 struct atk_acpi_input_buf {
185 	u32 id;
186 	u32 param1;
187 	u32 param2;
188 };
189 
190 static int atk_add(struct acpi_device *device);
191 static int atk_remove(struct acpi_device *device, int type);
192 static void atk_print_sensor(struct atk_data *data, union acpi_object *obj);
193 static int atk_read_value(struct atk_sensor_data *sensor, u64 *value);
194 static void atk_free_sensors(struct atk_data *data);
195 
196 static struct acpi_driver atk_driver = {
197 	.name	= ATK_HID,
198 	.class	= "hwmon",
199 	.ids	= atk_ids,
200 	.ops	= {
201 		.add	= atk_add,
202 		.remove	= atk_remove,
203 	},
204 };
205 
206 #define input_to_atk_sensor(attr) \
207 	container_of(attr, struct atk_sensor_data, input_attr)
208 
209 #define label_to_atk_sensor(attr) \
210 	container_of(attr, struct atk_sensor_data, label_attr)
211 
212 #define limit1_to_atk_sensor(attr) \
213 	container_of(attr, struct atk_sensor_data, limit1_attr)
214 
215 #define limit2_to_atk_sensor(attr) \
216 	container_of(attr, struct atk_sensor_data, limit2_attr)
217 
atk_input_show(struct device * dev,struct device_attribute * attr,char * buf)218 static ssize_t atk_input_show(struct device *dev,
219 		struct device_attribute *attr, char *buf)
220 {
221 	struct atk_sensor_data *s = input_to_atk_sensor(attr);
222 	u64 value;
223 	int err;
224 
225 	err = atk_read_value(s, &value);
226 	if (err)
227 		return err;
228 
229 	if (s->type == HWMON_TYPE_TEMP)
230 		/* ACPI returns decidegree */
231 		value *= 100;
232 
233 	return sprintf(buf, "%llu\n", value);
234 }
235 
atk_label_show(struct device * dev,struct device_attribute * attr,char * buf)236 static ssize_t atk_label_show(struct device *dev,
237 		struct device_attribute *attr, char *buf)
238 {
239 	struct atk_sensor_data *s = label_to_atk_sensor(attr);
240 
241 	return sprintf(buf, "%s\n", s->acpi_name);
242 }
243 
atk_limit1_show(struct device * dev,struct device_attribute * attr,char * buf)244 static ssize_t atk_limit1_show(struct device *dev,
245 		struct device_attribute *attr, char *buf)
246 {
247 	struct atk_sensor_data *s = limit1_to_atk_sensor(attr);
248 	u64 value = s->limit1;
249 
250 	if (s->type == HWMON_TYPE_TEMP)
251 		value *= 100;
252 
253 	return sprintf(buf, "%lld\n", value);
254 }
255 
atk_limit2_show(struct device * dev,struct device_attribute * attr,char * buf)256 static ssize_t atk_limit2_show(struct device *dev,
257 		struct device_attribute *attr, char *buf)
258 {
259 	struct atk_sensor_data *s = limit2_to_atk_sensor(attr);
260 	u64 value = s->limit2;
261 
262 	if (s->type == HWMON_TYPE_TEMP)
263 		value *= 100;
264 
265 	return sprintf(buf, "%lld\n", value);
266 }
267 
atk_name_show(struct device * dev,struct device_attribute * attr,char * buf)268 static ssize_t atk_name_show(struct device *dev,
269 				struct device_attribute *attr, char *buf)
270 {
271 	return sprintf(buf, "atk0110\n");
272 }
273 static struct device_attribute atk_name_attr =
274 		__ATTR(name, 0444, atk_name_show, NULL);
275 
atk_init_attribute(struct device_attribute * attr,char * name,sysfs_show_func show)276 static void atk_init_attribute(struct device_attribute *attr, char *name,
277 		sysfs_show_func show)
278 {
279 	sysfs_attr_init(&attr->attr);
280 	attr->attr.name = name;
281 	attr->attr.mode = 0444;
282 	attr->show = show;
283 	attr->store = NULL;
284 }
285 
286 
atk_get_pack_member(struct atk_data * data,union acpi_object * pack,enum atk_pack_member m)287 static union acpi_object *atk_get_pack_member(struct atk_data *data,
288 						union acpi_object *pack,
289 						enum atk_pack_member m)
290 {
291 	bool old_if = data->old_interface;
292 	int offset;
293 
294 	switch (m) {
295 	case HWMON_PACK_FLAGS:
296 		offset = old_if ? _HWMON_OLD_PACK_FLAGS : _HWMON_NEW_PACK_FLAGS;
297 		break;
298 	case HWMON_PACK_NAME:
299 		offset = old_if ? _HWMON_OLD_PACK_NAME : _HWMON_NEW_PACK_NAME;
300 		break;
301 	case HWMON_PACK_LIMIT1:
302 		offset = old_if ? _HWMON_OLD_PACK_LIMIT1 :
303 				  _HWMON_NEW_PACK_LIMIT1;
304 		break;
305 	case HWMON_PACK_LIMIT2:
306 		offset = old_if ? _HWMON_OLD_PACK_LIMIT2 :
307 				  _HWMON_NEW_PACK_LIMIT2;
308 		break;
309 	case HWMON_PACK_ENABLE:
310 		offset = old_if ? _HWMON_OLD_PACK_ENABLE :
311 				  _HWMON_NEW_PACK_ENABLE;
312 		break;
313 	default:
314 		return NULL;
315 	}
316 
317 	return &pack->package.elements[offset];
318 }
319 
320 
321 /*
322  * New package format is:
323  * - flag (int)
324  *	class - used for de-muxing the request to the correct GITn
325  *	type (volt, temp, fan)
326  *	sensor id |
327  *	sensor id - used for de-muxing the request _inside_ the GITn
328  * - name (str)
329  * - unknown (int)
330  * - unknown (int)
331  * - limit1 (int)
332  * - limit2 (int)
333  * - enable (int)
334  *
335  * The old package has the same format but it's missing the two unknown fields.
336  */
validate_hwmon_pack(struct atk_data * data,union acpi_object * obj)337 static int validate_hwmon_pack(struct atk_data *data, union acpi_object *obj)
338 {
339 	struct device *dev = &data->acpi_dev->dev;
340 	union acpi_object *tmp;
341 	bool old_if = data->old_interface;
342 	int const expected_size = old_if ? _HWMON_OLD_PACK_SIZE :
343 					   _HWMON_NEW_PACK_SIZE;
344 
345 	if (obj->type != ACPI_TYPE_PACKAGE) {
346 		dev_warn(dev, "Invalid type: %d\n", obj->type);
347 		return -EINVAL;
348 	}
349 
350 	if (obj->package.count != expected_size) {
351 		dev_warn(dev, "Invalid package size: %d, expected: %d\n",
352 				obj->package.count, expected_size);
353 		return -EINVAL;
354 	}
355 
356 	tmp = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS);
357 	if (tmp->type != ACPI_TYPE_INTEGER) {
358 		dev_warn(dev, "Invalid type (flag): %d\n", tmp->type);
359 		return -EINVAL;
360 	}
361 
362 	tmp = atk_get_pack_member(data, obj, HWMON_PACK_NAME);
363 	if (tmp->type != ACPI_TYPE_STRING) {
364 		dev_warn(dev, "Invalid type (name): %d\n", tmp->type);
365 		return -EINVAL;
366 	}
367 
368 	/* Don't check... we don't know what they're useful for anyway */
369 #if 0
370 	tmp = &obj->package.elements[HWMON_PACK_UNK1];
371 	if (tmp->type != ACPI_TYPE_INTEGER) {
372 		dev_warn(dev, "Invalid type (unk1): %d\n", tmp->type);
373 		return -EINVAL;
374 	}
375 
376 	tmp = &obj->package.elements[HWMON_PACK_UNK2];
377 	if (tmp->type != ACPI_TYPE_INTEGER) {
378 		dev_warn(dev, "Invalid type (unk2): %d\n", tmp->type);
379 		return -EINVAL;
380 	}
381 #endif
382 
383 	tmp = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT1);
384 	if (tmp->type != ACPI_TYPE_INTEGER) {
385 		dev_warn(dev, "Invalid type (limit1): %d\n", tmp->type);
386 		return -EINVAL;
387 	}
388 
389 	tmp = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT2);
390 	if (tmp->type != ACPI_TYPE_INTEGER) {
391 		dev_warn(dev, "Invalid type (limit2): %d\n", tmp->type);
392 		return -EINVAL;
393 	}
394 
395 	tmp = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE);
396 	if (tmp->type != ACPI_TYPE_INTEGER) {
397 		dev_warn(dev, "Invalid type (enable): %d\n", tmp->type);
398 		return -EINVAL;
399 	}
400 
401 	atk_print_sensor(data, obj);
402 
403 	return 0;
404 }
405 
406 #ifdef DEBUG
atk_sensor_type(union acpi_object * flags)407 static char const *atk_sensor_type(union acpi_object *flags)
408 {
409 	u64 type = flags->integer.value & ATK_TYPE_MASK;
410 	char const *what;
411 
412 	switch (type) {
413 	case HWMON_TYPE_VOLT:
414 		what = "voltage";
415 		break;
416 	case HWMON_TYPE_TEMP:
417 		what = "temperature";
418 		break;
419 	case HWMON_TYPE_FAN:
420 		what = "fan";
421 		break;
422 	default:
423 		what = "unknown";
424 		break;
425 	}
426 
427 	return what;
428 }
429 #endif
430 
atk_print_sensor(struct atk_data * data,union acpi_object * obj)431 static void atk_print_sensor(struct atk_data *data, union acpi_object *obj)
432 {
433 #ifdef DEBUG
434 	struct device *dev = &data->acpi_dev->dev;
435 	union acpi_object *flags;
436 	union acpi_object *name;
437 	union acpi_object *limit1;
438 	union acpi_object *limit2;
439 	union acpi_object *enable;
440 	char const *what;
441 
442 	flags = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS);
443 	name = atk_get_pack_member(data, obj, HWMON_PACK_NAME);
444 	limit1 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT1);
445 	limit2 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT2);
446 	enable = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE);
447 
448 	what = atk_sensor_type(flags);
449 
450 	dev_dbg(dev, "%s: %#llx %s [%llu-%llu] %s\n", what,
451 			flags->integer.value,
452 			name->string.pointer,
453 			limit1->integer.value, limit2->integer.value,
454 			enable->integer.value ? "enabled" : "disabled");
455 #endif
456 }
457 
atk_read_value_old(struct atk_sensor_data * sensor,u64 * value)458 static int atk_read_value_old(struct atk_sensor_data *sensor, u64 *value)
459 {
460 	struct atk_data *data = sensor->data;
461 	struct device *dev = &data->acpi_dev->dev;
462 	struct acpi_object_list params;
463 	union acpi_object id;
464 	acpi_status status;
465 	acpi_handle method;
466 
467 	switch (sensor->type) {
468 	case HWMON_TYPE_VOLT:
469 		method = data->rvlt_handle;
470 		break;
471 	case HWMON_TYPE_TEMP:
472 		method = data->rtmp_handle;
473 		break;
474 	case HWMON_TYPE_FAN:
475 		method = data->rfan_handle;
476 		break;
477 	default:
478 		return -EINVAL;
479 	}
480 
481 	id.type = ACPI_TYPE_INTEGER;
482 	id.integer.value = sensor->id;
483 
484 	params.count = 1;
485 	params.pointer = &id;
486 
487 	status = acpi_evaluate_integer(method, NULL, &params, value);
488 	if (status != AE_OK) {
489 		dev_warn(dev, "%s: ACPI exception: %s\n", __func__,
490 				acpi_format_exception(status));
491 		return -EIO;
492 	}
493 
494 	return 0;
495 }
496 
atk_ggrp(struct atk_data * data,u16 mux)497 static union acpi_object *atk_ggrp(struct atk_data *data, u16 mux)
498 {
499 	struct device *dev = &data->acpi_dev->dev;
500 	struct acpi_buffer buf;
501 	acpi_status ret;
502 	struct acpi_object_list params;
503 	union acpi_object id;
504 	union acpi_object *pack;
505 
506 	id.type = ACPI_TYPE_INTEGER;
507 	id.integer.value = mux;
508 	params.count = 1;
509 	params.pointer = &id;
510 
511 	buf.length = ACPI_ALLOCATE_BUFFER;
512 	ret = acpi_evaluate_object(data->enumerate_handle, NULL, &params, &buf);
513 	if (ret != AE_OK) {
514 		dev_err(dev, "GGRP[%#x] ACPI exception: %s\n", mux,
515 				acpi_format_exception(ret));
516 		return ERR_PTR(-EIO);
517 	}
518 	pack = buf.pointer;
519 	if (pack->type != ACPI_TYPE_PACKAGE) {
520 		/* Execution was successful, but the id was not found */
521 		ACPI_FREE(pack);
522 		return ERR_PTR(-ENOENT);
523 	}
524 
525 	if (pack->package.count < 1) {
526 		dev_err(dev, "GGRP[%#x] package is too small\n", mux);
527 		ACPI_FREE(pack);
528 		return ERR_PTR(-EIO);
529 	}
530 	return pack;
531 }
532 
atk_gitm(struct atk_data * data,u64 id)533 static union acpi_object *atk_gitm(struct atk_data *data, u64 id)
534 {
535 	struct device *dev = &data->acpi_dev->dev;
536 	struct atk_acpi_input_buf buf;
537 	union acpi_object tmp;
538 	struct acpi_object_list params;
539 	struct acpi_buffer ret;
540 	union acpi_object *obj;
541 	acpi_status status;
542 
543 	buf.id = id;
544 	buf.param1 = 0;
545 	buf.param2 = 0;
546 
547 	tmp.type = ACPI_TYPE_BUFFER;
548 	tmp.buffer.pointer = (u8 *)&buf;
549 	tmp.buffer.length = sizeof(buf);
550 
551 	params.count = 1;
552 	params.pointer = (void *)&tmp;
553 
554 	ret.length = ACPI_ALLOCATE_BUFFER;
555 	status = acpi_evaluate_object_typed(data->read_handle, NULL, &params,
556 			&ret, ACPI_TYPE_BUFFER);
557 	if (status != AE_OK) {
558 		dev_warn(dev, "GITM[%#llx] ACPI exception: %s\n", id,
559 				acpi_format_exception(status));
560 		return ERR_PTR(-EIO);
561 	}
562 	obj = ret.pointer;
563 
564 	/* Sanity check */
565 	if (obj->buffer.length < 8) {
566 		dev_warn(dev, "Unexpected ASBF length: %u\n",
567 				obj->buffer.length);
568 		ACPI_FREE(obj);
569 		return ERR_PTR(-EIO);
570 	}
571 	return obj;
572 }
573 
atk_sitm(struct atk_data * data,struct atk_acpi_input_buf * buf)574 static union acpi_object *atk_sitm(struct atk_data *data,
575 		struct atk_acpi_input_buf *buf)
576 {
577 	struct device *dev = &data->acpi_dev->dev;
578 	struct acpi_object_list params;
579 	union acpi_object tmp;
580 	struct acpi_buffer ret;
581 	union acpi_object *obj;
582 	acpi_status status;
583 
584 	tmp.type = ACPI_TYPE_BUFFER;
585 	tmp.buffer.pointer = (u8 *)buf;
586 	tmp.buffer.length = sizeof(*buf);
587 
588 	params.count = 1;
589 	params.pointer = &tmp;
590 
591 	ret.length = ACPI_ALLOCATE_BUFFER;
592 	status = acpi_evaluate_object_typed(data->write_handle, NULL, &params,
593 			&ret, ACPI_TYPE_BUFFER);
594 	if (status != AE_OK) {
595 		dev_warn(dev, "SITM[%#x] ACPI exception: %s\n", buf->id,
596 				acpi_format_exception(status));
597 		return ERR_PTR(-EIO);
598 	}
599 	obj = ret.pointer;
600 
601 	/* Sanity check */
602 	if (obj->buffer.length < 8) {
603 		dev_warn(dev, "Unexpected ASBF length: %u\n",
604 				obj->buffer.length);
605 		ACPI_FREE(obj);
606 		return ERR_PTR(-EIO);
607 	}
608 	return obj;
609 }
610 
atk_read_value_new(struct atk_sensor_data * sensor,u64 * value)611 static int atk_read_value_new(struct atk_sensor_data *sensor, u64 *value)
612 {
613 	struct atk_data *data = sensor->data;
614 	struct device *dev = &data->acpi_dev->dev;
615 	union acpi_object *obj;
616 	struct atk_acpi_ret_buffer *buf;
617 	int err = 0;
618 
619 	obj = atk_gitm(data, sensor->id);
620 	if (IS_ERR(obj))
621 		return PTR_ERR(obj);
622 
623 	buf = (struct atk_acpi_ret_buffer *)obj->buffer.pointer;
624 	if (buf->flags == 0) {
625 		/*
626 		 * The reading is not valid, possible causes:
627 		 * - sensor failure
628 		 * - enumeration was FUBAR (and we didn't notice)
629 		 */
630 		dev_warn(dev, "Read failed, sensor = %#llx\n", sensor->id);
631 		err = -EIO;
632 		goto out;
633 	}
634 
635 	*value = buf->value;
636 out:
637 	ACPI_FREE(obj);
638 	return err;
639 }
640 
atk_read_value(struct atk_sensor_data * sensor,u64 * value)641 static int atk_read_value(struct atk_sensor_data *sensor, u64 *value)
642 {
643 	int err;
644 
645 	if (!sensor->is_valid ||
646 	    time_after(jiffies, sensor->last_updated + CACHE_TIME)) {
647 		if (sensor->data->old_interface)
648 			err = atk_read_value_old(sensor, value);
649 		else
650 			err = atk_read_value_new(sensor, value);
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
atk_debugfs_gitm_get(void * p,u64 * val)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 
atk_acpi_print(char * buf,size_t sz,union acpi_object * obj)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 
atk_pack_print(char * buf,size_t sz,union acpi_object * pack)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 
atk_debugfs_ggrp_open(struct inode * inode,struct file * file)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 
atk_debugfs_ggrp_read(struct file * file,char __user * buf,size_t count,loff_t * pos)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 
atk_debugfs_ggrp_release(struct inode * inode,struct file * file)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 
atk_debugfs_init(struct atk_data * data)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 
atk_debugfs_cleanup(struct atk_data * data)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 
atk_debugfs_init(struct atk_data * data)837 static void atk_debugfs_init(struct atk_data *data)
838 {
839 }
840 
atk_debugfs_cleanup(struct atk_data * data)841 static void atk_debugfs_cleanup(struct atk_data *data)
842 {
843 }
844 #endif
845 
atk_add_sensor(struct atk_data * data,union acpi_object * obj)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->acpi_name);
966 	kfree(sensor);
967 	return err;
968 }
969 
atk_enumerate_old_hwmon(struct atk_data * data)970 static int atk_enumerate_old_hwmon(struct atk_data *data)
971 {
972 	struct device *dev = &data->acpi_dev->dev;
973 	struct acpi_buffer buf;
974 	union acpi_object *pack;
975 	acpi_status status;
976 	int i, ret;
977 	int count = 0;
978 
979 	/* Voltages */
980 	buf.length = ACPI_ALLOCATE_BUFFER;
981 	status = acpi_evaluate_object_typed(data->atk_handle,
982 			METHOD_OLD_ENUM_VLT, NULL, &buf, ACPI_TYPE_PACKAGE);
983 	if (status != AE_OK) {
984 		dev_warn(dev, METHOD_OLD_ENUM_VLT ": ACPI exception: %s\n",
985 				acpi_format_exception(status));
986 
987 		return -ENODEV;
988 	}
989 
990 	pack = buf.pointer;
991 	for (i = 1; i < pack->package.count; i++) {
992 		union acpi_object *obj = &pack->package.elements[i];
993 
994 		ret = atk_add_sensor(data, obj);
995 		if (ret > 0)
996 			count++;
997 	}
998 	ACPI_FREE(buf.pointer);
999 
1000 	/* Temperatures */
1001 	buf.length = ACPI_ALLOCATE_BUFFER;
1002 	status = acpi_evaluate_object_typed(data->atk_handle,
1003 			METHOD_OLD_ENUM_TMP, NULL, &buf, ACPI_TYPE_PACKAGE);
1004 	if (status != AE_OK) {
1005 		dev_warn(dev, METHOD_OLD_ENUM_TMP ": ACPI exception: %s\n",
1006 				acpi_format_exception(status));
1007 
1008 		ret = -ENODEV;
1009 		goto cleanup;
1010 	}
1011 
1012 	pack = buf.pointer;
1013 	for (i = 1; i < pack->package.count; i++) {
1014 		union acpi_object *obj = &pack->package.elements[i];
1015 
1016 		ret = atk_add_sensor(data, obj);
1017 		if (ret > 0)
1018 			count++;
1019 	}
1020 	ACPI_FREE(buf.pointer);
1021 
1022 	/* Fans */
1023 	buf.length = ACPI_ALLOCATE_BUFFER;
1024 	status = acpi_evaluate_object_typed(data->atk_handle,
1025 			METHOD_OLD_ENUM_FAN, NULL, &buf, ACPI_TYPE_PACKAGE);
1026 	if (status != AE_OK) {
1027 		dev_warn(dev, METHOD_OLD_ENUM_FAN ": ACPI exception: %s\n",
1028 				acpi_format_exception(status));
1029 
1030 		ret = -ENODEV;
1031 		goto cleanup;
1032 	}
1033 
1034 	pack = buf.pointer;
1035 	for (i = 1; i < pack->package.count; i++) {
1036 		union acpi_object *obj = &pack->package.elements[i];
1037 
1038 		ret = atk_add_sensor(data, obj);
1039 		if (ret > 0)
1040 			count++;
1041 	}
1042 	ACPI_FREE(buf.pointer);
1043 
1044 	return count;
1045 cleanup:
1046 	atk_free_sensors(data);
1047 	return ret;
1048 }
1049 
atk_ec_present(struct atk_data * data)1050 static int atk_ec_present(struct atk_data *data)
1051 {
1052 	struct device *dev = &data->acpi_dev->dev;
1053 	union acpi_object *pack;
1054 	union acpi_object *ec;
1055 	int ret;
1056 	int i;
1057 
1058 	pack = atk_ggrp(data, ATK_MUX_MGMT);
1059 	if (IS_ERR(pack)) {
1060 		if (PTR_ERR(pack) == -ENOENT) {
1061 			/* The MGMT class does not exists - that's ok */
1062 			dev_dbg(dev, "Class %#llx not found\n", ATK_MUX_MGMT);
1063 			return 0;
1064 		}
1065 		return PTR_ERR(pack);
1066 	}
1067 
1068 	/* Search the EC */
1069 	ec = NULL;
1070 	for (i = 0; i < pack->package.count; i++) {
1071 		union acpi_object *obj = &pack->package.elements[i];
1072 		union acpi_object *id;
1073 
1074 		if (obj->type != ACPI_TYPE_PACKAGE)
1075 			continue;
1076 
1077 		id = &obj->package.elements[0];
1078 		if (id->type != ACPI_TYPE_INTEGER)
1079 			continue;
1080 
1081 		if (id->integer.value == ATK_EC_ID) {
1082 			ec = obj;
1083 			break;
1084 		}
1085 	}
1086 
1087 	ret = (ec != NULL);
1088 	if (!ret)
1089 		/* The system has no EC */
1090 		dev_dbg(dev, "EC not found\n");
1091 
1092 	ACPI_FREE(pack);
1093 	return ret;
1094 }
1095 
atk_ec_enabled(struct atk_data * data)1096 static int atk_ec_enabled(struct atk_data *data)
1097 {
1098 	struct device *dev = &data->acpi_dev->dev;
1099 	union acpi_object *obj;
1100 	struct atk_acpi_ret_buffer *buf;
1101 	int err;
1102 
1103 	obj = atk_gitm(data, ATK_EC_ID);
1104 	if (IS_ERR(obj)) {
1105 		dev_err(dev, "Unable to query EC status\n");
1106 		return PTR_ERR(obj);
1107 	}
1108 	buf = (struct atk_acpi_ret_buffer *)obj->buffer.pointer;
1109 
1110 	if (buf->flags == 0) {
1111 		dev_err(dev, "Unable to query EC status\n");
1112 		err = -EIO;
1113 	} else {
1114 		err = (buf->value != 0);
1115 		dev_dbg(dev, "EC is %sabled\n",
1116 				err ? "en" : "dis");
1117 	}
1118 
1119 	ACPI_FREE(obj);
1120 	return err;
1121 }
1122 
atk_ec_ctl(struct atk_data * data,int enable)1123 static int atk_ec_ctl(struct atk_data *data, int enable)
1124 {
1125 	struct device *dev = &data->acpi_dev->dev;
1126 	union acpi_object *obj;
1127 	struct atk_acpi_input_buf sitm;
1128 	struct atk_acpi_ret_buffer *ec_ret;
1129 	int err = 0;
1130 
1131 	sitm.id = ATK_EC_ID;
1132 	sitm.param1 = enable;
1133 	sitm.param2 = 0;
1134 
1135 	obj = atk_sitm(data, &sitm);
1136 	if (IS_ERR(obj)) {
1137 		dev_err(dev, "Failed to %sable the EC\n",
1138 				enable ? "en" : "dis");
1139 		return PTR_ERR(obj);
1140 	}
1141 	ec_ret = (struct atk_acpi_ret_buffer *)obj->buffer.pointer;
1142 	if (ec_ret->flags == 0) {
1143 		dev_err(dev, "Failed to %sable the EC\n",
1144 				enable ? "en" : "dis");
1145 		err = -EIO;
1146 	} else {
1147 		dev_info(dev, "EC %sabled\n",
1148 				enable ? "en" : "dis");
1149 	}
1150 
1151 	ACPI_FREE(obj);
1152 	return err;
1153 }
1154 
atk_enumerate_new_hwmon(struct atk_data * data)1155 static int atk_enumerate_new_hwmon(struct atk_data *data)
1156 {
1157 	struct device *dev = &data->acpi_dev->dev;
1158 	union acpi_object *pack;
1159 	int err;
1160 	int i;
1161 
1162 	err = atk_ec_present(data);
1163 	if (err < 0)
1164 		return err;
1165 	if (err) {
1166 		err = atk_ec_enabled(data);
1167 		if (err < 0)
1168 			return err;
1169 		/* If the EC was disabled we will disable it again on unload */
1170 		data->disable_ec = err;
1171 
1172 		err = atk_ec_ctl(data, 1);
1173 		if (err) {
1174 			data->disable_ec = false;
1175 			return err;
1176 		}
1177 	}
1178 
1179 	dev_dbg(dev, "Enumerating hwmon sensors\n");
1180 
1181 	pack = atk_ggrp(data, ATK_MUX_HWMON);
1182 	if (IS_ERR(pack))
1183 		return PTR_ERR(pack);
1184 
1185 	for (i = 0; i < pack->package.count; i++) {
1186 		union acpi_object *obj = &pack->package.elements[i];
1187 
1188 		atk_add_sensor(data, obj);
1189 	}
1190 
1191 	err = data->voltage_count + data->temperature_count + data->fan_count;
1192 
1193 	ACPI_FREE(pack);
1194 	return err;
1195 }
1196 
atk_create_files(struct atk_data * data)1197 static int atk_create_files(struct atk_data *data)
1198 {
1199 	struct atk_sensor_data *s;
1200 	int err;
1201 
1202 	list_for_each_entry(s, &data->sensor_list, list) {
1203 		err = device_create_file(data->hwmon_dev, &s->input_attr);
1204 		if (err)
1205 			return err;
1206 		err = device_create_file(data->hwmon_dev, &s->label_attr);
1207 		if (err)
1208 			return err;
1209 		err = device_create_file(data->hwmon_dev, &s->limit1_attr);
1210 		if (err)
1211 			return err;
1212 		err = device_create_file(data->hwmon_dev, &s->limit2_attr);
1213 		if (err)
1214 			return err;
1215 	}
1216 
1217 	err = device_create_file(data->hwmon_dev, &atk_name_attr);
1218 
1219 	return err;
1220 }
1221 
atk_remove_files(struct atk_data * data)1222 static void atk_remove_files(struct atk_data *data)
1223 {
1224 	struct atk_sensor_data *s;
1225 
1226 	list_for_each_entry(s, &data->sensor_list, list) {
1227 		device_remove_file(data->hwmon_dev, &s->input_attr);
1228 		device_remove_file(data->hwmon_dev, &s->label_attr);
1229 		device_remove_file(data->hwmon_dev, &s->limit1_attr);
1230 		device_remove_file(data->hwmon_dev, &s->limit2_attr);
1231 	}
1232 	device_remove_file(data->hwmon_dev, &atk_name_attr);
1233 }
1234 
atk_free_sensors(struct atk_data * data)1235 static void atk_free_sensors(struct atk_data *data)
1236 {
1237 	struct list_head *head = &data->sensor_list;
1238 	struct atk_sensor_data *s, *tmp;
1239 
1240 	list_for_each_entry_safe(s, tmp, head, list) {
1241 		kfree(s->acpi_name);
1242 		kfree(s);
1243 	}
1244 }
1245 
atk_register_hwmon(struct atk_data * data)1246 static int atk_register_hwmon(struct atk_data *data)
1247 {
1248 	struct device *dev = &data->acpi_dev->dev;
1249 	int err;
1250 
1251 	dev_dbg(dev, "registering hwmon device\n");
1252 	data->hwmon_dev = hwmon_device_register(dev);
1253 	if (IS_ERR(data->hwmon_dev))
1254 		return PTR_ERR(data->hwmon_dev);
1255 
1256 	dev_dbg(dev, "populating sysfs directory\n");
1257 	err = atk_create_files(data);
1258 	if (err)
1259 		goto remove;
1260 
1261 	return 0;
1262 remove:
1263 	/* Cleanup the registered files */
1264 	atk_remove_files(data);
1265 	hwmon_device_unregister(data->hwmon_dev);
1266 	return err;
1267 }
1268 
atk_probe_if(struct atk_data * data)1269 static int atk_probe_if(struct atk_data *data)
1270 {
1271 	struct device *dev = &data->acpi_dev->dev;
1272 	acpi_handle ret;
1273 	acpi_status status;
1274 	int err = 0;
1275 
1276 	/* RTMP: read temperature */
1277 	status = acpi_get_handle(data->atk_handle, METHOD_OLD_READ_TMP, &ret);
1278 	if (ACPI_SUCCESS(status))
1279 		data->rtmp_handle = ret;
1280 	else
1281 		dev_dbg(dev, "method " METHOD_OLD_READ_TMP " not found: %s\n",
1282 				acpi_format_exception(status));
1283 
1284 	/* RVLT: read voltage */
1285 	status = acpi_get_handle(data->atk_handle, METHOD_OLD_READ_VLT, &ret);
1286 	if (ACPI_SUCCESS(status))
1287 		data->rvlt_handle = ret;
1288 	else
1289 		dev_dbg(dev, "method " METHOD_OLD_READ_VLT " not found: %s\n",
1290 				acpi_format_exception(status));
1291 
1292 	/* RFAN: read fan status */
1293 	status = acpi_get_handle(data->atk_handle, METHOD_OLD_READ_FAN, &ret);
1294 	if (ACPI_SUCCESS(status))
1295 		data->rfan_handle = ret;
1296 	else
1297 		dev_dbg(dev, "method " METHOD_OLD_READ_FAN " not found: %s\n",
1298 				acpi_format_exception(status));
1299 
1300 	/* Enumeration */
1301 	status = acpi_get_handle(data->atk_handle, METHOD_ENUMERATE, &ret);
1302 	if (ACPI_SUCCESS(status))
1303 		data->enumerate_handle = ret;
1304 	else
1305 		dev_dbg(dev, "method " METHOD_ENUMERATE " not found: %s\n",
1306 				acpi_format_exception(status));
1307 
1308 	/* De-multiplexer (read) */
1309 	status = acpi_get_handle(data->atk_handle, METHOD_READ, &ret);
1310 	if (ACPI_SUCCESS(status))
1311 		data->read_handle = ret;
1312 	else
1313 		dev_dbg(dev, "method " METHOD_READ " not found: %s\n",
1314 				acpi_format_exception(status));
1315 
1316 	/* De-multiplexer (write) */
1317 	status = acpi_get_handle(data->atk_handle, METHOD_WRITE, &ret);
1318 	if (ACPI_SUCCESS(status))
1319 		data->write_handle = ret;
1320 	else
1321 		dev_dbg(dev, "method " METHOD_WRITE " not found: %s\n",
1322 				 acpi_format_exception(status));
1323 
1324 	/*
1325 	 * Check for hwmon methods: first check "old" style methods; note that
1326 	 * both may be present: in this case we stick to the old interface;
1327 	 * analysis of multiple DSDTs indicates that when both interfaces
1328 	 * are present the new one (GGRP/GITM) is not functional.
1329 	 */
1330 	if (new_if)
1331 		dev_info(dev, "Overriding interface detection\n");
1332 	if (data->rtmp_handle &&
1333 			data->rvlt_handle && data->rfan_handle && !new_if)
1334 		data->old_interface = true;
1335 	else if (data->enumerate_handle && data->read_handle &&
1336 			data->write_handle)
1337 		data->old_interface = false;
1338 	else
1339 		err = -ENODEV;
1340 
1341 	return err;
1342 }
1343 
atk_add(struct acpi_device * device)1344 static int atk_add(struct acpi_device *device)
1345 {
1346 	acpi_status ret;
1347 	int err;
1348 	struct acpi_buffer buf;
1349 	union acpi_object *obj;
1350 	struct atk_data *data;
1351 
1352 	dev_dbg(&device->dev, "adding...\n");
1353 
1354 	data = kzalloc(sizeof(*data), GFP_KERNEL);
1355 	if (!data)
1356 		return -ENOMEM;
1357 
1358 	data->acpi_dev = device;
1359 	data->atk_handle = device->handle;
1360 	INIT_LIST_HEAD(&data->sensor_list);
1361 	data->disable_ec = false;
1362 
1363 	buf.length = ACPI_ALLOCATE_BUFFER;
1364 	ret = acpi_evaluate_object_typed(data->atk_handle, BOARD_ID, NULL,
1365 			&buf, ACPI_TYPE_PACKAGE);
1366 	if (ret != AE_OK) {
1367 		dev_dbg(&device->dev, "atk: method MBIF not found\n");
1368 	} else {
1369 		obj = buf.pointer;
1370 		if (obj->package.count >= 2) {
1371 			union acpi_object *id = &obj->package.elements[1];
1372 			if (id->type == ACPI_TYPE_STRING)
1373 				dev_dbg(&device->dev, "board ID = %s\n",
1374 					id->string.pointer);
1375 		}
1376 		ACPI_FREE(buf.pointer);
1377 	}
1378 
1379 	err = atk_probe_if(data);
1380 	if (err) {
1381 		dev_err(&device->dev, "No usable hwmon interface detected\n");
1382 		goto out;
1383 	}
1384 
1385 	if (data->old_interface) {
1386 		dev_dbg(&device->dev, "Using old hwmon interface\n");
1387 		err = atk_enumerate_old_hwmon(data);
1388 	} else {
1389 		dev_dbg(&device->dev, "Using new hwmon interface\n");
1390 		err = atk_enumerate_new_hwmon(data);
1391 	}
1392 	if (err < 0)
1393 		goto out;
1394 	if (err == 0) {
1395 		dev_info(&device->dev,
1396 			 "No usable sensor detected, bailing out\n");
1397 		err = -ENODEV;
1398 		goto out;
1399 	}
1400 
1401 	err = atk_register_hwmon(data);
1402 	if (err)
1403 		goto cleanup;
1404 
1405 	atk_debugfs_init(data);
1406 
1407 	device->driver_data = data;
1408 	return 0;
1409 cleanup:
1410 	atk_free_sensors(data);
1411 out:
1412 	if (data->disable_ec)
1413 		atk_ec_ctl(data, 0);
1414 	kfree(data);
1415 	return err;
1416 }
1417 
atk_remove(struct acpi_device * device,int type)1418 static int atk_remove(struct acpi_device *device, int type)
1419 {
1420 	struct atk_data *data = device->driver_data;
1421 	dev_dbg(&device->dev, "removing...\n");
1422 
1423 	device->driver_data = NULL;
1424 
1425 	atk_debugfs_cleanup(data);
1426 
1427 	atk_remove_files(data);
1428 	atk_free_sensors(data);
1429 	hwmon_device_unregister(data->hwmon_dev);
1430 
1431 	if (data->disable_ec) {
1432 		if (atk_ec_ctl(data, 0))
1433 			dev_err(&device->dev, "Failed to disable EC\n");
1434 	}
1435 
1436 	kfree(data);
1437 
1438 	return 0;
1439 }
1440 
atk0110_init(void)1441 static int __init atk0110_init(void)
1442 {
1443 	int ret;
1444 
1445 	/* Make sure it's safe to access the device through ACPI */
1446 	if (!acpi_resources_are_enforced()) {
1447 		pr_err("Resources not safely usable due to acpi_enforce_resources kernel parameter\n");
1448 		return -EBUSY;
1449 	}
1450 
1451 	if (dmi_check_system(atk_force_new_if))
1452 		new_if = true;
1453 
1454 	ret = acpi_bus_register_driver(&atk_driver);
1455 	if (ret)
1456 		pr_info("acpi_bus_register_driver failed: %d\n", ret);
1457 
1458 	return ret;
1459 }
1460 
atk0110_exit(void)1461 static void __exit atk0110_exit(void)
1462 {
1463 	acpi_bus_unregister_driver(&atk_driver);
1464 }
1465 
1466 module_init(atk0110_init);
1467 module_exit(atk0110_exit);
1468 
1469 MODULE_LICENSE("GPL");
1470