thermal_core.c 42 KB

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