thermal_core.c 40 KB

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