thermal_core.c 47 KB

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