thermal_core.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278
  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. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; version 2 of the License.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  22. *
  23. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  24. */
  25. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  26. #include <linux/module.h>
  27. #include <linux/device.h>
  28. #include <linux/err.h>
  29. #include <linux/slab.h>
  30. #include <linux/kdev_t.h>
  31. #include <linux/idr.h>
  32. #include <linux/thermal.h>
  33. #include <linux/reboot.h>
  34. #include <linux/string.h>
  35. #include <linux/of.h>
  36. #include <net/netlink.h>
  37. #include <net/genetlink.h>
  38. #include <linux/suspend.h>
  39. #define CREATE_TRACE_POINTS
  40. #include <trace/events/thermal.h>
  41. #include "thermal_core.h"
  42. #include "thermal_hwmon.h"
  43. MODULE_AUTHOR("Zhang Rui");
  44. MODULE_DESCRIPTION("Generic thermal management sysfs support");
  45. MODULE_LICENSE("GPL v2");
  46. static DEFINE_IDR(thermal_tz_idr);
  47. static DEFINE_IDR(thermal_cdev_idr);
  48. static DEFINE_MUTEX(thermal_idr_lock);
  49. static LIST_HEAD(thermal_tz_list);
  50. static LIST_HEAD(thermal_cdev_list);
  51. static LIST_HEAD(thermal_governor_list);
  52. static DEFINE_MUTEX(thermal_list_lock);
  53. static DEFINE_MUTEX(thermal_governor_lock);
  54. static atomic_t in_suspend;
  55. static struct thermal_governor *def_governor;
  56. static struct thermal_governor *__find_governor(const char *name)
  57. {
  58. struct thermal_governor *pos;
  59. if (!name || !name[0])
  60. return def_governor;
  61. list_for_each_entry(pos, &thermal_governor_list, governor_list)
  62. if (!strncasecmp(name, pos->name, THERMAL_NAME_LENGTH))
  63. return pos;
  64. return NULL;
  65. }
  66. /**
  67. * bind_previous_governor() - bind the previous governor of the thermal zone
  68. * @tz: a valid pointer to a struct thermal_zone_device
  69. * @failed_gov_name: the name of the governor that failed to register
  70. *
  71. * Register the previous governor of the thermal zone after a new
  72. * governor has failed to be bound.
  73. */
  74. static void bind_previous_governor(struct thermal_zone_device *tz,
  75. const char *failed_gov_name)
  76. {
  77. if (tz->governor && tz->governor->bind_to_tz) {
  78. if (tz->governor->bind_to_tz(tz)) {
  79. dev_err(&tz->device,
  80. "governor %s failed to bind and the previous one (%s) failed to bind again, thermal zone %s has no governor\n",
  81. failed_gov_name, tz->governor->name, tz->type);
  82. tz->governor = NULL;
  83. }
  84. }
  85. }
  86. /**
  87. * thermal_set_governor() - Switch to another governor
  88. * @tz: a valid pointer to a struct thermal_zone_device
  89. * @new_gov: pointer to the new governor
  90. *
  91. * Change the governor of thermal zone @tz.
  92. *
  93. * Return: 0 on success, an error if the new governor's bind_to_tz() failed.
  94. */
  95. static int thermal_set_governor(struct thermal_zone_device *tz,
  96. struct thermal_governor *new_gov)
  97. {
  98. int ret = 0;
  99. if (tz->governor && tz->governor->unbind_from_tz)
  100. tz->governor->unbind_from_tz(tz);
  101. if (new_gov && new_gov->bind_to_tz) {
  102. ret = new_gov->bind_to_tz(tz);
  103. if (ret) {
  104. bind_previous_governor(tz, new_gov->name);
  105. return ret;
  106. }
  107. }
  108. tz->governor = new_gov;
  109. return ret;
  110. }
  111. int thermal_register_governor(struct thermal_governor *governor)
  112. {
  113. int err;
  114. const char *name;
  115. struct thermal_zone_device *pos;
  116. if (!governor)
  117. return -EINVAL;
  118. mutex_lock(&thermal_governor_lock);
  119. err = -EBUSY;
  120. if (__find_governor(governor->name) == NULL) {
  121. err = 0;
  122. list_add(&governor->governor_list, &thermal_governor_list);
  123. if (!def_governor && !strncmp(governor->name,
  124. DEFAULT_THERMAL_GOVERNOR, THERMAL_NAME_LENGTH))
  125. def_governor = governor;
  126. }
  127. mutex_lock(&thermal_list_lock);
  128. list_for_each_entry(pos, &thermal_tz_list, node) {
  129. /*
  130. * only thermal zones with specified tz->tzp->governor_name
  131. * may run with tz->govenor unset
  132. */
  133. if (pos->governor)
  134. continue;
  135. name = pos->tzp->governor_name;
  136. if (!strncasecmp(name, governor->name, THERMAL_NAME_LENGTH)) {
  137. int ret;
  138. ret = thermal_set_governor(pos, governor);
  139. if (ret)
  140. dev_err(&pos->device,
  141. "Failed to set governor %s for thermal zone %s: %d\n",
  142. governor->name, pos->type, ret);
  143. }
  144. }
  145. mutex_unlock(&thermal_list_lock);
  146. mutex_unlock(&thermal_governor_lock);
  147. return err;
  148. }
  149. void thermal_unregister_governor(struct thermal_governor *governor)
  150. {
  151. struct thermal_zone_device *pos;
  152. if (!governor)
  153. return;
  154. mutex_lock(&thermal_governor_lock);
  155. if (__find_governor(governor->name) == NULL)
  156. goto exit;
  157. mutex_lock(&thermal_list_lock);
  158. list_for_each_entry(pos, &thermal_tz_list, node) {
  159. if (!strncasecmp(pos->governor->name, governor->name,
  160. THERMAL_NAME_LENGTH))
  161. thermal_set_governor(pos, NULL);
  162. }
  163. mutex_unlock(&thermal_list_lock);
  164. list_del(&governor->governor_list);
  165. exit:
  166. mutex_unlock(&thermal_governor_lock);
  167. return;
  168. }
  169. static int get_idr(struct idr *idr, struct mutex *lock, int *id)
  170. {
  171. int ret;
  172. if (lock)
  173. mutex_lock(lock);
  174. ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
  175. if (lock)
  176. mutex_unlock(lock);
  177. if (unlikely(ret < 0))
  178. return ret;
  179. *id = ret;
  180. return 0;
  181. }
  182. static void release_idr(struct idr *idr, struct mutex *lock, int id)
  183. {
  184. if (lock)
  185. mutex_lock(lock);
  186. idr_remove(idr, id);
  187. if (lock)
  188. mutex_unlock(lock);
  189. }
  190. int get_tz_trend(struct thermal_zone_device *tz, int trip)
  191. {
  192. enum thermal_trend trend;
  193. if (tz->emul_temperature || !tz->ops->get_trend ||
  194. tz->ops->get_trend(tz, trip, &trend)) {
  195. if (tz->temperature > tz->last_temperature)
  196. trend = THERMAL_TREND_RAISING;
  197. else if (tz->temperature < tz->last_temperature)
  198. trend = THERMAL_TREND_DROPPING;
  199. else
  200. trend = THERMAL_TREND_STABLE;
  201. }
  202. return trend;
  203. }
  204. EXPORT_SYMBOL(get_tz_trend);
  205. struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz,
  206. struct thermal_cooling_device *cdev, int trip)
  207. {
  208. struct thermal_instance *pos = NULL;
  209. struct thermal_instance *target_instance = NULL;
  210. mutex_lock(&tz->lock);
  211. mutex_lock(&cdev->lock);
  212. list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
  213. if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
  214. target_instance = pos;
  215. break;
  216. }
  217. }
  218. mutex_unlock(&cdev->lock);
  219. mutex_unlock(&tz->lock);
  220. return target_instance;
  221. }
  222. EXPORT_SYMBOL(get_thermal_instance);
  223. static void print_bind_err_msg(struct thermal_zone_device *tz,
  224. struct thermal_cooling_device *cdev, int ret)
  225. {
  226. dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
  227. tz->type, cdev->type, ret);
  228. }
  229. static void __bind(struct thermal_zone_device *tz, int mask,
  230. struct thermal_cooling_device *cdev,
  231. unsigned long *limits,
  232. unsigned int weight)
  233. {
  234. int i, ret;
  235. for (i = 0; i < tz->trips; i++) {
  236. if (mask & (1 << i)) {
  237. unsigned long upper, lower;
  238. upper = THERMAL_NO_LIMIT;
  239. lower = THERMAL_NO_LIMIT;
  240. if (limits) {
  241. lower = limits[i * 2];
  242. upper = limits[i * 2 + 1];
  243. }
  244. ret = thermal_zone_bind_cooling_device(tz, i, cdev,
  245. upper, lower,
  246. weight);
  247. if (ret)
  248. print_bind_err_msg(tz, cdev, ret);
  249. }
  250. }
  251. }
  252. static void __unbind(struct thermal_zone_device *tz, int mask,
  253. struct thermal_cooling_device *cdev)
  254. {
  255. int i;
  256. for (i = 0; i < tz->trips; i++)
  257. if (mask & (1 << i))
  258. thermal_zone_unbind_cooling_device(tz, i, cdev);
  259. }
  260. static void bind_cdev(struct thermal_cooling_device *cdev)
  261. {
  262. int i, ret;
  263. const struct thermal_zone_params *tzp;
  264. struct thermal_zone_device *pos = NULL;
  265. mutex_lock(&thermal_list_lock);
  266. list_for_each_entry(pos, &thermal_tz_list, node) {
  267. if (!pos->tzp && !pos->ops->bind)
  268. continue;
  269. if (pos->ops->bind) {
  270. ret = pos->ops->bind(pos, cdev);
  271. if (ret)
  272. print_bind_err_msg(pos, cdev, ret);
  273. continue;
  274. }
  275. tzp = pos->tzp;
  276. if (!tzp || !tzp->tbp)
  277. continue;
  278. for (i = 0; i < tzp->num_tbps; i++) {
  279. if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
  280. continue;
  281. if (tzp->tbp[i].match(pos, cdev))
  282. continue;
  283. tzp->tbp[i].cdev = cdev;
  284. __bind(pos, tzp->tbp[i].trip_mask, cdev,
  285. tzp->tbp[i].binding_limits,
  286. tzp->tbp[i].weight);
  287. }
  288. }
  289. mutex_unlock(&thermal_list_lock);
  290. }
  291. static void bind_tz(struct thermal_zone_device *tz)
  292. {
  293. int i, ret;
  294. struct thermal_cooling_device *pos = NULL;
  295. const struct thermal_zone_params *tzp = tz->tzp;
  296. if (!tzp && !tz->ops->bind)
  297. return;
  298. mutex_lock(&thermal_list_lock);
  299. /* If there is ops->bind, try to use ops->bind */
  300. if (tz->ops->bind) {
  301. list_for_each_entry(pos, &thermal_cdev_list, node) {
  302. ret = tz->ops->bind(tz, pos);
  303. if (ret)
  304. print_bind_err_msg(tz, pos, ret);
  305. }
  306. goto exit;
  307. }
  308. if (!tzp || !tzp->tbp)
  309. goto exit;
  310. list_for_each_entry(pos, &thermal_cdev_list, node) {
  311. for (i = 0; i < tzp->num_tbps; i++) {
  312. if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
  313. continue;
  314. if (tzp->tbp[i].match(tz, pos))
  315. continue;
  316. tzp->tbp[i].cdev = pos;
  317. __bind(tz, tzp->tbp[i].trip_mask, pos,
  318. tzp->tbp[i].binding_limits,
  319. tzp->tbp[i].weight);
  320. }
  321. }
  322. exit:
  323. mutex_unlock(&thermal_list_lock);
  324. }
  325. static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
  326. int delay)
  327. {
  328. if (delay > 1000)
  329. mod_delayed_work(system_freezable_wq, &tz->poll_queue,
  330. round_jiffies(msecs_to_jiffies(delay)));
  331. else if (delay)
  332. mod_delayed_work(system_freezable_wq, &tz->poll_queue,
  333. msecs_to_jiffies(delay));
  334. else
  335. cancel_delayed_work(&tz->poll_queue);
  336. }
  337. static void monitor_thermal_zone(struct thermal_zone_device *tz)
  338. {
  339. mutex_lock(&tz->lock);
  340. if (tz->passive)
  341. thermal_zone_device_set_polling(tz, tz->passive_delay);
  342. else if (tz->polling_delay)
  343. thermal_zone_device_set_polling(tz, tz->polling_delay);
  344. else
  345. thermal_zone_device_set_polling(tz, 0);
  346. mutex_unlock(&tz->lock);
  347. }
  348. static void handle_non_critical_trips(struct thermal_zone_device *tz,
  349. int trip, enum thermal_trip_type trip_type)
  350. {
  351. tz->governor ? tz->governor->throttle(tz, trip) :
  352. def_governor->throttle(tz, trip);
  353. }
  354. static void handle_critical_trips(struct thermal_zone_device *tz,
  355. int trip, enum thermal_trip_type trip_type)
  356. {
  357. int trip_temp;
  358. tz->ops->get_trip_temp(tz, trip, &trip_temp);
  359. /* If we have not crossed the trip_temp, we do not care. */
  360. if (trip_temp <= 0 || tz->temperature < trip_temp)
  361. return;
  362. trace_thermal_zone_trip(tz, trip, trip_type);
  363. if (tz->ops->notify)
  364. tz->ops->notify(tz, trip, trip_type);
  365. if (trip_type == THERMAL_TRIP_CRITICAL) {
  366. dev_emerg(&tz->device,
  367. "critical temperature reached(%d C),shutting down\n",
  368. tz->temperature / 1000);
  369. orderly_poweroff(true);
  370. }
  371. }
  372. static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
  373. {
  374. enum thermal_trip_type type;
  375. /* Ignore disabled trip points */
  376. if (test_bit(trip, &tz->trips_disabled))
  377. return;
  378. tz->ops->get_trip_type(tz, trip, &type);
  379. if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
  380. handle_critical_trips(tz, trip, type);
  381. else
  382. handle_non_critical_trips(tz, trip, type);
  383. /*
  384. * Alright, we handled this trip successfully.
  385. * So, start monitoring again.
  386. */
  387. monitor_thermal_zone(tz);
  388. }
  389. /**
  390. * thermal_zone_get_temp() - returns the temperature of a thermal zone
  391. * @tz: a valid pointer to a struct thermal_zone_device
  392. * @temp: a valid pointer to where to store the resulting temperature.
  393. *
  394. * When a valid thermal zone reference is passed, it will fetch its
  395. * temperature and fill @temp.
  396. *
  397. * Return: On success returns 0, an error code otherwise
  398. */
  399. int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
  400. {
  401. int ret = -EINVAL;
  402. int count;
  403. int crit_temp = INT_MAX;
  404. enum thermal_trip_type type;
  405. if (!tz || IS_ERR(tz) || !tz->ops->get_temp)
  406. goto exit;
  407. mutex_lock(&tz->lock);
  408. ret = tz->ops->get_temp(tz, temp);
  409. if (IS_ENABLED(CONFIG_THERMAL_EMULATION) && tz->emul_temperature) {
  410. for (count = 0; count < tz->trips; count++) {
  411. ret = tz->ops->get_trip_type(tz, count, &type);
  412. if (!ret && type == THERMAL_TRIP_CRITICAL) {
  413. ret = tz->ops->get_trip_temp(tz, count,
  414. &crit_temp);
  415. break;
  416. }
  417. }
  418. /*
  419. * Only allow emulating a temperature when the real temperature
  420. * is below the critical temperature so that the emulation code
  421. * cannot hide critical conditions.
  422. */
  423. if (!ret && *temp < crit_temp)
  424. *temp = tz->emul_temperature;
  425. }
  426. mutex_unlock(&tz->lock);
  427. exit:
  428. return ret;
  429. }
  430. EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
  431. static void update_temperature(struct thermal_zone_device *tz)
  432. {
  433. int temp, ret;
  434. ret = thermal_zone_get_temp(tz, &temp);
  435. if (ret) {
  436. if (ret != -EAGAIN)
  437. dev_warn(&tz->device,
  438. "failed to read out thermal zone (%d)\n",
  439. ret);
  440. return;
  441. }
  442. mutex_lock(&tz->lock);
  443. tz->last_temperature = tz->temperature;
  444. tz->temperature = temp;
  445. mutex_unlock(&tz->lock);
  446. trace_thermal_temperature(tz);
  447. if (tz->last_temperature == THERMAL_TEMP_INVALID)
  448. dev_dbg(&tz->device, "last_temperature N/A, current_temperature=%d\n",
  449. tz->temperature);
  450. else
  451. dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n",
  452. tz->last_temperature, tz->temperature);
  453. }
  454. static void thermal_zone_device_reset(struct thermal_zone_device *tz)
  455. {
  456. struct thermal_instance *pos;
  457. tz->temperature = THERMAL_TEMP_INVALID;
  458. tz->passive = 0;
  459. list_for_each_entry(pos, &tz->thermal_instances, tz_node)
  460. pos->initialized = false;
  461. }
  462. void thermal_zone_device_update(struct thermal_zone_device *tz)
  463. {
  464. int count;
  465. if (atomic_read(&in_suspend))
  466. return;
  467. if (!tz->ops->get_temp)
  468. return;
  469. update_temperature(tz);
  470. for (count = 0; count < tz->trips; count++)
  471. handle_thermal_trip(tz, count);
  472. }
  473. EXPORT_SYMBOL_GPL(thermal_zone_device_update);
  474. static void thermal_zone_device_check(struct work_struct *work)
  475. {
  476. struct thermal_zone_device *tz = container_of(work, struct
  477. thermal_zone_device,
  478. poll_queue.work);
  479. thermal_zone_device_update(tz);
  480. }
  481. /* sys I/F for thermal zone */
  482. #define to_thermal_zone(_dev) \
  483. container_of(_dev, struct thermal_zone_device, device)
  484. static ssize_t
  485. type_show(struct device *dev, struct device_attribute *attr, char *buf)
  486. {
  487. struct thermal_zone_device *tz = to_thermal_zone(dev);
  488. return sprintf(buf, "%s\n", tz->type);
  489. }
  490. static ssize_t
  491. temp_show(struct device *dev, struct device_attribute *attr, char *buf)
  492. {
  493. struct thermal_zone_device *tz = to_thermal_zone(dev);
  494. int temperature, ret;
  495. ret = thermal_zone_get_temp(tz, &temperature);
  496. if (ret)
  497. return ret;
  498. return sprintf(buf, "%d\n", temperature);
  499. }
  500. static ssize_t
  501. mode_show(struct device *dev, struct device_attribute *attr, char *buf)
  502. {
  503. struct thermal_zone_device *tz = to_thermal_zone(dev);
  504. enum thermal_device_mode mode;
  505. int result;
  506. if (!tz->ops->get_mode)
  507. return -EPERM;
  508. result = tz->ops->get_mode(tz, &mode);
  509. if (result)
  510. return result;
  511. return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
  512. : "disabled");
  513. }
  514. static ssize_t
  515. mode_store(struct device *dev, struct device_attribute *attr,
  516. const char *buf, size_t count)
  517. {
  518. struct thermal_zone_device *tz = to_thermal_zone(dev);
  519. int result;
  520. if (!tz->ops->set_mode)
  521. return -EPERM;
  522. if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
  523. result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
  524. else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
  525. result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
  526. else
  527. result = -EINVAL;
  528. if (result)
  529. return result;
  530. return count;
  531. }
  532. static ssize_t
  533. trip_point_type_show(struct device *dev, struct device_attribute *attr,
  534. char *buf)
  535. {
  536. struct thermal_zone_device *tz = to_thermal_zone(dev);
  537. enum thermal_trip_type type;
  538. int trip, result;
  539. if (!tz->ops->get_trip_type)
  540. return -EPERM;
  541. if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
  542. return -EINVAL;
  543. result = tz->ops->get_trip_type(tz, trip, &type);
  544. if (result)
  545. return result;
  546. switch (type) {
  547. case THERMAL_TRIP_CRITICAL:
  548. return sprintf(buf, "critical\n");
  549. case THERMAL_TRIP_HOT:
  550. return sprintf(buf, "hot\n");
  551. case THERMAL_TRIP_PASSIVE:
  552. return sprintf(buf, "passive\n");
  553. case THERMAL_TRIP_ACTIVE:
  554. return sprintf(buf, "active\n");
  555. default:
  556. return sprintf(buf, "unknown\n");
  557. }
  558. }
  559. static ssize_t
  560. trip_point_temp_store(struct device *dev, struct device_attribute *attr,
  561. const char *buf, size_t count)
  562. {
  563. struct thermal_zone_device *tz = to_thermal_zone(dev);
  564. int trip, ret;
  565. unsigned long temperature;
  566. if (!tz->ops->set_trip_temp)
  567. return -EPERM;
  568. if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
  569. return -EINVAL;
  570. if (kstrtoul(buf, 10, &temperature))
  571. return -EINVAL;
  572. ret = tz->ops->set_trip_temp(tz, trip, temperature);
  573. if (ret)
  574. return ret;
  575. thermal_zone_device_update(tz);
  576. return count;
  577. }
  578. static ssize_t
  579. trip_point_temp_show(struct device *dev, struct device_attribute *attr,
  580. char *buf)
  581. {
  582. struct thermal_zone_device *tz = to_thermal_zone(dev);
  583. int trip, ret;
  584. int temperature;
  585. if (!tz->ops->get_trip_temp)
  586. return -EPERM;
  587. if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
  588. return -EINVAL;
  589. ret = tz->ops->get_trip_temp(tz, trip, &temperature);
  590. if (ret)
  591. return ret;
  592. return sprintf(buf, "%d\n", temperature);
  593. }
  594. static ssize_t
  595. trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
  596. const char *buf, size_t count)
  597. {
  598. struct thermal_zone_device *tz = to_thermal_zone(dev);
  599. int trip, ret;
  600. int temperature;
  601. if (!tz->ops->set_trip_hyst)
  602. return -EPERM;
  603. if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
  604. return -EINVAL;
  605. if (kstrtoint(buf, 10, &temperature))
  606. return -EINVAL;
  607. /*
  608. * We are not doing any check on the 'temperature' value
  609. * here. The driver implementing 'set_trip_hyst' has to
  610. * take care of this.
  611. */
  612. ret = tz->ops->set_trip_hyst(tz, trip, temperature);
  613. return ret ? ret : count;
  614. }
  615. static ssize_t
  616. trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
  617. char *buf)
  618. {
  619. struct thermal_zone_device *tz = to_thermal_zone(dev);
  620. int trip, ret;
  621. int temperature;
  622. if (!tz->ops->get_trip_hyst)
  623. return -EPERM;
  624. if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
  625. return -EINVAL;
  626. ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
  627. return ret ? ret : sprintf(buf, "%d\n", temperature);
  628. }
  629. static ssize_t
  630. passive_store(struct device *dev, struct device_attribute *attr,
  631. const char *buf, size_t count)
  632. {
  633. struct thermal_zone_device *tz = to_thermal_zone(dev);
  634. struct thermal_cooling_device *cdev = NULL;
  635. int state;
  636. if (!sscanf(buf, "%d\n", &state))
  637. return -EINVAL;
  638. /* sanity check: values below 1000 millicelcius don't make sense
  639. * and can cause the system to go into a thermal heart attack
  640. */
  641. if (state && state < 1000)
  642. return -EINVAL;
  643. if (state && !tz->forced_passive) {
  644. mutex_lock(&thermal_list_lock);
  645. list_for_each_entry(cdev, &thermal_cdev_list, node) {
  646. if (!strncmp("Processor", cdev->type,
  647. sizeof("Processor")))
  648. thermal_zone_bind_cooling_device(tz,
  649. THERMAL_TRIPS_NONE, cdev,
  650. THERMAL_NO_LIMIT,
  651. THERMAL_NO_LIMIT,
  652. THERMAL_WEIGHT_DEFAULT);
  653. }
  654. mutex_unlock(&thermal_list_lock);
  655. if (!tz->passive_delay)
  656. tz->passive_delay = 1000;
  657. } else if (!state && tz->forced_passive) {
  658. mutex_lock(&thermal_list_lock);
  659. list_for_each_entry(cdev, &thermal_cdev_list, node) {
  660. if (!strncmp("Processor", cdev->type,
  661. sizeof("Processor")))
  662. thermal_zone_unbind_cooling_device(tz,
  663. THERMAL_TRIPS_NONE,
  664. cdev);
  665. }
  666. mutex_unlock(&thermal_list_lock);
  667. tz->passive_delay = 0;
  668. }
  669. tz->forced_passive = state;
  670. thermal_zone_device_update(tz);
  671. return count;
  672. }
  673. static ssize_t
  674. passive_show(struct device *dev, struct device_attribute *attr,
  675. char *buf)
  676. {
  677. struct thermal_zone_device *tz = to_thermal_zone(dev);
  678. return sprintf(buf, "%d\n", tz->forced_passive);
  679. }
  680. static ssize_t
  681. policy_store(struct device *dev, struct device_attribute *attr,
  682. const char *buf, size_t count)
  683. {
  684. int ret = -EINVAL;
  685. struct thermal_zone_device *tz = to_thermal_zone(dev);
  686. struct thermal_governor *gov;
  687. char name[THERMAL_NAME_LENGTH];
  688. snprintf(name, sizeof(name), "%s", buf);
  689. mutex_lock(&thermal_governor_lock);
  690. mutex_lock(&tz->lock);
  691. gov = __find_governor(strim(name));
  692. if (!gov)
  693. goto exit;
  694. ret = thermal_set_governor(tz, gov);
  695. if (!ret)
  696. ret = count;
  697. exit:
  698. mutex_unlock(&tz->lock);
  699. mutex_unlock(&thermal_governor_lock);
  700. return ret;
  701. }
  702. static ssize_t
  703. policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
  704. {
  705. struct thermal_zone_device *tz = to_thermal_zone(dev);
  706. return sprintf(buf, "%s\n", tz->governor->name);
  707. }
  708. static ssize_t
  709. available_policies_show(struct device *dev, struct device_attribute *devattr,
  710. char *buf)
  711. {
  712. struct thermal_governor *pos;
  713. ssize_t count = 0;
  714. ssize_t size = PAGE_SIZE;
  715. mutex_lock(&thermal_governor_lock);
  716. list_for_each_entry(pos, &thermal_governor_list, governor_list) {
  717. size = PAGE_SIZE - count;
  718. count += scnprintf(buf + count, size, "%s ", pos->name);
  719. }
  720. count += scnprintf(buf + count, size, "\n");
  721. mutex_unlock(&thermal_governor_lock);
  722. return count;
  723. }
  724. static ssize_t
  725. emul_temp_store(struct device *dev, struct device_attribute *attr,
  726. const char *buf, size_t count)
  727. {
  728. struct thermal_zone_device *tz = to_thermal_zone(dev);
  729. int ret = 0;
  730. unsigned long temperature;
  731. if (kstrtoul(buf, 10, &temperature))
  732. return -EINVAL;
  733. if (!tz->ops->set_emul_temp) {
  734. mutex_lock(&tz->lock);
  735. tz->emul_temperature = temperature;
  736. mutex_unlock(&tz->lock);
  737. } else {
  738. ret = tz->ops->set_emul_temp(tz, temperature);
  739. }
  740. if (!ret)
  741. thermal_zone_device_update(tz);
  742. return ret ? ret : count;
  743. }
  744. static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
  745. static ssize_t
  746. sustainable_power_show(struct device *dev, struct device_attribute *devattr,
  747. char *buf)
  748. {
  749. struct thermal_zone_device *tz = to_thermal_zone(dev);
  750. if (tz->tzp)
  751. return sprintf(buf, "%u\n", tz->tzp->sustainable_power);
  752. else
  753. return -EIO;
  754. }
  755. static ssize_t
  756. sustainable_power_store(struct device *dev, struct device_attribute *devattr,
  757. const char *buf, size_t count)
  758. {
  759. struct thermal_zone_device *tz = to_thermal_zone(dev);
  760. u32 sustainable_power;
  761. if (!tz->tzp)
  762. return -EIO;
  763. if (kstrtou32(buf, 10, &sustainable_power))
  764. return -EINVAL;
  765. tz->tzp->sustainable_power = sustainable_power;
  766. return count;
  767. }
  768. static DEVICE_ATTR(sustainable_power, S_IWUSR | S_IRUGO, sustainable_power_show,
  769. sustainable_power_store);
  770. #define create_s32_tzp_attr(name) \
  771. static ssize_t \
  772. name##_show(struct device *dev, struct device_attribute *devattr, \
  773. char *buf) \
  774. { \
  775. struct thermal_zone_device *tz = to_thermal_zone(dev); \
  776. \
  777. if (tz->tzp) \
  778. return sprintf(buf, "%u\n", tz->tzp->name); \
  779. else \
  780. return -EIO; \
  781. } \
  782. \
  783. static ssize_t \
  784. name##_store(struct device *dev, struct device_attribute *devattr, \
  785. const char *buf, size_t count) \
  786. { \
  787. struct thermal_zone_device *tz = to_thermal_zone(dev); \
  788. s32 value; \
  789. \
  790. if (!tz->tzp) \
  791. return -EIO; \
  792. \
  793. if (kstrtos32(buf, 10, &value)) \
  794. return -EINVAL; \
  795. \
  796. tz->tzp->name = value; \
  797. \
  798. return count; \
  799. } \
  800. static DEVICE_ATTR(name, S_IWUSR | S_IRUGO, name##_show, name##_store)
  801. create_s32_tzp_attr(k_po);
  802. create_s32_tzp_attr(k_pu);
  803. create_s32_tzp_attr(k_i);
  804. create_s32_tzp_attr(k_d);
  805. create_s32_tzp_attr(integral_cutoff);
  806. create_s32_tzp_attr(slope);
  807. create_s32_tzp_attr(offset);
  808. #undef create_s32_tzp_attr
  809. static struct device_attribute *dev_tzp_attrs[] = {
  810. &dev_attr_sustainable_power,
  811. &dev_attr_k_po,
  812. &dev_attr_k_pu,
  813. &dev_attr_k_i,
  814. &dev_attr_k_d,
  815. &dev_attr_integral_cutoff,
  816. &dev_attr_slope,
  817. &dev_attr_offset,
  818. };
  819. static int create_tzp_attrs(struct device *dev)
  820. {
  821. int i;
  822. for (i = 0; i < ARRAY_SIZE(dev_tzp_attrs); i++) {
  823. int ret;
  824. struct device_attribute *dev_attr = dev_tzp_attrs[i];
  825. ret = device_create_file(dev, dev_attr);
  826. if (ret)
  827. return ret;
  828. }
  829. return 0;
  830. }
  831. /**
  832. * power_actor_get_max_power() - get the maximum power that a cdev can consume
  833. * @cdev: pointer to &thermal_cooling_device
  834. * @tz: a valid thermal zone device pointer
  835. * @max_power: pointer in which to store the maximum power
  836. *
  837. * Calculate the maximum power consumption in milliwats that the
  838. * cooling device can currently consume and store it in @max_power.
  839. *
  840. * Return: 0 on success, -EINVAL if @cdev doesn't support the
  841. * power_actor API or -E* on other error.
  842. */
  843. int power_actor_get_max_power(struct thermal_cooling_device *cdev,
  844. struct thermal_zone_device *tz, u32 *max_power)
  845. {
  846. if (!cdev_is_power_actor(cdev))
  847. return -EINVAL;
  848. return cdev->ops->state2power(cdev, tz, 0, max_power);
  849. }
  850. /**
  851. * power_actor_get_min_power() - get the mainimum power that a cdev can consume
  852. * @cdev: pointer to &thermal_cooling_device
  853. * @tz: a valid thermal zone device pointer
  854. * @min_power: pointer in which to store the minimum power
  855. *
  856. * Calculate the minimum power consumption in milliwatts that the
  857. * cooling device can currently consume and store it in @min_power.
  858. *
  859. * Return: 0 on success, -EINVAL if @cdev doesn't support the
  860. * power_actor API or -E* on other error.
  861. */
  862. int power_actor_get_min_power(struct thermal_cooling_device *cdev,
  863. struct thermal_zone_device *tz, u32 *min_power)
  864. {
  865. unsigned long max_state;
  866. int ret;
  867. if (!cdev_is_power_actor(cdev))
  868. return -EINVAL;
  869. ret = cdev->ops->get_max_state(cdev, &max_state);
  870. if (ret)
  871. return ret;
  872. return cdev->ops->state2power(cdev, tz, max_state, min_power);
  873. }
  874. /**
  875. * power_actor_set_power() - limit the maximum power that a cooling device can consume
  876. * @cdev: pointer to &thermal_cooling_device
  877. * @instance: thermal instance to update
  878. * @power: the power in milliwatts
  879. *
  880. * Set the cooling device to consume at most @power milliwatts.
  881. *
  882. * Return: 0 on success, -EINVAL if the cooling device does not
  883. * implement the power actor API or -E* for other failures.
  884. */
  885. int power_actor_set_power(struct thermal_cooling_device *cdev,
  886. struct thermal_instance *instance, u32 power)
  887. {
  888. unsigned long state;
  889. int ret;
  890. if (!cdev_is_power_actor(cdev))
  891. return -EINVAL;
  892. ret = cdev->ops->power2state(cdev, instance->tz, power, &state);
  893. if (ret)
  894. return ret;
  895. instance->target = state;
  896. cdev->updated = false;
  897. thermal_cdev_update(cdev);
  898. return 0;
  899. }
  900. static DEVICE_ATTR(type, 0444, type_show, NULL);
  901. static DEVICE_ATTR(temp, 0444, temp_show, NULL);
  902. static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
  903. static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
  904. static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
  905. static DEVICE_ATTR(available_policies, S_IRUGO, available_policies_show, NULL);
  906. /* sys I/F for cooling device */
  907. #define to_cooling_device(_dev) \
  908. container_of(_dev, struct thermal_cooling_device, device)
  909. static ssize_t
  910. thermal_cooling_device_type_show(struct device *dev,
  911. struct device_attribute *attr, char *buf)
  912. {
  913. struct thermal_cooling_device *cdev = to_cooling_device(dev);
  914. return sprintf(buf, "%s\n", cdev->type);
  915. }
  916. static ssize_t
  917. thermal_cooling_device_max_state_show(struct device *dev,
  918. struct device_attribute *attr, char *buf)
  919. {
  920. struct thermal_cooling_device *cdev = to_cooling_device(dev);
  921. unsigned long state;
  922. int ret;
  923. ret = cdev->ops->get_max_state(cdev, &state);
  924. if (ret)
  925. return ret;
  926. return sprintf(buf, "%ld\n", state);
  927. }
  928. static ssize_t
  929. thermal_cooling_device_cur_state_show(struct device *dev,
  930. struct device_attribute *attr, char *buf)
  931. {
  932. struct thermal_cooling_device *cdev = to_cooling_device(dev);
  933. unsigned long state;
  934. int ret;
  935. ret = cdev->ops->get_cur_state(cdev, &state);
  936. if (ret)
  937. return ret;
  938. return sprintf(buf, "%ld\n", state);
  939. }
  940. static ssize_t
  941. thermal_cooling_device_cur_state_store(struct device *dev,
  942. struct device_attribute *attr,
  943. const char *buf, size_t count)
  944. {
  945. struct thermal_cooling_device *cdev = to_cooling_device(dev);
  946. unsigned long state;
  947. int result;
  948. if (!sscanf(buf, "%ld\n", &state))
  949. return -EINVAL;
  950. if ((long)state < 0)
  951. return -EINVAL;
  952. result = cdev->ops->set_cur_state(cdev, state);
  953. if (result)
  954. return result;
  955. return count;
  956. }
  957. static struct device_attribute dev_attr_cdev_type =
  958. __ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
  959. static DEVICE_ATTR(max_state, 0444,
  960. thermal_cooling_device_max_state_show, NULL);
  961. static DEVICE_ATTR(cur_state, 0644,
  962. thermal_cooling_device_cur_state_show,
  963. thermal_cooling_device_cur_state_store);
  964. static ssize_t
  965. thermal_cooling_device_trip_point_show(struct device *dev,
  966. struct device_attribute *attr, char *buf)
  967. {
  968. struct thermal_instance *instance;
  969. instance =
  970. container_of(attr, struct thermal_instance, attr);
  971. if (instance->trip == THERMAL_TRIPS_NONE)
  972. return sprintf(buf, "-1\n");
  973. else
  974. return sprintf(buf, "%d\n", instance->trip);
  975. }
  976. static struct attribute *cooling_device_attrs[] = {
  977. &dev_attr_cdev_type.attr,
  978. &dev_attr_max_state.attr,
  979. &dev_attr_cur_state.attr,
  980. NULL,
  981. };
  982. static const struct attribute_group cooling_device_attr_group = {
  983. .attrs = cooling_device_attrs,
  984. };
  985. static const struct attribute_group *cooling_device_attr_groups[] = {
  986. &cooling_device_attr_group,
  987. NULL,
  988. };
  989. static ssize_t
  990. thermal_cooling_device_weight_show(struct device *dev,
  991. struct device_attribute *attr, char *buf)
  992. {
  993. struct thermal_instance *instance;
  994. instance = container_of(attr, struct thermal_instance, weight_attr);
  995. return sprintf(buf, "%d\n", instance->weight);
  996. }
  997. static ssize_t
  998. thermal_cooling_device_weight_store(struct device *dev,
  999. struct device_attribute *attr,
  1000. const char *buf, size_t count)
  1001. {
  1002. struct thermal_instance *instance;
  1003. int ret, weight;
  1004. ret = kstrtoint(buf, 0, &weight);
  1005. if (ret)
  1006. return ret;
  1007. instance = container_of(attr, struct thermal_instance, weight_attr);
  1008. instance->weight = weight;
  1009. return count;
  1010. }
  1011. /* Device management */
  1012. /**
  1013. * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
  1014. * @tz: pointer to struct thermal_zone_device
  1015. * @trip: indicates which trip point the cooling devices is
  1016. * associated with in this thermal zone.
  1017. * @cdev: pointer to struct thermal_cooling_device
  1018. * @upper: the Maximum cooling state for this trip point.
  1019. * THERMAL_NO_LIMIT means no upper limit,
  1020. * and the cooling device can be in max_state.
  1021. * @lower: the Minimum cooling state can be used for this trip point.
  1022. * THERMAL_NO_LIMIT means no lower limit,
  1023. * and the cooling device can be in cooling state 0.
  1024. * @weight: The weight of the cooling device to be bound to the
  1025. * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
  1026. * default value
  1027. *
  1028. * This interface function bind a thermal cooling device to the certain trip
  1029. * point of a thermal zone device.
  1030. * This function is usually called in the thermal zone device .bind callback.
  1031. *
  1032. * Return: 0 on success, the proper error value otherwise.
  1033. */
  1034. int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
  1035. int trip,
  1036. struct thermal_cooling_device *cdev,
  1037. unsigned long upper, unsigned long lower,
  1038. unsigned int weight)
  1039. {
  1040. struct thermal_instance *dev;
  1041. struct thermal_instance *pos;
  1042. struct thermal_zone_device *pos1;
  1043. struct thermal_cooling_device *pos2;
  1044. unsigned long max_state;
  1045. int result, ret;
  1046. if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
  1047. return -EINVAL;
  1048. list_for_each_entry(pos1, &thermal_tz_list, node) {
  1049. if (pos1 == tz)
  1050. break;
  1051. }
  1052. list_for_each_entry(pos2, &thermal_cdev_list, node) {
  1053. if (pos2 == cdev)
  1054. break;
  1055. }
  1056. if (tz != pos1 || cdev != pos2)
  1057. return -EINVAL;
  1058. ret = cdev->ops->get_max_state(cdev, &max_state);
  1059. if (ret)
  1060. return ret;
  1061. /* lower default 0, upper default max_state */
  1062. lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
  1063. upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
  1064. if (lower > upper || upper > max_state)
  1065. return -EINVAL;
  1066. dev =
  1067. kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
  1068. if (!dev)
  1069. return -ENOMEM;
  1070. dev->tz = tz;
  1071. dev->cdev = cdev;
  1072. dev->trip = trip;
  1073. dev->upper = upper;
  1074. dev->lower = lower;
  1075. dev->target = THERMAL_NO_TARGET;
  1076. dev->weight = weight;
  1077. result = get_idr(&tz->idr, &tz->lock, &dev->id);
  1078. if (result)
  1079. goto free_mem;
  1080. sprintf(dev->name, "cdev%d", dev->id);
  1081. result =
  1082. sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
  1083. if (result)
  1084. goto release_idr;
  1085. sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
  1086. sysfs_attr_init(&dev->attr.attr);
  1087. dev->attr.attr.name = dev->attr_name;
  1088. dev->attr.attr.mode = 0444;
  1089. dev->attr.show = thermal_cooling_device_trip_point_show;
  1090. result = device_create_file(&tz->device, &dev->attr);
  1091. if (result)
  1092. goto remove_symbol_link;
  1093. sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
  1094. sysfs_attr_init(&dev->weight_attr.attr);
  1095. dev->weight_attr.attr.name = dev->weight_attr_name;
  1096. dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
  1097. dev->weight_attr.show = thermal_cooling_device_weight_show;
  1098. dev->weight_attr.store = thermal_cooling_device_weight_store;
  1099. result = device_create_file(&tz->device, &dev->weight_attr);
  1100. if (result)
  1101. goto remove_trip_file;
  1102. mutex_lock(&tz->lock);
  1103. mutex_lock(&cdev->lock);
  1104. list_for_each_entry(pos, &tz->thermal_instances, tz_node)
  1105. if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
  1106. result = -EEXIST;
  1107. break;
  1108. }
  1109. if (!result) {
  1110. list_add_tail(&dev->tz_node, &tz->thermal_instances);
  1111. list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
  1112. atomic_set(&tz->need_update, 1);
  1113. }
  1114. mutex_unlock(&cdev->lock);
  1115. mutex_unlock(&tz->lock);
  1116. if (!result)
  1117. return 0;
  1118. device_remove_file(&tz->device, &dev->weight_attr);
  1119. remove_trip_file:
  1120. device_remove_file(&tz->device, &dev->attr);
  1121. remove_symbol_link:
  1122. sysfs_remove_link(&tz->device.kobj, dev->name);
  1123. release_idr:
  1124. release_idr(&tz->idr, &tz->lock, dev->id);
  1125. free_mem:
  1126. kfree(dev);
  1127. return result;
  1128. }
  1129. EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
  1130. /**
  1131. * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
  1132. * thermal zone.
  1133. * @tz: pointer to a struct thermal_zone_device.
  1134. * @trip: indicates which trip point the cooling devices is
  1135. * associated with in this thermal zone.
  1136. * @cdev: pointer to a struct thermal_cooling_device.
  1137. *
  1138. * This interface function unbind a thermal cooling device from the certain
  1139. * trip point of a thermal zone device.
  1140. * This function is usually called in the thermal zone device .unbind callback.
  1141. *
  1142. * Return: 0 on success, the proper error value otherwise.
  1143. */
  1144. int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
  1145. int trip,
  1146. struct thermal_cooling_device *cdev)
  1147. {
  1148. struct thermal_instance *pos, *next;
  1149. mutex_lock(&tz->lock);
  1150. mutex_lock(&cdev->lock);
  1151. list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
  1152. if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
  1153. list_del(&pos->tz_node);
  1154. list_del(&pos->cdev_node);
  1155. mutex_unlock(&cdev->lock);
  1156. mutex_unlock(&tz->lock);
  1157. goto unbind;
  1158. }
  1159. }
  1160. mutex_unlock(&cdev->lock);
  1161. mutex_unlock(&tz->lock);
  1162. return -ENODEV;
  1163. unbind:
  1164. device_remove_file(&tz->device, &pos->weight_attr);
  1165. device_remove_file(&tz->device, &pos->attr);
  1166. sysfs_remove_link(&tz->device.kobj, pos->name);
  1167. release_idr(&tz->idr, &tz->lock, pos->id);
  1168. kfree(pos);
  1169. return 0;
  1170. }
  1171. EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
  1172. static void thermal_release(struct device *dev)
  1173. {
  1174. struct thermal_zone_device *tz;
  1175. struct thermal_cooling_device *cdev;
  1176. if (!strncmp(dev_name(dev), "thermal_zone",
  1177. sizeof("thermal_zone") - 1)) {
  1178. tz = to_thermal_zone(dev);
  1179. kfree(tz);
  1180. } else if(!strncmp(dev_name(dev), "cooling_device",
  1181. sizeof("cooling_device") - 1)){
  1182. cdev = to_cooling_device(dev);
  1183. kfree(cdev);
  1184. }
  1185. }
  1186. static struct class thermal_class = {
  1187. .name = "thermal",
  1188. .dev_release = thermal_release,
  1189. };
  1190. /**
  1191. * __thermal_cooling_device_register() - register a new thermal cooling device
  1192. * @np: a pointer to a device tree node.
  1193. * @type: the thermal cooling device type.
  1194. * @devdata: device private data.
  1195. * @ops: standard thermal cooling devices callbacks.
  1196. *
  1197. * This interface function adds a new thermal cooling device (fan/processor/...)
  1198. * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
  1199. * to all the thermal zone devices registered at the same time.
  1200. * It also gives the opportunity to link the cooling device to a device tree
  1201. * node, so that it can be bound to a thermal zone created out of device tree.
  1202. *
  1203. * Return: a pointer to the created struct thermal_cooling_device or an
  1204. * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
  1205. */
  1206. static struct thermal_cooling_device *
  1207. __thermal_cooling_device_register(struct device_node *np,
  1208. char *type, void *devdata,
  1209. const struct thermal_cooling_device_ops *ops)
  1210. {
  1211. struct thermal_cooling_device *cdev;
  1212. struct thermal_zone_device *pos = NULL;
  1213. int result;
  1214. if (type && strlen(type) >= THERMAL_NAME_LENGTH)
  1215. return ERR_PTR(-EINVAL);
  1216. if (!ops || !ops->get_max_state || !ops->get_cur_state ||
  1217. !ops->set_cur_state)
  1218. return ERR_PTR(-EINVAL);
  1219. cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
  1220. if (!cdev)
  1221. return ERR_PTR(-ENOMEM);
  1222. result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
  1223. if (result) {
  1224. kfree(cdev);
  1225. return ERR_PTR(result);
  1226. }
  1227. strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
  1228. mutex_init(&cdev->lock);
  1229. INIT_LIST_HEAD(&cdev->thermal_instances);
  1230. cdev->np = np;
  1231. cdev->ops = ops;
  1232. cdev->updated = false;
  1233. cdev->device.class = &thermal_class;
  1234. cdev->device.groups = cooling_device_attr_groups;
  1235. cdev->devdata = devdata;
  1236. dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
  1237. result = device_register(&cdev->device);
  1238. if (result) {
  1239. release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
  1240. kfree(cdev);
  1241. return ERR_PTR(result);
  1242. }
  1243. /* Add 'this' new cdev to the global cdev list */
  1244. mutex_lock(&thermal_list_lock);
  1245. list_add(&cdev->node, &thermal_cdev_list);
  1246. mutex_unlock(&thermal_list_lock);
  1247. /* Update binding information for 'this' new cdev */
  1248. bind_cdev(cdev);
  1249. mutex_lock(&thermal_list_lock);
  1250. list_for_each_entry(pos, &thermal_tz_list, node)
  1251. if (atomic_cmpxchg(&pos->need_update, 1, 0))
  1252. thermal_zone_device_update(pos);
  1253. mutex_unlock(&thermal_list_lock);
  1254. return cdev;
  1255. }
  1256. /**
  1257. * thermal_cooling_device_register() - register a new thermal cooling device
  1258. * @type: the thermal cooling device type.
  1259. * @devdata: device private data.
  1260. * @ops: standard thermal cooling devices callbacks.
  1261. *
  1262. * This interface function adds a new thermal cooling device (fan/processor/...)
  1263. * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
  1264. * to all the thermal zone devices registered at the same time.
  1265. *
  1266. * Return: a pointer to the created struct thermal_cooling_device or an
  1267. * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
  1268. */
  1269. struct thermal_cooling_device *
  1270. thermal_cooling_device_register(char *type, void *devdata,
  1271. const struct thermal_cooling_device_ops *ops)
  1272. {
  1273. return __thermal_cooling_device_register(NULL, type, devdata, ops);
  1274. }
  1275. EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
  1276. /**
  1277. * thermal_of_cooling_device_register() - register an OF thermal cooling device
  1278. * @np: a pointer to a device tree node.
  1279. * @type: the thermal cooling device type.
  1280. * @devdata: device private data.
  1281. * @ops: standard thermal cooling devices callbacks.
  1282. *
  1283. * This function will register a cooling device with device tree node reference.
  1284. * This interface function adds a new thermal cooling device (fan/processor/...)
  1285. * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
  1286. * to all the thermal zone devices registered at the same time.
  1287. *
  1288. * Return: a pointer to the created struct thermal_cooling_device or an
  1289. * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
  1290. */
  1291. struct thermal_cooling_device *
  1292. thermal_of_cooling_device_register(struct device_node *np,
  1293. char *type, void *devdata,
  1294. const struct thermal_cooling_device_ops *ops)
  1295. {
  1296. return __thermal_cooling_device_register(np, type, devdata, ops);
  1297. }
  1298. EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
  1299. /**
  1300. * thermal_cooling_device_unregister - removes the registered thermal cooling device
  1301. * @cdev: the thermal cooling device to remove.
  1302. *
  1303. * thermal_cooling_device_unregister() must be called when the device is no
  1304. * longer needed.
  1305. */
  1306. void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
  1307. {
  1308. int i;
  1309. const struct thermal_zone_params *tzp;
  1310. struct thermal_zone_device *tz;
  1311. struct thermal_cooling_device *pos = NULL;
  1312. if (!cdev)
  1313. return;
  1314. mutex_lock(&thermal_list_lock);
  1315. list_for_each_entry(pos, &thermal_cdev_list, node)
  1316. if (pos == cdev)
  1317. break;
  1318. if (pos != cdev) {
  1319. /* thermal cooling device not found */
  1320. mutex_unlock(&thermal_list_lock);
  1321. return;
  1322. }
  1323. list_del(&cdev->node);
  1324. /* Unbind all thermal zones associated with 'this' cdev */
  1325. list_for_each_entry(tz, &thermal_tz_list, node) {
  1326. if (tz->ops->unbind) {
  1327. tz->ops->unbind(tz, cdev);
  1328. continue;
  1329. }
  1330. if (!tz->tzp || !tz->tzp->tbp)
  1331. continue;
  1332. tzp = tz->tzp;
  1333. for (i = 0; i < tzp->num_tbps; i++) {
  1334. if (tzp->tbp[i].cdev == cdev) {
  1335. __unbind(tz, tzp->tbp[i].trip_mask, cdev);
  1336. tzp->tbp[i].cdev = NULL;
  1337. }
  1338. }
  1339. }
  1340. mutex_unlock(&thermal_list_lock);
  1341. if (cdev->type[0])
  1342. device_remove_file(&cdev->device, &dev_attr_cdev_type);
  1343. device_remove_file(&cdev->device, &dev_attr_max_state);
  1344. device_remove_file(&cdev->device, &dev_attr_cur_state);
  1345. release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
  1346. device_unregister(&cdev->device);
  1347. return;
  1348. }
  1349. EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
  1350. void thermal_cdev_update(struct thermal_cooling_device *cdev)
  1351. {
  1352. struct thermal_instance *instance;
  1353. unsigned long target = 0;
  1354. /* cooling device is updated*/
  1355. if (cdev->updated)
  1356. return;
  1357. mutex_lock(&cdev->lock);
  1358. /* Make sure cdev enters the deepest cooling state */
  1359. list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
  1360. dev_dbg(&cdev->device, "zone%d->target=%lu\n",
  1361. instance->tz->id, instance->target);
  1362. if (instance->target == THERMAL_NO_TARGET)
  1363. continue;
  1364. if (instance->target > target)
  1365. target = instance->target;
  1366. }
  1367. mutex_unlock(&cdev->lock);
  1368. cdev->ops->set_cur_state(cdev, target);
  1369. cdev->updated = true;
  1370. trace_cdev_update(cdev, target);
  1371. dev_dbg(&cdev->device, "set to state %lu\n", target);
  1372. }
  1373. EXPORT_SYMBOL(thermal_cdev_update);
  1374. /**
  1375. * thermal_notify_framework - Sensor drivers use this API to notify framework
  1376. * @tz: thermal zone device
  1377. * @trip: indicates which trip point has been crossed
  1378. *
  1379. * This function handles the trip events from sensor drivers. It starts
  1380. * throttling the cooling devices according to the policy configured.
  1381. * For CRITICAL and HOT trip points, this notifies the respective drivers,
  1382. * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
  1383. * The throttling policy is based on the configured platform data; if no
  1384. * platform data is provided, this uses the step_wise throttling policy.
  1385. */
  1386. void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
  1387. {
  1388. handle_thermal_trip(tz, trip);
  1389. }
  1390. EXPORT_SYMBOL_GPL(thermal_notify_framework);
  1391. /**
  1392. * create_trip_attrs() - create attributes for trip points
  1393. * @tz: the thermal zone device
  1394. * @mask: Writeable trip point bitmap.
  1395. *
  1396. * helper function to instantiate sysfs entries for every trip
  1397. * point and its properties of a struct thermal_zone_device.
  1398. *
  1399. * Return: 0 on success, the proper error value otherwise.
  1400. */
  1401. static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
  1402. {
  1403. int indx;
  1404. int size = sizeof(struct thermal_attr) * tz->trips;
  1405. tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
  1406. if (!tz->trip_type_attrs)
  1407. return -ENOMEM;
  1408. tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
  1409. if (!tz->trip_temp_attrs) {
  1410. kfree(tz->trip_type_attrs);
  1411. return -ENOMEM;
  1412. }
  1413. if (tz->ops->get_trip_hyst) {
  1414. tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
  1415. if (!tz->trip_hyst_attrs) {
  1416. kfree(tz->trip_type_attrs);
  1417. kfree(tz->trip_temp_attrs);
  1418. return -ENOMEM;
  1419. }
  1420. }
  1421. for (indx = 0; indx < tz->trips; indx++) {
  1422. /* create trip type attribute */
  1423. snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
  1424. "trip_point_%d_type", indx);
  1425. sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
  1426. tz->trip_type_attrs[indx].attr.attr.name =
  1427. tz->trip_type_attrs[indx].name;
  1428. tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
  1429. tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
  1430. device_create_file(&tz->device,
  1431. &tz->trip_type_attrs[indx].attr);
  1432. /* create trip temp attribute */
  1433. snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
  1434. "trip_point_%d_temp", indx);
  1435. sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
  1436. tz->trip_temp_attrs[indx].attr.attr.name =
  1437. tz->trip_temp_attrs[indx].name;
  1438. tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
  1439. tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
  1440. if (IS_ENABLED(CONFIG_THERMAL_WRITABLE_TRIPS) &&
  1441. mask & (1 << indx)) {
  1442. tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
  1443. tz->trip_temp_attrs[indx].attr.store =
  1444. trip_point_temp_store;
  1445. }
  1446. device_create_file(&tz->device,
  1447. &tz->trip_temp_attrs[indx].attr);
  1448. /* create Optional trip hyst attribute */
  1449. if (!tz->ops->get_trip_hyst)
  1450. continue;
  1451. snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
  1452. "trip_point_%d_hyst", indx);
  1453. sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
  1454. tz->trip_hyst_attrs[indx].attr.attr.name =
  1455. tz->trip_hyst_attrs[indx].name;
  1456. tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
  1457. tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
  1458. if (tz->ops->set_trip_hyst) {
  1459. tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
  1460. tz->trip_hyst_attrs[indx].attr.store =
  1461. trip_point_hyst_store;
  1462. }
  1463. device_create_file(&tz->device,
  1464. &tz->trip_hyst_attrs[indx].attr);
  1465. }
  1466. return 0;
  1467. }
  1468. static void remove_trip_attrs(struct thermal_zone_device *tz)
  1469. {
  1470. int indx;
  1471. for (indx = 0; indx < tz->trips; indx++) {
  1472. device_remove_file(&tz->device,
  1473. &tz->trip_type_attrs[indx].attr);
  1474. device_remove_file(&tz->device,
  1475. &tz->trip_temp_attrs[indx].attr);
  1476. if (tz->ops->get_trip_hyst)
  1477. device_remove_file(&tz->device,
  1478. &tz->trip_hyst_attrs[indx].attr);
  1479. }
  1480. kfree(tz->trip_type_attrs);
  1481. kfree(tz->trip_temp_attrs);
  1482. kfree(tz->trip_hyst_attrs);
  1483. }
  1484. /**
  1485. * thermal_zone_device_register() - register a new thermal zone device
  1486. * @type: the thermal zone device type
  1487. * @trips: the number of trip points the thermal zone support
  1488. * @mask: a bit string indicating the writeablility of trip points
  1489. * @devdata: private device data
  1490. * @ops: standard thermal zone device callbacks
  1491. * @tzp: thermal zone platform parameters
  1492. * @passive_delay: number of milliseconds to wait between polls when
  1493. * performing passive cooling
  1494. * @polling_delay: number of milliseconds to wait between polls when checking
  1495. * whether trip points have been crossed (0 for interrupt
  1496. * driven systems)
  1497. *
  1498. * This interface function adds a new thermal zone device (sensor) to
  1499. * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
  1500. * thermal cooling devices registered at the same time.
  1501. * thermal_zone_device_unregister() must be called when the device is no
  1502. * longer needed. The passive cooling depends on the .get_trend() return value.
  1503. *
  1504. * Return: a pointer to the created struct thermal_zone_device or an
  1505. * in case of error, an ERR_PTR. Caller must check return value with
  1506. * IS_ERR*() helpers.
  1507. */
  1508. struct thermal_zone_device *thermal_zone_device_register(const char *type,
  1509. int trips, int mask, void *devdata,
  1510. struct thermal_zone_device_ops *ops,
  1511. struct thermal_zone_params *tzp,
  1512. int passive_delay, int polling_delay)
  1513. {
  1514. struct thermal_zone_device *tz;
  1515. enum thermal_trip_type trip_type;
  1516. int trip_temp;
  1517. int result;
  1518. int count;
  1519. int passive = 0;
  1520. struct thermal_governor *governor;
  1521. if (type && strlen(type) >= THERMAL_NAME_LENGTH)
  1522. return ERR_PTR(-EINVAL);
  1523. if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
  1524. return ERR_PTR(-EINVAL);
  1525. if (!ops)
  1526. return ERR_PTR(-EINVAL);
  1527. if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
  1528. return ERR_PTR(-EINVAL);
  1529. tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
  1530. if (!tz)
  1531. return ERR_PTR(-ENOMEM);
  1532. INIT_LIST_HEAD(&tz->thermal_instances);
  1533. idr_init(&tz->idr);
  1534. mutex_init(&tz->lock);
  1535. result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
  1536. if (result) {
  1537. kfree(tz);
  1538. return ERR_PTR(result);
  1539. }
  1540. strlcpy(tz->type, type ? : "", sizeof(tz->type));
  1541. tz->ops = ops;
  1542. tz->tzp = tzp;
  1543. tz->device.class = &thermal_class;
  1544. tz->devdata = devdata;
  1545. tz->trips = trips;
  1546. tz->passive_delay = passive_delay;
  1547. tz->polling_delay = polling_delay;
  1548. /* A new thermal zone needs to be updated anyway. */
  1549. atomic_set(&tz->need_update, 1);
  1550. dev_set_name(&tz->device, "thermal_zone%d", tz->id);
  1551. result = device_register(&tz->device);
  1552. if (result) {
  1553. release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
  1554. kfree(tz);
  1555. return ERR_PTR(result);
  1556. }
  1557. /* sys I/F */
  1558. if (type) {
  1559. result = device_create_file(&tz->device, &dev_attr_type);
  1560. if (result)
  1561. goto unregister;
  1562. }
  1563. result = device_create_file(&tz->device, &dev_attr_temp);
  1564. if (result)
  1565. goto unregister;
  1566. if (ops->get_mode) {
  1567. result = device_create_file(&tz->device, &dev_attr_mode);
  1568. if (result)
  1569. goto unregister;
  1570. }
  1571. result = create_trip_attrs(tz, mask);
  1572. if (result)
  1573. goto unregister;
  1574. for (count = 0; count < trips; count++) {
  1575. if (tz->ops->get_trip_type(tz, count, &trip_type))
  1576. set_bit(count, &tz->trips_disabled);
  1577. if (trip_type == THERMAL_TRIP_PASSIVE)
  1578. passive = 1;
  1579. if (tz->ops->get_trip_temp(tz, count, &trip_temp))
  1580. set_bit(count, &tz->trips_disabled);
  1581. /* Check for bogus trip points */
  1582. if (trip_temp == 0)
  1583. set_bit(count, &tz->trips_disabled);
  1584. }
  1585. if (!passive) {
  1586. result = device_create_file(&tz->device, &dev_attr_passive);
  1587. if (result)
  1588. goto unregister;
  1589. }
  1590. if (IS_ENABLED(CONFIG_THERMAL_EMULATION)) {
  1591. result = device_create_file(&tz->device, &dev_attr_emul_temp);
  1592. if (result)
  1593. goto unregister;
  1594. }
  1595. /* Create policy attribute */
  1596. result = device_create_file(&tz->device, &dev_attr_policy);
  1597. if (result)
  1598. goto unregister;
  1599. /* Add thermal zone params */
  1600. result = create_tzp_attrs(&tz->device);
  1601. if (result)
  1602. goto unregister;
  1603. /* Create available_policies attribute */
  1604. result = device_create_file(&tz->device, &dev_attr_available_policies);
  1605. if (result)
  1606. goto unregister;
  1607. /* Update 'this' zone's governor information */
  1608. mutex_lock(&thermal_governor_lock);
  1609. if (tz->tzp)
  1610. governor = __find_governor(tz->tzp->governor_name);
  1611. else
  1612. governor = def_governor;
  1613. result = thermal_set_governor(tz, governor);
  1614. if (result) {
  1615. mutex_unlock(&thermal_governor_lock);
  1616. goto unregister;
  1617. }
  1618. mutex_unlock(&thermal_governor_lock);
  1619. if (!tz->tzp || !tz->tzp->no_hwmon) {
  1620. result = thermal_add_hwmon_sysfs(tz);
  1621. if (result)
  1622. goto unregister;
  1623. }
  1624. mutex_lock(&thermal_list_lock);
  1625. list_add_tail(&tz->node, &thermal_tz_list);
  1626. mutex_unlock(&thermal_list_lock);
  1627. /* Bind cooling devices for this zone */
  1628. bind_tz(tz);
  1629. INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
  1630. thermal_zone_device_reset(tz);
  1631. /* Update the new thermal zone and mark it as already updated. */
  1632. if (atomic_cmpxchg(&tz->need_update, 1, 0))
  1633. thermal_zone_device_update(tz);
  1634. return tz;
  1635. unregister:
  1636. release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
  1637. device_unregister(&tz->device);
  1638. return ERR_PTR(result);
  1639. }
  1640. EXPORT_SYMBOL_GPL(thermal_zone_device_register);
  1641. /**
  1642. * thermal_device_unregister - removes the registered thermal zone device
  1643. * @tz: the thermal zone device to remove
  1644. */
  1645. void thermal_zone_device_unregister(struct thermal_zone_device *tz)
  1646. {
  1647. int i;
  1648. const struct thermal_zone_params *tzp;
  1649. struct thermal_cooling_device *cdev;
  1650. struct thermal_zone_device *pos = NULL;
  1651. if (!tz)
  1652. return;
  1653. tzp = tz->tzp;
  1654. mutex_lock(&thermal_list_lock);
  1655. list_for_each_entry(pos, &thermal_tz_list, node)
  1656. if (pos == tz)
  1657. break;
  1658. if (pos != tz) {
  1659. /* thermal zone device not found */
  1660. mutex_unlock(&thermal_list_lock);
  1661. return;
  1662. }
  1663. list_del(&tz->node);
  1664. /* Unbind all cdevs associated with 'this' thermal zone */
  1665. list_for_each_entry(cdev, &thermal_cdev_list, node) {
  1666. if (tz->ops->unbind) {
  1667. tz->ops->unbind(tz, cdev);
  1668. continue;
  1669. }
  1670. if (!tzp || !tzp->tbp)
  1671. break;
  1672. for (i = 0; i < tzp->num_tbps; i++) {
  1673. if (tzp->tbp[i].cdev == cdev) {
  1674. __unbind(tz, tzp->tbp[i].trip_mask, cdev);
  1675. tzp->tbp[i].cdev = NULL;
  1676. }
  1677. }
  1678. }
  1679. mutex_unlock(&thermal_list_lock);
  1680. thermal_zone_device_set_polling(tz, 0);
  1681. if (tz->type[0])
  1682. device_remove_file(&tz->device, &dev_attr_type);
  1683. device_remove_file(&tz->device, &dev_attr_temp);
  1684. if (tz->ops->get_mode)
  1685. device_remove_file(&tz->device, &dev_attr_mode);
  1686. device_remove_file(&tz->device, &dev_attr_policy);
  1687. device_remove_file(&tz->device, &dev_attr_available_policies);
  1688. remove_trip_attrs(tz);
  1689. thermal_set_governor(tz, NULL);
  1690. thermal_remove_hwmon_sysfs(tz);
  1691. release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
  1692. idr_destroy(&tz->idr);
  1693. mutex_destroy(&tz->lock);
  1694. device_unregister(&tz->device);
  1695. return;
  1696. }
  1697. EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
  1698. /**
  1699. * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
  1700. * @name: thermal zone name to fetch the temperature
  1701. *
  1702. * When only one zone is found with the passed name, returns a reference to it.
  1703. *
  1704. * Return: On success returns a reference to an unique thermal zone with
  1705. * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
  1706. * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
  1707. */
  1708. struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
  1709. {
  1710. struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
  1711. unsigned int found = 0;
  1712. if (!name)
  1713. goto exit;
  1714. mutex_lock(&thermal_list_lock);
  1715. list_for_each_entry(pos, &thermal_tz_list, node)
  1716. if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
  1717. found++;
  1718. ref = pos;
  1719. }
  1720. mutex_unlock(&thermal_list_lock);
  1721. /* nothing has been found, thus an error code for it */
  1722. if (found == 0)
  1723. ref = ERR_PTR(-ENODEV);
  1724. else if (found > 1)
  1725. /* Success only when an unique zone is found */
  1726. ref = ERR_PTR(-EEXIST);
  1727. exit:
  1728. return ref;
  1729. }
  1730. EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
  1731. #ifdef CONFIG_NET
  1732. static const struct genl_multicast_group thermal_event_mcgrps[] = {
  1733. { .name = THERMAL_GENL_MCAST_GROUP_NAME, },
  1734. };
  1735. static struct genl_family thermal_event_genl_family = {
  1736. .id = GENL_ID_GENERATE,
  1737. .name = THERMAL_GENL_FAMILY_NAME,
  1738. .version = THERMAL_GENL_VERSION,
  1739. .maxattr = THERMAL_GENL_ATTR_MAX,
  1740. .mcgrps = thermal_event_mcgrps,
  1741. .n_mcgrps = ARRAY_SIZE(thermal_event_mcgrps),
  1742. };
  1743. int thermal_generate_netlink_event(struct thermal_zone_device *tz,
  1744. enum events event)
  1745. {
  1746. struct sk_buff *skb;
  1747. struct nlattr *attr;
  1748. struct thermal_genl_event *thermal_event;
  1749. void *msg_header;
  1750. int size;
  1751. int result;
  1752. static unsigned int thermal_event_seqnum;
  1753. if (!tz)
  1754. return -EINVAL;
  1755. /* allocate memory */
  1756. size = nla_total_size(sizeof(struct thermal_genl_event)) +
  1757. nla_total_size(0);
  1758. skb = genlmsg_new(size, GFP_ATOMIC);
  1759. if (!skb)
  1760. return -ENOMEM;
  1761. /* add the genetlink message header */
  1762. msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
  1763. &thermal_event_genl_family, 0,
  1764. THERMAL_GENL_CMD_EVENT);
  1765. if (!msg_header) {
  1766. nlmsg_free(skb);
  1767. return -ENOMEM;
  1768. }
  1769. /* fill the data */
  1770. attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
  1771. sizeof(struct thermal_genl_event));
  1772. if (!attr) {
  1773. nlmsg_free(skb);
  1774. return -EINVAL;
  1775. }
  1776. thermal_event = nla_data(attr);
  1777. if (!thermal_event) {
  1778. nlmsg_free(skb);
  1779. return -EINVAL;
  1780. }
  1781. memset(thermal_event, 0, sizeof(struct thermal_genl_event));
  1782. thermal_event->orig = tz->id;
  1783. thermal_event->event = event;
  1784. /* send multicast genetlink message */
  1785. genlmsg_end(skb, msg_header);
  1786. result = genlmsg_multicast(&thermal_event_genl_family, skb, 0,
  1787. 0, GFP_ATOMIC);
  1788. if (result)
  1789. dev_err(&tz->device, "Failed to send netlink event:%d", result);
  1790. return result;
  1791. }
  1792. EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
  1793. static int genetlink_init(void)
  1794. {
  1795. return genl_register_family(&thermal_event_genl_family);
  1796. }
  1797. static void genetlink_exit(void)
  1798. {
  1799. genl_unregister_family(&thermal_event_genl_family);
  1800. }
  1801. #else /* !CONFIG_NET */
  1802. static inline int genetlink_init(void) { return 0; }
  1803. static inline void genetlink_exit(void) {}
  1804. #endif /* !CONFIG_NET */
  1805. static int __init thermal_register_governors(void)
  1806. {
  1807. int result;
  1808. result = thermal_gov_step_wise_register();
  1809. if (result)
  1810. return result;
  1811. result = thermal_gov_fair_share_register();
  1812. if (result)
  1813. return result;
  1814. result = thermal_gov_bang_bang_register();
  1815. if (result)
  1816. return result;
  1817. result = thermal_gov_user_space_register();
  1818. if (result)
  1819. return result;
  1820. return thermal_gov_power_allocator_register();
  1821. }
  1822. static void thermal_unregister_governors(void)
  1823. {
  1824. thermal_gov_step_wise_unregister();
  1825. thermal_gov_fair_share_unregister();
  1826. thermal_gov_bang_bang_unregister();
  1827. thermal_gov_user_space_unregister();
  1828. thermal_gov_power_allocator_unregister();
  1829. }
  1830. static int thermal_pm_notify(struct notifier_block *nb,
  1831. unsigned long mode, void *_unused)
  1832. {
  1833. struct thermal_zone_device *tz;
  1834. switch (mode) {
  1835. case PM_HIBERNATION_PREPARE:
  1836. case PM_RESTORE_PREPARE:
  1837. case PM_SUSPEND_PREPARE:
  1838. atomic_set(&in_suspend, 1);
  1839. break;
  1840. case PM_POST_HIBERNATION:
  1841. case PM_POST_RESTORE:
  1842. case PM_POST_SUSPEND:
  1843. atomic_set(&in_suspend, 0);
  1844. list_for_each_entry(tz, &thermal_tz_list, node) {
  1845. thermal_zone_device_reset(tz);
  1846. thermal_zone_device_update(tz);
  1847. }
  1848. break;
  1849. default:
  1850. break;
  1851. }
  1852. return 0;
  1853. }
  1854. static struct notifier_block thermal_pm_nb = {
  1855. .notifier_call = thermal_pm_notify,
  1856. };
  1857. static int __init thermal_init(void)
  1858. {
  1859. int result;
  1860. result = thermal_register_governors();
  1861. if (result)
  1862. goto error;
  1863. result = class_register(&thermal_class);
  1864. if (result)
  1865. goto unregister_governors;
  1866. result = genetlink_init();
  1867. if (result)
  1868. goto unregister_class;
  1869. result = of_parse_thermal_zones();
  1870. if (result)
  1871. goto exit_netlink;
  1872. result = register_pm_notifier(&thermal_pm_nb);
  1873. if (result)
  1874. pr_warn("Thermal: Can not register suspend notifier, return %d\n",
  1875. result);
  1876. return 0;
  1877. exit_netlink:
  1878. genetlink_exit();
  1879. unregister_class:
  1880. class_unregister(&thermal_class);
  1881. unregister_governors:
  1882. thermal_unregister_governors();
  1883. error:
  1884. idr_destroy(&thermal_tz_idr);
  1885. idr_destroy(&thermal_cdev_idr);
  1886. mutex_destroy(&thermal_idr_lock);
  1887. mutex_destroy(&thermal_list_lock);
  1888. mutex_destroy(&thermal_governor_lock);
  1889. return result;
  1890. }
  1891. static void __exit thermal_exit(void)
  1892. {
  1893. unregister_pm_notifier(&thermal_pm_nb);
  1894. of_thermal_destroy_zones();
  1895. genetlink_exit();
  1896. class_unregister(&thermal_class);
  1897. thermal_unregister_governors();
  1898. idr_destroy(&thermal_tz_idr);
  1899. idr_destroy(&thermal_cdev_idr);
  1900. mutex_destroy(&thermal_idr_lock);
  1901. mutex_destroy(&thermal_list_lock);
  1902. mutex_destroy(&thermal_governor_lock);
  1903. }
  1904. fs_initcall(thermal_init);
  1905. module_exit(thermal_exit);