thermal_sys.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  1. /*
  2. * thermal.c - Generic Thermal Management Sysfs support.
  3. *
  4. * Copyright (C) 2008 Intel Corp
  5. * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
  6. * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
  7. *
  8. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; version 2 of the License.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  22. *
  23. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  24. */
  25. #include <linux/module.h>
  26. #include <linux/device.h>
  27. #include <linux/err.h>
  28. #include <linux/kdev_t.h>
  29. #include <linux/idr.h>
  30. #include <linux/thermal.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/reboot.h>
  33. MODULE_AUTHOR("Zhang Rui");
  34. MODULE_DESCRIPTION("Generic thermal management sysfs support");
  35. MODULE_LICENSE("GPL");
  36. #define PREFIX "Thermal: "
  37. struct thermal_cooling_device_instance {
  38. int id;
  39. char name[THERMAL_NAME_LENGTH];
  40. struct thermal_zone_device *tz;
  41. struct thermal_cooling_device *cdev;
  42. int trip;
  43. char attr_name[THERMAL_NAME_LENGTH];
  44. struct device_attribute attr;
  45. struct list_head node;
  46. };
  47. static DEFINE_IDR(thermal_tz_idr);
  48. static DEFINE_IDR(thermal_cdev_idr);
  49. static DEFINE_MUTEX(thermal_idr_lock);
  50. static LIST_HEAD(thermal_tz_list);
  51. static LIST_HEAD(thermal_cdev_list);
  52. static DEFINE_MUTEX(thermal_list_lock);
  53. static int get_idr(struct idr *idr, struct mutex *lock, int *id)
  54. {
  55. int err;
  56. again:
  57. if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
  58. return -ENOMEM;
  59. if (lock)
  60. mutex_lock(lock);
  61. err = idr_get_new(idr, NULL, id);
  62. if (lock)
  63. mutex_unlock(lock);
  64. if (unlikely(err == -EAGAIN))
  65. goto again;
  66. else if (unlikely(err))
  67. return err;
  68. *id = *id & MAX_ID_MASK;
  69. return 0;
  70. }
  71. static void release_idr(struct idr *idr, struct mutex *lock, int id)
  72. {
  73. if (lock)
  74. mutex_lock(lock);
  75. idr_remove(idr, id);
  76. if (lock)
  77. mutex_unlock(lock);
  78. }
  79. /* sys I/F for thermal zone */
  80. #define to_thermal_zone(_dev) \
  81. container_of(_dev, struct thermal_zone_device, device)
  82. static ssize_t
  83. type_show(struct device *dev, struct device_attribute *attr, char *buf)
  84. {
  85. struct thermal_zone_device *tz = to_thermal_zone(dev);
  86. return sprintf(buf, "%s\n", tz->type);
  87. }
  88. static ssize_t
  89. temp_show(struct device *dev, struct device_attribute *attr, char *buf)
  90. {
  91. struct thermal_zone_device *tz = to_thermal_zone(dev);
  92. long temperature;
  93. int ret;
  94. if (!tz->ops->get_temp)
  95. return -EPERM;
  96. ret = tz->ops->get_temp(tz, &temperature);
  97. if (ret)
  98. return ret;
  99. return sprintf(buf, "%ld\n", temperature);
  100. }
  101. static ssize_t
  102. mode_show(struct device *dev, struct device_attribute *attr, char *buf)
  103. {
  104. struct thermal_zone_device *tz = to_thermal_zone(dev);
  105. enum thermal_device_mode mode;
  106. int result;
  107. if (!tz->ops->get_mode)
  108. return -EPERM;
  109. result = tz->ops->get_mode(tz, &mode);
  110. if (result)
  111. return result;
  112. return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
  113. : "disabled");
  114. }
  115. static ssize_t
  116. mode_store(struct device *dev, struct device_attribute *attr,
  117. const char *buf, size_t count)
  118. {
  119. struct thermal_zone_device *tz = to_thermal_zone(dev);
  120. int result;
  121. if (!tz->ops->set_mode)
  122. return -EPERM;
  123. if (!strncmp(buf, "enabled", sizeof("enabled")))
  124. result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
  125. else if (!strncmp(buf, "disabled", sizeof("disabled")))
  126. result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
  127. else
  128. result = -EINVAL;
  129. if (result)
  130. return result;
  131. return count;
  132. }
  133. static ssize_t
  134. trip_point_type_show(struct device *dev, struct device_attribute *attr,
  135. char *buf)
  136. {
  137. struct thermal_zone_device *tz = to_thermal_zone(dev);
  138. enum thermal_trip_type type;
  139. int trip, result;
  140. if (!tz->ops->get_trip_type)
  141. return -EPERM;
  142. if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
  143. return -EINVAL;
  144. result = tz->ops->get_trip_type(tz, trip, &type);
  145. if (result)
  146. return result;
  147. switch (type) {
  148. case THERMAL_TRIP_CRITICAL:
  149. return sprintf(buf, "critical\n");
  150. case THERMAL_TRIP_HOT:
  151. return sprintf(buf, "hot\n");
  152. case THERMAL_TRIP_PASSIVE:
  153. return sprintf(buf, "passive\n");
  154. case THERMAL_TRIP_ACTIVE:
  155. return sprintf(buf, "active\n");
  156. default:
  157. return sprintf(buf, "unknown\n");
  158. }
  159. }
  160. static ssize_t
  161. trip_point_temp_show(struct device *dev, struct device_attribute *attr,
  162. char *buf)
  163. {
  164. struct thermal_zone_device *tz = to_thermal_zone(dev);
  165. int trip, ret;
  166. long temperature;
  167. if (!tz->ops->get_trip_temp)
  168. return -EPERM;
  169. if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
  170. return -EINVAL;
  171. ret = tz->ops->get_trip_temp(tz, trip, &temperature);
  172. if (ret)
  173. return ret;
  174. return sprintf(buf, "%ld\n", temperature);
  175. }
  176. static ssize_t
  177. passive_store(struct device *dev, struct device_attribute *attr,
  178. const char *buf, size_t count)
  179. {
  180. struct thermal_zone_device *tz = to_thermal_zone(dev);
  181. struct thermal_cooling_device *cdev = NULL;
  182. int state;
  183. if (!sscanf(buf, "%d\n", &state))
  184. return -EINVAL;
  185. /* sanity check: values below 1000 millicelcius don't make sense
  186. * and can cause the system to go into a thermal heart attack
  187. */
  188. if (state && state < 1000)
  189. return -EINVAL;
  190. if (state && !tz->forced_passive) {
  191. mutex_lock(&thermal_list_lock);
  192. list_for_each_entry(cdev, &thermal_cdev_list, node) {
  193. if (!strncmp("Processor", cdev->type,
  194. sizeof("Processor")))
  195. thermal_zone_bind_cooling_device(tz,
  196. THERMAL_TRIPS_NONE,
  197. cdev);
  198. }
  199. mutex_unlock(&thermal_list_lock);
  200. if (!tz->passive_delay)
  201. tz->passive_delay = 1000;
  202. } else if (!state && tz->forced_passive) {
  203. mutex_lock(&thermal_list_lock);
  204. list_for_each_entry(cdev, &thermal_cdev_list, node) {
  205. if (!strncmp("Processor", cdev->type,
  206. sizeof("Processor")))
  207. thermal_zone_unbind_cooling_device(tz,
  208. THERMAL_TRIPS_NONE,
  209. cdev);
  210. }
  211. mutex_unlock(&thermal_list_lock);
  212. tz->passive_delay = 0;
  213. }
  214. tz->tc1 = 1;
  215. tz->tc2 = 1;
  216. tz->forced_passive = state;
  217. thermal_zone_device_update(tz);
  218. return count;
  219. }
  220. static ssize_t
  221. passive_show(struct device *dev, struct device_attribute *attr,
  222. char *buf)
  223. {
  224. struct thermal_zone_device *tz = to_thermal_zone(dev);
  225. return sprintf(buf, "%d\n", tz->forced_passive);
  226. }
  227. static DEVICE_ATTR(type, 0444, type_show, NULL);
  228. static DEVICE_ATTR(temp, 0444, temp_show, NULL);
  229. static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
  230. static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, \
  231. passive_store);
  232. static struct device_attribute trip_point_attrs[] = {
  233. __ATTR(trip_point_0_type, 0444, trip_point_type_show, NULL),
  234. __ATTR(trip_point_0_temp, 0444, trip_point_temp_show, NULL),
  235. __ATTR(trip_point_1_type, 0444, trip_point_type_show, NULL),
  236. __ATTR(trip_point_1_temp, 0444, trip_point_temp_show, NULL),
  237. __ATTR(trip_point_2_type, 0444, trip_point_type_show, NULL),
  238. __ATTR(trip_point_2_temp, 0444, trip_point_temp_show, NULL),
  239. __ATTR(trip_point_3_type, 0444, trip_point_type_show, NULL),
  240. __ATTR(trip_point_3_temp, 0444, trip_point_temp_show, NULL),
  241. __ATTR(trip_point_4_type, 0444, trip_point_type_show, NULL),
  242. __ATTR(trip_point_4_temp, 0444, trip_point_temp_show, NULL),
  243. __ATTR(trip_point_5_type, 0444, trip_point_type_show, NULL),
  244. __ATTR(trip_point_5_temp, 0444, trip_point_temp_show, NULL),
  245. __ATTR(trip_point_6_type, 0444, trip_point_type_show, NULL),
  246. __ATTR(trip_point_6_temp, 0444, trip_point_temp_show, NULL),
  247. __ATTR(trip_point_7_type, 0444, trip_point_type_show, NULL),
  248. __ATTR(trip_point_7_temp, 0444, trip_point_temp_show, NULL),
  249. __ATTR(trip_point_8_type, 0444, trip_point_type_show, NULL),
  250. __ATTR(trip_point_8_temp, 0444, trip_point_temp_show, NULL),
  251. __ATTR(trip_point_9_type, 0444, trip_point_type_show, NULL),
  252. __ATTR(trip_point_9_temp, 0444, trip_point_temp_show, NULL),
  253. __ATTR(trip_point_10_type, 0444, trip_point_type_show, NULL),
  254. __ATTR(trip_point_10_temp, 0444, trip_point_temp_show, NULL),
  255. __ATTR(trip_point_11_type, 0444, trip_point_type_show, NULL),
  256. __ATTR(trip_point_11_temp, 0444, trip_point_temp_show, NULL),
  257. };
  258. #define TRIP_POINT_ATTR_ADD(_dev, _index, result) \
  259. do { \
  260. result = device_create_file(_dev, \
  261. &trip_point_attrs[_index * 2]); \
  262. if (result) \
  263. break; \
  264. result = device_create_file(_dev, \
  265. &trip_point_attrs[_index * 2 + 1]); \
  266. } while (0)
  267. #define TRIP_POINT_ATTR_REMOVE(_dev, _index) \
  268. do { \
  269. device_remove_file(_dev, &trip_point_attrs[_index * 2]); \
  270. device_remove_file(_dev, &trip_point_attrs[_index * 2 + 1]); \
  271. } while (0)
  272. /* sys I/F for cooling device */
  273. #define to_cooling_device(_dev) \
  274. container_of(_dev, struct thermal_cooling_device, device)
  275. static ssize_t
  276. thermal_cooling_device_type_show(struct device *dev,
  277. struct device_attribute *attr, char *buf)
  278. {
  279. struct thermal_cooling_device *cdev = to_cooling_device(dev);
  280. return sprintf(buf, "%s\n", cdev->type);
  281. }
  282. static ssize_t
  283. thermal_cooling_device_max_state_show(struct device *dev,
  284. struct device_attribute *attr, char *buf)
  285. {
  286. struct thermal_cooling_device *cdev = to_cooling_device(dev);
  287. unsigned long state;
  288. int ret;
  289. ret = cdev->ops->get_max_state(cdev, &state);
  290. if (ret)
  291. return ret;
  292. return sprintf(buf, "%ld\n", state);
  293. }
  294. static ssize_t
  295. thermal_cooling_device_cur_state_show(struct device *dev,
  296. struct device_attribute *attr, char *buf)
  297. {
  298. struct thermal_cooling_device *cdev = to_cooling_device(dev);
  299. unsigned long state;
  300. int ret;
  301. ret = cdev->ops->get_cur_state(cdev, &state);
  302. if (ret)
  303. return ret;
  304. return sprintf(buf, "%ld\n", state);
  305. }
  306. static ssize_t
  307. thermal_cooling_device_cur_state_store(struct device *dev,
  308. struct device_attribute *attr,
  309. const char *buf, size_t count)
  310. {
  311. struct thermal_cooling_device *cdev = to_cooling_device(dev);
  312. unsigned long state;
  313. int result;
  314. if (!sscanf(buf, "%ld\n", &state))
  315. return -EINVAL;
  316. if ((long)state < 0)
  317. return -EINVAL;
  318. result = cdev->ops->set_cur_state(cdev, state);
  319. if (result)
  320. return result;
  321. return count;
  322. }
  323. static struct device_attribute dev_attr_cdev_type =
  324. __ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
  325. static DEVICE_ATTR(max_state, 0444,
  326. thermal_cooling_device_max_state_show, NULL);
  327. static DEVICE_ATTR(cur_state, 0644,
  328. thermal_cooling_device_cur_state_show,
  329. thermal_cooling_device_cur_state_store);
  330. static ssize_t
  331. thermal_cooling_device_trip_point_show(struct device *dev,
  332. struct device_attribute *attr, char *buf)
  333. {
  334. struct thermal_cooling_device_instance *instance;
  335. instance =
  336. container_of(attr, struct thermal_cooling_device_instance, attr);
  337. if (instance->trip == THERMAL_TRIPS_NONE)
  338. return sprintf(buf, "-1\n");
  339. else
  340. return sprintf(buf, "%d\n", instance->trip);
  341. }
  342. /* Device management */
  343. #if defined(CONFIG_THERMAL_HWMON)
  344. /* hwmon sys I/F */
  345. #include <linux/hwmon.h>
  346. static LIST_HEAD(thermal_hwmon_list);
  347. static ssize_t
  348. name_show(struct device *dev, struct device_attribute *attr, char *buf)
  349. {
  350. struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
  351. return sprintf(buf, "%s\n", hwmon->type);
  352. }
  353. static DEVICE_ATTR(name, 0444, name_show, NULL);
  354. static ssize_t
  355. temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
  356. {
  357. long temperature;
  358. int ret;
  359. struct thermal_hwmon_attr *hwmon_attr
  360. = container_of(attr, struct thermal_hwmon_attr, attr);
  361. struct thermal_zone_device *tz
  362. = container_of(hwmon_attr, struct thermal_zone_device,
  363. temp_input);
  364. ret = tz->ops->get_temp(tz, &temperature);
  365. if (ret)
  366. return ret;
  367. return sprintf(buf, "%ld\n", temperature);
  368. }
  369. static ssize_t
  370. temp_crit_show(struct device *dev, struct device_attribute *attr,
  371. char *buf)
  372. {
  373. struct thermal_hwmon_attr *hwmon_attr
  374. = container_of(attr, struct thermal_hwmon_attr, attr);
  375. struct thermal_zone_device *tz
  376. = container_of(hwmon_attr, struct thermal_zone_device,
  377. temp_crit);
  378. long temperature;
  379. int ret;
  380. ret = tz->ops->get_trip_temp(tz, 0, &temperature);
  381. if (ret)
  382. return ret;
  383. return sprintf(buf, "%ld\n", temperature);
  384. }
  385. static int
  386. thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
  387. {
  388. struct thermal_hwmon_device *hwmon;
  389. int new_hwmon_device = 1;
  390. int result;
  391. mutex_lock(&thermal_list_lock);
  392. list_for_each_entry(hwmon, &thermal_hwmon_list, node)
  393. if (!strcmp(hwmon->type, tz->type)) {
  394. new_hwmon_device = 0;
  395. mutex_unlock(&thermal_list_lock);
  396. goto register_sys_interface;
  397. }
  398. mutex_unlock(&thermal_list_lock);
  399. hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
  400. if (!hwmon)
  401. return -ENOMEM;
  402. INIT_LIST_HEAD(&hwmon->tz_list);
  403. strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
  404. hwmon->device = hwmon_device_register(NULL);
  405. if (IS_ERR(hwmon->device)) {
  406. result = PTR_ERR(hwmon->device);
  407. goto free_mem;
  408. }
  409. dev_set_drvdata(hwmon->device, hwmon);
  410. result = device_create_file(hwmon->device, &dev_attr_name);
  411. if (result)
  412. goto unregister_hwmon_device;
  413. register_sys_interface:
  414. tz->hwmon = hwmon;
  415. hwmon->count++;
  416. snprintf(tz->temp_input.name, THERMAL_NAME_LENGTH,
  417. "temp%d_input", hwmon->count);
  418. tz->temp_input.attr.attr.name = tz->temp_input.name;
  419. tz->temp_input.attr.attr.mode = 0444;
  420. tz->temp_input.attr.show = temp_input_show;
  421. result = device_create_file(hwmon->device, &tz->temp_input.attr);
  422. if (result)
  423. goto unregister_hwmon_device;
  424. if (tz->ops->get_crit_temp) {
  425. unsigned long temperature;
  426. if (!tz->ops->get_crit_temp(tz, &temperature)) {
  427. snprintf(tz->temp_crit.name, THERMAL_NAME_LENGTH,
  428. "temp%d_crit", hwmon->count);
  429. tz->temp_crit.attr.attr.name = tz->temp_crit.name;
  430. tz->temp_crit.attr.attr.mode = 0444;
  431. tz->temp_crit.attr.show = temp_crit_show;
  432. result = device_create_file(hwmon->device,
  433. &tz->temp_crit.attr);
  434. if (result)
  435. goto unregister_hwmon_device;
  436. }
  437. }
  438. mutex_lock(&thermal_list_lock);
  439. if (new_hwmon_device)
  440. list_add_tail(&hwmon->node, &thermal_hwmon_list);
  441. list_add_tail(&tz->hwmon_node, &hwmon->tz_list);
  442. mutex_unlock(&thermal_list_lock);
  443. return 0;
  444. unregister_hwmon_device:
  445. device_remove_file(hwmon->device, &tz->temp_crit.attr);
  446. device_remove_file(hwmon->device, &tz->temp_input.attr);
  447. if (new_hwmon_device) {
  448. device_remove_file(hwmon->device, &dev_attr_name);
  449. hwmon_device_unregister(hwmon->device);
  450. }
  451. free_mem:
  452. if (new_hwmon_device)
  453. kfree(hwmon);
  454. return result;
  455. }
  456. static void
  457. thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
  458. {
  459. struct thermal_hwmon_device *hwmon = tz->hwmon;
  460. tz->hwmon = NULL;
  461. device_remove_file(hwmon->device, &tz->temp_input.attr);
  462. device_remove_file(hwmon->device, &tz->temp_crit.attr);
  463. mutex_lock(&thermal_list_lock);
  464. list_del(&tz->hwmon_node);
  465. if (!list_empty(&hwmon->tz_list)) {
  466. mutex_unlock(&thermal_list_lock);
  467. return;
  468. }
  469. list_del(&hwmon->node);
  470. mutex_unlock(&thermal_list_lock);
  471. device_remove_file(hwmon->device, &dev_attr_name);
  472. hwmon_device_unregister(hwmon->device);
  473. kfree(hwmon);
  474. }
  475. #else
  476. static int
  477. thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
  478. {
  479. return 0;
  480. }
  481. static void
  482. thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
  483. {
  484. }
  485. #endif
  486. static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
  487. int delay)
  488. {
  489. cancel_delayed_work(&(tz->poll_queue));
  490. if (!delay)
  491. return;
  492. if (delay > 1000)
  493. schedule_delayed_work(&(tz->poll_queue),
  494. round_jiffies(msecs_to_jiffies(delay)));
  495. else
  496. schedule_delayed_work(&(tz->poll_queue),
  497. msecs_to_jiffies(delay));
  498. }
  499. static void thermal_zone_device_passive(struct thermal_zone_device *tz,
  500. int temp, int trip_temp, int trip)
  501. {
  502. int trend = 0;
  503. struct thermal_cooling_device_instance *instance;
  504. struct thermal_cooling_device *cdev;
  505. long state, max_state;
  506. /*
  507. * Above Trip?
  508. * -----------
  509. * Calculate the thermal trend (using the passive cooling equation)
  510. * and modify the performance limit for all passive cooling devices
  511. * accordingly. Note that we assume symmetry.
  512. */
  513. if (temp >= trip_temp) {
  514. tz->passive = true;
  515. trend = (tz->tc1 * (temp - tz->last_temperature)) +
  516. (tz->tc2 * (temp - trip_temp));
  517. /* Heating up? */
  518. if (trend > 0) {
  519. list_for_each_entry(instance, &tz->cooling_devices,
  520. node) {
  521. if (instance->trip != trip)
  522. continue;
  523. cdev = instance->cdev;
  524. cdev->ops->get_cur_state(cdev, &state);
  525. cdev->ops->get_max_state(cdev, &max_state);
  526. if (state++ < max_state)
  527. cdev->ops->set_cur_state(cdev, state);
  528. }
  529. } else if (trend < 0) { /* Cooling off? */
  530. list_for_each_entry(instance, &tz->cooling_devices,
  531. node) {
  532. if (instance->trip != trip)
  533. continue;
  534. cdev = instance->cdev;
  535. cdev->ops->get_cur_state(cdev, &state);
  536. cdev->ops->get_max_state(cdev, &max_state);
  537. if (state > 0)
  538. cdev->ops->set_cur_state(cdev, --state);
  539. }
  540. }
  541. return;
  542. }
  543. /*
  544. * Below Trip?
  545. * -----------
  546. * Implement passive cooling hysteresis to slowly increase performance
  547. * and avoid thrashing around the passive trip point. Note that we
  548. * assume symmetry.
  549. */
  550. list_for_each_entry(instance, &tz->cooling_devices, node) {
  551. if (instance->trip != trip)
  552. continue;
  553. cdev = instance->cdev;
  554. cdev->ops->get_cur_state(cdev, &state);
  555. cdev->ops->get_max_state(cdev, &max_state);
  556. if (state > 0)
  557. cdev->ops->set_cur_state(cdev, --state);
  558. if (state == 0)
  559. tz->passive = false;
  560. }
  561. }
  562. static void thermal_zone_device_check(struct work_struct *work)
  563. {
  564. struct thermal_zone_device *tz = container_of(work, struct
  565. thermal_zone_device,
  566. poll_queue.work);
  567. thermal_zone_device_update(tz);
  568. }
  569. /**
  570. * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
  571. * @tz: thermal zone device
  572. * @trip: indicates which trip point the cooling devices is
  573. * associated with in this thermal zone.
  574. * @cdev: thermal cooling device
  575. *
  576. * This function is usually called in the thermal zone device .bind callback.
  577. */
  578. int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
  579. int trip,
  580. struct thermal_cooling_device *cdev)
  581. {
  582. struct thermal_cooling_device_instance *dev;
  583. struct thermal_cooling_device_instance *pos;
  584. struct thermal_zone_device *pos1;
  585. struct thermal_cooling_device *pos2;
  586. int result;
  587. if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
  588. return -EINVAL;
  589. list_for_each_entry(pos1, &thermal_tz_list, node) {
  590. if (pos1 == tz)
  591. break;
  592. }
  593. list_for_each_entry(pos2, &thermal_cdev_list, node) {
  594. if (pos2 == cdev)
  595. break;
  596. }
  597. if (tz != pos1 || cdev != pos2)
  598. return -EINVAL;
  599. dev =
  600. kzalloc(sizeof(struct thermal_cooling_device_instance), GFP_KERNEL);
  601. if (!dev)
  602. return -ENOMEM;
  603. dev->tz = tz;
  604. dev->cdev = cdev;
  605. dev->trip = trip;
  606. result = get_idr(&tz->idr, &tz->lock, &dev->id);
  607. if (result)
  608. goto free_mem;
  609. sprintf(dev->name, "cdev%d", dev->id);
  610. result =
  611. sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
  612. if (result)
  613. goto release_idr;
  614. sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
  615. dev->attr.attr.name = dev->attr_name;
  616. dev->attr.attr.mode = 0444;
  617. dev->attr.show = thermal_cooling_device_trip_point_show;
  618. result = device_create_file(&tz->device, &dev->attr);
  619. if (result)
  620. goto remove_symbol_link;
  621. mutex_lock(&tz->lock);
  622. list_for_each_entry(pos, &tz->cooling_devices, node)
  623. if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
  624. result = -EEXIST;
  625. break;
  626. }
  627. if (!result)
  628. list_add_tail(&dev->node, &tz->cooling_devices);
  629. mutex_unlock(&tz->lock);
  630. if (!result)
  631. return 0;
  632. device_remove_file(&tz->device, &dev->attr);
  633. remove_symbol_link:
  634. sysfs_remove_link(&tz->device.kobj, dev->name);
  635. release_idr:
  636. release_idr(&tz->idr, &tz->lock, dev->id);
  637. free_mem:
  638. kfree(dev);
  639. return result;
  640. }
  641. EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
  642. /**
  643. * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
  644. * @tz: thermal zone device
  645. * @trip: indicates which trip point the cooling devices is
  646. * associated with in this thermal zone.
  647. * @cdev: thermal cooling device
  648. *
  649. * This function is usually called in the thermal zone device .unbind callback.
  650. */
  651. int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
  652. int trip,
  653. struct thermal_cooling_device *cdev)
  654. {
  655. struct thermal_cooling_device_instance *pos, *next;
  656. mutex_lock(&tz->lock);
  657. list_for_each_entry_safe(pos, next, &tz->cooling_devices, node) {
  658. if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
  659. list_del(&pos->node);
  660. mutex_unlock(&tz->lock);
  661. goto unbind;
  662. }
  663. }
  664. mutex_unlock(&tz->lock);
  665. return -ENODEV;
  666. unbind:
  667. device_remove_file(&tz->device, &pos->attr);
  668. sysfs_remove_link(&tz->device.kobj, pos->name);
  669. release_idr(&tz->idr, &tz->lock, pos->id);
  670. kfree(pos);
  671. return 0;
  672. }
  673. EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
  674. static void thermal_release(struct device *dev)
  675. {
  676. struct thermal_zone_device *tz;
  677. struct thermal_cooling_device *cdev;
  678. if (!strncmp(dev_name(dev), "thermal_zone", sizeof "thermal_zone" - 1)) {
  679. tz = to_thermal_zone(dev);
  680. kfree(tz);
  681. } else {
  682. cdev = to_cooling_device(dev);
  683. kfree(cdev);
  684. }
  685. }
  686. static struct class thermal_class = {
  687. .name = "thermal",
  688. .dev_release = thermal_release,
  689. };
  690. /**
  691. * thermal_cooling_device_register - register a new thermal cooling device
  692. * @type: the thermal cooling device type.
  693. * @devdata: device private data.
  694. * @ops: standard thermal cooling devices callbacks.
  695. */
  696. struct thermal_cooling_device *thermal_cooling_device_register(char *type,
  697. void *devdata,
  698. struct
  699. thermal_cooling_device_ops
  700. *ops)
  701. {
  702. struct thermal_cooling_device *cdev;
  703. struct thermal_zone_device *pos;
  704. int result;
  705. if (strlen(type) >= THERMAL_NAME_LENGTH)
  706. return ERR_PTR(-EINVAL);
  707. if (!ops || !ops->get_max_state || !ops->get_cur_state ||
  708. !ops->set_cur_state)
  709. return ERR_PTR(-EINVAL);
  710. cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
  711. if (!cdev)
  712. return ERR_PTR(-ENOMEM);
  713. result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
  714. if (result) {
  715. kfree(cdev);
  716. return ERR_PTR(result);
  717. }
  718. strcpy(cdev->type, type);
  719. cdev->ops = ops;
  720. cdev->device.class = &thermal_class;
  721. cdev->devdata = devdata;
  722. dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
  723. result = device_register(&cdev->device);
  724. if (result) {
  725. release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
  726. kfree(cdev);
  727. return ERR_PTR(result);
  728. }
  729. /* sys I/F */
  730. if (type) {
  731. result = device_create_file(&cdev->device, &dev_attr_cdev_type);
  732. if (result)
  733. goto unregister;
  734. }
  735. result = device_create_file(&cdev->device, &dev_attr_max_state);
  736. if (result)
  737. goto unregister;
  738. result = device_create_file(&cdev->device, &dev_attr_cur_state);
  739. if (result)
  740. goto unregister;
  741. mutex_lock(&thermal_list_lock);
  742. list_add(&cdev->node, &thermal_cdev_list);
  743. list_for_each_entry(pos, &thermal_tz_list, node) {
  744. if (!pos->ops->bind)
  745. continue;
  746. result = pos->ops->bind(pos, cdev);
  747. if (result)
  748. break;
  749. }
  750. mutex_unlock(&thermal_list_lock);
  751. if (!result)
  752. return cdev;
  753. unregister:
  754. release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
  755. device_unregister(&cdev->device);
  756. return ERR_PTR(result);
  757. }
  758. EXPORT_SYMBOL(thermal_cooling_device_register);
  759. /**
  760. * thermal_cooling_device_unregister - removes the registered thermal cooling device
  761. * @cdev: the thermal cooling device to remove.
  762. *
  763. * thermal_cooling_device_unregister() must be called when the device is no
  764. * longer needed.
  765. */
  766. void thermal_cooling_device_unregister(struct
  767. thermal_cooling_device
  768. *cdev)
  769. {
  770. struct thermal_zone_device *tz;
  771. struct thermal_cooling_device *pos = NULL;
  772. if (!cdev)
  773. return;
  774. mutex_lock(&thermal_list_lock);
  775. list_for_each_entry(pos, &thermal_cdev_list, node)
  776. if (pos == cdev)
  777. break;
  778. if (pos != cdev) {
  779. /* thermal cooling device not found */
  780. mutex_unlock(&thermal_list_lock);
  781. return;
  782. }
  783. list_del(&cdev->node);
  784. list_for_each_entry(tz, &thermal_tz_list, node) {
  785. if (!tz->ops->unbind)
  786. continue;
  787. tz->ops->unbind(tz, cdev);
  788. }
  789. mutex_unlock(&thermal_list_lock);
  790. if (cdev->type[0])
  791. device_remove_file(&cdev->device, &dev_attr_cdev_type);
  792. device_remove_file(&cdev->device, &dev_attr_max_state);
  793. device_remove_file(&cdev->device, &dev_attr_cur_state);
  794. release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
  795. device_unregister(&cdev->device);
  796. return;
  797. }
  798. EXPORT_SYMBOL(thermal_cooling_device_unregister);
  799. /**
  800. * thermal_zone_device_update - force an update of a thermal zone's state
  801. * @ttz: the thermal zone to update
  802. */
  803. void thermal_zone_device_update(struct thermal_zone_device *tz)
  804. {
  805. int count, ret = 0;
  806. long temp, trip_temp;
  807. enum thermal_trip_type trip_type;
  808. struct thermal_cooling_device_instance *instance;
  809. struct thermal_cooling_device *cdev;
  810. mutex_lock(&tz->lock);
  811. if (tz->ops->get_temp(tz, &temp)) {
  812. /* get_temp failed - retry it later */
  813. printk(KERN_WARNING PREFIX "failed to read out thermal zone "
  814. "%d\n", tz->id);
  815. goto leave;
  816. }
  817. for (count = 0; count < tz->trips; count++) {
  818. tz->ops->get_trip_type(tz, count, &trip_type);
  819. tz->ops->get_trip_temp(tz, count, &trip_temp);
  820. switch (trip_type) {
  821. case THERMAL_TRIP_CRITICAL:
  822. if (temp >= trip_temp) {
  823. if (tz->ops->notify)
  824. ret = tz->ops->notify(tz, count,
  825. trip_type);
  826. if (!ret) {
  827. printk(KERN_EMERG
  828. "Critical temperature reached (%ld C), shutting down.\n",
  829. temp/1000);
  830. orderly_poweroff(true);
  831. }
  832. }
  833. break;
  834. case THERMAL_TRIP_HOT:
  835. if (temp >= trip_temp)
  836. if (tz->ops->notify)
  837. tz->ops->notify(tz, count, trip_type);
  838. break;
  839. case THERMAL_TRIP_ACTIVE:
  840. list_for_each_entry(instance, &tz->cooling_devices,
  841. node) {
  842. if (instance->trip != count)
  843. continue;
  844. cdev = instance->cdev;
  845. if (temp >= trip_temp)
  846. cdev->ops->set_cur_state(cdev, 1);
  847. else
  848. cdev->ops->set_cur_state(cdev, 0);
  849. }
  850. break;
  851. case THERMAL_TRIP_PASSIVE:
  852. if (temp >= trip_temp || tz->passive)
  853. thermal_zone_device_passive(tz, temp,
  854. trip_temp, count);
  855. break;
  856. }
  857. }
  858. if (tz->forced_passive)
  859. thermal_zone_device_passive(tz, temp, tz->forced_passive,
  860. THERMAL_TRIPS_NONE);
  861. tz->last_temperature = temp;
  862. leave:
  863. if (tz->passive)
  864. thermal_zone_device_set_polling(tz, tz->passive_delay);
  865. else if (tz->polling_delay)
  866. thermal_zone_device_set_polling(tz, tz->polling_delay);
  867. else
  868. thermal_zone_device_set_polling(tz, 0);
  869. mutex_unlock(&tz->lock);
  870. }
  871. EXPORT_SYMBOL(thermal_zone_device_update);
  872. /**
  873. * thermal_zone_device_register - register a new thermal zone device
  874. * @type: the thermal zone device type
  875. * @trips: the number of trip points the thermal zone support
  876. * @devdata: private device data
  877. * @ops: standard thermal zone device callbacks
  878. * @tc1: thermal coefficient 1 for passive calculations
  879. * @tc2: thermal coefficient 2 for passive calculations
  880. * @passive_delay: number of milliseconds to wait between polls when
  881. * performing passive cooling
  882. * @polling_delay: number of milliseconds to wait between polls when checking
  883. * whether trip points have been crossed (0 for interrupt
  884. * driven systems)
  885. *
  886. * thermal_zone_device_unregister() must be called when the device is no
  887. * longer needed. The passive cooling formula uses tc1 and tc2 as described in
  888. * section 11.1.5.1 of the ACPI specification 3.0.
  889. */
  890. struct thermal_zone_device *thermal_zone_device_register(char *type,
  891. int trips,
  892. void *devdata, struct
  893. thermal_zone_device_ops
  894. *ops, int tc1, int
  895. tc2,
  896. int passive_delay,
  897. int polling_delay)
  898. {
  899. struct thermal_zone_device *tz;
  900. struct thermal_cooling_device *pos;
  901. enum thermal_trip_type trip_type;
  902. int result;
  903. int count;
  904. int passive = 0;
  905. if (strlen(type) >= THERMAL_NAME_LENGTH)
  906. return ERR_PTR(-EINVAL);
  907. if (trips > THERMAL_MAX_TRIPS || trips < 0)
  908. return ERR_PTR(-EINVAL);
  909. if (!ops || !ops->get_temp)
  910. return ERR_PTR(-EINVAL);
  911. tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
  912. if (!tz)
  913. return ERR_PTR(-ENOMEM);
  914. INIT_LIST_HEAD(&tz->cooling_devices);
  915. idr_init(&tz->idr);
  916. mutex_init(&tz->lock);
  917. result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
  918. if (result) {
  919. kfree(tz);
  920. return ERR_PTR(result);
  921. }
  922. strcpy(tz->type, type);
  923. tz->ops = ops;
  924. tz->device.class = &thermal_class;
  925. tz->devdata = devdata;
  926. tz->trips = trips;
  927. tz->tc1 = tc1;
  928. tz->tc2 = tc2;
  929. tz->passive_delay = passive_delay;
  930. tz->polling_delay = polling_delay;
  931. dev_set_name(&tz->device, "thermal_zone%d", tz->id);
  932. result = device_register(&tz->device);
  933. if (result) {
  934. release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
  935. kfree(tz);
  936. return ERR_PTR(result);
  937. }
  938. /* sys I/F */
  939. if (type) {
  940. result = device_create_file(&tz->device, &dev_attr_type);
  941. if (result)
  942. goto unregister;
  943. }
  944. result = device_create_file(&tz->device, &dev_attr_temp);
  945. if (result)
  946. goto unregister;
  947. if (ops->get_mode) {
  948. result = device_create_file(&tz->device, &dev_attr_mode);
  949. if (result)
  950. goto unregister;
  951. }
  952. for (count = 0; count < trips; count++) {
  953. TRIP_POINT_ATTR_ADD(&tz->device, count, result);
  954. if (result)
  955. goto unregister;
  956. tz->ops->get_trip_type(tz, count, &trip_type);
  957. if (trip_type == THERMAL_TRIP_PASSIVE)
  958. passive = 1;
  959. }
  960. if (!passive)
  961. result = device_create_file(&tz->device,
  962. &dev_attr_passive);
  963. if (result)
  964. goto unregister;
  965. result = thermal_add_hwmon_sysfs(tz);
  966. if (result)
  967. goto unregister;
  968. mutex_lock(&thermal_list_lock);
  969. list_add_tail(&tz->node, &thermal_tz_list);
  970. if (ops->bind)
  971. list_for_each_entry(pos, &thermal_cdev_list, node) {
  972. result = ops->bind(tz, pos);
  973. if (result)
  974. break;
  975. }
  976. mutex_unlock(&thermal_list_lock);
  977. INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
  978. thermal_zone_device_update(tz);
  979. if (!result)
  980. return tz;
  981. unregister:
  982. release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
  983. device_unregister(&tz->device);
  984. return ERR_PTR(result);
  985. }
  986. EXPORT_SYMBOL(thermal_zone_device_register);
  987. /**
  988. * thermal_device_unregister - removes the registered thermal zone device
  989. * @tz: the thermal zone device to remove
  990. */
  991. void thermal_zone_device_unregister(struct thermal_zone_device *tz)
  992. {
  993. struct thermal_cooling_device *cdev;
  994. struct thermal_zone_device *pos = NULL;
  995. int count;
  996. if (!tz)
  997. return;
  998. mutex_lock(&thermal_list_lock);
  999. list_for_each_entry(pos, &thermal_tz_list, node)
  1000. if (pos == tz)
  1001. break;
  1002. if (pos != tz) {
  1003. /* thermal zone device not found */
  1004. mutex_unlock(&thermal_list_lock);
  1005. return;
  1006. }
  1007. list_del(&tz->node);
  1008. if (tz->ops->unbind)
  1009. list_for_each_entry(cdev, &thermal_cdev_list, node)
  1010. tz->ops->unbind(tz, cdev);
  1011. mutex_unlock(&thermal_list_lock);
  1012. thermal_zone_device_set_polling(tz, 0);
  1013. if (tz->type[0])
  1014. device_remove_file(&tz->device, &dev_attr_type);
  1015. device_remove_file(&tz->device, &dev_attr_temp);
  1016. if (tz->ops->get_mode)
  1017. device_remove_file(&tz->device, &dev_attr_mode);
  1018. for (count = 0; count < tz->trips; count++)
  1019. TRIP_POINT_ATTR_REMOVE(&tz->device, count);
  1020. thermal_remove_hwmon_sysfs(tz);
  1021. release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
  1022. idr_destroy(&tz->idr);
  1023. mutex_destroy(&tz->lock);
  1024. device_unregister(&tz->device);
  1025. return;
  1026. }
  1027. EXPORT_SYMBOL(thermal_zone_device_unregister);
  1028. static int __init thermal_init(void)
  1029. {
  1030. int result = 0;
  1031. result = class_register(&thermal_class);
  1032. if (result) {
  1033. idr_destroy(&thermal_tz_idr);
  1034. idr_destroy(&thermal_cdev_idr);
  1035. mutex_destroy(&thermal_idr_lock);
  1036. mutex_destroy(&thermal_list_lock);
  1037. }
  1038. return result;
  1039. }
  1040. static void __exit thermal_exit(void)
  1041. {
  1042. class_unregister(&thermal_class);
  1043. idr_destroy(&thermal_tz_idr);
  1044. idr_destroy(&thermal_cdev_idr);
  1045. mutex_destroy(&thermal_idr_lock);
  1046. mutex_destroy(&thermal_list_lock);
  1047. }
  1048. subsys_initcall(thermal_init);
  1049. module_exit(thermal_exit);