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
;
128 struct attribute_group attr_group
;
129 const struct attribute_group
*attr_groups
[2];
138 typedef ssize_t (*sysfs_show_func
)(struct device
*dev
,
139 struct device_attribute
*attr
, char *buf
);
141 static const struct acpi_device_id atk_ids
[] = {
145 MODULE_DEVICE_TABLE(acpi
, atk_ids
);
147 #define ATTR_NAME_SIZE 16 /* Worst case is "tempN_input" */
149 struct atk_sensor_data
{
150 struct list_head list
;
151 struct atk_data
*data
;
152 struct device_attribute label_attr
;
153 struct device_attribute input_attr
;
154 struct device_attribute limit1_attr
;
155 struct device_attribute limit2_attr
;
156 char label_attr_name
[ATTR_NAME_SIZE
];
157 char input_attr_name
[ATTR_NAME_SIZE
];
158 char limit1_attr_name
[ATTR_NAME_SIZE
];
159 char limit2_attr_name
[ATTR_NAME_SIZE
];
165 unsigned long last_updated
; /* in jiffies */
167 char const *acpi_name
;
171 * Return buffer format:
172 * [0-3] "value" is valid flag
174 * [8- ] unknown stuff on newer mobos
176 struct atk_acpi_ret_buffer
{
182 /* Input buffer used for GITM and SITM methods */
183 struct atk_acpi_input_buf
{
189 static int atk_add(struct acpi_device
*device
);
190 static int atk_remove(struct acpi_device
*device
);
191 static void atk_print_sensor(struct atk_data
*data
, union acpi_object
*obj
);
192 static int atk_read_value(struct atk_sensor_data
*sensor
, u64
*value
);
194 static struct acpi_driver atk_driver
= {
200 .remove
= atk_remove
,
204 #define input_to_atk_sensor(attr) \
205 container_of(attr, struct atk_sensor_data, input_attr)
207 #define label_to_atk_sensor(attr) \
208 container_of(attr, struct atk_sensor_data, label_attr)
210 #define limit1_to_atk_sensor(attr) \
211 container_of(attr, struct atk_sensor_data, limit1_attr)
213 #define limit2_to_atk_sensor(attr) \
214 container_of(attr, struct atk_sensor_data, limit2_attr)
216 static ssize_t
atk_input_show(struct device
*dev
,
217 struct device_attribute
*attr
, char *buf
)
219 struct atk_sensor_data
*s
= input_to_atk_sensor(attr
);
223 err
= atk_read_value(s
, &value
);
227 if (s
->type
== HWMON_TYPE_TEMP
)
228 /* ACPI returns decidegree */
231 return sprintf(buf
, "%llu\n", value
);
234 static ssize_t
atk_label_show(struct device
*dev
,
235 struct device_attribute
*attr
, char *buf
)
237 struct atk_sensor_data
*s
= label_to_atk_sensor(attr
);
239 return sprintf(buf
, "%s\n", s
->acpi_name
);
242 static ssize_t
atk_limit1_show(struct device
*dev
,
243 struct device_attribute
*attr
, char *buf
)
245 struct atk_sensor_data
*s
= limit1_to_atk_sensor(attr
);
246 u64 value
= s
->limit1
;
248 if (s
->type
== HWMON_TYPE_TEMP
)
251 return sprintf(buf
, "%lld\n", value
);
254 static ssize_t
atk_limit2_show(struct device
*dev
,
255 struct device_attribute
*attr
, char *buf
)
257 struct atk_sensor_data
*s
= limit2_to_atk_sensor(attr
);
258 u64 value
= s
->limit2
;
260 if (s
->type
== HWMON_TYPE_TEMP
)
263 return sprintf(buf
, "%lld\n", value
);
266 static void atk_init_attribute(struct device_attribute
*attr
, char *name
,
267 sysfs_show_func show
)
269 sysfs_attr_init(&attr
->attr
);
270 attr
->attr
.name
= name
;
271 attr
->attr
.mode
= 0444;
277 static union acpi_object
*atk_get_pack_member(struct atk_data
*data
,
278 union acpi_object
*pack
,
279 enum atk_pack_member m
)
281 bool old_if
= data
->old_interface
;
285 case HWMON_PACK_FLAGS
:
286 offset
= old_if
? _HWMON_OLD_PACK_FLAGS
: _HWMON_NEW_PACK_FLAGS
;
288 case HWMON_PACK_NAME
:
289 offset
= old_if
? _HWMON_OLD_PACK_NAME
: _HWMON_NEW_PACK_NAME
;
291 case HWMON_PACK_LIMIT1
:
292 offset
= old_if
? _HWMON_OLD_PACK_LIMIT1
:
293 _HWMON_NEW_PACK_LIMIT1
;
295 case HWMON_PACK_LIMIT2
:
296 offset
= old_if
? _HWMON_OLD_PACK_LIMIT2
:
297 _HWMON_NEW_PACK_LIMIT2
;
299 case HWMON_PACK_ENABLE
:
300 offset
= old_if
? _HWMON_OLD_PACK_ENABLE
:
301 _HWMON_NEW_PACK_ENABLE
;
307 return &pack
->package
.elements
[offset
];
312 * New package format is:
314 * class - used for de-muxing the request to the correct GITn
315 * type (volt, temp, fan)
317 * sensor id - used for de-muxing the request _inside_ the GITn
325 * The old package has the same format but it's missing the two unknown fields.
327 static int validate_hwmon_pack(struct atk_data
*data
, union acpi_object
*obj
)
329 struct device
*dev
= &data
->acpi_dev
->dev
;
330 union acpi_object
*tmp
;
331 bool old_if
= data
->old_interface
;
332 int const expected_size
= old_if
? _HWMON_OLD_PACK_SIZE
:
333 _HWMON_NEW_PACK_SIZE
;
335 if (obj
->type
!= ACPI_TYPE_PACKAGE
) {
336 dev_warn(dev
, "Invalid type: %d\n", obj
->type
);
340 if (obj
->package
.count
!= expected_size
) {
341 dev_warn(dev
, "Invalid package size: %d, expected: %d\n",
342 obj
->package
.count
, expected_size
);
346 tmp
= atk_get_pack_member(data
, obj
, HWMON_PACK_FLAGS
);
347 if (tmp
->type
!= ACPI_TYPE_INTEGER
) {
348 dev_warn(dev
, "Invalid type (flag): %d\n", tmp
->type
);
352 tmp
= atk_get_pack_member(data
, obj
, HWMON_PACK_NAME
);
353 if (tmp
->type
!= ACPI_TYPE_STRING
) {
354 dev_warn(dev
, "Invalid type (name): %d\n", tmp
->type
);
358 /* Don't check... we don't know what they're useful for anyway */
360 tmp
= &obj
->package
.elements
[HWMON_PACK_UNK1
];
361 if (tmp
->type
!= ACPI_TYPE_INTEGER
) {
362 dev_warn(dev
, "Invalid type (unk1): %d\n", tmp
->type
);
366 tmp
= &obj
->package
.elements
[HWMON_PACK_UNK2
];
367 if (tmp
->type
!= ACPI_TYPE_INTEGER
) {
368 dev_warn(dev
, "Invalid type (unk2): %d\n", tmp
->type
);
373 tmp
= atk_get_pack_member(data
, obj
, HWMON_PACK_LIMIT1
);
374 if (tmp
->type
!= ACPI_TYPE_INTEGER
) {
375 dev_warn(dev
, "Invalid type (limit1): %d\n", tmp
->type
);
379 tmp
= atk_get_pack_member(data
, obj
, HWMON_PACK_LIMIT2
);
380 if (tmp
->type
!= ACPI_TYPE_INTEGER
) {
381 dev_warn(dev
, "Invalid type (limit2): %d\n", tmp
->type
);
385 tmp
= atk_get_pack_member(data
, obj
, HWMON_PACK_ENABLE
);
386 if (tmp
->type
!= ACPI_TYPE_INTEGER
) {
387 dev_warn(dev
, "Invalid type (enable): %d\n", tmp
->type
);
391 atk_print_sensor(data
, obj
);
397 static char const *atk_sensor_type(union acpi_object
*flags
)
399 u64 type
= flags
->integer
.value
& ATK_TYPE_MASK
;
403 case HWMON_TYPE_VOLT
:
406 case HWMON_TYPE_TEMP
:
407 what
= "temperature";
421 static void atk_print_sensor(struct atk_data
*data
, union acpi_object
*obj
)
424 struct device
*dev
= &data
->acpi_dev
->dev
;
425 union acpi_object
*flags
;
426 union acpi_object
*name
;
427 union acpi_object
*limit1
;
428 union acpi_object
*limit2
;
429 union acpi_object
*enable
;
432 flags
= atk_get_pack_member(data
, obj
, HWMON_PACK_FLAGS
);
433 name
= atk_get_pack_member(data
, obj
, HWMON_PACK_NAME
);
434 limit1
= atk_get_pack_member(data
, obj
, HWMON_PACK_LIMIT1
);
435 limit2
= atk_get_pack_member(data
, obj
, HWMON_PACK_LIMIT2
);
436 enable
= atk_get_pack_member(data
, obj
, HWMON_PACK_ENABLE
);
438 what
= atk_sensor_type(flags
);
440 dev_dbg(dev
, "%s: %#llx %s [%llu-%llu] %s\n", what
,
441 flags
->integer
.value
,
442 name
->string
.pointer
,
443 limit1
->integer
.value
, limit2
->integer
.value
,
444 enable
->integer
.value
? "enabled" : "disabled");
448 static int atk_read_value_old(struct atk_sensor_data
*sensor
, u64
*value
)
450 struct atk_data
*data
= sensor
->data
;
451 struct device
*dev
= &data
->acpi_dev
->dev
;
452 struct acpi_object_list params
;
453 union acpi_object id
;
457 switch (sensor
->type
) {
458 case HWMON_TYPE_VOLT
:
459 method
= data
->rvlt_handle
;
461 case HWMON_TYPE_TEMP
:
462 method
= data
->rtmp_handle
;
465 method
= data
->rfan_handle
;
471 id
.type
= ACPI_TYPE_INTEGER
;
472 id
.integer
.value
= sensor
->id
;
475 params
.pointer
= &id
;
477 status
= acpi_evaluate_integer(method
, NULL
, ¶ms
, value
);
478 if (status
!= AE_OK
) {
479 dev_warn(dev
, "%s: ACPI exception: %s\n", __func__
,
480 acpi_format_exception(status
));
487 static union acpi_object
*atk_ggrp(struct atk_data
*data
, u16 mux
)
489 struct device
*dev
= &data
->acpi_dev
->dev
;
490 struct acpi_buffer buf
;
492 struct acpi_object_list params
;
493 union acpi_object id
;
494 union acpi_object
*pack
;
496 id
.type
= ACPI_TYPE_INTEGER
;
497 id
.integer
.value
= mux
;
499 params
.pointer
= &id
;
501 buf
.length
= ACPI_ALLOCATE_BUFFER
;
502 ret
= acpi_evaluate_object(data
->enumerate_handle
, NULL
, ¶ms
, &buf
);
504 dev_err(dev
, "GGRP[%#x] ACPI exception: %s\n", mux
,
505 acpi_format_exception(ret
));
506 return ERR_PTR(-EIO
);
509 if (pack
->type
!= ACPI_TYPE_PACKAGE
) {
510 /* Execution was successful, but the id was not found */
512 return ERR_PTR(-ENOENT
);
515 if (pack
->package
.count
< 1) {
516 dev_err(dev
, "GGRP[%#x] package is too small\n", mux
);
518 return ERR_PTR(-EIO
);
523 static union acpi_object
*atk_gitm(struct atk_data
*data
, u64 id
)
525 struct device
*dev
= &data
->acpi_dev
->dev
;
526 struct atk_acpi_input_buf buf
;
527 union acpi_object tmp
;
528 struct acpi_object_list params
;
529 struct acpi_buffer ret
;
530 union acpi_object
*obj
;
537 tmp
.type
= ACPI_TYPE_BUFFER
;
538 tmp
.buffer
.pointer
= (u8
*)&buf
;
539 tmp
.buffer
.length
= sizeof(buf
);
542 params
.pointer
= (void *)&tmp
;
544 ret
.length
= ACPI_ALLOCATE_BUFFER
;
545 status
= acpi_evaluate_object_typed(data
->read_handle
, NULL
, ¶ms
,
546 &ret
, ACPI_TYPE_BUFFER
);
547 if (status
!= AE_OK
) {
548 dev_warn(dev
, "GITM[%#llx] ACPI exception: %s\n", id
,
549 acpi_format_exception(status
));
550 return ERR_PTR(-EIO
);
555 if (obj
->buffer
.length
< 8) {
556 dev_warn(dev
, "Unexpected ASBF length: %u\n",
559 return ERR_PTR(-EIO
);
564 static union acpi_object
*atk_sitm(struct atk_data
*data
,
565 struct atk_acpi_input_buf
*buf
)
567 struct device
*dev
= &data
->acpi_dev
->dev
;
568 struct acpi_object_list params
;
569 union acpi_object tmp
;
570 struct acpi_buffer ret
;
571 union acpi_object
*obj
;
574 tmp
.type
= ACPI_TYPE_BUFFER
;
575 tmp
.buffer
.pointer
= (u8
*)buf
;
576 tmp
.buffer
.length
= sizeof(*buf
);
579 params
.pointer
= &tmp
;
581 ret
.length
= ACPI_ALLOCATE_BUFFER
;
582 status
= acpi_evaluate_object_typed(data
->write_handle
, NULL
, ¶ms
,
583 &ret
, ACPI_TYPE_BUFFER
);
584 if (status
!= AE_OK
) {
585 dev_warn(dev
, "SITM[%#x] ACPI exception: %s\n", buf
->id
,
586 acpi_format_exception(status
));
587 return ERR_PTR(-EIO
);
592 if (obj
->buffer
.length
< 8) {
593 dev_warn(dev
, "Unexpected ASBF length: %u\n",
596 return ERR_PTR(-EIO
);
601 static int atk_read_value_new(struct atk_sensor_data
*sensor
, u64
*value
)
603 struct atk_data
*data
= sensor
->data
;
604 struct device
*dev
= &data
->acpi_dev
->dev
;
605 union acpi_object
*obj
;
606 struct atk_acpi_ret_buffer
*buf
;
609 obj
= atk_gitm(data
, sensor
->id
);
613 buf
= (struct atk_acpi_ret_buffer
*)obj
->buffer
.pointer
;
614 if (buf
->flags
== 0) {
616 * The reading is not valid, possible causes:
618 * - enumeration was FUBAR (and we didn't notice)
620 dev_warn(dev
, "Read failed, sensor = %#llx\n", sensor
->id
);
631 static int atk_read_value(struct atk_sensor_data
*sensor
, u64
*value
)
635 if (!sensor
->is_valid
||
636 time_after(jiffies
, sensor
->last_updated
+ CACHE_TIME
)) {
637 if (sensor
->data
->old_interface
)
638 err
= atk_read_value_old(sensor
, value
);
640 err
= atk_read_value_new(sensor
, value
);
645 sensor
->is_valid
= true;
646 sensor
->last_updated
= jiffies
;
647 sensor
->cached_value
= *value
;
649 *value
= sensor
->cached_value
;
656 #ifdef CONFIG_DEBUG_FS
657 static int atk_debugfs_gitm_get(void *p
, u64
*val
)
659 struct atk_data
*data
= p
;
660 union acpi_object
*ret
;
661 struct atk_acpi_ret_buffer
*buf
;
664 if (!data
->read_handle
)
667 if (!data
->debugfs
.id
)
670 ret
= atk_gitm(data
, data
->debugfs
.id
);
674 buf
= (struct atk_acpi_ret_buffer
*)ret
->buffer
.pointer
;
684 DEFINE_SIMPLE_ATTRIBUTE(atk_debugfs_gitm
,
685 atk_debugfs_gitm_get
,
689 static int atk_acpi_print(char *buf
, size_t sz
, union acpi_object
*obj
)
694 case ACPI_TYPE_INTEGER
:
695 ret
= snprintf(buf
, sz
, "0x%08llx\n", obj
->integer
.value
);
697 case ACPI_TYPE_STRING
:
698 ret
= snprintf(buf
, sz
, "%s\n", obj
->string
.pointer
);
705 static void atk_pack_print(char *buf
, size_t sz
, union acpi_object
*pack
)
710 for (i
= 0; i
< pack
->package
.count
; i
++) {
711 union acpi_object
*obj
= &pack
->package
.elements
[i
];
713 ret
= atk_acpi_print(buf
, sz
, obj
);
721 static int atk_debugfs_ggrp_open(struct inode
*inode
, struct file
*file
)
723 struct atk_data
*data
= inode
->i_private
;
725 union acpi_object
*ret
;
729 if (!data
->enumerate_handle
)
731 if (!data
->debugfs
.id
)
734 cls
= (data
->debugfs
.id
& 0xff000000) >> 24;
735 ret
= atk_ggrp(data
, cls
);
739 for (i
= 0; i
< ret
->package
.count
; i
++) {
740 union acpi_object
*pack
= &ret
->package
.elements
[i
];
741 union acpi_object
*id
;
743 if (pack
->type
!= ACPI_TYPE_PACKAGE
)
745 if (!pack
->package
.count
)
747 id
= &pack
->package
.elements
[0];
748 if (id
->integer
.value
== data
->debugfs
.id
) {
749 /* Print the package */
750 buf
= kzalloc(512, GFP_KERNEL
);
755 atk_pack_print(buf
, 512, pack
);
764 file
->private_data
= buf
;
766 return nonseekable_open(inode
, file
);
769 static ssize_t
atk_debugfs_ggrp_read(struct file
*file
, char __user
*buf
,
770 size_t count
, loff_t
*pos
)
772 char *str
= file
->private_data
;
773 size_t len
= strlen(str
);
775 return simple_read_from_buffer(buf
, count
, pos
, str
, len
);
778 static int atk_debugfs_ggrp_release(struct inode
*inode
, struct file
*file
)
780 kfree(file
->private_data
);
784 static const struct file_operations atk_debugfs_ggrp_fops
= {
785 .read
= atk_debugfs_ggrp_read
,
786 .open
= atk_debugfs_ggrp_open
,
787 .release
= atk_debugfs_ggrp_release
,
791 static void atk_debugfs_init(struct atk_data
*data
)
796 data
->debugfs
.id
= 0;
798 d
= debugfs_create_dir("asus_atk0110", NULL
);
802 f
= debugfs_create_x32("id", S_IRUSR
| S_IWUSR
, d
, &data
->debugfs
.id
);
806 f
= debugfs_create_file("gitm", S_IRUSR
, d
, data
,
811 f
= debugfs_create_file("ggrp", S_IRUSR
, d
, data
,
812 &atk_debugfs_ggrp_fops
);
816 data
->debugfs
.root
= d
;
820 debugfs_remove_recursive(d
);
823 static void atk_debugfs_cleanup(struct atk_data
*data
)
825 debugfs_remove_recursive(data
->debugfs
.root
);
828 #else /* CONFIG_DEBUG_FS */
830 static void atk_debugfs_init(struct atk_data
*data
)
834 static void atk_debugfs_cleanup(struct atk_data
*data
)
839 static int atk_add_sensor(struct atk_data
*data
, union acpi_object
*obj
)
841 struct device
*dev
= &data
->acpi_dev
->dev
;
842 union acpi_object
*flags
;
843 union acpi_object
*name
;
844 union acpi_object
*limit1
;
845 union acpi_object
*limit2
;
846 union acpi_object
*enable
;
847 struct atk_sensor_data
*sensor
;
848 char const *base_name
;
849 char const *limit1_name
;
850 char const *limit2_name
;
856 if (obj
->type
!= ACPI_TYPE_PACKAGE
) {
858 dev_warn(dev
, "Unknown type for ACPI object: (%d)\n",
863 err
= validate_hwmon_pack(data
, obj
);
867 /* Ok, we have a valid hwmon package */
868 type
= atk_get_pack_member(data
, obj
, HWMON_PACK_FLAGS
)->integer
.value
872 case HWMON_TYPE_VOLT
:
876 num
= &data
->voltage_count
;
879 case HWMON_TYPE_TEMP
:
882 limit2_name
= "crit";
883 num
= &data
->temperature_count
;
890 num
= &data
->fan_count
;
894 dev_warn(dev
, "Unknown sensor type: %#llx\n", type
);
898 enable
= atk_get_pack_member(data
, obj
, HWMON_PACK_ENABLE
);
899 if (!enable
->integer
.value
)
900 /* sensor is disabled */
903 flags
= atk_get_pack_member(data
, obj
, HWMON_PACK_FLAGS
);
904 name
= atk_get_pack_member(data
, obj
, HWMON_PACK_NAME
);
905 limit1
= atk_get_pack_member(data
, obj
, HWMON_PACK_LIMIT1
);
906 limit2
= atk_get_pack_member(data
, obj
, HWMON_PACK_LIMIT2
);
908 sensor
= devm_kzalloc(dev
, sizeof(*sensor
), GFP_KERNEL
);
912 sensor
->acpi_name
= devm_kstrdup(dev
, name
->string
.pointer
, GFP_KERNEL
);
913 if (!sensor
->acpi_name
)
916 INIT_LIST_HEAD(&sensor
->list
);
919 sensor
->id
= flags
->integer
.value
;
920 sensor
->limit1
= limit1
->integer
.value
;
921 if (data
->old_interface
)
922 sensor
->limit2
= limit2
->integer
.value
;
924 /* The upper limit is expressed as delta from lower limit */
925 sensor
->limit2
= sensor
->limit1
+ limit2
->integer
.value
;
927 snprintf(sensor
->input_attr_name
, ATTR_NAME_SIZE
,
928 "%s%d_input", base_name
, start
+ *num
);
929 atk_init_attribute(&sensor
->input_attr
,
930 sensor
->input_attr_name
,
933 snprintf(sensor
->label_attr_name
, ATTR_NAME_SIZE
,
934 "%s%d_label", base_name
, start
+ *num
);
935 atk_init_attribute(&sensor
->label_attr
,
936 sensor
->label_attr_name
,
939 snprintf(sensor
->limit1_attr_name
, ATTR_NAME_SIZE
,
940 "%s%d_%s", base_name
, start
+ *num
, limit1_name
);
941 atk_init_attribute(&sensor
->limit1_attr
,
942 sensor
->limit1_attr_name
,
945 snprintf(sensor
->limit2_attr_name
, ATTR_NAME_SIZE
,
946 "%s%d_%s", base_name
, start
+ *num
, limit2_name
);
947 atk_init_attribute(&sensor
->limit2_attr
,
948 sensor
->limit2_attr_name
,
951 list_add(&sensor
->list
, &data
->sensor_list
);
957 static int atk_enumerate_old_hwmon(struct atk_data
*data
)
959 struct device
*dev
= &data
->acpi_dev
->dev
;
960 struct acpi_buffer buf
;
961 union acpi_object
*pack
;
967 buf
.length
= ACPI_ALLOCATE_BUFFER
;
968 status
= acpi_evaluate_object_typed(data
->atk_handle
,
969 METHOD_OLD_ENUM_VLT
, NULL
, &buf
, ACPI_TYPE_PACKAGE
);
970 if (status
!= AE_OK
) {
971 dev_warn(dev
, METHOD_OLD_ENUM_VLT
": ACPI exception: %s\n",
972 acpi_format_exception(status
));
978 for (i
= 1; i
< pack
->package
.count
; i
++) {
979 union acpi_object
*obj
= &pack
->package
.elements
[i
];
981 ret
= atk_add_sensor(data
, obj
);
985 ACPI_FREE(buf
.pointer
);
988 buf
.length
= ACPI_ALLOCATE_BUFFER
;
989 status
= acpi_evaluate_object_typed(data
->atk_handle
,
990 METHOD_OLD_ENUM_TMP
, NULL
, &buf
, ACPI_TYPE_PACKAGE
);
991 if (status
!= AE_OK
) {
992 dev_warn(dev
, METHOD_OLD_ENUM_TMP
": ACPI exception: %s\n",
993 acpi_format_exception(status
));
999 for (i
= 1; i
< pack
->package
.count
; i
++) {
1000 union acpi_object
*obj
= &pack
->package
.elements
[i
];
1002 ret
= atk_add_sensor(data
, obj
);
1006 ACPI_FREE(buf
.pointer
);
1009 buf
.length
= ACPI_ALLOCATE_BUFFER
;
1010 status
= acpi_evaluate_object_typed(data
->atk_handle
,
1011 METHOD_OLD_ENUM_FAN
, NULL
, &buf
, ACPI_TYPE_PACKAGE
);
1012 if (status
!= AE_OK
) {
1013 dev_warn(dev
, METHOD_OLD_ENUM_FAN
": ACPI exception: %s\n",
1014 acpi_format_exception(status
));
1020 for (i
= 1; i
< pack
->package
.count
; i
++) {
1021 union acpi_object
*obj
= &pack
->package
.elements
[i
];
1023 ret
= atk_add_sensor(data
, obj
);
1027 ACPI_FREE(buf
.pointer
);
1032 static int atk_ec_present(struct atk_data
*data
)
1034 struct device
*dev
= &data
->acpi_dev
->dev
;
1035 union acpi_object
*pack
;
1036 union acpi_object
*ec
;
1040 pack
= atk_ggrp(data
, ATK_MUX_MGMT
);
1042 if (PTR_ERR(pack
) == -ENOENT
) {
1043 /* The MGMT class does not exists - that's ok */
1044 dev_dbg(dev
, "Class %#llx not found\n", ATK_MUX_MGMT
);
1047 return PTR_ERR(pack
);
1052 for (i
= 0; i
< pack
->package
.count
; i
++) {
1053 union acpi_object
*obj
= &pack
->package
.elements
[i
];
1054 union acpi_object
*id
;
1056 if (obj
->type
!= ACPI_TYPE_PACKAGE
)
1059 id
= &obj
->package
.elements
[0];
1060 if (id
->type
!= ACPI_TYPE_INTEGER
)
1063 if (id
->integer
.value
== ATK_EC_ID
) {
1071 /* The system has no EC */
1072 dev_dbg(dev
, "EC not found\n");
1078 static int atk_ec_enabled(struct atk_data
*data
)
1080 struct device
*dev
= &data
->acpi_dev
->dev
;
1081 union acpi_object
*obj
;
1082 struct atk_acpi_ret_buffer
*buf
;
1085 obj
= atk_gitm(data
, ATK_EC_ID
);
1087 dev_err(dev
, "Unable to query EC status\n");
1088 return PTR_ERR(obj
);
1090 buf
= (struct atk_acpi_ret_buffer
*)obj
->buffer
.pointer
;
1092 if (buf
->flags
== 0) {
1093 dev_err(dev
, "Unable to query EC status\n");
1096 err
= (buf
->value
!= 0);
1097 dev_dbg(dev
, "EC is %sabled\n",
1098 err
? "en" : "dis");
1105 static int atk_ec_ctl(struct atk_data
*data
, int enable
)
1107 struct device
*dev
= &data
->acpi_dev
->dev
;
1108 union acpi_object
*obj
;
1109 struct atk_acpi_input_buf sitm
;
1110 struct atk_acpi_ret_buffer
*ec_ret
;
1113 sitm
.id
= ATK_EC_ID
;
1114 sitm
.param1
= enable
;
1117 obj
= atk_sitm(data
, &sitm
);
1119 dev_err(dev
, "Failed to %sable the EC\n",
1120 enable
? "en" : "dis");
1121 return PTR_ERR(obj
);
1123 ec_ret
= (struct atk_acpi_ret_buffer
*)obj
->buffer
.pointer
;
1124 if (ec_ret
->flags
== 0) {
1125 dev_err(dev
, "Failed to %sable the EC\n",
1126 enable
? "en" : "dis");
1129 dev_info(dev
, "EC %sabled\n",
1130 enable
? "en" : "dis");
1137 static int atk_enumerate_new_hwmon(struct atk_data
*data
)
1139 struct device
*dev
= &data
->acpi_dev
->dev
;
1140 union acpi_object
*pack
;
1144 err
= atk_ec_present(data
);
1148 err
= atk_ec_enabled(data
);
1151 /* If the EC was disabled we will disable it again on unload */
1152 data
->disable_ec
= err
;
1154 err
= atk_ec_ctl(data
, 1);
1156 data
->disable_ec
= false;
1161 dev_dbg(dev
, "Enumerating hwmon sensors\n");
1163 pack
= atk_ggrp(data
, ATK_MUX_HWMON
);
1165 return PTR_ERR(pack
);
1167 for (i
= 0; i
< pack
->package
.count
; i
++) {
1168 union acpi_object
*obj
= &pack
->package
.elements
[i
];
1170 atk_add_sensor(data
, obj
);
1173 err
= data
->voltage_count
+ data
->temperature_count
+ data
->fan_count
;
1179 static int atk_init_attribute_groups(struct atk_data
*data
)
1181 struct device
*dev
= &data
->acpi_dev
->dev
;
1182 struct atk_sensor_data
*s
;
1183 struct attribute
**attrs
;
1185 int len
= (data
->voltage_count
+ data
->temperature_count
1186 + data
->fan_count
) * 4 + 1;
1188 attrs
= devm_kcalloc(dev
, len
, sizeof(struct attribute
*), GFP_KERNEL
);
1192 list_for_each_entry(s
, &data
->sensor_list
, list
) {
1193 attrs
[i
++] = &s
->input_attr
.attr
;
1194 attrs
[i
++] = &s
->label_attr
.attr
;
1195 attrs
[i
++] = &s
->limit1_attr
.attr
;
1196 attrs
[i
++] = &s
->limit2_attr
.attr
;
1199 data
->attr_group
.attrs
= attrs
;
1200 data
->attr_groups
[0] = &data
->attr_group
;
1205 static int atk_register_hwmon(struct atk_data
*data
)
1207 struct device
*dev
= &data
->acpi_dev
->dev
;
1209 dev_dbg(dev
, "registering hwmon device\n");
1210 data
->hwmon_dev
= hwmon_device_register_with_groups(dev
, "atk0110",
1213 if (IS_ERR(data
->hwmon_dev
))
1214 return PTR_ERR(data
->hwmon_dev
);
1219 static int atk_probe_if(struct atk_data
*data
)
1221 struct device
*dev
= &data
->acpi_dev
->dev
;
1226 /* RTMP: read temperature */
1227 status
= acpi_get_handle(data
->atk_handle
, METHOD_OLD_READ_TMP
, &ret
);
1228 if (ACPI_SUCCESS(status
))
1229 data
->rtmp_handle
= ret
;
1231 dev_dbg(dev
, "method " METHOD_OLD_READ_TMP
" not found: %s\n",
1232 acpi_format_exception(status
));
1234 /* RVLT: read voltage */
1235 status
= acpi_get_handle(data
->atk_handle
, METHOD_OLD_READ_VLT
, &ret
);
1236 if (ACPI_SUCCESS(status
))
1237 data
->rvlt_handle
= ret
;
1239 dev_dbg(dev
, "method " METHOD_OLD_READ_VLT
" not found: %s\n",
1240 acpi_format_exception(status
));
1242 /* RFAN: read fan status */
1243 status
= acpi_get_handle(data
->atk_handle
, METHOD_OLD_READ_FAN
, &ret
);
1244 if (ACPI_SUCCESS(status
))
1245 data
->rfan_handle
= ret
;
1247 dev_dbg(dev
, "method " METHOD_OLD_READ_FAN
" not found: %s\n",
1248 acpi_format_exception(status
));
1251 status
= acpi_get_handle(data
->atk_handle
, METHOD_ENUMERATE
, &ret
);
1252 if (ACPI_SUCCESS(status
))
1253 data
->enumerate_handle
= ret
;
1255 dev_dbg(dev
, "method " METHOD_ENUMERATE
" not found: %s\n",
1256 acpi_format_exception(status
));
1258 /* De-multiplexer (read) */
1259 status
= acpi_get_handle(data
->atk_handle
, METHOD_READ
, &ret
);
1260 if (ACPI_SUCCESS(status
))
1261 data
->read_handle
= ret
;
1263 dev_dbg(dev
, "method " METHOD_READ
" not found: %s\n",
1264 acpi_format_exception(status
));
1266 /* De-multiplexer (write) */
1267 status
= acpi_get_handle(data
->atk_handle
, METHOD_WRITE
, &ret
);
1268 if (ACPI_SUCCESS(status
))
1269 data
->write_handle
= ret
;
1271 dev_dbg(dev
, "method " METHOD_WRITE
" not found: %s\n",
1272 acpi_format_exception(status
));
1275 * Check for hwmon methods: first check "old" style methods; note that
1276 * both may be present: in this case we stick to the old interface;
1277 * analysis of multiple DSDTs indicates that when both interfaces
1278 * are present the new one (GGRP/GITM) is not functional.
1281 dev_info(dev
, "Overriding interface detection\n");
1282 if (data
->rtmp_handle
&&
1283 data
->rvlt_handle
&& data
->rfan_handle
&& !new_if
)
1284 data
->old_interface
= true;
1285 else if (data
->enumerate_handle
&& data
->read_handle
&&
1287 data
->old_interface
= false;
1294 static int atk_add(struct acpi_device
*device
)
1298 struct acpi_buffer buf
;
1299 union acpi_object
*obj
;
1300 struct atk_data
*data
;
1302 dev_dbg(&device
->dev
, "adding...\n");
1304 data
= devm_kzalloc(&device
->dev
, sizeof(*data
), GFP_KERNEL
);
1308 data
->acpi_dev
= device
;
1309 data
->atk_handle
= device
->handle
;
1310 INIT_LIST_HEAD(&data
->sensor_list
);
1311 data
->disable_ec
= false;
1313 buf
.length
= ACPI_ALLOCATE_BUFFER
;
1314 ret
= acpi_evaluate_object_typed(data
->atk_handle
, BOARD_ID
, NULL
,
1315 &buf
, ACPI_TYPE_PACKAGE
);
1317 dev_dbg(&device
->dev
, "atk: method MBIF not found\n");
1320 if (obj
->package
.count
>= 2) {
1321 union acpi_object
*id
= &obj
->package
.elements
[1];
1322 if (id
->type
== ACPI_TYPE_STRING
)
1323 dev_dbg(&device
->dev
, "board ID = %s\n",
1324 id
->string
.pointer
);
1326 ACPI_FREE(buf
.pointer
);
1329 err
= atk_probe_if(data
);
1331 dev_err(&device
->dev
, "No usable hwmon interface detected\n");
1335 if (data
->old_interface
) {
1336 dev_dbg(&device
->dev
, "Using old hwmon interface\n");
1337 err
= atk_enumerate_old_hwmon(data
);
1339 dev_dbg(&device
->dev
, "Using new hwmon interface\n");
1340 err
= atk_enumerate_new_hwmon(data
);
1345 dev_info(&device
->dev
,
1346 "No usable sensor detected, bailing out\n");
1351 err
= atk_init_attribute_groups(data
);
1354 err
= atk_register_hwmon(data
);
1358 atk_debugfs_init(data
);
1360 device
->driver_data
= data
;
1363 if (data
->disable_ec
)
1364 atk_ec_ctl(data
, 0);
1368 static int atk_remove(struct acpi_device
*device
)
1370 struct atk_data
*data
= device
->driver_data
;
1371 dev_dbg(&device
->dev
, "removing...\n");
1373 device
->driver_data
= NULL
;
1375 atk_debugfs_cleanup(data
);
1377 hwmon_device_unregister(data
->hwmon_dev
);
1379 if (data
->disable_ec
) {
1380 if (atk_ec_ctl(data
, 0))
1381 dev_err(&device
->dev
, "Failed to disable EC\n");
1387 static int __init
atk0110_init(void)
1391 /* Make sure it's safe to access the device through ACPI */
1392 if (!acpi_resources_are_enforced()) {
1393 pr_err("Resources not safely usable due to acpi_enforce_resources kernel parameter\n");
1397 if (dmi_check_system(atk_force_new_if
))
1400 ret
= acpi_bus_register_driver(&atk_driver
);
1402 pr_info("acpi_bus_register_driver failed: %d\n", ret
);
1407 static void __exit
atk0110_exit(void)
1409 acpi_bus_unregister_driver(&atk_driver
);
1412 module_init(atk0110_init
);
1413 module_exit(atk0110_exit
);
1415 MODULE_LICENSE("GPL");