thermal_sys.c 38 KB

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