acpi_power_meter.c 24 KB

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