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>
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
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
{
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
87 struct device
*hwmon_dev
;
88 acpi_handle atk_handle
;
89 struct acpi_device
*acpi_dev
;
94 acpi_handle rtmp_handle
;
95 acpi_handle rvlt_handle
;
96 acpi_handle rfan_handle
;
98 acpi_handle enumerate_handle
;
99 acpi_handle read_handle
;
100 acpi_handle write_handle
;
105 int temperature_count
;
107 struct list_head sensor_list
;
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
[] = {
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
];
143 unsigned long last_updated
; /* in jiffies */
145 char const *acpi_name
;
148 /* Return buffer format:
149 * [0-3] "value" is valid flag
151 * [8- ] unknown stuff on newer mobos
153 struct atk_acpi_ret_buffer
{
159 /* Input buffer used for GITM and SITM methods */
160 struct atk_acpi_input_buf
{
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
= {
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
);
201 err
= atk_read_value(s
, &value
);
205 if (s
->type
== HWMON_TYPE_TEMP
)
206 /* ACPI returns decidegree */
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
)
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
)
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;
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
;
270 case HWMON_PACK_FLAGS
:
271 offset
= old_if
? _HWMON_OLD_PACK_FLAGS
: _HWMON_NEW_PACK_FLAGS
;
273 case HWMON_PACK_NAME
:
274 offset
= old_if
? _HWMON_OLD_PACK_NAME
: _HWMON_NEW_PACK_NAME
;
276 case HWMON_PACK_LIMIT1
:
277 offset
= old_if
? _HWMON_OLD_PACK_LIMIT1
:
278 _HWMON_NEW_PACK_LIMIT1
;
280 case HWMON_PACK_LIMIT2
:
281 offset
= old_if
? _HWMON_OLD_PACK_LIMIT2
:
282 _HWMON_NEW_PACK_LIMIT2
;
284 case HWMON_PACK_ENABLE
:
285 offset
= old_if
? _HWMON_OLD_PACK_ENABLE
:
286 _HWMON_NEW_PACK_ENABLE
;
292 return &pack
->package
.elements
[offset
];
296 /* New package format is:
298 * class - used for de-muxing the request to the correct GITn
299 * type (volt, temp, fan)
301 * sensor id - used for de-muxing the request _inside_ the GITn
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
);
324 if (obj
->package
.count
!= expected_size
) {
325 dev_warn(dev
, "Invalid package size: %d, expected: %d\n",
326 obj
->package
.count
, expected_size
);
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
);
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
);
342 /* Don't check... we don't know what they're useful for anyway */
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
);
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
);
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
);
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
);
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
);
375 atk_print_sensor(data
, obj
);
381 static char const *atk_sensor_type(union acpi_object
*flags
)
383 u64 type
= flags
->integer
.value
& ATK_TYPE_MASK
;
387 case HWMON_TYPE_VOLT
:
390 case HWMON_TYPE_TEMP
:
391 what
= "temperature";
405 static void atk_print_sensor(struct atk_data
*data
, union acpi_object
*obj
)
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
;
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");
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
;
441 switch (sensor
->type
) {
442 case HWMON_TYPE_VOLT
:
443 method
= data
->rvlt_handle
;
445 case HWMON_TYPE_TEMP
:
446 method
= data
->rtmp_handle
;
449 method
= data
->rfan_handle
;
455 id
.type
= ACPI_TYPE_INTEGER
;
456 id
.integer
.value
= sensor
->id
;
459 params
.pointer
= &id
;
461 status
= acpi_evaluate_integer(method
, NULL
, ¶ms
, value
);
462 if (status
!= AE_OK
) {
463 dev_warn(dev
, "%s: ACPI exception: %s\n", __func__
,
464 acpi_format_exception(status
));
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
;
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
;
483 params
.pointer
= &id
;
485 buf
.length
= ACPI_ALLOCATE_BUFFER
;
486 ret
= acpi_evaluate_object(data
->enumerate_handle
, NULL
, ¶ms
, &buf
);
488 dev_err(dev
, "GGRP[%#x] ACPI exception: %s\n", mux
,
489 acpi_format_exception(ret
));
490 return ERR_PTR(-EIO
);
493 if (pack
->type
!= ACPI_TYPE_PACKAGE
) {
494 /* Execution was successful, but the id was not found */
496 return ERR_PTR(-ENOENT
);
499 if (pack
->package
.count
< 1) {
500 dev_err(dev
, "GGRP[%#x] package is too small\n", mux
);
502 return ERR_PTR(-EIO
);
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
;
521 tmp
.type
= ACPI_TYPE_BUFFER
;
522 tmp
.buffer
.pointer
= (u8
*)&buf
;
523 tmp
.buffer
.length
= sizeof(buf
);
526 params
.pointer
= (void *)&tmp
;
528 ret
.length
= ACPI_ALLOCATE_BUFFER
;
529 status
= acpi_evaluate_object_typed(data
->read_handle
, NULL
, ¶ms
,
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
);
539 if (obj
->buffer
.length
< 8) {
540 dev_warn(dev
, "Unexpected ASBF length: %u\n",
543 return ERR_PTR(-EIO
);
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
;
558 tmp
.type
= ACPI_TYPE_BUFFER
;
559 tmp
.buffer
.pointer
= (u8
*)buf
;
560 tmp
.buffer
.length
= sizeof(*buf
);
563 params
.pointer
= &tmp
;
565 ret
.length
= ACPI_ALLOCATE_BUFFER
;
566 status
= acpi_evaluate_object_typed(data
->write_handle
, NULL
, ¶ms
,
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
);
576 if (obj
->buffer
.length
< 8) {
577 dev_warn(dev
, "Unexpected ASBF length: %u\n",
580 return ERR_PTR(-EIO
);
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
;
593 obj
= atk_gitm(data
, sensor
->id
);
597 buf
= (struct atk_acpi_ret_buffer
*)obj
->buffer
.pointer
;
598 if (buf
->flags
== 0) {
599 /* The reading is not valid, possible causes:
601 * - enumeration was FUBAR (and we didn't notice)
603 dev_warn(dev
, "Read failed, sensor = %#llx\n", sensor
->id
);
614 static int atk_read_value(struct atk_sensor_data
*sensor
, u64
*value
)
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
);
623 err
= atk_read_value_new(sensor
, value
);
625 sensor
->is_valid
= true;
626 sensor
->last_updated
= jiffies
;
627 sensor
->cached_value
= *value
;
629 *value
= sensor
->cached_value
;
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
;
644 if (!data
->read_handle
)
647 if (!data
->debugfs
.id
)
650 ret
= atk_gitm(data
, data
->debugfs
.id
);
654 buf
= (struct atk_acpi_ret_buffer
*)ret
->buffer
.pointer
;
663 DEFINE_SIMPLE_ATTRIBUTE(atk_debugfs_gitm
,
664 atk_debugfs_gitm_get
,
668 static int atk_acpi_print(char *buf
, size_t sz
, union acpi_object
*obj
)
673 case ACPI_TYPE_INTEGER
:
674 ret
= snprintf(buf
, sz
, "0x%08llx\n", obj
->integer
.value
);
676 case ACPI_TYPE_STRING
:
677 ret
= snprintf(buf
, sz
, "%s\n", obj
->string
.pointer
);
684 static void atk_pack_print(char *buf
, size_t sz
, union acpi_object
*pack
)
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
);
700 static int atk_debugfs_ggrp_open(struct inode
*inode
, struct file
*file
)
702 struct atk_data
*data
= inode
->i_private
;
704 union acpi_object
*ret
;
708 if (!data
->enumerate_handle
)
710 if (!data
->debugfs
.id
)
713 cls
= (data
->debugfs
.id
& 0xff000000) >> 24;
714 ret
= atk_ggrp(data
, cls
);
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
)
724 if (!pack
->package
.count
)
726 id
= &pack
->package
.elements
[0];
727 if (id
->integer
.value
== data
->debugfs
.id
) {
728 /* Print the package */
729 buf
= kzalloc(512, GFP_KERNEL
);
734 atk_pack_print(buf
, 512, pack
);
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
);
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
,
770 static void atk_debugfs_init(struct atk_data
*data
)
775 data
->debugfs
.id
= 0;
777 d
= debugfs_create_dir("asus_atk0110", NULL
);
781 f
= debugfs_create_x32("id", S_IRUSR
| S_IWUSR
, d
, &data
->debugfs
.id
);
785 f
= debugfs_create_file("gitm", S_IRUSR
, d
, data
,
790 f
= debugfs_create_file("ggrp", S_IRUSR
, d
, data
,
791 &atk_debugfs_ggrp_fops
);
795 data
->debugfs
.root
= d
;
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
)
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
;
835 if (obj
->type
!= ACPI_TYPE_PACKAGE
) {
837 dev_warn(dev
, "Unknown type for ACPI object: (%d)\n",
842 err
= validate_hwmon_pack(data
, obj
);
846 /* Ok, we have a valid hwmon package */
847 type
= atk_get_pack_member(data
, obj
, HWMON_PACK_FLAGS
)->integer
.value
851 case HWMON_TYPE_VOLT
:
855 num
= &data
->voltage_count
;
858 case HWMON_TYPE_TEMP
:
861 limit2_name
= "crit";
862 num
= &data
->temperature_count
;
869 num
= &data
->fan_count
;
873 dev_warn(dev
, "Unknown sensor type: %#llx\n", type
);
877 enable
= atk_get_pack_member(data
, obj
, HWMON_PACK_ENABLE
);
878 if (!enable
->integer
.value
)
879 /* sensor is disabled */
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
);
891 sensor
->acpi_name
= kstrdup(name
->string
.pointer
, GFP_KERNEL
);
892 if (!sensor
->acpi_name
) {
897 INIT_LIST_HEAD(&sensor
->list
);
900 sensor
->id
= flags
->integer
.value
;
901 sensor
->limit1
= limit1
->integer
.value
;
902 if (data
->old_interface
)
903 sensor
->limit2
= limit2
->integer
.value
;
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
,
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
,
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
,
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
,
932 list_add(&sensor
->list
, &data
->sensor_list
);
937 kfree(sensor
->acpi_name
);
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
;
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
));
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
);
970 ACPI_FREE(buf
.pointer
);
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
));
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_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
));
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
);
1018 atk_free_sensors(data
);
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
;
1030 pack
= atk_ggrp(data
, ATK_MUX_MGMT
);
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
);
1037 return PTR_ERR(pack
);
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
)
1049 id
= &obj
->package
.elements
[0];
1050 if (id
->type
!= ACPI_TYPE_INTEGER
)
1053 if (id
->integer
.value
== ATK_EC_ID
) {
1061 /* The system has no EC */
1062 dev_dbg(dev
, "EC not found\n");
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
;
1075 obj
= atk_gitm(data
, ATK_EC_ID
);
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");
1086 err
= (buf
->value
!= 0);
1087 dev_dbg(dev
, "EC is %sabled\n",
1088 err
? "en" : "dis");
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
;
1103 sitm
.id
= ATK_EC_ID
;
1104 sitm
.param1
= enable
;
1107 obj
= atk_sitm(data
, &sitm
);
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");
1119 dev_info(dev
, "EC %sabled\n",
1120 enable
? "en" : "dis");
1127 static int atk_enumerate_new_hwmon(struct atk_data
*data
)
1129 struct device
*dev
= &data
->acpi_dev
->dev
;
1130 union acpi_object
*pack
;
1134 err
= atk_ec_present(data
);
1138 err
= atk_ec_enabled(data
);
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);
1146 data
->disable_ec
= false;
1151 dev_dbg(dev
, "Enumerating hwmon sensors\n");
1153 pack
= atk_ggrp(data
, ATK_MUX_HWMON
);
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
;
1169 static int atk_create_files(struct atk_data
*data
)
1171 struct atk_sensor_data
*s
;
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
);
1179 sysfs_attr_init(&s
->label_attr
.attr
);
1180 err
= device_create_file(data
->hwmon_dev
, &s
->label_attr
);
1183 sysfs_attr_init(&s
->limit1_attr
.attr
);
1184 err
= device_create_file(data
->hwmon_dev
, &s
->limit1_attr
);
1187 sysfs_attr_init(&s
->limit2_attr
.attr
);
1188 err
= device_create_file(data
->hwmon_dev
, &s
->limit2_attr
);
1193 err
= device_create_file(data
->hwmon_dev
, &atk_name_attr
);
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
);
1222 static int atk_register_hwmon(struct atk_data
*data
)
1224 struct device
*dev
= &data
->acpi_dev
->dev
;
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
);
1239 /* Cleanup the registered files */
1240 atk_remove_files(data
);
1241 hwmon_device_unregister(data
->hwmon_dev
);
1245 static int atk_probe_if(struct atk_data
*data
)
1247 struct device
*dev
= &data
->acpi_dev
->dev
;
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
;
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
;
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
;
1273 dev_dbg(dev
, "method " METHOD_OLD_READ_FAN
" not found: %s\n",
1274 acpi_format_exception(status
));
1277 status
= acpi_get_handle(data
->atk_handle
, METHOD_ENUMERATE
, &ret
);
1278 if (ACPI_SUCCESS(status
))
1279 data
->enumerate_handle
= ret
;
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
;
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
;
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
&&
1309 data
->old_interface
= false;
1316 static int atk_add(struct acpi_device
*device
)
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
);
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
);
1339 dev_dbg(&device
->dev
, "atk: method MBIF not found\n");
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
);
1353 dev_err(&device
->dev
, "No usable hwmon interface detected\n");
1357 if (data
->old_interface
) {
1358 dev_dbg(&device
->dev
, "Using old hwmon interface\n");
1359 err
= atk_enumerate_old_hwmon(data
);
1361 dev_dbg(&device
->dev
, "Using new hwmon interface\n");
1362 err
= atk_enumerate_new_hwmon(data
);
1367 dev_info(&device
->dev
,
1368 "No usable sensor detected, bailing out\n");
1373 err
= atk_register_hwmon(data
);
1377 atk_debugfs_init(data
);
1379 device
->driver_data
= data
;
1382 atk_free_sensors(data
);
1384 if (data
->disable_ec
)
1385 atk_ec_ctl(data
, 0);
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");
1413 static int __init
atk0110_init(void)
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");
1423 ret
= acpi_bus_register_driver(&atk_driver
);
1425 pr_info("acpi_bus_register_driver failed: %d\n", 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");