thermal_sys.c 47 KB

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