thermal_core.c 44 KB

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