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