thp: clear_copy_huge_page
[linux/fpc-iii.git] / drivers / hwmon / asus_atk0110.c
blob2d68cf3c223b7cb401a76481d9064aa040ea2eef
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>
17 #include <acpi/acpi.h>
18 #include <acpi/acpixf.h>
19 #include <acpi/acpi_drivers.h>
20 #include <acpi/acpi_bus.h>
23 #define ATK_HID "ATK0110"
25 /* Minimum time between readings, enforced in order to avoid
26 * hogging the CPU.
28 #define CACHE_TIME HZ
30 #define BOARD_ID "MBIF"
31 #define METHOD_ENUMERATE "GGRP"
32 #define METHOD_READ "GITM"
33 #define METHOD_WRITE "SITM"
34 #define METHOD_OLD_READ_TMP "RTMP"
35 #define METHOD_OLD_READ_VLT "RVLT"
36 #define METHOD_OLD_READ_FAN "RFAN"
37 #define METHOD_OLD_ENUM_TMP "TSIF"
38 #define METHOD_OLD_ENUM_VLT "VSIF"
39 #define METHOD_OLD_ENUM_FAN "FSIF"
41 #define ATK_MUX_HWMON 0x00000006ULL
42 #define ATK_MUX_MGMT 0x00000011ULL
44 #define ATK_CLASS_MASK 0xff000000ULL
45 #define ATK_CLASS_FREQ_CTL 0x03000000ULL
46 #define ATK_CLASS_FAN_CTL 0x04000000ULL
47 #define ATK_CLASS_HWMON 0x06000000ULL
48 #define ATK_CLASS_MGMT 0x11000000ULL
50 #define ATK_TYPE_MASK 0x00ff0000ULL
51 #define HWMON_TYPE_VOLT 0x00020000ULL
52 #define HWMON_TYPE_TEMP 0x00030000ULL
53 #define HWMON_TYPE_FAN 0x00040000ULL
55 #define ATK_ELEMENT_ID_MASK 0x0000ffffULL
57 #define ATK_EC_ID 0x11060004ULL
59 enum atk_pack_member {
60 HWMON_PACK_FLAGS,
61 HWMON_PACK_NAME,
62 HWMON_PACK_LIMIT1,
63 HWMON_PACK_LIMIT2,
64 HWMON_PACK_ENABLE
67 /* New package format */
68 #define _HWMON_NEW_PACK_SIZE 7
69 #define _HWMON_NEW_PACK_FLAGS 0
70 #define _HWMON_NEW_PACK_NAME 1
71 #define _HWMON_NEW_PACK_UNK1 2
72 #define _HWMON_NEW_PACK_UNK2 3
73 #define _HWMON_NEW_PACK_LIMIT1 4
74 #define _HWMON_NEW_PACK_LIMIT2 5
75 #define _HWMON_NEW_PACK_ENABLE 6
77 /* Old package format */
78 #define _HWMON_OLD_PACK_SIZE 5
79 #define _HWMON_OLD_PACK_FLAGS 0
80 #define _HWMON_OLD_PACK_NAME 1
81 #define _HWMON_OLD_PACK_LIMIT1 2
82 #define _HWMON_OLD_PACK_LIMIT2 3
83 #define _HWMON_OLD_PACK_ENABLE 4
86 struct atk_data {
87 struct device *hwmon_dev;
88 acpi_handle atk_handle;
89 struct acpi_device *acpi_dev;
91 bool old_interface;
93 /* old interface */
94 acpi_handle rtmp_handle;
95 acpi_handle rvlt_handle;
96 acpi_handle rfan_handle;
97 /* new inteface */
98 acpi_handle enumerate_handle;
99 acpi_handle read_handle;
100 acpi_handle write_handle;
102 bool disable_ec;
104 int voltage_count;
105 int temperature_count;
106 int fan_count;
107 struct list_head sensor_list;
109 struct {
110 struct dentry *root;
111 u32 id;
112 } debugfs;
116 typedef ssize_t (*sysfs_show_func)(struct device *dev,
117 struct device_attribute *attr, char *buf);
119 static const struct acpi_device_id atk_ids[] = {
120 {ATK_HID, 0},
121 {"", 0},
123 MODULE_DEVICE_TABLE(acpi, atk_ids);
125 #define ATTR_NAME_SIZE 16 /* Worst case is "tempN_input" */
127 struct atk_sensor_data {
128 struct list_head list;
129 struct atk_data *data;
130 struct device_attribute label_attr;
131 struct device_attribute input_attr;
132 struct device_attribute limit1_attr;
133 struct device_attribute limit2_attr;
134 char label_attr_name[ATTR_NAME_SIZE];
135 char input_attr_name[ATTR_NAME_SIZE];
136 char limit1_attr_name[ATTR_NAME_SIZE];
137 char limit2_attr_name[ATTR_NAME_SIZE];
138 u64 id;
139 u64 type;
140 u64 limit1;
141 u64 limit2;
142 u64 cached_value;
143 unsigned long last_updated; /* in jiffies */
144 bool is_valid;
145 char const *acpi_name;
148 /* Return buffer format:
149 * [0-3] "value" is valid flag
150 * [4-7] value
151 * [8- ] unknown stuff on newer mobos
153 struct atk_acpi_ret_buffer {
154 u32 flags;
155 u32 value;
156 u8 data[];
159 /* Input buffer used for GITM and SITM methods */
160 struct atk_acpi_input_buf {
161 u32 id;
162 u32 param1;
163 u32 param2;
166 static int atk_add(struct acpi_device *device);
167 static int atk_remove(struct acpi_device *device, int type);
168 static void atk_print_sensor(struct atk_data *data, union acpi_object *obj);
169 static int atk_read_value(struct atk_sensor_data *sensor, u64 *value);
170 static void atk_free_sensors(struct atk_data *data);
172 static struct acpi_driver atk_driver = {
173 .name = ATK_HID,
174 .class = "hwmon",
175 .ids = atk_ids,
176 .ops = {
177 .add = atk_add,
178 .remove = atk_remove,
182 #define input_to_atk_sensor(attr) \
183 container_of(attr, struct atk_sensor_data, input_attr)
185 #define label_to_atk_sensor(attr) \
186 container_of(attr, struct atk_sensor_data, label_attr)
188 #define limit1_to_atk_sensor(attr) \
189 container_of(attr, struct atk_sensor_data, limit1_attr)
191 #define limit2_to_atk_sensor(attr) \
192 container_of(attr, struct atk_sensor_data, limit2_attr)
194 static ssize_t atk_input_show(struct device *dev,
195 struct device_attribute *attr, char *buf)
197 struct atk_sensor_data *s = input_to_atk_sensor(attr);
198 u64 value;
199 int err;
201 err = atk_read_value(s, &value);
202 if (err)
203 return err;
205 if (s->type == HWMON_TYPE_TEMP)
206 /* ACPI returns decidegree */
207 value *= 100;
209 return sprintf(buf, "%llu\n", value);
212 static ssize_t atk_label_show(struct device *dev,
213 struct device_attribute *attr, char *buf)
215 struct atk_sensor_data *s = label_to_atk_sensor(attr);
217 return sprintf(buf, "%s\n", s->acpi_name);
220 static ssize_t atk_limit1_show(struct device *dev,
221 struct device_attribute *attr, char *buf)
223 struct atk_sensor_data *s = limit1_to_atk_sensor(attr);
224 u64 value = s->limit1;
226 if (s->type == HWMON_TYPE_TEMP)
227 value *= 100;
229 return sprintf(buf, "%lld\n", value);
232 static ssize_t atk_limit2_show(struct device *dev,
233 struct device_attribute *attr, char *buf)
235 struct atk_sensor_data *s = limit2_to_atk_sensor(attr);
236 u64 value = s->limit2;
238 if (s->type == HWMON_TYPE_TEMP)
239 value *= 100;
241 return sprintf(buf, "%lld\n", value);
244 static ssize_t atk_name_show(struct device *dev,
245 struct device_attribute *attr, char *buf)
247 return sprintf(buf, "atk0110\n");
249 static struct device_attribute atk_name_attr =
250 __ATTR(name, 0444, atk_name_show, NULL);
252 static void atk_init_attribute(struct device_attribute *attr, char *name,
253 sysfs_show_func show)
255 attr->attr.name = name;
256 attr->attr.mode = 0444;
257 attr->show = show;
258 attr->store = NULL;
262 static union acpi_object *atk_get_pack_member(struct atk_data *data,
263 union acpi_object *pack,
264 enum atk_pack_member m)
266 bool old_if = data->old_interface;
267 int offset;
269 switch (m) {
270 case HWMON_PACK_FLAGS:
271 offset = old_if ? _HWMON_OLD_PACK_FLAGS : _HWMON_NEW_PACK_FLAGS;
272 break;
273 case HWMON_PACK_NAME:
274 offset = old_if ? _HWMON_OLD_PACK_NAME : _HWMON_NEW_PACK_NAME;
275 break;
276 case HWMON_PACK_LIMIT1:
277 offset = old_if ? _HWMON_OLD_PACK_LIMIT1 :
278 _HWMON_NEW_PACK_LIMIT1;
279 break;
280 case HWMON_PACK_LIMIT2:
281 offset = old_if ? _HWMON_OLD_PACK_LIMIT2 :
282 _HWMON_NEW_PACK_LIMIT2;
283 break;
284 case HWMON_PACK_ENABLE:
285 offset = old_if ? _HWMON_OLD_PACK_ENABLE :
286 _HWMON_NEW_PACK_ENABLE;
287 break;
288 default:
289 return NULL;
292 return &pack->package.elements[offset];
296 /* New package format is:
297 * - flag (int)
298 * class - used for de-muxing the request to the correct GITn
299 * type (volt, temp, fan)
300 * sensor id |
301 * sensor id - used for de-muxing the request _inside_ the GITn
302 * - name (str)
303 * - unknown (int)
304 * - unknown (int)
305 * - limit1 (int)
306 * - limit2 (int)
307 * - enable (int)
309 * The old package has the same format but it's missing the two unknown fields.
311 static int validate_hwmon_pack(struct atk_data *data, union acpi_object *obj)
313 struct device *dev = &data->acpi_dev->dev;
314 union acpi_object *tmp;
315 bool old_if = data->old_interface;
316 int const expected_size = old_if ? _HWMON_OLD_PACK_SIZE :
317 _HWMON_NEW_PACK_SIZE;
319 if (obj->type != ACPI_TYPE_PACKAGE) {
320 dev_warn(dev, "Invalid type: %d\n", obj->type);
321 return -EINVAL;
324 if (obj->package.count != expected_size) {
325 dev_warn(dev, "Invalid package size: %d, expected: %d\n",
326 obj->package.count, expected_size);
327 return -EINVAL;
330 tmp = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS);
331 if (tmp->type != ACPI_TYPE_INTEGER) {
332 dev_warn(dev, "Invalid type (flag): %d\n", tmp->type);
333 return -EINVAL;
336 tmp = atk_get_pack_member(data, obj, HWMON_PACK_NAME);
337 if (tmp->type != ACPI_TYPE_STRING) {
338 dev_warn(dev, "Invalid type (name): %d\n", tmp->type);
339 return -EINVAL;
342 /* Don't check... we don't know what they're useful for anyway */
343 #if 0
344 tmp = &obj->package.elements[HWMON_PACK_UNK1];
345 if (tmp->type != ACPI_TYPE_INTEGER) {
346 dev_warn(dev, "Invalid type (unk1): %d\n", tmp->type);
347 return -EINVAL;
350 tmp = &obj->package.elements[HWMON_PACK_UNK2];
351 if (tmp->type != ACPI_TYPE_INTEGER) {
352 dev_warn(dev, "Invalid type (unk2): %d\n", tmp->type);
353 return -EINVAL;
355 #endif
357 tmp = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT1);
358 if (tmp->type != ACPI_TYPE_INTEGER) {
359 dev_warn(dev, "Invalid type (limit1): %d\n", tmp->type);
360 return -EINVAL;
363 tmp = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT2);
364 if (tmp->type != ACPI_TYPE_INTEGER) {
365 dev_warn(dev, "Invalid type (limit2): %d\n", tmp->type);
366 return -EINVAL;
369 tmp = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE);
370 if (tmp->type != ACPI_TYPE_INTEGER) {
371 dev_warn(dev, "Invalid type (enable): %d\n", tmp->type);
372 return -EINVAL;
375 atk_print_sensor(data, obj);
377 return 0;
380 #ifdef DEBUG
381 static char const *atk_sensor_type(union acpi_object *flags)
383 u64 type = flags->integer.value & ATK_TYPE_MASK;
384 char const *what;
386 switch (type) {
387 case HWMON_TYPE_VOLT:
388 what = "voltage";
389 break;
390 case HWMON_TYPE_TEMP:
391 what = "temperature";
392 break;
393 case HWMON_TYPE_FAN:
394 what = "fan";
395 break;
396 default:
397 what = "unknown";
398 break;
401 return what;
403 #endif
405 static void atk_print_sensor(struct atk_data *data, union acpi_object *obj)
407 #ifdef DEBUG
408 struct device *dev = &data->acpi_dev->dev;
409 union acpi_object *flags;
410 union acpi_object *name;
411 union acpi_object *limit1;
412 union acpi_object *limit2;
413 union acpi_object *enable;
414 char const *what;
416 flags = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS);
417 name = atk_get_pack_member(data, obj, HWMON_PACK_NAME);
418 limit1 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT1);
419 limit2 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT2);
420 enable = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE);
422 what = atk_sensor_type(flags);
424 dev_dbg(dev, "%s: %#llx %s [%llu-%llu] %s\n", what,
425 flags->integer.value,
426 name->string.pointer,
427 limit1->integer.value, limit2->integer.value,
428 enable->integer.value ? "enabled" : "disabled");
429 #endif
432 static int atk_read_value_old(struct atk_sensor_data *sensor, u64 *value)
434 struct atk_data *data = sensor->data;
435 struct device *dev = &data->acpi_dev->dev;
436 struct acpi_object_list params;
437 union acpi_object id;
438 acpi_status status;
439 acpi_handle method;
441 switch (sensor->type) {
442 case HWMON_TYPE_VOLT:
443 method = data->rvlt_handle;
444 break;
445 case HWMON_TYPE_TEMP:
446 method = data->rtmp_handle;
447 break;
448 case HWMON_TYPE_FAN:
449 method = data->rfan_handle;
450 break;
451 default:
452 return -EINVAL;
455 id.type = ACPI_TYPE_INTEGER;
456 id.integer.value = sensor->id;
458 params.count = 1;
459 params.pointer = &id;
461 status = acpi_evaluate_integer(method, NULL, &params, value);
462 if (status != AE_OK) {
463 dev_warn(dev, "%s: ACPI exception: %s\n", __func__,
464 acpi_format_exception(status));
465 return -EIO;
468 return 0;
471 static union acpi_object *atk_ggrp(struct atk_data *data, u16 mux)
473 struct device *dev = &data->acpi_dev->dev;
474 struct acpi_buffer buf;
475 acpi_status ret;
476 struct acpi_object_list params;
477 union acpi_object id;
478 union acpi_object *pack;
480 id.type = ACPI_TYPE_INTEGER;
481 id.integer.value = mux;
482 params.count = 1;
483 params.pointer = &id;
485 buf.length = ACPI_ALLOCATE_BUFFER;
486 ret = acpi_evaluate_object(data->enumerate_handle, NULL, &params, &buf);
487 if (ret != AE_OK) {
488 dev_err(dev, "GGRP[%#x] ACPI exception: %s\n", mux,
489 acpi_format_exception(ret));
490 return ERR_PTR(-EIO);
492 pack = buf.pointer;
493 if (pack->type != ACPI_TYPE_PACKAGE) {
494 /* Execution was successful, but the id was not found */
495 ACPI_FREE(pack);
496 return ERR_PTR(-ENOENT);
499 if (pack->package.count < 1) {
500 dev_err(dev, "GGRP[%#x] package is too small\n", mux);
501 ACPI_FREE(pack);
502 return ERR_PTR(-EIO);
504 return pack;
507 static union acpi_object *atk_gitm(struct atk_data *data, u64 id)
509 struct device *dev = &data->acpi_dev->dev;
510 struct atk_acpi_input_buf buf;
511 union acpi_object tmp;
512 struct acpi_object_list params;
513 struct acpi_buffer ret;
514 union acpi_object *obj;
515 acpi_status status;
517 buf.id = id;
518 buf.param1 = 0;
519 buf.param2 = 0;
521 tmp.type = ACPI_TYPE_BUFFER;
522 tmp.buffer.pointer = (u8 *)&buf;
523 tmp.buffer.length = sizeof(buf);
525 params.count = 1;
526 params.pointer = (void *)&tmp;
528 ret.length = ACPI_ALLOCATE_BUFFER;
529 status = acpi_evaluate_object_typed(data->read_handle, NULL, &params,
530 &ret, ACPI_TYPE_BUFFER);
531 if (status != AE_OK) {
532 dev_warn(dev, "GITM[%#llx] ACPI exception: %s\n", id,
533 acpi_format_exception(status));
534 return ERR_PTR(-EIO);
536 obj = ret.pointer;
538 /* Sanity check */
539 if (obj->buffer.length < 8) {
540 dev_warn(dev, "Unexpected ASBF length: %u\n",
541 obj->buffer.length);
542 ACPI_FREE(obj);
543 return ERR_PTR(-EIO);
545 return obj;
548 static union acpi_object *atk_sitm(struct atk_data *data,
549 struct atk_acpi_input_buf *buf)
551 struct device *dev = &data->acpi_dev->dev;
552 struct acpi_object_list params;
553 union acpi_object tmp;
554 struct acpi_buffer ret;
555 union acpi_object *obj;
556 acpi_status status;
558 tmp.type = ACPI_TYPE_BUFFER;
559 tmp.buffer.pointer = (u8 *)buf;
560 tmp.buffer.length = sizeof(*buf);
562 params.count = 1;
563 params.pointer = &tmp;
565 ret.length = ACPI_ALLOCATE_BUFFER;
566 status = acpi_evaluate_object_typed(data->write_handle, NULL, &params,
567 &ret, ACPI_TYPE_BUFFER);
568 if (status != AE_OK) {
569 dev_warn(dev, "SITM[%#x] ACPI exception: %s\n", buf->id,
570 acpi_format_exception(status));
571 return ERR_PTR(-EIO);
573 obj = ret.pointer;
575 /* Sanity check */
576 if (obj->buffer.length < 8) {
577 dev_warn(dev, "Unexpected ASBF length: %u\n",
578 obj->buffer.length);
579 ACPI_FREE(obj);
580 return ERR_PTR(-EIO);
582 return obj;
585 static int atk_read_value_new(struct atk_sensor_data *sensor, u64 *value)
587 struct atk_data *data = sensor->data;
588 struct device *dev = &data->acpi_dev->dev;
589 union acpi_object *obj;
590 struct atk_acpi_ret_buffer *buf;
591 int err = 0;
593 obj = atk_gitm(data, sensor->id);
594 if (IS_ERR(obj))
595 return PTR_ERR(obj);
597 buf = (struct atk_acpi_ret_buffer *)obj->buffer.pointer;
598 if (buf->flags == 0) {
599 /* The reading is not valid, possible causes:
600 * - sensor failure
601 * - enumeration was FUBAR (and we didn't notice)
603 dev_warn(dev, "Read failed, sensor = %#llx\n", sensor->id);
604 err = -EIO;
605 goto out;
608 *value = buf->value;
609 out:
610 ACPI_FREE(obj);
611 return err;
614 static int atk_read_value(struct atk_sensor_data *sensor, u64 *value)
616 int err;
618 if (!sensor->is_valid ||
619 time_after(jiffies, sensor->last_updated + CACHE_TIME)) {
620 if (sensor->data->old_interface)
621 err = atk_read_value_old(sensor, value);
622 else
623 err = atk_read_value_new(sensor, value);
625 sensor->is_valid = true;
626 sensor->last_updated = jiffies;
627 sensor->cached_value = *value;
628 } else {
629 *value = sensor->cached_value;
630 err = 0;
633 return err;
636 #ifdef CONFIG_DEBUG_FS
637 static int atk_debugfs_gitm_get(void *p, u64 *val)
639 struct atk_data *data = p;
640 union acpi_object *ret;
641 struct atk_acpi_ret_buffer *buf;
642 int err = 0;
644 if (!data->read_handle)
645 return -ENODEV;
647 if (!data->debugfs.id)
648 return -EINVAL;
650 ret = atk_gitm(data, data->debugfs.id);
651 if (IS_ERR(ret))
652 return PTR_ERR(ret);
654 buf = (struct atk_acpi_ret_buffer *)ret->buffer.pointer;
655 if (buf->flags)
656 *val = buf->value;
657 else
658 err = -EIO;
660 return err;
663 DEFINE_SIMPLE_ATTRIBUTE(atk_debugfs_gitm,
664 atk_debugfs_gitm_get,
665 NULL,
666 "0x%08llx\n")
668 static int atk_acpi_print(char *buf, size_t sz, union acpi_object *obj)
670 int ret = 0;
672 switch (obj->type) {
673 case ACPI_TYPE_INTEGER:
674 ret = snprintf(buf, sz, "0x%08llx\n", obj->integer.value);
675 break;
676 case ACPI_TYPE_STRING:
677 ret = snprintf(buf, sz, "%s\n", obj->string.pointer);
678 break;
681 return ret;
684 static void atk_pack_print(char *buf, size_t sz, union acpi_object *pack)
686 int ret;
687 int i;
689 for (i = 0; i < pack->package.count; i++) {
690 union acpi_object *obj = &pack->package.elements[i];
692 ret = atk_acpi_print(buf, sz, obj);
693 if (ret >= sz)
694 break;
695 buf += ret;
696 sz -= ret;
700 static int atk_debugfs_ggrp_open(struct inode *inode, struct file *file)
702 struct atk_data *data = inode->i_private;
703 char *buf = NULL;
704 union acpi_object *ret;
705 u8 cls;
706 int i;
708 if (!data->enumerate_handle)
709 return -ENODEV;
710 if (!data->debugfs.id)
711 return -EINVAL;
713 cls = (data->debugfs.id & 0xff000000) >> 24;
714 ret = atk_ggrp(data, cls);
715 if (IS_ERR(ret))
716 return PTR_ERR(ret);
718 for (i = 0; i < ret->package.count; i++) {
719 union acpi_object *pack = &ret->package.elements[i];
720 union acpi_object *id;
722 if (pack->type != ACPI_TYPE_PACKAGE)
723 continue;
724 if (!pack->package.count)
725 continue;
726 id = &pack->package.elements[0];
727 if (id->integer.value == data->debugfs.id) {
728 /* Print the package */
729 buf = kzalloc(512, GFP_KERNEL);
730 if (!buf) {
731 ACPI_FREE(ret);
732 return -ENOMEM;
734 atk_pack_print(buf, 512, pack);
735 break;
738 ACPI_FREE(ret);
740 if (!buf)
741 return -EINVAL;
743 file->private_data = buf;
745 return nonseekable_open(inode, file);
748 static ssize_t atk_debugfs_ggrp_read(struct file *file, char __user *buf,
749 size_t count, loff_t *pos)
751 char *str = file->private_data;
752 size_t len = strlen(str);
754 return simple_read_from_buffer(buf, count, pos, str, len);
757 static int atk_debugfs_ggrp_release(struct inode *inode, struct file *file)
759 kfree(file->private_data);
760 return 0;
763 static const struct file_operations atk_debugfs_ggrp_fops = {
764 .read = atk_debugfs_ggrp_read,
765 .open = atk_debugfs_ggrp_open,
766 .release = atk_debugfs_ggrp_release,
767 .llseek = no_llseek,
770 static void atk_debugfs_init(struct atk_data *data)
772 struct dentry *d;
773 struct dentry *f;
775 data->debugfs.id = 0;
777 d = debugfs_create_dir("asus_atk0110", NULL);
778 if (!d || IS_ERR(d))
779 return;
781 f = debugfs_create_x32("id", S_IRUSR | S_IWUSR, d, &data->debugfs.id);
782 if (!f || IS_ERR(f))
783 goto cleanup;
785 f = debugfs_create_file("gitm", S_IRUSR, d, data,
786 &atk_debugfs_gitm);
787 if (!f || IS_ERR(f))
788 goto cleanup;
790 f = debugfs_create_file("ggrp", S_IRUSR, d, data,
791 &atk_debugfs_ggrp_fops);
792 if (!f || IS_ERR(f))
793 goto cleanup;
795 data->debugfs.root = d;
797 return;
798 cleanup:
799 debugfs_remove_recursive(d);
802 static void atk_debugfs_cleanup(struct atk_data *data)
804 debugfs_remove_recursive(data->debugfs.root);
807 #else /* CONFIG_DEBUG_FS */
809 static void atk_debugfs_init(struct atk_data *data)
813 static void atk_debugfs_cleanup(struct atk_data *data)
816 #endif
818 static int atk_add_sensor(struct atk_data *data, union acpi_object *obj)
820 struct device *dev = &data->acpi_dev->dev;
821 union acpi_object *flags;
822 union acpi_object *name;
823 union acpi_object *limit1;
824 union acpi_object *limit2;
825 union acpi_object *enable;
826 struct atk_sensor_data *sensor;
827 char const *base_name;
828 char const *limit1_name;
829 char const *limit2_name;
830 u64 type;
831 int err;
832 int *num;
833 int start;
835 if (obj->type != ACPI_TYPE_PACKAGE) {
836 /* wft is this? */
837 dev_warn(dev, "Unknown type for ACPI object: (%d)\n",
838 obj->type);
839 return -EINVAL;
842 err = validate_hwmon_pack(data, obj);
843 if (err)
844 return err;
846 /* Ok, we have a valid hwmon package */
847 type = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS)->integer.value
848 & ATK_TYPE_MASK;
850 switch (type) {
851 case HWMON_TYPE_VOLT:
852 base_name = "in";
853 limit1_name = "min";
854 limit2_name = "max";
855 num = &data->voltage_count;
856 start = 0;
857 break;
858 case HWMON_TYPE_TEMP:
859 base_name = "temp";
860 limit1_name = "max";
861 limit2_name = "crit";
862 num = &data->temperature_count;
863 start = 1;
864 break;
865 case HWMON_TYPE_FAN:
866 base_name = "fan";
867 limit1_name = "min";
868 limit2_name = "max";
869 num = &data->fan_count;
870 start = 1;
871 break;
872 default:
873 dev_warn(dev, "Unknown sensor type: %#llx\n", type);
874 return -EINVAL;
877 enable = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE);
878 if (!enable->integer.value)
879 /* sensor is disabled */
880 return 0;
882 flags = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS);
883 name = atk_get_pack_member(data, obj, HWMON_PACK_NAME);
884 limit1 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT1);
885 limit2 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT2);
887 sensor = kzalloc(sizeof(*sensor), GFP_KERNEL);
888 if (!sensor)
889 return -ENOMEM;
891 sensor->acpi_name = kstrdup(name->string.pointer, GFP_KERNEL);
892 if (!sensor->acpi_name) {
893 err = -ENOMEM;
894 goto out;
897 INIT_LIST_HEAD(&sensor->list);
898 sensor->type = type;
899 sensor->data = data;
900 sensor->id = flags->integer.value;
901 sensor->limit1 = limit1->integer.value;
902 if (data->old_interface)
903 sensor->limit2 = limit2->integer.value;
904 else
905 /* The upper limit is expressed as delta from lower limit */
906 sensor->limit2 = sensor->limit1 + limit2->integer.value;
908 snprintf(sensor->input_attr_name, ATTR_NAME_SIZE,
909 "%s%d_input", base_name, start + *num);
910 atk_init_attribute(&sensor->input_attr,
911 sensor->input_attr_name,
912 atk_input_show);
914 snprintf(sensor->label_attr_name, ATTR_NAME_SIZE,
915 "%s%d_label", base_name, start + *num);
916 atk_init_attribute(&sensor->label_attr,
917 sensor->label_attr_name,
918 atk_label_show);
920 snprintf(sensor->limit1_attr_name, ATTR_NAME_SIZE,
921 "%s%d_%s", base_name, start + *num, limit1_name);
922 atk_init_attribute(&sensor->limit1_attr,
923 sensor->limit1_attr_name,
924 atk_limit1_show);
926 snprintf(sensor->limit2_attr_name, ATTR_NAME_SIZE,
927 "%s%d_%s", base_name, start + *num, limit2_name);
928 atk_init_attribute(&sensor->limit2_attr,
929 sensor->limit2_attr_name,
930 atk_limit2_show);
932 list_add(&sensor->list, &data->sensor_list);
933 (*num)++;
935 return 1;
936 out:
937 kfree(sensor->acpi_name);
938 kfree(sensor);
939 return err;
942 static int atk_enumerate_old_hwmon(struct atk_data *data)
944 struct device *dev = &data->acpi_dev->dev;
945 struct acpi_buffer buf;
946 union acpi_object *pack;
947 acpi_status status;
948 int i, ret;
949 int count = 0;
951 /* Voltages */
952 buf.length = ACPI_ALLOCATE_BUFFER;
953 status = acpi_evaluate_object_typed(data->atk_handle,
954 METHOD_OLD_ENUM_VLT, NULL, &buf, ACPI_TYPE_PACKAGE);
955 if (status != AE_OK) {
956 dev_warn(dev, METHOD_OLD_ENUM_VLT ": ACPI exception: %s\n",
957 acpi_format_exception(status));
959 return -ENODEV;
962 pack = buf.pointer;
963 for (i = 1; i < pack->package.count; i++) {
964 union acpi_object *obj = &pack->package.elements[i];
966 ret = atk_add_sensor(data, obj);
967 if (ret > 0)
968 count++;
970 ACPI_FREE(buf.pointer);
972 /* Temperatures */
973 buf.length = ACPI_ALLOCATE_BUFFER;
974 status = acpi_evaluate_object_typed(data->atk_handle,
975 METHOD_OLD_ENUM_TMP, NULL, &buf, ACPI_TYPE_PACKAGE);
976 if (status != AE_OK) {
977 dev_warn(dev, METHOD_OLD_ENUM_TMP ": ACPI exception: %s\n",
978 acpi_format_exception(status));
980 ret = -ENODEV;
981 goto cleanup;
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 /* Fans */
995 buf.length = ACPI_ALLOCATE_BUFFER;
996 status = acpi_evaluate_object_typed(data->atk_handle,
997 METHOD_OLD_ENUM_FAN, NULL, &buf, ACPI_TYPE_PACKAGE);
998 if (status != AE_OK) {
999 dev_warn(dev, METHOD_OLD_ENUM_FAN ": 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 return count;
1017 cleanup:
1018 atk_free_sensors(data);
1019 return ret;
1022 static int atk_ec_present(struct atk_data *data)
1024 struct device *dev = &data->acpi_dev->dev;
1025 union acpi_object *pack;
1026 union acpi_object *ec;
1027 int ret;
1028 int i;
1030 pack = atk_ggrp(data, ATK_MUX_MGMT);
1031 if (IS_ERR(pack)) {
1032 if (PTR_ERR(pack) == -ENOENT) {
1033 /* The MGMT class does not exists - that's ok */
1034 dev_dbg(dev, "Class %#llx not found\n", ATK_MUX_MGMT);
1035 return 0;
1037 return PTR_ERR(pack);
1040 /* Search the EC */
1041 ec = NULL;
1042 for (i = 0; i < pack->package.count; i++) {
1043 union acpi_object *obj = &pack->package.elements[i];
1044 union acpi_object *id;
1046 if (obj->type != ACPI_TYPE_PACKAGE)
1047 continue;
1049 id = &obj->package.elements[0];
1050 if (id->type != ACPI_TYPE_INTEGER)
1051 continue;
1053 if (id->integer.value == ATK_EC_ID) {
1054 ec = obj;
1055 break;
1059 ret = (ec != NULL);
1060 if (!ret)
1061 /* The system has no EC */
1062 dev_dbg(dev, "EC not found\n");
1064 ACPI_FREE(pack);
1065 return ret;
1068 static int atk_ec_enabled(struct atk_data *data)
1070 struct device *dev = &data->acpi_dev->dev;
1071 union acpi_object *obj;
1072 struct atk_acpi_ret_buffer *buf;
1073 int err;
1075 obj = atk_gitm(data, ATK_EC_ID);
1076 if (IS_ERR(obj)) {
1077 dev_err(dev, "Unable to query EC status\n");
1078 return PTR_ERR(obj);
1080 buf = (struct atk_acpi_ret_buffer *)obj->buffer.pointer;
1082 if (buf->flags == 0) {
1083 dev_err(dev, "Unable to query EC status\n");
1084 err = -EIO;
1085 } else {
1086 err = (buf->value != 0);
1087 dev_dbg(dev, "EC is %sabled\n",
1088 err ? "en" : "dis");
1091 ACPI_FREE(obj);
1092 return err;
1095 static int atk_ec_ctl(struct atk_data *data, int enable)
1097 struct device *dev = &data->acpi_dev->dev;
1098 union acpi_object *obj;
1099 struct atk_acpi_input_buf sitm;
1100 struct atk_acpi_ret_buffer *ec_ret;
1101 int err = 0;
1103 sitm.id = ATK_EC_ID;
1104 sitm.param1 = enable;
1105 sitm.param2 = 0;
1107 obj = atk_sitm(data, &sitm);
1108 if (IS_ERR(obj)) {
1109 dev_err(dev, "Failed to %sable the EC\n",
1110 enable ? "en" : "dis");
1111 return PTR_ERR(obj);
1113 ec_ret = (struct atk_acpi_ret_buffer *)obj->buffer.pointer;
1114 if (ec_ret->flags == 0) {
1115 dev_err(dev, "Failed to %sable the EC\n",
1116 enable ? "en" : "dis");
1117 err = -EIO;
1118 } else {
1119 dev_info(dev, "EC %sabled\n",
1120 enable ? "en" : "dis");
1123 ACPI_FREE(obj);
1124 return err;
1127 static int atk_enumerate_new_hwmon(struct atk_data *data)
1129 struct device *dev = &data->acpi_dev->dev;
1130 union acpi_object *pack;
1131 int err;
1132 int i;
1134 err = atk_ec_present(data);
1135 if (err < 0)
1136 return err;
1137 if (err) {
1138 err = atk_ec_enabled(data);
1139 if (err < 0)
1140 return err;
1141 /* If the EC was disabled we will disable it again on unload */
1142 data->disable_ec = err;
1144 err = atk_ec_ctl(data, 1);
1145 if (err) {
1146 data->disable_ec = false;
1147 return err;
1151 dev_dbg(dev, "Enumerating hwmon sensors\n");
1153 pack = atk_ggrp(data, ATK_MUX_HWMON);
1154 if (IS_ERR(pack))
1155 return PTR_ERR(pack);
1157 for (i = 0; i < pack->package.count; i++) {
1158 union acpi_object *obj = &pack->package.elements[i];
1160 atk_add_sensor(data, obj);
1163 err = data->voltage_count + data->temperature_count + data->fan_count;
1165 ACPI_FREE(pack);
1166 return err;
1169 static int atk_create_files(struct atk_data *data)
1171 struct atk_sensor_data *s;
1172 int err;
1174 list_for_each_entry(s, &data->sensor_list, list) {
1175 sysfs_attr_init(&s->input_attr.attr);
1176 err = device_create_file(data->hwmon_dev, &s->input_attr);
1177 if (err)
1178 return err;
1179 sysfs_attr_init(&s->label_attr.attr);
1180 err = device_create_file(data->hwmon_dev, &s->label_attr);
1181 if (err)
1182 return err;
1183 sysfs_attr_init(&s->limit1_attr.attr);
1184 err = device_create_file(data->hwmon_dev, &s->limit1_attr);
1185 if (err)
1186 return err;
1187 sysfs_attr_init(&s->limit2_attr.attr);
1188 err = device_create_file(data->hwmon_dev, &s->limit2_attr);
1189 if (err)
1190 return err;
1193 err = device_create_file(data->hwmon_dev, &atk_name_attr);
1195 return err;
1198 static void atk_remove_files(struct atk_data *data)
1200 struct atk_sensor_data *s;
1202 list_for_each_entry(s, &data->sensor_list, list) {
1203 device_remove_file(data->hwmon_dev, &s->input_attr);
1204 device_remove_file(data->hwmon_dev, &s->label_attr);
1205 device_remove_file(data->hwmon_dev, &s->limit1_attr);
1206 device_remove_file(data->hwmon_dev, &s->limit2_attr);
1208 device_remove_file(data->hwmon_dev, &atk_name_attr);
1211 static void atk_free_sensors(struct atk_data *data)
1213 struct list_head *head = &data->sensor_list;
1214 struct atk_sensor_data *s, *tmp;
1216 list_for_each_entry_safe(s, tmp, head, list) {
1217 kfree(s->acpi_name);
1218 kfree(s);
1222 static int atk_register_hwmon(struct atk_data *data)
1224 struct device *dev = &data->acpi_dev->dev;
1225 int err;
1227 dev_dbg(dev, "registering hwmon device\n");
1228 data->hwmon_dev = hwmon_device_register(dev);
1229 if (IS_ERR(data->hwmon_dev))
1230 return PTR_ERR(data->hwmon_dev);
1232 dev_dbg(dev, "populating sysfs directory\n");
1233 err = atk_create_files(data);
1234 if (err)
1235 goto remove;
1237 return 0;
1238 remove:
1239 /* Cleanup the registered files */
1240 atk_remove_files(data);
1241 hwmon_device_unregister(data->hwmon_dev);
1242 return err;
1245 static int atk_probe_if(struct atk_data *data)
1247 struct device *dev = &data->acpi_dev->dev;
1248 acpi_handle ret;
1249 acpi_status status;
1250 int err = 0;
1252 /* RTMP: read temperature */
1253 status = acpi_get_handle(data->atk_handle, METHOD_OLD_READ_TMP, &ret);
1254 if (ACPI_SUCCESS(status))
1255 data->rtmp_handle = ret;
1256 else
1257 dev_dbg(dev, "method " METHOD_OLD_READ_TMP " not found: %s\n",
1258 acpi_format_exception(status));
1260 /* RVLT: read voltage */
1261 status = acpi_get_handle(data->atk_handle, METHOD_OLD_READ_VLT, &ret);
1262 if (ACPI_SUCCESS(status))
1263 data->rvlt_handle = ret;
1264 else
1265 dev_dbg(dev, "method " METHOD_OLD_READ_VLT " not found: %s\n",
1266 acpi_format_exception(status));
1268 /* RFAN: read fan status */
1269 status = acpi_get_handle(data->atk_handle, METHOD_OLD_READ_FAN, &ret);
1270 if (ACPI_SUCCESS(status))
1271 data->rfan_handle = ret;
1272 else
1273 dev_dbg(dev, "method " METHOD_OLD_READ_FAN " not found: %s\n",
1274 acpi_format_exception(status));
1276 /* Enumeration */
1277 status = acpi_get_handle(data->atk_handle, METHOD_ENUMERATE, &ret);
1278 if (ACPI_SUCCESS(status))
1279 data->enumerate_handle = ret;
1280 else
1281 dev_dbg(dev, "method " METHOD_ENUMERATE " not found: %s\n",
1282 acpi_format_exception(status));
1284 /* De-multiplexer (read) */
1285 status = acpi_get_handle(data->atk_handle, METHOD_READ, &ret);
1286 if (ACPI_SUCCESS(status))
1287 data->read_handle = ret;
1288 else
1289 dev_dbg(dev, "method " METHOD_READ " not found: %s\n",
1290 acpi_format_exception(status));
1292 /* De-multiplexer (write) */
1293 status = acpi_get_handle(data->atk_handle, METHOD_WRITE, &ret);
1294 if (ACPI_SUCCESS(status))
1295 data->write_handle = ret;
1296 else
1297 dev_dbg(dev, "method " METHOD_WRITE " not found: %s\n",
1298 acpi_format_exception(status));
1300 /* Check for hwmon methods: first check "old" style methods; note that
1301 * both may be present: in this case we stick to the old interface;
1302 * analysis of multiple DSDTs indicates that when both interfaces
1303 * are present the new one (GGRP/GITM) is not functional.
1305 if (data->rtmp_handle && data->rvlt_handle && data->rfan_handle)
1306 data->old_interface = true;
1307 else if (data->enumerate_handle && data->read_handle &&
1308 data->write_handle)
1309 data->old_interface = false;
1310 else
1311 err = -ENODEV;
1313 return err;
1316 static int atk_add(struct acpi_device *device)
1318 acpi_status ret;
1319 int err;
1320 struct acpi_buffer buf;
1321 union acpi_object *obj;
1322 struct atk_data *data;
1324 dev_dbg(&device->dev, "adding...\n");
1326 data = kzalloc(sizeof(*data), GFP_KERNEL);
1327 if (!data)
1328 return -ENOMEM;
1330 data->acpi_dev = device;
1331 data->atk_handle = device->handle;
1332 INIT_LIST_HEAD(&data->sensor_list);
1333 data->disable_ec = false;
1335 buf.length = ACPI_ALLOCATE_BUFFER;
1336 ret = acpi_evaluate_object_typed(data->atk_handle, BOARD_ID, NULL,
1337 &buf, ACPI_TYPE_PACKAGE);
1338 if (ret != AE_OK) {
1339 dev_dbg(&device->dev, "atk: method MBIF not found\n");
1340 } else {
1341 obj = buf.pointer;
1342 if (obj->package.count >= 2) {
1343 union acpi_object *id = &obj->package.elements[1];
1344 if (id->type == ACPI_TYPE_STRING)
1345 dev_dbg(&device->dev, "board ID = %s\n",
1346 id->string.pointer);
1348 ACPI_FREE(buf.pointer);
1351 err = atk_probe_if(data);
1352 if (err) {
1353 dev_err(&device->dev, "No usable hwmon interface detected\n");
1354 goto out;
1357 if (data->old_interface) {
1358 dev_dbg(&device->dev, "Using old hwmon interface\n");
1359 err = atk_enumerate_old_hwmon(data);
1360 } else {
1361 dev_dbg(&device->dev, "Using new hwmon interface\n");
1362 err = atk_enumerate_new_hwmon(data);
1364 if (err < 0)
1365 goto out;
1366 if (err == 0) {
1367 dev_info(&device->dev,
1368 "No usable sensor detected, bailing out\n");
1369 err = -ENODEV;
1370 goto out;
1373 err = atk_register_hwmon(data);
1374 if (err)
1375 goto cleanup;
1377 atk_debugfs_init(data);
1379 device->driver_data = data;
1380 return 0;
1381 cleanup:
1382 atk_free_sensors(data);
1383 out:
1384 if (data->disable_ec)
1385 atk_ec_ctl(data, 0);
1386 kfree(data);
1387 return err;
1390 static int atk_remove(struct acpi_device *device, int type)
1392 struct atk_data *data = device->driver_data;
1393 dev_dbg(&device->dev, "removing...\n");
1395 device->driver_data = NULL;
1397 atk_debugfs_cleanup(data);
1399 atk_remove_files(data);
1400 atk_free_sensors(data);
1401 hwmon_device_unregister(data->hwmon_dev);
1403 if (data->disable_ec) {
1404 if (atk_ec_ctl(data, 0))
1405 dev_err(&device->dev, "Failed to disable EC\n");
1408 kfree(data);
1410 return 0;
1413 static int __init atk0110_init(void)
1415 int ret;
1417 /* Make sure it's safe to access the device through ACPI */
1418 if (!acpi_resources_are_enforced()) {
1419 pr_err("Resources not safely usable due to acpi_enforce_resources kernel parameter\n");
1420 return -EBUSY;
1423 ret = acpi_bus_register_driver(&atk_driver);
1424 if (ret)
1425 pr_info("acpi_bus_register_driver failed: %d\n", ret);
1427 return ret;
1430 static void __exit atk0110_exit(void)
1432 acpi_bus_unregister_driver(&atk_driver);
1435 module_init(atk0110_init);
1436 module_exit(atk0110_exit);
1438 MODULE_LICENSE("GPL");