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