thermal_sys.c 32 KB

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