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