thermal_core.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541
  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. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; version 2 of the License.
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/module.h>
  14. #include <linux/device.h>
  15. #include <linux/err.h>
  16. #include <linux/slab.h>
  17. #include <linux/kdev_t.h>
  18. #include <linux/idr.h>
  19. #include <linux/thermal.h>
  20. #include <linux/reboot.h>
  21. #include <linux/string.h>
  22. #include <linux/of.h>
  23. #include <net/netlink.h>
  24. #include <net/genetlink.h>
  25. #include <linux/suspend.h>
  26. #define CREATE_TRACE_POINTS
  27. #include <trace/events/thermal.h>
  28. #include "thermal_core.h"
  29. #include "thermal_hwmon.h"
  30. MODULE_AUTHOR("Zhang Rui");
  31. MODULE_DESCRIPTION("Generic thermal management sysfs support");
  32. MODULE_LICENSE("GPL v2");
  33. static DEFINE_IDR(thermal_tz_idr);
  34. static DEFINE_IDR(thermal_cdev_idr);
  35. static DEFINE_MUTEX(thermal_idr_lock);
  36. static LIST_HEAD(thermal_tz_list);
  37. static LIST_HEAD(thermal_cdev_list);
  38. static LIST_HEAD(thermal_governor_list);
  39. static DEFINE_MUTEX(thermal_list_lock);
  40. static DEFINE_MUTEX(thermal_governor_lock);
  41. static atomic_t in_suspend;
  42. static struct thermal_governor *def_governor;
  43. /*
  44. * Governor section: set of functions to handle thermal governors
  45. *
  46. * Functions to help in the life cycle of thermal governors within
  47. * the thermal core and by the thermal governor code.
  48. */
  49. static struct thermal_governor *__find_governor(const char *name)
  50. {
  51. struct thermal_governor *pos;
  52. if (!name || !name[0])
  53. return def_governor;
  54. list_for_each_entry(pos, &thermal_governor_list, governor_list)
  55. if (!strncasecmp(name, pos->name, THERMAL_NAME_LENGTH))
  56. return pos;
  57. return NULL;
  58. }
  59. /**
  60. * bind_previous_governor() - bind the previous governor of the thermal zone
  61. * @tz: a valid pointer to a struct thermal_zone_device
  62. * @failed_gov_name: the name of the governor that failed to register
  63. *
  64. * Register the previous governor of the thermal zone after a new
  65. * governor has failed to be bound.
  66. */
  67. static void bind_previous_governor(struct thermal_zone_device *tz,
  68. const char *failed_gov_name)
  69. {
  70. if (tz->governor && tz->governor->bind_to_tz) {
  71. if (tz->governor->bind_to_tz(tz)) {
  72. dev_err(&tz->device,
  73. "governor %s failed to bind and the previous one (%s) failed to bind again, thermal zone %s has no governor\n",
  74. failed_gov_name, tz->governor->name, tz->type);
  75. tz->governor = NULL;
  76. }
  77. }
  78. }
  79. /**
  80. * thermal_set_governor() - Switch to another governor
  81. * @tz: a valid pointer to a struct thermal_zone_device
  82. * @new_gov: pointer to the new governor
  83. *
  84. * Change the governor of thermal zone @tz.
  85. *
  86. * Return: 0 on success, an error if the new governor's bind_to_tz() failed.
  87. */
  88. static int thermal_set_governor(struct thermal_zone_device *tz,
  89. struct thermal_governor *new_gov)
  90. {
  91. int ret = 0;
  92. if (tz->governor && tz->governor->unbind_from_tz)
  93. tz->governor->unbind_from_tz(tz);
  94. if (new_gov && new_gov->bind_to_tz) {
  95. ret = new_gov->bind_to_tz(tz);
  96. if (ret) {
  97. bind_previous_governor(tz, new_gov->name);
  98. return ret;
  99. }
  100. }
  101. tz->governor = new_gov;
  102. return ret;
  103. }
  104. int thermal_register_governor(struct thermal_governor *governor)
  105. {
  106. int err;
  107. const char *name;
  108. struct thermal_zone_device *pos;
  109. if (!governor)
  110. return -EINVAL;
  111. mutex_lock(&thermal_governor_lock);
  112. err = -EBUSY;
  113. if (!__find_governor(governor->name)) {
  114. bool match_default;
  115. err = 0;
  116. list_add(&governor->governor_list, &thermal_governor_list);
  117. match_default = !strncmp(governor->name,
  118. DEFAULT_THERMAL_GOVERNOR,
  119. THERMAL_NAME_LENGTH);
  120. if (!def_governor && match_default)
  121. def_governor = governor;
  122. }
  123. mutex_lock(&thermal_list_lock);
  124. list_for_each_entry(pos, &thermal_tz_list, node) {
  125. /*
  126. * only thermal zones with specified tz->tzp->governor_name
  127. * may run with tz->govenor unset
  128. */
  129. if (pos->governor)
  130. continue;
  131. name = pos->tzp->governor_name;
  132. if (!strncasecmp(name, governor->name, THERMAL_NAME_LENGTH)) {
  133. int ret;
  134. ret = thermal_set_governor(pos, governor);
  135. if (ret)
  136. dev_err(&pos->device,
  137. "Failed to set governor %s for thermal zone %s: %d\n",
  138. governor->name, pos->type, ret);
  139. }
  140. }
  141. mutex_unlock(&thermal_list_lock);
  142. mutex_unlock(&thermal_governor_lock);
  143. return err;
  144. }
  145. void thermal_unregister_governor(struct thermal_governor *governor)
  146. {
  147. struct thermal_zone_device *pos;
  148. if (!governor)
  149. return;
  150. mutex_lock(&thermal_governor_lock);
  151. if (!__find_governor(governor->name))
  152. goto exit;
  153. mutex_lock(&thermal_list_lock);
  154. list_for_each_entry(pos, &thermal_tz_list, node) {
  155. if (!strncasecmp(pos->governor->name, governor->name,
  156. THERMAL_NAME_LENGTH))
  157. thermal_set_governor(pos, NULL);
  158. }
  159. mutex_unlock(&thermal_list_lock);
  160. list_del(&governor->governor_list);
  161. exit:
  162. mutex_unlock(&thermal_governor_lock);
  163. }
  164. int thermal_zone_device_set_policy(struct thermal_zone_device *tz,
  165. char *policy)
  166. {
  167. struct thermal_governor *gov;
  168. int ret = -EINVAL;
  169. mutex_lock(&thermal_governor_lock);
  170. mutex_lock(&tz->lock);
  171. gov = __find_governor(strim(policy));
  172. if (!gov)
  173. goto exit;
  174. ret = thermal_set_governor(tz, gov);
  175. exit:
  176. mutex_unlock(&tz->lock);
  177. mutex_unlock(&thermal_governor_lock);
  178. return ret;
  179. }
  180. int thermal_build_list_of_policies(char *buf)
  181. {
  182. struct thermal_governor *pos;
  183. ssize_t count = 0;
  184. ssize_t size = PAGE_SIZE;
  185. mutex_lock(&thermal_governor_lock);
  186. list_for_each_entry(pos, &thermal_governor_list, governor_list) {
  187. size = PAGE_SIZE - count;
  188. count += scnprintf(buf + count, size, "%s ", pos->name);
  189. }
  190. count += scnprintf(buf + count, size, "\n");
  191. mutex_unlock(&thermal_governor_lock);
  192. return count;
  193. }
  194. static int __init thermal_register_governors(void)
  195. {
  196. int result;
  197. result = thermal_gov_step_wise_register();
  198. if (result)
  199. return result;
  200. result = thermal_gov_fair_share_register();
  201. if (result)
  202. return result;
  203. result = thermal_gov_bang_bang_register();
  204. if (result)
  205. return result;
  206. result = thermal_gov_user_space_register();
  207. if (result)
  208. return result;
  209. return thermal_gov_power_allocator_register();
  210. }
  211. static void thermal_unregister_governors(void)
  212. {
  213. thermal_gov_step_wise_unregister();
  214. thermal_gov_fair_share_unregister();
  215. thermal_gov_bang_bang_unregister();
  216. thermal_gov_user_space_unregister();
  217. thermal_gov_power_allocator_unregister();
  218. }
  219. /*
  220. * Zone update section: main control loop applied to each zone while monitoring
  221. *
  222. * in polling mode. The monitoring is done using a workqueue.
  223. * Same update may be done on a zone by calling thermal_zone_device_update().
  224. *
  225. * An update means:
  226. * - Non-critical trips will invoke the governor responsible for that zone;
  227. * - Hot trips will produce a notification to userspace;
  228. * - Critical trip point will cause a system shutdown.
  229. */
  230. static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
  231. int delay)
  232. {
  233. if (delay > 1000)
  234. mod_delayed_work(system_freezable_wq, &tz->poll_queue,
  235. round_jiffies(msecs_to_jiffies(delay)));
  236. else if (delay)
  237. mod_delayed_work(system_freezable_wq, &tz->poll_queue,
  238. msecs_to_jiffies(delay));
  239. else
  240. cancel_delayed_work(&tz->poll_queue);
  241. }
  242. static void monitor_thermal_zone(struct thermal_zone_device *tz)
  243. {
  244. mutex_lock(&tz->lock);
  245. if (tz->passive)
  246. thermal_zone_device_set_polling(tz, tz->passive_delay);
  247. else if (tz->polling_delay)
  248. thermal_zone_device_set_polling(tz, tz->polling_delay);
  249. else
  250. thermal_zone_device_set_polling(tz, 0);
  251. mutex_unlock(&tz->lock);
  252. }
  253. static void handle_non_critical_trips(struct thermal_zone_device *tz,
  254. int trip,
  255. enum thermal_trip_type trip_type)
  256. {
  257. tz->governor ? tz->governor->throttle(tz, trip) :
  258. def_governor->throttle(tz, trip);
  259. }
  260. static void handle_critical_trips(struct thermal_zone_device *tz,
  261. int trip, enum thermal_trip_type trip_type)
  262. {
  263. int trip_temp;
  264. tz->ops->get_trip_temp(tz, trip, &trip_temp);
  265. /* If we have not crossed the trip_temp, we do not care. */
  266. if (trip_temp <= 0 || tz->temperature < trip_temp)
  267. return;
  268. trace_thermal_zone_trip(tz, trip, trip_type);
  269. if (tz->ops->notify)
  270. tz->ops->notify(tz, trip, trip_type);
  271. if (trip_type == THERMAL_TRIP_CRITICAL) {
  272. dev_emerg(&tz->device,
  273. "critical temperature reached(%d C),shutting down\n",
  274. tz->temperature / 1000);
  275. orderly_poweroff(true);
  276. }
  277. }
  278. static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
  279. {
  280. enum thermal_trip_type type;
  281. /* Ignore disabled trip points */
  282. if (test_bit(trip, &tz->trips_disabled))
  283. return;
  284. tz->ops->get_trip_type(tz, trip, &type);
  285. if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
  286. handle_critical_trips(tz, trip, type);
  287. else
  288. handle_non_critical_trips(tz, trip, type);
  289. /*
  290. * Alright, we handled this trip successfully.
  291. * So, start monitoring again.
  292. */
  293. monitor_thermal_zone(tz);
  294. }
  295. static void update_temperature(struct thermal_zone_device *tz)
  296. {
  297. int temp, ret;
  298. ret = thermal_zone_get_temp(tz, &temp);
  299. if (ret) {
  300. if (ret != -EAGAIN)
  301. dev_warn(&tz->device,
  302. "failed to read out thermal zone (%d)\n",
  303. ret);
  304. return;
  305. }
  306. mutex_lock(&tz->lock);
  307. tz->last_temperature = tz->temperature;
  308. tz->temperature = temp;
  309. mutex_unlock(&tz->lock);
  310. trace_thermal_temperature(tz);
  311. if (tz->last_temperature == THERMAL_TEMP_INVALID)
  312. dev_dbg(&tz->device, "last_temperature N/A, current_temperature=%d\n",
  313. tz->temperature);
  314. else
  315. dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n",
  316. tz->last_temperature, tz->temperature);
  317. }
  318. static void thermal_zone_device_reset(struct thermal_zone_device *tz)
  319. {
  320. struct thermal_instance *pos;
  321. tz->temperature = THERMAL_TEMP_INVALID;
  322. tz->passive = 0;
  323. list_for_each_entry(pos, &tz->thermal_instances, tz_node)
  324. pos->initialized = false;
  325. }
  326. void thermal_zone_device_update(struct thermal_zone_device *tz,
  327. enum thermal_notify_event event)
  328. {
  329. int count;
  330. if (atomic_read(&in_suspend))
  331. return;
  332. if (!tz->ops->get_temp)
  333. return;
  334. update_temperature(tz);
  335. thermal_zone_set_trips(tz);
  336. tz->notify_event = event;
  337. for (count = 0; count < tz->trips; count++)
  338. handle_thermal_trip(tz, count);
  339. }
  340. EXPORT_SYMBOL_GPL(thermal_zone_device_update);
  341. /**
  342. * thermal_notify_framework - Sensor drivers use this API to notify framework
  343. * @tz: thermal zone device
  344. * @trip: indicates which trip point has been crossed
  345. *
  346. * This function handles the trip events from sensor drivers. It starts
  347. * throttling the cooling devices according to the policy configured.
  348. * For CRITICAL and HOT trip points, this notifies the respective drivers,
  349. * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
  350. * The throttling policy is based on the configured platform data; if no
  351. * platform data is provided, this uses the step_wise throttling policy.
  352. */
  353. void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
  354. {
  355. handle_thermal_trip(tz, trip);
  356. }
  357. EXPORT_SYMBOL_GPL(thermal_notify_framework);
  358. static void thermal_zone_device_check(struct work_struct *work)
  359. {
  360. struct thermal_zone_device *tz = container_of(work, struct
  361. thermal_zone_device,
  362. poll_queue.work);
  363. thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
  364. }
  365. /*
  366. * Power actor section: interface to power actors to estimate power
  367. *
  368. * Set of functions used to interact to cooling devices that know
  369. * how to estimate their devices power consumption.
  370. */
  371. /**
  372. * power_actor_get_max_power() - get the maximum power that a cdev can consume
  373. * @cdev: pointer to &thermal_cooling_device
  374. * @tz: a valid thermal zone device pointer
  375. * @max_power: pointer in which to store the maximum power
  376. *
  377. * Calculate the maximum power consumption in milliwats that the
  378. * cooling device can currently consume and store it in @max_power.
  379. *
  380. * Return: 0 on success, -EINVAL if @cdev doesn't support the
  381. * power_actor API or -E* on other error.
  382. */
  383. int power_actor_get_max_power(struct thermal_cooling_device *cdev,
  384. struct thermal_zone_device *tz, u32 *max_power)
  385. {
  386. if (!cdev_is_power_actor(cdev))
  387. return -EINVAL;
  388. return cdev->ops->state2power(cdev, tz, 0, max_power);
  389. }
  390. /**
  391. * power_actor_get_min_power() - get the mainimum power that a cdev can consume
  392. * @cdev: pointer to &thermal_cooling_device
  393. * @tz: a valid thermal zone device pointer
  394. * @min_power: pointer in which to store the minimum power
  395. *
  396. * Calculate the minimum power consumption in milliwatts that the
  397. * cooling device can currently consume and store it in @min_power.
  398. *
  399. * Return: 0 on success, -EINVAL if @cdev doesn't support the
  400. * power_actor API or -E* on other error.
  401. */
  402. int power_actor_get_min_power(struct thermal_cooling_device *cdev,
  403. struct thermal_zone_device *tz, u32 *min_power)
  404. {
  405. unsigned long max_state;
  406. int ret;
  407. if (!cdev_is_power_actor(cdev))
  408. return -EINVAL;
  409. ret = cdev->ops->get_max_state(cdev, &max_state);
  410. if (ret)
  411. return ret;
  412. return cdev->ops->state2power(cdev, tz, max_state, min_power);
  413. }
  414. /**
  415. * power_actor_set_power() - limit the maximum power a cooling device consumes
  416. * @cdev: pointer to &thermal_cooling_device
  417. * @instance: thermal instance to update
  418. * @power: the power in milliwatts
  419. *
  420. * Set the cooling device to consume at most @power milliwatts. The limit is
  421. * expected to be a cap at the maximum power consumption.
  422. *
  423. * Return: 0 on success, -EINVAL if the cooling device does not
  424. * implement the power actor API or -E* for other failures.
  425. */
  426. int power_actor_set_power(struct thermal_cooling_device *cdev,
  427. struct thermal_instance *instance, u32 power)
  428. {
  429. unsigned long state;
  430. int ret;
  431. if (!cdev_is_power_actor(cdev))
  432. return -EINVAL;
  433. ret = cdev->ops->power2state(cdev, instance->tz, power, &state);
  434. if (ret)
  435. return ret;
  436. instance->target = state;
  437. mutex_lock(&cdev->lock);
  438. cdev->updated = false;
  439. mutex_unlock(&cdev->lock);
  440. thermal_cdev_update(cdev);
  441. return 0;
  442. }
  443. void thermal_zone_device_rebind_exception(struct thermal_zone_device *tz,
  444. const char *cdev_type, size_t size)
  445. {
  446. struct thermal_cooling_device *cdev = NULL;
  447. mutex_lock(&thermal_list_lock);
  448. list_for_each_entry(cdev, &thermal_cdev_list, node) {
  449. /* skip non matching cdevs */
  450. if (strncmp(cdev_type, cdev->type, size))
  451. continue;
  452. /* re binding the exception matching the type pattern */
  453. thermal_zone_bind_cooling_device(tz, THERMAL_TRIPS_NONE, cdev,
  454. THERMAL_NO_LIMIT,
  455. THERMAL_NO_LIMIT,
  456. THERMAL_WEIGHT_DEFAULT);
  457. }
  458. mutex_unlock(&thermal_list_lock);
  459. }
  460. void thermal_zone_device_unbind_exception(struct thermal_zone_device *tz,
  461. const char *cdev_type, size_t size)
  462. {
  463. struct thermal_cooling_device *cdev = NULL;
  464. mutex_lock(&thermal_list_lock);
  465. list_for_each_entry(cdev, &thermal_cdev_list, node) {
  466. /* skip non matching cdevs */
  467. if (strncmp(cdev_type, cdev->type, size))
  468. continue;
  469. /* unbinding the exception matching the type pattern */
  470. thermal_zone_unbind_cooling_device(tz, THERMAL_TRIPS_NONE,
  471. cdev);
  472. }
  473. mutex_unlock(&thermal_list_lock);
  474. }
  475. /*
  476. * Device management section: cooling devices, zones devices, and binding
  477. *
  478. * Set of functions provided by the thermal core for:
  479. * - cooling devices lifecycle: registration, unregistration,
  480. * binding, and unbinding.
  481. * - thermal zone devices lifecycle: registration, unregistration,
  482. * binding, and unbinding.
  483. */
  484. static int get_idr(struct idr *idr, struct mutex *lock, int *id)
  485. {
  486. int ret;
  487. if (lock)
  488. mutex_lock(lock);
  489. ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
  490. if (lock)
  491. mutex_unlock(lock);
  492. if (unlikely(ret < 0))
  493. return ret;
  494. *id = ret;
  495. return 0;
  496. }
  497. static void release_idr(struct idr *idr, struct mutex *lock, int id)
  498. {
  499. if (lock)
  500. mutex_lock(lock);
  501. idr_remove(idr, id);
  502. if (lock)
  503. mutex_unlock(lock);
  504. }
  505. /**
  506. * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
  507. * @tz: pointer to struct thermal_zone_device
  508. * @trip: indicates which trip point the cooling devices is
  509. * associated with in this thermal zone.
  510. * @cdev: pointer to struct thermal_cooling_device
  511. * @upper: the Maximum cooling state for this trip point.
  512. * THERMAL_NO_LIMIT means no upper limit,
  513. * and the cooling device can be in max_state.
  514. * @lower: the Minimum cooling state can be used for this trip point.
  515. * THERMAL_NO_LIMIT means no lower limit,
  516. * and the cooling device can be in cooling state 0.
  517. * @weight: The weight of the cooling device to be bound to the
  518. * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
  519. * default value
  520. *
  521. * This interface function bind a thermal cooling device to the certain trip
  522. * point of a thermal zone device.
  523. * This function is usually called in the thermal zone device .bind callback.
  524. *
  525. * Return: 0 on success, the proper error value otherwise.
  526. */
  527. int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
  528. int trip,
  529. struct thermal_cooling_device *cdev,
  530. unsigned long upper, unsigned long lower,
  531. unsigned int weight)
  532. {
  533. struct thermal_instance *dev;
  534. struct thermal_instance *pos;
  535. struct thermal_zone_device *pos1;
  536. struct thermal_cooling_device *pos2;
  537. unsigned long max_state;
  538. int result, ret;
  539. if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
  540. return -EINVAL;
  541. list_for_each_entry(pos1, &thermal_tz_list, node) {
  542. if (pos1 == tz)
  543. break;
  544. }
  545. list_for_each_entry(pos2, &thermal_cdev_list, node) {
  546. if (pos2 == cdev)
  547. break;
  548. }
  549. if (tz != pos1 || cdev != pos2)
  550. return -EINVAL;
  551. ret = cdev->ops->get_max_state(cdev, &max_state);
  552. if (ret)
  553. return ret;
  554. /* lower default 0, upper default max_state */
  555. lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
  556. upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
  557. if (lower > upper || upper > max_state)
  558. return -EINVAL;
  559. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  560. if (!dev)
  561. return -ENOMEM;
  562. dev->tz = tz;
  563. dev->cdev = cdev;
  564. dev->trip = trip;
  565. dev->upper = upper;
  566. dev->lower = lower;
  567. dev->target = THERMAL_NO_TARGET;
  568. dev->weight = weight;
  569. result = get_idr(&tz->idr, &tz->lock, &dev->id);
  570. if (result)
  571. goto free_mem;
  572. sprintf(dev->name, "cdev%d", dev->id);
  573. result =
  574. sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
  575. if (result)
  576. goto release_idr;
  577. sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
  578. sysfs_attr_init(&dev->attr.attr);
  579. dev->attr.attr.name = dev->attr_name;
  580. dev->attr.attr.mode = 0444;
  581. dev->attr.show = thermal_cooling_device_trip_point_show;
  582. result = device_create_file(&tz->device, &dev->attr);
  583. if (result)
  584. goto remove_symbol_link;
  585. sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
  586. sysfs_attr_init(&dev->weight_attr.attr);
  587. dev->weight_attr.attr.name = dev->weight_attr_name;
  588. dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
  589. dev->weight_attr.show = thermal_cooling_device_weight_show;
  590. dev->weight_attr.store = thermal_cooling_device_weight_store;
  591. result = device_create_file(&tz->device, &dev->weight_attr);
  592. if (result)
  593. goto remove_trip_file;
  594. mutex_lock(&tz->lock);
  595. mutex_lock(&cdev->lock);
  596. list_for_each_entry(pos, &tz->thermal_instances, tz_node)
  597. if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
  598. result = -EEXIST;
  599. break;
  600. }
  601. if (!result) {
  602. list_add_tail(&dev->tz_node, &tz->thermal_instances);
  603. list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
  604. atomic_set(&tz->need_update, 1);
  605. }
  606. mutex_unlock(&cdev->lock);
  607. mutex_unlock(&tz->lock);
  608. if (!result)
  609. return 0;
  610. device_remove_file(&tz->device, &dev->weight_attr);
  611. remove_trip_file:
  612. device_remove_file(&tz->device, &dev->attr);
  613. remove_symbol_link:
  614. sysfs_remove_link(&tz->device.kobj, dev->name);
  615. release_idr:
  616. release_idr(&tz->idr, &tz->lock, dev->id);
  617. free_mem:
  618. kfree(dev);
  619. return result;
  620. }
  621. EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
  622. /**
  623. * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
  624. * thermal zone.
  625. * @tz: pointer to a struct thermal_zone_device.
  626. * @trip: indicates which trip point the cooling devices is
  627. * associated with in this thermal zone.
  628. * @cdev: pointer to a struct thermal_cooling_device.
  629. *
  630. * This interface function unbind a thermal cooling device from the certain
  631. * trip point of a thermal zone device.
  632. * This function is usually called in the thermal zone device .unbind callback.
  633. *
  634. * Return: 0 on success, the proper error value otherwise.
  635. */
  636. int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
  637. int trip,
  638. struct thermal_cooling_device *cdev)
  639. {
  640. struct thermal_instance *pos, *next;
  641. mutex_lock(&tz->lock);
  642. mutex_lock(&cdev->lock);
  643. list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
  644. if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
  645. list_del(&pos->tz_node);
  646. list_del(&pos->cdev_node);
  647. mutex_unlock(&cdev->lock);
  648. mutex_unlock(&tz->lock);
  649. goto unbind;
  650. }
  651. }
  652. mutex_unlock(&cdev->lock);
  653. mutex_unlock(&tz->lock);
  654. return -ENODEV;
  655. unbind:
  656. device_remove_file(&tz->device, &pos->weight_attr);
  657. device_remove_file(&tz->device, &pos->attr);
  658. sysfs_remove_link(&tz->device.kobj, pos->name);
  659. release_idr(&tz->idr, &tz->lock, pos->id);
  660. kfree(pos);
  661. return 0;
  662. }
  663. EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
  664. static void thermal_release(struct device *dev)
  665. {
  666. struct thermal_zone_device *tz;
  667. struct thermal_cooling_device *cdev;
  668. if (!strncmp(dev_name(dev), "thermal_zone",
  669. sizeof("thermal_zone") - 1)) {
  670. tz = to_thermal_zone(dev);
  671. kfree(tz);
  672. } else if (!strncmp(dev_name(dev), "cooling_device",
  673. sizeof("cooling_device") - 1)) {
  674. cdev = to_cooling_device(dev);
  675. kfree(cdev);
  676. }
  677. }
  678. static struct class thermal_class = {
  679. .name = "thermal",
  680. .dev_release = thermal_release,
  681. };
  682. static inline
  683. void print_bind_err_msg(struct thermal_zone_device *tz,
  684. struct thermal_cooling_device *cdev, int ret)
  685. {
  686. dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
  687. tz->type, cdev->type, ret);
  688. }
  689. static void __bind(struct thermal_zone_device *tz, int mask,
  690. struct thermal_cooling_device *cdev,
  691. unsigned long *limits,
  692. unsigned int weight)
  693. {
  694. int i, ret;
  695. for (i = 0; i < tz->trips; i++) {
  696. if (mask & (1 << i)) {
  697. unsigned long upper, lower;
  698. upper = THERMAL_NO_LIMIT;
  699. lower = THERMAL_NO_LIMIT;
  700. if (limits) {
  701. lower = limits[i * 2];
  702. upper = limits[i * 2 + 1];
  703. }
  704. ret = thermal_zone_bind_cooling_device(tz, i, cdev,
  705. upper, lower,
  706. weight);
  707. if (ret)
  708. print_bind_err_msg(tz, cdev, ret);
  709. }
  710. }
  711. }
  712. static void bind_cdev(struct thermal_cooling_device *cdev)
  713. {
  714. int i, ret;
  715. const struct thermal_zone_params *tzp;
  716. struct thermal_zone_device *pos = NULL;
  717. mutex_lock(&thermal_list_lock);
  718. list_for_each_entry(pos, &thermal_tz_list, node) {
  719. if (!pos->tzp && !pos->ops->bind)
  720. continue;
  721. if (pos->ops->bind) {
  722. ret = pos->ops->bind(pos, cdev);
  723. if (ret)
  724. print_bind_err_msg(pos, cdev, ret);
  725. continue;
  726. }
  727. tzp = pos->tzp;
  728. if (!tzp || !tzp->tbp)
  729. continue;
  730. for (i = 0; i < tzp->num_tbps; i++) {
  731. if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
  732. continue;
  733. if (tzp->tbp[i].match(pos, cdev))
  734. continue;
  735. tzp->tbp[i].cdev = cdev;
  736. __bind(pos, tzp->tbp[i].trip_mask, cdev,
  737. tzp->tbp[i].binding_limits,
  738. tzp->tbp[i].weight);
  739. }
  740. }
  741. mutex_unlock(&thermal_list_lock);
  742. }
  743. /**
  744. * __thermal_cooling_device_register() - register a new thermal cooling device
  745. * @np: a pointer to a device tree node.
  746. * @type: the thermal cooling device type.
  747. * @devdata: device private data.
  748. * @ops: standard thermal cooling devices callbacks.
  749. *
  750. * This interface function adds a new thermal cooling device (fan/processor/...)
  751. * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
  752. * to all the thermal zone devices registered at the same time.
  753. * It also gives the opportunity to link the cooling device to a device tree
  754. * node, so that it can be bound to a thermal zone created out of device tree.
  755. *
  756. * Return: a pointer to the created struct thermal_cooling_device or an
  757. * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
  758. */
  759. static struct thermal_cooling_device *
  760. __thermal_cooling_device_register(struct device_node *np,
  761. char *type, void *devdata,
  762. const struct thermal_cooling_device_ops *ops)
  763. {
  764. struct thermal_cooling_device *cdev;
  765. struct thermal_zone_device *pos = NULL;
  766. int result;
  767. if (type && strlen(type) >= THERMAL_NAME_LENGTH)
  768. return ERR_PTR(-EINVAL);
  769. if (!ops || !ops->get_max_state || !ops->get_cur_state ||
  770. !ops->set_cur_state)
  771. return ERR_PTR(-EINVAL);
  772. cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
  773. if (!cdev)
  774. return ERR_PTR(-ENOMEM);
  775. result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
  776. if (result) {
  777. kfree(cdev);
  778. return ERR_PTR(result);
  779. }
  780. strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
  781. mutex_init(&cdev->lock);
  782. INIT_LIST_HEAD(&cdev->thermal_instances);
  783. cdev->np = np;
  784. cdev->ops = ops;
  785. cdev->updated = false;
  786. cdev->device.class = &thermal_class;
  787. thermal_cooling_device_setup_sysfs(cdev);
  788. cdev->devdata = devdata;
  789. dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
  790. result = device_register(&cdev->device);
  791. if (result) {
  792. release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
  793. kfree(cdev);
  794. return ERR_PTR(result);
  795. }
  796. /* Add 'this' new cdev to the global cdev list */
  797. mutex_lock(&thermal_list_lock);
  798. list_add(&cdev->node, &thermal_cdev_list);
  799. mutex_unlock(&thermal_list_lock);
  800. /* Update binding information for 'this' new cdev */
  801. bind_cdev(cdev);
  802. mutex_lock(&thermal_list_lock);
  803. list_for_each_entry(pos, &thermal_tz_list, node)
  804. if (atomic_cmpxchg(&pos->need_update, 1, 0))
  805. thermal_zone_device_update(pos,
  806. THERMAL_EVENT_UNSPECIFIED);
  807. mutex_unlock(&thermal_list_lock);
  808. return cdev;
  809. }
  810. /**
  811. * thermal_cooling_device_register() - register a new thermal cooling device
  812. * @type: the thermal cooling device type.
  813. * @devdata: device private data.
  814. * @ops: standard thermal cooling devices callbacks.
  815. *
  816. * This interface function adds a new thermal cooling device (fan/processor/...)
  817. * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
  818. * to all the thermal zone devices registered at the same time.
  819. *
  820. * Return: a pointer to the created struct thermal_cooling_device or an
  821. * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
  822. */
  823. struct thermal_cooling_device *
  824. thermal_cooling_device_register(char *type, void *devdata,
  825. const struct thermal_cooling_device_ops *ops)
  826. {
  827. return __thermal_cooling_device_register(NULL, type, devdata, ops);
  828. }
  829. EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
  830. /**
  831. * thermal_of_cooling_device_register() - register an OF thermal cooling device
  832. * @np: a pointer to a device tree node.
  833. * @type: the thermal cooling device type.
  834. * @devdata: device private data.
  835. * @ops: standard thermal cooling devices callbacks.
  836. *
  837. * This function will register a cooling device with device tree node reference.
  838. * This interface function adds a new thermal cooling device (fan/processor/...)
  839. * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
  840. * to all the thermal zone devices registered at the same time.
  841. *
  842. * Return: a pointer to the created struct thermal_cooling_device or an
  843. * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
  844. */
  845. struct thermal_cooling_device *
  846. thermal_of_cooling_device_register(struct device_node *np,
  847. char *type, void *devdata,
  848. const struct thermal_cooling_device_ops *ops)
  849. {
  850. return __thermal_cooling_device_register(np, type, devdata, ops);
  851. }
  852. EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
  853. static void __unbind(struct thermal_zone_device *tz, int mask,
  854. struct thermal_cooling_device *cdev)
  855. {
  856. int i;
  857. for (i = 0; i < tz->trips; i++)
  858. if (mask & (1 << i))
  859. thermal_zone_unbind_cooling_device(tz, i, cdev);
  860. }
  861. /**
  862. * thermal_cooling_device_unregister - removes a thermal cooling device
  863. * @cdev: the thermal cooling device to remove.
  864. *
  865. * thermal_cooling_device_unregister() must be called when a registered
  866. * thermal cooling device is no longer needed.
  867. */
  868. void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
  869. {
  870. int i;
  871. const struct thermal_zone_params *tzp;
  872. struct thermal_zone_device *tz;
  873. struct thermal_cooling_device *pos = NULL;
  874. if (!cdev)
  875. return;
  876. mutex_lock(&thermal_list_lock);
  877. list_for_each_entry(pos, &thermal_cdev_list, node)
  878. if (pos == cdev)
  879. break;
  880. if (pos != cdev) {
  881. /* thermal cooling device not found */
  882. mutex_unlock(&thermal_list_lock);
  883. return;
  884. }
  885. list_del(&cdev->node);
  886. /* Unbind all thermal zones associated with 'this' cdev */
  887. list_for_each_entry(tz, &thermal_tz_list, node) {
  888. if (tz->ops->unbind) {
  889. tz->ops->unbind(tz, cdev);
  890. continue;
  891. }
  892. if (!tz->tzp || !tz->tzp->tbp)
  893. continue;
  894. tzp = tz->tzp;
  895. for (i = 0; i < tzp->num_tbps; i++) {
  896. if (tzp->tbp[i].cdev == cdev) {
  897. __unbind(tz, tzp->tbp[i].trip_mask, cdev);
  898. tzp->tbp[i].cdev = NULL;
  899. }
  900. }
  901. }
  902. mutex_unlock(&thermal_list_lock);
  903. release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
  904. device_unregister(&cdev->device);
  905. }
  906. EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
  907. static void bind_tz(struct thermal_zone_device *tz)
  908. {
  909. int i, ret;
  910. struct thermal_cooling_device *pos = NULL;
  911. const struct thermal_zone_params *tzp = tz->tzp;
  912. if (!tzp && !tz->ops->bind)
  913. return;
  914. mutex_lock(&thermal_list_lock);
  915. /* If there is ops->bind, try to use ops->bind */
  916. if (tz->ops->bind) {
  917. list_for_each_entry(pos, &thermal_cdev_list, node) {
  918. ret = tz->ops->bind(tz, pos);
  919. if (ret)
  920. print_bind_err_msg(tz, pos, ret);
  921. }
  922. goto exit;
  923. }
  924. if (!tzp || !tzp->tbp)
  925. goto exit;
  926. list_for_each_entry(pos, &thermal_cdev_list, node) {
  927. for (i = 0; i < tzp->num_tbps; i++) {
  928. if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
  929. continue;
  930. if (tzp->tbp[i].match(tz, pos))
  931. continue;
  932. tzp->tbp[i].cdev = pos;
  933. __bind(tz, tzp->tbp[i].trip_mask, pos,
  934. tzp->tbp[i].binding_limits,
  935. tzp->tbp[i].weight);
  936. }
  937. }
  938. exit:
  939. mutex_unlock(&thermal_list_lock);
  940. }
  941. /**
  942. * thermal_zone_device_register() - register a new thermal zone device
  943. * @type: the thermal zone device type
  944. * @trips: the number of trip points the thermal zone support
  945. * @mask: a bit string indicating the writeablility of trip points
  946. * @devdata: private device data
  947. * @ops: standard thermal zone device callbacks
  948. * @tzp: thermal zone platform parameters
  949. * @passive_delay: number of milliseconds to wait between polls when
  950. * performing passive cooling
  951. * @polling_delay: number of milliseconds to wait between polls when checking
  952. * whether trip points have been crossed (0 for interrupt
  953. * driven systems)
  954. *
  955. * This interface function adds a new thermal zone device (sensor) to
  956. * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
  957. * thermal cooling devices registered at the same time.
  958. * thermal_zone_device_unregister() must be called when the device is no
  959. * longer needed. The passive cooling depends on the .get_trend() return value.
  960. *
  961. * Return: a pointer to the created struct thermal_zone_device or an
  962. * in case of error, an ERR_PTR. Caller must check return value with
  963. * IS_ERR*() helpers.
  964. */
  965. struct thermal_zone_device *
  966. thermal_zone_device_register(const char *type, int trips, int mask,
  967. void *devdata, struct thermal_zone_device_ops *ops,
  968. struct thermal_zone_params *tzp, int passive_delay,
  969. int polling_delay)
  970. {
  971. struct thermal_zone_device *tz;
  972. enum thermal_trip_type trip_type;
  973. int trip_temp;
  974. int result;
  975. int count;
  976. struct thermal_governor *governor;
  977. if (!type || strlen(type) == 0)
  978. return ERR_PTR(-EINVAL);
  979. if (type && strlen(type) >= THERMAL_NAME_LENGTH)
  980. return ERR_PTR(-EINVAL);
  981. if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
  982. return ERR_PTR(-EINVAL);
  983. if (!ops)
  984. return ERR_PTR(-EINVAL);
  985. if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
  986. return ERR_PTR(-EINVAL);
  987. tz = kzalloc(sizeof(*tz), GFP_KERNEL);
  988. if (!tz)
  989. return ERR_PTR(-ENOMEM);
  990. INIT_LIST_HEAD(&tz->thermal_instances);
  991. idr_init(&tz->idr);
  992. mutex_init(&tz->lock);
  993. result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
  994. if (result) {
  995. kfree(tz);
  996. return ERR_PTR(result);
  997. }
  998. strlcpy(tz->type, type, sizeof(tz->type));
  999. tz->ops = ops;
  1000. tz->tzp = tzp;
  1001. tz->device.class = &thermal_class;
  1002. tz->devdata = devdata;
  1003. tz->trips = trips;
  1004. tz->passive_delay = passive_delay;
  1005. tz->polling_delay = polling_delay;
  1006. /* sys I/F */
  1007. /* Add nodes that are always present via .groups */
  1008. result = thermal_zone_create_device_groups(tz, mask);
  1009. if (result)
  1010. goto unregister;
  1011. /* A new thermal zone needs to be updated anyway. */
  1012. atomic_set(&tz->need_update, 1);
  1013. dev_set_name(&tz->device, "thermal_zone%d", tz->id);
  1014. result = device_register(&tz->device);
  1015. if (result) {
  1016. release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
  1017. kfree(tz);
  1018. return ERR_PTR(result);
  1019. }
  1020. for (count = 0; count < trips; count++) {
  1021. if (tz->ops->get_trip_type(tz, count, &trip_type))
  1022. set_bit(count, &tz->trips_disabled);
  1023. if (tz->ops->get_trip_temp(tz, count, &trip_temp))
  1024. set_bit(count, &tz->trips_disabled);
  1025. /* Check for bogus trip points */
  1026. if (trip_temp == 0)
  1027. set_bit(count, &tz->trips_disabled);
  1028. }
  1029. /* Update 'this' zone's governor information */
  1030. mutex_lock(&thermal_governor_lock);
  1031. if (tz->tzp)
  1032. governor = __find_governor(tz->tzp->governor_name);
  1033. else
  1034. governor = def_governor;
  1035. result = thermal_set_governor(tz, governor);
  1036. if (result) {
  1037. mutex_unlock(&thermal_governor_lock);
  1038. goto unregister;
  1039. }
  1040. mutex_unlock(&thermal_governor_lock);
  1041. if (!tz->tzp || !tz->tzp->no_hwmon) {
  1042. result = thermal_add_hwmon_sysfs(tz);
  1043. if (result)
  1044. goto unregister;
  1045. }
  1046. mutex_lock(&thermal_list_lock);
  1047. list_add_tail(&tz->node, &thermal_tz_list);
  1048. mutex_unlock(&thermal_list_lock);
  1049. /* Bind cooling devices for this zone */
  1050. bind_tz(tz);
  1051. INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check);
  1052. thermal_zone_device_reset(tz);
  1053. /* Update the new thermal zone and mark it as already updated. */
  1054. if (atomic_cmpxchg(&tz->need_update, 1, 0))
  1055. thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
  1056. return tz;
  1057. unregister:
  1058. release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
  1059. device_unregister(&tz->device);
  1060. return ERR_PTR(result);
  1061. }
  1062. EXPORT_SYMBOL_GPL(thermal_zone_device_register);
  1063. /**
  1064. * thermal_device_unregister - removes the registered thermal zone device
  1065. * @tz: the thermal zone device to remove
  1066. */
  1067. void thermal_zone_device_unregister(struct thermal_zone_device *tz)
  1068. {
  1069. int i;
  1070. const struct thermal_zone_params *tzp;
  1071. struct thermal_cooling_device *cdev;
  1072. struct thermal_zone_device *pos = NULL;
  1073. if (!tz)
  1074. return;
  1075. tzp = tz->tzp;
  1076. mutex_lock(&thermal_list_lock);
  1077. list_for_each_entry(pos, &thermal_tz_list, node)
  1078. if (pos == tz)
  1079. break;
  1080. if (pos != tz) {
  1081. /* thermal zone device not found */
  1082. mutex_unlock(&thermal_list_lock);
  1083. return;
  1084. }
  1085. list_del(&tz->node);
  1086. /* Unbind all cdevs associated with 'this' thermal zone */
  1087. list_for_each_entry(cdev, &thermal_cdev_list, node) {
  1088. if (tz->ops->unbind) {
  1089. tz->ops->unbind(tz, cdev);
  1090. continue;
  1091. }
  1092. if (!tzp || !tzp->tbp)
  1093. break;
  1094. for (i = 0; i < tzp->num_tbps; i++) {
  1095. if (tzp->tbp[i].cdev == cdev) {
  1096. __unbind(tz, tzp->tbp[i].trip_mask, cdev);
  1097. tzp->tbp[i].cdev = NULL;
  1098. }
  1099. }
  1100. }
  1101. mutex_unlock(&thermal_list_lock);
  1102. thermal_zone_device_set_polling(tz, 0);
  1103. kfree(tz->trip_type_attrs);
  1104. kfree(tz->trip_temp_attrs);
  1105. kfree(tz->trip_hyst_attrs);
  1106. kfree(tz->trips_attribute_group.attrs);
  1107. thermal_set_governor(tz, NULL);
  1108. thermal_remove_hwmon_sysfs(tz);
  1109. release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
  1110. idr_destroy(&tz->idr);
  1111. mutex_destroy(&tz->lock);
  1112. device_unregister(&tz->device);
  1113. kfree(tz->device.groups);
  1114. }
  1115. EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
  1116. /**
  1117. * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
  1118. * @name: thermal zone name to fetch the temperature
  1119. *
  1120. * When only one zone is found with the passed name, returns a reference to it.
  1121. *
  1122. * Return: On success returns a reference to an unique thermal zone with
  1123. * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
  1124. * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
  1125. */
  1126. struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
  1127. {
  1128. struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
  1129. unsigned int found = 0;
  1130. if (!name)
  1131. goto exit;
  1132. mutex_lock(&thermal_list_lock);
  1133. list_for_each_entry(pos, &thermal_tz_list, node)
  1134. if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
  1135. found++;
  1136. ref = pos;
  1137. }
  1138. mutex_unlock(&thermal_list_lock);
  1139. /* nothing has been found, thus an error code for it */
  1140. if (found == 0)
  1141. ref = ERR_PTR(-ENODEV);
  1142. else if (found > 1)
  1143. /* Success only when an unique zone is found */
  1144. ref = ERR_PTR(-EEXIST);
  1145. exit:
  1146. return ref;
  1147. }
  1148. EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
  1149. #ifdef CONFIG_NET
  1150. static const struct genl_multicast_group thermal_event_mcgrps[] = {
  1151. { .name = THERMAL_GENL_MCAST_GROUP_NAME, },
  1152. };
  1153. static struct genl_family thermal_event_genl_family __ro_after_init = {
  1154. .module = THIS_MODULE,
  1155. .name = THERMAL_GENL_FAMILY_NAME,
  1156. .version = THERMAL_GENL_VERSION,
  1157. .maxattr = THERMAL_GENL_ATTR_MAX,
  1158. .mcgrps = thermal_event_mcgrps,
  1159. .n_mcgrps = ARRAY_SIZE(thermal_event_mcgrps),
  1160. };
  1161. int thermal_generate_netlink_event(struct thermal_zone_device *tz,
  1162. enum events event)
  1163. {
  1164. struct sk_buff *skb;
  1165. struct nlattr *attr;
  1166. struct thermal_genl_event *thermal_event;
  1167. void *msg_header;
  1168. int size;
  1169. int result;
  1170. static unsigned int thermal_event_seqnum;
  1171. if (!tz)
  1172. return -EINVAL;
  1173. /* allocate memory */
  1174. size = nla_total_size(sizeof(struct thermal_genl_event)) +
  1175. nla_total_size(0);
  1176. skb = genlmsg_new(size, GFP_ATOMIC);
  1177. if (!skb)
  1178. return -ENOMEM;
  1179. /* add the genetlink message header */
  1180. msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
  1181. &thermal_event_genl_family, 0,
  1182. THERMAL_GENL_CMD_EVENT);
  1183. if (!msg_header) {
  1184. nlmsg_free(skb);
  1185. return -ENOMEM;
  1186. }
  1187. /* fill the data */
  1188. attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
  1189. sizeof(struct thermal_genl_event));
  1190. if (!attr) {
  1191. nlmsg_free(skb);
  1192. return -EINVAL;
  1193. }
  1194. thermal_event = nla_data(attr);
  1195. if (!thermal_event) {
  1196. nlmsg_free(skb);
  1197. return -EINVAL;
  1198. }
  1199. memset(thermal_event, 0, sizeof(struct thermal_genl_event));
  1200. thermal_event->orig = tz->id;
  1201. thermal_event->event = event;
  1202. /* send multicast genetlink message */
  1203. genlmsg_end(skb, msg_header);
  1204. result = genlmsg_multicast(&thermal_event_genl_family, skb, 0,
  1205. 0, GFP_ATOMIC);
  1206. if (result)
  1207. dev_err(&tz->device, "Failed to send netlink event:%d", result);
  1208. return result;
  1209. }
  1210. EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
  1211. static int __init genetlink_init(void)
  1212. {
  1213. return genl_register_family(&thermal_event_genl_family);
  1214. }
  1215. static void genetlink_exit(void)
  1216. {
  1217. genl_unregister_family(&thermal_event_genl_family);
  1218. }
  1219. #else /* !CONFIG_NET */
  1220. static inline int genetlink_init(void) { return 0; }
  1221. static inline void genetlink_exit(void) {}
  1222. #endif /* !CONFIG_NET */
  1223. static int thermal_pm_notify(struct notifier_block *nb,
  1224. unsigned long mode, void *_unused)
  1225. {
  1226. struct thermal_zone_device *tz;
  1227. switch (mode) {
  1228. case PM_HIBERNATION_PREPARE:
  1229. case PM_RESTORE_PREPARE:
  1230. case PM_SUSPEND_PREPARE:
  1231. atomic_set(&in_suspend, 1);
  1232. break;
  1233. case PM_POST_HIBERNATION:
  1234. case PM_POST_RESTORE:
  1235. case PM_POST_SUSPEND:
  1236. atomic_set(&in_suspend, 0);
  1237. list_for_each_entry(tz, &thermal_tz_list, node) {
  1238. thermal_zone_device_reset(tz);
  1239. thermal_zone_device_update(tz,
  1240. THERMAL_EVENT_UNSPECIFIED);
  1241. }
  1242. break;
  1243. default:
  1244. break;
  1245. }
  1246. return 0;
  1247. }
  1248. static struct notifier_block thermal_pm_nb = {
  1249. .notifier_call = thermal_pm_notify,
  1250. };
  1251. static int __init thermal_init(void)
  1252. {
  1253. int result;
  1254. result = thermal_register_governors();
  1255. if (result)
  1256. goto error;
  1257. result = class_register(&thermal_class);
  1258. if (result)
  1259. goto unregister_governors;
  1260. result = genetlink_init();
  1261. if (result)
  1262. goto unregister_class;
  1263. result = of_parse_thermal_zones();
  1264. if (result)
  1265. goto exit_netlink;
  1266. result = register_pm_notifier(&thermal_pm_nb);
  1267. if (result)
  1268. pr_warn("Thermal: Can not register suspend notifier, return %d\n",
  1269. result);
  1270. return 0;
  1271. exit_netlink:
  1272. genetlink_exit();
  1273. unregister_class:
  1274. class_unregister(&thermal_class);
  1275. unregister_governors:
  1276. thermal_unregister_governors();
  1277. error:
  1278. idr_destroy(&thermal_tz_idr);
  1279. idr_destroy(&thermal_cdev_idr);
  1280. mutex_destroy(&thermal_idr_lock);
  1281. mutex_destroy(&thermal_list_lock);
  1282. mutex_destroy(&thermal_governor_lock);
  1283. return result;
  1284. }
  1285. static void __exit thermal_exit(void)
  1286. {
  1287. unregister_pm_notifier(&thermal_pm_nb);
  1288. of_thermal_destroy_zones();
  1289. genetlink_exit();
  1290. class_unregister(&thermal_class);
  1291. thermal_unregister_governors();
  1292. idr_destroy(&thermal_tz_idr);
  1293. idr_destroy(&thermal_cdev_idr);
  1294. mutex_destroy(&thermal_idr_lock);
  1295. mutex_destroy(&thermal_list_lock);
  1296. mutex_destroy(&thermal_governor_lock);
  1297. }
  1298. fs_initcall(thermal_init);
  1299. module_exit(thermal_exit);