thermal_sys.c 45 KB

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