Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 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 | |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/device.h> |
| 28 | #include <linux/err.h> |
| 29 | #include <linux/kdev_t.h> |
| 30 | #include <linux/idr.h> |
| 31 | #include <linux/thermal.h> |
| 32 | #include <linux/spinlock.h> |
| 33 | |
| 34 | MODULE_AUTHOR("Zhang Rui") |
| 35 | MODULE_DESCRIPTION("Generic thermal management sysfs support"); |
| 36 | MODULE_LICENSE("GPL"); |
| 37 | |
| 38 | #define PREFIX "Thermal: " |
| 39 | |
| 40 | struct thermal_cooling_device_instance { |
| 41 | int id; |
| 42 | char name[THERMAL_NAME_LENGTH]; |
| 43 | struct thermal_zone_device *tz; |
| 44 | struct thermal_cooling_device *cdev; |
| 45 | int trip; |
| 46 | char attr_name[THERMAL_NAME_LENGTH]; |
| 47 | struct device_attribute attr; |
| 48 | struct list_head node; |
| 49 | }; |
| 50 | |
| 51 | static DEFINE_IDR(thermal_tz_idr); |
| 52 | static DEFINE_IDR(thermal_cdev_idr); |
| 53 | static DEFINE_MUTEX(thermal_idr_lock); |
| 54 | |
| 55 | static LIST_HEAD(thermal_tz_list); |
| 56 | static LIST_HEAD(thermal_cdev_list); |
| 57 | static DEFINE_MUTEX(thermal_list_lock); |
| 58 | |
| 59 | static int get_idr(struct idr *idr, struct mutex *lock, int *id) |
| 60 | { |
| 61 | int err; |
| 62 | |
| 63 | again: |
| 64 | if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0)) |
| 65 | return -ENOMEM; |
| 66 | |
| 67 | if (lock) |
| 68 | mutex_lock(lock); |
| 69 | err = idr_get_new(idr, NULL, id); |
| 70 | if (lock) |
| 71 | mutex_unlock(lock); |
| 72 | if (unlikely(err == -EAGAIN)) |
| 73 | goto again; |
| 74 | else if (unlikely(err)) |
| 75 | return err; |
| 76 | |
| 77 | *id = *id & MAX_ID_MASK; |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static void release_idr(struct idr *idr, struct mutex *lock, int id) |
| 82 | { |
| 83 | if (lock) |
| 84 | mutex_lock(lock); |
| 85 | idr_remove(idr, id); |
| 86 | if (lock) |
| 87 | mutex_unlock(lock); |
| 88 | } |
| 89 | |
| 90 | /* sys I/F for thermal zone */ |
| 91 | |
| 92 | #define to_thermal_zone(_dev) \ |
| 93 | container_of(_dev, struct thermal_zone_device, device) |
| 94 | |
| 95 | static ssize_t |
| 96 | type_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 97 | { |
| 98 | struct thermal_zone_device *tz = to_thermal_zone(dev); |
| 99 | |
| 100 | return sprintf(buf, "%s\n", tz->type); |
| 101 | } |
| 102 | |
| 103 | static ssize_t |
| 104 | temp_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 105 | { |
| 106 | struct thermal_zone_device *tz = to_thermal_zone(dev); |
| 107 | |
| 108 | if (!tz->ops->get_temp) |
| 109 | return -EPERM; |
| 110 | |
| 111 | return tz->ops->get_temp(tz, buf); |
| 112 | } |
| 113 | |
| 114 | static ssize_t |
| 115 | mode_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 116 | { |
| 117 | struct thermal_zone_device *tz = to_thermal_zone(dev); |
| 118 | |
| 119 | if (!tz->ops->get_mode) |
| 120 | return -EPERM; |
| 121 | |
| 122 | return tz->ops->get_mode(tz, buf); |
| 123 | } |
| 124 | |
| 125 | static ssize_t |
| 126 | mode_store(struct device *dev, struct device_attribute *attr, |
| 127 | const char *buf, size_t count) |
| 128 | { |
| 129 | struct thermal_zone_device *tz = to_thermal_zone(dev); |
| 130 | int result; |
| 131 | |
| 132 | if (!tz->ops->set_mode) |
| 133 | return -EPERM; |
| 134 | |
| 135 | result = tz->ops->set_mode(tz, buf); |
| 136 | if (result) |
| 137 | return result; |
| 138 | |
| 139 | return count; |
| 140 | } |
| 141 | |
| 142 | static ssize_t |
| 143 | trip_point_type_show(struct device *dev, struct device_attribute *attr, |
| 144 | char *buf) |
| 145 | { |
| 146 | struct thermal_zone_device *tz = to_thermal_zone(dev); |
| 147 | int trip; |
| 148 | |
| 149 | if (!tz->ops->get_trip_type) |
| 150 | return -EPERM; |
| 151 | |
| 152 | if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip)) |
| 153 | return -EINVAL; |
| 154 | |
| 155 | return tz->ops->get_trip_type(tz, trip, buf); |
| 156 | } |
| 157 | |
| 158 | static ssize_t |
| 159 | trip_point_temp_show(struct device *dev, struct device_attribute *attr, |
| 160 | char *buf) |
| 161 | { |
| 162 | struct thermal_zone_device *tz = to_thermal_zone(dev); |
| 163 | int trip; |
| 164 | |
| 165 | if (!tz->ops->get_trip_temp) |
| 166 | return -EPERM; |
| 167 | |
| 168 | if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip)) |
| 169 | return -EINVAL; |
| 170 | |
| 171 | return tz->ops->get_trip_temp(tz, trip, buf); |
| 172 | } |
| 173 | |
| 174 | static DEVICE_ATTR(type, 0444, type_show, NULL); |
| 175 | static DEVICE_ATTR(temp, 0444, temp_show, NULL); |
| 176 | static DEVICE_ATTR(mode, 0644, mode_show, mode_store); |
| 177 | |
| 178 | static struct device_attribute trip_point_attrs[] = { |
| 179 | __ATTR(trip_point_0_type, 0444, trip_point_type_show, NULL), |
| 180 | __ATTR(trip_point_0_temp, 0444, trip_point_temp_show, NULL), |
| 181 | __ATTR(trip_point_1_type, 0444, trip_point_type_show, NULL), |
| 182 | __ATTR(trip_point_1_temp, 0444, trip_point_temp_show, NULL), |
| 183 | __ATTR(trip_point_2_type, 0444, trip_point_type_show, NULL), |
| 184 | __ATTR(trip_point_2_temp, 0444, trip_point_temp_show, NULL), |
| 185 | __ATTR(trip_point_3_type, 0444, trip_point_type_show, NULL), |
| 186 | __ATTR(trip_point_3_temp, 0444, trip_point_temp_show, NULL), |
| 187 | __ATTR(trip_point_4_type, 0444, trip_point_type_show, NULL), |
| 188 | __ATTR(trip_point_4_temp, 0444, trip_point_temp_show, NULL), |
| 189 | __ATTR(trip_point_5_type, 0444, trip_point_type_show, NULL), |
| 190 | __ATTR(trip_point_5_temp, 0444, trip_point_temp_show, NULL), |
| 191 | __ATTR(trip_point_6_type, 0444, trip_point_type_show, NULL), |
| 192 | __ATTR(trip_point_6_temp, 0444, trip_point_temp_show, NULL), |
| 193 | __ATTR(trip_point_7_type, 0444, trip_point_type_show, NULL), |
| 194 | __ATTR(trip_point_7_temp, 0444, trip_point_temp_show, NULL), |
| 195 | __ATTR(trip_point_8_type, 0444, trip_point_type_show, NULL), |
| 196 | __ATTR(trip_point_8_temp, 0444, trip_point_temp_show, NULL), |
| 197 | __ATTR(trip_point_9_type, 0444, trip_point_type_show, NULL), |
| 198 | __ATTR(trip_point_9_temp, 0444, trip_point_temp_show, NULL), |
| 199 | }; |
| 200 | |
| 201 | #define TRIP_POINT_ATTR_ADD(_dev, _index, result) \ |
| 202 | do { \ |
| 203 | result = device_create_file(_dev, \ |
| 204 | &trip_point_attrs[_index * 2]); \ |
| 205 | if (result) \ |
| 206 | break; \ |
| 207 | result = device_create_file(_dev, \ |
| 208 | &trip_point_attrs[_index * 2 + 1]); \ |
| 209 | } while (0) |
| 210 | |
| 211 | #define TRIP_POINT_ATTR_REMOVE(_dev, _index) \ |
| 212 | do { \ |
| 213 | device_remove_file(_dev, &trip_point_attrs[_index * 2]); \ |
| 214 | device_remove_file(_dev, &trip_point_attrs[_index * 2 + 1]); \ |
| 215 | } while (0) |
| 216 | |
| 217 | /* sys I/F for cooling device */ |
| 218 | #define to_cooling_device(_dev) \ |
| 219 | container_of(_dev, struct thermal_cooling_device, device) |
| 220 | |
| 221 | static ssize_t |
| 222 | thermal_cooling_device_type_show(struct device *dev, |
| 223 | struct device_attribute *attr, char *buf) |
| 224 | { |
| 225 | struct thermal_cooling_device *cdev = to_cooling_device(dev); |
| 226 | |
| 227 | return sprintf(buf, "%s\n", cdev->type); |
| 228 | } |
| 229 | |
| 230 | static ssize_t |
| 231 | thermal_cooling_device_max_state_show(struct device *dev, |
| 232 | struct device_attribute *attr, char *buf) |
| 233 | { |
| 234 | struct thermal_cooling_device *cdev = to_cooling_device(dev); |
| 235 | |
| 236 | return cdev->ops->get_max_state(cdev, buf); |
| 237 | } |
| 238 | |
| 239 | static ssize_t |
| 240 | thermal_cooling_device_cur_state_show(struct device *dev, |
| 241 | struct device_attribute *attr, char *buf) |
| 242 | { |
| 243 | struct thermal_cooling_device *cdev = to_cooling_device(dev); |
| 244 | |
| 245 | return cdev->ops->get_cur_state(cdev, buf); |
| 246 | } |
| 247 | |
| 248 | static ssize_t |
| 249 | thermal_cooling_device_cur_state_store(struct device *dev, |
| 250 | struct device_attribute *attr, |
| 251 | const char *buf, size_t count) |
| 252 | { |
| 253 | struct thermal_cooling_device *cdev = to_cooling_device(dev); |
| 254 | int state; |
| 255 | int result; |
| 256 | |
| 257 | if (!sscanf(buf, "%d\n", &state)) |
| 258 | return -EINVAL; |
| 259 | |
| 260 | if (state < 0) |
| 261 | return -EINVAL; |
| 262 | |
| 263 | result = cdev->ops->set_cur_state(cdev, state); |
| 264 | if (result) |
| 265 | return result; |
| 266 | return count; |
| 267 | } |
| 268 | |
| 269 | static struct device_attribute dev_attr_cdev_type = |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 270 | __ATTR(type, 0444, thermal_cooling_device_type_show, NULL); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 271 | static DEVICE_ATTR(max_state, 0444, |
| 272 | thermal_cooling_device_max_state_show, NULL); |
| 273 | static DEVICE_ATTR(cur_state, 0644, |
| 274 | thermal_cooling_device_cur_state_show, |
| 275 | thermal_cooling_device_cur_state_store); |
| 276 | |
| 277 | static ssize_t |
| 278 | thermal_cooling_device_trip_point_show(struct device *dev, |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 279 | struct device_attribute *attr, char *buf) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 280 | { |
| 281 | struct thermal_cooling_device_instance *instance; |
| 282 | |
| 283 | instance = |
| 284 | container_of(attr, struct thermal_cooling_device_instance, attr); |
| 285 | |
| 286 | if (instance->trip == THERMAL_TRIPS_NONE) |
| 287 | return sprintf(buf, "-1\n"); |
| 288 | else |
| 289 | return sprintf(buf, "%d\n", instance->trip); |
| 290 | } |
| 291 | |
| 292 | /* Device management */ |
| 293 | |
| 294 | /** |
| 295 | * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 296 | * @tz: thermal zone device |
| 297 | * @trip: indicates which trip point the cooling devices is |
| 298 | * associated with in this thermal zone. |
| 299 | * @cdev: thermal cooling device |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 300 | * |
| 301 | * This function is usually called in the thermal zone device .bind callback. |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 302 | */ |
| 303 | int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz, |
| 304 | int trip, |
| 305 | struct thermal_cooling_device *cdev) |
| 306 | { |
| 307 | struct thermal_cooling_device_instance *dev; |
| 308 | struct thermal_cooling_device_instance *pos; |
| 309 | int result; |
| 310 | |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 311 | if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE)) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 312 | return -EINVAL; |
| 313 | |
| 314 | if (!tz || !cdev) |
| 315 | return -EINVAL; |
| 316 | |
| 317 | dev = |
| 318 | kzalloc(sizeof(struct thermal_cooling_device_instance), GFP_KERNEL); |
| 319 | if (!dev) |
| 320 | return -ENOMEM; |
| 321 | dev->tz = tz; |
| 322 | dev->cdev = cdev; |
| 323 | dev->trip = trip; |
| 324 | result = get_idr(&tz->idr, &tz->lock, &dev->id); |
| 325 | if (result) |
| 326 | goto free_mem; |
| 327 | |
| 328 | sprintf(dev->name, "cdev%d", dev->id); |
| 329 | result = |
| 330 | sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name); |
| 331 | if (result) |
| 332 | goto release_idr; |
| 333 | |
| 334 | sprintf(dev->attr_name, "cdev%d_trip_point", dev->id); |
| 335 | dev->attr.attr.name = dev->attr_name; |
| 336 | dev->attr.attr.mode = 0444; |
| 337 | dev->attr.show = thermal_cooling_device_trip_point_show; |
| 338 | result = device_create_file(&tz->device, &dev->attr); |
| 339 | if (result) |
| 340 | goto remove_symbol_link; |
| 341 | |
| 342 | mutex_lock(&tz->lock); |
| 343 | list_for_each_entry(pos, &tz->cooling_devices, node) |
| 344 | if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { |
| 345 | result = -EEXIST; |
| 346 | break; |
| 347 | } |
| 348 | if (!result) |
| 349 | list_add_tail(&dev->node, &tz->cooling_devices); |
| 350 | mutex_unlock(&tz->lock); |
| 351 | |
| 352 | if (!result) |
| 353 | return 0; |
| 354 | |
| 355 | device_remove_file(&tz->device, &dev->attr); |
| 356 | remove_symbol_link: |
| 357 | sysfs_remove_link(&tz->device.kobj, dev->name); |
| 358 | release_idr: |
| 359 | release_idr(&tz->idr, &tz->lock, dev->id); |
| 360 | free_mem: |
| 361 | kfree(dev); |
| 362 | return result; |
| 363 | } |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 364 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 365 | EXPORT_SYMBOL(thermal_zone_bind_cooling_device); |
| 366 | |
| 367 | /** |
| 368 | * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 369 | * @tz: thermal zone device |
| 370 | * @trip: indicates which trip point the cooling devices is |
| 371 | * associated with in this thermal zone. |
| 372 | * @cdev: thermal cooling device |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 373 | * |
| 374 | * This function is usually called in the thermal zone device .unbind callback. |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 375 | */ |
| 376 | int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz, |
| 377 | int trip, |
| 378 | struct thermal_cooling_device *cdev) |
| 379 | { |
| 380 | struct thermal_cooling_device_instance *pos, *next; |
| 381 | |
| 382 | mutex_lock(&tz->lock); |
| 383 | list_for_each_entry_safe(pos, next, &tz->cooling_devices, node) { |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 384 | if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 385 | list_del(&pos->node); |
| 386 | mutex_unlock(&tz->lock); |
| 387 | goto unbind; |
| 388 | } |
| 389 | } |
| 390 | mutex_unlock(&tz->lock); |
| 391 | |
| 392 | return -ENODEV; |
| 393 | |
| 394 | unbind: |
| 395 | device_remove_file(&tz->device, &pos->attr); |
| 396 | sysfs_remove_link(&tz->device.kobj, pos->name); |
| 397 | release_idr(&tz->idr, &tz->lock, pos->id); |
| 398 | kfree(pos); |
| 399 | return 0; |
| 400 | } |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 401 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 402 | EXPORT_SYMBOL(thermal_zone_unbind_cooling_device); |
| 403 | |
| 404 | static void thermal_release(struct device *dev) |
| 405 | { |
| 406 | struct thermal_zone_device *tz; |
| 407 | struct thermal_cooling_device *cdev; |
| 408 | |
| 409 | if (!strncmp(dev->bus_id, "thermal_zone", sizeof "thermal_zone" - 1)) { |
| 410 | tz = to_thermal_zone(dev); |
| 411 | kfree(tz); |
| 412 | } else { |
| 413 | cdev = to_cooling_device(dev); |
| 414 | kfree(cdev); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | static struct class thermal_class = { |
| 419 | .name = "thermal", |
| 420 | .dev_release = thermal_release, |
| 421 | }; |
| 422 | |
| 423 | /** |
| 424 | * thermal_cooling_device_register - register a new thermal cooling device |
| 425 | * @type: the thermal cooling device type. |
| 426 | * @devdata: device private data. |
| 427 | * @ops: standard thermal cooling devices callbacks. |
| 428 | */ |
| 429 | struct thermal_cooling_device *thermal_cooling_device_register(char *type, |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 430 | void *devdata, |
| 431 | struct |
| 432 | thermal_cooling_device_ops |
| 433 | *ops) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 434 | { |
| 435 | struct thermal_cooling_device *cdev; |
| 436 | struct thermal_zone_device *pos; |
| 437 | int result; |
| 438 | |
| 439 | if (strlen(type) >= THERMAL_NAME_LENGTH) |
| 440 | return NULL; |
| 441 | |
| 442 | if (!ops || !ops->get_max_state || !ops->get_cur_state || |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 443 | !ops->set_cur_state) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 444 | return NULL; |
| 445 | |
| 446 | cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL); |
| 447 | if (!cdev) |
| 448 | return NULL; |
| 449 | |
| 450 | result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id); |
| 451 | if (result) { |
| 452 | kfree(cdev); |
| 453 | return NULL; |
| 454 | } |
| 455 | |
| 456 | strcpy(cdev->type, type); |
| 457 | cdev->ops = ops; |
| 458 | cdev->device.class = &thermal_class; |
| 459 | cdev->devdata = devdata; |
| 460 | sprintf(cdev->device.bus_id, "cooling_device%d", cdev->id); |
| 461 | result = device_register(&cdev->device); |
| 462 | if (result) { |
| 463 | release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id); |
| 464 | kfree(cdev); |
| 465 | return NULL; |
| 466 | } |
| 467 | |
| 468 | /* sys I/F */ |
| 469 | if (type) { |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 470 | result = device_create_file(&cdev->device, &dev_attr_cdev_type); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 471 | if (result) |
| 472 | goto unregister; |
| 473 | } |
| 474 | |
| 475 | result = device_create_file(&cdev->device, &dev_attr_max_state); |
| 476 | if (result) |
| 477 | goto unregister; |
| 478 | |
| 479 | result = device_create_file(&cdev->device, &dev_attr_cur_state); |
| 480 | if (result) |
| 481 | goto unregister; |
| 482 | |
| 483 | mutex_lock(&thermal_list_lock); |
| 484 | list_add(&cdev->node, &thermal_cdev_list); |
| 485 | list_for_each_entry(pos, &thermal_tz_list, node) { |
| 486 | if (!pos->ops->bind) |
| 487 | continue; |
| 488 | result = pos->ops->bind(pos, cdev); |
| 489 | if (result) |
| 490 | break; |
| 491 | |
| 492 | } |
| 493 | mutex_unlock(&thermal_list_lock); |
| 494 | |
| 495 | if (!result) |
| 496 | return cdev; |
| 497 | |
| 498 | unregister: |
| 499 | release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id); |
| 500 | device_unregister(&cdev->device); |
| 501 | return NULL; |
| 502 | } |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 503 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 504 | EXPORT_SYMBOL(thermal_cooling_device_register); |
| 505 | |
| 506 | /** |
| 507 | * thermal_cooling_device_unregister - removes the registered thermal cooling device |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 508 | * @cdev: the thermal cooling device to remove. |
| 509 | * |
| 510 | * thermal_cooling_device_unregister() must be called when the device is no |
| 511 | * longer needed. |
| 512 | */ |
| 513 | void thermal_cooling_device_unregister(struct |
| 514 | thermal_cooling_device |
| 515 | *cdev) |
| 516 | { |
| 517 | struct thermal_zone_device *tz; |
| 518 | struct thermal_cooling_device *pos = NULL; |
| 519 | |
| 520 | if (!cdev) |
| 521 | return; |
| 522 | |
| 523 | mutex_lock(&thermal_list_lock); |
| 524 | list_for_each_entry(pos, &thermal_cdev_list, node) |
| 525 | if (pos == cdev) |
| 526 | break; |
| 527 | if (pos != cdev) { |
| 528 | /* thermal cooling device not found */ |
| 529 | mutex_unlock(&thermal_list_lock); |
| 530 | return; |
| 531 | } |
| 532 | list_del(&cdev->node); |
| 533 | list_for_each_entry(tz, &thermal_tz_list, node) { |
| 534 | if (!tz->ops->unbind) |
| 535 | continue; |
| 536 | tz->ops->unbind(tz, cdev); |
| 537 | } |
| 538 | mutex_unlock(&thermal_list_lock); |
| 539 | if (cdev->type[0]) |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 540 | device_remove_file(&cdev->device, &dev_attr_cdev_type); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 541 | device_remove_file(&cdev->device, &dev_attr_max_state); |
| 542 | device_remove_file(&cdev->device, &dev_attr_cur_state); |
| 543 | |
| 544 | release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id); |
| 545 | device_unregister(&cdev->device); |
| 546 | return; |
| 547 | } |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 548 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 549 | EXPORT_SYMBOL(thermal_cooling_device_unregister); |
| 550 | |
| 551 | /** |
| 552 | * thermal_zone_device_register - register a new thermal zone device |
| 553 | * @type: the thermal zone device type |
| 554 | * @trips: the number of trip points the thermal zone support |
| 555 | * @devdata: private device data |
| 556 | * @ops: standard thermal zone device callbacks |
| 557 | * |
| 558 | * thermal_zone_device_unregister() must be called when the device is no |
| 559 | * longer needed. |
| 560 | */ |
| 561 | struct thermal_zone_device *thermal_zone_device_register(char *type, |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 562 | int trips, |
| 563 | void *devdata, struct |
| 564 | thermal_zone_device_ops |
| 565 | *ops) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 566 | { |
| 567 | struct thermal_zone_device *tz; |
| 568 | struct thermal_cooling_device *pos; |
| 569 | int result; |
| 570 | int count; |
| 571 | |
| 572 | if (strlen(type) >= THERMAL_NAME_LENGTH) |
| 573 | return NULL; |
| 574 | |
| 575 | if (trips > THERMAL_MAX_TRIPS || trips < 0) |
| 576 | return NULL; |
| 577 | |
| 578 | if (!ops || !ops->get_temp) |
| 579 | return NULL; |
| 580 | |
| 581 | tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL); |
| 582 | if (!tz) |
| 583 | return NULL; |
| 584 | |
| 585 | INIT_LIST_HEAD(&tz->cooling_devices); |
| 586 | idr_init(&tz->idr); |
| 587 | mutex_init(&tz->lock); |
| 588 | result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id); |
| 589 | if (result) { |
| 590 | kfree(tz); |
| 591 | return NULL; |
| 592 | } |
| 593 | |
| 594 | strcpy(tz->type, type); |
| 595 | tz->ops = ops; |
| 596 | tz->device.class = &thermal_class; |
| 597 | tz->devdata = devdata; |
| 598 | tz->trips = trips; |
| 599 | sprintf(tz->device.bus_id, "thermal_zone%d", tz->id); |
| 600 | result = device_register(&tz->device); |
| 601 | if (result) { |
| 602 | release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id); |
| 603 | kfree(tz); |
| 604 | return NULL; |
| 605 | } |
| 606 | |
| 607 | /* sys I/F */ |
| 608 | if (type) { |
| 609 | result = device_create_file(&tz->device, &dev_attr_type); |
| 610 | if (result) |
| 611 | goto unregister; |
| 612 | } |
| 613 | |
| 614 | result = device_create_file(&tz->device, &dev_attr_temp); |
| 615 | if (result) |
| 616 | goto unregister; |
| 617 | |
| 618 | if (ops->get_mode) { |
| 619 | result = device_create_file(&tz->device, &dev_attr_mode); |
| 620 | if (result) |
| 621 | goto unregister; |
| 622 | } |
| 623 | |
| 624 | for (count = 0; count < trips; count++) { |
| 625 | TRIP_POINT_ATTR_ADD(&tz->device, count, result); |
| 626 | if (result) |
| 627 | goto unregister; |
| 628 | } |
| 629 | |
| 630 | mutex_lock(&thermal_list_lock); |
| 631 | list_add_tail(&tz->node, &thermal_tz_list); |
| 632 | if (ops->bind) |
| 633 | list_for_each_entry(pos, &thermal_cdev_list, node) { |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 634 | result = ops->bind(tz, pos); |
| 635 | if (result) |
| 636 | break; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 637 | } |
| 638 | mutex_unlock(&thermal_list_lock); |
| 639 | |
| 640 | if (!result) |
| 641 | return tz; |
| 642 | |
| 643 | unregister: |
| 644 | release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id); |
| 645 | device_unregister(&tz->device); |
| 646 | return NULL; |
| 647 | } |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 648 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 649 | EXPORT_SYMBOL(thermal_zone_device_register); |
| 650 | |
| 651 | /** |
| 652 | * thermal_device_unregister - removes the registered thermal zone device |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 653 | * @tz: the thermal zone device to remove |
| 654 | */ |
| 655 | void thermal_zone_device_unregister(struct thermal_zone_device *tz) |
| 656 | { |
| 657 | struct thermal_cooling_device *cdev; |
| 658 | struct thermal_zone_device *pos = NULL; |
| 659 | int count; |
| 660 | |
| 661 | if (!tz) |
| 662 | return; |
| 663 | |
| 664 | mutex_lock(&thermal_list_lock); |
| 665 | list_for_each_entry(pos, &thermal_tz_list, node) |
| 666 | if (pos == tz) |
| 667 | break; |
| 668 | if (pos != tz) { |
| 669 | /* thermal zone device not found */ |
| 670 | mutex_unlock(&thermal_list_lock); |
| 671 | return; |
| 672 | } |
| 673 | list_del(&tz->node); |
| 674 | if (tz->ops->unbind) |
| 675 | list_for_each_entry(cdev, &thermal_cdev_list, node) |
| 676 | tz->ops->unbind(tz, cdev); |
| 677 | mutex_unlock(&thermal_list_lock); |
| 678 | |
| 679 | if (tz->type[0]) |
| 680 | device_remove_file(&tz->device, &dev_attr_type); |
| 681 | device_remove_file(&tz->device, &dev_attr_temp); |
| 682 | if (tz->ops->get_mode) |
| 683 | device_remove_file(&tz->device, &dev_attr_mode); |
| 684 | |
| 685 | for (count = 0; count < tz->trips; count++) |
| 686 | TRIP_POINT_ATTR_REMOVE(&tz->device, count); |
| 687 | |
| 688 | release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id); |
| 689 | idr_destroy(&tz->idr); |
| 690 | mutex_destroy(&tz->lock); |
| 691 | device_unregister(&tz->device); |
| 692 | return; |
| 693 | } |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 694 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 695 | EXPORT_SYMBOL(thermal_zone_device_unregister); |
| 696 | |
| 697 | static int __init thermal_init(void) |
| 698 | { |
| 699 | int result = 0; |
| 700 | |
| 701 | result = class_register(&thermal_class); |
| 702 | if (result) { |
| 703 | idr_destroy(&thermal_tz_idr); |
| 704 | idr_destroy(&thermal_cdev_idr); |
| 705 | mutex_destroy(&thermal_idr_lock); |
| 706 | mutex_destroy(&thermal_list_lock); |
| 707 | } |
| 708 | return result; |
| 709 | } |
| 710 | |
| 711 | static void __exit thermal_exit(void) |
| 712 | { |
| 713 | class_unregister(&thermal_class); |
| 714 | idr_destroy(&thermal_tz_idr); |
| 715 | idr_destroy(&thermal_cdev_idr); |
| 716 | mutex_destroy(&thermal_idr_lock); |
| 717 | mutex_destroy(&thermal_list_lock); |
| 718 | } |
| 719 | |
| 720 | subsys_initcall(thermal_init); |
| 721 | module_exit(thermal_exit); |