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/kernel.h>
9 #include <linux/hwmon.h>
10 #include <linux/list.h>
11 #include <linux/module.h>
13 #include <acpi/acpi.h>
14 #include <acpi/acpixf.h>
15 #include <acpi/acpi_drivers.h>
16 #include <acpi/acpi_bus.h>
19 #define ATK_HID "ATK0110"
21 /* Minimum time between readings, enforced in order to avoid
26 #define BOARD_ID "MBIF"
27 #define METHOD_ENUMERATE "GGRP"
28 #define METHOD_READ "GITM"
29 #define METHOD_WRITE "SITM"
30 #define METHOD_OLD_READ_TMP "RTMP"
31 #define METHOD_OLD_READ_VLT "RVLT"
32 #define METHOD_OLD_READ_FAN "RFAN"
33 #define METHOD_OLD_ENUM_TMP "TSIF"
34 #define METHOD_OLD_ENUM_VLT "VSIF"
35 #define METHOD_OLD_ENUM_FAN "FSIF"
37 #define ATK_MUX_HWMON 0x00000006ULL
38 #define ATK_MUX_MGMT 0x00000011ULL
40 #define ATK_CLASS_MASK 0xff000000ULL
41 #define ATK_CLASS_FREQ_CTL 0x03000000ULL
42 #define ATK_CLASS_FAN_CTL 0x04000000ULL
43 #define ATK_CLASS_HWMON 0x06000000ULL
44 #define ATK_CLASS_MGMT 0x11000000ULL
46 #define ATK_TYPE_MASK 0x00ff0000ULL
47 #define HWMON_TYPE_VOLT 0x00020000ULL
48 #define HWMON_TYPE_TEMP 0x00030000ULL
49 #define HWMON_TYPE_FAN 0x00040000ULL
51 #define ATK_ELEMENT_ID_MASK 0x0000ffffULL
53 #define ATK_EC_ID 0x11060004ULL
55 enum atk_pack_member
{
63 /* New package format */
64 #define _HWMON_NEW_PACK_SIZE 7
65 #define _HWMON_NEW_PACK_FLAGS 0
66 #define _HWMON_NEW_PACK_NAME 1
67 #define _HWMON_NEW_PACK_UNK1 2
68 #define _HWMON_NEW_PACK_UNK2 3
69 #define _HWMON_NEW_PACK_LIMIT1 4
70 #define _HWMON_NEW_PACK_LIMIT2 5
71 #define _HWMON_NEW_PACK_ENABLE 6
73 /* Old package format */
74 #define _HWMON_OLD_PACK_SIZE 5
75 #define _HWMON_OLD_PACK_FLAGS 0
76 #define _HWMON_OLD_PACK_NAME 1
77 #define _HWMON_OLD_PACK_LIMIT1 2
78 #define _HWMON_OLD_PACK_LIMIT2 3
79 #define _HWMON_OLD_PACK_ENABLE 4
83 struct device
*hwmon_dev
;
84 acpi_handle atk_handle
;
85 struct acpi_device
*acpi_dev
;
90 acpi_handle rtmp_handle
;
91 acpi_handle rvlt_handle
;
92 acpi_handle rfan_handle
;
94 acpi_handle enumerate_handle
;
95 acpi_handle read_handle
;
96 acpi_handle write_handle
;
101 int temperature_count
;
103 struct list_head sensor_list
;
107 typedef ssize_t (*sysfs_show_func
)(struct device
*dev
,
108 struct device_attribute
*attr
, char *buf
);
110 static const struct acpi_device_id atk_ids
[] = {
114 MODULE_DEVICE_TABLE(acpi
, atk_ids
);
116 #define ATTR_NAME_SIZE 16 /* Worst case is "tempN_input" */
118 struct atk_sensor_data
{
119 struct list_head list
;
120 struct atk_data
*data
;
121 struct device_attribute label_attr
;
122 struct device_attribute input_attr
;
123 struct device_attribute limit1_attr
;
124 struct device_attribute limit2_attr
;
125 char label_attr_name
[ATTR_NAME_SIZE
];
126 char input_attr_name
[ATTR_NAME_SIZE
];
127 char limit1_attr_name
[ATTR_NAME_SIZE
];
128 char limit2_attr_name
[ATTR_NAME_SIZE
];
134 unsigned long last_updated
; /* in jiffies */
136 char const *acpi_name
;
139 /* Return buffer format:
140 * [0-3] "value" is valid flag
142 * [8- ] unknown stuff on newer mobos
144 struct atk_acpi_ret_buffer
{
150 /* Input buffer used for GITM and SITM methods */
151 struct atk_acpi_input_buf
{
157 static int atk_add(struct acpi_device
*device
);
158 static int atk_remove(struct acpi_device
*device
, int type
);
159 static void atk_print_sensor(struct atk_data
*data
, union acpi_object
*obj
);
160 static int atk_read_value(struct atk_sensor_data
*sensor
, u64
*value
);
161 static void atk_free_sensors(struct atk_data
*data
);
163 static struct acpi_driver atk_driver
= {
169 .remove
= atk_remove
,
173 #define input_to_atk_sensor(attr) \
174 container_of(attr, struct atk_sensor_data, input_attr)
176 #define label_to_atk_sensor(attr) \
177 container_of(attr, struct atk_sensor_data, label_attr)
179 #define limit1_to_atk_sensor(attr) \
180 container_of(attr, struct atk_sensor_data, limit1_attr)
182 #define limit2_to_atk_sensor(attr) \
183 container_of(attr, struct atk_sensor_data, limit2_attr)
185 static ssize_t
atk_input_show(struct device
*dev
,
186 struct device_attribute
*attr
, char *buf
)
188 struct atk_sensor_data
*s
= input_to_atk_sensor(attr
);
192 err
= atk_read_value(s
, &value
);
196 if (s
->type
== HWMON_TYPE_TEMP
)
197 /* ACPI returns decidegree */
200 return sprintf(buf
, "%llu\n", value
);
203 static ssize_t
atk_label_show(struct device
*dev
,
204 struct device_attribute
*attr
, char *buf
)
206 struct atk_sensor_data
*s
= label_to_atk_sensor(attr
);
208 return sprintf(buf
, "%s\n", s
->acpi_name
);
211 static ssize_t
atk_limit1_show(struct device
*dev
,
212 struct device_attribute
*attr
, char *buf
)
214 struct atk_sensor_data
*s
= limit1_to_atk_sensor(attr
);
215 u64 value
= s
->limit1
;
217 if (s
->type
== HWMON_TYPE_TEMP
)
220 return sprintf(buf
, "%lld\n", value
);
223 static ssize_t
atk_limit2_show(struct device
*dev
,
224 struct device_attribute
*attr
, char *buf
)
226 struct atk_sensor_data
*s
= limit2_to_atk_sensor(attr
);
227 u64 value
= s
->limit2
;
229 if (s
->type
== HWMON_TYPE_TEMP
)
232 return sprintf(buf
, "%lld\n", value
);
235 static ssize_t
atk_name_show(struct device
*dev
,
236 struct device_attribute
*attr
, char *buf
)
238 return sprintf(buf
, "atk0110\n");
240 static struct device_attribute atk_name_attr
=
241 __ATTR(name
, 0444, atk_name_show
, NULL
);
243 static void atk_init_attribute(struct device_attribute
*attr
, char *name
,
244 sysfs_show_func show
)
246 attr
->attr
.name
= name
;
247 attr
->attr
.mode
= 0444;
253 static union acpi_object
*atk_get_pack_member(struct atk_data
*data
,
254 union acpi_object
*pack
,
255 enum atk_pack_member m
)
257 bool old_if
= data
->old_interface
;
261 case HWMON_PACK_FLAGS
:
262 offset
= old_if
? _HWMON_OLD_PACK_FLAGS
: _HWMON_NEW_PACK_FLAGS
;
264 case HWMON_PACK_NAME
:
265 offset
= old_if
? _HWMON_OLD_PACK_NAME
: _HWMON_NEW_PACK_NAME
;
267 case HWMON_PACK_LIMIT1
:
268 offset
= old_if
? _HWMON_OLD_PACK_LIMIT1
:
269 _HWMON_NEW_PACK_LIMIT1
;
271 case HWMON_PACK_LIMIT2
:
272 offset
= old_if
? _HWMON_OLD_PACK_LIMIT2
:
273 _HWMON_NEW_PACK_LIMIT2
;
275 case HWMON_PACK_ENABLE
:
276 offset
= old_if
? _HWMON_OLD_PACK_ENABLE
:
277 _HWMON_NEW_PACK_ENABLE
;
283 return &pack
->package
.elements
[offset
];
287 /* New package format is:
289 * class - used for de-muxing the request to the correct GITn
290 * type (volt, temp, fan)
292 * sensor id - used for de-muxing the request _inside_ the GITn
300 * The old package has the same format but it's missing the two unknown fields.
302 static int validate_hwmon_pack(struct atk_data
*data
, union acpi_object
*obj
)
304 struct device
*dev
= &data
->acpi_dev
->dev
;
305 union acpi_object
*tmp
;
306 bool old_if
= data
->old_interface
;
307 int const expected_size
= old_if
? _HWMON_OLD_PACK_SIZE
:
308 _HWMON_NEW_PACK_SIZE
;
310 if (obj
->type
!= ACPI_TYPE_PACKAGE
) {
311 dev_warn(dev
, "Invalid type: %d\n", obj
->type
);
315 if (obj
->package
.count
!= expected_size
) {
316 dev_warn(dev
, "Invalid package size: %d, expected: %d\n",
317 obj
->package
.count
, expected_size
);
321 tmp
= atk_get_pack_member(data
, obj
, HWMON_PACK_FLAGS
);
322 if (tmp
->type
!= ACPI_TYPE_INTEGER
) {
323 dev_warn(dev
, "Invalid type (flag): %d\n", tmp
->type
);
327 tmp
= atk_get_pack_member(data
, obj
, HWMON_PACK_NAME
);
328 if (tmp
->type
!= ACPI_TYPE_STRING
) {
329 dev_warn(dev
, "Invalid type (name): %d\n", tmp
->type
);
333 /* Don't check... we don't know what they're useful for anyway */
335 tmp
= &obj
->package
.elements
[HWMON_PACK_UNK1
];
336 if (tmp
->type
!= ACPI_TYPE_INTEGER
) {
337 dev_warn(dev
, "Invalid type (unk1): %d\n", tmp
->type
);
341 tmp
= &obj
->package
.elements
[HWMON_PACK_UNK2
];
342 if (tmp
->type
!= ACPI_TYPE_INTEGER
) {
343 dev_warn(dev
, "Invalid type (unk2): %d\n", tmp
->type
);
348 tmp
= atk_get_pack_member(data
, obj
, HWMON_PACK_LIMIT1
);
349 if (tmp
->type
!= ACPI_TYPE_INTEGER
) {
350 dev_warn(dev
, "Invalid type (limit1): %d\n", tmp
->type
);
354 tmp
= atk_get_pack_member(data
, obj
, HWMON_PACK_LIMIT2
);
355 if (tmp
->type
!= ACPI_TYPE_INTEGER
) {
356 dev_warn(dev
, "Invalid type (limit2): %d\n", tmp
->type
);
360 tmp
= atk_get_pack_member(data
, obj
, HWMON_PACK_ENABLE
);
361 if (tmp
->type
!= ACPI_TYPE_INTEGER
) {
362 dev_warn(dev
, "Invalid type (enable): %d\n", tmp
->type
);
366 atk_print_sensor(data
, obj
);
372 static char const *atk_sensor_type(union acpi_object
*flags
)
374 u64 type
= flags
->integer
.value
& ATK_TYPE_MASK
;
378 case HWMON_TYPE_VOLT
:
381 case HWMON_TYPE_TEMP
:
382 what
= "temperature";
396 static void atk_print_sensor(struct atk_data
*data
, union acpi_object
*obj
)
399 struct device
*dev
= &data
->acpi_dev
->dev
;
400 union acpi_object
*flags
;
401 union acpi_object
*name
;
402 union acpi_object
*limit1
;
403 union acpi_object
*limit2
;
404 union acpi_object
*enable
;
407 flags
= atk_get_pack_member(data
, obj
, HWMON_PACK_FLAGS
);
408 name
= atk_get_pack_member(data
, obj
, HWMON_PACK_NAME
);
409 limit1
= atk_get_pack_member(data
, obj
, HWMON_PACK_LIMIT1
);
410 limit2
= atk_get_pack_member(data
, obj
, HWMON_PACK_LIMIT2
);
411 enable
= atk_get_pack_member(data
, obj
, HWMON_PACK_ENABLE
);
413 what
= atk_sensor_type(flags
);
415 dev_dbg(dev
, "%s: %#llx %s [%llu-%llu] %s\n", what
,
416 flags
->integer
.value
,
417 name
->string
.pointer
,
418 limit1
->integer
.value
, limit2
->integer
.value
,
419 enable
->integer
.value
? "enabled" : "disabled");
423 static int atk_read_value_old(struct atk_sensor_data
*sensor
, u64
*value
)
425 struct atk_data
*data
= sensor
->data
;
426 struct device
*dev
= &data
->acpi_dev
->dev
;
427 struct acpi_object_list params
;
428 union acpi_object id
;
432 switch (sensor
->type
) {
433 case HWMON_TYPE_VOLT
:
434 method
= data
->rvlt_handle
;
436 case HWMON_TYPE_TEMP
:
437 method
= data
->rtmp_handle
;
440 method
= data
->rfan_handle
;
446 id
.type
= ACPI_TYPE_INTEGER
;
447 id
.integer
.value
= sensor
->id
;
450 params
.pointer
= &id
;
452 status
= acpi_evaluate_integer(method
, NULL
, ¶ms
, value
);
453 if (status
!= AE_OK
) {
454 dev_warn(dev
, "%s: ACPI exception: %s\n", __func__
,
455 acpi_format_exception(status
));
462 static union acpi_object
*atk_ggrp(struct atk_data
*data
, u16 mux
)
464 struct device
*dev
= &data
->acpi_dev
->dev
;
465 struct acpi_buffer buf
;
467 struct acpi_object_list params
;
468 union acpi_object id
;
469 union acpi_object
*pack
;
471 id
.type
= ACPI_TYPE_INTEGER
;
472 id
.integer
.value
= mux
;
474 params
.pointer
= &id
;
476 buf
.length
= ACPI_ALLOCATE_BUFFER
;
477 ret
= acpi_evaluate_object(data
->enumerate_handle
, NULL
, ¶ms
, &buf
);
479 dev_err(dev
, "GGRP[%#x] ACPI exception: %s\n", mux
,
480 acpi_format_exception(ret
));
481 return ERR_PTR(-EIO
);
484 if (pack
->type
!= ACPI_TYPE_PACKAGE
) {
485 /* Execution was successful, but the id was not found */
487 return ERR_PTR(-ENOENT
);
490 if (pack
->package
.count
< 1) {
491 dev_err(dev
, "GGRP[%#x] package is too small\n", mux
);
493 return ERR_PTR(-EIO
);
498 static union acpi_object
*atk_gitm(struct atk_data
*data
, u64 id
)
500 struct device
*dev
= &data
->acpi_dev
->dev
;
501 struct atk_acpi_input_buf buf
;
502 union acpi_object tmp
;
503 struct acpi_object_list params
;
504 struct acpi_buffer ret
;
505 union acpi_object
*obj
;
512 tmp
.type
= ACPI_TYPE_BUFFER
;
513 tmp
.buffer
.pointer
= (u8
*)&buf
;
514 tmp
.buffer
.length
= sizeof(buf
);
517 params
.pointer
= (void *)&tmp
;
519 ret
.length
= ACPI_ALLOCATE_BUFFER
;
520 status
= acpi_evaluate_object_typed(data
->read_handle
, NULL
, ¶ms
,
521 &ret
, ACPI_TYPE_BUFFER
);
522 if (status
!= AE_OK
) {
523 dev_warn(dev
, "GITM[%#llx] ACPI exception: %s\n", id
,
524 acpi_format_exception(status
));
525 return ERR_PTR(-EIO
);
530 if (obj
->buffer
.length
< 8) {
531 dev_warn(dev
, "Unexpected ASBF length: %u\n",
534 return ERR_PTR(-EIO
);
539 static union acpi_object
*atk_sitm(struct atk_data
*data
,
540 struct atk_acpi_input_buf
*buf
)
542 struct device
*dev
= &data
->acpi_dev
->dev
;
543 struct acpi_object_list params
;
544 union acpi_object tmp
;
545 struct acpi_buffer ret
;
546 union acpi_object
*obj
;
549 tmp
.type
= ACPI_TYPE_BUFFER
;
550 tmp
.buffer
.pointer
= (u8
*)buf
;
551 tmp
.buffer
.length
= sizeof(*buf
);
554 params
.pointer
= &tmp
;
556 ret
.length
= ACPI_ALLOCATE_BUFFER
;
557 status
= acpi_evaluate_object_typed(data
->write_handle
, NULL
, ¶ms
,
558 &ret
, ACPI_TYPE_BUFFER
);
559 if (status
!= AE_OK
) {
560 dev_warn(dev
, "SITM[%#x] ACPI exception: %s\n", buf
->id
,
561 acpi_format_exception(status
));
562 return ERR_PTR(-EIO
);
567 if (obj
->buffer
.length
< 8) {
568 dev_warn(dev
, "Unexpected ASBF length: %u\n",
571 return ERR_PTR(-EIO
);
576 static int atk_read_value_new(struct atk_sensor_data
*sensor
, u64
*value
)
578 struct atk_data
*data
= sensor
->data
;
579 struct device
*dev
= &data
->acpi_dev
->dev
;
580 union acpi_object
*obj
;
581 struct atk_acpi_ret_buffer
*buf
;
584 obj
= atk_gitm(data
, sensor
->id
);
588 buf
= (struct atk_acpi_ret_buffer
*)obj
->buffer
.pointer
;
589 if (buf
->flags
== 0) {
590 /* The reading is not valid, possible causes:
592 * - enumeration was FUBAR (and we didn't notice)
594 dev_warn(dev
, "Read failed, sensor = %#llx\n", sensor
->id
);
605 static int atk_read_value(struct atk_sensor_data
*sensor
, u64
*value
)
609 if (!sensor
->is_valid
||
610 time_after(jiffies
, sensor
->last_updated
+ CACHE_TIME
)) {
611 if (sensor
->data
->old_interface
)
612 err
= atk_read_value_old(sensor
, value
);
614 err
= atk_read_value_new(sensor
, value
);
616 sensor
->is_valid
= true;
617 sensor
->last_updated
= jiffies
;
618 sensor
->cached_value
= *value
;
620 *value
= sensor
->cached_value
;
627 static int atk_add_sensor(struct atk_data
*data
, union acpi_object
*obj
)
629 struct device
*dev
= &data
->acpi_dev
->dev
;
630 union acpi_object
*flags
;
631 union acpi_object
*name
;
632 union acpi_object
*limit1
;
633 union acpi_object
*limit2
;
634 union acpi_object
*enable
;
635 struct atk_sensor_data
*sensor
;
636 char const *base_name
;
637 char const *limit1_name
;
638 char const *limit2_name
;
644 if (obj
->type
!= ACPI_TYPE_PACKAGE
) {
646 dev_warn(dev
, "Unknown type for ACPI object: (%d)\n",
651 err
= validate_hwmon_pack(data
, obj
);
655 /* Ok, we have a valid hwmon package */
656 type
= atk_get_pack_member(data
, obj
, HWMON_PACK_FLAGS
)->integer
.value
660 case HWMON_TYPE_VOLT
:
664 num
= &data
->voltage_count
;
667 case HWMON_TYPE_TEMP
:
670 limit2_name
= "crit";
671 num
= &data
->temperature_count
;
678 num
= &data
->fan_count
;
682 dev_warn(dev
, "Unknown sensor type: %#llx\n", type
);
686 enable
= atk_get_pack_member(data
, obj
, HWMON_PACK_ENABLE
);
687 if (!enable
->integer
.value
)
688 /* sensor is disabled */
691 flags
= atk_get_pack_member(data
, obj
, HWMON_PACK_FLAGS
);
692 name
= atk_get_pack_member(data
, obj
, HWMON_PACK_NAME
);
693 limit1
= atk_get_pack_member(data
, obj
, HWMON_PACK_LIMIT1
);
694 limit2
= atk_get_pack_member(data
, obj
, HWMON_PACK_LIMIT2
);
696 sensor
= kzalloc(sizeof(*sensor
), GFP_KERNEL
);
700 sensor
->acpi_name
= kstrdup(name
->string
.pointer
, GFP_KERNEL
);
701 if (!sensor
->acpi_name
) {
706 INIT_LIST_HEAD(&sensor
->list
);
709 sensor
->id
= flags
->integer
.value
;
710 sensor
->limit1
= limit1
->integer
.value
;
711 if (data
->old_interface
)
712 sensor
->limit2
= limit2
->integer
.value
;
714 /* The upper limit is expressed as delta from lower limit */
715 sensor
->limit2
= sensor
->limit1
+ limit2
->integer
.value
;
717 snprintf(sensor
->input_attr_name
, ATTR_NAME_SIZE
,
718 "%s%d_input", base_name
, start
+ *num
);
719 atk_init_attribute(&sensor
->input_attr
,
720 sensor
->input_attr_name
,
723 snprintf(sensor
->label_attr_name
, ATTR_NAME_SIZE
,
724 "%s%d_label", base_name
, start
+ *num
);
725 atk_init_attribute(&sensor
->label_attr
,
726 sensor
->label_attr_name
,
729 snprintf(sensor
->limit1_attr_name
, ATTR_NAME_SIZE
,
730 "%s%d_%s", base_name
, start
+ *num
, limit1_name
);
731 atk_init_attribute(&sensor
->limit1_attr
,
732 sensor
->limit1_attr_name
,
735 snprintf(sensor
->limit2_attr_name
, ATTR_NAME_SIZE
,
736 "%s%d_%s", base_name
, start
+ *num
, limit2_name
);
737 atk_init_attribute(&sensor
->limit2_attr
,
738 sensor
->limit2_attr_name
,
741 list_add(&sensor
->list
, &data
->sensor_list
);
746 kfree(sensor
->acpi_name
);
751 static int atk_enumerate_old_hwmon(struct atk_data
*data
)
753 struct device
*dev
= &data
->acpi_dev
->dev
;
754 struct acpi_buffer buf
;
755 union acpi_object
*pack
;
761 buf
.length
= ACPI_ALLOCATE_BUFFER
;
762 status
= acpi_evaluate_object_typed(data
->atk_handle
,
763 METHOD_OLD_ENUM_VLT
, NULL
, &buf
, ACPI_TYPE_PACKAGE
);
764 if (status
!= AE_OK
) {
765 dev_warn(dev
, METHOD_OLD_ENUM_VLT
": ACPI exception: %s\n",
766 acpi_format_exception(status
));
772 for (i
= 1; i
< pack
->package
.count
; i
++) {
773 union acpi_object
*obj
= &pack
->package
.elements
[i
];
775 ret
= atk_add_sensor(data
, obj
);
779 ACPI_FREE(buf
.pointer
);
782 buf
.length
= ACPI_ALLOCATE_BUFFER
;
783 status
= acpi_evaluate_object_typed(data
->atk_handle
,
784 METHOD_OLD_ENUM_TMP
, NULL
, &buf
, ACPI_TYPE_PACKAGE
);
785 if (status
!= AE_OK
) {
786 dev_warn(dev
, METHOD_OLD_ENUM_TMP
": ACPI exception: %s\n",
787 acpi_format_exception(status
));
794 for (i
= 1; i
< pack
->package
.count
; i
++) {
795 union acpi_object
*obj
= &pack
->package
.elements
[i
];
797 ret
= atk_add_sensor(data
, obj
);
801 ACPI_FREE(buf
.pointer
);
804 buf
.length
= ACPI_ALLOCATE_BUFFER
;
805 status
= acpi_evaluate_object_typed(data
->atk_handle
,
806 METHOD_OLD_ENUM_FAN
, NULL
, &buf
, ACPI_TYPE_PACKAGE
);
807 if (status
!= AE_OK
) {
808 dev_warn(dev
, METHOD_OLD_ENUM_FAN
": ACPI exception: %s\n",
809 acpi_format_exception(status
));
816 for (i
= 1; i
< pack
->package
.count
; i
++) {
817 union acpi_object
*obj
= &pack
->package
.elements
[i
];
819 ret
= atk_add_sensor(data
, obj
);
823 ACPI_FREE(buf
.pointer
);
827 atk_free_sensors(data
);
831 static int atk_ec_present(struct atk_data
*data
)
833 struct device
*dev
= &data
->acpi_dev
->dev
;
834 union acpi_object
*pack
;
835 union acpi_object
*ec
;
839 pack
= atk_ggrp(data
, ATK_MUX_MGMT
);
841 if (PTR_ERR(pack
) == -ENOENT
) {
842 /* The MGMT class does not exists - that's ok */
843 dev_dbg(dev
, "Class %#llx not found\n", ATK_MUX_MGMT
);
846 return PTR_ERR(pack
);
851 for (i
= 0; i
< pack
->package
.count
; i
++) {
852 union acpi_object
*obj
= &pack
->package
.elements
[i
];
853 union acpi_object
*id
;
855 if (obj
->type
!= ACPI_TYPE_PACKAGE
)
858 id
= &obj
->package
.elements
[0];
859 if (id
->type
!= ACPI_TYPE_INTEGER
)
862 if (id
->integer
.value
== ATK_EC_ID
) {
870 /* The system has no EC */
871 dev_dbg(dev
, "EC not found\n");
877 static int atk_ec_enabled(struct atk_data
*data
)
879 struct device
*dev
= &data
->acpi_dev
->dev
;
880 union acpi_object
*obj
;
881 struct atk_acpi_ret_buffer
*buf
;
884 obj
= atk_gitm(data
, ATK_EC_ID
);
886 dev_err(dev
, "Unable to query EC status\n");
889 buf
= (struct atk_acpi_ret_buffer
*)obj
->buffer
.pointer
;
891 if (buf
->flags
== 0) {
892 dev_err(dev
, "Unable to query EC status\n");
895 err
= (buf
->value
!= 0);
896 dev_dbg(dev
, "EC is %sabled\n",
904 static int atk_ec_ctl(struct atk_data
*data
, int enable
)
906 struct device
*dev
= &data
->acpi_dev
->dev
;
907 union acpi_object
*obj
;
908 struct atk_acpi_input_buf sitm
;
909 struct atk_acpi_ret_buffer
*ec_ret
;
913 sitm
.param1
= enable
;
916 obj
= atk_sitm(data
, &sitm
);
918 dev_err(dev
, "Failed to %sable the EC\n",
919 enable
? "en" : "dis");
922 ec_ret
= (struct atk_acpi_ret_buffer
*)obj
->buffer
.pointer
;
923 if (ec_ret
->flags
== 0) {
924 dev_err(dev
, "Failed to %sable the EC\n",
925 enable
? "en" : "dis");
928 dev_info(dev
, "EC %sabled\n",
929 enable
? "en" : "dis");
936 static int atk_enumerate_new_hwmon(struct atk_data
*data
)
938 struct device
*dev
= &data
->acpi_dev
->dev
;
939 union acpi_object
*pack
;
943 err
= atk_ec_present(data
);
947 err
= atk_ec_enabled(data
);
950 /* If the EC was disabled we will disable it again on unload */
951 data
->disable_ec
= err
;
953 err
= atk_ec_ctl(data
, 1);
955 data
->disable_ec
= false;
960 dev_dbg(dev
, "Enumerating hwmon sensors\n");
962 pack
= atk_ggrp(data
, ATK_MUX_HWMON
);
964 return PTR_ERR(pack
);
966 for (i
= 0; i
< pack
->package
.count
; i
++) {
967 union acpi_object
*obj
= &pack
->package
.elements
[i
];
969 atk_add_sensor(data
, obj
);
972 err
= data
->voltage_count
+ data
->temperature_count
+ data
->fan_count
;
978 static int atk_create_files(struct atk_data
*data
)
980 struct atk_sensor_data
*s
;
983 list_for_each_entry(s
, &data
->sensor_list
, list
) {
984 err
= device_create_file(data
->hwmon_dev
, &s
->input_attr
);
987 err
= device_create_file(data
->hwmon_dev
, &s
->label_attr
);
990 err
= device_create_file(data
->hwmon_dev
, &s
->limit1_attr
);
993 err
= device_create_file(data
->hwmon_dev
, &s
->limit2_attr
);
998 err
= device_create_file(data
->hwmon_dev
, &atk_name_attr
);
1003 static void atk_remove_files(struct atk_data
*data
)
1005 struct atk_sensor_data
*s
;
1007 list_for_each_entry(s
, &data
->sensor_list
, list
) {
1008 device_remove_file(data
->hwmon_dev
, &s
->input_attr
);
1009 device_remove_file(data
->hwmon_dev
, &s
->label_attr
);
1010 device_remove_file(data
->hwmon_dev
, &s
->limit1_attr
);
1011 device_remove_file(data
->hwmon_dev
, &s
->limit2_attr
);
1013 device_remove_file(data
->hwmon_dev
, &atk_name_attr
);
1016 static void atk_free_sensors(struct atk_data
*data
)
1018 struct list_head
*head
= &data
->sensor_list
;
1019 struct atk_sensor_data
*s
, *tmp
;
1021 list_for_each_entry_safe(s
, tmp
, head
, list
) {
1022 kfree(s
->acpi_name
);
1027 static int atk_register_hwmon(struct atk_data
*data
)
1029 struct device
*dev
= &data
->acpi_dev
->dev
;
1032 dev_dbg(dev
, "registering hwmon device\n");
1033 data
->hwmon_dev
= hwmon_device_register(dev
);
1034 if (IS_ERR(data
->hwmon_dev
))
1035 return PTR_ERR(data
->hwmon_dev
);
1037 dev_dbg(dev
, "populating sysfs directory\n");
1038 err
= atk_create_files(data
);
1044 /* Cleanup the registered files */
1045 atk_remove_files(data
);
1046 hwmon_device_unregister(data
->hwmon_dev
);
1050 static int atk_check_old_if(struct atk_data
*data
)
1052 struct device
*dev
= &data
->acpi_dev
->dev
;
1056 /* RTMP: read temperature */
1057 status
= acpi_get_handle(data
->atk_handle
, METHOD_OLD_READ_TMP
, &ret
);
1058 if (status
!= AE_OK
) {
1059 dev_dbg(dev
, "method " METHOD_OLD_READ_TMP
" not found: %s\n",
1060 acpi_format_exception(status
));
1063 data
->rtmp_handle
= ret
;
1065 /* RVLT: read voltage */
1066 status
= acpi_get_handle(data
->atk_handle
, METHOD_OLD_READ_VLT
, &ret
);
1067 if (status
!= AE_OK
) {
1068 dev_dbg(dev
, "method " METHOD_OLD_READ_VLT
" not found: %s\n",
1069 acpi_format_exception(status
));
1072 data
->rvlt_handle
= ret
;
1074 /* RFAN: read fan status */
1075 status
= acpi_get_handle(data
->atk_handle
, METHOD_OLD_READ_FAN
, &ret
);
1076 if (status
!= AE_OK
) {
1077 dev_dbg(dev
, "method " METHOD_OLD_READ_FAN
" not found: %s\n",
1078 acpi_format_exception(status
));
1081 data
->rfan_handle
= ret
;
1086 static int atk_check_new_if(struct atk_data
*data
)
1088 struct device
*dev
= &data
->acpi_dev
->dev
;
1093 status
= acpi_get_handle(data
->atk_handle
, METHOD_ENUMERATE
, &ret
);
1094 if (status
!= AE_OK
) {
1095 dev_dbg(dev
, "method " METHOD_ENUMERATE
" not found: %s\n",
1096 acpi_format_exception(status
));
1099 data
->enumerate_handle
= ret
;
1101 /* De-multiplexer (read) */
1102 status
= acpi_get_handle(data
->atk_handle
, METHOD_READ
, &ret
);
1103 if (status
!= AE_OK
) {
1104 dev_dbg(dev
, "method " METHOD_READ
" not found: %s\n",
1105 acpi_format_exception(status
));
1108 data
->read_handle
= ret
;
1110 /* De-multiplexer (write) */
1111 status
= acpi_get_handle(data
->atk_handle
, METHOD_WRITE
, &ret
);
1112 if (status
!= AE_OK
) {
1113 dev_dbg(dev
, "method " METHOD_READ
" not found: %s\n",
1114 acpi_format_exception(status
));
1117 data
->write_handle
= ret
;
1122 static int atk_add(struct acpi_device
*device
)
1126 struct acpi_buffer buf
;
1127 union acpi_object
*obj
;
1128 struct atk_data
*data
;
1130 dev_dbg(&device
->dev
, "adding...\n");
1132 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
1136 data
->acpi_dev
= device
;
1137 data
->atk_handle
= device
->handle
;
1138 INIT_LIST_HEAD(&data
->sensor_list
);
1139 data
->disable_ec
= false;
1141 buf
.length
= ACPI_ALLOCATE_BUFFER
;
1142 ret
= acpi_evaluate_object_typed(data
->atk_handle
, BOARD_ID
, NULL
,
1143 &buf
, ACPI_TYPE_PACKAGE
);
1145 dev_dbg(&device
->dev
, "atk: method MBIF not found\n");
1151 if (obj
->package
.count
>= 2 &&
1152 obj
->package
.elements
[1].type
== ACPI_TYPE_STRING
) {
1153 dev_dbg(&device
->dev
, "board ID = %s\n",
1154 obj
->package
.elements
[1].string
.pointer
);
1156 ACPI_FREE(buf
.pointer
);
1158 /* Check for hwmon methods: first check "old" style methods; note that
1159 * both may be present: in this case we stick to the old interface;
1160 * analysis of multiple DSDTs indicates that when both interfaces
1161 * are present the new one (GGRP/GITM) is not functional.
1163 err
= atk_check_old_if(data
);
1165 dev_dbg(&device
->dev
, "Using old hwmon interface\n");
1166 data
->old_interface
= true;
1168 err
= atk_check_new_if(data
);
1172 dev_dbg(&device
->dev
, "Using new hwmon interface\n");
1173 data
->old_interface
= false;
1176 if (data
->old_interface
)
1177 err
= atk_enumerate_old_hwmon(data
);
1179 err
= atk_enumerate_new_hwmon(data
);
1183 dev_info(&device
->dev
,
1184 "No usable sensor detected, bailing out\n");
1189 err
= atk_register_hwmon(data
);
1193 device
->driver_data
= data
;
1196 atk_free_sensors(data
);
1198 if (data
->disable_ec
)
1199 atk_ec_ctl(data
, 0);
1204 static int atk_remove(struct acpi_device
*device
, int type
)
1206 struct atk_data
*data
= device
->driver_data
;
1207 dev_dbg(&device
->dev
, "removing...\n");
1209 device
->driver_data
= NULL
;
1211 atk_remove_files(data
);
1212 atk_free_sensors(data
);
1213 hwmon_device_unregister(data
->hwmon_dev
);
1215 if (data
->disable_ec
) {
1216 if (atk_ec_ctl(data
, 0))
1217 dev_err(&device
->dev
, "Failed to disable EC\n");
1225 static int __init
atk0110_init(void)
1229 ret
= acpi_bus_register_driver(&atk_driver
);
1231 pr_info("atk: acpi_bus_register_driver failed: %d\n", ret
);
1236 static void __exit
atk0110_exit(void)
1238 acpi_bus_unregister_driver(&atk_driver
);
1241 module_init(atk0110_init
);
1242 module_exit(atk0110_exit
);
1244 MODULE_LICENSE("GPL");