acpi_power_meter.c 24 KB

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