thermal_sysfs.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * thermal.c - sysfs interface of thermal devices
  4. *
  5. * Copyright (C) 2016 Eduardo Valentin <edubezval@gmail.com>
  6. *
  7. * Highly based on original thermal_core.c
  8. * Copyright (C) 2008 Intel Corp
  9. * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
  10. * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/sysfs.h>
  14. #include <linux/device.h>
  15. #include <linux/err.h>
  16. #include <linux/slab.h>
  17. #include <linux/string.h>
  18. #include <linux/jiffies.h>
  19. #include "thermal_core.h"
  20. /* sys I/F for thermal zone */
  21. static ssize_t
  22. type_show(struct device *dev, struct device_attribute *attr, char *buf)
  23. {
  24. struct thermal_zone_device *tz = to_thermal_zone(dev);
  25. return sprintf(buf, "%s\n", tz->type);
  26. }
  27. static ssize_t
  28. temp_show(struct device *dev, struct device_attribute *attr, char *buf)
  29. {
  30. struct thermal_zone_device *tz = to_thermal_zone(dev);
  31. int temperature, ret;
  32. ret = thermal_zone_get_temp(tz, &temperature);
  33. if (ret)
  34. return ret;
  35. return sprintf(buf, "%d\n", temperature);
  36. }
  37. static ssize_t
  38. mode_show(struct device *dev, struct device_attribute *attr, char *buf)
  39. {
  40. struct thermal_zone_device *tz = to_thermal_zone(dev);
  41. enum thermal_device_mode mode;
  42. int result;
  43. if (!tz->ops->get_mode)
  44. return -EPERM;
  45. result = tz->ops->get_mode(tz, &mode);
  46. if (result)
  47. return result;
  48. return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
  49. : "disabled");
  50. }
  51. static ssize_t
  52. mode_store(struct device *dev, struct device_attribute *attr,
  53. const char *buf, size_t count)
  54. {
  55. struct thermal_zone_device *tz = to_thermal_zone(dev);
  56. int result;
  57. if (!tz->ops->set_mode)
  58. return -EPERM;
  59. if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
  60. result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
  61. else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
  62. result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
  63. else
  64. result = -EINVAL;
  65. if (result)
  66. return result;
  67. return count;
  68. }
  69. static ssize_t
  70. trip_point_type_show(struct device *dev, struct device_attribute *attr,
  71. char *buf)
  72. {
  73. struct thermal_zone_device *tz = to_thermal_zone(dev);
  74. enum thermal_trip_type type;
  75. int trip, result;
  76. if (!tz->ops->get_trip_type)
  77. return -EPERM;
  78. if (sscanf(attr->attr.name, "trip_point_%d_type", &trip) != 1)
  79. return -EINVAL;
  80. result = tz->ops->get_trip_type(tz, trip, &type);
  81. if (result)
  82. return result;
  83. switch (type) {
  84. case THERMAL_TRIP_CRITICAL:
  85. return sprintf(buf, "critical\n");
  86. case THERMAL_TRIP_HOT:
  87. return sprintf(buf, "hot\n");
  88. case THERMAL_TRIP_PASSIVE:
  89. return sprintf(buf, "passive\n");
  90. case THERMAL_TRIP_ACTIVE:
  91. return sprintf(buf, "active\n");
  92. default:
  93. return sprintf(buf, "unknown\n");
  94. }
  95. }
  96. static ssize_t
  97. trip_point_temp_store(struct device *dev, struct device_attribute *attr,
  98. const char *buf, size_t count)
  99. {
  100. struct thermal_zone_device *tz = to_thermal_zone(dev);
  101. int trip, ret;
  102. int temperature;
  103. if (!tz->ops->set_trip_temp)
  104. return -EPERM;
  105. if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip) != 1)
  106. return -EINVAL;
  107. if (kstrtoint(buf, 10, &temperature))
  108. return -EINVAL;
  109. ret = tz->ops->set_trip_temp(tz, trip, temperature);
  110. if (ret)
  111. return ret;
  112. thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
  113. return count;
  114. }
  115. static ssize_t
  116. trip_point_temp_show(struct device *dev, struct device_attribute *attr,
  117. char *buf)
  118. {
  119. struct thermal_zone_device *tz = to_thermal_zone(dev);
  120. int trip, ret;
  121. int temperature;
  122. if (!tz->ops->get_trip_temp)
  123. return -EPERM;
  124. if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip) != 1)
  125. return -EINVAL;
  126. ret = tz->ops->get_trip_temp(tz, trip, &temperature);
  127. if (ret)
  128. return ret;
  129. return sprintf(buf, "%d\n", temperature);
  130. }
  131. static ssize_t
  132. trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
  133. const char *buf, size_t count)
  134. {
  135. struct thermal_zone_device *tz = to_thermal_zone(dev);
  136. int trip, ret;
  137. int temperature;
  138. if (!tz->ops->set_trip_hyst)
  139. return -EPERM;
  140. if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip) != 1)
  141. return -EINVAL;
  142. if (kstrtoint(buf, 10, &temperature))
  143. return -EINVAL;
  144. /*
  145. * We are not doing any check on the 'temperature' value
  146. * here. The driver implementing 'set_trip_hyst' has to
  147. * take care of this.
  148. */
  149. ret = tz->ops->set_trip_hyst(tz, trip, temperature);
  150. if (!ret)
  151. thermal_zone_set_trips(tz);
  152. return ret ? ret : count;
  153. }
  154. static ssize_t
  155. trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
  156. char *buf)
  157. {
  158. struct thermal_zone_device *tz = to_thermal_zone(dev);
  159. int trip, ret;
  160. int temperature;
  161. if (!tz->ops->get_trip_hyst)
  162. return -EPERM;
  163. if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip) != 1)
  164. return -EINVAL;
  165. ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
  166. return ret ? ret : sprintf(buf, "%d\n", temperature);
  167. }
  168. static ssize_t
  169. passive_store(struct device *dev, struct device_attribute *attr,
  170. const char *buf, size_t count)
  171. {
  172. struct thermal_zone_device *tz = to_thermal_zone(dev);
  173. int state;
  174. if (sscanf(buf, "%d\n", &state) != 1)
  175. return -EINVAL;
  176. /* sanity check: values below 1000 millicelcius don't make sense
  177. * and can cause the system to go into a thermal heart attack
  178. */
  179. if (state && state < 1000)
  180. return -EINVAL;
  181. if (state && !tz->forced_passive) {
  182. if (!tz->passive_delay)
  183. tz->passive_delay = 1000;
  184. thermal_zone_device_rebind_exception(tz, "Processor",
  185. sizeof("Processor"));
  186. } else if (!state && tz->forced_passive) {
  187. tz->passive_delay = 0;
  188. thermal_zone_device_unbind_exception(tz, "Processor",
  189. sizeof("Processor"));
  190. }
  191. tz->forced_passive = state;
  192. thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
  193. return count;
  194. }
  195. static ssize_t
  196. passive_show(struct device *dev, struct device_attribute *attr,
  197. char *buf)
  198. {
  199. struct thermal_zone_device *tz = to_thermal_zone(dev);
  200. return sprintf(buf, "%d\n", tz->forced_passive);
  201. }
  202. static ssize_t
  203. policy_store(struct device *dev, struct device_attribute *attr,
  204. const char *buf, size_t count)
  205. {
  206. struct thermal_zone_device *tz = to_thermal_zone(dev);
  207. char name[THERMAL_NAME_LENGTH];
  208. int ret;
  209. snprintf(name, sizeof(name), "%s", buf);
  210. ret = thermal_zone_device_set_policy(tz, name);
  211. if (!ret)
  212. ret = count;
  213. return ret;
  214. }
  215. static ssize_t
  216. policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
  217. {
  218. struct thermal_zone_device *tz = to_thermal_zone(dev);
  219. return sprintf(buf, "%s\n", tz->governor->name);
  220. }
  221. static ssize_t
  222. available_policies_show(struct device *dev, struct device_attribute *devattr,
  223. char *buf)
  224. {
  225. return thermal_build_list_of_policies(buf);
  226. }
  227. #if (IS_ENABLED(CONFIG_THERMAL_EMULATION))
  228. static ssize_t
  229. emul_temp_store(struct device *dev, struct device_attribute *attr,
  230. const char *buf, size_t count)
  231. {
  232. struct thermal_zone_device *tz = to_thermal_zone(dev);
  233. int ret = 0;
  234. int temperature;
  235. if (kstrtoint(buf, 10, &temperature))
  236. return -EINVAL;
  237. if (!tz->ops->set_emul_temp) {
  238. mutex_lock(&tz->lock);
  239. tz->emul_temperature = temperature;
  240. mutex_unlock(&tz->lock);
  241. } else {
  242. ret = tz->ops->set_emul_temp(tz, temperature);
  243. }
  244. if (!ret)
  245. thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
  246. return ret ? ret : count;
  247. }
  248. static DEVICE_ATTR_WO(emul_temp);
  249. #endif
  250. static ssize_t
  251. sustainable_power_show(struct device *dev, struct device_attribute *devattr,
  252. char *buf)
  253. {
  254. struct thermal_zone_device *tz = to_thermal_zone(dev);
  255. if (tz->tzp)
  256. return sprintf(buf, "%u\n", tz->tzp->sustainable_power);
  257. else
  258. return -EIO;
  259. }
  260. static ssize_t
  261. sustainable_power_store(struct device *dev, struct device_attribute *devattr,
  262. const char *buf, size_t count)
  263. {
  264. struct thermal_zone_device *tz = to_thermal_zone(dev);
  265. u32 sustainable_power;
  266. if (!tz->tzp)
  267. return -EIO;
  268. if (kstrtou32(buf, 10, &sustainable_power))
  269. return -EINVAL;
  270. tz->tzp->sustainable_power = sustainable_power;
  271. return count;
  272. }
  273. #define create_s32_tzp_attr(name) \
  274. static ssize_t \
  275. name##_show(struct device *dev, struct device_attribute *devattr, \
  276. char *buf) \
  277. { \
  278. struct thermal_zone_device *tz = to_thermal_zone(dev); \
  279. \
  280. if (tz->tzp) \
  281. return sprintf(buf, "%d\n", tz->tzp->name); \
  282. else \
  283. return -EIO; \
  284. } \
  285. \
  286. static ssize_t \
  287. name##_store(struct device *dev, struct device_attribute *devattr, \
  288. const char *buf, size_t count) \
  289. { \
  290. struct thermal_zone_device *tz = to_thermal_zone(dev); \
  291. s32 value; \
  292. \
  293. if (!tz->tzp) \
  294. return -EIO; \
  295. \
  296. if (kstrtos32(buf, 10, &value)) \
  297. return -EINVAL; \
  298. \
  299. tz->tzp->name = value; \
  300. \
  301. return count; \
  302. } \
  303. static DEVICE_ATTR_RW(name)
  304. create_s32_tzp_attr(k_po);
  305. create_s32_tzp_attr(k_pu);
  306. create_s32_tzp_attr(k_i);
  307. create_s32_tzp_attr(k_d);
  308. create_s32_tzp_attr(integral_cutoff);
  309. create_s32_tzp_attr(slope);
  310. create_s32_tzp_attr(offset);
  311. #undef create_s32_tzp_attr
  312. /*
  313. * These are thermal zone device attributes that will always be present.
  314. * All the attributes created for tzp (create_s32_tzp_attr) also are always
  315. * present on the sysfs interface.
  316. */
  317. static DEVICE_ATTR_RO(type);
  318. static DEVICE_ATTR_RO(temp);
  319. static DEVICE_ATTR_RW(policy);
  320. static DEVICE_ATTR_RO(available_policies);
  321. static DEVICE_ATTR_RW(sustainable_power);
  322. /* These thermal zone device attributes are created based on conditions */
  323. static DEVICE_ATTR_RW(mode);
  324. static DEVICE_ATTR_RW(passive);
  325. /* These attributes are unconditionally added to a thermal zone */
  326. static struct attribute *thermal_zone_dev_attrs[] = {
  327. &dev_attr_type.attr,
  328. &dev_attr_temp.attr,
  329. #if (IS_ENABLED(CONFIG_THERMAL_EMULATION))
  330. &dev_attr_emul_temp.attr,
  331. #endif
  332. &dev_attr_policy.attr,
  333. &dev_attr_available_policies.attr,
  334. &dev_attr_sustainable_power.attr,
  335. &dev_attr_k_po.attr,
  336. &dev_attr_k_pu.attr,
  337. &dev_attr_k_i.attr,
  338. &dev_attr_k_d.attr,
  339. &dev_attr_integral_cutoff.attr,
  340. &dev_attr_slope.attr,
  341. &dev_attr_offset.attr,
  342. NULL,
  343. };
  344. static struct attribute_group thermal_zone_attribute_group = {
  345. .attrs = thermal_zone_dev_attrs,
  346. };
  347. /* We expose mode only if .get_mode is present */
  348. static struct attribute *thermal_zone_mode_attrs[] = {
  349. &dev_attr_mode.attr,
  350. NULL,
  351. };
  352. static umode_t thermal_zone_mode_is_visible(struct kobject *kobj,
  353. struct attribute *attr,
  354. int attrno)
  355. {
  356. struct device *dev = container_of(kobj, struct device, kobj);
  357. struct thermal_zone_device *tz;
  358. tz = container_of(dev, struct thermal_zone_device, device);
  359. if (tz->ops->get_mode)
  360. return attr->mode;
  361. return 0;
  362. }
  363. static struct attribute_group thermal_zone_mode_attribute_group = {
  364. .attrs = thermal_zone_mode_attrs,
  365. .is_visible = thermal_zone_mode_is_visible,
  366. };
  367. /* We expose passive only if passive trips are present */
  368. static struct attribute *thermal_zone_passive_attrs[] = {
  369. &dev_attr_passive.attr,
  370. NULL,
  371. };
  372. static umode_t thermal_zone_passive_is_visible(struct kobject *kobj,
  373. struct attribute *attr,
  374. int attrno)
  375. {
  376. struct device *dev = container_of(kobj, struct device, kobj);
  377. struct thermal_zone_device *tz;
  378. enum thermal_trip_type trip_type;
  379. int count, passive = 0;
  380. tz = container_of(dev, struct thermal_zone_device, device);
  381. for (count = 0; count < tz->trips && !passive; count++) {
  382. tz->ops->get_trip_type(tz, count, &trip_type);
  383. if (trip_type == THERMAL_TRIP_PASSIVE)
  384. passive = 1;
  385. }
  386. if (!passive)
  387. return attr->mode;
  388. return 0;
  389. }
  390. static struct attribute_group thermal_zone_passive_attribute_group = {
  391. .attrs = thermal_zone_passive_attrs,
  392. .is_visible = thermal_zone_passive_is_visible,
  393. };
  394. static const struct attribute_group *thermal_zone_attribute_groups[] = {
  395. &thermal_zone_attribute_group,
  396. &thermal_zone_mode_attribute_group,
  397. &thermal_zone_passive_attribute_group,
  398. /* This is not NULL terminated as we create the group dynamically */
  399. };
  400. /**
  401. * create_trip_attrs() - create attributes for trip points
  402. * @tz: the thermal zone device
  403. * @mask: Writeable trip point bitmap.
  404. *
  405. * helper function to instantiate sysfs entries for every trip
  406. * point and its properties of a struct thermal_zone_device.
  407. *
  408. * Return: 0 on success, the proper error value otherwise.
  409. */
  410. static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
  411. {
  412. struct attribute **attrs;
  413. int indx;
  414. /* This function works only for zones with at least one trip */
  415. if (tz->trips <= 0)
  416. return -EINVAL;
  417. tz->trip_type_attrs = kcalloc(tz->trips, sizeof(*tz->trip_type_attrs),
  418. GFP_KERNEL);
  419. if (!tz->trip_type_attrs)
  420. return -ENOMEM;
  421. tz->trip_temp_attrs = kcalloc(tz->trips, sizeof(*tz->trip_temp_attrs),
  422. GFP_KERNEL);
  423. if (!tz->trip_temp_attrs) {
  424. kfree(tz->trip_type_attrs);
  425. return -ENOMEM;
  426. }
  427. if (tz->ops->get_trip_hyst) {
  428. tz->trip_hyst_attrs = kcalloc(tz->trips,
  429. sizeof(*tz->trip_hyst_attrs),
  430. GFP_KERNEL);
  431. if (!tz->trip_hyst_attrs) {
  432. kfree(tz->trip_type_attrs);
  433. kfree(tz->trip_temp_attrs);
  434. return -ENOMEM;
  435. }
  436. }
  437. attrs = kcalloc(tz->trips * 3 + 1, sizeof(*attrs), GFP_KERNEL);
  438. if (!attrs) {
  439. kfree(tz->trip_type_attrs);
  440. kfree(tz->trip_temp_attrs);
  441. if (tz->ops->get_trip_hyst)
  442. kfree(tz->trip_hyst_attrs);
  443. return -ENOMEM;
  444. }
  445. for (indx = 0; indx < tz->trips; indx++) {
  446. /* create trip type attribute */
  447. snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
  448. "trip_point_%d_type", indx);
  449. sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
  450. tz->trip_type_attrs[indx].attr.attr.name =
  451. tz->trip_type_attrs[indx].name;
  452. tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
  453. tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
  454. attrs[indx] = &tz->trip_type_attrs[indx].attr.attr;
  455. /* create trip temp attribute */
  456. snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
  457. "trip_point_%d_temp", indx);
  458. sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
  459. tz->trip_temp_attrs[indx].attr.attr.name =
  460. tz->trip_temp_attrs[indx].name;
  461. tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
  462. tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
  463. if (IS_ENABLED(CONFIG_THERMAL_WRITABLE_TRIPS) &&
  464. mask & (1 << indx)) {
  465. tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
  466. tz->trip_temp_attrs[indx].attr.store =
  467. trip_point_temp_store;
  468. }
  469. attrs[indx + tz->trips] = &tz->trip_temp_attrs[indx].attr.attr;
  470. /* create Optional trip hyst attribute */
  471. if (!tz->ops->get_trip_hyst)
  472. continue;
  473. snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
  474. "trip_point_%d_hyst", indx);
  475. sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
  476. tz->trip_hyst_attrs[indx].attr.attr.name =
  477. tz->trip_hyst_attrs[indx].name;
  478. tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
  479. tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
  480. if (tz->ops->set_trip_hyst) {
  481. tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
  482. tz->trip_hyst_attrs[indx].attr.store =
  483. trip_point_hyst_store;
  484. }
  485. attrs[indx + tz->trips * 2] =
  486. &tz->trip_hyst_attrs[indx].attr.attr;
  487. }
  488. attrs[tz->trips * 3] = NULL;
  489. tz->trips_attribute_group.attrs = attrs;
  490. return 0;
  491. }
  492. /**
  493. * destroy_trip_attrs() - destroy attributes for trip points
  494. * @tz: the thermal zone device
  495. *
  496. * helper function to free resources allocated by create_trip_attrs()
  497. */
  498. static void destroy_trip_attrs(struct thermal_zone_device *tz)
  499. {
  500. if (!tz)
  501. return;
  502. kfree(tz->trip_type_attrs);
  503. kfree(tz->trip_temp_attrs);
  504. if (tz->ops->get_trip_hyst)
  505. kfree(tz->trip_hyst_attrs);
  506. kfree(tz->trips_attribute_group.attrs);
  507. }
  508. int thermal_zone_create_device_groups(struct thermal_zone_device *tz,
  509. int mask)
  510. {
  511. const struct attribute_group **groups;
  512. int i, size, result;
  513. /* we need one extra for trips and the NULL to terminate the array */
  514. size = ARRAY_SIZE(thermal_zone_attribute_groups) + 2;
  515. /* This also takes care of API requirement to be NULL terminated */
  516. groups = kcalloc(size, sizeof(*groups), GFP_KERNEL);
  517. if (!groups)
  518. return -ENOMEM;
  519. for (i = 0; i < size - 2; i++)
  520. groups[i] = thermal_zone_attribute_groups[i];
  521. if (tz->trips) {
  522. result = create_trip_attrs(tz, mask);
  523. if (result) {
  524. kfree(groups);
  525. return result;
  526. }
  527. groups[size - 2] = &tz->trips_attribute_group;
  528. }
  529. tz->device.groups = groups;
  530. return 0;
  531. }
  532. void thermal_zone_destroy_device_groups(struct thermal_zone_device *tz)
  533. {
  534. if (!tz)
  535. return;
  536. if (tz->trips)
  537. destroy_trip_attrs(tz);
  538. kfree(tz->device.groups);
  539. }
  540. /* sys I/F for cooling device */
  541. static ssize_t
  542. cdev_type_show(struct device *dev, struct device_attribute *attr, char *buf)
  543. {
  544. struct thermal_cooling_device *cdev = to_cooling_device(dev);
  545. return sprintf(buf, "%s\n", cdev->type);
  546. }
  547. static ssize_t max_state_show(struct device *dev, struct device_attribute *attr,
  548. char *buf)
  549. {
  550. struct thermal_cooling_device *cdev = to_cooling_device(dev);
  551. unsigned long state;
  552. int ret;
  553. ret = cdev->ops->get_max_state(cdev, &state);
  554. if (ret)
  555. return ret;
  556. return sprintf(buf, "%ld\n", state);
  557. }
  558. static ssize_t cur_state_show(struct device *dev, struct device_attribute *attr,
  559. char *buf)
  560. {
  561. struct thermal_cooling_device *cdev = to_cooling_device(dev);
  562. unsigned long state;
  563. int ret;
  564. ret = cdev->ops->get_cur_state(cdev, &state);
  565. if (ret)
  566. return ret;
  567. return sprintf(buf, "%ld\n", state);
  568. }
  569. static ssize_t
  570. cur_state_store(struct device *dev, struct device_attribute *attr,
  571. const char *buf, size_t count)
  572. {
  573. struct thermal_cooling_device *cdev = to_cooling_device(dev);
  574. unsigned long state;
  575. int result;
  576. if (sscanf(buf, "%ld\n", &state) != 1)
  577. return -EINVAL;
  578. if ((long)state < 0)
  579. return -EINVAL;
  580. mutex_lock(&cdev->lock);
  581. result = cdev->ops->set_cur_state(cdev, state);
  582. if (!result)
  583. thermal_cooling_device_stats_update(cdev, state);
  584. mutex_unlock(&cdev->lock);
  585. return result ? result : count;
  586. }
  587. static struct device_attribute
  588. dev_attr_cdev_type = __ATTR(type, 0444, cdev_type_show, NULL);
  589. static DEVICE_ATTR_RO(max_state);
  590. static DEVICE_ATTR_RW(cur_state);
  591. static struct attribute *cooling_device_attrs[] = {
  592. &dev_attr_cdev_type.attr,
  593. &dev_attr_max_state.attr,
  594. &dev_attr_cur_state.attr,
  595. NULL,
  596. };
  597. static const struct attribute_group cooling_device_attr_group = {
  598. .attrs = cooling_device_attrs,
  599. };
  600. static const struct attribute_group *cooling_device_attr_groups[] = {
  601. &cooling_device_attr_group,
  602. NULL, /* Space allocated for cooling_device_stats_attr_group */
  603. NULL,
  604. };
  605. #ifdef CONFIG_THERMAL_STATISTICS
  606. struct cooling_dev_stats {
  607. spinlock_t lock;
  608. unsigned int total_trans;
  609. unsigned long state;
  610. unsigned long max_states;
  611. ktime_t last_time;
  612. ktime_t *time_in_state;
  613. unsigned int *trans_table;
  614. };
  615. static void update_time_in_state(struct cooling_dev_stats *stats)
  616. {
  617. ktime_t now = ktime_get(), delta;
  618. delta = ktime_sub(now, stats->last_time);
  619. stats->time_in_state[stats->state] =
  620. ktime_add(stats->time_in_state[stats->state], delta);
  621. stats->last_time = now;
  622. }
  623. void thermal_cooling_device_stats_update(struct thermal_cooling_device *cdev,
  624. unsigned long new_state)
  625. {
  626. struct cooling_dev_stats *stats = cdev->stats;
  627. if (!stats)
  628. return;
  629. spin_lock(&stats->lock);
  630. if (stats->state == new_state)
  631. goto unlock;
  632. update_time_in_state(stats);
  633. stats->trans_table[stats->state * stats->max_states + new_state]++;
  634. stats->state = new_state;
  635. stats->total_trans++;
  636. unlock:
  637. spin_unlock(&stats->lock);
  638. }
  639. static ssize_t total_trans_show(struct device *dev,
  640. struct device_attribute *attr, char *buf)
  641. {
  642. struct thermal_cooling_device *cdev = to_cooling_device(dev);
  643. struct cooling_dev_stats *stats = cdev->stats;
  644. int ret;
  645. spin_lock(&stats->lock);
  646. ret = sprintf(buf, "%u\n", stats->total_trans);
  647. spin_unlock(&stats->lock);
  648. return ret;
  649. }
  650. static ssize_t
  651. time_in_state_ms_show(struct device *dev, struct device_attribute *attr,
  652. char *buf)
  653. {
  654. struct thermal_cooling_device *cdev = to_cooling_device(dev);
  655. struct cooling_dev_stats *stats = cdev->stats;
  656. ssize_t len = 0;
  657. int i;
  658. spin_lock(&stats->lock);
  659. update_time_in_state(stats);
  660. for (i = 0; i < stats->max_states; i++) {
  661. len += sprintf(buf + len, "state%u\t%llu\n", i,
  662. ktime_to_ms(stats->time_in_state[i]));
  663. }
  664. spin_unlock(&stats->lock);
  665. return len;
  666. }
  667. static ssize_t
  668. reset_store(struct device *dev, struct device_attribute *attr, const char *buf,
  669. size_t count)
  670. {
  671. struct thermal_cooling_device *cdev = to_cooling_device(dev);
  672. struct cooling_dev_stats *stats = cdev->stats;
  673. int i, states = stats->max_states;
  674. spin_lock(&stats->lock);
  675. stats->total_trans = 0;
  676. stats->last_time = ktime_get();
  677. memset(stats->trans_table, 0,
  678. states * states * sizeof(*stats->trans_table));
  679. for (i = 0; i < stats->max_states; i++)
  680. stats->time_in_state[i] = ktime_set(0, 0);
  681. spin_unlock(&stats->lock);
  682. return count;
  683. }
  684. static ssize_t trans_table_show(struct device *dev,
  685. struct device_attribute *attr, char *buf)
  686. {
  687. struct thermal_cooling_device *cdev = to_cooling_device(dev);
  688. struct cooling_dev_stats *stats = cdev->stats;
  689. ssize_t len = 0;
  690. int i, j;
  691. len += snprintf(buf + len, PAGE_SIZE - len, " From : To\n");
  692. len += snprintf(buf + len, PAGE_SIZE - len, " : ");
  693. for (i = 0; i < stats->max_states; i++) {
  694. if (len >= PAGE_SIZE)
  695. break;
  696. len += snprintf(buf + len, PAGE_SIZE - len, "state%2u ", i);
  697. }
  698. if (len >= PAGE_SIZE)
  699. return PAGE_SIZE;
  700. len += snprintf(buf + len, PAGE_SIZE - len, "\n");
  701. for (i = 0; i < stats->max_states; i++) {
  702. if (len >= PAGE_SIZE)
  703. break;
  704. len += snprintf(buf + len, PAGE_SIZE - len, "state%2u:", i);
  705. for (j = 0; j < stats->max_states; j++) {
  706. if (len >= PAGE_SIZE)
  707. break;
  708. len += snprintf(buf + len, PAGE_SIZE - len, "%8u ",
  709. stats->trans_table[i * stats->max_states + j]);
  710. }
  711. if (len >= PAGE_SIZE)
  712. break;
  713. len += snprintf(buf + len, PAGE_SIZE - len, "\n");
  714. }
  715. if (len >= PAGE_SIZE) {
  716. pr_warn_once("Thermal transition table exceeds PAGE_SIZE. Disabling\n");
  717. return -EFBIG;
  718. }
  719. return len;
  720. }
  721. static DEVICE_ATTR_RO(total_trans);
  722. static DEVICE_ATTR_RO(time_in_state_ms);
  723. static DEVICE_ATTR_WO(reset);
  724. static DEVICE_ATTR_RO(trans_table);
  725. static struct attribute *cooling_device_stats_attrs[] = {
  726. &dev_attr_total_trans.attr,
  727. &dev_attr_time_in_state_ms.attr,
  728. &dev_attr_reset.attr,
  729. &dev_attr_trans_table.attr,
  730. NULL
  731. };
  732. static const struct attribute_group cooling_device_stats_attr_group = {
  733. .attrs = cooling_device_stats_attrs,
  734. .name = "stats"
  735. };
  736. static void cooling_device_stats_setup(struct thermal_cooling_device *cdev)
  737. {
  738. struct cooling_dev_stats *stats;
  739. unsigned long states;
  740. int var;
  741. if (cdev->ops->get_max_state(cdev, &states))
  742. return;
  743. states++; /* Total number of states is highest state + 1 */
  744. var = sizeof(*stats);
  745. var += sizeof(*stats->time_in_state) * states;
  746. var += sizeof(*stats->trans_table) * states * states;
  747. stats = kzalloc(var, GFP_KERNEL);
  748. if (!stats)
  749. return;
  750. stats->time_in_state = (ktime_t *)(stats + 1);
  751. stats->trans_table = (unsigned int *)(stats->time_in_state + states);
  752. cdev->stats = stats;
  753. stats->last_time = ktime_get();
  754. stats->max_states = states;
  755. spin_lock_init(&stats->lock);
  756. /* Fill the empty slot left in cooling_device_attr_groups */
  757. var = ARRAY_SIZE(cooling_device_attr_groups) - 2;
  758. cooling_device_attr_groups[var] = &cooling_device_stats_attr_group;
  759. }
  760. static void cooling_device_stats_destroy(struct thermal_cooling_device *cdev)
  761. {
  762. kfree(cdev->stats);
  763. cdev->stats = NULL;
  764. }
  765. #else
  766. static inline void
  767. cooling_device_stats_setup(struct thermal_cooling_device *cdev) {}
  768. static inline void
  769. cooling_device_stats_destroy(struct thermal_cooling_device *cdev) {}
  770. #endif /* CONFIG_THERMAL_STATISTICS */
  771. void thermal_cooling_device_setup_sysfs(struct thermal_cooling_device *cdev)
  772. {
  773. cooling_device_stats_setup(cdev);
  774. cdev->device.groups = cooling_device_attr_groups;
  775. }
  776. void thermal_cooling_device_destroy_sysfs(struct thermal_cooling_device *cdev)
  777. {
  778. cooling_device_stats_destroy(cdev);
  779. }
  780. /* these helper will be used only at the time of bindig */
  781. ssize_t
  782. trip_point_show(struct device *dev, struct device_attribute *attr, char *buf)
  783. {
  784. struct thermal_instance *instance;
  785. instance =
  786. container_of(attr, struct thermal_instance, attr);
  787. if (instance->trip == THERMAL_TRIPS_NONE)
  788. return sprintf(buf, "-1\n");
  789. else
  790. return sprintf(buf, "%d\n", instance->trip);
  791. }
  792. ssize_t
  793. weight_show(struct device *dev, struct device_attribute *attr, char *buf)
  794. {
  795. struct thermal_instance *instance;
  796. instance = container_of(attr, struct thermal_instance, weight_attr);
  797. return sprintf(buf, "%d\n", instance->weight);
  798. }
  799. ssize_t weight_store(struct device *dev, struct device_attribute *attr,
  800. const char *buf, size_t count)
  801. {
  802. struct thermal_instance *instance;
  803. int ret, weight;
  804. ret = kstrtoint(buf, 0, &weight);
  805. if (ret)
  806. return ret;
  807. instance = container_of(attr, struct thermal_instance, weight_attr);
  808. instance->weight = weight;
  809. return count;
  810. }