thermal_sys.c 45 KB

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