thermal_core.c 58 KB

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