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>
18 #include <acpi/acpi.h>
19 #include <acpi/acpixf.h>
20 #include <acpi/acpi_drivers.h>
21 #include <acpi/acpi_bus.h>
24 #define ATK_HID "ATK0110"
27 module_param(new_if
, bool, 0);
28 MODULE_PARM_DESC(new_if
, "Override detection heuristic and force the use of the new ATK0110 interface");
30 static const struct dmi_system_id __initconst atk_force_new_if
[] = {
32 /* Old interface has broken MCH temp monitoring */
33 .ident
= "Asus Sabertooth X58",
35 DMI_MATCH(DMI_BOARD_NAME
, "SABERTOOTH X58")
42 * Minimum time between readings, enforced in order to avoid
47 #define BOARD_ID "MBIF"
48 #define METHOD_ENUMERATE "GGRP"
49 #define METHOD_READ "GITM"
50 #define METHOD_WRITE "SITM"
51 #define METHOD_OLD_READ_TMP "RTMP"
52 #define METHOD_OLD_READ_VLT "RVLT"
53 #define METHOD_OLD_READ_FAN "RFAN"
54 #define METHOD_OLD_ENUM_TMP "TSIF"
55 #define METHOD_OLD_ENUM_VLT "VSIF"
56 #define METHOD_OLD_ENUM_FAN "FSIF"
58 #define ATK_MUX_HWMON 0x00000006ULL
59 #define ATK_MUX_MGMT 0x00000011ULL
61 #define ATK_CLASS_MASK 0xff000000ULL
62 #define ATK_CLASS_FREQ_CTL 0x03000000ULL
63 #define ATK_CLASS_FAN_CTL 0x04000000ULL
64 #define ATK_CLASS_HWMON 0x06000000ULL
65 #define ATK_CLASS_MGMT 0x11000000ULL
67 #define ATK_TYPE_MASK 0x00ff0000ULL
68 #define HWMON_TYPE_VOLT 0x00020000ULL
69 #define HWMON_TYPE_TEMP 0x00030000ULL
70 #define HWMON_TYPE_FAN 0x00040000ULL
72 #define ATK_ELEMENT_ID_MASK 0x0000ffffULL
74 #define ATK_EC_ID 0x11060004ULL
76 enum atk_pack_member
{
84 /* New package format */
85 #define _HWMON_NEW_PACK_SIZE 7
86 #define _HWMON_NEW_PACK_FLAGS 0
87 #define _HWMON_NEW_PACK_NAME 1
88 #define _HWMON_NEW_PACK_UNK1 2
89 #define _HWMON_NEW_PACK_UNK2 3
90 #define _HWMON_NEW_PACK_LIMIT1 4
91 #define _HWMON_NEW_PACK_LIMIT2 5
92 #define _HWMON_NEW_PACK_ENABLE 6
94 /* Old package format */
95 #define _HWMON_OLD_PACK_SIZE 5
96 #define _HWMON_OLD_PACK_FLAGS 0
97 #define _HWMON_OLD_PACK_NAME 1
98 #define _HWMON_OLD_PACK_LIMIT1 2
99 #define _HWMON_OLD_PACK_LIMIT2 3
100 #define _HWMON_OLD_PACK_ENABLE 4
104 struct device
*hwmon_dev
;
105 acpi_handle atk_handle
;
106 struct acpi_device
*acpi_dev
;
111 acpi_handle rtmp_handle
;
112 acpi_handle rvlt_handle
;
113 acpi_handle rfan_handle
;
115 acpi_handle enumerate_handle
;
116 acpi_handle read_handle
;
117 acpi_handle write_handle
;
122 int temperature_count
;
124 struct list_head sensor_list
;
133 typedef ssize_t (*sysfs_show_func
)(struct device
*dev
,
134 struct device_attribute
*attr
, char *buf
);
136 static const struct acpi_device_id atk_ids
[] = {
140 MODULE_DEVICE_TABLE(acpi
, atk_ids
);
142 #define ATTR_NAME_SIZE 16 /* Worst case is "tempN_input" */
144 struct atk_sensor_data
{
145 struct list_head list
;
146 struct atk_data
*data
;
147 struct device_attribute label_attr
;
148 struct device_attribute input_attr
;
149 struct device_attribute limit1_attr
;
150 struct device_attribute limit2_attr
;
151 char label_attr_name
[ATTR_NAME_SIZE
];
152 char input_attr_name
[ATTR_NAME_SIZE
];
153 char limit1_attr_name
[ATTR_NAME_SIZE
];
154 char limit2_attr_name
[ATTR_NAME_SIZE
];
160 unsigned long last_updated
; /* in jiffies */
162 char const *acpi_name
;
166 * Return buffer format:
167 * [0-3] "value" is valid flag
169 * [8- ] unknown stuff on newer mobos
171 struct atk_acpi_ret_buffer
{
177 /* Input buffer used for GITM and SITM methods */
178 struct atk_acpi_input_buf
{
184 static int atk_add(struct acpi_device
*device
);
185 static int atk_remove(struct acpi_device
*device
, int type
);
186 static void atk_print_sensor(struct atk_data
*data
, union acpi_object
*obj
);
187 static int atk_read_value(struct atk_sensor_data
*sensor
, u64
*value
);
188 static void atk_free_sensors(struct atk_data
*data
);
190 static struct acpi_driver atk_driver
= {
196 .remove
= atk_remove
,
200 #define input_to_atk_sensor(attr) \
201 container_of(attr, struct atk_sensor_data, input_attr)
203 #define label_to_atk_sensor(attr) \
204 container_of(attr, struct atk_sensor_data, label_attr)
206 #define limit1_to_atk_sensor(attr) \
207 container_of(attr, struct atk_sensor_data, limit1_attr)
209 #define limit2_to_atk_sensor(attr) \
210 container_of(attr, struct atk_sensor_data, limit2_attr)
212 static ssize_t
atk_input_show(struct device
*dev
,
213 struct device_attribute
*attr
, char *buf
)
215 struct atk_sensor_data
*s
= input_to_atk_sensor(attr
);
219 err
= atk_read_value(s
, &value
);
223 if (s
->type
== HWMON_TYPE_TEMP
)
224 /* ACPI returns decidegree */
227 return sprintf(buf
, "%llu\n", value
);
230 static ssize_t
atk_label_show(struct device
*dev
,
231 struct device_attribute
*attr
, char *buf
)
233 struct atk_sensor_data
*s
= label_to_atk_sensor(attr
);
235 return sprintf(buf
, "%s\n", s
->acpi_name
);
238 static ssize_t
atk_limit1_show(struct device
*dev
,
239 struct device_attribute
*attr
, char *buf
)
241 struct atk_sensor_data
*s
= limit1_to_atk_sensor(attr
);
242 u64 value
= s
->limit1
;
244 if (s
->type
== HWMON_TYPE_TEMP
)
247 return sprintf(buf
, "%lld\n", value
);
250 static ssize_t
atk_limit2_show(struct device
*dev
,
251 struct device_attribute
*attr
, char *buf
)
253 struct atk_sensor_data
*s
= limit2_to_atk_sensor(attr
);
254 u64 value
= s
->limit2
;
256 if (s
->type
== HWMON_TYPE_TEMP
)
259 return sprintf(buf
, "%lld\n", value
);
262 static ssize_t
atk_name_show(struct device
*dev
,
263 struct device_attribute
*attr
, char *buf
)
265 return sprintf(buf
, "atk0110\n");
267 static struct device_attribute atk_name_attr
=
268 __ATTR(name
, 0444, atk_name_show
, NULL
);
270 static void atk_init_attribute(struct device_attribute
*attr
, char *name
,
271 sysfs_show_func show
)
273 sysfs_attr_init(&attr
->attr
);
274 attr
->attr
.name
= name
;
275 attr
->attr
.mode
= 0444;
281 static union acpi_object
*atk_get_pack_member(struct atk_data
*data
,
282 union acpi_object
*pack
,
283 enum atk_pack_member m
)
285 bool old_if
= data
->old_interface
;
289 case HWMON_PACK_FLAGS
:
290 offset
= old_if
? _HWMON_OLD_PACK_FLAGS
: _HWMON_NEW_PACK_FLAGS
;
292 case HWMON_PACK_NAME
:
293 offset
= old_if
? _HWMON_OLD_PACK_NAME
: _HWMON_NEW_PACK_NAME
;
295 case HWMON_PACK_LIMIT1
:
296 offset
= old_if
? _HWMON_OLD_PACK_LIMIT1
:
297 _HWMON_NEW_PACK_LIMIT1
;
299 case HWMON_PACK_LIMIT2
:
300 offset
= old_if
? _HWMON_OLD_PACK_LIMIT2
:
301 _HWMON_NEW_PACK_LIMIT2
;
303 case HWMON_PACK_ENABLE
:
304 offset
= old_if
? _HWMON_OLD_PACK_ENABLE
:
305 _HWMON_NEW_PACK_ENABLE
;
311 return &pack
->package
.elements
[offset
];
316 * New package format is:
318 * class - used for de-muxing the request to the correct GITn
319 * type (volt, temp, fan)
321 * sensor id - used for de-muxing the request _inside_ the GITn
329 * The old package has the same format but it's missing the two unknown fields.
331 static int validate_hwmon_pack(struct atk_data
*data
, union acpi_object
*obj
)
333 struct device
*dev
= &data
->acpi_dev
->dev
;
334 union acpi_object
*tmp
;
335 bool old_if
= data
->old_interface
;
336 int const expected_size
= old_if
? _HWMON_OLD_PACK_SIZE
:
337 _HWMON_NEW_PACK_SIZE
;
339 if (obj
->type
!= ACPI_TYPE_PACKAGE
) {
340 dev_warn(dev
, "Invalid type: %d\n", obj
->type
);
344 if (obj
->package
.count
!= expected_size
) {
345 dev_warn(dev
, "Invalid package size: %d, expected: %d\n",
346 obj
->package
.count
, expected_size
);
350 tmp
= atk_get_pack_member(data
, obj
, HWMON_PACK_FLAGS
);
351 if (tmp
->type
!= ACPI_TYPE_INTEGER
) {
352 dev_warn(dev
, "Invalid type (flag): %d\n", tmp
->type
);
356 tmp
= atk_get_pack_member(data
, obj
, HWMON_PACK_NAME
);
357 if (tmp
->type
!= ACPI_TYPE_STRING
) {
358 dev_warn(dev
, "Invalid type (name): %d\n", tmp
->type
);
362 /* Don't check... we don't know what they're useful for anyway */
364 tmp
= &obj
->package
.elements
[HWMON_PACK_UNK1
];
365 if (tmp
->type
!= ACPI_TYPE_INTEGER
) {
366 dev_warn(dev
, "Invalid type (unk1): %d\n", tmp
->type
);
370 tmp
= &obj
->package
.elements
[HWMON_PACK_UNK2
];
371 if (tmp
->type
!= ACPI_TYPE_INTEGER
) {
372 dev_warn(dev
, "Invalid type (unk2): %d\n", tmp
->type
);
377 tmp
= atk_get_pack_member(data
, obj
, HWMON_PACK_LIMIT1
);
378 if (tmp
->type
!= ACPI_TYPE_INTEGER
) {
379 dev_warn(dev
, "Invalid type (limit1): %d\n", tmp
->type
);
383 tmp
= atk_get_pack_member(data
, obj
, HWMON_PACK_LIMIT2
);
384 if (tmp
->type
!= ACPI_TYPE_INTEGER
) {
385 dev_warn(dev
, "Invalid type (limit2): %d\n", tmp
->type
);
389 tmp
= atk_get_pack_member(data
, obj
, HWMON_PACK_ENABLE
);
390 if (tmp
->type
!= ACPI_TYPE_INTEGER
) {
391 dev_warn(dev
, "Invalid type (enable): %d\n", tmp
->type
);
395 atk_print_sensor(data
, obj
);
401 static char const *atk_sensor_type(union acpi_object
*flags
)
403 u64 type
= flags
->integer
.value
& ATK_TYPE_MASK
;
407 case HWMON_TYPE_VOLT
:
410 case HWMON_TYPE_TEMP
:
411 what
= "temperature";
425 static void atk_print_sensor(struct atk_data
*data
, union acpi_object
*obj
)
428 struct device
*dev
= &data
->acpi_dev
->dev
;
429 union acpi_object
*flags
;
430 union acpi_object
*name
;
431 union acpi_object
*limit1
;
432 union acpi_object
*limit2
;
433 union acpi_object
*enable
;
436 flags
= atk_get_pack_member(data
, obj
, HWMON_PACK_FLAGS
);
437 name
= atk_get_pack_member(data
, obj
, HWMON_PACK_NAME
);
438 limit1
= atk_get_pack_member(data
, obj
, HWMON_PACK_LIMIT1
);
439 limit2
= atk_get_pack_member(data
, obj
, HWMON_PACK_LIMIT2
);
440 enable
= atk_get_pack_member(data
, obj
, HWMON_PACK_ENABLE
);
442 what
= atk_sensor_type(flags
);
444 dev_dbg(dev
, "%s: %#llx %s [%llu-%llu] %s\n", what
,
445 flags
->integer
.value
,
446 name
->string
.pointer
,
447 limit1
->integer
.value
, limit2
->integer
.value
,
448 enable
->integer
.value
? "enabled" : "disabled");
452 static int atk_read_value_old(struct atk_sensor_data
*sensor
, u64
*value
)
454 struct atk_data
*data
= sensor
->data
;
455 struct device
*dev
= &data
->acpi_dev
->dev
;
456 struct acpi_object_list params
;
457 union acpi_object id
;
461 switch (sensor
->type
) {
462 case HWMON_TYPE_VOLT
:
463 method
= data
->rvlt_handle
;
465 case HWMON_TYPE_TEMP
:
466 method
= data
->rtmp_handle
;
469 method
= data
->rfan_handle
;
475 id
.type
= ACPI_TYPE_INTEGER
;
476 id
.integer
.value
= sensor
->id
;
479 params
.pointer
= &id
;
481 status
= acpi_evaluate_integer(method
, NULL
, ¶ms
, value
);
482 if (status
!= AE_OK
) {
483 dev_warn(dev
, "%s: ACPI exception: %s\n", __func__
,
484 acpi_format_exception(status
));
491 static union acpi_object
*atk_ggrp(struct atk_data
*data
, u16 mux
)
493 struct device
*dev
= &data
->acpi_dev
->dev
;
494 struct acpi_buffer buf
;
496 struct acpi_object_list params
;
497 union acpi_object id
;
498 union acpi_object
*pack
;
500 id
.type
= ACPI_TYPE_INTEGER
;
501 id
.integer
.value
= mux
;
503 params
.pointer
= &id
;
505 buf
.length
= ACPI_ALLOCATE_BUFFER
;
506 ret
= acpi_evaluate_object(data
->enumerate_handle
, NULL
, ¶ms
, &buf
);
508 dev_err(dev
, "GGRP[%#x] ACPI exception: %s\n", mux
,
509 acpi_format_exception(ret
));
510 return ERR_PTR(-EIO
);
513 if (pack
->type
!= ACPI_TYPE_PACKAGE
) {
514 /* Execution was successful, but the id was not found */
516 return ERR_PTR(-ENOENT
);
519 if (pack
->package
.count
< 1) {
520 dev_err(dev
, "GGRP[%#x] package is too small\n", mux
);
522 return ERR_PTR(-EIO
);
527 static union acpi_object
*atk_gitm(struct atk_data
*data
, u64 id
)
529 struct device
*dev
= &data
->acpi_dev
->dev
;
530 struct atk_acpi_input_buf buf
;
531 union acpi_object tmp
;
532 struct acpi_object_list params
;
533 struct acpi_buffer ret
;
534 union acpi_object
*obj
;
541 tmp
.type
= ACPI_TYPE_BUFFER
;
542 tmp
.buffer
.pointer
= (u8
*)&buf
;
543 tmp
.buffer
.length
= sizeof(buf
);
546 params
.pointer
= (void *)&tmp
;
548 ret
.length
= ACPI_ALLOCATE_BUFFER
;
549 status
= acpi_evaluate_object_typed(data
->read_handle
, NULL
, ¶ms
,
550 &ret
, ACPI_TYPE_BUFFER
);
551 if (status
!= AE_OK
) {
552 dev_warn(dev
, "GITM[%#llx] ACPI exception: %s\n", id
,
553 acpi_format_exception(status
));
554 return ERR_PTR(-EIO
);
559 if (obj
->buffer
.length
< 8) {
560 dev_warn(dev
, "Unexpected ASBF length: %u\n",
563 return ERR_PTR(-EIO
);
568 static union acpi_object
*atk_sitm(struct atk_data
*data
,
569 struct atk_acpi_input_buf
*buf
)
571 struct device
*dev
= &data
->acpi_dev
->dev
;
572 struct acpi_object_list params
;
573 union acpi_object tmp
;
574 struct acpi_buffer ret
;
575 union acpi_object
*obj
;
578 tmp
.type
= ACPI_TYPE_BUFFER
;
579 tmp
.buffer
.pointer
= (u8
*)buf
;
580 tmp
.buffer
.length
= sizeof(*buf
);
583 params
.pointer
= &tmp
;
585 ret
.length
= ACPI_ALLOCATE_BUFFER
;
586 status
= acpi_evaluate_object_typed(data
->write_handle
, NULL
, ¶ms
,
587 &ret
, ACPI_TYPE_BUFFER
);
588 if (status
!= AE_OK
) {
589 dev_warn(dev
, "SITM[%#x] ACPI exception: %s\n", buf
->id
,
590 acpi_format_exception(status
));
591 return ERR_PTR(-EIO
);
596 if (obj
->buffer
.length
< 8) {
597 dev_warn(dev
, "Unexpected ASBF length: %u\n",
600 return ERR_PTR(-EIO
);
605 static int atk_read_value_new(struct atk_sensor_data
*sensor
, u64
*value
)
607 struct atk_data
*data
= sensor
->data
;
608 struct device
*dev
= &data
->acpi_dev
->dev
;
609 union acpi_object
*obj
;
610 struct atk_acpi_ret_buffer
*buf
;
613 obj
= atk_gitm(data
, sensor
->id
);
617 buf
= (struct atk_acpi_ret_buffer
*)obj
->buffer
.pointer
;
618 if (buf
->flags
== 0) {
620 * The reading is not valid, possible causes:
622 * - enumeration was FUBAR (and we didn't notice)
624 dev_warn(dev
, "Read failed, sensor = %#llx\n", sensor
->id
);
635 static int atk_read_value(struct atk_sensor_data
*sensor
, u64
*value
)
639 if (!sensor
->is_valid
||
640 time_after(jiffies
, sensor
->last_updated
+ CACHE_TIME
)) {
641 if (sensor
->data
->old_interface
)
642 err
= atk_read_value_old(sensor
, value
);
644 err
= atk_read_value_new(sensor
, value
);
646 sensor
->is_valid
= true;
647 sensor
->last_updated
= jiffies
;
648 sensor
->cached_value
= *value
;
650 *value
= sensor
->cached_value
;
657 #ifdef CONFIG_DEBUG_FS
658 static int atk_debugfs_gitm_get(void *p
, u64
*val
)
660 struct atk_data
*data
= p
;
661 union acpi_object
*ret
;
662 struct atk_acpi_ret_buffer
*buf
;
665 if (!data
->read_handle
)
668 if (!data
->debugfs
.id
)
671 ret
= atk_gitm(data
, data
->debugfs
.id
);
675 buf
= (struct atk_acpi_ret_buffer
*)ret
->buffer
.pointer
;
685 DEFINE_SIMPLE_ATTRIBUTE(atk_debugfs_gitm
,
686 atk_debugfs_gitm_get
,
690 static int atk_acpi_print(char *buf
, size_t sz
, union acpi_object
*obj
)
695 case ACPI_TYPE_INTEGER
:
696 ret
= snprintf(buf
, sz
, "0x%08llx\n", obj
->integer
.value
);
698 case ACPI_TYPE_STRING
:
699 ret
= snprintf(buf
, sz
, "%s\n", obj
->string
.pointer
);
706 static void atk_pack_print(char *buf
, size_t sz
, union acpi_object
*pack
)
711 for (i
= 0; i
< pack
->package
.count
; i
++) {
712 union acpi_object
*obj
= &pack
->package
.elements
[i
];
714 ret
= atk_acpi_print(buf
, sz
, obj
);
722 static int atk_debugfs_ggrp_open(struct inode
*inode
, struct file
*file
)
724 struct atk_data
*data
= inode
->i_private
;
726 union acpi_object
*ret
;
730 if (!data
->enumerate_handle
)
732 if (!data
->debugfs
.id
)
735 cls
= (data
->debugfs
.id
& 0xff000000) >> 24;
736 ret
= atk_ggrp(data
, cls
);
740 for (i
= 0; i
< ret
->package
.count
; i
++) {
741 union acpi_object
*pack
= &ret
->package
.elements
[i
];
742 union acpi_object
*id
;
744 if (pack
->type
!= ACPI_TYPE_PACKAGE
)
746 if (!pack
->package
.count
)
748 id
= &pack
->package
.elements
[0];
749 if (id
->integer
.value
== data
->debugfs
.id
) {
750 /* Print the package */
751 buf
= kzalloc(512, GFP_KERNEL
);
756 atk_pack_print(buf
, 512, pack
);
765 file
->private_data
= buf
;
767 return nonseekable_open(inode
, file
);
770 static ssize_t
atk_debugfs_ggrp_read(struct file
*file
, char __user
*buf
,
771 size_t count
, loff_t
*pos
)
773 char *str
= file
->private_data
;
774 size_t len
= strlen(str
);
776 return simple_read_from_buffer(buf
, count
, pos
, str
, len
);
779 static int atk_debugfs_ggrp_release(struct inode
*inode
, struct file
*file
)
781 kfree(file
->private_data
);
785 static const struct file_operations atk_debugfs_ggrp_fops
= {
786 .read
= atk_debugfs_ggrp_read
,
787 .open
= atk_debugfs_ggrp_open
,
788 .release
= atk_debugfs_ggrp_release
,
792 static void atk_debugfs_init(struct atk_data
*data
)
797 data
->debugfs
.id
= 0;
799 d
= debugfs_create_dir("asus_atk0110", NULL
);
803 f
= debugfs_create_x32("id", S_IRUSR
| S_IWUSR
, d
, &data
->debugfs
.id
);
807 f
= debugfs_create_file("gitm", S_IRUSR
, d
, data
,
812 f
= debugfs_create_file("ggrp", S_IRUSR
, d
, data
,
813 &atk_debugfs_ggrp_fops
);
817 data
->debugfs
.root
= d
;
821 debugfs_remove_recursive(d
);
824 static void atk_debugfs_cleanup(struct atk_data
*data
)
826 debugfs_remove_recursive(data
->debugfs
.root
);
829 #else /* CONFIG_DEBUG_FS */
831 static void atk_debugfs_init(struct atk_data
*data
)
835 static void atk_debugfs_cleanup(struct atk_data
*data
)
840 static int atk_add_sensor(struct atk_data
*data
, union acpi_object
*obj
)
842 struct device
*dev
= &data
->acpi_dev
->dev
;
843 union acpi_object
*flags
;
844 union acpi_object
*name
;
845 union acpi_object
*limit1
;
846 union acpi_object
*limit2
;
847 union acpi_object
*enable
;
848 struct atk_sensor_data
*sensor
;
849 char const *base_name
;
850 char const *limit1_name
;
851 char const *limit2_name
;
857 if (obj
->type
!= ACPI_TYPE_PACKAGE
) {
859 dev_warn(dev
, "Unknown type for ACPI object: (%d)\n",
864 err
= validate_hwmon_pack(data
, obj
);
868 /* Ok, we have a valid hwmon package */
869 type
= atk_get_pack_member(data
, obj
, HWMON_PACK_FLAGS
)->integer
.value
873 case HWMON_TYPE_VOLT
:
877 num
= &data
->voltage_count
;
880 case HWMON_TYPE_TEMP
:
883 limit2_name
= "crit";
884 num
= &data
->temperature_count
;
891 num
= &data
->fan_count
;
895 dev_warn(dev
, "Unknown sensor type: %#llx\n", type
);
899 enable
= atk_get_pack_member(data
, obj
, HWMON_PACK_ENABLE
);
900 if (!enable
->integer
.value
)
901 /* sensor is disabled */
904 flags
= atk_get_pack_member(data
, obj
, HWMON_PACK_FLAGS
);
905 name
= atk_get_pack_member(data
, obj
, HWMON_PACK_NAME
);
906 limit1
= atk_get_pack_member(data
, obj
, HWMON_PACK_LIMIT1
);
907 limit2
= atk_get_pack_member(data
, obj
, HWMON_PACK_LIMIT2
);
909 sensor
= kzalloc(sizeof(*sensor
), GFP_KERNEL
);
913 sensor
->acpi_name
= kstrdup(name
->string
.pointer
, GFP_KERNEL
);
914 if (!sensor
->acpi_name
) {
919 INIT_LIST_HEAD(&sensor
->list
);
922 sensor
->id
= flags
->integer
.value
;
923 sensor
->limit1
= limit1
->integer
.value
;
924 if (data
->old_interface
)
925 sensor
->limit2
= limit2
->integer
.value
;
927 /* The upper limit is expressed as delta from lower limit */
928 sensor
->limit2
= sensor
->limit1
+ limit2
->integer
.value
;
930 snprintf(sensor
->input_attr_name
, ATTR_NAME_SIZE
,
931 "%s%d_input", base_name
, start
+ *num
);
932 atk_init_attribute(&sensor
->input_attr
,
933 sensor
->input_attr_name
,
936 snprintf(sensor
->label_attr_name
, ATTR_NAME_SIZE
,
937 "%s%d_label", base_name
, start
+ *num
);
938 atk_init_attribute(&sensor
->label_attr
,
939 sensor
->label_attr_name
,
942 snprintf(sensor
->limit1_attr_name
, ATTR_NAME_SIZE
,
943 "%s%d_%s", base_name
, start
+ *num
, limit1_name
);
944 atk_init_attribute(&sensor
->limit1_attr
,
945 sensor
->limit1_attr_name
,
948 snprintf(sensor
->limit2_attr_name
, ATTR_NAME_SIZE
,
949 "%s%d_%s", base_name
, start
+ *num
, limit2_name
);
950 atk_init_attribute(&sensor
->limit2_attr
,
951 sensor
->limit2_attr_name
,
954 list_add(&sensor
->list
, &data
->sensor_list
);
959 kfree(sensor
->acpi_name
);
964 static int atk_enumerate_old_hwmon(struct atk_data
*data
)
966 struct device
*dev
= &data
->acpi_dev
->dev
;
967 struct acpi_buffer buf
;
968 union acpi_object
*pack
;
974 buf
.length
= ACPI_ALLOCATE_BUFFER
;
975 status
= acpi_evaluate_object_typed(data
->atk_handle
,
976 METHOD_OLD_ENUM_VLT
, NULL
, &buf
, ACPI_TYPE_PACKAGE
);
977 if (status
!= AE_OK
) {
978 dev_warn(dev
, METHOD_OLD_ENUM_VLT
": ACPI exception: %s\n",
979 acpi_format_exception(status
));
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
);
992 ACPI_FREE(buf
.pointer
);
995 buf
.length
= ACPI_ALLOCATE_BUFFER
;
996 status
= acpi_evaluate_object_typed(data
->atk_handle
,
997 METHOD_OLD_ENUM_TMP
, NULL
, &buf
, ACPI_TYPE_PACKAGE
);
998 if (status
!= AE_OK
) {
999 dev_warn(dev
, METHOD_OLD_ENUM_TMP
": ACPI exception: %s\n",
1000 acpi_format_exception(status
));
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
);
1014 ACPI_FREE(buf
.pointer
);
1017 buf
.length
= ACPI_ALLOCATE_BUFFER
;
1018 status
= acpi_evaluate_object_typed(data
->atk_handle
,
1019 METHOD_OLD_ENUM_FAN
, NULL
, &buf
, ACPI_TYPE_PACKAGE
);
1020 if (status
!= AE_OK
) {
1021 dev_warn(dev
, METHOD_OLD_ENUM_FAN
": ACPI exception: %s\n",
1022 acpi_format_exception(status
));
1029 for (i
= 1; i
< pack
->package
.count
; i
++) {
1030 union acpi_object
*obj
= &pack
->package
.elements
[i
];
1032 ret
= atk_add_sensor(data
, obj
);
1036 ACPI_FREE(buf
.pointer
);
1040 atk_free_sensors(data
);
1044 static int atk_ec_present(struct atk_data
*data
)
1046 struct device
*dev
= &data
->acpi_dev
->dev
;
1047 union acpi_object
*pack
;
1048 union acpi_object
*ec
;
1052 pack
= atk_ggrp(data
, ATK_MUX_MGMT
);
1054 if (PTR_ERR(pack
) == -ENOENT
) {
1055 /* The MGMT class does not exists - that's ok */
1056 dev_dbg(dev
, "Class %#llx not found\n", ATK_MUX_MGMT
);
1059 return PTR_ERR(pack
);
1064 for (i
= 0; i
< pack
->package
.count
; i
++) {
1065 union acpi_object
*obj
= &pack
->package
.elements
[i
];
1066 union acpi_object
*id
;
1068 if (obj
->type
!= ACPI_TYPE_PACKAGE
)
1071 id
= &obj
->package
.elements
[0];
1072 if (id
->type
!= ACPI_TYPE_INTEGER
)
1075 if (id
->integer
.value
== ATK_EC_ID
) {
1083 /* The system has no EC */
1084 dev_dbg(dev
, "EC not found\n");
1090 static int atk_ec_enabled(struct atk_data
*data
)
1092 struct device
*dev
= &data
->acpi_dev
->dev
;
1093 union acpi_object
*obj
;
1094 struct atk_acpi_ret_buffer
*buf
;
1097 obj
= atk_gitm(data
, ATK_EC_ID
);
1099 dev_err(dev
, "Unable to query EC status\n");
1100 return PTR_ERR(obj
);
1102 buf
= (struct atk_acpi_ret_buffer
*)obj
->buffer
.pointer
;
1104 if (buf
->flags
== 0) {
1105 dev_err(dev
, "Unable to query EC status\n");
1108 err
= (buf
->value
!= 0);
1109 dev_dbg(dev
, "EC is %sabled\n",
1110 err
? "en" : "dis");
1117 static int atk_ec_ctl(struct atk_data
*data
, int enable
)
1119 struct device
*dev
= &data
->acpi_dev
->dev
;
1120 union acpi_object
*obj
;
1121 struct atk_acpi_input_buf sitm
;
1122 struct atk_acpi_ret_buffer
*ec_ret
;
1125 sitm
.id
= ATK_EC_ID
;
1126 sitm
.param1
= enable
;
1129 obj
= atk_sitm(data
, &sitm
);
1131 dev_err(dev
, "Failed to %sable the EC\n",
1132 enable
? "en" : "dis");
1133 return PTR_ERR(obj
);
1135 ec_ret
= (struct atk_acpi_ret_buffer
*)obj
->buffer
.pointer
;
1136 if (ec_ret
->flags
== 0) {
1137 dev_err(dev
, "Failed to %sable the EC\n",
1138 enable
? "en" : "dis");
1141 dev_info(dev
, "EC %sabled\n",
1142 enable
? "en" : "dis");
1149 static int atk_enumerate_new_hwmon(struct atk_data
*data
)
1151 struct device
*dev
= &data
->acpi_dev
->dev
;
1152 union acpi_object
*pack
;
1156 err
= atk_ec_present(data
);
1160 err
= atk_ec_enabled(data
);
1163 /* If the EC was disabled we will disable it again on unload */
1164 data
->disable_ec
= err
;
1166 err
= atk_ec_ctl(data
, 1);
1168 data
->disable_ec
= false;
1173 dev_dbg(dev
, "Enumerating hwmon sensors\n");
1175 pack
= atk_ggrp(data
, ATK_MUX_HWMON
);
1177 return PTR_ERR(pack
);
1179 for (i
= 0; i
< pack
->package
.count
; i
++) {
1180 union acpi_object
*obj
= &pack
->package
.elements
[i
];
1182 atk_add_sensor(data
, obj
);
1185 err
= data
->voltage_count
+ data
->temperature_count
+ data
->fan_count
;
1191 static int atk_create_files(struct atk_data
*data
)
1193 struct atk_sensor_data
*s
;
1196 list_for_each_entry(s
, &data
->sensor_list
, list
) {
1197 err
= device_create_file(data
->hwmon_dev
, &s
->input_attr
);
1200 err
= device_create_file(data
->hwmon_dev
, &s
->label_attr
);
1203 err
= device_create_file(data
->hwmon_dev
, &s
->limit1_attr
);
1206 err
= device_create_file(data
->hwmon_dev
, &s
->limit2_attr
);
1211 err
= device_create_file(data
->hwmon_dev
, &atk_name_attr
);
1216 static void atk_remove_files(struct atk_data
*data
)
1218 struct atk_sensor_data
*s
;
1220 list_for_each_entry(s
, &data
->sensor_list
, list
) {
1221 device_remove_file(data
->hwmon_dev
, &s
->input_attr
);
1222 device_remove_file(data
->hwmon_dev
, &s
->label_attr
);
1223 device_remove_file(data
->hwmon_dev
, &s
->limit1_attr
);
1224 device_remove_file(data
->hwmon_dev
, &s
->limit2_attr
);
1226 device_remove_file(data
->hwmon_dev
, &atk_name_attr
);
1229 static void atk_free_sensors(struct atk_data
*data
)
1231 struct list_head
*head
= &data
->sensor_list
;
1232 struct atk_sensor_data
*s
, *tmp
;
1234 list_for_each_entry_safe(s
, tmp
, head
, list
) {
1235 kfree(s
->acpi_name
);
1240 static int atk_register_hwmon(struct atk_data
*data
)
1242 struct device
*dev
= &data
->acpi_dev
->dev
;
1245 dev_dbg(dev
, "registering hwmon device\n");
1246 data
->hwmon_dev
= hwmon_device_register(dev
);
1247 if (IS_ERR(data
->hwmon_dev
))
1248 return PTR_ERR(data
->hwmon_dev
);
1250 dev_dbg(dev
, "populating sysfs directory\n");
1251 err
= atk_create_files(data
);
1257 /* Cleanup the registered files */
1258 atk_remove_files(data
);
1259 hwmon_device_unregister(data
->hwmon_dev
);
1263 static int atk_probe_if(struct atk_data
*data
)
1265 struct device
*dev
= &data
->acpi_dev
->dev
;
1270 /* RTMP: read temperature */
1271 status
= acpi_get_handle(data
->atk_handle
, METHOD_OLD_READ_TMP
, &ret
);
1272 if (ACPI_SUCCESS(status
))
1273 data
->rtmp_handle
= ret
;
1275 dev_dbg(dev
, "method " METHOD_OLD_READ_TMP
" not found: %s\n",
1276 acpi_format_exception(status
));
1278 /* RVLT: read voltage */
1279 status
= acpi_get_handle(data
->atk_handle
, METHOD_OLD_READ_VLT
, &ret
);
1280 if (ACPI_SUCCESS(status
))
1281 data
->rvlt_handle
= ret
;
1283 dev_dbg(dev
, "method " METHOD_OLD_READ_VLT
" not found: %s\n",
1284 acpi_format_exception(status
));
1286 /* RFAN: read fan status */
1287 status
= acpi_get_handle(data
->atk_handle
, METHOD_OLD_READ_FAN
, &ret
);
1288 if (ACPI_SUCCESS(status
))
1289 data
->rfan_handle
= ret
;
1291 dev_dbg(dev
, "method " METHOD_OLD_READ_FAN
" not found: %s\n",
1292 acpi_format_exception(status
));
1295 status
= acpi_get_handle(data
->atk_handle
, METHOD_ENUMERATE
, &ret
);
1296 if (ACPI_SUCCESS(status
))
1297 data
->enumerate_handle
= ret
;
1299 dev_dbg(dev
, "method " METHOD_ENUMERATE
" not found: %s\n",
1300 acpi_format_exception(status
));
1302 /* De-multiplexer (read) */
1303 status
= acpi_get_handle(data
->atk_handle
, METHOD_READ
, &ret
);
1304 if (ACPI_SUCCESS(status
))
1305 data
->read_handle
= ret
;
1307 dev_dbg(dev
, "method " METHOD_READ
" not found: %s\n",
1308 acpi_format_exception(status
));
1310 /* De-multiplexer (write) */
1311 status
= acpi_get_handle(data
->atk_handle
, METHOD_WRITE
, &ret
);
1312 if (ACPI_SUCCESS(status
))
1313 data
->write_handle
= ret
;
1315 dev_dbg(dev
, "method " METHOD_WRITE
" not found: %s\n",
1316 acpi_format_exception(status
));
1319 * Check for hwmon methods: first check "old" style methods; note that
1320 * both may be present: in this case we stick to the old interface;
1321 * analysis of multiple DSDTs indicates that when both interfaces
1322 * are present the new one (GGRP/GITM) is not functional.
1325 dev_info(dev
, "Overriding interface detection\n");
1326 if (data
->rtmp_handle
&&
1327 data
->rvlt_handle
&& data
->rfan_handle
&& !new_if
)
1328 data
->old_interface
= true;
1329 else if (data
->enumerate_handle
&& data
->read_handle
&&
1331 data
->old_interface
= false;
1338 static int atk_add(struct acpi_device
*device
)
1342 struct acpi_buffer buf
;
1343 union acpi_object
*obj
;
1344 struct atk_data
*data
;
1346 dev_dbg(&device
->dev
, "adding...\n");
1348 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
1352 data
->acpi_dev
= device
;
1353 data
->atk_handle
= device
->handle
;
1354 INIT_LIST_HEAD(&data
->sensor_list
);
1355 data
->disable_ec
= false;
1357 buf
.length
= ACPI_ALLOCATE_BUFFER
;
1358 ret
= acpi_evaluate_object_typed(data
->atk_handle
, BOARD_ID
, NULL
,
1359 &buf
, ACPI_TYPE_PACKAGE
);
1361 dev_dbg(&device
->dev
, "atk: method MBIF not found\n");
1364 if (obj
->package
.count
>= 2) {
1365 union acpi_object
*id
= &obj
->package
.elements
[1];
1366 if (id
->type
== ACPI_TYPE_STRING
)
1367 dev_dbg(&device
->dev
, "board ID = %s\n",
1368 id
->string
.pointer
);
1370 ACPI_FREE(buf
.pointer
);
1373 err
= atk_probe_if(data
);
1375 dev_err(&device
->dev
, "No usable hwmon interface detected\n");
1379 if (data
->old_interface
) {
1380 dev_dbg(&device
->dev
, "Using old hwmon interface\n");
1381 err
= atk_enumerate_old_hwmon(data
);
1383 dev_dbg(&device
->dev
, "Using new hwmon interface\n");
1384 err
= atk_enumerate_new_hwmon(data
);
1389 dev_info(&device
->dev
,
1390 "No usable sensor detected, bailing out\n");
1395 err
= atk_register_hwmon(data
);
1399 atk_debugfs_init(data
);
1401 device
->driver_data
= data
;
1404 atk_free_sensors(data
);
1406 if (data
->disable_ec
)
1407 atk_ec_ctl(data
, 0);
1412 static int atk_remove(struct acpi_device
*device
, int type
)
1414 struct atk_data
*data
= device
->driver_data
;
1415 dev_dbg(&device
->dev
, "removing...\n");
1417 device
->driver_data
= NULL
;
1419 atk_debugfs_cleanup(data
);
1421 atk_remove_files(data
);
1422 atk_free_sensors(data
);
1423 hwmon_device_unregister(data
->hwmon_dev
);
1425 if (data
->disable_ec
) {
1426 if (atk_ec_ctl(data
, 0))
1427 dev_err(&device
->dev
, "Failed to disable EC\n");
1435 static int __init
atk0110_init(void)
1439 /* Make sure it's safe to access the device through ACPI */
1440 if (!acpi_resources_are_enforced()) {
1441 pr_err("Resources not safely usable due to acpi_enforce_resources kernel parameter\n");
1445 if (dmi_check_system(atk_force_new_if
))
1448 ret
= acpi_bus_register_driver(&atk_driver
);
1450 pr_info("acpi_bus_register_driver failed: %d\n", ret
);
1455 static void __exit
atk0110_exit(void)
1457 acpi_bus_unregister_driver(&atk_driver
);
1460 module_init(atk0110_init
);
1461 module_exit(atk0110_exit
);
1463 MODULE_LICENSE("GPL");