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 | |
Joe Perches | c5a01dd | 2012-03-21 12:55:02 -0700 | [diff] [blame] | 26 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 27 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 28 | #include <linux/module.h> |
| 29 | #include <linux/device.h> |
| 30 | #include <linux/err.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 31 | #include <linux/slab.h> |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 32 | #include <linux/kdev_t.h> |
| 33 | #include <linux/idr.h> |
| 34 | #include <linux/thermal.h> |
| 35 | #include <linux/spinlock.h> |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 36 | #include <linux/reboot.h> |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 37 | #include <net/netlink.h> |
| 38 | #include <net/genetlink.h> |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 39 | |
Durgadoss R | 71350db | 2012-09-18 11:04:53 +0530 | [diff] [blame] | 40 | #include "thermal_core.h" |
| 41 | |
Zhang Rui | 63c4ec9 | 2008-04-21 16:07:13 +0800 | [diff] [blame] | 42 | MODULE_AUTHOR("Zhang Rui"); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 43 | MODULE_DESCRIPTION("Generic thermal management sysfs support"); |
| 44 | MODULE_LICENSE("GPL"); |
| 45 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 46 | static DEFINE_IDR(thermal_tz_idr); |
| 47 | static DEFINE_IDR(thermal_cdev_idr); |
| 48 | static DEFINE_MUTEX(thermal_idr_lock); |
| 49 | |
| 50 | static LIST_HEAD(thermal_tz_list); |
| 51 | static LIST_HEAD(thermal_cdev_list); |
Durgadoss R | a4a1548 | 2012-09-18 11:04:57 +0530 | [diff] [blame] | 52 | static LIST_HEAD(thermal_governor_list); |
| 53 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 54 | static DEFINE_MUTEX(thermal_list_lock); |
Durgadoss R | a4a1548 | 2012-09-18 11:04:57 +0530 | [diff] [blame] | 55 | static DEFINE_MUTEX(thermal_governor_lock); |
| 56 | |
| 57 | static struct thermal_governor *__find_governor(const char *name) |
| 58 | { |
| 59 | struct thermal_governor *pos; |
| 60 | |
| 61 | list_for_each_entry(pos, &thermal_governor_list, governor_list) |
| 62 | if (!strnicmp(name, pos->name, THERMAL_NAME_LENGTH)) |
| 63 | return pos; |
| 64 | |
| 65 | return NULL; |
| 66 | } |
| 67 | |
| 68 | int thermal_register_governor(struct thermal_governor *governor) |
| 69 | { |
| 70 | int err; |
| 71 | const char *name; |
| 72 | struct thermal_zone_device *pos; |
| 73 | |
| 74 | if (!governor) |
| 75 | return -EINVAL; |
| 76 | |
| 77 | mutex_lock(&thermal_governor_lock); |
| 78 | |
| 79 | err = -EBUSY; |
| 80 | if (__find_governor(governor->name) == NULL) { |
| 81 | err = 0; |
| 82 | list_add(&governor->governor_list, &thermal_governor_list); |
| 83 | } |
| 84 | |
| 85 | mutex_lock(&thermal_list_lock); |
| 86 | |
| 87 | list_for_each_entry(pos, &thermal_tz_list, node) { |
| 88 | if (pos->governor) |
| 89 | continue; |
| 90 | if (pos->tzp) |
| 91 | name = pos->tzp->governor_name; |
| 92 | else |
| 93 | name = DEFAULT_THERMAL_GOVERNOR; |
| 94 | if (!strnicmp(name, governor->name, THERMAL_NAME_LENGTH)) |
| 95 | pos->governor = governor; |
| 96 | } |
| 97 | |
| 98 | mutex_unlock(&thermal_list_lock); |
| 99 | mutex_unlock(&thermal_governor_lock); |
| 100 | |
| 101 | return err; |
| 102 | } |
| 103 | EXPORT_SYMBOL_GPL(thermal_register_governor); |
| 104 | |
| 105 | void thermal_unregister_governor(struct thermal_governor *governor) |
| 106 | { |
| 107 | struct thermal_zone_device *pos; |
| 108 | |
| 109 | if (!governor) |
| 110 | return; |
| 111 | |
| 112 | mutex_lock(&thermal_governor_lock); |
| 113 | |
| 114 | if (__find_governor(governor->name) == NULL) |
| 115 | goto exit; |
| 116 | |
| 117 | mutex_lock(&thermal_list_lock); |
| 118 | |
| 119 | list_for_each_entry(pos, &thermal_tz_list, node) { |
| 120 | if (!strnicmp(pos->governor->name, governor->name, |
| 121 | THERMAL_NAME_LENGTH)) |
| 122 | pos->governor = NULL; |
| 123 | } |
| 124 | |
| 125 | mutex_unlock(&thermal_list_lock); |
| 126 | list_del(&governor->governor_list); |
| 127 | exit: |
| 128 | mutex_unlock(&thermal_governor_lock); |
| 129 | return; |
| 130 | } |
| 131 | EXPORT_SYMBOL_GPL(thermal_unregister_governor); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 132 | |
| 133 | static int get_idr(struct idr *idr, struct mutex *lock, int *id) |
| 134 | { |
| 135 | int err; |
| 136 | |
Joe Perches | caca8b8 | 2012-03-21 12:55:02 -0700 | [diff] [blame] | 137 | again: |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 138 | if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0)) |
| 139 | return -ENOMEM; |
| 140 | |
| 141 | if (lock) |
| 142 | mutex_lock(lock); |
| 143 | err = idr_get_new(idr, NULL, id); |
| 144 | if (lock) |
| 145 | mutex_unlock(lock); |
| 146 | if (unlikely(err == -EAGAIN)) |
| 147 | goto again; |
| 148 | else if (unlikely(err)) |
| 149 | return err; |
| 150 | |
Fengguang Wu | 125c4c7 | 2012-10-04 17:13:15 -0700 | [diff] [blame] | 151 | *id = *id & MAX_IDR_MASK; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | static void release_idr(struct idr *idr, struct mutex *lock, int id) |
| 156 | { |
| 157 | if (lock) |
| 158 | mutex_lock(lock); |
| 159 | idr_remove(idr, id); |
| 160 | if (lock) |
| 161 | mutex_unlock(lock); |
| 162 | } |
| 163 | |
Durgadoss R | 9b4298a | 2012-09-18 11:04:54 +0530 | [diff] [blame] | 164 | int get_tz_trend(struct thermal_zone_device *tz, int trip) |
| 165 | { |
| 166 | enum thermal_trend trend; |
| 167 | |
| 168 | if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) { |
| 169 | if (tz->temperature > tz->last_temperature) |
| 170 | trend = THERMAL_TREND_RAISING; |
| 171 | else if (tz->temperature < tz->last_temperature) |
| 172 | trend = THERMAL_TREND_DROPPING; |
| 173 | else |
| 174 | trend = THERMAL_TREND_STABLE; |
| 175 | } |
| 176 | |
| 177 | return trend; |
| 178 | } |
| 179 | EXPORT_SYMBOL(get_tz_trend); |
| 180 | |
| 181 | struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz, |
| 182 | struct thermal_cooling_device *cdev, int trip) |
| 183 | { |
| 184 | struct thermal_instance *pos = NULL; |
| 185 | struct thermal_instance *target_instance = NULL; |
| 186 | |
| 187 | mutex_lock(&tz->lock); |
| 188 | mutex_lock(&cdev->lock); |
| 189 | |
| 190 | list_for_each_entry(pos, &tz->thermal_instances, tz_node) { |
| 191 | if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { |
| 192 | target_instance = pos; |
| 193 | break; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | mutex_unlock(&cdev->lock); |
| 198 | mutex_unlock(&tz->lock); |
| 199 | |
| 200 | return target_instance; |
| 201 | } |
| 202 | EXPORT_SYMBOL(get_thermal_instance); |
| 203 | |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 204 | static void print_bind_err_msg(struct thermal_zone_device *tz, |
| 205 | struct thermal_cooling_device *cdev, int ret) |
| 206 | { |
| 207 | dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n", |
| 208 | tz->type, cdev->type, ret); |
| 209 | } |
| 210 | |
| 211 | static void __bind(struct thermal_zone_device *tz, int mask, |
| 212 | struct thermal_cooling_device *cdev) |
| 213 | { |
| 214 | int i, ret; |
| 215 | |
| 216 | for (i = 0; i < tz->trips; i++) { |
| 217 | if (mask & (1 << i)) { |
| 218 | ret = thermal_zone_bind_cooling_device(tz, i, cdev, |
| 219 | THERMAL_NO_LIMIT, THERMAL_NO_LIMIT); |
| 220 | if (ret) |
| 221 | print_bind_err_msg(tz, cdev, ret); |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | static void __unbind(struct thermal_zone_device *tz, int mask, |
| 227 | struct thermal_cooling_device *cdev) |
| 228 | { |
| 229 | int i; |
| 230 | |
| 231 | for (i = 0; i < tz->trips; i++) |
| 232 | if (mask & (1 << i)) |
| 233 | thermal_zone_unbind_cooling_device(tz, i, cdev); |
| 234 | } |
| 235 | |
| 236 | static void bind_cdev(struct thermal_cooling_device *cdev) |
| 237 | { |
| 238 | int i, ret; |
| 239 | const struct thermal_zone_params *tzp; |
| 240 | struct thermal_zone_device *pos = NULL; |
| 241 | |
| 242 | mutex_lock(&thermal_list_lock); |
| 243 | |
| 244 | list_for_each_entry(pos, &thermal_tz_list, node) { |
| 245 | if (!pos->tzp && !pos->ops->bind) |
| 246 | continue; |
| 247 | |
| 248 | if (!pos->tzp && pos->ops->bind) { |
| 249 | ret = pos->ops->bind(pos, cdev); |
| 250 | if (ret) |
| 251 | print_bind_err_msg(pos, cdev, ret); |
| 252 | } |
| 253 | |
| 254 | tzp = pos->tzp; |
| 255 | if (!tzp->tbp) |
| 256 | return; |
| 257 | |
| 258 | for (i = 0; i < tzp->num_tbps; i++) { |
| 259 | if (tzp->tbp[i].cdev || !tzp->tbp[i].match) |
| 260 | continue; |
| 261 | if (tzp->tbp[i].match(pos, cdev)) |
| 262 | continue; |
| 263 | tzp->tbp[i].cdev = cdev; |
| 264 | __bind(pos, tzp->tbp[i].trip_mask, cdev); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | mutex_unlock(&thermal_list_lock); |
| 269 | } |
| 270 | |
| 271 | static void bind_tz(struct thermal_zone_device *tz) |
| 272 | { |
| 273 | int i, ret; |
| 274 | struct thermal_cooling_device *pos = NULL; |
| 275 | const struct thermal_zone_params *tzp = tz->tzp; |
| 276 | |
| 277 | if (!tzp && !tz->ops->bind) |
| 278 | return; |
| 279 | |
| 280 | mutex_lock(&thermal_list_lock); |
| 281 | |
| 282 | /* If there is no platform data, try to use ops->bind */ |
| 283 | if (!tzp && tz->ops->bind) { |
| 284 | list_for_each_entry(pos, &thermal_cdev_list, node) { |
| 285 | ret = tz->ops->bind(tz, pos); |
| 286 | if (ret) |
| 287 | print_bind_err_msg(tz, pos, ret); |
| 288 | } |
| 289 | goto exit; |
| 290 | } |
| 291 | |
| 292 | if (!tzp->tbp) |
| 293 | goto exit; |
| 294 | |
| 295 | list_for_each_entry(pos, &thermal_cdev_list, node) { |
| 296 | for (i = 0; i < tzp->num_tbps; i++) { |
| 297 | if (tzp->tbp[i].cdev || !tzp->tbp[i].match) |
| 298 | continue; |
| 299 | if (tzp->tbp[i].match(tz, pos)) |
| 300 | continue; |
| 301 | tzp->tbp[i].cdev = pos; |
| 302 | __bind(tz, tzp->tbp[i].trip_mask, pos); |
| 303 | } |
| 304 | } |
| 305 | exit: |
| 306 | mutex_unlock(&thermal_list_lock); |
| 307 | } |
| 308 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 309 | /* sys I/F for thermal zone */ |
| 310 | |
| 311 | #define to_thermal_zone(_dev) \ |
| 312 | container_of(_dev, struct thermal_zone_device, device) |
| 313 | |
| 314 | static ssize_t |
| 315 | type_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 316 | { |
| 317 | struct thermal_zone_device *tz = to_thermal_zone(dev); |
| 318 | |
| 319 | return sprintf(buf, "%s\n", tz->type); |
| 320 | } |
| 321 | |
| 322 | static ssize_t |
| 323 | temp_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 324 | { |
| 325 | struct thermal_zone_device *tz = to_thermal_zone(dev); |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 326 | long temperature; |
| 327 | int ret; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 328 | |
| 329 | if (!tz->ops->get_temp) |
| 330 | return -EPERM; |
| 331 | |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 332 | ret = tz->ops->get_temp(tz, &temperature); |
| 333 | |
| 334 | if (ret) |
| 335 | return ret; |
| 336 | |
| 337 | return sprintf(buf, "%ld\n", temperature); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | static ssize_t |
| 341 | mode_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 342 | { |
| 343 | struct thermal_zone_device *tz = to_thermal_zone(dev); |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 344 | enum thermal_device_mode mode; |
| 345 | int result; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 346 | |
| 347 | if (!tz->ops->get_mode) |
| 348 | return -EPERM; |
| 349 | |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 350 | result = tz->ops->get_mode(tz, &mode); |
| 351 | if (result) |
| 352 | return result; |
| 353 | |
| 354 | return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled" |
| 355 | : "disabled"); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | static ssize_t |
| 359 | mode_store(struct device *dev, struct device_attribute *attr, |
| 360 | const char *buf, size_t count) |
| 361 | { |
| 362 | struct thermal_zone_device *tz = to_thermal_zone(dev); |
| 363 | int result; |
| 364 | |
| 365 | if (!tz->ops->set_mode) |
| 366 | return -EPERM; |
| 367 | |
Amit Daniel Kachhap | f1f0e2a | 2012-03-21 16:40:01 +0530 | [diff] [blame] | 368 | if (!strncmp(buf, "enabled", sizeof("enabled") - 1)) |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 369 | result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED); |
Amit Daniel Kachhap | f1f0e2a | 2012-03-21 16:40:01 +0530 | [diff] [blame] | 370 | else if (!strncmp(buf, "disabled", sizeof("disabled") - 1)) |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 371 | result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED); |
| 372 | else |
| 373 | result = -EINVAL; |
| 374 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 375 | if (result) |
| 376 | return result; |
| 377 | |
| 378 | return count; |
| 379 | } |
| 380 | |
| 381 | static ssize_t |
| 382 | trip_point_type_show(struct device *dev, struct device_attribute *attr, |
| 383 | char *buf) |
| 384 | { |
| 385 | struct thermal_zone_device *tz = to_thermal_zone(dev); |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 386 | enum thermal_trip_type type; |
| 387 | int trip, result; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 388 | |
| 389 | if (!tz->ops->get_trip_type) |
| 390 | return -EPERM; |
| 391 | |
| 392 | if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip)) |
| 393 | return -EINVAL; |
| 394 | |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 395 | result = tz->ops->get_trip_type(tz, trip, &type); |
| 396 | if (result) |
| 397 | return result; |
| 398 | |
| 399 | switch (type) { |
| 400 | case THERMAL_TRIP_CRITICAL: |
Amit Kucheria | 625120a4 | 2009-10-16 12:46:02 +0300 | [diff] [blame] | 401 | return sprintf(buf, "critical\n"); |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 402 | case THERMAL_TRIP_HOT: |
Amit Kucheria | 625120a4 | 2009-10-16 12:46:02 +0300 | [diff] [blame] | 403 | return sprintf(buf, "hot\n"); |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 404 | case THERMAL_TRIP_PASSIVE: |
Amit Kucheria | 625120a4 | 2009-10-16 12:46:02 +0300 | [diff] [blame] | 405 | return sprintf(buf, "passive\n"); |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 406 | case THERMAL_TRIP_ACTIVE: |
Amit Kucheria | 625120a4 | 2009-10-16 12:46:02 +0300 | [diff] [blame] | 407 | return sprintf(buf, "active\n"); |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 408 | default: |
Amit Kucheria | 625120a4 | 2009-10-16 12:46:02 +0300 | [diff] [blame] | 409 | return sprintf(buf, "unknown\n"); |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 410 | } |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | static ssize_t |
Durgadoss R | c56f5c0 | 2012-07-25 10:10:58 +0800 | [diff] [blame] | 414 | trip_point_temp_store(struct device *dev, struct device_attribute *attr, |
| 415 | const char *buf, size_t count) |
| 416 | { |
| 417 | struct thermal_zone_device *tz = to_thermal_zone(dev); |
| 418 | int trip, ret; |
| 419 | unsigned long temperature; |
| 420 | |
| 421 | if (!tz->ops->set_trip_temp) |
| 422 | return -EPERM; |
| 423 | |
| 424 | if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip)) |
| 425 | return -EINVAL; |
| 426 | |
| 427 | if (kstrtoul(buf, 10, &temperature)) |
| 428 | return -EINVAL; |
| 429 | |
| 430 | ret = tz->ops->set_trip_temp(tz, trip, temperature); |
| 431 | |
| 432 | return ret ? ret : count; |
| 433 | } |
| 434 | |
| 435 | static ssize_t |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 436 | trip_point_temp_show(struct device *dev, struct device_attribute *attr, |
| 437 | char *buf) |
| 438 | { |
| 439 | struct thermal_zone_device *tz = to_thermal_zone(dev); |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 440 | int trip, ret; |
| 441 | long temperature; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 442 | |
| 443 | if (!tz->ops->get_trip_temp) |
| 444 | return -EPERM; |
| 445 | |
| 446 | if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip)) |
| 447 | return -EINVAL; |
| 448 | |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 449 | ret = tz->ops->get_trip_temp(tz, trip, &temperature); |
| 450 | |
| 451 | if (ret) |
| 452 | return ret; |
| 453 | |
| 454 | return sprintf(buf, "%ld\n", temperature); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 455 | } |
| 456 | |
Matthew Garrett | 03a971a | 2008-12-03 18:00:38 +0000 | [diff] [blame] | 457 | static ssize_t |
Durgadoss R | 27365a6 | 2012-07-25 10:10:59 +0800 | [diff] [blame] | 458 | trip_point_hyst_store(struct device *dev, struct device_attribute *attr, |
| 459 | const char *buf, size_t count) |
| 460 | { |
| 461 | struct thermal_zone_device *tz = to_thermal_zone(dev); |
| 462 | int trip, ret; |
| 463 | unsigned long temperature; |
| 464 | |
| 465 | if (!tz->ops->set_trip_hyst) |
| 466 | return -EPERM; |
| 467 | |
| 468 | if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip)) |
| 469 | return -EINVAL; |
| 470 | |
| 471 | if (kstrtoul(buf, 10, &temperature)) |
| 472 | return -EINVAL; |
| 473 | |
| 474 | /* |
| 475 | * We are not doing any check on the 'temperature' value |
| 476 | * here. The driver implementing 'set_trip_hyst' has to |
| 477 | * take care of this. |
| 478 | */ |
| 479 | ret = tz->ops->set_trip_hyst(tz, trip, temperature); |
| 480 | |
| 481 | return ret ? ret : count; |
| 482 | } |
| 483 | |
| 484 | static ssize_t |
| 485 | trip_point_hyst_show(struct device *dev, struct device_attribute *attr, |
| 486 | char *buf) |
| 487 | { |
| 488 | struct thermal_zone_device *tz = to_thermal_zone(dev); |
| 489 | int trip, ret; |
| 490 | unsigned long temperature; |
| 491 | |
| 492 | if (!tz->ops->get_trip_hyst) |
| 493 | return -EPERM; |
| 494 | |
| 495 | if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip)) |
| 496 | return -EINVAL; |
| 497 | |
| 498 | ret = tz->ops->get_trip_hyst(tz, trip, &temperature); |
| 499 | |
| 500 | return ret ? ret : sprintf(buf, "%ld\n", temperature); |
| 501 | } |
| 502 | |
| 503 | static ssize_t |
Matthew Garrett | 03a971a | 2008-12-03 18:00:38 +0000 | [diff] [blame] | 504 | passive_store(struct device *dev, struct device_attribute *attr, |
| 505 | const char *buf, size_t count) |
| 506 | { |
| 507 | struct thermal_zone_device *tz = to_thermal_zone(dev); |
| 508 | struct thermal_cooling_device *cdev = NULL; |
| 509 | int state; |
| 510 | |
| 511 | if (!sscanf(buf, "%d\n", &state)) |
| 512 | return -EINVAL; |
| 513 | |
Frans Pop | 3d8e3ad | 2009-10-26 08:39:02 +0100 | [diff] [blame] | 514 | /* sanity check: values below 1000 millicelcius don't make sense |
| 515 | * and can cause the system to go into a thermal heart attack |
| 516 | */ |
| 517 | if (state && state < 1000) |
| 518 | return -EINVAL; |
| 519 | |
Matthew Garrett | 03a971a | 2008-12-03 18:00:38 +0000 | [diff] [blame] | 520 | if (state && !tz->forced_passive) { |
| 521 | mutex_lock(&thermal_list_lock); |
| 522 | list_for_each_entry(cdev, &thermal_cdev_list, node) { |
| 523 | if (!strncmp("Processor", cdev->type, |
| 524 | sizeof("Processor"))) |
| 525 | thermal_zone_bind_cooling_device(tz, |
Zhang Rui | 9d99842 | 2012-06-26 16:35:57 +0800 | [diff] [blame] | 526 | THERMAL_TRIPS_NONE, cdev, |
| 527 | THERMAL_NO_LIMIT, |
| 528 | THERMAL_NO_LIMIT); |
Matthew Garrett | 03a971a | 2008-12-03 18:00:38 +0000 | [diff] [blame] | 529 | } |
| 530 | mutex_unlock(&thermal_list_lock); |
Frans Pop | e4143b0 | 2009-10-26 08:39:03 +0100 | [diff] [blame] | 531 | if (!tz->passive_delay) |
| 532 | tz->passive_delay = 1000; |
Matthew Garrett | 03a971a | 2008-12-03 18:00:38 +0000 | [diff] [blame] | 533 | } else if (!state && tz->forced_passive) { |
| 534 | mutex_lock(&thermal_list_lock); |
| 535 | list_for_each_entry(cdev, &thermal_cdev_list, node) { |
| 536 | if (!strncmp("Processor", cdev->type, |
| 537 | sizeof("Processor"))) |
| 538 | thermal_zone_unbind_cooling_device(tz, |
| 539 | THERMAL_TRIPS_NONE, |
| 540 | cdev); |
| 541 | } |
| 542 | mutex_unlock(&thermal_list_lock); |
Frans Pop | e4143b0 | 2009-10-26 08:39:03 +0100 | [diff] [blame] | 543 | tz->passive_delay = 0; |
Matthew Garrett | 03a971a | 2008-12-03 18:00:38 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Matthew Garrett | 03a971a | 2008-12-03 18:00:38 +0000 | [diff] [blame] | 546 | tz->forced_passive = state; |
| 547 | |
| 548 | thermal_zone_device_update(tz); |
| 549 | |
| 550 | return count; |
| 551 | } |
| 552 | |
| 553 | static ssize_t |
| 554 | passive_show(struct device *dev, struct device_attribute *attr, |
| 555 | char *buf) |
| 556 | { |
| 557 | struct thermal_zone_device *tz = to_thermal_zone(dev); |
| 558 | |
| 559 | return sprintf(buf, "%d\n", tz->forced_passive); |
| 560 | } |
| 561 | |
Durgadoss R | 5a2c090 | 2012-09-18 11:04:58 +0530 | [diff] [blame] | 562 | static ssize_t |
| 563 | policy_store(struct device *dev, struct device_attribute *attr, |
| 564 | const char *buf, size_t count) |
| 565 | { |
| 566 | int ret = -EINVAL; |
| 567 | struct thermal_zone_device *tz = to_thermal_zone(dev); |
| 568 | struct thermal_governor *gov; |
| 569 | |
| 570 | mutex_lock(&thermal_governor_lock); |
| 571 | |
| 572 | gov = __find_governor(buf); |
| 573 | if (!gov) |
| 574 | goto exit; |
| 575 | |
| 576 | tz->governor = gov; |
| 577 | ret = count; |
| 578 | |
| 579 | exit: |
| 580 | mutex_unlock(&thermal_governor_lock); |
| 581 | return ret; |
| 582 | } |
| 583 | |
| 584 | static ssize_t |
| 585 | policy_show(struct device *dev, struct device_attribute *devattr, char *buf) |
| 586 | { |
| 587 | struct thermal_zone_device *tz = to_thermal_zone(dev); |
| 588 | |
| 589 | return sprintf(buf, "%s\n", tz->governor->name); |
| 590 | } |
| 591 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 592 | static DEVICE_ATTR(type, 0444, type_show, NULL); |
| 593 | static DEVICE_ATTR(temp, 0444, temp_show, NULL); |
| 594 | static DEVICE_ATTR(mode, 0644, mode_show, mode_store); |
Joe Perches | 886ee54 | 2012-03-21 12:55:01 -0700 | [diff] [blame] | 595 | static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store); |
Durgadoss R | 5a2c090 | 2012-09-18 11:04:58 +0530 | [diff] [blame] | 596 | static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 597 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 598 | /* sys I/F for cooling device */ |
| 599 | #define to_cooling_device(_dev) \ |
| 600 | container_of(_dev, struct thermal_cooling_device, device) |
| 601 | |
| 602 | static ssize_t |
| 603 | thermal_cooling_device_type_show(struct device *dev, |
| 604 | struct device_attribute *attr, char *buf) |
| 605 | { |
| 606 | struct thermal_cooling_device *cdev = to_cooling_device(dev); |
| 607 | |
| 608 | return sprintf(buf, "%s\n", cdev->type); |
| 609 | } |
| 610 | |
| 611 | static ssize_t |
| 612 | thermal_cooling_device_max_state_show(struct device *dev, |
| 613 | struct device_attribute *attr, char *buf) |
| 614 | { |
| 615 | struct thermal_cooling_device *cdev = to_cooling_device(dev); |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 616 | unsigned long state; |
| 617 | int ret; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 618 | |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 619 | ret = cdev->ops->get_max_state(cdev, &state); |
| 620 | if (ret) |
| 621 | return ret; |
| 622 | return sprintf(buf, "%ld\n", state); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | static ssize_t |
| 626 | thermal_cooling_device_cur_state_show(struct device *dev, |
| 627 | struct device_attribute *attr, char *buf) |
| 628 | { |
| 629 | struct thermal_cooling_device *cdev = to_cooling_device(dev); |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 630 | unsigned long state; |
| 631 | int ret; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 632 | |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 633 | ret = cdev->ops->get_cur_state(cdev, &state); |
| 634 | if (ret) |
| 635 | return ret; |
| 636 | return sprintf(buf, "%ld\n", state); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | static ssize_t |
| 640 | thermal_cooling_device_cur_state_store(struct device *dev, |
| 641 | struct device_attribute *attr, |
| 642 | const char *buf, size_t count) |
| 643 | { |
| 644 | struct thermal_cooling_device *cdev = to_cooling_device(dev); |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 645 | unsigned long state; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 646 | int result; |
| 647 | |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 648 | if (!sscanf(buf, "%ld\n", &state)) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 649 | return -EINVAL; |
| 650 | |
Roel Kluin | edb9491 | 2009-12-15 22:46:50 +0100 | [diff] [blame] | 651 | if ((long)state < 0) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 652 | return -EINVAL; |
| 653 | |
| 654 | result = cdev->ops->set_cur_state(cdev, state); |
| 655 | if (result) |
| 656 | return result; |
| 657 | return count; |
| 658 | } |
| 659 | |
| 660 | static struct device_attribute dev_attr_cdev_type = |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 661 | __ATTR(type, 0444, thermal_cooling_device_type_show, NULL); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 662 | static DEVICE_ATTR(max_state, 0444, |
| 663 | thermal_cooling_device_max_state_show, NULL); |
| 664 | static DEVICE_ATTR(cur_state, 0644, |
| 665 | thermal_cooling_device_cur_state_show, |
| 666 | thermal_cooling_device_cur_state_store); |
| 667 | |
| 668 | static ssize_t |
| 669 | thermal_cooling_device_trip_point_show(struct device *dev, |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 670 | struct device_attribute *attr, char *buf) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 671 | { |
Zhang Rui | b81b6ba | 2012-06-27 10:08:19 +0800 | [diff] [blame] | 672 | struct thermal_instance *instance; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 673 | |
| 674 | instance = |
Zhang Rui | b81b6ba | 2012-06-27 10:08:19 +0800 | [diff] [blame] | 675 | container_of(attr, struct thermal_instance, attr); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 676 | |
| 677 | if (instance->trip == THERMAL_TRIPS_NONE) |
| 678 | return sprintf(buf, "-1\n"); |
| 679 | else |
| 680 | return sprintf(buf, "%d\n", instance->trip); |
| 681 | } |
| 682 | |
| 683 | /* Device management */ |
| 684 | |
Rene Herman | 16d7523 | 2008-06-24 19:38:56 +0200 | [diff] [blame] | 685 | #if defined(CONFIG_THERMAL_HWMON) |
| 686 | |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 687 | /* hwmon sys I/F */ |
| 688 | #include <linux/hwmon.h> |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 689 | |
| 690 | /* thermal zone devices with the same type share one hwmon device */ |
| 691 | struct thermal_hwmon_device { |
| 692 | char type[THERMAL_NAME_LENGTH]; |
| 693 | struct device *device; |
| 694 | int count; |
| 695 | struct list_head tz_list; |
| 696 | struct list_head node; |
| 697 | }; |
| 698 | |
| 699 | struct thermal_hwmon_attr { |
| 700 | struct device_attribute attr; |
| 701 | char name[16]; |
| 702 | }; |
| 703 | |
| 704 | /* one temperature input for each thermal zone */ |
| 705 | struct thermal_hwmon_temp { |
| 706 | struct list_head hwmon_node; |
| 707 | struct thermal_zone_device *tz; |
| 708 | struct thermal_hwmon_attr temp_input; /* hwmon sys attr */ |
| 709 | struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */ |
| 710 | }; |
| 711 | |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 712 | static LIST_HEAD(thermal_hwmon_list); |
| 713 | |
| 714 | static ssize_t |
| 715 | name_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 716 | { |
Greg Kroah-Hartman | 0e968a3 | 2009-04-30 14:43:31 -0700 | [diff] [blame] | 717 | struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev); |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 718 | return sprintf(buf, "%s\n", hwmon->type); |
| 719 | } |
| 720 | static DEVICE_ATTR(name, 0444, name_show, NULL); |
| 721 | |
| 722 | static ssize_t |
| 723 | temp_input_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 724 | { |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 725 | long temperature; |
| 726 | int ret; |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 727 | struct thermal_hwmon_attr *hwmon_attr |
| 728 | = container_of(attr, struct thermal_hwmon_attr, attr); |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 729 | struct thermal_hwmon_temp *temp |
| 730 | = container_of(hwmon_attr, struct thermal_hwmon_temp, |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 731 | temp_input); |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 732 | struct thermal_zone_device *tz = temp->tz; |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 733 | |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 734 | ret = tz->ops->get_temp(tz, &temperature); |
| 735 | |
| 736 | if (ret) |
| 737 | return ret; |
| 738 | |
| 739 | return sprintf(buf, "%ld\n", temperature); |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | static ssize_t |
| 743 | temp_crit_show(struct device *dev, struct device_attribute *attr, |
| 744 | char *buf) |
| 745 | { |
| 746 | struct thermal_hwmon_attr *hwmon_attr |
| 747 | = container_of(attr, struct thermal_hwmon_attr, attr); |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 748 | struct thermal_hwmon_temp *temp |
| 749 | = container_of(hwmon_attr, struct thermal_hwmon_temp, |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 750 | temp_crit); |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 751 | struct thermal_zone_device *tz = temp->tz; |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 752 | long temperature; |
| 753 | int ret; |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 754 | |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 755 | ret = tz->ops->get_trip_temp(tz, 0, &temperature); |
| 756 | if (ret) |
| 757 | return ret; |
| 758 | |
| 759 | return sprintf(buf, "%ld\n", temperature); |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | |
Jean Delvare | 0d97d7a | 2011-07-28 13:48:41 -0700 | [diff] [blame] | 763 | static struct thermal_hwmon_device * |
| 764 | thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz) |
| 765 | { |
| 766 | struct thermal_hwmon_device *hwmon; |
| 767 | |
| 768 | mutex_lock(&thermal_list_lock); |
| 769 | list_for_each_entry(hwmon, &thermal_hwmon_list, node) |
| 770 | if (!strcmp(hwmon->type, tz->type)) { |
| 771 | mutex_unlock(&thermal_list_lock); |
| 772 | return hwmon; |
| 773 | } |
| 774 | mutex_unlock(&thermal_list_lock); |
| 775 | |
| 776 | return NULL; |
| 777 | } |
| 778 | |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 779 | /* Find the temperature input matching a given thermal zone */ |
| 780 | static struct thermal_hwmon_temp * |
| 781 | thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon, |
| 782 | const struct thermal_zone_device *tz) |
| 783 | { |
| 784 | struct thermal_hwmon_temp *temp; |
| 785 | |
| 786 | mutex_lock(&thermal_list_lock); |
| 787 | list_for_each_entry(temp, &hwmon->tz_list, hwmon_node) |
| 788 | if (temp->tz == tz) { |
| 789 | mutex_unlock(&thermal_list_lock); |
| 790 | return temp; |
| 791 | } |
| 792 | mutex_unlock(&thermal_list_lock); |
| 793 | |
| 794 | return NULL; |
| 795 | } |
| 796 | |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 797 | static int |
| 798 | thermal_add_hwmon_sysfs(struct thermal_zone_device *tz) |
| 799 | { |
| 800 | struct thermal_hwmon_device *hwmon; |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 801 | struct thermal_hwmon_temp *temp; |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 802 | int new_hwmon_device = 1; |
| 803 | int result; |
| 804 | |
Jean Delvare | 0d97d7a | 2011-07-28 13:48:41 -0700 | [diff] [blame] | 805 | hwmon = thermal_hwmon_lookup_by_type(tz); |
| 806 | if (hwmon) { |
| 807 | new_hwmon_device = 0; |
| 808 | goto register_sys_interface; |
| 809 | } |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 810 | |
| 811 | hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL); |
| 812 | if (!hwmon) |
| 813 | return -ENOMEM; |
| 814 | |
| 815 | INIT_LIST_HEAD(&hwmon->tz_list); |
| 816 | strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH); |
| 817 | hwmon->device = hwmon_device_register(NULL); |
| 818 | if (IS_ERR(hwmon->device)) { |
| 819 | result = PTR_ERR(hwmon->device); |
| 820 | goto free_mem; |
| 821 | } |
Greg Kroah-Hartman | 0e968a3 | 2009-04-30 14:43:31 -0700 | [diff] [blame] | 822 | dev_set_drvdata(hwmon->device, hwmon); |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 823 | result = device_create_file(hwmon->device, &dev_attr_name); |
| 824 | if (result) |
Durgadoss R | b299eb5 | 2011-03-03 04:30:13 +0530 | [diff] [blame] | 825 | goto free_mem; |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 826 | |
| 827 | register_sys_interface: |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 828 | temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL); |
| 829 | if (!temp) { |
| 830 | result = -ENOMEM; |
| 831 | goto unregister_name; |
| 832 | } |
| 833 | |
| 834 | temp->tz = tz; |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 835 | hwmon->count++; |
| 836 | |
Guenter Roeck | 79a4916 | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 837 | snprintf(temp->temp_input.name, sizeof(temp->temp_input.name), |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 838 | "temp%d_input", hwmon->count); |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 839 | temp->temp_input.attr.attr.name = temp->temp_input.name; |
| 840 | temp->temp_input.attr.attr.mode = 0444; |
| 841 | temp->temp_input.attr.show = temp_input_show; |
| 842 | sysfs_attr_init(&temp->temp_input.attr.attr); |
| 843 | result = device_create_file(hwmon->device, &temp->temp_input.attr); |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 844 | if (result) |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 845 | goto free_temp_mem; |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 846 | |
| 847 | if (tz->ops->get_crit_temp) { |
| 848 | unsigned long temperature; |
| 849 | if (!tz->ops->get_crit_temp(tz, &temperature)) { |
Guenter Roeck | 79a4916 | 2012-07-21 10:53:48 +1000 | [diff] [blame] | 850 | snprintf(temp->temp_crit.name, |
| 851 | sizeof(temp->temp_crit.name), |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 852 | "temp%d_crit", hwmon->count); |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 853 | temp->temp_crit.attr.attr.name = temp->temp_crit.name; |
| 854 | temp->temp_crit.attr.attr.mode = 0444; |
| 855 | temp->temp_crit.attr.show = temp_crit_show; |
| 856 | sysfs_attr_init(&temp->temp_crit.attr.attr); |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 857 | result = device_create_file(hwmon->device, |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 858 | &temp->temp_crit.attr); |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 859 | if (result) |
Durgadoss R | b299eb5 | 2011-03-03 04:30:13 +0530 | [diff] [blame] | 860 | goto unregister_input; |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 861 | } |
| 862 | } |
| 863 | |
| 864 | mutex_lock(&thermal_list_lock); |
| 865 | if (new_hwmon_device) |
| 866 | list_add_tail(&hwmon->node, &thermal_hwmon_list); |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 867 | list_add_tail(&temp->hwmon_node, &hwmon->tz_list); |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 868 | mutex_unlock(&thermal_list_lock); |
| 869 | |
| 870 | return 0; |
| 871 | |
Durgadoss R | b299eb5 | 2011-03-03 04:30:13 +0530 | [diff] [blame] | 872 | unregister_input: |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 873 | device_remove_file(hwmon->device, &temp->temp_input.attr); |
| 874 | free_temp_mem: |
| 875 | kfree(temp); |
Durgadoss R | b299eb5 | 2011-03-03 04:30:13 +0530 | [diff] [blame] | 876 | unregister_name: |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 877 | if (new_hwmon_device) { |
| 878 | device_remove_file(hwmon->device, &dev_attr_name); |
| 879 | hwmon_device_unregister(hwmon->device); |
| 880 | } |
| 881 | free_mem: |
| 882 | if (new_hwmon_device) |
| 883 | kfree(hwmon); |
| 884 | |
| 885 | return result; |
| 886 | } |
| 887 | |
| 888 | static void |
| 889 | thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz) |
| 890 | { |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 891 | struct thermal_hwmon_device *hwmon; |
| 892 | struct thermal_hwmon_temp *temp; |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 893 | |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 894 | hwmon = thermal_hwmon_lookup_by_type(tz); |
| 895 | if (unlikely(!hwmon)) { |
| 896 | /* Should never happen... */ |
| 897 | dev_dbg(&tz->device, "hwmon device lookup failed!\n"); |
| 898 | return; |
| 899 | } |
| 900 | |
| 901 | temp = thermal_hwmon_lookup_temp(hwmon, tz); |
| 902 | if (unlikely(!temp)) { |
| 903 | /* Should never happen... */ |
| 904 | dev_dbg(&tz->device, "temperature input lookup failed!\n"); |
| 905 | return; |
| 906 | } |
| 907 | |
| 908 | device_remove_file(hwmon->device, &temp->temp_input.attr); |
Durgadoss R | b299eb5 | 2011-03-03 04:30:13 +0530 | [diff] [blame] | 909 | if (tz->ops->get_crit_temp) |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 910 | device_remove_file(hwmon->device, &temp->temp_crit.attr); |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 911 | |
| 912 | mutex_lock(&thermal_list_lock); |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 913 | list_del(&temp->hwmon_node); |
| 914 | kfree(temp); |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 915 | if (!list_empty(&hwmon->tz_list)) { |
| 916 | mutex_unlock(&thermal_list_lock); |
| 917 | return; |
| 918 | } |
| 919 | list_del(&hwmon->node); |
| 920 | mutex_unlock(&thermal_list_lock); |
| 921 | |
| 922 | device_remove_file(hwmon->device, &dev_attr_name); |
| 923 | hwmon_device_unregister(hwmon->device); |
| 924 | kfree(hwmon); |
| 925 | } |
| 926 | #else |
| 927 | static int |
| 928 | thermal_add_hwmon_sysfs(struct thermal_zone_device *tz) |
| 929 | { |
| 930 | return 0; |
| 931 | } |
| 932 | |
| 933 | static void |
| 934 | thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz) |
| 935 | { |
| 936 | } |
| 937 | #endif |
| 938 | |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 939 | static void thermal_zone_device_set_polling(struct thermal_zone_device *tz, |
| 940 | int delay) |
| 941 | { |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 942 | if (delay > 1000) |
Tejun Heo | 41f63c5 | 2012-08-03 10:30:47 -0700 | [diff] [blame] | 943 | mod_delayed_work(system_freezable_wq, &tz->poll_queue, |
| 944 | round_jiffies(msecs_to_jiffies(delay))); |
| 945 | else if (delay) |
| 946 | mod_delayed_work(system_freezable_wq, &tz->poll_queue, |
| 947 | msecs_to_jiffies(delay)); |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 948 | else |
Tejun Heo | 41f63c5 | 2012-08-03 10:30:47 -0700 | [diff] [blame] | 949 | cancel_delayed_work(&tz->poll_queue); |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 950 | } |
| 951 | |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 952 | static void thermal_zone_device_check(struct work_struct *work) |
| 953 | { |
| 954 | struct thermal_zone_device *tz = container_of(work, struct |
| 955 | thermal_zone_device, |
| 956 | poll_queue.work); |
| 957 | thermal_zone_device_update(tz); |
| 958 | } |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 959 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 960 | /** |
| 961 | * 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] | 962 | * @tz: thermal zone device |
| 963 | * @trip: indicates which trip point the cooling devices is |
| 964 | * associated with in this thermal zone. |
| 965 | * @cdev: thermal cooling device |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 966 | * |
| 967 | * This function is usually called in the thermal zone device .bind callback. |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 968 | */ |
| 969 | int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz, |
| 970 | int trip, |
Zhang Rui | 9d99842 | 2012-06-26 16:35:57 +0800 | [diff] [blame] | 971 | struct thermal_cooling_device *cdev, |
| 972 | unsigned long upper, unsigned long lower) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 973 | { |
Zhang Rui | b81b6ba | 2012-06-27 10:08:19 +0800 | [diff] [blame] | 974 | struct thermal_instance *dev; |
| 975 | struct thermal_instance *pos; |
Thomas Sujith | c751670 | 2008-02-15 00:58:50 -0500 | [diff] [blame] | 976 | struct thermal_zone_device *pos1; |
| 977 | struct thermal_cooling_device *pos2; |
Zhang Rui | 74051ba | 2012-06-26 16:27:22 +0800 | [diff] [blame] | 978 | unsigned long max_state; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 979 | int result; |
| 980 | |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 981 | if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE)) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 982 | return -EINVAL; |
| 983 | |
Thomas Sujith | c751670 | 2008-02-15 00:58:50 -0500 | [diff] [blame] | 984 | list_for_each_entry(pos1, &thermal_tz_list, node) { |
| 985 | if (pos1 == tz) |
| 986 | break; |
| 987 | } |
| 988 | list_for_each_entry(pos2, &thermal_cdev_list, node) { |
| 989 | if (pos2 == cdev) |
| 990 | break; |
| 991 | } |
| 992 | |
| 993 | if (tz != pos1 || cdev != pos2) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 994 | return -EINVAL; |
| 995 | |
Zhang Rui | 9d99842 | 2012-06-26 16:35:57 +0800 | [diff] [blame] | 996 | cdev->ops->get_max_state(cdev, &max_state); |
| 997 | |
| 998 | /* lower default 0, upper default max_state */ |
| 999 | lower = lower == THERMAL_NO_LIMIT ? 0 : lower; |
| 1000 | upper = upper == THERMAL_NO_LIMIT ? max_state : upper; |
| 1001 | |
| 1002 | if (lower > upper || upper > max_state) |
| 1003 | return -EINVAL; |
| 1004 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1005 | dev = |
Zhang Rui | b81b6ba | 2012-06-27 10:08:19 +0800 | [diff] [blame] | 1006 | kzalloc(sizeof(struct thermal_instance), GFP_KERNEL); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1007 | if (!dev) |
| 1008 | return -ENOMEM; |
| 1009 | dev->tz = tz; |
| 1010 | dev->cdev = cdev; |
| 1011 | dev->trip = trip; |
Zhang Rui | 9d99842 | 2012-06-26 16:35:57 +0800 | [diff] [blame] | 1012 | dev->upper = upper; |
| 1013 | dev->lower = lower; |
Zhang Rui | ce119f8 | 2012-06-27 14:13:04 +0800 | [diff] [blame] | 1014 | dev->target = THERMAL_NO_TARGET; |
Zhang Rui | 74051ba | 2012-06-26 16:27:22 +0800 | [diff] [blame] | 1015 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1016 | result = get_idr(&tz->idr, &tz->lock, &dev->id); |
| 1017 | if (result) |
| 1018 | goto free_mem; |
| 1019 | |
| 1020 | sprintf(dev->name, "cdev%d", dev->id); |
| 1021 | result = |
| 1022 | sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name); |
| 1023 | if (result) |
| 1024 | goto release_idr; |
| 1025 | |
| 1026 | sprintf(dev->attr_name, "cdev%d_trip_point", dev->id); |
Sergey Senozhatsky | 975f8c5 | 2010-04-06 14:34:51 -0700 | [diff] [blame] | 1027 | sysfs_attr_init(&dev->attr.attr); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1028 | dev->attr.attr.name = dev->attr_name; |
| 1029 | dev->attr.attr.mode = 0444; |
| 1030 | dev->attr.show = thermal_cooling_device_trip_point_show; |
| 1031 | result = device_create_file(&tz->device, &dev->attr); |
| 1032 | if (result) |
| 1033 | goto remove_symbol_link; |
| 1034 | |
| 1035 | mutex_lock(&tz->lock); |
Zhang Rui | f4a821c | 2012-07-24 16:56:21 +0800 | [diff] [blame] | 1036 | mutex_lock(&cdev->lock); |
Zhang Rui | cddf31b | 2012-06-27 10:09:36 +0800 | [diff] [blame] | 1037 | list_for_each_entry(pos, &tz->thermal_instances, tz_node) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1038 | if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { |
| 1039 | result = -EEXIST; |
| 1040 | break; |
| 1041 | } |
Zhang Rui | b5e4ae6 | 2012-06-27 14:11:52 +0800 | [diff] [blame] | 1042 | if (!result) { |
Zhang Rui | cddf31b | 2012-06-27 10:09:36 +0800 | [diff] [blame] | 1043 | list_add_tail(&dev->tz_node, &tz->thermal_instances); |
Zhang Rui | b5e4ae6 | 2012-06-27 14:11:52 +0800 | [diff] [blame] | 1044 | list_add_tail(&dev->cdev_node, &cdev->thermal_instances); |
| 1045 | } |
Zhang Rui | f4a821c | 2012-07-24 16:56:21 +0800 | [diff] [blame] | 1046 | mutex_unlock(&cdev->lock); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1047 | mutex_unlock(&tz->lock); |
| 1048 | |
| 1049 | if (!result) |
| 1050 | return 0; |
| 1051 | |
| 1052 | device_remove_file(&tz->device, &dev->attr); |
Joe Perches | caca8b8 | 2012-03-21 12:55:02 -0700 | [diff] [blame] | 1053 | remove_symbol_link: |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1054 | sysfs_remove_link(&tz->device.kobj, dev->name); |
Joe Perches | caca8b8 | 2012-03-21 12:55:02 -0700 | [diff] [blame] | 1055 | release_idr: |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1056 | release_idr(&tz->idr, &tz->lock, dev->id); |
Joe Perches | caca8b8 | 2012-03-21 12:55:02 -0700 | [diff] [blame] | 1057 | free_mem: |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1058 | kfree(dev); |
| 1059 | return result; |
| 1060 | } |
| 1061 | EXPORT_SYMBOL(thermal_zone_bind_cooling_device); |
| 1062 | |
| 1063 | /** |
| 1064 | * 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] | 1065 | * @tz: thermal zone device |
| 1066 | * @trip: indicates which trip point the cooling devices is |
| 1067 | * associated with in this thermal zone. |
| 1068 | * @cdev: thermal cooling device |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 1069 | * |
| 1070 | * This function is usually called in the thermal zone device .unbind callback. |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1071 | */ |
| 1072 | int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz, |
| 1073 | int trip, |
| 1074 | struct thermal_cooling_device *cdev) |
| 1075 | { |
Zhang Rui | b81b6ba | 2012-06-27 10:08:19 +0800 | [diff] [blame] | 1076 | struct thermal_instance *pos, *next; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1077 | |
| 1078 | mutex_lock(&tz->lock); |
Zhang Rui | f4a821c | 2012-07-24 16:56:21 +0800 | [diff] [blame] | 1079 | mutex_lock(&cdev->lock); |
Zhang Rui | cddf31b | 2012-06-27 10:09:36 +0800 | [diff] [blame] | 1080 | list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) { |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 1081 | if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { |
Zhang Rui | cddf31b | 2012-06-27 10:09:36 +0800 | [diff] [blame] | 1082 | list_del(&pos->tz_node); |
Zhang Rui | b5e4ae6 | 2012-06-27 14:11:52 +0800 | [diff] [blame] | 1083 | list_del(&pos->cdev_node); |
Zhang Rui | f4a821c | 2012-07-24 16:56:21 +0800 | [diff] [blame] | 1084 | mutex_unlock(&cdev->lock); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1085 | mutex_unlock(&tz->lock); |
| 1086 | goto unbind; |
| 1087 | } |
| 1088 | } |
Zhang Rui | f4a821c | 2012-07-24 16:56:21 +0800 | [diff] [blame] | 1089 | mutex_unlock(&cdev->lock); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1090 | mutex_unlock(&tz->lock); |
| 1091 | |
| 1092 | return -ENODEV; |
| 1093 | |
Joe Perches | caca8b8 | 2012-03-21 12:55:02 -0700 | [diff] [blame] | 1094 | unbind: |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1095 | device_remove_file(&tz->device, &pos->attr); |
| 1096 | sysfs_remove_link(&tz->device.kobj, pos->name); |
| 1097 | release_idr(&tz->idr, &tz->lock, pos->id); |
| 1098 | kfree(pos); |
| 1099 | return 0; |
| 1100 | } |
| 1101 | EXPORT_SYMBOL(thermal_zone_unbind_cooling_device); |
| 1102 | |
| 1103 | static void thermal_release(struct device *dev) |
| 1104 | { |
| 1105 | struct thermal_zone_device *tz; |
| 1106 | struct thermal_cooling_device *cdev; |
| 1107 | |
Joe Perches | caca8b8 | 2012-03-21 12:55:02 -0700 | [diff] [blame] | 1108 | if (!strncmp(dev_name(dev), "thermal_zone", |
| 1109 | sizeof("thermal_zone") - 1)) { |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1110 | tz = to_thermal_zone(dev); |
| 1111 | kfree(tz); |
| 1112 | } else { |
| 1113 | cdev = to_cooling_device(dev); |
| 1114 | kfree(cdev); |
| 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | static struct class thermal_class = { |
| 1119 | .name = "thermal", |
| 1120 | .dev_release = thermal_release, |
| 1121 | }; |
| 1122 | |
| 1123 | /** |
| 1124 | * thermal_cooling_device_register - register a new thermal cooling device |
| 1125 | * @type: the thermal cooling device type. |
| 1126 | * @devdata: device private data. |
| 1127 | * @ops: standard thermal cooling devices callbacks. |
| 1128 | */ |
Joe Perches | caca8b8 | 2012-03-21 12:55:02 -0700 | [diff] [blame] | 1129 | struct thermal_cooling_device * |
| 1130 | thermal_cooling_device_register(char *type, void *devdata, |
| 1131 | const struct thermal_cooling_device_ops *ops) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1132 | { |
| 1133 | struct thermal_cooling_device *cdev; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1134 | int result; |
| 1135 | |
Guenter Roeck | 204dd1d | 2012-08-07 22:36:45 -0700 | [diff] [blame] | 1136 | if (type && strlen(type) >= THERMAL_NAME_LENGTH) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 1137 | return ERR_PTR(-EINVAL); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1138 | |
| 1139 | if (!ops || !ops->get_max_state || !ops->get_cur_state || |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 1140 | !ops->set_cur_state) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 1141 | return ERR_PTR(-EINVAL); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1142 | |
| 1143 | cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL); |
| 1144 | if (!cdev) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 1145 | return ERR_PTR(-ENOMEM); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1146 | |
| 1147 | result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id); |
| 1148 | if (result) { |
| 1149 | kfree(cdev); |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 1150 | return ERR_PTR(result); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1151 | } |
| 1152 | |
Guenter Roeck | 204dd1d | 2012-08-07 22:36:45 -0700 | [diff] [blame] | 1153 | strcpy(cdev->type, type ? : ""); |
Zhang Rui | f4a821c | 2012-07-24 16:56:21 +0800 | [diff] [blame] | 1154 | mutex_init(&cdev->lock); |
Zhang Rui | b5e4ae6 | 2012-06-27 14:11:52 +0800 | [diff] [blame] | 1155 | INIT_LIST_HEAD(&cdev->thermal_instances); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1156 | cdev->ops = ops; |
Zhang Rui | ce119f8 | 2012-06-27 14:13:04 +0800 | [diff] [blame] | 1157 | cdev->updated = true; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1158 | cdev->device.class = &thermal_class; |
| 1159 | cdev->devdata = devdata; |
Kay Sievers | 354655e | 2009-01-06 10:44:37 -0800 | [diff] [blame] | 1160 | dev_set_name(&cdev->device, "cooling_device%d", cdev->id); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1161 | result = device_register(&cdev->device); |
| 1162 | if (result) { |
| 1163 | release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id); |
| 1164 | kfree(cdev); |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 1165 | return ERR_PTR(result); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1166 | } |
| 1167 | |
| 1168 | /* sys I/F */ |
| 1169 | if (type) { |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 1170 | result = device_create_file(&cdev->device, &dev_attr_cdev_type); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1171 | if (result) |
| 1172 | goto unregister; |
| 1173 | } |
| 1174 | |
| 1175 | result = device_create_file(&cdev->device, &dev_attr_max_state); |
| 1176 | if (result) |
| 1177 | goto unregister; |
| 1178 | |
| 1179 | result = device_create_file(&cdev->device, &dev_attr_cur_state); |
| 1180 | if (result) |
| 1181 | goto unregister; |
| 1182 | |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 1183 | /* Add 'this' new cdev to the global cdev list */ |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1184 | mutex_lock(&thermal_list_lock); |
| 1185 | list_add(&cdev->node, &thermal_cdev_list); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1186 | mutex_unlock(&thermal_list_lock); |
| 1187 | |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 1188 | /* Update binding information for 'this' new cdev */ |
| 1189 | bind_cdev(cdev); |
| 1190 | |
| 1191 | return cdev; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1192 | |
Joe Perches | caca8b8 | 2012-03-21 12:55:02 -0700 | [diff] [blame] | 1193 | unregister: |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1194 | release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id); |
| 1195 | device_unregister(&cdev->device); |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 1196 | return ERR_PTR(result); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1197 | } |
| 1198 | EXPORT_SYMBOL(thermal_cooling_device_register); |
| 1199 | |
| 1200 | /** |
| 1201 | * thermal_cooling_device_unregister - removes the registered thermal cooling device |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1202 | * @cdev: the thermal cooling device to remove. |
| 1203 | * |
| 1204 | * thermal_cooling_device_unregister() must be called when the device is no |
| 1205 | * longer needed. |
| 1206 | */ |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 1207 | void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1208 | { |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 1209 | int i; |
| 1210 | const struct thermal_zone_params *tzp; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1211 | struct thermal_zone_device *tz; |
| 1212 | struct thermal_cooling_device *pos = NULL; |
| 1213 | |
| 1214 | if (!cdev) |
| 1215 | return; |
| 1216 | |
| 1217 | mutex_lock(&thermal_list_lock); |
| 1218 | list_for_each_entry(pos, &thermal_cdev_list, node) |
| 1219 | if (pos == cdev) |
| 1220 | break; |
| 1221 | if (pos != cdev) { |
| 1222 | /* thermal cooling device not found */ |
| 1223 | mutex_unlock(&thermal_list_lock); |
| 1224 | return; |
| 1225 | } |
| 1226 | list_del(&cdev->node); |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 1227 | |
| 1228 | /* Unbind all thermal zones associated with 'this' cdev */ |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1229 | list_for_each_entry(tz, &thermal_tz_list, node) { |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 1230 | if (tz->ops->unbind) { |
| 1231 | tz->ops->unbind(tz, cdev); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1232 | continue; |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | if (!tz->tzp || !tz->tzp->tbp) |
| 1236 | continue; |
| 1237 | |
| 1238 | tzp = tz->tzp; |
| 1239 | for (i = 0; i < tzp->num_tbps; i++) { |
| 1240 | if (tzp->tbp[i].cdev == cdev) { |
| 1241 | __unbind(tz, tzp->tbp[i].trip_mask, cdev); |
| 1242 | tzp->tbp[i].cdev = NULL; |
| 1243 | } |
| 1244 | } |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1245 | } |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 1246 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1247 | mutex_unlock(&thermal_list_lock); |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 1248 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1249 | if (cdev->type[0]) |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 1250 | device_remove_file(&cdev->device, &dev_attr_cdev_type); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1251 | device_remove_file(&cdev->device, &dev_attr_max_state); |
| 1252 | device_remove_file(&cdev->device, &dev_attr_cur_state); |
| 1253 | |
| 1254 | release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id); |
| 1255 | device_unregister(&cdev->device); |
| 1256 | return; |
| 1257 | } |
| 1258 | EXPORT_SYMBOL(thermal_cooling_device_unregister); |
| 1259 | |
Durgadoss R | dc76548 | 2012-09-18 11:05:00 +0530 | [diff] [blame^] | 1260 | void thermal_cdev_update(struct thermal_cooling_device *cdev) |
Zhang Rui | ce119f8 | 2012-06-27 14:13:04 +0800 | [diff] [blame] | 1261 | { |
| 1262 | struct thermal_instance *instance; |
| 1263 | unsigned long target = 0; |
| 1264 | |
| 1265 | /* cooling device is updated*/ |
| 1266 | if (cdev->updated) |
| 1267 | return; |
| 1268 | |
Zhang Rui | f4a821c | 2012-07-24 16:56:21 +0800 | [diff] [blame] | 1269 | mutex_lock(&cdev->lock); |
Zhang Rui | ce119f8 | 2012-06-27 14:13:04 +0800 | [diff] [blame] | 1270 | /* Make sure cdev enters the deepest cooling state */ |
| 1271 | list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) { |
| 1272 | if (instance->target == THERMAL_NO_TARGET) |
| 1273 | continue; |
| 1274 | if (instance->target > target) |
| 1275 | target = instance->target; |
| 1276 | } |
Zhang Rui | f4a821c | 2012-07-24 16:56:21 +0800 | [diff] [blame] | 1277 | mutex_unlock(&cdev->lock); |
Zhang Rui | ce119f8 | 2012-06-27 14:13:04 +0800 | [diff] [blame] | 1278 | cdev->ops->set_cur_state(cdev, target); |
| 1279 | cdev->updated = true; |
| 1280 | } |
Durgadoss R | dc76548 | 2012-09-18 11:05:00 +0530 | [diff] [blame^] | 1281 | EXPORT_SYMBOL(thermal_cdev_update); |
Zhang Rui | ce119f8 | 2012-06-27 14:13:04 +0800 | [diff] [blame] | 1282 | |
| 1283 | static void thermal_zone_do_update(struct thermal_zone_device *tz) |
| 1284 | { |
| 1285 | struct thermal_instance *instance; |
| 1286 | |
| 1287 | list_for_each_entry(instance, &tz->thermal_instances, tz_node) |
Durgadoss R | dc76548 | 2012-09-18 11:05:00 +0530 | [diff] [blame^] | 1288 | thermal_cdev_update(instance->cdev); |
Zhang Rui | ce119f8 | 2012-06-27 14:13:04 +0800 | [diff] [blame] | 1289 | } |
| 1290 | |
Zhang Rui | 4ae46be | 2012-06-27 10:05:39 +0800 | [diff] [blame] | 1291 | /* |
Zhang Rui | 908b9fb | 2012-06-27 14:14:05 +0800 | [diff] [blame] | 1292 | * Cooling algorithm for both active and passive cooling |
Zhang Rui | 4ae46be | 2012-06-27 10:05:39 +0800 | [diff] [blame] | 1293 | * |
| 1294 | * 1. if the temperature is higher than a trip point, |
| 1295 | * a. if the trend is THERMAL_TREND_RAISING, use higher cooling |
| 1296 | * state for this trip point |
| 1297 | * b. if the trend is THERMAL_TREND_DROPPING, use lower cooling |
| 1298 | * state for this trip point |
| 1299 | * |
| 1300 | * 2. if the temperature is lower than a trip point, use lower |
| 1301 | * cooling state for this trip point |
| 1302 | * |
| 1303 | * Note that this behaves the same as the previous passive cooling |
| 1304 | * algorithm. |
| 1305 | */ |
| 1306 | |
| 1307 | static void thermal_zone_trip_update(struct thermal_zone_device *tz, |
| 1308 | int trip, long temp) |
| 1309 | { |
Zhang Rui | b81b6ba | 2012-06-27 10:08:19 +0800 | [diff] [blame] | 1310 | struct thermal_instance *instance; |
Zhang Rui | 4ae46be | 2012-06-27 10:05:39 +0800 | [diff] [blame] | 1311 | struct thermal_cooling_device *cdev = NULL; |
| 1312 | unsigned long cur_state, max_state; |
| 1313 | long trip_temp; |
Zhang Rui | 908b9fb | 2012-06-27 14:14:05 +0800 | [diff] [blame] | 1314 | enum thermal_trip_type trip_type; |
Zhang Rui | 4ae46be | 2012-06-27 10:05:39 +0800 | [diff] [blame] | 1315 | enum thermal_trend trend; |
| 1316 | |
Zhang Rui | 908b9fb | 2012-06-27 14:14:05 +0800 | [diff] [blame] | 1317 | if (trip == THERMAL_TRIPS_NONE) { |
| 1318 | trip_temp = tz->forced_passive; |
| 1319 | trip_type = THERMAL_TRIPS_NONE; |
| 1320 | } else { |
| 1321 | tz->ops->get_trip_temp(tz, trip, &trip_temp); |
| 1322 | tz->ops->get_trip_type(tz, trip, &trip_type); |
| 1323 | } |
Zhang Rui | 4ae46be | 2012-06-27 10:05:39 +0800 | [diff] [blame] | 1324 | |
| 1325 | if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) { |
| 1326 | /* |
| 1327 | * compare the current temperature and previous temperature |
| 1328 | * to get the thermal trend, if no special requirement |
| 1329 | */ |
| 1330 | if (tz->temperature > tz->last_temperature) |
| 1331 | trend = THERMAL_TREND_RAISING; |
| 1332 | else if (tz->temperature < tz->last_temperature) |
| 1333 | trend = THERMAL_TREND_DROPPING; |
| 1334 | else |
| 1335 | trend = THERMAL_TREND_STABLE; |
| 1336 | } |
| 1337 | |
| 1338 | if (temp >= trip_temp) { |
Zhang Rui | cddf31b | 2012-06-27 10:09:36 +0800 | [diff] [blame] | 1339 | list_for_each_entry(instance, &tz->thermal_instances, tz_node) { |
Zhang Rui | 4ae46be | 2012-06-27 10:05:39 +0800 | [diff] [blame] | 1340 | if (instance->trip != trip) |
| 1341 | continue; |
| 1342 | |
| 1343 | cdev = instance->cdev; |
| 1344 | |
| 1345 | cdev->ops->get_cur_state(cdev, &cur_state); |
| 1346 | cdev->ops->get_max_state(cdev, &max_state); |
| 1347 | |
| 1348 | if (trend == THERMAL_TREND_RAISING) { |
| 1349 | cur_state = cur_state < instance->upper ? |
| 1350 | (cur_state + 1) : instance->upper; |
| 1351 | } else if (trend == THERMAL_TREND_DROPPING) { |
| 1352 | cur_state = cur_state > instance->lower ? |
| 1353 | (cur_state - 1) : instance->lower; |
| 1354 | } |
Zhang Rui | 908b9fb | 2012-06-27 14:14:05 +0800 | [diff] [blame] | 1355 | |
| 1356 | /* activate a passive thermal instance */ |
| 1357 | if ((trip_type == THERMAL_TRIP_PASSIVE || |
| 1358 | trip_type == THERMAL_TRIPS_NONE) && |
| 1359 | instance->target == THERMAL_NO_TARGET) |
| 1360 | tz->passive++; |
| 1361 | |
Zhang Rui | ce119f8 | 2012-06-27 14:13:04 +0800 | [diff] [blame] | 1362 | instance->target = cur_state; |
| 1363 | cdev->updated = false; /* cooling device needs update */ |
Zhang Rui | 4ae46be | 2012-06-27 10:05:39 +0800 | [diff] [blame] | 1364 | } |
| 1365 | } else { /* below trip */ |
Zhang Rui | cddf31b | 2012-06-27 10:09:36 +0800 | [diff] [blame] | 1366 | list_for_each_entry(instance, &tz->thermal_instances, tz_node) { |
Zhang Rui | 4ae46be | 2012-06-27 10:05:39 +0800 | [diff] [blame] | 1367 | if (instance->trip != trip) |
| 1368 | continue; |
| 1369 | |
Zhang Rui | ce119f8 | 2012-06-27 14:13:04 +0800 | [diff] [blame] | 1370 | /* Do not use the inactive thermal instance */ |
| 1371 | if (instance->target == THERMAL_NO_TARGET) |
| 1372 | continue; |
Zhang Rui | 4ae46be | 2012-06-27 10:05:39 +0800 | [diff] [blame] | 1373 | cdev = instance->cdev; |
| 1374 | cdev->ops->get_cur_state(cdev, &cur_state); |
| 1375 | |
| 1376 | cur_state = cur_state > instance->lower ? |
Zhang Rui | ce119f8 | 2012-06-27 14:13:04 +0800 | [diff] [blame] | 1377 | (cur_state - 1) : THERMAL_NO_TARGET; |
Zhang Rui | 908b9fb | 2012-06-27 14:14:05 +0800 | [diff] [blame] | 1378 | |
| 1379 | /* deactivate a passive thermal instance */ |
| 1380 | if ((trip_type == THERMAL_TRIP_PASSIVE || |
| 1381 | trip_type == THERMAL_TRIPS_NONE) && |
| 1382 | cur_state == THERMAL_NO_TARGET) |
| 1383 | tz->passive--; |
Zhang Rui | ce119f8 | 2012-06-27 14:13:04 +0800 | [diff] [blame] | 1384 | instance->target = cur_state; |
| 1385 | cdev->updated = false; /* cooling device needs update */ |
Zhang Rui | 4ae46be | 2012-06-27 10:05:39 +0800 | [diff] [blame] | 1386 | } |
| 1387 | } |
| 1388 | |
| 1389 | return; |
| 1390 | } |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1391 | /** |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1392 | * thermal_zone_device_update - force an update of a thermal zone's state |
| 1393 | * @ttz: the thermal zone to update |
| 1394 | */ |
| 1395 | |
| 1396 | void thermal_zone_device_update(struct thermal_zone_device *tz) |
| 1397 | { |
| 1398 | int count, ret = 0; |
| 1399 | long temp, trip_temp; |
| 1400 | enum thermal_trip_type trip_type; |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1401 | |
| 1402 | mutex_lock(&tz->lock); |
| 1403 | |
Michael Brunner | 0d28816 | 2009-08-26 14:29:25 -0700 | [diff] [blame] | 1404 | if (tz->ops->get_temp(tz, &temp)) { |
| 1405 | /* get_temp failed - retry it later */ |
Joe Perches | c5a01dd | 2012-03-21 12:55:02 -0700 | [diff] [blame] | 1406 | pr_warn("failed to read out thermal zone %d\n", tz->id); |
Michael Brunner | 0d28816 | 2009-08-26 14:29:25 -0700 | [diff] [blame] | 1407 | goto leave; |
| 1408 | } |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1409 | |
Zhang Rui | 601f3d4 | 2012-06-27 09:54:33 +0800 | [diff] [blame] | 1410 | tz->last_temperature = tz->temperature; |
| 1411 | tz->temperature = temp; |
| 1412 | |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1413 | for (count = 0; count < tz->trips; count++) { |
| 1414 | tz->ops->get_trip_type(tz, count, &trip_type); |
| 1415 | tz->ops->get_trip_temp(tz, count, &trip_temp); |
| 1416 | |
| 1417 | switch (trip_type) { |
| 1418 | case THERMAL_TRIP_CRITICAL: |
Vladimir Zajac | 2932135 | 2009-05-06 19:34:21 +0200 | [diff] [blame] | 1419 | if (temp >= trip_temp) { |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1420 | if (tz->ops->notify) |
| 1421 | ret = tz->ops->notify(tz, count, |
| 1422 | trip_type); |
| 1423 | if (!ret) { |
Joe Perches | c5a01dd | 2012-03-21 12:55:02 -0700 | [diff] [blame] | 1424 | pr_emerg("Critical temperature reached (%ld C), shutting down\n", |
| 1425 | temp/1000); |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1426 | orderly_poweroff(true); |
| 1427 | } |
| 1428 | } |
| 1429 | break; |
| 1430 | case THERMAL_TRIP_HOT: |
Vladimir Zajac | 2932135 | 2009-05-06 19:34:21 +0200 | [diff] [blame] | 1431 | if (temp >= trip_temp) |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1432 | if (tz->ops->notify) |
| 1433 | tz->ops->notify(tz, count, trip_type); |
| 1434 | break; |
| 1435 | case THERMAL_TRIP_ACTIVE: |
Zhang Rui | 4ae46be | 2012-06-27 10:05:39 +0800 | [diff] [blame] | 1436 | thermal_zone_trip_update(tz, count, temp); |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1437 | break; |
| 1438 | case THERMAL_TRIP_PASSIVE: |
Vladimir Zajac | 2932135 | 2009-05-06 19:34:21 +0200 | [diff] [blame] | 1439 | if (temp >= trip_temp || tz->passive) |
Zhang Rui | 908b9fb | 2012-06-27 14:14:05 +0800 | [diff] [blame] | 1440 | thermal_zone_trip_update(tz, count, temp); |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1441 | break; |
| 1442 | } |
| 1443 | } |
Matthew Garrett | 03a971a | 2008-12-03 18:00:38 +0000 | [diff] [blame] | 1444 | |
| 1445 | if (tz->forced_passive) |
Zhang Rui | 908b9fb | 2012-06-27 14:14:05 +0800 | [diff] [blame] | 1446 | thermal_zone_trip_update(tz, THERMAL_TRIPS_NONE, temp); |
| 1447 | thermal_zone_do_update(tz); |
Michael Brunner | 0d28816 | 2009-08-26 14:29:25 -0700 | [diff] [blame] | 1448 | |
Joe Perches | caca8b8 | 2012-03-21 12:55:02 -0700 | [diff] [blame] | 1449 | leave: |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1450 | if (tz->passive) |
| 1451 | thermal_zone_device_set_polling(tz, tz->passive_delay); |
| 1452 | else if (tz->polling_delay) |
| 1453 | thermal_zone_device_set_polling(tz, tz->polling_delay); |
Frans Pop | 3767cb5 | 2009-10-26 08:39:04 +0100 | [diff] [blame] | 1454 | else |
| 1455 | thermal_zone_device_set_polling(tz, 0); |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1456 | mutex_unlock(&tz->lock); |
| 1457 | } |
| 1458 | EXPORT_SYMBOL(thermal_zone_device_update); |
| 1459 | |
| 1460 | /** |
Durgadoss R | c56f5c0 | 2012-07-25 10:10:58 +0800 | [diff] [blame] | 1461 | * create_trip_attrs - create attributes for trip points |
| 1462 | * @tz: the thermal zone device |
| 1463 | * @mask: Writeable trip point bitmap. |
| 1464 | */ |
| 1465 | static int create_trip_attrs(struct thermal_zone_device *tz, int mask) |
| 1466 | { |
| 1467 | int indx; |
Durgadoss R | 27365a6 | 2012-07-25 10:10:59 +0800 | [diff] [blame] | 1468 | int size = sizeof(struct thermal_attr) * tz->trips; |
Durgadoss R | c56f5c0 | 2012-07-25 10:10:58 +0800 | [diff] [blame] | 1469 | |
Durgadoss R | 27365a6 | 2012-07-25 10:10:59 +0800 | [diff] [blame] | 1470 | tz->trip_type_attrs = kzalloc(size, GFP_KERNEL); |
Durgadoss R | c56f5c0 | 2012-07-25 10:10:58 +0800 | [diff] [blame] | 1471 | if (!tz->trip_type_attrs) |
| 1472 | return -ENOMEM; |
| 1473 | |
Durgadoss R | 27365a6 | 2012-07-25 10:10:59 +0800 | [diff] [blame] | 1474 | tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL); |
Durgadoss R | c56f5c0 | 2012-07-25 10:10:58 +0800 | [diff] [blame] | 1475 | if (!tz->trip_temp_attrs) { |
| 1476 | kfree(tz->trip_type_attrs); |
| 1477 | return -ENOMEM; |
| 1478 | } |
| 1479 | |
Durgadoss R | 27365a6 | 2012-07-25 10:10:59 +0800 | [diff] [blame] | 1480 | if (tz->ops->get_trip_hyst) { |
| 1481 | tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL); |
| 1482 | if (!tz->trip_hyst_attrs) { |
| 1483 | kfree(tz->trip_type_attrs); |
| 1484 | kfree(tz->trip_temp_attrs); |
| 1485 | return -ENOMEM; |
| 1486 | } |
| 1487 | } |
Durgadoss R | c56f5c0 | 2012-07-25 10:10:58 +0800 | [diff] [blame] | 1488 | |
Durgadoss R | 27365a6 | 2012-07-25 10:10:59 +0800 | [diff] [blame] | 1489 | |
| 1490 | for (indx = 0; indx < tz->trips; indx++) { |
Durgadoss R | c56f5c0 | 2012-07-25 10:10:58 +0800 | [diff] [blame] | 1491 | /* create trip type attribute */ |
| 1492 | snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH, |
| 1493 | "trip_point_%d_type", indx); |
| 1494 | |
| 1495 | sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr); |
| 1496 | tz->trip_type_attrs[indx].attr.attr.name = |
| 1497 | tz->trip_type_attrs[indx].name; |
| 1498 | tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO; |
| 1499 | tz->trip_type_attrs[indx].attr.show = trip_point_type_show; |
| 1500 | |
| 1501 | device_create_file(&tz->device, |
| 1502 | &tz->trip_type_attrs[indx].attr); |
| 1503 | |
| 1504 | /* create trip temp attribute */ |
| 1505 | snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH, |
| 1506 | "trip_point_%d_temp", indx); |
| 1507 | |
| 1508 | sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr); |
| 1509 | tz->trip_temp_attrs[indx].attr.attr.name = |
| 1510 | tz->trip_temp_attrs[indx].name; |
| 1511 | tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO; |
| 1512 | tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show; |
| 1513 | if (mask & (1 << indx)) { |
| 1514 | tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR; |
| 1515 | tz->trip_temp_attrs[indx].attr.store = |
| 1516 | trip_point_temp_store; |
| 1517 | } |
| 1518 | |
| 1519 | device_create_file(&tz->device, |
| 1520 | &tz->trip_temp_attrs[indx].attr); |
Durgadoss R | 27365a6 | 2012-07-25 10:10:59 +0800 | [diff] [blame] | 1521 | |
| 1522 | /* create Optional trip hyst attribute */ |
| 1523 | if (!tz->ops->get_trip_hyst) |
| 1524 | continue; |
| 1525 | snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH, |
| 1526 | "trip_point_%d_hyst", indx); |
| 1527 | |
| 1528 | sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr); |
| 1529 | tz->trip_hyst_attrs[indx].attr.attr.name = |
| 1530 | tz->trip_hyst_attrs[indx].name; |
| 1531 | tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO; |
| 1532 | tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show; |
| 1533 | if (tz->ops->set_trip_hyst) { |
| 1534 | tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR; |
| 1535 | tz->trip_hyst_attrs[indx].attr.store = |
| 1536 | trip_point_hyst_store; |
| 1537 | } |
| 1538 | |
| 1539 | device_create_file(&tz->device, |
| 1540 | &tz->trip_hyst_attrs[indx].attr); |
Durgadoss R | c56f5c0 | 2012-07-25 10:10:58 +0800 | [diff] [blame] | 1541 | } |
| 1542 | return 0; |
| 1543 | } |
| 1544 | |
| 1545 | static void remove_trip_attrs(struct thermal_zone_device *tz) |
| 1546 | { |
| 1547 | int indx; |
| 1548 | |
| 1549 | for (indx = 0; indx < tz->trips; indx++) { |
| 1550 | device_remove_file(&tz->device, |
| 1551 | &tz->trip_type_attrs[indx].attr); |
| 1552 | device_remove_file(&tz->device, |
| 1553 | &tz->trip_temp_attrs[indx].attr); |
Durgadoss R | 27365a6 | 2012-07-25 10:10:59 +0800 | [diff] [blame] | 1554 | if (tz->ops->get_trip_hyst) |
| 1555 | device_remove_file(&tz->device, |
| 1556 | &tz->trip_hyst_attrs[indx].attr); |
Durgadoss R | c56f5c0 | 2012-07-25 10:10:58 +0800 | [diff] [blame] | 1557 | } |
| 1558 | kfree(tz->trip_type_attrs); |
| 1559 | kfree(tz->trip_temp_attrs); |
Durgadoss R | 27365a6 | 2012-07-25 10:10:59 +0800 | [diff] [blame] | 1560 | kfree(tz->trip_hyst_attrs); |
Durgadoss R | c56f5c0 | 2012-07-25 10:10:58 +0800 | [diff] [blame] | 1561 | } |
| 1562 | |
| 1563 | /** |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1564 | * thermal_zone_device_register - register a new thermal zone device |
| 1565 | * @type: the thermal zone device type |
| 1566 | * @trips: the number of trip points the thermal zone support |
Durgadoss R | c56f5c0 | 2012-07-25 10:10:58 +0800 | [diff] [blame] | 1567 | * @mask: a bit string indicating the writeablility of trip points |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1568 | * @devdata: private device data |
| 1569 | * @ops: standard thermal zone device callbacks |
Durgadoss R | 50125a9 | 2012-09-18 11:04:56 +0530 | [diff] [blame] | 1570 | * @tzp: thermal zone platform parameters |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1571 | * @passive_delay: number of milliseconds to wait between polls when |
| 1572 | * performing passive cooling |
| 1573 | * @polling_delay: number of milliseconds to wait between polls when checking |
| 1574 | * whether trip points have been crossed (0 for interrupt |
| 1575 | * driven systems) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1576 | * |
| 1577 | * thermal_zone_device_unregister() must be called when the device is no |
Zhang Rui | 1b7ddb8 | 2012-06-27 09:51:12 +0800 | [diff] [blame] | 1578 | * longer needed. The passive cooling depends on the .get_trend() return value. |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1579 | */ |
Anton Vorontsov | 4b1bf58 | 2012-07-31 04:39:30 -0700 | [diff] [blame] | 1580 | struct thermal_zone_device *thermal_zone_device_register(const char *type, |
Durgadoss R | c56f5c0 | 2012-07-25 10:10:58 +0800 | [diff] [blame] | 1581 | int trips, int mask, void *devdata, |
Alan Cox | 5b275ce | 2010-11-11 15:27:29 +0000 | [diff] [blame] | 1582 | const struct thermal_zone_device_ops *ops, |
Durgadoss R | 50125a9 | 2012-09-18 11:04:56 +0530 | [diff] [blame] | 1583 | const struct thermal_zone_params *tzp, |
Zhang Rui | 1b7ddb8 | 2012-06-27 09:51:12 +0800 | [diff] [blame] | 1584 | int passive_delay, int polling_delay) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1585 | { |
| 1586 | struct thermal_zone_device *tz; |
Matthew Garrett | 03a971a | 2008-12-03 18:00:38 +0000 | [diff] [blame] | 1587 | enum thermal_trip_type trip_type; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1588 | int result; |
| 1589 | int count; |
Matthew Garrett | 03a971a | 2008-12-03 18:00:38 +0000 | [diff] [blame] | 1590 | int passive = 0; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1591 | |
Guenter Roeck | 204dd1d | 2012-08-07 22:36:45 -0700 | [diff] [blame] | 1592 | if (type && strlen(type) >= THERMAL_NAME_LENGTH) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 1593 | return ERR_PTR(-EINVAL); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1594 | |
Durgadoss R | c56f5c0 | 2012-07-25 10:10:58 +0800 | [diff] [blame] | 1595 | if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 1596 | return ERR_PTR(-EINVAL); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1597 | |
| 1598 | if (!ops || !ops->get_temp) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 1599 | return ERR_PTR(-EINVAL); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1600 | |
| 1601 | tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL); |
| 1602 | if (!tz) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 1603 | return ERR_PTR(-ENOMEM); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1604 | |
Zhang Rui | 2d37413 | 2012-06-27 10:09:00 +0800 | [diff] [blame] | 1605 | INIT_LIST_HEAD(&tz->thermal_instances); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1606 | idr_init(&tz->idr); |
| 1607 | mutex_init(&tz->lock); |
| 1608 | result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id); |
| 1609 | if (result) { |
| 1610 | kfree(tz); |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 1611 | return ERR_PTR(result); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1612 | } |
| 1613 | |
Guenter Roeck | 204dd1d | 2012-08-07 22:36:45 -0700 | [diff] [blame] | 1614 | strcpy(tz->type, type ? : ""); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1615 | tz->ops = ops; |
Durgadoss R | 50125a9 | 2012-09-18 11:04:56 +0530 | [diff] [blame] | 1616 | tz->tzp = tzp; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1617 | tz->device.class = &thermal_class; |
| 1618 | tz->devdata = devdata; |
| 1619 | tz->trips = trips; |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1620 | tz->passive_delay = passive_delay; |
| 1621 | tz->polling_delay = polling_delay; |
| 1622 | |
Kay Sievers | 354655e | 2009-01-06 10:44:37 -0800 | [diff] [blame] | 1623 | dev_set_name(&tz->device, "thermal_zone%d", tz->id); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1624 | result = device_register(&tz->device); |
| 1625 | if (result) { |
| 1626 | release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id); |
| 1627 | kfree(tz); |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 1628 | return ERR_PTR(result); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1629 | } |
| 1630 | |
| 1631 | /* sys I/F */ |
| 1632 | if (type) { |
| 1633 | result = device_create_file(&tz->device, &dev_attr_type); |
| 1634 | if (result) |
| 1635 | goto unregister; |
| 1636 | } |
| 1637 | |
| 1638 | result = device_create_file(&tz->device, &dev_attr_temp); |
| 1639 | if (result) |
| 1640 | goto unregister; |
| 1641 | |
| 1642 | if (ops->get_mode) { |
| 1643 | result = device_create_file(&tz->device, &dev_attr_mode); |
| 1644 | if (result) |
| 1645 | goto unregister; |
| 1646 | } |
| 1647 | |
Durgadoss R | c56f5c0 | 2012-07-25 10:10:58 +0800 | [diff] [blame] | 1648 | result = create_trip_attrs(tz, mask); |
| 1649 | if (result) |
| 1650 | goto unregister; |
| 1651 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1652 | for (count = 0; count < trips; count++) { |
Matthew Garrett | 03a971a | 2008-12-03 18:00:38 +0000 | [diff] [blame] | 1653 | tz->ops->get_trip_type(tz, count, &trip_type); |
| 1654 | if (trip_type == THERMAL_TRIP_PASSIVE) |
| 1655 | passive = 1; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1656 | } |
| 1657 | |
Durgadoss R | 5a2c090 | 2012-09-18 11:04:58 +0530 | [diff] [blame] | 1658 | if (!passive) { |
| 1659 | result = device_create_file(&tz->device, &dev_attr_passive); |
| 1660 | if (result) |
| 1661 | goto unregister; |
| 1662 | } |
Matthew Garrett | 03a971a | 2008-12-03 18:00:38 +0000 | [diff] [blame] | 1663 | |
Durgadoss R | 5a2c090 | 2012-09-18 11:04:58 +0530 | [diff] [blame] | 1664 | /* Create policy attribute */ |
| 1665 | result = device_create_file(&tz->device, &dev_attr_policy); |
Matthew Garrett | 03a971a | 2008-12-03 18:00:38 +0000 | [diff] [blame] | 1666 | if (result) |
| 1667 | goto unregister; |
| 1668 | |
Durgadoss R | a4a1548 | 2012-09-18 11:04:57 +0530 | [diff] [blame] | 1669 | /* Update 'this' zone's governor information */ |
| 1670 | mutex_lock(&thermal_governor_lock); |
| 1671 | |
| 1672 | if (tz->tzp) |
| 1673 | tz->governor = __find_governor(tz->tzp->governor_name); |
| 1674 | else |
| 1675 | tz->governor = __find_governor(DEFAULT_THERMAL_GOVERNOR); |
| 1676 | |
| 1677 | mutex_unlock(&thermal_governor_lock); |
| 1678 | |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 1679 | result = thermal_add_hwmon_sysfs(tz); |
| 1680 | if (result) |
| 1681 | goto unregister; |
| 1682 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1683 | mutex_lock(&thermal_list_lock); |
| 1684 | list_add_tail(&tz->node, &thermal_tz_list); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1685 | mutex_unlock(&thermal_list_lock); |
| 1686 | |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 1687 | /* Bind cooling devices for this zone */ |
| 1688 | bind_tz(tz); |
| 1689 | |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1690 | INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check); |
| 1691 | |
| 1692 | thermal_zone_device_update(tz); |
| 1693 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1694 | if (!result) |
| 1695 | return tz; |
| 1696 | |
Joe Perches | caca8b8 | 2012-03-21 12:55:02 -0700 | [diff] [blame] | 1697 | unregister: |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1698 | release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id); |
| 1699 | device_unregister(&tz->device); |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 1700 | return ERR_PTR(result); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1701 | } |
| 1702 | EXPORT_SYMBOL(thermal_zone_device_register); |
| 1703 | |
| 1704 | /** |
| 1705 | * thermal_device_unregister - removes the registered thermal zone device |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1706 | * @tz: the thermal zone device to remove |
| 1707 | */ |
| 1708 | void thermal_zone_device_unregister(struct thermal_zone_device *tz) |
| 1709 | { |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 1710 | int i; |
| 1711 | const struct thermal_zone_params *tzp; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1712 | struct thermal_cooling_device *cdev; |
| 1713 | struct thermal_zone_device *pos = NULL; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1714 | |
| 1715 | if (!tz) |
| 1716 | return; |
| 1717 | |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 1718 | tzp = tz->tzp; |
| 1719 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1720 | mutex_lock(&thermal_list_lock); |
| 1721 | list_for_each_entry(pos, &thermal_tz_list, node) |
| 1722 | if (pos == tz) |
| 1723 | break; |
| 1724 | if (pos != tz) { |
| 1725 | /* thermal zone device not found */ |
| 1726 | mutex_unlock(&thermal_list_lock); |
| 1727 | return; |
| 1728 | } |
| 1729 | list_del(&tz->node); |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 1730 | |
| 1731 | /* Unbind all cdevs associated with 'this' thermal zone */ |
| 1732 | list_for_each_entry(cdev, &thermal_cdev_list, node) { |
| 1733 | if (tz->ops->unbind) { |
| 1734 | tz->ops->unbind(tz, cdev); |
| 1735 | continue; |
| 1736 | } |
| 1737 | |
| 1738 | if (!tzp || !tzp->tbp) |
| 1739 | break; |
| 1740 | |
| 1741 | for (i = 0; i < tzp->num_tbps; i++) { |
| 1742 | if (tzp->tbp[i].cdev == cdev) { |
| 1743 | __unbind(tz, tzp->tbp[i].trip_mask, cdev); |
| 1744 | tzp->tbp[i].cdev = NULL; |
| 1745 | } |
| 1746 | } |
| 1747 | } |
| 1748 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1749 | mutex_unlock(&thermal_list_lock); |
| 1750 | |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1751 | thermal_zone_device_set_polling(tz, 0); |
| 1752 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1753 | if (tz->type[0]) |
| 1754 | device_remove_file(&tz->device, &dev_attr_type); |
| 1755 | device_remove_file(&tz->device, &dev_attr_temp); |
| 1756 | if (tz->ops->get_mode) |
| 1757 | device_remove_file(&tz->device, &dev_attr_mode); |
Durgadoss R | 5a2c090 | 2012-09-18 11:04:58 +0530 | [diff] [blame] | 1758 | device_remove_file(&tz->device, &dev_attr_policy); |
Durgadoss R | c56f5c0 | 2012-07-25 10:10:58 +0800 | [diff] [blame] | 1759 | remove_trip_attrs(tz); |
Durgadoss R | a4a1548 | 2012-09-18 11:04:57 +0530 | [diff] [blame] | 1760 | tz->governor = NULL; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1761 | |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 1762 | thermal_remove_hwmon_sysfs(tz); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1763 | release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id); |
| 1764 | idr_destroy(&tz->idr); |
| 1765 | mutex_destroy(&tz->lock); |
| 1766 | device_unregister(&tz->device); |
| 1767 | return; |
| 1768 | } |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1769 | EXPORT_SYMBOL(thermal_zone_device_unregister); |
| 1770 | |
Rafael J. Wysocki | af06216 | 2011-03-01 01:12:19 +0100 | [diff] [blame] | 1771 | #ifdef CONFIG_NET |
| 1772 | static struct genl_family thermal_event_genl_family = { |
| 1773 | .id = GENL_ID_GENERATE, |
| 1774 | .name = THERMAL_GENL_FAMILY_NAME, |
| 1775 | .version = THERMAL_GENL_VERSION, |
| 1776 | .maxattr = THERMAL_GENL_ATTR_MAX, |
| 1777 | }; |
| 1778 | |
| 1779 | static struct genl_multicast_group thermal_event_mcgrp = { |
| 1780 | .name = THERMAL_GENL_MCAST_GROUP_NAME, |
| 1781 | }; |
| 1782 | |
Jean Delvare | 2d58d7e | 2011-11-04 10:31:04 +0100 | [diff] [blame] | 1783 | int thermal_generate_netlink_event(u32 orig, enum events event) |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1784 | { |
| 1785 | struct sk_buff *skb; |
| 1786 | struct nlattr *attr; |
| 1787 | struct thermal_genl_event *thermal_event; |
| 1788 | void *msg_header; |
| 1789 | int size; |
| 1790 | int result; |
Fabio Estevam | b11de07 | 2012-03-21 12:55:00 -0700 | [diff] [blame] | 1791 | static unsigned int thermal_event_seqnum; |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1792 | |
| 1793 | /* allocate memory */ |
Joe Perches | 886ee54 | 2012-03-21 12:55:01 -0700 | [diff] [blame] | 1794 | size = nla_total_size(sizeof(struct thermal_genl_event)) + |
| 1795 | nla_total_size(0); |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1796 | |
| 1797 | skb = genlmsg_new(size, GFP_ATOMIC); |
| 1798 | if (!skb) |
| 1799 | return -ENOMEM; |
| 1800 | |
| 1801 | /* add the genetlink message header */ |
| 1802 | msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++, |
| 1803 | &thermal_event_genl_family, 0, |
| 1804 | THERMAL_GENL_CMD_EVENT); |
| 1805 | if (!msg_header) { |
| 1806 | nlmsg_free(skb); |
| 1807 | return -ENOMEM; |
| 1808 | } |
| 1809 | |
| 1810 | /* fill the data */ |
Joe Perches | 886ee54 | 2012-03-21 12:55:01 -0700 | [diff] [blame] | 1811 | attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT, |
| 1812 | sizeof(struct thermal_genl_event)); |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1813 | |
| 1814 | if (!attr) { |
| 1815 | nlmsg_free(skb); |
| 1816 | return -EINVAL; |
| 1817 | } |
| 1818 | |
| 1819 | thermal_event = nla_data(attr); |
| 1820 | if (!thermal_event) { |
| 1821 | nlmsg_free(skb); |
| 1822 | return -EINVAL; |
| 1823 | } |
| 1824 | |
| 1825 | memset(thermal_event, 0, sizeof(struct thermal_genl_event)); |
| 1826 | |
| 1827 | thermal_event->orig = orig; |
| 1828 | thermal_event->event = event; |
| 1829 | |
| 1830 | /* send multicast genetlink message */ |
| 1831 | result = genlmsg_end(skb, msg_header); |
| 1832 | if (result < 0) { |
| 1833 | nlmsg_free(skb); |
| 1834 | return result; |
| 1835 | } |
| 1836 | |
| 1837 | result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC); |
| 1838 | if (result) |
Joe Perches | c5a01dd | 2012-03-21 12:55:02 -0700 | [diff] [blame] | 1839 | pr_info("failed to send netlink event:%d\n", result); |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1840 | |
| 1841 | return result; |
| 1842 | } |
Jean Delvare | 2d58d7e | 2011-11-04 10:31:04 +0100 | [diff] [blame] | 1843 | EXPORT_SYMBOL(thermal_generate_netlink_event); |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1844 | |
| 1845 | static int genetlink_init(void) |
| 1846 | { |
| 1847 | int result; |
| 1848 | |
| 1849 | result = genl_register_family(&thermal_event_genl_family); |
| 1850 | if (result) |
| 1851 | return result; |
| 1852 | |
| 1853 | result = genl_register_mc_group(&thermal_event_genl_family, |
| 1854 | &thermal_event_mcgrp); |
| 1855 | if (result) |
| 1856 | genl_unregister_family(&thermal_event_genl_family); |
| 1857 | return result; |
| 1858 | } |
| 1859 | |
Rafael J. Wysocki | af06216 | 2011-03-01 01:12:19 +0100 | [diff] [blame] | 1860 | static void genetlink_exit(void) |
| 1861 | { |
| 1862 | genl_unregister_family(&thermal_event_genl_family); |
| 1863 | } |
| 1864 | #else /* !CONFIG_NET */ |
| 1865 | static inline int genetlink_init(void) { return 0; } |
| 1866 | static inline void genetlink_exit(void) {} |
| 1867 | #endif /* !CONFIG_NET */ |
| 1868 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1869 | static int __init thermal_init(void) |
| 1870 | { |
| 1871 | int result = 0; |
| 1872 | |
| 1873 | result = class_register(&thermal_class); |
| 1874 | if (result) { |
| 1875 | idr_destroy(&thermal_tz_idr); |
| 1876 | idr_destroy(&thermal_cdev_idr); |
| 1877 | mutex_destroy(&thermal_idr_lock); |
| 1878 | mutex_destroy(&thermal_list_lock); |
| 1879 | } |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1880 | result = genetlink_init(); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1881 | return result; |
| 1882 | } |
| 1883 | |
| 1884 | static void __exit thermal_exit(void) |
| 1885 | { |
| 1886 | class_unregister(&thermal_class); |
| 1887 | idr_destroy(&thermal_tz_idr); |
| 1888 | idr_destroy(&thermal_cdev_idr); |
| 1889 | mutex_destroy(&thermal_idr_lock); |
| 1890 | mutex_destroy(&thermal_list_lock); |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1891 | genetlink_exit(); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1892 | } |
| 1893 | |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1894 | fs_initcall(thermal_init); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1895 | module_exit(thermal_exit); |