thermal_core.c 50 KB

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