thermal_sys.c 45 KB

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