acpi_power_meter.c 24 KB

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