acpi_power_meter.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  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 bool 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 = kstrtoul(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 = kstrtoul(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 = kstrtoul(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. val = "";
  335. }
  336. return sprintf(buf, "%s\n", val);
  337. }
  338. static ssize_t show_val(struct device *dev,
  339. struct device_attribute *devattr,
  340. char *buf)
  341. {
  342. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  343. struct acpi_device *acpi_dev = to_acpi_device(dev);
  344. struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
  345. u64 val = 0;
  346. switch (attr->index) {
  347. case 0:
  348. val = resource->caps.min_avg_interval;
  349. break;
  350. case 1:
  351. val = resource->caps.max_avg_interval;
  352. break;
  353. case 2:
  354. val = resource->caps.min_cap * 1000;
  355. break;
  356. case 3:
  357. val = resource->caps.max_cap * 1000;
  358. break;
  359. case 4:
  360. if (resource->caps.hysteresis == UNKNOWN_HYSTERESIS)
  361. return sprintf(buf, "unknown\n");
  362. val = resource->caps.hysteresis * 1000;
  363. break;
  364. case 5:
  365. if (resource->caps.flags & POWER_METER_IS_BATTERY)
  366. val = 1;
  367. else
  368. val = 0;
  369. break;
  370. case 6:
  371. if (resource->power > resource->cap)
  372. val = 1;
  373. else
  374. val = 0;
  375. break;
  376. case 7:
  377. case 8:
  378. if (resource->trip[attr->index - 7] < 0)
  379. return sprintf(buf, "unknown\n");
  380. val = resource->trip[attr->index - 7] * 1000;
  381. break;
  382. default:
  383. BUG();
  384. }
  385. return sprintf(buf, "%llu\n", val);
  386. }
  387. static ssize_t show_accuracy(struct device *dev,
  388. struct device_attribute *devattr,
  389. char *buf)
  390. {
  391. struct acpi_device *acpi_dev = to_acpi_device(dev);
  392. struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
  393. unsigned int acc = resource->caps.accuracy;
  394. return sprintf(buf, "%u.%u%%\n", acc / 1000, acc % 1000);
  395. }
  396. static ssize_t show_name(struct device *dev,
  397. struct device_attribute *devattr,
  398. char *buf)
  399. {
  400. return sprintf(buf, "%s\n", ACPI_POWER_METER_NAME);
  401. }
  402. /* Sensor descriptions. If you add a sensor, update NUM_SENSORS above! */
  403. static struct ro_sensor_template meter_ro_attrs[] = {
  404. {POWER_AVERAGE_NAME, show_power, 0},
  405. {"power1_accuracy", show_accuracy, 0},
  406. {"power1_average_interval_min", show_val, 0},
  407. {"power1_average_interval_max", show_val, 1},
  408. {"power1_is_battery", show_val, 5},
  409. {NULL, NULL, 0},
  410. };
  411. static struct rw_sensor_template meter_rw_attrs[] = {
  412. {POWER_AVG_INTERVAL_NAME, show_avg_interval, set_avg_interval, 0},
  413. {NULL, NULL, NULL, 0},
  414. };
  415. static struct ro_sensor_template misc_cap_attrs[] = {
  416. {"power1_cap_min", show_val, 2},
  417. {"power1_cap_max", show_val, 3},
  418. {"power1_cap_hyst", show_val, 4},
  419. {POWER_ALARM_NAME, show_val, 6},
  420. {NULL, NULL, 0},
  421. };
  422. static struct ro_sensor_template ro_cap_attrs[] = {
  423. {POWER_CAP_NAME, show_cap, 0},
  424. {NULL, NULL, 0},
  425. };
  426. static struct rw_sensor_template rw_cap_attrs[] = {
  427. {POWER_CAP_NAME, show_cap, set_cap, 0},
  428. {NULL, NULL, NULL, 0},
  429. };
  430. static struct rw_sensor_template trip_attrs[] = {
  431. {"power1_average_min", show_val, set_trip, 7},
  432. {"power1_average_max", show_val, set_trip, 8},
  433. {NULL, NULL, NULL, 0},
  434. };
  435. static struct ro_sensor_template misc_attrs[] = {
  436. {"name", show_name, 0},
  437. {"power1_model_number", show_str, 0},
  438. {"power1_oem_info", show_str, 2},
  439. {"power1_serial_number", show_str, 1},
  440. {NULL, NULL, 0},
  441. };
  442. /* Read power domain data */
  443. static void remove_domain_devices(struct acpi_power_meter_resource *resource)
  444. {
  445. int i;
  446. if (!resource->num_domain_devices)
  447. return;
  448. for (i = 0; i < resource->num_domain_devices; i++) {
  449. struct acpi_device *obj = resource->domain_devices[i];
  450. if (!obj)
  451. continue;
  452. sysfs_remove_link(resource->holders_dir,
  453. kobject_name(&obj->dev.kobj));
  454. put_device(&obj->dev);
  455. }
  456. kfree(resource->domain_devices);
  457. kobject_put(resource->holders_dir);
  458. resource->num_domain_devices = 0;
  459. }
  460. static int read_domain_devices(struct acpi_power_meter_resource *resource)
  461. {
  462. int res = 0;
  463. int i;
  464. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  465. union acpi_object *pss;
  466. acpi_status status;
  467. status = acpi_evaluate_object(resource->acpi_dev->handle, "_PMD", NULL,
  468. &buffer);
  469. if (ACPI_FAILURE(status)) {
  470. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PMD"));
  471. return -ENODEV;
  472. }
  473. pss = buffer.pointer;
  474. if (!pss ||
  475. pss->type != ACPI_TYPE_PACKAGE) {
  476. dev_err(&resource->acpi_dev->dev, ACPI_POWER_METER_NAME
  477. "Invalid _PMD data\n");
  478. res = -EFAULT;
  479. goto end;
  480. }
  481. if (!pss->package.count)
  482. goto end;
  483. resource->domain_devices = kzalloc(sizeof(struct acpi_device *) *
  484. pss->package.count, GFP_KERNEL);
  485. if (!resource->domain_devices) {
  486. res = -ENOMEM;
  487. goto end;
  488. }
  489. resource->holders_dir = kobject_create_and_add("measures",
  490. &resource->acpi_dev->dev.kobj);
  491. if (!resource->holders_dir) {
  492. res = -ENOMEM;
  493. goto exit_free;
  494. }
  495. resource->num_domain_devices = pss->package.count;
  496. for (i = 0; i < pss->package.count; i++) {
  497. struct acpi_device *obj;
  498. union acpi_object *element = &(pss->package.elements[i]);
  499. /* Refuse non-references */
  500. if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
  501. continue;
  502. /* Create a symlink to domain objects */
  503. resource->domain_devices[i] = NULL;
  504. status = acpi_bus_get_device(element->reference.handle,
  505. &resource->domain_devices[i]);
  506. if (ACPI_FAILURE(status))
  507. continue;
  508. obj = resource->domain_devices[i];
  509. get_device(&obj->dev);
  510. res = sysfs_create_link(resource->holders_dir, &obj->dev.kobj,
  511. kobject_name(&obj->dev.kobj));
  512. if (res) {
  513. put_device(&obj->dev);
  514. resource->domain_devices[i] = NULL;
  515. }
  516. }
  517. res = 0;
  518. goto end;
  519. exit_free:
  520. kfree(resource->domain_devices);
  521. end:
  522. kfree(buffer.pointer);
  523. return res;
  524. }
  525. /* Registration and deregistration */
  526. static int register_ro_attrs(struct acpi_power_meter_resource *resource,
  527. struct ro_sensor_template *ro)
  528. {
  529. struct device *dev = &resource->acpi_dev->dev;
  530. struct sensor_device_attribute *sensors =
  531. &resource->sensors[resource->num_sensors];
  532. int res = 0;
  533. while (ro->label) {
  534. sensors->dev_attr.attr.name = ro->label;
  535. sensors->dev_attr.attr.mode = S_IRUGO;
  536. sensors->dev_attr.show = ro->show;
  537. sensors->index = ro->index;
  538. sysfs_attr_init(&sensors->dev_attr.attr);
  539. res = device_create_file(dev, &sensors->dev_attr);
  540. if (res) {
  541. sensors->dev_attr.attr.name = NULL;
  542. goto error;
  543. }
  544. sensors++;
  545. resource->num_sensors++;
  546. ro++;
  547. }
  548. error:
  549. return res;
  550. }
  551. static int register_rw_attrs(struct acpi_power_meter_resource *resource,
  552. struct rw_sensor_template *rw)
  553. {
  554. struct device *dev = &resource->acpi_dev->dev;
  555. struct sensor_device_attribute *sensors =
  556. &resource->sensors[resource->num_sensors];
  557. int res = 0;
  558. while (rw->label) {
  559. sensors->dev_attr.attr.name = rw->label;
  560. sensors->dev_attr.attr.mode = S_IRUGO | S_IWUSR;
  561. sensors->dev_attr.show = rw->show;
  562. sensors->dev_attr.store = rw->set;
  563. sensors->index = rw->index;
  564. sysfs_attr_init(&sensors->dev_attr.attr);
  565. res = device_create_file(dev, &sensors->dev_attr);
  566. if (res) {
  567. sensors->dev_attr.attr.name = NULL;
  568. goto error;
  569. }
  570. sensors++;
  571. resource->num_sensors++;
  572. rw++;
  573. }
  574. error:
  575. return res;
  576. }
  577. static void remove_attrs(struct acpi_power_meter_resource *resource)
  578. {
  579. int i;
  580. for (i = 0; i < resource->num_sensors; i++) {
  581. if (!resource->sensors[i].dev_attr.attr.name)
  582. continue;
  583. device_remove_file(&resource->acpi_dev->dev,
  584. &resource->sensors[i].dev_attr);
  585. }
  586. remove_domain_devices(resource);
  587. resource->num_sensors = 0;
  588. }
  589. static int setup_attrs(struct acpi_power_meter_resource *resource)
  590. {
  591. int res = 0;
  592. res = read_domain_devices(resource);
  593. if (res)
  594. return res;
  595. if (resource->caps.flags & POWER_METER_CAN_MEASURE) {
  596. res = register_ro_attrs(resource, meter_ro_attrs);
  597. if (res)
  598. goto error;
  599. res = register_rw_attrs(resource, meter_rw_attrs);
  600. if (res)
  601. goto error;
  602. }
  603. if (resource->caps.flags & POWER_METER_CAN_CAP) {
  604. if (!can_cap_in_hardware()) {
  605. dev_err(&resource->acpi_dev->dev,
  606. "Ignoring unsafe software power cap!\n");
  607. goto skip_unsafe_cap;
  608. }
  609. if (resource->caps.configurable_cap) {
  610. res = register_rw_attrs(resource, rw_cap_attrs);
  611. if (res)
  612. goto error;
  613. } else {
  614. res = register_ro_attrs(resource, ro_cap_attrs);
  615. if (res)
  616. goto error;
  617. }
  618. res = register_ro_attrs(resource, misc_cap_attrs);
  619. if (res)
  620. goto error;
  621. }
  622. skip_unsafe_cap:
  623. if (resource->caps.flags & POWER_METER_CAN_TRIP) {
  624. res = register_rw_attrs(resource, trip_attrs);
  625. if (res)
  626. goto error;
  627. }
  628. res = register_ro_attrs(resource, misc_attrs);
  629. if (res)
  630. goto error;
  631. return res;
  632. error:
  633. remove_attrs(resource);
  634. return res;
  635. }
  636. static void free_capabilities(struct acpi_power_meter_resource *resource)
  637. {
  638. acpi_string *str;
  639. int i;
  640. str = &resource->model_number;
  641. for (i = 0; i < 3; i++, str++)
  642. kfree(*str);
  643. }
  644. static int read_capabilities(struct acpi_power_meter_resource *resource)
  645. {
  646. int res = 0;
  647. int i;
  648. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  649. struct acpi_buffer state = { 0, NULL };
  650. struct acpi_buffer format = { sizeof("NNNNNNNNNNN"), "NNNNNNNNNNN" };
  651. union acpi_object *pss;
  652. acpi_string *str;
  653. acpi_status status;
  654. status = acpi_evaluate_object(resource->acpi_dev->handle, "_PMC", NULL,
  655. &buffer);
  656. if (ACPI_FAILURE(status)) {
  657. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PMC"));
  658. return -ENODEV;
  659. }
  660. pss = buffer.pointer;
  661. if (!pss ||
  662. pss->type != ACPI_TYPE_PACKAGE ||
  663. pss->package.count != 14) {
  664. dev_err(&resource->acpi_dev->dev, ACPI_POWER_METER_NAME
  665. "Invalid _PMC data\n");
  666. res = -EFAULT;
  667. goto end;
  668. }
  669. /* Grab all the integer data at once */
  670. state.length = sizeof(struct acpi_power_meter_capabilities);
  671. state.pointer = &resource->caps;
  672. status = acpi_extract_package(pss, &format, &state);
  673. if (ACPI_FAILURE(status)) {
  674. ACPI_EXCEPTION((AE_INFO, status, "Invalid data"));
  675. res = -EFAULT;
  676. goto end;
  677. }
  678. if (resource->caps.units) {
  679. dev_err(&resource->acpi_dev->dev, ACPI_POWER_METER_NAME
  680. "Unknown units %llu.\n",
  681. resource->caps.units);
  682. res = -EINVAL;
  683. goto end;
  684. }
  685. /* Grab the string data */
  686. str = &resource->model_number;
  687. for (i = 11; i < 14; i++) {
  688. union acpi_object *element = &(pss->package.elements[i]);
  689. if (element->type != ACPI_TYPE_STRING) {
  690. res = -EINVAL;
  691. goto error;
  692. }
  693. *str = kzalloc(sizeof(u8) * (element->string.length + 1),
  694. GFP_KERNEL);
  695. if (!*str) {
  696. res = -ENOMEM;
  697. goto error;
  698. }
  699. strncpy(*str, element->string.pointer, element->string.length);
  700. str++;
  701. }
  702. dev_info(&resource->acpi_dev->dev, "Found ACPI power meter.\n");
  703. goto end;
  704. error:
  705. str = &resource->model_number;
  706. for (i = 0; i < 3; i++, str++)
  707. kfree(*str);
  708. end:
  709. kfree(buffer.pointer);
  710. return res;
  711. }
  712. /* Handle ACPI event notifications */
  713. static void acpi_power_meter_notify(struct acpi_device *device, u32 event)
  714. {
  715. struct acpi_power_meter_resource *resource;
  716. int res;
  717. if (!device || !acpi_driver_data(device))
  718. return;
  719. resource = acpi_driver_data(device);
  720. mutex_lock(&resource->lock);
  721. switch (event) {
  722. case METER_NOTIFY_CONFIG:
  723. free_capabilities(resource);
  724. res = read_capabilities(resource);
  725. if (res)
  726. break;
  727. remove_attrs(resource);
  728. setup_attrs(resource);
  729. break;
  730. case METER_NOTIFY_TRIP:
  731. sysfs_notify(&device->dev.kobj, NULL, POWER_AVERAGE_NAME);
  732. update_meter(resource);
  733. break;
  734. case METER_NOTIFY_CAP:
  735. sysfs_notify(&device->dev.kobj, NULL, POWER_CAP_NAME);
  736. update_cap(resource);
  737. break;
  738. case METER_NOTIFY_INTERVAL:
  739. sysfs_notify(&device->dev.kobj, NULL, POWER_AVG_INTERVAL_NAME);
  740. update_avg_interval(resource);
  741. break;
  742. case METER_NOTIFY_CAPPING:
  743. sysfs_notify(&device->dev.kobj, NULL, POWER_ALARM_NAME);
  744. dev_info(&device->dev, "Capping in progress.\n");
  745. break;
  746. default:
  747. BUG();
  748. }
  749. mutex_unlock(&resource->lock);
  750. acpi_bus_generate_netlink_event(ACPI_POWER_METER_CLASS,
  751. dev_name(&device->dev), event, 0);
  752. }
  753. static int acpi_power_meter_add(struct acpi_device *device)
  754. {
  755. int res;
  756. struct acpi_power_meter_resource *resource;
  757. if (!device)
  758. return -EINVAL;
  759. resource = kzalloc(sizeof(struct acpi_power_meter_resource),
  760. GFP_KERNEL);
  761. if (!resource)
  762. return -ENOMEM;
  763. resource->sensors_valid = 0;
  764. resource->acpi_dev = device;
  765. mutex_init(&resource->lock);
  766. strcpy(acpi_device_name(device), ACPI_POWER_METER_DEVICE_NAME);
  767. strcpy(acpi_device_class(device), ACPI_POWER_METER_CLASS);
  768. device->driver_data = resource;
  769. free_capabilities(resource);
  770. res = read_capabilities(resource);
  771. if (res)
  772. goto exit_free;
  773. resource->trip[0] = resource->trip[1] = -1;
  774. res = setup_attrs(resource);
  775. if (res)
  776. goto exit_free;
  777. resource->hwmon_dev = hwmon_device_register(&device->dev);
  778. if (IS_ERR(resource->hwmon_dev)) {
  779. res = PTR_ERR(resource->hwmon_dev);
  780. goto exit_remove;
  781. }
  782. res = 0;
  783. goto exit;
  784. exit_remove:
  785. remove_attrs(resource);
  786. exit_free:
  787. kfree(resource);
  788. exit:
  789. return res;
  790. }
  791. static int acpi_power_meter_remove(struct acpi_device *device, int type)
  792. {
  793. struct acpi_power_meter_resource *resource;
  794. if (!device || !acpi_driver_data(device))
  795. return -EINVAL;
  796. resource = acpi_driver_data(device);
  797. hwmon_device_unregister(resource->hwmon_dev);
  798. free_capabilities(resource);
  799. remove_attrs(resource);
  800. kfree(resource);
  801. return 0;
  802. }
  803. static int acpi_power_meter_resume(struct acpi_device *device)
  804. {
  805. struct acpi_power_meter_resource *resource;
  806. if (!device || !acpi_driver_data(device))
  807. return -EINVAL;
  808. resource = acpi_driver_data(device);
  809. free_capabilities(resource);
  810. read_capabilities(resource);
  811. return 0;
  812. }
  813. static struct acpi_driver acpi_power_meter_driver = {
  814. .name = "power_meter",
  815. .class = ACPI_POWER_METER_CLASS,
  816. .ids = power_meter_ids,
  817. .ops = {
  818. .add = acpi_power_meter_add,
  819. .remove = acpi_power_meter_remove,
  820. .resume = acpi_power_meter_resume,
  821. .notify = acpi_power_meter_notify,
  822. },
  823. };
  824. /* Module init/exit routines */
  825. static int __init enable_cap_knobs(const struct dmi_system_id *d)
  826. {
  827. cap_in_hardware = 1;
  828. return 0;
  829. }
  830. static struct dmi_system_id __initdata pm_dmi_table[] = {
  831. {
  832. enable_cap_knobs, "IBM Active Energy Manager",
  833. {
  834. DMI_MATCH(DMI_SYS_VENDOR, "IBM")
  835. },
  836. },
  837. {}
  838. };
  839. static int __init acpi_power_meter_init(void)
  840. {
  841. int result;
  842. if (acpi_disabled)
  843. return -ENODEV;
  844. dmi_check_system(pm_dmi_table);
  845. result = acpi_bus_register_driver(&acpi_power_meter_driver);
  846. if (result < 0)
  847. return -ENODEV;
  848. return 0;
  849. }
  850. static void __exit acpi_power_meter_exit(void)
  851. {
  852. acpi_bus_unregister_driver(&acpi_power_meter_driver);
  853. }
  854. MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
  855. MODULE_DESCRIPTION("ACPI 4.0 power meter driver");
  856. MODULE_LICENSE("GPL");
  857. module_param(force_cap_on, bool, 0644);
  858. MODULE_PARM_DESC(force_cap_on, "Enable power cap even it is unsafe to do so.");
  859. module_init(acpi_power_meter_init);
  860. module_exit(acpi_power_meter_exit);