thermal_sys.c 41 KB

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