Luca Tettamanti | 2c03d07 | 2009-04-07 15:32:59 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007-2009 Luca Tettamanti <kronos.it@gmail.com> |
| 3 | * |
| 4 | * This file is released under the GPLv2 |
| 5 | * See COPYING in the top level directory of the kernel tree. |
| 6 | */ |
| 7 | |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/hwmon.h> |
| 10 | #include <linux/list.h> |
| 11 | #include <linux/module.h> |
| 12 | |
| 13 | #include <acpi/acpi.h> |
| 14 | #include <acpi/acpixf.h> |
| 15 | #include <acpi/acpi_drivers.h> |
| 16 | #include <acpi/acpi_bus.h> |
| 17 | |
| 18 | |
| 19 | #define ATK_HID "ATK0110" |
| 20 | |
| 21 | /* Minimum time between readings, enforced in order to avoid |
| 22 | * hogging the CPU. |
| 23 | */ |
| 24 | #define CACHE_TIME HZ |
| 25 | |
| 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" |
| 36 | |
| 37 | #define ATK_MUX_HWMON 0x00000006ULL |
| 38 | |
| 39 | #define ATK_CLASS_MASK 0xff000000ULL |
| 40 | #define ATK_CLASS_FREQ_CTL 0x03000000ULL |
| 41 | #define ATK_CLASS_FAN_CTL 0x04000000ULL |
| 42 | #define ATK_CLASS_HWMON 0x06000000ULL |
| 43 | |
| 44 | #define ATK_TYPE_MASK 0x00ff0000ULL |
| 45 | #define HWMON_TYPE_VOLT 0x00020000ULL |
| 46 | #define HWMON_TYPE_TEMP 0x00030000ULL |
| 47 | #define HWMON_TYPE_FAN 0x00040000ULL |
| 48 | |
| 49 | #define HWMON_SENSOR_ID_MASK 0x0000ffffULL |
| 50 | |
| 51 | enum atk_pack_member { |
| 52 | HWMON_PACK_FLAGS, |
| 53 | HWMON_PACK_NAME, |
| 54 | HWMON_PACK_LIMIT1, |
| 55 | HWMON_PACK_LIMIT2, |
| 56 | HWMON_PACK_ENABLE |
| 57 | }; |
| 58 | |
| 59 | /* New package format */ |
| 60 | #define _HWMON_NEW_PACK_SIZE 7 |
| 61 | #define _HWMON_NEW_PACK_FLAGS 0 |
| 62 | #define _HWMON_NEW_PACK_NAME 1 |
| 63 | #define _HWMON_NEW_PACK_UNK1 2 |
| 64 | #define _HWMON_NEW_PACK_UNK2 3 |
| 65 | #define _HWMON_NEW_PACK_LIMIT1 4 |
| 66 | #define _HWMON_NEW_PACK_LIMIT2 5 |
| 67 | #define _HWMON_NEW_PACK_ENABLE 6 |
| 68 | |
| 69 | /* Old package format */ |
| 70 | #define _HWMON_OLD_PACK_SIZE 5 |
| 71 | #define _HWMON_OLD_PACK_FLAGS 0 |
| 72 | #define _HWMON_OLD_PACK_NAME 1 |
| 73 | #define _HWMON_OLD_PACK_LIMIT1 2 |
| 74 | #define _HWMON_OLD_PACK_LIMIT2 3 |
| 75 | #define _HWMON_OLD_PACK_ENABLE 4 |
| 76 | |
| 77 | |
| 78 | struct atk_data { |
| 79 | struct device *hwmon_dev; |
| 80 | acpi_handle atk_handle; |
| 81 | struct acpi_device *acpi_dev; |
| 82 | |
| 83 | bool old_interface; |
| 84 | |
| 85 | /* old interface */ |
| 86 | acpi_handle rtmp_handle; |
| 87 | acpi_handle rvlt_handle; |
| 88 | acpi_handle rfan_handle; |
| 89 | /* new inteface */ |
| 90 | acpi_handle enumerate_handle; |
| 91 | acpi_handle read_handle; |
| 92 | |
| 93 | int voltage_count; |
| 94 | int temperature_count; |
| 95 | int fan_count; |
| 96 | struct list_head sensor_list; |
| 97 | }; |
| 98 | |
| 99 | |
| 100 | typedef ssize_t (*sysfs_show_func)(struct device *dev, |
| 101 | struct device_attribute *attr, char *buf); |
| 102 | |
| 103 | static const struct acpi_device_id atk_ids[] = { |
| 104 | {ATK_HID, 0}, |
| 105 | {"", 0}, |
| 106 | }; |
| 107 | MODULE_DEVICE_TABLE(acpi, atk_ids); |
| 108 | |
| 109 | #define ATTR_NAME_SIZE 16 /* Worst case is "tempN_input" */ |
| 110 | |
| 111 | struct atk_sensor_data { |
| 112 | struct list_head list; |
| 113 | struct atk_data *data; |
| 114 | struct device_attribute label_attr; |
| 115 | struct device_attribute input_attr; |
| 116 | struct device_attribute limit1_attr; |
| 117 | struct device_attribute limit2_attr; |
| 118 | char label_attr_name[ATTR_NAME_SIZE]; |
| 119 | char input_attr_name[ATTR_NAME_SIZE]; |
| 120 | char limit1_attr_name[ATTR_NAME_SIZE]; |
| 121 | char limit2_attr_name[ATTR_NAME_SIZE]; |
| 122 | u64 id; |
| 123 | u64 type; |
| 124 | u64 limit1; |
| 125 | u64 limit2; |
| 126 | u64 cached_value; |
| 127 | unsigned long last_updated; /* in jiffies */ |
| 128 | bool is_valid; |
| 129 | char const *acpi_name; |
| 130 | }; |
| 131 | |
| 132 | struct atk_acpi_buffer_u64 { |
| 133 | union acpi_object buf; |
| 134 | u64 value; |
| 135 | }; |
| 136 | |
| 137 | static int atk_add(struct acpi_device *device); |
| 138 | static int atk_remove(struct acpi_device *device, int type); |
| 139 | static void atk_print_sensor(struct atk_data *data, union acpi_object *obj); |
| 140 | static int atk_read_value(struct atk_sensor_data *sensor, u64 *value); |
| 141 | static void atk_free_sensors(struct atk_data *data); |
| 142 | |
| 143 | static struct acpi_driver atk_driver = { |
| 144 | .name = ATK_HID, |
| 145 | .class = "hwmon", |
| 146 | .ids = atk_ids, |
| 147 | .ops = { |
| 148 | .add = atk_add, |
| 149 | .remove = atk_remove, |
| 150 | }, |
| 151 | }; |
| 152 | |
| 153 | #define input_to_atk_sensor(attr) \ |
| 154 | container_of(attr, struct atk_sensor_data, input_attr) |
| 155 | |
| 156 | #define label_to_atk_sensor(attr) \ |
| 157 | container_of(attr, struct atk_sensor_data, label_attr) |
| 158 | |
| 159 | #define limit1_to_atk_sensor(attr) \ |
| 160 | container_of(attr, struct atk_sensor_data, limit1_attr) |
| 161 | |
| 162 | #define limit2_to_atk_sensor(attr) \ |
| 163 | container_of(attr, struct atk_sensor_data, limit2_attr) |
| 164 | |
| 165 | static ssize_t atk_input_show(struct device *dev, |
| 166 | struct device_attribute *attr, char *buf) |
| 167 | { |
| 168 | struct atk_sensor_data *s = input_to_atk_sensor(attr); |
| 169 | u64 value; |
| 170 | int err; |
| 171 | |
| 172 | err = atk_read_value(s, &value); |
| 173 | if (err) |
| 174 | return err; |
| 175 | |
| 176 | if (s->type == HWMON_TYPE_TEMP) |
| 177 | /* ACPI returns decidegree */ |
| 178 | value *= 100; |
| 179 | |
| 180 | return sprintf(buf, "%llu\n", value); |
| 181 | } |
| 182 | |
| 183 | static ssize_t atk_label_show(struct device *dev, |
| 184 | struct device_attribute *attr, char *buf) |
| 185 | { |
| 186 | struct atk_sensor_data *s = label_to_atk_sensor(attr); |
| 187 | |
| 188 | return sprintf(buf, "%s\n", s->acpi_name); |
| 189 | } |
| 190 | |
| 191 | static ssize_t atk_limit1_show(struct device *dev, |
| 192 | struct device_attribute *attr, char *buf) |
| 193 | { |
| 194 | struct atk_sensor_data *s = limit1_to_atk_sensor(attr); |
| 195 | u64 value = s->limit1; |
| 196 | |
| 197 | if (s->type == HWMON_TYPE_TEMP) |
| 198 | value *= 100; |
| 199 | |
| 200 | return sprintf(buf, "%lld\n", value); |
| 201 | } |
| 202 | |
| 203 | static ssize_t atk_limit2_show(struct device *dev, |
| 204 | struct device_attribute *attr, char *buf) |
| 205 | { |
| 206 | struct atk_sensor_data *s = limit2_to_atk_sensor(attr); |
| 207 | u64 value = s->limit2; |
| 208 | |
| 209 | if (s->type == HWMON_TYPE_TEMP) |
| 210 | value *= 100; |
| 211 | |
| 212 | return sprintf(buf, "%lld\n", value); |
| 213 | } |
| 214 | |
| 215 | static ssize_t atk_name_show(struct device *dev, |
| 216 | struct device_attribute *attr, char *buf) |
| 217 | { |
| 218 | return sprintf(buf, "atk0110\n"); |
| 219 | } |
| 220 | static struct device_attribute atk_name_attr = |
| 221 | __ATTR(name, 0444, atk_name_show, NULL); |
| 222 | |
| 223 | static void atk_init_attribute(struct device_attribute *attr, char *name, |
| 224 | sysfs_show_func show) |
| 225 | { |
| 226 | attr->attr.name = name; |
| 227 | attr->attr.mode = 0444; |
| 228 | attr->show = show; |
| 229 | attr->store = NULL; |
| 230 | } |
| 231 | |
| 232 | |
| 233 | static union acpi_object *atk_get_pack_member(struct atk_data *data, |
| 234 | union acpi_object *pack, |
| 235 | enum atk_pack_member m) |
| 236 | { |
| 237 | bool old_if = data->old_interface; |
| 238 | int offset; |
| 239 | |
| 240 | switch (m) { |
| 241 | case HWMON_PACK_FLAGS: |
| 242 | offset = old_if ? _HWMON_OLD_PACK_FLAGS : _HWMON_NEW_PACK_FLAGS; |
| 243 | break; |
| 244 | case HWMON_PACK_NAME: |
| 245 | offset = old_if ? _HWMON_OLD_PACK_NAME : _HWMON_NEW_PACK_NAME; |
| 246 | break; |
| 247 | case HWMON_PACK_LIMIT1: |
| 248 | offset = old_if ? _HWMON_OLD_PACK_LIMIT1 : |
| 249 | _HWMON_NEW_PACK_LIMIT1; |
| 250 | break; |
| 251 | case HWMON_PACK_LIMIT2: |
| 252 | offset = old_if ? _HWMON_OLD_PACK_LIMIT2 : |
| 253 | _HWMON_NEW_PACK_LIMIT2; |
| 254 | break; |
| 255 | case HWMON_PACK_ENABLE: |
| 256 | offset = old_if ? _HWMON_OLD_PACK_ENABLE : |
| 257 | _HWMON_NEW_PACK_ENABLE; |
| 258 | break; |
| 259 | default: |
| 260 | return NULL; |
| 261 | } |
| 262 | |
| 263 | return &pack->package.elements[offset]; |
| 264 | } |
| 265 | |
| 266 | |
| 267 | /* New package format is: |
| 268 | * - flag (int) |
| 269 | * class - used for de-muxing the request to the correct GITn |
| 270 | * type (volt, temp, fan) |
| 271 | * sensor id | |
| 272 | * sensor id - used for de-muxing the request _inside_ the GITn |
| 273 | * - name (str) |
| 274 | * - unknown (int) |
| 275 | * - unknown (int) |
| 276 | * - limit1 (int) |
| 277 | * - limit2 (int) |
| 278 | * - enable (int) |
| 279 | * |
| 280 | * The old package has the same format but it's missing the two unknown fields. |
| 281 | */ |
| 282 | static int validate_hwmon_pack(struct atk_data *data, union acpi_object *obj) |
| 283 | { |
| 284 | struct device *dev = &data->acpi_dev->dev; |
| 285 | union acpi_object *tmp; |
| 286 | bool old_if = data->old_interface; |
| 287 | int const expected_size = old_if ? _HWMON_OLD_PACK_SIZE : |
| 288 | _HWMON_NEW_PACK_SIZE; |
| 289 | |
| 290 | if (obj->type != ACPI_TYPE_PACKAGE) { |
| 291 | dev_warn(dev, "Invalid type: %d\n", obj->type); |
| 292 | return -EINVAL; |
| 293 | } |
| 294 | |
| 295 | if (obj->package.count != expected_size) { |
| 296 | dev_warn(dev, "Invalid package size: %d, expected: %d\n", |
| 297 | obj->package.count, expected_size); |
| 298 | return -EINVAL; |
| 299 | } |
| 300 | |
| 301 | tmp = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS); |
| 302 | if (tmp->type != ACPI_TYPE_INTEGER) { |
| 303 | dev_warn(dev, "Invalid type (flag): %d\n", tmp->type); |
| 304 | return -EINVAL; |
| 305 | } |
| 306 | |
| 307 | tmp = atk_get_pack_member(data, obj, HWMON_PACK_NAME); |
| 308 | if (tmp->type != ACPI_TYPE_STRING) { |
| 309 | dev_warn(dev, "Invalid type (name): %d\n", tmp->type); |
| 310 | return -EINVAL; |
| 311 | } |
| 312 | |
| 313 | /* Don't check... we don't know what they're useful for anyway */ |
| 314 | #if 0 |
| 315 | tmp = &obj->package.elements[HWMON_PACK_UNK1]; |
| 316 | if (tmp->type != ACPI_TYPE_INTEGER) { |
| 317 | dev_warn(dev, "Invalid type (unk1): %d\n", tmp->type); |
| 318 | return -EINVAL; |
| 319 | } |
| 320 | |
| 321 | tmp = &obj->package.elements[HWMON_PACK_UNK2]; |
| 322 | if (tmp->type != ACPI_TYPE_INTEGER) { |
| 323 | dev_warn(dev, "Invalid type (unk2): %d\n", tmp->type); |
| 324 | return -EINVAL; |
| 325 | } |
| 326 | #endif |
| 327 | |
| 328 | tmp = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT1); |
| 329 | if (tmp->type != ACPI_TYPE_INTEGER) { |
| 330 | dev_warn(dev, "Invalid type (limit1): %d\n", tmp->type); |
| 331 | return -EINVAL; |
| 332 | } |
| 333 | |
| 334 | tmp = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT2); |
| 335 | if (tmp->type != ACPI_TYPE_INTEGER) { |
| 336 | dev_warn(dev, "Invalid type (limit2): %d\n", tmp->type); |
| 337 | return -EINVAL; |
| 338 | } |
| 339 | |
| 340 | tmp = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE); |
| 341 | if (tmp->type != ACPI_TYPE_INTEGER) { |
| 342 | dev_warn(dev, "Invalid type (enable): %d\n", tmp->type); |
| 343 | return -EINVAL; |
| 344 | } |
| 345 | |
| 346 | atk_print_sensor(data, obj); |
| 347 | |
| 348 | return 0; |
| 349 | } |
| 350 | |
Luca Tettamanti | b900870 | 2009-05-08 20:27:28 +0200 | [diff] [blame] | 351 | #ifdef DEBUG |
Luca Tettamanti | 2c03d07 | 2009-04-07 15:32:59 +0200 | [diff] [blame] | 352 | static char const *atk_sensor_type(union acpi_object *flags) |
| 353 | { |
| 354 | u64 type = flags->integer.value & ATK_TYPE_MASK; |
| 355 | char const *what; |
| 356 | |
| 357 | switch (type) { |
| 358 | case HWMON_TYPE_VOLT: |
| 359 | what = "voltage"; |
| 360 | break; |
| 361 | case HWMON_TYPE_TEMP: |
| 362 | what = "temperature"; |
| 363 | break; |
| 364 | case HWMON_TYPE_FAN: |
| 365 | what = "fan"; |
| 366 | break; |
| 367 | default: |
| 368 | what = "unknown"; |
| 369 | break; |
| 370 | } |
| 371 | |
| 372 | return what; |
| 373 | } |
Luca Tettamanti | b900870 | 2009-05-08 20:27:28 +0200 | [diff] [blame] | 374 | #endif |
Luca Tettamanti | 2c03d07 | 2009-04-07 15:32:59 +0200 | [diff] [blame] | 375 | |
| 376 | static void atk_print_sensor(struct atk_data *data, union acpi_object *obj) |
| 377 | { |
| 378 | #ifdef DEBUG |
| 379 | struct device *dev = &data->acpi_dev->dev; |
| 380 | union acpi_object *flags; |
| 381 | union acpi_object *name; |
| 382 | union acpi_object *limit1; |
| 383 | union acpi_object *limit2; |
| 384 | union acpi_object *enable; |
| 385 | char const *what; |
| 386 | |
| 387 | flags = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS); |
| 388 | name = atk_get_pack_member(data, obj, HWMON_PACK_NAME); |
| 389 | limit1 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT1); |
| 390 | limit2 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT2); |
| 391 | enable = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE); |
| 392 | |
| 393 | what = atk_sensor_type(flags); |
| 394 | |
| 395 | dev_dbg(dev, "%s: %#llx %s [%llu-%llu] %s\n", what, |
| 396 | flags->integer.value, |
| 397 | name->string.pointer, |
| 398 | limit1->integer.value, limit2->integer.value, |
| 399 | enable->integer.value ? "enabled" : "disabled"); |
| 400 | #endif |
| 401 | } |
| 402 | |
| 403 | static int atk_read_value_old(struct atk_sensor_data *sensor, u64 *value) |
| 404 | { |
| 405 | struct atk_data *data = sensor->data; |
| 406 | struct device *dev = &data->acpi_dev->dev; |
| 407 | struct acpi_object_list params; |
| 408 | union acpi_object id; |
| 409 | acpi_status status; |
| 410 | acpi_handle method; |
| 411 | |
| 412 | switch (sensor->type) { |
| 413 | case HWMON_TYPE_VOLT: |
| 414 | method = data->rvlt_handle; |
| 415 | break; |
| 416 | case HWMON_TYPE_TEMP: |
| 417 | method = data->rtmp_handle; |
| 418 | break; |
| 419 | case HWMON_TYPE_FAN: |
| 420 | method = data->rfan_handle; |
| 421 | break; |
| 422 | default: |
| 423 | return -EINVAL; |
| 424 | } |
| 425 | |
| 426 | id.type = ACPI_TYPE_INTEGER; |
| 427 | id.integer.value = sensor->id; |
| 428 | |
| 429 | params.count = 1; |
| 430 | params.pointer = &id; |
| 431 | |
| 432 | status = acpi_evaluate_integer(method, NULL, ¶ms, value); |
| 433 | if (status != AE_OK) { |
| 434 | dev_warn(dev, "%s: ACPI exception: %s\n", __func__, |
| 435 | acpi_format_exception(status)); |
| 436 | return -EIO; |
| 437 | } |
| 438 | |
| 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | static int atk_read_value_new(struct atk_sensor_data *sensor, u64 *value) |
| 443 | { |
| 444 | struct atk_data *data = sensor->data; |
| 445 | struct device *dev = &data->acpi_dev->dev; |
| 446 | struct acpi_object_list params; |
| 447 | struct acpi_buffer ret; |
| 448 | union acpi_object id; |
| 449 | struct atk_acpi_buffer_u64 tmp; |
| 450 | acpi_status status; |
| 451 | |
| 452 | id.type = ACPI_TYPE_INTEGER; |
| 453 | id.integer.value = sensor->id; |
| 454 | |
| 455 | params.count = 1; |
| 456 | params.pointer = &id; |
| 457 | |
| 458 | tmp.buf.type = ACPI_TYPE_BUFFER; |
| 459 | tmp.buf.buffer.pointer = (u8 *)&tmp.value; |
| 460 | tmp.buf.buffer.length = sizeof(u64); |
| 461 | ret.length = sizeof(tmp); |
| 462 | ret.pointer = &tmp; |
| 463 | |
| 464 | status = acpi_evaluate_object_typed(data->read_handle, NULL, ¶ms, |
| 465 | &ret, ACPI_TYPE_BUFFER); |
| 466 | if (status != AE_OK) { |
| 467 | dev_warn(dev, "%s: ACPI exception: %s\n", __func__, |
| 468 | acpi_format_exception(status)); |
| 469 | return -EIO; |
| 470 | } |
| 471 | |
| 472 | /* Return buffer format: |
| 473 | * [0-3] "value" is valid flag |
| 474 | * [4-7] value |
| 475 | */ |
| 476 | if (!(tmp.value & 0xffffffff)) { |
| 477 | /* The reading is not valid, possible causes: |
| 478 | * - sensor failure |
| 479 | * - enumeration was FUBAR (and we didn't notice) |
| 480 | */ |
| 481 | dev_info(dev, "Failure: %#llx\n", tmp.value); |
| 482 | return -EIO; |
| 483 | } |
| 484 | |
| 485 | *value = (tmp.value & 0xffffffff00000000ULL) >> 32; |
| 486 | |
| 487 | return 0; |
| 488 | } |
| 489 | |
| 490 | static int atk_read_value(struct atk_sensor_data *sensor, u64 *value) |
| 491 | { |
| 492 | int err; |
| 493 | |
| 494 | if (!sensor->is_valid || |
| 495 | time_after(jiffies, sensor->last_updated + CACHE_TIME)) { |
| 496 | if (sensor->data->old_interface) |
| 497 | err = atk_read_value_old(sensor, value); |
| 498 | else |
| 499 | err = atk_read_value_new(sensor, value); |
| 500 | |
| 501 | sensor->is_valid = true; |
| 502 | sensor->last_updated = jiffies; |
| 503 | sensor->cached_value = *value; |
| 504 | } else { |
| 505 | *value = sensor->cached_value; |
| 506 | err = 0; |
| 507 | } |
| 508 | |
| 509 | return err; |
| 510 | } |
| 511 | |
| 512 | static int atk_add_sensor(struct atk_data *data, union acpi_object *obj) |
| 513 | { |
| 514 | struct device *dev = &data->acpi_dev->dev; |
| 515 | union acpi_object *flags; |
| 516 | union acpi_object *name; |
| 517 | union acpi_object *limit1; |
| 518 | union acpi_object *limit2; |
| 519 | union acpi_object *enable; |
| 520 | struct atk_sensor_data *sensor; |
| 521 | char const *base_name; |
| 522 | char const *limit1_name; |
| 523 | char const *limit2_name; |
| 524 | u64 type; |
| 525 | int err; |
| 526 | int *num; |
| 527 | int start; |
| 528 | |
| 529 | if (obj->type != ACPI_TYPE_PACKAGE) { |
| 530 | /* wft is this? */ |
| 531 | dev_warn(dev, "Unknown type for ACPI object: (%d)\n", |
| 532 | obj->type); |
| 533 | return -EINVAL; |
| 534 | } |
| 535 | |
| 536 | err = validate_hwmon_pack(data, obj); |
| 537 | if (err) |
| 538 | return err; |
| 539 | |
| 540 | /* Ok, we have a valid hwmon package */ |
| 541 | type = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS)->integer.value |
| 542 | & ATK_TYPE_MASK; |
| 543 | |
| 544 | switch (type) { |
| 545 | case HWMON_TYPE_VOLT: |
| 546 | base_name = "in"; |
| 547 | limit1_name = "min"; |
| 548 | limit2_name = "max"; |
| 549 | num = &data->voltage_count; |
| 550 | start = 0; |
| 551 | break; |
| 552 | case HWMON_TYPE_TEMP: |
| 553 | base_name = "temp"; |
| 554 | limit1_name = "max"; |
| 555 | limit2_name = "crit"; |
| 556 | num = &data->temperature_count; |
| 557 | start = 1; |
| 558 | break; |
| 559 | case HWMON_TYPE_FAN: |
| 560 | base_name = "fan"; |
| 561 | limit1_name = "min"; |
| 562 | limit2_name = "max"; |
| 563 | num = &data->fan_count; |
| 564 | start = 1; |
| 565 | break; |
| 566 | default: |
| 567 | dev_warn(dev, "Unknown sensor type: %#llx\n", type); |
| 568 | return -EINVAL; |
| 569 | } |
| 570 | |
| 571 | enable = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE); |
| 572 | if (!enable->integer.value) |
| 573 | /* sensor is disabled */ |
| 574 | return 0; |
| 575 | |
| 576 | flags = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS); |
| 577 | name = atk_get_pack_member(data, obj, HWMON_PACK_NAME); |
| 578 | limit1 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT1); |
| 579 | limit2 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT2); |
| 580 | |
| 581 | sensor = kzalloc(sizeof(*sensor), GFP_KERNEL); |
| 582 | if (!sensor) |
| 583 | return -ENOMEM; |
| 584 | |
| 585 | sensor->acpi_name = kstrdup(name->string.pointer, GFP_KERNEL); |
| 586 | if (!sensor->acpi_name) { |
| 587 | err = -ENOMEM; |
| 588 | goto out; |
| 589 | } |
| 590 | |
| 591 | INIT_LIST_HEAD(&sensor->list); |
| 592 | sensor->type = type; |
| 593 | sensor->data = data; |
| 594 | sensor->id = flags->integer.value; |
| 595 | sensor->limit1 = limit1->integer.value; |
| 596 | sensor->limit2 = limit2->integer.value; |
| 597 | |
| 598 | snprintf(sensor->input_attr_name, ATTR_NAME_SIZE, |
| 599 | "%s%d_input", base_name, start + *num); |
| 600 | atk_init_attribute(&sensor->input_attr, |
| 601 | sensor->input_attr_name, |
| 602 | atk_input_show); |
| 603 | |
| 604 | snprintf(sensor->label_attr_name, ATTR_NAME_SIZE, |
| 605 | "%s%d_label", base_name, start + *num); |
| 606 | atk_init_attribute(&sensor->label_attr, |
| 607 | sensor->label_attr_name, |
| 608 | atk_label_show); |
| 609 | |
| 610 | snprintf(sensor->limit1_attr_name, ATTR_NAME_SIZE, |
| 611 | "%s%d_%s", base_name, start + *num, limit1_name); |
| 612 | atk_init_attribute(&sensor->limit1_attr, |
| 613 | sensor->limit1_attr_name, |
| 614 | atk_limit1_show); |
| 615 | |
| 616 | snprintf(sensor->limit2_attr_name, ATTR_NAME_SIZE, |
| 617 | "%s%d_%s", base_name, start + *num, limit2_name); |
| 618 | atk_init_attribute(&sensor->limit2_attr, |
| 619 | sensor->limit2_attr_name, |
| 620 | atk_limit2_show); |
| 621 | |
| 622 | list_add(&sensor->list, &data->sensor_list); |
| 623 | (*num)++; |
| 624 | |
| 625 | return 1; |
| 626 | out: |
| 627 | kfree(sensor->acpi_name); |
| 628 | kfree(sensor); |
| 629 | return err; |
| 630 | } |
| 631 | |
| 632 | static int atk_enumerate_old_hwmon(struct atk_data *data) |
| 633 | { |
| 634 | struct device *dev = &data->acpi_dev->dev; |
| 635 | struct acpi_buffer buf; |
| 636 | union acpi_object *pack; |
| 637 | acpi_status status; |
| 638 | int i, ret; |
| 639 | int count = 0; |
| 640 | |
| 641 | /* Voltages */ |
| 642 | buf.length = ACPI_ALLOCATE_BUFFER; |
| 643 | status = acpi_evaluate_object_typed(data->atk_handle, |
| 644 | METHOD_OLD_ENUM_VLT, NULL, &buf, ACPI_TYPE_PACKAGE); |
| 645 | if (status != AE_OK) { |
| 646 | dev_warn(dev, METHOD_OLD_ENUM_VLT ": ACPI exception: %s\n", |
| 647 | acpi_format_exception(status)); |
| 648 | |
| 649 | return -ENODEV; |
| 650 | } |
| 651 | |
| 652 | pack = buf.pointer; |
| 653 | for (i = 1; i < pack->package.count; i++) { |
| 654 | union acpi_object *obj = &pack->package.elements[i]; |
| 655 | |
| 656 | ret = atk_add_sensor(data, obj); |
| 657 | if (ret > 0) |
| 658 | count++; |
| 659 | } |
| 660 | ACPI_FREE(buf.pointer); |
| 661 | |
| 662 | /* Temperatures */ |
| 663 | buf.length = ACPI_ALLOCATE_BUFFER; |
| 664 | status = acpi_evaluate_object_typed(data->atk_handle, |
| 665 | METHOD_OLD_ENUM_TMP, NULL, &buf, ACPI_TYPE_PACKAGE); |
| 666 | if (status != AE_OK) { |
| 667 | dev_warn(dev, METHOD_OLD_ENUM_TMP ": ACPI exception: %s\n", |
| 668 | acpi_format_exception(status)); |
| 669 | |
| 670 | ret = -ENODEV; |
| 671 | goto cleanup; |
| 672 | } |
| 673 | |
| 674 | pack = buf.pointer; |
| 675 | for (i = 1; i < pack->package.count; i++) { |
| 676 | union acpi_object *obj = &pack->package.elements[i]; |
| 677 | |
| 678 | ret = atk_add_sensor(data, obj); |
| 679 | if (ret > 0) |
| 680 | count++; |
| 681 | } |
| 682 | ACPI_FREE(buf.pointer); |
| 683 | |
| 684 | /* Fans */ |
| 685 | buf.length = ACPI_ALLOCATE_BUFFER; |
| 686 | status = acpi_evaluate_object_typed(data->atk_handle, |
| 687 | METHOD_OLD_ENUM_FAN, NULL, &buf, ACPI_TYPE_PACKAGE); |
| 688 | if (status != AE_OK) { |
| 689 | dev_warn(dev, METHOD_OLD_ENUM_FAN ": ACPI exception: %s\n", |
| 690 | acpi_format_exception(status)); |
| 691 | |
| 692 | ret = -ENODEV; |
| 693 | goto cleanup; |
| 694 | } |
| 695 | |
| 696 | pack = buf.pointer; |
| 697 | for (i = 1; i < pack->package.count; i++) { |
| 698 | union acpi_object *obj = &pack->package.elements[i]; |
| 699 | |
| 700 | ret = atk_add_sensor(data, obj); |
| 701 | if (ret > 0) |
| 702 | count++; |
| 703 | } |
| 704 | ACPI_FREE(buf.pointer); |
| 705 | |
| 706 | return count; |
| 707 | cleanup: |
| 708 | atk_free_sensors(data); |
| 709 | return ret; |
| 710 | } |
| 711 | |
| 712 | static int atk_enumerate_new_hwmon(struct atk_data *data) |
| 713 | { |
| 714 | struct device *dev = &data->acpi_dev->dev; |
| 715 | struct acpi_buffer buf; |
| 716 | acpi_status ret; |
| 717 | struct acpi_object_list params; |
| 718 | union acpi_object id; |
| 719 | union acpi_object *pack; |
| 720 | int err; |
| 721 | int i; |
| 722 | |
| 723 | dev_dbg(dev, "Enumerating hwmon sensors\n"); |
| 724 | |
| 725 | id.type = ACPI_TYPE_INTEGER; |
| 726 | id.integer.value = ATK_MUX_HWMON; |
| 727 | params.count = 1; |
| 728 | params.pointer = &id; |
| 729 | |
| 730 | buf.length = ACPI_ALLOCATE_BUFFER; |
| 731 | ret = acpi_evaluate_object_typed(data->enumerate_handle, NULL, ¶ms, |
| 732 | &buf, ACPI_TYPE_PACKAGE); |
| 733 | if (ret != AE_OK) { |
| 734 | dev_warn(dev, METHOD_ENUMERATE ": ACPI exception: %s\n", |
| 735 | acpi_format_exception(ret)); |
| 736 | return -ENODEV; |
| 737 | } |
| 738 | |
| 739 | /* Result must be a package */ |
| 740 | pack = buf.pointer; |
| 741 | |
| 742 | if (pack->package.count < 1) { |
| 743 | dev_dbg(dev, "%s: hwmon package is too small: %d\n", __func__, |
| 744 | pack->package.count); |
| 745 | err = -EINVAL; |
| 746 | goto out; |
| 747 | } |
| 748 | |
| 749 | for (i = 0; i < pack->package.count; i++) { |
| 750 | union acpi_object *obj = &pack->package.elements[i]; |
| 751 | |
| 752 | atk_add_sensor(data, obj); |
| 753 | } |
| 754 | |
| 755 | err = data->voltage_count + data->temperature_count + data->fan_count; |
| 756 | |
| 757 | out: |
| 758 | ACPI_FREE(buf.pointer); |
| 759 | return err; |
| 760 | } |
| 761 | |
| 762 | static int atk_create_files(struct atk_data *data) |
| 763 | { |
| 764 | struct atk_sensor_data *s; |
| 765 | int err; |
| 766 | |
| 767 | list_for_each_entry(s, &data->sensor_list, list) { |
| 768 | err = device_create_file(data->hwmon_dev, &s->input_attr); |
| 769 | if (err) |
| 770 | return err; |
| 771 | err = device_create_file(data->hwmon_dev, &s->label_attr); |
| 772 | if (err) |
| 773 | return err; |
| 774 | err = device_create_file(data->hwmon_dev, &s->limit1_attr); |
| 775 | if (err) |
| 776 | return err; |
| 777 | err = device_create_file(data->hwmon_dev, &s->limit2_attr); |
| 778 | if (err) |
| 779 | return err; |
| 780 | } |
| 781 | |
| 782 | err = device_create_file(data->hwmon_dev, &atk_name_attr); |
| 783 | |
| 784 | return err; |
| 785 | } |
| 786 | |
| 787 | static void atk_remove_files(struct atk_data *data) |
| 788 | { |
| 789 | struct atk_sensor_data *s; |
| 790 | |
| 791 | list_for_each_entry(s, &data->sensor_list, list) { |
| 792 | device_remove_file(data->hwmon_dev, &s->input_attr); |
| 793 | device_remove_file(data->hwmon_dev, &s->label_attr); |
| 794 | device_remove_file(data->hwmon_dev, &s->limit1_attr); |
| 795 | device_remove_file(data->hwmon_dev, &s->limit2_attr); |
| 796 | } |
| 797 | device_remove_file(data->hwmon_dev, &atk_name_attr); |
| 798 | } |
| 799 | |
| 800 | static void atk_free_sensors(struct atk_data *data) |
| 801 | { |
| 802 | struct list_head *head = &data->sensor_list; |
| 803 | struct atk_sensor_data *s, *tmp; |
| 804 | |
| 805 | list_for_each_entry_safe(s, tmp, head, list) { |
| 806 | kfree(s->acpi_name); |
| 807 | kfree(s); |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | static int atk_register_hwmon(struct atk_data *data) |
| 812 | { |
| 813 | struct device *dev = &data->acpi_dev->dev; |
| 814 | int err; |
| 815 | |
| 816 | dev_dbg(dev, "registering hwmon device\n"); |
| 817 | data->hwmon_dev = hwmon_device_register(dev); |
| 818 | if (IS_ERR(data->hwmon_dev)) |
| 819 | return PTR_ERR(data->hwmon_dev); |
| 820 | |
| 821 | dev_dbg(dev, "populating sysfs directory\n"); |
| 822 | err = atk_create_files(data); |
| 823 | if (err) |
| 824 | goto remove; |
| 825 | |
| 826 | return 0; |
| 827 | remove: |
| 828 | /* Cleanup the registered files */ |
| 829 | atk_remove_files(data); |
| 830 | hwmon_device_unregister(data->hwmon_dev); |
| 831 | return err; |
| 832 | } |
| 833 | |
| 834 | static int atk_check_old_if(struct atk_data *data) |
| 835 | { |
| 836 | struct device *dev = &data->acpi_dev->dev; |
| 837 | acpi_handle ret; |
| 838 | acpi_status status; |
| 839 | |
| 840 | /* RTMP: read temperature */ |
| 841 | status = acpi_get_handle(data->atk_handle, METHOD_OLD_READ_TMP, &ret); |
| 842 | if (status != AE_OK) { |
| 843 | dev_dbg(dev, "method " METHOD_OLD_READ_TMP " not found: %s\n", |
| 844 | acpi_format_exception(status)); |
| 845 | return -ENODEV; |
| 846 | } |
| 847 | data->rtmp_handle = ret; |
| 848 | |
| 849 | /* RVLT: read voltage */ |
| 850 | status = acpi_get_handle(data->atk_handle, METHOD_OLD_READ_VLT, &ret); |
| 851 | if (status != AE_OK) { |
| 852 | dev_dbg(dev, "method " METHOD_OLD_READ_VLT " not found: %s\n", |
| 853 | acpi_format_exception(status)); |
| 854 | return -ENODEV; |
| 855 | } |
| 856 | data->rvlt_handle = ret; |
| 857 | |
| 858 | /* RFAN: read fan status */ |
| 859 | status = acpi_get_handle(data->atk_handle, METHOD_OLD_READ_FAN, &ret); |
| 860 | if (status != AE_OK) { |
| 861 | dev_dbg(dev, "method " METHOD_OLD_READ_FAN " not found: %s\n", |
| 862 | acpi_format_exception(status)); |
| 863 | return -ENODEV; |
| 864 | } |
| 865 | data->rfan_handle = ret; |
| 866 | |
| 867 | return 0; |
| 868 | } |
| 869 | |
| 870 | static int atk_check_new_if(struct atk_data *data) |
| 871 | { |
| 872 | struct device *dev = &data->acpi_dev->dev; |
| 873 | acpi_handle ret; |
| 874 | acpi_status status; |
| 875 | |
| 876 | /* Enumeration */ |
| 877 | status = acpi_get_handle(data->atk_handle, METHOD_ENUMERATE, &ret); |
| 878 | if (status != AE_OK) { |
| 879 | dev_dbg(dev, "method " METHOD_ENUMERATE " not found: %s\n", |
| 880 | acpi_format_exception(status)); |
| 881 | return -ENODEV; |
| 882 | } |
| 883 | data->enumerate_handle = ret; |
| 884 | |
| 885 | /* De-multiplexer (read) */ |
| 886 | status = acpi_get_handle(data->atk_handle, METHOD_READ, &ret); |
| 887 | if (status != AE_OK) { |
| 888 | dev_dbg(dev, "method " METHOD_READ " not found: %s\n", |
| 889 | acpi_format_exception(status)); |
| 890 | return -ENODEV; |
| 891 | } |
| 892 | data->read_handle = ret; |
| 893 | |
| 894 | return 0; |
| 895 | } |
| 896 | |
| 897 | static int atk_add(struct acpi_device *device) |
| 898 | { |
| 899 | acpi_status ret; |
| 900 | int err; |
| 901 | struct acpi_buffer buf; |
| 902 | union acpi_object *obj; |
| 903 | struct atk_data *data; |
| 904 | |
| 905 | dev_dbg(&device->dev, "adding...\n"); |
| 906 | |
| 907 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 908 | if (!data) |
| 909 | return -ENOMEM; |
| 910 | |
| 911 | data->acpi_dev = device; |
| 912 | data->atk_handle = device->handle; |
| 913 | INIT_LIST_HEAD(&data->sensor_list); |
| 914 | |
| 915 | buf.length = ACPI_ALLOCATE_BUFFER; |
| 916 | ret = acpi_evaluate_object_typed(data->atk_handle, BOARD_ID, NULL, |
| 917 | &buf, ACPI_TYPE_PACKAGE); |
| 918 | if (ret != AE_OK) { |
| 919 | dev_dbg(&device->dev, "atk: method MBIF not found\n"); |
| 920 | err = -ENODEV; |
| 921 | goto out; |
| 922 | } |
| 923 | |
| 924 | obj = buf.pointer; |
| 925 | if (obj->package.count >= 2 && |
| 926 | obj->package.elements[1].type == ACPI_TYPE_STRING) { |
| 927 | dev_dbg(&device->dev, "board ID = %s\n", |
| 928 | obj->package.elements[1].string.pointer); |
| 929 | } |
| 930 | ACPI_FREE(buf.pointer); |
| 931 | |
| 932 | /* Check for hwmon methods: first check "old" style methods; note that |
| 933 | * both may be present: in this case we stick to the old interface; |
| 934 | * analysis of multiple DSDTs indicates that when both interfaces |
| 935 | * are present the new one (GGRP/GITM) is not functional. |
| 936 | */ |
| 937 | err = atk_check_old_if(data); |
| 938 | if (!err) { |
| 939 | dev_dbg(&device->dev, "Using old hwmon interface\n"); |
| 940 | data->old_interface = true; |
| 941 | } else { |
| 942 | err = atk_check_new_if(data); |
| 943 | if (err) |
| 944 | goto out; |
| 945 | |
| 946 | dev_dbg(&device->dev, "Using new hwmon interface\n"); |
| 947 | data->old_interface = false; |
| 948 | } |
| 949 | |
| 950 | if (data->old_interface) |
| 951 | err = atk_enumerate_old_hwmon(data); |
| 952 | else |
| 953 | err = atk_enumerate_new_hwmon(data); |
| 954 | if (err < 0) |
| 955 | goto out; |
| 956 | if (err == 0) { |
| 957 | dev_info(&device->dev, |
| 958 | "No usable sensor detected, bailing out\n"); |
| 959 | err = -ENODEV; |
| 960 | goto out; |
| 961 | } |
| 962 | |
| 963 | err = atk_register_hwmon(data); |
| 964 | if (err) |
| 965 | goto cleanup; |
| 966 | |
| 967 | device->driver_data = data; |
| 968 | return 0; |
| 969 | cleanup: |
| 970 | atk_free_sensors(data); |
| 971 | out: |
| 972 | kfree(data); |
| 973 | return err; |
| 974 | } |
| 975 | |
| 976 | static int atk_remove(struct acpi_device *device, int type) |
| 977 | { |
| 978 | struct atk_data *data = device->driver_data; |
| 979 | dev_dbg(&device->dev, "removing...\n"); |
| 980 | |
| 981 | device->driver_data = NULL; |
| 982 | |
| 983 | atk_remove_files(data); |
| 984 | atk_free_sensors(data); |
| 985 | hwmon_device_unregister(data->hwmon_dev); |
| 986 | |
| 987 | kfree(data); |
| 988 | |
| 989 | return 0; |
| 990 | } |
| 991 | |
| 992 | static int __init atk0110_init(void) |
| 993 | { |
| 994 | int ret; |
| 995 | |
| 996 | ret = acpi_bus_register_driver(&atk_driver); |
| 997 | if (ret) |
| 998 | pr_info("atk: acpi_bus_register_driver failed: %d\n", ret); |
| 999 | |
| 1000 | return ret; |
| 1001 | } |
| 1002 | |
| 1003 | static void __exit atk0110_exit(void) |
| 1004 | { |
| 1005 | acpi_bus_unregister_driver(&atk_driver); |
| 1006 | } |
| 1007 | |
| 1008 | module_init(atk0110_init); |
| 1009 | module_exit(atk0110_exit); |
| 1010 | |
| 1011 | MODULE_LICENSE("GPL"); |