thermal_core.c 71 KB

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