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