thermal_sys.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  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");
  150. case THERMAL_TRIP_HOT:
  151. return sprintf(buf, "hot");
  152. case THERMAL_TRIP_PASSIVE:
  153. return sprintf(buf, "passive");
  154. case THERMAL_TRIP_ACTIVE:
  155. return sprintf(buf, "active");
  156. default:
  157. return sprintf(buf, "unknown");
  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. if (state && !tz->forced_passive) {
  186. mutex_lock(&thermal_list_lock);
  187. list_for_each_entry(cdev, &thermal_cdev_list, node) {
  188. if (!strncmp("Processor", cdev->type,
  189. sizeof("Processor")))
  190. thermal_zone_bind_cooling_device(tz,
  191. THERMAL_TRIPS_NONE,
  192. cdev);
  193. }
  194. mutex_unlock(&thermal_list_lock);
  195. } else if (!state && tz->forced_passive) {
  196. mutex_lock(&thermal_list_lock);
  197. list_for_each_entry(cdev, &thermal_cdev_list, node) {
  198. if (!strncmp("Processor", cdev->type,
  199. sizeof("Processor")))
  200. thermal_zone_unbind_cooling_device(tz,
  201. THERMAL_TRIPS_NONE,
  202. cdev);
  203. }
  204. mutex_unlock(&thermal_list_lock);
  205. }
  206. tz->tc1 = 1;
  207. tz->tc2 = 1;
  208. if (!tz->passive_delay)
  209. tz->passive_delay = 1000;
  210. if (!tz->polling_delay)
  211. tz->polling_delay = 10000;
  212. tz->forced_passive = state;
  213. thermal_zone_device_update(tz);
  214. return count;
  215. }
  216. static ssize_t
  217. passive_show(struct device *dev, struct device_attribute *attr,
  218. char *buf)
  219. {
  220. struct thermal_zone_device *tz = to_thermal_zone(dev);
  221. return sprintf(buf, "%d\n", tz->forced_passive);
  222. }
  223. static DEVICE_ATTR(type, 0444, type_show, NULL);
  224. static DEVICE_ATTR(temp, 0444, temp_show, NULL);
  225. static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
  226. static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, \
  227. passive_store);
  228. static struct device_attribute trip_point_attrs[] = {
  229. __ATTR(trip_point_0_type, 0444, trip_point_type_show, NULL),
  230. __ATTR(trip_point_0_temp, 0444, trip_point_temp_show, NULL),
  231. __ATTR(trip_point_1_type, 0444, trip_point_type_show, NULL),
  232. __ATTR(trip_point_1_temp, 0444, trip_point_temp_show, NULL),
  233. __ATTR(trip_point_2_type, 0444, trip_point_type_show, NULL),
  234. __ATTR(trip_point_2_temp, 0444, trip_point_temp_show, NULL),
  235. __ATTR(trip_point_3_type, 0444, trip_point_type_show, NULL),
  236. __ATTR(trip_point_3_temp, 0444, trip_point_temp_show, NULL),
  237. __ATTR(trip_point_4_type, 0444, trip_point_type_show, NULL),
  238. __ATTR(trip_point_4_temp, 0444, trip_point_temp_show, NULL),
  239. __ATTR(trip_point_5_type, 0444, trip_point_type_show, NULL),
  240. __ATTR(trip_point_5_temp, 0444, trip_point_temp_show, NULL),
  241. __ATTR(trip_point_6_type, 0444, trip_point_type_show, NULL),
  242. __ATTR(trip_point_6_temp, 0444, trip_point_temp_show, NULL),
  243. __ATTR(trip_point_7_type, 0444, trip_point_type_show, NULL),
  244. __ATTR(trip_point_7_temp, 0444, trip_point_temp_show, NULL),
  245. __ATTR(trip_point_8_type, 0444, trip_point_type_show, NULL),
  246. __ATTR(trip_point_8_temp, 0444, trip_point_temp_show, NULL),
  247. __ATTR(trip_point_9_type, 0444, trip_point_type_show, NULL),
  248. __ATTR(trip_point_9_temp, 0444, trip_point_temp_show, NULL),
  249. __ATTR(trip_point_10_type, 0444, trip_point_type_show, NULL),
  250. __ATTR(trip_point_10_temp, 0444, trip_point_temp_show, NULL),
  251. __ATTR(trip_point_11_type, 0444, trip_point_type_show, NULL),
  252. __ATTR(trip_point_11_temp, 0444, trip_point_temp_show, NULL),
  253. };
  254. #define TRIP_POINT_ATTR_ADD(_dev, _index, result) \
  255. do { \
  256. result = device_create_file(_dev, \
  257. &trip_point_attrs[_index * 2]); \
  258. if (result) \
  259. break; \
  260. result = device_create_file(_dev, \
  261. &trip_point_attrs[_index * 2 + 1]); \
  262. } while (0)
  263. #define TRIP_POINT_ATTR_REMOVE(_dev, _index) \
  264. do { \
  265. device_remove_file(_dev, &trip_point_attrs[_index * 2]); \
  266. device_remove_file(_dev, &trip_point_attrs[_index * 2 + 1]); \
  267. } while (0)
  268. /* sys I/F for cooling device */
  269. #define to_cooling_device(_dev) \
  270. container_of(_dev, struct thermal_cooling_device, device)
  271. static ssize_t
  272. thermal_cooling_device_type_show(struct device *dev,
  273. struct device_attribute *attr, char *buf)
  274. {
  275. struct thermal_cooling_device *cdev = to_cooling_device(dev);
  276. return sprintf(buf, "%s\n", cdev->type);
  277. }
  278. static ssize_t
  279. thermal_cooling_device_max_state_show(struct device *dev,
  280. struct device_attribute *attr, char *buf)
  281. {
  282. struct thermal_cooling_device *cdev = to_cooling_device(dev);
  283. unsigned long state;
  284. int ret;
  285. ret = cdev->ops->get_max_state(cdev, &state);
  286. if (ret)
  287. return ret;
  288. return sprintf(buf, "%ld\n", state);
  289. }
  290. static ssize_t
  291. thermal_cooling_device_cur_state_show(struct device *dev,
  292. struct device_attribute *attr, char *buf)
  293. {
  294. struct thermal_cooling_device *cdev = to_cooling_device(dev);
  295. unsigned long state;
  296. int ret;
  297. ret = cdev->ops->get_cur_state(cdev, &state);
  298. if (ret)
  299. return ret;
  300. return sprintf(buf, "%ld\n", state);
  301. }
  302. static ssize_t
  303. thermal_cooling_device_cur_state_store(struct device *dev,
  304. struct device_attribute *attr,
  305. const char *buf, size_t count)
  306. {
  307. struct thermal_cooling_device *cdev = to_cooling_device(dev);
  308. unsigned long state;
  309. int result;
  310. if (!sscanf(buf, "%ld\n", &state))
  311. return -EINVAL;
  312. if (state < 0)
  313. return -EINVAL;
  314. result = cdev->ops->set_cur_state(cdev, state);
  315. if (result)
  316. return result;
  317. return count;
  318. }
  319. static struct device_attribute dev_attr_cdev_type =
  320. __ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
  321. static DEVICE_ATTR(max_state, 0444,
  322. thermal_cooling_device_max_state_show, NULL);
  323. static DEVICE_ATTR(cur_state, 0644,
  324. thermal_cooling_device_cur_state_show,
  325. thermal_cooling_device_cur_state_store);
  326. static ssize_t
  327. thermal_cooling_device_trip_point_show(struct device *dev,
  328. struct device_attribute *attr, char *buf)
  329. {
  330. struct thermal_cooling_device_instance *instance;
  331. instance =
  332. container_of(attr, struct thermal_cooling_device_instance, attr);
  333. if (instance->trip == THERMAL_TRIPS_NONE)
  334. return sprintf(buf, "-1\n");
  335. else
  336. return sprintf(buf, "%d\n", instance->trip);
  337. }
  338. /* Device management */
  339. #if defined(CONFIG_THERMAL_HWMON)
  340. /* hwmon sys I/F */
  341. #include <linux/hwmon.h>
  342. static LIST_HEAD(thermal_hwmon_list);
  343. static ssize_t
  344. name_show(struct device *dev, struct device_attribute *attr, char *buf)
  345. {
  346. struct thermal_hwmon_device *hwmon = dev->driver_data;
  347. return sprintf(buf, "%s\n", hwmon->type);
  348. }
  349. static DEVICE_ATTR(name, 0444, name_show, NULL);
  350. static ssize_t
  351. temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
  352. {
  353. long temperature;
  354. int ret;
  355. struct thermal_hwmon_attr *hwmon_attr
  356. = container_of(attr, struct thermal_hwmon_attr, attr);
  357. struct thermal_zone_device *tz
  358. = container_of(hwmon_attr, struct thermal_zone_device,
  359. temp_input);
  360. ret = tz->ops->get_temp(tz, &temperature);
  361. if (ret)
  362. return ret;
  363. return sprintf(buf, "%ld\n", temperature);
  364. }
  365. static ssize_t
  366. temp_crit_show(struct device *dev, struct device_attribute *attr,
  367. char *buf)
  368. {
  369. struct thermal_hwmon_attr *hwmon_attr
  370. = container_of(attr, struct thermal_hwmon_attr, attr);
  371. struct thermal_zone_device *tz
  372. = container_of(hwmon_attr, struct thermal_zone_device,
  373. temp_crit);
  374. long temperature;
  375. int ret;
  376. ret = tz->ops->get_trip_temp(tz, 0, &temperature);
  377. if (ret)
  378. return ret;
  379. return sprintf(buf, "%ld\n", temperature);
  380. }
  381. static int
  382. thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
  383. {
  384. struct thermal_hwmon_device *hwmon;
  385. int new_hwmon_device = 1;
  386. int result;
  387. mutex_lock(&thermal_list_lock);
  388. list_for_each_entry(hwmon, &thermal_hwmon_list, node)
  389. if (!strcmp(hwmon->type, tz->type)) {
  390. new_hwmon_device = 0;
  391. mutex_unlock(&thermal_list_lock);
  392. goto register_sys_interface;
  393. }
  394. mutex_unlock(&thermal_list_lock);
  395. hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
  396. if (!hwmon)
  397. return -ENOMEM;
  398. INIT_LIST_HEAD(&hwmon->tz_list);
  399. strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
  400. hwmon->device = hwmon_device_register(NULL);
  401. if (IS_ERR(hwmon->device)) {
  402. result = PTR_ERR(hwmon->device);
  403. goto free_mem;
  404. }
  405. hwmon->device->driver_data = hwmon;
  406. result = device_create_file(hwmon->device, &dev_attr_name);
  407. if (result)
  408. goto unregister_hwmon_device;
  409. register_sys_interface:
  410. tz->hwmon = hwmon;
  411. hwmon->count++;
  412. snprintf(tz->temp_input.name, THERMAL_NAME_LENGTH,
  413. "temp%d_input", hwmon->count);
  414. tz->temp_input.attr.attr.name = tz->temp_input.name;
  415. tz->temp_input.attr.attr.mode = 0444;
  416. tz->temp_input.attr.show = temp_input_show;
  417. result = device_create_file(hwmon->device, &tz->temp_input.attr);
  418. if (result)
  419. goto unregister_hwmon_device;
  420. if (tz->ops->get_crit_temp) {
  421. unsigned long temperature;
  422. if (!tz->ops->get_crit_temp(tz, &temperature)) {
  423. snprintf(tz->temp_crit.name, THERMAL_NAME_LENGTH,
  424. "temp%d_crit", hwmon->count);
  425. tz->temp_crit.attr.attr.name = tz->temp_crit.name;
  426. tz->temp_crit.attr.attr.mode = 0444;
  427. tz->temp_crit.attr.show = temp_crit_show;
  428. result = device_create_file(hwmon->device,
  429. &tz->temp_crit.attr);
  430. if (result)
  431. goto unregister_hwmon_device;
  432. }
  433. }
  434. mutex_lock(&thermal_list_lock);
  435. if (new_hwmon_device)
  436. list_add_tail(&hwmon->node, &thermal_hwmon_list);
  437. list_add_tail(&tz->hwmon_node, &hwmon->tz_list);
  438. mutex_unlock(&thermal_list_lock);
  439. return 0;
  440. unregister_hwmon_device:
  441. device_remove_file(hwmon->device, &tz->temp_crit.attr);
  442. device_remove_file(hwmon->device, &tz->temp_input.attr);
  443. if (new_hwmon_device) {
  444. device_remove_file(hwmon->device, &dev_attr_name);
  445. hwmon_device_unregister(hwmon->device);
  446. }
  447. free_mem:
  448. if (new_hwmon_device)
  449. kfree(hwmon);
  450. return result;
  451. }
  452. static void
  453. thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
  454. {
  455. struct thermal_hwmon_device *hwmon = tz->hwmon;
  456. tz->hwmon = NULL;
  457. device_remove_file(hwmon->device, &tz->temp_input.attr);
  458. device_remove_file(hwmon->device, &tz->temp_crit.attr);
  459. mutex_lock(&thermal_list_lock);
  460. list_del(&tz->hwmon_node);
  461. if (!list_empty(&hwmon->tz_list)) {
  462. mutex_unlock(&thermal_list_lock);
  463. return;
  464. }
  465. list_del(&hwmon->node);
  466. mutex_unlock(&thermal_list_lock);
  467. device_remove_file(hwmon->device, &dev_attr_name);
  468. hwmon_device_unregister(hwmon->device);
  469. kfree(hwmon);
  470. }
  471. #else
  472. static int
  473. thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
  474. {
  475. return 0;
  476. }
  477. static void
  478. thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
  479. {
  480. }
  481. #endif
  482. static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
  483. int delay)
  484. {
  485. cancel_delayed_work(&(tz->poll_queue));
  486. if (!delay)
  487. return;
  488. if (delay > 1000)
  489. schedule_delayed_work(&(tz->poll_queue),
  490. round_jiffies(msecs_to_jiffies(delay)));
  491. else
  492. schedule_delayed_work(&(tz->poll_queue),
  493. msecs_to_jiffies(delay));
  494. }
  495. static void thermal_zone_device_passive(struct thermal_zone_device *tz,
  496. int temp, int trip_temp, int trip)
  497. {
  498. int trend = 0;
  499. struct thermal_cooling_device_instance *instance;
  500. struct thermal_cooling_device *cdev;
  501. long state, max_state;
  502. /*
  503. * Above Trip?
  504. * -----------
  505. * Calculate the thermal trend (using the passive cooling equation)
  506. * and modify the performance limit for all passive cooling devices
  507. * accordingly. Note that we assume symmetry.
  508. */
  509. if (temp >= trip_temp) {
  510. tz->passive = true;
  511. trend = (tz->tc1 * (temp - tz->last_temperature)) +
  512. (tz->tc2 * (temp - trip_temp));
  513. /* Heating up? */
  514. if (trend > 0) {
  515. list_for_each_entry(instance, &tz->cooling_devices,
  516. node) {
  517. if (instance->trip != trip)
  518. continue;
  519. cdev = instance->cdev;
  520. cdev->ops->get_cur_state(cdev, &state);
  521. cdev->ops->get_max_state(cdev, &max_state);
  522. if (state++ < max_state)
  523. cdev->ops->set_cur_state(cdev, state);
  524. }
  525. } else if (trend < 0) { /* Cooling off? */
  526. list_for_each_entry(instance, &tz->cooling_devices,
  527. node) {
  528. if (instance->trip != trip)
  529. continue;
  530. cdev = instance->cdev;
  531. cdev->ops->get_cur_state(cdev, &state);
  532. cdev->ops->get_max_state(cdev, &max_state);
  533. if (state > 0)
  534. cdev->ops->set_cur_state(cdev, --state);
  535. }
  536. }
  537. return;
  538. }
  539. /*
  540. * Below Trip?
  541. * -----------
  542. * Implement passive cooling hysteresis to slowly increase performance
  543. * and avoid thrashing around the passive trip point. Note that we
  544. * assume symmetry.
  545. */
  546. list_for_each_entry(instance, &tz->cooling_devices, node) {
  547. if (instance->trip != trip)
  548. continue;
  549. cdev = instance->cdev;
  550. cdev->ops->get_cur_state(cdev, &state);
  551. cdev->ops->get_max_state(cdev, &max_state);
  552. if (state > 0)
  553. cdev->ops->set_cur_state(cdev, --state);
  554. if (state == 0)
  555. tz->passive = false;
  556. }
  557. }
  558. static void thermal_zone_device_check(struct work_struct *work)
  559. {
  560. struct thermal_zone_device *tz = container_of(work, struct
  561. thermal_zone_device,
  562. poll_queue.work);
  563. thermal_zone_device_update(tz);
  564. }
  565. /**
  566. * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
  567. * @tz: thermal zone device
  568. * @trip: indicates which trip point the cooling devices is
  569. * associated with in this thermal zone.
  570. * @cdev: thermal cooling device
  571. *
  572. * This function is usually called in the thermal zone device .bind callback.
  573. */
  574. int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
  575. int trip,
  576. struct thermal_cooling_device *cdev)
  577. {
  578. struct thermal_cooling_device_instance *dev;
  579. struct thermal_cooling_device_instance *pos;
  580. struct thermal_zone_device *pos1;
  581. struct thermal_cooling_device *pos2;
  582. int result;
  583. if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
  584. return -EINVAL;
  585. list_for_each_entry(pos1, &thermal_tz_list, node) {
  586. if (pos1 == tz)
  587. break;
  588. }
  589. list_for_each_entry(pos2, &thermal_cdev_list, node) {
  590. if (pos2 == cdev)
  591. break;
  592. }
  593. if (tz != pos1 || cdev != pos2)
  594. return -EINVAL;
  595. dev =
  596. kzalloc(sizeof(struct thermal_cooling_device_instance), GFP_KERNEL);
  597. if (!dev)
  598. return -ENOMEM;
  599. dev->tz = tz;
  600. dev->cdev = cdev;
  601. dev->trip = trip;
  602. result = get_idr(&tz->idr, &tz->lock, &dev->id);
  603. if (result)
  604. goto free_mem;
  605. sprintf(dev->name, "cdev%d", dev->id);
  606. result =
  607. sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
  608. if (result)
  609. goto release_idr;
  610. sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
  611. dev->attr.attr.name = dev->attr_name;
  612. dev->attr.attr.mode = 0444;
  613. dev->attr.show = thermal_cooling_device_trip_point_show;
  614. result = device_create_file(&tz->device, &dev->attr);
  615. if (result)
  616. goto remove_symbol_link;
  617. mutex_lock(&tz->lock);
  618. list_for_each_entry(pos, &tz->cooling_devices, node)
  619. if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
  620. result = -EEXIST;
  621. break;
  622. }
  623. if (!result)
  624. list_add_tail(&dev->node, &tz->cooling_devices);
  625. mutex_unlock(&tz->lock);
  626. if (!result)
  627. return 0;
  628. device_remove_file(&tz->device, &dev->attr);
  629. remove_symbol_link:
  630. sysfs_remove_link(&tz->device.kobj, dev->name);
  631. release_idr:
  632. release_idr(&tz->idr, &tz->lock, dev->id);
  633. free_mem:
  634. kfree(dev);
  635. return result;
  636. }
  637. EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
  638. /**
  639. * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
  640. * @tz: thermal zone device
  641. * @trip: indicates which trip point the cooling devices is
  642. * associated with in this thermal zone.
  643. * @cdev: thermal cooling device
  644. *
  645. * This function is usually called in the thermal zone device .unbind callback.
  646. */
  647. int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
  648. int trip,
  649. struct thermal_cooling_device *cdev)
  650. {
  651. struct thermal_cooling_device_instance *pos, *next;
  652. mutex_lock(&tz->lock);
  653. list_for_each_entry_safe(pos, next, &tz->cooling_devices, node) {
  654. if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
  655. list_del(&pos->node);
  656. mutex_unlock(&tz->lock);
  657. goto unbind;
  658. }
  659. }
  660. mutex_unlock(&tz->lock);
  661. return -ENODEV;
  662. unbind:
  663. device_remove_file(&tz->device, &pos->attr);
  664. sysfs_remove_link(&tz->device.kobj, pos->name);
  665. release_idr(&tz->idr, &tz->lock, pos->id);
  666. kfree(pos);
  667. return 0;
  668. }
  669. EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
  670. static void thermal_release(struct device *dev)
  671. {
  672. struct thermal_zone_device *tz;
  673. struct thermal_cooling_device *cdev;
  674. if (!strncmp(dev_name(dev), "thermal_zone", sizeof "thermal_zone" - 1)) {
  675. tz = to_thermal_zone(dev);
  676. kfree(tz);
  677. } else {
  678. cdev = to_cooling_device(dev);
  679. kfree(cdev);
  680. }
  681. }
  682. static struct class thermal_class = {
  683. .name = "thermal",
  684. .dev_release = thermal_release,
  685. };
  686. /**
  687. * thermal_cooling_device_register - register a new thermal cooling device
  688. * @type: the thermal cooling device type.
  689. * @devdata: device private data.
  690. * @ops: standard thermal cooling devices callbacks.
  691. */
  692. struct thermal_cooling_device *thermal_cooling_device_register(char *type,
  693. void *devdata,
  694. struct
  695. thermal_cooling_device_ops
  696. *ops)
  697. {
  698. struct thermal_cooling_device *cdev;
  699. struct thermal_zone_device *pos;
  700. int result;
  701. if (strlen(type) >= THERMAL_NAME_LENGTH)
  702. return ERR_PTR(-EINVAL);
  703. if (!ops || !ops->get_max_state || !ops->get_cur_state ||
  704. !ops->set_cur_state)
  705. return ERR_PTR(-EINVAL);
  706. cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
  707. if (!cdev)
  708. return ERR_PTR(-ENOMEM);
  709. result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
  710. if (result) {
  711. kfree(cdev);
  712. return ERR_PTR(result);
  713. }
  714. strcpy(cdev->type, type);
  715. cdev->ops = ops;
  716. cdev->device.class = &thermal_class;
  717. cdev->devdata = devdata;
  718. dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
  719. result = device_register(&cdev->device);
  720. if (result) {
  721. release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
  722. kfree(cdev);
  723. return ERR_PTR(result);
  724. }
  725. /* sys I/F */
  726. if (type) {
  727. result = device_create_file(&cdev->device, &dev_attr_cdev_type);
  728. if (result)
  729. goto unregister;
  730. }
  731. result = device_create_file(&cdev->device, &dev_attr_max_state);
  732. if (result)
  733. goto unregister;
  734. result = device_create_file(&cdev->device, &dev_attr_cur_state);
  735. if (result)
  736. goto unregister;
  737. mutex_lock(&thermal_list_lock);
  738. list_add(&cdev->node, &thermal_cdev_list);
  739. list_for_each_entry(pos, &thermal_tz_list, node) {
  740. if (!pos->ops->bind)
  741. continue;
  742. result = pos->ops->bind(pos, cdev);
  743. if (result)
  744. break;
  745. }
  746. mutex_unlock(&thermal_list_lock);
  747. if (!result)
  748. return cdev;
  749. unregister:
  750. release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
  751. device_unregister(&cdev->device);
  752. return ERR_PTR(result);
  753. }
  754. EXPORT_SYMBOL(thermal_cooling_device_register);
  755. /**
  756. * thermal_cooling_device_unregister - removes the registered thermal cooling device
  757. * @cdev: the thermal cooling device to remove.
  758. *
  759. * thermal_cooling_device_unregister() must be called when the device is no
  760. * longer needed.
  761. */
  762. void thermal_cooling_device_unregister(struct
  763. thermal_cooling_device
  764. *cdev)
  765. {
  766. struct thermal_zone_device *tz;
  767. struct thermal_cooling_device *pos = NULL;
  768. if (!cdev)
  769. return;
  770. mutex_lock(&thermal_list_lock);
  771. list_for_each_entry(pos, &thermal_cdev_list, node)
  772. if (pos == cdev)
  773. break;
  774. if (pos != cdev) {
  775. /* thermal cooling device not found */
  776. mutex_unlock(&thermal_list_lock);
  777. return;
  778. }
  779. list_del(&cdev->node);
  780. list_for_each_entry(tz, &thermal_tz_list, node) {
  781. if (!tz->ops->unbind)
  782. continue;
  783. tz->ops->unbind(tz, cdev);
  784. }
  785. mutex_unlock(&thermal_list_lock);
  786. if (cdev->type[0])
  787. device_remove_file(&cdev->device, &dev_attr_cdev_type);
  788. device_remove_file(&cdev->device, &dev_attr_max_state);
  789. device_remove_file(&cdev->device, &dev_attr_cur_state);
  790. release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
  791. device_unregister(&cdev->device);
  792. return;
  793. }
  794. EXPORT_SYMBOL(thermal_cooling_device_unregister);
  795. /**
  796. * thermal_zone_device_update - force an update of a thermal zone's state
  797. * @ttz: the thermal zone to update
  798. */
  799. void thermal_zone_device_update(struct thermal_zone_device *tz)
  800. {
  801. int count, ret = 0;
  802. long temp, trip_temp;
  803. enum thermal_trip_type trip_type;
  804. struct thermal_cooling_device_instance *instance;
  805. struct thermal_cooling_device *cdev;
  806. mutex_lock(&tz->lock);
  807. tz->ops->get_temp(tz, &temp);
  808. for (count = 0; count < tz->trips; count++) {
  809. tz->ops->get_trip_type(tz, count, &trip_type);
  810. tz->ops->get_trip_temp(tz, count, &trip_temp);
  811. switch (trip_type) {
  812. case THERMAL_TRIP_CRITICAL:
  813. if (temp > trip_temp) {
  814. if (tz->ops->notify)
  815. ret = tz->ops->notify(tz, count,
  816. trip_type);
  817. if (!ret) {
  818. printk(KERN_EMERG
  819. "Critical temperature reached (%ld C), shutting down.\n",
  820. temp/1000);
  821. orderly_poweroff(true);
  822. }
  823. }
  824. break;
  825. case THERMAL_TRIP_HOT:
  826. if (temp > trip_temp)
  827. if (tz->ops->notify)
  828. tz->ops->notify(tz, count, trip_type);
  829. break;
  830. case THERMAL_TRIP_ACTIVE:
  831. list_for_each_entry(instance, &tz->cooling_devices,
  832. node) {
  833. if (instance->trip != count)
  834. continue;
  835. cdev = instance->cdev;
  836. if (temp > trip_temp)
  837. cdev->ops->set_cur_state(cdev, 1);
  838. else
  839. cdev->ops->set_cur_state(cdev, 0);
  840. }
  841. break;
  842. case THERMAL_TRIP_PASSIVE:
  843. if (temp > trip_temp || tz->passive)
  844. thermal_zone_device_passive(tz, temp,
  845. trip_temp, count);
  846. break;
  847. }
  848. }
  849. if (tz->forced_passive)
  850. thermal_zone_device_passive(tz, temp, tz->forced_passive,
  851. THERMAL_TRIPS_NONE);
  852. tz->last_temperature = temp;
  853. if (tz->passive)
  854. thermal_zone_device_set_polling(tz, tz->passive_delay);
  855. else if (tz->polling_delay)
  856. thermal_zone_device_set_polling(tz, tz->polling_delay);
  857. mutex_unlock(&tz->lock);
  858. }
  859. EXPORT_SYMBOL(thermal_zone_device_update);
  860. /**
  861. * thermal_zone_device_register - register a new thermal zone device
  862. * @type: the thermal zone device type
  863. * @trips: the number of trip points the thermal zone support
  864. * @devdata: private device data
  865. * @ops: standard thermal zone device callbacks
  866. * @tc1: thermal coefficient 1 for passive calculations
  867. * @tc2: thermal coefficient 2 for passive calculations
  868. * @passive_delay: number of milliseconds to wait between polls when
  869. * performing passive cooling
  870. * @polling_delay: number of milliseconds to wait between polls when checking
  871. * whether trip points have been crossed (0 for interrupt
  872. * driven systems)
  873. *
  874. * thermal_zone_device_unregister() must be called when the device is no
  875. * longer needed. The passive cooling formula uses tc1 and tc2 as described in
  876. * section 11.1.5.1 of the ACPI specification 3.0.
  877. */
  878. struct thermal_zone_device *thermal_zone_device_register(char *type,
  879. int trips,
  880. void *devdata, struct
  881. thermal_zone_device_ops
  882. *ops, int tc1, int
  883. tc2,
  884. int passive_delay,
  885. int polling_delay)
  886. {
  887. struct thermal_zone_device *tz;
  888. struct thermal_cooling_device *pos;
  889. enum thermal_trip_type trip_type;
  890. int result;
  891. int count;
  892. int passive = 0;
  893. if (strlen(type) >= THERMAL_NAME_LENGTH)
  894. return ERR_PTR(-EINVAL);
  895. if (trips > THERMAL_MAX_TRIPS || trips < 0)
  896. return ERR_PTR(-EINVAL);
  897. if (!ops || !ops->get_temp)
  898. return ERR_PTR(-EINVAL);
  899. tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
  900. if (!tz)
  901. return ERR_PTR(-ENOMEM);
  902. INIT_LIST_HEAD(&tz->cooling_devices);
  903. idr_init(&tz->idr);
  904. mutex_init(&tz->lock);
  905. result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
  906. if (result) {
  907. kfree(tz);
  908. return ERR_PTR(result);
  909. }
  910. strcpy(tz->type, type);
  911. tz->ops = ops;
  912. tz->device.class = &thermal_class;
  913. tz->devdata = devdata;
  914. tz->trips = trips;
  915. tz->tc1 = tc1;
  916. tz->tc2 = tc2;
  917. tz->passive_delay = passive_delay;
  918. tz->polling_delay = polling_delay;
  919. dev_set_name(&tz->device, "thermal_zone%d", tz->id);
  920. result = device_register(&tz->device);
  921. if (result) {
  922. release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
  923. kfree(tz);
  924. return ERR_PTR(result);
  925. }
  926. /* sys I/F */
  927. if (type) {
  928. result = device_create_file(&tz->device, &dev_attr_type);
  929. if (result)
  930. goto unregister;
  931. }
  932. result = device_create_file(&tz->device, &dev_attr_temp);
  933. if (result)
  934. goto unregister;
  935. if (ops->get_mode) {
  936. result = device_create_file(&tz->device, &dev_attr_mode);
  937. if (result)
  938. goto unregister;
  939. }
  940. for (count = 0; count < trips; count++) {
  941. TRIP_POINT_ATTR_ADD(&tz->device, count, result);
  942. if (result)
  943. goto unregister;
  944. tz->ops->get_trip_type(tz, count, &trip_type);
  945. if (trip_type == THERMAL_TRIP_PASSIVE)
  946. passive = 1;
  947. }
  948. if (!passive)
  949. result = device_create_file(&tz->device,
  950. &dev_attr_passive);
  951. if (result)
  952. goto unregister;
  953. result = thermal_add_hwmon_sysfs(tz);
  954. if (result)
  955. goto unregister;
  956. mutex_lock(&thermal_list_lock);
  957. list_add_tail(&tz->node, &thermal_tz_list);
  958. if (ops->bind)
  959. list_for_each_entry(pos, &thermal_cdev_list, node) {
  960. result = ops->bind(tz, pos);
  961. if (result)
  962. break;
  963. }
  964. mutex_unlock(&thermal_list_lock);
  965. INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
  966. thermal_zone_device_update(tz);
  967. if (!result)
  968. return tz;
  969. unregister:
  970. release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
  971. device_unregister(&tz->device);
  972. return ERR_PTR(result);
  973. }
  974. EXPORT_SYMBOL(thermal_zone_device_register);
  975. /**
  976. * thermal_device_unregister - removes the registered thermal zone device
  977. * @tz: the thermal zone device to remove
  978. */
  979. void thermal_zone_device_unregister(struct thermal_zone_device *tz)
  980. {
  981. struct thermal_cooling_device *cdev;
  982. struct thermal_zone_device *pos = NULL;
  983. int count;
  984. if (!tz)
  985. return;
  986. mutex_lock(&thermal_list_lock);
  987. list_for_each_entry(pos, &thermal_tz_list, node)
  988. if (pos == tz)
  989. break;
  990. if (pos != tz) {
  991. /* thermal zone device not found */
  992. mutex_unlock(&thermal_list_lock);
  993. return;
  994. }
  995. list_del(&tz->node);
  996. if (tz->ops->unbind)
  997. list_for_each_entry(cdev, &thermal_cdev_list, node)
  998. tz->ops->unbind(tz, cdev);
  999. mutex_unlock(&thermal_list_lock);
  1000. thermal_zone_device_set_polling(tz, 0);
  1001. if (tz->type[0])
  1002. device_remove_file(&tz->device, &dev_attr_type);
  1003. device_remove_file(&tz->device, &dev_attr_temp);
  1004. if (tz->ops->get_mode)
  1005. device_remove_file(&tz->device, &dev_attr_mode);
  1006. for (count = 0; count < tz->trips; count++)
  1007. TRIP_POINT_ATTR_REMOVE(&tz->device, count);
  1008. thermal_remove_hwmon_sysfs(tz);
  1009. release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
  1010. idr_destroy(&tz->idr);
  1011. mutex_destroy(&tz->lock);
  1012. device_unregister(&tz->device);
  1013. return;
  1014. }
  1015. EXPORT_SYMBOL(thermal_zone_device_unregister);
  1016. static int __init thermal_init(void)
  1017. {
  1018. int result = 0;
  1019. result = class_register(&thermal_class);
  1020. if (result) {
  1021. idr_destroy(&thermal_tz_idr);
  1022. idr_destroy(&thermal_cdev_idr);
  1023. mutex_destroy(&thermal_idr_lock);
  1024. mutex_destroy(&thermal_list_lock);
  1025. }
  1026. return result;
  1027. }
  1028. static void __exit thermal_exit(void)
  1029. {
  1030. class_unregister(&thermal_class);
  1031. idr_destroy(&thermal_tz_idr);
  1032. idr_destroy(&thermal_cdev_idr);
  1033. mutex_destroy(&thermal_idr_lock);
  1034. mutex_destroy(&thermal_list_lock);
  1035. }
  1036. subsys_initcall(thermal_init);
  1037. module_exit(thermal_exit);