2 * Copyright (C) 2007-2009 Luca Tettamanti <kronos.it@gmail.com>
4 * This file is released under the GPLv2
5 * See COPYING in the top level directory of the kernel tree.
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/debugfs.h>
11 #include <linux/kernel.h>
12 #include <linux/hwmon.h>
13 #include <linux/list.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/dmi.h>
17 #include <linux/jiffies.h>
18 #include <linux/err.h>
19 #include <linux/acpi.h>
21 #define ATK_HID "ATK0110"
24 module_param(new_if
, bool, 0);
25 MODULE_PARM_DESC(new_if
, "Override detection heuristic and force the use of the new ATK0110 interface");
27 static const struct dmi_system_id __initconst atk_force_new_if
[] = {
29 /* Old interface has broken MCH temp monitoring */
30 .ident
= "Asus Sabertooth X58",
32 DMI_MATCH(DMI_BOARD_NAME
, "SABERTOOTH X58")
35 /* Old interface reads the same sensor for fan0 and fan1 */
36 .ident
= "Asus M5A78L",
38 DMI_MATCH(DMI_BOARD_NAME
, "M5A78L")
45 * Minimum time between readings, enforced in order to avoid
50 #define BOARD_ID "MBIF"
51 #define METHOD_ENUMERATE "GGRP"
52 #define METHOD_READ "GITM"
53 #define METHOD_WRITE "SITM"
54 #define METHOD_OLD_READ_TMP "RTMP"
55 #define METHOD_OLD_READ_VLT "RVLT"
56 #define METHOD_OLD_READ_FAN "RFAN"
57 #define METHOD_OLD_ENUM_TMP "TSIF"
58 #define METHOD_OLD_ENUM_VLT "VSIF"
59 #define METHOD_OLD_ENUM_FAN "FSIF"
61 #define ATK_MUX_HWMON 0x00000006ULL
62 #define ATK_MUX_MGMT 0x00000011ULL
64 #define ATK_CLASS_MASK 0xff000000ULL
65 #define ATK_CLASS_FREQ_CTL 0x03000000ULL
66 #define ATK_CLASS_FAN_CTL 0x04000000ULL
67 #define ATK_CLASS_HWMON 0x06000000ULL
68 #define ATK_CLASS_MGMT 0x11000000ULL
70 #define ATK_TYPE_MASK 0x00ff0000ULL
71 #define HWMON_TYPE_VOLT 0x00020000ULL
72 #define HWMON_TYPE_TEMP 0x00030000ULL
73 #define HWMON_TYPE_FAN 0x00040000ULL
75 #define ATK_ELEMENT_ID_MASK 0x0000ffffULL
77 #define ATK_EC_ID 0x11060004ULL
79 enum atk_pack_member
{
87 /* New package format */
88 #define _HWMON_NEW_PACK_SIZE 7
89 #define _HWMON_NEW_PACK_FLAGS 0
90 #define _HWMON_NEW_PACK_NAME 1
91 #define _HWMON_NEW_PACK_UNK1 2
92 #define _HWMON_NEW_PACK_UNK2 3
93 #define _HWMON_NEW_PACK_LIMIT1 4
94 #define _HWMON_NEW_PACK_LIMIT2 5
95 #define _HWMON_NEW_PACK_ENABLE 6
97 /* Old package format */
98 #define _HWMON_OLD_PACK_SIZE 5
99 #define _HWMON_OLD_PACK_FLAGS 0
100 #define _HWMON_OLD_PACK_NAME 1
101 #define _HWMON_OLD_PACK_LIMIT1 2
102 #define _HWMON_OLD_PACK_LIMIT2 3
103 #define _HWMON_OLD_PACK_ENABLE 4
107 struct device
*hwmon_dev
;
108 acpi_handle atk_handle
;
109 struct acpi_device
*acpi_dev
;
114 acpi_handle rtmp_handle
;
115 acpi_handle rvlt_handle
;
116 acpi_handle rfan_handle
;
118 acpi_handle enumerate_handle
;
119 acpi_handle read_handle
;
120 acpi_handle write_handle
;
125 int temperature_count
;
127 struct list_head sensor_list
;
136 typedef ssize_t (*sysfs_show_func
)(struct device
*dev
,
137 struct device_attribute
*attr
, char *buf
);
139 static const struct acpi_device_id atk_ids
[] = {
143 MODULE_DEVICE_TABLE(acpi
, atk_ids
);
145 #define ATTR_NAME_SIZE 16 /* Worst case is "tempN_input" */
147 struct atk_sensor_data
{
148 struct list_head list
;
149 struct atk_data
*data
;
150 struct device_attribute label_attr
;
151 struct device_attribute input_attr
;
152 struct device_attribute limit1_attr
;
153 struct device_attribute limit2_attr
;
154 char label_attr_name
[ATTR_NAME_SIZE
];
155 char input_attr_name
[ATTR_NAME_SIZE
];
156 char limit1_attr_name
[ATTR_NAME_SIZE
];
157 char limit2_attr_name
[ATTR_NAME_SIZE
];
163 unsigned long last_updated
; /* in jiffies */
165 char const *acpi_name
;
169 * Return buffer format:
170 * [0-3] "value" is valid flag
172 * [8- ] unknown stuff on newer mobos
174 struct atk_acpi_ret_buffer
{
180 /* Input buffer used for GITM and SITM methods */
181 struct atk_acpi_input_buf
{
187 static int atk_add(struct acpi_device
*device
);
188 static int atk_remove(struct acpi_device
*device
);
189 static void atk_print_sensor(struct atk_data
*data
, union acpi_object
*obj
);
190 static int atk_read_value(struct atk_sensor_data
*sensor
, u64
*value
);
191 static void atk_free_sensors(struct atk_data
*data
);
193 static struct acpi_driver atk_driver
= {
199 .remove
= atk_remove
,
203 #define input_to_atk_sensor(attr) \
204 container_of(attr, struct atk_sensor_data, input_attr)
206 #define label_to_atk_sensor(attr) \
207 container_of(attr, struct atk_sensor_data, label_attr)
209 #define limit1_to_atk_sensor(attr) \
210 container_of(attr, struct atk_sensor_data, limit1_attr)
212 #define limit2_to_atk_sensor(attr) \
213 container_of(attr, struct atk_sensor_data, limit2_attr)
215 static ssize_t
atk_input_show(struct device
*dev
,
216 struct device_attribute
*attr
, char *buf
)
218 struct atk_sensor_data
*s
= input_to_atk_sensor(attr
);
222 err
= atk_read_value(s
, &value
);
226 if (s
->type
== HWMON_TYPE_TEMP
)
227 /* ACPI returns decidegree */
230 return sprintf(buf
, "%llu\n", value
);
233 static ssize_t
atk_label_show(struct device
*dev
,
234 struct device_attribute
*attr
, char *buf
)
236 struct atk_sensor_data
*s
= label_to_atk_sensor(attr
);
238 return sprintf(buf
, "%s\n", s
->acpi_name
);
241 static ssize_t
atk_limit1_show(struct device
*dev
,
242 struct device_attribute
*attr
, char *buf
)
244 struct atk_sensor_data
*s
= limit1_to_atk_sensor(attr
);
245 u64 value
= s
->limit1
;
247 if (s
->type
== HWMON_TYPE_TEMP
)
250 return sprintf(buf
, "%lld\n", value
);
253 static ssize_t
atk_limit2_show(struct device
*dev
,
254 struct device_attribute
*attr
, char *buf
)
256 struct atk_sensor_data
*s
= limit2_to_atk_sensor(attr
);
257 u64 value
= s
->limit2
;
259 if (s
->type
== HWMON_TYPE_TEMP
)
262 return sprintf(buf
, "%lld\n", value
);
265 static ssize_t
atk_name_show(struct device
*dev
,
266 struct device_attribute
*attr
, char *buf
)
268 return sprintf(buf
, "atk0110\n");
270 static struct device_attribute atk_name_attr
=
271 __ATTR(name
, 0444, atk_name_show
, NULL
);
273 static void atk_init_attribute(struct device_attribute
*attr
, char *name
,
274 sysfs_show_func show
)
276 sysfs_attr_init(&attr
->attr
);
277 attr
->attr
.name
= name
;
278 attr
->attr
.mode
= 0444;
284 static union acpi_object
*atk_get_pack_member(struct atk_data
*data
,
285 union acpi_object
*pack
,
286 enum atk_pack_member m
)
288 bool old_if
= data
->old_interface
;
292 case HWMON_PACK_FLAGS
:
293 offset
= old_if
? _HWMON_OLD_PACK_FLAGS
: _HWMON_NEW_PACK_FLAGS
;
295 case HWMON_PACK_NAME
:
296 offset
= old_if
? _HWMON_OLD_PACK_NAME
: _HWMON_NEW_PACK_NAME
;
298 case HWMON_PACK_LIMIT1
:
299 offset
= old_if
? _HWMON_OLD_PACK_LIMIT1
:
300 _HWMON_NEW_PACK_LIMIT1
;
302 case HWMON_PACK_LIMIT2
:
303 offset
= old_if
? _HWMON_OLD_PACK_LIMIT2
:
304 _HWMON_NEW_PACK_LIMIT2
;
306 case HWMON_PACK_ENABLE
:
307 offset
= old_if
? _HWMON_OLD_PACK_ENABLE
:
308 _HWMON_NEW_PACK_ENABLE
;
314 return &pack
->package
.elements
[offset
];
319 * New package format is:
321 * class - used for de-muxing the request to the correct GITn
322 * type (volt, temp, fan)
324 * sensor id - used for de-muxing the request _inside_ the GITn
332 * The old package has the same format but it's missing the two unknown fields.
334 static int validate_hwmon_pack(struct atk_data
*data
, union acpi_object
*obj
)
336 struct device
*dev
= &data
->acpi_dev
->dev
;
337 union acpi_object
*tmp
;
338 bool old_if
= data
->old_interface
;
339 int const expected_size
= old_if
? _HWMON_OLD_PACK_SIZE
:
340 _HWMON_NEW_PACK_SIZE
;
342 if (obj
->type
!= ACPI_TYPE_PACKAGE
) {
343 dev_warn(dev
, "Invalid type: %d\n", obj
->type
);
347 if (obj
->package
.count
!= expected_size
) {
348 dev_warn(dev
, "Invalid package size: %d, expected: %d\n",
349 obj
->package
.count
, expected_size
);
353 tmp
= atk_get_pack_member(data
, obj
, HWMON_PACK_FLAGS
);
354 if (tmp
->type
!= ACPI_TYPE_INTEGER
) {
355 dev_warn(dev
, "Invalid type (flag): %d\n", tmp
->type
);
359 tmp
= atk_get_pack_member(data
, obj
, HWMON_PACK_NAME
);
360 if (tmp
->type
!= ACPI_TYPE_STRING
) {
361 dev_warn(dev
, "Invalid type (name): %d\n", tmp
->type
);
365 /* Don't check... we don't know what they're useful for anyway */
367 tmp
= &obj
->package
.elements
[HWMON_PACK_UNK1
];
368 if (tmp
->type
!= ACPI_TYPE_INTEGER
) {
369 dev_warn(dev
, "Invalid type (unk1): %d\n", tmp
->type
);
373 tmp
= &obj
->package
.elements
[HWMON_PACK_UNK2
];
374 if (tmp
->type
!= ACPI_TYPE_INTEGER
) {
375 dev_warn(dev
, "Invalid type (unk2): %d\n", tmp
->type
);
380 tmp
= atk_get_pack_member(data
, obj
, HWMON_PACK_LIMIT1
);
381 if (tmp
->type
!= ACPI_TYPE_INTEGER
) {
382 dev_warn(dev
, "Invalid type (limit1): %d\n", tmp
->type
);
386 tmp
= atk_get_pack_member(data
, obj
, HWMON_PACK_LIMIT2
);
387 if (tmp
->type
!= ACPI_TYPE_INTEGER
) {
388 dev_warn(dev
, "Invalid type (limit2): %d\n", tmp
->type
);
392 tmp
= atk_get_pack_member(data
, obj
, HWMON_PACK_ENABLE
);
393 if (tmp
->type
!= ACPI_TYPE_INTEGER
) {
394 dev_warn(dev
, "Invalid type (enable): %d\n", tmp
->type
);
398 atk_print_sensor(data
, obj
);
404 static char const *atk_sensor_type(union acpi_object
*flags
)
406 u64 type
= flags
->integer
.value
& ATK_TYPE_MASK
;
410 case HWMON_TYPE_VOLT
:
413 case HWMON_TYPE_TEMP
:
414 what
= "temperature";
428 static void atk_print_sensor(struct atk_data
*data
, union acpi_object
*obj
)
431 struct device
*dev
= &data
->acpi_dev
->dev
;
432 union acpi_object
*flags
;
433 union acpi_object
*name
;
434 union acpi_object
*limit1
;
435 union acpi_object
*limit2
;
436 union acpi_object
*enable
;
439 flags
= atk_get_pack_member(data
, obj
, HWMON_PACK_FLAGS
);
440 name
= atk_get_pack_member(data
, obj
, HWMON_PACK_NAME
);
441 limit1
= atk_get_pack_member(data
, obj
, HWMON_PACK_LIMIT1
);
442 limit2
= atk_get_pack_member(data
, obj
, HWMON_PACK_LIMIT2
);
443 enable
= atk_get_pack_member(data
, obj
, HWMON_PACK_ENABLE
);
445 what
= atk_sensor_type(flags
);
447 dev_dbg(dev
, "%s: %#llx %s [%llu-%llu] %s\n", what
,
448 flags
->integer
.value
,
449 name
->string
.pointer
,
450 limit1
->integer
.value
, limit2
->integer
.value
,
451 enable
->integer
.value
? "enabled" : "disabled");
455 static int atk_read_value_old(struct atk_sensor_data
*sensor
, u64
*value
)
457 struct atk_data
*data
= sensor
->data
;
458 struct device
*dev
= &data
->acpi_dev
->dev
;
459 struct acpi_object_list params
;
460 union acpi_object id
;
464 switch (sensor
->type
) {
465 case HWMON_TYPE_VOLT
:
466 method
= data
->rvlt_handle
;
468 case HWMON_TYPE_TEMP
:
469 method
= data
->rtmp_handle
;
472 method
= data
->rfan_handle
;
478 id
.type
= ACPI_TYPE_INTEGER
;
479 id
.integer
.value
= sensor
->id
;
482 params
.pointer
= &id
;
484 status
= acpi_evaluate_integer(method
, NULL
, ¶ms
, value
);
485 if (status
!= AE_OK
) {
486 dev_warn(dev
, "%s: ACPI exception: %s\n", __func__
,
487 acpi_format_exception(status
));
494 static union acpi_object
*atk_ggrp(struct atk_data
*data
, u16 mux
)
496 struct device
*dev
= &data
->acpi_dev
->dev
;
497 struct acpi_buffer buf
;
499 struct acpi_object_list params
;
500 union acpi_object id
;
501 union acpi_object
*pack
;
503 id
.type
= ACPI_TYPE_INTEGER
;
504 id
.integer
.value
= mux
;
506 params
.pointer
= &id
;
508 buf
.length
= ACPI_ALLOCATE_BUFFER
;
509 ret
= acpi_evaluate_object(data
->enumerate_handle
, NULL
, ¶ms
, &buf
);
511 dev_err(dev
, "GGRP[%#x] ACPI exception: %s\n", mux
,
512 acpi_format_exception(ret
));
513 return ERR_PTR(-EIO
);
516 if (pack
->type
!= ACPI_TYPE_PACKAGE
) {
517 /* Execution was successful, but the id was not found */
519 return ERR_PTR(-ENOENT
);
522 if (pack
->package
.count
< 1) {
523 dev_err(dev
, "GGRP[%#x] package is too small\n", mux
);
525 return ERR_PTR(-EIO
);
530 static union acpi_object
*atk_gitm(struct atk_data
*data
, u64 id
)
532 struct device
*dev
= &data
->acpi_dev
->dev
;
533 struct atk_acpi_input_buf buf
;
534 union acpi_object tmp
;
535 struct acpi_object_list params
;
536 struct acpi_buffer ret
;
537 union acpi_object
*obj
;
544 tmp
.type
= ACPI_TYPE_BUFFER
;
545 tmp
.buffer
.pointer
= (u8
*)&buf
;
546 tmp
.buffer
.length
= sizeof(buf
);
549 params
.pointer
= (void *)&tmp
;
551 ret
.length
= ACPI_ALLOCATE_BUFFER
;
552 status
= acpi_evaluate_object_typed(data
->read_handle
, NULL
, ¶ms
,
553 &ret
, ACPI_TYPE_BUFFER
);
554 if (status
!= AE_OK
) {
555 dev_warn(dev
, "GITM[%#llx] ACPI exception: %s\n", id
,
556 acpi_format_exception(status
));
557 return ERR_PTR(-EIO
);
562 if (obj
->buffer
.length
< 8) {
563 dev_warn(dev
, "Unexpected ASBF length: %u\n",
566 return ERR_PTR(-EIO
);
571 static union acpi_object
*atk_sitm(struct atk_data
*data
,
572 struct atk_acpi_input_buf
*buf
)
574 struct device
*dev
= &data
->acpi_dev
->dev
;
575 struct acpi_object_list params
;
576 union acpi_object tmp
;
577 struct acpi_buffer ret
;
578 union acpi_object
*obj
;
581 tmp
.type
= ACPI_TYPE_BUFFER
;
582 tmp
.buffer
.pointer
= (u8
*)buf
;
583 tmp
.buffer
.length
= sizeof(*buf
);
586 params
.pointer
= &tmp
;
588 ret
.length
= ACPI_ALLOCATE_BUFFER
;
589 status
= acpi_evaluate_object_typed(data
->write_handle
, NULL
, ¶ms
,
590 &ret
, ACPI_TYPE_BUFFER
);
591 if (status
!= AE_OK
) {
592 dev_warn(dev
, "SITM[%#x] ACPI exception: %s\n", buf
->id
,
593 acpi_format_exception(status
));
594 return ERR_PTR(-EIO
);
599 if (obj
->buffer
.length
< 8) {
600 dev_warn(dev
, "Unexpected ASBF length: %u\n",
603 return ERR_PTR(-EIO
);
608 static int atk_read_value_new(struct atk_sensor_data
*sensor
, u64
*value
)
610 struct atk_data
*data
= sensor
->data
;
611 struct device
*dev
= &data
->acpi_dev
->dev
;
612 union acpi_object
*obj
;
613 struct atk_acpi_ret_buffer
*buf
;
616 obj
= atk_gitm(data
, sensor
->id
);
620 buf
= (struct atk_acpi_ret_buffer
*)obj
->buffer
.pointer
;
621 if (buf
->flags
== 0) {
623 * The reading is not valid, possible causes:
625 * - enumeration was FUBAR (and we didn't notice)
627 dev_warn(dev
, "Read failed, sensor = %#llx\n", sensor
->id
);
638 static int atk_read_value(struct atk_sensor_data
*sensor
, u64
*value
)
642 if (!sensor
->is_valid
||
643 time_after(jiffies
, sensor
->last_updated
+ CACHE_TIME
)) {
644 if (sensor
->data
->old_interface
)
645 err
= atk_read_value_old(sensor
, value
);
647 err
= atk_read_value_new(sensor
, value
);
649 sensor
->is_valid
= true;
650 sensor
->last_updated
= jiffies
;
651 sensor
->cached_value
= *value
;
653 *value
= sensor
->cached_value
;
660 #ifdef CONFIG_DEBUG_FS
661 static int atk_debugfs_gitm_get(void *p
, u64
*val
)
663 struct atk_data
*data
= p
;
664 union acpi_object
*ret
;
665 struct atk_acpi_ret_buffer
*buf
;
668 if (!data
->read_handle
)
671 if (!data
->debugfs
.id
)
674 ret
= atk_gitm(data
, data
->debugfs
.id
);
678 buf
= (struct atk_acpi_ret_buffer
*)ret
->buffer
.pointer
;
688 DEFINE_SIMPLE_ATTRIBUTE(atk_debugfs_gitm
,
689 atk_debugfs_gitm_get
,
693 static int atk_acpi_print(char *buf
, size_t sz
, union acpi_object
*obj
)
698 case ACPI_TYPE_INTEGER
:
699 ret
= snprintf(buf
, sz
, "0x%08llx\n", obj
->integer
.value
);
701 case ACPI_TYPE_STRING
:
702 ret
= snprintf(buf
, sz
, "%s\n", obj
->string
.pointer
);
709 static void atk_pack_print(char *buf
, size_t sz
, union acpi_object
*pack
)
714 for (i
= 0; i
< pack
->package
.count
; i
++) {
715 union acpi_object
*obj
= &pack
->package
.elements
[i
];
717 ret
= atk_acpi_print(buf
, sz
, obj
);
725 static int atk_debugfs_ggrp_open(struct inode
*inode
, struct file
*file
)
727 struct atk_data
*data
= inode
->i_private
;
729 union acpi_object
*ret
;
733 if (!data
->enumerate_handle
)
735 if (!data
->debugfs
.id
)
738 cls
= (data
->debugfs
.id
& 0xff000000) >> 24;
739 ret
= atk_ggrp(data
, cls
);
743 for (i
= 0; i
< ret
->package
.count
; i
++) {
744 union acpi_object
*pack
= &ret
->package
.elements
[i
];
745 union acpi_object
*id
;
747 if (pack
->type
!= ACPI_TYPE_PACKAGE
)
749 if (!pack
->package
.count
)
751 id
= &pack
->package
.elements
[0];
752 if (id
->integer
.value
== data
->debugfs
.id
) {
753 /* Print the package */
754 buf
= kzalloc(512, GFP_KERNEL
);
759 atk_pack_print(buf
, 512, pack
);
768 file
->private_data
= buf
;
770 return nonseekable_open(inode
, file
);
773 static ssize_t
atk_debugfs_ggrp_read(struct file
*file
, char __user
*buf
,
774 size_t count
, loff_t
*pos
)
776 char *str
= file
->private_data
;
777 size_t len
= strlen(str
);
779 return simple_read_from_buffer(buf
, count
, pos
, str
, len
);
782 static int atk_debugfs_ggrp_release(struct inode
*inode
, struct file
*file
)
784 kfree(file
->private_data
);
788 static const struct file_operations atk_debugfs_ggrp_fops
= {
789 .read
= atk_debugfs_ggrp_read
,
790 .open
= atk_debugfs_ggrp_open
,
791 .release
= atk_debugfs_ggrp_release
,
795 static void atk_debugfs_init(struct atk_data
*data
)
800 data
->debugfs
.id
= 0;
802 d
= debugfs_create_dir("asus_atk0110", NULL
);
806 f
= debugfs_create_x32("id", S_IRUSR
| S_IWUSR
, d
, &data
->debugfs
.id
);
810 f
= debugfs_create_file("gitm", S_IRUSR
, d
, data
,
815 f
= debugfs_create_file("ggrp", S_IRUSR
, d
, data
,
816 &atk_debugfs_ggrp_fops
);
820 data
->debugfs
.root
= d
;
824 debugfs_remove_recursive(d
);
827 static void atk_debugfs_cleanup(struct atk_data
*data
)
829 debugfs_remove_recursive(data
->debugfs
.root
);
832 #else /* CONFIG_DEBUG_FS */
834 static void atk_debugfs_init(struct atk_data
*data
)
838 static void atk_debugfs_cleanup(struct atk_data
*data
)
843 static int atk_add_sensor(struct atk_data
*data
, union acpi_object
*obj
)
845 struct device
*dev
= &data
->acpi_dev
->dev
;
846 union acpi_object
*flags
;
847 union acpi_object
*name
;
848 union acpi_object
*limit1
;
849 union acpi_object
*limit2
;
850 union acpi_object
*enable
;
851 struct atk_sensor_data
*sensor
;
852 char const *base_name
;
853 char const *limit1_name
;
854 char const *limit2_name
;
860 if (obj
->type
!= ACPI_TYPE_PACKAGE
) {
862 dev_warn(dev
, "Unknown type for ACPI object: (%d)\n",
867 err
= validate_hwmon_pack(data
, obj
);
871 /* Ok, we have a valid hwmon package */
872 type
= atk_get_pack_member(data
, obj
, HWMON_PACK_FLAGS
)->integer
.value
876 case HWMON_TYPE_VOLT
:
880 num
= &data
->voltage_count
;
883 case HWMON_TYPE_TEMP
:
886 limit2_name
= "crit";
887 num
= &data
->temperature_count
;
894 num
= &data
->fan_count
;
898 dev_warn(dev
, "Unknown sensor type: %#llx\n", type
);
902 enable
= atk_get_pack_member(data
, obj
, HWMON_PACK_ENABLE
);
903 if (!enable
->integer
.value
)
904 /* sensor is disabled */
907 flags
= atk_get_pack_member(data
, obj
, HWMON_PACK_FLAGS
);
908 name
= atk_get_pack_member(data
, obj
, HWMON_PACK_NAME
);
909 limit1
= atk_get_pack_member(data
, obj
, HWMON_PACK_LIMIT1
);
910 limit2
= atk_get_pack_member(data
, obj
, HWMON_PACK_LIMIT2
);
912 sensor
= kzalloc(sizeof(*sensor
), GFP_KERNEL
);
916 sensor
->acpi_name
= kstrdup(name
->string
.pointer
, GFP_KERNEL
);
917 if (!sensor
->acpi_name
) {
922 INIT_LIST_HEAD(&sensor
->list
);
925 sensor
->id
= flags
->integer
.value
;
926 sensor
->limit1
= limit1
->integer
.value
;
927 if (data
->old_interface
)
928 sensor
->limit2
= limit2
->integer
.value
;
930 /* The upper limit is expressed as delta from lower limit */
931 sensor
->limit2
= sensor
->limit1
+ limit2
->integer
.value
;
933 snprintf(sensor
->input_attr_name
, ATTR_NAME_SIZE
,
934 "%s%d_input", base_name
, start
+ *num
);
935 atk_init_attribute(&sensor
->input_attr
,
936 sensor
->input_attr_name
,
939 snprintf(sensor
->label_attr_name
, ATTR_NAME_SIZE
,
940 "%s%d_label", base_name
, start
+ *num
);
941 atk_init_attribute(&sensor
->label_attr
,
942 sensor
->label_attr_name
,
945 snprintf(sensor
->limit1_attr_name
, ATTR_NAME_SIZE
,
946 "%s%d_%s", base_name
, start
+ *num
, limit1_name
);
947 atk_init_attribute(&sensor
->limit1_attr
,
948 sensor
->limit1_attr_name
,
951 snprintf(sensor
->limit2_attr_name
, ATTR_NAME_SIZE
,
952 "%s%d_%s", base_name
, start
+ *num
, limit2_name
);
953 atk_init_attribute(&sensor
->limit2_attr
,
954 sensor
->limit2_attr_name
,
957 list_add(&sensor
->list
, &data
->sensor_list
);
966 static int atk_enumerate_old_hwmon(struct atk_data
*data
)
968 struct device
*dev
= &data
->acpi_dev
->dev
;
969 struct acpi_buffer buf
;
970 union acpi_object
*pack
;
976 buf
.length
= ACPI_ALLOCATE_BUFFER
;
977 status
= acpi_evaluate_object_typed(data
->atk_handle
,
978 METHOD_OLD_ENUM_VLT
, NULL
, &buf
, ACPI_TYPE_PACKAGE
);
979 if (status
!= AE_OK
) {
980 dev_warn(dev
, METHOD_OLD_ENUM_VLT
": ACPI exception: %s\n",
981 acpi_format_exception(status
));
987 for (i
= 1; i
< pack
->package
.count
; i
++) {
988 union acpi_object
*obj
= &pack
->package
.elements
[i
];
990 ret
= atk_add_sensor(data
, obj
);
994 ACPI_FREE(buf
.pointer
);
997 buf
.length
= ACPI_ALLOCATE_BUFFER
;
998 status
= acpi_evaluate_object_typed(data
->atk_handle
,
999 METHOD_OLD_ENUM_TMP
, NULL
, &buf
, ACPI_TYPE_PACKAGE
);
1000 if (status
!= AE_OK
) {
1001 dev_warn(dev
, METHOD_OLD_ENUM_TMP
": ACPI exception: %s\n",
1002 acpi_format_exception(status
));
1009 for (i
= 1; i
< pack
->package
.count
; i
++) {
1010 union acpi_object
*obj
= &pack
->package
.elements
[i
];
1012 ret
= atk_add_sensor(data
, obj
);
1016 ACPI_FREE(buf
.pointer
);
1019 buf
.length
= ACPI_ALLOCATE_BUFFER
;
1020 status
= acpi_evaluate_object_typed(data
->atk_handle
,
1021 METHOD_OLD_ENUM_FAN
, NULL
, &buf
, ACPI_TYPE_PACKAGE
);
1022 if (status
!= AE_OK
) {
1023 dev_warn(dev
, METHOD_OLD_ENUM_FAN
": ACPI exception: %s\n",
1024 acpi_format_exception(status
));
1031 for (i
= 1; i
< pack
->package
.count
; i
++) {
1032 union acpi_object
*obj
= &pack
->package
.elements
[i
];
1034 ret
= atk_add_sensor(data
, obj
);
1038 ACPI_FREE(buf
.pointer
);
1042 atk_free_sensors(data
);
1046 static int atk_ec_present(struct atk_data
*data
)
1048 struct device
*dev
= &data
->acpi_dev
->dev
;
1049 union acpi_object
*pack
;
1050 union acpi_object
*ec
;
1054 pack
= atk_ggrp(data
, ATK_MUX_MGMT
);
1056 if (PTR_ERR(pack
) == -ENOENT
) {
1057 /* The MGMT class does not exists - that's ok */
1058 dev_dbg(dev
, "Class %#llx not found\n", ATK_MUX_MGMT
);
1061 return PTR_ERR(pack
);
1066 for (i
= 0; i
< pack
->package
.count
; i
++) {
1067 union acpi_object
*obj
= &pack
->package
.elements
[i
];
1068 union acpi_object
*id
;
1070 if (obj
->type
!= ACPI_TYPE_PACKAGE
)
1073 id
= &obj
->package
.elements
[0];
1074 if (id
->type
!= ACPI_TYPE_INTEGER
)
1077 if (id
->integer
.value
== ATK_EC_ID
) {
1085 /* The system has no EC */
1086 dev_dbg(dev
, "EC not found\n");
1092 static int atk_ec_enabled(struct atk_data
*data
)
1094 struct device
*dev
= &data
->acpi_dev
->dev
;
1095 union acpi_object
*obj
;
1096 struct atk_acpi_ret_buffer
*buf
;
1099 obj
= atk_gitm(data
, ATK_EC_ID
);
1101 dev_err(dev
, "Unable to query EC status\n");
1102 return PTR_ERR(obj
);
1104 buf
= (struct atk_acpi_ret_buffer
*)obj
->buffer
.pointer
;
1106 if (buf
->flags
== 0) {
1107 dev_err(dev
, "Unable to query EC status\n");
1110 err
= (buf
->value
!= 0);
1111 dev_dbg(dev
, "EC is %sabled\n",
1112 err
? "en" : "dis");
1119 static int atk_ec_ctl(struct atk_data
*data
, int enable
)
1121 struct device
*dev
= &data
->acpi_dev
->dev
;
1122 union acpi_object
*obj
;
1123 struct atk_acpi_input_buf sitm
;
1124 struct atk_acpi_ret_buffer
*ec_ret
;
1127 sitm
.id
= ATK_EC_ID
;
1128 sitm
.param1
= enable
;
1131 obj
= atk_sitm(data
, &sitm
);
1133 dev_err(dev
, "Failed to %sable the EC\n",
1134 enable
? "en" : "dis");
1135 return PTR_ERR(obj
);
1137 ec_ret
= (struct atk_acpi_ret_buffer
*)obj
->buffer
.pointer
;
1138 if (ec_ret
->flags
== 0) {
1139 dev_err(dev
, "Failed to %sable the EC\n",
1140 enable
? "en" : "dis");
1143 dev_info(dev
, "EC %sabled\n",
1144 enable
? "en" : "dis");
1151 static int atk_enumerate_new_hwmon(struct atk_data
*data
)
1153 struct device
*dev
= &data
->acpi_dev
->dev
;
1154 union acpi_object
*pack
;
1158 err
= atk_ec_present(data
);
1162 err
= atk_ec_enabled(data
);
1165 /* If the EC was disabled we will disable it again on unload */
1166 data
->disable_ec
= err
;
1168 err
= atk_ec_ctl(data
, 1);
1170 data
->disable_ec
= false;
1175 dev_dbg(dev
, "Enumerating hwmon sensors\n");
1177 pack
= atk_ggrp(data
, ATK_MUX_HWMON
);
1179 return PTR_ERR(pack
);
1181 for (i
= 0; i
< pack
->package
.count
; i
++) {
1182 union acpi_object
*obj
= &pack
->package
.elements
[i
];
1184 atk_add_sensor(data
, obj
);
1187 err
= data
->voltage_count
+ data
->temperature_count
+ data
->fan_count
;
1193 static int atk_create_files(struct atk_data
*data
)
1195 struct atk_sensor_data
*s
;
1198 list_for_each_entry(s
, &data
->sensor_list
, list
) {
1199 err
= device_create_file(data
->hwmon_dev
, &s
->input_attr
);
1202 err
= device_create_file(data
->hwmon_dev
, &s
->label_attr
);
1205 err
= device_create_file(data
->hwmon_dev
, &s
->limit1_attr
);
1208 err
= device_create_file(data
->hwmon_dev
, &s
->limit2_attr
);
1213 err
= device_create_file(data
->hwmon_dev
, &atk_name_attr
);
1218 static void atk_remove_files(struct atk_data
*data
)
1220 struct atk_sensor_data
*s
;
1222 list_for_each_entry(s
, &data
->sensor_list
, list
) {
1223 device_remove_file(data
->hwmon_dev
, &s
->input_attr
);
1224 device_remove_file(data
->hwmon_dev
, &s
->label_attr
);
1225 device_remove_file(data
->hwmon_dev
, &s
->limit1_attr
);
1226 device_remove_file(data
->hwmon_dev
, &s
->limit2_attr
);
1228 device_remove_file(data
->hwmon_dev
, &atk_name_attr
);
1231 static void atk_free_sensors(struct atk_data
*data
)
1233 struct list_head
*head
= &data
->sensor_list
;
1234 struct atk_sensor_data
*s
, *tmp
;
1236 list_for_each_entry_safe(s
, tmp
, head
, list
) {
1237 kfree(s
->acpi_name
);
1242 static int atk_register_hwmon(struct atk_data
*data
)
1244 struct device
*dev
= &data
->acpi_dev
->dev
;
1247 dev_dbg(dev
, "registering hwmon device\n");
1248 data
->hwmon_dev
= hwmon_device_register(dev
);
1249 if (IS_ERR(data
->hwmon_dev
))
1250 return PTR_ERR(data
->hwmon_dev
);
1252 dev_dbg(dev
, "populating sysfs directory\n");
1253 err
= atk_create_files(data
);
1259 /* Cleanup the registered files */
1260 atk_remove_files(data
);
1261 hwmon_device_unregister(data
->hwmon_dev
);
1265 static int atk_probe_if(struct atk_data
*data
)
1267 struct device
*dev
= &data
->acpi_dev
->dev
;
1272 /* RTMP: read temperature */
1273 status
= acpi_get_handle(data
->atk_handle
, METHOD_OLD_READ_TMP
, &ret
);
1274 if (ACPI_SUCCESS(status
))
1275 data
->rtmp_handle
= ret
;
1277 dev_dbg(dev
, "method " METHOD_OLD_READ_TMP
" not found: %s\n",
1278 acpi_format_exception(status
));
1280 /* RVLT: read voltage */
1281 status
= acpi_get_handle(data
->atk_handle
, METHOD_OLD_READ_VLT
, &ret
);
1282 if (ACPI_SUCCESS(status
))
1283 data
->rvlt_handle
= ret
;
1285 dev_dbg(dev
, "method " METHOD_OLD_READ_VLT
" not found: %s\n",
1286 acpi_format_exception(status
));
1288 /* RFAN: read fan status */
1289 status
= acpi_get_handle(data
->atk_handle
, METHOD_OLD_READ_FAN
, &ret
);
1290 if (ACPI_SUCCESS(status
))
1291 data
->rfan_handle
= ret
;
1293 dev_dbg(dev
, "method " METHOD_OLD_READ_FAN
" not found: %s\n",
1294 acpi_format_exception(status
));
1297 status
= acpi_get_handle(data
->atk_handle
, METHOD_ENUMERATE
, &ret
);
1298 if (ACPI_SUCCESS(status
))
1299 data
->enumerate_handle
= ret
;
1301 dev_dbg(dev
, "method " METHOD_ENUMERATE
" not found: %s\n",
1302 acpi_format_exception(status
));
1304 /* De-multiplexer (read) */
1305 status
= acpi_get_handle(data
->atk_handle
, METHOD_READ
, &ret
);
1306 if (ACPI_SUCCESS(status
))
1307 data
->read_handle
= ret
;
1309 dev_dbg(dev
, "method " METHOD_READ
" not found: %s\n",
1310 acpi_format_exception(status
));
1312 /* De-multiplexer (write) */
1313 status
= acpi_get_handle(data
->atk_handle
, METHOD_WRITE
, &ret
);
1314 if (ACPI_SUCCESS(status
))
1315 data
->write_handle
= ret
;
1317 dev_dbg(dev
, "method " METHOD_WRITE
" not found: %s\n",
1318 acpi_format_exception(status
));
1321 * Check for hwmon methods: first check "old" style methods; note that
1322 * both may be present: in this case we stick to the old interface;
1323 * analysis of multiple DSDTs indicates that when both interfaces
1324 * are present the new one (GGRP/GITM) is not functional.
1327 dev_info(dev
, "Overriding interface detection\n");
1328 if (data
->rtmp_handle
&&
1329 data
->rvlt_handle
&& data
->rfan_handle
&& !new_if
)
1330 data
->old_interface
= true;
1331 else if (data
->enumerate_handle
&& data
->read_handle
&&
1333 data
->old_interface
= false;
1340 static int atk_add(struct acpi_device
*device
)
1344 struct acpi_buffer buf
;
1345 union acpi_object
*obj
;
1346 struct atk_data
*data
;
1348 dev_dbg(&device
->dev
, "adding...\n");
1350 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
1354 data
->acpi_dev
= device
;
1355 data
->atk_handle
= device
->handle
;
1356 INIT_LIST_HEAD(&data
->sensor_list
);
1357 data
->disable_ec
= false;
1359 buf
.length
= ACPI_ALLOCATE_BUFFER
;
1360 ret
= acpi_evaluate_object_typed(data
->atk_handle
, BOARD_ID
, NULL
,
1361 &buf
, ACPI_TYPE_PACKAGE
);
1363 dev_dbg(&device
->dev
, "atk: method MBIF not found\n");
1366 if (obj
->package
.count
>= 2) {
1367 union acpi_object
*id
= &obj
->package
.elements
[1];
1368 if (id
->type
== ACPI_TYPE_STRING
)
1369 dev_dbg(&device
->dev
, "board ID = %s\n",
1370 id
->string
.pointer
);
1372 ACPI_FREE(buf
.pointer
);
1375 err
= atk_probe_if(data
);
1377 dev_err(&device
->dev
, "No usable hwmon interface detected\n");
1381 if (data
->old_interface
) {
1382 dev_dbg(&device
->dev
, "Using old hwmon interface\n");
1383 err
= atk_enumerate_old_hwmon(data
);
1385 dev_dbg(&device
->dev
, "Using new hwmon interface\n");
1386 err
= atk_enumerate_new_hwmon(data
);
1391 dev_info(&device
->dev
,
1392 "No usable sensor detected, bailing out\n");
1397 err
= atk_register_hwmon(data
);
1401 atk_debugfs_init(data
);
1403 device
->driver_data
= data
;
1406 atk_free_sensors(data
);
1408 if (data
->disable_ec
)
1409 atk_ec_ctl(data
, 0);
1414 static int atk_remove(struct acpi_device
*device
)
1416 struct atk_data
*data
= device
->driver_data
;
1417 dev_dbg(&device
->dev
, "removing...\n");
1419 device
->driver_data
= NULL
;
1421 atk_debugfs_cleanup(data
);
1423 atk_remove_files(data
);
1424 atk_free_sensors(data
);
1425 hwmon_device_unregister(data
->hwmon_dev
);
1427 if (data
->disable_ec
) {
1428 if (atk_ec_ctl(data
, 0))
1429 dev_err(&device
->dev
, "Failed to disable EC\n");
1437 static int __init
atk0110_init(void)
1441 /* Make sure it's safe to access the device through ACPI */
1442 if (!acpi_resources_are_enforced()) {
1443 pr_err("Resources not safely usable due to acpi_enforce_resources kernel parameter\n");
1447 if (dmi_check_system(atk_force_new_if
))
1450 ret
= acpi_bus_register_driver(&atk_driver
);
1452 pr_info("acpi_bus_register_driver failed: %d\n", ret
);
1457 static void __exit
atk0110_exit(void)
1459 acpi_bus_unregister_driver(&atk_driver
);
1462 module_init(atk0110_init
);
1463 module_exit(atk0110_exit
);
1465 MODULE_LICENSE("GPL");