thermal_core.c 55 KB

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