thermal.c 19 KB

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