net: lpc_eth: Fix rename of dev_hw_addr_random
[linux-2.6.git] / drivers / hwmon / asus_atk0110.c
blob351d1f4593e7a8276da3ff7cdd9b286d4ea2b097
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>
18 #include <acpi/acpi.h>
19 #include <acpi/acpixf.h>
20 #include <acpi/acpi_drivers.h>
21 #include <acpi/acpi_bus.h>
24 #define ATK_HID "ATK0110"
26 static bool new_if;
27 module_param(new_if, bool, 0);
28 MODULE_PARM_DESC(new_if, "Override detection heuristic and force the use of the new ATK0110 interface");
30 static const struct dmi_system_id __initconst atk_force_new_if[] = {
32 /* Old interface has broken MCH temp monitoring */
33 .ident = "Asus Sabertooth X58",
34 .matches = {
35 DMI_MATCH(DMI_BOARD_NAME, "SABERTOOTH X58")
38 { }
42 * Minimum time between readings, enforced in order to avoid
43 * hogging the CPU.
45 #define CACHE_TIME HZ
47 #define BOARD_ID "MBIF"
48 #define METHOD_ENUMERATE "GGRP"
49 #define METHOD_READ "GITM"
50 #define METHOD_WRITE "SITM"
51 #define METHOD_OLD_READ_TMP "RTMP"
52 #define METHOD_OLD_READ_VLT "RVLT"
53 #define METHOD_OLD_READ_FAN "RFAN"
54 #define METHOD_OLD_ENUM_TMP "TSIF"
55 #define METHOD_OLD_ENUM_VLT "VSIF"
56 #define METHOD_OLD_ENUM_FAN "FSIF"
58 #define ATK_MUX_HWMON 0x00000006ULL
59 #define ATK_MUX_MGMT 0x00000011ULL
61 #define ATK_CLASS_MASK 0xff000000ULL
62 #define ATK_CLASS_FREQ_CTL 0x03000000ULL
63 #define ATK_CLASS_FAN_CTL 0x04000000ULL
64 #define ATK_CLASS_HWMON 0x06000000ULL
65 #define ATK_CLASS_MGMT 0x11000000ULL
67 #define ATK_TYPE_MASK 0x00ff0000ULL
68 #define HWMON_TYPE_VOLT 0x00020000ULL
69 #define HWMON_TYPE_TEMP 0x00030000ULL
70 #define HWMON_TYPE_FAN 0x00040000ULL
72 #define ATK_ELEMENT_ID_MASK 0x0000ffffULL
74 #define ATK_EC_ID 0x11060004ULL
76 enum atk_pack_member {
77 HWMON_PACK_FLAGS,
78 HWMON_PACK_NAME,
79 HWMON_PACK_LIMIT1,
80 HWMON_PACK_LIMIT2,
81 HWMON_PACK_ENABLE
84 /* New package format */
85 #define _HWMON_NEW_PACK_SIZE 7
86 #define _HWMON_NEW_PACK_FLAGS 0
87 #define _HWMON_NEW_PACK_NAME 1
88 #define _HWMON_NEW_PACK_UNK1 2
89 #define _HWMON_NEW_PACK_UNK2 3
90 #define _HWMON_NEW_PACK_LIMIT1 4
91 #define _HWMON_NEW_PACK_LIMIT2 5
92 #define _HWMON_NEW_PACK_ENABLE 6
94 /* Old package format */
95 #define _HWMON_OLD_PACK_SIZE 5
96 #define _HWMON_OLD_PACK_FLAGS 0
97 #define _HWMON_OLD_PACK_NAME 1
98 #define _HWMON_OLD_PACK_LIMIT1 2
99 #define _HWMON_OLD_PACK_LIMIT2 3
100 #define _HWMON_OLD_PACK_ENABLE 4
103 struct atk_data {
104 struct device *hwmon_dev;
105 acpi_handle atk_handle;
106 struct acpi_device *acpi_dev;
108 bool old_interface;
110 /* old interface */
111 acpi_handle rtmp_handle;
112 acpi_handle rvlt_handle;
113 acpi_handle rfan_handle;
114 /* new inteface */
115 acpi_handle enumerate_handle;
116 acpi_handle read_handle;
117 acpi_handle write_handle;
119 bool disable_ec;
121 int voltage_count;
122 int temperature_count;
123 int fan_count;
124 struct list_head sensor_list;
126 struct {
127 struct dentry *root;
128 u32 id;
129 } debugfs;
133 typedef ssize_t (*sysfs_show_func)(struct device *dev,
134 struct device_attribute *attr, char *buf);
136 static const struct acpi_device_id atk_ids[] = {
137 {ATK_HID, 0},
138 {"", 0},
140 MODULE_DEVICE_TABLE(acpi, atk_ids);
142 #define ATTR_NAME_SIZE 16 /* Worst case is "tempN_input" */
144 struct atk_sensor_data {
145 struct list_head list;
146 struct atk_data *data;
147 struct device_attribute label_attr;
148 struct device_attribute input_attr;
149 struct device_attribute limit1_attr;
150 struct device_attribute limit2_attr;
151 char label_attr_name[ATTR_NAME_SIZE];
152 char input_attr_name[ATTR_NAME_SIZE];
153 char limit1_attr_name[ATTR_NAME_SIZE];
154 char limit2_attr_name[ATTR_NAME_SIZE];
155 u64 id;
156 u64 type;
157 u64 limit1;
158 u64 limit2;
159 u64 cached_value;
160 unsigned long last_updated; /* in jiffies */
161 bool is_valid;
162 char const *acpi_name;
166 * Return buffer format:
167 * [0-3] "value" is valid flag
168 * [4-7] value
169 * [8- ] unknown stuff on newer mobos
171 struct atk_acpi_ret_buffer {
172 u32 flags;
173 u32 value;
174 u8 data[];
177 /* Input buffer used for GITM and SITM methods */
178 struct atk_acpi_input_buf {
179 u32 id;
180 u32 param1;
181 u32 param2;
184 static int atk_add(struct acpi_device *device);
185 static int atk_remove(struct acpi_device *device, int type);
186 static void atk_print_sensor(struct atk_data *data, union acpi_object *obj);
187 static int atk_read_value(struct atk_sensor_data *sensor, u64 *value);
188 static void atk_free_sensors(struct atk_data *data);
190 static struct acpi_driver atk_driver = {
191 .name = ATK_HID,
192 .class = "hwmon",
193 .ids = atk_ids,
194 .ops = {
195 .add = atk_add,
196 .remove = atk_remove,
200 #define input_to_atk_sensor(attr) \
201 container_of(attr, struct atk_sensor_data, input_attr)
203 #define label_to_atk_sensor(attr) \
204 container_of(attr, struct atk_sensor_data, label_attr)
206 #define limit1_to_atk_sensor(attr) \
207 container_of(attr, struct atk_sensor_data, limit1_attr)
209 #define limit2_to_atk_sensor(attr) \
210 container_of(attr, struct atk_sensor_data, limit2_attr)
212 static ssize_t atk_input_show(struct device *dev,
213 struct device_attribute *attr, char *buf)
215 struct atk_sensor_data *s = input_to_atk_sensor(attr);
216 u64 value;
217 int err;
219 err = atk_read_value(s, &value);
220 if (err)
221 return err;
223 if (s->type == HWMON_TYPE_TEMP)
224 /* ACPI returns decidegree */
225 value *= 100;
227 return sprintf(buf, "%llu\n", value);
230 static ssize_t atk_label_show(struct device *dev,
231 struct device_attribute *attr, char *buf)
233 struct atk_sensor_data *s = label_to_atk_sensor(attr);
235 return sprintf(buf, "%s\n", s->acpi_name);
238 static ssize_t atk_limit1_show(struct device *dev,
239 struct device_attribute *attr, char *buf)
241 struct atk_sensor_data *s = limit1_to_atk_sensor(attr);
242 u64 value = s->limit1;
244 if (s->type == HWMON_TYPE_TEMP)
245 value *= 100;
247 return sprintf(buf, "%lld\n", value);
250 static ssize_t atk_limit2_show(struct device *dev,
251 struct device_attribute *attr, char *buf)
253 struct atk_sensor_data *s = limit2_to_atk_sensor(attr);
254 u64 value = s->limit2;
256 if (s->type == HWMON_TYPE_TEMP)
257 value *= 100;
259 return sprintf(buf, "%lld\n", value);
262 static ssize_t atk_name_show(struct device *dev,
263 struct device_attribute *attr, char *buf)
265 return sprintf(buf, "atk0110\n");
267 static struct device_attribute atk_name_attr =
268 __ATTR(name, 0444, atk_name_show, NULL);
270 static void atk_init_attribute(struct device_attribute *attr, char *name,
271 sysfs_show_func show)
273 sysfs_attr_init(&attr->attr);
274 attr->attr.name = name;
275 attr->attr.mode = 0444;
276 attr->show = show;
277 attr->store = NULL;
281 static union acpi_object *atk_get_pack_member(struct atk_data *data,
282 union acpi_object *pack,
283 enum atk_pack_member m)
285 bool old_if = data->old_interface;
286 int offset;
288 switch (m) {
289 case HWMON_PACK_FLAGS:
290 offset = old_if ? _HWMON_OLD_PACK_FLAGS : _HWMON_NEW_PACK_FLAGS;
291 break;
292 case HWMON_PACK_NAME:
293 offset = old_if ? _HWMON_OLD_PACK_NAME : _HWMON_NEW_PACK_NAME;
294 break;
295 case HWMON_PACK_LIMIT1:
296 offset = old_if ? _HWMON_OLD_PACK_LIMIT1 :
297 _HWMON_NEW_PACK_LIMIT1;
298 break;
299 case HWMON_PACK_LIMIT2:
300 offset = old_if ? _HWMON_OLD_PACK_LIMIT2 :
301 _HWMON_NEW_PACK_LIMIT2;
302 break;
303 case HWMON_PACK_ENABLE:
304 offset = old_if ? _HWMON_OLD_PACK_ENABLE :
305 _HWMON_NEW_PACK_ENABLE;
306 break;
307 default:
308 return NULL;
311 return &pack->package.elements[offset];
316 * New package format is:
317 * - flag (int)
318 * class - used for de-muxing the request to the correct GITn
319 * type (volt, temp, fan)
320 * sensor id |
321 * sensor id - used for de-muxing the request _inside_ the GITn
322 * - name (str)
323 * - unknown (int)
324 * - unknown (int)
325 * - limit1 (int)
326 * - limit2 (int)
327 * - enable (int)
329 * The old package has the same format but it's missing the two unknown fields.
331 static int validate_hwmon_pack(struct atk_data *data, union acpi_object *obj)
333 struct device *dev = &data->acpi_dev->dev;
334 union acpi_object *tmp;
335 bool old_if = data->old_interface;
336 int const expected_size = old_if ? _HWMON_OLD_PACK_SIZE :
337 _HWMON_NEW_PACK_SIZE;
339 if (obj->type != ACPI_TYPE_PACKAGE) {
340 dev_warn(dev, "Invalid type: %d\n", obj->type);
341 return -EINVAL;
344 if (obj->package.count != expected_size) {
345 dev_warn(dev, "Invalid package size: %d, expected: %d\n",
346 obj->package.count, expected_size);
347 return -EINVAL;
350 tmp = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS);
351 if (tmp->type != ACPI_TYPE_INTEGER) {
352 dev_warn(dev, "Invalid type (flag): %d\n", tmp->type);
353 return -EINVAL;
356 tmp = atk_get_pack_member(data, obj, HWMON_PACK_NAME);
357 if (tmp->type != ACPI_TYPE_STRING) {
358 dev_warn(dev, "Invalid type (name): %d\n", tmp->type);
359 return -EINVAL;
362 /* Don't check... we don't know what they're useful for anyway */
363 #if 0
364 tmp = &obj->package.elements[HWMON_PACK_UNK1];
365 if (tmp->type != ACPI_TYPE_INTEGER) {
366 dev_warn(dev, "Invalid type (unk1): %d\n", tmp->type);
367 return -EINVAL;
370 tmp = &obj->package.elements[HWMON_PACK_UNK2];
371 if (tmp->type != ACPI_TYPE_INTEGER) {
372 dev_warn(dev, "Invalid type (unk2): %d\n", tmp->type);
373 return -EINVAL;
375 #endif
377 tmp = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT1);
378 if (tmp->type != ACPI_TYPE_INTEGER) {
379 dev_warn(dev, "Invalid type (limit1): %d\n", tmp->type);
380 return -EINVAL;
383 tmp = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT2);
384 if (tmp->type != ACPI_TYPE_INTEGER) {
385 dev_warn(dev, "Invalid type (limit2): %d\n", tmp->type);
386 return -EINVAL;
389 tmp = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE);
390 if (tmp->type != ACPI_TYPE_INTEGER) {
391 dev_warn(dev, "Invalid type (enable): %d\n", tmp->type);
392 return -EINVAL;
395 atk_print_sensor(data, obj);
397 return 0;
400 #ifdef DEBUG
401 static char const *atk_sensor_type(union acpi_object *flags)
403 u64 type = flags->integer.value & ATK_TYPE_MASK;
404 char const *what;
406 switch (type) {
407 case HWMON_TYPE_VOLT:
408 what = "voltage";
409 break;
410 case HWMON_TYPE_TEMP:
411 what = "temperature";
412 break;
413 case HWMON_TYPE_FAN:
414 what = "fan";
415 break;
416 default:
417 what = "unknown";
418 break;
421 return what;
423 #endif
425 static void atk_print_sensor(struct atk_data *data, union acpi_object *obj)
427 #ifdef DEBUG
428 struct device *dev = &data->acpi_dev->dev;
429 union acpi_object *flags;
430 union acpi_object *name;
431 union acpi_object *limit1;
432 union acpi_object *limit2;
433 union acpi_object *enable;
434 char const *what;
436 flags = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS);
437 name = atk_get_pack_member(data, obj, HWMON_PACK_NAME);
438 limit1 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT1);
439 limit2 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT2);
440 enable = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE);
442 what = atk_sensor_type(flags);
444 dev_dbg(dev, "%s: %#llx %s [%llu-%llu] %s\n", what,
445 flags->integer.value,
446 name->string.pointer,
447 limit1->integer.value, limit2->integer.value,
448 enable->integer.value ? "enabled" : "disabled");
449 #endif
452 static int atk_read_value_old(struct atk_sensor_data *sensor, u64 *value)
454 struct atk_data *data = sensor->data;
455 struct device *dev = &data->acpi_dev->dev;
456 struct acpi_object_list params;
457 union acpi_object id;
458 acpi_status status;
459 acpi_handle method;
461 switch (sensor->type) {
462 case HWMON_TYPE_VOLT:
463 method = data->rvlt_handle;
464 break;
465 case HWMON_TYPE_TEMP:
466 method = data->rtmp_handle;
467 break;
468 case HWMON_TYPE_FAN:
469 method = data->rfan_handle;
470 break;
471 default:
472 return -EINVAL;
475 id.type = ACPI_TYPE_INTEGER;
476 id.integer.value = sensor->id;
478 params.count = 1;
479 params.pointer = &id;
481 status = acpi_evaluate_integer(method, NULL, &params, value);
482 if (status != AE_OK) {
483 dev_warn(dev, "%s: ACPI exception: %s\n", __func__,
484 acpi_format_exception(status));
485 return -EIO;
488 return 0;
491 static union acpi_object *atk_ggrp(struct atk_data *data, u16 mux)
493 struct device *dev = &data->acpi_dev->dev;
494 struct acpi_buffer buf;
495 acpi_status ret;
496 struct acpi_object_list params;
497 union acpi_object id;
498 union acpi_object *pack;
500 id.type = ACPI_TYPE_INTEGER;
501 id.integer.value = mux;
502 params.count = 1;
503 params.pointer = &id;
505 buf.length = ACPI_ALLOCATE_BUFFER;
506 ret = acpi_evaluate_object(data->enumerate_handle, NULL, &params, &buf);
507 if (ret != AE_OK) {
508 dev_err(dev, "GGRP[%#x] ACPI exception: %s\n", mux,
509 acpi_format_exception(ret));
510 return ERR_PTR(-EIO);
512 pack = buf.pointer;
513 if (pack->type != ACPI_TYPE_PACKAGE) {
514 /* Execution was successful, but the id was not found */
515 ACPI_FREE(pack);
516 return ERR_PTR(-ENOENT);
519 if (pack->package.count < 1) {
520 dev_err(dev, "GGRP[%#x] package is too small\n", mux);
521 ACPI_FREE(pack);
522 return ERR_PTR(-EIO);
524 return pack;
527 static union acpi_object *atk_gitm(struct atk_data *data, u64 id)
529 struct device *dev = &data->acpi_dev->dev;
530 struct atk_acpi_input_buf buf;
531 union acpi_object tmp;
532 struct acpi_object_list params;
533 struct acpi_buffer ret;
534 union acpi_object *obj;
535 acpi_status status;
537 buf.id = id;
538 buf.param1 = 0;
539 buf.param2 = 0;
541 tmp.type = ACPI_TYPE_BUFFER;
542 tmp.buffer.pointer = (u8 *)&buf;
543 tmp.buffer.length = sizeof(buf);
545 params.count = 1;
546 params.pointer = (void *)&tmp;
548 ret.length = ACPI_ALLOCATE_BUFFER;
549 status = acpi_evaluate_object_typed(data->read_handle, NULL, &params,
550 &ret, ACPI_TYPE_BUFFER);
551 if (status != AE_OK) {
552 dev_warn(dev, "GITM[%#llx] ACPI exception: %s\n", id,
553 acpi_format_exception(status));
554 return ERR_PTR(-EIO);
556 obj = ret.pointer;
558 /* Sanity check */
559 if (obj->buffer.length < 8) {
560 dev_warn(dev, "Unexpected ASBF length: %u\n",
561 obj->buffer.length);
562 ACPI_FREE(obj);
563 return ERR_PTR(-EIO);
565 return obj;
568 static union acpi_object *atk_sitm(struct atk_data *data,
569 struct atk_acpi_input_buf *buf)
571 struct device *dev = &data->acpi_dev->dev;
572 struct acpi_object_list params;
573 union acpi_object tmp;
574 struct acpi_buffer ret;
575 union acpi_object *obj;
576 acpi_status status;
578 tmp.type = ACPI_TYPE_BUFFER;
579 tmp.buffer.pointer = (u8 *)buf;
580 tmp.buffer.length = sizeof(*buf);
582 params.count = 1;
583 params.pointer = &tmp;
585 ret.length = ACPI_ALLOCATE_BUFFER;
586 status = acpi_evaluate_object_typed(data->write_handle, NULL, &params,
587 &ret, ACPI_TYPE_BUFFER);
588 if (status != AE_OK) {
589 dev_warn(dev, "SITM[%#x] ACPI exception: %s\n", buf->id,
590 acpi_format_exception(status));
591 return ERR_PTR(-EIO);
593 obj = ret.pointer;
595 /* Sanity check */
596 if (obj->buffer.length < 8) {
597 dev_warn(dev, "Unexpected ASBF length: %u\n",
598 obj->buffer.length);
599 ACPI_FREE(obj);
600 return ERR_PTR(-EIO);
602 return obj;
605 static int atk_read_value_new(struct atk_sensor_data *sensor, u64 *value)
607 struct atk_data *data = sensor->data;
608 struct device *dev = &data->acpi_dev->dev;
609 union acpi_object *obj;
610 struct atk_acpi_ret_buffer *buf;
611 int err = 0;
613 obj = atk_gitm(data, sensor->id);
614 if (IS_ERR(obj))
615 return PTR_ERR(obj);
617 buf = (struct atk_acpi_ret_buffer *)obj->buffer.pointer;
618 if (buf->flags == 0) {
620 * The reading is not valid, possible causes:
621 * - sensor failure
622 * - enumeration was FUBAR (and we didn't notice)
624 dev_warn(dev, "Read failed, sensor = %#llx\n", sensor->id);
625 err = -EIO;
626 goto out;
629 *value = buf->value;
630 out:
631 ACPI_FREE(obj);
632 return err;
635 static int atk_read_value(struct atk_sensor_data *sensor, u64 *value)
637 int err;
639 if (!sensor->is_valid ||
640 time_after(jiffies, sensor->last_updated + CACHE_TIME)) {
641 if (sensor->data->old_interface)
642 err = atk_read_value_old(sensor, value);
643 else
644 err = atk_read_value_new(sensor, value);
646 sensor->is_valid = true;
647 sensor->last_updated = jiffies;
648 sensor->cached_value = *value;
649 } else {
650 *value = sensor->cached_value;
651 err = 0;
654 return err;
657 #ifdef CONFIG_DEBUG_FS
658 static int atk_debugfs_gitm_get(void *p, u64 *val)
660 struct atk_data *data = p;
661 union acpi_object *ret;
662 struct atk_acpi_ret_buffer *buf;
663 int err = 0;
665 if (!data->read_handle)
666 return -ENODEV;
668 if (!data->debugfs.id)
669 return -EINVAL;
671 ret = atk_gitm(data, data->debugfs.id);
672 if (IS_ERR(ret))
673 return PTR_ERR(ret);
675 buf = (struct atk_acpi_ret_buffer *)ret->buffer.pointer;
676 if (buf->flags)
677 *val = buf->value;
678 else
679 err = -EIO;
681 ACPI_FREE(ret);
682 return err;
685 DEFINE_SIMPLE_ATTRIBUTE(atk_debugfs_gitm,
686 atk_debugfs_gitm_get,
687 NULL,
688 "0x%08llx\n")
690 static int atk_acpi_print(char *buf, size_t sz, union acpi_object *obj)
692 int ret = 0;
694 switch (obj->type) {
695 case ACPI_TYPE_INTEGER:
696 ret = snprintf(buf, sz, "0x%08llx\n", obj->integer.value);
697 break;
698 case ACPI_TYPE_STRING:
699 ret = snprintf(buf, sz, "%s\n", obj->string.pointer);
700 break;
703 return ret;
706 static void atk_pack_print(char *buf, size_t sz, union acpi_object *pack)
708 int ret;
709 int i;
711 for (i = 0; i < pack->package.count; i++) {
712 union acpi_object *obj = &pack->package.elements[i];
714 ret = atk_acpi_print(buf, sz, obj);
715 if (ret >= sz)
716 break;
717 buf += ret;
718 sz -= ret;
722 static int atk_debugfs_ggrp_open(struct inode *inode, struct file *file)
724 struct atk_data *data = inode->i_private;
725 char *buf = NULL;
726 union acpi_object *ret;
727 u8 cls;
728 int i;
730 if (!data->enumerate_handle)
731 return -ENODEV;
732 if (!data->debugfs.id)
733 return -EINVAL;
735 cls = (data->debugfs.id & 0xff000000) >> 24;
736 ret = atk_ggrp(data, cls);
737 if (IS_ERR(ret))
738 return PTR_ERR(ret);
740 for (i = 0; i < ret->package.count; i++) {
741 union acpi_object *pack = &ret->package.elements[i];
742 union acpi_object *id;
744 if (pack->type != ACPI_TYPE_PACKAGE)
745 continue;
746 if (!pack->package.count)
747 continue;
748 id = &pack->package.elements[0];
749 if (id->integer.value == data->debugfs.id) {
750 /* Print the package */
751 buf = kzalloc(512, GFP_KERNEL);
752 if (!buf) {
753 ACPI_FREE(ret);
754 return -ENOMEM;
756 atk_pack_print(buf, 512, pack);
757 break;
760 ACPI_FREE(ret);
762 if (!buf)
763 return -EINVAL;
765 file->private_data = buf;
767 return nonseekable_open(inode, file);
770 static ssize_t atk_debugfs_ggrp_read(struct file *file, char __user *buf,
771 size_t count, loff_t *pos)
773 char *str = file->private_data;
774 size_t len = strlen(str);
776 return simple_read_from_buffer(buf, count, pos, str, len);
779 static int atk_debugfs_ggrp_release(struct inode *inode, struct file *file)
781 kfree(file->private_data);
782 return 0;
785 static const struct file_operations atk_debugfs_ggrp_fops = {
786 .read = atk_debugfs_ggrp_read,
787 .open = atk_debugfs_ggrp_open,
788 .release = atk_debugfs_ggrp_release,
789 .llseek = no_llseek,
792 static void atk_debugfs_init(struct atk_data *data)
794 struct dentry *d;
795 struct dentry *f;
797 data->debugfs.id = 0;
799 d = debugfs_create_dir("asus_atk0110", NULL);
800 if (!d || IS_ERR(d))
801 return;
803 f = debugfs_create_x32("id", S_IRUSR | S_IWUSR, d, &data->debugfs.id);
804 if (!f || IS_ERR(f))
805 goto cleanup;
807 f = debugfs_create_file("gitm", S_IRUSR, d, data,
808 &atk_debugfs_gitm);
809 if (!f || IS_ERR(f))
810 goto cleanup;
812 f = debugfs_create_file("ggrp", S_IRUSR, d, data,
813 &atk_debugfs_ggrp_fops);
814 if (!f || IS_ERR(f))
815 goto cleanup;
817 data->debugfs.root = d;
819 return;
820 cleanup:
821 debugfs_remove_recursive(d);
824 static void atk_debugfs_cleanup(struct atk_data *data)
826 debugfs_remove_recursive(data->debugfs.root);
829 #else /* CONFIG_DEBUG_FS */
831 static void atk_debugfs_init(struct atk_data *data)
835 static void atk_debugfs_cleanup(struct atk_data *data)
838 #endif
840 static int atk_add_sensor(struct atk_data *data, union acpi_object *obj)
842 struct device *dev = &data->acpi_dev->dev;
843 union acpi_object *flags;
844 union acpi_object *name;
845 union acpi_object *limit1;
846 union acpi_object *limit2;
847 union acpi_object *enable;
848 struct atk_sensor_data *sensor;
849 char const *base_name;
850 char const *limit1_name;
851 char const *limit2_name;
852 u64 type;
853 int err;
854 int *num;
855 int start;
857 if (obj->type != ACPI_TYPE_PACKAGE) {
858 /* wft is this? */
859 dev_warn(dev, "Unknown type for ACPI object: (%d)\n",
860 obj->type);
861 return -EINVAL;
864 err = validate_hwmon_pack(data, obj);
865 if (err)
866 return err;
868 /* Ok, we have a valid hwmon package */
869 type = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS)->integer.value
870 & ATK_TYPE_MASK;
872 switch (type) {
873 case HWMON_TYPE_VOLT:
874 base_name = "in";
875 limit1_name = "min";
876 limit2_name = "max";
877 num = &data->voltage_count;
878 start = 0;
879 break;
880 case HWMON_TYPE_TEMP:
881 base_name = "temp";
882 limit1_name = "max";
883 limit2_name = "crit";
884 num = &data->temperature_count;
885 start = 1;
886 break;
887 case HWMON_TYPE_FAN:
888 base_name = "fan";
889 limit1_name = "min";
890 limit2_name = "max";
891 num = &data->fan_count;
892 start = 1;
893 break;
894 default:
895 dev_warn(dev, "Unknown sensor type: %#llx\n", type);
896 return -EINVAL;
899 enable = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE);
900 if (!enable->integer.value)
901 /* sensor is disabled */
902 return 0;
904 flags = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS);
905 name = atk_get_pack_member(data, obj, HWMON_PACK_NAME);
906 limit1 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT1);
907 limit2 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT2);
909 sensor = kzalloc(sizeof(*sensor), GFP_KERNEL);
910 if (!sensor)
911 return -ENOMEM;
913 sensor->acpi_name = kstrdup(name->string.pointer, GFP_KERNEL);
914 if (!sensor->acpi_name) {
915 err = -ENOMEM;
916 goto out;
919 INIT_LIST_HEAD(&sensor->list);
920 sensor->type = type;
921 sensor->data = data;
922 sensor->id = flags->integer.value;
923 sensor->limit1 = limit1->integer.value;
924 if (data->old_interface)
925 sensor->limit2 = limit2->integer.value;
926 else
927 /* The upper limit is expressed as delta from lower limit */
928 sensor->limit2 = sensor->limit1 + limit2->integer.value;
930 snprintf(sensor->input_attr_name, ATTR_NAME_SIZE,
931 "%s%d_input", base_name, start + *num);
932 atk_init_attribute(&sensor->input_attr,
933 sensor->input_attr_name,
934 atk_input_show);
936 snprintf(sensor->label_attr_name, ATTR_NAME_SIZE,
937 "%s%d_label", base_name, start + *num);
938 atk_init_attribute(&sensor->label_attr,
939 sensor->label_attr_name,
940 atk_label_show);
942 snprintf(sensor->limit1_attr_name, ATTR_NAME_SIZE,
943 "%s%d_%s", base_name, start + *num, limit1_name);
944 atk_init_attribute(&sensor->limit1_attr,
945 sensor->limit1_attr_name,
946 atk_limit1_show);
948 snprintf(sensor->limit2_attr_name, ATTR_NAME_SIZE,
949 "%s%d_%s", base_name, start + *num, limit2_name);
950 atk_init_attribute(&sensor->limit2_attr,
951 sensor->limit2_attr_name,
952 atk_limit2_show);
954 list_add(&sensor->list, &data->sensor_list);
955 (*num)++;
957 return 1;
958 out:
959 kfree(sensor->acpi_name);
960 kfree(sensor);
961 return err;
964 static int atk_enumerate_old_hwmon(struct atk_data *data)
966 struct device *dev = &data->acpi_dev->dev;
967 struct acpi_buffer buf;
968 union acpi_object *pack;
969 acpi_status status;
970 int i, ret;
971 int count = 0;
973 /* Voltages */
974 buf.length = ACPI_ALLOCATE_BUFFER;
975 status = acpi_evaluate_object_typed(data->atk_handle,
976 METHOD_OLD_ENUM_VLT, NULL, &buf, ACPI_TYPE_PACKAGE);
977 if (status != AE_OK) {
978 dev_warn(dev, METHOD_OLD_ENUM_VLT ": ACPI exception: %s\n",
979 acpi_format_exception(status));
981 return -ENODEV;
984 pack = buf.pointer;
985 for (i = 1; i < pack->package.count; i++) {
986 union acpi_object *obj = &pack->package.elements[i];
988 ret = atk_add_sensor(data, obj);
989 if (ret > 0)
990 count++;
992 ACPI_FREE(buf.pointer);
994 /* Temperatures */
995 buf.length = ACPI_ALLOCATE_BUFFER;
996 status = acpi_evaluate_object_typed(data->atk_handle,
997 METHOD_OLD_ENUM_TMP, NULL, &buf, ACPI_TYPE_PACKAGE);
998 if (status != AE_OK) {
999 dev_warn(dev, METHOD_OLD_ENUM_TMP ": ACPI exception: %s\n",
1000 acpi_format_exception(status));
1002 ret = -ENODEV;
1003 goto cleanup;
1006 pack = buf.pointer;
1007 for (i = 1; i < pack->package.count; i++) {
1008 union acpi_object *obj = &pack->package.elements[i];
1010 ret = atk_add_sensor(data, obj);
1011 if (ret > 0)
1012 count++;
1014 ACPI_FREE(buf.pointer);
1016 /* Fans */
1017 buf.length = ACPI_ALLOCATE_BUFFER;
1018 status = acpi_evaluate_object_typed(data->atk_handle,
1019 METHOD_OLD_ENUM_FAN, NULL, &buf, ACPI_TYPE_PACKAGE);
1020 if (status != AE_OK) {
1021 dev_warn(dev, METHOD_OLD_ENUM_FAN ": ACPI exception: %s\n",
1022 acpi_format_exception(status));
1024 ret = -ENODEV;
1025 goto cleanup;
1028 pack = buf.pointer;
1029 for (i = 1; i < pack->package.count; i++) {
1030 union acpi_object *obj = &pack->package.elements[i];
1032 ret = atk_add_sensor(data, obj);
1033 if (ret > 0)
1034 count++;
1036 ACPI_FREE(buf.pointer);
1038 return count;
1039 cleanup:
1040 atk_free_sensors(data);
1041 return ret;
1044 static int atk_ec_present(struct atk_data *data)
1046 struct device *dev = &data->acpi_dev->dev;
1047 union acpi_object *pack;
1048 union acpi_object *ec;
1049 int ret;
1050 int i;
1052 pack = atk_ggrp(data, ATK_MUX_MGMT);
1053 if (IS_ERR(pack)) {
1054 if (PTR_ERR(pack) == -ENOENT) {
1055 /* The MGMT class does not exists - that's ok */
1056 dev_dbg(dev, "Class %#llx not found\n", ATK_MUX_MGMT);
1057 return 0;
1059 return PTR_ERR(pack);
1062 /* Search the EC */
1063 ec = NULL;
1064 for (i = 0; i < pack->package.count; i++) {
1065 union acpi_object *obj = &pack->package.elements[i];
1066 union acpi_object *id;
1068 if (obj->type != ACPI_TYPE_PACKAGE)
1069 continue;
1071 id = &obj->package.elements[0];
1072 if (id->type != ACPI_TYPE_INTEGER)
1073 continue;
1075 if (id->integer.value == ATK_EC_ID) {
1076 ec = obj;
1077 break;
1081 ret = (ec != NULL);
1082 if (!ret)
1083 /* The system has no EC */
1084 dev_dbg(dev, "EC not found\n");
1086 ACPI_FREE(pack);
1087 return ret;
1090 static int atk_ec_enabled(struct atk_data *data)
1092 struct device *dev = &data->acpi_dev->dev;
1093 union acpi_object *obj;
1094 struct atk_acpi_ret_buffer *buf;
1095 int err;
1097 obj = atk_gitm(data, ATK_EC_ID);
1098 if (IS_ERR(obj)) {
1099 dev_err(dev, "Unable to query EC status\n");
1100 return PTR_ERR(obj);
1102 buf = (struct atk_acpi_ret_buffer *)obj->buffer.pointer;
1104 if (buf->flags == 0) {
1105 dev_err(dev, "Unable to query EC status\n");
1106 err = -EIO;
1107 } else {
1108 err = (buf->value != 0);
1109 dev_dbg(dev, "EC is %sabled\n",
1110 err ? "en" : "dis");
1113 ACPI_FREE(obj);
1114 return err;
1117 static int atk_ec_ctl(struct atk_data *data, int enable)
1119 struct device *dev = &data->acpi_dev->dev;
1120 union acpi_object *obj;
1121 struct atk_acpi_input_buf sitm;
1122 struct atk_acpi_ret_buffer *ec_ret;
1123 int err = 0;
1125 sitm.id = ATK_EC_ID;
1126 sitm.param1 = enable;
1127 sitm.param2 = 0;
1129 obj = atk_sitm(data, &sitm);
1130 if (IS_ERR(obj)) {
1131 dev_err(dev, "Failed to %sable the EC\n",
1132 enable ? "en" : "dis");
1133 return PTR_ERR(obj);
1135 ec_ret = (struct atk_acpi_ret_buffer *)obj->buffer.pointer;
1136 if (ec_ret->flags == 0) {
1137 dev_err(dev, "Failed to %sable the EC\n",
1138 enable ? "en" : "dis");
1139 err = -EIO;
1140 } else {
1141 dev_info(dev, "EC %sabled\n",
1142 enable ? "en" : "dis");
1145 ACPI_FREE(obj);
1146 return err;
1149 static int atk_enumerate_new_hwmon(struct atk_data *data)
1151 struct device *dev = &data->acpi_dev->dev;
1152 union acpi_object *pack;
1153 int err;
1154 int i;
1156 err = atk_ec_present(data);
1157 if (err < 0)
1158 return err;
1159 if (err) {
1160 err = atk_ec_enabled(data);
1161 if (err < 0)
1162 return err;
1163 /* If the EC was disabled we will disable it again on unload */
1164 data->disable_ec = err;
1166 err = atk_ec_ctl(data, 1);
1167 if (err) {
1168 data->disable_ec = false;
1169 return err;
1173 dev_dbg(dev, "Enumerating hwmon sensors\n");
1175 pack = atk_ggrp(data, ATK_MUX_HWMON);
1176 if (IS_ERR(pack))
1177 return PTR_ERR(pack);
1179 for (i = 0; i < pack->package.count; i++) {
1180 union acpi_object *obj = &pack->package.elements[i];
1182 atk_add_sensor(data, obj);
1185 err = data->voltage_count + data->temperature_count + data->fan_count;
1187 ACPI_FREE(pack);
1188 return err;
1191 static int atk_create_files(struct atk_data *data)
1193 struct atk_sensor_data *s;
1194 int err;
1196 list_for_each_entry(s, &data->sensor_list, list) {
1197 err = device_create_file(data->hwmon_dev, &s->input_attr);
1198 if (err)
1199 return err;
1200 err = device_create_file(data->hwmon_dev, &s->label_attr);
1201 if (err)
1202 return err;
1203 err = device_create_file(data->hwmon_dev, &s->limit1_attr);
1204 if (err)
1205 return err;
1206 err = device_create_file(data->hwmon_dev, &s->limit2_attr);
1207 if (err)
1208 return err;
1211 err = device_create_file(data->hwmon_dev, &atk_name_attr);
1213 return err;
1216 static void atk_remove_files(struct atk_data *data)
1218 struct atk_sensor_data *s;
1220 list_for_each_entry(s, &data->sensor_list, list) {
1221 device_remove_file(data->hwmon_dev, &s->input_attr);
1222 device_remove_file(data->hwmon_dev, &s->label_attr);
1223 device_remove_file(data->hwmon_dev, &s->limit1_attr);
1224 device_remove_file(data->hwmon_dev, &s->limit2_attr);
1226 device_remove_file(data->hwmon_dev, &atk_name_attr);
1229 static void atk_free_sensors(struct atk_data *data)
1231 struct list_head *head = &data->sensor_list;
1232 struct atk_sensor_data *s, *tmp;
1234 list_for_each_entry_safe(s, tmp, head, list) {
1235 kfree(s->acpi_name);
1236 kfree(s);
1240 static int atk_register_hwmon(struct atk_data *data)
1242 struct device *dev = &data->acpi_dev->dev;
1243 int err;
1245 dev_dbg(dev, "registering hwmon device\n");
1246 data->hwmon_dev = hwmon_device_register(dev);
1247 if (IS_ERR(data->hwmon_dev))
1248 return PTR_ERR(data->hwmon_dev);
1250 dev_dbg(dev, "populating sysfs directory\n");
1251 err = atk_create_files(data);
1252 if (err)
1253 goto remove;
1255 return 0;
1256 remove:
1257 /* Cleanup the registered files */
1258 atk_remove_files(data);
1259 hwmon_device_unregister(data->hwmon_dev);
1260 return err;
1263 static int atk_probe_if(struct atk_data *data)
1265 struct device *dev = &data->acpi_dev->dev;
1266 acpi_handle ret;
1267 acpi_status status;
1268 int err = 0;
1270 /* RTMP: read temperature */
1271 status = acpi_get_handle(data->atk_handle, METHOD_OLD_READ_TMP, &ret);
1272 if (ACPI_SUCCESS(status))
1273 data->rtmp_handle = ret;
1274 else
1275 dev_dbg(dev, "method " METHOD_OLD_READ_TMP " not found: %s\n",
1276 acpi_format_exception(status));
1278 /* RVLT: read voltage */
1279 status = acpi_get_handle(data->atk_handle, METHOD_OLD_READ_VLT, &ret);
1280 if (ACPI_SUCCESS(status))
1281 data->rvlt_handle = ret;
1282 else
1283 dev_dbg(dev, "method " METHOD_OLD_READ_VLT " not found: %s\n",
1284 acpi_format_exception(status));
1286 /* RFAN: read fan status */
1287 status = acpi_get_handle(data->atk_handle, METHOD_OLD_READ_FAN, &ret);
1288 if (ACPI_SUCCESS(status))
1289 data->rfan_handle = ret;
1290 else
1291 dev_dbg(dev, "method " METHOD_OLD_READ_FAN " not found: %s\n",
1292 acpi_format_exception(status));
1294 /* Enumeration */
1295 status = acpi_get_handle(data->atk_handle, METHOD_ENUMERATE, &ret);
1296 if (ACPI_SUCCESS(status))
1297 data->enumerate_handle = ret;
1298 else
1299 dev_dbg(dev, "method " METHOD_ENUMERATE " not found: %s\n",
1300 acpi_format_exception(status));
1302 /* De-multiplexer (read) */
1303 status = acpi_get_handle(data->atk_handle, METHOD_READ, &ret);
1304 if (ACPI_SUCCESS(status))
1305 data->read_handle = ret;
1306 else
1307 dev_dbg(dev, "method " METHOD_READ " not found: %s\n",
1308 acpi_format_exception(status));
1310 /* De-multiplexer (write) */
1311 status = acpi_get_handle(data->atk_handle, METHOD_WRITE, &ret);
1312 if (ACPI_SUCCESS(status))
1313 data->write_handle = ret;
1314 else
1315 dev_dbg(dev, "method " METHOD_WRITE " not found: %s\n",
1316 acpi_format_exception(status));
1319 * Check for hwmon methods: first check "old" style methods; note that
1320 * both may be present: in this case we stick to the old interface;
1321 * analysis of multiple DSDTs indicates that when both interfaces
1322 * are present the new one (GGRP/GITM) is not functional.
1324 if (new_if)
1325 dev_info(dev, "Overriding interface detection\n");
1326 if (data->rtmp_handle &&
1327 data->rvlt_handle && data->rfan_handle && !new_if)
1328 data->old_interface = true;
1329 else if (data->enumerate_handle && data->read_handle &&
1330 data->write_handle)
1331 data->old_interface = false;
1332 else
1333 err = -ENODEV;
1335 return err;
1338 static int atk_add(struct acpi_device *device)
1340 acpi_status ret;
1341 int err;
1342 struct acpi_buffer buf;
1343 union acpi_object *obj;
1344 struct atk_data *data;
1346 dev_dbg(&device->dev, "adding...\n");
1348 data = kzalloc(sizeof(*data), GFP_KERNEL);
1349 if (!data)
1350 return -ENOMEM;
1352 data->acpi_dev = device;
1353 data->atk_handle = device->handle;
1354 INIT_LIST_HEAD(&data->sensor_list);
1355 data->disable_ec = false;
1357 buf.length = ACPI_ALLOCATE_BUFFER;
1358 ret = acpi_evaluate_object_typed(data->atk_handle, BOARD_ID, NULL,
1359 &buf, ACPI_TYPE_PACKAGE);
1360 if (ret != AE_OK) {
1361 dev_dbg(&device->dev, "atk: method MBIF not found\n");
1362 } else {
1363 obj = buf.pointer;
1364 if (obj->package.count >= 2) {
1365 union acpi_object *id = &obj->package.elements[1];
1366 if (id->type == ACPI_TYPE_STRING)
1367 dev_dbg(&device->dev, "board ID = %s\n",
1368 id->string.pointer);
1370 ACPI_FREE(buf.pointer);
1373 err = atk_probe_if(data);
1374 if (err) {
1375 dev_err(&device->dev, "No usable hwmon interface detected\n");
1376 goto out;
1379 if (data->old_interface) {
1380 dev_dbg(&device->dev, "Using old hwmon interface\n");
1381 err = atk_enumerate_old_hwmon(data);
1382 } else {
1383 dev_dbg(&device->dev, "Using new hwmon interface\n");
1384 err = atk_enumerate_new_hwmon(data);
1386 if (err < 0)
1387 goto out;
1388 if (err == 0) {
1389 dev_info(&device->dev,
1390 "No usable sensor detected, bailing out\n");
1391 err = -ENODEV;
1392 goto out;
1395 err = atk_register_hwmon(data);
1396 if (err)
1397 goto cleanup;
1399 atk_debugfs_init(data);
1401 device->driver_data = data;
1402 return 0;
1403 cleanup:
1404 atk_free_sensors(data);
1405 out:
1406 if (data->disable_ec)
1407 atk_ec_ctl(data, 0);
1408 kfree(data);
1409 return err;
1412 static int atk_remove(struct acpi_device *device, int type)
1414 struct atk_data *data = device->driver_data;
1415 dev_dbg(&device->dev, "removing...\n");
1417 device->driver_data = NULL;
1419 atk_debugfs_cleanup(data);
1421 atk_remove_files(data);
1422 atk_free_sensors(data);
1423 hwmon_device_unregister(data->hwmon_dev);
1425 if (data->disable_ec) {
1426 if (atk_ec_ctl(data, 0))
1427 dev_err(&device->dev, "Failed to disable EC\n");
1430 kfree(data);
1432 return 0;
1435 static int __init atk0110_init(void)
1437 int ret;
1439 /* Make sure it's safe to access the device through ACPI */
1440 if (!acpi_resources_are_enforced()) {
1441 pr_err("Resources not safely usable due to acpi_enforce_resources kernel parameter\n");
1442 return -EBUSY;
1445 if (dmi_check_system(atk_force_new_if))
1446 new_if = true;
1448 ret = acpi_bus_register_driver(&atk_driver);
1449 if (ret)
1450 pr_info("acpi_bus_register_driver failed: %d\n", ret);
1452 return ret;
1455 static void __exit atk0110_exit(void)
1457 acpi_bus_unregister_driver(&atk_driver);
1460 module_init(atk0110_init);
1461 module_exit(atk0110_exit);
1463 MODULE_LICENSE("GPL");