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.
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"
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",
37 DMI_MATCH(DMI_BOARD_NAME, "SABERTOOTH X58")
40 /* Old interface reads the same sensor for fan0 and fan1 */
41 .ident = "Asus M5A78L",
43 DMI_MATCH(DMI_BOARD_NAME, "M5A78L")
50 * Minimum time between readings, enforced in order to avoid
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 {
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
112 struct device *hwmon_dev;
113 acpi_handle atk_handle;
114 struct acpi_device *acpi_dev;
119 acpi_handle rtmp_handle;
120 acpi_handle rvlt_handle;
121 acpi_handle rfan_handle;
123 acpi_handle enumerate_handle;
124 acpi_handle read_handle;
125 acpi_handle write_handle;
130 int temperature_count;
132 struct list_head sensor_list;
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[] = {
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];
168 unsigned long last_updated; /* in jiffies */
170 char const *acpi_name;
174 * Return buffer format:
175 * [0-3] "value" is valid flag
177 * [8- ] unknown stuff on newer mobos
179 struct atk_acpi_ret_buffer {
185 /* Input buffer used for GITM and SITM methods */
186 struct atk_acpi_input_buf {
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 = {
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);
227 err = atk_read_value(s, &value);
231 if (s->type == HWMON_TYPE_TEMP)
232 /* ACPI returns decidegree */
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)
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)
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;
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;
297 case HWMON_PACK_FLAGS:
298 offset = old_if ? _HWMON_OLD_PACK_FLAGS : _HWMON_NEW_PACK_FLAGS;
300 case HWMON_PACK_NAME:
301 offset = old_if ? _HWMON_OLD_PACK_NAME : _HWMON_NEW_PACK_NAME;
303 case HWMON_PACK_LIMIT1:
304 offset = old_if ? _HWMON_OLD_PACK_LIMIT1 :
305 _HWMON_NEW_PACK_LIMIT1;
307 case HWMON_PACK_LIMIT2:
308 offset = old_if ? _HWMON_OLD_PACK_LIMIT2 :
309 _HWMON_NEW_PACK_LIMIT2;
311 case HWMON_PACK_ENABLE:
312 offset = old_if ? _HWMON_OLD_PACK_ENABLE :
313 _HWMON_NEW_PACK_ENABLE;
319 return &pack->package.elements[offset];
324 * New package format is:
326 * class - used for de-muxing the request to the correct GITn
327 * type (volt, temp, fan)
329 * sensor id - used for de-muxing the request _inside_ the GITn
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);
352 if (obj->package.count != expected_size) {
353 dev_warn(dev, "Invalid package size: %d, expected: %d\n",
354 obj->package.count, expected_size);
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);
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);
370 /* Don't check... we don't know what they're useful for anyway */
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);
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);
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);
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);
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);
403 atk_print_sensor(data, obj);
409 static char const *atk_sensor_type(union acpi_object *flags)
411 u64 type = flags->integer.value & ATK_TYPE_MASK;
415 case HWMON_TYPE_VOLT:
418 case HWMON_TYPE_TEMP:
419 what = "temperature";
433 static void atk_print_sensor(struct atk_data *data, union acpi_object *obj)
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;
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");
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;
469 switch (sensor->type) {
470 case HWMON_TYPE_VOLT:
471 method = data->rvlt_handle;
473 case HWMON_TYPE_TEMP:
474 method = data->rtmp_handle;
477 method = data->rfan_handle;
483 id.type = ACPI_TYPE_INTEGER;
484 id.integer.value = sensor->id;
487 params.pointer = &id;
489 status = acpi_evaluate_integer(method, NULL, ¶ms, value);
490 if (status != AE_OK) {
491 dev_warn(dev, "%s: ACPI exception: %s\n", __func__,
492 acpi_format_exception(status));
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;
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;
511 params.pointer = &id;
513 buf.length = ACPI_ALLOCATE_BUFFER;
514 ret = acpi_evaluate_object(data->enumerate_handle, NULL, ¶ms, &buf);
516 dev_err(dev, "GGRP[%#x] ACPI exception: %s\n", mux,
517 acpi_format_exception(ret));
518 return ERR_PTR(-EIO);
521 if (pack->type != ACPI_TYPE_PACKAGE) {
522 /* Execution was successful, but the id was not found */
524 return ERR_PTR(-ENOENT);
527 if (pack->package.count < 1) {
528 dev_err(dev, "GGRP[%#x] package is too small\n", mux);
530 return ERR_PTR(-EIO);
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;
549 tmp.type = ACPI_TYPE_BUFFER;
550 tmp.buffer.pointer = (u8 *)&buf;
551 tmp.buffer.length = sizeof(buf);
554 params.pointer = (void *)&tmp;
556 ret.length = ACPI_ALLOCATE_BUFFER;
557 status = acpi_evaluate_object_typed(data->read_handle, NULL, ¶ms,
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);
567 if (obj->buffer.length < 8) {
568 dev_warn(dev, "Unexpected ASBF length: %u\n",
571 return ERR_PTR(-EIO);
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;
586 tmp.type = ACPI_TYPE_BUFFER;
587 tmp.buffer.pointer = (u8 *)buf;
588 tmp.buffer.length = sizeof(*buf);
591 params.pointer = &tmp;
593 ret.length = ACPI_ALLOCATE_BUFFER;
594 status = acpi_evaluate_object_typed(data->write_handle, NULL, ¶ms,
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);
604 if (obj->buffer.length < 8) {
605 dev_warn(dev, "Unexpected ASBF length: %u\n",
608 return ERR_PTR(-EIO);
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;
621 obj = atk_gitm(data, sensor->id);
625 buf = (struct atk_acpi_ret_buffer *)obj->buffer.pointer;
626 if (buf->flags == 0) {
628 * The reading is not valid, possible causes:
630 * - enumeration was FUBAR (and we didn't notice)
632 dev_warn(dev, "Read failed, sensor = %#llx\n", sensor->id);
643 static int atk_read_value(struct atk_sensor_data *sensor, u64 *value)
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);
652 err = atk_read_value_new(sensor, value);
654 sensor->is_valid = true;
655 sensor->last_updated = jiffies;
656 sensor->cached_value = *value;
658 *value = sensor->cached_value;
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;
673 if (!data->read_handle)
676 if (!data->debugfs.id)
679 ret = atk_gitm(data, data->debugfs.id);
683 buf = (struct atk_acpi_ret_buffer *)ret->buffer.pointer;
693 DEFINE_SIMPLE_ATTRIBUTE(atk_debugfs_gitm,
694 atk_debugfs_gitm_get,
698 static int atk_acpi_print(char *buf, size_t sz, union acpi_object *obj)
703 case ACPI_TYPE_INTEGER:
704 ret = snprintf(buf, sz, "0x%08llx\n", obj->integer.value);
706 case ACPI_TYPE_STRING:
707 ret = snprintf(buf, sz, "%s\n", obj->string.pointer);
714 static void atk_pack_print(char *buf, size_t sz, union acpi_object *pack)
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);
730 static int atk_debugfs_ggrp_open(struct inode *inode, struct file *file)
732 struct atk_data *data = inode->i_private;
734 union acpi_object *ret;
738 if (!data->enumerate_handle)
740 if (!data->debugfs.id)
743 cls = (data->debugfs.id & 0xff000000) >> 24;
744 ret = atk_ggrp(data, cls);
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)
754 if (!pack->package.count)
756 id = &pack->package.elements[0];
757 if (id->integer.value == data->debugfs.id) {
758 /* Print the package */
759 buf = kzalloc(512, GFP_KERNEL);
764 atk_pack_print(buf, 512, pack);
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);
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,
800 static void atk_debugfs_init(struct atk_data *data)
805 data->debugfs.id = 0;
807 d = debugfs_create_dir("asus_atk0110", NULL);
811 f = debugfs_create_x32("id", S_IRUSR | S_IWUSR, d, &data->debugfs.id);
815 f = debugfs_create_file("gitm", S_IRUSR, d, data,
820 f = debugfs_create_file("ggrp", S_IRUSR, d, data,
821 &atk_debugfs_ggrp_fops);
825 data->debugfs.root = d;
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)
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;
865 if (obj->type != ACPI_TYPE_PACKAGE) {
867 dev_warn(dev, "Unknown type for ACPI object: (%d)\n",
872 err = validate_hwmon_pack(data, obj);
876 /* Ok, we have a valid hwmon package */
877 type = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS)->integer.value
881 case HWMON_TYPE_VOLT:
885 num = &data->voltage_count;
888 case HWMON_TYPE_TEMP:
891 limit2_name = "crit";
892 num = &data->temperature_count;
899 num = &data->fan_count;
903 dev_warn(dev, "Unknown sensor type: %#llx\n", type);
907 enable = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE);
908 if (!enable->integer.value)
909 /* sensor is disabled */
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);
921 sensor->acpi_name = kstrdup(name->string.pointer, GFP_KERNEL);
922 if (!sensor->acpi_name) {
927 INIT_LIST_HEAD(&sensor->list);
930 sensor->id = flags->integer.value;
931 sensor->limit1 = limit1->integer.value;
932 if (data->old_interface)
933 sensor->limit2 = limit2->integer.value;
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,
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,
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,
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,
962 list_add(&sensor->list, &data->sensor_list);
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;
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));
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);
999 ACPI_FREE(buf.pointer);
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));
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);
1021 ACPI_FREE(buf.pointer);
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));
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);
1043 ACPI_FREE(buf.pointer);
1047 atk_free_sensors(data);
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;
1059 pack = atk_ggrp(data, ATK_MUX_MGMT);
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);
1066 return PTR_ERR(pack);
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)
1078 id = &obj->package.elements[0];
1079 if (id->type != ACPI_TYPE_INTEGER)
1082 if (id->integer.value == ATK_EC_ID) {
1090 /* The system has no EC */
1091 dev_dbg(dev, "EC not found\n");
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;
1104 obj = atk_gitm(data, ATK_EC_ID);
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");
1115 err = (buf->value != 0);
1116 dev_dbg(dev, "EC is %sabled\n",
1117 err ? "en" : "dis");
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;
1132 sitm.id = ATK_EC_ID;
1133 sitm.param1 = enable;
1136 obj = atk_sitm(data, &sitm);
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");
1148 dev_info(dev, "EC %sabled\n",
1149 enable ? "en" : "dis");
1156 static int atk_enumerate_new_hwmon(struct atk_data *data)
1158 struct device *dev = &data->acpi_dev->dev;
1159 union acpi_object *pack;
1163 err = atk_ec_present(data);
1167 err = atk_ec_enabled(data);
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);
1175 data->disable_ec = false;
1180 dev_dbg(dev, "Enumerating hwmon sensors\n");
1182 pack = atk_ggrp(data, ATK_MUX_HWMON);
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;
1198 static int atk_create_files(struct atk_data *data)
1200 struct atk_sensor_data *s;
1203 list_for_each_entry(s, &data->sensor_list, list) {
1204 err = device_create_file(data->hwmon_dev, &s->input_attr);
1207 err = device_create_file(data->hwmon_dev, &s->label_attr);
1210 err = device_create_file(data->hwmon_dev, &s->limit1_attr);
1213 err = device_create_file(data->hwmon_dev, &s->limit2_attr);
1218 err = device_create_file(data->hwmon_dev, &atk_name_attr);
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);
1247 static int atk_register_hwmon(struct atk_data *data)
1249 struct device *dev = &data->acpi_dev->dev;
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);
1264 /* Cleanup the registered files */
1265 atk_remove_files(data);
1266 hwmon_device_unregister(data->hwmon_dev);
1270 static int atk_probe_if(struct atk_data *data)
1272 struct device *dev = &data->acpi_dev->dev;
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;
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;
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;
1298 dev_dbg(dev, "method " METHOD_OLD_READ_FAN " not found: %s\n",
1299 acpi_format_exception(status));
1302 status = acpi_get_handle(data->atk_handle, METHOD_ENUMERATE, &ret);
1303 if (ACPI_SUCCESS(status))
1304 data->enumerate_handle = ret;
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;
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;
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.
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 &&
1338 data->old_interface = false;
1345 static int atk_add(struct acpi_device *device)
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);
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);
1368 dev_dbg(&device->dev, "atk: method MBIF not found\n");
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);
1382 dev_err(&device->dev, "No usable hwmon interface detected\n");
1386 if (data->old_interface) {
1387 dev_dbg(&device->dev, "Using old hwmon interface\n");
1388 err = atk_enumerate_old_hwmon(data);
1390 dev_dbg(&device->dev, "Using new hwmon interface\n");
1391 err = atk_enumerate_new_hwmon(data);
1396 dev_info(&device->dev,
1397 "No usable sensor detected, bailing out\n");
1402 err = atk_register_hwmon(data);
1406 atk_debugfs_init(data);
1408 device->driver_data = data;
1411 atk_free_sensors(data);
1413 if (data->disable_ec)
1414 atk_ec_ctl(data, 0);
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");
1442 static int __init atk0110_init(void)
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");
1452 if (dmi_check_system(atk_force_new_if))
1455 ret = acpi_bus_register_driver(&atk_driver);
1457 pr_info("acpi_bus_register_driver failed: %d\n", 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");