blob: dc4ffadf81220a2541f68f773514d430491b75b7 [file] [log] [blame]
Darrick J. Wongde584af2009-09-18 12:41:09 -07001/*
2 * A hwmon driver for ACPI 4.0 power meters
3 * Copyright (C) 2009 IBM
4 *
5 * Author: Darrick J. Wong <djwong@us.ibm.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#include <linux/module.h>
23#include <linux/hwmon.h>
24#include <linux/hwmon-sysfs.h>
25#include <linux/jiffies.h>
26#include <linux/mutex.h>
27#include <linux/dmi.h>
28#include <linux/kdev_t.h>
29#include <linux/sched.h>
30#include <linux/time.h>
31#include <acpi/acpi_drivers.h>
32#include <acpi/acpi_bus.h>
33
34#define ACPI_POWER_METER_NAME "power_meter"
35ACPI_MODULE_NAME(ACPI_POWER_METER_NAME);
36#define ACPI_POWER_METER_DEVICE_NAME "Power Meter"
37#define ACPI_POWER_METER_CLASS "power_meter_resource"
38
39#define NUM_SENSORS 17
40
41#define POWER_METER_CAN_MEASURE (1 << 0)
42#define POWER_METER_CAN_TRIP (1 << 1)
43#define POWER_METER_CAN_CAP (1 << 2)
44#define POWER_METER_CAN_NOTIFY (1 << 3)
45#define POWER_METER_IS_BATTERY (1 << 8)
46#define UNKNOWN_HYSTERESIS 0xFFFFFFFF
47
48#define METER_NOTIFY_CONFIG 0x80
49#define METER_NOTIFY_TRIP 0x81
50#define METER_NOTIFY_CAP 0x82
51#define METER_NOTIFY_CAPPING 0x83
52#define METER_NOTIFY_INTERVAL 0x84
53
54#define POWER_AVERAGE_NAME "power1_average"
55#define POWER_CAP_NAME "power1_cap"
56#define POWER_AVG_INTERVAL_NAME "power1_average_interval"
57#define POWER_ALARM_NAME "power1_alarm"
58
59static int cap_in_hardware;
60static int force_cap_on;
61
62static int can_cap_in_hardware(void)
63{
64 return force_cap_on || cap_in_hardware;
65}
66
Márton Némethc97adf92010-01-10 17:15:36 +010067static const struct acpi_device_id power_meter_ids[] = {
Darrick J. Wongde584af2009-09-18 12:41:09 -070068 {"ACPI000D", 0},
69 {"", 0},
70};
71MODULE_DEVICE_TABLE(acpi, power_meter_ids);
72
73struct acpi_power_meter_capabilities {
74 acpi_integer flags;
75 acpi_integer units;
76 acpi_integer type;
77 acpi_integer accuracy;
78 acpi_integer sampling_time;
79 acpi_integer min_avg_interval;
80 acpi_integer max_avg_interval;
81 acpi_integer hysteresis;
82 acpi_integer configurable_cap;
83 acpi_integer min_cap;
84 acpi_integer max_cap;
85};
86
87struct acpi_power_meter_resource {
88 struct acpi_device *acpi_dev;
89 acpi_bus_id name;
90 struct mutex lock;
91 struct device *hwmon_dev;
92 struct acpi_power_meter_capabilities caps;
93 acpi_string model_number;
94 acpi_string serial_number;
95 acpi_string oem_info;
96 acpi_integer power;
97 acpi_integer cap;
98 acpi_integer avg_interval;
99 int sensors_valid;
100 unsigned long sensors_last_updated;
101 struct sensor_device_attribute sensors[NUM_SENSORS];
102 int num_sensors;
103 int trip[2];
104 int num_domain_devices;
105 struct acpi_device **domain_devices;
106 struct kobject *holders_dir;
107};
108
109struct ro_sensor_template {
110 char *label;
111 ssize_t (*show)(struct device *dev,
112 struct device_attribute *devattr,
113 char *buf);
114 int index;
115};
116
117struct rw_sensor_template {
118 char *label;
119 ssize_t (*show)(struct device *dev,
120 struct device_attribute *devattr,
121 char *buf);
122 ssize_t (*set)(struct device *dev,
123 struct device_attribute *devattr,
124 const char *buf, size_t count);
125 int index;
126};
127
128/* Averaging interval */
129static int update_avg_interval(struct acpi_power_meter_resource *resource)
130{
131 unsigned long long data;
132 acpi_status status;
133
134 status = acpi_evaluate_integer(resource->acpi_dev->handle, "_GAI",
135 NULL, &data);
136 if (ACPI_FAILURE(status)) {
137 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _GAI"));
138 return -ENODEV;
139 }
140
141 resource->avg_interval = data;
142 return 0;
143}
144
145static ssize_t show_avg_interval(struct device *dev,
146 struct device_attribute *devattr,
147 char *buf)
148{
149 struct acpi_device *acpi_dev = to_acpi_device(dev);
150 struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
151
152 mutex_lock(&resource->lock);
153 update_avg_interval(resource);
154 mutex_unlock(&resource->lock);
155
156 return sprintf(buf, "%llu\n", resource->avg_interval);
157}
158
159static ssize_t set_avg_interval(struct device *dev,
160 struct device_attribute *devattr,
161 const char *buf, size_t count)
162{
163 struct acpi_device *acpi_dev = to_acpi_device(dev);
164 struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
165 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
166 struct acpi_object_list args = { 1, &arg0 };
167 int res;
168 unsigned long temp;
169 unsigned long long data;
170 acpi_status status;
171
172 res = strict_strtoul(buf, 10, &temp);
173 if (res)
174 return res;
175
176 if (temp > resource->caps.max_avg_interval ||
177 temp < resource->caps.min_avg_interval)
178 return -EINVAL;
179 arg0.integer.value = temp;
180
181 mutex_lock(&resource->lock);
182 status = acpi_evaluate_integer(resource->acpi_dev->handle, "_PAI",
183 &args, &data);
184 if (!ACPI_FAILURE(status))
185 resource->avg_interval = temp;
186 mutex_unlock(&resource->lock);
187
188 if (ACPI_FAILURE(status)) {
189 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PAI"));
190 return -EINVAL;
191 }
192
193 /* _PAI returns 0 on success, nonzero otherwise */
194 if (data)
195 return -EINVAL;
196
197 return count;
198}
199
200/* Cap functions */
201static int update_cap(struct acpi_power_meter_resource *resource)
202{
203 unsigned long long data;
204 acpi_status status;
205
206 status = acpi_evaluate_integer(resource->acpi_dev->handle, "_GHL",
207 NULL, &data);
208 if (ACPI_FAILURE(status)) {
209 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _GHL"));
210 return -ENODEV;
211 }
212
213 resource->cap = data;
214 return 0;
215}
216
217static ssize_t show_cap(struct device *dev,
218 struct device_attribute *devattr,
219 char *buf)
220{
221 struct acpi_device *acpi_dev = to_acpi_device(dev);
222 struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
223
224 mutex_lock(&resource->lock);
225 update_cap(resource);
226 mutex_unlock(&resource->lock);
227
228 return sprintf(buf, "%llu\n", resource->cap * 1000);
229}
230
231static ssize_t set_cap(struct device *dev, struct device_attribute *devattr,
232 const char *buf, size_t count)
233{
234 struct acpi_device *acpi_dev = to_acpi_device(dev);
235 struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
236 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
237 struct acpi_object_list args = { 1, &arg0 };
238 int res;
239 unsigned long temp;
240 unsigned long long data;
241 acpi_status status;
242
243 res = strict_strtoul(buf, 10, &temp);
244 if (res)
245 return res;
246
247 temp /= 1000;
248 if (temp > resource->caps.max_cap || temp < resource->caps.min_cap)
249 return -EINVAL;
250 arg0.integer.value = temp;
251
252 mutex_lock(&resource->lock);
253 status = acpi_evaluate_integer(resource->acpi_dev->handle, "_SHL",
254 &args, &data);
255 if (!ACPI_FAILURE(status))
256 resource->cap = temp;
257 mutex_unlock(&resource->lock);
258
259 if (ACPI_FAILURE(status)) {
260 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _SHL"));
261 return -EINVAL;
262 }
263
264 /* _SHL returns 0 on success, nonzero otherwise */
265 if (data)
266 return -EINVAL;
267
268 return count;
269}
270
271/* Power meter trip points */
272static int set_acpi_trip(struct acpi_power_meter_resource *resource)
273{
274 union acpi_object arg_objs[] = {
275 {ACPI_TYPE_INTEGER},
276 {ACPI_TYPE_INTEGER}
277 };
278 struct acpi_object_list args = { 2, arg_objs };
279 unsigned long long data;
280 acpi_status status;
281
282 /* Both trip levels must be set */
283 if (resource->trip[0] < 0 || resource->trip[1] < 0)
284 return 0;
285
286 /* This driver stores min, max; ACPI wants max, min. */
287 arg_objs[0].integer.value = resource->trip[1];
288 arg_objs[1].integer.value = resource->trip[0];
289
290 status = acpi_evaluate_integer(resource->acpi_dev->handle, "_PTP",
291 &args, &data);
292 if (ACPI_FAILURE(status)) {
293 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PTP"));
294 return -EINVAL;
295 }
296
Darrick J. Wong22aeceb2009-10-21 18:01:37 -0700297 /* _PTP returns 0 on success, nonzero otherwise */
298 if (data)
299 return -EINVAL;
300
301 return 0;
Darrick J. Wongde584af2009-09-18 12:41:09 -0700302}
303
304static ssize_t set_trip(struct device *dev, struct device_attribute *devattr,
305 const char *buf, size_t count)
306{
307 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
308 struct acpi_device *acpi_dev = to_acpi_device(dev);
309 struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
310 int res;
311 unsigned long temp;
312
313 res = strict_strtoul(buf, 10, &temp);
314 if (res)
315 return res;
316
317 temp /= 1000;
318 if (temp < 0)
319 return -EINVAL;
320
321 mutex_lock(&resource->lock);
322 resource->trip[attr->index - 7] = temp;
323 res = set_acpi_trip(resource);
324 mutex_unlock(&resource->lock);
325
326 if (res)
327 return res;
328
329 return count;
330}
331
332/* Power meter */
333static int update_meter(struct acpi_power_meter_resource *resource)
334{
335 unsigned long long data;
336 acpi_status status;
337 unsigned long local_jiffies = jiffies;
338
339 if (time_before(local_jiffies, resource->sensors_last_updated +
340 msecs_to_jiffies(resource->caps.sampling_time)) &&
341 resource->sensors_valid)
342 return 0;
343
344 status = acpi_evaluate_integer(resource->acpi_dev->handle, "_PMM",
345 NULL, &data);
346 if (ACPI_FAILURE(status)) {
347 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PMM"));
348 return -ENODEV;
349 }
350
351 resource->power = data;
352 resource->sensors_valid = 1;
353 resource->sensors_last_updated = jiffies;
354 return 0;
355}
356
357static ssize_t show_power(struct device *dev,
358 struct device_attribute *devattr,
359 char *buf)
360{
361 struct acpi_device *acpi_dev = to_acpi_device(dev);
362 struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
363
364 mutex_lock(&resource->lock);
365 update_meter(resource);
366 mutex_unlock(&resource->lock);
367
368 return sprintf(buf, "%llu\n", resource->power * 1000);
369}
370
371/* Miscellaneous */
372static ssize_t show_str(struct device *dev,
373 struct device_attribute *devattr,
374 char *buf)
375{
376 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
377 struct acpi_device *acpi_dev = to_acpi_device(dev);
378 struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
379 acpi_string val;
380
381 switch (attr->index) {
382 case 0:
383 val = resource->model_number;
384 break;
385 case 1:
386 val = resource->serial_number;
387 break;
388 case 2:
389 val = resource->oem_info;
390 break;
391 default:
392 BUG();
393 }
394
395 return sprintf(buf, "%s\n", val);
396}
397
398static ssize_t show_val(struct device *dev,
399 struct device_attribute *devattr,
400 char *buf)
401{
402 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
403 struct acpi_device *acpi_dev = to_acpi_device(dev);
404 struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
405 acpi_integer val = 0;
406
407 switch (attr->index) {
408 case 0:
409 val = resource->caps.min_avg_interval;
410 break;
411 case 1:
412 val = resource->caps.max_avg_interval;
413 break;
414 case 2:
415 val = resource->caps.min_cap * 1000;
416 break;
417 case 3:
418 val = resource->caps.max_cap * 1000;
419 break;
420 case 4:
421 if (resource->caps.hysteresis == UNKNOWN_HYSTERESIS)
422 return sprintf(buf, "unknown\n");
423
424 val = resource->caps.hysteresis * 1000;
425 break;
426 case 5:
427 if (resource->caps.flags & POWER_METER_IS_BATTERY)
428 val = 1;
429 else
430 val = 0;
431 break;
432 case 6:
433 if (resource->power > resource->cap)
434 val = 1;
435 else
436 val = 0;
437 break;
438 case 7:
439 case 8:
440 if (resource->trip[attr->index - 7] < 0)
441 return sprintf(buf, "unknown\n");
442
443 val = resource->trip[attr->index - 7] * 1000;
444 break;
445 default:
446 BUG();
447 }
448
449 return sprintf(buf, "%llu\n", val);
450}
451
452static ssize_t show_accuracy(struct device *dev,
453 struct device_attribute *devattr,
454 char *buf)
455{
456 struct acpi_device *acpi_dev = to_acpi_device(dev);
457 struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
458 unsigned int acc = resource->caps.accuracy;
459
460 return sprintf(buf, "%u.%u%%\n", acc / 1000, acc % 1000);
461}
462
463static ssize_t show_name(struct device *dev,
464 struct device_attribute *devattr,
465 char *buf)
466{
467 return sprintf(buf, "%s\n", ACPI_POWER_METER_NAME);
468}
469
470/* Sensor descriptions. If you add a sensor, update NUM_SENSORS above! */
471static struct ro_sensor_template meter_ro_attrs[] = {
472{POWER_AVERAGE_NAME, show_power, 0},
473{"power1_accuracy", show_accuracy, 0},
474{"power1_average_interval_min", show_val, 0},
475{"power1_average_interval_max", show_val, 1},
476{"power1_is_battery", show_val, 5},
477{NULL, NULL, 0},
478};
479
480static struct rw_sensor_template meter_rw_attrs[] = {
481{POWER_AVG_INTERVAL_NAME, show_avg_interval, set_avg_interval, 0},
482{NULL, NULL, NULL, 0},
483};
484
485static struct ro_sensor_template misc_cap_attrs[] = {
486{"power1_cap_min", show_val, 2},
487{"power1_cap_max", show_val, 3},
488{"power1_cap_hyst", show_val, 4},
489{POWER_ALARM_NAME, show_val, 6},
490{NULL, NULL, 0},
491};
492
493static struct ro_sensor_template ro_cap_attrs[] = {
494{POWER_CAP_NAME, show_cap, 0},
495{NULL, NULL, 0},
496};
497
498static struct rw_sensor_template rw_cap_attrs[] = {
499{POWER_CAP_NAME, show_cap, set_cap, 0},
500{NULL, NULL, NULL, 0},
501};
502
503static struct rw_sensor_template trip_attrs[] = {
504{"power1_average_min", show_val, set_trip, 7},
505{"power1_average_max", show_val, set_trip, 8},
506{NULL, NULL, NULL, 0},
507};
508
509static struct ro_sensor_template misc_attrs[] = {
510{"name", show_name, 0},
511{"power1_model_number", show_str, 0},
512{"power1_oem_info", show_str, 2},
513{"power1_serial_number", show_str, 1},
514{NULL, NULL, 0},
515};
516
517/* Read power domain data */
518static void remove_domain_devices(struct acpi_power_meter_resource *resource)
519{
520 int i;
521
522 if (!resource->num_domain_devices)
523 return;
524
525 for (i = 0; i < resource->num_domain_devices; i++) {
526 struct acpi_device *obj = resource->domain_devices[i];
527 if (!obj)
528 continue;
529
530 sysfs_remove_link(resource->holders_dir,
531 kobject_name(&obj->dev.kobj));
532 put_device(&obj->dev);
533 }
534
535 kfree(resource->domain_devices);
536 kobject_put(resource->holders_dir);
Darren Jenkins7f07a602010-01-12 23:37:07 +1100537 resource->num_domain_devices = 0;
Darrick J. Wongde584af2009-09-18 12:41:09 -0700538}
539
540static int read_domain_devices(struct acpi_power_meter_resource *resource)
541{
542 int res = 0;
543 int i;
544 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
545 union acpi_object *pss;
546 acpi_status status;
547
548 status = acpi_evaluate_object(resource->acpi_dev->handle, "_PMD", NULL,
549 &buffer);
550 if (ACPI_FAILURE(status)) {
551 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PMD"));
552 return -ENODEV;
553 }
554
555 pss = buffer.pointer;
556 if (!pss ||
557 pss->type != ACPI_TYPE_PACKAGE) {
558 dev_err(&resource->acpi_dev->dev, ACPI_POWER_METER_NAME
559 "Invalid _PMD data\n");
560 res = -EFAULT;
561 goto end;
562 }
563
564 if (!pss->package.count)
565 goto end;
566
567 resource->domain_devices = kzalloc(sizeof(struct acpi_device *) *
568 pss->package.count, GFP_KERNEL);
569 if (!resource->domain_devices) {
570 res = -ENOMEM;
571 goto end;
572 }
573
574 resource->holders_dir = kobject_create_and_add("measures",
575 &resource->acpi_dev->dev.kobj);
576 if (!resource->holders_dir) {
577 res = -ENOMEM;
578 goto exit_free;
579 }
580
581 resource->num_domain_devices = pss->package.count;
582
583 for (i = 0; i < pss->package.count; i++) {
584 struct acpi_device *obj;
585 union acpi_object *element = &(pss->package.elements[i]);
586
587 /* Refuse non-references */
588 if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
589 continue;
590
591 /* Create a symlink to domain objects */
592 resource->domain_devices[i] = NULL;
593 status = acpi_bus_get_device(element->reference.handle,
594 &resource->domain_devices[i]);
595 if (ACPI_FAILURE(status))
596 continue;
597
598 obj = resource->domain_devices[i];
599 get_device(&obj->dev);
600
601 res = sysfs_create_link(resource->holders_dir, &obj->dev.kobj,
602 kobject_name(&obj->dev.kobj));
603 if (res) {
604 put_device(&obj->dev);
605 resource->domain_devices[i] = NULL;
606 }
607 }
608
609 res = 0;
610 goto end;
611
612exit_free:
613 kfree(resource->domain_devices);
614end:
615 kfree(buffer.pointer);
616 return res;
617}
618
619/* Registration and deregistration */
620static int register_ro_attrs(struct acpi_power_meter_resource *resource,
621 struct ro_sensor_template *ro)
622{
623 struct device *dev = &resource->acpi_dev->dev;
624 struct sensor_device_attribute *sensors =
625 &resource->sensors[resource->num_sensors];
626 int res = 0;
627
628 while (ro->label) {
629 sensors->dev_attr.attr.name = ro->label;
630 sensors->dev_attr.attr.mode = S_IRUGO;
631 sensors->dev_attr.show = ro->show;
632 sensors->index = ro->index;
633
634 res = device_create_file(dev, &sensors->dev_attr);
635 if (res) {
636 sensors->dev_attr.attr.name = NULL;
637 goto error;
638 }
639 sensors++;
640 resource->num_sensors++;
641 ro++;
642 }
643
644error:
645 return res;
646}
647
648static int register_rw_attrs(struct acpi_power_meter_resource *resource,
649 struct rw_sensor_template *rw)
650{
651 struct device *dev = &resource->acpi_dev->dev;
652 struct sensor_device_attribute *sensors =
653 &resource->sensors[resource->num_sensors];
654 int res = 0;
655
656 while (rw->label) {
657 sensors->dev_attr.attr.name = rw->label;
658 sensors->dev_attr.attr.mode = S_IRUGO | S_IWUSR;
659 sensors->dev_attr.show = rw->show;
660 sensors->dev_attr.store = rw->set;
661 sensors->index = rw->index;
662
663 res = device_create_file(dev, &sensors->dev_attr);
664 if (res) {
665 sensors->dev_attr.attr.name = NULL;
666 goto error;
667 }
668 sensors++;
669 resource->num_sensors++;
670 rw++;
671 }
672
673error:
674 return res;
675}
676
677static void remove_attrs(struct acpi_power_meter_resource *resource)
678{
679 int i;
680
681 for (i = 0; i < resource->num_sensors; i++) {
682 if (!resource->sensors[i].dev_attr.attr.name)
683 continue;
684 device_remove_file(&resource->acpi_dev->dev,
685 &resource->sensors[i].dev_attr);
686 }
687
688 remove_domain_devices(resource);
689
690 resource->num_sensors = 0;
691}
692
693static int setup_attrs(struct acpi_power_meter_resource *resource)
694{
695 int res = 0;
696
697 res = read_domain_devices(resource);
698 if (res)
699 return res;
700
701 if (resource->caps.flags & POWER_METER_CAN_MEASURE) {
702 res = register_ro_attrs(resource, meter_ro_attrs);
703 if (res)
704 goto error;
705 res = register_rw_attrs(resource, meter_rw_attrs);
706 if (res)
707 goto error;
708 }
709
710 if (resource->caps.flags & POWER_METER_CAN_CAP) {
711 if (!can_cap_in_hardware()) {
712 dev_err(&resource->acpi_dev->dev,
713 "Ignoring unsafe software power cap!\n");
714 goto skip_unsafe_cap;
715 }
716
717 if (resource->caps.configurable_cap) {
718 res = register_rw_attrs(resource, rw_cap_attrs);
719 if (res)
720 goto error;
721 } else {
722 res = register_ro_attrs(resource, ro_cap_attrs);
723 if (res)
724 goto error;
725 }
726 res = register_ro_attrs(resource, misc_cap_attrs);
727 if (res)
728 goto error;
729 }
730skip_unsafe_cap:
731
732 if (resource->caps.flags & POWER_METER_CAN_TRIP) {
733 res = register_rw_attrs(resource, trip_attrs);
734 if (res)
735 goto error;
736 }
737
738 res = register_ro_attrs(resource, misc_attrs);
739 if (res)
740 goto error;
741
742 return res;
743error:
Darrick J. Wongde584af2009-09-18 12:41:09 -0700744 remove_attrs(resource);
745 return res;
746}
747
748static void free_capabilities(struct acpi_power_meter_resource *resource)
749{
750 acpi_string *str;
751 int i;
752
753 str = &resource->model_number;
754 for (i = 0; i < 3; i++, str++)
755 kfree(*str);
756}
757
758static int read_capabilities(struct acpi_power_meter_resource *resource)
759{
760 int res = 0;
761 int i;
762 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
763 struct acpi_buffer state = { 0, NULL };
764 struct acpi_buffer format = { sizeof("NNNNNNNNNNN"), "NNNNNNNNNNN" };
765 union acpi_object *pss;
766 acpi_string *str;
767 acpi_status status;
768
769 status = acpi_evaluate_object(resource->acpi_dev->handle, "_PMC", NULL,
770 &buffer);
771 if (ACPI_FAILURE(status)) {
772 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PMC"));
773 return -ENODEV;
774 }
775
776 pss = buffer.pointer;
777 if (!pss ||
778 pss->type != ACPI_TYPE_PACKAGE ||
779 pss->package.count != 14) {
780 dev_err(&resource->acpi_dev->dev, ACPI_POWER_METER_NAME
781 "Invalid _PMC data\n");
782 res = -EFAULT;
783 goto end;
784 }
785
786 /* Grab all the integer data at once */
787 state.length = sizeof(struct acpi_power_meter_capabilities);
788 state.pointer = &resource->caps;
789
790 status = acpi_extract_package(pss, &format, &state);
791 if (ACPI_FAILURE(status)) {
792 ACPI_EXCEPTION((AE_INFO, status, "Invalid data"));
793 res = -EFAULT;
794 goto end;
795 }
796
797 if (resource->caps.units) {
798 dev_err(&resource->acpi_dev->dev, ACPI_POWER_METER_NAME
799 "Unknown units %llu.\n",
800 resource->caps.units);
801 res = -EINVAL;
802 goto end;
803 }
804
805 /* Grab the string data */
806 str = &resource->model_number;
807
808 for (i = 11; i < 14; i++) {
809 union acpi_object *element = &(pss->package.elements[i]);
810
811 if (element->type != ACPI_TYPE_STRING) {
812 res = -EINVAL;
813 goto error;
814 }
815
816 *str = kzalloc(sizeof(u8) * (element->string.length + 1),
817 GFP_KERNEL);
818 if (!*str) {
819 res = -ENOMEM;
820 goto error;
821 }
822
823 strncpy(*str, element->string.pointer, element->string.length);
824 str++;
825 }
826
827 dev_info(&resource->acpi_dev->dev, "Found ACPI power meter.\n");
828 goto end;
829error:
830 str = &resource->model_number;
831 for (i = 0; i < 3; i++, str++)
832 kfree(*str);
833end:
834 kfree(buffer.pointer);
835 return res;
836}
837
838/* Handle ACPI event notifications */
839static void acpi_power_meter_notify(struct acpi_device *device, u32 event)
840{
841 struct acpi_power_meter_resource *resource;
842 int res;
843
844 if (!device || !acpi_driver_data(device))
845 return;
846
847 resource = acpi_driver_data(device);
848
849 mutex_lock(&resource->lock);
850 switch (event) {
851 case METER_NOTIFY_CONFIG:
852 free_capabilities(resource);
853 res = read_capabilities(resource);
854 if (res)
855 break;
856
857 remove_attrs(resource);
858 setup_attrs(resource);
859 break;
860 case METER_NOTIFY_TRIP:
861 sysfs_notify(&device->dev.kobj, NULL, POWER_AVERAGE_NAME);
862 update_meter(resource);
863 break;
864 case METER_NOTIFY_CAP:
865 sysfs_notify(&device->dev.kobj, NULL, POWER_CAP_NAME);
866 update_cap(resource);
867 break;
868 case METER_NOTIFY_INTERVAL:
869 sysfs_notify(&device->dev.kobj, NULL, POWER_AVG_INTERVAL_NAME);
870 update_avg_interval(resource);
871 break;
872 case METER_NOTIFY_CAPPING:
873 sysfs_notify(&device->dev.kobj, NULL, POWER_ALARM_NAME);
874 dev_info(&device->dev, "Capping in progress.\n");
875 break;
876 default:
877 BUG();
878 }
879 mutex_unlock(&resource->lock);
880
881 acpi_bus_generate_netlink_event(ACPI_POWER_METER_CLASS,
882 dev_name(&device->dev), event, 0);
883}
884
885static int acpi_power_meter_add(struct acpi_device *device)
886{
887 int res;
888 struct acpi_power_meter_resource *resource;
889
890 if (!device)
891 return -EINVAL;
892
893 resource = kzalloc(sizeof(struct acpi_power_meter_resource),
894 GFP_KERNEL);
895 if (!resource)
896 return -ENOMEM;
897
898 resource->sensors_valid = 0;
899 resource->acpi_dev = device;
900 mutex_init(&resource->lock);
901 strcpy(acpi_device_name(device), ACPI_POWER_METER_DEVICE_NAME);
902 strcpy(acpi_device_class(device), ACPI_POWER_METER_CLASS);
903 device->driver_data = resource;
904
905 free_capabilities(resource);
906 res = read_capabilities(resource);
907 if (res)
908 goto exit_free;
909
910 resource->trip[0] = resource->trip[1] = -1;
911
912 res = setup_attrs(resource);
913 if (res)
914 goto exit_free;
915
916 resource->hwmon_dev = hwmon_device_register(&device->dev);
917 if (IS_ERR(resource->hwmon_dev)) {
918 res = PTR_ERR(resource->hwmon_dev);
919 goto exit_remove;
920 }
921
922 res = 0;
923 goto exit;
924
925exit_remove:
926 remove_attrs(resource);
927exit_free:
928 kfree(resource);
929exit:
930 return res;
931}
932
933static int acpi_power_meter_remove(struct acpi_device *device, int type)
934{
935 struct acpi_power_meter_resource *resource;
936
937 if (!device || !acpi_driver_data(device))
938 return -EINVAL;
939
940 resource = acpi_driver_data(device);
941 hwmon_device_unregister(resource->hwmon_dev);
942
943 free_capabilities(resource);
944 remove_attrs(resource);
945
946 kfree(resource);
947 return 0;
948}
949
950static int acpi_power_meter_resume(struct acpi_device *device)
951{
952 struct acpi_power_meter_resource *resource;
953
954 if (!device || !acpi_driver_data(device))
955 return -EINVAL;
956
957 resource = acpi_driver_data(device);
958 free_capabilities(resource);
959 read_capabilities(resource);
960
961 return 0;
962}
963
964static struct acpi_driver acpi_power_meter_driver = {
965 .name = "power_meter",
966 .class = ACPI_POWER_METER_CLASS,
967 .ids = power_meter_ids,
968 .ops = {
969 .add = acpi_power_meter_add,
970 .remove = acpi_power_meter_remove,
971 .resume = acpi_power_meter_resume,
972 .notify = acpi_power_meter_notify,
973 },
974};
975
976/* Module init/exit routines */
977static int __init enable_cap_knobs(const struct dmi_system_id *d)
978{
979 cap_in_hardware = 1;
980 return 0;
981}
982
983static struct dmi_system_id __initdata pm_dmi_table[] = {
984 {
985 enable_cap_knobs, "IBM Active Energy Manager",
986 {
987 DMI_MATCH(DMI_SYS_VENDOR, "IBM")
988 },
989 },
990 {}
991};
992
993static int __init acpi_power_meter_init(void)
994{
995 int result;
996
997 if (acpi_disabled)
998 return -ENODEV;
999
1000 dmi_check_system(pm_dmi_table);
1001
1002 result = acpi_bus_register_driver(&acpi_power_meter_driver);
1003 if (result < 0)
1004 return -ENODEV;
1005
1006 return 0;
1007}
1008
1009static void __exit acpi_power_meter_exit(void)
1010{
1011 acpi_bus_unregister_driver(&acpi_power_meter_driver);
1012}
1013
1014MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
1015MODULE_DESCRIPTION("ACPI 4.0 power meter driver");
1016MODULE_LICENSE("GPL");
1017
1018module_param(force_cap_on, bool, 0644);
1019MODULE_PARM_DESC(force_cap_on, "Enable power cap even it is unsafe to do so.");
1020
1021module_init(acpi_power_meter_init);
1022module_exit(acpi_power_meter_exit);