thermal_core.c 56 KB

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