thermal_sys.c 40 KB

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