thermal_core.c 42 KB

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