Linux 2.6.34-rc3
[pohmelfs.git] / drivers / hwmon / asus_atk0110.c
blob028284f544e306780860e97b6e5363c94b92056d
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 #include <linux/debugfs.h>
9 #include <linux/kernel.h>
10 #include <linux/hwmon.h>
11 #include <linux/list.h>
12 #include <linux/module.h>
14 #include <acpi/acpi.h>
15 #include <acpi/acpixf.h>
16 #include <acpi/acpi_drivers.h>
17 #include <acpi/acpi_bus.h>
20 #define ATK_HID "ATK0110"
22 /* Minimum time between readings, enforced in order to avoid
23 * hogging the CPU.
25 #define CACHE_TIME HZ
27 #define BOARD_ID "MBIF"
28 #define METHOD_ENUMERATE "GGRP"
29 #define METHOD_READ "GITM"
30 #define METHOD_WRITE "SITM"
31 #define METHOD_OLD_READ_TMP "RTMP"
32 #define METHOD_OLD_READ_VLT "RVLT"
33 #define METHOD_OLD_READ_FAN "RFAN"
34 #define METHOD_OLD_ENUM_TMP "TSIF"
35 #define METHOD_OLD_ENUM_VLT "VSIF"
36 #define METHOD_OLD_ENUM_FAN "FSIF"
38 #define ATK_MUX_HWMON 0x00000006ULL
39 #define ATK_MUX_MGMT 0x00000011ULL
41 #define ATK_CLASS_MASK 0xff000000ULL
42 #define ATK_CLASS_FREQ_CTL 0x03000000ULL
43 #define ATK_CLASS_FAN_CTL 0x04000000ULL
44 #define ATK_CLASS_HWMON 0x06000000ULL
45 #define ATK_CLASS_MGMT 0x11000000ULL
47 #define ATK_TYPE_MASK 0x00ff0000ULL
48 #define HWMON_TYPE_VOLT 0x00020000ULL
49 #define HWMON_TYPE_TEMP 0x00030000ULL
50 #define HWMON_TYPE_FAN 0x00040000ULL
52 #define ATK_ELEMENT_ID_MASK 0x0000ffffULL
54 #define ATK_EC_ID 0x11060004ULL
56 enum atk_pack_member {
57 HWMON_PACK_FLAGS,
58 HWMON_PACK_NAME,
59 HWMON_PACK_LIMIT1,
60 HWMON_PACK_LIMIT2,
61 HWMON_PACK_ENABLE
64 /* New package format */
65 #define _HWMON_NEW_PACK_SIZE 7
66 #define _HWMON_NEW_PACK_FLAGS 0
67 #define _HWMON_NEW_PACK_NAME 1
68 #define _HWMON_NEW_PACK_UNK1 2
69 #define _HWMON_NEW_PACK_UNK2 3
70 #define _HWMON_NEW_PACK_LIMIT1 4
71 #define _HWMON_NEW_PACK_LIMIT2 5
72 #define _HWMON_NEW_PACK_ENABLE 6
74 /* Old package format */
75 #define _HWMON_OLD_PACK_SIZE 5
76 #define _HWMON_OLD_PACK_FLAGS 0
77 #define _HWMON_OLD_PACK_NAME 1
78 #define _HWMON_OLD_PACK_LIMIT1 2
79 #define _HWMON_OLD_PACK_LIMIT2 3
80 #define _HWMON_OLD_PACK_ENABLE 4
83 struct atk_data {
84 struct device *hwmon_dev;
85 acpi_handle atk_handle;
86 struct acpi_device *acpi_dev;
88 bool old_interface;
90 /* old interface */
91 acpi_handle rtmp_handle;
92 acpi_handle rvlt_handle;
93 acpi_handle rfan_handle;
94 /* new inteface */
95 acpi_handle enumerate_handle;
96 acpi_handle read_handle;
97 acpi_handle write_handle;
99 bool disable_ec;
101 int voltage_count;
102 int temperature_count;
103 int fan_count;
104 struct list_head sensor_list;
106 struct {
107 struct dentry *root;
108 u32 id;
109 } debugfs;
113 typedef ssize_t (*sysfs_show_func)(struct device *dev,
114 struct device_attribute *attr, char *buf);
116 static const struct acpi_device_id atk_ids[] = {
117 {ATK_HID, 0},
118 {"", 0},
120 MODULE_DEVICE_TABLE(acpi, atk_ids);
122 #define ATTR_NAME_SIZE 16 /* Worst case is "tempN_input" */
124 struct atk_sensor_data {
125 struct list_head list;
126 struct atk_data *data;
127 struct device_attribute label_attr;
128 struct device_attribute input_attr;
129 struct device_attribute limit1_attr;
130 struct device_attribute limit2_attr;
131 char label_attr_name[ATTR_NAME_SIZE];
132 char input_attr_name[ATTR_NAME_SIZE];
133 char limit1_attr_name[ATTR_NAME_SIZE];
134 char limit2_attr_name[ATTR_NAME_SIZE];
135 u64 id;
136 u64 type;
137 u64 limit1;
138 u64 limit2;
139 u64 cached_value;
140 unsigned long last_updated; /* in jiffies */
141 bool is_valid;
142 char const *acpi_name;
145 /* Return buffer format:
146 * [0-3] "value" is valid flag
147 * [4-7] value
148 * [8- ] unknown stuff on newer mobos
150 struct atk_acpi_ret_buffer {
151 u32 flags;
152 u32 value;
153 u8 data[];
156 /* Input buffer used for GITM and SITM methods */
157 struct atk_acpi_input_buf {
158 u32 id;
159 u32 param1;
160 u32 param2;
163 static int atk_add(struct acpi_device *device);
164 static int atk_remove(struct acpi_device *device, int type);
165 static void atk_print_sensor(struct atk_data *data, union acpi_object *obj);
166 static int atk_read_value(struct atk_sensor_data *sensor, u64 *value);
167 static void atk_free_sensors(struct atk_data *data);
169 static struct acpi_driver atk_driver = {
170 .name = ATK_HID,
171 .class = "hwmon",
172 .ids = atk_ids,
173 .ops = {
174 .add = atk_add,
175 .remove = atk_remove,
179 #define input_to_atk_sensor(attr) \
180 container_of(attr, struct atk_sensor_data, input_attr)
182 #define label_to_atk_sensor(attr) \
183 container_of(attr, struct atk_sensor_data, label_attr)
185 #define limit1_to_atk_sensor(attr) \
186 container_of(attr, struct atk_sensor_data, limit1_attr)
188 #define limit2_to_atk_sensor(attr) \
189 container_of(attr, struct atk_sensor_data, limit2_attr)
191 static ssize_t atk_input_show(struct device *dev,
192 struct device_attribute *attr, char *buf)
194 struct atk_sensor_data *s = input_to_atk_sensor(attr);
195 u64 value;
196 int err;
198 err = atk_read_value(s, &value);
199 if (err)
200 return err;
202 if (s->type == HWMON_TYPE_TEMP)
203 /* ACPI returns decidegree */
204 value *= 100;
206 return sprintf(buf, "%llu\n", value);
209 static ssize_t atk_label_show(struct device *dev,
210 struct device_attribute *attr, char *buf)
212 struct atk_sensor_data *s = label_to_atk_sensor(attr);
214 return sprintf(buf, "%s\n", s->acpi_name);
217 static ssize_t atk_limit1_show(struct device *dev,
218 struct device_attribute *attr, char *buf)
220 struct atk_sensor_data *s = limit1_to_atk_sensor(attr);
221 u64 value = s->limit1;
223 if (s->type == HWMON_TYPE_TEMP)
224 value *= 100;
226 return sprintf(buf, "%lld\n", value);
229 static ssize_t atk_limit2_show(struct device *dev,
230 struct device_attribute *attr, char *buf)
232 struct atk_sensor_data *s = limit2_to_atk_sensor(attr);
233 u64 value = s->limit2;
235 if (s->type == HWMON_TYPE_TEMP)
236 value *= 100;
238 return sprintf(buf, "%lld\n", value);
241 static ssize_t atk_name_show(struct device *dev,
242 struct device_attribute *attr, char *buf)
244 return sprintf(buf, "atk0110\n");
246 static struct device_attribute atk_name_attr =
247 __ATTR(name, 0444, atk_name_show, NULL);
249 static void atk_init_attribute(struct device_attribute *attr, char *name,
250 sysfs_show_func show)
252 attr->attr.name = name;
253 attr->attr.mode = 0444;
254 attr->show = show;
255 attr->store = NULL;
259 static union acpi_object *atk_get_pack_member(struct atk_data *data,
260 union acpi_object *pack,
261 enum atk_pack_member m)
263 bool old_if = data->old_interface;
264 int offset;
266 switch (m) {
267 case HWMON_PACK_FLAGS:
268 offset = old_if ? _HWMON_OLD_PACK_FLAGS : _HWMON_NEW_PACK_FLAGS;
269 break;
270 case HWMON_PACK_NAME:
271 offset = old_if ? _HWMON_OLD_PACK_NAME : _HWMON_NEW_PACK_NAME;
272 break;
273 case HWMON_PACK_LIMIT1:
274 offset = old_if ? _HWMON_OLD_PACK_LIMIT1 :
275 _HWMON_NEW_PACK_LIMIT1;
276 break;
277 case HWMON_PACK_LIMIT2:
278 offset = old_if ? _HWMON_OLD_PACK_LIMIT2 :
279 _HWMON_NEW_PACK_LIMIT2;
280 break;
281 case HWMON_PACK_ENABLE:
282 offset = old_if ? _HWMON_OLD_PACK_ENABLE :
283 _HWMON_NEW_PACK_ENABLE;
284 break;
285 default:
286 return NULL;
289 return &pack->package.elements[offset];
293 /* New package format is:
294 * - flag (int)
295 * class - used for de-muxing the request to the correct GITn
296 * type (volt, temp, fan)
297 * sensor id |
298 * sensor id - used for de-muxing the request _inside_ the GITn
299 * - name (str)
300 * - unknown (int)
301 * - unknown (int)
302 * - limit1 (int)
303 * - limit2 (int)
304 * - enable (int)
306 * The old package has the same format but it's missing the two unknown fields.
308 static int validate_hwmon_pack(struct atk_data *data, union acpi_object *obj)
310 struct device *dev = &data->acpi_dev->dev;
311 union acpi_object *tmp;
312 bool old_if = data->old_interface;
313 int const expected_size = old_if ? _HWMON_OLD_PACK_SIZE :
314 _HWMON_NEW_PACK_SIZE;
316 if (obj->type != ACPI_TYPE_PACKAGE) {
317 dev_warn(dev, "Invalid type: %d\n", obj->type);
318 return -EINVAL;
321 if (obj->package.count != expected_size) {
322 dev_warn(dev, "Invalid package size: %d, expected: %d\n",
323 obj->package.count, expected_size);
324 return -EINVAL;
327 tmp = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS);
328 if (tmp->type != ACPI_TYPE_INTEGER) {
329 dev_warn(dev, "Invalid type (flag): %d\n", tmp->type);
330 return -EINVAL;
333 tmp = atk_get_pack_member(data, obj, HWMON_PACK_NAME);
334 if (tmp->type != ACPI_TYPE_STRING) {
335 dev_warn(dev, "Invalid type (name): %d\n", tmp->type);
336 return -EINVAL;
339 /* Don't check... we don't know what they're useful for anyway */
340 #if 0
341 tmp = &obj->package.elements[HWMON_PACK_UNK1];
342 if (tmp->type != ACPI_TYPE_INTEGER) {
343 dev_warn(dev, "Invalid type (unk1): %d\n", tmp->type);
344 return -EINVAL;
347 tmp = &obj->package.elements[HWMON_PACK_UNK2];
348 if (tmp->type != ACPI_TYPE_INTEGER) {
349 dev_warn(dev, "Invalid type (unk2): %d\n", tmp->type);
350 return -EINVAL;
352 #endif
354 tmp = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT1);
355 if (tmp->type != ACPI_TYPE_INTEGER) {
356 dev_warn(dev, "Invalid type (limit1): %d\n", tmp->type);
357 return -EINVAL;
360 tmp = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT2);
361 if (tmp->type != ACPI_TYPE_INTEGER) {
362 dev_warn(dev, "Invalid type (limit2): %d\n", tmp->type);
363 return -EINVAL;
366 tmp = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE);
367 if (tmp->type != ACPI_TYPE_INTEGER) {
368 dev_warn(dev, "Invalid type (enable): %d\n", tmp->type);
369 return -EINVAL;
372 atk_print_sensor(data, obj);
374 return 0;
377 #ifdef DEBUG
378 static char const *atk_sensor_type(union acpi_object *flags)
380 u64 type = flags->integer.value & ATK_TYPE_MASK;
381 char const *what;
383 switch (type) {
384 case HWMON_TYPE_VOLT:
385 what = "voltage";
386 break;
387 case HWMON_TYPE_TEMP:
388 what = "temperature";
389 break;
390 case HWMON_TYPE_FAN:
391 what = "fan";
392 break;
393 default:
394 what = "unknown";
395 break;
398 return what;
400 #endif
402 static void atk_print_sensor(struct atk_data *data, union acpi_object *obj)
404 #ifdef DEBUG
405 struct device *dev = &data->acpi_dev->dev;
406 union acpi_object *flags;
407 union acpi_object *name;
408 union acpi_object *limit1;
409 union acpi_object *limit2;
410 union acpi_object *enable;
411 char const *what;
413 flags = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS);
414 name = atk_get_pack_member(data, obj, HWMON_PACK_NAME);
415 limit1 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT1);
416 limit2 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT2);
417 enable = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE);
419 what = atk_sensor_type(flags);
421 dev_dbg(dev, "%s: %#llx %s [%llu-%llu] %s\n", what,
422 flags->integer.value,
423 name->string.pointer,
424 limit1->integer.value, limit2->integer.value,
425 enable->integer.value ? "enabled" : "disabled");
426 #endif
429 static int atk_read_value_old(struct atk_sensor_data *sensor, u64 *value)
431 struct atk_data *data = sensor->data;
432 struct device *dev = &data->acpi_dev->dev;
433 struct acpi_object_list params;
434 union acpi_object id;
435 acpi_status status;
436 acpi_handle method;
438 switch (sensor->type) {
439 case HWMON_TYPE_VOLT:
440 method = data->rvlt_handle;
441 break;
442 case HWMON_TYPE_TEMP:
443 method = data->rtmp_handle;
444 break;
445 case HWMON_TYPE_FAN:
446 method = data->rfan_handle;
447 break;
448 default:
449 return -EINVAL;
452 id.type = ACPI_TYPE_INTEGER;
453 id.integer.value = sensor->id;
455 params.count = 1;
456 params.pointer = &id;
458 status = acpi_evaluate_integer(method, NULL, &params, value);
459 if (status != AE_OK) {
460 dev_warn(dev, "%s: ACPI exception: %s\n", __func__,
461 acpi_format_exception(status));
462 return -EIO;
465 return 0;
468 static union acpi_object *atk_ggrp(struct atk_data *data, u16 mux)
470 struct device *dev = &data->acpi_dev->dev;
471 struct acpi_buffer buf;
472 acpi_status ret;
473 struct acpi_object_list params;
474 union acpi_object id;
475 union acpi_object *pack;
477 id.type = ACPI_TYPE_INTEGER;
478 id.integer.value = mux;
479 params.count = 1;
480 params.pointer = &id;
482 buf.length = ACPI_ALLOCATE_BUFFER;
483 ret = acpi_evaluate_object(data->enumerate_handle, NULL, &params, &buf);
484 if (ret != AE_OK) {
485 dev_err(dev, "GGRP[%#x] ACPI exception: %s\n", mux,
486 acpi_format_exception(ret));
487 return ERR_PTR(-EIO);
489 pack = buf.pointer;
490 if (pack->type != ACPI_TYPE_PACKAGE) {
491 /* Execution was successful, but the id was not found */
492 ACPI_FREE(pack);
493 return ERR_PTR(-ENOENT);
496 if (pack->package.count < 1) {
497 dev_err(dev, "GGRP[%#x] package is too small\n", mux);
498 ACPI_FREE(pack);
499 return ERR_PTR(-EIO);
501 return pack;
504 static union acpi_object *atk_gitm(struct atk_data *data, u64 id)
506 struct device *dev = &data->acpi_dev->dev;
507 struct atk_acpi_input_buf buf;
508 union acpi_object tmp;
509 struct acpi_object_list params;
510 struct acpi_buffer ret;
511 union acpi_object *obj;
512 acpi_status status;
514 buf.id = id;
515 buf.param1 = 0;
516 buf.param2 = 0;
518 tmp.type = ACPI_TYPE_BUFFER;
519 tmp.buffer.pointer = (u8 *)&buf;
520 tmp.buffer.length = sizeof(buf);
522 params.count = 1;
523 params.pointer = (void *)&tmp;
525 ret.length = ACPI_ALLOCATE_BUFFER;
526 status = acpi_evaluate_object_typed(data->read_handle, NULL, &params,
527 &ret, ACPI_TYPE_BUFFER);
528 if (status != AE_OK) {
529 dev_warn(dev, "GITM[%#llx] ACPI exception: %s\n", id,
530 acpi_format_exception(status));
531 return ERR_PTR(-EIO);
533 obj = ret.pointer;
535 /* Sanity check */
536 if (obj->buffer.length < 8) {
537 dev_warn(dev, "Unexpected ASBF length: %u\n",
538 obj->buffer.length);
539 ACPI_FREE(obj);
540 return ERR_PTR(-EIO);
542 return obj;
545 static union acpi_object *atk_sitm(struct atk_data *data,
546 struct atk_acpi_input_buf *buf)
548 struct device *dev = &data->acpi_dev->dev;
549 struct acpi_object_list params;
550 union acpi_object tmp;
551 struct acpi_buffer ret;
552 union acpi_object *obj;
553 acpi_status status;
555 tmp.type = ACPI_TYPE_BUFFER;
556 tmp.buffer.pointer = (u8 *)buf;
557 tmp.buffer.length = sizeof(*buf);
559 params.count = 1;
560 params.pointer = &tmp;
562 ret.length = ACPI_ALLOCATE_BUFFER;
563 status = acpi_evaluate_object_typed(data->write_handle, NULL, &params,
564 &ret, ACPI_TYPE_BUFFER);
565 if (status != AE_OK) {
566 dev_warn(dev, "SITM[%#x] ACPI exception: %s\n", buf->id,
567 acpi_format_exception(status));
568 return ERR_PTR(-EIO);
570 obj = ret.pointer;
572 /* Sanity check */
573 if (obj->buffer.length < 8) {
574 dev_warn(dev, "Unexpected ASBF length: %u\n",
575 obj->buffer.length);
576 ACPI_FREE(obj);
577 return ERR_PTR(-EIO);
579 return obj;
582 static int atk_read_value_new(struct atk_sensor_data *sensor, u64 *value)
584 struct atk_data *data = sensor->data;
585 struct device *dev = &data->acpi_dev->dev;
586 union acpi_object *obj;
587 struct atk_acpi_ret_buffer *buf;
588 int err = 0;
590 obj = atk_gitm(data, sensor->id);
591 if (IS_ERR(obj))
592 return PTR_ERR(obj);
594 buf = (struct atk_acpi_ret_buffer *)obj->buffer.pointer;
595 if (buf->flags == 0) {
596 /* The reading is not valid, possible causes:
597 * - sensor failure
598 * - enumeration was FUBAR (and we didn't notice)
600 dev_warn(dev, "Read failed, sensor = %#llx\n", sensor->id);
601 err = -EIO;
602 goto out;
605 *value = buf->value;
606 out:
607 ACPI_FREE(obj);
608 return err;
611 static int atk_read_value(struct atk_sensor_data *sensor, u64 *value)
613 int err;
615 if (!sensor->is_valid ||
616 time_after(jiffies, sensor->last_updated + CACHE_TIME)) {
617 if (sensor->data->old_interface)
618 err = atk_read_value_old(sensor, value);
619 else
620 err = atk_read_value_new(sensor, value);
622 sensor->is_valid = true;
623 sensor->last_updated = jiffies;
624 sensor->cached_value = *value;
625 } else {
626 *value = sensor->cached_value;
627 err = 0;
630 return err;
633 #ifdef CONFIG_DEBUG_FS
634 static int atk_debugfs_gitm_get(void *p, u64 *val)
636 struct atk_data *data = p;
637 union acpi_object *ret;
638 struct atk_acpi_ret_buffer *buf;
639 int err = 0;
641 if (!data->read_handle)
642 return -ENODEV;
644 if (!data->debugfs.id)
645 return -EINVAL;
647 ret = atk_gitm(data, data->debugfs.id);
648 if (IS_ERR(ret))
649 return PTR_ERR(ret);
651 buf = (struct atk_acpi_ret_buffer *)ret->buffer.pointer;
652 if (buf->flags)
653 *val = buf->value;
654 else
655 err = -EIO;
657 return err;
660 DEFINE_SIMPLE_ATTRIBUTE(atk_debugfs_gitm,
661 atk_debugfs_gitm_get,
662 NULL,
663 "0x%08llx\n")
665 static int atk_acpi_print(char *buf, size_t sz, union acpi_object *obj)
667 int ret = 0;
669 switch (obj->type) {
670 case ACPI_TYPE_INTEGER:
671 ret = snprintf(buf, sz, "0x%08llx\n", obj->integer.value);
672 break;
673 case ACPI_TYPE_STRING:
674 ret = snprintf(buf, sz, "%s\n", obj->string.pointer);
675 break;
678 return ret;
681 static void atk_pack_print(char *buf, size_t sz, union acpi_object *pack)
683 int ret;
684 int i;
686 for (i = 0; i < pack->package.count; i++) {
687 union acpi_object *obj = &pack->package.elements[i];
689 ret = atk_acpi_print(buf, sz, obj);
690 if (ret >= sz)
691 break;
692 buf += ret;
693 sz -= ret;
697 static int atk_debugfs_ggrp_open(struct inode *inode, struct file *file)
699 struct atk_data *data = inode->i_private;
700 char *buf = NULL;
701 union acpi_object *ret;
702 u8 cls;
703 int i;
705 if (!data->enumerate_handle)
706 return -ENODEV;
707 if (!data->debugfs.id)
708 return -EINVAL;
710 cls = (data->debugfs.id & 0xff000000) >> 24;
711 ret = atk_ggrp(data, cls);
712 if (IS_ERR(ret))
713 return PTR_ERR(ret);
715 for (i = 0; i < ret->package.count; i++) {
716 union acpi_object *pack = &ret->package.elements[i];
717 union acpi_object *id;
719 if (pack->type != ACPI_TYPE_PACKAGE)
720 continue;
721 if (!pack->package.count)
722 continue;
723 id = &pack->package.elements[0];
724 if (id->integer.value == data->debugfs.id) {
725 /* Print the package */
726 buf = kzalloc(512, GFP_KERNEL);
727 if (!buf) {
728 ACPI_FREE(ret);
729 return -ENOMEM;
731 atk_pack_print(buf, 512, pack);
732 break;
735 ACPI_FREE(ret);
737 if (!buf)
738 return -EINVAL;
740 file->private_data = buf;
742 return nonseekable_open(inode, file);
745 static ssize_t atk_debugfs_ggrp_read(struct file *file, char __user *buf,
746 size_t count, loff_t *pos)
748 char *str = file->private_data;
749 size_t len = strlen(str);
751 return simple_read_from_buffer(buf, count, pos, str, len);
754 static int atk_debugfs_ggrp_release(struct inode *inode, struct file *file)
756 kfree(file->private_data);
757 return 0;
760 static const struct file_operations atk_debugfs_ggrp_fops = {
761 .read = atk_debugfs_ggrp_read,
762 .open = atk_debugfs_ggrp_open,
763 .release = atk_debugfs_ggrp_release,
766 static void atk_debugfs_init(struct atk_data *data)
768 struct dentry *d;
769 struct dentry *f;
771 data->debugfs.id = 0;
773 d = debugfs_create_dir("asus_atk0110", NULL);
774 if (!d || IS_ERR(d))
775 return;
777 f = debugfs_create_x32("id", S_IRUSR | S_IWUSR, d, &data->debugfs.id);
778 if (!f || IS_ERR(f))
779 goto cleanup;
781 f = debugfs_create_file("gitm", S_IRUSR, d, data,
782 &atk_debugfs_gitm);
783 if (!f || IS_ERR(f))
784 goto cleanup;
786 f = debugfs_create_file("ggrp", S_IRUSR, d, data,
787 &atk_debugfs_ggrp_fops);
788 if (!f || IS_ERR(f))
789 goto cleanup;
791 data->debugfs.root = d;
793 return;
794 cleanup:
795 debugfs_remove_recursive(d);
798 static void atk_debugfs_cleanup(struct atk_data *data)
800 debugfs_remove_recursive(data->debugfs.root);
803 #else /* CONFIG_DEBUG_FS */
805 static void atk_debugfs_init(struct atk_data *data)
809 static void atk_debugfs_cleanup(struct atk_data *data)
812 #endif
814 static int atk_add_sensor(struct atk_data *data, union acpi_object *obj)
816 struct device *dev = &data->acpi_dev->dev;
817 union acpi_object *flags;
818 union acpi_object *name;
819 union acpi_object *limit1;
820 union acpi_object *limit2;
821 union acpi_object *enable;
822 struct atk_sensor_data *sensor;
823 char const *base_name;
824 char const *limit1_name;
825 char const *limit2_name;
826 u64 type;
827 int err;
828 int *num;
829 int start;
831 if (obj->type != ACPI_TYPE_PACKAGE) {
832 /* wft is this? */
833 dev_warn(dev, "Unknown type for ACPI object: (%d)\n",
834 obj->type);
835 return -EINVAL;
838 err = validate_hwmon_pack(data, obj);
839 if (err)
840 return err;
842 /* Ok, we have a valid hwmon package */
843 type = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS)->integer.value
844 & ATK_TYPE_MASK;
846 switch (type) {
847 case HWMON_TYPE_VOLT:
848 base_name = "in";
849 limit1_name = "min";
850 limit2_name = "max";
851 num = &data->voltage_count;
852 start = 0;
853 break;
854 case HWMON_TYPE_TEMP:
855 base_name = "temp";
856 limit1_name = "max";
857 limit2_name = "crit";
858 num = &data->temperature_count;
859 start = 1;
860 break;
861 case HWMON_TYPE_FAN:
862 base_name = "fan";
863 limit1_name = "min";
864 limit2_name = "max";
865 num = &data->fan_count;
866 start = 1;
867 break;
868 default:
869 dev_warn(dev, "Unknown sensor type: %#llx\n", type);
870 return -EINVAL;
873 enable = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE);
874 if (!enable->integer.value)
875 /* sensor is disabled */
876 return 0;
878 flags = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS);
879 name = atk_get_pack_member(data, obj, HWMON_PACK_NAME);
880 limit1 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT1);
881 limit2 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT2);
883 sensor = kzalloc(sizeof(*sensor), GFP_KERNEL);
884 if (!sensor)
885 return -ENOMEM;
887 sensor->acpi_name = kstrdup(name->string.pointer, GFP_KERNEL);
888 if (!sensor->acpi_name) {
889 err = -ENOMEM;
890 goto out;
893 INIT_LIST_HEAD(&sensor->list);
894 sensor->type = type;
895 sensor->data = data;
896 sensor->id = flags->integer.value;
897 sensor->limit1 = limit1->integer.value;
898 if (data->old_interface)
899 sensor->limit2 = limit2->integer.value;
900 else
901 /* The upper limit is expressed as delta from lower limit */
902 sensor->limit2 = sensor->limit1 + limit2->integer.value;
904 snprintf(sensor->input_attr_name, ATTR_NAME_SIZE,
905 "%s%d_input", base_name, start + *num);
906 atk_init_attribute(&sensor->input_attr,
907 sensor->input_attr_name,
908 atk_input_show);
910 snprintf(sensor->label_attr_name, ATTR_NAME_SIZE,
911 "%s%d_label", base_name, start + *num);
912 atk_init_attribute(&sensor->label_attr,
913 sensor->label_attr_name,
914 atk_label_show);
916 snprintf(sensor->limit1_attr_name, ATTR_NAME_SIZE,
917 "%s%d_%s", base_name, start + *num, limit1_name);
918 atk_init_attribute(&sensor->limit1_attr,
919 sensor->limit1_attr_name,
920 atk_limit1_show);
922 snprintf(sensor->limit2_attr_name, ATTR_NAME_SIZE,
923 "%s%d_%s", base_name, start + *num, limit2_name);
924 atk_init_attribute(&sensor->limit2_attr,
925 sensor->limit2_attr_name,
926 atk_limit2_show);
928 list_add(&sensor->list, &data->sensor_list);
929 (*num)++;
931 return 1;
932 out:
933 kfree(sensor->acpi_name);
934 kfree(sensor);
935 return err;
938 static int atk_enumerate_old_hwmon(struct atk_data *data)
940 struct device *dev = &data->acpi_dev->dev;
941 struct acpi_buffer buf;
942 union acpi_object *pack;
943 acpi_status status;
944 int i, ret;
945 int count = 0;
947 /* Voltages */
948 buf.length = ACPI_ALLOCATE_BUFFER;
949 status = acpi_evaluate_object_typed(data->atk_handle,
950 METHOD_OLD_ENUM_VLT, NULL, &buf, ACPI_TYPE_PACKAGE);
951 if (status != AE_OK) {
952 dev_warn(dev, METHOD_OLD_ENUM_VLT ": ACPI exception: %s\n",
953 acpi_format_exception(status));
955 return -ENODEV;
958 pack = buf.pointer;
959 for (i = 1; i < pack->package.count; i++) {
960 union acpi_object *obj = &pack->package.elements[i];
962 ret = atk_add_sensor(data, obj);
963 if (ret > 0)
964 count++;
966 ACPI_FREE(buf.pointer);
968 /* Temperatures */
969 buf.length = ACPI_ALLOCATE_BUFFER;
970 status = acpi_evaluate_object_typed(data->atk_handle,
971 METHOD_OLD_ENUM_TMP, NULL, &buf, ACPI_TYPE_PACKAGE);
972 if (status != AE_OK) {
973 dev_warn(dev, METHOD_OLD_ENUM_TMP ": ACPI exception: %s\n",
974 acpi_format_exception(status));
976 ret = -ENODEV;
977 goto cleanup;
980 pack = buf.pointer;
981 for (i = 1; i < pack->package.count; i++) {
982 union acpi_object *obj = &pack->package.elements[i];
984 ret = atk_add_sensor(data, obj);
985 if (ret > 0)
986 count++;
988 ACPI_FREE(buf.pointer);
990 /* Fans */
991 buf.length = ACPI_ALLOCATE_BUFFER;
992 status = acpi_evaluate_object_typed(data->atk_handle,
993 METHOD_OLD_ENUM_FAN, NULL, &buf, ACPI_TYPE_PACKAGE);
994 if (status != AE_OK) {
995 dev_warn(dev, METHOD_OLD_ENUM_FAN ": ACPI exception: %s\n",
996 acpi_format_exception(status));
998 ret = -ENODEV;
999 goto cleanup;
1002 pack = buf.pointer;
1003 for (i = 1; i < pack->package.count; i++) {
1004 union acpi_object *obj = &pack->package.elements[i];
1006 ret = atk_add_sensor(data, obj);
1007 if (ret > 0)
1008 count++;
1010 ACPI_FREE(buf.pointer);
1012 return count;
1013 cleanup:
1014 atk_free_sensors(data);
1015 return ret;
1018 static int atk_ec_present(struct atk_data *data)
1020 struct device *dev = &data->acpi_dev->dev;
1021 union acpi_object *pack;
1022 union acpi_object *ec;
1023 int ret;
1024 int i;
1026 pack = atk_ggrp(data, ATK_MUX_MGMT);
1027 if (IS_ERR(pack)) {
1028 if (PTR_ERR(pack) == -ENOENT) {
1029 /* The MGMT class does not exists - that's ok */
1030 dev_dbg(dev, "Class %#llx not found\n", ATK_MUX_MGMT);
1031 return 0;
1033 return PTR_ERR(pack);
1036 /* Search the EC */
1037 ec = NULL;
1038 for (i = 0; i < pack->package.count; i++) {
1039 union acpi_object *obj = &pack->package.elements[i];
1040 union acpi_object *id;
1042 if (obj->type != ACPI_TYPE_PACKAGE)
1043 continue;
1045 id = &obj->package.elements[0];
1046 if (id->type != ACPI_TYPE_INTEGER)
1047 continue;
1049 if (id->integer.value == ATK_EC_ID) {
1050 ec = obj;
1051 break;
1055 ret = (ec != NULL);
1056 if (!ret)
1057 /* The system has no EC */
1058 dev_dbg(dev, "EC not found\n");
1060 ACPI_FREE(pack);
1061 return ret;
1064 static int atk_ec_enabled(struct atk_data *data)
1066 struct device *dev = &data->acpi_dev->dev;
1067 union acpi_object *obj;
1068 struct atk_acpi_ret_buffer *buf;
1069 int err;
1071 obj = atk_gitm(data, ATK_EC_ID);
1072 if (IS_ERR(obj)) {
1073 dev_err(dev, "Unable to query EC status\n");
1074 return PTR_ERR(obj);
1076 buf = (struct atk_acpi_ret_buffer *)obj->buffer.pointer;
1078 if (buf->flags == 0) {
1079 dev_err(dev, "Unable to query EC status\n");
1080 err = -EIO;
1081 } else {
1082 err = (buf->value != 0);
1083 dev_dbg(dev, "EC is %sabled\n",
1084 err ? "en" : "dis");
1087 ACPI_FREE(obj);
1088 return err;
1091 static int atk_ec_ctl(struct atk_data *data, int enable)
1093 struct device *dev = &data->acpi_dev->dev;
1094 union acpi_object *obj;
1095 struct atk_acpi_input_buf sitm;
1096 struct atk_acpi_ret_buffer *ec_ret;
1097 int err = 0;
1099 sitm.id = ATK_EC_ID;
1100 sitm.param1 = enable;
1101 sitm.param2 = 0;
1103 obj = atk_sitm(data, &sitm);
1104 if (IS_ERR(obj)) {
1105 dev_err(dev, "Failed to %sable the EC\n",
1106 enable ? "en" : "dis");
1107 return PTR_ERR(obj);
1109 ec_ret = (struct atk_acpi_ret_buffer *)obj->buffer.pointer;
1110 if (ec_ret->flags == 0) {
1111 dev_err(dev, "Failed to %sable the EC\n",
1112 enable ? "en" : "dis");
1113 err = -EIO;
1114 } else {
1115 dev_info(dev, "EC %sabled\n",
1116 enable ? "en" : "dis");
1119 ACPI_FREE(obj);
1120 return err;
1123 static int atk_enumerate_new_hwmon(struct atk_data *data)
1125 struct device *dev = &data->acpi_dev->dev;
1126 union acpi_object *pack;
1127 int err;
1128 int i;
1130 err = atk_ec_present(data);
1131 if (err < 0)
1132 return err;
1133 if (err) {
1134 err = atk_ec_enabled(data);
1135 if (err < 0)
1136 return err;
1137 /* If the EC was disabled we will disable it again on unload */
1138 data->disable_ec = err;
1140 err = atk_ec_ctl(data, 1);
1141 if (err) {
1142 data->disable_ec = false;
1143 return err;
1147 dev_dbg(dev, "Enumerating hwmon sensors\n");
1149 pack = atk_ggrp(data, ATK_MUX_HWMON);
1150 if (IS_ERR(pack))
1151 return PTR_ERR(pack);
1153 for (i = 0; i < pack->package.count; i++) {
1154 union acpi_object *obj = &pack->package.elements[i];
1156 atk_add_sensor(data, obj);
1159 err = data->voltage_count + data->temperature_count + data->fan_count;
1161 ACPI_FREE(pack);
1162 return err;
1165 static int atk_create_files(struct atk_data *data)
1167 struct atk_sensor_data *s;
1168 int err;
1170 list_for_each_entry(s, &data->sensor_list, list) {
1171 err = device_create_file(data->hwmon_dev, &s->input_attr);
1172 if (err)
1173 return err;
1174 err = device_create_file(data->hwmon_dev, &s->label_attr);
1175 if (err)
1176 return err;
1177 err = device_create_file(data->hwmon_dev, &s->limit1_attr);
1178 if (err)
1179 return err;
1180 err = device_create_file(data->hwmon_dev, &s->limit2_attr);
1181 if (err)
1182 return err;
1185 err = device_create_file(data->hwmon_dev, &atk_name_attr);
1187 return err;
1190 static void atk_remove_files(struct atk_data *data)
1192 struct atk_sensor_data *s;
1194 list_for_each_entry(s, &data->sensor_list, list) {
1195 device_remove_file(data->hwmon_dev, &s->input_attr);
1196 device_remove_file(data->hwmon_dev, &s->label_attr);
1197 device_remove_file(data->hwmon_dev, &s->limit1_attr);
1198 device_remove_file(data->hwmon_dev, &s->limit2_attr);
1200 device_remove_file(data->hwmon_dev, &atk_name_attr);
1203 static void atk_free_sensors(struct atk_data *data)
1205 struct list_head *head = &data->sensor_list;
1206 struct atk_sensor_data *s, *tmp;
1208 list_for_each_entry_safe(s, tmp, head, list) {
1209 kfree(s->acpi_name);
1210 kfree(s);
1214 static int atk_register_hwmon(struct atk_data *data)
1216 struct device *dev = &data->acpi_dev->dev;
1217 int err;
1219 dev_dbg(dev, "registering hwmon device\n");
1220 data->hwmon_dev = hwmon_device_register(dev);
1221 if (IS_ERR(data->hwmon_dev))
1222 return PTR_ERR(data->hwmon_dev);
1224 dev_dbg(dev, "populating sysfs directory\n");
1225 err = atk_create_files(data);
1226 if (err)
1227 goto remove;
1229 return 0;
1230 remove:
1231 /* Cleanup the registered files */
1232 atk_remove_files(data);
1233 hwmon_device_unregister(data->hwmon_dev);
1234 return err;
1237 static int atk_probe_if(struct atk_data *data)
1239 struct device *dev = &data->acpi_dev->dev;
1240 acpi_handle ret;
1241 acpi_status status;
1242 int err = 0;
1244 /* RTMP: read temperature */
1245 status = acpi_get_handle(data->atk_handle, METHOD_OLD_READ_TMP, &ret);
1246 if (ACPI_SUCCESS(status))
1247 data->rtmp_handle = ret;
1248 else
1249 dev_dbg(dev, "method " METHOD_OLD_READ_TMP " not found: %s\n",
1250 acpi_format_exception(status));
1252 /* RVLT: read voltage */
1253 status = acpi_get_handle(data->atk_handle, METHOD_OLD_READ_VLT, &ret);
1254 if (ACPI_SUCCESS(status))
1255 data->rvlt_handle = ret;
1256 else
1257 dev_dbg(dev, "method " METHOD_OLD_READ_VLT " not found: %s\n",
1258 acpi_format_exception(status));
1260 /* RFAN: read fan status */
1261 status = acpi_get_handle(data->atk_handle, METHOD_OLD_READ_FAN, &ret);
1262 if (ACPI_SUCCESS(status))
1263 data->rfan_handle = ret;
1264 else
1265 dev_dbg(dev, "method " METHOD_OLD_READ_FAN " not found: %s\n",
1266 acpi_format_exception(status));
1268 /* Enumeration */
1269 status = acpi_get_handle(data->atk_handle, METHOD_ENUMERATE, &ret);
1270 if (ACPI_SUCCESS(status))
1271 data->enumerate_handle = ret;
1272 else
1273 dev_dbg(dev, "method " METHOD_ENUMERATE " not found: %s\n",
1274 acpi_format_exception(status));
1276 /* De-multiplexer (read) */
1277 status = acpi_get_handle(data->atk_handle, METHOD_READ, &ret);
1278 if (ACPI_SUCCESS(status))
1279 data->read_handle = ret;
1280 else
1281 dev_dbg(dev, "method " METHOD_READ " not found: %s\n",
1282 acpi_format_exception(status));
1284 /* De-multiplexer (write) */
1285 status = acpi_get_handle(data->atk_handle, METHOD_WRITE, &ret);
1286 if (ACPI_SUCCESS(status))
1287 data->write_handle = ret;
1288 else
1289 dev_dbg(dev, "method " METHOD_WRITE " not found: %s\n",
1290 acpi_format_exception(status));
1292 /* Check for hwmon methods: first check "old" style methods; note that
1293 * both may be present: in this case we stick to the old interface;
1294 * analysis of multiple DSDTs indicates that when both interfaces
1295 * are present the new one (GGRP/GITM) is not functional.
1297 if (data->rtmp_handle && data->rvlt_handle && data->rfan_handle)
1298 data->old_interface = true;
1299 else if (data->enumerate_handle && data->read_handle &&
1300 data->write_handle)
1301 data->old_interface = false;
1302 else
1303 err = -ENODEV;
1305 return err;
1308 static int atk_add(struct acpi_device *device)
1310 acpi_status ret;
1311 int err;
1312 struct acpi_buffer buf;
1313 union acpi_object *obj;
1314 struct atk_data *data;
1316 dev_dbg(&device->dev, "adding...\n");
1318 data = kzalloc(sizeof(*data), GFP_KERNEL);
1319 if (!data)
1320 return -ENOMEM;
1322 data->acpi_dev = device;
1323 data->atk_handle = device->handle;
1324 INIT_LIST_HEAD(&data->sensor_list);
1325 data->disable_ec = false;
1327 buf.length = ACPI_ALLOCATE_BUFFER;
1328 ret = acpi_evaluate_object_typed(data->atk_handle, BOARD_ID, NULL,
1329 &buf, ACPI_TYPE_PACKAGE);
1330 if (ret != AE_OK) {
1331 dev_dbg(&device->dev, "atk: method MBIF not found\n");
1332 } else {
1333 obj = buf.pointer;
1334 if (obj->package.count >= 2) {
1335 union acpi_object *id = &obj->package.elements[1];
1336 if (id->type == ACPI_TYPE_STRING)
1337 dev_dbg(&device->dev, "board ID = %s\n",
1338 id->string.pointer);
1340 ACPI_FREE(buf.pointer);
1343 err = atk_probe_if(data);
1344 if (err) {
1345 dev_err(&device->dev, "No usable hwmon interface detected\n");
1346 goto out;
1349 if (data->old_interface) {
1350 dev_dbg(&device->dev, "Using old hwmon interface\n");
1351 err = atk_enumerate_old_hwmon(data);
1352 } else {
1353 dev_dbg(&device->dev, "Using new hwmon interface\n");
1354 err = atk_enumerate_new_hwmon(data);
1356 if (err < 0)
1357 goto out;
1358 if (err == 0) {
1359 dev_info(&device->dev,
1360 "No usable sensor detected, bailing out\n");
1361 err = -ENODEV;
1362 goto out;
1365 err = atk_register_hwmon(data);
1366 if (err)
1367 goto cleanup;
1369 atk_debugfs_init(data);
1371 device->driver_data = data;
1372 return 0;
1373 cleanup:
1374 atk_free_sensors(data);
1375 out:
1376 if (data->disable_ec)
1377 atk_ec_ctl(data, 0);
1378 kfree(data);
1379 return err;
1382 static int atk_remove(struct acpi_device *device, int type)
1384 struct atk_data *data = device->driver_data;
1385 dev_dbg(&device->dev, "removing...\n");
1387 device->driver_data = NULL;
1389 atk_debugfs_cleanup(data);
1391 atk_remove_files(data);
1392 atk_free_sensors(data);
1393 hwmon_device_unregister(data->hwmon_dev);
1395 if (data->disable_ec) {
1396 if (atk_ec_ctl(data, 0))
1397 dev_err(&device->dev, "Failed to disable EC\n");
1400 kfree(data);
1402 return 0;
1405 static int __init atk0110_init(void)
1407 int ret;
1409 ret = acpi_bus_register_driver(&atk_driver);
1410 if (ret)
1411 pr_info("atk: acpi_bus_register_driver failed: %d\n", ret);
1413 return ret;
1416 static void __exit atk0110_exit(void)
1418 acpi_bus_unregister_driver(&atk_driver);
1421 module_init(atk0110_init);
1422 module_exit(atk0110_exit);
1424 MODULE_LICENSE("GPL");