thermal.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  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. * @tz: thermal zone device
  243. * @trip: indicates which trip point the cooling devices is
  244. * associated with in this thermal zone.
  245. * @cdev: thermal cooling device
  246. *
  247. * This function is usually called in the thermal zone device .bind callback.
  248. */
  249. int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
  250. int trip,
  251. struct thermal_cooling_device *cdev)
  252. {
  253. struct thermal_cooling_device_instance *dev;
  254. struct thermal_cooling_device_instance *pos;
  255. int result;
  256. if (trip >= tz->trips || (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. * @tz: thermal zone device
  306. * @trip: indicates which trip point the cooling devices is
  307. * associated with in this thermal zone.
  308. * @cdev: thermal cooling device
  309. *
  310. * This function is usually called in the thermal zone device .unbind callback.
  311. */
  312. int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
  313. int trip,
  314. struct thermal_cooling_device *cdev)
  315. {
  316. struct thermal_cooling_device_instance *pos, *next;
  317. mutex_lock(&tz->lock);
  318. list_for_each_entry_safe(pos, next, &tz->cooling_devices, node) {
  319. if (pos->tz == tz && pos->trip == trip && 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,
  359. struct
  360. thermal_cooling_device_ops
  361. *ops)
  362. {
  363. struct thermal_cooling_device *cdev;
  364. struct thermal_zone_device *pos;
  365. int result;
  366. if (strlen(type) >= THERMAL_NAME_LENGTH)
  367. return NULL;
  368. if (!ops || !ops->get_max_state || !ops->get_cur_state ||
  369. !ops->set_cur_state)
  370. return NULL;
  371. cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
  372. if (!cdev)
  373. return NULL;
  374. result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
  375. if (result) {
  376. kfree(cdev);
  377. return NULL;
  378. }
  379. strcpy(cdev->type, type);
  380. cdev->ops = ops;
  381. cdev->device.class = &thermal_class;
  382. cdev->devdata = devdata;
  383. sprintf(cdev->device.bus_id, "cooling_device%d", cdev->id);
  384. result = device_register(&cdev->device);
  385. if (result) {
  386. release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
  387. kfree(cdev);
  388. return NULL;
  389. }
  390. /* sys I/F */
  391. if (type) {
  392. result = device_create_file(&cdev->device, &dev_attr_cdev_type);
  393. if (result)
  394. goto unregister;
  395. }
  396. result = device_create_file(&cdev->device, &dev_attr_max_state);
  397. if (result)
  398. goto unregister;
  399. result = device_create_file(&cdev->device, &dev_attr_cur_state);
  400. if (result)
  401. goto unregister;
  402. mutex_lock(&thermal_list_lock);
  403. list_add(&cdev->node, &thermal_cdev_list);
  404. list_for_each_entry(pos, &thermal_tz_list, node) {
  405. if (!pos->ops->bind)
  406. continue;
  407. result = pos->ops->bind(pos, cdev);
  408. if (result)
  409. break;
  410. }
  411. mutex_unlock(&thermal_list_lock);
  412. if (!result)
  413. return cdev;
  414. unregister:
  415. release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
  416. device_unregister(&cdev->device);
  417. return NULL;
  418. }
  419. EXPORT_SYMBOL(thermal_cooling_device_register);
  420. /**
  421. * thermal_cooling_device_unregister - removes the registered thermal cooling device
  422. * @cdev: the thermal cooling device to remove.
  423. *
  424. * thermal_cooling_device_unregister() must be called when the device is no
  425. * longer needed.
  426. */
  427. void thermal_cooling_device_unregister(struct
  428. thermal_cooling_device
  429. *cdev)
  430. {
  431. struct thermal_zone_device *tz;
  432. struct thermal_cooling_device *pos = NULL;
  433. if (!cdev)
  434. return;
  435. mutex_lock(&thermal_list_lock);
  436. list_for_each_entry(pos, &thermal_cdev_list, node)
  437. if (pos == cdev)
  438. break;
  439. if (pos != cdev) {
  440. /* thermal cooling device not found */
  441. mutex_unlock(&thermal_list_lock);
  442. return;
  443. }
  444. list_del(&cdev->node);
  445. list_for_each_entry(tz, &thermal_tz_list, node) {
  446. if (!tz->ops->unbind)
  447. continue;
  448. tz->ops->unbind(tz, cdev);
  449. }
  450. mutex_unlock(&thermal_list_lock);
  451. if (cdev->type[0])
  452. device_remove_file(&cdev->device, &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,
  472. void *devdata, struct
  473. thermal_zone_device_ops
  474. *ops)
  475. {
  476. struct thermal_zone_device *tz;
  477. struct thermal_cooling_device *pos;
  478. int result;
  479. int count;
  480. if (strlen(type) >= THERMAL_NAME_LENGTH)
  481. return NULL;
  482. if (trips > THERMAL_MAX_TRIPS || trips < 0)
  483. return NULL;
  484. if (!ops || !ops->get_temp)
  485. return NULL;
  486. tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
  487. if (!tz)
  488. return NULL;
  489. INIT_LIST_HEAD(&tz->cooling_devices);
  490. idr_init(&tz->idr);
  491. mutex_init(&tz->lock);
  492. result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
  493. if (result) {
  494. kfree(tz);
  495. return NULL;
  496. }
  497. strcpy(tz->type, type);
  498. tz->ops = ops;
  499. tz->device.class = &thermal_class;
  500. tz->devdata = devdata;
  501. tz->trips = trips;
  502. sprintf(tz->device.bus_id, "thermal_zone%d", tz->id);
  503. result = device_register(&tz->device);
  504. if (result) {
  505. release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
  506. kfree(tz);
  507. return NULL;
  508. }
  509. /* sys I/F */
  510. if (type) {
  511. result = device_create_file(&tz->device, &dev_attr_type);
  512. if (result)
  513. goto unregister;
  514. }
  515. result = device_create_file(&tz->device, &dev_attr_temp);
  516. if (result)
  517. goto unregister;
  518. if (ops->get_mode) {
  519. result = device_create_file(&tz->device, &dev_attr_mode);
  520. if (result)
  521. goto unregister;
  522. }
  523. for (count = 0; count < trips; count++) {
  524. TRIP_POINT_ATTR_ADD(&tz->device, count, result);
  525. if (result)
  526. goto unregister;
  527. }
  528. mutex_lock(&thermal_list_lock);
  529. list_add_tail(&tz->node, &thermal_tz_list);
  530. if (ops->bind)
  531. list_for_each_entry(pos, &thermal_cdev_list, node) {
  532. result = ops->bind(tz, pos);
  533. if (result)
  534. break;
  535. }
  536. mutex_unlock(&thermal_list_lock);
  537. if (!result)
  538. return tz;
  539. unregister:
  540. release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
  541. device_unregister(&tz->device);
  542. return NULL;
  543. }
  544. EXPORT_SYMBOL(thermal_zone_device_register);
  545. /**
  546. * thermal_device_unregister - removes the registered thermal zone device
  547. * @tz: the thermal zone device to remove
  548. */
  549. void thermal_zone_device_unregister(struct thermal_zone_device *tz)
  550. {
  551. struct thermal_cooling_device *cdev;
  552. struct thermal_zone_device *pos = NULL;
  553. int count;
  554. if (!tz)
  555. return;
  556. mutex_lock(&thermal_list_lock);
  557. list_for_each_entry(pos, &thermal_tz_list, node)
  558. if (pos == tz)
  559. break;
  560. if (pos != tz) {
  561. /* thermal zone device not found */
  562. mutex_unlock(&thermal_list_lock);
  563. return;
  564. }
  565. list_del(&tz->node);
  566. if (tz->ops->unbind)
  567. list_for_each_entry(cdev, &thermal_cdev_list, node)
  568. tz->ops->unbind(tz, cdev);
  569. mutex_unlock(&thermal_list_lock);
  570. if (tz->type[0])
  571. device_remove_file(&tz->device, &dev_attr_type);
  572. device_remove_file(&tz->device, &dev_attr_temp);
  573. if (tz->ops->get_mode)
  574. device_remove_file(&tz->device, &dev_attr_mode);
  575. for (count = 0; count < tz->trips; count++)
  576. TRIP_POINT_ATTR_REMOVE(&tz->device, count);
  577. release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
  578. idr_destroy(&tz->idr);
  579. mutex_destroy(&tz->lock);
  580. device_unregister(&tz->device);
  581. return;
  582. }
  583. EXPORT_SYMBOL(thermal_zone_device_unregister);
  584. static int __init thermal_init(void)
  585. {
  586. int result = 0;
  587. result = class_register(&thermal_class);
  588. if (result) {
  589. idr_destroy(&thermal_tz_idr);
  590. idr_destroy(&thermal_cdev_idr);
  591. mutex_destroy(&thermal_idr_lock);
  592. mutex_destroy(&thermal_list_lock);
  593. }
  594. return result;
  595. }
  596. static void __exit thermal_exit(void)
  597. {
  598. class_unregister(&thermal_class);
  599. idr_destroy(&thermal_tz_idr);
  600. idr_destroy(&thermal_cdev_idr);
  601. mutex_destroy(&thermal_idr_lock);
  602. mutex_destroy(&thermal_list_lock);
  603. }
  604. subsys_initcall(thermal_init);
  605. module_exit(thermal_exit);