blob: 11f0675cb7e552456d36ec49c82a46d5cf016d04 [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 *
Zhang Rui203d3d42008-01-17 15:51:08 +08008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
Zhang Rui203d3d42008-01-17 15:51:08 +080011 */
12
Joe Perchesc5a01dd2012-03-21 12:55:02 -070013#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
Zhang Rui203d3d42008-01-17 15:51:08 +080015#include <linux/module.h>
16#include <linux/device.h>
17#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Zhang Rui203d3d42008-01-17 15:51:08 +080019#include <linux/kdev_t.h>
20#include <linux/idr.h>
21#include <linux/thermal.h>
Matthew Garrettb1569e92008-12-03 17:55:32 +000022#include <linux/reboot.h>
Andy Shevchenko42a5bf52013-05-17 11:52:02 +000023#include <linux/string.h>
Eduardo Valentina116b5d2013-09-26 15:55:01 -040024#include <linux/of.h>
R.Durgadoss4cb18722010-10-27 03:33:29 +053025#include <net/netlink.h>
26#include <net/genetlink.h>
Zhang Ruiff140fe2015-10-30 16:31:58 +080027#include <linux/suspend.h>
Zhang Rui203d3d42008-01-17 15:51:08 +080028
Punit Agrawal100a8fd2014-07-29 11:50:48 +010029#define CREATE_TRACE_POINTS
30#include <trace/events/thermal.h>
31
Durgadoss R71350db2012-09-18 11:04:53 +053032#include "thermal_core.h"
Eduardo Valentin0dd88792013-07-03 15:14:28 -040033#include "thermal_hwmon.h"
Durgadoss R71350db2012-09-18 11:04:53 +053034
Zhang Rui63c4ec92008-04-21 16:07:13 +080035MODULE_AUTHOR("Zhang Rui");
Zhang Rui203d3d42008-01-17 15:51:08 +080036MODULE_DESCRIPTION("Generic thermal management sysfs support");
Eduardo Valentin6d8d4972013-04-23 21:48:13 +000037MODULE_LICENSE("GPL v2");
Zhang Rui203d3d42008-01-17 15:51:08 +080038
Matthew Wilcoxb31ef822016-12-21 09:47:03 -080039static DEFINE_IDA(thermal_tz_ida);
40static DEFINE_IDA(thermal_cdev_ida);
Zhang Rui203d3d42008-01-17 15:51:08 +080041
42static LIST_HEAD(thermal_tz_list);
43static LIST_HEAD(thermal_cdev_list);
Durgadoss Ra4a15482012-09-18 11:04:57 +053044static LIST_HEAD(thermal_governor_list);
45
Zhang Rui203d3d42008-01-17 15:51:08 +080046static DEFINE_MUTEX(thermal_list_lock);
Durgadoss Ra4a15482012-09-18 11:04:57 +053047static DEFINE_MUTEX(thermal_governor_lock);
48
Zhang Ruiff140fe2015-10-30 16:31:58 +080049static atomic_t in_suspend;
50
Zhang Ruif2234bc2014-01-24 10:23:19 +080051static struct thermal_governor *def_governor;
52
Eduardo Valentin1b4f4842016-11-07 21:09:05 -080053/*
54 * Governor section: set of functions to handle thermal governors
55 *
56 * Functions to help in the life cycle of thermal governors within
57 * the thermal core and by the thermal governor code.
58 */
59
Durgadoss Ra4a15482012-09-18 11:04:57 +053060static struct thermal_governor *__find_governor(const char *name)
61{
62 struct thermal_governor *pos;
63
Zhang Ruif2234bc2014-01-24 10:23:19 +080064 if (!name || !name[0])
65 return def_governor;
66
Durgadoss Ra4a15482012-09-18 11:04:57 +053067 list_for_each_entry(pos, &thermal_governor_list, governor_list)
Rasmus Villemoes484ac2f2014-10-13 15:55:01 -070068 if (!strncasecmp(name, pos->name, THERMAL_NAME_LENGTH))
Durgadoss Ra4a15482012-09-18 11:04:57 +053069 return pos;
70
71 return NULL;
72}
73
Javi Merinoe33df1d2015-02-26 19:00:27 +000074/**
75 * bind_previous_governor() - bind the previous governor of the thermal zone
76 * @tz: a valid pointer to a struct thermal_zone_device
77 * @failed_gov_name: the name of the governor that failed to register
78 *
79 * Register the previous governor of the thermal zone after a new
80 * governor has failed to be bound.
81 */
82static void bind_previous_governor(struct thermal_zone_device *tz,
83 const char *failed_gov_name)
84{
85 if (tz->governor && tz->governor->bind_to_tz) {
86 if (tz->governor->bind_to_tz(tz)) {
87 dev_err(&tz->device,
88 "governor %s failed to bind and the previous one (%s) failed to bind again, thermal zone %s has no governor\n",
89 failed_gov_name, tz->governor->name, tz->type);
90 tz->governor = NULL;
91 }
92 }
93}
94
95/**
96 * thermal_set_governor() - Switch to another governor
97 * @tz: a valid pointer to a struct thermal_zone_device
98 * @new_gov: pointer to the new governor
99 *
100 * Change the governor of thermal zone @tz.
101 *
102 * Return: 0 on success, an error if the new governor's bind_to_tz() failed.
103 */
104static int thermal_set_governor(struct thermal_zone_device *tz,
105 struct thermal_governor *new_gov)
106{
107 int ret = 0;
108
109 if (tz->governor && tz->governor->unbind_from_tz)
110 tz->governor->unbind_from_tz(tz);
111
112 if (new_gov && new_gov->bind_to_tz) {
113 ret = new_gov->bind_to_tz(tz);
114 if (ret) {
115 bind_previous_governor(tz, new_gov->name);
116
117 return ret;
118 }
119 }
120
121 tz->governor = new_gov;
122
123 return ret;
124}
125
Durgadoss Ra4a15482012-09-18 11:04:57 +0530126int thermal_register_governor(struct thermal_governor *governor)
127{
128 int err;
129 const char *name;
130 struct thermal_zone_device *pos;
131
132 if (!governor)
133 return -EINVAL;
134
135 mutex_lock(&thermal_governor_lock);
136
137 err = -EBUSY;
Eduardo Valentin5027ba32016-11-07 21:09:20 -0800138 if (!__find_governor(governor->name)) {
Eduardo Valentineb7be322016-11-07 21:09:21 -0800139 bool match_default;
140
Durgadoss Ra4a15482012-09-18 11:04:57 +0530141 err = 0;
142 list_add(&governor->governor_list, &thermal_governor_list);
Eduardo Valentineb7be322016-11-07 21:09:21 -0800143 match_default = !strncmp(governor->name,
144 DEFAULT_THERMAL_GOVERNOR,
145 THERMAL_NAME_LENGTH);
146
147 if (!def_governor && match_default)
Zhang Ruif2234bc2014-01-24 10:23:19 +0800148 def_governor = governor;
Durgadoss Ra4a15482012-09-18 11:04:57 +0530149 }
150
151 mutex_lock(&thermal_list_lock);
152
153 list_for_each_entry(pos, &thermal_tz_list, node) {
Zhang Ruif2234bc2014-01-24 10:23:19 +0800154 /*
155 * only thermal zones with specified tz->tzp->governor_name
156 * may run with tz->govenor unset
157 */
Durgadoss Ra4a15482012-09-18 11:04:57 +0530158 if (pos->governor)
159 continue;
Zhang Ruif2234bc2014-01-24 10:23:19 +0800160
161 name = pos->tzp->governor_name;
162
Javi Merinoe33df1d2015-02-26 19:00:27 +0000163 if (!strncasecmp(name, governor->name, THERMAL_NAME_LENGTH)) {
164 int ret;
165
166 ret = thermal_set_governor(pos, governor);
167 if (ret)
168 dev_err(&pos->device,
169 "Failed to set governor %s for thermal zone %s: %d\n",
170 governor->name, pos->type, ret);
171 }
Durgadoss Ra4a15482012-09-18 11:04:57 +0530172 }
173
174 mutex_unlock(&thermal_list_lock);
175 mutex_unlock(&thermal_governor_lock);
176
177 return err;
178}
Durgadoss Ra4a15482012-09-18 11:04:57 +0530179
180void thermal_unregister_governor(struct thermal_governor *governor)
181{
182 struct thermal_zone_device *pos;
183
184 if (!governor)
185 return;
186
187 mutex_lock(&thermal_governor_lock);
188
Eduardo Valentin5027ba32016-11-07 21:09:20 -0800189 if (!__find_governor(governor->name))
Durgadoss Ra4a15482012-09-18 11:04:57 +0530190 goto exit;
191
192 mutex_lock(&thermal_list_lock);
193
194 list_for_each_entry(pos, &thermal_tz_list, node) {
Rasmus Villemoes484ac2f2014-10-13 15:55:01 -0700195 if (!strncasecmp(pos->governor->name, governor->name,
Eduardo Valentineb7be322016-11-07 21:09:21 -0800196 THERMAL_NAME_LENGTH))
Javi Merinoe33df1d2015-02-26 19:00:27 +0000197 thermal_set_governor(pos, NULL);
Durgadoss Ra4a15482012-09-18 11:04:57 +0530198 }
199
200 mutex_unlock(&thermal_list_lock);
201 list_del(&governor->governor_list);
202exit:
203 mutex_unlock(&thermal_governor_lock);
Durgadoss Ra4a15482012-09-18 11:04:57 +0530204}
Zhang Rui203d3d42008-01-17 15:51:08 +0800205
Eduardo Valentin1b4f4842016-11-07 21:09:05 -0800206int thermal_zone_device_set_policy(struct thermal_zone_device *tz,
207 char *policy)
Zhang Rui203d3d42008-01-17 15:51:08 +0800208{
Eduardo Valentin1b4f4842016-11-07 21:09:05 -0800209 struct thermal_governor *gov;
210 int ret = -EINVAL;
Zhang Rui203d3d42008-01-17 15:51:08 +0800211
Eduardo Valentin1b4f4842016-11-07 21:09:05 -0800212 mutex_lock(&thermal_governor_lock);
Durgadoss R9b4298a2012-09-18 11:04:54 +0530213 mutex_lock(&tz->lock);
Durgadoss R9b4298a2012-09-18 11:04:54 +0530214
Eduardo Valentin1b4f4842016-11-07 21:09:05 -0800215 gov = __find_governor(strim(policy));
216 if (!gov)
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530217 goto exit;
218
Eduardo Valentin1b4f4842016-11-07 21:09:05 -0800219 ret = thermal_set_governor(tz, gov);
220
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530221exit:
Eduardo Valentin1b4f4842016-11-07 21:09:05 -0800222 mutex_unlock(&tz->lock);
223 mutex_unlock(&thermal_governor_lock);
224
225 return ret;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530226}
227
Eduardo Valentin1b4f4842016-11-07 21:09:05 -0800228int thermal_build_list_of_policies(char *buf)
229{
230 struct thermal_governor *pos;
231 ssize_t count = 0;
232 ssize_t size = PAGE_SIZE;
233
234 mutex_lock(&thermal_governor_lock);
235
236 list_for_each_entry(pos, &thermal_governor_list, governor_list) {
237 size = PAGE_SIZE - count;
238 count += scnprintf(buf + count, size, "%s ", pos->name);
239 }
240 count += scnprintf(buf + count, size, "\n");
241
242 mutex_unlock(&thermal_governor_lock);
243
244 return count;
245}
246
247static int __init thermal_register_governors(void)
248{
249 int result;
250
251 result = thermal_gov_step_wise_register();
252 if (result)
253 return result;
254
255 result = thermal_gov_fair_share_register();
256 if (result)
257 return result;
258
259 result = thermal_gov_bang_bang_register();
260 if (result)
261 return result;
262
263 result = thermal_gov_user_space_register();
264 if (result)
265 return result;
266
267 return thermal_gov_power_allocator_register();
268}
269
270static void thermal_unregister_governors(void)
271{
272 thermal_gov_step_wise_unregister();
273 thermal_gov_fair_share_unregister();
274 thermal_gov_bang_bang_unregister();
275 thermal_gov_user_space_unregister();
276 thermal_gov_power_allocator_unregister();
277}
278
Eduardo Valentin8772e182016-11-07 21:09:15 -0800279/*
280 * Zone update section: main control loop applied to each zone while monitoring
281 *
282 * in polling mode. The monitoring is done using a workqueue.
283 * Same update may be done on a zone by calling thermal_zone_device_update().
284 *
285 * An update means:
286 * - Non-critical trips will invoke the governor responsible for that zone;
287 * - Hot trips will produce a notification to userspace;
288 * - Critical trip point will cause a system shutdown.
289 */
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530290static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
291 int delay)
292{
293 if (delay > 1000)
294 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
295 round_jiffies(msecs_to_jiffies(delay)));
296 else if (delay)
297 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
298 msecs_to_jiffies(delay));
299 else
300 cancel_delayed_work(&tz->poll_queue);
301}
302
303static void monitor_thermal_zone(struct thermal_zone_device *tz)
304{
305 mutex_lock(&tz->lock);
306
307 if (tz->passive)
308 thermal_zone_device_set_polling(tz, tz->passive_delay);
309 else if (tz->polling_delay)
310 thermal_zone_device_set_polling(tz, tz->polling_delay);
311 else
312 thermal_zone_device_set_polling(tz, 0);
313
314 mutex_unlock(&tz->lock);
315}
316
317static void handle_non_critical_trips(struct thermal_zone_device *tz,
Eduardo Valentineb7be322016-11-07 21:09:21 -0800318 int trip,
319 enum thermal_trip_type trip_type)
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530320{
Zhang Ruif2234bc2014-01-24 10:23:19 +0800321 tz->governor ? tz->governor->throttle(tz, trip) :
322 def_governor->throttle(tz, trip);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530323}
324
325static void handle_critical_trips(struct thermal_zone_device *tz,
Eduardo Valentineb7be322016-11-07 21:09:21 -0800326 int trip, enum thermal_trip_type trip_type)
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530327{
Sascha Hauer17e83512015-07-24 08:12:54 +0200328 int trip_temp;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530329
330 tz->ops->get_trip_temp(tz, trip, &trip_temp);
331
332 /* If we have not crossed the trip_temp, we do not care. */
Srinivas Pandruvada84ffe3e2014-11-12 15:43:29 -0800333 if (trip_temp <= 0 || tz->temperature < trip_temp)
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530334 return;
335
Punit Agrawal208cd822014-07-29 11:50:50 +0100336 trace_thermal_zone_trip(tz, trip, trip_type);
337
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530338 if (tz->ops->notify)
339 tz->ops->notify(tz, trip, trip_type);
340
341 if (trip_type == THERMAL_TRIP_CRITICAL) {
Eduardo Valentin923e0b12013-01-02 15:29:41 +0000342 dev_emerg(&tz->device,
343 "critical temperature reached(%d C),shutting down\n",
344 tz->temperature / 1000);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530345 orderly_poweroff(true);
346 }
347}
348
349static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
350{
351 enum thermal_trip_type type;
352
Zhang Rui81ad4272016-03-18 10:03:24 +0800353 /* Ignore disabled trip points */
354 if (test_bit(trip, &tz->trips_disabled))
355 return;
356
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530357 tz->ops->get_trip_type(tz, trip, &type);
358
359 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
360 handle_critical_trips(tz, trip, type);
361 else
362 handle_non_critical_trips(tz, trip, type);
363 /*
364 * Alright, we handled this trip successfully.
365 * So, start monitoring again.
366 */
367 monitor_thermal_zone(tz);
368}
369
370static void update_temperature(struct thermal_zone_device *tz)
371{
Sascha Hauer17e83512015-07-24 08:12:54 +0200372 int temp, ret;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530373
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000374 ret = thermal_zone_get_temp(tz, &temp);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530375 if (ret) {
Hans de Goede7e497a72015-03-21 15:02:55 +0100376 if (ret != -EAGAIN)
377 dev_warn(&tz->device,
378 "failed to read out thermal zone (%d)\n",
379 ret);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000380 return;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530381 }
382
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000383 mutex_lock(&tz->lock);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530384 tz->last_temperature = tz->temperature;
385 tz->temperature = temp;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530386 mutex_unlock(&tz->lock);
Aaron Lu06475b52013-12-02 13:54:26 +0800387
Punit Agrawal100a8fd2014-07-29 11:50:48 +0100388 trace_thermal_temperature(tz);
Zhang Ruibb431ba2015-10-30 16:31:47 +0800389 if (tz->last_temperature == THERMAL_TEMP_INVALID)
390 dev_dbg(&tz->device, "last_temperature N/A, current_temperature=%d\n",
391 tz->temperature);
392 else
393 dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n",
394 tz->last_temperature, tz->temperature);
395}
396
397static void thermal_zone_device_reset(struct thermal_zone_device *tz)
398{
399 struct thermal_instance *pos;
400
401 tz->temperature = THERMAL_TEMP_INVALID;
402 tz->passive = 0;
403 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
404 pos->initialized = false;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530405}
406
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700407void thermal_zone_device_update(struct thermal_zone_device *tz,
408 enum thermal_notify_event event)
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530409{
410 int count;
411
Zhang Ruiff140fe2015-10-30 16:31:58 +0800412 if (atomic_read(&in_suspend))
413 return;
414
Eduardo Valentin81bd4e12013-09-12 19:15:44 -0400415 if (!tz->ops->get_temp)
416 return;
417
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530418 update_temperature(tz);
419
Sascha Hauer060c0342016-06-22 16:42:01 +0800420 thermal_zone_set_trips(tz);
421
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700422 tz->notify_event = event;
423
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530424 for (count = 0; count < tz->trips; count++)
425 handle_thermal_trip(tz, count);
426}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000427EXPORT_SYMBOL_GPL(thermal_zone_device_update);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530428
Eduardo Valentin106339a2016-11-07 21:09:14 -0800429/**
430 * thermal_notify_framework - Sensor drivers use this API to notify framework
431 * @tz: thermal zone device
432 * @trip: indicates which trip point has been crossed
433 *
434 * This function handles the trip events from sensor drivers. It starts
435 * throttling the cooling devices according to the policy configured.
436 * For CRITICAL and HOT trip points, this notifies the respective drivers,
437 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
438 * The throttling policy is based on the configured platform data; if no
439 * platform data is provided, this uses the step_wise throttling policy.
440 */
441void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
442{
443 handle_thermal_trip(tz, trip);
444}
445EXPORT_SYMBOL_GPL(thermal_notify_framework);
446
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530447static void thermal_zone_device_check(struct work_struct *work)
448{
449 struct thermal_zone_device *tz = container_of(work, struct
450 thermal_zone_device,
451 poll_queue.work);
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700452 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530453}
454
Eduardo Valentin712afbd2016-11-07 21:09:16 -0800455/*
456 * Power actor section: interface to power actors to estimate power
457 *
458 * Set of functions used to interact to cooling devices that know
459 * how to estimate their devices power consumption.
460 */
Javi Merino9f382712015-03-26 15:53:02 +0000461
Javi Merino35b11d22015-02-26 19:00:28 +0000462/**
463 * power_actor_get_max_power() - get the maximum power that a cdev can consume
464 * @cdev: pointer to &thermal_cooling_device
465 * @tz: a valid thermal zone device pointer
466 * @max_power: pointer in which to store the maximum power
467 *
468 * Calculate the maximum power consumption in milliwats that the
469 * cooling device can currently consume and store it in @max_power.
470 *
471 * Return: 0 on success, -EINVAL if @cdev doesn't support the
472 * power_actor API or -E* on other error.
473 */
474int power_actor_get_max_power(struct thermal_cooling_device *cdev,
475 struct thermal_zone_device *tz, u32 *max_power)
476{
477 if (!cdev_is_power_actor(cdev))
478 return -EINVAL;
479
480 return cdev->ops->state2power(cdev, tz, 0, max_power);
481}
482
483/**
Javi Merinoc973c3b2015-09-14 14:23:50 +0100484 * power_actor_get_min_power() - get the mainimum power that a cdev can consume
485 * @cdev: pointer to &thermal_cooling_device
486 * @tz: a valid thermal zone device pointer
487 * @min_power: pointer in which to store the minimum power
488 *
489 * Calculate the minimum power consumption in milliwatts that the
490 * cooling device can currently consume and store it in @min_power.
491 *
492 * Return: 0 on success, -EINVAL if @cdev doesn't support the
493 * power_actor API or -E* on other error.
494 */
495int power_actor_get_min_power(struct thermal_cooling_device *cdev,
496 struct thermal_zone_device *tz, u32 *min_power)
497{
498 unsigned long max_state;
499 int ret;
500
501 if (!cdev_is_power_actor(cdev))
502 return -EINVAL;
503
504 ret = cdev->ops->get_max_state(cdev, &max_state);
505 if (ret)
506 return ret;
507
508 return cdev->ops->state2power(cdev, tz, max_state, min_power);
509}
510
511/**
Eduardo Valentin1a7e7cc2016-11-07 21:08:47 -0800512 * power_actor_set_power() - limit the maximum power a cooling device consumes
Javi Merino35b11d22015-02-26 19:00:28 +0000513 * @cdev: pointer to &thermal_cooling_device
514 * @instance: thermal instance to update
515 * @power: the power in milliwatts
516 *
Eduardo Valentin1a7e7cc2016-11-07 21:08:47 -0800517 * Set the cooling device to consume at most @power milliwatts. The limit is
518 * expected to be a cap at the maximum power consumption.
Javi Merino35b11d22015-02-26 19:00:28 +0000519 *
520 * Return: 0 on success, -EINVAL if the cooling device does not
521 * implement the power actor API or -E* for other failures.
522 */
523int power_actor_set_power(struct thermal_cooling_device *cdev,
524 struct thermal_instance *instance, u32 power)
525{
526 unsigned long state;
527 int ret;
528
529 if (!cdev_is_power_actor(cdev))
530 return -EINVAL;
531
532 ret = cdev->ops->power2state(cdev, instance->tz, power, &state);
533 if (ret)
534 return ret;
535
536 instance->target = state;
Michele Di Giorgiod0b73062016-06-02 15:25:31 +0100537 mutex_lock(&cdev->lock);
Javi Merino35b11d22015-02-26 19:00:28 +0000538 cdev->updated = false;
Michele Di Giorgiod0b73062016-06-02 15:25:31 +0100539 mutex_unlock(&cdev->lock);
Javi Merino35b11d22015-02-26 19:00:28 +0000540 thermal_cdev_update(cdev);
541
542 return 0;
543}
544
Eduardo Valentin3d0055d2016-11-07 21:08:54 -0800545void thermal_zone_device_rebind_exception(struct thermal_zone_device *tz,
546 const char *cdev_type, size_t size)
Zhang Rui203d3d42008-01-17 15:51:08 +0800547{
Eduardo Valentin3d0055d2016-11-07 21:08:54 -0800548 struct thermal_cooling_device *cdev = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +0800549
Eduardo Valentin3d0055d2016-11-07 21:08:54 -0800550 mutex_lock(&thermal_list_lock);
551 list_for_each_entry(cdev, &thermal_cdev_list, node) {
552 /* skip non matching cdevs */
553 if (strncmp(cdev_type, cdev->type, size))
554 continue;
555
556 /* re binding the exception matching the type pattern */
557 thermal_zone_bind_cooling_device(tz, THERMAL_TRIPS_NONE, cdev,
558 THERMAL_NO_LIMIT,
559 THERMAL_NO_LIMIT,
560 THERMAL_WEIGHT_DEFAULT);
561 }
562 mutex_unlock(&thermal_list_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800563}
564
Eduardo Valentin3d0055d2016-11-07 21:08:54 -0800565void thermal_zone_device_unbind_exception(struct thermal_zone_device *tz,
566 const char *cdev_type, size_t size)
Zhang Rui203d3d42008-01-17 15:51:08 +0800567{
Eduardo Valentin3d0055d2016-11-07 21:08:54 -0800568 struct thermal_cooling_device *cdev = NULL;
569
570 mutex_lock(&thermal_list_lock);
571 list_for_each_entry(cdev, &thermal_cdev_list, node) {
572 /* skip non matching cdevs */
573 if (strncmp(cdev_type, cdev->type, size))
574 continue;
575 /* unbinding the exception matching the type pattern */
576 thermal_zone_unbind_cooling_device(tz, THERMAL_TRIPS_NONE,
577 cdev);
578 }
579 mutex_unlock(&thermal_list_lock);
580}
581
Eduardo Valentin81193e22016-11-07 21:09:17 -0800582/*
583 * Device management section: cooling devices, zones devices, and binding
584 *
585 * Set of functions provided by the thermal core for:
586 * - cooling devices lifecycle: registration, unregistration,
587 * binding, and unbinding.
588 * - thermal zone devices lifecycle: registration, unregistration,
589 * binding, and unbinding.
590 */
Zhang Rui203d3d42008-01-17 15:51:08 +0800591
Zhang Rui203d3d42008-01-17 15:51:08 +0800592/**
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000593 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
594 * @tz: pointer to struct thermal_zone_device
Zhang Rui203d3d42008-01-17 15:51:08 +0800595 * @trip: indicates which trip point the cooling devices is
596 * associated with in this thermal zone.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000597 * @cdev: pointer to struct thermal_cooling_device
598 * @upper: the Maximum cooling state for this trip point.
599 * THERMAL_NO_LIMIT means no upper limit,
600 * and the cooling device can be in max_state.
601 * @lower: the Minimum cooling state can be used for this trip point.
602 * THERMAL_NO_LIMIT means no lower limit,
603 * and the cooling device can be in cooling state 0.
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000604 * @weight: The weight of the cooling device to be bound to the
605 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
606 * default value
Len Brown543a9562008-02-07 16:55:08 -0500607 *
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000608 * This interface function bind a thermal cooling device to the certain trip
609 * point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -0500610 * This function is usually called in the thermal zone device .bind callback.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000611 *
612 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +0800613 */
614int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
615 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +0800616 struct thermal_cooling_device *cdev,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000617 unsigned long upper, unsigned long lower,
618 unsigned int weight)
Zhang Rui203d3d42008-01-17 15:51:08 +0800619{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800620 struct thermal_instance *dev;
621 struct thermal_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -0500622 struct thermal_zone_device *pos1;
623 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +0800624 unsigned long max_state;
Lukasz Majewski9a3031d2014-11-18 11:16:30 +0100625 int result, ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800626
Len Brown543a9562008-02-07 16:55:08 -0500627 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +0800628 return -EINVAL;
629
Thomas Sujithc7516702008-02-15 00:58:50 -0500630 list_for_each_entry(pos1, &thermal_tz_list, node) {
631 if (pos1 == tz)
632 break;
633 }
634 list_for_each_entry(pos2, &thermal_cdev_list, node) {
635 if (pos2 == cdev)
636 break;
637 }
638
639 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +0800640 return -EINVAL;
641
Lukasz Majewski9a3031d2014-11-18 11:16:30 +0100642 ret = cdev->ops->get_max_state(cdev, &max_state);
643 if (ret)
644 return ret;
Zhang Rui9d998422012-06-26 16:35:57 +0800645
646 /* lower default 0, upper default max_state */
647 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
648 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
649
650 if (lower > upper || upper > max_state)
651 return -EINVAL;
652
Eduardo Valentin95e3ed12016-11-07 21:09:25 -0800653 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800654 if (!dev)
655 return -ENOMEM;
656 dev->tz = tz;
657 dev->cdev = cdev;
658 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +0800659 dev->upper = upper;
660 dev->lower = lower;
Zhang Ruice119f82012-06-27 14:13:04 +0800661 dev->target = THERMAL_NO_TARGET;
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000662 dev->weight = weight;
Zhang Rui74051ba2012-06-26 16:27:22 +0800663
Matthew Wilcoxb31ef822016-12-21 09:47:03 -0800664 result = ida_simple_get(&tz->ida, 0, 0, GFP_KERNEL);
665 if (result < 0)
Zhang Rui203d3d42008-01-17 15:51:08 +0800666 goto free_mem;
667
Matthew Wilcoxb31ef822016-12-21 09:47:03 -0800668 dev->id = result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800669 sprintf(dev->name, "cdev%d", dev->id);
670 result =
671 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
672 if (result)
Matthew Wilcoxb31ef822016-12-21 09:47:03 -0800673 goto release_ida;
Zhang Rui203d3d42008-01-17 15:51:08 +0800674
675 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -0700676 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800677 dev->attr.attr.name = dev->attr_name;
678 dev->attr.attr.mode = 0444;
679 dev->attr.show = thermal_cooling_device_trip_point_show;
680 result = device_create_file(&tz->device, &dev->attr);
681 if (result)
682 goto remove_symbol_link;
683
Javi Merinodb916512015-02-18 16:04:24 +0000684 sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
685 sysfs_attr_init(&dev->weight_attr.attr);
686 dev->weight_attr.attr.name = dev->weight_attr_name;
687 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
688 dev->weight_attr.show = thermal_cooling_device_weight_show;
689 dev->weight_attr.store = thermal_cooling_device_weight_store;
690 result = device_create_file(&tz->device, &dev->weight_attr);
691 if (result)
692 goto remove_trip_file;
693
Zhang Rui203d3d42008-01-17 15:51:08 +0800694 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800695 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +0800696 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
Eduardo Valentinb659a302016-11-07 21:09:23 -0800697 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
698 result = -EEXIST;
699 break;
700 }
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800701 if (!result) {
Zhang Ruicddf31b2012-06-27 10:09:36 +0800702 list_add_tail(&dev->tz_node, &tz->thermal_instances);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800703 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
Chen Yu4511f712015-10-30 16:32:10 +0800704 atomic_set(&tz->need_update, 1);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800705 }
Zhang Ruif4a821c2012-07-24 16:56:21 +0800706 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800707 mutex_unlock(&tz->lock);
708
709 if (!result)
710 return 0;
711
Javi Merinodb916512015-02-18 16:04:24 +0000712 device_remove_file(&tz->device, &dev->weight_attr);
713remove_trip_file:
Zhang Rui203d3d42008-01-17 15:51:08 +0800714 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b802012-03-21 12:55:02 -0700715remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +0800716 sysfs_remove_link(&tz->device.kobj, dev->name);
Matthew Wilcoxb31ef822016-12-21 09:47:03 -0800717release_ida:
718 ida_simple_remove(&tz->ida, dev->id);
Joe Perchescaca8b802012-03-21 12:55:02 -0700719free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +0800720 kfree(dev);
721 return result;
722}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000723EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +0800724
725/**
Eduardo Valentin9892e5d2013-04-23 21:48:17 +0000726 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
727 * thermal zone.
728 * @tz: pointer to a struct thermal_zone_device.
Zhang Rui203d3d42008-01-17 15:51:08 +0800729 * @trip: indicates which trip point the cooling devices is
730 * associated with in this thermal zone.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +0000731 * @cdev: pointer to a struct thermal_cooling_device.
Len Brown543a9562008-02-07 16:55:08 -0500732 *
Eduardo Valentin9892e5d2013-04-23 21:48:17 +0000733 * This interface function unbind a thermal cooling device from the certain
734 * trip point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -0500735 * This function is usually called in the thermal zone device .unbind callback.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +0000736 *
737 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +0800738 */
739int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
740 int trip,
741 struct thermal_cooling_device *cdev)
742{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800743 struct thermal_instance *pos, *next;
Zhang Rui203d3d42008-01-17 15:51:08 +0800744
745 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800746 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +0800747 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
Len Brown543a9562008-02-07 16:55:08 -0500748 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Ruicddf31b2012-06-27 10:09:36 +0800749 list_del(&pos->tz_node);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800750 list_del(&pos->cdev_node);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800751 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800752 mutex_unlock(&tz->lock);
753 goto unbind;
754 }
755 }
Zhang Ruif4a821c2012-07-24 16:56:21 +0800756 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800757 mutex_unlock(&tz->lock);
758
759 return -ENODEV;
760
Joe Perchescaca8b802012-03-21 12:55:02 -0700761unbind:
Viresh Kumar528464e2015-07-23 14:32:32 +0530762 device_remove_file(&tz->device, &pos->weight_attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800763 device_remove_file(&tz->device, &pos->attr);
764 sysfs_remove_link(&tz->device.kobj, pos->name);
Matthew Wilcoxb31ef822016-12-21 09:47:03 -0800765 ida_simple_remove(&tz->ida, pos->id);
Zhang Rui203d3d42008-01-17 15:51:08 +0800766 kfree(pos);
767 return 0;
768}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000769EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +0800770
771static void thermal_release(struct device *dev)
772{
773 struct thermal_zone_device *tz;
774 struct thermal_cooling_device *cdev;
775
Joe Perchescaca8b802012-03-21 12:55:02 -0700776 if (!strncmp(dev_name(dev), "thermal_zone",
777 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +0800778 tz = to_thermal_zone(dev);
Jacob von Chorusf53345e2016-12-30 14:07:52 -0500779 kfree(tz->trip_type_attrs);
780 kfree(tz->trip_temp_attrs);
781 kfree(tz->trip_hyst_attrs);
782 kfree(tz->trips_attribute_group.attrs);
783 kfree(tz->device.groups);
Zhang Rui203d3d42008-01-17 15:51:08 +0800784 kfree(tz);
Eduardo Valentinb659a302016-11-07 21:09:23 -0800785 } else if (!strncmp(dev_name(dev), "cooling_device",
786 sizeof("cooling_device") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +0800787 cdev = to_cooling_device(dev);
788 kfree(cdev);
789 }
790}
791
792static struct class thermal_class = {
793 .name = "thermal",
794 .dev_release = thermal_release,
795};
796
Eduardo Valentin4b0d3c22016-11-07 21:09:13 -0800797static inline
798void print_bind_err_msg(struct thermal_zone_device *tz,
799 struct thermal_cooling_device *cdev, int ret)
Eduardo Valentinf502ab82016-11-07 21:09:12 -0800800{
801 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
802 tz->type, cdev->type, ret);
803}
804
805static void __bind(struct thermal_zone_device *tz, int mask,
806 struct thermal_cooling_device *cdev,
807 unsigned long *limits,
808 unsigned int weight)
809{
810 int i, ret;
811
812 for (i = 0; i < tz->trips; i++) {
813 if (mask & (1 << i)) {
814 unsigned long upper, lower;
815
816 upper = THERMAL_NO_LIMIT;
817 lower = THERMAL_NO_LIMIT;
818 if (limits) {
819 lower = limits[i * 2];
820 upper = limits[i * 2 + 1];
821 }
822 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
823 upper, lower,
824 weight);
825 if (ret)
826 print_bind_err_msg(tz, cdev, ret);
827 }
828 }
829}
830
Eduardo Valentin949aad82016-11-07 21:09:09 -0800831static void bind_cdev(struct thermal_cooling_device *cdev)
832{
833 int i, ret;
834 const struct thermal_zone_params *tzp;
835 struct thermal_zone_device *pos = NULL;
836
837 mutex_lock(&thermal_list_lock);
838
839 list_for_each_entry(pos, &thermal_tz_list, node) {
840 if (!pos->tzp && !pos->ops->bind)
841 continue;
842
843 if (pos->ops->bind) {
844 ret = pos->ops->bind(pos, cdev);
845 if (ret)
846 print_bind_err_msg(pos, cdev, ret);
847 continue;
848 }
849
850 tzp = pos->tzp;
851 if (!tzp || !tzp->tbp)
852 continue;
853
854 for (i = 0; i < tzp->num_tbps; i++) {
855 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
856 continue;
857 if (tzp->tbp[i].match(pos, cdev))
858 continue;
859 tzp->tbp[i].cdev = cdev;
860 __bind(pos, tzp->tbp[i].trip_mask, cdev,
861 tzp->tbp[i].binding_limits,
862 tzp->tbp[i].weight);
863 }
864 }
865
866 mutex_unlock(&thermal_list_lock);
867}
868
Zhang Rui203d3d42008-01-17 15:51:08 +0800869/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400870 * __thermal_cooling_device_register() - register a new thermal cooling device
871 * @np: a pointer to a device tree node.
Zhang Rui203d3d42008-01-17 15:51:08 +0800872 * @type: the thermal cooling device type.
873 * @devdata: device private data.
874 * @ops: standard thermal cooling devices callbacks.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +0000875 *
876 * This interface function adds a new thermal cooling device (fan/processor/...)
877 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
878 * to all the thermal zone devices registered at the same time.
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400879 * It also gives the opportunity to link the cooling device to a device tree
880 * node, so that it can be bound to a thermal zone created out of device tree.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +0000881 *
882 * Return: a pointer to the created struct thermal_cooling_device or an
883 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +0800884 */
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400885static struct thermal_cooling_device *
886__thermal_cooling_device_register(struct device_node *np,
887 char *type, void *devdata,
888 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +0800889{
890 struct thermal_cooling_device *cdev;
Chen Yu4511f712015-10-30 16:32:10 +0800891 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +0800892 int result;
893
Guenter Roeck204dd1d2012-08-07 22:36:45 -0700894 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500895 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800896
897 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -0500898 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500899 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800900
Eduardo Valentin95e3ed12016-11-07 21:09:25 -0800901 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800902 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500903 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +0800904
Matthew Wilcoxb31ef822016-12-21 09:47:03 -0800905 result = ida_simple_get(&thermal_cdev_ida, 0, 0, GFP_KERNEL);
906 if (result < 0) {
Zhang Rui203d3d42008-01-17 15:51:08 +0800907 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500908 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800909 }
910
Matthew Wilcoxb31ef822016-12-21 09:47:03 -0800911 cdev->id = result;
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +0000912 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
Zhang Ruif4a821c2012-07-24 16:56:21 +0800913 mutex_init(&cdev->lock);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800914 INIT_LIST_HEAD(&cdev->thermal_instances);
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400915 cdev->np = np;
Zhang Rui203d3d42008-01-17 15:51:08 +0800916 cdev->ops = ops;
Ni Wade5ca0cce2014-02-17 11:02:55 +0800917 cdev->updated = false;
Zhang Rui203d3d42008-01-17 15:51:08 +0800918 cdev->device.class = &thermal_class;
Eduardo Valentin45cf2ec2016-11-07 21:09:02 -0800919 thermal_cooling_device_setup_sysfs(cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +0800920 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -0800921 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +0800922 result = device_register(&cdev->device);
923 if (result) {
Matthew Wilcoxb31ef822016-12-21 09:47:03 -0800924 ida_simple_remove(&thermal_cdev_ida, cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +0800925 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500926 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800927 }
928
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530929 /* Add 'this' new cdev to the global cdev list */
Zhang Rui203d3d42008-01-17 15:51:08 +0800930 mutex_lock(&thermal_list_lock);
931 list_add(&cdev->node, &thermal_cdev_list);
Zhang Rui203d3d42008-01-17 15:51:08 +0800932 mutex_unlock(&thermal_list_lock);
933
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530934 /* Update binding information for 'this' new cdev */
935 bind_cdev(cdev);
936
Chen Yu4511f712015-10-30 16:32:10 +0800937 mutex_lock(&thermal_list_lock);
938 list_for_each_entry(pos, &thermal_tz_list, node)
939 if (atomic_cmpxchg(&pos->need_update, 1, 0))
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700940 thermal_zone_device_update(pos,
941 THERMAL_EVENT_UNSPECIFIED);
Chen Yu4511f712015-10-30 16:32:10 +0800942 mutex_unlock(&thermal_list_lock);
943
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530944 return cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +0800945}
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400946
947/**
948 * thermal_cooling_device_register() - register a new thermal cooling device
949 * @type: the thermal cooling device type.
950 * @devdata: device private data.
951 * @ops: standard thermal cooling devices callbacks.
952 *
953 * This interface function adds a new thermal cooling device (fan/processor/...)
954 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
955 * to all the thermal zone devices registered at the same time.
956 *
957 * Return: a pointer to the created struct thermal_cooling_device or an
958 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
959 */
960struct thermal_cooling_device *
961thermal_cooling_device_register(char *type, void *devdata,
962 const struct thermal_cooling_device_ops *ops)
963{
964 return __thermal_cooling_device_register(NULL, type, devdata, ops);
965}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000966EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +0800967
968/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400969 * thermal_of_cooling_device_register() - register an OF thermal cooling device
970 * @np: a pointer to a device tree node.
971 * @type: the thermal cooling device type.
972 * @devdata: device private data.
973 * @ops: standard thermal cooling devices callbacks.
974 *
975 * This function will register a cooling device with device tree node reference.
976 * This interface function adds a new thermal cooling device (fan/processor/...)
977 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
978 * to all the thermal zone devices registered at the same time.
979 *
980 * Return: a pointer to the created struct thermal_cooling_device or an
981 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
982 */
983struct thermal_cooling_device *
984thermal_of_cooling_device_register(struct device_node *np,
985 char *type, void *devdata,
986 const struct thermal_cooling_device_ops *ops)
987{
988 return __thermal_cooling_device_register(np, type, devdata, ops);
989}
990EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
991
Eduardo Valentinf11997f2016-11-07 21:09:08 -0800992static void __unbind(struct thermal_zone_device *tz, int mask,
993 struct thermal_cooling_device *cdev)
994{
995 int i;
996
997 for (i = 0; i < tz->trips; i++)
998 if (mask & (1 << i))
999 thermal_zone_unbind_cooling_device(tz, i, cdev);
1000}
1001
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001002/**
Eduardo Valentin38e7b542016-11-07 21:09:24 -08001003 * thermal_cooling_device_unregister - removes a thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001004 * @cdev: the thermal cooling device to remove.
1005 *
Eduardo Valentin38e7b542016-11-07 21:09:24 -08001006 * thermal_cooling_device_unregister() must be called when a registered
1007 * thermal cooling device is no longer needed.
Zhang Rui203d3d42008-01-17 15:51:08 +08001008 */
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301009void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
Zhang Rui203d3d42008-01-17 15:51:08 +08001010{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301011 int i;
1012 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001013 struct thermal_zone_device *tz;
1014 struct thermal_cooling_device *pos = NULL;
1015
1016 if (!cdev)
1017 return;
1018
1019 mutex_lock(&thermal_list_lock);
1020 list_for_each_entry(pos, &thermal_cdev_list, node)
Eduardo Valentinb659a302016-11-07 21:09:23 -08001021 if (pos == cdev)
1022 break;
Zhang Rui203d3d42008-01-17 15:51:08 +08001023 if (pos != cdev) {
1024 /* thermal cooling device not found */
1025 mutex_unlock(&thermal_list_lock);
1026 return;
1027 }
1028 list_del(&cdev->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301029
1030 /* Unbind all thermal zones associated with 'this' cdev */
Zhang Rui203d3d42008-01-17 15:51:08 +08001031 list_for_each_entry(tz, &thermal_tz_list, node) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301032 if (tz->ops->unbind) {
1033 tz->ops->unbind(tz, cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +08001034 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301035 }
1036
1037 if (!tz->tzp || !tz->tzp->tbp)
1038 continue;
1039
1040 tzp = tz->tzp;
1041 for (i = 0; i < tzp->num_tbps; i++) {
1042 if (tzp->tbp[i].cdev == cdev) {
1043 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1044 tzp->tbp[i].cdev = NULL;
1045 }
1046 }
Zhang Rui203d3d42008-01-17 15:51:08 +08001047 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301048
Zhang Rui203d3d42008-01-17 15:51:08 +08001049 mutex_unlock(&thermal_list_lock);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301050
Matthew Wilcoxb31ef822016-12-21 09:47:03 -08001051 ida_simple_remove(&thermal_cdev_ida, cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001052 device_unregister(&cdev->device);
Zhang Rui203d3d42008-01-17 15:51:08 +08001053}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001054EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001055
Eduardo Valentin90f5b5b2016-11-07 21:09:10 -08001056static void bind_tz(struct thermal_zone_device *tz)
Zhang Ruice119f82012-06-27 14:13:04 +08001057{
Eduardo Valentin90f5b5b2016-11-07 21:09:10 -08001058 int i, ret;
1059 struct thermal_cooling_device *pos = NULL;
1060 const struct thermal_zone_params *tzp = tz->tzp;
Zhang Ruice119f82012-06-27 14:13:04 +08001061
Eduardo Valentin90f5b5b2016-11-07 21:09:10 -08001062 if (!tzp && !tz->ops->bind)
Michele Di Giorgiod0b73062016-06-02 15:25:31 +01001063 return;
Eduardo Valentin90f5b5b2016-11-07 21:09:10 -08001064
1065 mutex_lock(&thermal_list_lock);
1066
1067 /* If there is ops->bind, try to use ops->bind */
1068 if (tz->ops->bind) {
1069 list_for_each_entry(pos, &thermal_cdev_list, node) {
1070 ret = tz->ops->bind(tz, pos);
1071 if (ret)
1072 print_bind_err_msg(tz, pos, ret);
1073 }
1074 goto exit;
Michele Di Giorgiod0b73062016-06-02 15:25:31 +01001075 }
1076
Eduardo Valentin90f5b5b2016-11-07 21:09:10 -08001077 if (!tzp || !tzp->tbp)
1078 goto exit;
Zhang Ruice119f82012-06-27 14:13:04 +08001079
Eduardo Valentin90f5b5b2016-11-07 21:09:10 -08001080 list_for_each_entry(pos, &thermal_cdev_list, node) {
1081 for (i = 0; i < tzp->num_tbps; i++) {
1082 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
1083 continue;
1084 if (tzp->tbp[i].match(tz, pos))
1085 continue;
1086 tzp->tbp[i].cdev = pos;
1087 __bind(tz, tzp->tbp[i].trip_mask, pos,
1088 tzp->tbp[i].binding_limits,
1089 tzp->tbp[i].weight);
Durgadoss R27365a62012-07-25 10:10:59 +08001090 }
1091 }
Eduardo Valentin90f5b5b2016-11-07 21:09:10 -08001092exit:
1093 mutex_unlock(&thermal_list_lock);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001094}
1095
1096/**
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001097 * thermal_zone_device_register() - register a new thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001098 * @type: the thermal zone device type
1099 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001100 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08001101 * @devdata: private device data
1102 * @ops: standard thermal zone device callbacks
Durgadoss R50125a92012-09-18 11:04:56 +05301103 * @tzp: thermal zone platform parameters
Matthew Garrettb1569e92008-12-03 17:55:32 +00001104 * @passive_delay: number of milliseconds to wait between polls when
1105 * performing passive cooling
1106 * @polling_delay: number of milliseconds to wait between polls when checking
1107 * whether trip points have been crossed (0 for interrupt
1108 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001109 *
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001110 * This interface function adds a new thermal zone device (sensor) to
1111 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1112 * thermal cooling devices registered at the same time.
Zhang Rui203d3d42008-01-17 15:51:08 +08001113 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001114 * longer needed. The passive cooling depends on the .get_trend() return value.
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001115 *
1116 * Return: a pointer to the created struct thermal_zone_device or an
1117 * in case of error, an ERR_PTR. Caller must check return value with
1118 * IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001119 */
Eduardo Valentineb7be322016-11-07 21:09:21 -08001120struct thermal_zone_device *
1121thermal_zone_device_register(const char *type, int trips, int mask,
1122 void *devdata, struct thermal_zone_device_ops *ops,
1123 struct thermal_zone_params *tzp, int passive_delay,
1124 int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001125{
1126 struct thermal_zone_device *tz;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001127 enum thermal_trip_type trip_type;
Zhang Rui81ad4272016-03-18 10:03:24 +08001128 int trip_temp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001129 int result;
1130 int count;
Javi Merinoe33df1d2015-02-26 19:00:27 +00001131 struct thermal_governor *governor;
Zhang Rui203d3d42008-01-17 15:51:08 +08001132
Eduardo Valentin54fa38c2016-11-07 21:08:39 -08001133 if (!type || strlen(type) == 0)
1134 return ERR_PTR(-EINVAL);
1135
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001136 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001137 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001138
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001139 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001140 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001141
Eduardo Valentin81bd4e12013-09-12 19:15:44 -04001142 if (!ops)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001143 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001144
Jonghwa Lee83720d02013-05-18 09:50:26 +00001145 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
Eduardo Valentin6b2aa512013-01-02 15:29:42 +00001146 return ERR_PTR(-EINVAL);
1147
Eduardo Valentin95e3ed12016-11-07 21:09:25 -08001148 tz = kzalloc(sizeof(*tz), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001149 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001150 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001151
Zhang Rui2d374132012-06-27 10:09:00 +08001152 INIT_LIST_HEAD(&tz->thermal_instances);
Matthew Wilcoxb31ef822016-12-21 09:47:03 -08001153 ida_init(&tz->ida);
Zhang Rui203d3d42008-01-17 15:51:08 +08001154 mutex_init(&tz->lock);
Matthew Wilcoxb31ef822016-12-21 09:47:03 -08001155 result = ida_simple_get(&thermal_tz_ida, 0, 0, GFP_KERNEL);
1156 if (result < 0) {
Zhang Rui203d3d42008-01-17 15:51:08 +08001157 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001158 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001159 }
1160
Matthew Wilcoxb31ef822016-12-21 09:47:03 -08001161 tz->id = result;
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) {
Matthew Wilcoxb31ef822016-12-21 09:47:03 -08001183 ida_simple_remove(&thermal_tz_ida, tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001184 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
Eduardo Valentinb659a302016-11-07 21:09:23 -08001227 INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001228
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 Perchescaca8b802012-03-21 12:55:02 -07001236unregister:
Matthew Wilcoxb31ef822016-12-21 09:47:03 -08001237 ida_simple_remove(&thermal_tz_ida, tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001238 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)
Eduardo Valentinb659a302016-11-07 21:09:23 -08001261 if (pos == tz)
1262 break;
Zhang Rui203d3d42008-01-17 15:51:08 +08001263 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
Javi Merinoe33df1d2015-02-26 19:00:27 +00001292 thermal_set_governor(tz, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001293
Zhang Ruie68b16a2008-04-21 16:07:52 +08001294 thermal_remove_hwmon_sysfs(tz);
Matthew Wilcoxb31ef822016-12-21 09:47:03 -08001295 ida_simple_remove(&thermal_tz_ida, tz->id);
1296 ida_destroy(&tz->ida);
Zhang Rui203d3d42008-01-17 15:51:08 +08001297 mutex_destroy(&tz->lock);
1298 device_unregister(&tz->device);
Zhang Rui203d3d42008-01-17 15:51:08 +08001299}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001300EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001301
Eduardo Valentin63c4d912013-04-05 12:32:28 +00001302/**
1303 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1304 * @name: thermal zone name to fetch the temperature
1305 *
1306 * When only one zone is found with the passed name, returns a reference to it.
1307 *
1308 * Return: On success returns a reference to an unique thermal zone with
1309 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1310 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1311 */
1312struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1313{
1314 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1315 unsigned int found = 0;
1316
1317 if (!name)
1318 goto exit;
1319
1320 mutex_lock(&thermal_list_lock);
1321 list_for_each_entry(pos, &thermal_tz_list, node)
Rasmus Villemoes484ac2f2014-10-13 15:55:01 -07001322 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
Eduardo Valentin63c4d912013-04-05 12:32:28 +00001323 found++;
1324 ref = pos;
1325 }
1326 mutex_unlock(&thermal_list_lock);
1327
1328 /* nothing has been found, thus an error code for it */
1329 if (found == 0)
1330 ref = ERR_PTR(-ENODEV);
1331 else if (found > 1)
1332 /* Success only when an unique zone is found */
1333 ref = ERR_PTR(-EEXIST);
1334
1335exit:
1336 return ref;
1337}
1338EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1339
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001340#ifdef CONFIG_NET
Johannes Berg2a94fe42013-11-19 15:19:39 +01001341static const struct genl_multicast_group thermal_event_mcgrps[] = {
1342 { .name = THERMAL_GENL_MCAST_GROUP_NAME, },
1343};
1344
Johannes Berg56989f62016-10-24 14:40:05 +02001345static struct genl_family thermal_event_genl_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +02001346 .module = THIS_MODULE,
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001347 .name = THERMAL_GENL_FAMILY_NAME,
1348 .version = THERMAL_GENL_VERSION,
1349 .maxattr = THERMAL_GENL_ATTR_MAX,
Johannes Berg2a94fe42013-11-19 15:19:39 +01001350 .mcgrps = thermal_event_mcgrps,
1351 .n_mcgrps = ARRAY_SIZE(thermal_event_mcgrps),
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001352};
1353
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001354int thermal_generate_netlink_event(struct thermal_zone_device *tz,
Eduardo Valentineb7be322016-11-07 21:09:21 -08001355 enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05301356{
1357 struct sk_buff *skb;
1358 struct nlattr *attr;
1359 struct thermal_genl_event *thermal_event;
1360 void *msg_header;
1361 int size;
1362 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07001363 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301364
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001365 if (!tz)
1366 return -EINVAL;
1367
R.Durgadoss4cb18722010-10-27 03:33:29 +05301368 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07001369 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1370 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301371
1372 skb = genlmsg_new(size, GFP_ATOMIC);
1373 if (!skb)
1374 return -ENOMEM;
1375
1376 /* add the genetlink message header */
1377 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1378 &thermal_event_genl_family, 0,
1379 THERMAL_GENL_CMD_EVENT);
1380 if (!msg_header) {
1381 nlmsg_free(skb);
1382 return -ENOMEM;
1383 }
1384
1385 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07001386 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1387 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05301388
1389 if (!attr) {
1390 nlmsg_free(skb);
1391 return -EINVAL;
1392 }
1393
1394 thermal_event = nla_data(attr);
1395 if (!thermal_event) {
1396 nlmsg_free(skb);
1397 return -EINVAL;
1398 }
1399
1400 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1401
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001402 thermal_event->orig = tz->id;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301403 thermal_event->event = event;
1404
1405 /* send multicast genetlink message */
Johannes Berg053c0952015-01-16 22:09:00 +01001406 genlmsg_end(skb, msg_header);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301407
Johannes Berg68eb5502013-11-19 15:19:38 +01001408 result = genlmsg_multicast(&thermal_event_genl_family, skb, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +01001409 0, GFP_ATOMIC);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301410 if (result)
Eduardo Valentin923e0b12013-01-02 15:29:41 +00001411 dev_err(&tz->device, "Failed to send netlink event:%d", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301412
1413 return result;
1414}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001415EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301416
Johannes Berg56989f62016-10-24 14:40:05 +02001417static int __init genetlink_init(void)
R.Durgadoss4cb18722010-10-27 03:33:29 +05301418{
Johannes Berg2a94fe42013-11-19 15:19:39 +01001419 return genl_register_family(&thermal_event_genl_family);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301420}
1421
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001422static void genetlink_exit(void)
1423{
1424 genl_unregister_family(&thermal_event_genl_family);
1425}
1426#else /* !CONFIG_NET */
1427static inline int genetlink_init(void) { return 0; }
1428static inline void genetlink_exit(void) {}
1429#endif /* !CONFIG_NET */
1430
Zhang Ruiff140fe2015-10-30 16:31:58 +08001431static int thermal_pm_notify(struct notifier_block *nb,
Eduardo Valentineb7be322016-11-07 21:09:21 -08001432 unsigned long mode, void *_unused)
Zhang Ruiff140fe2015-10-30 16:31:58 +08001433{
1434 struct thermal_zone_device *tz;
1435
1436 switch (mode) {
1437 case PM_HIBERNATION_PREPARE:
1438 case PM_RESTORE_PREPARE:
1439 case PM_SUSPEND_PREPARE:
1440 atomic_set(&in_suspend, 1);
1441 break;
1442 case PM_POST_HIBERNATION:
1443 case PM_POST_RESTORE:
1444 case PM_POST_SUSPEND:
1445 atomic_set(&in_suspend, 0);
1446 list_for_each_entry(tz, &thermal_tz_list, node) {
1447 thermal_zone_device_reset(tz);
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -07001448 thermal_zone_device_update(tz,
1449 THERMAL_EVENT_UNSPECIFIED);
Zhang Ruiff140fe2015-10-30 16:31:58 +08001450 }
1451 break;
1452 default:
1453 break;
1454 }
1455 return 0;
1456}
1457
1458static struct notifier_block thermal_pm_nb = {
1459 .notifier_call = thermal_pm_notify,
1460};
1461
Zhang Rui203d3d42008-01-17 15:51:08 +08001462static int __init thermal_init(void)
1463{
Zhang Rui80a26a52013-03-26 16:38:29 +08001464 int result;
1465
1466 result = thermal_register_governors();
1467 if (result)
1468 goto error;
Zhang Rui203d3d42008-01-17 15:51:08 +08001469
1470 result = class_register(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08001471 if (result)
1472 goto unregister_governors;
1473
R.Durgadoss4cb18722010-10-27 03:33:29 +05301474 result = genetlink_init();
Zhang Rui80a26a52013-03-26 16:38:29 +08001475 if (result)
1476 goto unregister_class;
1477
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001478 result = of_parse_thermal_zones();
1479 if (result)
1480 goto exit_netlink;
1481
Zhang Ruiff140fe2015-10-30 16:31:58 +08001482 result = register_pm_notifier(&thermal_pm_nb);
1483 if (result)
1484 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
1485 result);
1486
Zhang Rui80a26a52013-03-26 16:38:29 +08001487 return 0;
1488
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001489exit_netlink:
1490 genetlink_exit();
Zhang Rui80a26a52013-03-26 16:38:29 +08001491unregister_class:
1492 class_unregister(&thermal_class);
Luis Henriques9d367e52014-12-03 21:20:21 +00001493unregister_governors:
1494 thermal_unregister_governors();
Zhang Rui80a26a52013-03-26 16:38:29 +08001495error:
Matthew Wilcoxb31ef822016-12-21 09:47:03 -08001496 ida_destroy(&thermal_tz_ida);
1497 ida_destroy(&thermal_cdev_ida);
Zhang Rui80a26a52013-03-26 16:38:29 +08001498 mutex_destroy(&thermal_list_lock);
1499 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001500 return result;
1501}
1502
1503static void __exit thermal_exit(void)
1504{
Zhang Ruiff140fe2015-10-30 16:31:58 +08001505 unregister_pm_notifier(&thermal_pm_nb);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001506 of_thermal_destroy_zones();
Zhang Rui80a26a52013-03-26 16:38:29 +08001507 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08001508 class_unregister(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08001509 thermal_unregister_governors();
Matthew Wilcoxb31ef822016-12-21 09:47:03 -08001510 ida_destroy(&thermal_tz_ida);
1511 ida_destroy(&thermal_cdev_ida);
Zhang Rui203d3d42008-01-17 15:51:08 +08001512 mutex_destroy(&thermal_list_lock);
Zhang Rui80a26a52013-03-26 16:38:29 +08001513 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001514}
1515
R.Durgadoss4cb18722010-10-27 03:33:29 +05301516fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08001517module_exit(thermal_exit);