thermal_core.c 48 KB

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