thermal_sys.c 43 KB

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