thermal.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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. #include <linux/module.h>
  26. #include <linux/device.h>
  27. #include <linux/err.h>
  28. #include <linux/kdev_t.h>
  29. #include <linux/idr.h>
  30. #include <linux/thermal.h>
  31. #include <linux/spinlock.h>
  32. MODULE_AUTHOR("Zhang Rui")
  33. MODULE_DESCRIPTION("Generic thermal management sysfs support");
  34. MODULE_LICENSE("GPL");
  35. #define PREFIX "Thermal: "
  36. struct thermal_cooling_device_instance {
  37. int id;
  38. char name[THERMAL_NAME_LENGTH];
  39. struct thermal_zone_device *tz;
  40. struct thermal_cooling_device *cdev;
  41. int trip;
  42. char attr_name[THERMAL_NAME_LENGTH];
  43. struct device_attribute attr;
  44. struct list_head node;
  45. };
  46. static DEFINE_IDR(thermal_tz_idr);
  47. static DEFINE_IDR(thermal_cdev_idr);
  48. static DEFINE_MUTEX(thermal_idr_lock);
  49. static LIST_HEAD(thermal_tz_list);
  50. static LIST_HEAD(thermal_cdev_list);
  51. static DEFINE_MUTEX(thermal_list_lock);
  52. static int get_idr(struct idr *idr, struct mutex *lock, int *id)
  53. {
  54. int err;
  55. again:
  56. if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
  57. return -ENOMEM;
  58. if (lock)
  59. mutex_lock(lock);
  60. err = idr_get_new(idr, NULL, id);
  61. if (lock)
  62. mutex_unlock(lock);
  63. if (unlikely(err == -EAGAIN))
  64. goto again;
  65. else if (unlikely(err))
  66. return err;
  67. *id = *id & MAX_ID_MASK;
  68. return 0;
  69. }
  70. static void release_idr(struct idr *idr, struct mutex *lock, int id)
  71. {
  72. if (lock)
  73. mutex_lock(lock);
  74. idr_remove(idr, id);
  75. if (lock)
  76. mutex_unlock(lock);
  77. }
  78. /* sys I/F for thermal zone */
  79. #define to_thermal_zone(_dev) \
  80. container_of(_dev, struct thermal_zone_device, device)
  81. static ssize_t
  82. type_show(struct device *dev, struct device_attribute *attr, char *buf)
  83. {
  84. struct thermal_zone_device *tz = to_thermal_zone(dev);
  85. return sprintf(buf, "%s\n", tz->type);
  86. }
  87. static ssize_t
  88. temp_show(struct device *dev, struct device_attribute *attr, char *buf)
  89. {
  90. struct thermal_zone_device *tz = to_thermal_zone(dev);
  91. if (!tz->ops->get_temp)
  92. return -EPERM;
  93. return tz->ops->get_temp(tz, buf);
  94. }
  95. static ssize_t
  96. mode_show(struct device *dev, struct device_attribute *attr, char *buf)
  97. {
  98. struct thermal_zone_device *tz = to_thermal_zone(dev);
  99. if (!tz->ops->get_mode)
  100. return -EPERM;
  101. return tz->ops->get_mode(tz, buf);
  102. }
  103. static ssize_t
  104. mode_store(struct device *dev, struct device_attribute *attr,
  105. const char *buf, size_t count)
  106. {
  107. struct thermal_zone_device *tz = to_thermal_zone(dev);
  108. int result;
  109. if (!tz->ops->set_mode)
  110. return -EPERM;
  111. result = tz->ops->set_mode(tz, buf);
  112. if (result)
  113. return result;
  114. return count;
  115. }
  116. static ssize_t
  117. trip_point_type_show(struct device *dev, struct device_attribute *attr,
  118. char *buf)
  119. {
  120. struct thermal_zone_device *tz = to_thermal_zone(dev);
  121. int trip;
  122. if (!tz->ops->get_trip_type)
  123. return -EPERM;
  124. if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
  125. return -EINVAL;
  126. return tz->ops->get_trip_type(tz, trip, buf);
  127. }
  128. static ssize_t
  129. trip_point_temp_show(struct device *dev, struct device_attribute *attr,
  130. char *buf)
  131. {
  132. struct thermal_zone_device *tz = to_thermal_zone(dev);
  133. int trip;
  134. if (!tz->ops->get_trip_temp)
  135. return -EPERM;
  136. if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
  137. return -EINVAL;
  138. return tz->ops->get_trip_temp(tz, trip, buf);
  139. }
  140. static DEVICE_ATTR(type, 0444, type_show, NULL);
  141. static DEVICE_ATTR(temp, 0444, temp_show, NULL);
  142. static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
  143. static struct device_attribute trip_point_attrs[] = {
  144. __ATTR(trip_point_0_type, 0444, trip_point_type_show, NULL),
  145. __ATTR(trip_point_0_temp, 0444, trip_point_temp_show, NULL),
  146. __ATTR(trip_point_1_type, 0444, trip_point_type_show, NULL),
  147. __ATTR(trip_point_1_temp, 0444, trip_point_temp_show, NULL),
  148. __ATTR(trip_point_2_type, 0444, trip_point_type_show, NULL),
  149. __ATTR(trip_point_2_temp, 0444, trip_point_temp_show, NULL),
  150. __ATTR(trip_point_3_type, 0444, trip_point_type_show, NULL),
  151. __ATTR(trip_point_3_temp, 0444, trip_point_temp_show, NULL),
  152. __ATTR(trip_point_4_type, 0444, trip_point_type_show, NULL),
  153. __ATTR(trip_point_4_temp, 0444, trip_point_temp_show, NULL),
  154. __ATTR(trip_point_5_type, 0444, trip_point_type_show, NULL),
  155. __ATTR(trip_point_5_temp, 0444, trip_point_temp_show, NULL),
  156. __ATTR(trip_point_6_type, 0444, trip_point_type_show, NULL),
  157. __ATTR(trip_point_6_temp, 0444, trip_point_temp_show, NULL),
  158. __ATTR(trip_point_7_type, 0444, trip_point_type_show, NULL),
  159. __ATTR(trip_point_7_temp, 0444, trip_point_temp_show, NULL),
  160. __ATTR(trip_point_8_type, 0444, trip_point_type_show, NULL),
  161. __ATTR(trip_point_8_temp, 0444, trip_point_temp_show, NULL),
  162. __ATTR(trip_point_9_type, 0444, trip_point_type_show, NULL),
  163. __ATTR(trip_point_9_temp, 0444, trip_point_temp_show, NULL),
  164. };
  165. #define TRIP_POINT_ATTR_ADD(_dev, _index, result) \
  166. do { \
  167. result = device_create_file(_dev, \
  168. &trip_point_attrs[_index * 2]); \
  169. if (result) \
  170. break; \
  171. result = device_create_file(_dev, \
  172. &trip_point_attrs[_index * 2 + 1]); \
  173. } while (0)
  174. #define TRIP_POINT_ATTR_REMOVE(_dev, _index) \
  175. do { \
  176. device_remove_file(_dev, &trip_point_attrs[_index * 2]); \
  177. device_remove_file(_dev, &trip_point_attrs[_index * 2 + 1]); \
  178. } while (0)
  179. /* sys I/F for cooling device */
  180. #define to_cooling_device(_dev) \
  181. container_of(_dev, struct thermal_cooling_device, device)
  182. static ssize_t
  183. thermal_cooling_device_type_show(struct device *dev,
  184. struct device_attribute *attr, char *buf)
  185. {
  186. struct thermal_cooling_device *cdev = to_cooling_device(dev);
  187. return sprintf(buf, "%s\n", cdev->type);
  188. }
  189. static ssize_t
  190. thermal_cooling_device_max_state_show(struct device *dev,
  191. struct device_attribute *attr, char *buf)
  192. {
  193. struct thermal_cooling_device *cdev = to_cooling_device(dev);
  194. return cdev->ops->get_max_state(cdev, buf);
  195. }
  196. static ssize_t
  197. thermal_cooling_device_cur_state_show(struct device *dev,
  198. struct device_attribute *attr, char *buf)
  199. {
  200. struct thermal_cooling_device *cdev = to_cooling_device(dev);
  201. return cdev->ops->get_cur_state(cdev, buf);
  202. }
  203. static ssize_t
  204. thermal_cooling_device_cur_state_store(struct device *dev,
  205. struct device_attribute *attr,
  206. const char *buf, size_t count)
  207. {
  208. struct thermal_cooling_device *cdev = to_cooling_device(dev);
  209. int state;
  210. int result;
  211. if (!sscanf(buf, "%d\n", &state))
  212. return -EINVAL;
  213. if (state < 0)
  214. return -EINVAL;
  215. result = cdev->ops->set_cur_state(cdev, state);
  216. if (result)
  217. return result;
  218. return count;
  219. }
  220. static struct device_attribute dev_attr_cdev_type =
  221. __ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
  222. static DEVICE_ATTR(max_state, 0444,
  223. thermal_cooling_device_max_state_show, NULL);
  224. static DEVICE_ATTR(cur_state, 0644,
  225. thermal_cooling_device_cur_state_show,
  226. thermal_cooling_device_cur_state_store);
  227. static ssize_t
  228. thermal_cooling_device_trip_point_show(struct device *dev,
  229. struct device_attribute *attr, char *buf)
  230. {
  231. struct thermal_cooling_device_instance *instance;
  232. instance =
  233. container_of(attr, struct thermal_cooling_device_instance, attr);
  234. if (instance->trip == THERMAL_TRIPS_NONE)
  235. return sprintf(buf, "-1\n");
  236. else
  237. return sprintf(buf, "%d\n", instance->trip);
  238. }
  239. /* Device management */
  240. /**
  241. * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
  242. * this function is usually called in the thermal zone device .bind callback.
  243. * @tz: thermal zone device
  244. * @trip: indicates which trip point the cooling devices is
  245. * associated with in this thermal zone.
  246. * @cdev: thermal cooling device
  247. */
  248. int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
  249. int trip,
  250. struct thermal_cooling_device *cdev)
  251. {
  252. struct thermal_cooling_device_instance *dev;
  253. struct thermal_cooling_device_instance *pos;
  254. int result;
  255. if (trip >= tz->trips ||
  256. (trip < 0 && trip != THERMAL_TRIPS_NONE))
  257. return -EINVAL;
  258. if (!tz || !cdev)
  259. return -EINVAL;
  260. dev =
  261. kzalloc(sizeof(struct thermal_cooling_device_instance), GFP_KERNEL);
  262. if (!dev)
  263. return -ENOMEM;
  264. dev->tz = tz;
  265. dev->cdev = cdev;
  266. dev->trip = trip;
  267. result = get_idr(&tz->idr, &tz->lock, &dev->id);
  268. if (result)
  269. goto free_mem;
  270. sprintf(dev->name, "cdev%d", dev->id);
  271. result =
  272. sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
  273. if (result)
  274. goto release_idr;
  275. sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
  276. dev->attr.attr.name = dev->attr_name;
  277. dev->attr.attr.mode = 0444;
  278. dev->attr.show = thermal_cooling_device_trip_point_show;
  279. result = device_create_file(&tz->device, &dev->attr);
  280. if (result)
  281. goto remove_symbol_link;
  282. mutex_lock(&tz->lock);
  283. list_for_each_entry(pos, &tz->cooling_devices, node)
  284. if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
  285. result = -EEXIST;
  286. break;
  287. }
  288. if (!result)
  289. list_add_tail(&dev->node, &tz->cooling_devices);
  290. mutex_unlock(&tz->lock);
  291. if (!result)
  292. return 0;
  293. device_remove_file(&tz->device, &dev->attr);
  294. remove_symbol_link:
  295. sysfs_remove_link(&tz->device.kobj, dev->name);
  296. release_idr:
  297. release_idr(&tz->idr, &tz->lock, dev->id);
  298. free_mem:
  299. kfree(dev);
  300. return result;
  301. }
  302. EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
  303. /**
  304. * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
  305. * this function is usually called in the thermal zone device .unbind callback.
  306. * @tz: thermal zone device
  307. * @trip: indicates which trip point the cooling devices is
  308. * associated with in this thermal zone.
  309. * @cdev: thermal cooling device
  310. */
  311. int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
  312. int trip,
  313. struct thermal_cooling_device *cdev)
  314. {
  315. struct thermal_cooling_device_instance *pos, *next;
  316. mutex_lock(&tz->lock);
  317. list_for_each_entry_safe(pos, next, &tz->cooling_devices, node) {
  318. if (pos->tz == tz && pos->trip == trip
  319. && pos->cdev == cdev) {
  320. list_del(&pos->node);
  321. mutex_unlock(&tz->lock);
  322. goto unbind;
  323. }
  324. }
  325. mutex_unlock(&tz->lock);
  326. return -ENODEV;
  327. unbind:
  328. device_remove_file(&tz->device, &pos->attr);
  329. sysfs_remove_link(&tz->device.kobj, pos->name);
  330. release_idr(&tz->idr, &tz->lock, pos->id);
  331. kfree(pos);
  332. return 0;
  333. }
  334. EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
  335. static void thermal_release(struct device *dev)
  336. {
  337. struct thermal_zone_device *tz;
  338. struct thermal_cooling_device *cdev;
  339. if (!strncmp(dev->bus_id, "thermal_zone", sizeof "thermal_zone" - 1)) {
  340. tz = to_thermal_zone(dev);
  341. kfree(tz);
  342. } else {
  343. cdev = to_cooling_device(dev);
  344. kfree(cdev);
  345. }
  346. }
  347. static struct class thermal_class = {
  348. .name = "thermal",
  349. .dev_release = thermal_release,
  350. };
  351. /**
  352. * thermal_cooling_device_register - register a new thermal cooling device
  353. * @type: the thermal cooling device type.
  354. * @devdata: device private data.
  355. * @ops: standard thermal cooling devices callbacks.
  356. */
  357. struct thermal_cooling_device *thermal_cooling_device_register(char *type,
  358. void *devdata, struct thermal_cooling_device_ops *ops)
  359. {
  360. struct thermal_cooling_device *cdev;
  361. struct thermal_zone_device *pos;
  362. int result;
  363. if (strlen(type) >= THERMAL_NAME_LENGTH)
  364. return NULL;
  365. if (!ops || !ops->get_max_state || !ops->get_cur_state ||
  366. !ops->set_cur_state)
  367. return NULL;
  368. cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
  369. if (!cdev)
  370. return NULL;
  371. result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
  372. if (result) {
  373. kfree(cdev);
  374. return NULL;
  375. }
  376. strcpy(cdev->type, type);
  377. cdev->ops = ops;
  378. cdev->device.class = &thermal_class;
  379. cdev->devdata = devdata;
  380. sprintf(cdev->device.bus_id, "cooling_device%d", cdev->id);
  381. result = device_register(&cdev->device);
  382. if (result) {
  383. release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
  384. kfree(cdev);
  385. return NULL;
  386. }
  387. /* sys I/F */
  388. if (type) {
  389. result = device_create_file(&cdev->device,
  390. &dev_attr_cdev_type);
  391. if (result)
  392. goto unregister;
  393. }
  394. result = device_create_file(&cdev->device, &dev_attr_max_state);
  395. if (result)
  396. goto unregister;
  397. result = device_create_file(&cdev->device, &dev_attr_cur_state);
  398. if (result)
  399. goto unregister;
  400. mutex_lock(&thermal_list_lock);
  401. list_add(&cdev->node, &thermal_cdev_list);
  402. list_for_each_entry(pos, &thermal_tz_list, node) {
  403. if (!pos->ops->bind)
  404. continue;
  405. result = pos->ops->bind(pos, cdev);
  406. if (result)
  407. break;
  408. }
  409. mutex_unlock(&thermal_list_lock);
  410. if (!result)
  411. return cdev;
  412. unregister:
  413. release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
  414. device_unregister(&cdev->device);
  415. return NULL;
  416. }
  417. EXPORT_SYMBOL(thermal_cooling_device_register);
  418. /**
  419. * thermal_cooling_device_unregister - removes the registered thermal cooling device
  420. *
  421. * @cdev: the thermal cooling device to remove.
  422. *
  423. * thermal_cooling_device_unregister() must be called when the device is no
  424. * longer needed.
  425. */
  426. void thermal_cooling_device_unregister(struct
  427. thermal_cooling_device
  428. *cdev)
  429. {
  430. struct thermal_zone_device *tz;
  431. struct thermal_cooling_device *pos = NULL;
  432. if (!cdev)
  433. return;
  434. mutex_lock(&thermal_list_lock);
  435. list_for_each_entry(pos, &thermal_cdev_list, node)
  436. if (pos == cdev)
  437. break;
  438. if (pos != cdev) {
  439. /* thermal cooling device not found */
  440. mutex_unlock(&thermal_list_lock);
  441. return;
  442. }
  443. list_del(&cdev->node);
  444. list_for_each_entry(tz, &thermal_tz_list, node) {
  445. if (!tz->ops->unbind)
  446. continue;
  447. tz->ops->unbind(tz, cdev);
  448. }
  449. mutex_unlock(&thermal_list_lock);
  450. if (cdev->type[0])
  451. device_remove_file(&cdev->device,
  452. &dev_attr_cdev_type);
  453. device_remove_file(&cdev->device, &dev_attr_max_state);
  454. device_remove_file(&cdev->device, &dev_attr_cur_state);
  455. release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
  456. device_unregister(&cdev->device);
  457. return;
  458. }
  459. EXPORT_SYMBOL(thermal_cooling_device_unregister);
  460. /**
  461. * thermal_zone_device_register - register a new thermal zone device
  462. * @type: the thermal zone device type
  463. * @trips: the number of trip points the thermal zone support
  464. * @devdata: private device data
  465. * @ops: standard thermal zone device callbacks
  466. *
  467. * thermal_zone_device_unregister() must be called when the device is no
  468. * longer needed.
  469. */
  470. struct thermal_zone_device *thermal_zone_device_register(char *type,
  471. int trips, void *devdata,
  472. struct thermal_zone_device_ops *ops)
  473. {
  474. struct thermal_zone_device *tz;
  475. struct thermal_cooling_device *pos;
  476. int result;
  477. int count;
  478. if (strlen(type) >= THERMAL_NAME_LENGTH)
  479. return NULL;
  480. if (trips > THERMAL_MAX_TRIPS || trips < 0)
  481. return NULL;
  482. if (!ops || !ops->get_temp)
  483. return NULL;
  484. tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
  485. if (!tz)
  486. return NULL;
  487. INIT_LIST_HEAD(&tz->cooling_devices);
  488. idr_init(&tz->idr);
  489. mutex_init(&tz->lock);
  490. result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
  491. if (result) {
  492. kfree(tz);
  493. return NULL;
  494. }
  495. strcpy(tz->type, type);
  496. tz->ops = ops;
  497. tz->device.class = &thermal_class;
  498. tz->devdata = devdata;
  499. tz->trips = trips;
  500. sprintf(tz->device.bus_id, "thermal_zone%d", tz->id);
  501. result = device_register(&tz->device);
  502. if (result) {
  503. release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
  504. kfree(tz);
  505. return NULL;
  506. }
  507. /* sys I/F */
  508. if (type) {
  509. result = device_create_file(&tz->device, &dev_attr_type);
  510. if (result)
  511. goto unregister;
  512. }
  513. result = device_create_file(&tz->device, &dev_attr_temp);
  514. if (result)
  515. goto unregister;
  516. if (ops->get_mode) {
  517. result = device_create_file(&tz->device, &dev_attr_mode);
  518. if (result)
  519. goto unregister;
  520. }
  521. for (count = 0; count < trips; count++) {
  522. TRIP_POINT_ATTR_ADD(&tz->device, count, result);
  523. if (result)
  524. goto unregister;
  525. }
  526. mutex_lock(&thermal_list_lock);
  527. list_add_tail(&tz->node, &thermal_tz_list);
  528. if (ops->bind)
  529. list_for_each_entry(pos, &thermal_cdev_list, node) {
  530. result = ops->bind(tz, pos);
  531. if (result)
  532. break;
  533. }
  534. mutex_unlock(&thermal_list_lock);
  535. if (!result)
  536. return tz;
  537. unregister:
  538. release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
  539. device_unregister(&tz->device);
  540. return NULL;
  541. }
  542. EXPORT_SYMBOL(thermal_zone_device_register);
  543. /**
  544. * thermal_device_unregister - removes the registered thermal zone device
  545. *
  546. * @tz: the thermal zone device to remove
  547. */
  548. void thermal_zone_device_unregister(struct thermal_zone_device *tz)
  549. {
  550. struct thermal_cooling_device *cdev;
  551. struct thermal_zone_device *pos = NULL;
  552. int count;
  553. if (!tz)
  554. return;
  555. mutex_lock(&thermal_list_lock);
  556. list_for_each_entry(pos, &thermal_tz_list, node)
  557. if (pos == tz)
  558. break;
  559. if (pos != tz) {
  560. /* thermal zone device not found */
  561. mutex_unlock(&thermal_list_lock);
  562. return;
  563. }
  564. list_del(&tz->node);
  565. if (tz->ops->unbind)
  566. list_for_each_entry(cdev, &thermal_cdev_list, node)
  567. tz->ops->unbind(tz, cdev);
  568. mutex_unlock(&thermal_list_lock);
  569. if (tz->type[0])
  570. device_remove_file(&tz->device, &dev_attr_type);
  571. device_remove_file(&tz->device, &dev_attr_temp);
  572. if (tz->ops->get_mode)
  573. device_remove_file(&tz->device, &dev_attr_mode);
  574. for (count = 0; count < tz->trips; count++)
  575. TRIP_POINT_ATTR_REMOVE(&tz->device, count);
  576. release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
  577. idr_destroy(&tz->idr);
  578. mutex_destroy(&tz->lock);
  579. device_unregister(&tz->device);
  580. return;
  581. }
  582. EXPORT_SYMBOL(thermal_zone_device_unregister);
  583. static int __init thermal_init(void)
  584. {
  585. int result = 0;
  586. result = class_register(&thermal_class);
  587. if (result) {
  588. idr_destroy(&thermal_tz_idr);
  589. idr_destroy(&thermal_cdev_idr);
  590. mutex_destroy(&thermal_idr_lock);
  591. mutex_destroy(&thermal_list_lock);
  592. }
  593. return result;
  594. }
  595. static void __exit thermal_exit(void)
  596. {
  597. class_unregister(&thermal_class);
  598. idr_destroy(&thermal_tz_idr);
  599. idr_destroy(&thermal_cdev_idr);
  600. mutex_destroy(&thermal_idr_lock);
  601. mutex_destroy(&thermal_list_lock);
  602. }
  603. subsys_initcall(thermal_init);
  604. module_exit(thermal_exit);