thermal_sys.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613
  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_IDR_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. if (delay > 1000)
  583. mod_delayed_work(system_freezable_wq, &tz->poll_queue,
  584. round_jiffies(msecs_to_jiffies(delay)));
  585. else if (delay)
  586. mod_delayed_work(system_freezable_wq, &tz->poll_queue,
  587. msecs_to_jiffies(delay));
  588. else
  589. cancel_delayed_work(&tz->poll_queue);
  590. }
  591. static void thermal_zone_device_check(struct work_struct *work)
  592. {
  593. struct thermal_zone_device *tz = container_of(work, struct
  594. thermal_zone_device,
  595. poll_queue.work);
  596. thermal_zone_device_update(tz);
  597. }
  598. /**
  599. * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
  600. * @tz: thermal zone device
  601. * @trip: indicates which trip point the cooling devices is
  602. * associated with in this thermal zone.
  603. * @cdev: thermal cooling device
  604. *
  605. * This function is usually called in the thermal zone device .bind callback.
  606. */
  607. int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
  608. int trip,
  609. struct thermal_cooling_device *cdev,
  610. unsigned long upper, unsigned long lower)
  611. {
  612. struct thermal_instance *dev;
  613. struct thermal_instance *pos;
  614. struct thermal_zone_device *pos1;
  615. struct thermal_cooling_device *pos2;
  616. unsigned long max_state;
  617. int result;
  618. if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
  619. return -EINVAL;
  620. list_for_each_entry(pos1, &thermal_tz_list, node) {
  621. if (pos1 == tz)
  622. break;
  623. }
  624. list_for_each_entry(pos2, &thermal_cdev_list, node) {
  625. if (pos2 == cdev)
  626. break;
  627. }
  628. if (tz != pos1 || cdev != pos2)
  629. return -EINVAL;
  630. cdev->ops->get_max_state(cdev, &max_state);
  631. /* lower default 0, upper default max_state */
  632. lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
  633. upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
  634. if (lower > upper || upper > max_state)
  635. return -EINVAL;
  636. dev =
  637. kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
  638. if (!dev)
  639. return -ENOMEM;
  640. dev->tz = tz;
  641. dev->cdev = cdev;
  642. dev->trip = trip;
  643. dev->upper = upper;
  644. dev->lower = lower;
  645. dev->target = THERMAL_NO_TARGET;
  646. result = get_idr(&tz->idr, &tz->lock, &dev->id);
  647. if (result)
  648. goto free_mem;
  649. sprintf(dev->name, "cdev%d", dev->id);
  650. result =
  651. sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
  652. if (result)
  653. goto release_idr;
  654. sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
  655. sysfs_attr_init(&dev->attr.attr);
  656. dev->attr.attr.name = dev->attr_name;
  657. dev->attr.attr.mode = 0444;
  658. dev->attr.show = thermal_cooling_device_trip_point_show;
  659. result = device_create_file(&tz->device, &dev->attr);
  660. if (result)
  661. goto remove_symbol_link;
  662. mutex_lock(&tz->lock);
  663. mutex_lock(&cdev->lock);
  664. list_for_each_entry(pos, &tz->thermal_instances, tz_node)
  665. if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
  666. result = -EEXIST;
  667. break;
  668. }
  669. if (!result) {
  670. list_add_tail(&dev->tz_node, &tz->thermal_instances);
  671. list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
  672. }
  673. mutex_unlock(&cdev->lock);
  674. mutex_unlock(&tz->lock);
  675. if (!result)
  676. return 0;
  677. device_remove_file(&tz->device, &dev->attr);
  678. remove_symbol_link:
  679. sysfs_remove_link(&tz->device.kobj, dev->name);
  680. release_idr:
  681. release_idr(&tz->idr, &tz->lock, dev->id);
  682. free_mem:
  683. kfree(dev);
  684. return result;
  685. }
  686. EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
  687. /**
  688. * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
  689. * @tz: thermal zone device
  690. * @trip: indicates which trip point the cooling devices is
  691. * associated with in this thermal zone.
  692. * @cdev: thermal cooling device
  693. *
  694. * This function is usually called in the thermal zone device .unbind callback.
  695. */
  696. int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
  697. int trip,
  698. struct thermal_cooling_device *cdev)
  699. {
  700. struct thermal_instance *pos, *next;
  701. mutex_lock(&tz->lock);
  702. mutex_lock(&cdev->lock);
  703. list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
  704. if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
  705. list_del(&pos->tz_node);
  706. list_del(&pos->cdev_node);
  707. mutex_unlock(&cdev->lock);
  708. mutex_unlock(&tz->lock);
  709. goto unbind;
  710. }
  711. }
  712. mutex_unlock(&cdev->lock);
  713. mutex_unlock(&tz->lock);
  714. return -ENODEV;
  715. unbind:
  716. device_remove_file(&tz->device, &pos->attr);
  717. sysfs_remove_link(&tz->device.kobj, pos->name);
  718. release_idr(&tz->idr, &tz->lock, pos->id);
  719. kfree(pos);
  720. return 0;
  721. }
  722. EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
  723. static void thermal_release(struct device *dev)
  724. {
  725. struct thermal_zone_device *tz;
  726. struct thermal_cooling_device *cdev;
  727. if (!strncmp(dev_name(dev), "thermal_zone",
  728. sizeof("thermal_zone") - 1)) {
  729. tz = to_thermal_zone(dev);
  730. kfree(tz);
  731. } else {
  732. cdev = to_cooling_device(dev);
  733. kfree(cdev);
  734. }
  735. }
  736. static struct class thermal_class = {
  737. .name = "thermal",
  738. .dev_release = thermal_release,
  739. };
  740. /**
  741. * thermal_cooling_device_register - register a new thermal cooling device
  742. * @type: the thermal cooling device type.
  743. * @devdata: device private data.
  744. * @ops: standard thermal cooling devices callbacks.
  745. */
  746. struct thermal_cooling_device *
  747. thermal_cooling_device_register(char *type, void *devdata,
  748. const struct thermal_cooling_device_ops *ops)
  749. {
  750. struct thermal_cooling_device *cdev;
  751. struct thermal_zone_device *pos;
  752. int result;
  753. if (type && strlen(type) >= THERMAL_NAME_LENGTH)
  754. return ERR_PTR(-EINVAL);
  755. if (!ops || !ops->get_max_state || !ops->get_cur_state ||
  756. !ops->set_cur_state)
  757. return ERR_PTR(-EINVAL);
  758. cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
  759. if (!cdev)
  760. return ERR_PTR(-ENOMEM);
  761. result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
  762. if (result) {
  763. kfree(cdev);
  764. return ERR_PTR(result);
  765. }
  766. strcpy(cdev->type, type ? : "");
  767. mutex_init(&cdev->lock);
  768. INIT_LIST_HEAD(&cdev->thermal_instances);
  769. cdev->ops = ops;
  770. cdev->updated = true;
  771. cdev->device.class = &thermal_class;
  772. cdev->devdata = devdata;
  773. dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
  774. result = device_register(&cdev->device);
  775. if (result) {
  776. release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
  777. kfree(cdev);
  778. return ERR_PTR(result);
  779. }
  780. /* sys I/F */
  781. if (type) {
  782. result = device_create_file(&cdev->device, &dev_attr_cdev_type);
  783. if (result)
  784. goto unregister;
  785. }
  786. result = device_create_file(&cdev->device, &dev_attr_max_state);
  787. if (result)
  788. goto unregister;
  789. result = device_create_file(&cdev->device, &dev_attr_cur_state);
  790. if (result)
  791. goto unregister;
  792. mutex_lock(&thermal_list_lock);
  793. list_add(&cdev->node, &thermal_cdev_list);
  794. list_for_each_entry(pos, &thermal_tz_list, node) {
  795. if (!pos->ops->bind)
  796. continue;
  797. result = pos->ops->bind(pos, cdev);
  798. if (result)
  799. break;
  800. }
  801. mutex_unlock(&thermal_list_lock);
  802. if (!result)
  803. return cdev;
  804. unregister:
  805. release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
  806. device_unregister(&cdev->device);
  807. return ERR_PTR(result);
  808. }
  809. EXPORT_SYMBOL(thermal_cooling_device_register);
  810. /**
  811. * thermal_cooling_device_unregister - removes the registered thermal cooling device
  812. * @cdev: the thermal cooling device to remove.
  813. *
  814. * thermal_cooling_device_unregister() must be called when the device is no
  815. * longer needed.
  816. */
  817. void thermal_cooling_device_unregister(struct
  818. thermal_cooling_device
  819. *cdev)
  820. {
  821. struct thermal_zone_device *tz;
  822. struct thermal_cooling_device *pos = NULL;
  823. if (!cdev)
  824. return;
  825. mutex_lock(&thermal_list_lock);
  826. list_for_each_entry(pos, &thermal_cdev_list, node)
  827. if (pos == cdev)
  828. break;
  829. if (pos != cdev) {
  830. /* thermal cooling device not found */
  831. mutex_unlock(&thermal_list_lock);
  832. return;
  833. }
  834. list_del(&cdev->node);
  835. list_for_each_entry(tz, &thermal_tz_list, node) {
  836. if (!tz->ops->unbind)
  837. continue;
  838. tz->ops->unbind(tz, cdev);
  839. }
  840. mutex_unlock(&thermal_list_lock);
  841. if (cdev->type[0])
  842. device_remove_file(&cdev->device, &dev_attr_cdev_type);
  843. device_remove_file(&cdev->device, &dev_attr_max_state);
  844. device_remove_file(&cdev->device, &dev_attr_cur_state);
  845. release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
  846. device_unregister(&cdev->device);
  847. return;
  848. }
  849. EXPORT_SYMBOL(thermal_cooling_device_unregister);
  850. static void thermal_cdev_do_update(struct thermal_cooling_device *cdev)
  851. {
  852. struct thermal_instance *instance;
  853. unsigned long target = 0;
  854. /* cooling device is updated*/
  855. if (cdev->updated)
  856. return;
  857. mutex_lock(&cdev->lock);
  858. /* Make sure cdev enters the deepest cooling state */
  859. list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
  860. if (instance->target == THERMAL_NO_TARGET)
  861. continue;
  862. if (instance->target > target)
  863. target = instance->target;
  864. }
  865. mutex_unlock(&cdev->lock);
  866. cdev->ops->set_cur_state(cdev, target);
  867. cdev->updated = true;
  868. }
  869. static void thermal_zone_do_update(struct thermal_zone_device *tz)
  870. {
  871. struct thermal_instance *instance;
  872. list_for_each_entry(instance, &tz->thermal_instances, tz_node)
  873. thermal_cdev_do_update(instance->cdev);
  874. }
  875. /*
  876. * Cooling algorithm for both active and passive cooling
  877. *
  878. * 1. if the temperature is higher than a trip point,
  879. * a. if the trend is THERMAL_TREND_RAISING, use higher cooling
  880. * state for this trip point
  881. * b. if the trend is THERMAL_TREND_DROPPING, use lower cooling
  882. * state for this trip point
  883. *
  884. * 2. if the temperature is lower than a trip point, use lower
  885. * cooling state for this trip point
  886. *
  887. * Note that this behaves the same as the previous passive cooling
  888. * algorithm.
  889. */
  890. static void thermal_zone_trip_update(struct thermal_zone_device *tz,
  891. int trip, long temp)
  892. {
  893. struct thermal_instance *instance;
  894. struct thermal_cooling_device *cdev = NULL;
  895. unsigned long cur_state, max_state;
  896. long trip_temp;
  897. enum thermal_trip_type trip_type;
  898. enum thermal_trend trend;
  899. if (trip == THERMAL_TRIPS_NONE) {
  900. trip_temp = tz->forced_passive;
  901. trip_type = THERMAL_TRIPS_NONE;
  902. } else {
  903. tz->ops->get_trip_temp(tz, trip, &trip_temp);
  904. tz->ops->get_trip_type(tz, trip, &trip_type);
  905. }
  906. if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) {
  907. /*
  908. * compare the current temperature and previous temperature
  909. * to get the thermal trend, if no special requirement
  910. */
  911. if (tz->temperature > tz->last_temperature)
  912. trend = THERMAL_TREND_RAISING;
  913. else if (tz->temperature < tz->last_temperature)
  914. trend = THERMAL_TREND_DROPPING;
  915. else
  916. trend = THERMAL_TREND_STABLE;
  917. }
  918. if (temp >= trip_temp) {
  919. list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
  920. if (instance->trip != trip)
  921. continue;
  922. cdev = instance->cdev;
  923. cdev->ops->get_cur_state(cdev, &cur_state);
  924. cdev->ops->get_max_state(cdev, &max_state);
  925. if (trend == THERMAL_TREND_RAISING) {
  926. cur_state = cur_state < instance->upper ?
  927. (cur_state + 1) : instance->upper;
  928. } else if (trend == THERMAL_TREND_DROPPING) {
  929. cur_state = cur_state > instance->lower ?
  930. (cur_state - 1) : instance->lower;
  931. }
  932. /* activate a passive thermal instance */
  933. if ((trip_type == THERMAL_TRIP_PASSIVE ||
  934. trip_type == THERMAL_TRIPS_NONE) &&
  935. instance->target == THERMAL_NO_TARGET)
  936. tz->passive++;
  937. instance->target = cur_state;
  938. cdev->updated = false; /* cooling device needs update */
  939. }
  940. } else { /* below trip */
  941. list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
  942. if (instance->trip != trip)
  943. continue;
  944. /* Do not use the inactive thermal instance */
  945. if (instance->target == THERMAL_NO_TARGET)
  946. continue;
  947. cdev = instance->cdev;
  948. cdev->ops->get_cur_state(cdev, &cur_state);
  949. cur_state = cur_state > instance->lower ?
  950. (cur_state - 1) : THERMAL_NO_TARGET;
  951. /* deactivate a passive thermal instance */
  952. if ((trip_type == THERMAL_TRIP_PASSIVE ||
  953. trip_type == THERMAL_TRIPS_NONE) &&
  954. cur_state == THERMAL_NO_TARGET)
  955. tz->passive--;
  956. instance->target = cur_state;
  957. cdev->updated = false; /* cooling device needs update */
  958. }
  959. }
  960. return;
  961. }
  962. /**
  963. * thermal_zone_device_update - force an update of a thermal zone's state
  964. * @ttz: the thermal zone to update
  965. */
  966. void thermal_zone_device_update(struct thermal_zone_device *tz)
  967. {
  968. int count, ret = 0;
  969. long temp, trip_temp;
  970. enum thermal_trip_type trip_type;
  971. mutex_lock(&tz->lock);
  972. if (tz->ops->get_temp(tz, &temp)) {
  973. /* get_temp failed - retry it later */
  974. pr_warn("failed to read out thermal zone %d\n", tz->id);
  975. goto leave;
  976. }
  977. tz->last_temperature = tz->temperature;
  978. tz->temperature = temp;
  979. for (count = 0; count < tz->trips; count++) {
  980. tz->ops->get_trip_type(tz, count, &trip_type);
  981. tz->ops->get_trip_temp(tz, count, &trip_temp);
  982. switch (trip_type) {
  983. case THERMAL_TRIP_CRITICAL:
  984. if (temp >= trip_temp) {
  985. if (tz->ops->notify)
  986. ret = tz->ops->notify(tz, count,
  987. trip_type);
  988. if (!ret) {
  989. pr_emerg("Critical temperature reached (%ld C), shutting down\n",
  990. temp/1000);
  991. orderly_poweroff(true);
  992. }
  993. }
  994. break;
  995. case THERMAL_TRIP_HOT:
  996. if (temp >= trip_temp)
  997. if (tz->ops->notify)
  998. tz->ops->notify(tz, count, trip_type);
  999. break;
  1000. case THERMAL_TRIP_ACTIVE:
  1001. thermal_zone_trip_update(tz, count, temp);
  1002. break;
  1003. case THERMAL_TRIP_PASSIVE:
  1004. if (temp >= trip_temp || tz->passive)
  1005. thermal_zone_trip_update(tz, count, temp);
  1006. break;
  1007. }
  1008. }
  1009. if (tz->forced_passive)
  1010. thermal_zone_trip_update(tz, THERMAL_TRIPS_NONE, temp);
  1011. thermal_zone_do_update(tz);
  1012. leave:
  1013. if (tz->passive)
  1014. thermal_zone_device_set_polling(tz, tz->passive_delay);
  1015. else if (tz->polling_delay)
  1016. thermal_zone_device_set_polling(tz, tz->polling_delay);
  1017. else
  1018. thermal_zone_device_set_polling(tz, 0);
  1019. mutex_unlock(&tz->lock);
  1020. }
  1021. EXPORT_SYMBOL(thermal_zone_device_update);
  1022. /**
  1023. * create_trip_attrs - create attributes for trip points
  1024. * @tz: the thermal zone device
  1025. * @mask: Writeable trip point bitmap.
  1026. */
  1027. static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
  1028. {
  1029. int indx;
  1030. int size = sizeof(struct thermal_attr) * tz->trips;
  1031. tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
  1032. if (!tz->trip_type_attrs)
  1033. return -ENOMEM;
  1034. tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
  1035. if (!tz->trip_temp_attrs) {
  1036. kfree(tz->trip_type_attrs);
  1037. return -ENOMEM;
  1038. }
  1039. if (tz->ops->get_trip_hyst) {
  1040. tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
  1041. if (!tz->trip_hyst_attrs) {
  1042. kfree(tz->trip_type_attrs);
  1043. kfree(tz->trip_temp_attrs);
  1044. return -ENOMEM;
  1045. }
  1046. }
  1047. for (indx = 0; indx < tz->trips; indx++) {
  1048. /* create trip type attribute */
  1049. snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
  1050. "trip_point_%d_type", indx);
  1051. sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
  1052. tz->trip_type_attrs[indx].attr.attr.name =
  1053. tz->trip_type_attrs[indx].name;
  1054. tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
  1055. tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
  1056. device_create_file(&tz->device,
  1057. &tz->trip_type_attrs[indx].attr);
  1058. /* create trip temp attribute */
  1059. snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
  1060. "trip_point_%d_temp", indx);
  1061. sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
  1062. tz->trip_temp_attrs[indx].attr.attr.name =
  1063. tz->trip_temp_attrs[indx].name;
  1064. tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
  1065. tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
  1066. if (mask & (1 << indx)) {
  1067. tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
  1068. tz->trip_temp_attrs[indx].attr.store =
  1069. trip_point_temp_store;
  1070. }
  1071. device_create_file(&tz->device,
  1072. &tz->trip_temp_attrs[indx].attr);
  1073. /* create Optional trip hyst attribute */
  1074. if (!tz->ops->get_trip_hyst)
  1075. continue;
  1076. snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
  1077. "trip_point_%d_hyst", indx);
  1078. sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
  1079. tz->trip_hyst_attrs[indx].attr.attr.name =
  1080. tz->trip_hyst_attrs[indx].name;
  1081. tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
  1082. tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
  1083. if (tz->ops->set_trip_hyst) {
  1084. tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
  1085. tz->trip_hyst_attrs[indx].attr.store =
  1086. trip_point_hyst_store;
  1087. }
  1088. device_create_file(&tz->device,
  1089. &tz->trip_hyst_attrs[indx].attr);
  1090. }
  1091. return 0;
  1092. }
  1093. static void remove_trip_attrs(struct thermal_zone_device *tz)
  1094. {
  1095. int indx;
  1096. for (indx = 0; indx < tz->trips; indx++) {
  1097. device_remove_file(&tz->device,
  1098. &tz->trip_type_attrs[indx].attr);
  1099. device_remove_file(&tz->device,
  1100. &tz->trip_temp_attrs[indx].attr);
  1101. if (tz->ops->get_trip_hyst)
  1102. device_remove_file(&tz->device,
  1103. &tz->trip_hyst_attrs[indx].attr);
  1104. }
  1105. kfree(tz->trip_type_attrs);
  1106. kfree(tz->trip_temp_attrs);
  1107. kfree(tz->trip_hyst_attrs);
  1108. }
  1109. /**
  1110. * thermal_zone_device_register - register a new thermal zone device
  1111. * @type: the thermal zone device type
  1112. * @trips: the number of trip points the thermal zone support
  1113. * @mask: a bit string indicating the writeablility of trip points
  1114. * @devdata: private device data
  1115. * @ops: standard thermal zone device callbacks
  1116. * @passive_delay: number of milliseconds to wait between polls when
  1117. * performing passive cooling
  1118. * @polling_delay: number of milliseconds to wait between polls when checking
  1119. * whether trip points have been crossed (0 for interrupt
  1120. * driven systems)
  1121. *
  1122. * thermal_zone_device_unregister() must be called when the device is no
  1123. * longer needed. The passive cooling depends on the .get_trend() return value.
  1124. */
  1125. struct thermal_zone_device *thermal_zone_device_register(const char *type,
  1126. int trips, int mask, void *devdata,
  1127. const struct thermal_zone_device_ops *ops,
  1128. int passive_delay, int polling_delay)
  1129. {
  1130. struct thermal_zone_device *tz;
  1131. struct thermal_cooling_device *pos;
  1132. enum thermal_trip_type trip_type;
  1133. int result;
  1134. int count;
  1135. int passive = 0;
  1136. if (type && strlen(type) >= THERMAL_NAME_LENGTH)
  1137. return ERR_PTR(-EINVAL);
  1138. if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
  1139. return ERR_PTR(-EINVAL);
  1140. if (!ops || !ops->get_temp)
  1141. return ERR_PTR(-EINVAL);
  1142. tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
  1143. if (!tz)
  1144. return ERR_PTR(-ENOMEM);
  1145. INIT_LIST_HEAD(&tz->thermal_instances);
  1146. idr_init(&tz->idr);
  1147. mutex_init(&tz->lock);
  1148. result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
  1149. if (result) {
  1150. kfree(tz);
  1151. return ERR_PTR(result);
  1152. }
  1153. strcpy(tz->type, type ? : "");
  1154. tz->ops = ops;
  1155. tz->device.class = &thermal_class;
  1156. tz->devdata = devdata;
  1157. tz->trips = trips;
  1158. tz->passive_delay = passive_delay;
  1159. tz->polling_delay = polling_delay;
  1160. dev_set_name(&tz->device, "thermal_zone%d", tz->id);
  1161. result = device_register(&tz->device);
  1162. if (result) {
  1163. release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
  1164. kfree(tz);
  1165. return ERR_PTR(result);
  1166. }
  1167. /* sys I/F */
  1168. if (type) {
  1169. result = device_create_file(&tz->device, &dev_attr_type);
  1170. if (result)
  1171. goto unregister;
  1172. }
  1173. result = device_create_file(&tz->device, &dev_attr_temp);
  1174. if (result)
  1175. goto unregister;
  1176. if (ops->get_mode) {
  1177. result = device_create_file(&tz->device, &dev_attr_mode);
  1178. if (result)
  1179. goto unregister;
  1180. }
  1181. result = create_trip_attrs(tz, mask);
  1182. if (result)
  1183. goto unregister;
  1184. for (count = 0; count < trips; count++) {
  1185. tz->ops->get_trip_type(tz, count, &trip_type);
  1186. if (trip_type == THERMAL_TRIP_PASSIVE)
  1187. passive = 1;
  1188. }
  1189. if (!passive)
  1190. result = device_create_file(&tz->device,
  1191. &dev_attr_passive);
  1192. if (result)
  1193. goto unregister;
  1194. result = thermal_add_hwmon_sysfs(tz);
  1195. if (result)
  1196. goto unregister;
  1197. mutex_lock(&thermal_list_lock);
  1198. list_add_tail(&tz->node, &thermal_tz_list);
  1199. if (ops->bind)
  1200. list_for_each_entry(pos, &thermal_cdev_list, node) {
  1201. result = ops->bind(tz, pos);
  1202. if (result)
  1203. break;
  1204. }
  1205. mutex_unlock(&thermal_list_lock);
  1206. INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
  1207. thermal_zone_device_update(tz);
  1208. if (!result)
  1209. return tz;
  1210. unregister:
  1211. release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
  1212. device_unregister(&tz->device);
  1213. return ERR_PTR(result);
  1214. }
  1215. EXPORT_SYMBOL(thermal_zone_device_register);
  1216. /**
  1217. * thermal_device_unregister - removes the registered thermal zone device
  1218. * @tz: the thermal zone device to remove
  1219. */
  1220. void thermal_zone_device_unregister(struct thermal_zone_device *tz)
  1221. {
  1222. struct thermal_cooling_device *cdev;
  1223. struct thermal_zone_device *pos = NULL;
  1224. if (!tz)
  1225. return;
  1226. mutex_lock(&thermal_list_lock);
  1227. list_for_each_entry(pos, &thermal_tz_list, node)
  1228. if (pos == tz)
  1229. break;
  1230. if (pos != tz) {
  1231. /* thermal zone device not found */
  1232. mutex_unlock(&thermal_list_lock);
  1233. return;
  1234. }
  1235. list_del(&tz->node);
  1236. if (tz->ops->unbind)
  1237. list_for_each_entry(cdev, &thermal_cdev_list, node)
  1238. tz->ops->unbind(tz, cdev);
  1239. mutex_unlock(&thermal_list_lock);
  1240. thermal_zone_device_set_polling(tz, 0);
  1241. if (tz->type[0])
  1242. device_remove_file(&tz->device, &dev_attr_type);
  1243. device_remove_file(&tz->device, &dev_attr_temp);
  1244. if (tz->ops->get_mode)
  1245. device_remove_file(&tz->device, &dev_attr_mode);
  1246. remove_trip_attrs(tz);
  1247. thermal_remove_hwmon_sysfs(tz);
  1248. release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
  1249. idr_destroy(&tz->idr);
  1250. mutex_destroy(&tz->lock);
  1251. device_unregister(&tz->device);
  1252. return;
  1253. }
  1254. EXPORT_SYMBOL(thermal_zone_device_unregister);
  1255. #ifdef CONFIG_NET
  1256. static struct genl_family thermal_event_genl_family = {
  1257. .id = GENL_ID_GENERATE,
  1258. .name = THERMAL_GENL_FAMILY_NAME,
  1259. .version = THERMAL_GENL_VERSION,
  1260. .maxattr = THERMAL_GENL_ATTR_MAX,
  1261. };
  1262. static struct genl_multicast_group thermal_event_mcgrp = {
  1263. .name = THERMAL_GENL_MCAST_GROUP_NAME,
  1264. };
  1265. int thermal_generate_netlink_event(u32 orig, enum events event)
  1266. {
  1267. struct sk_buff *skb;
  1268. struct nlattr *attr;
  1269. struct thermal_genl_event *thermal_event;
  1270. void *msg_header;
  1271. int size;
  1272. int result;
  1273. static unsigned int thermal_event_seqnum;
  1274. /* allocate memory */
  1275. size = nla_total_size(sizeof(struct thermal_genl_event)) +
  1276. nla_total_size(0);
  1277. skb = genlmsg_new(size, GFP_ATOMIC);
  1278. if (!skb)
  1279. return -ENOMEM;
  1280. /* add the genetlink message header */
  1281. msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
  1282. &thermal_event_genl_family, 0,
  1283. THERMAL_GENL_CMD_EVENT);
  1284. if (!msg_header) {
  1285. nlmsg_free(skb);
  1286. return -ENOMEM;
  1287. }
  1288. /* fill the data */
  1289. attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
  1290. sizeof(struct thermal_genl_event));
  1291. if (!attr) {
  1292. nlmsg_free(skb);
  1293. return -EINVAL;
  1294. }
  1295. thermal_event = nla_data(attr);
  1296. if (!thermal_event) {
  1297. nlmsg_free(skb);
  1298. return -EINVAL;
  1299. }
  1300. memset(thermal_event, 0, sizeof(struct thermal_genl_event));
  1301. thermal_event->orig = orig;
  1302. thermal_event->event = event;
  1303. /* send multicast genetlink message */
  1304. result = genlmsg_end(skb, msg_header);
  1305. if (result < 0) {
  1306. nlmsg_free(skb);
  1307. return result;
  1308. }
  1309. result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
  1310. if (result)
  1311. pr_info("failed to send netlink event:%d\n", result);
  1312. return result;
  1313. }
  1314. EXPORT_SYMBOL(thermal_generate_netlink_event);
  1315. static int genetlink_init(void)
  1316. {
  1317. int result;
  1318. result = genl_register_family(&thermal_event_genl_family);
  1319. if (result)
  1320. return result;
  1321. result = genl_register_mc_group(&thermal_event_genl_family,
  1322. &thermal_event_mcgrp);
  1323. if (result)
  1324. genl_unregister_family(&thermal_event_genl_family);
  1325. return result;
  1326. }
  1327. static void genetlink_exit(void)
  1328. {
  1329. genl_unregister_family(&thermal_event_genl_family);
  1330. }
  1331. #else /* !CONFIG_NET */
  1332. static inline int genetlink_init(void) { return 0; }
  1333. static inline void genetlink_exit(void) {}
  1334. #endif /* !CONFIG_NET */
  1335. static int __init thermal_init(void)
  1336. {
  1337. int result = 0;
  1338. result = class_register(&thermal_class);
  1339. if (result) {
  1340. idr_destroy(&thermal_tz_idr);
  1341. idr_destroy(&thermal_cdev_idr);
  1342. mutex_destroy(&thermal_idr_lock);
  1343. mutex_destroy(&thermal_list_lock);
  1344. }
  1345. result = genetlink_init();
  1346. return result;
  1347. }
  1348. static void __exit thermal_exit(void)
  1349. {
  1350. class_unregister(&thermal_class);
  1351. idr_destroy(&thermal_tz_idr);
  1352. idr_destroy(&thermal_cdev_idr);
  1353. mutex_destroy(&thermal_idr_lock);
  1354. mutex_destroy(&thermal_list_lock);
  1355. genetlink_exit();
  1356. }
  1357. fs_initcall(thermal_init);
  1358. module_exit(thermal_exit);