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 #include <linux/debugfs.h>
9 #include <linux/kernel.h>
10 #include <linux/hwmon.h>
11 #include <linux/list.h>
12 #include <linux/module.h>
14 #include <acpi/acpi.h>
15 #include <acpi/acpixf.h>
16 #include <acpi/acpi_drivers.h>
17 #include <acpi/acpi_bus.h>
20 #define ATK_HID "ATK0110"
22 /* Minimum time between readings, enforced in order to avoid
27 #define BOARD_ID "MBIF"
28 #define METHOD_ENUMERATE "GGRP"
29 #define METHOD_READ "GITM"
30 #define METHOD_WRITE "SITM"
31 #define METHOD_OLD_READ_TMP "RTMP"
32 #define METHOD_OLD_READ_VLT "RVLT"
33 #define METHOD_OLD_READ_FAN "RFAN"
34 #define METHOD_OLD_ENUM_TMP "TSIF"
35 #define METHOD_OLD_ENUM_VLT "VSIF"
36 #define METHOD_OLD_ENUM_FAN "FSIF"
38 #define ATK_MUX_HWMON 0x00000006ULL
39 #define ATK_MUX_MGMT 0x00000011ULL
41 #define ATK_CLASS_MASK 0xff000000ULL
42 #define ATK_CLASS_FREQ_CTL 0x03000000ULL
43 #define ATK_CLASS_FAN_CTL 0x04000000ULL
44 #define ATK_CLASS_HWMON 0x06000000ULL
45 #define ATK_CLASS_MGMT 0x11000000ULL
47 #define ATK_TYPE_MASK 0x00ff0000ULL
48 #define HWMON_TYPE_VOLT 0x00020000ULL
49 #define HWMON_TYPE_TEMP 0x00030000ULL
50 #define HWMON_TYPE_FAN 0x00040000ULL
52 #define ATK_ELEMENT_ID_MASK 0x0000ffffULL
54 #define ATK_EC_ID 0x11060004ULL
56 enum atk_pack_member
{
64 /* New package format */
65 #define _HWMON_NEW_PACK_SIZE 7
66 #define _HWMON_NEW_PACK_FLAGS 0
67 #define _HWMON_NEW_PACK_NAME 1
68 #define _HWMON_NEW_PACK_UNK1 2
69 #define _HWMON_NEW_PACK_UNK2 3
70 #define _HWMON_NEW_PACK_LIMIT1 4
71 #define _HWMON_NEW_PACK_LIMIT2 5
72 #define _HWMON_NEW_PACK_ENABLE 6
74 /* Old package format */
75 #define _HWMON_OLD_PACK_SIZE 5
76 #define _HWMON_OLD_PACK_FLAGS 0
77 #define _HWMON_OLD_PACK_NAME 1
78 #define _HWMON_OLD_PACK_LIMIT1 2
79 #define _HWMON_OLD_PACK_LIMIT2 3
80 #define _HWMON_OLD_PACK_ENABLE 4
84 struct device
*hwmon_dev
;
85 acpi_handle atk_handle
;
86 struct acpi_device
*acpi_dev
;
91 acpi_handle rtmp_handle
;
92 acpi_handle rvlt_handle
;
93 acpi_handle rfan_handle
;
95 acpi_handle enumerate_handle
;
96 acpi_handle read_handle
;
97 acpi_handle write_handle
;
102 int temperature_count
;
104 struct list_head sensor_list
;
113 typedef ssize_t (*sysfs_show_func
)(struct device
*dev
,
114 struct device_attribute
*attr
, char *buf
);
116 static const struct acpi_device_id atk_ids
[] = {
120 MODULE_DEVICE_TABLE(acpi
, atk_ids
);
122 #define ATTR_NAME_SIZE 16 /* Worst case is "tempN_input" */
124 struct atk_sensor_data
{
125 struct list_head list
;
126 struct atk_data
*data
;
127 struct device_attribute label_attr
;
128 struct device_attribute input_attr
;
129 struct device_attribute limit1_attr
;
130 struct device_attribute limit2_attr
;
131 char label_attr_name
[ATTR_NAME_SIZE
];
132 char input_attr_name
[ATTR_NAME_SIZE
];
133 char limit1_attr_name
[ATTR_NAME_SIZE
];
134 char limit2_attr_name
[ATTR_NAME_SIZE
];
140 unsigned long last_updated
; /* in jiffies */
142 char const *acpi_name
;
145 /* Return buffer format:
146 * [0-3] "value" is valid flag
148 * [8- ] unknown stuff on newer mobos
150 struct atk_acpi_ret_buffer
{
156 /* Input buffer used for GITM and SITM methods */
157 struct atk_acpi_input_buf
{
163 static int atk_add(struct acpi_device
*device
);
164 static int atk_remove(struct acpi_device
*device
, int type
);
165 static void atk_print_sensor(struct atk_data
*data
, union acpi_object
*obj
);
166 static int atk_read_value(struct atk_sensor_data
*sensor
, u64
*value
);
167 static void atk_free_sensors(struct atk_data
*data
);
169 static struct acpi_driver atk_driver
= {
175 .remove
= atk_remove
,
179 #define input_to_atk_sensor(attr) \
180 container_of(attr, struct atk_sensor_data, input_attr)
182 #define label_to_atk_sensor(attr) \
183 container_of(attr, struct atk_sensor_data, label_attr)
185 #define limit1_to_atk_sensor(attr) \
186 container_of(attr, struct atk_sensor_data, limit1_attr)
188 #define limit2_to_atk_sensor(attr) \
189 container_of(attr, struct atk_sensor_data, limit2_attr)
191 static ssize_t
atk_input_show(struct device
*dev
,
192 struct device_attribute
*attr
, char *buf
)
194 struct atk_sensor_data
*s
= input_to_atk_sensor(attr
);
198 err
= atk_read_value(s
, &value
);
202 if (s
->type
== HWMON_TYPE_TEMP
)
203 /* ACPI returns decidegree */
206 return sprintf(buf
, "%llu\n", value
);
209 static ssize_t
atk_label_show(struct device
*dev
,
210 struct device_attribute
*attr
, char *buf
)
212 struct atk_sensor_data
*s
= label_to_atk_sensor(attr
);
214 return sprintf(buf
, "%s\n", s
->acpi_name
);
217 static ssize_t
atk_limit1_show(struct device
*dev
,
218 struct device_attribute
*attr
, char *buf
)
220 struct atk_sensor_data
*s
= limit1_to_atk_sensor(attr
);
221 u64 value
= s
->limit1
;
223 if (s
->type
== HWMON_TYPE_TEMP
)
226 return sprintf(buf
, "%lld\n", value
);
229 static ssize_t
atk_limit2_show(struct device
*dev
,
230 struct device_attribute
*attr
, char *buf
)
232 struct atk_sensor_data
*s
= limit2_to_atk_sensor(attr
);
233 u64 value
= s
->limit2
;
235 if (s
->type
== HWMON_TYPE_TEMP
)
238 return sprintf(buf
, "%lld\n", value
);
241 static ssize_t
atk_name_show(struct device
*dev
,
242 struct device_attribute
*attr
, char *buf
)
244 return sprintf(buf
, "atk0110\n");
246 static struct device_attribute atk_name_attr
=
247 __ATTR(name
, 0444, atk_name_show
, NULL
);
249 static void atk_init_attribute(struct device_attribute
*attr
, char *name
,
250 sysfs_show_func show
)
252 attr
->attr
.name
= name
;
253 attr
->attr
.mode
= 0444;
259 static union acpi_object
*atk_get_pack_member(struct atk_data
*data
,
260 union acpi_object
*pack
,
261 enum atk_pack_member m
)
263 bool old_if
= data
->old_interface
;
267 case HWMON_PACK_FLAGS
:
268 offset
= old_if
? _HWMON_OLD_PACK_FLAGS
: _HWMON_NEW_PACK_FLAGS
;
270 case HWMON_PACK_NAME
:
271 offset
= old_if
? _HWMON_OLD_PACK_NAME
: _HWMON_NEW_PACK_NAME
;
273 case HWMON_PACK_LIMIT1
:
274 offset
= old_if
? _HWMON_OLD_PACK_LIMIT1
:
275 _HWMON_NEW_PACK_LIMIT1
;
277 case HWMON_PACK_LIMIT2
:
278 offset
= old_if
? _HWMON_OLD_PACK_LIMIT2
:
279 _HWMON_NEW_PACK_LIMIT2
;
281 case HWMON_PACK_ENABLE
:
282 offset
= old_if
? _HWMON_OLD_PACK_ENABLE
:
283 _HWMON_NEW_PACK_ENABLE
;
289 return &pack
->package
.elements
[offset
];
293 /* New package format is:
295 * class - used for de-muxing the request to the correct GITn
296 * type (volt, temp, fan)
298 * sensor id - used for de-muxing the request _inside_ the GITn
306 * The old package has the same format but it's missing the two unknown fields.
308 static int validate_hwmon_pack(struct atk_data
*data
, union acpi_object
*obj
)
310 struct device
*dev
= &data
->acpi_dev
->dev
;
311 union acpi_object
*tmp
;
312 bool old_if
= data
->old_interface
;
313 int const expected_size
= old_if
? _HWMON_OLD_PACK_SIZE
:
314 _HWMON_NEW_PACK_SIZE
;
316 if (obj
->type
!= ACPI_TYPE_PACKAGE
) {
317 dev_warn(dev
, "Invalid type: %d\n", obj
->type
);
321 if (obj
->package
.count
!= expected_size
) {
322 dev_warn(dev
, "Invalid package size: %d, expected: %d\n",
323 obj
->package
.count
, expected_size
);
327 tmp
= atk_get_pack_member(data
, obj
, HWMON_PACK_FLAGS
);
328 if (tmp
->type
!= ACPI_TYPE_INTEGER
) {
329 dev_warn(dev
, "Invalid type (flag): %d\n", tmp
->type
);
333 tmp
= atk_get_pack_member(data
, obj
, HWMON_PACK_NAME
);
334 if (tmp
->type
!= ACPI_TYPE_STRING
) {
335 dev_warn(dev
, "Invalid type (name): %d\n", tmp
->type
);
339 /* Don't check... we don't know what they're useful for anyway */
341 tmp
= &obj
->package
.elements
[HWMON_PACK_UNK1
];
342 if (tmp
->type
!= ACPI_TYPE_INTEGER
) {
343 dev_warn(dev
, "Invalid type (unk1): %d\n", tmp
->type
);
347 tmp
= &obj
->package
.elements
[HWMON_PACK_UNK2
];
348 if (tmp
->type
!= ACPI_TYPE_INTEGER
) {
349 dev_warn(dev
, "Invalid type (unk2): %d\n", tmp
->type
);
354 tmp
= atk_get_pack_member(data
, obj
, HWMON_PACK_LIMIT1
);
355 if (tmp
->type
!= ACPI_TYPE_INTEGER
) {
356 dev_warn(dev
, "Invalid type (limit1): %d\n", tmp
->type
);
360 tmp
= atk_get_pack_member(data
, obj
, HWMON_PACK_LIMIT2
);
361 if (tmp
->type
!= ACPI_TYPE_INTEGER
) {
362 dev_warn(dev
, "Invalid type (limit2): %d\n", tmp
->type
);
366 tmp
= atk_get_pack_member(data
, obj
, HWMON_PACK_ENABLE
);
367 if (tmp
->type
!= ACPI_TYPE_INTEGER
) {
368 dev_warn(dev
, "Invalid type (enable): %d\n", tmp
->type
);
372 atk_print_sensor(data
, obj
);
378 static char const *atk_sensor_type(union acpi_object
*flags
)
380 u64 type
= flags
->integer
.value
& ATK_TYPE_MASK
;
384 case HWMON_TYPE_VOLT
:
387 case HWMON_TYPE_TEMP
:
388 what
= "temperature";
402 static void atk_print_sensor(struct atk_data
*data
, union acpi_object
*obj
)
405 struct device
*dev
= &data
->acpi_dev
->dev
;
406 union acpi_object
*flags
;
407 union acpi_object
*name
;
408 union acpi_object
*limit1
;
409 union acpi_object
*limit2
;
410 union acpi_object
*enable
;
413 flags
= atk_get_pack_member(data
, obj
, HWMON_PACK_FLAGS
);
414 name
= atk_get_pack_member(data
, obj
, HWMON_PACK_NAME
);
415 limit1
= atk_get_pack_member(data
, obj
, HWMON_PACK_LIMIT1
);
416 limit2
= atk_get_pack_member(data
, obj
, HWMON_PACK_LIMIT2
);
417 enable
= atk_get_pack_member(data
, obj
, HWMON_PACK_ENABLE
);
419 what
= atk_sensor_type(flags
);
421 dev_dbg(dev
, "%s: %#llx %s [%llu-%llu] %s\n", what
,
422 flags
->integer
.value
,
423 name
->string
.pointer
,
424 limit1
->integer
.value
, limit2
->integer
.value
,
425 enable
->integer
.value
? "enabled" : "disabled");
429 static int atk_read_value_old(struct atk_sensor_data
*sensor
, u64
*value
)
431 struct atk_data
*data
= sensor
->data
;
432 struct device
*dev
= &data
->acpi_dev
->dev
;
433 struct acpi_object_list params
;
434 union acpi_object id
;
438 switch (sensor
->type
) {
439 case HWMON_TYPE_VOLT
:
440 method
= data
->rvlt_handle
;
442 case HWMON_TYPE_TEMP
:
443 method
= data
->rtmp_handle
;
446 method
= data
->rfan_handle
;
452 id
.type
= ACPI_TYPE_INTEGER
;
453 id
.integer
.value
= sensor
->id
;
456 params
.pointer
= &id
;
458 status
= acpi_evaluate_integer(method
, NULL
, ¶ms
, value
);
459 if (status
!= AE_OK
) {
460 dev_warn(dev
, "%s: ACPI exception: %s\n", __func__
,
461 acpi_format_exception(status
));
468 static union acpi_object
*atk_ggrp(struct atk_data
*data
, u16 mux
)
470 struct device
*dev
= &data
->acpi_dev
->dev
;
471 struct acpi_buffer buf
;
473 struct acpi_object_list params
;
474 union acpi_object id
;
475 union acpi_object
*pack
;
477 id
.type
= ACPI_TYPE_INTEGER
;
478 id
.integer
.value
= mux
;
480 params
.pointer
= &id
;
482 buf
.length
= ACPI_ALLOCATE_BUFFER
;
483 ret
= acpi_evaluate_object(data
->enumerate_handle
, NULL
, ¶ms
, &buf
);
485 dev_err(dev
, "GGRP[%#x] ACPI exception: %s\n", mux
,
486 acpi_format_exception(ret
));
487 return ERR_PTR(-EIO
);
490 if (pack
->type
!= ACPI_TYPE_PACKAGE
) {
491 /* Execution was successful, but the id was not found */
493 return ERR_PTR(-ENOENT
);
496 if (pack
->package
.count
< 1) {
497 dev_err(dev
, "GGRP[%#x] package is too small\n", mux
);
499 return ERR_PTR(-EIO
);
504 static union acpi_object
*atk_gitm(struct atk_data
*data
, u64 id
)
506 struct device
*dev
= &data
->acpi_dev
->dev
;
507 struct atk_acpi_input_buf buf
;
508 union acpi_object tmp
;
509 struct acpi_object_list params
;
510 struct acpi_buffer ret
;
511 union acpi_object
*obj
;
518 tmp
.type
= ACPI_TYPE_BUFFER
;
519 tmp
.buffer
.pointer
= (u8
*)&buf
;
520 tmp
.buffer
.length
= sizeof(buf
);
523 params
.pointer
= (void *)&tmp
;
525 ret
.length
= ACPI_ALLOCATE_BUFFER
;
526 status
= acpi_evaluate_object_typed(data
->read_handle
, NULL
, ¶ms
,
527 &ret
, ACPI_TYPE_BUFFER
);
528 if (status
!= AE_OK
) {
529 dev_warn(dev
, "GITM[%#llx] ACPI exception: %s\n", id
,
530 acpi_format_exception(status
));
531 return ERR_PTR(-EIO
);
536 if (obj
->buffer
.length
< 8) {
537 dev_warn(dev
, "Unexpected ASBF length: %u\n",
540 return ERR_PTR(-EIO
);
545 static union acpi_object
*atk_sitm(struct atk_data
*data
,
546 struct atk_acpi_input_buf
*buf
)
548 struct device
*dev
= &data
->acpi_dev
->dev
;
549 struct acpi_object_list params
;
550 union acpi_object tmp
;
551 struct acpi_buffer ret
;
552 union acpi_object
*obj
;
555 tmp
.type
= ACPI_TYPE_BUFFER
;
556 tmp
.buffer
.pointer
= (u8
*)buf
;
557 tmp
.buffer
.length
= sizeof(*buf
);
560 params
.pointer
= &tmp
;
562 ret
.length
= ACPI_ALLOCATE_BUFFER
;
563 status
= acpi_evaluate_object_typed(data
->write_handle
, NULL
, ¶ms
,
564 &ret
, ACPI_TYPE_BUFFER
);
565 if (status
!= AE_OK
) {
566 dev_warn(dev
, "SITM[%#x] ACPI exception: %s\n", buf
->id
,
567 acpi_format_exception(status
));
568 return ERR_PTR(-EIO
);
573 if (obj
->buffer
.length
< 8) {
574 dev_warn(dev
, "Unexpected ASBF length: %u\n",
577 return ERR_PTR(-EIO
);
582 static int atk_read_value_new(struct atk_sensor_data
*sensor
, u64
*value
)
584 struct atk_data
*data
= sensor
->data
;
585 struct device
*dev
= &data
->acpi_dev
->dev
;
586 union acpi_object
*obj
;
587 struct atk_acpi_ret_buffer
*buf
;
590 obj
= atk_gitm(data
, sensor
->id
);
594 buf
= (struct atk_acpi_ret_buffer
*)obj
->buffer
.pointer
;
595 if (buf
->flags
== 0) {
596 /* The reading is not valid, possible causes:
598 * - enumeration was FUBAR (and we didn't notice)
600 dev_warn(dev
, "Read failed, sensor = %#llx\n", sensor
->id
);
611 static int atk_read_value(struct atk_sensor_data
*sensor
, u64
*value
)
615 if (!sensor
->is_valid
||
616 time_after(jiffies
, sensor
->last_updated
+ CACHE_TIME
)) {
617 if (sensor
->data
->old_interface
)
618 err
= atk_read_value_old(sensor
, value
);
620 err
= atk_read_value_new(sensor
, value
);
622 sensor
->is_valid
= true;
623 sensor
->last_updated
= jiffies
;
624 sensor
->cached_value
= *value
;
626 *value
= sensor
->cached_value
;
633 #ifdef CONFIG_DEBUG_FS
634 static int atk_debugfs_gitm_get(void *p
, u64
*val
)
636 struct atk_data
*data
= p
;
637 union acpi_object
*ret
;
638 struct atk_acpi_ret_buffer
*buf
;
641 if (!data
->read_handle
)
644 if (!data
->debugfs
.id
)
647 ret
= atk_gitm(data
, data
->debugfs
.id
);
651 buf
= (struct atk_acpi_ret_buffer
*)ret
->buffer
.pointer
;
660 DEFINE_SIMPLE_ATTRIBUTE(atk_debugfs_gitm
,
661 atk_debugfs_gitm_get
,
665 static int atk_acpi_print(char *buf
, size_t sz
, union acpi_object
*obj
)
670 case ACPI_TYPE_INTEGER
:
671 ret
= snprintf(buf
, sz
, "0x%08llx\n", obj
->integer
.value
);
673 case ACPI_TYPE_STRING
:
674 ret
= snprintf(buf
, sz
, "%s\n", obj
->string
.pointer
);
681 static void atk_pack_print(char *buf
, size_t sz
, union acpi_object
*pack
)
686 for (i
= 0; i
< pack
->package
.count
; i
++) {
687 union acpi_object
*obj
= &pack
->package
.elements
[i
];
689 ret
= atk_acpi_print(buf
, sz
, obj
);
697 static int atk_debugfs_ggrp_open(struct inode
*inode
, struct file
*file
)
699 struct atk_data
*data
= inode
->i_private
;
701 union acpi_object
*ret
;
705 if (!data
->enumerate_handle
)
707 if (!data
->debugfs
.id
)
710 cls
= (data
->debugfs
.id
& 0xff000000) >> 24;
711 ret
= atk_ggrp(data
, cls
);
715 for (i
= 0; i
< ret
->package
.count
; i
++) {
716 union acpi_object
*pack
= &ret
->package
.elements
[i
];
717 union acpi_object
*id
;
719 if (pack
->type
!= ACPI_TYPE_PACKAGE
)
721 if (!pack
->package
.count
)
723 id
= &pack
->package
.elements
[0];
724 if (id
->integer
.value
== data
->debugfs
.id
) {
725 /* Print the package */
726 buf
= kzalloc(512, GFP_KERNEL
);
731 atk_pack_print(buf
, 512, pack
);
740 file
->private_data
= buf
;
742 return nonseekable_open(inode
, file
);
745 static ssize_t
atk_debugfs_ggrp_read(struct file
*file
, char __user
*buf
,
746 size_t count
, loff_t
*pos
)
748 char *str
= file
->private_data
;
749 size_t len
= strlen(str
);
751 return simple_read_from_buffer(buf
, count
, pos
, str
, len
);
754 static int atk_debugfs_ggrp_release(struct inode
*inode
, struct file
*file
)
756 kfree(file
->private_data
);
760 static const struct file_operations atk_debugfs_ggrp_fops
= {
761 .read
= atk_debugfs_ggrp_read
,
762 .open
= atk_debugfs_ggrp_open
,
763 .release
= atk_debugfs_ggrp_release
,
766 static void atk_debugfs_init(struct atk_data
*data
)
771 data
->debugfs
.id
= 0;
773 d
= debugfs_create_dir("asus_atk0110", NULL
);
777 f
= debugfs_create_x32("id", S_IRUSR
| S_IWUSR
, d
, &data
->debugfs
.id
);
781 f
= debugfs_create_file("gitm", S_IRUSR
, d
, data
,
786 f
= debugfs_create_file("ggrp", S_IRUSR
, d
, data
,
787 &atk_debugfs_ggrp_fops
);
791 data
->debugfs
.root
= d
;
795 debugfs_remove_recursive(d
);
798 static void atk_debugfs_cleanup(struct atk_data
*data
)
800 debugfs_remove_recursive(data
->debugfs
.root
);
803 #else /* CONFIG_DEBUG_FS */
805 static void atk_debugfs_init(struct atk_data
*data
)
809 static void atk_debugfs_cleanup(struct atk_data
*data
)
814 static int atk_add_sensor(struct atk_data
*data
, union acpi_object
*obj
)
816 struct device
*dev
= &data
->acpi_dev
->dev
;
817 union acpi_object
*flags
;
818 union acpi_object
*name
;
819 union acpi_object
*limit1
;
820 union acpi_object
*limit2
;
821 union acpi_object
*enable
;
822 struct atk_sensor_data
*sensor
;
823 char const *base_name
;
824 char const *limit1_name
;
825 char const *limit2_name
;
831 if (obj
->type
!= ACPI_TYPE_PACKAGE
) {
833 dev_warn(dev
, "Unknown type for ACPI object: (%d)\n",
838 err
= validate_hwmon_pack(data
, obj
);
842 /* Ok, we have a valid hwmon package */
843 type
= atk_get_pack_member(data
, obj
, HWMON_PACK_FLAGS
)->integer
.value
847 case HWMON_TYPE_VOLT
:
851 num
= &data
->voltage_count
;
854 case HWMON_TYPE_TEMP
:
857 limit2_name
= "crit";
858 num
= &data
->temperature_count
;
865 num
= &data
->fan_count
;
869 dev_warn(dev
, "Unknown sensor type: %#llx\n", type
);
873 enable
= atk_get_pack_member(data
, obj
, HWMON_PACK_ENABLE
);
874 if (!enable
->integer
.value
)
875 /* sensor is disabled */
878 flags
= atk_get_pack_member(data
, obj
, HWMON_PACK_FLAGS
);
879 name
= atk_get_pack_member(data
, obj
, HWMON_PACK_NAME
);
880 limit1
= atk_get_pack_member(data
, obj
, HWMON_PACK_LIMIT1
);
881 limit2
= atk_get_pack_member(data
, obj
, HWMON_PACK_LIMIT2
);
883 sensor
= kzalloc(sizeof(*sensor
), GFP_KERNEL
);
887 sensor
->acpi_name
= kstrdup(name
->string
.pointer
, GFP_KERNEL
);
888 if (!sensor
->acpi_name
) {
893 INIT_LIST_HEAD(&sensor
->list
);
896 sensor
->id
= flags
->integer
.value
;
897 sensor
->limit1
= limit1
->integer
.value
;
898 if (data
->old_interface
)
899 sensor
->limit2
= limit2
->integer
.value
;
901 /* The upper limit is expressed as delta from lower limit */
902 sensor
->limit2
= sensor
->limit1
+ limit2
->integer
.value
;
904 snprintf(sensor
->input_attr_name
, ATTR_NAME_SIZE
,
905 "%s%d_input", base_name
, start
+ *num
);
906 atk_init_attribute(&sensor
->input_attr
,
907 sensor
->input_attr_name
,
910 snprintf(sensor
->label_attr_name
, ATTR_NAME_SIZE
,
911 "%s%d_label", base_name
, start
+ *num
);
912 atk_init_attribute(&sensor
->label_attr
,
913 sensor
->label_attr_name
,
916 snprintf(sensor
->limit1_attr_name
, ATTR_NAME_SIZE
,
917 "%s%d_%s", base_name
, start
+ *num
, limit1_name
);
918 atk_init_attribute(&sensor
->limit1_attr
,
919 sensor
->limit1_attr_name
,
922 snprintf(sensor
->limit2_attr_name
, ATTR_NAME_SIZE
,
923 "%s%d_%s", base_name
, start
+ *num
, limit2_name
);
924 atk_init_attribute(&sensor
->limit2_attr
,
925 sensor
->limit2_attr_name
,
928 list_add(&sensor
->list
, &data
->sensor_list
);
933 kfree(sensor
->acpi_name
);
938 static int atk_enumerate_old_hwmon(struct atk_data
*data
)
940 struct device
*dev
= &data
->acpi_dev
->dev
;
941 struct acpi_buffer buf
;
942 union acpi_object
*pack
;
948 buf
.length
= ACPI_ALLOCATE_BUFFER
;
949 status
= acpi_evaluate_object_typed(data
->atk_handle
,
950 METHOD_OLD_ENUM_VLT
, NULL
, &buf
, ACPI_TYPE_PACKAGE
);
951 if (status
!= AE_OK
) {
952 dev_warn(dev
, METHOD_OLD_ENUM_VLT
": ACPI exception: %s\n",
953 acpi_format_exception(status
));
959 for (i
= 1; i
< pack
->package
.count
; i
++) {
960 union acpi_object
*obj
= &pack
->package
.elements
[i
];
962 ret
= atk_add_sensor(data
, obj
);
966 ACPI_FREE(buf
.pointer
);
969 buf
.length
= ACPI_ALLOCATE_BUFFER
;
970 status
= acpi_evaluate_object_typed(data
->atk_handle
,
971 METHOD_OLD_ENUM_TMP
, NULL
, &buf
, ACPI_TYPE_PACKAGE
);
972 if (status
!= AE_OK
) {
973 dev_warn(dev
, METHOD_OLD_ENUM_TMP
": ACPI exception: %s\n",
974 acpi_format_exception(status
));
981 for (i
= 1; i
< pack
->package
.count
; i
++) {
982 union acpi_object
*obj
= &pack
->package
.elements
[i
];
984 ret
= atk_add_sensor(data
, obj
);
988 ACPI_FREE(buf
.pointer
);
991 buf
.length
= ACPI_ALLOCATE_BUFFER
;
992 status
= acpi_evaluate_object_typed(data
->atk_handle
,
993 METHOD_OLD_ENUM_FAN
, NULL
, &buf
, ACPI_TYPE_PACKAGE
);
994 if (status
!= AE_OK
) {
995 dev_warn(dev
, METHOD_OLD_ENUM_FAN
": ACPI exception: %s\n",
996 acpi_format_exception(status
));
1003 for (i
= 1; i
< pack
->package
.count
; i
++) {
1004 union acpi_object
*obj
= &pack
->package
.elements
[i
];
1006 ret
= atk_add_sensor(data
, obj
);
1010 ACPI_FREE(buf
.pointer
);
1014 atk_free_sensors(data
);
1018 static int atk_ec_present(struct atk_data
*data
)
1020 struct device
*dev
= &data
->acpi_dev
->dev
;
1021 union acpi_object
*pack
;
1022 union acpi_object
*ec
;
1026 pack
= atk_ggrp(data
, ATK_MUX_MGMT
);
1028 if (PTR_ERR(pack
) == -ENOENT
) {
1029 /* The MGMT class does not exists - that's ok */
1030 dev_dbg(dev
, "Class %#llx not found\n", ATK_MUX_MGMT
);
1033 return PTR_ERR(pack
);
1038 for (i
= 0; i
< pack
->package
.count
; i
++) {
1039 union acpi_object
*obj
= &pack
->package
.elements
[i
];
1040 union acpi_object
*id
;
1042 if (obj
->type
!= ACPI_TYPE_PACKAGE
)
1045 id
= &obj
->package
.elements
[0];
1046 if (id
->type
!= ACPI_TYPE_INTEGER
)
1049 if (id
->integer
.value
== ATK_EC_ID
) {
1057 /* The system has no EC */
1058 dev_dbg(dev
, "EC not found\n");
1064 static int atk_ec_enabled(struct atk_data
*data
)
1066 struct device
*dev
= &data
->acpi_dev
->dev
;
1067 union acpi_object
*obj
;
1068 struct atk_acpi_ret_buffer
*buf
;
1071 obj
= atk_gitm(data
, ATK_EC_ID
);
1073 dev_err(dev
, "Unable to query EC status\n");
1074 return PTR_ERR(obj
);
1076 buf
= (struct atk_acpi_ret_buffer
*)obj
->buffer
.pointer
;
1078 if (buf
->flags
== 0) {
1079 dev_err(dev
, "Unable to query EC status\n");
1082 err
= (buf
->value
!= 0);
1083 dev_dbg(dev
, "EC is %sabled\n",
1084 err
? "en" : "dis");
1091 static int atk_ec_ctl(struct atk_data
*data
, int enable
)
1093 struct device
*dev
= &data
->acpi_dev
->dev
;
1094 union acpi_object
*obj
;
1095 struct atk_acpi_input_buf sitm
;
1096 struct atk_acpi_ret_buffer
*ec_ret
;
1099 sitm
.id
= ATK_EC_ID
;
1100 sitm
.param1
= enable
;
1103 obj
= atk_sitm(data
, &sitm
);
1105 dev_err(dev
, "Failed to %sable the EC\n",
1106 enable
? "en" : "dis");
1107 return PTR_ERR(obj
);
1109 ec_ret
= (struct atk_acpi_ret_buffer
*)obj
->buffer
.pointer
;
1110 if (ec_ret
->flags
== 0) {
1111 dev_err(dev
, "Failed to %sable the EC\n",
1112 enable
? "en" : "dis");
1115 dev_info(dev
, "EC %sabled\n",
1116 enable
? "en" : "dis");
1123 static int atk_enumerate_new_hwmon(struct atk_data
*data
)
1125 struct device
*dev
= &data
->acpi_dev
->dev
;
1126 union acpi_object
*pack
;
1130 err
= atk_ec_present(data
);
1134 err
= atk_ec_enabled(data
);
1137 /* If the EC was disabled we will disable it again on unload */
1138 data
->disable_ec
= err
;
1140 err
= atk_ec_ctl(data
, 1);
1142 data
->disable_ec
= false;
1147 dev_dbg(dev
, "Enumerating hwmon sensors\n");
1149 pack
= atk_ggrp(data
, ATK_MUX_HWMON
);
1151 return PTR_ERR(pack
);
1153 for (i
= 0; i
< pack
->package
.count
; i
++) {
1154 union acpi_object
*obj
= &pack
->package
.elements
[i
];
1156 atk_add_sensor(data
, obj
);
1159 err
= data
->voltage_count
+ data
->temperature_count
+ data
->fan_count
;
1165 static int atk_create_files(struct atk_data
*data
)
1167 struct atk_sensor_data
*s
;
1170 list_for_each_entry(s
, &data
->sensor_list
, list
) {
1171 err
= device_create_file(data
->hwmon_dev
, &s
->input_attr
);
1174 err
= device_create_file(data
->hwmon_dev
, &s
->label_attr
);
1177 err
= device_create_file(data
->hwmon_dev
, &s
->limit1_attr
);
1180 err
= device_create_file(data
->hwmon_dev
, &s
->limit2_attr
);
1185 err
= device_create_file(data
->hwmon_dev
, &atk_name_attr
);
1190 static void atk_remove_files(struct atk_data
*data
)
1192 struct atk_sensor_data
*s
;
1194 list_for_each_entry(s
, &data
->sensor_list
, list
) {
1195 device_remove_file(data
->hwmon_dev
, &s
->input_attr
);
1196 device_remove_file(data
->hwmon_dev
, &s
->label_attr
);
1197 device_remove_file(data
->hwmon_dev
, &s
->limit1_attr
);
1198 device_remove_file(data
->hwmon_dev
, &s
->limit2_attr
);
1200 device_remove_file(data
->hwmon_dev
, &atk_name_attr
);
1203 static void atk_free_sensors(struct atk_data
*data
)
1205 struct list_head
*head
= &data
->sensor_list
;
1206 struct atk_sensor_data
*s
, *tmp
;
1208 list_for_each_entry_safe(s
, tmp
, head
, list
) {
1209 kfree(s
->acpi_name
);
1214 static int atk_register_hwmon(struct atk_data
*data
)
1216 struct device
*dev
= &data
->acpi_dev
->dev
;
1219 dev_dbg(dev
, "registering hwmon device\n");
1220 data
->hwmon_dev
= hwmon_device_register(dev
);
1221 if (IS_ERR(data
->hwmon_dev
))
1222 return PTR_ERR(data
->hwmon_dev
);
1224 dev_dbg(dev
, "populating sysfs directory\n");
1225 err
= atk_create_files(data
);
1231 /* Cleanup the registered files */
1232 atk_remove_files(data
);
1233 hwmon_device_unregister(data
->hwmon_dev
);
1237 static int atk_probe_if(struct atk_data
*data
)
1239 struct device
*dev
= &data
->acpi_dev
->dev
;
1244 /* RTMP: read temperature */
1245 status
= acpi_get_handle(data
->atk_handle
, METHOD_OLD_READ_TMP
, &ret
);
1246 if (ACPI_SUCCESS(status
))
1247 data
->rtmp_handle
= ret
;
1249 dev_dbg(dev
, "method " METHOD_OLD_READ_TMP
" not found: %s\n",
1250 acpi_format_exception(status
));
1252 /* RVLT: read voltage */
1253 status
= acpi_get_handle(data
->atk_handle
, METHOD_OLD_READ_VLT
, &ret
);
1254 if (ACPI_SUCCESS(status
))
1255 data
->rvlt_handle
= ret
;
1257 dev_dbg(dev
, "method " METHOD_OLD_READ_VLT
" not found: %s\n",
1258 acpi_format_exception(status
));
1260 /* RFAN: read fan status */
1261 status
= acpi_get_handle(data
->atk_handle
, METHOD_OLD_READ_FAN
, &ret
);
1262 if (ACPI_SUCCESS(status
))
1263 data
->rfan_handle
= ret
;
1265 dev_dbg(dev
, "method " METHOD_OLD_READ_FAN
" not found: %s\n",
1266 acpi_format_exception(status
));
1269 status
= acpi_get_handle(data
->atk_handle
, METHOD_ENUMERATE
, &ret
);
1270 if (ACPI_SUCCESS(status
))
1271 data
->enumerate_handle
= ret
;
1273 dev_dbg(dev
, "method " METHOD_ENUMERATE
" not found: %s\n",
1274 acpi_format_exception(status
));
1276 /* De-multiplexer (read) */
1277 status
= acpi_get_handle(data
->atk_handle
, METHOD_READ
, &ret
);
1278 if (ACPI_SUCCESS(status
))
1279 data
->read_handle
= ret
;
1281 dev_dbg(dev
, "method " METHOD_READ
" not found: %s\n",
1282 acpi_format_exception(status
));
1284 /* De-multiplexer (write) */
1285 status
= acpi_get_handle(data
->atk_handle
, METHOD_WRITE
, &ret
);
1286 if (ACPI_SUCCESS(status
))
1287 data
->write_handle
= ret
;
1289 dev_dbg(dev
, "method " METHOD_WRITE
" not found: %s\n",
1290 acpi_format_exception(status
));
1292 /* Check for hwmon methods: first check "old" style methods; note that
1293 * both may be present: in this case we stick to the old interface;
1294 * analysis of multiple DSDTs indicates that when both interfaces
1295 * are present the new one (GGRP/GITM) is not functional.
1297 if (data
->rtmp_handle
&& data
->rvlt_handle
&& data
->rfan_handle
)
1298 data
->old_interface
= true;
1299 else if (data
->enumerate_handle
&& data
->read_handle
&&
1301 data
->old_interface
= false;
1308 static int atk_add(struct acpi_device
*device
)
1312 struct acpi_buffer buf
;
1313 union acpi_object
*obj
;
1314 struct atk_data
*data
;
1316 dev_dbg(&device
->dev
, "adding...\n");
1318 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
1322 data
->acpi_dev
= device
;
1323 data
->atk_handle
= device
->handle
;
1324 INIT_LIST_HEAD(&data
->sensor_list
);
1325 data
->disable_ec
= false;
1327 buf
.length
= ACPI_ALLOCATE_BUFFER
;
1328 ret
= acpi_evaluate_object_typed(data
->atk_handle
, BOARD_ID
, NULL
,
1329 &buf
, ACPI_TYPE_PACKAGE
);
1331 dev_dbg(&device
->dev
, "atk: method MBIF not found\n");
1334 if (obj
->package
.count
>= 2) {
1335 union acpi_object
*id
= &obj
->package
.elements
[1];
1336 if (id
->type
== ACPI_TYPE_STRING
)
1337 dev_dbg(&device
->dev
, "board ID = %s\n",
1338 id
->string
.pointer
);
1340 ACPI_FREE(buf
.pointer
);
1343 err
= atk_probe_if(data
);
1345 dev_err(&device
->dev
, "No usable hwmon interface detected\n");
1349 if (data
->old_interface
) {
1350 dev_dbg(&device
->dev
, "Using old hwmon interface\n");
1351 err
= atk_enumerate_old_hwmon(data
);
1353 dev_dbg(&device
->dev
, "Using new hwmon interface\n");
1354 err
= atk_enumerate_new_hwmon(data
);
1359 dev_info(&device
->dev
,
1360 "No usable sensor detected, bailing out\n");
1365 err
= atk_register_hwmon(data
);
1369 atk_debugfs_init(data
);
1371 device
->driver_data
= data
;
1374 atk_free_sensors(data
);
1376 if (data
->disable_ec
)
1377 atk_ec_ctl(data
, 0);
1382 static int atk_remove(struct acpi_device
*device
, int type
)
1384 struct atk_data
*data
= device
->driver_data
;
1385 dev_dbg(&device
->dev
, "removing...\n");
1387 device
->driver_data
= NULL
;
1389 atk_debugfs_cleanup(data
);
1391 atk_remove_files(data
);
1392 atk_free_sensors(data
);
1393 hwmon_device_unregister(data
->hwmon_dev
);
1395 if (data
->disable_ec
) {
1396 if (atk_ec_ctl(data
, 0))
1397 dev_err(&device
->dev
, "Failed to disable EC\n");
1405 static int __init
atk0110_init(void)
1409 ret
= acpi_bus_register_driver(&atk_driver
);
1411 pr_info("atk: acpi_bus_register_driver failed: %d\n", ret
);
1416 static void __exit
atk0110_exit(void)
1418 acpi_bus_unregister_driver(&atk_driver
);
1421 module_init(atk0110_init
);
1422 module_exit(atk0110_exit
);
1424 MODULE_LICENSE("GPL");