thermal_sys.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  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. #if defined(CONFIG_THERMAL_HWMON)
  245. /* hwmon sys I/F */
  246. #include <linux/hwmon.h>
  247. static LIST_HEAD(thermal_hwmon_list);
  248. static ssize_t
  249. name_show(struct device *dev, struct device_attribute *attr, char *buf)
  250. {
  251. struct thermal_hwmon_device *hwmon = dev->driver_data;
  252. return sprintf(buf, "%s\n", hwmon->type);
  253. }
  254. static DEVICE_ATTR(name, 0444, name_show, NULL);
  255. static ssize_t
  256. temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
  257. {
  258. struct thermal_hwmon_attr *hwmon_attr
  259. = container_of(attr, struct thermal_hwmon_attr, attr);
  260. struct thermal_zone_device *tz
  261. = container_of(hwmon_attr, struct thermal_zone_device,
  262. temp_input);
  263. return tz->ops->get_temp(tz, buf);
  264. }
  265. static ssize_t
  266. temp_crit_show(struct device *dev, struct device_attribute *attr,
  267. char *buf)
  268. {
  269. struct thermal_hwmon_attr *hwmon_attr
  270. = container_of(attr, struct thermal_hwmon_attr, attr);
  271. struct thermal_zone_device *tz
  272. = container_of(hwmon_attr, struct thermal_zone_device,
  273. temp_crit);
  274. return tz->ops->get_trip_temp(tz, 0, buf);
  275. }
  276. static int
  277. thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
  278. {
  279. struct thermal_hwmon_device *hwmon;
  280. int new_hwmon_device = 1;
  281. int result;
  282. mutex_lock(&thermal_list_lock);
  283. list_for_each_entry(hwmon, &thermal_hwmon_list, node)
  284. if (!strcmp(hwmon->type, tz->type)) {
  285. new_hwmon_device = 0;
  286. mutex_unlock(&thermal_list_lock);
  287. goto register_sys_interface;
  288. }
  289. mutex_unlock(&thermal_list_lock);
  290. hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
  291. if (!hwmon)
  292. return -ENOMEM;
  293. INIT_LIST_HEAD(&hwmon->tz_list);
  294. strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
  295. hwmon->device = hwmon_device_register(NULL);
  296. if (IS_ERR(hwmon->device)) {
  297. result = PTR_ERR(hwmon->device);
  298. goto free_mem;
  299. }
  300. hwmon->device->driver_data = hwmon;
  301. result = device_create_file(hwmon->device, &dev_attr_name);
  302. if (result)
  303. goto unregister_hwmon_device;
  304. register_sys_interface:
  305. tz->hwmon = hwmon;
  306. hwmon->count++;
  307. snprintf(tz->temp_input.name, THERMAL_NAME_LENGTH,
  308. "temp%d_input", hwmon->count);
  309. tz->temp_input.attr.attr.name = tz->temp_input.name;
  310. tz->temp_input.attr.attr.mode = 0444;
  311. tz->temp_input.attr.show = temp_input_show;
  312. result = device_create_file(hwmon->device, &tz->temp_input.attr);
  313. if (result)
  314. goto unregister_hwmon_device;
  315. if (tz->ops->get_crit_temp) {
  316. unsigned long temperature;
  317. if (!tz->ops->get_crit_temp(tz, &temperature)) {
  318. snprintf(tz->temp_crit.name, THERMAL_NAME_LENGTH,
  319. "temp%d_crit", hwmon->count);
  320. tz->temp_crit.attr.attr.name = tz->temp_crit.name;
  321. tz->temp_crit.attr.attr.mode = 0444;
  322. tz->temp_crit.attr.show = temp_crit_show;
  323. result = device_create_file(hwmon->device,
  324. &tz->temp_crit.attr);
  325. if (result)
  326. goto unregister_hwmon_device;
  327. }
  328. }
  329. mutex_lock(&thermal_list_lock);
  330. if (new_hwmon_device)
  331. list_add_tail(&hwmon->node, &thermal_hwmon_list);
  332. list_add_tail(&tz->hwmon_node, &hwmon->tz_list);
  333. mutex_unlock(&thermal_list_lock);
  334. return 0;
  335. unregister_hwmon_device:
  336. device_remove_file(hwmon->device, &tz->temp_crit.attr);
  337. device_remove_file(hwmon->device, &tz->temp_input.attr);
  338. if (new_hwmon_device) {
  339. device_remove_file(hwmon->device, &dev_attr_name);
  340. hwmon_device_unregister(hwmon->device);
  341. }
  342. free_mem:
  343. if (new_hwmon_device)
  344. kfree(hwmon);
  345. return result;
  346. }
  347. static void
  348. thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
  349. {
  350. struct thermal_hwmon_device *hwmon = tz->hwmon;
  351. tz->hwmon = NULL;
  352. device_remove_file(hwmon->device, &tz->temp_input.attr);
  353. device_remove_file(hwmon->device, &tz->temp_crit.attr);
  354. mutex_lock(&thermal_list_lock);
  355. list_del(&tz->hwmon_node);
  356. if (!list_empty(&hwmon->tz_list)) {
  357. mutex_unlock(&thermal_list_lock);
  358. return;
  359. }
  360. list_del(&hwmon->node);
  361. mutex_unlock(&thermal_list_lock);
  362. device_remove_file(hwmon->device, &dev_attr_name);
  363. hwmon_device_unregister(hwmon->device);
  364. kfree(hwmon);
  365. }
  366. #else
  367. static int
  368. thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
  369. {
  370. return 0;
  371. }
  372. static void
  373. thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
  374. {
  375. }
  376. #endif
  377. /**
  378. * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
  379. * @tz: thermal zone device
  380. * @trip: indicates which trip point the cooling devices is
  381. * associated with in this thermal zone.
  382. * @cdev: thermal cooling device
  383. *
  384. * This function is usually called in the thermal zone device .bind callback.
  385. */
  386. int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
  387. int trip,
  388. struct thermal_cooling_device *cdev)
  389. {
  390. struct thermal_cooling_device_instance *dev;
  391. struct thermal_cooling_device_instance *pos;
  392. struct thermal_zone_device *pos1;
  393. struct thermal_cooling_device *pos2;
  394. int result;
  395. if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
  396. return -EINVAL;
  397. list_for_each_entry(pos1, &thermal_tz_list, node) {
  398. if (pos1 == tz)
  399. break;
  400. }
  401. list_for_each_entry(pos2, &thermal_cdev_list, node) {
  402. if (pos2 == cdev)
  403. break;
  404. }
  405. if (tz != pos1 || cdev != pos2)
  406. return -EINVAL;
  407. dev =
  408. kzalloc(sizeof(struct thermal_cooling_device_instance), GFP_KERNEL);
  409. if (!dev)
  410. return -ENOMEM;
  411. dev->tz = tz;
  412. dev->cdev = cdev;
  413. dev->trip = trip;
  414. result = get_idr(&tz->idr, &tz->lock, &dev->id);
  415. if (result)
  416. goto free_mem;
  417. sprintf(dev->name, "cdev%d", dev->id);
  418. result =
  419. sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
  420. if (result)
  421. goto release_idr;
  422. sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
  423. dev->attr.attr.name = dev->attr_name;
  424. dev->attr.attr.mode = 0444;
  425. dev->attr.show = thermal_cooling_device_trip_point_show;
  426. result = device_create_file(&tz->device, &dev->attr);
  427. if (result)
  428. goto remove_symbol_link;
  429. mutex_lock(&tz->lock);
  430. list_for_each_entry(pos, &tz->cooling_devices, node)
  431. if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
  432. result = -EEXIST;
  433. break;
  434. }
  435. if (!result)
  436. list_add_tail(&dev->node, &tz->cooling_devices);
  437. mutex_unlock(&tz->lock);
  438. if (!result)
  439. return 0;
  440. device_remove_file(&tz->device, &dev->attr);
  441. remove_symbol_link:
  442. sysfs_remove_link(&tz->device.kobj, dev->name);
  443. release_idr:
  444. release_idr(&tz->idr, &tz->lock, dev->id);
  445. free_mem:
  446. kfree(dev);
  447. return result;
  448. }
  449. EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
  450. /**
  451. * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
  452. * @tz: thermal zone device
  453. * @trip: indicates which trip point the cooling devices is
  454. * associated with in this thermal zone.
  455. * @cdev: thermal cooling device
  456. *
  457. * This function is usually called in the thermal zone device .unbind callback.
  458. */
  459. int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
  460. int trip,
  461. struct thermal_cooling_device *cdev)
  462. {
  463. struct thermal_cooling_device_instance *pos, *next;
  464. mutex_lock(&tz->lock);
  465. list_for_each_entry_safe(pos, next, &tz->cooling_devices, node) {
  466. if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
  467. list_del(&pos->node);
  468. mutex_unlock(&tz->lock);
  469. goto unbind;
  470. }
  471. }
  472. mutex_unlock(&tz->lock);
  473. return -ENODEV;
  474. unbind:
  475. device_remove_file(&tz->device, &pos->attr);
  476. sysfs_remove_link(&tz->device.kobj, pos->name);
  477. release_idr(&tz->idr, &tz->lock, pos->id);
  478. kfree(pos);
  479. return 0;
  480. }
  481. EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
  482. static void thermal_release(struct device *dev)
  483. {
  484. struct thermal_zone_device *tz;
  485. struct thermal_cooling_device *cdev;
  486. if (!strncmp(dev->bus_id, "thermal_zone", sizeof "thermal_zone" - 1)) {
  487. tz = to_thermal_zone(dev);
  488. kfree(tz);
  489. } else {
  490. cdev = to_cooling_device(dev);
  491. kfree(cdev);
  492. }
  493. }
  494. static struct class thermal_class = {
  495. .name = "thermal",
  496. .dev_release = thermal_release,
  497. };
  498. /**
  499. * thermal_cooling_device_register - register a new thermal cooling device
  500. * @type: the thermal cooling device type.
  501. * @devdata: device private data.
  502. * @ops: standard thermal cooling devices callbacks.
  503. */
  504. struct thermal_cooling_device *thermal_cooling_device_register(char *type,
  505. void *devdata,
  506. struct
  507. thermal_cooling_device_ops
  508. *ops)
  509. {
  510. struct thermal_cooling_device *cdev;
  511. struct thermal_zone_device *pos;
  512. int result;
  513. if (strlen(type) >= THERMAL_NAME_LENGTH)
  514. return ERR_PTR(-EINVAL);
  515. if (!ops || !ops->get_max_state || !ops->get_cur_state ||
  516. !ops->set_cur_state)
  517. return ERR_PTR(-EINVAL);
  518. cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
  519. if (!cdev)
  520. return ERR_PTR(-ENOMEM);
  521. result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
  522. if (result) {
  523. kfree(cdev);
  524. return ERR_PTR(result);
  525. }
  526. strcpy(cdev->type, type);
  527. cdev->ops = ops;
  528. cdev->device.class = &thermal_class;
  529. cdev->devdata = devdata;
  530. sprintf(cdev->device.bus_id, "cooling_device%d", cdev->id);
  531. result = device_register(&cdev->device);
  532. if (result) {
  533. release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
  534. kfree(cdev);
  535. return ERR_PTR(result);
  536. }
  537. /* sys I/F */
  538. if (type) {
  539. result = device_create_file(&cdev->device, &dev_attr_cdev_type);
  540. if (result)
  541. goto unregister;
  542. }
  543. result = device_create_file(&cdev->device, &dev_attr_max_state);
  544. if (result)
  545. goto unregister;
  546. result = device_create_file(&cdev->device, &dev_attr_cur_state);
  547. if (result)
  548. goto unregister;
  549. mutex_lock(&thermal_list_lock);
  550. list_add(&cdev->node, &thermal_cdev_list);
  551. list_for_each_entry(pos, &thermal_tz_list, node) {
  552. if (!pos->ops->bind)
  553. continue;
  554. result = pos->ops->bind(pos, cdev);
  555. if (result)
  556. break;
  557. }
  558. mutex_unlock(&thermal_list_lock);
  559. if (!result)
  560. return cdev;
  561. unregister:
  562. release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
  563. device_unregister(&cdev->device);
  564. return ERR_PTR(result);
  565. }
  566. EXPORT_SYMBOL(thermal_cooling_device_register);
  567. /**
  568. * thermal_cooling_device_unregister - removes the registered thermal cooling device
  569. * @cdev: the thermal cooling device to remove.
  570. *
  571. * thermal_cooling_device_unregister() must be called when the device is no
  572. * longer needed.
  573. */
  574. void thermal_cooling_device_unregister(struct
  575. thermal_cooling_device
  576. *cdev)
  577. {
  578. struct thermal_zone_device *tz;
  579. struct thermal_cooling_device *pos = NULL;
  580. if (!cdev)
  581. return;
  582. mutex_lock(&thermal_list_lock);
  583. list_for_each_entry(pos, &thermal_cdev_list, node)
  584. if (pos == cdev)
  585. break;
  586. if (pos != cdev) {
  587. /* thermal cooling device not found */
  588. mutex_unlock(&thermal_list_lock);
  589. return;
  590. }
  591. list_del(&cdev->node);
  592. list_for_each_entry(tz, &thermal_tz_list, node) {
  593. if (!tz->ops->unbind)
  594. continue;
  595. tz->ops->unbind(tz, cdev);
  596. }
  597. mutex_unlock(&thermal_list_lock);
  598. if (cdev->type[0])
  599. device_remove_file(&cdev->device, &dev_attr_cdev_type);
  600. device_remove_file(&cdev->device, &dev_attr_max_state);
  601. device_remove_file(&cdev->device, &dev_attr_cur_state);
  602. release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
  603. device_unregister(&cdev->device);
  604. return;
  605. }
  606. EXPORT_SYMBOL(thermal_cooling_device_unregister);
  607. /**
  608. * thermal_zone_device_register - register a new thermal zone device
  609. * @type: the thermal zone device type
  610. * @trips: the number of trip points the thermal zone support
  611. * @devdata: private device data
  612. * @ops: standard thermal zone device callbacks
  613. *
  614. * thermal_zone_device_unregister() must be called when the device is no
  615. * longer needed.
  616. */
  617. struct thermal_zone_device *thermal_zone_device_register(char *type,
  618. int trips,
  619. void *devdata, struct
  620. thermal_zone_device_ops
  621. *ops)
  622. {
  623. struct thermal_zone_device *tz;
  624. struct thermal_cooling_device *pos;
  625. int result;
  626. int count;
  627. if (strlen(type) >= THERMAL_NAME_LENGTH)
  628. return ERR_PTR(-EINVAL);
  629. if (trips > THERMAL_MAX_TRIPS || trips < 0)
  630. return ERR_PTR(-EINVAL);
  631. if (!ops || !ops->get_temp)
  632. return ERR_PTR(-EINVAL);
  633. tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
  634. if (!tz)
  635. return ERR_PTR(-ENOMEM);
  636. INIT_LIST_HEAD(&tz->cooling_devices);
  637. idr_init(&tz->idr);
  638. mutex_init(&tz->lock);
  639. result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
  640. if (result) {
  641. kfree(tz);
  642. return ERR_PTR(result);
  643. }
  644. strcpy(tz->type, type);
  645. tz->ops = ops;
  646. tz->device.class = &thermal_class;
  647. tz->devdata = devdata;
  648. tz->trips = trips;
  649. sprintf(tz->device.bus_id, "thermal_zone%d", tz->id);
  650. result = device_register(&tz->device);
  651. if (result) {
  652. release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
  653. kfree(tz);
  654. return ERR_PTR(result);
  655. }
  656. /* sys I/F */
  657. if (type) {
  658. result = device_create_file(&tz->device, &dev_attr_type);
  659. if (result)
  660. goto unregister;
  661. }
  662. result = device_create_file(&tz->device, &dev_attr_temp);
  663. if (result)
  664. goto unregister;
  665. if (ops->get_mode) {
  666. result = device_create_file(&tz->device, &dev_attr_mode);
  667. if (result)
  668. goto unregister;
  669. }
  670. for (count = 0; count < trips; count++) {
  671. TRIP_POINT_ATTR_ADD(&tz->device, count, result);
  672. if (result)
  673. goto unregister;
  674. }
  675. result = thermal_add_hwmon_sysfs(tz);
  676. if (result)
  677. goto unregister;
  678. mutex_lock(&thermal_list_lock);
  679. list_add_tail(&tz->node, &thermal_tz_list);
  680. if (ops->bind)
  681. list_for_each_entry(pos, &thermal_cdev_list, node) {
  682. result = ops->bind(tz, pos);
  683. if (result)
  684. break;
  685. }
  686. mutex_unlock(&thermal_list_lock);
  687. if (!result)
  688. return tz;
  689. unregister:
  690. release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
  691. device_unregister(&tz->device);
  692. return ERR_PTR(result);
  693. }
  694. EXPORT_SYMBOL(thermal_zone_device_register);
  695. /**
  696. * thermal_device_unregister - removes the registered thermal zone device
  697. * @tz: the thermal zone device to remove
  698. */
  699. void thermal_zone_device_unregister(struct thermal_zone_device *tz)
  700. {
  701. struct thermal_cooling_device *cdev;
  702. struct thermal_zone_device *pos = NULL;
  703. int count;
  704. if (!tz)
  705. return;
  706. mutex_lock(&thermal_list_lock);
  707. list_for_each_entry(pos, &thermal_tz_list, node)
  708. if (pos == tz)
  709. break;
  710. if (pos != tz) {
  711. /* thermal zone device not found */
  712. mutex_unlock(&thermal_list_lock);
  713. return;
  714. }
  715. list_del(&tz->node);
  716. if (tz->ops->unbind)
  717. list_for_each_entry(cdev, &thermal_cdev_list, node)
  718. tz->ops->unbind(tz, cdev);
  719. mutex_unlock(&thermal_list_lock);
  720. if (tz->type[0])
  721. device_remove_file(&tz->device, &dev_attr_type);
  722. device_remove_file(&tz->device, &dev_attr_temp);
  723. if (tz->ops->get_mode)
  724. device_remove_file(&tz->device, &dev_attr_mode);
  725. for (count = 0; count < tz->trips; count++)
  726. TRIP_POINT_ATTR_REMOVE(&tz->device, count);
  727. thermal_remove_hwmon_sysfs(tz);
  728. release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
  729. idr_destroy(&tz->idr);
  730. mutex_destroy(&tz->lock);
  731. device_unregister(&tz->device);
  732. return;
  733. }
  734. EXPORT_SYMBOL(thermal_zone_device_unregister);
  735. static int __init thermal_init(void)
  736. {
  737. int result = 0;
  738. result = class_register(&thermal_class);
  739. if (result) {
  740. idr_destroy(&thermal_tz_idr);
  741. idr_destroy(&thermal_cdev_idr);
  742. mutex_destroy(&thermal_idr_lock);
  743. mutex_destroy(&thermal_list_lock);
  744. }
  745. return result;
  746. }
  747. static void __exit thermal_exit(void)
  748. {
  749. class_unregister(&thermal_class);
  750. idr_destroy(&thermal_tz_idr);
  751. idr_destroy(&thermal_cdev_idr);
  752. mutex_destroy(&thermal_idr_lock);
  753. mutex_destroy(&thermal_list_lock);
  754. }
  755. subsys_initcall(thermal_init);
  756. module_exit(thermal_exit);