acpi_power_meter.c 24 KB

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