thermal.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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. struct thermal_zone_device *pos1;
  256. struct thermal_cooling_device *pos2;
  257. int result;
  258. if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
  259. return -EINVAL;
  260. list_for_each_entry(pos1, &thermal_tz_list, node) {
  261. if (pos1 == tz)
  262. break;
  263. }
  264. list_for_each_entry(pos2, &thermal_cdev_list, node) {
  265. if (pos2 == cdev)
  266. break;
  267. }
  268. if (tz != pos1 || cdev != pos2)
  269. return -EINVAL;
  270. dev =
  271. kzalloc(sizeof(struct thermal_cooling_device_instance), GFP_KERNEL);
  272. if (!dev)
  273. return -ENOMEM;
  274. dev->tz = tz;
  275. dev->cdev = cdev;
  276. dev->trip = trip;
  277. result = get_idr(&tz->idr, &tz->lock, &dev->id);
  278. if (result)
  279. goto free_mem;
  280. sprintf(dev->name, "cdev%d", dev->id);
  281. result =
  282. sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
  283. if (result)
  284. goto release_idr;
  285. sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
  286. dev->attr.attr.name = dev->attr_name;
  287. dev->attr.attr.mode = 0444;
  288. dev->attr.show = thermal_cooling_device_trip_point_show;
  289. result = device_create_file(&tz->device, &dev->attr);
  290. if (result)
  291. goto remove_symbol_link;
  292. mutex_lock(&tz->lock);
  293. list_for_each_entry(pos, &tz->cooling_devices, node)
  294. if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
  295. result = -EEXIST;
  296. break;
  297. }
  298. if (!result)
  299. list_add_tail(&dev->node, &tz->cooling_devices);
  300. mutex_unlock(&tz->lock);
  301. if (!result)
  302. return 0;
  303. device_remove_file(&tz->device, &dev->attr);
  304. remove_symbol_link:
  305. sysfs_remove_link(&tz->device.kobj, dev->name);
  306. release_idr:
  307. release_idr(&tz->idr, &tz->lock, dev->id);
  308. free_mem:
  309. kfree(dev);
  310. return result;
  311. }
  312. EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
  313. /**
  314. * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
  315. * @tz: thermal zone device
  316. * @trip: indicates which trip point the cooling devices is
  317. * associated with in this thermal zone.
  318. * @cdev: thermal cooling device
  319. *
  320. * This function is usually called in the thermal zone device .unbind callback.
  321. */
  322. int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
  323. int trip,
  324. struct thermal_cooling_device *cdev)
  325. {
  326. struct thermal_cooling_device_instance *pos, *next;
  327. mutex_lock(&tz->lock);
  328. list_for_each_entry_safe(pos, next, &tz->cooling_devices, node) {
  329. if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
  330. list_del(&pos->node);
  331. mutex_unlock(&tz->lock);
  332. goto unbind;
  333. }
  334. }
  335. mutex_unlock(&tz->lock);
  336. return -ENODEV;
  337. unbind:
  338. device_remove_file(&tz->device, &pos->attr);
  339. sysfs_remove_link(&tz->device.kobj, pos->name);
  340. release_idr(&tz->idr, &tz->lock, pos->id);
  341. kfree(pos);
  342. return 0;
  343. }
  344. EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
  345. static void thermal_release(struct device *dev)
  346. {
  347. struct thermal_zone_device *tz;
  348. struct thermal_cooling_device *cdev;
  349. if (!strncmp(dev->bus_id, "thermal_zone", sizeof "thermal_zone" - 1)) {
  350. tz = to_thermal_zone(dev);
  351. kfree(tz);
  352. } else {
  353. cdev = to_cooling_device(dev);
  354. kfree(cdev);
  355. }
  356. }
  357. static struct class thermal_class = {
  358. .name = "thermal",
  359. .dev_release = thermal_release,
  360. };
  361. /**
  362. * thermal_cooling_device_register - register a new thermal cooling device
  363. * @type: the thermal cooling device type.
  364. * @devdata: device private data.
  365. * @ops: standard thermal cooling devices callbacks.
  366. */
  367. struct thermal_cooling_device *thermal_cooling_device_register(char *type,
  368. void *devdata,
  369. struct
  370. thermal_cooling_device_ops
  371. *ops)
  372. {
  373. struct thermal_cooling_device *cdev;
  374. struct thermal_zone_device *pos;
  375. int result;
  376. if (strlen(type) >= THERMAL_NAME_LENGTH)
  377. return ERR_PTR(-EINVAL);
  378. if (!ops || !ops->get_max_state || !ops->get_cur_state ||
  379. !ops->set_cur_state)
  380. return ERR_PTR(-EINVAL);
  381. cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
  382. if (!cdev)
  383. return ERR_PTR(-ENOMEM);
  384. result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
  385. if (result) {
  386. kfree(cdev);
  387. return ERR_PTR(result);
  388. }
  389. strcpy(cdev->type, type);
  390. cdev->ops = ops;
  391. cdev->device.class = &thermal_class;
  392. cdev->devdata = devdata;
  393. sprintf(cdev->device.bus_id, "cooling_device%d", cdev->id);
  394. result = device_register(&cdev->device);
  395. if (result) {
  396. release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
  397. kfree(cdev);
  398. return ERR_PTR(result);
  399. }
  400. /* sys I/F */
  401. if (type) {
  402. result = device_create_file(&cdev->device, &dev_attr_cdev_type);
  403. if (result)
  404. goto unregister;
  405. }
  406. result = device_create_file(&cdev->device, &dev_attr_max_state);
  407. if (result)
  408. goto unregister;
  409. result = device_create_file(&cdev->device, &dev_attr_cur_state);
  410. if (result)
  411. goto unregister;
  412. mutex_lock(&thermal_list_lock);
  413. list_add(&cdev->node, &thermal_cdev_list);
  414. list_for_each_entry(pos, &thermal_tz_list, node) {
  415. if (!pos->ops->bind)
  416. continue;
  417. result = pos->ops->bind(pos, cdev);
  418. if (result)
  419. break;
  420. }
  421. mutex_unlock(&thermal_list_lock);
  422. if (!result)
  423. return cdev;
  424. unregister:
  425. release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
  426. device_unregister(&cdev->device);
  427. return ERR_PTR(result);
  428. }
  429. EXPORT_SYMBOL(thermal_cooling_device_register);
  430. /**
  431. * thermal_cooling_device_unregister - removes the registered thermal cooling device
  432. * @cdev: the thermal cooling device to remove.
  433. *
  434. * thermal_cooling_device_unregister() must be called when the device is no
  435. * longer needed.
  436. */
  437. void thermal_cooling_device_unregister(struct
  438. thermal_cooling_device
  439. *cdev)
  440. {
  441. struct thermal_zone_device *tz;
  442. struct thermal_cooling_device *pos = NULL;
  443. if (!cdev)
  444. return;
  445. mutex_lock(&thermal_list_lock);
  446. list_for_each_entry(pos, &thermal_cdev_list, node)
  447. if (pos == cdev)
  448. break;
  449. if (pos != cdev) {
  450. /* thermal cooling device not found */
  451. mutex_unlock(&thermal_list_lock);
  452. return;
  453. }
  454. list_del(&cdev->node);
  455. list_for_each_entry(tz, &thermal_tz_list, node) {
  456. if (!tz->ops->unbind)
  457. continue;
  458. tz->ops->unbind(tz, cdev);
  459. }
  460. mutex_unlock(&thermal_list_lock);
  461. if (cdev->type[0])
  462. device_remove_file(&cdev->device, &dev_attr_cdev_type);
  463. device_remove_file(&cdev->device, &dev_attr_max_state);
  464. device_remove_file(&cdev->device, &dev_attr_cur_state);
  465. release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
  466. device_unregister(&cdev->device);
  467. return;
  468. }
  469. EXPORT_SYMBOL(thermal_cooling_device_unregister);
  470. /**
  471. * thermal_zone_device_register - register a new thermal zone device
  472. * @type: the thermal zone device type
  473. * @trips: the number of trip points the thermal zone support
  474. * @devdata: private device data
  475. * @ops: standard thermal zone device callbacks
  476. *
  477. * thermal_zone_device_unregister() must be called when the device is no
  478. * longer needed.
  479. */
  480. struct thermal_zone_device *thermal_zone_device_register(char *type,
  481. int trips,
  482. void *devdata, struct
  483. thermal_zone_device_ops
  484. *ops)
  485. {
  486. struct thermal_zone_device *tz;
  487. struct thermal_cooling_device *pos;
  488. int result;
  489. int count;
  490. if (strlen(type) >= THERMAL_NAME_LENGTH)
  491. return ERR_PTR(-EINVAL);
  492. if (trips > THERMAL_MAX_TRIPS || trips < 0)
  493. return ERR_PTR(-EINVAL);
  494. if (!ops || !ops->get_temp)
  495. return ERR_PTR(-EINVAL);
  496. tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
  497. if (!tz)
  498. return ERR_PTR(-ENOMEM);
  499. INIT_LIST_HEAD(&tz->cooling_devices);
  500. idr_init(&tz->idr);
  501. mutex_init(&tz->lock);
  502. result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
  503. if (result) {
  504. kfree(tz);
  505. return ERR_PTR(result);
  506. }
  507. strcpy(tz->type, type);
  508. tz->ops = ops;
  509. tz->device.class = &thermal_class;
  510. tz->devdata = devdata;
  511. tz->trips = trips;
  512. sprintf(tz->device.bus_id, "thermal_zone%d", tz->id);
  513. result = device_register(&tz->device);
  514. if (result) {
  515. release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
  516. kfree(tz);
  517. return ERR_PTR(result);
  518. }
  519. /* sys I/F */
  520. if (type) {
  521. result = device_create_file(&tz->device, &dev_attr_type);
  522. if (result)
  523. goto unregister;
  524. }
  525. result = device_create_file(&tz->device, &dev_attr_temp);
  526. if (result)
  527. goto unregister;
  528. if (ops->get_mode) {
  529. result = device_create_file(&tz->device, &dev_attr_mode);
  530. if (result)
  531. goto unregister;
  532. }
  533. for (count = 0; count < trips; count++) {
  534. TRIP_POINT_ATTR_ADD(&tz->device, count, result);
  535. if (result)
  536. goto unregister;
  537. }
  538. mutex_lock(&thermal_list_lock);
  539. list_add_tail(&tz->node, &thermal_tz_list);
  540. if (ops->bind)
  541. list_for_each_entry(pos, &thermal_cdev_list, node) {
  542. result = ops->bind(tz, pos);
  543. if (result)
  544. break;
  545. }
  546. mutex_unlock(&thermal_list_lock);
  547. if (!result)
  548. return tz;
  549. unregister:
  550. release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
  551. device_unregister(&tz->device);
  552. return ERR_PTR(result);
  553. }
  554. EXPORT_SYMBOL(thermal_zone_device_register);
  555. /**
  556. * thermal_device_unregister - removes the registered thermal zone device
  557. * @tz: the thermal zone device to remove
  558. */
  559. void thermal_zone_device_unregister(struct thermal_zone_device *tz)
  560. {
  561. struct thermal_cooling_device *cdev;
  562. struct thermal_zone_device *pos = NULL;
  563. int count;
  564. if (!tz)
  565. return;
  566. mutex_lock(&thermal_list_lock);
  567. list_for_each_entry(pos, &thermal_tz_list, node)
  568. if (pos == tz)
  569. break;
  570. if (pos != tz) {
  571. /* thermal zone device not found */
  572. mutex_unlock(&thermal_list_lock);
  573. return;
  574. }
  575. list_del(&tz->node);
  576. if (tz->ops->unbind)
  577. list_for_each_entry(cdev, &thermal_cdev_list, node)
  578. tz->ops->unbind(tz, cdev);
  579. mutex_unlock(&thermal_list_lock);
  580. if (tz->type[0])
  581. device_remove_file(&tz->device, &dev_attr_type);
  582. device_remove_file(&tz->device, &dev_attr_temp);
  583. if (tz->ops->get_mode)
  584. device_remove_file(&tz->device, &dev_attr_mode);
  585. for (count = 0; count < tz->trips; count++)
  586. TRIP_POINT_ATTR_REMOVE(&tz->device, count);
  587. release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
  588. idr_destroy(&tz->idr);
  589. mutex_destroy(&tz->lock);
  590. device_unregister(&tz->device);
  591. return;
  592. }
  593. EXPORT_SYMBOL(thermal_zone_device_unregister);
  594. static int __init thermal_init(void)
  595. {
  596. int result = 0;
  597. result = class_register(&thermal_class);
  598. if (result) {
  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. return result;
  605. }
  606. static void __exit thermal_exit(void)
  607. {
  608. class_unregister(&thermal_class);
  609. idr_destroy(&thermal_tz_idr);
  610. idr_destroy(&thermal_cdev_idr);
  611. mutex_destroy(&thermal_idr_lock);
  612. mutex_destroy(&thermal_list_lock);
  613. }
  614. subsys_initcall(thermal_init);
  615. module_exit(thermal_exit);