acpi_power_meter.c 23 KB

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