8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/kernel.h>
13 #include <linux/list.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
26 #define ATK_HID "ATK0110"
30 MODULE_PARM_DESC(new_if,
"Override detection heuristic and force the use of the new ATK0110 interface");
35 .ident =
"Asus Sabertooth X58",
41 .ident =
"Asus M5A78L",
55 #define BOARD_ID "MBIF"
56 #define METHOD_ENUMERATE "GGRP"
57 #define METHOD_READ "GITM"
58 #define METHOD_WRITE "SITM"
59 #define METHOD_OLD_READ_TMP "RTMP"
60 #define METHOD_OLD_READ_VLT "RVLT"
61 #define METHOD_OLD_READ_FAN "RFAN"
62 #define METHOD_OLD_ENUM_TMP "TSIF"
63 #define METHOD_OLD_ENUM_VLT "VSIF"
64 #define METHOD_OLD_ENUM_FAN "FSIF"
66 #define ATK_MUX_HWMON 0x00000006ULL
67 #define ATK_MUX_MGMT 0x00000011ULL
69 #define ATK_CLASS_MASK 0xff000000ULL
70 #define ATK_CLASS_FREQ_CTL 0x03000000ULL
71 #define ATK_CLASS_FAN_CTL 0x04000000ULL
72 #define ATK_CLASS_HWMON 0x06000000ULL
73 #define ATK_CLASS_MGMT 0x11000000ULL
75 #define ATK_TYPE_MASK 0x00ff0000ULL
76 #define HWMON_TYPE_VOLT 0x00020000ULL
77 #define HWMON_TYPE_TEMP 0x00030000ULL
78 #define HWMON_TYPE_FAN 0x00040000ULL
80 #define ATK_ELEMENT_ID_MASK 0x0000ffffULL
82 #define ATK_EC_ID 0x11060004ULL
93 #define _HWMON_NEW_PACK_SIZE 7
94 #define _HWMON_NEW_PACK_FLAGS 0
95 #define _HWMON_NEW_PACK_NAME 1
96 #define _HWMON_NEW_PACK_UNK1 2
97 #define _HWMON_NEW_PACK_UNK2 3
98 #define _HWMON_NEW_PACK_LIMIT1 4
99 #define _HWMON_NEW_PACK_LIMIT2 5
100 #define _HWMON_NEW_PACK_ENABLE 6
103 #define _HWMON_OLD_PACK_SIZE 5
104 #define _HWMON_OLD_PACK_FLAGS 0
105 #define _HWMON_OLD_PACK_NAME 1
106 #define _HWMON_OLD_PACK_LIMIT1 2
107 #define _HWMON_OLD_PACK_LIMIT2 3
108 #define _HWMON_OLD_PACK_ENABLE 4
150 #define ATTR_NAME_SIZE 16
192 static int atk_add(
struct acpi_device *
device);
193 static int atk_remove(
struct acpi_device *
device,
int type);
198 static struct acpi_driver atk_driver = {
204 .remove = atk_remove,
208 #define input_to_atk_sensor(attr) \
209 container_of(attr, struct atk_sensor_data, input_attr)
211 #define label_to_atk_sensor(attr) \
212 container_of(attr, struct atk_sensor_data, label_attr)
214 #define limit1_to_atk_sensor(attr) \
215 container_of(attr, struct atk_sensor_data, limit1_attr)
217 #define limit2_to_atk_sensor(attr) \
218 container_of(attr, struct atk_sensor_data, limit2_attr)
227 err = atk_read_value(s, &value);
235 return sprintf(buf,
"%llu\n", value);
255 return sprintf(buf,
"%lld\n", value);
267 return sprintf(buf,
"%lld\n", value);
273 return sprintf(buf,
"atk0110\n");
283 attr->
attr.mode = 0444;
352 if (obj->
package.count != expected_size) {
353 dev_warn(dev,
"Invalid package size: %d, expected: %d\n",
354 obj->
package.count, expected_size);
372 tmp = &obj->
package.elements[HWMON_PACK_UNK1];
378 tmp = &obj->
package.elements[HWMON_PACK_UNK2];
387 dev_warn(dev,
"Invalid type (limit1): %d\n", tmp->
type);
393 dev_warn(dev,
"Invalid type (limit2): %d\n", tmp->
type);
399 dev_warn(dev,
"Invalid type (enable): %d\n", tmp->
type);
403 atk_print_sensor(data, obj);
419 what =
"temperature";
450 what = atk_sensor_type(flags);
452 dev_dbg(dev,
"%s: %#llx %s [%llu-%llu] %s\n", what,
456 enable->
integer.value ?
"enabled" :
"disabled");
469 switch (sensor->
type) {
484 id.integer.value = sensor->
id;
490 if (status !=
AE_OK) {
491 dev_warn(dev,
"%s: ACPI exception: %s\n", __func__,
509 id.integer.value = mux;
516 dev_err(dev,
"GGRP[%#x] ACPI exception: %s\n", mux,
518 return ERR_PTR(-
EIO);
528 dev_err(dev,
"GGRP[%#x] package is too small\n", mux);
530 return ERR_PTR(-
EIO);
554 params.pointer = (
void *)&tmp;
559 if (status !=
AE_OK) {
560 dev_warn(dev,
"GITM[%#llx] ACPI exception: %s\n",
id,
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);
588 tmp.
buffer.length =
sizeof(*buf);
596 if (status !=
AE_OK) {
597 dev_warn(dev,
"SITM[%#x] ACPI exception: %s\n", buf->
id,
599 return ERR_PTR(-
EIO);
604 if (obj->
buffer.length < 8) {
605 dev_warn(dev,
"Unexpected ASBF length: %u\n",
608 return ERR_PTR(-
EIO);
621 obj = atk_gitm(data, sensor->
id);
632 dev_warn(dev,
"Read failed, sensor = %#llx\n", sensor->
id);
649 if (sensor->
data->old_interface)
650 err = atk_read_value_old(sensor, value);
652 err = atk_read_value_new(sensor, value);
665 #ifdef CONFIG_DEBUG_FS
666 static int atk_debugfs_gitm_get(
void *
p,
u64 *
val)
679 ret = atk_gitm(data, data->
debugfs.id);
694 atk_debugfs_gitm_get,
698 static
int atk_acpi_print(
char *buf,
size_t sz, union
acpi_object *obj)
704 ret =
snprintf(buf, sz,
"0x%08llx\n", obj->integer.value);
707 ret =
snprintf(buf, sz,
"%s\n", obj->string.pointer);
714 static void atk_pack_print(
char *buf,
size_t sz,
union acpi_object *pack)
719 for (i = 0; i < pack->
package.count; i++) {
722 ret = atk_acpi_print(buf, sz, obj);
743 cls = (data->
debugfs.id & 0xff000000) >> 24;
744 ret = atk_ggrp(data, cls);
748 for (i = 0; i < ret->
package.count; i++) {
756 id = &pack->
package.elements[0];
764 atk_pack_print(buf, 512, pack);
778 static ssize_t atk_debugfs_ggrp_read(
struct file *file,
char __user *buf,
787 static int atk_debugfs_ggrp_release(
struct inode *inode,
struct file *file)
794 .
read = atk_debugfs_ggrp_read,
795 .open = atk_debugfs_ggrp_open,
796 .release = atk_debugfs_ggrp_release,
800 static void atk_debugfs_init(
struct atk_data *data)
821 &atk_debugfs_ggrp_fops);
832 static void atk_debugfs_cleanup(
struct atk_data *data)
839 static void atk_debugfs_init(
struct atk_data *data)
843 static void atk_debugfs_cleanup(
struct atk_data *data)
857 char const *base_name;
858 char const *limit1_name;
859 char const *limit2_name;
867 dev_warn(dev,
"Unknown type for ACPI object: (%d)\n",
872 err = validate_hwmon_pack(data, obj);
891 limit2_name =
"crit";
903 dev_warn(dev,
"Unknown sensor type: %#llx\n", type);
917 sensor = kzalloc(
sizeof(*sensor),
GFP_KERNEL);
927 INIT_LIST_HEAD(&sensor->
list);
939 "%s%d_input", base_name, start + *num);
945 "%s%d_label", base_name, start + *num);
951 "%s%d_%s", base_name, start + *num, limit1_name);
957 "%s%d_%s", base_name, start + *num, limit2_name);
971 static int atk_enumerate_old_hwmon(
struct atk_data *data)
984 if (status !=
AE_OK) {
992 for (i = 1; i < pack->
package.count; i++) {
995 ret = atk_add_sensor(data, obj);
1005 if (status !=
AE_OK) {
1014 for (i = 1; i < pack->
package.count; i++) {
1017 ret = atk_add_sensor(data, obj);
1027 if (status !=
AE_OK) {
1036 for (i = 1; i < pack->
package.count; i++) {
1039 ret = atk_add_sensor(data, obj);
1047 atk_free_sensors(data);
1051 static int atk_ec_present(
struct atk_data *data)
1061 if (PTR_ERR(pack) == -
ENOENT) {
1066 return PTR_ERR(pack);
1071 for (i = 0; i < pack->
package.count; i++) {
1078 id = &obj->
package.elements[0];
1091 dev_dbg(dev,
"EC not found\n");
1097 static int atk_ec_enabled(
struct atk_data *data)
1106 dev_err(dev,
"Unable to query EC status\n");
1107 return PTR_ERR(obj);
1112 dev_err(dev,
"Unable to query EC status\n");
1115 err = (buf->
value != 0);
1116 dev_dbg(dev,
"EC is %sabled\n",
1117 err ?
"en" :
"dis");
1124 static int atk_ec_ctl(
struct atk_data *data,
int enable)
1136 obj = atk_sitm(data, &sitm);
1138 dev_err(dev,
"Failed to %sable the EC\n",
1139 enable ?
"en" :
"dis");
1140 return PTR_ERR(obj);
1144 dev_err(dev,
"Failed to %sable the EC\n",
1145 enable ?
"en" :
"dis");
1149 enable ?
"en" :
"dis");
1156 static int atk_enumerate_new_hwmon(
struct atk_data *data)
1163 err = atk_ec_present(data);
1167 err = atk_ec_enabled(data);
1173 err = atk_ec_ctl(data, 1);
1180 dev_dbg(dev,
"Enumerating hwmon sensors\n");
1184 return PTR_ERR(pack);
1186 for (i = 0; i < pack->
package.count; i++) {
1189 atk_add_sensor(data, obj);
1198 static int atk_create_files(
struct atk_data *data)
1223 static void atk_remove_files(
struct atk_data *data)
1236 static void atk_free_sensors(
struct atk_data *data)
1247 static int atk_register_hwmon(
struct atk_data *data)
1252 dev_dbg(dev,
"registering hwmon device\n");
1257 dev_dbg(dev,
"populating sysfs directory\n");
1258 err = atk_create_files(data);
1265 atk_remove_files(data);
1270 static int atk_probe_if(
struct atk_data *data)
1332 dev_info(dev,
"Overriding interface detection\n");
1345 static int atk_add(
struct acpi_device *
device)
1353 dev_dbg(&device->dev,
"adding...\n");
1368 dev_dbg(&device->dev,
"atk: method MBIF not found\n");
1371 if (obj->
package.count >= 2) {
1374 dev_dbg(&device->dev,
"board ID = %s\n",
1380 err = atk_probe_if(data);
1382 dev_err(&device->dev,
"No usable hwmon interface detected\n");
1387 dev_dbg(&device->dev,
"Using old hwmon interface\n");
1388 err = atk_enumerate_old_hwmon(data);
1390 dev_dbg(&device->dev,
"Using new hwmon interface\n");
1391 err = atk_enumerate_new_hwmon(data);
1397 "No usable sensor detected, bailing out\n");
1402 err = atk_register_hwmon(data);
1406 atk_debugfs_init(data);
1408 device->driver_data =
data;
1411 atk_free_sensors(data);
1414 atk_ec_ctl(data, 0);
1419 static int atk_remove(
struct acpi_device *device,
int type)
1421 struct atk_data *data = device->driver_data;
1422 dev_dbg(&device->dev,
"removing...\n");
1424 device->driver_data =
NULL;
1426 atk_debugfs_cleanup(data);
1428 atk_remove_files(data);
1429 atk_free_sensors(data);
1433 if (atk_ec_ctl(data, 0))
1434 dev_err(&device->dev,
"Failed to disable EC\n");
1442 static int __init atk0110_init(
void)
1448 pr_err(
"Resources not safely usable due to acpi_enforce_resources kernel parameter\n");
1457 pr_info(
"acpi_bus_register_driver failed: %d\n", ret);
1462 static void __exit atk0110_exit(
void)