thermal_core.c 41 KB

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