thermal_sys.c 40 KB

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