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 #include <linux/debugfs.h>
9 #include <linux/kernel.h>
10 #include <linux/hwmon.h>
11 #include <linux/list.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
15 #include <acpi/acpi.h>
16 #include <acpi/acpixf.h>
17 #include <acpi/acpi_drivers.h>
18 #include <acpi/acpi_bus.h>
21 #define ATK_HID "ATK0110"
23 /* Minimum time between readings, enforced in order to avoid
28 #define BOARD_ID "MBIF"
29 #define METHOD_ENUMERATE "GGRP"
30 #define METHOD_READ "GITM"
31 #define METHOD_WRITE "SITM"
32 #define METHOD_OLD_READ_TMP "RTMP"
33 #define METHOD_OLD_READ_VLT "RVLT"
34 #define METHOD_OLD_READ_FAN "RFAN"
35 #define METHOD_OLD_ENUM_TMP "TSIF"
36 #define METHOD_OLD_ENUM_VLT "VSIF"
37 #define METHOD_OLD_ENUM_FAN "FSIF"
39 #define ATK_MUX_HWMON 0x00000006ULL
40 #define ATK_MUX_MGMT 0x00000011ULL
42 #define ATK_CLASS_MASK 0xff000000ULL
43 #define ATK_CLASS_FREQ_CTL 0x03000000ULL
44 #define ATK_CLASS_FAN_CTL 0x04000000ULL
45 #define ATK_CLASS_HWMON 0x06000000ULL
46 #define ATK_CLASS_MGMT 0x11000000ULL
48 #define ATK_TYPE_MASK 0x00ff0000ULL
49 #define HWMON_TYPE_VOLT 0x00020000ULL
50 #define HWMON_TYPE_TEMP 0x00030000ULL
51 #define HWMON_TYPE_FAN 0x00040000ULL
53 #define ATK_ELEMENT_ID_MASK 0x0000ffffULL
55 #define ATK_EC_ID 0x11060004ULL
57 enum atk_pack_member {
65 /* New package format */
66 #define _HWMON_NEW_PACK_SIZE 7
67 #define _HWMON_NEW_PACK_FLAGS 0
68 #define _HWMON_NEW_PACK_NAME 1
69 #define _HWMON_NEW_PACK_UNK1 2
70 #define _HWMON_NEW_PACK_UNK2 3
71 #define _HWMON_NEW_PACK_LIMIT1 4
72 #define _HWMON_NEW_PACK_LIMIT2 5
73 #define _HWMON_NEW_PACK_ENABLE 6
75 /* Old package format */
76 #define _HWMON_OLD_PACK_SIZE 5
77 #define _HWMON_OLD_PACK_FLAGS 0
78 #define _HWMON_OLD_PACK_NAME 1
79 #define _HWMON_OLD_PACK_LIMIT1 2
80 #define _HWMON_OLD_PACK_LIMIT2 3
81 #define _HWMON_OLD_PACK_ENABLE 4
85 struct device *hwmon_dev;
86 acpi_handle atk_handle;
87 struct acpi_device *acpi_dev;
92 acpi_handle rtmp_handle;
93 acpi_handle rvlt_handle;
94 acpi_handle rfan_handle;
96 acpi_handle enumerate_handle;
97 acpi_handle read_handle;
98 acpi_handle write_handle;
103 int temperature_count;
105 struct list_head sensor_list;
114 typedef ssize_t (*sysfs_show_func)(struct device *dev,
115 struct device_attribute *attr, char *buf);
117 static const struct acpi_device_id atk_ids[] = {
121 MODULE_DEVICE_TABLE(acpi, atk_ids);
123 #define ATTR_NAME_SIZE 16 /* Worst case is "tempN_input" */
125 struct atk_sensor_data {
126 struct list_head list;
127 struct atk_data *data;
128 struct device_attribute label_attr;
129 struct device_attribute input_attr;
130 struct device_attribute limit1_attr;
131 struct device_attribute limit2_attr;
132 char label_attr_name[ATTR_NAME_SIZE];
133 char input_attr_name[ATTR_NAME_SIZE];
134 char limit1_attr_name[ATTR_NAME_SIZE];
135 char limit2_attr_name[ATTR_NAME_SIZE];
141 unsigned long last_updated; /* in jiffies */
143 char const *acpi_name;
146 /* Return buffer format:
147 * [0-3] "value" is valid flag
149 * [8- ] unknown stuff on newer mobos
151 struct atk_acpi_ret_buffer {
157 /* Input buffer used for GITM and SITM methods */
158 struct atk_acpi_input_buf {
164 static int atk_add(struct acpi_device *device);
165 static int atk_remove(struct acpi_device *device, int type);
166 static void atk_print_sensor(struct atk_data *data, union acpi_object *obj);
167 static int atk_read_value(struct atk_sensor_data *sensor, u64 *value);
168 static void atk_free_sensors(struct atk_data *data);
170 static struct acpi_driver atk_driver = {
176 .remove = atk_remove,
180 #define input_to_atk_sensor(attr) \
181 container_of(attr, struct atk_sensor_data, input_attr)
183 #define label_to_atk_sensor(attr) \
184 container_of(attr, struct atk_sensor_data, label_attr)
186 #define limit1_to_atk_sensor(attr) \
187 container_of(attr, struct atk_sensor_data, limit1_attr)
189 #define limit2_to_atk_sensor(attr) \
190 container_of(attr, struct atk_sensor_data, limit2_attr)
192 static ssize_t atk_input_show(struct device *dev,
193 struct device_attribute *attr, char *buf)
195 struct atk_sensor_data *s = input_to_atk_sensor(attr);
199 err = atk_read_value(s, &value);
203 if (s->type == HWMON_TYPE_TEMP)
204 /* ACPI returns decidegree */
207 return sprintf(buf, "%llu\n", value);
210 static ssize_t atk_label_show(struct device *dev,
211 struct device_attribute *attr, char *buf)
213 struct atk_sensor_data *s = label_to_atk_sensor(attr);
215 return sprintf(buf, "%s\n", s->acpi_name);
218 static ssize_t atk_limit1_show(struct device *dev,
219 struct device_attribute *attr, char *buf)
221 struct atk_sensor_data *s = limit1_to_atk_sensor(attr);
222 u64 value = s->limit1;
224 if (s->type == HWMON_TYPE_TEMP)
227 return sprintf(buf, "%lld\n", value);
230 static ssize_t atk_limit2_show(struct device *dev,
231 struct device_attribute *attr, char *buf)
233 struct atk_sensor_data *s = limit2_to_atk_sensor(attr);
234 u64 value = s->limit2;
236 if (s->type == HWMON_TYPE_TEMP)
239 return sprintf(buf, "%lld\n", value);
242 static ssize_t atk_name_show(struct device *dev,
243 struct device_attribute *attr, char *buf)
245 return sprintf(buf, "atk0110\n");
247 static struct device_attribute atk_name_attr =
248 __ATTR(name, 0444, atk_name_show, NULL);
250 static void atk_init_attribute(struct device_attribute *attr, char *name,
251 sysfs_show_func show)
253 attr->attr.name = name;
254 attr->attr.mode = 0444;
260 static union acpi_object *atk_get_pack_member(struct atk_data *data,
261 union acpi_object *pack,
262 enum atk_pack_member m)
264 bool old_if = data->old_interface;
268 case HWMON_PACK_FLAGS:
269 offset = old_if ? _HWMON_OLD_PACK_FLAGS : _HWMON_NEW_PACK_FLAGS;
271 case HWMON_PACK_NAME:
272 offset = old_if ? _HWMON_OLD_PACK_NAME : _HWMON_NEW_PACK_NAME;
274 case HWMON_PACK_LIMIT1:
275 offset = old_if ? _HWMON_OLD_PACK_LIMIT1 :
276 _HWMON_NEW_PACK_LIMIT1;
278 case HWMON_PACK_LIMIT2:
279 offset = old_if ? _HWMON_OLD_PACK_LIMIT2 :
280 _HWMON_NEW_PACK_LIMIT2;
282 case HWMON_PACK_ENABLE:
283 offset = old_if ? _HWMON_OLD_PACK_ENABLE :
284 _HWMON_NEW_PACK_ENABLE;
290 return &pack->package.elements[offset];
294 /* New package format is:
296 * class - used for de-muxing the request to the correct GITn
297 * type (volt, temp, fan)
299 * sensor id - used for de-muxing the request _inside_ the GITn
307 * The old package has the same format but it's missing the two unknown fields.
309 static int validate_hwmon_pack(struct atk_data *data, union acpi_object *obj)
311 struct device *dev = &data->acpi_dev->dev;
312 union acpi_object *tmp;
313 bool old_if = data->old_interface;
314 int const expected_size = old_if ? _HWMON_OLD_PACK_SIZE :
315 _HWMON_NEW_PACK_SIZE;
317 if (obj->type != ACPI_TYPE_PACKAGE) {
318 dev_warn(dev, "Invalid type: %d\n", obj->type);
322 if (obj->package.count != expected_size) {
323 dev_warn(dev, "Invalid package size: %d, expected: %d\n",
324 obj->package.count, expected_size);
328 tmp = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS);
329 if (tmp->type != ACPI_TYPE_INTEGER) {
330 dev_warn(dev, "Invalid type (flag): %d\n", tmp->type);
334 tmp = atk_get_pack_member(data, obj, HWMON_PACK_NAME);
335 if (tmp->type != ACPI_TYPE_STRING) {
336 dev_warn(dev, "Invalid type (name): %d\n", tmp->type);
340 /* Don't check... we don't know what they're useful for anyway */
342 tmp = &obj->package.elements[HWMON_PACK_UNK1];
343 if (tmp->type != ACPI_TYPE_INTEGER) {
344 dev_warn(dev, "Invalid type (unk1): %d\n", tmp->type);
348 tmp = &obj->package.elements[HWMON_PACK_UNK2];
349 if (tmp->type != ACPI_TYPE_INTEGER) {
350 dev_warn(dev, "Invalid type (unk2): %d\n", tmp->type);
355 tmp = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT1);
356 if (tmp->type != ACPI_TYPE_INTEGER) {
357 dev_warn(dev, "Invalid type (limit1): %d\n", tmp->type);
361 tmp = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT2);
362 if (tmp->type != ACPI_TYPE_INTEGER) {
363 dev_warn(dev, "Invalid type (limit2): %d\n", tmp->type);
367 tmp = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE);
368 if (tmp->type != ACPI_TYPE_INTEGER) {
369 dev_warn(dev, "Invalid type (enable): %d\n", tmp->type);
373 atk_print_sensor(data, obj);
379 static char const *atk_sensor_type(union acpi_object *flags)
381 u64 type = flags->integer.value & ATK_TYPE_MASK;
385 case HWMON_TYPE_VOLT:
388 case HWMON_TYPE_TEMP:
389 what = "temperature";
403 static void atk_print_sensor(struct atk_data *data, union acpi_object *obj)
406 struct device *dev = &data->acpi_dev->dev;
407 union acpi_object *flags;
408 union acpi_object *name;
409 union acpi_object *limit1;
410 union acpi_object *limit2;
411 union acpi_object *enable;
414 flags = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS);
415 name = atk_get_pack_member(data, obj, HWMON_PACK_NAME);
416 limit1 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT1);
417 limit2 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT2);
418 enable = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE);
420 what = atk_sensor_type(flags);
422 dev_dbg(dev, "%s: %#llx %s [%llu-%llu] %s\n", what,
423 flags->integer.value,
424 name->string.pointer,
425 limit1->integer.value, limit2->integer.value,
426 enable->integer.value ? "enabled" : "disabled");
430 static int atk_read_value_old(struct atk_sensor_data *sensor, u64 *value)
432 struct atk_data *data = sensor->data;
433 struct device *dev = &data->acpi_dev->dev;
434 struct acpi_object_list params;
435 union acpi_object id;
439 switch (sensor->type) {
440 case HWMON_TYPE_VOLT:
441 method = data->rvlt_handle;
443 case HWMON_TYPE_TEMP:
444 method = data->rtmp_handle;
447 method = data->rfan_handle;
453 id.type = ACPI_TYPE_INTEGER;
454 id.integer.value = sensor->id;
457 params.pointer = &id;
459 status = acpi_evaluate_integer(method, NULL, ¶ms, value);
460 if (status != AE_OK) {
461 dev_warn(dev, "%s: ACPI exception: %s\n", __func__,
462 acpi_format_exception(status));
469 static union acpi_object *atk_ggrp(struct atk_data *data, u16 mux)
471 struct device *dev = &data->acpi_dev->dev;
472 struct acpi_buffer buf;
474 struct acpi_object_list params;
475 union acpi_object id;
476 union acpi_object *pack;
478 id.type = ACPI_TYPE_INTEGER;
479 id.integer.value = mux;
481 params.pointer = &id;
483 buf.length = ACPI_ALLOCATE_BUFFER;
484 ret = acpi_evaluate_object(data->enumerate_handle, NULL, ¶ms, &buf);
486 dev_err(dev, "GGRP[%#x] ACPI exception: %s\n", mux,
487 acpi_format_exception(ret));
488 return ERR_PTR(-EIO);
491 if (pack->type != ACPI_TYPE_PACKAGE) {
492 /* Execution was successful, but the id was not found */
494 return ERR_PTR(-ENOENT);
497 if (pack->package.count < 1) {
498 dev_err(dev, "GGRP[%#x] package is too small\n", mux);
500 return ERR_PTR(-EIO);
505 static union acpi_object *atk_gitm(struct atk_data *data, u64 id)
507 struct device *dev = &data->acpi_dev->dev;
508 struct atk_acpi_input_buf buf;
509 union acpi_object tmp;
510 struct acpi_object_list params;
511 struct acpi_buffer ret;
512 union acpi_object *obj;
519 tmp.type = ACPI_TYPE_BUFFER;
520 tmp.buffer.pointer = (u8 *)&buf;
521 tmp.buffer.length = sizeof(buf);
524 params.pointer = (void *)&tmp;
526 ret.length = ACPI_ALLOCATE_BUFFER;
527 status = acpi_evaluate_object_typed(data->read_handle, NULL, ¶ms,
528 &ret, ACPI_TYPE_BUFFER);
529 if (status != AE_OK) {
530 dev_warn(dev, "GITM[%#llx] ACPI exception: %s\n", id,
531 acpi_format_exception(status));
532 return ERR_PTR(-EIO);
537 if (obj->buffer.length < 8) {
538 dev_warn(dev, "Unexpected ASBF length: %u\n",
541 return ERR_PTR(-EIO);
546 static union acpi_object *atk_sitm(struct atk_data *data,
547 struct atk_acpi_input_buf *buf)
549 struct device *dev = &data->acpi_dev->dev;
550 struct acpi_object_list params;
551 union acpi_object tmp;
552 struct acpi_buffer ret;
553 union acpi_object *obj;
556 tmp.type = ACPI_TYPE_BUFFER;
557 tmp.buffer.pointer = (u8 *)buf;
558 tmp.buffer.length = sizeof(*buf);
561 params.pointer = &tmp;
563 ret.length = ACPI_ALLOCATE_BUFFER;
564 status = acpi_evaluate_object_typed(data->write_handle, NULL, ¶ms,
565 &ret, ACPI_TYPE_BUFFER);
566 if (status != AE_OK) {
567 dev_warn(dev, "SITM[%#x] ACPI exception: %s\n", buf->id,
568 acpi_format_exception(status));
569 return ERR_PTR(-EIO);
574 if (obj->buffer.length < 8) {
575 dev_warn(dev, "Unexpected ASBF length: %u\n",
578 return ERR_PTR(-EIO);
583 static int atk_read_value_new(struct atk_sensor_data *sensor, u64 *value)
585 struct atk_data *data = sensor->data;
586 struct device *dev = &data->acpi_dev->dev;
587 union acpi_object *obj;
588 struct atk_acpi_ret_buffer *buf;
591 obj = atk_gitm(data, sensor->id);
595 buf = (struct atk_acpi_ret_buffer *)obj->buffer.pointer;
596 if (buf->flags == 0) {
597 /* The reading is not valid, possible causes:
599 * - enumeration was FUBAR (and we didn't notice)
601 dev_warn(dev, "Read failed, sensor = %#llx\n", sensor->id);
612 static int atk_read_value(struct atk_sensor_data *sensor, u64 *value)
616 if (!sensor->is_valid ||
617 time_after(jiffies, sensor->last_updated + CACHE_TIME)) {
618 if (sensor->data->old_interface)
619 err = atk_read_value_old(sensor, value);
621 err = atk_read_value_new(sensor, value);
623 sensor->is_valid = true;
624 sensor->last_updated = jiffies;
625 sensor->cached_value = *value;
627 *value = sensor->cached_value;
634 #ifdef CONFIG_DEBUG_FS
635 static int atk_debugfs_gitm_get(void *p, u64 *val)
637 struct atk_data *data = p;
638 union acpi_object *ret;
639 struct atk_acpi_ret_buffer *buf;
642 if (!data->read_handle)
645 if (!data->debugfs.id)
648 ret = atk_gitm(data, data->debugfs.id);
652 buf = (struct atk_acpi_ret_buffer *)ret->buffer.pointer;
661 DEFINE_SIMPLE_ATTRIBUTE(atk_debugfs_gitm,
662 atk_debugfs_gitm_get,
666 static int atk_acpi_print(char *buf, size_t sz, union acpi_object *obj)
671 case ACPI_TYPE_INTEGER:
672 ret = snprintf(buf, sz, "0x%08llx\n", obj->integer.value);
674 case ACPI_TYPE_STRING:
675 ret = snprintf(buf, sz, "%s\n", obj->string.pointer);
682 static void atk_pack_print(char *buf, size_t sz, union acpi_object *pack)
687 for (i = 0; i < pack->package.count; i++) {
688 union acpi_object *obj = &pack->package.elements[i];
690 ret = atk_acpi_print(buf, sz, obj);
698 static int atk_debugfs_ggrp_open(struct inode *inode, struct file *file)
700 struct atk_data *data = inode->i_private;
702 union acpi_object *ret;
706 if (!data->enumerate_handle)
708 if (!data->debugfs.id)
711 cls = (data->debugfs.id & 0xff000000) >> 24;
712 ret = atk_ggrp(data, cls);
716 for (i = 0; i < ret->package.count; i++) {
717 union acpi_object *pack = &ret->package.elements[i];
718 union acpi_object *id;
720 if (pack->type != ACPI_TYPE_PACKAGE)
722 if (!pack->package.count)
724 id = &pack->package.elements[0];
725 if (id->integer.value == data->debugfs.id) {
726 /* Print the package */
727 buf = kzalloc(512, GFP_KERNEL);
732 atk_pack_print(buf, 512, pack);
741 file->private_data = buf;
743 return nonseekable_open(inode, file);
746 static ssize_t atk_debugfs_ggrp_read(struct file *file, char __user *buf,
747 size_t count, loff_t *pos)
749 char *str = file->private_data;
750 size_t len = strlen(str);
752 return simple_read_from_buffer(buf, count, pos, str, len);
755 static int atk_debugfs_ggrp_release(struct inode *inode, struct file *file)
757 kfree(file->private_data);
761 static const struct file_operations atk_debugfs_ggrp_fops = {
762 .read = atk_debugfs_ggrp_read,
763 .open = atk_debugfs_ggrp_open,
764 .release = atk_debugfs_ggrp_release,
767 static void atk_debugfs_init(struct atk_data *data)
772 data->debugfs.id = 0;
774 d = debugfs_create_dir("asus_atk0110", NULL);
778 f = debugfs_create_x32("id", S_IRUSR | S_IWUSR, d, &data->debugfs.id);
782 f = debugfs_create_file("gitm", S_IRUSR, d, data,
787 f = debugfs_create_file("ggrp", S_IRUSR, d, data,
788 &atk_debugfs_ggrp_fops);
792 data->debugfs.root = d;
796 debugfs_remove_recursive(d);
799 static void atk_debugfs_cleanup(struct atk_data *data)
801 debugfs_remove_recursive(data->debugfs.root);
804 #else /* CONFIG_DEBUG_FS */
806 static void atk_debugfs_init(struct atk_data *data)
810 static void atk_debugfs_cleanup(struct atk_data *data)
815 static int atk_add_sensor(struct atk_data *data, union acpi_object *obj)
817 struct device *dev = &data->acpi_dev->dev;
818 union acpi_object *flags;
819 union acpi_object *name;
820 union acpi_object *limit1;
821 union acpi_object *limit2;
822 union acpi_object *enable;
823 struct atk_sensor_data *sensor;
824 char const *base_name;
825 char const *limit1_name;
826 char const *limit2_name;
832 if (obj->type != ACPI_TYPE_PACKAGE) {
834 dev_warn(dev, "Unknown type for ACPI object: (%d)\n",
839 err = validate_hwmon_pack(data, obj);
843 /* Ok, we have a valid hwmon package */
844 type = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS)->integer.value
848 case HWMON_TYPE_VOLT:
852 num = &data->voltage_count;
855 case HWMON_TYPE_TEMP:
858 limit2_name = "crit";
859 num = &data->temperature_count;
866 num = &data->fan_count;
870 dev_warn(dev, "Unknown sensor type: %#llx\n", type);
874 enable = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE);
875 if (!enable->integer.value)
876 /* sensor is disabled */
879 flags = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS);
880 name = atk_get_pack_member(data, obj, HWMON_PACK_NAME);
881 limit1 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT1);
882 limit2 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT2);
884 sensor = kzalloc(sizeof(*sensor), GFP_KERNEL);
888 sensor->acpi_name = kstrdup(name->string.pointer, GFP_KERNEL);
889 if (!sensor->acpi_name) {
894 INIT_LIST_HEAD(&sensor->list);
897 sensor->id = flags->integer.value;
898 sensor->limit1 = limit1->integer.value;
899 if (data->old_interface)
900 sensor->limit2 = limit2->integer.value;
902 /* The upper limit is expressed as delta from lower limit */
903 sensor->limit2 = sensor->limit1 + limit2->integer.value;
905 snprintf(sensor->input_attr_name, ATTR_NAME_SIZE,
906 "%s%d_input", base_name, start + *num);
907 atk_init_attribute(&sensor->input_attr,
908 sensor->input_attr_name,
911 snprintf(sensor->label_attr_name, ATTR_NAME_SIZE,
912 "%s%d_label", base_name, start + *num);
913 atk_init_attribute(&sensor->label_attr,
914 sensor->label_attr_name,
917 snprintf(sensor->limit1_attr_name, ATTR_NAME_SIZE,
918 "%s%d_%s", base_name, start + *num, limit1_name);
919 atk_init_attribute(&sensor->limit1_attr,
920 sensor->limit1_attr_name,
923 snprintf(sensor->limit2_attr_name, ATTR_NAME_SIZE,
924 "%s%d_%s", base_name, start + *num, limit2_name);
925 atk_init_attribute(&sensor->limit2_attr,
926 sensor->limit2_attr_name,
929 list_add(&sensor->list, &data->sensor_list);
934 kfree(sensor->acpi_name);
939 static int atk_enumerate_old_hwmon(struct atk_data *data)
941 struct device *dev = &data->acpi_dev->dev;
942 struct acpi_buffer buf;
943 union acpi_object *pack;
949 buf.length = ACPI_ALLOCATE_BUFFER;
950 status = acpi_evaluate_object_typed(data->atk_handle,
951 METHOD_OLD_ENUM_VLT, NULL, &buf, ACPI_TYPE_PACKAGE);
952 if (status != AE_OK) {
953 dev_warn(dev, METHOD_OLD_ENUM_VLT ": ACPI exception: %s\n",
954 acpi_format_exception(status));
960 for (i = 1; i < pack->package.count; i++) {
961 union acpi_object *obj = &pack->package.elements[i];
963 ret = atk_add_sensor(data, obj);
967 ACPI_FREE(buf.pointer);
970 buf.length = ACPI_ALLOCATE_BUFFER;
971 status = acpi_evaluate_object_typed(data->atk_handle,
972 METHOD_OLD_ENUM_TMP, NULL, &buf, ACPI_TYPE_PACKAGE);
973 if (status != AE_OK) {
974 dev_warn(dev, METHOD_OLD_ENUM_TMP ": ACPI exception: %s\n",
975 acpi_format_exception(status));
982 for (i = 1; i < pack->package.count; i++) {
983 union acpi_object *obj = &pack->package.elements[i];
985 ret = atk_add_sensor(data, obj);
989 ACPI_FREE(buf.pointer);
992 buf.length = ACPI_ALLOCATE_BUFFER;
993 status = acpi_evaluate_object_typed(data->atk_handle,
994 METHOD_OLD_ENUM_FAN, NULL, &buf, ACPI_TYPE_PACKAGE);
995 if (status != AE_OK) {
996 dev_warn(dev, METHOD_OLD_ENUM_FAN ": ACPI exception: %s\n",
997 acpi_format_exception(status));
1004 for (i = 1; i < pack->package.count; i++) {
1005 union acpi_object *obj = &pack->package.elements[i];
1007 ret = atk_add_sensor(data, obj);
1011 ACPI_FREE(buf.pointer);
1015 atk_free_sensors(data);
1019 static int atk_ec_present(struct atk_data *data)
1021 struct device *dev = &data->acpi_dev->dev;
1022 union acpi_object *pack;
1023 union acpi_object *ec;
1027 pack = atk_ggrp(data, ATK_MUX_MGMT);
1029 if (PTR_ERR(pack) == -ENOENT) {
1030 /* The MGMT class does not exists - that's ok */
1031 dev_dbg(dev, "Class %#llx not found\n", ATK_MUX_MGMT);
1034 return PTR_ERR(pack);
1039 for (i = 0; i < pack->package.count; i++) {
1040 union acpi_object *obj = &pack->package.elements[i];
1041 union acpi_object *id;
1043 if (obj->type != ACPI_TYPE_PACKAGE)
1046 id = &obj->package.elements[0];
1047 if (id->type != ACPI_TYPE_INTEGER)
1050 if (id->integer.value == ATK_EC_ID) {
1058 /* The system has no EC */
1059 dev_dbg(dev, "EC not found\n");
1065 static int atk_ec_enabled(struct atk_data *data)
1067 struct device *dev = &data->acpi_dev->dev;
1068 union acpi_object *obj;
1069 struct atk_acpi_ret_buffer *buf;
1072 obj = atk_gitm(data, ATK_EC_ID);
1074 dev_err(dev, "Unable to query EC status\n");
1075 return PTR_ERR(obj);
1077 buf = (struct atk_acpi_ret_buffer *)obj->buffer.pointer;
1079 if (buf->flags == 0) {
1080 dev_err(dev, "Unable to query EC status\n");
1083 err = (buf->value != 0);
1084 dev_dbg(dev, "EC is %sabled\n",
1085 err ? "en" : "dis");
1092 static int atk_ec_ctl(struct atk_data *data, int enable)
1094 struct device *dev = &data->acpi_dev->dev;
1095 union acpi_object *obj;
1096 struct atk_acpi_input_buf sitm;
1097 struct atk_acpi_ret_buffer *ec_ret;
1100 sitm.id = ATK_EC_ID;
1101 sitm.param1 = enable;
1104 obj = atk_sitm(data, &sitm);
1106 dev_err(dev, "Failed to %sable the EC\n",
1107 enable ? "en" : "dis");
1108 return PTR_ERR(obj);
1110 ec_ret = (struct atk_acpi_ret_buffer *)obj->buffer.pointer;
1111 if (ec_ret->flags == 0) {
1112 dev_err(dev, "Failed to %sable the EC\n",
1113 enable ? "en" : "dis");
1116 dev_info(dev, "EC %sabled\n",
1117 enable ? "en" : "dis");
1124 static int atk_enumerate_new_hwmon(struct atk_data *data)
1126 struct device *dev = &data->acpi_dev->dev;
1127 union acpi_object *pack;
1131 err = atk_ec_present(data);
1135 err = atk_ec_enabled(data);
1138 /* If the EC was disabled we will disable it again on unload */
1139 data->disable_ec = err;
1141 err = atk_ec_ctl(data, 1);
1143 data->disable_ec = false;
1148 dev_dbg(dev, "Enumerating hwmon sensors\n");
1150 pack = atk_ggrp(data, ATK_MUX_HWMON);
1152 return PTR_ERR(pack);
1154 for (i = 0; i < pack->package.count; i++) {
1155 union acpi_object *obj = &pack->package.elements[i];
1157 atk_add_sensor(data, obj);
1160 err = data->voltage_count + data->temperature_count + data->fan_count;
1166 static int atk_create_files(struct atk_data *data)
1168 struct atk_sensor_data *s;
1171 list_for_each_entry(s, &data->sensor_list, list) {
1172 sysfs_attr_init(&s->input_attr.attr);
1173 err = device_create_file(data->hwmon_dev, &s->input_attr);
1176 sysfs_attr_init(&s->label_attr.attr);
1177 err = device_create_file(data->hwmon_dev, &s->label_attr);
1180 sysfs_attr_init(&s->limit1_attr.attr);
1181 err = device_create_file(data->hwmon_dev, &s->limit1_attr);
1184 sysfs_attr_init(&s->limit2_attr.attr);
1185 err = device_create_file(data->hwmon_dev, &s->limit2_attr);
1190 err = device_create_file(data->hwmon_dev, &atk_name_attr);
1195 static void atk_remove_files(struct atk_data *data)
1197 struct atk_sensor_data *s;
1199 list_for_each_entry(s, &data->sensor_list, list) {
1200 device_remove_file(data->hwmon_dev, &s->input_attr);
1201 device_remove_file(data->hwmon_dev, &s->label_attr);
1202 device_remove_file(data->hwmon_dev, &s->limit1_attr);
1203 device_remove_file(data->hwmon_dev, &s->limit2_attr);
1205 device_remove_file(data->hwmon_dev, &atk_name_attr);
1208 static void atk_free_sensors(struct atk_data *data)
1210 struct list_head *head = &data->sensor_list;
1211 struct atk_sensor_data *s, *tmp;
1213 list_for_each_entry_safe(s, tmp, head, list) {
1214 kfree(s->acpi_name);
1219 static int atk_register_hwmon(struct atk_data *data)
1221 struct device *dev = &data->acpi_dev->dev;
1224 dev_dbg(dev, "registering hwmon device\n");
1225 data->hwmon_dev = hwmon_device_register(dev);
1226 if (IS_ERR(data->hwmon_dev))
1227 return PTR_ERR(data->hwmon_dev);
1229 dev_dbg(dev, "populating sysfs directory\n");
1230 err = atk_create_files(data);
1236 /* Cleanup the registered files */
1237 atk_remove_files(data);
1238 hwmon_device_unregister(data->hwmon_dev);
1242 static int atk_probe_if(struct atk_data *data)
1244 struct device *dev = &data->acpi_dev->dev;
1249 /* RTMP: read temperature */
1250 status = acpi_get_handle(data->atk_handle, METHOD_OLD_READ_TMP, &ret);
1251 if (ACPI_SUCCESS(status))
1252 data->rtmp_handle = ret;
1254 dev_dbg(dev, "method " METHOD_OLD_READ_TMP " not found: %s\n",
1255 acpi_format_exception(status));
1257 /* RVLT: read voltage */
1258 status = acpi_get_handle(data->atk_handle, METHOD_OLD_READ_VLT, &ret);
1259 if (ACPI_SUCCESS(status))
1260 data->rvlt_handle = ret;
1262 dev_dbg(dev, "method " METHOD_OLD_READ_VLT " not found: %s\n",
1263 acpi_format_exception(status));
1265 /* RFAN: read fan status */
1266 status = acpi_get_handle(data->atk_handle, METHOD_OLD_READ_FAN, &ret);
1267 if (ACPI_SUCCESS(status))
1268 data->rfan_handle = ret;
1270 dev_dbg(dev, "method " METHOD_OLD_READ_FAN " not found: %s\n",
1271 acpi_format_exception(status));
1274 status = acpi_get_handle(data->atk_handle, METHOD_ENUMERATE, &ret);
1275 if (ACPI_SUCCESS(status))
1276 data->enumerate_handle = ret;
1278 dev_dbg(dev, "method " METHOD_ENUMERATE " not found: %s\n",
1279 acpi_format_exception(status));
1281 /* De-multiplexer (read) */
1282 status = acpi_get_handle(data->atk_handle, METHOD_READ, &ret);
1283 if (ACPI_SUCCESS(status))
1284 data->read_handle = ret;
1286 dev_dbg(dev, "method " METHOD_READ " not found: %s\n",
1287 acpi_format_exception(status));
1289 /* De-multiplexer (write) */
1290 status = acpi_get_handle(data->atk_handle, METHOD_WRITE, &ret);
1291 if (ACPI_SUCCESS(status))
1292 data->write_handle = ret;
1294 dev_dbg(dev, "method " METHOD_WRITE " not found: %s\n",
1295 acpi_format_exception(status));
1297 /* Check for hwmon methods: first check "old" style methods; note that
1298 * both may be present: in this case we stick to the old interface;
1299 * analysis of multiple DSDTs indicates that when both interfaces
1300 * are present the new one (GGRP/GITM) is not functional.
1302 if (data->rtmp_handle && data->rvlt_handle && data->rfan_handle)
1303 data->old_interface = true;
1304 else if (data->enumerate_handle && data->read_handle &&
1306 data->old_interface = false;
1313 static int atk_add(struct acpi_device *device)
1317 struct acpi_buffer buf;
1318 union acpi_object *obj;
1319 struct atk_data *data;
1321 dev_dbg(&device->dev, "adding...\n");
1323 data = kzalloc(sizeof(*data), GFP_KERNEL);
1327 data->acpi_dev = device;
1328 data->atk_handle = device->handle;
1329 INIT_LIST_HEAD(&data->sensor_list);
1330 data->disable_ec = false;
1332 buf.length = ACPI_ALLOCATE_BUFFER;
1333 ret = acpi_evaluate_object_typed(data->atk_handle, BOARD_ID, NULL,
1334 &buf, ACPI_TYPE_PACKAGE);
1336 dev_dbg(&device->dev, "atk: method MBIF not found\n");
1339 if (obj->package.count >= 2) {
1340 union acpi_object *id = &obj->package.elements[1];
1341 if (id->type == ACPI_TYPE_STRING)
1342 dev_dbg(&device->dev, "board ID = %s\n",
1343 id->string.pointer);
1345 ACPI_FREE(buf.pointer);
1348 err = atk_probe_if(data);
1350 dev_err(&device->dev, "No usable hwmon interface detected\n");
1354 if (data->old_interface) {
1355 dev_dbg(&device->dev, "Using old hwmon interface\n");
1356 err = atk_enumerate_old_hwmon(data);
1358 dev_dbg(&device->dev, "Using new hwmon interface\n");
1359 err = atk_enumerate_new_hwmon(data);
1364 dev_info(&device->dev,
1365 "No usable sensor detected, bailing out\n");
1370 err = atk_register_hwmon(data);
1374 atk_debugfs_init(data);
1376 device->driver_data = data;
1379 atk_free_sensors(data);
1381 if (data->disable_ec)
1382 atk_ec_ctl(data, 0);
1387 static int atk_remove(struct acpi_device *device, int type)
1389 struct atk_data *data = device->driver_data;
1390 dev_dbg(&device->dev, "removing...\n");
1392 device->driver_data = NULL;
1394 atk_debugfs_cleanup(data);
1396 atk_remove_files(data);
1397 atk_free_sensors(data);
1398 hwmon_device_unregister(data->hwmon_dev);
1400 if (data->disable_ec) {
1401 if (atk_ec_ctl(data, 0))
1402 dev_err(&device->dev, "Failed to disable EC\n");
1410 static int __init atk0110_init(void)
1414 ret = acpi_bus_register_driver(&atk_driver);
1416 pr_info("atk: acpi_bus_register_driver failed: %d\n", ret);
1421 static void __exit atk0110_exit(void)
1423 acpi_bus_unregister_driver(&atk_driver);
1426 module_init(atk0110_init);
1427 module_exit(atk0110_exit);
1429 MODULE_LICENSE("GPL");