thermal_core.c 55 KB

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