thermal_core.c 42 KB

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