thermal_sys.c 41 KB

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