power_meter.c 24 KB

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