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 | * |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; version 2 of the License. |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 11 | */ |
| 12 | |
Joe Perches | c5a01dd | 2012-03-21 12:55:02 -0700 | [diff] [blame] | 13 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 14 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 15 | #include <linux/module.h> |
| 16 | #include <linux/device.h> |
| 17 | #include <linux/err.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 19 | #include <linux/kdev_t.h> |
| 20 | #include <linux/idr.h> |
| 21 | #include <linux/thermal.h> |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 22 | #include <linux/reboot.h> |
Andy Shevchenko | 42a5bf5 | 2013-05-17 11:52:02 +0000 | [diff] [blame] | 23 | #include <linux/string.h> |
Eduardo Valentin | a116b5d | 2013-09-26 15:55:01 -0400 | [diff] [blame] | 24 | #include <linux/of.h> |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 25 | #include <net/netlink.h> |
| 26 | #include <net/genetlink.h> |
Zhang Rui | ff140fe | 2015-10-30 16:31:58 +0800 | [diff] [blame] | 27 | #include <linux/suspend.h> |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 28 | |
Punit Agrawal | 100a8fd | 2014-07-29 11:50:48 +0100 | [diff] [blame] | 29 | #define CREATE_TRACE_POINTS |
| 30 | #include <trace/events/thermal.h> |
| 31 | |
Durgadoss R | 71350db | 2012-09-18 11:04:53 +0530 | [diff] [blame] | 32 | #include "thermal_core.h" |
Eduardo Valentin | 0dd8879 | 2013-07-03 15:14:28 -0400 | [diff] [blame] | 33 | #include "thermal_hwmon.h" |
Durgadoss R | 71350db | 2012-09-18 11:04:53 +0530 | [diff] [blame] | 34 | |
Zhang Rui | 63c4ec9 | 2008-04-21 16:07:13 +0800 | [diff] [blame] | 35 | MODULE_AUTHOR("Zhang Rui"); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 36 | MODULE_DESCRIPTION("Generic thermal management sysfs support"); |
Eduardo Valentin | 6d8d497 | 2013-04-23 21:48:13 +0000 | [diff] [blame] | 37 | MODULE_LICENSE("GPL v2"); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 38 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 39 | static DEFINE_IDR(thermal_tz_idr); |
| 40 | static DEFINE_IDR(thermal_cdev_idr); |
| 41 | static DEFINE_MUTEX(thermal_idr_lock); |
| 42 | |
| 43 | static LIST_HEAD(thermal_tz_list); |
| 44 | static LIST_HEAD(thermal_cdev_list); |
Durgadoss R | a4a1548 | 2012-09-18 11:04:57 +0530 | [diff] [blame] | 45 | static LIST_HEAD(thermal_governor_list); |
| 46 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 47 | static DEFINE_MUTEX(thermal_list_lock); |
Durgadoss R | a4a1548 | 2012-09-18 11:04:57 +0530 | [diff] [blame] | 48 | static DEFINE_MUTEX(thermal_governor_lock); |
| 49 | |
Zhang Rui | ff140fe | 2015-10-30 16:31:58 +0800 | [diff] [blame] | 50 | static atomic_t in_suspend; |
| 51 | |
Zhang Rui | f2234bc | 2014-01-24 10:23:19 +0800 | [diff] [blame] | 52 | static struct thermal_governor *def_governor; |
| 53 | |
Eduardo Valentin | 1b4f484 | 2016-11-07 21:09:05 -0800 | [diff] [blame] | 54 | /* |
| 55 | * Governor section: set of functions to handle thermal governors |
| 56 | * |
| 57 | * Functions to help in the life cycle of thermal governors within |
| 58 | * the thermal core and by the thermal governor code. |
| 59 | */ |
| 60 | |
Durgadoss R | a4a1548 | 2012-09-18 11:04:57 +0530 | [diff] [blame] | 61 | static struct thermal_governor *__find_governor(const char *name) |
| 62 | { |
| 63 | struct thermal_governor *pos; |
| 64 | |
Zhang Rui | f2234bc | 2014-01-24 10:23:19 +0800 | [diff] [blame] | 65 | if (!name || !name[0]) |
| 66 | return def_governor; |
| 67 | |
Durgadoss R | a4a1548 | 2012-09-18 11:04:57 +0530 | [diff] [blame] | 68 | list_for_each_entry(pos, &thermal_governor_list, governor_list) |
Rasmus Villemoes | 484ac2f | 2014-10-13 15:55:01 -0700 | [diff] [blame] | 69 | if (!strncasecmp(name, pos->name, THERMAL_NAME_LENGTH)) |
Durgadoss R | a4a1548 | 2012-09-18 11:04:57 +0530 | [diff] [blame] | 70 | return pos; |
| 71 | |
| 72 | return NULL; |
| 73 | } |
| 74 | |
Javi Merino | e33df1d | 2015-02-26 19:00:27 +0000 | [diff] [blame] | 75 | /** |
| 76 | * bind_previous_governor() - bind the previous governor of the thermal zone |
| 77 | * @tz: a valid pointer to a struct thermal_zone_device |
| 78 | * @failed_gov_name: the name of the governor that failed to register |
| 79 | * |
| 80 | * Register the previous governor of the thermal zone after a new |
| 81 | * governor has failed to be bound. |
| 82 | */ |
| 83 | static void bind_previous_governor(struct thermal_zone_device *tz, |
| 84 | const char *failed_gov_name) |
| 85 | { |
| 86 | if (tz->governor && tz->governor->bind_to_tz) { |
| 87 | if (tz->governor->bind_to_tz(tz)) { |
| 88 | dev_err(&tz->device, |
| 89 | "governor %s failed to bind and the previous one (%s) failed to bind again, thermal zone %s has no governor\n", |
| 90 | failed_gov_name, tz->governor->name, tz->type); |
| 91 | tz->governor = NULL; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * thermal_set_governor() - Switch to another governor |
| 98 | * @tz: a valid pointer to a struct thermal_zone_device |
| 99 | * @new_gov: pointer to the new governor |
| 100 | * |
| 101 | * Change the governor of thermal zone @tz. |
| 102 | * |
| 103 | * Return: 0 on success, an error if the new governor's bind_to_tz() failed. |
| 104 | */ |
| 105 | static int thermal_set_governor(struct thermal_zone_device *tz, |
| 106 | struct thermal_governor *new_gov) |
| 107 | { |
| 108 | int ret = 0; |
| 109 | |
| 110 | if (tz->governor && tz->governor->unbind_from_tz) |
| 111 | tz->governor->unbind_from_tz(tz); |
| 112 | |
| 113 | if (new_gov && new_gov->bind_to_tz) { |
| 114 | ret = new_gov->bind_to_tz(tz); |
| 115 | if (ret) { |
| 116 | bind_previous_governor(tz, new_gov->name); |
| 117 | |
| 118 | return ret; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | tz->governor = new_gov; |
| 123 | |
| 124 | return ret; |
| 125 | } |
| 126 | |
Durgadoss R | a4a1548 | 2012-09-18 11:04:57 +0530 | [diff] [blame] | 127 | int thermal_register_governor(struct thermal_governor *governor) |
| 128 | { |
| 129 | int err; |
| 130 | const char *name; |
| 131 | struct thermal_zone_device *pos; |
| 132 | |
| 133 | if (!governor) |
| 134 | return -EINVAL; |
| 135 | |
| 136 | mutex_lock(&thermal_governor_lock); |
| 137 | |
| 138 | err = -EBUSY; |
Eduardo Valentin | 5027ba3 | 2016-11-07 21:09:20 -0800 | [diff] [blame] | 139 | if (!__find_governor(governor->name)) { |
Eduardo Valentin | eb7be32 | 2016-11-07 21:09:21 -0800 | [diff] [blame] | 140 | bool match_default; |
| 141 | |
Durgadoss R | a4a1548 | 2012-09-18 11:04:57 +0530 | [diff] [blame] | 142 | err = 0; |
| 143 | list_add(&governor->governor_list, &thermal_governor_list); |
Eduardo Valentin | eb7be32 | 2016-11-07 21:09:21 -0800 | [diff] [blame] | 144 | match_default = !strncmp(governor->name, |
| 145 | DEFAULT_THERMAL_GOVERNOR, |
| 146 | THERMAL_NAME_LENGTH); |
| 147 | |
| 148 | if (!def_governor && match_default) |
Zhang Rui | f2234bc | 2014-01-24 10:23:19 +0800 | [diff] [blame] | 149 | def_governor = governor; |
Durgadoss R | a4a1548 | 2012-09-18 11:04:57 +0530 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | mutex_lock(&thermal_list_lock); |
| 153 | |
| 154 | list_for_each_entry(pos, &thermal_tz_list, node) { |
Zhang Rui | f2234bc | 2014-01-24 10:23:19 +0800 | [diff] [blame] | 155 | /* |
| 156 | * only thermal zones with specified tz->tzp->governor_name |
| 157 | * may run with tz->govenor unset |
| 158 | */ |
Durgadoss R | a4a1548 | 2012-09-18 11:04:57 +0530 | [diff] [blame] | 159 | if (pos->governor) |
| 160 | continue; |
Zhang Rui | f2234bc | 2014-01-24 10:23:19 +0800 | [diff] [blame] | 161 | |
| 162 | name = pos->tzp->governor_name; |
| 163 | |
Javi Merino | e33df1d | 2015-02-26 19:00:27 +0000 | [diff] [blame] | 164 | if (!strncasecmp(name, governor->name, THERMAL_NAME_LENGTH)) { |
| 165 | int ret; |
| 166 | |
| 167 | ret = thermal_set_governor(pos, governor); |
| 168 | if (ret) |
| 169 | dev_err(&pos->device, |
| 170 | "Failed to set governor %s for thermal zone %s: %d\n", |
| 171 | governor->name, pos->type, ret); |
| 172 | } |
Durgadoss R | a4a1548 | 2012-09-18 11:04:57 +0530 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | mutex_unlock(&thermal_list_lock); |
| 176 | mutex_unlock(&thermal_governor_lock); |
| 177 | |
| 178 | return err; |
| 179 | } |
Durgadoss R | a4a1548 | 2012-09-18 11:04:57 +0530 | [diff] [blame] | 180 | |
| 181 | void thermal_unregister_governor(struct thermal_governor *governor) |
| 182 | { |
| 183 | struct thermal_zone_device *pos; |
| 184 | |
| 185 | if (!governor) |
| 186 | return; |
| 187 | |
| 188 | mutex_lock(&thermal_governor_lock); |
| 189 | |
Eduardo Valentin | 5027ba3 | 2016-11-07 21:09:20 -0800 | [diff] [blame] | 190 | if (!__find_governor(governor->name)) |
Durgadoss R | a4a1548 | 2012-09-18 11:04:57 +0530 | [diff] [blame] | 191 | goto exit; |
| 192 | |
| 193 | mutex_lock(&thermal_list_lock); |
| 194 | |
| 195 | list_for_each_entry(pos, &thermal_tz_list, node) { |
Rasmus Villemoes | 484ac2f | 2014-10-13 15:55:01 -0700 | [diff] [blame] | 196 | if (!strncasecmp(pos->governor->name, governor->name, |
Eduardo Valentin | eb7be32 | 2016-11-07 21:09:21 -0800 | [diff] [blame] | 197 | THERMAL_NAME_LENGTH)) |
Javi Merino | e33df1d | 2015-02-26 19:00:27 +0000 | [diff] [blame] | 198 | thermal_set_governor(pos, NULL); |
Durgadoss R | a4a1548 | 2012-09-18 11:04:57 +0530 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | mutex_unlock(&thermal_list_lock); |
| 202 | list_del(&governor->governor_list); |
| 203 | exit: |
| 204 | mutex_unlock(&thermal_governor_lock); |
Durgadoss R | a4a1548 | 2012-09-18 11:04:57 +0530 | [diff] [blame] | 205 | } |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 206 | |
Eduardo Valentin | 1b4f484 | 2016-11-07 21:09:05 -0800 | [diff] [blame] | 207 | int thermal_zone_device_set_policy(struct thermal_zone_device *tz, |
| 208 | char *policy) |
| 209 | { |
| 210 | struct thermal_governor *gov; |
| 211 | int ret = -EINVAL; |
| 212 | |
| 213 | mutex_lock(&thermal_governor_lock); |
| 214 | mutex_lock(&tz->lock); |
| 215 | |
| 216 | gov = __find_governor(strim(policy)); |
| 217 | if (!gov) |
| 218 | goto exit; |
| 219 | |
| 220 | ret = thermal_set_governor(tz, gov); |
| 221 | |
| 222 | exit: |
| 223 | mutex_unlock(&tz->lock); |
| 224 | mutex_unlock(&thermal_governor_lock); |
| 225 | |
| 226 | return ret; |
| 227 | } |
| 228 | |
| 229 | int thermal_build_list_of_policies(char *buf) |
| 230 | { |
| 231 | struct thermal_governor *pos; |
| 232 | ssize_t count = 0; |
| 233 | ssize_t size = PAGE_SIZE; |
| 234 | |
| 235 | mutex_lock(&thermal_governor_lock); |
| 236 | |
| 237 | list_for_each_entry(pos, &thermal_governor_list, governor_list) { |
| 238 | size = PAGE_SIZE - count; |
| 239 | count += scnprintf(buf + count, size, "%s ", pos->name); |
| 240 | } |
| 241 | count += scnprintf(buf + count, size, "\n"); |
| 242 | |
| 243 | mutex_unlock(&thermal_governor_lock); |
| 244 | |
| 245 | return count; |
| 246 | } |
| 247 | |
| 248 | static int __init thermal_register_governors(void) |
| 249 | { |
| 250 | int result; |
| 251 | |
| 252 | result = thermal_gov_step_wise_register(); |
| 253 | if (result) |
| 254 | return result; |
| 255 | |
| 256 | result = thermal_gov_fair_share_register(); |
| 257 | if (result) |
| 258 | return result; |
| 259 | |
| 260 | result = thermal_gov_bang_bang_register(); |
| 261 | if (result) |
| 262 | return result; |
| 263 | |
| 264 | result = thermal_gov_user_space_register(); |
| 265 | if (result) |
| 266 | return result; |
| 267 | |
| 268 | return thermal_gov_power_allocator_register(); |
| 269 | } |
| 270 | |
| 271 | static void thermal_unregister_governors(void) |
| 272 | { |
| 273 | thermal_gov_step_wise_unregister(); |
| 274 | thermal_gov_fair_share_unregister(); |
| 275 | thermal_gov_bang_bang_unregister(); |
| 276 | thermal_gov_user_space_unregister(); |
| 277 | thermal_gov_power_allocator_unregister(); |
| 278 | } |
| 279 | |
Eduardo Valentin | 8772e18 | 2016-11-07 21:09:15 -0800 | [diff] [blame] | 280 | /* |
| 281 | * Zone update section: main control loop applied to each zone while monitoring |
| 282 | * |
| 283 | * in polling mode. The monitoring is done using a workqueue. |
| 284 | * Same update may be done on a zone by calling thermal_zone_device_update(). |
| 285 | * |
| 286 | * An update means: |
| 287 | * - Non-critical trips will invoke the governor responsible for that zone; |
| 288 | * - Hot trips will produce a notification to userspace; |
| 289 | * - Critical trip point will cause a system shutdown. |
| 290 | */ |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 291 | static void thermal_zone_device_set_polling(struct thermal_zone_device *tz, |
| 292 | int delay) |
| 293 | { |
| 294 | if (delay > 1000) |
| 295 | mod_delayed_work(system_freezable_wq, &tz->poll_queue, |
| 296 | round_jiffies(msecs_to_jiffies(delay))); |
| 297 | else if (delay) |
| 298 | mod_delayed_work(system_freezable_wq, &tz->poll_queue, |
| 299 | msecs_to_jiffies(delay)); |
| 300 | else |
| 301 | cancel_delayed_work(&tz->poll_queue); |
| 302 | } |
| 303 | |
| 304 | static void monitor_thermal_zone(struct thermal_zone_device *tz) |
| 305 | { |
| 306 | mutex_lock(&tz->lock); |
| 307 | |
| 308 | if (tz->passive) |
| 309 | thermal_zone_device_set_polling(tz, tz->passive_delay); |
| 310 | else if (tz->polling_delay) |
| 311 | thermal_zone_device_set_polling(tz, tz->polling_delay); |
| 312 | else |
| 313 | thermal_zone_device_set_polling(tz, 0); |
| 314 | |
| 315 | mutex_unlock(&tz->lock); |
| 316 | } |
| 317 | |
| 318 | static void handle_non_critical_trips(struct thermal_zone_device *tz, |
Eduardo Valentin | eb7be32 | 2016-11-07 21:09:21 -0800 | [diff] [blame] | 319 | int trip, |
| 320 | enum thermal_trip_type trip_type) |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 321 | { |
Zhang Rui | f2234bc | 2014-01-24 10:23:19 +0800 | [diff] [blame] | 322 | tz->governor ? tz->governor->throttle(tz, trip) : |
| 323 | def_governor->throttle(tz, trip); |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | static void handle_critical_trips(struct thermal_zone_device *tz, |
Eduardo Valentin | eb7be32 | 2016-11-07 21:09:21 -0800 | [diff] [blame] | 327 | int trip, enum thermal_trip_type trip_type) |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 328 | { |
Sascha Hauer | 17e8351 | 2015-07-24 08:12:54 +0200 | [diff] [blame] | 329 | int trip_temp; |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 330 | |
| 331 | tz->ops->get_trip_temp(tz, trip, &trip_temp); |
| 332 | |
| 333 | /* If we have not crossed the trip_temp, we do not care. */ |
Srinivas Pandruvada | 84ffe3e | 2014-11-12 15:43:29 -0800 | [diff] [blame] | 334 | if (trip_temp <= 0 || tz->temperature < trip_temp) |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 335 | return; |
| 336 | |
Punit Agrawal | 208cd82 | 2014-07-29 11:50:50 +0100 | [diff] [blame] | 337 | trace_thermal_zone_trip(tz, trip, trip_type); |
| 338 | |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 339 | if (tz->ops->notify) |
| 340 | tz->ops->notify(tz, trip, trip_type); |
| 341 | |
| 342 | if (trip_type == THERMAL_TRIP_CRITICAL) { |
Eduardo Valentin | 923e0b1 | 2013-01-02 15:29:41 +0000 | [diff] [blame] | 343 | dev_emerg(&tz->device, |
| 344 | "critical temperature reached(%d C),shutting down\n", |
| 345 | tz->temperature / 1000); |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 346 | orderly_poweroff(true); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | static void handle_thermal_trip(struct thermal_zone_device *tz, int trip) |
| 351 | { |
| 352 | enum thermal_trip_type type; |
| 353 | |
Zhang Rui | 81ad427 | 2016-03-18 10:03:24 +0800 | [diff] [blame] | 354 | /* Ignore disabled trip points */ |
| 355 | if (test_bit(trip, &tz->trips_disabled)) |
| 356 | return; |
| 357 | |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 358 | tz->ops->get_trip_type(tz, trip, &type); |
| 359 | |
| 360 | if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT) |
| 361 | handle_critical_trips(tz, trip, type); |
| 362 | else |
| 363 | handle_non_critical_trips(tz, trip, type); |
| 364 | /* |
| 365 | * Alright, we handled this trip successfully. |
| 366 | * So, start monitoring again. |
| 367 | */ |
| 368 | monitor_thermal_zone(tz); |
| 369 | } |
| 370 | |
| 371 | static void update_temperature(struct thermal_zone_device *tz) |
| 372 | { |
Sascha Hauer | 17e8351 | 2015-07-24 08:12:54 +0200 | [diff] [blame] | 373 | int temp, ret; |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 374 | |
Amit Daniel Kachhap | e6e238c | 2013-02-04 00:30:15 +0000 | [diff] [blame] | 375 | ret = thermal_zone_get_temp(tz, &temp); |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 376 | if (ret) { |
Hans de Goede | 7e497a7 | 2015-03-21 15:02:55 +0100 | [diff] [blame] | 377 | if (ret != -EAGAIN) |
| 378 | dev_warn(&tz->device, |
| 379 | "failed to read out thermal zone (%d)\n", |
| 380 | ret); |
Amit Daniel Kachhap | e6e238c | 2013-02-04 00:30:15 +0000 | [diff] [blame] | 381 | return; |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 382 | } |
| 383 | |
Amit Daniel Kachhap | e6e238c | 2013-02-04 00:30:15 +0000 | [diff] [blame] | 384 | mutex_lock(&tz->lock); |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 385 | tz->last_temperature = tz->temperature; |
| 386 | tz->temperature = temp; |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 387 | mutex_unlock(&tz->lock); |
Aaron Lu | 06475b5 | 2013-12-02 13:54:26 +0800 | [diff] [blame] | 388 | |
Punit Agrawal | 100a8fd | 2014-07-29 11:50:48 +0100 | [diff] [blame] | 389 | trace_thermal_temperature(tz); |
Zhang Rui | bb431ba | 2015-10-30 16:31:47 +0800 | [diff] [blame] | 390 | if (tz->last_temperature == THERMAL_TEMP_INVALID) |
| 391 | dev_dbg(&tz->device, "last_temperature N/A, current_temperature=%d\n", |
| 392 | tz->temperature); |
| 393 | else |
| 394 | dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n", |
| 395 | tz->last_temperature, tz->temperature); |
| 396 | } |
| 397 | |
| 398 | static void thermal_zone_device_reset(struct thermal_zone_device *tz) |
| 399 | { |
| 400 | struct thermal_instance *pos; |
| 401 | |
| 402 | tz->temperature = THERMAL_TEMP_INVALID; |
| 403 | tz->passive = 0; |
| 404 | list_for_each_entry(pos, &tz->thermal_instances, tz_node) |
| 405 | pos->initialized = false; |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 406 | } |
| 407 | |
Srinivas Pandruvada | 0e70f46 | 2016-08-26 16:21:16 -0700 | [diff] [blame] | 408 | void thermal_zone_device_update(struct thermal_zone_device *tz, |
| 409 | enum thermal_notify_event event) |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 410 | { |
| 411 | int count; |
| 412 | |
Zhang Rui | ff140fe | 2015-10-30 16:31:58 +0800 | [diff] [blame] | 413 | if (atomic_read(&in_suspend)) |
| 414 | return; |
| 415 | |
Eduardo Valentin | 81bd4e1 | 2013-09-12 19:15:44 -0400 | [diff] [blame] | 416 | if (!tz->ops->get_temp) |
| 417 | return; |
| 418 | |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 419 | update_temperature(tz); |
| 420 | |
Sascha Hauer | 060c034 | 2016-06-22 16:42:01 +0800 | [diff] [blame] | 421 | thermal_zone_set_trips(tz); |
| 422 | |
Srinivas Pandruvada | 0e70f46 | 2016-08-26 16:21:16 -0700 | [diff] [blame] | 423 | tz->notify_event = event; |
| 424 | |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 425 | for (count = 0; count < tz->trips; count++) |
| 426 | handle_thermal_trip(tz, count); |
| 427 | } |
Eduardo Valentin | 910cb1e | 2013-04-23 21:48:15 +0000 | [diff] [blame] | 428 | EXPORT_SYMBOL_GPL(thermal_zone_device_update); |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 429 | |
Eduardo Valentin | 106339a | 2016-11-07 21:09:14 -0800 | [diff] [blame] | 430 | /** |
| 431 | * thermal_notify_framework - Sensor drivers use this API to notify framework |
| 432 | * @tz: thermal zone device |
| 433 | * @trip: indicates which trip point has been crossed |
| 434 | * |
| 435 | * This function handles the trip events from sensor drivers. It starts |
| 436 | * throttling the cooling devices according to the policy configured. |
| 437 | * For CRITICAL and HOT trip points, this notifies the respective drivers, |
| 438 | * and does actual throttling for other trip points i.e ACTIVE and PASSIVE. |
| 439 | * The throttling policy is based on the configured platform data; if no |
| 440 | * platform data is provided, this uses the step_wise throttling policy. |
| 441 | */ |
| 442 | void thermal_notify_framework(struct thermal_zone_device *tz, int trip) |
| 443 | { |
| 444 | handle_thermal_trip(tz, trip); |
| 445 | } |
| 446 | EXPORT_SYMBOL_GPL(thermal_notify_framework); |
| 447 | |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 448 | static void thermal_zone_device_check(struct work_struct *work) |
| 449 | { |
| 450 | struct thermal_zone_device *tz = container_of(work, struct |
| 451 | thermal_zone_device, |
| 452 | poll_queue.work); |
Srinivas Pandruvada | 0e70f46 | 2016-08-26 16:21:16 -0700 | [diff] [blame] | 453 | thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 454 | } |
| 455 | |
Eduardo Valentin | 712afbd | 2016-11-07 21:09:16 -0800 | [diff] [blame] | 456 | /* |
| 457 | * Power actor section: interface to power actors to estimate power |
| 458 | * |
| 459 | * Set of functions used to interact to cooling devices that know |
| 460 | * how to estimate their devices power consumption. |
| 461 | */ |
| 462 | |
Eduardo Valentin | 1a7e7cc | 2016-11-07 21:08:47 -0800 | [diff] [blame] | 463 | /** |
| 464 | * power_actor_get_max_power() - get the maximum power that a cdev can consume |
| 465 | * @cdev: pointer to &thermal_cooling_device |
| 466 | * @tz: a valid thermal zone device pointer |
| 467 | * @max_power: pointer in which to store the maximum power |
| 468 | * |
| 469 | * Calculate the maximum power consumption in milliwats that the |
| 470 | * cooling device can currently consume and store it in @max_power. |
| 471 | * |
| 472 | * Return: 0 on success, -EINVAL if @cdev doesn't support the |
| 473 | * power_actor API or -E* on other error. |
| 474 | */ |
| 475 | int power_actor_get_max_power(struct thermal_cooling_device *cdev, |
| 476 | struct thermal_zone_device *tz, u32 *max_power) |
| 477 | { |
| 478 | if (!cdev_is_power_actor(cdev)) |
| 479 | return -EINVAL; |
| 480 | |
| 481 | return cdev->ops->state2power(cdev, tz, 0, max_power); |
| 482 | } |
| 483 | |
| 484 | /** |
| 485 | * power_actor_get_min_power() - get the mainimum power that a cdev can consume |
| 486 | * @cdev: pointer to &thermal_cooling_device |
| 487 | * @tz: a valid thermal zone device pointer |
| 488 | * @min_power: pointer in which to store the minimum power |
| 489 | * |
| 490 | * Calculate the minimum power consumption in milliwatts that the |
| 491 | * cooling device can currently consume and store it in @min_power. |
| 492 | * |
| 493 | * Return: 0 on success, -EINVAL if @cdev doesn't support the |
| 494 | * power_actor API or -E* on other error. |
| 495 | */ |
| 496 | int power_actor_get_min_power(struct thermal_cooling_device *cdev, |
| 497 | struct thermal_zone_device *tz, u32 *min_power) |
| 498 | { |
| 499 | unsigned long max_state; |
| 500 | int ret; |
| 501 | |
| 502 | if (!cdev_is_power_actor(cdev)) |
| 503 | return -EINVAL; |
| 504 | |
| 505 | ret = cdev->ops->get_max_state(cdev, &max_state); |
| 506 | if (ret) |
| 507 | return ret; |
| 508 | |
| 509 | return cdev->ops->state2power(cdev, tz, max_state, min_power); |
| 510 | } |
| 511 | |
| 512 | /** |
| 513 | * power_actor_set_power() - limit the maximum power a cooling device consumes |
| 514 | * @cdev: pointer to &thermal_cooling_device |
| 515 | * @instance: thermal instance to update |
| 516 | * @power: the power in milliwatts |
| 517 | * |
| 518 | * Set the cooling device to consume at most @power milliwatts. The limit is |
| 519 | * expected to be a cap at the maximum power consumption. |
| 520 | * |
| 521 | * Return: 0 on success, -EINVAL if the cooling device does not |
| 522 | * implement the power actor API or -E* for other failures. |
| 523 | */ |
| 524 | int power_actor_set_power(struct thermal_cooling_device *cdev, |
| 525 | struct thermal_instance *instance, u32 power) |
| 526 | { |
| 527 | unsigned long state; |
| 528 | int ret; |
| 529 | |
| 530 | if (!cdev_is_power_actor(cdev)) |
| 531 | return -EINVAL; |
| 532 | |
| 533 | ret = cdev->ops->power2state(cdev, instance->tz, power, &state); |
| 534 | if (ret) |
| 535 | return ret; |
| 536 | |
| 537 | instance->target = state; |
| 538 | mutex_lock(&cdev->lock); |
| 539 | cdev->updated = false; |
| 540 | mutex_unlock(&cdev->lock); |
| 541 | thermal_cdev_update(cdev); |
| 542 | |
| 543 | return 0; |
| 544 | } |
| 545 | |
Eduardo Valentin | 3d0055d | 2016-11-07 21:08:54 -0800 | [diff] [blame] | 546 | void thermal_zone_device_rebind_exception(struct thermal_zone_device *tz, |
| 547 | const char *cdev_type, size_t size) |
| 548 | { |
| 549 | struct thermal_cooling_device *cdev = NULL; |
| 550 | |
| 551 | mutex_lock(&thermal_list_lock); |
| 552 | list_for_each_entry(cdev, &thermal_cdev_list, node) { |
| 553 | /* skip non matching cdevs */ |
| 554 | if (strncmp(cdev_type, cdev->type, size)) |
| 555 | continue; |
| 556 | |
| 557 | /* re binding the exception matching the type pattern */ |
| 558 | thermal_zone_bind_cooling_device(tz, THERMAL_TRIPS_NONE, cdev, |
| 559 | THERMAL_NO_LIMIT, |
| 560 | THERMAL_NO_LIMIT, |
| 561 | THERMAL_WEIGHT_DEFAULT); |
| 562 | } |
| 563 | mutex_unlock(&thermal_list_lock); |
| 564 | } |
| 565 | |
| 566 | void thermal_zone_device_unbind_exception(struct thermal_zone_device *tz, |
| 567 | const char *cdev_type, size_t size) |
| 568 | { |
| 569 | struct thermal_cooling_device *cdev = NULL; |
| 570 | |
| 571 | mutex_lock(&thermal_list_lock); |
| 572 | list_for_each_entry(cdev, &thermal_cdev_list, node) { |
| 573 | /* skip non matching cdevs */ |
| 574 | if (strncmp(cdev_type, cdev->type, size)) |
| 575 | continue; |
| 576 | /* unbinding the exception matching the type pattern */ |
| 577 | thermal_zone_unbind_cooling_device(tz, THERMAL_TRIPS_NONE, |
| 578 | cdev); |
| 579 | } |
| 580 | mutex_unlock(&thermal_list_lock); |
| 581 | } |
| 582 | |
Eduardo Valentin | 81193e2 | 2016-11-07 21:09:17 -0800 | [diff] [blame] | 583 | /* |
| 584 | * Device management section: cooling devices, zones devices, and binding |
| 585 | * |
| 586 | * Set of functions provided by the thermal core for: |
| 587 | * - cooling devices lifecycle: registration, unregistration, |
| 588 | * binding, and unbinding. |
| 589 | * - thermal zone devices lifecycle: registration, unregistration, |
| 590 | * binding, and unbinding. |
| 591 | */ |
Eduardo Valentin | c30176f | 2016-11-07 21:09:06 -0800 | [diff] [blame] | 592 | static int get_idr(struct idr *idr, struct mutex *lock, int *id) |
| 593 | { |
| 594 | int ret; |
| 595 | |
| 596 | if (lock) |
| 597 | mutex_lock(lock); |
| 598 | ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL); |
| 599 | if (lock) |
| 600 | mutex_unlock(lock); |
| 601 | if (unlikely(ret < 0)) |
| 602 | return ret; |
| 603 | *id = ret; |
| 604 | return 0; |
| 605 | } |
| 606 | |
| 607 | static void release_idr(struct idr *idr, struct mutex *lock, int id) |
| 608 | { |
| 609 | if (lock) |
| 610 | mutex_lock(lock); |
| 611 | idr_remove(idr, id); |
| 612 | if (lock) |
| 613 | mutex_unlock(lock); |
| 614 | } |
| 615 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 616 | /** |
Eduardo Valentin | d2e4eb8 | 2013-04-23 21:48:16 +0000 | [diff] [blame] | 617 | * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone |
| 618 | * @tz: pointer to struct thermal_zone_device |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 619 | * @trip: indicates which trip point the cooling devices is |
| 620 | * associated with in this thermal zone. |
Eduardo Valentin | d2e4eb8 | 2013-04-23 21:48:16 +0000 | [diff] [blame] | 621 | * @cdev: pointer to struct thermal_cooling_device |
| 622 | * @upper: the Maximum cooling state for this trip point. |
| 623 | * THERMAL_NO_LIMIT means no upper limit, |
| 624 | * and the cooling device can be in max_state. |
| 625 | * @lower: the Minimum cooling state can be used for this trip point. |
| 626 | * THERMAL_NO_LIMIT means no lower limit, |
| 627 | * and the cooling device can be in cooling state 0. |
Kapileshwar Singh | 6cd9e9f | 2015-02-18 16:04:21 +0000 | [diff] [blame] | 628 | * @weight: The weight of the cooling device to be bound to the |
| 629 | * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the |
| 630 | * default value |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 631 | * |
Eduardo Valentin | d2e4eb8 | 2013-04-23 21:48:16 +0000 | [diff] [blame] | 632 | * This interface function bind a thermal cooling device to the certain trip |
| 633 | * point of a thermal zone device. |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 634 | * This function is usually called in the thermal zone device .bind callback. |
Eduardo Valentin | d2e4eb8 | 2013-04-23 21:48:16 +0000 | [diff] [blame] | 635 | * |
| 636 | * Return: 0 on success, the proper error value otherwise. |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 637 | */ |
| 638 | int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz, |
| 639 | int trip, |
Zhang Rui | 9d99842 | 2012-06-26 16:35:57 +0800 | [diff] [blame] | 640 | struct thermal_cooling_device *cdev, |
Kapileshwar Singh | 6cd9e9f | 2015-02-18 16:04:21 +0000 | [diff] [blame] | 641 | unsigned long upper, unsigned long lower, |
| 642 | unsigned int weight) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 643 | { |
Zhang Rui | b81b6ba | 2012-06-27 10:08:19 +0800 | [diff] [blame] | 644 | struct thermal_instance *dev; |
| 645 | struct thermal_instance *pos; |
Thomas Sujith | c751670 | 2008-02-15 00:58:50 -0500 | [diff] [blame] | 646 | struct thermal_zone_device *pos1; |
| 647 | struct thermal_cooling_device *pos2; |
Zhang Rui | 74051ba | 2012-06-26 16:27:22 +0800 | [diff] [blame] | 648 | unsigned long max_state; |
Lukasz Majewski | 9a3031d | 2014-11-18 11:16:30 +0100 | [diff] [blame] | 649 | int result, ret; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 650 | |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 651 | if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE)) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 652 | return -EINVAL; |
| 653 | |
Thomas Sujith | c751670 | 2008-02-15 00:58:50 -0500 | [diff] [blame] | 654 | list_for_each_entry(pos1, &thermal_tz_list, node) { |
| 655 | if (pos1 == tz) |
| 656 | break; |
| 657 | } |
| 658 | list_for_each_entry(pos2, &thermal_cdev_list, node) { |
| 659 | if (pos2 == cdev) |
| 660 | break; |
| 661 | } |
| 662 | |
| 663 | if (tz != pos1 || cdev != pos2) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 664 | return -EINVAL; |
| 665 | |
Lukasz Majewski | 9a3031d | 2014-11-18 11:16:30 +0100 | [diff] [blame] | 666 | ret = cdev->ops->get_max_state(cdev, &max_state); |
| 667 | if (ret) |
| 668 | return ret; |
Zhang Rui | 9d99842 | 2012-06-26 16:35:57 +0800 | [diff] [blame] | 669 | |
| 670 | /* lower default 0, upper default max_state */ |
| 671 | lower = lower == THERMAL_NO_LIMIT ? 0 : lower; |
| 672 | upper = upper == THERMAL_NO_LIMIT ? max_state : upper; |
| 673 | |
| 674 | if (lower > upper || upper > max_state) |
| 675 | return -EINVAL; |
| 676 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 677 | dev = |
Zhang Rui | b81b6ba | 2012-06-27 10:08:19 +0800 | [diff] [blame] | 678 | kzalloc(sizeof(struct thermal_instance), GFP_KERNEL); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 679 | if (!dev) |
| 680 | return -ENOMEM; |
| 681 | dev->tz = tz; |
| 682 | dev->cdev = cdev; |
| 683 | dev->trip = trip; |
Zhang Rui | 9d99842 | 2012-06-26 16:35:57 +0800 | [diff] [blame] | 684 | dev->upper = upper; |
| 685 | dev->lower = lower; |
Zhang Rui | ce119f8 | 2012-06-27 14:13:04 +0800 | [diff] [blame] | 686 | dev->target = THERMAL_NO_TARGET; |
Kapileshwar Singh | 6cd9e9f | 2015-02-18 16:04:21 +0000 | [diff] [blame] | 687 | dev->weight = weight; |
Zhang Rui | 74051ba | 2012-06-26 16:27:22 +0800 | [diff] [blame] | 688 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 689 | result = get_idr(&tz->idr, &tz->lock, &dev->id); |
| 690 | if (result) |
| 691 | goto free_mem; |
| 692 | |
| 693 | sprintf(dev->name, "cdev%d", dev->id); |
| 694 | result = |
| 695 | sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name); |
| 696 | if (result) |
| 697 | goto release_idr; |
| 698 | |
| 699 | sprintf(dev->attr_name, "cdev%d_trip_point", dev->id); |
Sergey Senozhatsky | 975f8c5 | 2010-04-06 14:34:51 -0700 | [diff] [blame] | 700 | sysfs_attr_init(&dev->attr.attr); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 701 | dev->attr.attr.name = dev->attr_name; |
| 702 | dev->attr.attr.mode = 0444; |
| 703 | dev->attr.show = thermal_cooling_device_trip_point_show; |
| 704 | result = device_create_file(&tz->device, &dev->attr); |
| 705 | if (result) |
| 706 | goto remove_symbol_link; |
| 707 | |
Javi Merino | db91651 | 2015-02-18 16:04:24 +0000 | [diff] [blame] | 708 | sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id); |
| 709 | sysfs_attr_init(&dev->weight_attr.attr); |
| 710 | dev->weight_attr.attr.name = dev->weight_attr_name; |
| 711 | dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO; |
| 712 | dev->weight_attr.show = thermal_cooling_device_weight_show; |
| 713 | dev->weight_attr.store = thermal_cooling_device_weight_store; |
| 714 | result = device_create_file(&tz->device, &dev->weight_attr); |
| 715 | if (result) |
| 716 | goto remove_trip_file; |
| 717 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 718 | mutex_lock(&tz->lock); |
Zhang Rui | f4a821c | 2012-07-24 16:56:21 +0800 | [diff] [blame] | 719 | mutex_lock(&cdev->lock); |
Zhang Rui | cddf31b | 2012-06-27 10:09:36 +0800 | [diff] [blame] | 720 | list_for_each_entry(pos, &tz->thermal_instances, tz_node) |
Eduardo Valentin | b659a30 | 2016-11-07 21:09:23 -0800 | [diff] [blame^] | 721 | if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { |
| 722 | result = -EEXIST; |
| 723 | break; |
| 724 | } |
Zhang Rui | b5e4ae6 | 2012-06-27 14:11:52 +0800 | [diff] [blame] | 725 | if (!result) { |
Zhang Rui | cddf31b | 2012-06-27 10:09:36 +0800 | [diff] [blame] | 726 | list_add_tail(&dev->tz_node, &tz->thermal_instances); |
Zhang Rui | b5e4ae6 | 2012-06-27 14:11:52 +0800 | [diff] [blame] | 727 | list_add_tail(&dev->cdev_node, &cdev->thermal_instances); |
Chen Yu | 4511f71 | 2015-10-30 16:32:10 +0800 | [diff] [blame] | 728 | atomic_set(&tz->need_update, 1); |
Zhang Rui | b5e4ae6 | 2012-06-27 14:11:52 +0800 | [diff] [blame] | 729 | } |
Zhang Rui | f4a821c | 2012-07-24 16:56:21 +0800 | [diff] [blame] | 730 | mutex_unlock(&cdev->lock); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 731 | mutex_unlock(&tz->lock); |
| 732 | |
| 733 | if (!result) |
| 734 | return 0; |
| 735 | |
Javi Merino | db91651 | 2015-02-18 16:04:24 +0000 | [diff] [blame] | 736 | device_remove_file(&tz->device, &dev->weight_attr); |
| 737 | remove_trip_file: |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 738 | device_remove_file(&tz->device, &dev->attr); |
Joe Perches | caca8b8 | 2012-03-21 12:55:02 -0700 | [diff] [blame] | 739 | remove_symbol_link: |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 740 | sysfs_remove_link(&tz->device.kobj, dev->name); |
Joe Perches | caca8b8 | 2012-03-21 12:55:02 -0700 | [diff] [blame] | 741 | release_idr: |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 742 | release_idr(&tz->idr, &tz->lock, dev->id); |
Joe Perches | caca8b8 | 2012-03-21 12:55:02 -0700 | [diff] [blame] | 743 | free_mem: |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 744 | kfree(dev); |
| 745 | return result; |
| 746 | } |
Eduardo Valentin | 910cb1e | 2013-04-23 21:48:15 +0000 | [diff] [blame] | 747 | EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 748 | |
| 749 | /** |
Eduardo Valentin | 9892e5d | 2013-04-23 21:48:17 +0000 | [diff] [blame] | 750 | * thermal_zone_unbind_cooling_device() - unbind a cooling device from a |
| 751 | * thermal zone. |
| 752 | * @tz: pointer to a struct thermal_zone_device. |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 753 | * @trip: indicates which trip point the cooling devices is |
| 754 | * associated with in this thermal zone. |
Eduardo Valentin | 9892e5d | 2013-04-23 21:48:17 +0000 | [diff] [blame] | 755 | * @cdev: pointer to a struct thermal_cooling_device. |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 756 | * |
Eduardo Valentin | 9892e5d | 2013-04-23 21:48:17 +0000 | [diff] [blame] | 757 | * This interface function unbind a thermal cooling device from the certain |
| 758 | * trip point of a thermal zone device. |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 759 | * This function is usually called in the thermal zone device .unbind callback. |
Eduardo Valentin | 9892e5d | 2013-04-23 21:48:17 +0000 | [diff] [blame] | 760 | * |
| 761 | * Return: 0 on success, the proper error value otherwise. |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 762 | */ |
| 763 | int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz, |
| 764 | int trip, |
| 765 | struct thermal_cooling_device *cdev) |
| 766 | { |
Zhang Rui | b81b6ba | 2012-06-27 10:08:19 +0800 | [diff] [blame] | 767 | struct thermal_instance *pos, *next; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 768 | |
| 769 | mutex_lock(&tz->lock); |
Zhang Rui | f4a821c | 2012-07-24 16:56:21 +0800 | [diff] [blame] | 770 | mutex_lock(&cdev->lock); |
Zhang Rui | cddf31b | 2012-06-27 10:09:36 +0800 | [diff] [blame] | 771 | list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) { |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 772 | if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { |
Zhang Rui | cddf31b | 2012-06-27 10:09:36 +0800 | [diff] [blame] | 773 | list_del(&pos->tz_node); |
Zhang Rui | b5e4ae6 | 2012-06-27 14:11:52 +0800 | [diff] [blame] | 774 | list_del(&pos->cdev_node); |
Zhang Rui | f4a821c | 2012-07-24 16:56:21 +0800 | [diff] [blame] | 775 | mutex_unlock(&cdev->lock); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 776 | mutex_unlock(&tz->lock); |
| 777 | goto unbind; |
| 778 | } |
| 779 | } |
Zhang Rui | f4a821c | 2012-07-24 16:56:21 +0800 | [diff] [blame] | 780 | mutex_unlock(&cdev->lock); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 781 | mutex_unlock(&tz->lock); |
| 782 | |
| 783 | return -ENODEV; |
| 784 | |
Joe Perches | caca8b8 | 2012-03-21 12:55:02 -0700 | [diff] [blame] | 785 | unbind: |
Viresh Kumar | 528464e | 2015-07-23 14:32:32 +0530 | [diff] [blame] | 786 | device_remove_file(&tz->device, &pos->weight_attr); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 787 | device_remove_file(&tz->device, &pos->attr); |
| 788 | sysfs_remove_link(&tz->device.kobj, pos->name); |
| 789 | release_idr(&tz->idr, &tz->lock, pos->id); |
| 790 | kfree(pos); |
| 791 | return 0; |
| 792 | } |
Eduardo Valentin | 910cb1e | 2013-04-23 21:48:15 +0000 | [diff] [blame] | 793 | EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 794 | |
| 795 | static void thermal_release(struct device *dev) |
| 796 | { |
| 797 | struct thermal_zone_device *tz; |
| 798 | struct thermal_cooling_device *cdev; |
| 799 | |
Joe Perches | caca8b8 | 2012-03-21 12:55:02 -0700 | [diff] [blame] | 800 | if (!strncmp(dev_name(dev), "thermal_zone", |
| 801 | sizeof("thermal_zone") - 1)) { |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 802 | tz = to_thermal_zone(dev); |
| 803 | kfree(tz); |
Eduardo Valentin | b659a30 | 2016-11-07 21:09:23 -0800 | [diff] [blame^] | 804 | } else if (!strncmp(dev_name(dev), "cooling_device", |
| 805 | sizeof("cooling_device") - 1)) { |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 806 | cdev = to_cooling_device(dev); |
| 807 | kfree(cdev); |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | static struct class thermal_class = { |
| 812 | .name = "thermal", |
| 813 | .dev_release = thermal_release, |
| 814 | }; |
| 815 | |
Eduardo Valentin | 4b0d3c2 | 2016-11-07 21:09:13 -0800 | [diff] [blame] | 816 | static inline |
| 817 | void print_bind_err_msg(struct thermal_zone_device *tz, |
| 818 | struct thermal_cooling_device *cdev, int ret) |
Eduardo Valentin | f502ab8 | 2016-11-07 21:09:12 -0800 | [diff] [blame] | 819 | { |
| 820 | dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n", |
| 821 | tz->type, cdev->type, ret); |
| 822 | } |
| 823 | |
| 824 | static void __bind(struct thermal_zone_device *tz, int mask, |
| 825 | struct thermal_cooling_device *cdev, |
| 826 | unsigned long *limits, |
| 827 | unsigned int weight) |
| 828 | { |
| 829 | int i, ret; |
| 830 | |
| 831 | for (i = 0; i < tz->trips; i++) { |
| 832 | if (mask & (1 << i)) { |
| 833 | unsigned long upper, lower; |
| 834 | |
| 835 | upper = THERMAL_NO_LIMIT; |
| 836 | lower = THERMAL_NO_LIMIT; |
| 837 | if (limits) { |
| 838 | lower = limits[i * 2]; |
| 839 | upper = limits[i * 2 + 1]; |
| 840 | } |
| 841 | ret = thermal_zone_bind_cooling_device(tz, i, cdev, |
| 842 | upper, lower, |
| 843 | weight); |
| 844 | if (ret) |
| 845 | print_bind_err_msg(tz, cdev, ret); |
| 846 | } |
| 847 | } |
| 848 | } |
| 849 | |
Eduardo Valentin | 949aad8 | 2016-11-07 21:09:09 -0800 | [diff] [blame] | 850 | static void bind_cdev(struct thermal_cooling_device *cdev) |
| 851 | { |
| 852 | int i, ret; |
| 853 | const struct thermal_zone_params *tzp; |
| 854 | struct thermal_zone_device *pos = NULL; |
| 855 | |
| 856 | mutex_lock(&thermal_list_lock); |
| 857 | |
| 858 | list_for_each_entry(pos, &thermal_tz_list, node) { |
| 859 | if (!pos->tzp && !pos->ops->bind) |
| 860 | continue; |
| 861 | |
| 862 | if (pos->ops->bind) { |
| 863 | ret = pos->ops->bind(pos, cdev); |
| 864 | if (ret) |
| 865 | print_bind_err_msg(pos, cdev, ret); |
| 866 | continue; |
| 867 | } |
| 868 | |
| 869 | tzp = pos->tzp; |
| 870 | if (!tzp || !tzp->tbp) |
| 871 | continue; |
| 872 | |
| 873 | for (i = 0; i < tzp->num_tbps; i++) { |
| 874 | if (tzp->tbp[i].cdev || !tzp->tbp[i].match) |
| 875 | continue; |
| 876 | if (tzp->tbp[i].match(pos, cdev)) |
| 877 | continue; |
| 878 | tzp->tbp[i].cdev = cdev; |
| 879 | __bind(pos, tzp->tbp[i].trip_mask, cdev, |
| 880 | tzp->tbp[i].binding_limits, |
| 881 | tzp->tbp[i].weight); |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | mutex_unlock(&thermal_list_lock); |
| 886 | } |
| 887 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 888 | /** |
Eduardo Valentin | a116b5d | 2013-09-26 15:55:01 -0400 | [diff] [blame] | 889 | * __thermal_cooling_device_register() - register a new thermal cooling device |
| 890 | * @np: a pointer to a device tree node. |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 891 | * @type: the thermal cooling device type. |
| 892 | * @devdata: device private data. |
| 893 | * @ops: standard thermal cooling devices callbacks. |
Eduardo Valentin | 3a6eccb | 2013-04-23 21:48:18 +0000 | [diff] [blame] | 894 | * |
| 895 | * This interface function adds a new thermal cooling device (fan/processor/...) |
| 896 | * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself |
| 897 | * to all the thermal zone devices registered at the same time. |
Eduardo Valentin | a116b5d | 2013-09-26 15:55:01 -0400 | [diff] [blame] | 898 | * It also gives the opportunity to link the cooling device to a device tree |
| 899 | * 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] | 900 | * |
| 901 | * Return: a pointer to the created struct thermal_cooling_device or an |
| 902 | * ERR_PTR. Caller must check return value with IS_ERR*() helpers. |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 903 | */ |
Eduardo Valentin | a116b5d | 2013-09-26 15:55:01 -0400 | [diff] [blame] | 904 | static struct thermal_cooling_device * |
| 905 | __thermal_cooling_device_register(struct device_node *np, |
| 906 | char *type, void *devdata, |
| 907 | const struct thermal_cooling_device_ops *ops) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 908 | { |
| 909 | struct thermal_cooling_device *cdev; |
Chen Yu | 4511f71 | 2015-10-30 16:32:10 +0800 | [diff] [blame] | 910 | struct thermal_zone_device *pos = NULL; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 911 | int result; |
| 912 | |
Guenter Roeck | 204dd1d | 2012-08-07 22:36:45 -0700 | [diff] [blame] | 913 | if (type && strlen(type) >= THERMAL_NAME_LENGTH) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 914 | return ERR_PTR(-EINVAL); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 915 | |
| 916 | if (!ops || !ops->get_max_state || !ops->get_cur_state || |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 917 | !ops->set_cur_state) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 918 | return ERR_PTR(-EINVAL); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 919 | |
| 920 | cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL); |
| 921 | if (!cdev) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 922 | return ERR_PTR(-ENOMEM); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 923 | |
| 924 | result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id); |
| 925 | if (result) { |
| 926 | kfree(cdev); |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 927 | return ERR_PTR(result); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 928 | } |
| 929 | |
Eduardo Valentin | c7a8b9d | 2013-04-23 21:48:12 +0000 | [diff] [blame] | 930 | strlcpy(cdev->type, type ? : "", sizeof(cdev->type)); |
Zhang Rui | f4a821c | 2012-07-24 16:56:21 +0800 | [diff] [blame] | 931 | mutex_init(&cdev->lock); |
Zhang Rui | b5e4ae6 | 2012-06-27 14:11:52 +0800 | [diff] [blame] | 932 | INIT_LIST_HEAD(&cdev->thermal_instances); |
Eduardo Valentin | a116b5d | 2013-09-26 15:55:01 -0400 | [diff] [blame] | 933 | cdev->np = np; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 934 | cdev->ops = ops; |
Ni Wade | 5ca0cce | 2014-02-17 11:02:55 +0800 | [diff] [blame] | 935 | cdev->updated = false; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 936 | cdev->device.class = &thermal_class; |
Eduardo Valentin | 45cf2ec | 2016-11-07 21:09:02 -0800 | [diff] [blame] | 937 | thermal_cooling_device_setup_sysfs(cdev); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 938 | cdev->devdata = devdata; |
Kay Sievers | 354655e | 2009-01-06 10:44:37 -0800 | [diff] [blame] | 939 | dev_set_name(&cdev->device, "cooling_device%d", cdev->id); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 940 | result = device_register(&cdev->device); |
| 941 | if (result) { |
| 942 | release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id); |
| 943 | kfree(cdev); |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 944 | return ERR_PTR(result); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 945 | } |
| 946 | |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 947 | /* Add 'this' new cdev to the global cdev list */ |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 948 | mutex_lock(&thermal_list_lock); |
| 949 | list_add(&cdev->node, &thermal_cdev_list); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 950 | mutex_unlock(&thermal_list_lock); |
| 951 | |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 952 | /* Update binding information for 'this' new cdev */ |
| 953 | bind_cdev(cdev); |
| 954 | |
Chen Yu | 4511f71 | 2015-10-30 16:32:10 +0800 | [diff] [blame] | 955 | mutex_lock(&thermal_list_lock); |
| 956 | list_for_each_entry(pos, &thermal_tz_list, node) |
| 957 | if (atomic_cmpxchg(&pos->need_update, 1, 0)) |
Srinivas Pandruvada | 0e70f46 | 2016-08-26 16:21:16 -0700 | [diff] [blame] | 958 | thermal_zone_device_update(pos, |
| 959 | THERMAL_EVENT_UNSPECIFIED); |
Chen Yu | 4511f71 | 2015-10-30 16:32:10 +0800 | [diff] [blame] | 960 | mutex_unlock(&thermal_list_lock); |
| 961 | |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 962 | return cdev; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 963 | } |
Eduardo Valentin | a116b5d | 2013-09-26 15:55:01 -0400 | [diff] [blame] | 964 | |
| 965 | /** |
| 966 | * thermal_cooling_device_register() - register a new thermal cooling device |
| 967 | * @type: the thermal cooling device type. |
| 968 | * @devdata: device private data. |
| 969 | * @ops: standard thermal cooling devices callbacks. |
| 970 | * |
| 971 | * This interface function adds a new thermal cooling device (fan/processor/...) |
| 972 | * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself |
| 973 | * to all the thermal zone devices registered at the same time. |
| 974 | * |
| 975 | * Return: a pointer to the created struct thermal_cooling_device or an |
| 976 | * ERR_PTR. Caller must check return value with IS_ERR*() helpers. |
| 977 | */ |
| 978 | struct thermal_cooling_device * |
| 979 | thermal_cooling_device_register(char *type, void *devdata, |
| 980 | const struct thermal_cooling_device_ops *ops) |
| 981 | { |
| 982 | return __thermal_cooling_device_register(NULL, type, devdata, ops); |
| 983 | } |
Eduardo Valentin | 910cb1e | 2013-04-23 21:48:15 +0000 | [diff] [blame] | 984 | EXPORT_SYMBOL_GPL(thermal_cooling_device_register); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 985 | |
| 986 | /** |
Eduardo Valentin | a116b5d | 2013-09-26 15:55:01 -0400 | [diff] [blame] | 987 | * thermal_of_cooling_device_register() - register an OF thermal cooling device |
| 988 | * @np: a pointer to a device tree node. |
| 989 | * @type: the thermal cooling device type. |
| 990 | * @devdata: device private data. |
| 991 | * @ops: standard thermal cooling devices callbacks. |
| 992 | * |
| 993 | * This function will register a cooling device with device tree node reference. |
| 994 | * This interface function adds a new thermal cooling device (fan/processor/...) |
| 995 | * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself |
| 996 | * to all the thermal zone devices registered at the same time. |
| 997 | * |
| 998 | * Return: a pointer to the created struct thermal_cooling_device or an |
| 999 | * ERR_PTR. Caller must check return value with IS_ERR*() helpers. |
| 1000 | */ |
| 1001 | struct thermal_cooling_device * |
| 1002 | thermal_of_cooling_device_register(struct device_node *np, |
| 1003 | char *type, void *devdata, |
| 1004 | const struct thermal_cooling_device_ops *ops) |
| 1005 | { |
| 1006 | return __thermal_cooling_device_register(np, type, devdata, ops); |
| 1007 | } |
| 1008 | EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register); |
| 1009 | |
Eduardo Valentin | f11997f | 2016-11-07 21:09:08 -0800 | [diff] [blame] | 1010 | static void __unbind(struct thermal_zone_device *tz, int mask, |
| 1011 | struct thermal_cooling_device *cdev) |
| 1012 | { |
| 1013 | int i; |
| 1014 | |
| 1015 | for (i = 0; i < tz->trips; i++) |
| 1016 | if (mask & (1 << i)) |
| 1017 | thermal_zone_unbind_cooling_device(tz, i, cdev); |
| 1018 | } |
| 1019 | |
Eduardo Valentin | a116b5d | 2013-09-26 15:55:01 -0400 | [diff] [blame] | 1020 | /** |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1021 | * thermal_cooling_device_unregister - removes the registered thermal cooling device |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1022 | * @cdev: the thermal cooling device to remove. |
| 1023 | * |
| 1024 | * thermal_cooling_device_unregister() must be called when the device is no |
| 1025 | * longer needed. |
| 1026 | */ |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 1027 | void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1028 | { |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 1029 | int i; |
| 1030 | const struct thermal_zone_params *tzp; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1031 | struct thermal_zone_device *tz; |
| 1032 | struct thermal_cooling_device *pos = NULL; |
| 1033 | |
| 1034 | if (!cdev) |
| 1035 | return; |
| 1036 | |
| 1037 | mutex_lock(&thermal_list_lock); |
| 1038 | list_for_each_entry(pos, &thermal_cdev_list, node) |
Eduardo Valentin | b659a30 | 2016-11-07 21:09:23 -0800 | [diff] [blame^] | 1039 | if (pos == cdev) |
| 1040 | break; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1041 | if (pos != cdev) { |
| 1042 | /* thermal cooling device not found */ |
| 1043 | mutex_unlock(&thermal_list_lock); |
| 1044 | return; |
| 1045 | } |
| 1046 | list_del(&cdev->node); |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 1047 | |
| 1048 | /* Unbind all thermal zones associated with 'this' cdev */ |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1049 | list_for_each_entry(tz, &thermal_tz_list, node) { |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 1050 | if (tz->ops->unbind) { |
| 1051 | tz->ops->unbind(tz, cdev); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1052 | continue; |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | if (!tz->tzp || !tz->tzp->tbp) |
| 1056 | continue; |
| 1057 | |
| 1058 | tzp = tz->tzp; |
| 1059 | for (i = 0; i < tzp->num_tbps; i++) { |
| 1060 | if (tzp->tbp[i].cdev == cdev) { |
| 1061 | __unbind(tz, tzp->tbp[i].trip_mask, cdev); |
| 1062 | tzp->tbp[i].cdev = NULL; |
| 1063 | } |
| 1064 | } |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1065 | } |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 1066 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1067 | mutex_unlock(&thermal_list_lock); |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 1068 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1069 | release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id); |
| 1070 | device_unregister(&cdev->device); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1071 | } |
Eduardo Valentin | 910cb1e | 2013-04-23 21:48:15 +0000 | [diff] [blame] | 1072 | EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1073 | |
Eduardo Valentin | 90f5b5b | 2016-11-07 21:09:10 -0800 | [diff] [blame] | 1074 | static void bind_tz(struct thermal_zone_device *tz) |
| 1075 | { |
| 1076 | int i, ret; |
| 1077 | struct thermal_cooling_device *pos = NULL; |
| 1078 | const struct thermal_zone_params *tzp = tz->tzp; |
| 1079 | |
| 1080 | if (!tzp && !tz->ops->bind) |
| 1081 | return; |
| 1082 | |
| 1083 | mutex_lock(&thermal_list_lock); |
| 1084 | |
| 1085 | /* If there is ops->bind, try to use ops->bind */ |
| 1086 | if (tz->ops->bind) { |
| 1087 | list_for_each_entry(pos, &thermal_cdev_list, node) { |
| 1088 | ret = tz->ops->bind(tz, pos); |
| 1089 | if (ret) |
| 1090 | print_bind_err_msg(tz, pos, ret); |
| 1091 | } |
| 1092 | goto exit; |
| 1093 | } |
| 1094 | |
| 1095 | if (!tzp || !tzp->tbp) |
| 1096 | goto exit; |
| 1097 | |
| 1098 | list_for_each_entry(pos, &thermal_cdev_list, node) { |
| 1099 | for (i = 0; i < tzp->num_tbps; i++) { |
| 1100 | if (tzp->tbp[i].cdev || !tzp->tbp[i].match) |
| 1101 | continue; |
| 1102 | if (tzp->tbp[i].match(tz, pos)) |
| 1103 | continue; |
| 1104 | tzp->tbp[i].cdev = pos; |
| 1105 | __bind(tz, tzp->tbp[i].trip_mask, pos, |
| 1106 | tzp->tbp[i].binding_limits, |
| 1107 | tzp->tbp[i].weight); |
| 1108 | } |
| 1109 | } |
| 1110 | exit: |
| 1111 | mutex_unlock(&thermal_list_lock); |
| 1112 | } |
| 1113 | |
Durgadoss R | f2b4caa | 2012-09-18 11:05:05 +0530 | [diff] [blame] | 1114 | /** |
Eduardo Valentin | a00e55f | 2013-04-23 21:48:20 +0000 | [diff] [blame] | 1115 | * thermal_zone_device_register() - register a new thermal zone device |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1116 | * @type: the thermal zone device type |
| 1117 | * @trips: the number of trip points the thermal zone support |
Durgadoss R | c56f5c0 | 2012-07-25 10:10:58 +0800 | [diff] [blame] | 1118 | * @mask: a bit string indicating the writeablility of trip points |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1119 | * @devdata: private device data |
| 1120 | * @ops: standard thermal zone device callbacks |
Durgadoss R | 50125a9 | 2012-09-18 11:04:56 +0530 | [diff] [blame] | 1121 | * @tzp: thermal zone platform parameters |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1122 | * @passive_delay: number of milliseconds to wait between polls when |
| 1123 | * performing passive cooling |
| 1124 | * @polling_delay: number of milliseconds to wait between polls when checking |
| 1125 | * whether trip points have been crossed (0 for interrupt |
| 1126 | * driven systems) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1127 | * |
Eduardo Valentin | a00e55f | 2013-04-23 21:48:20 +0000 | [diff] [blame] | 1128 | * This interface function adds a new thermal zone device (sensor) to |
| 1129 | * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the |
| 1130 | * thermal cooling devices registered at the same time. |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1131 | * thermal_zone_device_unregister() must be called when the device is no |
Zhang Rui | 1b7ddb8 | 2012-06-27 09:51:12 +0800 | [diff] [blame] | 1132 | * longer needed. The passive cooling depends on the .get_trend() return value. |
Eduardo Valentin | a00e55f | 2013-04-23 21:48:20 +0000 | [diff] [blame] | 1133 | * |
| 1134 | * Return: a pointer to the created struct thermal_zone_device or an |
| 1135 | * in case of error, an ERR_PTR. Caller must check return value with |
| 1136 | * IS_ERR*() helpers. |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1137 | */ |
Eduardo Valentin | eb7be32 | 2016-11-07 21:09:21 -0800 | [diff] [blame] | 1138 | struct thermal_zone_device * |
| 1139 | thermal_zone_device_register(const char *type, int trips, int mask, |
| 1140 | void *devdata, struct thermal_zone_device_ops *ops, |
| 1141 | struct thermal_zone_params *tzp, int passive_delay, |
| 1142 | int polling_delay) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1143 | { |
| 1144 | struct thermal_zone_device *tz; |
Matthew Garrett | 03a971a | 2008-12-03 18:00:38 +0000 | [diff] [blame] | 1145 | enum thermal_trip_type trip_type; |
Zhang Rui | 81ad427 | 2016-03-18 10:03:24 +0800 | [diff] [blame] | 1146 | int trip_temp; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1147 | int result; |
| 1148 | int count; |
Javi Merino | e33df1d | 2015-02-26 19:00:27 +0000 | [diff] [blame] | 1149 | struct thermal_governor *governor; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1150 | |
Eduardo Valentin | 54fa38c | 2016-11-07 21:08:39 -0800 | [diff] [blame] | 1151 | if (!type || strlen(type) == 0) |
| 1152 | return ERR_PTR(-EINVAL); |
| 1153 | |
Guenter Roeck | 204dd1d | 2012-08-07 22:36:45 -0700 | [diff] [blame] | 1154 | if (type && strlen(type) >= THERMAL_NAME_LENGTH) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 1155 | return ERR_PTR(-EINVAL); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1156 | |
Durgadoss R | c56f5c0 | 2012-07-25 10:10:58 +0800 | [diff] [blame] | 1157 | if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 1158 | return ERR_PTR(-EINVAL); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1159 | |
Eduardo Valentin | 81bd4e1 | 2013-09-12 19:15:44 -0400 | [diff] [blame] | 1160 | if (!ops) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 1161 | return ERR_PTR(-EINVAL); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1162 | |
Jonghwa Lee | 83720d0 | 2013-05-18 09:50:26 +0000 | [diff] [blame] | 1163 | if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp)) |
Eduardo Valentin | 6b2aa51 | 2013-01-02 15:29:42 +0000 | [diff] [blame] | 1164 | return ERR_PTR(-EINVAL); |
| 1165 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1166 | tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL); |
| 1167 | if (!tz) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 1168 | return ERR_PTR(-ENOMEM); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1169 | |
Zhang Rui | 2d37413 | 2012-06-27 10:09:00 +0800 | [diff] [blame] | 1170 | INIT_LIST_HEAD(&tz->thermal_instances); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1171 | idr_init(&tz->idr); |
| 1172 | mutex_init(&tz->lock); |
| 1173 | result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id); |
| 1174 | if (result) { |
| 1175 | kfree(tz); |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 1176 | return ERR_PTR(result); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1177 | } |
| 1178 | |
Eduardo Valentin | 54fa38c | 2016-11-07 21:08:39 -0800 | [diff] [blame] | 1179 | strlcpy(tz->type, type, sizeof(tz->type)); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1180 | tz->ops = ops; |
Durgadoss R | 50125a9 | 2012-09-18 11:04:56 +0530 | [diff] [blame] | 1181 | tz->tzp = tzp; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1182 | tz->device.class = &thermal_class; |
| 1183 | tz->devdata = devdata; |
| 1184 | tz->trips = trips; |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1185 | tz->passive_delay = passive_delay; |
| 1186 | tz->polling_delay = polling_delay; |
Eduardo Valentin | 1c60086 | 2016-11-07 21:08:42 -0800 | [diff] [blame] | 1187 | |
Eduardo Valentin | 4d0fe74 | 2016-11-07 21:08:52 -0800 | [diff] [blame] | 1188 | /* sys I/F */ |
Eduardo Valentin | 1c60086 | 2016-11-07 21:08:42 -0800 | [diff] [blame] | 1189 | /* Add nodes that are always present via .groups */ |
Eduardo Valentin | 4d0fe74 | 2016-11-07 21:08:52 -0800 | [diff] [blame] | 1190 | result = thermal_zone_create_device_groups(tz, mask); |
| 1191 | if (result) |
| 1192 | goto unregister; |
| 1193 | |
Chen Yu | 4511f71 | 2015-10-30 16:32:10 +0800 | [diff] [blame] | 1194 | /* A new thermal zone needs to be updated anyway. */ |
| 1195 | atomic_set(&tz->need_update, 1); |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1196 | |
Kay Sievers | 354655e | 2009-01-06 10:44:37 -0800 | [diff] [blame] | 1197 | dev_set_name(&tz->device, "thermal_zone%d", tz->id); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1198 | result = device_register(&tz->device); |
| 1199 | if (result) { |
| 1200 | release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id); |
| 1201 | kfree(tz); |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 1202 | return ERR_PTR(result); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1203 | } |
| 1204 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1205 | for (count = 0; count < trips; count++) { |
Zhang Rui | 81ad427 | 2016-03-18 10:03:24 +0800 | [diff] [blame] | 1206 | if (tz->ops->get_trip_type(tz, count, &trip_type)) |
| 1207 | set_bit(count, &tz->trips_disabled); |
Zhang Rui | 81ad427 | 2016-03-18 10:03:24 +0800 | [diff] [blame] | 1208 | if (tz->ops->get_trip_temp(tz, count, &trip_temp)) |
| 1209 | set_bit(count, &tz->trips_disabled); |
| 1210 | /* Check for bogus trip points */ |
| 1211 | if (trip_temp == 0) |
| 1212 | set_bit(count, &tz->trips_disabled); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1213 | } |
| 1214 | |
Durgadoss R | a4a1548 | 2012-09-18 11:04:57 +0530 | [diff] [blame] | 1215 | /* Update 'this' zone's governor information */ |
| 1216 | mutex_lock(&thermal_governor_lock); |
| 1217 | |
| 1218 | if (tz->tzp) |
Javi Merino | e33df1d | 2015-02-26 19:00:27 +0000 | [diff] [blame] | 1219 | governor = __find_governor(tz->tzp->governor_name); |
Durgadoss R | a4a1548 | 2012-09-18 11:04:57 +0530 | [diff] [blame] | 1220 | else |
Javi Merino | e33df1d | 2015-02-26 19:00:27 +0000 | [diff] [blame] | 1221 | governor = def_governor; |
| 1222 | |
| 1223 | result = thermal_set_governor(tz, governor); |
| 1224 | if (result) { |
| 1225 | mutex_unlock(&thermal_governor_lock); |
| 1226 | goto unregister; |
| 1227 | } |
Durgadoss R | a4a1548 | 2012-09-18 11:04:57 +0530 | [diff] [blame] | 1228 | |
| 1229 | mutex_unlock(&thermal_governor_lock); |
| 1230 | |
Eduardo Valentin | ccba4ff | 2013-08-15 11:34:17 -0400 | [diff] [blame] | 1231 | if (!tz->tzp || !tz->tzp->no_hwmon) { |
| 1232 | result = thermal_add_hwmon_sysfs(tz); |
| 1233 | if (result) |
| 1234 | goto unregister; |
| 1235 | } |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 1236 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1237 | mutex_lock(&thermal_list_lock); |
| 1238 | list_add_tail(&tz->node, &thermal_tz_list); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1239 | mutex_unlock(&thermal_list_lock); |
| 1240 | |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 1241 | /* Bind cooling devices for this zone */ |
| 1242 | bind_tz(tz); |
| 1243 | |
Eduardo Valentin | b659a30 | 2016-11-07 21:09:23 -0800 | [diff] [blame^] | 1244 | INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check); |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1245 | |
Zhang Rui | bb431ba | 2015-10-30 16:31:47 +0800 | [diff] [blame] | 1246 | thermal_zone_device_reset(tz); |
Chen Yu | 4511f71 | 2015-10-30 16:32:10 +0800 | [diff] [blame] | 1247 | /* Update the new thermal zone and mark it as already updated. */ |
| 1248 | if (atomic_cmpxchg(&tz->need_update, 1, 0)) |
Srinivas Pandruvada | 0e70f46 | 2016-08-26 16:21:16 -0700 | [diff] [blame] | 1249 | thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1250 | |
Yao Dongdong | 1401586 | 2014-10-28 07:40:25 +0000 | [diff] [blame] | 1251 | return tz; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1252 | |
Joe Perches | caca8b8 | 2012-03-21 12:55:02 -0700 | [diff] [blame] | 1253 | unregister: |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1254 | release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id); |
| 1255 | device_unregister(&tz->device); |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 1256 | return ERR_PTR(result); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1257 | } |
Eduardo Valentin | 910cb1e | 2013-04-23 21:48:15 +0000 | [diff] [blame] | 1258 | EXPORT_SYMBOL_GPL(thermal_zone_device_register); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1259 | |
| 1260 | /** |
| 1261 | * thermal_device_unregister - removes the registered thermal zone device |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1262 | * @tz: the thermal zone device to remove |
| 1263 | */ |
| 1264 | void thermal_zone_device_unregister(struct thermal_zone_device *tz) |
| 1265 | { |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 1266 | int i; |
| 1267 | const struct thermal_zone_params *tzp; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1268 | struct thermal_cooling_device *cdev; |
| 1269 | struct thermal_zone_device *pos = NULL; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1270 | |
| 1271 | if (!tz) |
| 1272 | return; |
| 1273 | |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 1274 | tzp = tz->tzp; |
| 1275 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1276 | mutex_lock(&thermal_list_lock); |
| 1277 | list_for_each_entry(pos, &thermal_tz_list, node) |
Eduardo Valentin | b659a30 | 2016-11-07 21:09:23 -0800 | [diff] [blame^] | 1278 | if (pos == tz) |
| 1279 | break; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1280 | if (pos != tz) { |
| 1281 | /* thermal zone device not found */ |
| 1282 | mutex_unlock(&thermal_list_lock); |
| 1283 | return; |
| 1284 | } |
| 1285 | list_del(&tz->node); |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 1286 | |
| 1287 | /* Unbind all cdevs associated with 'this' thermal zone */ |
| 1288 | list_for_each_entry(cdev, &thermal_cdev_list, node) { |
| 1289 | if (tz->ops->unbind) { |
| 1290 | tz->ops->unbind(tz, cdev); |
| 1291 | continue; |
| 1292 | } |
| 1293 | |
| 1294 | if (!tzp || !tzp->tbp) |
| 1295 | break; |
| 1296 | |
| 1297 | for (i = 0; i < tzp->num_tbps; i++) { |
| 1298 | if (tzp->tbp[i].cdev == cdev) { |
| 1299 | __unbind(tz, tzp->tbp[i].trip_mask, cdev); |
| 1300 | tzp->tbp[i].cdev = NULL; |
| 1301 | } |
| 1302 | } |
| 1303 | } |
| 1304 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1305 | mutex_unlock(&thermal_list_lock); |
| 1306 | |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1307 | thermal_zone_device_set_polling(tz, 0); |
| 1308 | |
Eduardo Valentin | 4d0fe74 | 2016-11-07 21:08:52 -0800 | [diff] [blame] | 1309 | kfree(tz->trip_type_attrs); |
| 1310 | kfree(tz->trip_temp_attrs); |
| 1311 | kfree(tz->trip_hyst_attrs); |
| 1312 | kfree(tz->trips_attribute_group.attrs); |
Javi Merino | e33df1d | 2015-02-26 19:00:27 +0000 | [diff] [blame] | 1313 | thermal_set_governor(tz, NULL); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1314 | |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 1315 | thermal_remove_hwmon_sysfs(tz); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1316 | release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id); |
| 1317 | idr_destroy(&tz->idr); |
| 1318 | mutex_destroy(&tz->lock); |
| 1319 | device_unregister(&tz->device); |
Eduardo Valentin | e161aef | 2016-11-07 21:08:51 -0800 | [diff] [blame] | 1320 | kfree(tz->device.groups); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1321 | } |
Eduardo Valentin | 910cb1e | 2013-04-23 21:48:15 +0000 | [diff] [blame] | 1322 | EXPORT_SYMBOL_GPL(thermal_zone_device_unregister); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1323 | |
Eduardo Valentin | 63c4d91 | 2013-04-05 12:32:28 +0000 | [diff] [blame] | 1324 | /** |
| 1325 | * thermal_zone_get_zone_by_name() - search for a zone and returns its ref |
| 1326 | * @name: thermal zone name to fetch the temperature |
| 1327 | * |
| 1328 | * When only one zone is found with the passed name, returns a reference to it. |
| 1329 | * |
| 1330 | * Return: On success returns a reference to an unique thermal zone with |
| 1331 | * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid |
| 1332 | * paramenters, -ENODEV for not found and -EEXIST for multiple matches). |
| 1333 | */ |
| 1334 | struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name) |
| 1335 | { |
| 1336 | struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL); |
| 1337 | unsigned int found = 0; |
| 1338 | |
| 1339 | if (!name) |
| 1340 | goto exit; |
| 1341 | |
| 1342 | mutex_lock(&thermal_list_lock); |
| 1343 | list_for_each_entry(pos, &thermal_tz_list, node) |
Rasmus Villemoes | 484ac2f | 2014-10-13 15:55:01 -0700 | [diff] [blame] | 1344 | if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) { |
Eduardo Valentin | 63c4d91 | 2013-04-05 12:32:28 +0000 | [diff] [blame] | 1345 | found++; |
| 1346 | ref = pos; |
| 1347 | } |
| 1348 | mutex_unlock(&thermal_list_lock); |
| 1349 | |
| 1350 | /* nothing has been found, thus an error code for it */ |
| 1351 | if (found == 0) |
| 1352 | ref = ERR_PTR(-ENODEV); |
| 1353 | else if (found > 1) |
| 1354 | /* Success only when an unique zone is found */ |
| 1355 | ref = ERR_PTR(-EEXIST); |
| 1356 | |
| 1357 | exit: |
| 1358 | return ref; |
| 1359 | } |
| 1360 | EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name); |
| 1361 | |
Rajendra Nayak | 4a7069a | 2016-05-05 14:21:42 +0530 | [diff] [blame] | 1362 | /** |
| 1363 | * thermal_zone_get_slope - return the slope attribute of the thermal zone |
| 1364 | * @tz: thermal zone device with the slope attribute |
| 1365 | * |
| 1366 | * Return: If the thermal zone device has a slope attribute, return it, else |
| 1367 | * return 1. |
| 1368 | */ |
| 1369 | int thermal_zone_get_slope(struct thermal_zone_device *tz) |
| 1370 | { |
| 1371 | if (tz && tz->tzp) |
| 1372 | return tz->tzp->slope; |
| 1373 | return 1; |
| 1374 | } |
| 1375 | EXPORT_SYMBOL_GPL(thermal_zone_get_slope); |
| 1376 | |
| 1377 | /** |
| 1378 | * thermal_zone_get_offset - return the offset attribute of the thermal zone |
| 1379 | * @tz: thermal zone device with the offset attribute |
| 1380 | * |
| 1381 | * Return: If the thermal zone device has a offset attribute, return it, else |
| 1382 | * return 0. |
| 1383 | */ |
| 1384 | int thermal_zone_get_offset(struct thermal_zone_device *tz) |
| 1385 | { |
| 1386 | if (tz && tz->tzp) |
| 1387 | return tz->tzp->offset; |
| 1388 | return 0; |
| 1389 | } |
| 1390 | EXPORT_SYMBOL_GPL(thermal_zone_get_offset); |
| 1391 | |
Rafael J. Wysocki | af06216 | 2011-03-01 01:12:19 +0100 | [diff] [blame] | 1392 | #ifdef CONFIG_NET |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 1393 | static const struct genl_multicast_group thermal_event_mcgrps[] = { |
| 1394 | { .name = THERMAL_GENL_MCAST_GROUP_NAME, }, |
| 1395 | }; |
| 1396 | |
Rafael J. Wysocki | af06216 | 2011-03-01 01:12:19 +0100 | [diff] [blame] | 1397 | static struct genl_family thermal_event_genl_family = { |
| 1398 | .id = GENL_ID_GENERATE, |
| 1399 | .name = THERMAL_GENL_FAMILY_NAME, |
| 1400 | .version = THERMAL_GENL_VERSION, |
| 1401 | .maxattr = THERMAL_GENL_ATTR_MAX, |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 1402 | .mcgrps = thermal_event_mcgrps, |
| 1403 | .n_mcgrps = ARRAY_SIZE(thermal_event_mcgrps), |
Rafael J. Wysocki | af06216 | 2011-03-01 01:12:19 +0100 | [diff] [blame] | 1404 | }; |
| 1405 | |
Eduardo Valentin | 8ab3e6a | 2013-01-02 15:29:39 +0000 | [diff] [blame] | 1406 | int thermal_generate_netlink_event(struct thermal_zone_device *tz, |
Eduardo Valentin | eb7be32 | 2016-11-07 21:09:21 -0800 | [diff] [blame] | 1407 | enum events event) |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1408 | { |
| 1409 | struct sk_buff *skb; |
| 1410 | struct nlattr *attr; |
| 1411 | struct thermal_genl_event *thermal_event; |
| 1412 | void *msg_header; |
| 1413 | int size; |
| 1414 | int result; |
Fabio Estevam | b11de07 | 2012-03-21 12:55:00 -0700 | [diff] [blame] | 1415 | static unsigned int thermal_event_seqnum; |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1416 | |
Eduardo Valentin | 8ab3e6a | 2013-01-02 15:29:39 +0000 | [diff] [blame] | 1417 | if (!tz) |
| 1418 | return -EINVAL; |
| 1419 | |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1420 | /* allocate memory */ |
Joe Perches | 886ee54 | 2012-03-21 12:55:01 -0700 | [diff] [blame] | 1421 | size = nla_total_size(sizeof(struct thermal_genl_event)) + |
| 1422 | nla_total_size(0); |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1423 | |
| 1424 | skb = genlmsg_new(size, GFP_ATOMIC); |
| 1425 | if (!skb) |
| 1426 | return -ENOMEM; |
| 1427 | |
| 1428 | /* add the genetlink message header */ |
| 1429 | msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++, |
| 1430 | &thermal_event_genl_family, 0, |
| 1431 | THERMAL_GENL_CMD_EVENT); |
| 1432 | if (!msg_header) { |
| 1433 | nlmsg_free(skb); |
| 1434 | return -ENOMEM; |
| 1435 | } |
| 1436 | |
| 1437 | /* fill the data */ |
Joe Perches | 886ee54 | 2012-03-21 12:55:01 -0700 | [diff] [blame] | 1438 | attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT, |
| 1439 | sizeof(struct thermal_genl_event)); |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1440 | |
| 1441 | if (!attr) { |
| 1442 | nlmsg_free(skb); |
| 1443 | return -EINVAL; |
| 1444 | } |
| 1445 | |
| 1446 | thermal_event = nla_data(attr); |
| 1447 | if (!thermal_event) { |
| 1448 | nlmsg_free(skb); |
| 1449 | return -EINVAL; |
| 1450 | } |
| 1451 | |
| 1452 | memset(thermal_event, 0, sizeof(struct thermal_genl_event)); |
| 1453 | |
Eduardo Valentin | 8ab3e6a | 2013-01-02 15:29:39 +0000 | [diff] [blame] | 1454 | thermal_event->orig = tz->id; |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1455 | thermal_event->event = event; |
| 1456 | |
| 1457 | /* send multicast genetlink message */ |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 1458 | genlmsg_end(skb, msg_header); |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1459 | |
Johannes Berg | 68eb550 | 2013-11-19 15:19:38 +0100 | [diff] [blame] | 1460 | result = genlmsg_multicast(&thermal_event_genl_family, skb, 0, |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 1461 | 0, GFP_ATOMIC); |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1462 | if (result) |
Eduardo Valentin | 923e0b1 | 2013-01-02 15:29:41 +0000 | [diff] [blame] | 1463 | dev_err(&tz->device, "Failed to send netlink event:%d", result); |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1464 | |
| 1465 | return result; |
| 1466 | } |
Eduardo Valentin | 910cb1e | 2013-04-23 21:48:15 +0000 | [diff] [blame] | 1467 | EXPORT_SYMBOL_GPL(thermal_generate_netlink_event); |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1468 | |
| 1469 | static int genetlink_init(void) |
| 1470 | { |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 1471 | return genl_register_family(&thermal_event_genl_family); |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1472 | } |
| 1473 | |
Rafael J. Wysocki | af06216 | 2011-03-01 01:12:19 +0100 | [diff] [blame] | 1474 | static void genetlink_exit(void) |
| 1475 | { |
| 1476 | genl_unregister_family(&thermal_event_genl_family); |
| 1477 | } |
| 1478 | #else /* !CONFIG_NET */ |
| 1479 | static inline int genetlink_init(void) { return 0; } |
| 1480 | static inline void genetlink_exit(void) {} |
| 1481 | #endif /* !CONFIG_NET */ |
| 1482 | |
Zhang Rui | ff140fe | 2015-10-30 16:31:58 +0800 | [diff] [blame] | 1483 | static int thermal_pm_notify(struct notifier_block *nb, |
Eduardo Valentin | eb7be32 | 2016-11-07 21:09:21 -0800 | [diff] [blame] | 1484 | unsigned long mode, void *_unused) |
Zhang Rui | ff140fe | 2015-10-30 16:31:58 +0800 | [diff] [blame] | 1485 | { |
| 1486 | struct thermal_zone_device *tz; |
| 1487 | |
| 1488 | switch (mode) { |
| 1489 | case PM_HIBERNATION_PREPARE: |
| 1490 | case PM_RESTORE_PREPARE: |
| 1491 | case PM_SUSPEND_PREPARE: |
| 1492 | atomic_set(&in_suspend, 1); |
| 1493 | break; |
| 1494 | case PM_POST_HIBERNATION: |
| 1495 | case PM_POST_RESTORE: |
| 1496 | case PM_POST_SUSPEND: |
| 1497 | atomic_set(&in_suspend, 0); |
| 1498 | list_for_each_entry(tz, &thermal_tz_list, node) { |
| 1499 | thermal_zone_device_reset(tz); |
Srinivas Pandruvada | 0e70f46 | 2016-08-26 16:21:16 -0700 | [diff] [blame] | 1500 | thermal_zone_device_update(tz, |
| 1501 | THERMAL_EVENT_UNSPECIFIED); |
Zhang Rui | ff140fe | 2015-10-30 16:31:58 +0800 | [diff] [blame] | 1502 | } |
| 1503 | break; |
| 1504 | default: |
| 1505 | break; |
| 1506 | } |
| 1507 | return 0; |
| 1508 | } |
| 1509 | |
| 1510 | static struct notifier_block thermal_pm_nb = { |
| 1511 | .notifier_call = thermal_pm_notify, |
| 1512 | }; |
| 1513 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1514 | static int __init thermal_init(void) |
| 1515 | { |
Zhang Rui | 80a26a5 | 2013-03-26 16:38:29 +0800 | [diff] [blame] | 1516 | int result; |
| 1517 | |
| 1518 | result = thermal_register_governors(); |
| 1519 | if (result) |
| 1520 | goto error; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1521 | |
| 1522 | result = class_register(&thermal_class); |
Zhang Rui | 80a26a5 | 2013-03-26 16:38:29 +0800 | [diff] [blame] | 1523 | if (result) |
| 1524 | goto unregister_governors; |
| 1525 | |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1526 | result = genetlink_init(); |
Zhang Rui | 80a26a5 | 2013-03-26 16:38:29 +0800 | [diff] [blame] | 1527 | if (result) |
| 1528 | goto unregister_class; |
| 1529 | |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1530 | result = of_parse_thermal_zones(); |
| 1531 | if (result) |
| 1532 | goto exit_netlink; |
| 1533 | |
Zhang Rui | ff140fe | 2015-10-30 16:31:58 +0800 | [diff] [blame] | 1534 | result = register_pm_notifier(&thermal_pm_nb); |
| 1535 | if (result) |
| 1536 | pr_warn("Thermal: Can not register suspend notifier, return %d\n", |
| 1537 | result); |
| 1538 | |
Zhang Rui | 80a26a5 | 2013-03-26 16:38:29 +0800 | [diff] [blame] | 1539 | return 0; |
| 1540 | |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1541 | exit_netlink: |
| 1542 | genetlink_exit(); |
Zhang Rui | 80a26a5 | 2013-03-26 16:38:29 +0800 | [diff] [blame] | 1543 | unregister_class: |
| 1544 | class_unregister(&thermal_class); |
Luis Henriques | 9d367e5 | 2014-12-03 21:20:21 +0000 | [diff] [blame] | 1545 | unregister_governors: |
| 1546 | thermal_unregister_governors(); |
Zhang Rui | 80a26a5 | 2013-03-26 16:38:29 +0800 | [diff] [blame] | 1547 | error: |
| 1548 | idr_destroy(&thermal_tz_idr); |
| 1549 | idr_destroy(&thermal_cdev_idr); |
| 1550 | mutex_destroy(&thermal_idr_lock); |
| 1551 | mutex_destroy(&thermal_list_lock); |
| 1552 | mutex_destroy(&thermal_governor_lock); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1553 | return result; |
| 1554 | } |
| 1555 | |
| 1556 | static void __exit thermal_exit(void) |
| 1557 | { |
Zhang Rui | ff140fe | 2015-10-30 16:31:58 +0800 | [diff] [blame] | 1558 | unregister_pm_notifier(&thermal_pm_nb); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1559 | of_thermal_destroy_zones(); |
Zhang Rui | 80a26a5 | 2013-03-26 16:38:29 +0800 | [diff] [blame] | 1560 | genetlink_exit(); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1561 | class_unregister(&thermal_class); |
Zhang Rui | 80a26a5 | 2013-03-26 16:38:29 +0800 | [diff] [blame] | 1562 | thermal_unregister_governors(); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1563 | idr_destroy(&thermal_tz_idr); |
| 1564 | idr_destroy(&thermal_cdev_idr); |
| 1565 | mutex_destroy(&thermal_idr_lock); |
| 1566 | mutex_destroy(&thermal_list_lock); |
Zhang Rui | 80a26a5 | 2013-03-26 16:38:29 +0800 | [diff] [blame] | 1567 | mutex_destroy(&thermal_governor_lock); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1568 | } |
| 1569 | |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1570 | fs_initcall(thermal_init); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1571 | module_exit(thermal_exit); |