thermal_core.c 42 KB

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