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