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