blob: c12193315a15ea192aa83055501c3eae379b2b97 [file] [log] [blame]
Zhang Rui203d3d42008-01-17 15:51:08 +08001/*
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 Perchesc5a01dd2012-03-21 12:55:02 -070026#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27
Zhang Rui203d3d42008-01-17 15:51:08 +080028#include <linux/module.h>
29#include <linux/device.h>
30#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Zhang Rui203d3d42008-01-17 15:51:08 +080032#include <linux/kdev_t.h>
33#include <linux/idr.h>
34#include <linux/thermal.h>
Matthew Garrettb1569e92008-12-03 17:55:32 +000035#include <linux/reboot.h>
Andy Shevchenko42a5bf52013-05-17 11:52:02 +000036#include <linux/string.h>
Eduardo Valentina116b5d2013-09-26 15:55:01 -040037#include <linux/of.h>
R.Durgadoss4cb18722010-10-27 03:33:29 +053038#include <net/netlink.h>
39#include <net/genetlink.h>
Zhang Ruiff140fe2015-10-30 16:31:58 +080040#include <linux/suspend.h>
Zhang Rui203d3d42008-01-17 15:51:08 +080041
Punit Agrawal100a8fd2014-07-29 11:50:48 +010042#define CREATE_TRACE_POINTS
43#include <trace/events/thermal.h>
44
Durgadoss R71350db2012-09-18 11:04:53 +053045#include "thermal_core.h"
Eduardo Valentin0dd88792013-07-03 15:14:28 -040046#include "thermal_hwmon.h"
Durgadoss R71350db2012-09-18 11:04:53 +053047
Zhang Rui63c4ec92008-04-21 16:07:13 +080048MODULE_AUTHOR("Zhang Rui");
Zhang Rui203d3d42008-01-17 15:51:08 +080049MODULE_DESCRIPTION("Generic thermal management sysfs support");
Eduardo Valentin6d8d4972013-04-23 21:48:13 +000050MODULE_LICENSE("GPL v2");
Zhang Rui203d3d42008-01-17 15:51:08 +080051
Zhang Rui203d3d42008-01-17 15:51:08 +080052static DEFINE_IDR(thermal_tz_idr);
53static DEFINE_IDR(thermal_cdev_idr);
54static DEFINE_MUTEX(thermal_idr_lock);
55
56static LIST_HEAD(thermal_tz_list);
57static LIST_HEAD(thermal_cdev_list);
Durgadoss Ra4a15482012-09-18 11:04:57 +053058static LIST_HEAD(thermal_governor_list);
59
Zhang Rui203d3d42008-01-17 15:51:08 +080060static DEFINE_MUTEX(thermal_list_lock);
Durgadoss Ra4a15482012-09-18 11:04:57 +053061static DEFINE_MUTEX(thermal_governor_lock);
62
Zhang Ruiff140fe2015-10-30 16:31:58 +080063static atomic_t in_suspend;
64
Zhang Ruif2234bc2014-01-24 10:23:19 +080065static struct thermal_governor *def_governor;
66
Eduardo Valentin1b4f4842016-11-07 21:09:05 -080067/*
68 * Governor section: set of functions to handle thermal governors
69 *
70 * Functions to help in the life cycle of thermal governors within
71 * the thermal core and by the thermal governor code.
72 */
73
Durgadoss Ra4a15482012-09-18 11:04:57 +053074static struct thermal_governor *__find_governor(const char *name)
75{
76 struct thermal_governor *pos;
77
Zhang Ruif2234bc2014-01-24 10:23:19 +080078 if (!name || !name[0])
79 return def_governor;
80
Durgadoss Ra4a15482012-09-18 11:04:57 +053081 list_for_each_entry(pos, &thermal_governor_list, governor_list)
Rasmus Villemoes484ac2f2014-10-13 15:55:01 -070082 if (!strncasecmp(name, pos->name, THERMAL_NAME_LENGTH))
Durgadoss Ra4a15482012-09-18 11:04:57 +053083 return pos;
84
85 return NULL;
86}
87
Javi Merinoe33df1d2015-02-26 19:00:27 +000088/**
89 * bind_previous_governor() - bind the previous governor of the thermal zone
90 * @tz: a valid pointer to a struct thermal_zone_device
91 * @failed_gov_name: the name of the governor that failed to register
92 *
93 * Register the previous governor of the thermal zone after a new
94 * governor has failed to be bound.
95 */
96static void bind_previous_governor(struct thermal_zone_device *tz,
97 const char *failed_gov_name)
98{
99 if (tz->governor && tz->governor->bind_to_tz) {
100 if (tz->governor->bind_to_tz(tz)) {
101 dev_err(&tz->device,
102 "governor %s failed to bind and the previous one (%s) failed to bind again, thermal zone %s has no governor\n",
103 failed_gov_name, tz->governor->name, tz->type);
104 tz->governor = NULL;
105 }
106 }
107}
108
109/**
110 * thermal_set_governor() - Switch to another governor
111 * @tz: a valid pointer to a struct thermal_zone_device
112 * @new_gov: pointer to the new governor
113 *
114 * Change the governor of thermal zone @tz.
115 *
116 * Return: 0 on success, an error if the new governor's bind_to_tz() failed.
117 */
118static int thermal_set_governor(struct thermal_zone_device *tz,
119 struct thermal_governor *new_gov)
120{
121 int ret = 0;
122
123 if (tz->governor && tz->governor->unbind_from_tz)
124 tz->governor->unbind_from_tz(tz);
125
126 if (new_gov && new_gov->bind_to_tz) {
127 ret = new_gov->bind_to_tz(tz);
128 if (ret) {
129 bind_previous_governor(tz, new_gov->name);
130
131 return ret;
132 }
133 }
134
135 tz->governor = new_gov;
136
137 return ret;
138}
139
Durgadoss Ra4a15482012-09-18 11:04:57 +0530140int thermal_register_governor(struct thermal_governor *governor)
141{
142 int err;
143 const char *name;
144 struct thermal_zone_device *pos;
145
146 if (!governor)
147 return -EINVAL;
148
149 mutex_lock(&thermal_governor_lock);
150
151 err = -EBUSY;
152 if (__find_governor(governor->name) == NULL) {
153 err = 0;
154 list_add(&governor->governor_list, &thermal_governor_list);
Zhang Ruif2234bc2014-01-24 10:23:19 +0800155 if (!def_governor && !strncmp(governor->name,
156 DEFAULT_THERMAL_GOVERNOR, THERMAL_NAME_LENGTH))
157 def_governor = governor;
Durgadoss Ra4a15482012-09-18 11:04:57 +0530158 }
159
160 mutex_lock(&thermal_list_lock);
161
162 list_for_each_entry(pos, &thermal_tz_list, node) {
Zhang Ruif2234bc2014-01-24 10:23:19 +0800163 /*
164 * only thermal zones with specified tz->tzp->governor_name
165 * may run with tz->govenor unset
166 */
Durgadoss Ra4a15482012-09-18 11:04:57 +0530167 if (pos->governor)
168 continue;
Zhang Ruif2234bc2014-01-24 10:23:19 +0800169
170 name = pos->tzp->governor_name;
171
Javi Merinoe33df1d2015-02-26 19:00:27 +0000172 if (!strncasecmp(name, governor->name, THERMAL_NAME_LENGTH)) {
173 int ret;
174
175 ret = thermal_set_governor(pos, governor);
176 if (ret)
177 dev_err(&pos->device,
178 "Failed to set governor %s for thermal zone %s: %d\n",
179 governor->name, pos->type, ret);
180 }
Durgadoss Ra4a15482012-09-18 11:04:57 +0530181 }
182
183 mutex_unlock(&thermal_list_lock);
184 mutex_unlock(&thermal_governor_lock);
185
186 return err;
187}
Durgadoss Ra4a15482012-09-18 11:04:57 +0530188
189void thermal_unregister_governor(struct thermal_governor *governor)
190{
191 struct thermal_zone_device *pos;
192
193 if (!governor)
194 return;
195
196 mutex_lock(&thermal_governor_lock);
197
198 if (__find_governor(governor->name) == NULL)
199 goto exit;
200
201 mutex_lock(&thermal_list_lock);
202
203 list_for_each_entry(pos, &thermal_tz_list, node) {
Rasmus Villemoes484ac2f2014-10-13 15:55:01 -0700204 if (!strncasecmp(pos->governor->name, governor->name,
Durgadoss Ra4a15482012-09-18 11:04:57 +0530205 THERMAL_NAME_LENGTH))
Javi Merinoe33df1d2015-02-26 19:00:27 +0000206 thermal_set_governor(pos, NULL);
Durgadoss Ra4a15482012-09-18 11:04:57 +0530207 }
208
209 mutex_unlock(&thermal_list_lock);
210 list_del(&governor->governor_list);
211exit:
212 mutex_unlock(&thermal_governor_lock);
213 return;
214}
Zhang Rui203d3d42008-01-17 15:51:08 +0800215
Eduardo Valentin1b4f4842016-11-07 21:09:05 -0800216int thermal_zone_device_set_policy(struct thermal_zone_device *tz,
217 char *policy)
218{
219 struct thermal_governor *gov;
220 int ret = -EINVAL;
221
222 mutex_lock(&thermal_governor_lock);
223 mutex_lock(&tz->lock);
224
225 gov = __find_governor(strim(policy));
226 if (!gov)
227 goto exit;
228
229 ret = thermal_set_governor(tz, gov);
230
231exit:
232 mutex_unlock(&tz->lock);
233 mutex_unlock(&thermal_governor_lock);
234
235 return ret;
236}
237
238int thermal_build_list_of_policies(char *buf)
239{
240 struct thermal_governor *pos;
241 ssize_t count = 0;
242 ssize_t size = PAGE_SIZE;
243
244 mutex_lock(&thermal_governor_lock);
245
246 list_for_each_entry(pos, &thermal_governor_list, governor_list) {
247 size = PAGE_SIZE - count;
248 count += scnprintf(buf + count, size, "%s ", pos->name);
249 }
250 count += scnprintf(buf + count, size, "\n");
251
252 mutex_unlock(&thermal_governor_lock);
253
254 return count;
255}
256
257static int __init thermal_register_governors(void)
258{
259 int result;
260
261 result = thermal_gov_step_wise_register();
262 if (result)
263 return result;
264
265 result = thermal_gov_fair_share_register();
266 if (result)
267 return result;
268
269 result = thermal_gov_bang_bang_register();
270 if (result)
271 return result;
272
273 result = thermal_gov_user_space_register();
274 if (result)
275 return result;
276
277 return thermal_gov_power_allocator_register();
278}
279
280static void thermal_unregister_governors(void)
281{
282 thermal_gov_step_wise_unregister();
283 thermal_gov_fair_share_unregister();
284 thermal_gov_bang_bang_unregister();
285 thermal_gov_user_space_unregister();
286 thermal_gov_power_allocator_unregister();
287}
288
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530289static void print_bind_err_msg(struct thermal_zone_device *tz,
290 struct thermal_cooling_device *cdev, int ret)
291{
292 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
293 tz->type, cdev->type, ret);
294}
295
296static void __bind(struct thermal_zone_device *tz, int mask,
Eduardo Valentina8892d832013-07-16 15:26:28 -0400297 struct thermal_cooling_device *cdev,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000298 unsigned long *limits,
299 unsigned int weight)
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530300{
301 int i, ret;
302
303 for (i = 0; i < tz->trips; i++) {
304 if (mask & (1 << i)) {
Eduardo Valentina8892d832013-07-16 15:26:28 -0400305 unsigned long upper, lower;
306
307 upper = THERMAL_NO_LIMIT;
308 lower = THERMAL_NO_LIMIT;
309 if (limits) {
310 lower = limits[i * 2];
311 upper = limits[i * 2 + 1];
312 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530313 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000314 upper, lower,
315 weight);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530316 if (ret)
317 print_bind_err_msg(tz, cdev, ret);
318 }
319 }
320}
321
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530322static void bind_cdev(struct thermal_cooling_device *cdev)
323{
324 int i, ret;
325 const struct thermal_zone_params *tzp;
326 struct thermal_zone_device *pos = NULL;
327
328 mutex_lock(&thermal_list_lock);
329
330 list_for_each_entry(pos, &thermal_tz_list, node) {
331 if (!pos->tzp && !pos->ops->bind)
332 continue;
333
Ni Wadea9f2d192013-11-06 14:30:13 +0800334 if (pos->ops->bind) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530335 ret = pos->ops->bind(pos, cdev);
336 if (ret)
337 print_bind_err_msg(pos, cdev, ret);
Ni Wadea9f2d192013-11-06 14:30:13 +0800338 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530339 }
340
341 tzp = pos->tzp;
Hugh Dickins791700cd2012-09-27 16:48:28 -0700342 if (!tzp || !tzp->tbp)
343 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530344
345 for (i = 0; i < tzp->num_tbps; i++) {
346 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
347 continue;
348 if (tzp->tbp[i].match(pos, cdev))
349 continue;
350 tzp->tbp[i].cdev = cdev;
Eduardo Valentina8892d832013-07-16 15:26:28 -0400351 __bind(pos, tzp->tbp[i].trip_mask, cdev,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000352 tzp->tbp[i].binding_limits,
353 tzp->tbp[i].weight);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530354 }
355 }
356
357 mutex_unlock(&thermal_list_lock);
358}
359
360static void bind_tz(struct thermal_zone_device *tz)
361{
362 int i, ret;
363 struct thermal_cooling_device *pos = NULL;
364 const struct thermal_zone_params *tzp = tz->tzp;
365
366 if (!tzp && !tz->ops->bind)
367 return;
368
369 mutex_lock(&thermal_list_lock);
370
Ni Wadea9f2d192013-11-06 14:30:13 +0800371 /* If there is ops->bind, try to use ops->bind */
372 if (tz->ops->bind) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530373 list_for_each_entry(pos, &thermal_cdev_list, node) {
374 ret = tz->ops->bind(tz, pos);
375 if (ret)
376 print_bind_err_msg(tz, pos, ret);
377 }
378 goto exit;
379 }
380
Hugh Dickins791700cd2012-09-27 16:48:28 -0700381 if (!tzp || !tzp->tbp)
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530382 goto exit;
383
384 list_for_each_entry(pos, &thermal_cdev_list, node) {
385 for (i = 0; i < tzp->num_tbps; i++) {
386 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
387 continue;
388 if (tzp->tbp[i].match(tz, pos))
389 continue;
390 tzp->tbp[i].cdev = pos;
Eduardo Valentina8892d832013-07-16 15:26:28 -0400391 __bind(tz, tzp->tbp[i].trip_mask, pos,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000392 tzp->tbp[i].binding_limits,
393 tzp->tbp[i].weight);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530394 }
395 }
396exit:
397 mutex_unlock(&thermal_list_lock);
398}
399
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530400static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
401 int delay)
402{
403 if (delay > 1000)
404 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
405 round_jiffies(msecs_to_jiffies(delay)));
406 else if (delay)
407 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
408 msecs_to_jiffies(delay));
409 else
410 cancel_delayed_work(&tz->poll_queue);
411}
412
413static void monitor_thermal_zone(struct thermal_zone_device *tz)
414{
415 mutex_lock(&tz->lock);
416
417 if (tz->passive)
418 thermal_zone_device_set_polling(tz, tz->passive_delay);
419 else if (tz->polling_delay)
420 thermal_zone_device_set_polling(tz, tz->polling_delay);
421 else
422 thermal_zone_device_set_polling(tz, 0);
423
424 mutex_unlock(&tz->lock);
425}
426
427static void handle_non_critical_trips(struct thermal_zone_device *tz,
428 int trip, enum thermal_trip_type trip_type)
429{
Zhang Ruif2234bc2014-01-24 10:23:19 +0800430 tz->governor ? tz->governor->throttle(tz, trip) :
431 def_governor->throttle(tz, trip);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530432}
433
434static void handle_critical_trips(struct thermal_zone_device *tz,
435 int trip, enum thermal_trip_type trip_type)
436{
Sascha Hauer17e83512015-07-24 08:12:54 +0200437 int trip_temp;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530438
439 tz->ops->get_trip_temp(tz, trip, &trip_temp);
440
441 /* If we have not crossed the trip_temp, we do not care. */
Srinivas Pandruvada84ffe3e2014-11-12 15:43:29 -0800442 if (trip_temp <= 0 || tz->temperature < trip_temp)
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530443 return;
444
Punit Agrawal208cd822014-07-29 11:50:50 +0100445 trace_thermal_zone_trip(tz, trip, trip_type);
446
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530447 if (tz->ops->notify)
448 tz->ops->notify(tz, trip, trip_type);
449
450 if (trip_type == THERMAL_TRIP_CRITICAL) {
Eduardo Valentin923e0b12013-01-02 15:29:41 +0000451 dev_emerg(&tz->device,
452 "critical temperature reached(%d C),shutting down\n",
453 tz->temperature / 1000);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530454 orderly_poweroff(true);
455 }
456}
457
458static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
459{
460 enum thermal_trip_type type;
461
Zhang Rui81ad4272016-03-18 10:03:24 +0800462 /* Ignore disabled trip points */
463 if (test_bit(trip, &tz->trips_disabled))
464 return;
465
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530466 tz->ops->get_trip_type(tz, trip, &type);
467
468 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
469 handle_critical_trips(tz, trip, type);
470 else
471 handle_non_critical_trips(tz, trip, type);
472 /*
473 * Alright, we handled this trip successfully.
474 * So, start monitoring again.
475 */
476 monitor_thermal_zone(tz);
477}
478
479static void update_temperature(struct thermal_zone_device *tz)
480{
Sascha Hauer17e83512015-07-24 08:12:54 +0200481 int temp, ret;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530482
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000483 ret = thermal_zone_get_temp(tz, &temp);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530484 if (ret) {
Hans de Goede7e497a72015-03-21 15:02:55 +0100485 if (ret != -EAGAIN)
486 dev_warn(&tz->device,
487 "failed to read out thermal zone (%d)\n",
488 ret);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000489 return;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530490 }
491
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000492 mutex_lock(&tz->lock);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530493 tz->last_temperature = tz->temperature;
494 tz->temperature = temp;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530495 mutex_unlock(&tz->lock);
Aaron Lu06475b52013-12-02 13:54:26 +0800496
Punit Agrawal100a8fd2014-07-29 11:50:48 +0100497 trace_thermal_temperature(tz);
Zhang Ruibb431ba2015-10-30 16:31:47 +0800498 if (tz->last_temperature == THERMAL_TEMP_INVALID)
499 dev_dbg(&tz->device, "last_temperature N/A, current_temperature=%d\n",
500 tz->temperature);
501 else
502 dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n",
503 tz->last_temperature, tz->temperature);
504}
505
506static void thermal_zone_device_reset(struct thermal_zone_device *tz)
507{
508 struct thermal_instance *pos;
509
510 tz->temperature = THERMAL_TEMP_INVALID;
511 tz->passive = 0;
512 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
513 pos->initialized = false;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530514}
515
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700516void thermal_zone_device_update(struct thermal_zone_device *tz,
517 enum thermal_notify_event event)
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530518{
519 int count;
520
Zhang Ruiff140fe2015-10-30 16:31:58 +0800521 if (atomic_read(&in_suspend))
522 return;
523
Eduardo Valentin81bd4e12013-09-12 19:15:44 -0400524 if (!tz->ops->get_temp)
525 return;
526
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530527 update_temperature(tz);
528
Sascha Hauer060c0342016-06-22 16:42:01 +0800529 thermal_zone_set_trips(tz);
530
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700531 tz->notify_event = event;
532
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530533 for (count = 0; count < tz->trips; count++)
534 handle_thermal_trip(tz, count);
535}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000536EXPORT_SYMBOL_GPL(thermal_zone_device_update);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530537
538static void thermal_zone_device_check(struct work_struct *work)
539{
540 struct thermal_zone_device *tz = container_of(work, struct
541 thermal_zone_device,
542 poll_queue.work);
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700543 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530544}
545
Eduardo Valentin1a7e7cc2016-11-07 21:08:47 -0800546/**
547 * power_actor_get_max_power() - get the maximum power that a cdev can consume
548 * @cdev: pointer to &thermal_cooling_device
549 * @tz: a valid thermal zone device pointer
550 * @max_power: pointer in which to store the maximum power
551 *
552 * Calculate the maximum power consumption in milliwats that the
553 * cooling device can currently consume and store it in @max_power.
554 *
555 * Return: 0 on success, -EINVAL if @cdev doesn't support the
556 * power_actor API or -E* on other error.
557 */
558int power_actor_get_max_power(struct thermal_cooling_device *cdev,
559 struct thermal_zone_device *tz, u32 *max_power)
560{
561 if (!cdev_is_power_actor(cdev))
562 return -EINVAL;
563
564 return cdev->ops->state2power(cdev, tz, 0, max_power);
565}
566
567/**
568 * power_actor_get_min_power() - get the mainimum power that a cdev can consume
569 * @cdev: pointer to &thermal_cooling_device
570 * @tz: a valid thermal zone device pointer
571 * @min_power: pointer in which to store the minimum power
572 *
573 * Calculate the minimum power consumption in milliwatts that the
574 * cooling device can currently consume and store it in @min_power.
575 *
576 * Return: 0 on success, -EINVAL if @cdev doesn't support the
577 * power_actor API or -E* on other error.
578 */
579int power_actor_get_min_power(struct thermal_cooling_device *cdev,
580 struct thermal_zone_device *tz, u32 *min_power)
581{
582 unsigned long max_state;
583 int ret;
584
585 if (!cdev_is_power_actor(cdev))
586 return -EINVAL;
587
588 ret = cdev->ops->get_max_state(cdev, &max_state);
589 if (ret)
590 return ret;
591
592 return cdev->ops->state2power(cdev, tz, max_state, min_power);
593}
594
595/**
596 * power_actor_set_power() - limit the maximum power a cooling device consumes
597 * @cdev: pointer to &thermal_cooling_device
598 * @instance: thermal instance to update
599 * @power: the power in milliwatts
600 *
601 * Set the cooling device to consume at most @power milliwatts. The limit is
602 * expected to be a cap at the maximum power consumption.
603 *
604 * Return: 0 on success, -EINVAL if the cooling device does not
605 * implement the power actor API or -E* for other failures.
606 */
607int power_actor_set_power(struct thermal_cooling_device *cdev,
608 struct thermal_instance *instance, u32 power)
609{
610 unsigned long state;
611 int ret;
612
613 if (!cdev_is_power_actor(cdev))
614 return -EINVAL;
615
616 ret = cdev->ops->power2state(cdev, instance->tz, power, &state);
617 if (ret)
618 return ret;
619
620 instance->target = state;
621 mutex_lock(&cdev->lock);
622 cdev->updated = false;
623 mutex_unlock(&cdev->lock);
624 thermal_cdev_update(cdev);
625
626 return 0;
627}
628
Eduardo Valentin3d0055d2016-11-07 21:08:54 -0800629void thermal_zone_device_rebind_exception(struct thermal_zone_device *tz,
630 const char *cdev_type, size_t size)
631{
632 struct thermal_cooling_device *cdev = NULL;
633
634 mutex_lock(&thermal_list_lock);
635 list_for_each_entry(cdev, &thermal_cdev_list, node) {
636 /* skip non matching cdevs */
637 if (strncmp(cdev_type, cdev->type, size))
638 continue;
639
640 /* re binding the exception matching the type pattern */
641 thermal_zone_bind_cooling_device(tz, THERMAL_TRIPS_NONE, cdev,
642 THERMAL_NO_LIMIT,
643 THERMAL_NO_LIMIT,
644 THERMAL_WEIGHT_DEFAULT);
645 }
646 mutex_unlock(&thermal_list_lock);
647}
648
649void thermal_zone_device_unbind_exception(struct thermal_zone_device *tz,
650 const char *cdev_type, size_t size)
651{
652 struct thermal_cooling_device *cdev = NULL;
653
654 mutex_lock(&thermal_list_lock);
655 list_for_each_entry(cdev, &thermal_cdev_list, node) {
656 /* skip non matching cdevs */
657 if (strncmp(cdev_type, cdev->type, size))
658 continue;
659 /* unbinding the exception matching the type pattern */
660 thermal_zone_unbind_cooling_device(tz, THERMAL_TRIPS_NONE,
661 cdev);
662 }
663 mutex_unlock(&thermal_list_lock);
664}
665
Zhang Rui203d3d42008-01-17 15:51:08 +0800666/* Device management */
667
Eduardo Valentinc30176f2016-11-07 21:09:06 -0800668static int get_idr(struct idr *idr, struct mutex *lock, int *id)
669{
670 int ret;
671
672 if (lock)
673 mutex_lock(lock);
674 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
675 if (lock)
676 mutex_unlock(lock);
677 if (unlikely(ret < 0))
678 return ret;
679 *id = ret;
680 return 0;
681}
682
683static void release_idr(struct idr *idr, struct mutex *lock, int id)
684{
685 if (lock)
686 mutex_lock(lock);
687 idr_remove(idr, id);
688 if (lock)
689 mutex_unlock(lock);
690}
691
Zhang Rui203d3d42008-01-17 15:51:08 +0800692/**
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000693 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
694 * @tz: pointer to struct thermal_zone_device
Zhang Rui203d3d42008-01-17 15:51:08 +0800695 * @trip: indicates which trip point the cooling devices is
696 * associated with in this thermal zone.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000697 * @cdev: pointer to struct thermal_cooling_device
698 * @upper: the Maximum cooling state for this trip point.
699 * THERMAL_NO_LIMIT means no upper limit,
700 * and the cooling device can be in max_state.
701 * @lower: the Minimum cooling state can be used for this trip point.
702 * THERMAL_NO_LIMIT means no lower limit,
703 * and the cooling device can be in cooling state 0.
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000704 * @weight: The weight of the cooling device to be bound to the
705 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
706 * default value
Len Brown543a9562008-02-07 16:55:08 -0500707 *
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000708 * This interface function bind a thermal cooling device to the certain trip
709 * point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -0500710 * This function is usually called in the thermal zone device .bind callback.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000711 *
712 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +0800713 */
714int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
715 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +0800716 struct thermal_cooling_device *cdev,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000717 unsigned long upper, unsigned long lower,
718 unsigned int weight)
Zhang Rui203d3d42008-01-17 15:51:08 +0800719{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800720 struct thermal_instance *dev;
721 struct thermal_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -0500722 struct thermal_zone_device *pos1;
723 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +0800724 unsigned long max_state;
Lukasz Majewski9a3031d2014-11-18 11:16:30 +0100725 int result, ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800726
Len Brown543a9562008-02-07 16:55:08 -0500727 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +0800728 return -EINVAL;
729
Thomas Sujithc7516702008-02-15 00:58:50 -0500730 list_for_each_entry(pos1, &thermal_tz_list, node) {
731 if (pos1 == tz)
732 break;
733 }
734 list_for_each_entry(pos2, &thermal_cdev_list, node) {
735 if (pos2 == cdev)
736 break;
737 }
738
739 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +0800740 return -EINVAL;
741
Lukasz Majewski9a3031d2014-11-18 11:16:30 +0100742 ret = cdev->ops->get_max_state(cdev, &max_state);
743 if (ret)
744 return ret;
Zhang Rui9d998422012-06-26 16:35:57 +0800745
746 /* lower default 0, upper default max_state */
747 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
748 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
749
750 if (lower > upper || upper > max_state)
751 return -EINVAL;
752
Zhang Rui203d3d42008-01-17 15:51:08 +0800753 dev =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800754 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800755 if (!dev)
756 return -ENOMEM;
757 dev->tz = tz;
758 dev->cdev = cdev;
759 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +0800760 dev->upper = upper;
761 dev->lower = lower;
Zhang Ruice119f82012-06-27 14:13:04 +0800762 dev->target = THERMAL_NO_TARGET;
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000763 dev->weight = weight;
Zhang Rui74051ba2012-06-26 16:27:22 +0800764
Zhang Rui203d3d42008-01-17 15:51:08 +0800765 result = get_idr(&tz->idr, &tz->lock, &dev->id);
766 if (result)
767 goto free_mem;
768
769 sprintf(dev->name, "cdev%d", dev->id);
770 result =
771 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
772 if (result)
773 goto release_idr;
774
775 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -0700776 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800777 dev->attr.attr.name = dev->attr_name;
778 dev->attr.attr.mode = 0444;
779 dev->attr.show = thermal_cooling_device_trip_point_show;
780 result = device_create_file(&tz->device, &dev->attr);
781 if (result)
782 goto remove_symbol_link;
783
Javi Merinodb916512015-02-18 16:04:24 +0000784 sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
785 sysfs_attr_init(&dev->weight_attr.attr);
786 dev->weight_attr.attr.name = dev->weight_attr_name;
787 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
788 dev->weight_attr.show = thermal_cooling_device_weight_show;
789 dev->weight_attr.store = thermal_cooling_device_weight_store;
790 result = device_create_file(&tz->device, &dev->weight_attr);
791 if (result)
792 goto remove_trip_file;
793
Zhang Rui203d3d42008-01-17 15:51:08 +0800794 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800795 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +0800796 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
Zhang Rui203d3d42008-01-17 15:51:08 +0800797 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
798 result = -EEXIST;
799 break;
800 }
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800801 if (!result) {
Zhang Ruicddf31b2012-06-27 10:09:36 +0800802 list_add_tail(&dev->tz_node, &tz->thermal_instances);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800803 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
Chen Yu4511f712015-10-30 16:32:10 +0800804 atomic_set(&tz->need_update, 1);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800805 }
Zhang Ruif4a821c2012-07-24 16:56:21 +0800806 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800807 mutex_unlock(&tz->lock);
808
809 if (!result)
810 return 0;
811
Javi Merinodb916512015-02-18 16:04:24 +0000812 device_remove_file(&tz->device, &dev->weight_attr);
813remove_trip_file:
Zhang Rui203d3d42008-01-17 15:51:08 +0800814 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b82012-03-21 12:55:02 -0700815remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +0800816 sysfs_remove_link(&tz->device.kobj, dev->name);
Joe Perchescaca8b82012-03-21 12:55:02 -0700817release_idr:
Zhang Rui203d3d42008-01-17 15:51:08 +0800818 release_idr(&tz->idr, &tz->lock, dev->id);
Joe Perchescaca8b82012-03-21 12:55:02 -0700819free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +0800820 kfree(dev);
821 return result;
822}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000823EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +0800824
825/**
Eduardo Valentin9892e5d2013-04-23 21:48:17 +0000826 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
827 * thermal zone.
828 * @tz: pointer to a struct thermal_zone_device.
Zhang Rui203d3d42008-01-17 15:51:08 +0800829 * @trip: indicates which trip point the cooling devices is
830 * associated with in this thermal zone.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +0000831 * @cdev: pointer to a struct thermal_cooling_device.
Len Brown543a9562008-02-07 16:55:08 -0500832 *
Eduardo Valentin9892e5d2013-04-23 21:48:17 +0000833 * This interface function unbind a thermal cooling device from the certain
834 * trip point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -0500835 * This function is usually called in the thermal zone device .unbind callback.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +0000836 *
837 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +0800838 */
839int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
840 int trip,
841 struct thermal_cooling_device *cdev)
842{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800843 struct thermal_instance *pos, *next;
Zhang Rui203d3d42008-01-17 15:51:08 +0800844
845 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800846 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +0800847 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
Len Brown543a9562008-02-07 16:55:08 -0500848 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Ruicddf31b2012-06-27 10:09:36 +0800849 list_del(&pos->tz_node);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800850 list_del(&pos->cdev_node);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800851 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800852 mutex_unlock(&tz->lock);
853 goto unbind;
854 }
855 }
Zhang Ruif4a821c2012-07-24 16:56:21 +0800856 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800857 mutex_unlock(&tz->lock);
858
859 return -ENODEV;
860
Joe Perchescaca8b82012-03-21 12:55:02 -0700861unbind:
Viresh Kumar528464e2015-07-23 14:32:32 +0530862 device_remove_file(&tz->device, &pos->weight_attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800863 device_remove_file(&tz->device, &pos->attr);
864 sysfs_remove_link(&tz->device.kobj, pos->name);
865 release_idr(&tz->idr, &tz->lock, pos->id);
866 kfree(pos);
867 return 0;
868}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000869EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +0800870
871static void thermal_release(struct device *dev)
872{
873 struct thermal_zone_device *tz;
874 struct thermal_cooling_device *cdev;
875
Joe Perchescaca8b82012-03-21 12:55:02 -0700876 if (!strncmp(dev_name(dev), "thermal_zone",
877 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +0800878 tz = to_thermal_zone(dev);
879 kfree(tz);
durgadoss.r@intel.com732e4c82013-10-02 00:08:00 +0530880 } else if(!strncmp(dev_name(dev), "cooling_device",
881 sizeof("cooling_device") - 1)){
Zhang Rui203d3d42008-01-17 15:51:08 +0800882 cdev = to_cooling_device(dev);
883 kfree(cdev);
884 }
885}
886
887static struct class thermal_class = {
888 .name = "thermal",
889 .dev_release = thermal_release,
890};
891
892/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400893 * __thermal_cooling_device_register() - register a new thermal cooling device
894 * @np: a pointer to a device tree node.
Zhang Rui203d3d42008-01-17 15:51:08 +0800895 * @type: the thermal cooling device type.
896 * @devdata: device private data.
897 * @ops: standard thermal cooling devices callbacks.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +0000898 *
899 * This interface function adds a new thermal cooling device (fan/processor/...)
900 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
901 * to all the thermal zone devices registered at the same time.
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400902 * It also gives the opportunity to link the cooling device to a device tree
903 * node, so that it can be bound to a thermal zone created out of device tree.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +0000904 *
905 * Return: a pointer to the created struct thermal_cooling_device or an
906 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +0800907 */
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400908static struct thermal_cooling_device *
909__thermal_cooling_device_register(struct device_node *np,
910 char *type, void *devdata,
911 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +0800912{
913 struct thermal_cooling_device *cdev;
Chen Yu4511f712015-10-30 16:32:10 +0800914 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +0800915 int result;
916
Guenter Roeck204dd1d2012-08-07 22:36:45 -0700917 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500918 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800919
920 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -0500921 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500922 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800923
924 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
925 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500926 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +0800927
928 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
929 if (result) {
930 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500931 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800932 }
933
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +0000934 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
Zhang Ruif4a821c2012-07-24 16:56:21 +0800935 mutex_init(&cdev->lock);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800936 INIT_LIST_HEAD(&cdev->thermal_instances);
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400937 cdev->np = np;
Zhang Rui203d3d42008-01-17 15:51:08 +0800938 cdev->ops = ops;
Ni Wade5ca0cce2014-02-17 11:02:55 +0800939 cdev->updated = false;
Zhang Rui203d3d42008-01-17 15:51:08 +0800940 cdev->device.class = &thermal_class;
Eduardo Valentin45cf2ec2016-11-07 21:09:02 -0800941 thermal_cooling_device_setup_sysfs(cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +0800942 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -0800943 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +0800944 result = device_register(&cdev->device);
945 if (result) {
946 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
947 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500948 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800949 }
950
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530951 /* Add 'this' new cdev to the global cdev list */
Zhang Rui203d3d42008-01-17 15:51:08 +0800952 mutex_lock(&thermal_list_lock);
953 list_add(&cdev->node, &thermal_cdev_list);
Zhang Rui203d3d42008-01-17 15:51:08 +0800954 mutex_unlock(&thermal_list_lock);
955
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530956 /* Update binding information for 'this' new cdev */
957 bind_cdev(cdev);
958
Chen Yu4511f712015-10-30 16:32:10 +0800959 mutex_lock(&thermal_list_lock);
960 list_for_each_entry(pos, &thermal_tz_list, node)
961 if (atomic_cmpxchg(&pos->need_update, 1, 0))
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700962 thermal_zone_device_update(pos,
963 THERMAL_EVENT_UNSPECIFIED);
Chen Yu4511f712015-10-30 16:32:10 +0800964 mutex_unlock(&thermal_list_lock);
965
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530966 return cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +0800967}
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400968
969/**
970 * thermal_cooling_device_register() - register a new thermal cooling device
971 * @type: the thermal cooling device type.
972 * @devdata: device private data.
973 * @ops: standard thermal cooling devices callbacks.
974 *
975 * This interface function adds a new thermal cooling device (fan/processor/...)
976 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
977 * to all the thermal zone devices registered at the same time.
978 *
979 * Return: a pointer to the created struct thermal_cooling_device or an
980 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
981 */
982struct thermal_cooling_device *
983thermal_cooling_device_register(char *type, void *devdata,
984 const struct thermal_cooling_device_ops *ops)
985{
986 return __thermal_cooling_device_register(NULL, type, devdata, ops);
987}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000988EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +0800989
990/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400991 * thermal_of_cooling_device_register() - register an OF thermal cooling device
992 * @np: a pointer to a device tree node.
993 * @type: the thermal cooling device type.
994 * @devdata: device private data.
995 * @ops: standard thermal cooling devices callbacks.
996 *
997 * This function will register a cooling device with device tree node reference.
998 * This interface function adds a new thermal cooling device (fan/processor/...)
999 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1000 * to all the thermal zone devices registered at the same time.
1001 *
1002 * Return: a pointer to the created struct thermal_cooling_device or an
1003 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1004 */
1005struct thermal_cooling_device *
1006thermal_of_cooling_device_register(struct device_node *np,
1007 char *type, void *devdata,
1008 const struct thermal_cooling_device_ops *ops)
1009{
1010 return __thermal_cooling_device_register(np, type, devdata, ops);
1011}
1012EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1013
Eduardo Valentinf11997f2016-11-07 21:09:08 -08001014static void __unbind(struct thermal_zone_device *tz, int mask,
1015 struct thermal_cooling_device *cdev)
1016{
1017 int i;
1018
1019 for (i = 0; i < tz->trips; i++)
1020 if (mask & (1 << i))
1021 thermal_zone_unbind_cooling_device(tz, i, cdev);
1022}
1023
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001024/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001025 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001026 * @cdev: the thermal cooling device to remove.
1027 *
1028 * thermal_cooling_device_unregister() must be called when the device is no
1029 * longer needed.
1030 */
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301031void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
Zhang Rui203d3d42008-01-17 15:51:08 +08001032{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301033 int i;
1034 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001035 struct thermal_zone_device *tz;
1036 struct thermal_cooling_device *pos = NULL;
1037
1038 if (!cdev)
1039 return;
1040
1041 mutex_lock(&thermal_list_lock);
1042 list_for_each_entry(pos, &thermal_cdev_list, node)
1043 if (pos == cdev)
1044 break;
1045 if (pos != cdev) {
1046 /* thermal cooling device not found */
1047 mutex_unlock(&thermal_list_lock);
1048 return;
1049 }
1050 list_del(&cdev->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301051
1052 /* Unbind all thermal zones associated with 'this' cdev */
Zhang Rui203d3d42008-01-17 15:51:08 +08001053 list_for_each_entry(tz, &thermal_tz_list, node) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301054 if (tz->ops->unbind) {
1055 tz->ops->unbind(tz, cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +08001056 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301057 }
1058
1059 if (!tz->tzp || !tz->tzp->tbp)
1060 continue;
1061
1062 tzp = tz->tzp;
1063 for (i = 0; i < tzp->num_tbps; i++) {
1064 if (tzp->tbp[i].cdev == cdev) {
1065 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1066 tzp->tbp[i].cdev = NULL;
1067 }
1068 }
Zhang Rui203d3d42008-01-17 15:51:08 +08001069 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301070
Zhang Rui203d3d42008-01-17 15:51:08 +08001071 mutex_unlock(&thermal_list_lock);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301072
Zhang Rui203d3d42008-01-17 15:51:08 +08001073 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1074 device_unregister(&cdev->device);
1075 return;
1076}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001077EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001078
Matthew Garrettb1569e92008-12-03 17:55:32 +00001079/**
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001080 * thermal_notify_framework - Sensor drivers use this API to notify framework
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301081 * @tz: thermal zone device
1082 * @trip: indicates which trip point has been crossed
1083 *
1084 * This function handles the trip events from sensor drivers. It starts
1085 * throttling the cooling devices according to the policy configured.
1086 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1087 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1088 * The throttling policy is based on the configured platform data; if no
1089 * platform data is provided, this uses the step_wise throttling policy.
1090 */
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001091void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301092{
1093 handle_thermal_trip(tz, trip);
1094}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001095EXPORT_SYMBOL_GPL(thermal_notify_framework);
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301096
1097/**
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001098 * thermal_zone_device_register() - register a new thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001099 * @type: the thermal zone device type
1100 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001101 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08001102 * @devdata: private device data
1103 * @ops: standard thermal zone device callbacks
Durgadoss R50125a92012-09-18 11:04:56 +05301104 * @tzp: thermal zone platform parameters
Matthew Garrettb1569e92008-12-03 17:55:32 +00001105 * @passive_delay: number of milliseconds to wait between polls when
1106 * performing passive cooling
1107 * @polling_delay: number of milliseconds to wait between polls when checking
1108 * whether trip points have been crossed (0 for interrupt
1109 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001110 *
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001111 * This interface function adds a new thermal zone device (sensor) to
1112 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1113 * thermal cooling devices registered at the same time.
Zhang Rui203d3d42008-01-17 15:51:08 +08001114 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001115 * longer needed. The passive cooling depends on the .get_trend() return value.
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001116 *
1117 * Return: a pointer to the created struct thermal_zone_device or an
1118 * in case of error, an ERR_PTR. Caller must check return value with
1119 * IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001120 */
Anton Vorontsov4b1bf582012-07-31 04:39:30 -07001121struct thermal_zone_device *thermal_zone_device_register(const char *type,
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001122 int trips, int mask, void *devdata,
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001123 struct thermal_zone_device_ops *ops,
Javi Merino6b775e82015-03-02 17:17:19 +00001124 struct thermal_zone_params *tzp,
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001125 int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001126{
1127 struct thermal_zone_device *tz;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001128 enum thermal_trip_type trip_type;
Zhang Rui81ad4272016-03-18 10:03:24 +08001129 int trip_temp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001130 int result;
1131 int count;
Javi Merinoe33df1d2015-02-26 19:00:27 +00001132 struct thermal_governor *governor;
Zhang Rui203d3d42008-01-17 15:51:08 +08001133
Eduardo Valentin54fa38c2016-11-07 21:08:39 -08001134 if (!type || strlen(type) == 0)
1135 return ERR_PTR(-EINVAL);
1136
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001137 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001138 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001139
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001140 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001141 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001142
Eduardo Valentin81bd4e12013-09-12 19:15:44 -04001143 if (!ops)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001144 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001145
Jonghwa Lee83720d02013-05-18 09:50:26 +00001146 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
Eduardo Valentin6b2aa512013-01-02 15:29:42 +00001147 return ERR_PTR(-EINVAL);
1148
Zhang Rui203d3d42008-01-17 15:51:08 +08001149 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1150 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001151 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001152
Zhang Rui2d374132012-06-27 10:09:00 +08001153 INIT_LIST_HEAD(&tz->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001154 idr_init(&tz->idr);
1155 mutex_init(&tz->lock);
1156 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1157 if (result) {
1158 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001159 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001160 }
1161
Eduardo Valentin54fa38c2016-11-07 21:08:39 -08001162 strlcpy(tz->type, type, sizeof(tz->type));
Zhang Rui203d3d42008-01-17 15:51:08 +08001163 tz->ops = ops;
Durgadoss R50125a92012-09-18 11:04:56 +05301164 tz->tzp = tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001165 tz->device.class = &thermal_class;
1166 tz->devdata = devdata;
1167 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001168 tz->passive_delay = passive_delay;
1169 tz->polling_delay = polling_delay;
Eduardo Valentin1c600862016-11-07 21:08:42 -08001170
Eduardo Valentin4d0fe742016-11-07 21:08:52 -08001171 /* sys I/F */
Eduardo Valentin1c600862016-11-07 21:08:42 -08001172 /* Add nodes that are always present via .groups */
Eduardo Valentin4d0fe742016-11-07 21:08:52 -08001173 result = thermal_zone_create_device_groups(tz, mask);
1174 if (result)
1175 goto unregister;
1176
Chen Yu4511f712015-10-30 16:32:10 +08001177 /* A new thermal zone needs to be updated anyway. */
1178 atomic_set(&tz->need_update, 1);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001179
Kay Sievers354655e2009-01-06 10:44:37 -08001180 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001181 result = device_register(&tz->device);
1182 if (result) {
1183 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1184 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001185 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001186 }
1187
Zhang Rui203d3d42008-01-17 15:51:08 +08001188 for (count = 0; count < trips; count++) {
Zhang Rui81ad4272016-03-18 10:03:24 +08001189 if (tz->ops->get_trip_type(tz, count, &trip_type))
1190 set_bit(count, &tz->trips_disabled);
Zhang Rui81ad4272016-03-18 10:03:24 +08001191 if (tz->ops->get_trip_temp(tz, count, &trip_temp))
1192 set_bit(count, &tz->trips_disabled);
1193 /* Check for bogus trip points */
1194 if (trip_temp == 0)
1195 set_bit(count, &tz->trips_disabled);
Zhang Rui203d3d42008-01-17 15:51:08 +08001196 }
1197
Durgadoss Ra4a15482012-09-18 11:04:57 +05301198 /* Update 'this' zone's governor information */
1199 mutex_lock(&thermal_governor_lock);
1200
1201 if (tz->tzp)
Javi Merinoe33df1d2015-02-26 19:00:27 +00001202 governor = __find_governor(tz->tzp->governor_name);
Durgadoss Ra4a15482012-09-18 11:04:57 +05301203 else
Javi Merinoe33df1d2015-02-26 19:00:27 +00001204 governor = def_governor;
1205
1206 result = thermal_set_governor(tz, governor);
1207 if (result) {
1208 mutex_unlock(&thermal_governor_lock);
1209 goto unregister;
1210 }
Durgadoss Ra4a15482012-09-18 11:04:57 +05301211
1212 mutex_unlock(&thermal_governor_lock);
1213
Eduardo Valentinccba4ff2013-08-15 11:34:17 -04001214 if (!tz->tzp || !tz->tzp->no_hwmon) {
1215 result = thermal_add_hwmon_sysfs(tz);
1216 if (result)
1217 goto unregister;
1218 }
Zhang Ruie68b16a2008-04-21 16:07:52 +08001219
Zhang Rui203d3d42008-01-17 15:51:08 +08001220 mutex_lock(&thermal_list_lock);
1221 list_add_tail(&tz->node, &thermal_tz_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001222 mutex_unlock(&thermal_list_lock);
1223
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301224 /* Bind cooling devices for this zone */
1225 bind_tz(tz);
1226
Matthew Garrettb1569e92008-12-03 17:55:32 +00001227 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1228
Zhang Ruibb431ba2015-10-30 16:31:47 +08001229 thermal_zone_device_reset(tz);
Chen Yu4511f712015-10-30 16:32:10 +08001230 /* Update the new thermal zone and mark it as already updated. */
1231 if (atomic_cmpxchg(&tz->need_update, 1, 0))
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -07001232 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001233
Yao Dongdong14015862014-10-28 07:40:25 +00001234 return tz;
Zhang Rui203d3d42008-01-17 15:51:08 +08001235
Joe Perchescaca8b82012-03-21 12:55:02 -07001236unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001237 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1238 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001239 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001240}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001241EXPORT_SYMBOL_GPL(thermal_zone_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001242
1243/**
1244 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001245 * @tz: the thermal zone device to remove
1246 */
1247void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1248{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301249 int i;
1250 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001251 struct thermal_cooling_device *cdev;
1252 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001253
1254 if (!tz)
1255 return;
1256
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301257 tzp = tz->tzp;
1258
Zhang Rui203d3d42008-01-17 15:51:08 +08001259 mutex_lock(&thermal_list_lock);
1260 list_for_each_entry(pos, &thermal_tz_list, node)
1261 if (pos == tz)
1262 break;
1263 if (pos != tz) {
1264 /* thermal zone device not found */
1265 mutex_unlock(&thermal_list_lock);
1266 return;
1267 }
1268 list_del(&tz->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301269
1270 /* Unbind all cdevs associated with 'this' thermal zone */
1271 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1272 if (tz->ops->unbind) {
1273 tz->ops->unbind(tz, cdev);
1274 continue;
1275 }
1276
1277 if (!tzp || !tzp->tbp)
1278 break;
1279
1280 for (i = 0; i < tzp->num_tbps; i++) {
1281 if (tzp->tbp[i].cdev == cdev) {
1282 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1283 tzp->tbp[i].cdev = NULL;
1284 }
1285 }
1286 }
1287
Zhang Rui203d3d42008-01-17 15:51:08 +08001288 mutex_unlock(&thermal_list_lock);
1289
Matthew Garrettb1569e92008-12-03 17:55:32 +00001290 thermal_zone_device_set_polling(tz, 0);
1291
Eduardo Valentin4d0fe742016-11-07 21:08:52 -08001292 kfree(tz->trip_type_attrs);
1293 kfree(tz->trip_temp_attrs);
1294 kfree(tz->trip_hyst_attrs);
1295 kfree(tz->trips_attribute_group.attrs);
Javi Merinoe33df1d2015-02-26 19:00:27 +00001296 thermal_set_governor(tz, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001297
Zhang Ruie68b16a2008-04-21 16:07:52 +08001298 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001299 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1300 idr_destroy(&tz->idr);
1301 mutex_destroy(&tz->lock);
1302 device_unregister(&tz->device);
Eduardo Valentine161aef2016-11-07 21:08:51 -08001303 kfree(tz->device.groups);
Zhang Rui203d3d42008-01-17 15:51:08 +08001304}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001305EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001306
Eduardo Valentin63c4d912013-04-05 12:32:28 +00001307/**
1308 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1309 * @name: thermal zone name to fetch the temperature
1310 *
1311 * When only one zone is found with the passed name, returns a reference to it.
1312 *
1313 * Return: On success returns a reference to an unique thermal zone with
1314 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1315 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1316 */
1317struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1318{
1319 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1320 unsigned int found = 0;
1321
1322 if (!name)
1323 goto exit;
1324
1325 mutex_lock(&thermal_list_lock);
1326 list_for_each_entry(pos, &thermal_tz_list, node)
Rasmus Villemoes484ac2f2014-10-13 15:55:01 -07001327 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
Eduardo Valentin63c4d912013-04-05 12:32:28 +00001328 found++;
1329 ref = pos;
1330 }
1331 mutex_unlock(&thermal_list_lock);
1332
1333 /* nothing has been found, thus an error code for it */
1334 if (found == 0)
1335 ref = ERR_PTR(-ENODEV);
1336 else if (found > 1)
1337 /* Success only when an unique zone is found */
1338 ref = ERR_PTR(-EEXIST);
1339
1340exit:
1341 return ref;
1342}
1343EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1344
Rajendra Nayak4a7069a2016-05-05 14:21:42 +05301345/**
1346 * thermal_zone_get_slope - return the slope attribute of the thermal zone
1347 * @tz: thermal zone device with the slope attribute
1348 *
1349 * Return: If the thermal zone device has a slope attribute, return it, else
1350 * return 1.
1351 */
1352int thermal_zone_get_slope(struct thermal_zone_device *tz)
1353{
1354 if (tz && tz->tzp)
1355 return tz->tzp->slope;
1356 return 1;
1357}
1358EXPORT_SYMBOL_GPL(thermal_zone_get_slope);
1359
1360/**
1361 * thermal_zone_get_offset - return the offset attribute of the thermal zone
1362 * @tz: thermal zone device with the offset attribute
1363 *
1364 * Return: If the thermal zone device has a offset attribute, return it, else
1365 * return 0.
1366 */
1367int thermal_zone_get_offset(struct thermal_zone_device *tz)
1368{
1369 if (tz && tz->tzp)
1370 return tz->tzp->offset;
1371 return 0;
1372}
1373EXPORT_SYMBOL_GPL(thermal_zone_get_offset);
1374
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001375#ifdef CONFIG_NET
Johannes Berg2a94fe42013-11-19 15:19:39 +01001376static const struct genl_multicast_group thermal_event_mcgrps[] = {
1377 { .name = THERMAL_GENL_MCAST_GROUP_NAME, },
1378};
1379
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001380static struct genl_family thermal_event_genl_family = {
1381 .id = GENL_ID_GENERATE,
1382 .name = THERMAL_GENL_FAMILY_NAME,
1383 .version = THERMAL_GENL_VERSION,
1384 .maxattr = THERMAL_GENL_ATTR_MAX,
Johannes Berg2a94fe42013-11-19 15:19:39 +01001385 .mcgrps = thermal_event_mcgrps,
1386 .n_mcgrps = ARRAY_SIZE(thermal_event_mcgrps),
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001387};
1388
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001389int thermal_generate_netlink_event(struct thermal_zone_device *tz,
1390 enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05301391{
1392 struct sk_buff *skb;
1393 struct nlattr *attr;
1394 struct thermal_genl_event *thermal_event;
1395 void *msg_header;
1396 int size;
1397 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07001398 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301399
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001400 if (!tz)
1401 return -EINVAL;
1402
R.Durgadoss4cb18722010-10-27 03:33:29 +05301403 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07001404 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1405 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301406
1407 skb = genlmsg_new(size, GFP_ATOMIC);
1408 if (!skb)
1409 return -ENOMEM;
1410
1411 /* add the genetlink message header */
1412 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1413 &thermal_event_genl_family, 0,
1414 THERMAL_GENL_CMD_EVENT);
1415 if (!msg_header) {
1416 nlmsg_free(skb);
1417 return -ENOMEM;
1418 }
1419
1420 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07001421 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1422 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05301423
1424 if (!attr) {
1425 nlmsg_free(skb);
1426 return -EINVAL;
1427 }
1428
1429 thermal_event = nla_data(attr);
1430 if (!thermal_event) {
1431 nlmsg_free(skb);
1432 return -EINVAL;
1433 }
1434
1435 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1436
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001437 thermal_event->orig = tz->id;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301438 thermal_event->event = event;
1439
1440 /* send multicast genetlink message */
Johannes Berg053c0952015-01-16 22:09:00 +01001441 genlmsg_end(skb, msg_header);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301442
Johannes Berg68eb5502013-11-19 15:19:38 +01001443 result = genlmsg_multicast(&thermal_event_genl_family, skb, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +01001444 0, GFP_ATOMIC);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301445 if (result)
Eduardo Valentin923e0b12013-01-02 15:29:41 +00001446 dev_err(&tz->device, "Failed to send netlink event:%d", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301447
1448 return result;
1449}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001450EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301451
1452static int genetlink_init(void)
1453{
Johannes Berg2a94fe42013-11-19 15:19:39 +01001454 return genl_register_family(&thermal_event_genl_family);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301455}
1456
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001457static void genetlink_exit(void)
1458{
1459 genl_unregister_family(&thermal_event_genl_family);
1460}
1461#else /* !CONFIG_NET */
1462static inline int genetlink_init(void) { return 0; }
1463static inline void genetlink_exit(void) {}
1464#endif /* !CONFIG_NET */
1465
Zhang Ruiff140fe2015-10-30 16:31:58 +08001466static int thermal_pm_notify(struct notifier_block *nb,
1467 unsigned long mode, void *_unused)
1468{
1469 struct thermal_zone_device *tz;
1470
1471 switch (mode) {
1472 case PM_HIBERNATION_PREPARE:
1473 case PM_RESTORE_PREPARE:
1474 case PM_SUSPEND_PREPARE:
1475 atomic_set(&in_suspend, 1);
1476 break;
1477 case PM_POST_HIBERNATION:
1478 case PM_POST_RESTORE:
1479 case PM_POST_SUSPEND:
1480 atomic_set(&in_suspend, 0);
1481 list_for_each_entry(tz, &thermal_tz_list, node) {
1482 thermal_zone_device_reset(tz);
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -07001483 thermal_zone_device_update(tz,
1484 THERMAL_EVENT_UNSPECIFIED);
Zhang Ruiff140fe2015-10-30 16:31:58 +08001485 }
1486 break;
1487 default:
1488 break;
1489 }
1490 return 0;
1491}
1492
1493static struct notifier_block thermal_pm_nb = {
1494 .notifier_call = thermal_pm_notify,
1495};
1496
Zhang Rui203d3d42008-01-17 15:51:08 +08001497static int __init thermal_init(void)
1498{
Zhang Rui80a26a52013-03-26 16:38:29 +08001499 int result;
1500
1501 result = thermal_register_governors();
1502 if (result)
1503 goto error;
Zhang Rui203d3d42008-01-17 15:51:08 +08001504
1505 result = class_register(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08001506 if (result)
1507 goto unregister_governors;
1508
R.Durgadoss4cb18722010-10-27 03:33:29 +05301509 result = genetlink_init();
Zhang Rui80a26a52013-03-26 16:38:29 +08001510 if (result)
1511 goto unregister_class;
1512
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001513 result = of_parse_thermal_zones();
1514 if (result)
1515 goto exit_netlink;
1516
Zhang Ruiff140fe2015-10-30 16:31:58 +08001517 result = register_pm_notifier(&thermal_pm_nb);
1518 if (result)
1519 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
1520 result);
1521
Zhang Rui80a26a52013-03-26 16:38:29 +08001522 return 0;
1523
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001524exit_netlink:
1525 genetlink_exit();
Zhang Rui80a26a52013-03-26 16:38:29 +08001526unregister_class:
1527 class_unregister(&thermal_class);
Luis Henriques9d367e52014-12-03 21:20:21 +00001528unregister_governors:
1529 thermal_unregister_governors();
Zhang Rui80a26a52013-03-26 16:38:29 +08001530error:
1531 idr_destroy(&thermal_tz_idr);
1532 idr_destroy(&thermal_cdev_idr);
1533 mutex_destroy(&thermal_idr_lock);
1534 mutex_destroy(&thermal_list_lock);
1535 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001536 return result;
1537}
1538
1539static void __exit thermal_exit(void)
1540{
Zhang Ruiff140fe2015-10-30 16:31:58 +08001541 unregister_pm_notifier(&thermal_pm_nb);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001542 of_thermal_destroy_zones();
Zhang Rui80a26a52013-03-26 16:38:29 +08001543 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08001544 class_unregister(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08001545 thermal_unregister_governors();
Zhang Rui203d3d42008-01-17 15:51:08 +08001546 idr_destroy(&thermal_tz_idr);
1547 idr_destroy(&thermal_cdev_idr);
1548 mutex_destroy(&thermal_idr_lock);
1549 mutex_destroy(&thermal_list_lock);
Zhang Rui80a26a52013-03-26 16:38:29 +08001550 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001551}
1552
R.Durgadoss4cb18722010-10-27 03:33:29 +05301553fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08001554module_exit(thermal_exit);