acpi_power_meter.c 24 KB

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