thermal_core.c 47 KB

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