thermal_core.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867
  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 (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. gov = __find_governor(strim(name));
  614. if (!gov)
  615. goto exit;
  616. tz->governor = gov;
  617. ret = count;
  618. exit:
  619. mutex_unlock(&thermal_governor_lock);
  620. return ret;
  621. }
  622. static ssize_t
  623. policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
  624. {
  625. struct thermal_zone_device *tz = to_thermal_zone(dev);
  626. return sprintf(buf, "%s\n", tz->governor->name);
  627. }
  628. #ifdef CONFIG_THERMAL_EMULATION
  629. static ssize_t
  630. emul_temp_store(struct device *dev, struct device_attribute *attr,
  631. const char *buf, size_t count)
  632. {
  633. struct thermal_zone_device *tz = to_thermal_zone(dev);
  634. int ret = 0;
  635. unsigned long temperature;
  636. if (kstrtoul(buf, 10, &temperature))
  637. return -EINVAL;
  638. if (!tz->ops->set_emul_temp) {
  639. mutex_lock(&tz->lock);
  640. tz->emul_temperature = temperature;
  641. mutex_unlock(&tz->lock);
  642. } else {
  643. ret = tz->ops->set_emul_temp(tz, temperature);
  644. }
  645. if (!ret)
  646. thermal_zone_device_update(tz);
  647. return ret ? ret : count;
  648. }
  649. static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
  650. #endif/*CONFIG_THERMAL_EMULATION*/
  651. static DEVICE_ATTR(type, 0444, type_show, NULL);
  652. static DEVICE_ATTR(temp, 0444, temp_show, NULL);
  653. static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
  654. static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
  655. static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
  656. /* sys I/F for cooling device */
  657. #define to_cooling_device(_dev) \
  658. container_of(_dev, struct thermal_cooling_device, device)
  659. static ssize_t
  660. thermal_cooling_device_type_show(struct device *dev,
  661. struct device_attribute *attr, char *buf)
  662. {
  663. struct thermal_cooling_device *cdev = to_cooling_device(dev);
  664. return sprintf(buf, "%s\n", cdev->type);
  665. }
  666. static ssize_t
  667. thermal_cooling_device_max_state_show(struct device *dev,
  668. struct device_attribute *attr, char *buf)
  669. {
  670. struct thermal_cooling_device *cdev = to_cooling_device(dev);
  671. unsigned long state;
  672. int ret;
  673. ret = cdev->ops->get_max_state(cdev, &state);
  674. if (ret)
  675. return ret;
  676. return sprintf(buf, "%ld\n", state);
  677. }
  678. static ssize_t
  679. thermal_cooling_device_cur_state_show(struct device *dev,
  680. struct device_attribute *attr, char *buf)
  681. {
  682. struct thermal_cooling_device *cdev = to_cooling_device(dev);
  683. unsigned long state;
  684. int ret;
  685. ret = cdev->ops->get_cur_state(cdev, &state);
  686. if (ret)
  687. return ret;
  688. return sprintf(buf, "%ld\n", state);
  689. }
  690. static ssize_t
  691. thermal_cooling_device_cur_state_store(struct device *dev,
  692. struct device_attribute *attr,
  693. const char *buf, size_t count)
  694. {
  695. struct thermal_cooling_device *cdev = to_cooling_device(dev);
  696. unsigned long state;
  697. int result;
  698. if (!sscanf(buf, "%ld\n", &state))
  699. return -EINVAL;
  700. if ((long)state < 0)
  701. return -EINVAL;
  702. result = cdev->ops->set_cur_state(cdev, state);
  703. if (result)
  704. return result;
  705. return count;
  706. }
  707. static struct device_attribute dev_attr_cdev_type =
  708. __ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
  709. static DEVICE_ATTR(max_state, 0444,
  710. thermal_cooling_device_max_state_show, NULL);
  711. static DEVICE_ATTR(cur_state, 0644,
  712. thermal_cooling_device_cur_state_show,
  713. thermal_cooling_device_cur_state_store);
  714. static ssize_t
  715. thermal_cooling_device_trip_point_show(struct device *dev,
  716. struct device_attribute *attr, char *buf)
  717. {
  718. struct thermal_instance *instance;
  719. instance =
  720. container_of(attr, struct thermal_instance, attr);
  721. if (instance->trip == THERMAL_TRIPS_NONE)
  722. return sprintf(buf, "-1\n");
  723. else
  724. return sprintf(buf, "%d\n", instance->trip);
  725. }
  726. /* Device management */
  727. /**
  728. * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
  729. * @tz: pointer to struct thermal_zone_device
  730. * @trip: indicates which trip point the cooling devices is
  731. * associated with in this thermal zone.
  732. * @cdev: pointer to struct thermal_cooling_device
  733. * @upper: the Maximum cooling state for this trip point.
  734. * THERMAL_NO_LIMIT means no upper limit,
  735. * and the cooling device can be in max_state.
  736. * @lower: the Minimum cooling state can be used for this trip point.
  737. * THERMAL_NO_LIMIT means no lower limit,
  738. * and the cooling device can be in cooling state 0.
  739. *
  740. * This interface function bind a thermal cooling device to the certain trip
  741. * point of a thermal zone device.
  742. * This function is usually called in the thermal zone device .bind callback.
  743. *
  744. * Return: 0 on success, the proper error value otherwise.
  745. */
  746. int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
  747. int trip,
  748. struct thermal_cooling_device *cdev,
  749. unsigned long upper, unsigned long lower)
  750. {
  751. struct thermal_instance *dev;
  752. struct thermal_instance *pos;
  753. struct thermal_zone_device *pos1;
  754. struct thermal_cooling_device *pos2;
  755. unsigned long max_state;
  756. int result;
  757. if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
  758. return -EINVAL;
  759. list_for_each_entry(pos1, &thermal_tz_list, node) {
  760. if (pos1 == tz)
  761. break;
  762. }
  763. list_for_each_entry(pos2, &thermal_cdev_list, node) {
  764. if (pos2 == cdev)
  765. break;
  766. }
  767. if (tz != pos1 || cdev != pos2)
  768. return -EINVAL;
  769. cdev->ops->get_max_state(cdev, &max_state);
  770. /* lower default 0, upper default max_state */
  771. lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
  772. upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
  773. if (lower > upper || upper > max_state)
  774. return -EINVAL;
  775. dev =
  776. kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
  777. if (!dev)
  778. return -ENOMEM;
  779. dev->tz = tz;
  780. dev->cdev = cdev;
  781. dev->trip = trip;
  782. dev->upper = upper;
  783. dev->lower = lower;
  784. dev->target = THERMAL_NO_TARGET;
  785. result = get_idr(&tz->idr, &tz->lock, &dev->id);
  786. if (result)
  787. goto free_mem;
  788. sprintf(dev->name, "cdev%d", dev->id);
  789. result =
  790. sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
  791. if (result)
  792. goto release_idr;
  793. sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
  794. sysfs_attr_init(&dev->attr.attr);
  795. dev->attr.attr.name = dev->attr_name;
  796. dev->attr.attr.mode = 0444;
  797. dev->attr.show = thermal_cooling_device_trip_point_show;
  798. result = device_create_file(&tz->device, &dev->attr);
  799. if (result)
  800. goto remove_symbol_link;
  801. mutex_lock(&tz->lock);
  802. mutex_lock(&cdev->lock);
  803. list_for_each_entry(pos, &tz->thermal_instances, tz_node)
  804. if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
  805. result = -EEXIST;
  806. break;
  807. }
  808. if (!result) {
  809. list_add_tail(&dev->tz_node, &tz->thermal_instances);
  810. list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
  811. }
  812. mutex_unlock(&cdev->lock);
  813. mutex_unlock(&tz->lock);
  814. if (!result)
  815. return 0;
  816. device_remove_file(&tz->device, &dev->attr);
  817. remove_symbol_link:
  818. sysfs_remove_link(&tz->device.kobj, dev->name);
  819. release_idr:
  820. release_idr(&tz->idr, &tz->lock, dev->id);
  821. free_mem:
  822. kfree(dev);
  823. return result;
  824. }
  825. EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
  826. /**
  827. * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
  828. * thermal zone.
  829. * @tz: pointer to a struct thermal_zone_device.
  830. * @trip: indicates which trip point the cooling devices is
  831. * associated with in this thermal zone.
  832. * @cdev: pointer to a struct thermal_cooling_device.
  833. *
  834. * This interface function unbind a thermal cooling device from the certain
  835. * trip point of a thermal zone device.
  836. * This function is usually called in the thermal zone device .unbind callback.
  837. *
  838. * Return: 0 on success, the proper error value otherwise.
  839. */
  840. int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
  841. int trip,
  842. struct thermal_cooling_device *cdev)
  843. {
  844. struct thermal_instance *pos, *next;
  845. mutex_lock(&tz->lock);
  846. mutex_lock(&cdev->lock);
  847. list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
  848. if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
  849. list_del(&pos->tz_node);
  850. list_del(&pos->cdev_node);
  851. mutex_unlock(&cdev->lock);
  852. mutex_unlock(&tz->lock);
  853. goto unbind;
  854. }
  855. }
  856. mutex_unlock(&cdev->lock);
  857. mutex_unlock(&tz->lock);
  858. return -ENODEV;
  859. unbind:
  860. device_remove_file(&tz->device, &pos->attr);
  861. sysfs_remove_link(&tz->device.kobj, pos->name);
  862. release_idr(&tz->idr, &tz->lock, pos->id);
  863. kfree(pos);
  864. return 0;
  865. }
  866. EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
  867. static void thermal_release(struct device *dev)
  868. {
  869. struct thermal_zone_device *tz;
  870. struct thermal_cooling_device *cdev;
  871. if (!strncmp(dev_name(dev), "thermal_zone",
  872. sizeof("thermal_zone") - 1)) {
  873. tz = to_thermal_zone(dev);
  874. kfree(tz);
  875. } else if(!strncmp(dev_name(dev), "cooling_device",
  876. sizeof("cooling_device") - 1)){
  877. cdev = to_cooling_device(dev);
  878. kfree(cdev);
  879. }
  880. }
  881. static struct class thermal_class = {
  882. .name = "thermal",
  883. .dev_release = thermal_release,
  884. };
  885. /**
  886. * __thermal_cooling_device_register() - register a new thermal cooling device
  887. * @np: a pointer to a device tree node.
  888. * @type: the thermal cooling device type.
  889. * @devdata: device private data.
  890. * @ops: standard thermal cooling devices callbacks.
  891. *
  892. * This interface function adds a new thermal cooling device (fan/processor/...)
  893. * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
  894. * to all the thermal zone devices registered at the same time.
  895. * It also gives the opportunity to link the cooling device to a device tree
  896. * node, so that it can be bound to a thermal zone created out of device tree.
  897. *
  898. * Return: a pointer to the created struct thermal_cooling_device or an
  899. * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
  900. */
  901. static struct thermal_cooling_device *
  902. __thermal_cooling_device_register(struct device_node *np,
  903. char *type, void *devdata,
  904. const struct thermal_cooling_device_ops *ops)
  905. {
  906. struct thermal_cooling_device *cdev;
  907. int result;
  908. if (type && strlen(type) >= THERMAL_NAME_LENGTH)
  909. return ERR_PTR(-EINVAL);
  910. if (!ops || !ops->get_max_state || !ops->get_cur_state ||
  911. !ops->set_cur_state)
  912. return ERR_PTR(-EINVAL);
  913. cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
  914. if (!cdev)
  915. return ERR_PTR(-ENOMEM);
  916. result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
  917. if (result) {
  918. kfree(cdev);
  919. return ERR_PTR(result);
  920. }
  921. strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
  922. mutex_init(&cdev->lock);
  923. INIT_LIST_HEAD(&cdev->thermal_instances);
  924. cdev->np = np;
  925. cdev->ops = ops;
  926. cdev->updated = false;
  927. cdev->device.class = &thermal_class;
  928. cdev->devdata = devdata;
  929. dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
  930. result = device_register(&cdev->device);
  931. if (result) {
  932. release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
  933. kfree(cdev);
  934. return ERR_PTR(result);
  935. }
  936. /* sys I/F */
  937. if (type) {
  938. result = device_create_file(&cdev->device, &dev_attr_cdev_type);
  939. if (result)
  940. goto unregister;
  941. }
  942. result = device_create_file(&cdev->device, &dev_attr_max_state);
  943. if (result)
  944. goto unregister;
  945. result = device_create_file(&cdev->device, &dev_attr_cur_state);
  946. if (result)
  947. goto unregister;
  948. /* Add 'this' new cdev to the global cdev list */
  949. mutex_lock(&thermal_list_lock);
  950. list_add(&cdev->node, &thermal_cdev_list);
  951. mutex_unlock(&thermal_list_lock);
  952. /* Update binding information for 'this' new cdev */
  953. bind_cdev(cdev);
  954. return cdev;
  955. unregister:
  956. release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
  957. device_unregister(&cdev->device);
  958. return ERR_PTR(result);
  959. }
  960. /**
  961. * thermal_cooling_device_register() - register a new thermal cooling device
  962. * @type: the thermal cooling device type.
  963. * @devdata: device private data.
  964. * @ops: standard thermal cooling devices callbacks.
  965. *
  966. * This interface function adds a new thermal cooling device (fan/processor/...)
  967. * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
  968. * to all the thermal zone devices registered at the same time.
  969. *
  970. * Return: a pointer to the created struct thermal_cooling_device or an
  971. * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
  972. */
  973. struct thermal_cooling_device *
  974. thermal_cooling_device_register(char *type, void *devdata,
  975. const struct thermal_cooling_device_ops *ops)
  976. {
  977. return __thermal_cooling_device_register(NULL, type, devdata, ops);
  978. }
  979. EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
  980. /**
  981. * thermal_of_cooling_device_register() - register an OF thermal cooling device
  982. * @np: a pointer to a device tree node.
  983. * @type: the thermal cooling device type.
  984. * @devdata: device private data.
  985. * @ops: standard thermal cooling devices callbacks.
  986. *
  987. * This function will register a cooling device with device tree node reference.
  988. * This interface function adds a new thermal cooling device (fan/processor/...)
  989. * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
  990. * to all the thermal zone devices registered at the same time.
  991. *
  992. * Return: a pointer to the created struct thermal_cooling_device or an
  993. * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
  994. */
  995. struct thermal_cooling_device *
  996. thermal_of_cooling_device_register(struct device_node *np,
  997. char *type, void *devdata,
  998. const struct thermal_cooling_device_ops *ops)
  999. {
  1000. return __thermal_cooling_device_register(np, type, devdata, ops);
  1001. }
  1002. EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
  1003. /**
  1004. * thermal_cooling_device_unregister - removes the registered thermal cooling device
  1005. * @cdev: the thermal cooling device to remove.
  1006. *
  1007. * thermal_cooling_device_unregister() must be called when the device is no
  1008. * longer needed.
  1009. */
  1010. void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
  1011. {
  1012. int i;
  1013. const struct thermal_zone_params *tzp;
  1014. struct thermal_zone_device *tz;
  1015. struct thermal_cooling_device *pos = NULL;
  1016. if (!cdev)
  1017. return;
  1018. mutex_lock(&thermal_list_lock);
  1019. list_for_each_entry(pos, &thermal_cdev_list, node)
  1020. if (pos == cdev)
  1021. break;
  1022. if (pos != cdev) {
  1023. /* thermal cooling device not found */
  1024. mutex_unlock(&thermal_list_lock);
  1025. return;
  1026. }
  1027. list_del(&cdev->node);
  1028. /* Unbind all thermal zones associated with 'this' cdev */
  1029. list_for_each_entry(tz, &thermal_tz_list, node) {
  1030. if (tz->ops->unbind) {
  1031. tz->ops->unbind(tz, cdev);
  1032. continue;
  1033. }
  1034. if (!tz->tzp || !tz->tzp->tbp)
  1035. continue;
  1036. tzp = tz->tzp;
  1037. for (i = 0; i < tzp->num_tbps; i++) {
  1038. if (tzp->tbp[i].cdev == cdev) {
  1039. __unbind(tz, tzp->tbp[i].trip_mask, cdev);
  1040. tzp->tbp[i].cdev = NULL;
  1041. }
  1042. }
  1043. }
  1044. mutex_unlock(&thermal_list_lock);
  1045. if (cdev->type[0])
  1046. device_remove_file(&cdev->device, &dev_attr_cdev_type);
  1047. device_remove_file(&cdev->device, &dev_attr_max_state);
  1048. device_remove_file(&cdev->device, &dev_attr_cur_state);
  1049. release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
  1050. device_unregister(&cdev->device);
  1051. return;
  1052. }
  1053. EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
  1054. void thermal_cdev_update(struct thermal_cooling_device *cdev)
  1055. {
  1056. struct thermal_instance *instance;
  1057. unsigned long target = 0;
  1058. /* cooling device is updated*/
  1059. if (cdev->updated)
  1060. return;
  1061. mutex_lock(&cdev->lock);
  1062. /* Make sure cdev enters the deepest cooling state */
  1063. list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
  1064. dev_dbg(&cdev->device, "zone%d->target=%lu\n",
  1065. instance->tz->id, instance->target);
  1066. if (instance->target == THERMAL_NO_TARGET)
  1067. continue;
  1068. if (instance->target > target)
  1069. target = instance->target;
  1070. }
  1071. mutex_unlock(&cdev->lock);
  1072. cdev->ops->set_cur_state(cdev, target);
  1073. cdev->updated = true;
  1074. trace_cdev_update(cdev, target);
  1075. dev_dbg(&cdev->device, "set to state %lu\n", target);
  1076. }
  1077. EXPORT_SYMBOL(thermal_cdev_update);
  1078. /**
  1079. * thermal_notify_framework - Sensor drivers use this API to notify framework
  1080. * @tz: thermal zone device
  1081. * @trip: indicates which trip point has been crossed
  1082. *
  1083. * This function handles the trip events from sensor drivers. It starts
  1084. * throttling the cooling devices according to the policy configured.
  1085. * For CRITICAL and HOT trip points, this notifies the respective drivers,
  1086. * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
  1087. * The throttling policy is based on the configured platform data; if no
  1088. * platform data is provided, this uses the step_wise throttling policy.
  1089. */
  1090. void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
  1091. {
  1092. handle_thermal_trip(tz, trip);
  1093. }
  1094. EXPORT_SYMBOL_GPL(thermal_notify_framework);
  1095. /**
  1096. * create_trip_attrs() - create attributes for trip points
  1097. * @tz: the thermal zone device
  1098. * @mask: Writeable trip point bitmap.
  1099. *
  1100. * helper function to instantiate sysfs entries for every trip
  1101. * point and its properties of a struct thermal_zone_device.
  1102. *
  1103. * Return: 0 on success, the proper error value otherwise.
  1104. */
  1105. static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
  1106. {
  1107. int indx;
  1108. int size = sizeof(struct thermal_attr) * tz->trips;
  1109. tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
  1110. if (!tz->trip_type_attrs)
  1111. return -ENOMEM;
  1112. tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
  1113. if (!tz->trip_temp_attrs) {
  1114. kfree(tz->trip_type_attrs);
  1115. return -ENOMEM;
  1116. }
  1117. if (tz->ops->get_trip_hyst) {
  1118. tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
  1119. if (!tz->trip_hyst_attrs) {
  1120. kfree(tz->trip_type_attrs);
  1121. kfree(tz->trip_temp_attrs);
  1122. return -ENOMEM;
  1123. }
  1124. }
  1125. for (indx = 0; indx < tz->trips; indx++) {
  1126. /* create trip type attribute */
  1127. snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
  1128. "trip_point_%d_type", indx);
  1129. sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
  1130. tz->trip_type_attrs[indx].attr.attr.name =
  1131. tz->trip_type_attrs[indx].name;
  1132. tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
  1133. tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
  1134. device_create_file(&tz->device,
  1135. &tz->trip_type_attrs[indx].attr);
  1136. /* create trip temp attribute */
  1137. snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
  1138. "trip_point_%d_temp", indx);
  1139. sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
  1140. tz->trip_temp_attrs[indx].attr.attr.name =
  1141. tz->trip_temp_attrs[indx].name;
  1142. tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
  1143. tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
  1144. if (mask & (1 << indx)) {
  1145. tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
  1146. tz->trip_temp_attrs[indx].attr.store =
  1147. trip_point_temp_store;
  1148. }
  1149. device_create_file(&tz->device,
  1150. &tz->trip_temp_attrs[indx].attr);
  1151. /* create Optional trip hyst attribute */
  1152. if (!tz->ops->get_trip_hyst)
  1153. continue;
  1154. snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
  1155. "trip_point_%d_hyst", indx);
  1156. sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
  1157. tz->trip_hyst_attrs[indx].attr.attr.name =
  1158. tz->trip_hyst_attrs[indx].name;
  1159. tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
  1160. tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
  1161. if (tz->ops->set_trip_hyst) {
  1162. tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
  1163. tz->trip_hyst_attrs[indx].attr.store =
  1164. trip_point_hyst_store;
  1165. }
  1166. device_create_file(&tz->device,
  1167. &tz->trip_hyst_attrs[indx].attr);
  1168. }
  1169. return 0;
  1170. }
  1171. static void remove_trip_attrs(struct thermal_zone_device *tz)
  1172. {
  1173. int indx;
  1174. for (indx = 0; indx < tz->trips; indx++) {
  1175. device_remove_file(&tz->device,
  1176. &tz->trip_type_attrs[indx].attr);
  1177. device_remove_file(&tz->device,
  1178. &tz->trip_temp_attrs[indx].attr);
  1179. if (tz->ops->get_trip_hyst)
  1180. device_remove_file(&tz->device,
  1181. &tz->trip_hyst_attrs[indx].attr);
  1182. }
  1183. kfree(tz->trip_type_attrs);
  1184. kfree(tz->trip_temp_attrs);
  1185. kfree(tz->trip_hyst_attrs);
  1186. }
  1187. /**
  1188. * thermal_zone_device_register() - register a new thermal zone device
  1189. * @type: the thermal zone device type
  1190. * @trips: the number of trip points the thermal zone support
  1191. * @mask: a bit string indicating the writeablility of trip points
  1192. * @devdata: private device data
  1193. * @ops: standard thermal zone device callbacks
  1194. * @tzp: thermal zone platform parameters
  1195. * @passive_delay: number of milliseconds to wait between polls when
  1196. * performing passive cooling
  1197. * @polling_delay: number of milliseconds to wait between polls when checking
  1198. * whether trip points have been crossed (0 for interrupt
  1199. * driven systems)
  1200. *
  1201. * This interface function adds a new thermal zone device (sensor) to
  1202. * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
  1203. * thermal cooling devices registered at the same time.
  1204. * thermal_zone_device_unregister() must be called when the device is no
  1205. * longer needed. The passive cooling depends on the .get_trend() return value.
  1206. *
  1207. * Return: a pointer to the created struct thermal_zone_device or an
  1208. * in case of error, an ERR_PTR. Caller must check return value with
  1209. * IS_ERR*() helpers.
  1210. */
  1211. struct thermal_zone_device *thermal_zone_device_register(const char *type,
  1212. int trips, int mask, void *devdata,
  1213. struct thermal_zone_device_ops *ops,
  1214. const struct thermal_zone_params *tzp,
  1215. int passive_delay, int polling_delay)
  1216. {
  1217. struct thermal_zone_device *tz;
  1218. enum thermal_trip_type trip_type;
  1219. int result;
  1220. int count;
  1221. int passive = 0;
  1222. if (type && strlen(type) >= THERMAL_NAME_LENGTH)
  1223. return ERR_PTR(-EINVAL);
  1224. if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
  1225. return ERR_PTR(-EINVAL);
  1226. if (!ops)
  1227. return ERR_PTR(-EINVAL);
  1228. if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
  1229. return ERR_PTR(-EINVAL);
  1230. tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
  1231. if (!tz)
  1232. return ERR_PTR(-ENOMEM);
  1233. INIT_LIST_HEAD(&tz->thermal_instances);
  1234. idr_init(&tz->idr);
  1235. mutex_init(&tz->lock);
  1236. result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
  1237. if (result) {
  1238. kfree(tz);
  1239. return ERR_PTR(result);
  1240. }
  1241. strlcpy(tz->type, type ? : "", sizeof(tz->type));
  1242. tz->ops = ops;
  1243. tz->tzp = tzp;
  1244. tz->device.class = &thermal_class;
  1245. tz->devdata = devdata;
  1246. tz->trips = trips;
  1247. tz->passive_delay = passive_delay;
  1248. tz->polling_delay = polling_delay;
  1249. dev_set_name(&tz->device, "thermal_zone%d", tz->id);
  1250. result = device_register(&tz->device);
  1251. if (result) {
  1252. release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
  1253. kfree(tz);
  1254. return ERR_PTR(result);
  1255. }
  1256. /* sys I/F */
  1257. if (type) {
  1258. result = device_create_file(&tz->device, &dev_attr_type);
  1259. if (result)
  1260. goto unregister;
  1261. }
  1262. result = device_create_file(&tz->device, &dev_attr_temp);
  1263. if (result)
  1264. goto unregister;
  1265. if (ops->get_mode) {
  1266. result = device_create_file(&tz->device, &dev_attr_mode);
  1267. if (result)
  1268. goto unregister;
  1269. }
  1270. result = create_trip_attrs(tz, mask);
  1271. if (result)
  1272. goto unregister;
  1273. for (count = 0; count < trips; count++) {
  1274. tz->ops->get_trip_type(tz, count, &trip_type);
  1275. if (trip_type == THERMAL_TRIP_PASSIVE)
  1276. passive = 1;
  1277. }
  1278. if (!passive) {
  1279. result = device_create_file(&tz->device, &dev_attr_passive);
  1280. if (result)
  1281. goto unregister;
  1282. }
  1283. #ifdef CONFIG_THERMAL_EMULATION
  1284. result = device_create_file(&tz->device, &dev_attr_emul_temp);
  1285. if (result)
  1286. goto unregister;
  1287. #endif
  1288. /* Create policy attribute */
  1289. result = device_create_file(&tz->device, &dev_attr_policy);
  1290. if (result)
  1291. goto unregister;
  1292. /* Update 'this' zone's governor information */
  1293. mutex_lock(&thermal_governor_lock);
  1294. if (tz->tzp)
  1295. tz->governor = __find_governor(tz->tzp->governor_name);
  1296. else
  1297. tz->governor = def_governor;
  1298. mutex_unlock(&thermal_governor_lock);
  1299. if (!tz->tzp || !tz->tzp->no_hwmon) {
  1300. result = thermal_add_hwmon_sysfs(tz);
  1301. if (result)
  1302. goto unregister;
  1303. }
  1304. mutex_lock(&thermal_list_lock);
  1305. list_add_tail(&tz->node, &thermal_tz_list);
  1306. mutex_unlock(&thermal_list_lock);
  1307. /* Bind cooling devices for this zone */
  1308. bind_tz(tz);
  1309. INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
  1310. if (!tz->ops->get_temp)
  1311. thermal_zone_device_set_polling(tz, 0);
  1312. thermal_zone_device_update(tz);
  1313. return tz;
  1314. unregister:
  1315. release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
  1316. device_unregister(&tz->device);
  1317. return ERR_PTR(result);
  1318. }
  1319. EXPORT_SYMBOL_GPL(thermal_zone_device_register);
  1320. /**
  1321. * thermal_device_unregister - removes the registered thermal zone device
  1322. * @tz: the thermal zone device to remove
  1323. */
  1324. void thermal_zone_device_unregister(struct thermal_zone_device *tz)
  1325. {
  1326. int i;
  1327. const struct thermal_zone_params *tzp;
  1328. struct thermal_cooling_device *cdev;
  1329. struct thermal_zone_device *pos = NULL;
  1330. if (!tz)
  1331. return;
  1332. cancel_delayed_work_sync(&(tz->poll_queue)); /* force stop pending/running delayed work*/
  1333. tzp = tz->tzp;
  1334. mutex_lock(&thermal_list_lock);
  1335. list_for_each_entry(pos, &thermal_tz_list, node)
  1336. if (pos == tz)
  1337. break;
  1338. if (pos != tz) {
  1339. /* thermal zone device not found */
  1340. mutex_unlock(&thermal_list_lock);
  1341. return;
  1342. }
  1343. list_del(&tz->node);
  1344. /* Unbind all cdevs associated with 'this' thermal zone */
  1345. list_for_each_entry(cdev, &thermal_cdev_list, node) {
  1346. if (tz->ops->unbind) {
  1347. tz->ops->unbind(tz, cdev);
  1348. continue;
  1349. }
  1350. if (!tzp || !tzp->tbp)
  1351. break;
  1352. for (i = 0; i < tzp->num_tbps; i++) {
  1353. if (tzp->tbp[i].cdev == cdev) {
  1354. __unbind(tz, tzp->tbp[i].trip_mask, cdev);
  1355. tzp->tbp[i].cdev = NULL;
  1356. }
  1357. }
  1358. }
  1359. mutex_unlock(&thermal_list_lock);
  1360. /*thermal_zone_device_set_polling(tz, 0);*/
  1361. if (tz->type[0])
  1362. device_remove_file(&tz->device, &dev_attr_type);
  1363. device_remove_file(&tz->device, &dev_attr_temp);
  1364. if (tz->ops->get_mode)
  1365. device_remove_file(&tz->device, &dev_attr_mode);
  1366. device_remove_file(&tz->device, &dev_attr_policy);
  1367. remove_trip_attrs(tz);
  1368. tz->governor = NULL;
  1369. thermal_remove_hwmon_sysfs(tz);
  1370. release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
  1371. idr_destroy(&tz->idr);
  1372. mutex_destroy(&tz->lock);
  1373. device_unregister(&tz->device);
  1374. return;
  1375. }
  1376. EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
  1377. /**
  1378. * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
  1379. * @name: thermal zone name to fetch the temperature
  1380. *
  1381. * When only one zone is found with the passed name, returns a reference to it.
  1382. *
  1383. * Return: On success returns a reference to an unique thermal zone with
  1384. * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
  1385. * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
  1386. */
  1387. struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
  1388. {
  1389. struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
  1390. unsigned int found = 0;
  1391. if (!name)
  1392. goto exit;
  1393. mutex_lock(&thermal_list_lock);
  1394. list_for_each_entry(pos, &thermal_tz_list, node)
  1395. if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
  1396. found++;
  1397. ref = pos;
  1398. }
  1399. mutex_unlock(&thermal_list_lock);
  1400. /* nothing has been found, thus an error code for it */
  1401. if (found == 0)
  1402. ref = ERR_PTR(-ENODEV);
  1403. else if (found > 1)
  1404. /* Success only when an unique zone is found */
  1405. ref = ERR_PTR(-EEXIST);
  1406. exit:
  1407. return ref;
  1408. }
  1409. EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
  1410. #ifdef CONFIG_NET
  1411. static const struct genl_multicast_group thermal_event_mcgrps[] = {
  1412. { .name = THERMAL_GENL_MCAST_GROUP_NAME, },
  1413. };
  1414. static struct genl_family thermal_event_genl_family = {
  1415. .id = GENL_ID_GENERATE,
  1416. .name = THERMAL_GENL_FAMILY_NAME,
  1417. .version = THERMAL_GENL_VERSION,
  1418. .maxattr = THERMAL_GENL_ATTR_MAX,
  1419. .mcgrps = thermal_event_mcgrps,
  1420. .n_mcgrps = ARRAY_SIZE(thermal_event_mcgrps),
  1421. };
  1422. int thermal_generate_netlink_event(struct thermal_zone_device *tz,
  1423. enum events event)
  1424. {
  1425. struct sk_buff *skb;
  1426. struct nlattr *attr;
  1427. struct thermal_genl_event *thermal_event;
  1428. void *msg_header;
  1429. int size;
  1430. int result;
  1431. static unsigned int thermal_event_seqnum;
  1432. if (!tz)
  1433. return -EINVAL;
  1434. /* allocate memory */
  1435. size = nla_total_size(sizeof(struct thermal_genl_event)) +
  1436. nla_total_size(0);
  1437. skb = genlmsg_new(size, GFP_ATOMIC);
  1438. if (!skb)
  1439. return -ENOMEM;
  1440. /* add the genetlink message header */
  1441. msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
  1442. &thermal_event_genl_family, 0,
  1443. THERMAL_GENL_CMD_EVENT);
  1444. if (!msg_header) {
  1445. nlmsg_free(skb);
  1446. return -ENOMEM;
  1447. }
  1448. /* fill the data */
  1449. attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
  1450. sizeof(struct thermal_genl_event));
  1451. if (!attr) {
  1452. nlmsg_free(skb);
  1453. return -EINVAL;
  1454. }
  1455. thermal_event = nla_data(attr);
  1456. if (!thermal_event) {
  1457. nlmsg_free(skb);
  1458. return -EINVAL;
  1459. }
  1460. memset(thermal_event, 0, sizeof(struct thermal_genl_event));
  1461. thermal_event->orig = tz->id;
  1462. thermal_event->event = event;
  1463. /* send multicast genetlink message */
  1464. result = genlmsg_end(skb, msg_header);
  1465. if (result < 0) {
  1466. nlmsg_free(skb);
  1467. return result;
  1468. }
  1469. result = genlmsg_multicast(&thermal_event_genl_family, skb, 0,
  1470. 0, GFP_ATOMIC);
  1471. if (result)
  1472. dev_err(&tz->device, "Failed to send netlink event:%d", result);
  1473. return result;
  1474. }
  1475. EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
  1476. static int genetlink_init(void)
  1477. {
  1478. return genl_register_family(&thermal_event_genl_family);
  1479. }
  1480. static void genetlink_exit(void)
  1481. {
  1482. genl_unregister_family(&thermal_event_genl_family);
  1483. }
  1484. #else /* !CONFIG_NET */
  1485. static inline int genetlink_init(void) { return 0; }
  1486. static inline void genetlink_exit(void) {}
  1487. #endif /* !CONFIG_NET */
  1488. static int __init thermal_register_governors(void)
  1489. {
  1490. int result;
  1491. result = thermal_gov_step_wise_register();
  1492. if (result)
  1493. return result;
  1494. result = thermal_gov_fair_share_register();
  1495. if (result)
  1496. return result;
  1497. result = thermal_gov_bang_bang_register();
  1498. if (result)
  1499. return result;
  1500. return thermal_gov_user_space_register();
  1501. }
  1502. static void thermal_unregister_governors(void)
  1503. {
  1504. thermal_gov_step_wise_unregister();
  1505. thermal_gov_fair_share_unregister();
  1506. thermal_gov_bang_bang_unregister();
  1507. thermal_gov_user_space_unregister();
  1508. }
  1509. static int __init thermal_init(void)
  1510. {
  1511. int result;
  1512. result = thermal_register_governors();
  1513. if (result)
  1514. goto error;
  1515. result = class_register(&thermal_class);
  1516. if (result)
  1517. goto unregister_governors;
  1518. result = genetlink_init();
  1519. if (result)
  1520. goto unregister_class;
  1521. result = of_parse_thermal_zones();
  1522. if (result)
  1523. goto exit_netlink;
  1524. return 0;
  1525. exit_netlink:
  1526. genetlink_exit();
  1527. unregister_class:
  1528. class_unregister(&thermal_class);
  1529. unregister_governors:
  1530. thermal_unregister_governors();
  1531. error:
  1532. idr_destroy(&thermal_tz_idr);
  1533. idr_destroy(&thermal_cdev_idr);
  1534. mutex_destroy(&thermal_idr_lock);
  1535. mutex_destroy(&thermal_list_lock);
  1536. mutex_destroy(&thermal_governor_lock);
  1537. return result;
  1538. }
  1539. static void __exit thermal_exit(void)
  1540. {
  1541. of_thermal_destroy_zones();
  1542. genetlink_exit();
  1543. class_unregister(&thermal_class);
  1544. thermal_unregister_governors();
  1545. idr_destroy(&thermal_tz_idr);
  1546. idr_destroy(&thermal_cdev_idr);
  1547. mutex_destroy(&thermal_idr_lock);
  1548. mutex_destroy(&thermal_list_lock);
  1549. mutex_destroy(&thermal_governor_lock);
  1550. }
  1551. fs_initcall(thermal_init);
  1552. module_exit(thermal_exit);