thermal_sys.c 29 KB

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