power_meter.c 24 KB

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