Linux 3.12.39
[linux/fpc-iii.git] / drivers / hwmon / asus_atk0110.c
blobb25c64302cbc1911f5d272e50dc05f8477b326c9
1 /*
2 * Copyright (C) 2007-2009 Luca Tettamanti <kronos.it@gmail.com>
4 * This file is released under the GPLv2
5 * See COPYING in the top level directory of the kernel tree.
6 */
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
20 #include <acpi/acpi.h>
21 #include <acpi/acpixf.h>
22 #include <acpi/acpi_drivers.h>
23 #include <acpi/acpi_bus.h>
26 #define ATK_HID "ATK0110"
28 static bool new_if;
29 module_param(new_if, bool, 0);
30 MODULE_PARM_DESC(new_if, "Override detection heuristic and force the use of the new ATK0110 interface");
32 static const struct dmi_system_id __initconst atk_force_new_if[] = {
34 /* Old interface has broken MCH temp monitoring */
35 .ident = "Asus Sabertooth X58",
36 .matches = {
37 DMI_MATCH(DMI_BOARD_NAME, "SABERTOOTH X58")
39 }, {
40 /* Old interface reads the same sensor for fan0 and fan1 */
41 .ident = "Asus M5A78L",
42 .matches = {
43 DMI_MATCH(DMI_BOARD_NAME, "M5A78L")
46 { }
50 * Minimum time between readings, enforced in order to avoid
51 * hogging the CPU.
53 #define CACHE_TIME HZ
55 #define BOARD_ID "MBIF"
56 #define METHOD_ENUMERATE "GGRP"
57 #define METHOD_READ "GITM"
58 #define METHOD_WRITE "SITM"
59 #define METHOD_OLD_READ_TMP "RTMP"
60 #define METHOD_OLD_READ_VLT "RVLT"
61 #define METHOD_OLD_READ_FAN "RFAN"
62 #define METHOD_OLD_ENUM_TMP "TSIF"
63 #define METHOD_OLD_ENUM_VLT "VSIF"
64 #define METHOD_OLD_ENUM_FAN "FSIF"
66 #define ATK_MUX_HWMON 0x00000006ULL
67 #define ATK_MUX_MGMT 0x00000011ULL
69 #define ATK_CLASS_MASK 0xff000000ULL
70 #define ATK_CLASS_FREQ_CTL 0x03000000ULL
71 #define ATK_CLASS_FAN_CTL 0x04000000ULL
72 #define ATK_CLASS_HWMON 0x06000000ULL
73 #define ATK_CLASS_MGMT 0x11000000ULL
75 #define ATK_TYPE_MASK 0x00ff0000ULL
76 #define HWMON_TYPE_VOLT 0x00020000ULL
77 #define HWMON_TYPE_TEMP 0x00030000ULL
78 #define HWMON_TYPE_FAN 0x00040000ULL
80 #define ATK_ELEMENT_ID_MASK 0x0000ffffULL
82 #define ATK_EC_ID 0x11060004ULL
84 enum atk_pack_member {
85 HWMON_PACK_FLAGS,
86 HWMON_PACK_NAME,
87 HWMON_PACK_LIMIT1,
88 HWMON_PACK_LIMIT2,
89 HWMON_PACK_ENABLE
92 /* New package format */
93 #define _HWMON_NEW_PACK_SIZE 7
94 #define _HWMON_NEW_PACK_FLAGS 0
95 #define _HWMON_NEW_PACK_NAME 1
96 #define _HWMON_NEW_PACK_UNK1 2
97 #define _HWMON_NEW_PACK_UNK2 3
98 #define _HWMON_NEW_PACK_LIMIT1 4
99 #define _HWMON_NEW_PACK_LIMIT2 5
100 #define _HWMON_NEW_PACK_ENABLE 6
102 /* Old package format */
103 #define _HWMON_OLD_PACK_SIZE 5
104 #define _HWMON_OLD_PACK_FLAGS 0
105 #define _HWMON_OLD_PACK_NAME 1
106 #define _HWMON_OLD_PACK_LIMIT1 2
107 #define _HWMON_OLD_PACK_LIMIT2 3
108 #define _HWMON_OLD_PACK_ENABLE 4
111 struct atk_data {
112 struct device *hwmon_dev;
113 acpi_handle atk_handle;
114 struct acpi_device *acpi_dev;
116 bool old_interface;
118 /* old interface */
119 acpi_handle rtmp_handle;
120 acpi_handle rvlt_handle;
121 acpi_handle rfan_handle;
122 /* new inteface */
123 acpi_handle enumerate_handle;
124 acpi_handle read_handle;
125 acpi_handle write_handle;
127 bool disable_ec;
129 int voltage_count;
130 int temperature_count;
131 int fan_count;
132 struct list_head sensor_list;
134 struct {
135 struct dentry *root;
136 u32 id;
137 } debugfs;
141 typedef ssize_t (*sysfs_show_func)(struct device *dev,
142 struct device_attribute *attr, char *buf);
144 static const struct acpi_device_id atk_ids[] = {
145 {ATK_HID, 0},
146 {"", 0},
148 MODULE_DEVICE_TABLE(acpi, atk_ids);
150 #define ATTR_NAME_SIZE 16 /* Worst case is "tempN_input" */
152 struct atk_sensor_data {
153 struct list_head list;
154 struct atk_data *data;
155 struct device_attribute label_attr;
156 struct device_attribute input_attr;
157 struct device_attribute limit1_attr;
158 struct device_attribute limit2_attr;
159 char label_attr_name[ATTR_NAME_SIZE];
160 char input_attr_name[ATTR_NAME_SIZE];
161 char limit1_attr_name[ATTR_NAME_SIZE];
162 char limit2_attr_name[ATTR_NAME_SIZE];
163 u64 id;
164 u64 type;
165 u64 limit1;
166 u64 limit2;
167 u64 cached_value;
168 unsigned long last_updated; /* in jiffies */
169 bool is_valid;
170 char const *acpi_name;
174 * Return buffer format:
175 * [0-3] "value" is valid flag
176 * [4-7] value
177 * [8- ] unknown stuff on newer mobos
179 struct atk_acpi_ret_buffer {
180 u32 flags;
181 u32 value;
182 u8 data[];
185 /* Input buffer used for GITM and SITM methods */
186 struct atk_acpi_input_buf {
187 u32 id;
188 u32 param1;
189 u32 param2;
192 static int atk_add(struct acpi_device *device);
193 static int atk_remove(struct acpi_device *device);
194 static void atk_print_sensor(struct atk_data *data, union acpi_object *obj);
195 static int atk_read_value(struct atk_sensor_data *sensor, u64 *value);
196 static void atk_free_sensors(struct atk_data *data);
198 static struct acpi_driver atk_driver = {
199 .name = ATK_HID,
200 .class = "hwmon",
201 .ids = atk_ids,
202 .ops = {
203 .add = atk_add,
204 .remove = atk_remove,
208 #define input_to_atk_sensor(attr) \
209 container_of(attr, struct atk_sensor_data, input_attr)
211 #define label_to_atk_sensor(attr) \
212 container_of(attr, struct atk_sensor_data, label_attr)
214 #define limit1_to_atk_sensor(attr) \
215 container_of(attr, struct atk_sensor_data, limit1_attr)
217 #define limit2_to_atk_sensor(attr) \
218 container_of(attr, struct atk_sensor_data, limit2_attr)
220 static ssize_t atk_input_show(struct device *dev,
221 struct device_attribute *attr, char *buf)
223 struct atk_sensor_data *s = input_to_atk_sensor(attr);
224 u64 value;
225 int err;
227 err = atk_read_value(s, &value);
228 if (err)
229 return err;
231 if (s->type == HWMON_TYPE_TEMP)
232 /* ACPI returns decidegree */
233 value *= 100;
235 return sprintf(buf, "%llu\n", value);
238 static ssize_t atk_label_show(struct device *dev,
239 struct device_attribute *attr, char *buf)
241 struct atk_sensor_data *s = label_to_atk_sensor(attr);
243 return sprintf(buf, "%s\n", s->acpi_name);
246 static ssize_t atk_limit1_show(struct device *dev,
247 struct device_attribute *attr, char *buf)
249 struct atk_sensor_data *s = limit1_to_atk_sensor(attr);
250 u64 value = s->limit1;
252 if (s->type == HWMON_TYPE_TEMP)
253 value *= 100;
255 return sprintf(buf, "%lld\n", value);
258 static ssize_t atk_limit2_show(struct device *dev,
259 struct device_attribute *attr, char *buf)
261 struct atk_sensor_data *s = limit2_to_atk_sensor(attr);
262 u64 value = s->limit2;
264 if (s->type == HWMON_TYPE_TEMP)
265 value *= 100;
267 return sprintf(buf, "%lld\n", value);
270 static ssize_t atk_name_show(struct device *dev,
271 struct device_attribute *attr, char *buf)
273 return sprintf(buf, "atk0110\n");
275 static struct device_attribute atk_name_attr =
276 __ATTR(name, 0444, atk_name_show, NULL);
278 static void atk_init_attribute(struct device_attribute *attr, char *name,
279 sysfs_show_func show)
281 sysfs_attr_init(&attr->attr);
282 attr->attr.name = name;
283 attr->attr.mode = 0444;
284 attr->show = show;
285 attr->store = NULL;
289 static union acpi_object *atk_get_pack_member(struct atk_data *data,
290 union acpi_object *pack,
291 enum atk_pack_member m)
293 bool old_if = data->old_interface;
294 int offset;
296 switch (m) {
297 case HWMON_PACK_FLAGS:
298 offset = old_if ? _HWMON_OLD_PACK_FLAGS : _HWMON_NEW_PACK_FLAGS;
299 break;
300 case HWMON_PACK_NAME:
301 offset = old_if ? _HWMON_OLD_PACK_NAME : _HWMON_NEW_PACK_NAME;
302 break;
303 case HWMON_PACK_LIMIT1:
304 offset = old_if ? _HWMON_OLD_PACK_LIMIT1 :
305 _HWMON_NEW_PACK_LIMIT1;
306 break;
307 case HWMON_PACK_LIMIT2:
308 offset = old_if ? _HWMON_OLD_PACK_LIMIT2 :
309 _HWMON_NEW_PACK_LIMIT2;
310 break;
311 case HWMON_PACK_ENABLE:
312 offset = old_if ? _HWMON_OLD_PACK_ENABLE :
313 _HWMON_NEW_PACK_ENABLE;
314 break;
315 default:
316 return NULL;
319 return &pack->package.elements[offset];
324 * New package format is:
325 * - flag (int)
326 * class - used for de-muxing the request to the correct GITn
327 * type (volt, temp, fan)
328 * sensor id |
329 * sensor id - used for de-muxing the request _inside_ the GITn
330 * - name (str)
331 * - unknown (int)
332 * - unknown (int)
333 * - limit1 (int)
334 * - limit2 (int)
335 * - enable (int)
337 * The old package has the same format but it's missing the two unknown fields.
339 static int validate_hwmon_pack(struct atk_data *data, union acpi_object *obj)
341 struct device *dev = &data->acpi_dev->dev;
342 union acpi_object *tmp;
343 bool old_if = data->old_interface;
344 int const expected_size = old_if ? _HWMON_OLD_PACK_SIZE :
345 _HWMON_NEW_PACK_SIZE;
347 if (obj->type != ACPI_TYPE_PACKAGE) {
348 dev_warn(dev, "Invalid type: %d\n", obj->type);
349 return -EINVAL;
352 if (obj->package.count != expected_size) {
353 dev_warn(dev, "Invalid package size: %d, expected: %d\n",
354 obj->package.count, expected_size);
355 return -EINVAL;
358 tmp = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS);
359 if (tmp->type != ACPI_TYPE_INTEGER) {
360 dev_warn(dev, "Invalid type (flag): %d\n", tmp->type);
361 return -EINVAL;
364 tmp = atk_get_pack_member(data, obj, HWMON_PACK_NAME);
365 if (tmp->type != ACPI_TYPE_STRING) {
366 dev_warn(dev, "Invalid type (name): %d\n", tmp->type);
367 return -EINVAL;
370 /* Don't check... we don't know what they're useful for anyway */
371 #if 0
372 tmp = &obj->package.elements[HWMON_PACK_UNK1];
373 if (tmp->type != ACPI_TYPE_INTEGER) {
374 dev_warn(dev, "Invalid type (unk1): %d\n", tmp->type);
375 return -EINVAL;
378 tmp = &obj->package.elements[HWMON_PACK_UNK2];
379 if (tmp->type != ACPI_TYPE_INTEGER) {
380 dev_warn(dev, "Invalid type (unk2): %d\n", tmp->type);
381 return -EINVAL;
383 #endif
385 tmp = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT1);
386 if (tmp->type != ACPI_TYPE_INTEGER) {
387 dev_warn(dev, "Invalid type (limit1): %d\n", tmp->type);
388 return -EINVAL;
391 tmp = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT2);
392 if (tmp->type != ACPI_TYPE_INTEGER) {
393 dev_warn(dev, "Invalid type (limit2): %d\n", tmp->type);
394 return -EINVAL;
397 tmp = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE);
398 if (tmp->type != ACPI_TYPE_INTEGER) {
399 dev_warn(dev, "Invalid type (enable): %d\n", tmp->type);
400 return -EINVAL;
403 atk_print_sensor(data, obj);
405 return 0;
408 #ifdef DEBUG
409 static char const *atk_sensor_type(union acpi_object *flags)
411 u64 type = flags->integer.value & ATK_TYPE_MASK;
412 char const *what;
414 switch (type) {
415 case HWMON_TYPE_VOLT:
416 what = "voltage";
417 break;
418 case HWMON_TYPE_TEMP:
419 what = "temperature";
420 break;
421 case HWMON_TYPE_FAN:
422 what = "fan";
423 break;
424 default:
425 what = "unknown";
426 break;
429 return what;
431 #endif
433 static void atk_print_sensor(struct atk_data *data, union acpi_object *obj)
435 #ifdef DEBUG
436 struct device *dev = &data->acpi_dev->dev;
437 union acpi_object *flags;
438 union acpi_object *name;
439 union acpi_object *limit1;
440 union acpi_object *limit2;
441 union acpi_object *enable;
442 char const *what;
444 flags = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS);
445 name = atk_get_pack_member(data, obj, HWMON_PACK_NAME);
446 limit1 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT1);
447 limit2 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT2);
448 enable = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE);
450 what = atk_sensor_type(flags);
452 dev_dbg(dev, "%s: %#llx %s [%llu-%llu] %s\n", what,
453 flags->integer.value,
454 name->string.pointer,
455 limit1->integer.value, limit2->integer.value,
456 enable->integer.value ? "enabled" : "disabled");
457 #endif
460 static int atk_read_value_old(struct atk_sensor_data *sensor, u64 *value)
462 struct atk_data *data = sensor->data;
463 struct device *dev = &data->acpi_dev->dev;
464 struct acpi_object_list params;
465 union acpi_object id;
466 acpi_status status;
467 acpi_handle method;
469 switch (sensor->type) {
470 case HWMON_TYPE_VOLT:
471 method = data->rvlt_handle;
472 break;
473 case HWMON_TYPE_TEMP:
474 method = data->rtmp_handle;
475 break;
476 case HWMON_TYPE_FAN:
477 method = data->rfan_handle;
478 break;
479 default:
480 return -EINVAL;
483 id.type = ACPI_TYPE_INTEGER;
484 id.integer.value = sensor->id;
486 params.count = 1;
487 params.pointer = &id;
489 status = acpi_evaluate_integer(method, NULL, &params, value);
490 if (status != AE_OK) {
491 dev_warn(dev, "%s: ACPI exception: %s\n", __func__,
492 acpi_format_exception(status));
493 return -EIO;
496 return 0;
499 static union acpi_object *atk_ggrp(struct atk_data *data, u16 mux)
501 struct device *dev = &data->acpi_dev->dev;
502 struct acpi_buffer buf;
503 acpi_status ret;
504 struct acpi_object_list params;
505 union acpi_object id;
506 union acpi_object *pack;
508 id.type = ACPI_TYPE_INTEGER;
509 id.integer.value = mux;
510 params.count = 1;
511 params.pointer = &id;
513 buf.length = ACPI_ALLOCATE_BUFFER;
514 ret = acpi_evaluate_object(data->enumerate_handle, NULL, &params, &buf);
515 if (ret != AE_OK) {
516 dev_err(dev, "GGRP[%#x] ACPI exception: %s\n", mux,
517 acpi_format_exception(ret));
518 return ERR_PTR(-EIO);
520 pack = buf.pointer;
521 if (pack->type != ACPI_TYPE_PACKAGE) {
522 /* Execution was successful, but the id was not found */
523 ACPI_FREE(pack);
524 return ERR_PTR(-ENOENT);
527 if (pack->package.count < 1) {
528 dev_err(dev, "GGRP[%#x] package is too small\n", mux);
529 ACPI_FREE(pack);
530 return ERR_PTR(-EIO);
532 return pack;
535 static union acpi_object *atk_gitm(struct atk_data *data, u64 id)
537 struct device *dev = &data->acpi_dev->dev;
538 struct atk_acpi_input_buf buf;
539 union acpi_object tmp;
540 struct acpi_object_list params;
541 struct acpi_buffer ret;
542 union acpi_object *obj;
543 acpi_status status;
545 buf.id = id;
546 buf.param1 = 0;
547 buf.param2 = 0;
549 tmp.type = ACPI_TYPE_BUFFER;
550 tmp.buffer.pointer = (u8 *)&buf;
551 tmp.buffer.length = sizeof(buf);
553 params.count = 1;
554 params.pointer = (void *)&tmp;
556 ret.length = ACPI_ALLOCATE_BUFFER;
557 status = acpi_evaluate_object_typed(data->read_handle, NULL, &params,
558 &ret, ACPI_TYPE_BUFFER);
559 if (status != AE_OK) {
560 dev_warn(dev, "GITM[%#llx] ACPI exception: %s\n", id,
561 acpi_format_exception(status));
562 return ERR_PTR(-EIO);
564 obj = ret.pointer;
566 /* Sanity check */
567 if (obj->buffer.length < 8) {
568 dev_warn(dev, "Unexpected ASBF length: %u\n",
569 obj->buffer.length);
570 ACPI_FREE(obj);
571 return ERR_PTR(-EIO);
573 return obj;
576 static union acpi_object *atk_sitm(struct atk_data *data,
577 struct atk_acpi_input_buf *buf)
579 struct device *dev = &data->acpi_dev->dev;
580 struct acpi_object_list params;
581 union acpi_object tmp;
582 struct acpi_buffer ret;
583 union acpi_object *obj;
584 acpi_status status;
586 tmp.type = ACPI_TYPE_BUFFER;
587 tmp.buffer.pointer = (u8 *)buf;
588 tmp.buffer.length = sizeof(*buf);
590 params.count = 1;
591 params.pointer = &tmp;
593 ret.length = ACPI_ALLOCATE_BUFFER;
594 status = acpi_evaluate_object_typed(data->write_handle, NULL, &params,
595 &ret, ACPI_TYPE_BUFFER);
596 if (status != AE_OK) {
597 dev_warn(dev, "SITM[%#x] ACPI exception: %s\n", buf->id,
598 acpi_format_exception(status));
599 return ERR_PTR(-EIO);
601 obj = ret.pointer;
603 /* Sanity check */
604 if (obj->buffer.length < 8) {
605 dev_warn(dev, "Unexpected ASBF length: %u\n",
606 obj->buffer.length);
607 ACPI_FREE(obj);
608 return ERR_PTR(-EIO);
610 return obj;
613 static int atk_read_value_new(struct atk_sensor_data *sensor, u64 *value)
615 struct atk_data *data = sensor->data;
616 struct device *dev = &data->acpi_dev->dev;
617 union acpi_object *obj;
618 struct atk_acpi_ret_buffer *buf;
619 int err = 0;
621 obj = atk_gitm(data, sensor->id);
622 if (IS_ERR(obj))
623 return PTR_ERR(obj);
625 buf = (struct atk_acpi_ret_buffer *)obj->buffer.pointer;
626 if (buf->flags == 0) {
628 * The reading is not valid, possible causes:
629 * - sensor failure
630 * - enumeration was FUBAR (and we didn't notice)
632 dev_warn(dev, "Read failed, sensor = %#llx\n", sensor->id);
633 err = -EIO;
634 goto out;
637 *value = buf->value;
638 out:
639 ACPI_FREE(obj);
640 return err;
643 static int atk_read_value(struct atk_sensor_data *sensor, u64 *value)
645 int err;
647 if (!sensor->is_valid ||
648 time_after(jiffies, sensor->last_updated + CACHE_TIME)) {
649 if (sensor->data->old_interface)
650 err = atk_read_value_old(sensor, value);
651 else
652 err = atk_read_value_new(sensor, value);
654 sensor->is_valid = true;
655 sensor->last_updated = jiffies;
656 sensor->cached_value = *value;
657 } else {
658 *value = sensor->cached_value;
659 err = 0;
662 return err;
665 #ifdef CONFIG_DEBUG_FS
666 static int atk_debugfs_gitm_get(void *p, u64 *val)
668 struct atk_data *data = p;
669 union acpi_object *ret;
670 struct atk_acpi_ret_buffer *buf;
671 int err = 0;
673 if (!data->read_handle)
674 return -ENODEV;
676 if (!data->debugfs.id)
677 return -EINVAL;
679 ret = atk_gitm(data, data->debugfs.id);
680 if (IS_ERR(ret))
681 return PTR_ERR(ret);
683 buf = (struct atk_acpi_ret_buffer *)ret->buffer.pointer;
684 if (buf->flags)
685 *val = buf->value;
686 else
687 err = -EIO;
689 ACPI_FREE(ret);
690 return err;
693 DEFINE_SIMPLE_ATTRIBUTE(atk_debugfs_gitm,
694 atk_debugfs_gitm_get,
695 NULL,
696 "0x%08llx\n")
698 static int atk_acpi_print(char *buf, size_t sz, union acpi_object *obj)
700 int ret = 0;
702 switch (obj->type) {
703 case ACPI_TYPE_INTEGER:
704 ret = snprintf(buf, sz, "0x%08llx\n", obj->integer.value);
705 break;
706 case ACPI_TYPE_STRING:
707 ret = snprintf(buf, sz, "%s\n", obj->string.pointer);
708 break;
711 return ret;
714 static void atk_pack_print(char *buf, size_t sz, union acpi_object *pack)
716 int ret;
717 int i;
719 for (i = 0; i < pack->package.count; i++) {
720 union acpi_object *obj = &pack->package.elements[i];
722 ret = atk_acpi_print(buf, sz, obj);
723 if (ret >= sz)
724 break;
725 buf += ret;
726 sz -= ret;
730 static int atk_debugfs_ggrp_open(struct inode *inode, struct file *file)
732 struct atk_data *data = inode->i_private;
733 char *buf = NULL;
734 union acpi_object *ret;
735 u8 cls;
736 int i;
738 if (!data->enumerate_handle)
739 return -ENODEV;
740 if (!data->debugfs.id)
741 return -EINVAL;
743 cls = (data->debugfs.id & 0xff000000) >> 24;
744 ret = atk_ggrp(data, cls);
745 if (IS_ERR(ret))
746 return PTR_ERR(ret);
748 for (i = 0; i < ret->package.count; i++) {
749 union acpi_object *pack = &ret->package.elements[i];
750 union acpi_object *id;
752 if (pack->type != ACPI_TYPE_PACKAGE)
753 continue;
754 if (!pack->package.count)
755 continue;
756 id = &pack->package.elements[0];
757 if (id->integer.value == data->debugfs.id) {
758 /* Print the package */
759 buf = kzalloc(512, GFP_KERNEL);
760 if (!buf) {
761 ACPI_FREE(ret);
762 return -ENOMEM;
764 atk_pack_print(buf, 512, pack);
765 break;
768 ACPI_FREE(ret);
770 if (!buf)
771 return -EINVAL;
773 file->private_data = buf;
775 return nonseekable_open(inode, file);
778 static ssize_t atk_debugfs_ggrp_read(struct file *file, char __user *buf,
779 size_t count, loff_t *pos)
781 char *str = file->private_data;
782 size_t len = strlen(str);
784 return simple_read_from_buffer(buf, count, pos, str, len);
787 static int atk_debugfs_ggrp_release(struct inode *inode, struct file *file)
789 kfree(file->private_data);
790 return 0;
793 static const struct file_operations atk_debugfs_ggrp_fops = {
794 .read = atk_debugfs_ggrp_read,
795 .open = atk_debugfs_ggrp_open,
796 .release = atk_debugfs_ggrp_release,
797 .llseek = no_llseek,
800 static void atk_debugfs_init(struct atk_data *data)
802 struct dentry *d;
803 struct dentry *f;
805 data->debugfs.id = 0;
807 d = debugfs_create_dir("asus_atk0110", NULL);
808 if (!d || IS_ERR(d))
809 return;
811 f = debugfs_create_x32("id", S_IRUSR | S_IWUSR, d, &data->debugfs.id);
812 if (!f || IS_ERR(f))
813 goto cleanup;
815 f = debugfs_create_file("gitm", S_IRUSR, d, data,
816 &atk_debugfs_gitm);
817 if (!f || IS_ERR(f))
818 goto cleanup;
820 f = debugfs_create_file("ggrp", S_IRUSR, d, data,
821 &atk_debugfs_ggrp_fops);
822 if (!f || IS_ERR(f))
823 goto cleanup;
825 data->debugfs.root = d;
827 return;
828 cleanup:
829 debugfs_remove_recursive(d);
832 static void atk_debugfs_cleanup(struct atk_data *data)
834 debugfs_remove_recursive(data->debugfs.root);
837 #else /* CONFIG_DEBUG_FS */
839 static void atk_debugfs_init(struct atk_data *data)
843 static void atk_debugfs_cleanup(struct atk_data *data)
846 #endif
848 static int atk_add_sensor(struct atk_data *data, union acpi_object *obj)
850 struct device *dev = &data->acpi_dev->dev;
851 union acpi_object *flags;
852 union acpi_object *name;
853 union acpi_object *limit1;
854 union acpi_object *limit2;
855 union acpi_object *enable;
856 struct atk_sensor_data *sensor;
857 char const *base_name;
858 char const *limit1_name;
859 char const *limit2_name;
860 u64 type;
861 int err;
862 int *num;
863 int start;
865 if (obj->type != ACPI_TYPE_PACKAGE) {
866 /* wft is this? */
867 dev_warn(dev, "Unknown type for ACPI object: (%d)\n",
868 obj->type);
869 return -EINVAL;
872 err = validate_hwmon_pack(data, obj);
873 if (err)
874 return err;
876 /* Ok, we have a valid hwmon package */
877 type = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS)->integer.value
878 & ATK_TYPE_MASK;
880 switch (type) {
881 case HWMON_TYPE_VOLT:
882 base_name = "in";
883 limit1_name = "min";
884 limit2_name = "max";
885 num = &data->voltage_count;
886 start = 0;
887 break;
888 case HWMON_TYPE_TEMP:
889 base_name = "temp";
890 limit1_name = "max";
891 limit2_name = "crit";
892 num = &data->temperature_count;
893 start = 1;
894 break;
895 case HWMON_TYPE_FAN:
896 base_name = "fan";
897 limit1_name = "min";
898 limit2_name = "max";
899 num = &data->fan_count;
900 start = 1;
901 break;
902 default:
903 dev_warn(dev, "Unknown sensor type: %#llx\n", type);
904 return -EINVAL;
907 enable = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE);
908 if (!enable->integer.value)
909 /* sensor is disabled */
910 return 0;
912 flags = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS);
913 name = atk_get_pack_member(data, obj, HWMON_PACK_NAME);
914 limit1 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT1);
915 limit2 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT2);
917 sensor = kzalloc(sizeof(*sensor), GFP_KERNEL);
918 if (!sensor)
919 return -ENOMEM;
921 sensor->acpi_name = kstrdup(name->string.pointer, GFP_KERNEL);
922 if (!sensor->acpi_name) {
923 err = -ENOMEM;
924 goto out;
927 INIT_LIST_HEAD(&sensor->list);
928 sensor->type = type;
929 sensor->data = data;
930 sensor->id = flags->integer.value;
931 sensor->limit1 = limit1->integer.value;
932 if (data->old_interface)
933 sensor->limit2 = limit2->integer.value;
934 else
935 /* The upper limit is expressed as delta from lower limit */
936 sensor->limit2 = sensor->limit1 + limit2->integer.value;
938 snprintf(sensor->input_attr_name, ATTR_NAME_SIZE,
939 "%s%d_input", base_name, start + *num);
940 atk_init_attribute(&sensor->input_attr,
941 sensor->input_attr_name,
942 atk_input_show);
944 snprintf(sensor->label_attr_name, ATTR_NAME_SIZE,
945 "%s%d_label", base_name, start + *num);
946 atk_init_attribute(&sensor->label_attr,
947 sensor->label_attr_name,
948 atk_label_show);
950 snprintf(sensor->limit1_attr_name, ATTR_NAME_SIZE,
951 "%s%d_%s", base_name, start + *num, limit1_name);
952 atk_init_attribute(&sensor->limit1_attr,
953 sensor->limit1_attr_name,
954 atk_limit1_show);
956 snprintf(sensor->limit2_attr_name, ATTR_NAME_SIZE,
957 "%s%d_%s", base_name, start + *num, limit2_name);
958 atk_init_attribute(&sensor->limit2_attr,
959 sensor->limit2_attr_name,
960 atk_limit2_show);
962 list_add(&sensor->list, &data->sensor_list);
963 (*num)++;
965 return 1;
966 out:
967 kfree(sensor);
968 return err;
971 static int atk_enumerate_old_hwmon(struct atk_data *data)
973 struct device *dev = &data->acpi_dev->dev;
974 struct acpi_buffer buf;
975 union acpi_object *pack;
976 acpi_status status;
977 int i, ret;
978 int count = 0;
980 /* Voltages */
981 buf.length = ACPI_ALLOCATE_BUFFER;
982 status = acpi_evaluate_object_typed(data->atk_handle,
983 METHOD_OLD_ENUM_VLT, NULL, &buf, ACPI_TYPE_PACKAGE);
984 if (status != AE_OK) {
985 dev_warn(dev, METHOD_OLD_ENUM_VLT ": ACPI exception: %s\n",
986 acpi_format_exception(status));
988 return -ENODEV;
991 pack = buf.pointer;
992 for (i = 1; i < pack->package.count; i++) {
993 union acpi_object *obj = &pack->package.elements[i];
995 ret = atk_add_sensor(data, obj);
996 if (ret > 0)
997 count++;
999 ACPI_FREE(buf.pointer);
1001 /* Temperatures */
1002 buf.length = ACPI_ALLOCATE_BUFFER;
1003 status = acpi_evaluate_object_typed(data->atk_handle,
1004 METHOD_OLD_ENUM_TMP, NULL, &buf, ACPI_TYPE_PACKAGE);
1005 if (status != AE_OK) {
1006 dev_warn(dev, METHOD_OLD_ENUM_TMP ": ACPI exception: %s\n",
1007 acpi_format_exception(status));
1009 ret = -ENODEV;
1010 goto cleanup;
1013 pack = buf.pointer;
1014 for (i = 1; i < pack->package.count; i++) {
1015 union acpi_object *obj = &pack->package.elements[i];
1017 ret = atk_add_sensor(data, obj);
1018 if (ret > 0)
1019 count++;
1021 ACPI_FREE(buf.pointer);
1023 /* Fans */
1024 buf.length = ACPI_ALLOCATE_BUFFER;
1025 status = acpi_evaluate_object_typed(data->atk_handle,
1026 METHOD_OLD_ENUM_FAN, NULL, &buf, ACPI_TYPE_PACKAGE);
1027 if (status != AE_OK) {
1028 dev_warn(dev, METHOD_OLD_ENUM_FAN ": ACPI exception: %s\n",
1029 acpi_format_exception(status));
1031 ret = -ENODEV;
1032 goto cleanup;
1035 pack = buf.pointer;
1036 for (i = 1; i < pack->package.count; i++) {
1037 union acpi_object *obj = &pack->package.elements[i];
1039 ret = atk_add_sensor(data, obj);
1040 if (ret > 0)
1041 count++;
1043 ACPI_FREE(buf.pointer);
1045 return count;
1046 cleanup:
1047 atk_free_sensors(data);
1048 return ret;
1051 static int atk_ec_present(struct atk_data *data)
1053 struct device *dev = &data->acpi_dev->dev;
1054 union acpi_object *pack;
1055 union acpi_object *ec;
1056 int ret;
1057 int i;
1059 pack = atk_ggrp(data, ATK_MUX_MGMT);
1060 if (IS_ERR(pack)) {
1061 if (PTR_ERR(pack) == -ENOENT) {
1062 /* The MGMT class does not exists - that's ok */
1063 dev_dbg(dev, "Class %#llx not found\n", ATK_MUX_MGMT);
1064 return 0;
1066 return PTR_ERR(pack);
1069 /* Search the EC */
1070 ec = NULL;
1071 for (i = 0; i < pack->package.count; i++) {
1072 union acpi_object *obj = &pack->package.elements[i];
1073 union acpi_object *id;
1075 if (obj->type != ACPI_TYPE_PACKAGE)
1076 continue;
1078 id = &obj->package.elements[0];
1079 if (id->type != ACPI_TYPE_INTEGER)
1080 continue;
1082 if (id->integer.value == ATK_EC_ID) {
1083 ec = obj;
1084 break;
1088 ret = (ec != NULL);
1089 if (!ret)
1090 /* The system has no EC */
1091 dev_dbg(dev, "EC not found\n");
1093 ACPI_FREE(pack);
1094 return ret;
1097 static int atk_ec_enabled(struct atk_data *data)
1099 struct device *dev = &data->acpi_dev->dev;
1100 union acpi_object *obj;
1101 struct atk_acpi_ret_buffer *buf;
1102 int err;
1104 obj = atk_gitm(data, ATK_EC_ID);
1105 if (IS_ERR(obj)) {
1106 dev_err(dev, "Unable to query EC status\n");
1107 return PTR_ERR(obj);
1109 buf = (struct atk_acpi_ret_buffer *)obj->buffer.pointer;
1111 if (buf->flags == 0) {
1112 dev_err(dev, "Unable to query EC status\n");
1113 err = -EIO;
1114 } else {
1115 err = (buf->value != 0);
1116 dev_dbg(dev, "EC is %sabled\n",
1117 err ? "en" : "dis");
1120 ACPI_FREE(obj);
1121 return err;
1124 static int atk_ec_ctl(struct atk_data *data, int enable)
1126 struct device *dev = &data->acpi_dev->dev;
1127 union acpi_object *obj;
1128 struct atk_acpi_input_buf sitm;
1129 struct atk_acpi_ret_buffer *ec_ret;
1130 int err = 0;
1132 sitm.id = ATK_EC_ID;
1133 sitm.param1 = enable;
1134 sitm.param2 = 0;
1136 obj = atk_sitm(data, &sitm);
1137 if (IS_ERR(obj)) {
1138 dev_err(dev, "Failed to %sable the EC\n",
1139 enable ? "en" : "dis");
1140 return PTR_ERR(obj);
1142 ec_ret = (struct atk_acpi_ret_buffer *)obj->buffer.pointer;
1143 if (ec_ret->flags == 0) {
1144 dev_err(dev, "Failed to %sable the EC\n",
1145 enable ? "en" : "dis");
1146 err = -EIO;
1147 } else {
1148 dev_info(dev, "EC %sabled\n",
1149 enable ? "en" : "dis");
1152 ACPI_FREE(obj);
1153 return err;
1156 static int atk_enumerate_new_hwmon(struct atk_data *data)
1158 struct device *dev = &data->acpi_dev->dev;
1159 union acpi_object *pack;
1160 int err;
1161 int i;
1163 err = atk_ec_present(data);
1164 if (err < 0)
1165 return err;
1166 if (err) {
1167 err = atk_ec_enabled(data);
1168 if (err < 0)
1169 return err;
1170 /* If the EC was disabled we will disable it again on unload */
1171 data->disable_ec = err;
1173 err = atk_ec_ctl(data, 1);
1174 if (err) {
1175 data->disable_ec = false;
1176 return err;
1180 dev_dbg(dev, "Enumerating hwmon sensors\n");
1182 pack = atk_ggrp(data, ATK_MUX_HWMON);
1183 if (IS_ERR(pack))
1184 return PTR_ERR(pack);
1186 for (i = 0; i < pack->package.count; i++) {
1187 union acpi_object *obj = &pack->package.elements[i];
1189 atk_add_sensor(data, obj);
1192 err = data->voltage_count + data->temperature_count + data->fan_count;
1194 ACPI_FREE(pack);
1195 return err;
1198 static int atk_create_files(struct atk_data *data)
1200 struct atk_sensor_data *s;
1201 int err;
1203 list_for_each_entry(s, &data->sensor_list, list) {
1204 err = device_create_file(data->hwmon_dev, &s->input_attr);
1205 if (err)
1206 return err;
1207 err = device_create_file(data->hwmon_dev, &s->label_attr);
1208 if (err)
1209 return err;
1210 err = device_create_file(data->hwmon_dev, &s->limit1_attr);
1211 if (err)
1212 return err;
1213 err = device_create_file(data->hwmon_dev, &s->limit2_attr);
1214 if (err)
1215 return err;
1218 err = device_create_file(data->hwmon_dev, &atk_name_attr);
1220 return err;
1223 static void atk_remove_files(struct atk_data *data)
1225 struct atk_sensor_data *s;
1227 list_for_each_entry(s, &data->sensor_list, list) {
1228 device_remove_file(data->hwmon_dev, &s->input_attr);
1229 device_remove_file(data->hwmon_dev, &s->label_attr);
1230 device_remove_file(data->hwmon_dev, &s->limit1_attr);
1231 device_remove_file(data->hwmon_dev, &s->limit2_attr);
1233 device_remove_file(data->hwmon_dev, &atk_name_attr);
1236 static void atk_free_sensors(struct atk_data *data)
1238 struct list_head *head = &data->sensor_list;
1239 struct atk_sensor_data *s, *tmp;
1241 list_for_each_entry_safe(s, tmp, head, list) {
1242 kfree(s->acpi_name);
1243 kfree(s);
1247 static int atk_register_hwmon(struct atk_data *data)
1249 struct device *dev = &data->acpi_dev->dev;
1250 int err;
1252 dev_dbg(dev, "registering hwmon device\n");
1253 data->hwmon_dev = hwmon_device_register(dev);
1254 if (IS_ERR(data->hwmon_dev))
1255 return PTR_ERR(data->hwmon_dev);
1257 dev_dbg(dev, "populating sysfs directory\n");
1258 err = atk_create_files(data);
1259 if (err)
1260 goto remove;
1262 return 0;
1263 remove:
1264 /* Cleanup the registered files */
1265 atk_remove_files(data);
1266 hwmon_device_unregister(data->hwmon_dev);
1267 return err;
1270 static int atk_probe_if(struct atk_data *data)
1272 struct device *dev = &data->acpi_dev->dev;
1273 acpi_handle ret;
1274 acpi_status status;
1275 int err = 0;
1277 /* RTMP: read temperature */
1278 status = acpi_get_handle(data->atk_handle, METHOD_OLD_READ_TMP, &ret);
1279 if (ACPI_SUCCESS(status))
1280 data->rtmp_handle = ret;
1281 else
1282 dev_dbg(dev, "method " METHOD_OLD_READ_TMP " not found: %s\n",
1283 acpi_format_exception(status));
1285 /* RVLT: read voltage */
1286 status = acpi_get_handle(data->atk_handle, METHOD_OLD_READ_VLT, &ret);
1287 if (ACPI_SUCCESS(status))
1288 data->rvlt_handle = ret;
1289 else
1290 dev_dbg(dev, "method " METHOD_OLD_READ_VLT " not found: %s\n",
1291 acpi_format_exception(status));
1293 /* RFAN: read fan status */
1294 status = acpi_get_handle(data->atk_handle, METHOD_OLD_READ_FAN, &ret);
1295 if (ACPI_SUCCESS(status))
1296 data->rfan_handle = ret;
1297 else
1298 dev_dbg(dev, "method " METHOD_OLD_READ_FAN " not found: %s\n",
1299 acpi_format_exception(status));
1301 /* Enumeration */
1302 status = acpi_get_handle(data->atk_handle, METHOD_ENUMERATE, &ret);
1303 if (ACPI_SUCCESS(status))
1304 data->enumerate_handle = ret;
1305 else
1306 dev_dbg(dev, "method " METHOD_ENUMERATE " not found: %s\n",
1307 acpi_format_exception(status));
1309 /* De-multiplexer (read) */
1310 status = acpi_get_handle(data->atk_handle, METHOD_READ, &ret);
1311 if (ACPI_SUCCESS(status))
1312 data->read_handle = ret;
1313 else
1314 dev_dbg(dev, "method " METHOD_READ " not found: %s\n",
1315 acpi_format_exception(status));
1317 /* De-multiplexer (write) */
1318 status = acpi_get_handle(data->atk_handle, METHOD_WRITE, &ret);
1319 if (ACPI_SUCCESS(status))
1320 data->write_handle = ret;
1321 else
1322 dev_dbg(dev, "method " METHOD_WRITE " not found: %s\n",
1323 acpi_format_exception(status));
1326 * Check for hwmon methods: first check "old" style methods; note that
1327 * both may be present: in this case we stick to the old interface;
1328 * analysis of multiple DSDTs indicates that when both interfaces
1329 * are present the new one (GGRP/GITM) is not functional.
1331 if (new_if)
1332 dev_info(dev, "Overriding interface detection\n");
1333 if (data->rtmp_handle &&
1334 data->rvlt_handle && data->rfan_handle && !new_if)
1335 data->old_interface = true;
1336 else if (data->enumerate_handle && data->read_handle &&
1337 data->write_handle)
1338 data->old_interface = false;
1339 else
1340 err = -ENODEV;
1342 return err;
1345 static int atk_add(struct acpi_device *device)
1347 acpi_status ret;
1348 int err;
1349 struct acpi_buffer buf;
1350 union acpi_object *obj;
1351 struct atk_data *data;
1353 dev_dbg(&device->dev, "adding...\n");
1355 data = kzalloc(sizeof(*data), GFP_KERNEL);
1356 if (!data)
1357 return -ENOMEM;
1359 data->acpi_dev = device;
1360 data->atk_handle = device->handle;
1361 INIT_LIST_HEAD(&data->sensor_list);
1362 data->disable_ec = false;
1364 buf.length = ACPI_ALLOCATE_BUFFER;
1365 ret = acpi_evaluate_object_typed(data->atk_handle, BOARD_ID, NULL,
1366 &buf, ACPI_TYPE_PACKAGE);
1367 if (ret != AE_OK) {
1368 dev_dbg(&device->dev, "atk: method MBIF not found\n");
1369 } else {
1370 obj = buf.pointer;
1371 if (obj->package.count >= 2) {
1372 union acpi_object *id = &obj->package.elements[1];
1373 if (id->type == ACPI_TYPE_STRING)
1374 dev_dbg(&device->dev, "board ID = %s\n",
1375 id->string.pointer);
1377 ACPI_FREE(buf.pointer);
1380 err = atk_probe_if(data);
1381 if (err) {
1382 dev_err(&device->dev, "No usable hwmon interface detected\n");
1383 goto out;
1386 if (data->old_interface) {
1387 dev_dbg(&device->dev, "Using old hwmon interface\n");
1388 err = atk_enumerate_old_hwmon(data);
1389 } else {
1390 dev_dbg(&device->dev, "Using new hwmon interface\n");
1391 err = atk_enumerate_new_hwmon(data);
1393 if (err < 0)
1394 goto out;
1395 if (err == 0) {
1396 dev_info(&device->dev,
1397 "No usable sensor detected, bailing out\n");
1398 err = -ENODEV;
1399 goto out;
1402 err = atk_register_hwmon(data);
1403 if (err)
1404 goto cleanup;
1406 atk_debugfs_init(data);
1408 device->driver_data = data;
1409 return 0;
1410 cleanup:
1411 atk_free_sensors(data);
1412 out:
1413 if (data->disable_ec)
1414 atk_ec_ctl(data, 0);
1415 kfree(data);
1416 return err;
1419 static int atk_remove(struct acpi_device *device)
1421 struct atk_data *data = device->driver_data;
1422 dev_dbg(&device->dev, "removing...\n");
1424 device->driver_data = NULL;
1426 atk_debugfs_cleanup(data);
1428 atk_remove_files(data);
1429 atk_free_sensors(data);
1430 hwmon_device_unregister(data->hwmon_dev);
1432 if (data->disable_ec) {
1433 if (atk_ec_ctl(data, 0))
1434 dev_err(&device->dev, "Failed to disable EC\n");
1437 kfree(data);
1439 return 0;
1442 static int __init atk0110_init(void)
1444 int ret;
1446 /* Make sure it's safe to access the device through ACPI */
1447 if (!acpi_resources_are_enforced()) {
1448 pr_err("Resources not safely usable due to acpi_enforce_resources kernel parameter\n");
1449 return -EBUSY;
1452 if (dmi_check_system(atk_force_new_if))
1453 new_if = true;
1455 ret = acpi_bus_register_driver(&atk_driver);
1456 if (ret)
1457 pr_info("acpi_bus_register_driver failed: %d\n", ret);
1459 return ret;
1462 static void __exit atk0110_exit(void)
1464 acpi_bus_unregister_driver(&atk_driver);
1467 module_init(atk0110_init);
1468 module_exit(atk0110_exit);
1470 MODULE_LICENSE("GPL");