acpi_power_meter.c 24 KB

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