thermal_sysfs.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. /*
  2. * thermal.c - sysfs interface of thermal devices
  3. *
  4. * Copyright (C) 2016 Eduardo Valentin <edubezval@gmail.com>
  5. *
  6. * Highly based on original thermal_core.c
  7. * Copyright (C) 2008 Intel Corp
  8. * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
  9. * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; version 2 of the License.
  14. */
  15. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/sysfs.h>
  17. #include <linux/device.h>
  18. #include <linux/err.h>
  19. #include <linux/slab.h>
  20. #include <linux/string.h>
  21. #include "thermal_core.h"
  22. /* sys I/F for thermal zone */
  23. static ssize_t
  24. type_show(struct device *dev, struct device_attribute *attr, char *buf)
  25. {
  26. struct thermal_zone_device *tz = to_thermal_zone(dev);
  27. return sprintf(buf, "%s\n", tz->type);
  28. }
  29. static ssize_t
  30. temp_show(struct device *dev, struct device_attribute *attr, char *buf)
  31. {
  32. struct thermal_zone_device *tz = to_thermal_zone(dev);
  33. int temperature, ret;
  34. ret = thermal_zone_get_temp(tz, &temperature);
  35. if (ret)
  36. return ret;
  37. return sprintf(buf, "%d\n", temperature);
  38. }
  39. static ssize_t
  40. mode_show(struct device *dev, struct device_attribute *attr, char *buf)
  41. {
  42. struct thermal_zone_device *tz = to_thermal_zone(dev);
  43. enum thermal_device_mode mode;
  44. int result;
  45. if (!tz->ops->get_mode)
  46. return -EPERM;
  47. result = tz->ops->get_mode(tz, &mode);
  48. if (result)
  49. return result;
  50. return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
  51. : "disabled");
  52. }
  53. static ssize_t
  54. mode_store(struct device *dev, struct device_attribute *attr,
  55. const char *buf, size_t count)
  56. {
  57. struct thermal_zone_device *tz = to_thermal_zone(dev);
  58. int result;
  59. if (!tz->ops->set_mode)
  60. return -EPERM;
  61. if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
  62. result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
  63. else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
  64. result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
  65. else
  66. result = -EINVAL;
  67. if (result)
  68. return result;
  69. return count;
  70. }
  71. static ssize_t
  72. trip_point_type_show(struct device *dev, struct device_attribute *attr,
  73. char *buf)
  74. {
  75. struct thermal_zone_device *tz = to_thermal_zone(dev);
  76. enum thermal_trip_type type;
  77. int trip, result;
  78. if (!tz->ops->get_trip_type)
  79. return -EPERM;
  80. if (sscanf(attr->attr.name, "trip_point_%d_type", &trip) != 1)
  81. return -EINVAL;
  82. result = tz->ops->get_trip_type(tz, trip, &type);
  83. if (result)
  84. return result;
  85. switch (type) {
  86. case THERMAL_TRIP_CRITICAL:
  87. return sprintf(buf, "critical\n");
  88. case THERMAL_TRIP_HOT:
  89. return sprintf(buf, "hot\n");
  90. case THERMAL_TRIP_PASSIVE:
  91. return sprintf(buf, "passive\n");
  92. case THERMAL_TRIP_ACTIVE:
  93. return sprintf(buf, "active\n");
  94. default:
  95. return sprintf(buf, "unknown\n");
  96. }
  97. }
  98. static ssize_t
  99. trip_point_temp_store(struct device *dev, struct device_attribute *attr,
  100. const char *buf, size_t count)
  101. {
  102. struct thermal_zone_device *tz = to_thermal_zone(dev);
  103. int trip, ret;
  104. int temperature;
  105. if (!tz->ops->set_trip_temp)
  106. return -EPERM;
  107. if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip) != 1)
  108. return -EINVAL;
  109. if (kstrtoint(buf, 10, &temperature))
  110. return -EINVAL;
  111. ret = tz->ops->set_trip_temp(tz, trip, temperature);
  112. if (ret)
  113. return ret;
  114. thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
  115. return count;
  116. }
  117. static ssize_t
  118. trip_point_temp_show(struct device *dev, struct device_attribute *attr,
  119. char *buf)
  120. {
  121. struct thermal_zone_device *tz = to_thermal_zone(dev);
  122. int trip, ret;
  123. int temperature;
  124. if (!tz->ops->get_trip_temp)
  125. return -EPERM;
  126. if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip) != 1)
  127. return -EINVAL;
  128. ret = tz->ops->get_trip_temp(tz, trip, &temperature);
  129. if (ret)
  130. return ret;
  131. return sprintf(buf, "%d\n", temperature);
  132. }
  133. static ssize_t
  134. trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
  135. const char *buf, size_t count)
  136. {
  137. struct thermal_zone_device *tz = to_thermal_zone(dev);
  138. int trip, ret;
  139. int temperature;
  140. if (!tz->ops->set_trip_hyst)
  141. return -EPERM;
  142. if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip) != 1)
  143. return -EINVAL;
  144. if (kstrtoint(buf, 10, &temperature))
  145. return -EINVAL;
  146. /*
  147. * We are not doing any check on the 'temperature' value
  148. * here. The driver implementing 'set_trip_hyst' has to
  149. * take care of this.
  150. */
  151. ret = tz->ops->set_trip_hyst(tz, trip, temperature);
  152. if (!ret)
  153. thermal_zone_set_trips(tz);
  154. return ret ? ret : count;
  155. }
  156. static ssize_t
  157. trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
  158. char *buf)
  159. {
  160. struct thermal_zone_device *tz = to_thermal_zone(dev);
  161. int trip, ret;
  162. int temperature;
  163. if (!tz->ops->get_trip_hyst)
  164. return -EPERM;
  165. if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip) != 1)
  166. return -EINVAL;
  167. ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
  168. return ret ? ret : sprintf(buf, "%d\n", temperature);
  169. }
  170. static ssize_t
  171. passive_store(struct device *dev, struct device_attribute *attr,
  172. const char *buf, size_t count)
  173. {
  174. struct thermal_zone_device *tz = to_thermal_zone(dev);
  175. int state;
  176. if (sscanf(buf, "%d\n", &state) != 1)
  177. return -EINVAL;
  178. /* sanity check: values below 1000 millicelcius don't make sense
  179. * and can cause the system to go into a thermal heart attack
  180. */
  181. if (state && state < 1000)
  182. return -EINVAL;
  183. if (state && !tz->forced_passive) {
  184. if (!tz->passive_delay)
  185. tz->passive_delay = 1000;
  186. thermal_zone_device_rebind_exception(tz, "Processor",
  187. sizeof("Processor"));
  188. } else if (!state && tz->forced_passive) {
  189. tz->passive_delay = 0;
  190. thermal_zone_device_unbind_exception(tz, "Processor",
  191. sizeof("Processor"));
  192. }
  193. tz->forced_passive = state;
  194. thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
  195. return count;
  196. }
  197. static ssize_t
  198. passive_show(struct device *dev, struct device_attribute *attr,
  199. char *buf)
  200. {
  201. struct thermal_zone_device *tz = to_thermal_zone(dev);
  202. return sprintf(buf, "%d\n", tz->forced_passive);
  203. }
  204. static ssize_t
  205. policy_store(struct device *dev, struct device_attribute *attr,
  206. const char *buf, size_t count)
  207. {
  208. struct thermal_zone_device *tz = to_thermal_zone(dev);
  209. char name[THERMAL_NAME_LENGTH];
  210. int ret;
  211. snprintf(name, sizeof(name), "%s", buf);
  212. ret = thermal_zone_device_set_policy(tz, name);
  213. if (!ret)
  214. ret = count;
  215. return ret;
  216. }
  217. static ssize_t
  218. policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
  219. {
  220. struct thermal_zone_device *tz = to_thermal_zone(dev);
  221. return sprintf(buf, "%s\n", tz->governor->name);
  222. }
  223. static ssize_t
  224. available_policies_show(struct device *dev, struct device_attribute *devattr,
  225. char *buf)
  226. {
  227. return thermal_build_list_of_policies(buf);
  228. }
  229. #if (IS_ENABLED(CONFIG_THERMAL_EMULATION))
  230. static ssize_t
  231. emul_temp_store(struct device *dev, struct device_attribute *attr,
  232. const char *buf, size_t count)
  233. {
  234. struct thermal_zone_device *tz = to_thermal_zone(dev);
  235. int ret = 0;
  236. int temperature;
  237. if (kstrtoint(buf, 10, &temperature))
  238. return -EINVAL;
  239. if (!tz->ops->set_emul_temp) {
  240. mutex_lock(&tz->lock);
  241. tz->emul_temperature = temperature;
  242. mutex_unlock(&tz->lock);
  243. } else {
  244. ret = tz->ops->set_emul_temp(tz, temperature);
  245. }
  246. if (!ret)
  247. thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
  248. return ret ? ret : count;
  249. }
  250. static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
  251. #endif
  252. static ssize_t
  253. sustainable_power_show(struct device *dev, struct device_attribute *devattr,
  254. char *buf)
  255. {
  256. struct thermal_zone_device *tz = to_thermal_zone(dev);
  257. if (tz->tzp)
  258. return sprintf(buf, "%u\n", tz->tzp->sustainable_power);
  259. else
  260. return -EIO;
  261. }
  262. static ssize_t
  263. sustainable_power_store(struct device *dev, struct device_attribute *devattr,
  264. const char *buf, size_t count)
  265. {
  266. struct thermal_zone_device *tz = to_thermal_zone(dev);
  267. u32 sustainable_power;
  268. if (!tz->tzp)
  269. return -EIO;
  270. if (kstrtou32(buf, 10, &sustainable_power))
  271. return -EINVAL;
  272. tz->tzp->sustainable_power = sustainable_power;
  273. return count;
  274. }
  275. #define create_s32_tzp_attr(name) \
  276. static ssize_t \
  277. name##_show(struct device *dev, struct device_attribute *devattr, \
  278. char *buf) \
  279. { \
  280. struct thermal_zone_device *tz = to_thermal_zone(dev); \
  281. \
  282. if (tz->tzp) \
  283. return sprintf(buf, "%d\n", tz->tzp->name); \
  284. else \
  285. return -EIO; \
  286. } \
  287. \
  288. static ssize_t \
  289. name##_store(struct device *dev, struct device_attribute *devattr, \
  290. const char *buf, size_t count) \
  291. { \
  292. struct thermal_zone_device *tz = to_thermal_zone(dev); \
  293. s32 value; \
  294. \
  295. if (!tz->tzp) \
  296. return -EIO; \
  297. \
  298. if (kstrtos32(buf, 10, &value)) \
  299. return -EINVAL; \
  300. \
  301. tz->tzp->name = value; \
  302. \
  303. return count; \
  304. } \
  305. static DEVICE_ATTR(name, S_IWUSR | S_IRUGO, name##_show, name##_store)
  306. create_s32_tzp_attr(k_po);
  307. create_s32_tzp_attr(k_pu);
  308. create_s32_tzp_attr(k_i);
  309. create_s32_tzp_attr(k_d);
  310. create_s32_tzp_attr(integral_cutoff);
  311. create_s32_tzp_attr(slope);
  312. create_s32_tzp_attr(offset);
  313. #undef create_s32_tzp_attr
  314. /*
  315. * These are thermal zone device attributes that will always be present.
  316. * All the attributes created for tzp (create_s32_tzp_attr) also are always
  317. * present on the sysfs interface.
  318. */
  319. static DEVICE_ATTR(type, 0444, type_show, NULL);
  320. static DEVICE_ATTR(temp, 0444, temp_show, NULL);
  321. static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
  322. static DEVICE_ATTR(available_policies, S_IRUGO, available_policies_show, NULL);
  323. static DEVICE_ATTR(sustainable_power, S_IWUSR | S_IRUGO, sustainable_power_show,
  324. sustainable_power_store);
  325. /* These thermal zone device attributes are created based on conditions */
  326. static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
  327. static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
  328. /* These attributes are unconditionally added to a thermal zone */
  329. static struct attribute *thermal_zone_dev_attrs[] = {
  330. &dev_attr_type.attr,
  331. &dev_attr_temp.attr,
  332. #if (IS_ENABLED(CONFIG_THERMAL_EMULATION))
  333. &dev_attr_emul_temp.attr,
  334. #endif
  335. &dev_attr_policy.attr,
  336. &dev_attr_available_policies.attr,
  337. &dev_attr_sustainable_power.attr,
  338. &dev_attr_k_po.attr,
  339. &dev_attr_k_pu.attr,
  340. &dev_attr_k_i.attr,
  341. &dev_attr_k_d.attr,
  342. &dev_attr_integral_cutoff.attr,
  343. &dev_attr_slope.attr,
  344. &dev_attr_offset.attr,
  345. NULL,
  346. };
  347. static struct attribute_group thermal_zone_attribute_group = {
  348. .attrs = thermal_zone_dev_attrs,
  349. };
  350. /* We expose mode only if .get_mode is present */
  351. static struct attribute *thermal_zone_mode_attrs[] = {
  352. &dev_attr_mode.attr,
  353. NULL,
  354. };
  355. static umode_t thermal_zone_mode_is_visible(struct kobject *kobj,
  356. struct attribute *attr,
  357. int attrno)
  358. {
  359. struct device *dev = container_of(kobj, struct device, kobj);
  360. struct thermal_zone_device *tz;
  361. tz = container_of(dev, struct thermal_zone_device, device);
  362. if (tz->ops->get_mode)
  363. return attr->mode;
  364. return 0;
  365. }
  366. static struct attribute_group thermal_zone_mode_attribute_group = {
  367. .attrs = thermal_zone_mode_attrs,
  368. .is_visible = thermal_zone_mode_is_visible,
  369. };
  370. /* We expose passive only if passive trips are present */
  371. static struct attribute *thermal_zone_passive_attrs[] = {
  372. &dev_attr_passive.attr,
  373. NULL,
  374. };
  375. static umode_t thermal_zone_passive_is_visible(struct kobject *kobj,
  376. struct attribute *attr,
  377. int attrno)
  378. {
  379. struct device *dev = container_of(kobj, struct device, kobj);
  380. struct thermal_zone_device *tz;
  381. enum thermal_trip_type trip_type;
  382. int count, passive = 0;
  383. tz = container_of(dev, struct thermal_zone_device, device);
  384. for (count = 0; count < tz->trips && !passive; count++) {
  385. tz->ops->get_trip_type(tz, count, &trip_type);
  386. if (trip_type == THERMAL_TRIP_PASSIVE)
  387. passive = 1;
  388. }
  389. if (!passive)
  390. return attr->mode;
  391. return 0;
  392. }
  393. static struct attribute_group thermal_zone_passive_attribute_group = {
  394. .attrs = thermal_zone_passive_attrs,
  395. .is_visible = thermal_zone_passive_is_visible,
  396. };
  397. static const struct attribute_group *thermal_zone_attribute_groups[] = {
  398. &thermal_zone_attribute_group,
  399. &thermal_zone_mode_attribute_group,
  400. &thermal_zone_passive_attribute_group,
  401. /* This is not NULL terminated as we create the group dynamically */
  402. };
  403. /**
  404. * create_trip_attrs() - create attributes for trip points
  405. * @tz: the thermal zone device
  406. * @mask: Writeable trip point bitmap.
  407. *
  408. * helper function to instantiate sysfs entries for every trip
  409. * point and its properties of a struct thermal_zone_device.
  410. *
  411. * Return: 0 on success, the proper error value otherwise.
  412. */
  413. static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
  414. {
  415. struct attribute **attrs;
  416. int indx;
  417. /* This function works only for zones with at least one trip */
  418. if (tz->trips <= 0)
  419. return -EINVAL;
  420. tz->trip_type_attrs = kcalloc(tz->trips, sizeof(*tz->trip_type_attrs),
  421. GFP_KERNEL);
  422. if (!tz->trip_type_attrs)
  423. return -ENOMEM;
  424. tz->trip_temp_attrs = kcalloc(tz->trips, sizeof(*tz->trip_temp_attrs),
  425. GFP_KERNEL);
  426. if (!tz->trip_temp_attrs) {
  427. kfree(tz->trip_type_attrs);
  428. return -ENOMEM;
  429. }
  430. if (tz->ops->get_trip_hyst) {
  431. tz->trip_hyst_attrs = kcalloc(tz->trips,
  432. sizeof(*tz->trip_hyst_attrs),
  433. GFP_KERNEL);
  434. if (!tz->trip_hyst_attrs) {
  435. kfree(tz->trip_type_attrs);
  436. kfree(tz->trip_temp_attrs);
  437. return -ENOMEM;
  438. }
  439. }
  440. attrs = kcalloc(tz->trips * 3 + 1, sizeof(*attrs), GFP_KERNEL);
  441. if (!attrs) {
  442. kfree(tz->trip_type_attrs);
  443. kfree(tz->trip_temp_attrs);
  444. if (tz->ops->get_trip_hyst)
  445. kfree(tz->trip_hyst_attrs);
  446. return -ENOMEM;
  447. }
  448. for (indx = 0; indx < tz->trips; indx++) {
  449. /* create trip type attribute */
  450. snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
  451. "trip_point_%d_type", indx);
  452. sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
  453. tz->trip_type_attrs[indx].attr.attr.name =
  454. tz->trip_type_attrs[indx].name;
  455. tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
  456. tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
  457. attrs[indx] = &tz->trip_type_attrs[indx].attr.attr;
  458. /* create trip temp attribute */
  459. snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
  460. "trip_point_%d_temp", indx);
  461. sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
  462. tz->trip_temp_attrs[indx].attr.attr.name =
  463. tz->trip_temp_attrs[indx].name;
  464. tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
  465. tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
  466. if (IS_ENABLED(CONFIG_THERMAL_WRITABLE_TRIPS) &&
  467. mask & (1 << indx)) {
  468. tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
  469. tz->trip_temp_attrs[indx].attr.store =
  470. trip_point_temp_store;
  471. }
  472. attrs[indx + tz->trips] = &tz->trip_temp_attrs[indx].attr.attr;
  473. /* create Optional trip hyst attribute */
  474. if (!tz->ops->get_trip_hyst)
  475. continue;
  476. snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
  477. "trip_point_%d_hyst", indx);
  478. sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
  479. tz->trip_hyst_attrs[indx].attr.attr.name =
  480. tz->trip_hyst_attrs[indx].name;
  481. tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
  482. tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
  483. if (tz->ops->set_trip_hyst) {
  484. tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
  485. tz->trip_hyst_attrs[indx].attr.store =
  486. trip_point_hyst_store;
  487. }
  488. attrs[indx + tz->trips * 2] =
  489. &tz->trip_hyst_attrs[indx].attr.attr;
  490. }
  491. attrs[tz->trips * 3] = NULL;
  492. tz->trips_attribute_group.attrs = attrs;
  493. return 0;
  494. }
  495. int thermal_zone_create_device_groups(struct thermal_zone_device *tz,
  496. int mask)
  497. {
  498. const struct attribute_group **groups;
  499. int i, size, result;
  500. /* we need one extra for trips and the NULL to terminate the array */
  501. size = ARRAY_SIZE(thermal_zone_attribute_groups) + 2;
  502. /* This also takes care of API requirement to be NULL terminated */
  503. groups = kcalloc(size, sizeof(*groups), GFP_KERNEL);
  504. if (!groups)
  505. return -ENOMEM;
  506. for (i = 0; i < size - 2; i++)
  507. groups[i] = thermal_zone_attribute_groups[i];
  508. if (tz->trips) {
  509. result = create_trip_attrs(tz, mask);
  510. if (result) {
  511. kfree(groups);
  512. return result;
  513. }
  514. groups[size - 2] = &tz->trips_attribute_group;
  515. }
  516. tz->device.groups = groups;
  517. return 0;
  518. }
  519. /* sys I/F for cooling device */
  520. static ssize_t
  521. thermal_cooling_device_type_show(struct device *dev,
  522. struct device_attribute *attr, char *buf)
  523. {
  524. struct thermal_cooling_device *cdev = to_cooling_device(dev);
  525. return sprintf(buf, "%s\n", cdev->type);
  526. }
  527. static ssize_t
  528. thermal_cooling_device_max_state_show(struct device *dev,
  529. struct device_attribute *attr, char *buf)
  530. {
  531. struct thermal_cooling_device *cdev = to_cooling_device(dev);
  532. unsigned long state;
  533. int ret;
  534. ret = cdev->ops->get_max_state(cdev, &state);
  535. if (ret)
  536. return ret;
  537. return sprintf(buf, "%ld\n", state);
  538. }
  539. static ssize_t
  540. thermal_cooling_device_cur_state_show(struct device *dev,
  541. struct device_attribute *attr, char *buf)
  542. {
  543. struct thermal_cooling_device *cdev = to_cooling_device(dev);
  544. unsigned long state;
  545. int ret;
  546. ret = cdev->ops->get_cur_state(cdev, &state);
  547. if (ret)
  548. return ret;
  549. return sprintf(buf, "%ld\n", state);
  550. }
  551. static ssize_t
  552. thermal_cooling_device_cur_state_store(struct device *dev,
  553. struct device_attribute *attr,
  554. const char *buf, size_t count)
  555. {
  556. struct thermal_cooling_device *cdev = to_cooling_device(dev);
  557. unsigned long state;
  558. int result;
  559. if (sscanf(buf, "%ld\n", &state) != 1)
  560. return -EINVAL;
  561. if ((long)state < 0)
  562. return -EINVAL;
  563. result = cdev->ops->set_cur_state(cdev, state);
  564. if (result)
  565. return result;
  566. return count;
  567. }
  568. static struct device_attribute dev_attr_cdev_type =
  569. __ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
  570. static DEVICE_ATTR(max_state, 0444,
  571. thermal_cooling_device_max_state_show, NULL);
  572. static DEVICE_ATTR(cur_state, 0644,
  573. thermal_cooling_device_cur_state_show,
  574. thermal_cooling_device_cur_state_store);
  575. static struct attribute *cooling_device_attrs[] = {
  576. &dev_attr_cdev_type.attr,
  577. &dev_attr_max_state.attr,
  578. &dev_attr_cur_state.attr,
  579. NULL,
  580. };
  581. static const struct attribute_group cooling_device_attr_group = {
  582. .attrs = cooling_device_attrs,
  583. };
  584. static const struct attribute_group *cooling_device_attr_groups[] = {
  585. &cooling_device_attr_group,
  586. NULL,
  587. };
  588. void thermal_cooling_device_setup_sysfs(struct thermal_cooling_device *cdev)
  589. {
  590. cdev->device.groups = cooling_device_attr_groups;
  591. }
  592. /* these helper will be used only at the time of bindig */
  593. ssize_t
  594. thermal_cooling_device_trip_point_show(struct device *dev,
  595. struct device_attribute *attr, char *buf)
  596. {
  597. struct thermal_instance *instance;
  598. instance =
  599. container_of(attr, struct thermal_instance, attr);
  600. if (instance->trip == THERMAL_TRIPS_NONE)
  601. return sprintf(buf, "-1\n");
  602. else
  603. return sprintf(buf, "%d\n", instance->trip);
  604. }
  605. ssize_t
  606. thermal_cooling_device_weight_show(struct device *dev,
  607. struct device_attribute *attr, char *buf)
  608. {
  609. struct thermal_instance *instance;
  610. instance = container_of(attr, struct thermal_instance, weight_attr);
  611. return sprintf(buf, "%d\n", instance->weight);
  612. }
  613. ssize_t
  614. thermal_cooling_device_weight_store(struct device *dev,
  615. struct device_attribute *attr,
  616. const char *buf, size_t count)
  617. {
  618. struct thermal_instance *instance;
  619. int ret, weight;
  620. ret = kstrtoint(buf, 0, &weight);
  621. if (ret)
  622. return ret;
  623. instance = container_of(attr, struct thermal_instance, weight_attr);
  624. instance->weight = weight;
  625. return count;
  626. }