thermal_core.c 58 KB

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