thermal_core.c 47 KB

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