thermal_sys.c 47 KB

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