blob: 56dbcfb3e30170346f16826a37cff51b641e8ef2 [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
Joe Perchesac561492010-10-20 06:51:30 +00008#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
Luca Tettamanti7e5eab12010-01-10 20:52:33 +010010#include <linux/debugfs.h>
Luca Tettamanti2c03d072009-04-07 15:32:59 +020011#include <linux/kernel.h>
12#include <linux/hwmon.h>
13#include <linux/list.h>
14#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Luca Tettamanti86ca33e2011-01-22 16:07:11 +010016#include <linux/dmi.h>
Jean Delvaredcd8f392012-10-10 15:25:56 +020017#include <linux/jiffies.h>
Jean Delvarefa845742012-10-10 15:25:56 +020018#include <linux/err.h>
Luca Tettamanti2c03d072009-04-07 15:32:59 +020019
20#include <acpi/acpi.h>
21#include <acpi/acpixf.h>
22#include <acpi/acpi_drivers.h>
23#include <acpi/acpi_bus.h>
24
25
26#define ATK_HID "ATK0110"
27
Luca Tettamanti86ca33e2011-01-22 16:07:11 +010028static bool new_if;
29module_param(new_if, bool, 0);
30MODULE_PARM_DESC(new_if, "Override detection heuristic and force the use of the new ATK0110 interface");
31
32static const struct dmi_system_id __initconst atk_force_new_if[] = {
33 {
34 /* Old interface has broken MCH temp monitoring */
35 .ident = "Asus Sabertooth X58",
36 .matches = {
37 DMI_MATCH(DMI_BOARD_NAME, "SABERTOOTH X58")
38 }
Luca Tettamanti43ca6cb2012-08-21 17:36:28 +020039 }, {
40 /* Old interface reads the same sensor for fan0 and fan1 */
41 .ident = "Asus M5A78L",
42 .matches = {
43 DMI_MATCH(DMI_BOARD_NAME, "M5A78L")
44 }
Luca Tettamanti86ca33e2011-01-22 16:07:11 +010045 },
46 { }
47};
48
Guenter Roeck75bdc932012-01-19 11:02:16 -080049/*
50 * Minimum time between readings, enforced in order to avoid
Luca Tettamanti2c03d072009-04-07 15:32:59 +020051 * hogging the CPU.
52 */
53#define CACHE_TIME HZ
54
55#define BOARD_ID "MBIF"
56#define METHOD_ENUMERATE "GGRP"
57#define METHOD_READ "GITM"
58#define METHOD_WRITE "SITM"
59#define METHOD_OLD_READ_TMP "RTMP"
60#define METHOD_OLD_READ_VLT "RVLT"
61#define METHOD_OLD_READ_FAN "RFAN"
62#define METHOD_OLD_ENUM_TMP "TSIF"
63#define METHOD_OLD_ENUM_VLT "VSIF"
64#define METHOD_OLD_ENUM_FAN "FSIF"
65
66#define ATK_MUX_HWMON 0x00000006ULL
Luca Tettamanti9e6eba62009-10-09 20:35:18 +020067#define ATK_MUX_MGMT 0x00000011ULL
Luca Tettamanti2c03d072009-04-07 15:32:59 +020068
69#define ATK_CLASS_MASK 0xff000000ULL
70#define ATK_CLASS_FREQ_CTL 0x03000000ULL
71#define ATK_CLASS_FAN_CTL 0x04000000ULL
72#define ATK_CLASS_HWMON 0x06000000ULL
Luca Tettamanti9e6eba62009-10-09 20:35:18 +020073#define ATK_CLASS_MGMT 0x11000000ULL
Luca Tettamanti2c03d072009-04-07 15:32:59 +020074
75#define ATK_TYPE_MASK 0x00ff0000ULL
76#define HWMON_TYPE_VOLT 0x00020000ULL
77#define HWMON_TYPE_TEMP 0x00030000ULL
78#define HWMON_TYPE_FAN 0x00040000ULL
79
Luca Tettamanti9e6eba62009-10-09 20:35:18 +020080#define ATK_ELEMENT_ID_MASK 0x0000ffffULL
81
82#define ATK_EC_ID 0x11060004ULL
Luca Tettamanti2c03d072009-04-07 15:32:59 +020083
84enum atk_pack_member {
85 HWMON_PACK_FLAGS,
86 HWMON_PACK_NAME,
87 HWMON_PACK_LIMIT1,
88 HWMON_PACK_LIMIT2,
89 HWMON_PACK_ENABLE
90};
91
92/* New package format */
93#define _HWMON_NEW_PACK_SIZE 7
94#define _HWMON_NEW_PACK_FLAGS 0
95#define _HWMON_NEW_PACK_NAME 1
96#define _HWMON_NEW_PACK_UNK1 2
97#define _HWMON_NEW_PACK_UNK2 3
98#define _HWMON_NEW_PACK_LIMIT1 4
99#define _HWMON_NEW_PACK_LIMIT2 5
100#define _HWMON_NEW_PACK_ENABLE 6
101
102/* Old package format */
103#define _HWMON_OLD_PACK_SIZE 5
104#define _HWMON_OLD_PACK_FLAGS 0
105#define _HWMON_OLD_PACK_NAME 1
106#define _HWMON_OLD_PACK_LIMIT1 2
107#define _HWMON_OLD_PACK_LIMIT2 3
108#define _HWMON_OLD_PACK_ENABLE 4
109
110
111struct atk_data {
112 struct device *hwmon_dev;
113 acpi_handle atk_handle;
114 struct acpi_device *acpi_dev;
115
116 bool old_interface;
117
118 /* old interface */
119 acpi_handle rtmp_handle;
120 acpi_handle rvlt_handle;
121 acpi_handle rfan_handle;
122 /* new inteface */
123 acpi_handle enumerate_handle;
124 acpi_handle read_handle;
Luca Tettamanti9e6eba62009-10-09 20:35:18 +0200125 acpi_handle write_handle;
126
127 bool disable_ec;
Luca Tettamanti2c03d072009-04-07 15:32:59 +0200128
129 int voltage_count;
130 int temperature_count;
131 int fan_count;
132 struct list_head sensor_list;
Luca Tettamanti7e5eab12010-01-10 20:52:33 +0100133
134 struct {
135 struct dentry *root;
136 u32 id;
137 } debugfs;
Luca Tettamanti2c03d072009-04-07 15:32:59 +0200138};
139
140
141typedef ssize_t (*sysfs_show_func)(struct device *dev,
142 struct device_attribute *attr, char *buf);
143
144static const struct acpi_device_id atk_ids[] = {
145 {ATK_HID, 0},
146 {"", 0},
147};
148MODULE_DEVICE_TABLE(acpi, atk_ids);
149
150#define ATTR_NAME_SIZE 16 /* Worst case is "tempN_input" */
151
152struct atk_sensor_data {
153 struct list_head list;
154 struct atk_data *data;
155 struct device_attribute label_attr;
156 struct device_attribute input_attr;
157 struct device_attribute limit1_attr;
158 struct device_attribute limit2_attr;
159 char label_attr_name[ATTR_NAME_SIZE];
160 char input_attr_name[ATTR_NAME_SIZE];
161 char limit1_attr_name[ATTR_NAME_SIZE];
162 char limit2_attr_name[ATTR_NAME_SIZE];
163 u64 id;
164 u64 type;
165 u64 limit1;
166 u64 limit2;
167 u64 cached_value;
168 unsigned long last_updated; /* in jiffies */
169 bool is_valid;
170 char const *acpi_name;
171};
172
Guenter Roeck75bdc932012-01-19 11:02:16 -0800173/*
174 * Return buffer format:
Luca Tettamanti18e255552009-10-09 20:35:18 +0200175 * [0-3] "value" is valid flag
176 * [4-7] value
177 * [8- ] unknown stuff on newer mobos
178 */
179struct atk_acpi_ret_buffer {
180 u32 flags;
181 u32 value;
182 u8 data[];
183};
184
185/* Input buffer used for GITM and SITM methods */
186struct atk_acpi_input_buf {
187 u32 id;
188 u32 param1;
189 u32 param2;
Luca Tettamanti2c03d072009-04-07 15:32:59 +0200190};
191
192static int atk_add(struct acpi_device *device);
193static int atk_remove(struct acpi_device *device, int type);
194static void atk_print_sensor(struct atk_data *data, union acpi_object *obj);
195static int atk_read_value(struct atk_sensor_data *sensor, u64 *value);
196static void atk_free_sensors(struct atk_data *data);
197
198static struct acpi_driver atk_driver = {
199 .name = ATK_HID,
200 .class = "hwmon",
201 .ids = atk_ids,
202 .ops = {
203 .add = atk_add,
204 .remove = atk_remove,
205 },
206};
207
208#define input_to_atk_sensor(attr) \
209 container_of(attr, struct atk_sensor_data, input_attr)
210
211#define label_to_atk_sensor(attr) \
212 container_of(attr, struct atk_sensor_data, label_attr)
213
214#define limit1_to_atk_sensor(attr) \
215 container_of(attr, struct atk_sensor_data, limit1_attr)
216
217#define limit2_to_atk_sensor(attr) \
218 container_of(attr, struct atk_sensor_data, limit2_attr)
219
220static ssize_t atk_input_show(struct device *dev,
221 struct device_attribute *attr, char *buf)
222{
223 struct atk_sensor_data *s = input_to_atk_sensor(attr);
224 u64 value;
225 int err;
226
227 err = atk_read_value(s, &value);
228 if (err)
229 return err;
230
231 if (s->type == HWMON_TYPE_TEMP)
232 /* ACPI returns decidegree */
233 value *= 100;
234
235 return sprintf(buf, "%llu\n", value);
236}
237
238static ssize_t atk_label_show(struct device *dev,
239 struct device_attribute *attr, char *buf)
240{
241 struct atk_sensor_data *s = label_to_atk_sensor(attr);
242
243 return sprintf(buf, "%s\n", s->acpi_name);
244}
245
246static ssize_t atk_limit1_show(struct device *dev,
247 struct device_attribute *attr, char *buf)
248{
249 struct atk_sensor_data *s = limit1_to_atk_sensor(attr);
250 u64 value = s->limit1;
251
252 if (s->type == HWMON_TYPE_TEMP)
253 value *= 100;
254
255 return sprintf(buf, "%lld\n", value);
256}
257
258static ssize_t atk_limit2_show(struct device *dev,
259 struct device_attribute *attr, char *buf)
260{
261 struct atk_sensor_data *s = limit2_to_atk_sensor(attr);
262 u64 value = s->limit2;
263
264 if (s->type == HWMON_TYPE_TEMP)
265 value *= 100;
266
267 return sprintf(buf, "%lld\n", value);
268}
269
270static ssize_t atk_name_show(struct device *dev,
271 struct device_attribute *attr, char *buf)
272{
273 return sprintf(buf, "atk0110\n");
274}
275static struct device_attribute atk_name_attr =
276 __ATTR(name, 0444, atk_name_show, NULL);
277
278static void atk_init_attribute(struct device_attribute *attr, char *name,
279 sysfs_show_func show)
280{
Guenter Roeck9a2d55b2011-05-24 12:19:05 -0700281 sysfs_attr_init(&attr->attr);
Luca Tettamanti2c03d072009-04-07 15:32:59 +0200282 attr->attr.name = name;
283 attr->attr.mode = 0444;
284 attr->show = show;
285 attr->store = NULL;
286}
287
288
289static union acpi_object *atk_get_pack_member(struct atk_data *data,
290 union acpi_object *pack,
291 enum atk_pack_member m)
292{
293 bool old_if = data->old_interface;
294 int offset;
295
296 switch (m) {
297 case HWMON_PACK_FLAGS:
298 offset = old_if ? _HWMON_OLD_PACK_FLAGS : _HWMON_NEW_PACK_FLAGS;
299 break;
300 case HWMON_PACK_NAME:
301 offset = old_if ? _HWMON_OLD_PACK_NAME : _HWMON_NEW_PACK_NAME;
302 break;
303 case HWMON_PACK_LIMIT1:
304 offset = old_if ? _HWMON_OLD_PACK_LIMIT1 :
305 _HWMON_NEW_PACK_LIMIT1;
306 break;
307 case HWMON_PACK_LIMIT2:
308 offset = old_if ? _HWMON_OLD_PACK_LIMIT2 :
309 _HWMON_NEW_PACK_LIMIT2;
310 break;
311 case HWMON_PACK_ENABLE:
312 offset = old_if ? _HWMON_OLD_PACK_ENABLE :
313 _HWMON_NEW_PACK_ENABLE;
314 break;
315 default:
316 return NULL;
317 }
318
319 return &pack->package.elements[offset];
320}
321
322
Guenter Roeck75bdc932012-01-19 11:02:16 -0800323/*
324 * New package format is:
Luca Tettamanti2c03d072009-04-07 15:32:59 +0200325 * - flag (int)
326 * class - used for de-muxing the request to the correct GITn
327 * type (volt, temp, fan)
328 * sensor id |
329 * sensor id - used for de-muxing the request _inside_ the GITn
330 * - name (str)
331 * - unknown (int)
332 * - unknown (int)
333 * - limit1 (int)
334 * - limit2 (int)
335 * - enable (int)
336 *
337 * The old package has the same format but it's missing the two unknown fields.
338 */
339static int validate_hwmon_pack(struct atk_data *data, union acpi_object *obj)
340{
341 struct device *dev = &data->acpi_dev->dev;
342 union acpi_object *tmp;
343 bool old_if = data->old_interface;
344 int const expected_size = old_if ? _HWMON_OLD_PACK_SIZE :
345 _HWMON_NEW_PACK_SIZE;
346
347 if (obj->type != ACPI_TYPE_PACKAGE) {
348 dev_warn(dev, "Invalid type: %d\n", obj->type);
349 return -EINVAL;
350 }
351
352 if (obj->package.count != expected_size) {
353 dev_warn(dev, "Invalid package size: %d, expected: %d\n",
354 obj->package.count, expected_size);
355 return -EINVAL;
356 }
357
358 tmp = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS);
359 if (tmp->type != ACPI_TYPE_INTEGER) {
360 dev_warn(dev, "Invalid type (flag): %d\n", tmp->type);
361 return -EINVAL;
362 }
363
364 tmp = atk_get_pack_member(data, obj, HWMON_PACK_NAME);
365 if (tmp->type != ACPI_TYPE_STRING) {
366 dev_warn(dev, "Invalid type (name): %d\n", tmp->type);
367 return -EINVAL;
368 }
369
370 /* Don't check... we don't know what they're useful for anyway */
371#if 0
372 tmp = &obj->package.elements[HWMON_PACK_UNK1];
373 if (tmp->type != ACPI_TYPE_INTEGER) {
374 dev_warn(dev, "Invalid type (unk1): %d\n", tmp->type);
375 return -EINVAL;
376 }
377
378 tmp = &obj->package.elements[HWMON_PACK_UNK2];
379 if (tmp->type != ACPI_TYPE_INTEGER) {
380 dev_warn(dev, "Invalid type (unk2): %d\n", tmp->type);
381 return -EINVAL;
382 }
383#endif
384
385 tmp = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT1);
386 if (tmp->type != ACPI_TYPE_INTEGER) {
387 dev_warn(dev, "Invalid type (limit1): %d\n", tmp->type);
388 return -EINVAL;
389 }
390
391 tmp = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT2);
392 if (tmp->type != ACPI_TYPE_INTEGER) {
393 dev_warn(dev, "Invalid type (limit2): %d\n", tmp->type);
394 return -EINVAL;
395 }
396
397 tmp = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE);
398 if (tmp->type != ACPI_TYPE_INTEGER) {
399 dev_warn(dev, "Invalid type (enable): %d\n", tmp->type);
400 return -EINVAL;
401 }
402
403 atk_print_sensor(data, obj);
404
405 return 0;
406}
407
Luca Tettamantib9008702009-05-08 20:27:28 +0200408#ifdef DEBUG
Luca Tettamanti2c03d072009-04-07 15:32:59 +0200409static char const *atk_sensor_type(union acpi_object *flags)
410{
411 u64 type = flags->integer.value & ATK_TYPE_MASK;
412 char const *what;
413
414 switch (type) {
415 case HWMON_TYPE_VOLT:
416 what = "voltage";
417 break;
418 case HWMON_TYPE_TEMP:
419 what = "temperature";
420 break;
421 case HWMON_TYPE_FAN:
422 what = "fan";
423 break;
424 default:
425 what = "unknown";
426 break;
427 }
428
429 return what;
430}
Luca Tettamantib9008702009-05-08 20:27:28 +0200431#endif
Luca Tettamanti2c03d072009-04-07 15:32:59 +0200432
433static void atk_print_sensor(struct atk_data *data, union acpi_object *obj)
434{
435#ifdef DEBUG
436 struct device *dev = &data->acpi_dev->dev;
437 union acpi_object *flags;
438 union acpi_object *name;
439 union acpi_object *limit1;
440 union acpi_object *limit2;
441 union acpi_object *enable;
442 char const *what;
443
444 flags = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS);
445 name = atk_get_pack_member(data, obj, HWMON_PACK_NAME);
446 limit1 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT1);
447 limit2 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT2);
448 enable = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE);
449
450 what = atk_sensor_type(flags);
451
452 dev_dbg(dev, "%s: %#llx %s [%llu-%llu] %s\n", what,
453 flags->integer.value,
454 name->string.pointer,
455 limit1->integer.value, limit2->integer.value,
456 enable->integer.value ? "enabled" : "disabled");
457#endif
458}
459
460static int atk_read_value_old(struct atk_sensor_data *sensor, u64 *value)
461{
462 struct atk_data *data = sensor->data;
463 struct device *dev = &data->acpi_dev->dev;
464 struct acpi_object_list params;
465 union acpi_object id;
466 acpi_status status;
467 acpi_handle method;
468
469 switch (sensor->type) {
470 case HWMON_TYPE_VOLT:
471 method = data->rvlt_handle;
472 break;
473 case HWMON_TYPE_TEMP:
474 method = data->rtmp_handle;
475 break;
476 case HWMON_TYPE_FAN:
477 method = data->rfan_handle;
478 break;
479 default:
480 return -EINVAL;
481 }
482
483 id.type = ACPI_TYPE_INTEGER;
484 id.integer.value = sensor->id;
485
486 params.count = 1;
487 params.pointer = &id;
488
489 status = acpi_evaluate_integer(method, NULL, &params, value);
490 if (status != AE_OK) {
491 dev_warn(dev, "%s: ACPI exception: %s\n", __func__,
492 acpi_format_exception(status));
493 return -EIO;
494 }
495
496 return 0;
497}
498
Luca Tettamanti18e255552009-10-09 20:35:18 +0200499static union acpi_object *atk_ggrp(struct atk_data *data, u16 mux)
500{
501 struct device *dev = &data->acpi_dev->dev;
502 struct acpi_buffer buf;
503 acpi_status ret;
504 struct acpi_object_list params;
505 union acpi_object id;
506 union acpi_object *pack;
507
508 id.type = ACPI_TYPE_INTEGER;
509 id.integer.value = mux;
510 params.count = 1;
511 params.pointer = &id;
512
513 buf.length = ACPI_ALLOCATE_BUFFER;
514 ret = acpi_evaluate_object(data->enumerate_handle, NULL, &params, &buf);
515 if (ret != AE_OK) {
516 dev_err(dev, "GGRP[%#x] ACPI exception: %s\n", mux,
517 acpi_format_exception(ret));
518 return ERR_PTR(-EIO);
519 }
520 pack = buf.pointer;
521 if (pack->type != ACPI_TYPE_PACKAGE) {
522 /* Execution was successful, but the id was not found */
523 ACPI_FREE(pack);
524 return ERR_PTR(-ENOENT);
525 }
526
527 if (pack->package.count < 1) {
528 dev_err(dev, "GGRP[%#x] package is too small\n", mux);
529 ACPI_FREE(pack);
530 return ERR_PTR(-EIO);
531 }
532 return pack;
533}
534
535static union acpi_object *atk_gitm(struct atk_data *data, u64 id)
536{
537 struct device *dev = &data->acpi_dev->dev;
538 struct atk_acpi_input_buf buf;
539 union acpi_object tmp;
540 struct acpi_object_list params;
541 struct acpi_buffer ret;
542 union acpi_object *obj;
543 acpi_status status;
544
545 buf.id = id;
546 buf.param1 = 0;
547 buf.param2 = 0;
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 = (void *)&tmp;
555
556 ret.length = ACPI_ALLOCATE_BUFFER;
557 status = acpi_evaluate_object_typed(data->read_handle, NULL, &params,
558 &ret, ACPI_TYPE_BUFFER);
559 if (status != AE_OK) {
560 dev_warn(dev, "GITM[%#llx] ACPI exception: %s\n", 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 Tettamanti9e6eba62009-10-09 20:35:18 +0200576static union acpi_object *atk_sitm(struct atk_data *data,
577 struct atk_acpi_input_buf *buf)
578{
579 struct device *dev = &data->acpi_dev->dev;
580 struct acpi_object_list params;
581 union acpi_object tmp;
582 struct acpi_buffer ret;
583 union acpi_object *obj;
584 acpi_status status;
585
586 tmp.type = ACPI_TYPE_BUFFER;
587 tmp.buffer.pointer = (u8 *)buf;
588 tmp.buffer.length = sizeof(*buf);
589
590 params.count = 1;
591 params.pointer = &tmp;
592
593 ret.length = ACPI_ALLOCATE_BUFFER;
594 status = acpi_evaluate_object_typed(data->write_handle, NULL, &params,
595 &ret, ACPI_TYPE_BUFFER);
596 if (status != AE_OK) {
597 dev_warn(dev, "SITM[%#x] ACPI exception: %s\n", buf->id,
598 acpi_format_exception(status));
599 return ERR_PTR(-EIO);
600 }
601 obj = ret.pointer;
602
603 /* Sanity check */
604 if (obj->buffer.length < 8) {
605 dev_warn(dev, "Unexpected ASBF length: %u\n",
606 obj->buffer.length);
607 ACPI_FREE(obj);
608 return ERR_PTR(-EIO);
609 }
610 return obj;
611}
612
Luca Tettamanti2c03d072009-04-07 15:32:59 +0200613static int atk_read_value_new(struct atk_sensor_data *sensor, u64 *value)
614{
615 struct atk_data *data = sensor->data;
616 struct device *dev = &data->acpi_dev->dev;
Luca Tettamanti18e255552009-10-09 20:35:18 +0200617 union acpi_object *obj;
618 struct atk_acpi_ret_buffer *buf;
619 int err = 0;
Luca Tettamanti2c03d072009-04-07 15:32:59 +0200620
Luca Tettamanti18e255552009-10-09 20:35:18 +0200621 obj = atk_gitm(data, sensor->id);
622 if (IS_ERR(obj))
623 return PTR_ERR(obj);
Luca Tettamanti2c03d072009-04-07 15:32:59 +0200624
Luca Tettamanti18e255552009-10-09 20:35:18 +0200625 buf = (struct atk_acpi_ret_buffer *)obj->buffer.pointer;
626 if (buf->flags == 0) {
Guenter Roeck75bdc932012-01-19 11:02:16 -0800627 /*
628 * The reading is not valid, possible causes:
Luca Tettamanti2c03d072009-04-07 15:32:59 +0200629 * - sensor failure
630 * - enumeration was FUBAR (and we didn't notice)
631 */
Luca Tettamanti18e255552009-10-09 20:35:18 +0200632 dev_warn(dev, "Read failed, sensor = %#llx\n", sensor->id);
633 err = -EIO;
634 goto out;
Luca Tettamanti2c03d072009-04-07 15:32:59 +0200635 }
636
Luca Tettamanti18e255552009-10-09 20:35:18 +0200637 *value = buf->value;
638out:
639 ACPI_FREE(obj);
640 return err;
Luca Tettamanti2c03d072009-04-07 15:32:59 +0200641}
642
643static int atk_read_value(struct atk_sensor_data *sensor, u64 *value)
644{
645 int err;
646
647 if (!sensor->is_valid ||
648 time_after(jiffies, sensor->last_updated + CACHE_TIME)) {
649 if (sensor->data->old_interface)
650 err = atk_read_value_old(sensor, value);
651 else
652 err = atk_read_value_new(sensor, value);
653
654 sensor->is_valid = true;
655 sensor->last_updated = jiffies;
656 sensor->cached_value = *value;
657 } else {
658 *value = sensor->cached_value;
659 err = 0;
660 }
661
662 return err;
663}
664
Luca Tettamanti7e5eab12010-01-10 20:52:33 +0100665#ifdef CONFIG_DEBUG_FS
666static int atk_debugfs_gitm_get(void *p, u64 *val)
667{
668 struct atk_data *data = p;
669 union acpi_object *ret;
670 struct atk_acpi_ret_buffer *buf;
671 int err = 0;
672
673 if (!data->read_handle)
674 return -ENODEV;
675
676 if (!data->debugfs.id)
677 return -EINVAL;
678
679 ret = atk_gitm(data, data->debugfs.id);
680 if (IS_ERR(ret))
681 return PTR_ERR(ret);
682
683 buf = (struct atk_acpi_ret_buffer *)ret->buffer.pointer;
684 if (buf->flags)
685 *val = buf->value;
686 else
687 err = -EIO;
688
Luca Tettamanti0b8e77f2011-07-17 18:39:18 +0200689 ACPI_FREE(ret);
Luca Tettamanti7e5eab12010-01-10 20:52:33 +0100690 return err;
691}
692
693DEFINE_SIMPLE_ATTRIBUTE(atk_debugfs_gitm,
694 atk_debugfs_gitm_get,
695 NULL,
696 "0x%08llx\n")
697
698static int atk_acpi_print(char *buf, size_t sz, union acpi_object *obj)
699{
700 int ret = 0;
701
702 switch (obj->type) {
703 case ACPI_TYPE_INTEGER:
704 ret = snprintf(buf, sz, "0x%08llx\n", obj->integer.value);
705 break;
706 case ACPI_TYPE_STRING:
707 ret = snprintf(buf, sz, "%s\n", obj->string.pointer);
708 break;
709 }
710
711 return ret;
712}
713
714static void atk_pack_print(char *buf, size_t sz, union acpi_object *pack)
715{
716 int ret;
717 int i;
718
719 for (i = 0; i < pack->package.count; i++) {
720 union acpi_object *obj = &pack->package.elements[i];
721
722 ret = atk_acpi_print(buf, sz, obj);
723 if (ret >= sz)
724 break;
725 buf += ret;
726 sz -= ret;
727 }
728}
729
730static int atk_debugfs_ggrp_open(struct inode *inode, struct file *file)
731{
732 struct atk_data *data = inode->i_private;
733 char *buf = NULL;
734 union acpi_object *ret;
735 u8 cls;
736 int i;
737
738 if (!data->enumerate_handle)
739 return -ENODEV;
740 if (!data->debugfs.id)
741 return -EINVAL;
742
743 cls = (data->debugfs.id & 0xff000000) >> 24;
744 ret = atk_ggrp(data, cls);
745 if (IS_ERR(ret))
746 return PTR_ERR(ret);
747
748 for (i = 0; i < ret->package.count; i++) {
749 union acpi_object *pack = &ret->package.elements[i];
750 union acpi_object *id;
751
752 if (pack->type != ACPI_TYPE_PACKAGE)
753 continue;
754 if (!pack->package.count)
755 continue;
756 id = &pack->package.elements[0];
757 if (id->integer.value == data->debugfs.id) {
758 /* Print the package */
759 buf = kzalloc(512, GFP_KERNEL);
760 if (!buf) {
761 ACPI_FREE(ret);
762 return -ENOMEM;
763 }
764 atk_pack_print(buf, 512, pack);
765 break;
766 }
767 }
768 ACPI_FREE(ret);
769
770 if (!buf)
771 return -EINVAL;
772
773 file->private_data = buf;
774
775 return nonseekable_open(inode, file);
776}
777
778static ssize_t atk_debugfs_ggrp_read(struct file *file, char __user *buf,
779 size_t count, loff_t *pos)
780{
781 char *str = file->private_data;
782 size_t len = strlen(str);
783
784 return simple_read_from_buffer(buf, count, pos, str, len);
785}
786
787static int atk_debugfs_ggrp_release(struct inode *inode, struct file *file)
788{
789 kfree(file->private_data);
790 return 0;
791}
792
793static const struct file_operations atk_debugfs_ggrp_fops = {
794 .read = atk_debugfs_ggrp_read,
795 .open = atk_debugfs_ggrp_open,
796 .release = atk_debugfs_ggrp_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200797 .llseek = no_llseek,
Luca Tettamanti7e5eab12010-01-10 20:52:33 +0100798};
799
800static void atk_debugfs_init(struct atk_data *data)
801{
802 struct dentry *d;
803 struct dentry *f;
804
805 data->debugfs.id = 0;
806
807 d = debugfs_create_dir("asus_atk0110", NULL);
808 if (!d || IS_ERR(d))
809 return;
810
811 f = debugfs_create_x32("id", S_IRUSR | S_IWUSR, d, &data->debugfs.id);
812 if (!f || IS_ERR(f))
813 goto cleanup;
814
815 f = debugfs_create_file("gitm", S_IRUSR, d, data,
816 &atk_debugfs_gitm);
817 if (!f || IS_ERR(f))
818 goto cleanup;
819
820 f = debugfs_create_file("ggrp", S_IRUSR, d, data,
821 &atk_debugfs_ggrp_fops);
822 if (!f || IS_ERR(f))
823 goto cleanup;
824
825 data->debugfs.root = d;
826
827 return;
828cleanup:
829 debugfs_remove_recursive(d);
830}
831
832static void atk_debugfs_cleanup(struct atk_data *data)
833{
834 debugfs_remove_recursive(data->debugfs.root);
835}
836
837#else /* CONFIG_DEBUG_FS */
838
839static void atk_debugfs_init(struct atk_data *data)
840{
841}
842
843static void atk_debugfs_cleanup(struct atk_data *data)
844{
845}
846#endif
847
Luca Tettamanti2c03d072009-04-07 15:32:59 +0200848static int atk_add_sensor(struct atk_data *data, union acpi_object *obj)
849{
850 struct device *dev = &data->acpi_dev->dev;
851 union acpi_object *flags;
852 union acpi_object *name;
853 union acpi_object *limit1;
854 union acpi_object *limit2;
855 union acpi_object *enable;
856 struct atk_sensor_data *sensor;
857 char const *base_name;
858 char const *limit1_name;
859 char const *limit2_name;
860 u64 type;
861 int err;
862 int *num;
863 int start;
864
865 if (obj->type != ACPI_TYPE_PACKAGE) {
866 /* wft is this? */
867 dev_warn(dev, "Unknown type for ACPI object: (%d)\n",
868 obj->type);
869 return -EINVAL;
870 }
871
872 err = validate_hwmon_pack(data, obj);
873 if (err)
874 return err;
875
876 /* Ok, we have a valid hwmon package */
877 type = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS)->integer.value
878 & ATK_TYPE_MASK;
879
880 switch (type) {
881 case HWMON_TYPE_VOLT:
882 base_name = "in";
883 limit1_name = "min";
884 limit2_name = "max";
885 num = &data->voltage_count;
886 start = 0;
887 break;
888 case HWMON_TYPE_TEMP:
889 base_name = "temp";
890 limit1_name = "max";
891 limit2_name = "crit";
892 num = &data->temperature_count;
893 start = 1;
894 break;
895 case HWMON_TYPE_FAN:
896 base_name = "fan";
897 limit1_name = "min";
898 limit2_name = "max";
899 num = &data->fan_count;
900 start = 1;
901 break;
902 default:
903 dev_warn(dev, "Unknown sensor type: %#llx\n", type);
904 return -EINVAL;
905 }
906
907 enable = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE);
908 if (!enable->integer.value)
909 /* sensor is disabled */
910 return 0;
911
912 flags = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS);
913 name = atk_get_pack_member(data, obj, HWMON_PACK_NAME);
914 limit1 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT1);
915 limit2 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT2);
916
917 sensor = kzalloc(sizeof(*sensor), GFP_KERNEL);
918 if (!sensor)
919 return -ENOMEM;
920
921 sensor->acpi_name = kstrdup(name->string.pointer, GFP_KERNEL);
922 if (!sensor->acpi_name) {
923 err = -ENOMEM;
924 goto out;
925 }
926
927 INIT_LIST_HEAD(&sensor->list);
928 sensor->type = type;
929 sensor->data = data;
930 sensor->id = flags->integer.value;
931 sensor->limit1 = limit1->integer.value;
Luca Tettamanti8d282492009-07-28 16:31:39 +0200932 if (data->old_interface)
933 sensor->limit2 = limit2->integer.value;
934 else
935 /* The upper limit is expressed as delta from lower limit */
936 sensor->limit2 = sensor->limit1 + limit2->integer.value;
Luca Tettamanti2c03d072009-04-07 15:32:59 +0200937
938 snprintf(sensor->input_attr_name, ATTR_NAME_SIZE,
939 "%s%d_input", base_name, start + *num);
940 atk_init_attribute(&sensor->input_attr,
941 sensor->input_attr_name,
942 atk_input_show);
943
944 snprintf(sensor->label_attr_name, ATTR_NAME_SIZE,
945 "%s%d_label", base_name, start + *num);
946 atk_init_attribute(&sensor->label_attr,
947 sensor->label_attr_name,
948 atk_label_show);
949
950 snprintf(sensor->limit1_attr_name, ATTR_NAME_SIZE,
951 "%s%d_%s", base_name, start + *num, limit1_name);
952 atk_init_attribute(&sensor->limit1_attr,
953 sensor->limit1_attr_name,
954 atk_limit1_show);
955
956 snprintf(sensor->limit2_attr_name, ATTR_NAME_SIZE,
957 "%s%d_%s", base_name, start + *num, limit2_name);
958 atk_init_attribute(&sensor->limit2_attr,
959 sensor->limit2_attr_name,
960 atk_limit2_show);
961
962 list_add(&sensor->list, &data->sensor_list);
963 (*num)++;
964
965 return 1;
966out:
Luca Tettamanti2c03d072009-04-07 15:32:59 +0200967 kfree(sensor);
968 return err;
969}
970
971static int atk_enumerate_old_hwmon(struct atk_data *data)
972{
973 struct device *dev = &data->acpi_dev->dev;
974 struct acpi_buffer buf;
975 union acpi_object *pack;
976 acpi_status status;
977 int i, ret;
978 int count = 0;
979
980 /* Voltages */
981 buf.length = ACPI_ALLOCATE_BUFFER;
982 status = acpi_evaluate_object_typed(data->atk_handle,
983 METHOD_OLD_ENUM_VLT, NULL, &buf, ACPI_TYPE_PACKAGE);
984 if (status != AE_OK) {
985 dev_warn(dev, METHOD_OLD_ENUM_VLT ": ACPI exception: %s\n",
986 acpi_format_exception(status));
987
988 return -ENODEV;
989 }
990
991 pack = buf.pointer;
992 for (i = 1; i < pack->package.count; i++) {
993 union acpi_object *obj = &pack->package.elements[i];
994
995 ret = atk_add_sensor(data, obj);
996 if (ret > 0)
997 count++;
998 }
999 ACPI_FREE(buf.pointer);
1000
1001 /* Temperatures */
1002 buf.length = ACPI_ALLOCATE_BUFFER;
1003 status = acpi_evaluate_object_typed(data->atk_handle,
1004 METHOD_OLD_ENUM_TMP, NULL, &buf, ACPI_TYPE_PACKAGE);
1005 if (status != AE_OK) {
1006 dev_warn(dev, METHOD_OLD_ENUM_TMP ": ACPI exception: %s\n",
1007 acpi_format_exception(status));
1008
1009 ret = -ENODEV;
1010 goto cleanup;
1011 }
1012
1013 pack = buf.pointer;
1014 for (i = 1; i < pack->package.count; i++) {
1015 union acpi_object *obj = &pack->package.elements[i];
1016
1017 ret = atk_add_sensor(data, obj);
1018 if (ret > 0)
1019 count++;
1020 }
1021 ACPI_FREE(buf.pointer);
1022
1023 /* Fans */
1024 buf.length = ACPI_ALLOCATE_BUFFER;
1025 status = acpi_evaluate_object_typed(data->atk_handle,
1026 METHOD_OLD_ENUM_FAN, NULL, &buf, ACPI_TYPE_PACKAGE);
1027 if (status != AE_OK) {
1028 dev_warn(dev, METHOD_OLD_ENUM_FAN ": ACPI exception: %s\n",
1029 acpi_format_exception(status));
1030
1031 ret = -ENODEV;
1032 goto cleanup;
1033 }
1034
1035 pack = buf.pointer;
1036 for (i = 1; i < pack->package.count; i++) {
1037 union acpi_object *obj = &pack->package.elements[i];
1038
1039 ret = atk_add_sensor(data, obj);
1040 if (ret > 0)
1041 count++;
1042 }
1043 ACPI_FREE(buf.pointer);
1044
1045 return count;
1046cleanup:
1047 atk_free_sensors(data);
1048 return ret;
1049}
1050
Luca Tettamanti9e6eba62009-10-09 20:35:18 +02001051static int atk_ec_present(struct atk_data *data)
1052{
1053 struct device *dev = &data->acpi_dev->dev;
1054 union acpi_object *pack;
1055 union acpi_object *ec;
1056 int ret;
1057 int i;
1058
1059 pack = atk_ggrp(data, ATK_MUX_MGMT);
1060 if (IS_ERR(pack)) {
1061 if (PTR_ERR(pack) == -ENOENT) {
1062 /* The MGMT class does not exists - that's ok */
1063 dev_dbg(dev, "Class %#llx not found\n", ATK_MUX_MGMT);
1064 return 0;
1065 }
1066 return PTR_ERR(pack);
1067 }
1068
1069 /* Search the EC */
1070 ec = NULL;
1071 for (i = 0; i < pack->package.count; i++) {
1072 union acpi_object *obj = &pack->package.elements[i];
1073 union acpi_object *id;
1074
1075 if (obj->type != ACPI_TYPE_PACKAGE)
1076 continue;
1077
1078 id = &obj->package.elements[0];
1079 if (id->type != ACPI_TYPE_INTEGER)
1080 continue;
1081
1082 if (id->integer.value == ATK_EC_ID) {
1083 ec = obj;
1084 break;
1085 }
1086 }
1087
1088 ret = (ec != NULL);
1089 if (!ret)
1090 /* The system has no EC */
1091 dev_dbg(dev, "EC not found\n");
1092
1093 ACPI_FREE(pack);
1094 return ret;
1095}
1096
1097static int atk_ec_enabled(struct atk_data *data)
1098{
1099 struct device *dev = &data->acpi_dev->dev;
1100 union acpi_object *obj;
1101 struct atk_acpi_ret_buffer *buf;
1102 int err;
1103
1104 obj = atk_gitm(data, ATK_EC_ID);
1105 if (IS_ERR(obj)) {
1106 dev_err(dev, "Unable to query EC status\n");
1107 return PTR_ERR(obj);
1108 }
1109 buf = (struct atk_acpi_ret_buffer *)obj->buffer.pointer;
1110
1111 if (buf->flags == 0) {
1112 dev_err(dev, "Unable to query EC status\n");
1113 err = -EIO;
1114 } else {
1115 err = (buf->value != 0);
1116 dev_dbg(dev, "EC is %sabled\n",
1117 err ? "en" : "dis");
1118 }
1119
1120 ACPI_FREE(obj);
1121 return err;
1122}
1123
1124static int atk_ec_ctl(struct atk_data *data, int enable)
1125{
1126 struct device *dev = &data->acpi_dev->dev;
1127 union acpi_object *obj;
1128 struct atk_acpi_input_buf sitm;
1129 struct atk_acpi_ret_buffer *ec_ret;
1130 int err = 0;
1131
1132 sitm.id = ATK_EC_ID;
1133 sitm.param1 = enable;
1134 sitm.param2 = 0;
1135
1136 obj = atk_sitm(data, &sitm);
1137 if (IS_ERR(obj)) {
1138 dev_err(dev, "Failed to %sable the EC\n",
1139 enable ? "en" : "dis");
1140 return PTR_ERR(obj);
1141 }
1142 ec_ret = (struct atk_acpi_ret_buffer *)obj->buffer.pointer;
1143 if (ec_ret->flags == 0) {
1144 dev_err(dev, "Failed to %sable the EC\n",
1145 enable ? "en" : "dis");
1146 err = -EIO;
1147 } else {
1148 dev_info(dev, "EC %sabled\n",
1149 enable ? "en" : "dis");
1150 }
1151
1152 ACPI_FREE(obj);
1153 return err;
1154}
1155
Luca Tettamanti2c03d072009-04-07 15:32:59 +02001156static int atk_enumerate_new_hwmon(struct atk_data *data)
1157{
1158 struct device *dev = &data->acpi_dev->dev;
Luca Tettamanti2c03d072009-04-07 15:32:59 +02001159 union acpi_object *pack;
1160 int err;
1161 int i;
1162
Luca Tettamanti9e6eba62009-10-09 20:35:18 +02001163 err = atk_ec_present(data);
1164 if (err < 0)
1165 return err;
1166 if (err) {
1167 err = atk_ec_enabled(data);
1168 if (err < 0)
1169 return err;
1170 /* If the EC was disabled we will disable it again on unload */
1171 data->disable_ec = err;
1172
1173 err = atk_ec_ctl(data, 1);
1174 if (err) {
1175 data->disable_ec = false;
1176 return err;
1177 }
1178 }
1179
Luca Tettamanti2c03d072009-04-07 15:32:59 +02001180 dev_dbg(dev, "Enumerating hwmon sensors\n");
1181
Luca Tettamanti18e255552009-10-09 20:35:18 +02001182 pack = atk_ggrp(data, ATK_MUX_HWMON);
1183 if (IS_ERR(pack))
1184 return PTR_ERR(pack);
Luca Tettamanti2c03d072009-04-07 15:32:59 +02001185
1186 for (i = 0; i < pack->package.count; i++) {
1187 union acpi_object *obj = &pack->package.elements[i];
1188
1189 atk_add_sensor(data, obj);
1190 }
1191
1192 err = data->voltage_count + data->temperature_count + data->fan_count;
1193
Luca Tettamanti18e255552009-10-09 20:35:18 +02001194 ACPI_FREE(pack);
Luca Tettamanti2c03d072009-04-07 15:32:59 +02001195 return err;
1196}
1197
1198static int atk_create_files(struct atk_data *data)
1199{
1200 struct atk_sensor_data *s;
1201 int err;
1202
1203 list_for_each_entry(s, &data->sensor_list, list) {
1204 err = device_create_file(data->hwmon_dev, &s->input_attr);
1205 if (err)
1206 return err;
1207 err = device_create_file(data->hwmon_dev, &s->label_attr);
1208 if (err)
1209 return err;
1210 err = device_create_file(data->hwmon_dev, &s->limit1_attr);
1211 if (err)
1212 return err;
1213 err = device_create_file(data->hwmon_dev, &s->limit2_attr);
1214 if (err)
1215 return err;
1216 }
1217
1218 err = device_create_file(data->hwmon_dev, &atk_name_attr);
1219
1220 return err;
1221}
1222
1223static void atk_remove_files(struct atk_data *data)
1224{
1225 struct atk_sensor_data *s;
1226
1227 list_for_each_entry(s, &data->sensor_list, list) {
1228 device_remove_file(data->hwmon_dev, &s->input_attr);
1229 device_remove_file(data->hwmon_dev, &s->label_attr);
1230 device_remove_file(data->hwmon_dev, &s->limit1_attr);
1231 device_remove_file(data->hwmon_dev, &s->limit2_attr);
1232 }
1233 device_remove_file(data->hwmon_dev, &atk_name_attr);
1234}
1235
1236static void atk_free_sensors(struct atk_data *data)
1237{
1238 struct list_head *head = &data->sensor_list;
1239 struct atk_sensor_data *s, *tmp;
1240
1241 list_for_each_entry_safe(s, tmp, head, list) {
1242 kfree(s->acpi_name);
1243 kfree(s);
1244 }
1245}
1246
1247static int atk_register_hwmon(struct atk_data *data)
1248{
1249 struct device *dev = &data->acpi_dev->dev;
1250 int err;
1251
1252 dev_dbg(dev, "registering hwmon device\n");
1253 data->hwmon_dev = hwmon_device_register(dev);
1254 if (IS_ERR(data->hwmon_dev))
1255 return PTR_ERR(data->hwmon_dev);
1256
1257 dev_dbg(dev, "populating sysfs directory\n");
1258 err = atk_create_files(data);
1259 if (err)
1260 goto remove;
1261
1262 return 0;
1263remove:
1264 /* Cleanup the registered files */
1265 atk_remove_files(data);
1266 hwmon_device_unregister(data->hwmon_dev);
1267 return err;
1268}
1269
Luca Tettamanti8ba406b2010-01-10 20:52:33 +01001270static int atk_probe_if(struct atk_data *data)
Luca Tettamanti2c03d072009-04-07 15:32:59 +02001271{
1272 struct device *dev = &data->acpi_dev->dev;
1273 acpi_handle ret;
1274 acpi_status status;
Luca Tettamanti8ba406b2010-01-10 20:52:33 +01001275 int err = 0;
Luca Tettamanti2c03d072009-04-07 15:32:59 +02001276
1277 /* RTMP: read temperature */
1278 status = acpi_get_handle(data->atk_handle, METHOD_OLD_READ_TMP, &ret);
Luca Tettamanti8ba406b2010-01-10 20:52:33 +01001279 if (ACPI_SUCCESS(status))
1280 data->rtmp_handle = ret;
1281 else
Luca Tettamanti2c03d072009-04-07 15:32:59 +02001282 dev_dbg(dev, "method " METHOD_OLD_READ_TMP " not found: %s\n",
1283 acpi_format_exception(status));
Luca Tettamanti2c03d072009-04-07 15:32:59 +02001284
1285 /* RVLT: read voltage */
1286 status = acpi_get_handle(data->atk_handle, METHOD_OLD_READ_VLT, &ret);
Luca Tettamanti8ba406b2010-01-10 20:52:33 +01001287 if (ACPI_SUCCESS(status))
1288 data->rvlt_handle = ret;
1289 else
Luca Tettamanti2c03d072009-04-07 15:32:59 +02001290 dev_dbg(dev, "method " METHOD_OLD_READ_VLT " not found: %s\n",
1291 acpi_format_exception(status));
Luca Tettamanti2c03d072009-04-07 15:32:59 +02001292
1293 /* RFAN: read fan status */
1294 status = acpi_get_handle(data->atk_handle, METHOD_OLD_READ_FAN, &ret);
Luca Tettamanti8ba406b2010-01-10 20:52:33 +01001295 if (ACPI_SUCCESS(status))
1296 data->rfan_handle = ret;
1297 else
Luca Tettamanti2c03d072009-04-07 15:32:59 +02001298 dev_dbg(dev, "method " METHOD_OLD_READ_FAN " not found: %s\n",
1299 acpi_format_exception(status));
Luca Tettamanti2c03d072009-04-07 15:32:59 +02001300
1301 /* Enumeration */
1302 status = acpi_get_handle(data->atk_handle, METHOD_ENUMERATE, &ret);
Luca Tettamanti8ba406b2010-01-10 20:52:33 +01001303 if (ACPI_SUCCESS(status))
1304 data->enumerate_handle = ret;
1305 else
Luca Tettamanti2c03d072009-04-07 15:32:59 +02001306 dev_dbg(dev, "method " METHOD_ENUMERATE " not found: %s\n",
1307 acpi_format_exception(status));
Luca Tettamanti2c03d072009-04-07 15:32:59 +02001308
1309 /* De-multiplexer (read) */
1310 status = acpi_get_handle(data->atk_handle, METHOD_READ, &ret);
Luca Tettamanti8ba406b2010-01-10 20:52:33 +01001311 if (ACPI_SUCCESS(status))
1312 data->read_handle = ret;
1313 else
Luca Tettamanti2c03d072009-04-07 15:32:59 +02001314 dev_dbg(dev, "method " METHOD_READ " not found: %s\n",
1315 acpi_format_exception(status));
Luca Tettamanti2c03d072009-04-07 15:32:59 +02001316
Luca Tettamanti9e6eba62009-10-09 20:35:18 +02001317 /* De-multiplexer (write) */
1318 status = acpi_get_handle(data->atk_handle, METHOD_WRITE, &ret);
Luca Tettamanti8ba406b2010-01-10 20:52:33 +01001319 if (ACPI_SUCCESS(status))
1320 data->write_handle = ret;
1321 else
1322 dev_dbg(dev, "method " METHOD_WRITE " not found: %s\n",
Luca Tettamanti9e6eba62009-10-09 20:35:18 +02001323 acpi_format_exception(status));
Luca Tettamanti9e6eba62009-10-09 20:35:18 +02001324
Guenter Roeck75bdc932012-01-19 11:02:16 -08001325 /*
1326 * Check for hwmon methods: first check "old" style methods; note that
Luca Tettamanti8ba406b2010-01-10 20:52:33 +01001327 * both may be present: in this case we stick to the old interface;
1328 * analysis of multiple DSDTs indicates that when both interfaces
1329 * are present the new one (GGRP/GITM) is not functional.
1330 */
Luca Tettamanti86ca33e2011-01-22 16:07:11 +01001331 if (new_if)
1332 dev_info(dev, "Overriding interface detection\n");
Frans Meulenbroeksc6e8ac02012-01-08 19:34:09 +01001333 if (data->rtmp_handle &&
1334 data->rvlt_handle && data->rfan_handle && !new_if)
Luca Tettamanti8ba406b2010-01-10 20:52:33 +01001335 data->old_interface = true;
1336 else if (data->enumerate_handle && data->read_handle &&
1337 data->write_handle)
1338 data->old_interface = false;
1339 else
1340 err = -ENODEV;
1341
1342 return err;
Luca Tettamanti2c03d072009-04-07 15:32:59 +02001343}
1344
1345static int atk_add(struct acpi_device *device)
1346{
1347 acpi_status ret;
1348 int err;
1349 struct acpi_buffer buf;
1350 union acpi_object *obj;
1351 struct atk_data *data;
1352
1353 dev_dbg(&device->dev, "adding...\n");
1354
1355 data = kzalloc(sizeof(*data), GFP_KERNEL);
1356 if (!data)
1357 return -ENOMEM;
1358
1359 data->acpi_dev = device;
1360 data->atk_handle = device->handle;
1361 INIT_LIST_HEAD(&data->sensor_list);
Luca Tettamanti9e6eba62009-10-09 20:35:18 +02001362 data->disable_ec = false;
Luca Tettamanti2c03d072009-04-07 15:32:59 +02001363
1364 buf.length = ACPI_ALLOCATE_BUFFER;
1365 ret = acpi_evaluate_object_typed(data->atk_handle, BOARD_ID, NULL,
1366 &buf, ACPI_TYPE_PACKAGE);
1367 if (ret != AE_OK) {
1368 dev_dbg(&device->dev, "atk: method MBIF not found\n");
Luca Tettamanti55424822010-01-25 15:00:49 +01001369 } else {
1370 obj = buf.pointer;
1371 if (obj->package.count >= 2) {
1372 union acpi_object *id = &obj->package.elements[1];
1373 if (id->type == ACPI_TYPE_STRING)
1374 dev_dbg(&device->dev, "board ID = %s\n",
1375 id->string.pointer);
1376 }
1377 ACPI_FREE(buf.pointer);
Luca Tettamanti2c03d072009-04-07 15:32:59 +02001378 }
1379
Luca Tettamanti8ba406b2010-01-10 20:52:33 +01001380 err = atk_probe_if(data);
1381 if (err) {
1382 dev_err(&device->dev, "No usable hwmon interface detected\n");
1383 goto out;
Luca Tettamanti2c03d072009-04-07 15:32:59 +02001384 }
1385
Luca Tettamanti8ba406b2010-01-10 20:52:33 +01001386 if (data->old_interface) {
1387 dev_dbg(&device->dev, "Using old hwmon interface\n");
Luca Tettamanti2c03d072009-04-07 15:32:59 +02001388 err = atk_enumerate_old_hwmon(data);
Luca Tettamanti8ba406b2010-01-10 20:52:33 +01001389 } else {
1390 dev_dbg(&device->dev, "Using new hwmon interface\n");
Luca Tettamanti2c03d072009-04-07 15:32:59 +02001391 err = atk_enumerate_new_hwmon(data);
Luca Tettamanti8ba406b2010-01-10 20:52:33 +01001392 }
Luca Tettamanti2c03d072009-04-07 15:32:59 +02001393 if (err < 0)
1394 goto out;
1395 if (err == 0) {
1396 dev_info(&device->dev,
1397 "No usable sensor detected, bailing out\n");
1398 err = -ENODEV;
1399 goto out;
1400 }
1401
1402 err = atk_register_hwmon(data);
1403 if (err)
1404 goto cleanup;
1405
Luca Tettamanti7e5eab12010-01-10 20:52:33 +01001406 atk_debugfs_init(data);
1407
Luca Tettamanti2c03d072009-04-07 15:32:59 +02001408 device->driver_data = data;
1409 return 0;
1410cleanup:
1411 atk_free_sensors(data);
1412out:
Luca Tettamanti9e6eba62009-10-09 20:35:18 +02001413 if (data->disable_ec)
1414 atk_ec_ctl(data, 0);
Luca Tettamanti2c03d072009-04-07 15:32:59 +02001415 kfree(data);
1416 return err;
1417}
1418
1419static int atk_remove(struct acpi_device *device, int type)
1420{
1421 struct atk_data *data = device->driver_data;
1422 dev_dbg(&device->dev, "removing...\n");
1423
1424 device->driver_data = NULL;
1425
Luca Tettamanti7e5eab12010-01-10 20:52:33 +01001426 atk_debugfs_cleanup(data);
1427
Luca Tettamanti2c03d072009-04-07 15:32:59 +02001428 atk_remove_files(data);
1429 atk_free_sensors(data);
1430 hwmon_device_unregister(data->hwmon_dev);
1431
Luca Tettamanti9e6eba62009-10-09 20:35:18 +02001432 if (data->disable_ec) {
1433 if (atk_ec_ctl(data, 0))
1434 dev_err(&device->dev, "Failed to disable EC\n");
1435 }
1436
Luca Tettamanti2c03d072009-04-07 15:32:59 +02001437 kfree(data);
1438
1439 return 0;
1440}
1441
1442static int __init atk0110_init(void)
1443{
1444 int ret;
1445
Jean Delvare70dd6be2010-05-27 19:58:37 +02001446 /* Make sure it's safe to access the device through ACPI */
1447 if (!acpi_resources_are_enforced()) {
Joe Perchesac561492010-10-20 06:51:30 +00001448 pr_err("Resources not safely usable due to acpi_enforce_resources kernel parameter\n");
Jean Delvare70dd6be2010-05-27 19:58:37 +02001449 return -EBUSY;
1450 }
1451
Luca Tettamanti86ca33e2011-01-22 16:07:11 +01001452 if (dmi_check_system(atk_force_new_if))
1453 new_if = true;
1454
Luca Tettamanti2c03d072009-04-07 15:32:59 +02001455 ret = acpi_bus_register_driver(&atk_driver);
1456 if (ret)
Joe Perchesac561492010-10-20 06:51:30 +00001457 pr_info("acpi_bus_register_driver failed: %d\n", ret);
Luca Tettamanti2c03d072009-04-07 15:32:59 +02001458
1459 return ret;
1460}
1461
1462static void __exit atk0110_exit(void)
1463{
1464 acpi_bus_unregister_driver(&atk_driver);
1465}
1466
1467module_init(atk0110_init);
1468module_exit(atk0110_exit);
1469
1470MODULE_LICENSE("GPL");