thermal_core.c 47 KB

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