0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/module.h>
0010 #include <linux/hwmon.h>
0011 #include <linux/hwmon-sysfs.h>
0012 #include <linux/jiffies.h>
0013 #include <linux/mutex.h>
0014 #include <linux/dmi.h>
0015 #include <linux/slab.h>
0016 #include <linux/kdev_t.h>
0017 #include <linux/sched.h>
0018 #include <linux/time.h>
0019 #include <linux/err.h>
0020 #include <linux/acpi.h>
0021
0022 #define ACPI_POWER_METER_NAME "power_meter"
0023 #define ACPI_POWER_METER_DEVICE_NAME "Power Meter"
0024 #define ACPI_POWER_METER_CLASS "pwr_meter_resource"
0025
0026 #define NUM_SENSORS 17
0027
0028 #define POWER_METER_CAN_MEASURE (1 << 0)
0029 #define POWER_METER_CAN_TRIP (1 << 1)
0030 #define POWER_METER_CAN_CAP (1 << 2)
0031 #define POWER_METER_CAN_NOTIFY (1 << 3)
0032 #define POWER_METER_IS_BATTERY (1 << 8)
0033 #define UNKNOWN_HYSTERESIS 0xFFFFFFFF
0034
0035 #define METER_NOTIFY_CONFIG 0x80
0036 #define METER_NOTIFY_TRIP 0x81
0037 #define METER_NOTIFY_CAP 0x82
0038 #define METER_NOTIFY_CAPPING 0x83
0039 #define METER_NOTIFY_INTERVAL 0x84
0040
0041 #define POWER_AVERAGE_NAME "power1_average"
0042 #define POWER_CAP_NAME "power1_cap"
0043 #define POWER_AVG_INTERVAL_NAME "power1_average_interval"
0044 #define POWER_ALARM_NAME "power1_alarm"
0045
0046 static int cap_in_hardware;
0047 static bool force_cap_on;
0048
0049 static int can_cap_in_hardware(void)
0050 {
0051 return force_cap_on || cap_in_hardware;
0052 }
0053
0054 static const struct acpi_device_id power_meter_ids[] = {
0055 {"ACPI000D", 0},
0056 {"", 0},
0057 };
0058 MODULE_DEVICE_TABLE(acpi, power_meter_ids);
0059
0060 struct acpi_power_meter_capabilities {
0061 u64 flags;
0062 u64 units;
0063 u64 type;
0064 u64 accuracy;
0065 u64 sampling_time;
0066 u64 min_avg_interval;
0067 u64 max_avg_interval;
0068 u64 hysteresis;
0069 u64 configurable_cap;
0070 u64 min_cap;
0071 u64 max_cap;
0072 };
0073
0074 struct acpi_power_meter_resource {
0075 struct acpi_device *acpi_dev;
0076 acpi_bus_id name;
0077 struct mutex lock;
0078 struct device *hwmon_dev;
0079 struct acpi_power_meter_capabilities caps;
0080 acpi_string model_number;
0081 acpi_string serial_number;
0082 acpi_string oem_info;
0083 u64 power;
0084 u64 cap;
0085 u64 avg_interval;
0086 int sensors_valid;
0087 unsigned long sensors_last_updated;
0088 struct sensor_device_attribute sensors[NUM_SENSORS];
0089 int num_sensors;
0090 s64 trip[2];
0091 int num_domain_devices;
0092 struct acpi_device **domain_devices;
0093 struct kobject *holders_dir;
0094 };
0095
0096 struct sensor_template {
0097 char *label;
0098 ssize_t (*show)(struct device *dev,
0099 struct device_attribute *devattr,
0100 char *buf);
0101 ssize_t (*set)(struct device *dev,
0102 struct device_attribute *devattr,
0103 const char *buf, size_t count);
0104 int index;
0105 };
0106
0107
0108 static int update_avg_interval(struct acpi_power_meter_resource *resource)
0109 {
0110 unsigned long long data;
0111 acpi_status status;
0112
0113 status = acpi_evaluate_integer(resource->acpi_dev->handle, "_GAI",
0114 NULL, &data);
0115 if (ACPI_FAILURE(status)) {
0116 acpi_evaluation_failure_warn(resource->acpi_dev->handle, "_GAI",
0117 status);
0118 return -ENODEV;
0119 }
0120
0121 resource->avg_interval = data;
0122 return 0;
0123 }
0124
0125 static ssize_t show_avg_interval(struct device *dev,
0126 struct device_attribute *devattr,
0127 char *buf)
0128 {
0129 struct acpi_device *acpi_dev = to_acpi_device(dev);
0130 struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
0131
0132 mutex_lock(&resource->lock);
0133 update_avg_interval(resource);
0134 mutex_unlock(&resource->lock);
0135
0136 return sprintf(buf, "%llu\n", resource->avg_interval);
0137 }
0138
0139 static ssize_t set_avg_interval(struct device *dev,
0140 struct device_attribute *devattr,
0141 const char *buf, size_t count)
0142 {
0143 struct acpi_device *acpi_dev = to_acpi_device(dev);
0144 struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
0145 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
0146 struct acpi_object_list args = { 1, &arg0 };
0147 int res;
0148 unsigned long temp;
0149 unsigned long long data;
0150 acpi_status status;
0151
0152 res = kstrtoul(buf, 10, &temp);
0153 if (res)
0154 return res;
0155
0156 if (temp > resource->caps.max_avg_interval ||
0157 temp < resource->caps.min_avg_interval)
0158 return -EINVAL;
0159 arg0.integer.value = temp;
0160
0161 mutex_lock(&resource->lock);
0162 status = acpi_evaluate_integer(resource->acpi_dev->handle, "_PAI",
0163 &args, &data);
0164 if (ACPI_SUCCESS(status))
0165 resource->avg_interval = temp;
0166 mutex_unlock(&resource->lock);
0167
0168 if (ACPI_FAILURE(status)) {
0169 acpi_evaluation_failure_warn(resource->acpi_dev->handle, "_PAI",
0170 status);
0171 return -EINVAL;
0172 }
0173
0174
0175 if (data)
0176 return -EINVAL;
0177
0178 return count;
0179 }
0180
0181
0182 static int update_cap(struct acpi_power_meter_resource *resource)
0183 {
0184 unsigned long long data;
0185 acpi_status status;
0186
0187 status = acpi_evaluate_integer(resource->acpi_dev->handle, "_GHL",
0188 NULL, &data);
0189 if (ACPI_FAILURE(status)) {
0190 acpi_evaluation_failure_warn(resource->acpi_dev->handle, "_GHL",
0191 status);
0192 return -ENODEV;
0193 }
0194
0195 resource->cap = data;
0196 return 0;
0197 }
0198
0199 static ssize_t show_cap(struct device *dev,
0200 struct device_attribute *devattr,
0201 char *buf)
0202 {
0203 struct acpi_device *acpi_dev = to_acpi_device(dev);
0204 struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
0205
0206 mutex_lock(&resource->lock);
0207 update_cap(resource);
0208 mutex_unlock(&resource->lock);
0209
0210 return sprintf(buf, "%llu\n", resource->cap * 1000);
0211 }
0212
0213 static ssize_t set_cap(struct device *dev, struct device_attribute *devattr,
0214 const char *buf, size_t count)
0215 {
0216 struct acpi_device *acpi_dev = to_acpi_device(dev);
0217 struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
0218 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
0219 struct acpi_object_list args = { 1, &arg0 };
0220 int res;
0221 unsigned long temp;
0222 unsigned long long data;
0223 acpi_status status;
0224
0225 res = kstrtoul(buf, 10, &temp);
0226 if (res)
0227 return res;
0228
0229 temp = DIV_ROUND_CLOSEST(temp, 1000);
0230 if (temp > resource->caps.max_cap || temp < resource->caps.min_cap)
0231 return -EINVAL;
0232 arg0.integer.value = temp;
0233
0234 mutex_lock(&resource->lock);
0235 status = acpi_evaluate_integer(resource->acpi_dev->handle, "_SHL",
0236 &args, &data);
0237 if (ACPI_SUCCESS(status))
0238 resource->cap = temp;
0239 mutex_unlock(&resource->lock);
0240
0241 if (ACPI_FAILURE(status)) {
0242 acpi_evaluation_failure_warn(resource->acpi_dev->handle, "_SHL",
0243 status);
0244 return -EINVAL;
0245 }
0246
0247
0248 if (data)
0249 return -EINVAL;
0250
0251 return count;
0252 }
0253
0254
0255 static int set_acpi_trip(struct acpi_power_meter_resource *resource)
0256 {
0257 union acpi_object arg_objs[] = {
0258 {ACPI_TYPE_INTEGER},
0259 {ACPI_TYPE_INTEGER}
0260 };
0261 struct acpi_object_list args = { 2, arg_objs };
0262 unsigned long long data;
0263 acpi_status status;
0264
0265
0266 if (resource->trip[0] < 0 || resource->trip[1] < 0)
0267 return 0;
0268
0269
0270 arg_objs[0].integer.value = resource->trip[1];
0271 arg_objs[1].integer.value = resource->trip[0];
0272
0273 status = acpi_evaluate_integer(resource->acpi_dev->handle, "_PTP",
0274 &args, &data);
0275 if (ACPI_FAILURE(status)) {
0276 acpi_evaluation_failure_warn(resource->acpi_dev->handle, "_PTP",
0277 status);
0278 return -EINVAL;
0279 }
0280
0281
0282 if (data)
0283 return -EINVAL;
0284
0285 return 0;
0286 }
0287
0288 static ssize_t set_trip(struct device *dev, struct device_attribute *devattr,
0289 const char *buf, size_t count)
0290 {
0291 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0292 struct acpi_device *acpi_dev = to_acpi_device(dev);
0293 struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
0294 int res;
0295 unsigned long temp;
0296
0297 res = kstrtoul(buf, 10, &temp);
0298 if (res)
0299 return res;
0300
0301 temp = DIV_ROUND_CLOSEST(temp, 1000);
0302
0303 mutex_lock(&resource->lock);
0304 resource->trip[attr->index - 7] = temp;
0305 res = set_acpi_trip(resource);
0306 mutex_unlock(&resource->lock);
0307
0308 if (res)
0309 return res;
0310
0311 return count;
0312 }
0313
0314
0315 static int update_meter(struct acpi_power_meter_resource *resource)
0316 {
0317 unsigned long long data;
0318 acpi_status status;
0319 unsigned long local_jiffies = jiffies;
0320
0321 if (time_before(local_jiffies, resource->sensors_last_updated +
0322 msecs_to_jiffies(resource->caps.sampling_time)) &&
0323 resource->sensors_valid)
0324 return 0;
0325
0326 status = acpi_evaluate_integer(resource->acpi_dev->handle, "_PMM",
0327 NULL, &data);
0328 if (ACPI_FAILURE(status)) {
0329 acpi_evaluation_failure_warn(resource->acpi_dev->handle, "_PMM",
0330 status);
0331 return -ENODEV;
0332 }
0333
0334 resource->power = data;
0335 resource->sensors_valid = 1;
0336 resource->sensors_last_updated = jiffies;
0337 return 0;
0338 }
0339
0340 static ssize_t show_power(struct device *dev,
0341 struct device_attribute *devattr,
0342 char *buf)
0343 {
0344 struct acpi_device *acpi_dev = to_acpi_device(dev);
0345 struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
0346
0347 mutex_lock(&resource->lock);
0348 update_meter(resource);
0349 mutex_unlock(&resource->lock);
0350
0351 return sprintf(buf, "%llu\n", resource->power * 1000);
0352 }
0353
0354
0355 static ssize_t show_str(struct device *dev,
0356 struct device_attribute *devattr,
0357 char *buf)
0358 {
0359 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0360 struct acpi_device *acpi_dev = to_acpi_device(dev);
0361 struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
0362 acpi_string val;
0363 int ret;
0364
0365 mutex_lock(&resource->lock);
0366 switch (attr->index) {
0367 case 0:
0368 val = resource->model_number;
0369 break;
0370 case 1:
0371 val = resource->serial_number;
0372 break;
0373 case 2:
0374 val = resource->oem_info;
0375 break;
0376 default:
0377 WARN(1, "Implementation error: unexpected attribute index %d\n",
0378 attr->index);
0379 val = "";
0380 break;
0381 }
0382 ret = sprintf(buf, "%s\n", val);
0383 mutex_unlock(&resource->lock);
0384 return ret;
0385 }
0386
0387 static ssize_t show_val(struct device *dev,
0388 struct device_attribute *devattr,
0389 char *buf)
0390 {
0391 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0392 struct acpi_device *acpi_dev = to_acpi_device(dev);
0393 struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
0394 u64 val = 0;
0395
0396 switch (attr->index) {
0397 case 0:
0398 val = resource->caps.min_avg_interval;
0399 break;
0400 case 1:
0401 val = resource->caps.max_avg_interval;
0402 break;
0403 case 2:
0404 val = resource->caps.min_cap * 1000;
0405 break;
0406 case 3:
0407 val = resource->caps.max_cap * 1000;
0408 break;
0409 case 4:
0410 if (resource->caps.hysteresis == UNKNOWN_HYSTERESIS)
0411 return sprintf(buf, "unknown\n");
0412
0413 val = resource->caps.hysteresis * 1000;
0414 break;
0415 case 5:
0416 if (resource->caps.flags & POWER_METER_IS_BATTERY)
0417 val = 1;
0418 else
0419 val = 0;
0420 break;
0421 case 6:
0422 if (resource->power > resource->cap)
0423 val = 1;
0424 else
0425 val = 0;
0426 break;
0427 case 7:
0428 case 8:
0429 if (resource->trip[attr->index - 7] < 0)
0430 return sprintf(buf, "unknown\n");
0431
0432 val = resource->trip[attr->index - 7] * 1000;
0433 break;
0434 default:
0435 WARN(1, "Implementation error: unexpected attribute index %d\n",
0436 attr->index);
0437 break;
0438 }
0439
0440 return sprintf(buf, "%llu\n", val);
0441 }
0442
0443 static ssize_t show_accuracy(struct device *dev,
0444 struct device_attribute *devattr,
0445 char *buf)
0446 {
0447 struct acpi_device *acpi_dev = to_acpi_device(dev);
0448 struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
0449 unsigned int acc = resource->caps.accuracy;
0450
0451 return sprintf(buf, "%u.%u%%\n", acc / 1000, acc % 1000);
0452 }
0453
0454 static ssize_t show_name(struct device *dev,
0455 struct device_attribute *devattr,
0456 char *buf)
0457 {
0458 return sprintf(buf, "%s\n", ACPI_POWER_METER_NAME);
0459 }
0460
0461 #define RO_SENSOR_TEMPLATE(_label, _show, _index) \
0462 { \
0463 .label = _label, \
0464 .show = _show, \
0465 .index = _index, \
0466 }
0467
0468 #define RW_SENSOR_TEMPLATE(_label, _show, _set, _index) \
0469 { \
0470 .label = _label, \
0471 .show = _show, \
0472 .set = _set, \
0473 .index = _index, \
0474 }
0475
0476
0477 static struct sensor_template meter_attrs[] = {
0478 RO_SENSOR_TEMPLATE(POWER_AVERAGE_NAME, show_power, 0),
0479 RO_SENSOR_TEMPLATE("power1_accuracy", show_accuracy, 0),
0480 RO_SENSOR_TEMPLATE("power1_average_interval_min", show_val, 0),
0481 RO_SENSOR_TEMPLATE("power1_average_interval_max", show_val, 1),
0482 RO_SENSOR_TEMPLATE("power1_is_battery", show_val, 5),
0483 RW_SENSOR_TEMPLATE(POWER_AVG_INTERVAL_NAME, show_avg_interval,
0484 set_avg_interval, 0),
0485 {},
0486 };
0487
0488 static struct sensor_template misc_cap_attrs[] = {
0489 RO_SENSOR_TEMPLATE("power1_cap_min", show_val, 2),
0490 RO_SENSOR_TEMPLATE("power1_cap_max", show_val, 3),
0491 RO_SENSOR_TEMPLATE("power1_cap_hyst", show_val, 4),
0492 RO_SENSOR_TEMPLATE(POWER_ALARM_NAME, show_val, 6),
0493 {},
0494 };
0495
0496 static struct sensor_template ro_cap_attrs[] = {
0497 RO_SENSOR_TEMPLATE(POWER_CAP_NAME, show_cap, 0),
0498 {},
0499 };
0500
0501 static struct sensor_template rw_cap_attrs[] = {
0502 RW_SENSOR_TEMPLATE(POWER_CAP_NAME, show_cap, set_cap, 0),
0503 {},
0504 };
0505
0506 static struct sensor_template trip_attrs[] = {
0507 RW_SENSOR_TEMPLATE("power1_average_min", show_val, set_trip, 7),
0508 RW_SENSOR_TEMPLATE("power1_average_max", show_val, set_trip, 8),
0509 {},
0510 };
0511
0512 static struct sensor_template misc_attrs[] = {
0513 RO_SENSOR_TEMPLATE("name", show_name, 0),
0514 RO_SENSOR_TEMPLATE("power1_model_number", show_str, 0),
0515 RO_SENSOR_TEMPLATE("power1_oem_info", show_str, 2),
0516 RO_SENSOR_TEMPLATE("power1_serial_number", show_str, 1),
0517 {},
0518 };
0519
0520 #undef RO_SENSOR_TEMPLATE
0521 #undef RW_SENSOR_TEMPLATE
0522
0523
0524 static void remove_domain_devices(struct acpi_power_meter_resource *resource)
0525 {
0526 int i;
0527
0528 if (!resource->num_domain_devices)
0529 return;
0530
0531 for (i = 0; i < resource->num_domain_devices; i++) {
0532 struct acpi_device *obj = resource->domain_devices[i];
0533
0534 if (!obj)
0535 continue;
0536
0537 sysfs_remove_link(resource->holders_dir,
0538 kobject_name(&obj->dev.kobj));
0539 acpi_dev_put(obj);
0540 }
0541
0542 kfree(resource->domain_devices);
0543 kobject_put(resource->holders_dir);
0544 resource->num_domain_devices = 0;
0545 }
0546
0547 static int read_domain_devices(struct acpi_power_meter_resource *resource)
0548 {
0549 int res = 0;
0550 int i;
0551 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
0552 union acpi_object *pss;
0553 acpi_status status;
0554
0555 status = acpi_evaluate_object(resource->acpi_dev->handle, "_PMD", NULL,
0556 &buffer);
0557 if (ACPI_FAILURE(status)) {
0558 acpi_evaluation_failure_warn(resource->acpi_dev->handle, "_PMD",
0559 status);
0560 return -ENODEV;
0561 }
0562
0563 pss = buffer.pointer;
0564 if (!pss ||
0565 pss->type != ACPI_TYPE_PACKAGE) {
0566 dev_err(&resource->acpi_dev->dev, ACPI_POWER_METER_NAME
0567 "Invalid _PMD data\n");
0568 res = -EFAULT;
0569 goto end;
0570 }
0571
0572 if (!pss->package.count)
0573 goto end;
0574
0575 resource->domain_devices = kcalloc(pss->package.count,
0576 sizeof(struct acpi_device *),
0577 GFP_KERNEL);
0578 if (!resource->domain_devices) {
0579 res = -ENOMEM;
0580 goto end;
0581 }
0582
0583 resource->holders_dir = kobject_create_and_add("measures",
0584 &resource->acpi_dev->dev.kobj);
0585 if (!resource->holders_dir) {
0586 res = -ENOMEM;
0587 goto exit_free;
0588 }
0589
0590 resource->num_domain_devices = pss->package.count;
0591
0592 for (i = 0; i < pss->package.count; i++) {
0593 struct acpi_device *obj;
0594 union acpi_object *element = &pss->package.elements[i];
0595
0596
0597 if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
0598 continue;
0599
0600
0601 obj = acpi_bus_get_acpi_device(element->reference.handle);
0602 resource->domain_devices[i] = obj;
0603 if (!obj)
0604 continue;
0605
0606 res = sysfs_create_link(resource->holders_dir, &obj->dev.kobj,
0607 kobject_name(&obj->dev.kobj));
0608 if (res) {
0609 acpi_dev_put(obj);
0610 resource->domain_devices[i] = NULL;
0611 }
0612 }
0613
0614 res = 0;
0615 goto end;
0616
0617 exit_free:
0618 kfree(resource->domain_devices);
0619 end:
0620 kfree(buffer.pointer);
0621 return res;
0622 }
0623
0624
0625 static int register_attrs(struct acpi_power_meter_resource *resource,
0626 struct sensor_template *attrs)
0627 {
0628 struct device *dev = &resource->acpi_dev->dev;
0629 struct sensor_device_attribute *sensors =
0630 &resource->sensors[resource->num_sensors];
0631 int res = 0;
0632
0633 while (attrs->label) {
0634 sensors->dev_attr.attr.name = attrs->label;
0635 sensors->dev_attr.attr.mode = 0444;
0636 sensors->dev_attr.show = attrs->show;
0637 sensors->index = attrs->index;
0638
0639 if (attrs->set) {
0640 sensors->dev_attr.attr.mode |= 0200;
0641 sensors->dev_attr.store = attrs->set;
0642 }
0643
0644 sysfs_attr_init(&sensors->dev_attr.attr);
0645 res = device_create_file(dev, &sensors->dev_attr);
0646 if (res) {
0647 sensors->dev_attr.attr.name = NULL;
0648 goto error;
0649 }
0650 sensors++;
0651 resource->num_sensors++;
0652 attrs++;
0653 }
0654
0655 error:
0656 return res;
0657 }
0658
0659 static void remove_attrs(struct acpi_power_meter_resource *resource)
0660 {
0661 int i;
0662
0663 for (i = 0; i < resource->num_sensors; i++) {
0664 if (!resource->sensors[i].dev_attr.attr.name)
0665 continue;
0666 device_remove_file(&resource->acpi_dev->dev,
0667 &resource->sensors[i].dev_attr);
0668 }
0669
0670 remove_domain_devices(resource);
0671
0672 resource->num_sensors = 0;
0673 }
0674
0675 static int setup_attrs(struct acpi_power_meter_resource *resource)
0676 {
0677 int res = 0;
0678
0679 res = read_domain_devices(resource);
0680 if (res)
0681 return res;
0682
0683 if (resource->caps.flags & POWER_METER_CAN_MEASURE) {
0684 res = register_attrs(resource, meter_attrs);
0685 if (res)
0686 goto error;
0687 }
0688
0689 if (resource->caps.flags & POWER_METER_CAN_CAP) {
0690 if (!can_cap_in_hardware()) {
0691 dev_warn(&resource->acpi_dev->dev,
0692 "Ignoring unsafe software power cap!\n");
0693 goto skip_unsafe_cap;
0694 }
0695
0696 if (resource->caps.configurable_cap)
0697 res = register_attrs(resource, rw_cap_attrs);
0698 else
0699 res = register_attrs(resource, ro_cap_attrs);
0700
0701 if (res)
0702 goto error;
0703
0704 res = register_attrs(resource, misc_cap_attrs);
0705 if (res)
0706 goto error;
0707 }
0708
0709 skip_unsafe_cap:
0710 if (resource->caps.flags & POWER_METER_CAN_TRIP) {
0711 res = register_attrs(resource, trip_attrs);
0712 if (res)
0713 goto error;
0714 }
0715
0716 res = register_attrs(resource, misc_attrs);
0717 if (res)
0718 goto error;
0719
0720 return res;
0721 error:
0722 remove_attrs(resource);
0723 return res;
0724 }
0725
0726 static void free_capabilities(struct acpi_power_meter_resource *resource)
0727 {
0728 acpi_string *str;
0729 int i;
0730
0731 str = &resource->model_number;
0732 for (i = 0; i < 3; i++, str++) {
0733 kfree(*str);
0734 *str = NULL;
0735 }
0736 }
0737
0738 static int read_capabilities(struct acpi_power_meter_resource *resource)
0739 {
0740 int res = 0;
0741 int i;
0742 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
0743 struct acpi_buffer state = { 0, NULL };
0744 struct acpi_buffer format = { sizeof("NNNNNNNNNNN"), "NNNNNNNNNNN" };
0745 union acpi_object *pss;
0746 acpi_string *str;
0747 acpi_status status;
0748
0749 status = acpi_evaluate_object(resource->acpi_dev->handle, "_PMC", NULL,
0750 &buffer);
0751 if (ACPI_FAILURE(status)) {
0752 acpi_evaluation_failure_warn(resource->acpi_dev->handle, "_PMC",
0753 status);
0754 return -ENODEV;
0755 }
0756
0757 pss = buffer.pointer;
0758 if (!pss ||
0759 pss->type != ACPI_TYPE_PACKAGE ||
0760 pss->package.count != 14) {
0761 dev_err(&resource->acpi_dev->dev, ACPI_POWER_METER_NAME
0762 "Invalid _PMC data\n");
0763 res = -EFAULT;
0764 goto end;
0765 }
0766
0767
0768 state.length = sizeof(struct acpi_power_meter_capabilities);
0769 state.pointer = &resource->caps;
0770
0771 status = acpi_extract_package(pss, &format, &state);
0772 if (ACPI_FAILURE(status)) {
0773 dev_err(&resource->acpi_dev->dev, ACPI_POWER_METER_NAME
0774 "_PMC package parsing failed: %s\n",
0775 acpi_format_exception(status));
0776 res = -EFAULT;
0777 goto end;
0778 }
0779
0780 if (resource->caps.units) {
0781 dev_err(&resource->acpi_dev->dev, ACPI_POWER_METER_NAME
0782 "Unknown units %llu.\n",
0783 resource->caps.units);
0784 res = -EINVAL;
0785 goto end;
0786 }
0787
0788
0789 str = &resource->model_number;
0790
0791 for (i = 11; i < 14; i++) {
0792 union acpi_object *element = &pss->package.elements[i];
0793
0794 if (element->type != ACPI_TYPE_STRING) {
0795 res = -EINVAL;
0796 goto error;
0797 }
0798
0799 *str = kcalloc(element->string.length + 1, sizeof(u8),
0800 GFP_KERNEL);
0801 if (!*str) {
0802 res = -ENOMEM;
0803 goto error;
0804 }
0805
0806 strncpy(*str, element->string.pointer, element->string.length);
0807 str++;
0808 }
0809
0810 dev_info(&resource->acpi_dev->dev, "Found ACPI power meter.\n");
0811 goto end;
0812 error:
0813 free_capabilities(resource);
0814 end:
0815 kfree(buffer.pointer);
0816 return res;
0817 }
0818
0819
0820 static void acpi_power_meter_notify(struct acpi_device *device, u32 event)
0821 {
0822 struct acpi_power_meter_resource *resource;
0823 int res;
0824
0825 if (!device || !acpi_driver_data(device))
0826 return;
0827
0828 resource = acpi_driver_data(device);
0829
0830 switch (event) {
0831 case METER_NOTIFY_CONFIG:
0832 mutex_lock(&resource->lock);
0833 free_capabilities(resource);
0834 res = read_capabilities(resource);
0835 mutex_unlock(&resource->lock);
0836 if (res)
0837 break;
0838
0839 remove_attrs(resource);
0840 setup_attrs(resource);
0841 break;
0842 case METER_NOTIFY_TRIP:
0843 sysfs_notify(&device->dev.kobj, NULL, POWER_AVERAGE_NAME);
0844 break;
0845 case METER_NOTIFY_CAP:
0846 sysfs_notify(&device->dev.kobj, NULL, POWER_CAP_NAME);
0847 break;
0848 case METER_NOTIFY_INTERVAL:
0849 sysfs_notify(&device->dev.kobj, NULL, POWER_AVG_INTERVAL_NAME);
0850 break;
0851 case METER_NOTIFY_CAPPING:
0852 sysfs_notify(&device->dev.kobj, NULL, POWER_ALARM_NAME);
0853 dev_info(&device->dev, "Capping in progress.\n");
0854 break;
0855 default:
0856 WARN(1, "Unexpected event %d\n", event);
0857 break;
0858 }
0859
0860 acpi_bus_generate_netlink_event(ACPI_POWER_METER_CLASS,
0861 dev_name(&device->dev), event, 0);
0862 }
0863
0864 static int acpi_power_meter_add(struct acpi_device *device)
0865 {
0866 int res;
0867 struct acpi_power_meter_resource *resource;
0868
0869 if (!device)
0870 return -EINVAL;
0871
0872 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
0873 if (!resource)
0874 return -ENOMEM;
0875
0876 resource->sensors_valid = 0;
0877 resource->acpi_dev = device;
0878 mutex_init(&resource->lock);
0879 strcpy(acpi_device_name(device), ACPI_POWER_METER_DEVICE_NAME);
0880 strcpy(acpi_device_class(device), ACPI_POWER_METER_CLASS);
0881 device->driver_data = resource;
0882
0883 res = read_capabilities(resource);
0884 if (res)
0885 goto exit_free;
0886
0887 resource->trip[0] = -1;
0888 resource->trip[1] = -1;
0889
0890 res = setup_attrs(resource);
0891 if (res)
0892 goto exit_free_capability;
0893
0894 resource->hwmon_dev = hwmon_device_register(&device->dev);
0895 if (IS_ERR(resource->hwmon_dev)) {
0896 res = PTR_ERR(resource->hwmon_dev);
0897 goto exit_remove;
0898 }
0899
0900 res = 0;
0901 goto exit;
0902
0903 exit_remove:
0904 remove_attrs(resource);
0905 exit_free_capability:
0906 free_capabilities(resource);
0907 exit_free:
0908 kfree(resource);
0909 exit:
0910 return res;
0911 }
0912
0913 static int acpi_power_meter_remove(struct acpi_device *device)
0914 {
0915 struct acpi_power_meter_resource *resource;
0916
0917 if (!device || !acpi_driver_data(device))
0918 return -EINVAL;
0919
0920 resource = acpi_driver_data(device);
0921 hwmon_device_unregister(resource->hwmon_dev);
0922
0923 remove_attrs(resource);
0924 free_capabilities(resource);
0925
0926 kfree(resource);
0927 return 0;
0928 }
0929
0930 #ifdef CONFIG_PM_SLEEP
0931
0932 static int acpi_power_meter_resume(struct device *dev)
0933 {
0934 struct acpi_power_meter_resource *resource;
0935
0936 if (!dev)
0937 return -EINVAL;
0938
0939 resource = acpi_driver_data(to_acpi_device(dev));
0940 if (!resource)
0941 return -EINVAL;
0942
0943 free_capabilities(resource);
0944 read_capabilities(resource);
0945
0946 return 0;
0947 }
0948
0949 #endif
0950
0951 static SIMPLE_DEV_PM_OPS(acpi_power_meter_pm, NULL, acpi_power_meter_resume);
0952
0953 static struct acpi_driver acpi_power_meter_driver = {
0954 .name = "power_meter",
0955 .class = ACPI_POWER_METER_CLASS,
0956 .ids = power_meter_ids,
0957 .ops = {
0958 .add = acpi_power_meter_add,
0959 .remove = acpi_power_meter_remove,
0960 .notify = acpi_power_meter_notify,
0961 },
0962 .drv.pm = &acpi_power_meter_pm,
0963 };
0964
0965
0966 static int __init enable_cap_knobs(const struct dmi_system_id *d)
0967 {
0968 cap_in_hardware = 1;
0969 return 0;
0970 }
0971
0972 static const struct dmi_system_id pm_dmi_table[] __initconst = {
0973 {
0974 enable_cap_knobs, "IBM Active Energy Manager",
0975 {
0976 DMI_MATCH(DMI_SYS_VENDOR, "IBM")
0977 },
0978 },
0979 {}
0980 };
0981
0982 static int __init acpi_power_meter_init(void)
0983 {
0984 int result;
0985
0986 if (acpi_disabled)
0987 return -ENODEV;
0988
0989 dmi_check_system(pm_dmi_table);
0990
0991 result = acpi_bus_register_driver(&acpi_power_meter_driver);
0992 if (result < 0)
0993 return result;
0994
0995 return 0;
0996 }
0997
0998 static void __exit acpi_power_meter_exit(void)
0999 {
1000 acpi_bus_unregister_driver(&acpi_power_meter_driver);
1001 }
1002
1003 MODULE_AUTHOR("Darrick J. Wong <darrick.wong@oracle.com>");
1004 MODULE_DESCRIPTION("ACPI 4.0 power meter driver");
1005 MODULE_LICENSE("GPL");
1006
1007 module_param(force_cap_on, bool, 0644);
1008 MODULE_PARM_DESC(force_cap_on, "Enable power cap even it is unsafe to do so.");
1009
1010 module_init(acpi_power_meter_init);
1011 module_exit(acpi_power_meter_exit);