thermal_sys.c 41 KB

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