[ARM] pxa: update defconfig for Verdex Pro
[linux-2.6/verdex.git] / drivers / hwmon / asus_atk0110.c
blob5a3ee00c0e7d2b65baafde7fefdb62a9fe935262
1 /*
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.
6 */
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
22 * hogging the CPU.
24 #define CACHE_TIME HZ
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 {
56 HWMON_PACK_FLAGS,
57 HWMON_PACK_NAME,
58 HWMON_PACK_LIMIT1,
59 HWMON_PACK_LIMIT2,
60 HWMON_PACK_ENABLE
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
82 struct atk_data {
83 struct device *hwmon_dev;
84 acpi_handle atk_handle;
85 struct acpi_device *acpi_dev;
87 bool old_interface;
89 /* old interface */
90 acpi_handle rtmp_handle;
91 acpi_handle rvlt_handle;
92 acpi_handle rfan_handle;
93 /* new inteface */
94 acpi_handle enumerate_handle;
95 acpi_handle read_handle;
96 acpi_handle write_handle;
98 bool disable_ec;
100 int voltage_count;
101 int temperature_count;
102 int fan_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[] = {
111 {ATK_HID, 0},
112 {"", 0},
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];
129 u64 id;
130 u64 type;
131 u64 limit1;
132 u64 limit2;
133 u64 cached_value;
134 unsigned long last_updated; /* in jiffies */
135 bool is_valid;
136 char const *acpi_name;
139 /* Return buffer format:
140 * [0-3] "value" is valid flag
141 * [4-7] value
142 * [8- ] unknown stuff on newer mobos
144 struct atk_acpi_ret_buffer {
145 u32 flags;
146 u32 value;
147 u8 data[];
150 /* Input buffer used for GITM and SITM methods */
151 struct atk_acpi_input_buf {
152 u32 id;
153 u32 param1;
154 u32 param2;
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 = {
164 .name = ATK_HID,
165 .class = "hwmon",
166 .ids = atk_ids,
167 .ops = {
168 .add = atk_add,
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);
189 u64 value;
190 int err;
192 err = atk_read_value(s, &value);
193 if (err)
194 return err;
196 if (s->type == HWMON_TYPE_TEMP)
197 /* ACPI returns decidegree */
198 value *= 100;
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)
218 value *= 100;
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)
230 value *= 100;
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;
248 attr->show = show;
249 attr->store = NULL;
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;
258 int offset;
260 switch (m) {
261 case HWMON_PACK_FLAGS:
262 offset = old_if ? _HWMON_OLD_PACK_FLAGS : _HWMON_NEW_PACK_FLAGS;
263 break;
264 case HWMON_PACK_NAME:
265 offset = old_if ? _HWMON_OLD_PACK_NAME : _HWMON_NEW_PACK_NAME;
266 break;
267 case HWMON_PACK_LIMIT1:
268 offset = old_if ? _HWMON_OLD_PACK_LIMIT1 :
269 _HWMON_NEW_PACK_LIMIT1;
270 break;
271 case HWMON_PACK_LIMIT2:
272 offset = old_if ? _HWMON_OLD_PACK_LIMIT2 :
273 _HWMON_NEW_PACK_LIMIT2;
274 break;
275 case HWMON_PACK_ENABLE:
276 offset = old_if ? _HWMON_OLD_PACK_ENABLE :
277 _HWMON_NEW_PACK_ENABLE;
278 break;
279 default:
280 return NULL;
283 return &pack->package.elements[offset];
287 /* New package format is:
288 * - flag (int)
289 * class - used for de-muxing the request to the correct GITn
290 * type (volt, temp, fan)
291 * sensor id |
292 * sensor id - used for de-muxing the request _inside_ the GITn
293 * - name (str)
294 * - unknown (int)
295 * - unknown (int)
296 * - limit1 (int)
297 * - limit2 (int)
298 * - enable (int)
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);
312 return -EINVAL;
315 if (obj->package.count != expected_size) {
316 dev_warn(dev, "Invalid package size: %d, expected: %d\n",
317 obj->package.count, expected_size);
318 return -EINVAL;
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);
324 return -EINVAL;
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);
330 return -EINVAL;
333 /* Don't check... we don't know what they're useful for anyway */
334 #if 0
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);
338 return -EINVAL;
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);
344 return -EINVAL;
346 #endif
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);
351 return -EINVAL;
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);
357 return -EINVAL;
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);
363 return -EINVAL;
366 atk_print_sensor(data, obj);
368 return 0;
371 #ifdef DEBUG
372 static char const *atk_sensor_type(union acpi_object *flags)
374 u64 type = flags->integer.value & ATK_TYPE_MASK;
375 char const *what;
377 switch (type) {
378 case HWMON_TYPE_VOLT:
379 what = "voltage";
380 break;
381 case HWMON_TYPE_TEMP:
382 what = "temperature";
383 break;
384 case HWMON_TYPE_FAN:
385 what = "fan";
386 break;
387 default:
388 what = "unknown";
389 break;
392 return what;
394 #endif
396 static void atk_print_sensor(struct atk_data *data, union acpi_object *obj)
398 #ifdef DEBUG
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;
405 char const *what;
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");
420 #endif
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;
429 acpi_status status;
430 acpi_handle method;
432 switch (sensor->type) {
433 case HWMON_TYPE_VOLT:
434 method = data->rvlt_handle;
435 break;
436 case HWMON_TYPE_TEMP:
437 method = data->rtmp_handle;
438 break;
439 case HWMON_TYPE_FAN:
440 method = data->rfan_handle;
441 break;
442 default:
443 return -EINVAL;
446 id.type = ACPI_TYPE_INTEGER;
447 id.integer.value = sensor->id;
449 params.count = 1;
450 params.pointer = &id;
452 status = acpi_evaluate_integer(method, NULL, &params, value);
453 if (status != AE_OK) {
454 dev_warn(dev, "%s: ACPI exception: %s\n", __func__,
455 acpi_format_exception(status));
456 return -EIO;
459 return 0;
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;
466 acpi_status ret;
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;
473 params.count = 1;
474 params.pointer = &id;
476 buf.length = ACPI_ALLOCATE_BUFFER;
477 ret = acpi_evaluate_object(data->enumerate_handle, NULL, &params, &buf);
478 if (ret != AE_OK) {
479 dev_err(dev, "GGRP[%#x] ACPI exception: %s\n", mux,
480 acpi_format_exception(ret));
481 return ERR_PTR(-EIO);
483 pack = buf.pointer;
484 if (pack->type != ACPI_TYPE_PACKAGE) {
485 /* Execution was successful, but the id was not found */
486 ACPI_FREE(pack);
487 return ERR_PTR(-ENOENT);
490 if (pack->package.count < 1) {
491 dev_err(dev, "GGRP[%#x] package is too small\n", mux);
492 ACPI_FREE(pack);
493 return ERR_PTR(-EIO);
495 return pack;
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;
506 acpi_status status;
508 buf.id = id;
509 buf.param1 = 0;
510 buf.param2 = 0;
512 tmp.type = ACPI_TYPE_BUFFER;
513 tmp.buffer.pointer = (u8 *)&buf;
514 tmp.buffer.length = sizeof(buf);
516 params.count = 1;
517 params.pointer = (void *)&tmp;
519 ret.length = ACPI_ALLOCATE_BUFFER;
520 status = acpi_evaluate_object_typed(data->read_handle, NULL, &params,
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);
527 obj = ret.pointer;
529 /* Sanity check */
530 if (obj->buffer.length < 8) {
531 dev_warn(dev, "Unexpected ASBF length: %u\n",
532 obj->buffer.length);
533 ACPI_FREE(obj);
534 return ERR_PTR(-EIO);
536 return obj;
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;
547 acpi_status status;
549 tmp.type = ACPI_TYPE_BUFFER;
550 tmp.buffer.pointer = (u8 *)buf;
551 tmp.buffer.length = sizeof(*buf);
553 params.count = 1;
554 params.pointer = &tmp;
556 ret.length = ACPI_ALLOCATE_BUFFER;
557 status = acpi_evaluate_object_typed(data->write_handle, NULL, &params,
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);
564 obj = ret.pointer;
566 /* Sanity check */
567 if (obj->buffer.length < 8) {
568 dev_warn(dev, "Unexpected ASBF length: %u\n",
569 obj->buffer.length);
570 ACPI_FREE(obj);
571 return ERR_PTR(-EIO);
573 return obj;
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;
582 int err = 0;
584 obj = atk_gitm(data, sensor->id);
585 if (IS_ERR(obj))
586 return PTR_ERR(obj);
588 buf = (struct atk_acpi_ret_buffer *)obj->buffer.pointer;
589 if (buf->flags == 0) {
590 /* The reading is not valid, possible causes:
591 * - sensor failure
592 * - enumeration was FUBAR (and we didn't notice)
594 dev_warn(dev, "Read failed, sensor = %#llx\n", sensor->id);
595 err = -EIO;
596 goto out;
599 *value = buf->value;
600 out:
601 ACPI_FREE(obj);
602 return err;
605 static int atk_read_value(struct atk_sensor_data *sensor, u64 *value)
607 int err;
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);
613 else
614 err = atk_read_value_new(sensor, value);
616 sensor->is_valid = true;
617 sensor->last_updated = jiffies;
618 sensor->cached_value = *value;
619 } else {
620 *value = sensor->cached_value;
621 err = 0;
624 return err;
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;
639 u64 type;
640 int err;
641 int *num;
642 int start;
644 if (obj->type != ACPI_TYPE_PACKAGE) {
645 /* wft is this? */
646 dev_warn(dev, "Unknown type for ACPI object: (%d)\n",
647 obj->type);
648 return -EINVAL;
651 err = validate_hwmon_pack(data, obj);
652 if (err)
653 return err;
655 /* Ok, we have a valid hwmon package */
656 type = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS)->integer.value
657 & ATK_TYPE_MASK;
659 switch (type) {
660 case HWMON_TYPE_VOLT:
661 base_name = "in";
662 limit1_name = "min";
663 limit2_name = "max";
664 num = &data->voltage_count;
665 start = 0;
666 break;
667 case HWMON_TYPE_TEMP:
668 base_name = "temp";
669 limit1_name = "max";
670 limit2_name = "crit";
671 num = &data->temperature_count;
672 start = 1;
673 break;
674 case HWMON_TYPE_FAN:
675 base_name = "fan";
676 limit1_name = "min";
677 limit2_name = "max";
678 num = &data->fan_count;
679 start = 1;
680 break;
681 default:
682 dev_warn(dev, "Unknown sensor type: %#llx\n", type);
683 return -EINVAL;
686 enable = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE);
687 if (!enable->integer.value)
688 /* sensor is disabled */
689 return 0;
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);
697 if (!sensor)
698 return -ENOMEM;
700 sensor->acpi_name = kstrdup(name->string.pointer, GFP_KERNEL);
701 if (!sensor->acpi_name) {
702 err = -ENOMEM;
703 goto out;
706 INIT_LIST_HEAD(&sensor->list);
707 sensor->type = type;
708 sensor->data = data;
709 sensor->id = flags->integer.value;
710 sensor->limit1 = limit1->integer.value;
711 if (data->old_interface)
712 sensor->limit2 = limit2->integer.value;
713 else
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,
721 atk_input_show);
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,
727 atk_label_show);
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,
733 atk_limit1_show);
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,
739 atk_limit2_show);
741 list_add(&sensor->list, &data->sensor_list);
742 (*num)++;
744 return 1;
745 out:
746 kfree(sensor->acpi_name);
747 kfree(sensor);
748 return err;
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;
756 acpi_status status;
757 int i, ret;
758 int count = 0;
760 /* Voltages */
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));
768 return -ENODEV;
771 pack = buf.pointer;
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);
776 if (ret > 0)
777 count++;
779 ACPI_FREE(buf.pointer);
781 /* Temperatures */
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));
789 ret = -ENODEV;
790 goto cleanup;
793 pack = buf.pointer;
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);
798 if (ret > 0)
799 count++;
801 ACPI_FREE(buf.pointer);
803 /* Fans */
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));
811 ret = -ENODEV;
812 goto cleanup;
815 pack = buf.pointer;
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);
820 if (ret > 0)
821 count++;
823 ACPI_FREE(buf.pointer);
825 return count;
826 cleanup:
827 atk_free_sensors(data);
828 return ret;
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;
836 int ret;
837 int i;
839 pack = atk_ggrp(data, ATK_MUX_MGMT);
840 if (IS_ERR(pack)) {
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);
844 return 0;
846 return PTR_ERR(pack);
849 /* Search the EC */
850 ec = NULL;
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)
856 continue;
858 id = &obj->package.elements[0];
859 if (id->type != ACPI_TYPE_INTEGER)
860 continue;
862 if (id->integer.value == ATK_EC_ID) {
863 ec = obj;
864 break;
868 ret = (ec != NULL);
869 if (!ret)
870 /* The system has no EC */
871 dev_dbg(dev, "EC not found\n");
873 ACPI_FREE(pack);
874 return ret;
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;
882 int err;
884 obj = atk_gitm(data, ATK_EC_ID);
885 if (IS_ERR(obj)) {
886 dev_err(dev, "Unable to query EC status\n");
887 return PTR_ERR(obj);
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");
893 err = -EIO;
894 } else {
895 err = (buf->value != 0);
896 dev_dbg(dev, "EC is %sabled\n",
897 err ? "en" : "dis");
900 ACPI_FREE(obj);
901 return err;
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;
910 int err = 0;
912 sitm.id = ATK_EC_ID;
913 sitm.param1 = enable;
914 sitm.param2 = 0;
916 obj = atk_sitm(data, &sitm);
917 if (IS_ERR(obj)) {
918 dev_err(dev, "Failed to %sable the EC\n",
919 enable ? "en" : "dis");
920 return PTR_ERR(obj);
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");
926 err = -EIO;
927 } else {
928 dev_info(dev, "EC %sabled\n",
929 enable ? "en" : "dis");
932 ACPI_FREE(obj);
933 return err;
936 static int atk_enumerate_new_hwmon(struct atk_data *data)
938 struct device *dev = &data->acpi_dev->dev;
939 union acpi_object *pack;
940 int err;
941 int i;
943 err = atk_ec_present(data);
944 if (err < 0)
945 return err;
946 if (err) {
947 err = atk_ec_enabled(data);
948 if (err < 0)
949 return err;
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);
954 if (err) {
955 data->disable_ec = false;
956 return err;
960 dev_dbg(dev, "Enumerating hwmon sensors\n");
962 pack = atk_ggrp(data, ATK_MUX_HWMON);
963 if (IS_ERR(pack))
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;
974 ACPI_FREE(pack);
975 return err;
978 static int atk_create_files(struct atk_data *data)
980 struct atk_sensor_data *s;
981 int err;
983 list_for_each_entry(s, &data->sensor_list, list) {
984 err = device_create_file(data->hwmon_dev, &s->input_attr);
985 if (err)
986 return err;
987 err = device_create_file(data->hwmon_dev, &s->label_attr);
988 if (err)
989 return err;
990 err = device_create_file(data->hwmon_dev, &s->limit1_attr);
991 if (err)
992 return err;
993 err = device_create_file(data->hwmon_dev, &s->limit2_attr);
994 if (err)
995 return err;
998 err = device_create_file(data->hwmon_dev, &atk_name_attr);
1000 return err;
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);
1023 kfree(s);
1027 static int atk_register_hwmon(struct atk_data *data)
1029 struct device *dev = &data->acpi_dev->dev;
1030 int err;
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);
1039 if (err)
1040 goto remove;
1042 return 0;
1043 remove:
1044 /* Cleanup the registered files */
1045 atk_remove_files(data);
1046 hwmon_device_unregister(data->hwmon_dev);
1047 return err;
1050 static int atk_check_old_if(struct atk_data *data)
1052 struct device *dev = &data->acpi_dev->dev;
1053 acpi_handle ret;
1054 acpi_status status;
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));
1061 return -ENODEV;
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));
1070 return -ENODEV;
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));
1079 return -ENODEV;
1081 data->rfan_handle = ret;
1083 return 0;
1086 static int atk_check_new_if(struct atk_data *data)
1088 struct device *dev = &data->acpi_dev->dev;
1089 acpi_handle ret;
1090 acpi_status status;
1092 /* Enumeration */
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));
1097 return -ENODEV;
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));
1106 return -ENODEV;
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));
1115 return -ENODEV;
1117 data->write_handle = ret;
1119 return 0;
1122 static int atk_add(struct acpi_device *device)
1124 acpi_status ret;
1125 int err;
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);
1133 if (!data)
1134 return -ENOMEM;
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);
1144 if (ret != AE_OK) {
1145 dev_dbg(&device->dev, "atk: method MBIF not found\n");
1146 err = -ENODEV;
1147 goto out;
1150 obj = buf.pointer;
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);
1164 if (!err) {
1165 dev_dbg(&device->dev, "Using old hwmon interface\n");
1166 data->old_interface = true;
1167 } else {
1168 err = atk_check_new_if(data);
1169 if (err)
1170 goto out;
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);
1178 else
1179 err = atk_enumerate_new_hwmon(data);
1180 if (err < 0)
1181 goto out;
1182 if (err == 0) {
1183 dev_info(&device->dev,
1184 "No usable sensor detected, bailing out\n");
1185 err = -ENODEV;
1186 goto out;
1189 err = atk_register_hwmon(data);
1190 if (err)
1191 goto cleanup;
1193 device->driver_data = data;
1194 return 0;
1195 cleanup:
1196 atk_free_sensors(data);
1197 out:
1198 if (data->disable_ec)
1199 atk_ec_ctl(data, 0);
1200 kfree(data);
1201 return err;
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");
1220 kfree(data);
1222 return 0;
1225 static int __init atk0110_init(void)
1227 int ret;
1229 ret = acpi_bus_register_driver(&atk_driver);
1230 if (ret)
1231 pr_info("atk: acpi_bus_register_driver failed: %d\n", ret);
1233 return 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");