thermal_core.c 61 KB

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