thermal_core.c 44 KB

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