thermal_sys.c 46 KB

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