blob: 5a3ee00c0e7d2b65baafde7fefdb62a9fe935262 [file] [log] [blame]
Luca Tettamanti2c03d072009-04-07 15:32:59 +02001/*
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
Luca Tettamanti9e6eba62009-10-09 20:35:18 +020038#define ATK_MUX_MGMT 0x00000011ULL
Luca Tettamanti2c03d072009-04-07 15:32:59 +020039
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
Luca Tettamanti9e6eba62009-10-09 20:35:18 +020044#define ATK_CLASS_MGMT 0x11000000ULL
Luca Tettamanti2c03d072009-04-07 15:32:59 +020045
46#define ATK_TYPE_MASK 0x00ff0000ULL
47#define HWMON_TYPE_VOLT 0x00020000ULL
48#define HWMON_TYPE_TEMP 0x00030000ULL
49#define HWMON_TYPE_FAN 0x00040000ULL
50
Luca Tettamanti9e6eba62009-10-09 20:35:18 +020051#define ATK_ELEMENT_ID_MASK 0x0000ffffULL
52
53#define ATK_EC_ID 0x11060004ULL
Luca Tettamanti2c03d072009-04-07 15:32:59 +020054
55enum atk_pack_member {
56 HWMON_PACK_FLAGS,
57 HWMON_PACK_NAME,
58 HWMON_PACK_LIMIT1,
59 HWMON_PACK_LIMIT2,
60 HWMON_PACK_ENABLE
61};
62
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
72
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
80
81
82struct atk_data {
83 struct device *hwmon_dev;
84 acpi_handle atk_handle;
85 struct acpi_device *acpi_dev;
86
87 bool old_interface;
88
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;
Luca Tettamanti9e6eba62009-10-09 20:35:18 +020096 acpi_handle write_handle;
97
98 bool disable_ec;
Luca Tettamanti2c03d072009-04-07 15:32:59 +020099
100 int voltage_count;
101 int temperature_count;
102 int fan_count;
103 struct list_head sensor_list;
104};
105
106
107typedef ssize_t (*sysfs_show_func)(struct device *dev,
108 struct device_attribute *attr, char *buf);
109
110static const struct acpi_device_id atk_ids[] = {
111 {ATK_HID, 0},
112 {"", 0},
113};
114MODULE_DEVICE_TABLE(acpi, atk_ids);
115
116#define ATTR_NAME_SIZE 16 /* Worst case is "tempN_input" */
117
118struct 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;
137};
138
Luca Tettamanti18e255552009-10-09 20:35:18 +0200139/* Return buffer format:
140 * [0-3] "value" is valid flag
141 * [4-7] value
142 * [8- ] unknown stuff on newer mobos
143 */
144struct atk_acpi_ret_buffer {
145 u32 flags;
146 u32 value;
147 u8 data[];
148};
149
150/* Input buffer used for GITM and SITM methods */
151struct atk_acpi_input_buf {
152 u32 id;
153 u32 param1;
154 u32 param2;
Luca Tettamanti2c03d072009-04-07 15:32:59 +0200155};
156
157static int atk_add(struct acpi_device *device);
158static int atk_remove(struct acpi_device *device, int type);
159static void atk_print_sensor(struct atk_data *data, union acpi_object *obj);
160static int atk_read_value(struct atk_sensor_data *sensor, u64 *value);
161static void atk_free_sensors(struct atk_data *data);
162
163static 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,
170 },
171};
172
173#define input_to_atk_sensor(attr) \
174 container_of(attr, struct atk_sensor_data, input_attr)
175
176#define label_to_atk_sensor(attr) \
177 container_of(attr, struct atk_sensor_data, label_attr)
178
179#define limit1_to_atk_sensor(attr) \
180 container_of(attr, struct atk_sensor_data, limit1_attr)
181
182#define limit2_to_atk_sensor(attr) \
183 container_of(attr, struct atk_sensor_data, limit2_attr)
184
185static ssize_t atk_input_show(struct device *dev,
186 struct device_attribute *attr, char *buf)
187{
188 struct atk_sensor_data *s = input_to_atk_sensor(attr);
189 u64 value;
190 int err;
191
192 err = atk_read_value(s, &value);
193 if (err)
194 return err;
195
196 if (s->type == HWMON_TYPE_TEMP)
197 /* ACPI returns decidegree */
198 value *= 100;
199
200 return sprintf(buf, "%llu\n", value);
201}
202
203static ssize_t atk_label_show(struct device *dev,
204 struct device_attribute *attr, char *buf)
205{
206 struct atk_sensor_data *s = label_to_atk_sensor(attr);
207
208 return sprintf(buf, "%s\n", s->acpi_name);
209}
210
211static ssize_t atk_limit1_show(struct device *dev,
212 struct device_attribute *attr, char *buf)
213{
214 struct atk_sensor_data *s = limit1_to_atk_sensor(attr);
215 u64 value = s->limit1;
216
217 if (s->type == HWMON_TYPE_TEMP)
218 value *= 100;
219
220 return sprintf(buf, "%lld\n", value);
221}
222
223static ssize_t atk_limit2_show(struct device *dev,
224 struct device_attribute *attr, char *buf)
225{
226 struct atk_sensor_data *s = limit2_to_atk_sensor(attr);
227 u64 value = s->limit2;
228
229 if (s->type == HWMON_TYPE_TEMP)
230 value *= 100;
231
232 return sprintf(buf, "%lld\n", value);
233}
234
235static ssize_t atk_name_show(struct device *dev,
236 struct device_attribute *attr, char *buf)
237{
238 return sprintf(buf, "atk0110\n");
239}
240static struct device_attribute atk_name_attr =
241 __ATTR(name, 0444, atk_name_show, NULL);
242
243static void atk_init_attribute(struct device_attribute *attr, char *name,
244 sysfs_show_func show)
245{
246 attr->attr.name = name;
247 attr->attr.mode = 0444;
248 attr->show = show;
249 attr->store = NULL;
250}
251
252
253static union acpi_object *atk_get_pack_member(struct atk_data *data,
254 union acpi_object *pack,
255 enum atk_pack_member m)
256{
257 bool old_if = data->old_interface;
258 int offset;
259
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;
281 }
282
283 return &pack->package.elements[offset];
284}
285
286
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)
299 *
300 * The old package has the same format but it's missing the two unknown fields.
301 */
302static int validate_hwmon_pack(struct atk_data *data, union acpi_object *obj)
303{
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;
309
310 if (obj->type != ACPI_TYPE_PACKAGE) {
311 dev_warn(dev, "Invalid type: %d\n", obj->type);
312 return -EINVAL;
313 }
314
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;
319 }
320
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;
325 }
326
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;
331 }
332
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;
339 }
340
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;
345 }
346#endif
347
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;
352 }
353
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;
358 }
359
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;
364 }
365
366 atk_print_sensor(data, obj);
367
368 return 0;
369}
370
Luca Tettamantib9008702009-05-08 20:27:28 +0200371#ifdef DEBUG
Luca Tettamanti2c03d072009-04-07 15:32:59 +0200372static char const *atk_sensor_type(union acpi_object *flags)
373{
374 u64 type = flags->integer.value & ATK_TYPE_MASK;
375 char const *what;
376
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;
390 }
391
392 return what;
393}
Luca Tettamantib9008702009-05-08 20:27:28 +0200394#endif
Luca Tettamanti2c03d072009-04-07 15:32:59 +0200395
396static void atk_print_sensor(struct atk_data *data, union acpi_object *obj)
397{
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;
406
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);
412
413 what = atk_sensor_type(flags);
414
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
421}
422
423static int atk_read_value_old(struct atk_sensor_data *sensor, u64 *value)
424{
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;
431
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;
444 }
445
446 id.type = ACPI_TYPE_INTEGER;
447 id.integer.value = sensor->id;
448
449 params.count = 1;
450 params.pointer = &id;
451
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;
457 }
458
459 return 0;
460}
461
Luca Tettamanti18e255552009-10-09 20:35:18 +0200462static union acpi_object *atk_ggrp(struct atk_data *data, u16 mux)
463{
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;
470
471 id.type = ACPI_TYPE_INTEGER;
472 id.integer.value = mux;
473 params.count = 1;
474 params.pointer = &id;
475
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);
482 }
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);
488 }
489
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);
494 }
495 return pack;
496}
497
498static union acpi_object *atk_gitm(struct atk_data *data, u64 id)
499{
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;
507
508 buf.id = id;
509 buf.param1 = 0;
510 buf.param2 = 0;
511
512 tmp.type = ACPI_TYPE_BUFFER;
513 tmp.buffer.pointer = (u8 *)&buf;
514 tmp.buffer.length = sizeof(buf);
515
516 params.count = 1;
517 params.pointer = (void *)&tmp;
518
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);
526 }
527 obj = ret.pointer;
528
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);
535 }
536 return obj;
537}
538
Luca Tettamanti9e6eba62009-10-09 20:35:18 +0200539static union acpi_object *atk_sitm(struct atk_data *data,
540 struct atk_acpi_input_buf *buf)
541{
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;
548
549 tmp.type = ACPI_TYPE_BUFFER;
550 tmp.buffer.pointer = (u8 *)buf;
551 tmp.buffer.length = sizeof(*buf);
552
553 params.count = 1;
554 params.pointer = &tmp;
555
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);
563 }
564 obj = ret.pointer;
565
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);
572 }
573 return obj;
574}
575
Luca Tettamanti2c03d072009-04-07 15:32:59 +0200576static int atk_read_value_new(struct atk_sensor_data *sensor, u64 *value)
577{
578 struct atk_data *data = sensor->data;
579 struct device *dev = &data->acpi_dev->dev;
Luca Tettamanti18e255552009-10-09 20:35:18 +0200580 union acpi_object *obj;
581 struct atk_acpi_ret_buffer *buf;
582 int err = 0;
Luca Tettamanti2c03d072009-04-07 15:32:59 +0200583
Luca Tettamanti18e255552009-10-09 20:35:18 +0200584 obj = atk_gitm(data, sensor->id);
585 if (IS_ERR(obj))
586 return PTR_ERR(obj);
Luca Tettamanti2c03d072009-04-07 15:32:59 +0200587
Luca Tettamanti18e255552009-10-09 20:35:18 +0200588 buf = (struct atk_acpi_ret_buffer *)obj->buffer.pointer;
589 if (buf->flags == 0) {
Luca Tettamanti2c03d072009-04-07 15:32:59 +0200590 /* The reading is not valid, possible causes:
591 * - sensor failure
592 * - enumeration was FUBAR (and we didn't notice)
593 */
Luca Tettamanti18e255552009-10-09 20:35:18 +0200594 dev_warn(dev, "Read failed, sensor = %#llx\n", sensor->id);
595 err = -EIO;
596 goto out;
Luca Tettamanti2c03d072009-04-07 15:32:59 +0200597 }
598
Luca Tettamanti18e255552009-10-09 20:35:18 +0200599 *value = buf->value;
600out:
601 ACPI_FREE(obj);
602 return err;
Luca Tettamanti2c03d072009-04-07 15:32:59 +0200603}
604
605static int atk_read_value(struct atk_sensor_data *sensor, u64 *value)
606{
607 int err;
608
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);
615
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;
622 }
623
624 return err;
625}
626
627static int atk_add_sensor(struct atk_data *data, union acpi_object *obj)
628{
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;
643
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;
649 }
650
651 err = validate_hwmon_pack(data, obj);
652 if (err)
653 return err;
654
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;
658
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;
684 }
685
686 enable = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE);
687 if (!enable->integer.value)
688 /* sensor is disabled */
689 return 0;
690
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);
695
696 sensor = kzalloc(sizeof(*sensor), GFP_KERNEL);
697 if (!sensor)
698 return -ENOMEM;
699
700 sensor->acpi_name = kstrdup(name->string.pointer, GFP_KERNEL);
701 if (!sensor->acpi_name) {
702 err = -ENOMEM;
703 goto out;
704 }
705
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;
Luca Tettamanti8d282492009-07-28 16:31:39 +0200711 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;
Luca Tettamanti2c03d072009-04-07 15:32:59 +0200716
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);
722
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);
728
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);
734
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);
740
741 list_add(&sensor->list, &data->sensor_list);
742 (*num)++;
743
744 return 1;
745out:
746 kfree(sensor->acpi_name);
747 kfree(sensor);
748 return err;
749}
750
751static int atk_enumerate_old_hwmon(struct atk_data *data)
752{
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;
759
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));
767
768 return -ENODEV;
769 }
770
771 pack = buf.pointer;
772 for (i = 1; i < pack->package.count; i++) {
773 union acpi_object *obj = &pack->package.elements[i];
774
775 ret = atk_add_sensor(data, obj);
776 if (ret > 0)
777 count++;
778 }
779 ACPI_FREE(buf.pointer);
780
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));
788
789 ret = -ENODEV;
790 goto cleanup;
791 }
792
793 pack = buf.pointer;
794 for (i = 1; i < pack->package.count; i++) {
795 union acpi_object *obj = &pack->package.elements[i];
796
797 ret = atk_add_sensor(data, obj);
798 if (ret > 0)
799 count++;
800 }
801 ACPI_FREE(buf.pointer);
802
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));
810
811 ret = -ENODEV;
812 goto cleanup;
813 }
814
815 pack = buf.pointer;
816 for (i = 1; i < pack->package.count; i++) {
817 union acpi_object *obj = &pack->package.elements[i];
818
819 ret = atk_add_sensor(data, obj);
820 if (ret > 0)
821 count++;
822 }
823 ACPI_FREE(buf.pointer);
824
825 return count;
826cleanup:
827 atk_free_sensors(data);
828 return ret;
829}
830
Luca Tettamanti9e6eba62009-10-09 20:35:18 +0200831static int atk_ec_present(struct atk_data *data)
832{
833 struct device *dev = &data->acpi_dev->dev;
834 union acpi_object *pack;
835 union acpi_object *ec;
836 int ret;
837 int i;
838
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;
845 }
846 return PTR_ERR(pack);
847 }
848
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;
854
855 if (obj->type != ACPI_TYPE_PACKAGE)
856 continue;
857
858 id = &obj->package.elements[0];
859 if (id->type != ACPI_TYPE_INTEGER)
860 continue;
861
862 if (id->integer.value == ATK_EC_ID) {
863 ec = obj;
864 break;
865 }
866 }
867
868 ret = (ec != NULL);
869 if (!ret)
870 /* The system has no EC */
871 dev_dbg(dev, "EC not found\n");
872
873 ACPI_FREE(pack);
874 return ret;
875}
876
877static int atk_ec_enabled(struct atk_data *data)
878{
879 struct device *dev = &data->acpi_dev->dev;
880 union acpi_object *obj;
881 struct atk_acpi_ret_buffer *buf;
882 int err;
883
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);
888 }
889 buf = (struct atk_acpi_ret_buffer *)obj->buffer.pointer;
890
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");
898 }
899
900 ACPI_FREE(obj);
901 return err;
902}
903
904static int atk_ec_ctl(struct atk_data *data, int enable)
905{
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;
911
912 sitm.id = ATK_EC_ID;
913 sitm.param1 = enable;
914 sitm.param2 = 0;
915
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);
921 }
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");
930 }
931
932 ACPI_FREE(obj);
933 return err;
934}
935
Luca Tettamanti2c03d072009-04-07 15:32:59 +0200936static int atk_enumerate_new_hwmon(struct atk_data *data)
937{
938 struct device *dev = &data->acpi_dev->dev;
Luca Tettamanti2c03d072009-04-07 15:32:59 +0200939 union acpi_object *pack;
940 int err;
941 int i;
942
Luca Tettamanti9e6eba62009-10-09 20:35:18 +0200943 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;
952
953 err = atk_ec_ctl(data, 1);
954 if (err) {
955 data->disable_ec = false;
956 return err;
957 }
958 }
959
Luca Tettamanti2c03d072009-04-07 15:32:59 +0200960 dev_dbg(dev, "Enumerating hwmon sensors\n");
961
Luca Tettamanti18e255552009-10-09 20:35:18 +0200962 pack = atk_ggrp(data, ATK_MUX_HWMON);
963 if (IS_ERR(pack))
964 return PTR_ERR(pack);
Luca Tettamanti2c03d072009-04-07 15:32:59 +0200965
966 for (i = 0; i < pack->package.count; i++) {
967 union acpi_object *obj = &pack->package.elements[i];
968
969 atk_add_sensor(data, obj);
970 }
971
972 err = data->voltage_count + data->temperature_count + data->fan_count;
973
Luca Tettamanti18e255552009-10-09 20:35:18 +0200974 ACPI_FREE(pack);
Luca Tettamanti2c03d072009-04-07 15:32:59 +0200975 return err;
976}
977
978static int atk_create_files(struct atk_data *data)
979{
980 struct atk_sensor_data *s;
981 int err;
982
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;
996 }
997
998 err = device_create_file(data->hwmon_dev, &atk_name_attr);
999
1000 return err;
1001}
1002
1003static void atk_remove_files(struct atk_data *data)
1004{
1005 struct atk_sensor_data *s;
1006
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);
1012 }
1013 device_remove_file(data->hwmon_dev, &atk_name_attr);
1014}
1015
1016static void atk_free_sensors(struct atk_data *data)
1017{
1018 struct list_head *head = &data->sensor_list;
1019 struct atk_sensor_data *s, *tmp;
1020
1021 list_for_each_entry_safe(s, tmp, head, list) {
1022 kfree(s->acpi_name);
1023 kfree(s);
1024 }
1025}
1026
1027static int atk_register_hwmon(struct atk_data *data)
1028{
1029 struct device *dev = &data->acpi_dev->dev;
1030 int err;
1031
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);
1036
1037 dev_dbg(dev, "populating sysfs directory\n");
1038 err = atk_create_files(data);
1039 if (err)
1040 goto remove;
1041
1042 return 0;
1043remove:
1044 /* Cleanup the registered files */
1045 atk_remove_files(data);
1046 hwmon_device_unregister(data->hwmon_dev);
1047 return err;
1048}
1049
1050static int atk_check_old_if(struct atk_data *data)
1051{
1052 struct device *dev = &data->acpi_dev->dev;
1053 acpi_handle ret;
1054 acpi_status status;
1055
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;
1062 }
1063 data->rtmp_handle = ret;
1064
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;
1071 }
1072 data->rvlt_handle = ret;
1073
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;
1080 }
1081 data->rfan_handle = ret;
1082
1083 return 0;
1084}
1085
1086static int atk_check_new_if(struct atk_data *data)
1087{
1088 struct device *dev = &data->acpi_dev->dev;
1089 acpi_handle ret;
1090 acpi_status status;
1091
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;
1098 }
1099 data->enumerate_handle = ret;
1100
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;
1107 }
1108 data->read_handle = ret;
1109
Luca Tettamanti9e6eba62009-10-09 20:35:18 +02001110 /* 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;
1116 }
1117 data->write_handle = ret;
1118
Luca Tettamanti2c03d072009-04-07 15:32:59 +02001119 return 0;
1120}
1121
1122static int atk_add(struct acpi_device *device)
1123{
1124 acpi_status ret;
1125 int err;
1126 struct acpi_buffer buf;
1127 union acpi_object *obj;
1128 struct atk_data *data;
1129
1130 dev_dbg(&device->dev, "adding...\n");
1131
1132 data = kzalloc(sizeof(*data), GFP_KERNEL);
1133 if (!data)
1134 return -ENOMEM;
1135
1136 data->acpi_dev = device;
1137 data->atk_handle = device->handle;
1138 INIT_LIST_HEAD(&data->sensor_list);
Luca Tettamanti9e6eba62009-10-09 20:35:18 +02001139 data->disable_ec = false;
Luca Tettamanti2c03d072009-04-07 15:32:59 +02001140
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;
1148 }
1149
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);
1155 }
1156 ACPI_FREE(buf.pointer);
1157
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.
1162 */
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;
1171
1172 dev_dbg(&device->dev, "Using new hwmon interface\n");
1173 data->old_interface = false;
1174 }
1175
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;
1187 }
1188
1189 err = atk_register_hwmon(data);
1190 if (err)
1191 goto cleanup;
1192
1193 device->driver_data = data;
1194 return 0;
1195cleanup:
1196 atk_free_sensors(data);
1197out:
Luca Tettamanti9e6eba62009-10-09 20:35:18 +02001198 if (data->disable_ec)
1199 atk_ec_ctl(data, 0);
Luca Tettamanti2c03d072009-04-07 15:32:59 +02001200 kfree(data);
1201 return err;
1202}
1203
1204static int atk_remove(struct acpi_device *device, int type)
1205{
1206 struct atk_data *data = device->driver_data;
1207 dev_dbg(&device->dev, "removing...\n");
1208
1209 device->driver_data = NULL;
1210
1211 atk_remove_files(data);
1212 atk_free_sensors(data);
1213 hwmon_device_unregister(data->hwmon_dev);
1214
Luca Tettamanti9e6eba62009-10-09 20:35:18 +02001215 if (data->disable_ec) {
1216 if (atk_ec_ctl(data, 0))
1217 dev_err(&device->dev, "Failed to disable EC\n");
1218 }
1219
Luca Tettamanti2c03d072009-04-07 15:32:59 +02001220 kfree(data);
1221
1222 return 0;
1223}
1224
1225static int __init atk0110_init(void)
1226{
1227 int ret;
1228
1229 ret = acpi_bus_register_driver(&atk_driver);
1230 if (ret)
1231 pr_info("atk: acpi_bus_register_driver failed: %d\n", ret);
1232
1233 return ret;
1234}
1235
1236static void __exit atk0110_exit(void)
1237{
1238 acpi_bus_unregister_driver(&atk_driver);
1239}
1240
1241module_init(atk0110_init);
1242module_exit(atk0110_exit);
1243
1244MODULE_LICENSE("GPL");