blob: 01b1a4e064c0e4c35114c208b505aba1978e4951 [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
Eduardo Valentin8772e182016-11-07 21:09:15 -0800289/*
290 * Zone update section: main control loop applied to each zone while monitoring
291 *
292 * in polling mode. The monitoring is done using a workqueue.
293 * Same update may be done on a zone by calling thermal_zone_device_update().
294 *
295 * An update means:
296 * - Non-critical trips will invoke the governor responsible for that zone;
297 * - Hot trips will produce a notification to userspace;
298 * - Critical trip point will cause a system shutdown.
299 */
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530300static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
301 int delay)
302{
303 if (delay > 1000)
304 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
305 round_jiffies(msecs_to_jiffies(delay)));
306 else if (delay)
307 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
308 msecs_to_jiffies(delay));
309 else
310 cancel_delayed_work(&tz->poll_queue);
311}
312
313static void monitor_thermal_zone(struct thermal_zone_device *tz)
314{
315 mutex_lock(&tz->lock);
316
317 if (tz->passive)
318 thermal_zone_device_set_polling(tz, tz->passive_delay);
319 else if (tz->polling_delay)
320 thermal_zone_device_set_polling(tz, tz->polling_delay);
321 else
322 thermal_zone_device_set_polling(tz, 0);
323
324 mutex_unlock(&tz->lock);
325}
326
327static void handle_non_critical_trips(struct thermal_zone_device *tz,
328 int trip, enum thermal_trip_type trip_type)
329{
Zhang Ruif2234bc2014-01-24 10:23:19 +0800330 tz->governor ? tz->governor->throttle(tz, trip) :
331 def_governor->throttle(tz, trip);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530332}
333
334static void handle_critical_trips(struct thermal_zone_device *tz,
335 int trip, enum thermal_trip_type trip_type)
336{
Sascha Hauer17e83512015-07-24 08:12:54 +0200337 int trip_temp;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530338
339 tz->ops->get_trip_temp(tz, trip, &trip_temp);
340
341 /* If we have not crossed the trip_temp, we do not care. */
Srinivas Pandruvada84ffe3e2014-11-12 15:43:29 -0800342 if (trip_temp <= 0 || tz->temperature < trip_temp)
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530343 return;
344
Punit Agrawal208cd822014-07-29 11:50:50 +0100345 trace_thermal_zone_trip(tz, trip, trip_type);
346
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530347 if (tz->ops->notify)
348 tz->ops->notify(tz, trip, trip_type);
349
350 if (trip_type == THERMAL_TRIP_CRITICAL) {
Eduardo Valentin923e0b12013-01-02 15:29:41 +0000351 dev_emerg(&tz->device,
352 "critical temperature reached(%d C),shutting down\n",
353 tz->temperature / 1000);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530354 orderly_poweroff(true);
355 }
356}
357
358static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
359{
360 enum thermal_trip_type type;
361
Zhang Rui81ad4272016-03-18 10:03:24 +0800362 /* Ignore disabled trip points */
363 if (test_bit(trip, &tz->trips_disabled))
364 return;
365
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530366 tz->ops->get_trip_type(tz, trip, &type);
367
368 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
369 handle_critical_trips(tz, trip, type);
370 else
371 handle_non_critical_trips(tz, trip, type);
372 /*
373 * Alright, we handled this trip successfully.
374 * So, start monitoring again.
375 */
376 monitor_thermal_zone(tz);
377}
378
379static void update_temperature(struct thermal_zone_device *tz)
380{
Sascha Hauer17e83512015-07-24 08:12:54 +0200381 int temp, ret;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530382
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000383 ret = thermal_zone_get_temp(tz, &temp);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530384 if (ret) {
Hans de Goede7e497a72015-03-21 15:02:55 +0100385 if (ret != -EAGAIN)
386 dev_warn(&tz->device,
387 "failed to read out thermal zone (%d)\n",
388 ret);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000389 return;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530390 }
391
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000392 mutex_lock(&tz->lock);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530393 tz->last_temperature = tz->temperature;
394 tz->temperature = temp;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530395 mutex_unlock(&tz->lock);
Aaron Lu06475b52013-12-02 13:54:26 +0800396
Punit Agrawal100a8fd2014-07-29 11:50:48 +0100397 trace_thermal_temperature(tz);
Zhang Ruibb431ba2015-10-30 16:31:47 +0800398 if (tz->last_temperature == THERMAL_TEMP_INVALID)
399 dev_dbg(&tz->device, "last_temperature N/A, current_temperature=%d\n",
400 tz->temperature);
401 else
402 dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n",
403 tz->last_temperature, tz->temperature);
404}
405
406static void thermal_zone_device_reset(struct thermal_zone_device *tz)
407{
408 struct thermal_instance *pos;
409
410 tz->temperature = THERMAL_TEMP_INVALID;
411 tz->passive = 0;
412 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
413 pos->initialized = false;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530414}
415
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700416void thermal_zone_device_update(struct thermal_zone_device *tz,
417 enum thermal_notify_event event)
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530418{
419 int count;
420
Zhang Ruiff140fe2015-10-30 16:31:58 +0800421 if (atomic_read(&in_suspend))
422 return;
423
Eduardo Valentin81bd4e12013-09-12 19:15:44 -0400424 if (!tz->ops->get_temp)
425 return;
426
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530427 update_temperature(tz);
428
Sascha Hauer060c0342016-06-22 16:42:01 +0800429 thermal_zone_set_trips(tz);
430
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700431 tz->notify_event = event;
432
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530433 for (count = 0; count < tz->trips; count++)
434 handle_thermal_trip(tz, count);
435}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000436EXPORT_SYMBOL_GPL(thermal_zone_device_update);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530437
Eduardo Valentin106339a2016-11-07 21:09:14 -0800438/**
439 * thermal_notify_framework - Sensor drivers use this API to notify framework
440 * @tz: thermal zone device
441 * @trip: indicates which trip point has been crossed
442 *
443 * This function handles the trip events from sensor drivers. It starts
444 * throttling the cooling devices according to the policy configured.
445 * For CRITICAL and HOT trip points, this notifies the respective drivers,
446 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
447 * The throttling policy is based on the configured platform data; if no
448 * platform data is provided, this uses the step_wise throttling policy.
449 */
450void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
451{
452 handle_thermal_trip(tz, trip);
453}
454EXPORT_SYMBOL_GPL(thermal_notify_framework);
455
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530456static void thermal_zone_device_check(struct work_struct *work)
457{
458 struct thermal_zone_device *tz = container_of(work, struct
459 thermal_zone_device,
460 poll_queue.work);
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700461 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530462}
463
Eduardo Valentin712afbd2016-11-07 21:09:16 -0800464/*
465 * Power actor section: interface to power actors to estimate power
466 *
467 * Set of functions used to interact to cooling devices that know
468 * how to estimate their devices power consumption.
469 */
470
Eduardo Valentin1a7e7cc2016-11-07 21:08:47 -0800471/**
472 * power_actor_get_max_power() - get the maximum power that a cdev can consume
473 * @cdev: pointer to &thermal_cooling_device
474 * @tz: a valid thermal zone device pointer
475 * @max_power: pointer in which to store the maximum power
476 *
477 * Calculate the maximum power consumption in milliwats that the
478 * cooling device can currently consume and store it in @max_power.
479 *
480 * Return: 0 on success, -EINVAL if @cdev doesn't support the
481 * power_actor API or -E* on other error.
482 */
483int power_actor_get_max_power(struct thermal_cooling_device *cdev,
484 struct thermal_zone_device *tz, u32 *max_power)
485{
486 if (!cdev_is_power_actor(cdev))
487 return -EINVAL;
488
489 return cdev->ops->state2power(cdev, tz, 0, max_power);
490}
491
492/**
493 * power_actor_get_min_power() - get the mainimum power that a cdev can consume
494 * @cdev: pointer to &thermal_cooling_device
495 * @tz: a valid thermal zone device pointer
496 * @min_power: pointer in which to store the minimum power
497 *
498 * Calculate the minimum power consumption in milliwatts that the
499 * cooling device can currently consume and store it in @min_power.
500 *
501 * Return: 0 on success, -EINVAL if @cdev doesn't support the
502 * power_actor API or -E* on other error.
503 */
504int power_actor_get_min_power(struct thermal_cooling_device *cdev,
505 struct thermal_zone_device *tz, u32 *min_power)
506{
507 unsigned long max_state;
508 int ret;
509
510 if (!cdev_is_power_actor(cdev))
511 return -EINVAL;
512
513 ret = cdev->ops->get_max_state(cdev, &max_state);
514 if (ret)
515 return ret;
516
517 return cdev->ops->state2power(cdev, tz, max_state, min_power);
518}
519
520/**
521 * power_actor_set_power() - limit the maximum power a cooling device consumes
522 * @cdev: pointer to &thermal_cooling_device
523 * @instance: thermal instance to update
524 * @power: the power in milliwatts
525 *
526 * Set the cooling device to consume at most @power milliwatts. The limit is
527 * expected to be a cap at the maximum power consumption.
528 *
529 * Return: 0 on success, -EINVAL if the cooling device does not
530 * implement the power actor API or -E* for other failures.
531 */
532int power_actor_set_power(struct thermal_cooling_device *cdev,
533 struct thermal_instance *instance, u32 power)
534{
535 unsigned long state;
536 int ret;
537
538 if (!cdev_is_power_actor(cdev))
539 return -EINVAL;
540
541 ret = cdev->ops->power2state(cdev, instance->tz, power, &state);
542 if (ret)
543 return ret;
544
545 instance->target = state;
546 mutex_lock(&cdev->lock);
547 cdev->updated = false;
548 mutex_unlock(&cdev->lock);
549 thermal_cdev_update(cdev);
550
551 return 0;
552}
553
Eduardo Valentin3d0055d2016-11-07 21:08:54 -0800554void thermal_zone_device_rebind_exception(struct thermal_zone_device *tz,
555 const char *cdev_type, size_t size)
556{
557 struct thermal_cooling_device *cdev = NULL;
558
559 mutex_lock(&thermal_list_lock);
560 list_for_each_entry(cdev, &thermal_cdev_list, node) {
561 /* skip non matching cdevs */
562 if (strncmp(cdev_type, cdev->type, size))
563 continue;
564
565 /* re binding the exception matching the type pattern */
566 thermal_zone_bind_cooling_device(tz, THERMAL_TRIPS_NONE, cdev,
567 THERMAL_NO_LIMIT,
568 THERMAL_NO_LIMIT,
569 THERMAL_WEIGHT_DEFAULT);
570 }
571 mutex_unlock(&thermal_list_lock);
572}
573
574void thermal_zone_device_unbind_exception(struct thermal_zone_device *tz,
575 const char *cdev_type, size_t size)
576{
577 struct thermal_cooling_device *cdev = NULL;
578
579 mutex_lock(&thermal_list_lock);
580 list_for_each_entry(cdev, &thermal_cdev_list, node) {
581 /* skip non matching cdevs */
582 if (strncmp(cdev_type, cdev->type, size))
583 continue;
584 /* unbinding the exception matching the type pattern */
585 thermal_zone_unbind_cooling_device(tz, THERMAL_TRIPS_NONE,
586 cdev);
587 }
588 mutex_unlock(&thermal_list_lock);
589}
590
Eduardo Valentin81193e22016-11-07 21:09:17 -0800591/*
592 * Device management section: cooling devices, zones devices, and binding
593 *
594 * Set of functions provided by the thermal core for:
595 * - cooling devices lifecycle: registration, unregistration,
596 * binding, and unbinding.
597 * - thermal zone devices lifecycle: registration, unregistration,
598 * binding, and unbinding.
599 */
Eduardo Valentinc30176f2016-11-07 21:09:06 -0800600static int get_idr(struct idr *idr, struct mutex *lock, int *id)
601{
602 int ret;
603
604 if (lock)
605 mutex_lock(lock);
606 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
607 if (lock)
608 mutex_unlock(lock);
609 if (unlikely(ret < 0))
610 return ret;
611 *id = ret;
612 return 0;
613}
614
615static void release_idr(struct idr *idr, struct mutex *lock, int id)
616{
617 if (lock)
618 mutex_lock(lock);
619 idr_remove(idr, id);
620 if (lock)
621 mutex_unlock(lock);
622}
623
Zhang Rui203d3d42008-01-17 15:51:08 +0800624/**
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000625 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
626 * @tz: pointer to struct thermal_zone_device
Zhang Rui203d3d42008-01-17 15:51:08 +0800627 * @trip: indicates which trip point the cooling devices is
628 * associated with in this thermal zone.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000629 * @cdev: pointer to struct thermal_cooling_device
630 * @upper: the Maximum cooling state for this trip point.
631 * THERMAL_NO_LIMIT means no upper limit,
632 * and the cooling device can be in max_state.
633 * @lower: the Minimum cooling state can be used for this trip point.
634 * THERMAL_NO_LIMIT means no lower limit,
635 * and the cooling device can be in cooling state 0.
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000636 * @weight: The weight of the cooling device to be bound to the
637 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
638 * default value
Len Brown543a9562008-02-07 16:55:08 -0500639 *
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000640 * This interface function bind a thermal cooling device to the certain trip
641 * point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -0500642 * This function is usually called in the thermal zone device .bind callback.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000643 *
644 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +0800645 */
646int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
647 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +0800648 struct thermal_cooling_device *cdev,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000649 unsigned long upper, unsigned long lower,
650 unsigned int weight)
Zhang Rui203d3d42008-01-17 15:51:08 +0800651{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800652 struct thermal_instance *dev;
653 struct thermal_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -0500654 struct thermal_zone_device *pos1;
655 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +0800656 unsigned long max_state;
Lukasz Majewski9a3031d2014-11-18 11:16:30 +0100657 int result, ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800658
Len Brown543a9562008-02-07 16:55:08 -0500659 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +0800660 return -EINVAL;
661
Thomas Sujithc7516702008-02-15 00:58:50 -0500662 list_for_each_entry(pos1, &thermal_tz_list, node) {
663 if (pos1 == tz)
664 break;
665 }
666 list_for_each_entry(pos2, &thermal_cdev_list, node) {
667 if (pos2 == cdev)
668 break;
669 }
670
671 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +0800672 return -EINVAL;
673
Lukasz Majewski9a3031d2014-11-18 11:16:30 +0100674 ret = cdev->ops->get_max_state(cdev, &max_state);
675 if (ret)
676 return ret;
Zhang Rui9d998422012-06-26 16:35:57 +0800677
678 /* lower default 0, upper default max_state */
679 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
680 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
681
682 if (lower > upper || upper > max_state)
683 return -EINVAL;
684
Zhang Rui203d3d42008-01-17 15:51:08 +0800685 dev =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800686 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800687 if (!dev)
688 return -ENOMEM;
689 dev->tz = tz;
690 dev->cdev = cdev;
691 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +0800692 dev->upper = upper;
693 dev->lower = lower;
Zhang Ruice119f82012-06-27 14:13:04 +0800694 dev->target = THERMAL_NO_TARGET;
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000695 dev->weight = weight;
Zhang Rui74051ba2012-06-26 16:27:22 +0800696
Zhang Rui203d3d42008-01-17 15:51:08 +0800697 result = get_idr(&tz->idr, &tz->lock, &dev->id);
698 if (result)
699 goto free_mem;
700
701 sprintf(dev->name, "cdev%d", dev->id);
702 result =
703 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
704 if (result)
705 goto release_idr;
706
707 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -0700708 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800709 dev->attr.attr.name = dev->attr_name;
710 dev->attr.attr.mode = 0444;
711 dev->attr.show = thermal_cooling_device_trip_point_show;
712 result = device_create_file(&tz->device, &dev->attr);
713 if (result)
714 goto remove_symbol_link;
715
Javi Merinodb916512015-02-18 16:04:24 +0000716 sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
717 sysfs_attr_init(&dev->weight_attr.attr);
718 dev->weight_attr.attr.name = dev->weight_attr_name;
719 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
720 dev->weight_attr.show = thermal_cooling_device_weight_show;
721 dev->weight_attr.store = thermal_cooling_device_weight_store;
722 result = device_create_file(&tz->device, &dev->weight_attr);
723 if (result)
724 goto remove_trip_file;
725
Zhang Rui203d3d42008-01-17 15:51:08 +0800726 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800727 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +0800728 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
Zhang Rui203d3d42008-01-17 15:51:08 +0800729 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
730 result = -EEXIST;
731 break;
732 }
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800733 if (!result) {
Zhang Ruicddf31b2012-06-27 10:09:36 +0800734 list_add_tail(&dev->tz_node, &tz->thermal_instances);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800735 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
Chen Yu4511f712015-10-30 16:32:10 +0800736 atomic_set(&tz->need_update, 1);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800737 }
Zhang Ruif4a821c2012-07-24 16:56:21 +0800738 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800739 mutex_unlock(&tz->lock);
740
741 if (!result)
742 return 0;
743
Javi Merinodb916512015-02-18 16:04:24 +0000744 device_remove_file(&tz->device, &dev->weight_attr);
745remove_trip_file:
Zhang Rui203d3d42008-01-17 15:51:08 +0800746 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b802012-03-21 12:55:02 -0700747remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +0800748 sysfs_remove_link(&tz->device.kobj, dev->name);
Joe Perchescaca8b802012-03-21 12:55:02 -0700749release_idr:
Zhang Rui203d3d42008-01-17 15:51:08 +0800750 release_idr(&tz->idr, &tz->lock, dev->id);
Joe Perchescaca8b802012-03-21 12:55:02 -0700751free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +0800752 kfree(dev);
753 return result;
754}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000755EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +0800756
757/**
Eduardo Valentin9892e5d2013-04-23 21:48:17 +0000758 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
759 * thermal zone.
760 * @tz: pointer to a struct thermal_zone_device.
Zhang Rui203d3d42008-01-17 15:51:08 +0800761 * @trip: indicates which trip point the cooling devices is
762 * associated with in this thermal zone.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +0000763 * @cdev: pointer to a struct thermal_cooling_device.
Len Brown543a9562008-02-07 16:55:08 -0500764 *
Eduardo Valentin9892e5d2013-04-23 21:48:17 +0000765 * This interface function unbind a thermal cooling device from the certain
766 * trip point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -0500767 * This function is usually called in the thermal zone device .unbind callback.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +0000768 *
769 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +0800770 */
771int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
772 int trip,
773 struct thermal_cooling_device *cdev)
774{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800775 struct thermal_instance *pos, *next;
Zhang Rui203d3d42008-01-17 15:51:08 +0800776
777 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800778 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +0800779 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
Len Brown543a9562008-02-07 16:55:08 -0500780 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Ruicddf31b2012-06-27 10:09:36 +0800781 list_del(&pos->tz_node);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800782 list_del(&pos->cdev_node);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800783 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800784 mutex_unlock(&tz->lock);
785 goto unbind;
786 }
787 }
Zhang Ruif4a821c2012-07-24 16:56:21 +0800788 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800789 mutex_unlock(&tz->lock);
790
791 return -ENODEV;
792
Joe Perchescaca8b802012-03-21 12:55:02 -0700793unbind:
Viresh Kumar528464e2015-07-23 14:32:32 +0530794 device_remove_file(&tz->device, &pos->weight_attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800795 device_remove_file(&tz->device, &pos->attr);
796 sysfs_remove_link(&tz->device.kobj, pos->name);
797 release_idr(&tz->idr, &tz->lock, pos->id);
798 kfree(pos);
799 return 0;
800}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000801EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +0800802
803static void thermal_release(struct device *dev)
804{
805 struct thermal_zone_device *tz;
806 struct thermal_cooling_device *cdev;
807
Joe Perchescaca8b802012-03-21 12:55:02 -0700808 if (!strncmp(dev_name(dev), "thermal_zone",
809 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +0800810 tz = to_thermal_zone(dev);
811 kfree(tz);
durgadoss.r@intel.com732e4c82013-10-02 00:08:00 +0530812 } else if(!strncmp(dev_name(dev), "cooling_device",
813 sizeof("cooling_device") - 1)){
Zhang Rui203d3d42008-01-17 15:51:08 +0800814 cdev = to_cooling_device(dev);
815 kfree(cdev);
816 }
817}
818
819static struct class thermal_class = {
820 .name = "thermal",
821 .dev_release = thermal_release,
822};
823
Eduardo Valentin4b0d3c22016-11-07 21:09:13 -0800824static inline
825void print_bind_err_msg(struct thermal_zone_device *tz,
826 struct thermal_cooling_device *cdev, int ret)
Eduardo Valentinf502ab82016-11-07 21:09:12 -0800827{
828 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
829 tz->type, cdev->type, ret);
830}
831
832static void __bind(struct thermal_zone_device *tz, int mask,
833 struct thermal_cooling_device *cdev,
834 unsigned long *limits,
835 unsigned int weight)
836{
837 int i, ret;
838
839 for (i = 0; i < tz->trips; i++) {
840 if (mask & (1 << i)) {
841 unsigned long upper, lower;
842
843 upper = THERMAL_NO_LIMIT;
844 lower = THERMAL_NO_LIMIT;
845 if (limits) {
846 lower = limits[i * 2];
847 upper = limits[i * 2 + 1];
848 }
849 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
850 upper, lower,
851 weight);
852 if (ret)
853 print_bind_err_msg(tz, cdev, ret);
854 }
855 }
856}
857
Eduardo Valentin949aad82016-11-07 21:09:09 -0800858static void bind_cdev(struct thermal_cooling_device *cdev)
859{
860 int i, ret;
861 const struct thermal_zone_params *tzp;
862 struct thermal_zone_device *pos = NULL;
863
864 mutex_lock(&thermal_list_lock);
865
866 list_for_each_entry(pos, &thermal_tz_list, node) {
867 if (!pos->tzp && !pos->ops->bind)
868 continue;
869
870 if (pos->ops->bind) {
871 ret = pos->ops->bind(pos, cdev);
872 if (ret)
873 print_bind_err_msg(pos, cdev, ret);
874 continue;
875 }
876
877 tzp = pos->tzp;
878 if (!tzp || !tzp->tbp)
879 continue;
880
881 for (i = 0; i < tzp->num_tbps; i++) {
882 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
883 continue;
884 if (tzp->tbp[i].match(pos, cdev))
885 continue;
886 tzp->tbp[i].cdev = cdev;
887 __bind(pos, tzp->tbp[i].trip_mask, cdev,
888 tzp->tbp[i].binding_limits,
889 tzp->tbp[i].weight);
890 }
891 }
892
893 mutex_unlock(&thermal_list_lock);
894}
895
Zhang Rui203d3d42008-01-17 15:51:08 +0800896/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400897 * __thermal_cooling_device_register() - register a new thermal cooling device
898 * @np: a pointer to a device tree node.
Zhang Rui203d3d42008-01-17 15:51:08 +0800899 * @type: the thermal cooling device type.
900 * @devdata: device private data.
901 * @ops: standard thermal cooling devices callbacks.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +0000902 *
903 * This interface function adds a new thermal cooling device (fan/processor/...)
904 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
905 * to all the thermal zone devices registered at the same time.
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400906 * It also gives the opportunity to link the cooling device to a device tree
907 * node, so that it can be bound to a thermal zone created out of device tree.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +0000908 *
909 * Return: a pointer to the created struct thermal_cooling_device or an
910 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +0800911 */
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400912static struct thermal_cooling_device *
913__thermal_cooling_device_register(struct device_node *np,
914 char *type, void *devdata,
915 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +0800916{
917 struct thermal_cooling_device *cdev;
Chen Yu4511f712015-10-30 16:32:10 +0800918 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +0800919 int result;
920
Guenter Roeck204dd1d2012-08-07 22:36:45 -0700921 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500922 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800923
924 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -0500925 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500926 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800927
928 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
929 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500930 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +0800931
932 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
933 if (result) {
934 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500935 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800936 }
937
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +0000938 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
Zhang Ruif4a821c2012-07-24 16:56:21 +0800939 mutex_init(&cdev->lock);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800940 INIT_LIST_HEAD(&cdev->thermal_instances);
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400941 cdev->np = np;
Zhang Rui203d3d42008-01-17 15:51:08 +0800942 cdev->ops = ops;
Ni Wade5ca0cce2014-02-17 11:02:55 +0800943 cdev->updated = false;
Zhang Rui203d3d42008-01-17 15:51:08 +0800944 cdev->device.class = &thermal_class;
Eduardo Valentin45cf2ec2016-11-07 21:09:02 -0800945 thermal_cooling_device_setup_sysfs(cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +0800946 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -0800947 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +0800948 result = device_register(&cdev->device);
949 if (result) {
950 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
951 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500952 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800953 }
954
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530955 /* Add 'this' new cdev to the global cdev list */
Zhang Rui203d3d42008-01-17 15:51:08 +0800956 mutex_lock(&thermal_list_lock);
957 list_add(&cdev->node, &thermal_cdev_list);
Zhang Rui203d3d42008-01-17 15:51:08 +0800958 mutex_unlock(&thermal_list_lock);
959
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530960 /* Update binding information for 'this' new cdev */
961 bind_cdev(cdev);
962
Chen Yu4511f712015-10-30 16:32:10 +0800963 mutex_lock(&thermal_list_lock);
964 list_for_each_entry(pos, &thermal_tz_list, node)
965 if (atomic_cmpxchg(&pos->need_update, 1, 0))
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700966 thermal_zone_device_update(pos,
967 THERMAL_EVENT_UNSPECIFIED);
Chen Yu4511f712015-10-30 16:32:10 +0800968 mutex_unlock(&thermal_list_lock);
969
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530970 return cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +0800971}
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400972
973/**
974 * thermal_cooling_device_register() - register a new thermal cooling device
975 * @type: the thermal cooling device type.
976 * @devdata: device private data.
977 * @ops: standard thermal cooling devices callbacks.
978 *
979 * This interface function adds a new thermal cooling device (fan/processor/...)
980 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
981 * to all the thermal zone devices registered at the same time.
982 *
983 * Return: a pointer to the created struct thermal_cooling_device or an
984 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
985 */
986struct thermal_cooling_device *
987thermal_cooling_device_register(char *type, void *devdata,
988 const struct thermal_cooling_device_ops *ops)
989{
990 return __thermal_cooling_device_register(NULL, type, devdata, ops);
991}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000992EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +0800993
994/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400995 * thermal_of_cooling_device_register() - register an OF thermal cooling device
996 * @np: a pointer to a device tree node.
997 * @type: the thermal cooling device type.
998 * @devdata: device private data.
999 * @ops: standard thermal cooling devices callbacks.
1000 *
1001 * This function will register a cooling device with device tree node reference.
1002 * This interface function adds a new thermal cooling device (fan/processor/...)
1003 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1004 * to all the thermal zone devices registered at the same time.
1005 *
1006 * Return: a pointer to the created struct thermal_cooling_device or an
1007 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1008 */
1009struct thermal_cooling_device *
1010thermal_of_cooling_device_register(struct device_node *np,
1011 char *type, void *devdata,
1012 const struct thermal_cooling_device_ops *ops)
1013{
1014 return __thermal_cooling_device_register(np, type, devdata, ops);
1015}
1016EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1017
Eduardo Valentinf11997f2016-11-07 21:09:08 -08001018static void __unbind(struct thermal_zone_device *tz, int mask,
1019 struct thermal_cooling_device *cdev)
1020{
1021 int i;
1022
1023 for (i = 0; i < tz->trips; i++)
1024 if (mask & (1 << i))
1025 thermal_zone_unbind_cooling_device(tz, i, cdev);
1026}
1027
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001028/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001029 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001030 * @cdev: the thermal cooling device to remove.
1031 *
1032 * thermal_cooling_device_unregister() must be called when the device is no
1033 * longer needed.
1034 */
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301035void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
Zhang Rui203d3d42008-01-17 15:51:08 +08001036{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301037 int i;
1038 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001039 struct thermal_zone_device *tz;
1040 struct thermal_cooling_device *pos = NULL;
1041
1042 if (!cdev)
1043 return;
1044
1045 mutex_lock(&thermal_list_lock);
1046 list_for_each_entry(pos, &thermal_cdev_list, node)
1047 if (pos == cdev)
1048 break;
1049 if (pos != cdev) {
1050 /* thermal cooling device not found */
1051 mutex_unlock(&thermal_list_lock);
1052 return;
1053 }
1054 list_del(&cdev->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301055
1056 /* Unbind all thermal zones associated with 'this' cdev */
Zhang Rui203d3d42008-01-17 15:51:08 +08001057 list_for_each_entry(tz, &thermal_tz_list, node) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301058 if (tz->ops->unbind) {
1059 tz->ops->unbind(tz, cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +08001060 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301061 }
1062
1063 if (!tz->tzp || !tz->tzp->tbp)
1064 continue;
1065
1066 tzp = tz->tzp;
1067 for (i = 0; i < tzp->num_tbps; i++) {
1068 if (tzp->tbp[i].cdev == cdev) {
1069 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1070 tzp->tbp[i].cdev = NULL;
1071 }
1072 }
Zhang Rui203d3d42008-01-17 15:51:08 +08001073 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301074
Zhang Rui203d3d42008-01-17 15:51:08 +08001075 mutex_unlock(&thermal_list_lock);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301076
Zhang Rui203d3d42008-01-17 15:51:08 +08001077 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1078 device_unregister(&cdev->device);
1079 return;
1080}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001081EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001082
Eduardo Valentin90f5b5b2016-11-07 21:09:10 -08001083static void bind_tz(struct thermal_zone_device *tz)
1084{
1085 int i, ret;
1086 struct thermal_cooling_device *pos = NULL;
1087 const struct thermal_zone_params *tzp = tz->tzp;
1088
1089 if (!tzp && !tz->ops->bind)
1090 return;
1091
1092 mutex_lock(&thermal_list_lock);
1093
1094 /* If there is ops->bind, try to use ops->bind */
1095 if (tz->ops->bind) {
1096 list_for_each_entry(pos, &thermal_cdev_list, node) {
1097 ret = tz->ops->bind(tz, pos);
1098 if (ret)
1099 print_bind_err_msg(tz, pos, ret);
1100 }
1101 goto exit;
1102 }
1103
1104 if (!tzp || !tzp->tbp)
1105 goto exit;
1106
1107 list_for_each_entry(pos, &thermal_cdev_list, node) {
1108 for (i = 0; i < tzp->num_tbps; i++) {
1109 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
1110 continue;
1111 if (tzp->tbp[i].match(tz, pos))
1112 continue;
1113 tzp->tbp[i].cdev = pos;
1114 __bind(tz, tzp->tbp[i].trip_mask, pos,
1115 tzp->tbp[i].binding_limits,
1116 tzp->tbp[i].weight);
1117 }
1118 }
1119exit:
1120 mutex_unlock(&thermal_list_lock);
1121}
1122
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301123/**
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001124 * thermal_zone_device_register() - register a new thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001125 * @type: the thermal zone device type
1126 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001127 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08001128 * @devdata: private device data
1129 * @ops: standard thermal zone device callbacks
Durgadoss R50125a92012-09-18 11:04:56 +05301130 * @tzp: thermal zone platform parameters
Matthew Garrettb1569e92008-12-03 17:55:32 +00001131 * @passive_delay: number of milliseconds to wait between polls when
1132 * performing passive cooling
1133 * @polling_delay: number of milliseconds to wait between polls when checking
1134 * whether trip points have been crossed (0 for interrupt
1135 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001136 *
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001137 * This interface function adds a new thermal zone device (sensor) to
1138 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1139 * thermal cooling devices registered at the same time.
Zhang Rui203d3d42008-01-17 15:51:08 +08001140 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001141 * longer needed. The passive cooling depends on the .get_trend() return value.
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001142 *
1143 * Return: a pointer to the created struct thermal_zone_device or an
1144 * in case of error, an ERR_PTR. Caller must check return value with
1145 * IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001146 */
Anton Vorontsov4b1bf582012-07-31 04:39:30 -07001147struct thermal_zone_device *thermal_zone_device_register(const char *type,
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001148 int trips, int mask, void *devdata,
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001149 struct thermal_zone_device_ops *ops,
Javi Merino6b775e82015-03-02 17:17:19 +00001150 struct thermal_zone_params *tzp,
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001151 int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001152{
1153 struct thermal_zone_device *tz;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001154 enum thermal_trip_type trip_type;
Zhang Rui81ad4272016-03-18 10:03:24 +08001155 int trip_temp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001156 int result;
1157 int count;
Javi Merinoe33df1d2015-02-26 19:00:27 +00001158 struct thermal_governor *governor;
Zhang Rui203d3d42008-01-17 15:51:08 +08001159
Eduardo Valentin54fa38c2016-11-07 21:08:39 -08001160 if (!type || strlen(type) == 0)
1161 return ERR_PTR(-EINVAL);
1162
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001163 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001164 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001165
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001166 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001167 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001168
Eduardo Valentin81bd4e12013-09-12 19:15:44 -04001169 if (!ops)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001170 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001171
Jonghwa Lee83720d02013-05-18 09:50:26 +00001172 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
Eduardo Valentin6b2aa512013-01-02 15:29:42 +00001173 return ERR_PTR(-EINVAL);
1174
Zhang Rui203d3d42008-01-17 15:51:08 +08001175 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1176 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001177 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001178
Zhang Rui2d374132012-06-27 10:09:00 +08001179 INIT_LIST_HEAD(&tz->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001180 idr_init(&tz->idr);
1181 mutex_init(&tz->lock);
1182 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1183 if (result) {
1184 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001185 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001186 }
1187
Eduardo Valentin54fa38c2016-11-07 21:08:39 -08001188 strlcpy(tz->type, type, sizeof(tz->type));
Zhang Rui203d3d42008-01-17 15:51:08 +08001189 tz->ops = ops;
Durgadoss R50125a92012-09-18 11:04:56 +05301190 tz->tzp = tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001191 tz->device.class = &thermal_class;
1192 tz->devdata = devdata;
1193 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001194 tz->passive_delay = passive_delay;
1195 tz->polling_delay = polling_delay;
Eduardo Valentin1c600862016-11-07 21:08:42 -08001196
Eduardo Valentin4d0fe742016-11-07 21:08:52 -08001197 /* sys I/F */
Eduardo Valentin1c600862016-11-07 21:08:42 -08001198 /* Add nodes that are always present via .groups */
Eduardo Valentin4d0fe742016-11-07 21:08:52 -08001199 result = thermal_zone_create_device_groups(tz, mask);
1200 if (result)
1201 goto unregister;
1202
Chen Yu4511f712015-10-30 16:32:10 +08001203 /* A new thermal zone needs to be updated anyway. */
1204 atomic_set(&tz->need_update, 1);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001205
Kay Sievers354655e2009-01-06 10:44:37 -08001206 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001207 result = device_register(&tz->device);
1208 if (result) {
1209 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1210 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001211 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001212 }
1213
Zhang Rui203d3d42008-01-17 15:51:08 +08001214 for (count = 0; count < trips; count++) {
Zhang Rui81ad4272016-03-18 10:03:24 +08001215 if (tz->ops->get_trip_type(tz, count, &trip_type))
1216 set_bit(count, &tz->trips_disabled);
Zhang Rui81ad4272016-03-18 10:03:24 +08001217 if (tz->ops->get_trip_temp(tz, count, &trip_temp))
1218 set_bit(count, &tz->trips_disabled);
1219 /* Check for bogus trip points */
1220 if (trip_temp == 0)
1221 set_bit(count, &tz->trips_disabled);
Zhang Rui203d3d42008-01-17 15:51:08 +08001222 }
1223
Durgadoss Ra4a15482012-09-18 11:04:57 +05301224 /* Update 'this' zone's governor information */
1225 mutex_lock(&thermal_governor_lock);
1226
1227 if (tz->tzp)
Javi Merinoe33df1d2015-02-26 19:00:27 +00001228 governor = __find_governor(tz->tzp->governor_name);
Durgadoss Ra4a15482012-09-18 11:04:57 +05301229 else
Javi Merinoe33df1d2015-02-26 19:00:27 +00001230 governor = def_governor;
1231
1232 result = thermal_set_governor(tz, governor);
1233 if (result) {
1234 mutex_unlock(&thermal_governor_lock);
1235 goto unregister;
1236 }
Durgadoss Ra4a15482012-09-18 11:04:57 +05301237
1238 mutex_unlock(&thermal_governor_lock);
1239
Eduardo Valentinccba4ff2013-08-15 11:34:17 -04001240 if (!tz->tzp || !tz->tzp->no_hwmon) {
1241 result = thermal_add_hwmon_sysfs(tz);
1242 if (result)
1243 goto unregister;
1244 }
Zhang Ruie68b16a2008-04-21 16:07:52 +08001245
Zhang Rui203d3d42008-01-17 15:51:08 +08001246 mutex_lock(&thermal_list_lock);
1247 list_add_tail(&tz->node, &thermal_tz_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001248 mutex_unlock(&thermal_list_lock);
1249
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301250 /* Bind cooling devices for this zone */
1251 bind_tz(tz);
1252
Matthew Garrettb1569e92008-12-03 17:55:32 +00001253 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1254
Zhang Ruibb431ba2015-10-30 16:31:47 +08001255 thermal_zone_device_reset(tz);
Chen Yu4511f712015-10-30 16:32:10 +08001256 /* Update the new thermal zone and mark it as already updated. */
1257 if (atomic_cmpxchg(&tz->need_update, 1, 0))
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -07001258 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001259
Yao Dongdong14015862014-10-28 07:40:25 +00001260 return tz;
Zhang Rui203d3d42008-01-17 15:51:08 +08001261
Joe Perchescaca8b802012-03-21 12:55:02 -07001262unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001263 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1264 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001265 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001266}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001267EXPORT_SYMBOL_GPL(thermal_zone_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001268
1269/**
1270 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001271 * @tz: the thermal zone device to remove
1272 */
1273void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1274{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301275 int i;
1276 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001277 struct thermal_cooling_device *cdev;
1278 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001279
1280 if (!tz)
1281 return;
1282
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301283 tzp = tz->tzp;
1284
Zhang Rui203d3d42008-01-17 15:51:08 +08001285 mutex_lock(&thermal_list_lock);
1286 list_for_each_entry(pos, &thermal_tz_list, node)
1287 if (pos == tz)
1288 break;
1289 if (pos != tz) {
1290 /* thermal zone device not found */
1291 mutex_unlock(&thermal_list_lock);
1292 return;
1293 }
1294 list_del(&tz->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301295
1296 /* Unbind all cdevs associated with 'this' thermal zone */
1297 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1298 if (tz->ops->unbind) {
1299 tz->ops->unbind(tz, cdev);
1300 continue;
1301 }
1302
1303 if (!tzp || !tzp->tbp)
1304 break;
1305
1306 for (i = 0; i < tzp->num_tbps; i++) {
1307 if (tzp->tbp[i].cdev == cdev) {
1308 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1309 tzp->tbp[i].cdev = NULL;
1310 }
1311 }
1312 }
1313
Zhang Rui203d3d42008-01-17 15:51:08 +08001314 mutex_unlock(&thermal_list_lock);
1315
Matthew Garrettb1569e92008-12-03 17:55:32 +00001316 thermal_zone_device_set_polling(tz, 0);
1317
Eduardo Valentin4d0fe742016-11-07 21:08:52 -08001318 kfree(tz->trip_type_attrs);
1319 kfree(tz->trip_temp_attrs);
1320 kfree(tz->trip_hyst_attrs);
1321 kfree(tz->trips_attribute_group.attrs);
Javi Merinoe33df1d2015-02-26 19:00:27 +00001322 thermal_set_governor(tz, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001323
Zhang Ruie68b16a2008-04-21 16:07:52 +08001324 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001325 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1326 idr_destroy(&tz->idr);
1327 mutex_destroy(&tz->lock);
1328 device_unregister(&tz->device);
Eduardo Valentine161aef2016-11-07 21:08:51 -08001329 kfree(tz->device.groups);
Zhang Rui203d3d42008-01-17 15:51:08 +08001330}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001331EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001332
Eduardo Valentin63c4d912013-04-05 12:32:28 +00001333/**
1334 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1335 * @name: thermal zone name to fetch the temperature
1336 *
1337 * When only one zone is found with the passed name, returns a reference to it.
1338 *
1339 * Return: On success returns a reference to an unique thermal zone with
1340 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1341 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1342 */
1343struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1344{
1345 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1346 unsigned int found = 0;
1347
1348 if (!name)
1349 goto exit;
1350
1351 mutex_lock(&thermal_list_lock);
1352 list_for_each_entry(pos, &thermal_tz_list, node)
Rasmus Villemoes484ac2f2014-10-13 15:55:01 -07001353 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
Eduardo Valentin63c4d912013-04-05 12:32:28 +00001354 found++;
1355 ref = pos;
1356 }
1357 mutex_unlock(&thermal_list_lock);
1358
1359 /* nothing has been found, thus an error code for it */
1360 if (found == 0)
1361 ref = ERR_PTR(-ENODEV);
1362 else if (found > 1)
1363 /* Success only when an unique zone is found */
1364 ref = ERR_PTR(-EEXIST);
1365
1366exit:
1367 return ref;
1368}
1369EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1370
Rajendra Nayak4a7069a2016-05-05 14:21:42 +05301371/**
1372 * thermal_zone_get_slope - return the slope attribute of the thermal zone
1373 * @tz: thermal zone device with the slope attribute
1374 *
1375 * Return: If the thermal zone device has a slope attribute, return it, else
1376 * return 1.
1377 */
1378int thermal_zone_get_slope(struct thermal_zone_device *tz)
1379{
1380 if (tz && tz->tzp)
1381 return tz->tzp->slope;
1382 return 1;
1383}
1384EXPORT_SYMBOL_GPL(thermal_zone_get_slope);
1385
1386/**
1387 * thermal_zone_get_offset - return the offset attribute of the thermal zone
1388 * @tz: thermal zone device with the offset attribute
1389 *
1390 * Return: If the thermal zone device has a offset attribute, return it, else
1391 * return 0.
1392 */
1393int thermal_zone_get_offset(struct thermal_zone_device *tz)
1394{
1395 if (tz && tz->tzp)
1396 return tz->tzp->offset;
1397 return 0;
1398}
1399EXPORT_SYMBOL_GPL(thermal_zone_get_offset);
1400
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001401#ifdef CONFIG_NET
Johannes Berg2a94fe42013-11-19 15:19:39 +01001402static const struct genl_multicast_group thermal_event_mcgrps[] = {
1403 { .name = THERMAL_GENL_MCAST_GROUP_NAME, },
1404};
1405
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001406static struct genl_family thermal_event_genl_family = {
1407 .id = GENL_ID_GENERATE,
1408 .name = THERMAL_GENL_FAMILY_NAME,
1409 .version = THERMAL_GENL_VERSION,
1410 .maxattr = THERMAL_GENL_ATTR_MAX,
Johannes Berg2a94fe42013-11-19 15:19:39 +01001411 .mcgrps = thermal_event_mcgrps,
1412 .n_mcgrps = ARRAY_SIZE(thermal_event_mcgrps),
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001413};
1414
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001415int thermal_generate_netlink_event(struct thermal_zone_device *tz,
1416 enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05301417{
1418 struct sk_buff *skb;
1419 struct nlattr *attr;
1420 struct thermal_genl_event *thermal_event;
1421 void *msg_header;
1422 int size;
1423 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07001424 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301425
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001426 if (!tz)
1427 return -EINVAL;
1428
R.Durgadoss4cb18722010-10-27 03:33:29 +05301429 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07001430 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1431 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301432
1433 skb = genlmsg_new(size, GFP_ATOMIC);
1434 if (!skb)
1435 return -ENOMEM;
1436
1437 /* add the genetlink message header */
1438 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1439 &thermal_event_genl_family, 0,
1440 THERMAL_GENL_CMD_EVENT);
1441 if (!msg_header) {
1442 nlmsg_free(skb);
1443 return -ENOMEM;
1444 }
1445
1446 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07001447 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1448 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05301449
1450 if (!attr) {
1451 nlmsg_free(skb);
1452 return -EINVAL;
1453 }
1454
1455 thermal_event = nla_data(attr);
1456 if (!thermal_event) {
1457 nlmsg_free(skb);
1458 return -EINVAL;
1459 }
1460
1461 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1462
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001463 thermal_event->orig = tz->id;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301464 thermal_event->event = event;
1465
1466 /* send multicast genetlink message */
Johannes Berg053c0952015-01-16 22:09:00 +01001467 genlmsg_end(skb, msg_header);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301468
Johannes Berg68eb5502013-11-19 15:19:38 +01001469 result = genlmsg_multicast(&thermal_event_genl_family, skb, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +01001470 0, GFP_ATOMIC);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301471 if (result)
Eduardo Valentin923e0b12013-01-02 15:29:41 +00001472 dev_err(&tz->device, "Failed to send netlink event:%d", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301473
1474 return result;
1475}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001476EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301477
1478static int genetlink_init(void)
1479{
Johannes Berg2a94fe42013-11-19 15:19:39 +01001480 return genl_register_family(&thermal_event_genl_family);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301481}
1482
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001483static void genetlink_exit(void)
1484{
1485 genl_unregister_family(&thermal_event_genl_family);
1486}
1487#else /* !CONFIG_NET */
1488static inline int genetlink_init(void) { return 0; }
1489static inline void genetlink_exit(void) {}
1490#endif /* !CONFIG_NET */
1491
Zhang Ruiff140fe2015-10-30 16:31:58 +08001492static int thermal_pm_notify(struct notifier_block *nb,
1493 unsigned long mode, void *_unused)
1494{
1495 struct thermal_zone_device *tz;
1496
1497 switch (mode) {
1498 case PM_HIBERNATION_PREPARE:
1499 case PM_RESTORE_PREPARE:
1500 case PM_SUSPEND_PREPARE:
1501 atomic_set(&in_suspend, 1);
1502 break;
1503 case PM_POST_HIBERNATION:
1504 case PM_POST_RESTORE:
1505 case PM_POST_SUSPEND:
1506 atomic_set(&in_suspend, 0);
1507 list_for_each_entry(tz, &thermal_tz_list, node) {
1508 thermal_zone_device_reset(tz);
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -07001509 thermal_zone_device_update(tz,
1510 THERMAL_EVENT_UNSPECIFIED);
Zhang Ruiff140fe2015-10-30 16:31:58 +08001511 }
1512 break;
1513 default:
1514 break;
1515 }
1516 return 0;
1517}
1518
1519static struct notifier_block thermal_pm_nb = {
1520 .notifier_call = thermal_pm_notify,
1521};
1522
Zhang Rui203d3d42008-01-17 15:51:08 +08001523static int __init thermal_init(void)
1524{
Zhang Rui80a26a52013-03-26 16:38:29 +08001525 int result;
1526
1527 result = thermal_register_governors();
1528 if (result)
1529 goto error;
Zhang Rui203d3d42008-01-17 15:51:08 +08001530
1531 result = class_register(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08001532 if (result)
1533 goto unregister_governors;
1534
R.Durgadoss4cb18722010-10-27 03:33:29 +05301535 result = genetlink_init();
Zhang Rui80a26a52013-03-26 16:38:29 +08001536 if (result)
1537 goto unregister_class;
1538
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001539 result = of_parse_thermal_zones();
1540 if (result)
1541 goto exit_netlink;
1542
Zhang Ruiff140fe2015-10-30 16:31:58 +08001543 result = register_pm_notifier(&thermal_pm_nb);
1544 if (result)
1545 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
1546 result);
1547
Zhang Rui80a26a52013-03-26 16:38:29 +08001548 return 0;
1549
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001550exit_netlink:
1551 genetlink_exit();
Zhang Rui80a26a52013-03-26 16:38:29 +08001552unregister_class:
1553 class_unregister(&thermal_class);
Luis Henriques9d367e52014-12-03 21:20:21 +00001554unregister_governors:
1555 thermal_unregister_governors();
Zhang Rui80a26a52013-03-26 16:38:29 +08001556error:
1557 idr_destroy(&thermal_tz_idr);
1558 idr_destroy(&thermal_cdev_idr);
1559 mutex_destroy(&thermal_idr_lock);
1560 mutex_destroy(&thermal_list_lock);
1561 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001562 return result;
1563}
1564
1565static void __exit thermal_exit(void)
1566{
Zhang Ruiff140fe2015-10-30 16:31:58 +08001567 unregister_pm_notifier(&thermal_pm_nb);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001568 of_thermal_destroy_zones();
Zhang Rui80a26a52013-03-26 16:38:29 +08001569 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08001570 class_unregister(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08001571 thermal_unregister_governors();
Zhang Rui203d3d42008-01-17 15:51:08 +08001572 idr_destroy(&thermal_tz_idr);
1573 idr_destroy(&thermal_cdev_idr);
1574 mutex_destroy(&thermal_idr_lock);
1575 mutex_destroy(&thermal_list_lock);
Zhang Rui80a26a52013-03-26 16:38:29 +08001576 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001577}
1578
R.Durgadoss4cb18722010-10-27 03:33:29 +05301579fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08001580module_exit(thermal_exit);