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