thermal_sys.c 44 KB

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