thermal_sys.c 46 KB

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