thermal_core.c 44 KB

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