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; |
Thomas Sujith | c751670 | 2008-02-15 00:58:50 -0500 | [diff] [blame] | 309 | struct thermal_zone_device *pos1; |
| 310 | struct thermal_cooling_device *pos2; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 311 | int result; |
| 312 | |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 313 | if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE)) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 314 | return -EINVAL; |
| 315 | |
Thomas Sujith | c751670 | 2008-02-15 00:58:50 -0500 | [diff] [blame] | 316 | list_for_each_entry(pos1, &thermal_tz_list, node) { |
| 317 | if (pos1 == tz) |
| 318 | break; |
| 319 | } |
| 320 | list_for_each_entry(pos2, &thermal_cdev_list, node) { |
| 321 | if (pos2 == cdev) |
| 322 | break; |
| 323 | } |
| 324 | |
| 325 | if (tz != pos1 || cdev != pos2) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 326 | return -EINVAL; |
| 327 | |
| 328 | dev = |
| 329 | kzalloc(sizeof(struct thermal_cooling_device_instance), GFP_KERNEL); |
| 330 | if (!dev) |
| 331 | return -ENOMEM; |
| 332 | dev->tz = tz; |
| 333 | dev->cdev = cdev; |
| 334 | dev->trip = trip; |
| 335 | result = get_idr(&tz->idr, &tz->lock, &dev->id); |
| 336 | if (result) |
| 337 | goto free_mem; |
| 338 | |
| 339 | sprintf(dev->name, "cdev%d", dev->id); |
| 340 | result = |
| 341 | sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name); |
| 342 | if (result) |
| 343 | goto release_idr; |
| 344 | |
| 345 | sprintf(dev->attr_name, "cdev%d_trip_point", dev->id); |
| 346 | dev->attr.attr.name = dev->attr_name; |
| 347 | dev->attr.attr.mode = 0444; |
| 348 | dev->attr.show = thermal_cooling_device_trip_point_show; |
| 349 | result = device_create_file(&tz->device, &dev->attr); |
| 350 | if (result) |
| 351 | goto remove_symbol_link; |
| 352 | |
| 353 | mutex_lock(&tz->lock); |
| 354 | list_for_each_entry(pos, &tz->cooling_devices, node) |
| 355 | if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { |
| 356 | result = -EEXIST; |
| 357 | break; |
| 358 | } |
| 359 | if (!result) |
| 360 | list_add_tail(&dev->node, &tz->cooling_devices); |
| 361 | mutex_unlock(&tz->lock); |
| 362 | |
| 363 | if (!result) |
| 364 | return 0; |
| 365 | |
| 366 | device_remove_file(&tz->device, &dev->attr); |
| 367 | remove_symbol_link: |
| 368 | sysfs_remove_link(&tz->device.kobj, dev->name); |
| 369 | release_idr: |
| 370 | release_idr(&tz->idr, &tz->lock, dev->id); |
| 371 | free_mem: |
| 372 | kfree(dev); |
| 373 | return result; |
| 374 | } |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 375 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 376 | EXPORT_SYMBOL(thermal_zone_bind_cooling_device); |
| 377 | |
| 378 | /** |
| 379 | * 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] | 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 |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 384 | * |
| 385 | * This function is usually called in the thermal zone device .unbind callback. |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 386 | */ |
| 387 | int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz, |
| 388 | int trip, |
| 389 | struct thermal_cooling_device *cdev) |
| 390 | { |
| 391 | struct thermal_cooling_device_instance *pos, *next; |
| 392 | |
| 393 | mutex_lock(&tz->lock); |
| 394 | list_for_each_entry_safe(pos, next, &tz->cooling_devices, node) { |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 395 | if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 396 | list_del(&pos->node); |
| 397 | mutex_unlock(&tz->lock); |
| 398 | goto unbind; |
| 399 | } |
| 400 | } |
| 401 | mutex_unlock(&tz->lock); |
| 402 | |
| 403 | return -ENODEV; |
| 404 | |
| 405 | unbind: |
| 406 | device_remove_file(&tz->device, &pos->attr); |
| 407 | sysfs_remove_link(&tz->device.kobj, pos->name); |
| 408 | release_idr(&tz->idr, &tz->lock, pos->id); |
| 409 | kfree(pos); |
| 410 | return 0; |
| 411 | } |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 412 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 413 | EXPORT_SYMBOL(thermal_zone_unbind_cooling_device); |
| 414 | |
| 415 | static void thermal_release(struct device *dev) |
| 416 | { |
| 417 | struct thermal_zone_device *tz; |
| 418 | struct thermal_cooling_device *cdev; |
| 419 | |
| 420 | if (!strncmp(dev->bus_id, "thermal_zone", sizeof "thermal_zone" - 1)) { |
| 421 | tz = to_thermal_zone(dev); |
| 422 | kfree(tz); |
| 423 | } else { |
| 424 | cdev = to_cooling_device(dev); |
| 425 | kfree(cdev); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | static struct class thermal_class = { |
| 430 | .name = "thermal", |
| 431 | .dev_release = thermal_release, |
| 432 | }; |
| 433 | |
| 434 | /** |
| 435 | * thermal_cooling_device_register - register a new thermal cooling device |
| 436 | * @type: the thermal cooling device type. |
| 437 | * @devdata: device private data. |
| 438 | * @ops: standard thermal cooling devices callbacks. |
| 439 | */ |
| 440 | struct thermal_cooling_device *thermal_cooling_device_register(char *type, |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 441 | void *devdata, |
| 442 | struct |
| 443 | thermal_cooling_device_ops |
| 444 | *ops) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 445 | { |
| 446 | struct thermal_cooling_device *cdev; |
| 447 | struct thermal_zone_device *pos; |
| 448 | int result; |
| 449 | |
| 450 | if (strlen(type) >= THERMAL_NAME_LENGTH) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 451 | return ERR_PTR(-EINVAL); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 452 | |
| 453 | if (!ops || !ops->get_max_state || !ops->get_cur_state || |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 454 | !ops->set_cur_state) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 455 | return ERR_PTR(-EINVAL); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 456 | |
| 457 | cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL); |
| 458 | if (!cdev) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 459 | return ERR_PTR(-ENOMEM); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 460 | |
| 461 | result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id); |
| 462 | if (result) { |
| 463 | kfree(cdev); |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 464 | return ERR_PTR(result); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | strcpy(cdev->type, type); |
| 468 | cdev->ops = ops; |
| 469 | cdev->device.class = &thermal_class; |
| 470 | cdev->devdata = devdata; |
| 471 | sprintf(cdev->device.bus_id, "cooling_device%d", cdev->id); |
| 472 | result = device_register(&cdev->device); |
| 473 | if (result) { |
| 474 | release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id); |
| 475 | kfree(cdev); |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 476 | return ERR_PTR(result); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | /* sys I/F */ |
| 480 | if (type) { |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 481 | result = device_create_file(&cdev->device, &dev_attr_cdev_type); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 482 | if (result) |
| 483 | goto unregister; |
| 484 | } |
| 485 | |
| 486 | result = device_create_file(&cdev->device, &dev_attr_max_state); |
| 487 | if (result) |
| 488 | goto unregister; |
| 489 | |
| 490 | result = device_create_file(&cdev->device, &dev_attr_cur_state); |
| 491 | if (result) |
| 492 | goto unregister; |
| 493 | |
| 494 | mutex_lock(&thermal_list_lock); |
| 495 | list_add(&cdev->node, &thermal_cdev_list); |
| 496 | list_for_each_entry(pos, &thermal_tz_list, node) { |
| 497 | if (!pos->ops->bind) |
| 498 | continue; |
| 499 | result = pos->ops->bind(pos, cdev); |
| 500 | if (result) |
| 501 | break; |
| 502 | |
| 503 | } |
| 504 | mutex_unlock(&thermal_list_lock); |
| 505 | |
| 506 | if (!result) |
| 507 | return cdev; |
| 508 | |
| 509 | unregister: |
| 510 | release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id); |
| 511 | device_unregister(&cdev->device); |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 512 | return ERR_PTR(result); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 513 | } |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 514 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 515 | EXPORT_SYMBOL(thermal_cooling_device_register); |
| 516 | |
| 517 | /** |
| 518 | * thermal_cooling_device_unregister - removes the registered thermal cooling device |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 519 | * @cdev: the thermal cooling device to remove. |
| 520 | * |
| 521 | * thermal_cooling_device_unregister() must be called when the device is no |
| 522 | * longer needed. |
| 523 | */ |
| 524 | void thermal_cooling_device_unregister(struct |
| 525 | thermal_cooling_device |
| 526 | *cdev) |
| 527 | { |
| 528 | struct thermal_zone_device *tz; |
| 529 | struct thermal_cooling_device *pos = NULL; |
| 530 | |
| 531 | if (!cdev) |
| 532 | return; |
| 533 | |
| 534 | mutex_lock(&thermal_list_lock); |
| 535 | list_for_each_entry(pos, &thermal_cdev_list, node) |
| 536 | if (pos == cdev) |
| 537 | break; |
| 538 | if (pos != cdev) { |
| 539 | /* thermal cooling device not found */ |
| 540 | mutex_unlock(&thermal_list_lock); |
| 541 | return; |
| 542 | } |
| 543 | list_del(&cdev->node); |
| 544 | list_for_each_entry(tz, &thermal_tz_list, node) { |
| 545 | if (!tz->ops->unbind) |
| 546 | continue; |
| 547 | tz->ops->unbind(tz, cdev); |
| 548 | } |
| 549 | mutex_unlock(&thermal_list_lock); |
| 550 | if (cdev->type[0]) |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 551 | device_remove_file(&cdev->device, &dev_attr_cdev_type); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 552 | device_remove_file(&cdev->device, &dev_attr_max_state); |
| 553 | device_remove_file(&cdev->device, &dev_attr_cur_state); |
| 554 | |
| 555 | release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id); |
| 556 | device_unregister(&cdev->device); |
| 557 | return; |
| 558 | } |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 559 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 560 | EXPORT_SYMBOL(thermal_cooling_device_unregister); |
| 561 | |
| 562 | /** |
| 563 | * thermal_zone_device_register - register a new thermal zone device |
| 564 | * @type: the thermal zone device type |
| 565 | * @trips: the number of trip points the thermal zone support |
| 566 | * @devdata: private device data |
| 567 | * @ops: standard thermal zone device callbacks |
| 568 | * |
| 569 | * thermal_zone_device_unregister() must be called when the device is no |
| 570 | * longer needed. |
| 571 | */ |
| 572 | struct thermal_zone_device *thermal_zone_device_register(char *type, |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 573 | int trips, |
| 574 | void *devdata, struct |
| 575 | thermal_zone_device_ops |
| 576 | *ops) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 577 | { |
| 578 | struct thermal_zone_device *tz; |
| 579 | struct thermal_cooling_device *pos; |
| 580 | int result; |
| 581 | int count; |
| 582 | |
| 583 | if (strlen(type) >= THERMAL_NAME_LENGTH) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 584 | return ERR_PTR(-EINVAL); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 585 | |
| 586 | if (trips > THERMAL_MAX_TRIPS || trips < 0) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 587 | return ERR_PTR(-EINVAL); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 588 | |
| 589 | if (!ops || !ops->get_temp) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 590 | return ERR_PTR(-EINVAL); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 591 | |
| 592 | tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL); |
| 593 | if (!tz) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 594 | return ERR_PTR(-ENOMEM); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 595 | |
| 596 | INIT_LIST_HEAD(&tz->cooling_devices); |
| 597 | idr_init(&tz->idr); |
| 598 | mutex_init(&tz->lock); |
| 599 | result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id); |
| 600 | if (result) { |
| 601 | kfree(tz); |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 602 | return ERR_PTR(result); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | strcpy(tz->type, type); |
| 606 | tz->ops = ops; |
| 607 | tz->device.class = &thermal_class; |
| 608 | tz->devdata = devdata; |
| 609 | tz->trips = trips; |
| 610 | sprintf(tz->device.bus_id, "thermal_zone%d", tz->id); |
| 611 | result = device_register(&tz->device); |
| 612 | if (result) { |
| 613 | release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id); |
| 614 | kfree(tz); |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 615 | return ERR_PTR(result); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | /* sys I/F */ |
| 619 | if (type) { |
| 620 | result = device_create_file(&tz->device, &dev_attr_type); |
| 621 | if (result) |
| 622 | goto unregister; |
| 623 | } |
| 624 | |
| 625 | result = device_create_file(&tz->device, &dev_attr_temp); |
| 626 | if (result) |
| 627 | goto unregister; |
| 628 | |
| 629 | if (ops->get_mode) { |
| 630 | result = device_create_file(&tz->device, &dev_attr_mode); |
| 631 | if (result) |
| 632 | goto unregister; |
| 633 | } |
| 634 | |
| 635 | for (count = 0; count < trips; count++) { |
| 636 | TRIP_POINT_ATTR_ADD(&tz->device, count, result); |
| 637 | if (result) |
| 638 | goto unregister; |
| 639 | } |
| 640 | |
| 641 | mutex_lock(&thermal_list_lock); |
| 642 | list_add_tail(&tz->node, &thermal_tz_list); |
| 643 | if (ops->bind) |
| 644 | list_for_each_entry(pos, &thermal_cdev_list, node) { |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 645 | result = ops->bind(tz, pos); |
| 646 | if (result) |
| 647 | break; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 648 | } |
| 649 | mutex_unlock(&thermal_list_lock); |
| 650 | |
| 651 | if (!result) |
| 652 | return tz; |
| 653 | |
| 654 | unregister: |
| 655 | release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id); |
| 656 | device_unregister(&tz->device); |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 657 | return ERR_PTR(result); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 658 | } |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 659 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 660 | EXPORT_SYMBOL(thermal_zone_device_register); |
| 661 | |
| 662 | /** |
| 663 | * thermal_device_unregister - removes the registered thermal zone device |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 664 | * @tz: the thermal zone device to remove |
| 665 | */ |
| 666 | void thermal_zone_device_unregister(struct thermal_zone_device *tz) |
| 667 | { |
| 668 | struct thermal_cooling_device *cdev; |
| 669 | struct thermal_zone_device *pos = NULL; |
| 670 | int count; |
| 671 | |
| 672 | if (!tz) |
| 673 | return; |
| 674 | |
| 675 | mutex_lock(&thermal_list_lock); |
| 676 | list_for_each_entry(pos, &thermal_tz_list, node) |
| 677 | if (pos == tz) |
| 678 | break; |
| 679 | if (pos != tz) { |
| 680 | /* thermal zone device not found */ |
| 681 | mutex_unlock(&thermal_list_lock); |
| 682 | return; |
| 683 | } |
| 684 | list_del(&tz->node); |
| 685 | if (tz->ops->unbind) |
| 686 | list_for_each_entry(cdev, &thermal_cdev_list, node) |
| 687 | tz->ops->unbind(tz, cdev); |
| 688 | mutex_unlock(&thermal_list_lock); |
| 689 | |
| 690 | if (tz->type[0]) |
| 691 | device_remove_file(&tz->device, &dev_attr_type); |
| 692 | device_remove_file(&tz->device, &dev_attr_temp); |
| 693 | if (tz->ops->get_mode) |
| 694 | device_remove_file(&tz->device, &dev_attr_mode); |
| 695 | |
| 696 | for (count = 0; count < tz->trips; count++) |
| 697 | TRIP_POINT_ATTR_REMOVE(&tz->device, count); |
| 698 | |
| 699 | release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id); |
| 700 | idr_destroy(&tz->idr); |
| 701 | mutex_destroy(&tz->lock); |
| 702 | device_unregister(&tz->device); |
| 703 | return; |
| 704 | } |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 705 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 706 | EXPORT_SYMBOL(thermal_zone_device_unregister); |
| 707 | |
| 708 | static int __init thermal_init(void) |
| 709 | { |
| 710 | int result = 0; |
| 711 | |
| 712 | result = class_register(&thermal_class); |
| 713 | if (result) { |
| 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 | return result; |
| 720 | } |
| 721 | |
| 722 | static void __exit thermal_exit(void) |
| 723 | { |
| 724 | class_unregister(&thermal_class); |
| 725 | idr_destroy(&thermal_tz_idr); |
| 726 | idr_destroy(&thermal_cdev_idr); |
| 727 | mutex_destroy(&thermal_idr_lock); |
| 728 | mutex_destroy(&thermal_list_lock); |
| 729 | } |
| 730 | |
| 731 | subsys_initcall(thermal_init); |
| 732 | module_exit(thermal_exit); |