thermal_core.c 55 KB

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