blob: 2b1b0ba393a4b75b827fea732ea42f510563a29e [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);
Keerthye441fd62017-04-18 09:59:58 +053048static DEFINE_MUTEX(poweroff_lock);
Durgadoss Ra4a15482012-09-18 11:04:57 +053049
Zhang Ruiff140fe2015-10-30 16:31:58 +080050static atomic_t in_suspend;
Keerthye441fd62017-04-18 09:59:58 +053051static bool power_off_triggered;
Zhang Ruiff140fe2015-10-30 16:31:58 +080052
Zhang Ruif2234bc2014-01-24 10:23:19 +080053static struct thermal_governor *def_governor;
54
Eduardo Valentin1b4f4842016-11-07 21:09:05 -080055/*
56 * Governor section: set of functions to handle thermal governors
57 *
58 * Functions to help in the life cycle of thermal governors within
59 * the thermal core and by the thermal governor code.
60 */
61
Durgadoss Ra4a15482012-09-18 11:04:57 +053062static struct thermal_governor *__find_governor(const char *name)
63{
64 struct thermal_governor *pos;
65
Zhang Ruif2234bc2014-01-24 10:23:19 +080066 if (!name || !name[0])
67 return def_governor;
68
Durgadoss Ra4a15482012-09-18 11:04:57 +053069 list_for_each_entry(pos, &thermal_governor_list, governor_list)
Rasmus Villemoes484ac2f2014-10-13 15:55:01 -070070 if (!strncasecmp(name, pos->name, THERMAL_NAME_LENGTH))
Durgadoss Ra4a15482012-09-18 11:04:57 +053071 return pos;
72
73 return NULL;
74}
75
Javi Merinoe33df1d2015-02-26 19:00:27 +000076/**
77 * bind_previous_governor() - bind the previous governor of the thermal zone
78 * @tz: a valid pointer to a struct thermal_zone_device
79 * @failed_gov_name: the name of the governor that failed to register
80 *
81 * Register the previous governor of the thermal zone after a new
82 * governor has failed to be bound.
83 */
84static void bind_previous_governor(struct thermal_zone_device *tz,
85 const char *failed_gov_name)
86{
87 if (tz->governor && tz->governor->bind_to_tz) {
88 if (tz->governor->bind_to_tz(tz)) {
89 dev_err(&tz->device,
90 "governor %s failed to bind and the previous one (%s) failed to bind again, thermal zone %s has no governor\n",
91 failed_gov_name, tz->governor->name, tz->type);
92 tz->governor = NULL;
93 }
94 }
95}
96
97/**
98 * thermal_set_governor() - Switch to another governor
99 * @tz: a valid pointer to a struct thermal_zone_device
100 * @new_gov: pointer to the new governor
101 *
102 * Change the governor of thermal zone @tz.
103 *
104 * Return: 0 on success, an error if the new governor's bind_to_tz() failed.
105 */
106static int thermal_set_governor(struct thermal_zone_device *tz,
107 struct thermal_governor *new_gov)
108{
109 int ret = 0;
110
111 if (tz->governor && tz->governor->unbind_from_tz)
112 tz->governor->unbind_from_tz(tz);
113
114 if (new_gov && new_gov->bind_to_tz) {
115 ret = new_gov->bind_to_tz(tz);
116 if (ret) {
117 bind_previous_governor(tz, new_gov->name);
118
119 return ret;
120 }
121 }
122
123 tz->governor = new_gov;
124
125 return ret;
126}
127
Durgadoss Ra4a15482012-09-18 11:04:57 +0530128int thermal_register_governor(struct thermal_governor *governor)
129{
130 int err;
131 const char *name;
132 struct thermal_zone_device *pos;
133
134 if (!governor)
135 return -EINVAL;
136
137 mutex_lock(&thermal_governor_lock);
138
139 err = -EBUSY;
Eduardo Valentin5027ba32016-11-07 21:09:20 -0800140 if (!__find_governor(governor->name)) {
Eduardo Valentineb7be322016-11-07 21:09:21 -0800141 bool match_default;
142
Durgadoss Ra4a15482012-09-18 11:04:57 +0530143 err = 0;
144 list_add(&governor->governor_list, &thermal_governor_list);
Eduardo Valentineb7be322016-11-07 21:09:21 -0800145 match_default = !strncmp(governor->name,
146 DEFAULT_THERMAL_GOVERNOR,
147 THERMAL_NAME_LENGTH);
148
149 if (!def_governor && match_default)
Zhang Ruif2234bc2014-01-24 10:23:19 +0800150 def_governor = governor;
Durgadoss Ra4a15482012-09-18 11:04:57 +0530151 }
152
153 mutex_lock(&thermal_list_lock);
154
155 list_for_each_entry(pos, &thermal_tz_list, node) {
Zhang Ruif2234bc2014-01-24 10:23:19 +0800156 /*
157 * only thermal zones with specified tz->tzp->governor_name
158 * may run with tz->govenor unset
159 */
Durgadoss Ra4a15482012-09-18 11:04:57 +0530160 if (pos->governor)
161 continue;
Zhang Ruif2234bc2014-01-24 10:23:19 +0800162
163 name = pos->tzp->governor_name;
164
Javi Merinoe33df1d2015-02-26 19:00:27 +0000165 if (!strncasecmp(name, governor->name, THERMAL_NAME_LENGTH)) {
166 int ret;
167
168 ret = thermal_set_governor(pos, governor);
169 if (ret)
170 dev_err(&pos->device,
171 "Failed to set governor %s for thermal zone %s: %d\n",
172 governor->name, pos->type, ret);
173 }
Durgadoss Ra4a15482012-09-18 11:04:57 +0530174 }
175
176 mutex_unlock(&thermal_list_lock);
177 mutex_unlock(&thermal_governor_lock);
178
179 return err;
180}
Durgadoss Ra4a15482012-09-18 11:04:57 +0530181
182void thermal_unregister_governor(struct thermal_governor *governor)
183{
184 struct thermal_zone_device *pos;
185
186 if (!governor)
187 return;
188
189 mutex_lock(&thermal_governor_lock);
190
Eduardo Valentin5027ba32016-11-07 21:09:20 -0800191 if (!__find_governor(governor->name))
Durgadoss Ra4a15482012-09-18 11:04:57 +0530192 goto exit;
193
194 mutex_lock(&thermal_list_lock);
195
196 list_for_each_entry(pos, &thermal_tz_list, node) {
Rasmus Villemoes484ac2f2014-10-13 15:55:01 -0700197 if (!strncasecmp(pos->governor->name, governor->name,
Eduardo Valentineb7be322016-11-07 21:09:21 -0800198 THERMAL_NAME_LENGTH))
Javi Merinoe33df1d2015-02-26 19:00:27 +0000199 thermal_set_governor(pos, NULL);
Durgadoss Ra4a15482012-09-18 11:04:57 +0530200 }
201
202 mutex_unlock(&thermal_list_lock);
203 list_del(&governor->governor_list);
204exit:
205 mutex_unlock(&thermal_governor_lock);
Durgadoss Ra4a15482012-09-18 11:04:57 +0530206}
Zhang Rui203d3d42008-01-17 15:51:08 +0800207
Eduardo Valentin1b4f4842016-11-07 21:09:05 -0800208int thermal_zone_device_set_policy(struct thermal_zone_device *tz,
209 char *policy)
Zhang Rui203d3d42008-01-17 15:51:08 +0800210{
Eduardo Valentin1b4f4842016-11-07 21:09:05 -0800211 struct thermal_governor *gov;
212 int ret = -EINVAL;
Zhang Rui203d3d42008-01-17 15:51:08 +0800213
Eduardo Valentin1b4f4842016-11-07 21:09:05 -0800214 mutex_lock(&thermal_governor_lock);
Durgadoss R9b4298a2012-09-18 11:04:54 +0530215 mutex_lock(&tz->lock);
Durgadoss R9b4298a2012-09-18 11:04:54 +0530216
Eduardo Valentin1b4f4842016-11-07 21:09:05 -0800217 gov = __find_governor(strim(policy));
218 if (!gov)
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530219 goto exit;
220
Eduardo Valentin1b4f4842016-11-07 21:09:05 -0800221 ret = thermal_set_governor(tz, gov);
222
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530223exit:
Eduardo Valentin1b4f4842016-11-07 21:09:05 -0800224 mutex_unlock(&tz->lock);
225 mutex_unlock(&thermal_governor_lock);
226
227 return ret;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530228}
229
Eduardo Valentin1b4f4842016-11-07 21:09:05 -0800230int thermal_build_list_of_policies(char *buf)
231{
232 struct thermal_governor *pos;
233 ssize_t count = 0;
234 ssize_t size = PAGE_SIZE;
235
236 mutex_lock(&thermal_governor_lock);
237
238 list_for_each_entry(pos, &thermal_governor_list, governor_list) {
239 size = PAGE_SIZE - count;
240 count += scnprintf(buf + count, size, "%s ", pos->name);
241 }
242 count += scnprintf(buf + count, size, "\n");
243
244 mutex_unlock(&thermal_governor_lock);
245
246 return count;
247}
248
249static int __init thermal_register_governors(void)
250{
251 int result;
252
253 result = thermal_gov_step_wise_register();
254 if (result)
255 return result;
256
257 result = thermal_gov_fair_share_register();
258 if (result)
259 return result;
260
261 result = thermal_gov_bang_bang_register();
262 if (result)
263 return result;
264
265 result = thermal_gov_user_space_register();
266 if (result)
267 return result;
268
269 return thermal_gov_power_allocator_register();
270}
271
272static void thermal_unregister_governors(void)
273{
274 thermal_gov_step_wise_unregister();
275 thermal_gov_fair_share_unregister();
276 thermal_gov_bang_bang_unregister();
277 thermal_gov_user_space_unregister();
278 thermal_gov_power_allocator_unregister();
279}
280
Eduardo Valentin8772e182016-11-07 21:09:15 -0800281/*
282 * Zone update section: main control loop applied to each zone while monitoring
283 *
284 * in polling mode. The monitoring is done using a workqueue.
285 * Same update may be done on a zone by calling thermal_zone_device_update().
286 *
287 * An update means:
288 * - Non-critical trips will invoke the governor responsible for that zone;
289 * - Hot trips will produce a notification to userspace;
290 * - Critical trip point will cause a system shutdown.
291 */
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530292static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
293 int delay)
294{
295 if (delay > 1000)
296 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
297 round_jiffies(msecs_to_jiffies(delay)));
298 else if (delay)
299 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
300 msecs_to_jiffies(delay));
301 else
302 cancel_delayed_work(&tz->poll_queue);
303}
304
305static void monitor_thermal_zone(struct thermal_zone_device *tz)
306{
307 mutex_lock(&tz->lock);
308
309 if (tz->passive)
310 thermal_zone_device_set_polling(tz, tz->passive_delay);
311 else if (tz->polling_delay)
312 thermal_zone_device_set_polling(tz, tz->polling_delay);
313 else
314 thermal_zone_device_set_polling(tz, 0);
315
316 mutex_unlock(&tz->lock);
317}
318
319static void handle_non_critical_trips(struct thermal_zone_device *tz,
Eduardo Valentineb7be322016-11-07 21:09:21 -0800320 int trip,
321 enum thermal_trip_type trip_type)
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530322{
Zhang Ruif2234bc2014-01-24 10:23:19 +0800323 tz->governor ? tz->governor->throttle(tz, trip) :
324 def_governor->throttle(tz, trip);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530325}
326
Keerthyef1d87e2017-04-18 09:59:59 +0530327/**
328 * thermal_emergency_poweroff_func - emergency poweroff work after a known delay
329 * @work: work_struct associated with the emergency poweroff function
330 *
331 * This function is called in very critical situations to force
332 * a kernel poweroff after a configurable timeout value.
333 */
334static void thermal_emergency_poweroff_func(struct work_struct *work)
335{
336 /*
337 * We have reached here after the emergency thermal shutdown
338 * Waiting period has expired. This means orderly_poweroff has
339 * not been able to shut off the system for some reason.
340 * Try to shut down the system immediately using kernel_power_off
341 * if populated
342 */
343 WARN(1, "Attempting kernel_power_off: Temperature too high\n");
344 kernel_power_off();
345
346 /*
347 * Worst of the worst case trigger emergency restart
348 */
349 WARN(1, "Attempting emergency_restart: Temperature too high\n");
350 emergency_restart();
351}
352
353static DECLARE_DELAYED_WORK(thermal_emergency_poweroff_work,
354 thermal_emergency_poweroff_func);
355
356/**
357 * thermal_emergency_poweroff - Trigger an emergency system poweroff
358 *
359 * This may be called from any critical situation to trigger a system shutdown
360 * after a known period of time. By default this is not scheduled.
361 */
Colin Ian Kingc4b379d2017-05-08 11:36:43 +0100362static void thermal_emergency_poweroff(void)
Keerthyef1d87e2017-04-18 09:59:59 +0530363{
364 int poweroff_delay_ms = CONFIG_THERMAL_EMERGENCY_POWEROFF_DELAY_MS;
365 /*
366 * poweroff_delay_ms must be a carefully profiled positive value.
367 * Its a must for thermal_emergency_poweroff_work to be scheduled
368 */
369 if (poweroff_delay_ms <= 0)
370 return;
371 schedule_delayed_work(&thermal_emergency_poweroff_work,
372 msecs_to_jiffies(poweroff_delay_ms));
373}
374
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530375static void handle_critical_trips(struct thermal_zone_device *tz,
Eduardo Valentineb7be322016-11-07 21:09:21 -0800376 int trip, enum thermal_trip_type trip_type)
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530377{
Sascha Hauer17e83512015-07-24 08:12:54 +0200378 int trip_temp;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530379
380 tz->ops->get_trip_temp(tz, trip, &trip_temp);
381
382 /* If we have not crossed the trip_temp, we do not care. */
Srinivas Pandruvada84ffe3e2014-11-12 15:43:29 -0800383 if (trip_temp <= 0 || tz->temperature < trip_temp)
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530384 return;
385
Punit Agrawal208cd822014-07-29 11:50:50 +0100386 trace_thermal_zone_trip(tz, trip, trip_type);
387
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530388 if (tz->ops->notify)
389 tz->ops->notify(tz, trip, trip_type);
390
391 if (trip_type == THERMAL_TRIP_CRITICAL) {
Eduardo Valentin923e0b12013-01-02 15:29:41 +0000392 dev_emerg(&tz->device,
Icenowy Zheng039f6cf2017-07-23 22:21:49 +0800393 "critical temperature reached (%d C), shutting down\n",
Eduardo Valentin923e0b12013-01-02 15:29:41 +0000394 tz->temperature / 1000);
Keerthye441fd62017-04-18 09:59:58 +0530395 mutex_lock(&poweroff_lock);
396 if (!power_off_triggered) {
Keerthyef1d87e2017-04-18 09:59:59 +0530397 /*
398 * Queue a backup emergency shutdown in the event of
399 * orderly_poweroff failure
400 */
401 thermal_emergency_poweroff();
Keerthye441fd62017-04-18 09:59:58 +0530402 orderly_poweroff(true);
403 power_off_triggered = true;
404 }
405 mutex_unlock(&poweroff_lock);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530406 }
407}
408
409static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
410{
411 enum thermal_trip_type type;
412
Zhang Rui81ad4272016-03-18 10:03:24 +0800413 /* Ignore disabled trip points */
414 if (test_bit(trip, &tz->trips_disabled))
415 return;
416
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530417 tz->ops->get_trip_type(tz, trip, &type);
418
419 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
420 handle_critical_trips(tz, trip, type);
421 else
422 handle_non_critical_trips(tz, trip, type);
423 /*
424 * Alright, we handled this trip successfully.
425 * So, start monitoring again.
426 */
427 monitor_thermal_zone(tz);
428}
429
430static void update_temperature(struct thermal_zone_device *tz)
431{
Sascha Hauer17e83512015-07-24 08:12:54 +0200432 int temp, ret;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530433
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000434 ret = thermal_zone_get_temp(tz, &temp);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530435 if (ret) {
Hans de Goede7e497a72015-03-21 15:02:55 +0100436 if (ret != -EAGAIN)
437 dev_warn(&tz->device,
438 "failed to read out thermal zone (%d)\n",
439 ret);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000440 return;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530441 }
442
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000443 mutex_lock(&tz->lock);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530444 tz->last_temperature = tz->temperature;
445 tz->temperature = temp;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530446 mutex_unlock(&tz->lock);
Aaron Lu06475b52013-12-02 13:54:26 +0800447
Punit Agrawal100a8fd2014-07-29 11:50:48 +0100448 trace_thermal_temperature(tz);
Zhang Ruibb431ba2015-10-30 16:31:47 +0800449 if (tz->last_temperature == THERMAL_TEMP_INVALID)
450 dev_dbg(&tz->device, "last_temperature N/A, current_temperature=%d\n",
451 tz->temperature);
452 else
453 dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n",
454 tz->last_temperature, tz->temperature);
455}
456
457static void thermal_zone_device_reset(struct thermal_zone_device *tz)
458{
459 struct thermal_instance *pos;
460
461 tz->temperature = THERMAL_TEMP_INVALID;
462 tz->passive = 0;
463 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
464 pos->initialized = false;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530465}
466
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700467void thermal_zone_device_update(struct thermal_zone_device *tz,
468 enum thermal_notify_event event)
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530469{
470 int count;
471
Zhang Ruiff140fe2015-10-30 16:31:58 +0800472 if (atomic_read(&in_suspend))
473 return;
474
Eduardo Valentin81bd4e12013-09-12 19:15:44 -0400475 if (!tz->ops->get_temp)
476 return;
477
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530478 update_temperature(tz);
479
Sascha Hauer060c0342016-06-22 16:42:01 +0800480 thermal_zone_set_trips(tz);
481
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700482 tz->notify_event = event;
483
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530484 for (count = 0; count < tz->trips; count++)
485 handle_thermal_trip(tz, count);
486}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000487EXPORT_SYMBOL_GPL(thermal_zone_device_update);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530488
Eduardo Valentin106339a2016-11-07 21:09:14 -0800489/**
490 * thermal_notify_framework - Sensor drivers use this API to notify framework
491 * @tz: thermal zone device
492 * @trip: indicates which trip point has been crossed
493 *
494 * This function handles the trip events from sensor drivers. It starts
495 * throttling the cooling devices according to the policy configured.
496 * For CRITICAL and HOT trip points, this notifies the respective drivers,
497 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
498 * The throttling policy is based on the configured platform data; if no
499 * platform data is provided, this uses the step_wise throttling policy.
500 */
501void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
502{
503 handle_thermal_trip(tz, trip);
504}
505EXPORT_SYMBOL_GPL(thermal_notify_framework);
506
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530507static void thermal_zone_device_check(struct work_struct *work)
508{
509 struct thermal_zone_device *tz = container_of(work, struct
510 thermal_zone_device,
511 poll_queue.work);
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700512 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530513}
514
Eduardo Valentin712afbd2016-11-07 21:09:16 -0800515/*
516 * Power actor section: interface to power actors to estimate power
517 *
518 * Set of functions used to interact to cooling devices that know
519 * how to estimate their devices power consumption.
520 */
Javi Merino9f382712015-03-26 15:53:02 +0000521
Javi Merino35b11d22015-02-26 19:00:28 +0000522/**
523 * power_actor_get_max_power() - get the maximum power that a cdev can consume
524 * @cdev: pointer to &thermal_cooling_device
525 * @tz: a valid thermal zone device pointer
526 * @max_power: pointer in which to store the maximum power
527 *
528 * Calculate the maximum power consumption in milliwats that the
529 * cooling device can currently consume and store it in @max_power.
530 *
531 * Return: 0 on success, -EINVAL if @cdev doesn't support the
532 * power_actor API or -E* on other error.
533 */
534int power_actor_get_max_power(struct thermal_cooling_device *cdev,
535 struct thermal_zone_device *tz, u32 *max_power)
536{
537 if (!cdev_is_power_actor(cdev))
538 return -EINVAL;
539
540 return cdev->ops->state2power(cdev, tz, 0, max_power);
541}
542
543/**
Javi Merinoc973c3b2015-09-14 14:23:50 +0100544 * power_actor_get_min_power() - get the mainimum power that a cdev can consume
545 * @cdev: pointer to &thermal_cooling_device
546 * @tz: a valid thermal zone device pointer
547 * @min_power: pointer in which to store the minimum power
548 *
549 * Calculate the minimum power consumption in milliwatts that the
550 * cooling device can currently consume and store it in @min_power.
551 *
552 * Return: 0 on success, -EINVAL if @cdev doesn't support the
553 * power_actor API or -E* on other error.
554 */
555int power_actor_get_min_power(struct thermal_cooling_device *cdev,
556 struct thermal_zone_device *tz, u32 *min_power)
557{
558 unsigned long max_state;
559 int ret;
560
561 if (!cdev_is_power_actor(cdev))
562 return -EINVAL;
563
564 ret = cdev->ops->get_max_state(cdev, &max_state);
565 if (ret)
566 return ret;
567
568 return cdev->ops->state2power(cdev, tz, max_state, min_power);
569}
570
571/**
Eduardo Valentin1a7e7cc2016-11-07 21:08:47 -0800572 * power_actor_set_power() - limit the maximum power a cooling device consumes
Javi Merino35b11d22015-02-26 19:00:28 +0000573 * @cdev: pointer to &thermal_cooling_device
574 * @instance: thermal instance to update
575 * @power: the power in milliwatts
576 *
Eduardo Valentin1a7e7cc2016-11-07 21:08:47 -0800577 * Set the cooling device to consume at most @power milliwatts. The limit is
578 * expected to be a cap at the maximum power consumption.
Javi Merino35b11d22015-02-26 19:00:28 +0000579 *
580 * Return: 0 on success, -EINVAL if the cooling device does not
581 * implement the power actor API or -E* for other failures.
582 */
583int power_actor_set_power(struct thermal_cooling_device *cdev,
584 struct thermal_instance *instance, u32 power)
585{
586 unsigned long state;
587 int ret;
588
589 if (!cdev_is_power_actor(cdev))
590 return -EINVAL;
591
592 ret = cdev->ops->power2state(cdev, instance->tz, power, &state);
593 if (ret)
594 return ret;
595
596 instance->target = state;
Michele Di Giorgiod0b73062016-06-02 15:25:31 +0100597 mutex_lock(&cdev->lock);
Javi Merino35b11d22015-02-26 19:00:28 +0000598 cdev->updated = false;
Michele Di Giorgiod0b73062016-06-02 15:25:31 +0100599 mutex_unlock(&cdev->lock);
Javi Merino35b11d22015-02-26 19:00:28 +0000600 thermal_cdev_update(cdev);
601
602 return 0;
603}
604
Eduardo Valentin3d0055d2016-11-07 21:08:54 -0800605void thermal_zone_device_rebind_exception(struct thermal_zone_device *tz,
606 const char *cdev_type, size_t size)
Zhang Rui203d3d42008-01-17 15:51:08 +0800607{
Eduardo Valentin3d0055d2016-11-07 21:08:54 -0800608 struct thermal_cooling_device *cdev = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +0800609
Eduardo Valentin3d0055d2016-11-07 21:08:54 -0800610 mutex_lock(&thermal_list_lock);
611 list_for_each_entry(cdev, &thermal_cdev_list, node) {
612 /* skip non matching cdevs */
613 if (strncmp(cdev_type, cdev->type, size))
614 continue;
615
616 /* re binding the exception matching the type pattern */
617 thermal_zone_bind_cooling_device(tz, THERMAL_TRIPS_NONE, cdev,
618 THERMAL_NO_LIMIT,
619 THERMAL_NO_LIMIT,
620 THERMAL_WEIGHT_DEFAULT);
621 }
622 mutex_unlock(&thermal_list_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800623}
624
Eduardo Valentin3d0055d2016-11-07 21:08:54 -0800625void thermal_zone_device_unbind_exception(struct thermal_zone_device *tz,
626 const char *cdev_type, size_t size)
Zhang Rui203d3d42008-01-17 15:51:08 +0800627{
Eduardo Valentin3d0055d2016-11-07 21:08:54 -0800628 struct thermal_cooling_device *cdev = NULL;
629
630 mutex_lock(&thermal_list_lock);
631 list_for_each_entry(cdev, &thermal_cdev_list, node) {
632 /* skip non matching cdevs */
633 if (strncmp(cdev_type, cdev->type, size))
634 continue;
635 /* unbinding the exception matching the type pattern */
636 thermal_zone_unbind_cooling_device(tz, THERMAL_TRIPS_NONE,
637 cdev);
638 }
639 mutex_unlock(&thermal_list_lock);
640}
641
Eduardo Valentin81193e22016-11-07 21:09:17 -0800642/*
643 * Device management section: cooling devices, zones devices, and binding
644 *
645 * Set of functions provided by the thermal core for:
646 * - cooling devices lifecycle: registration, unregistration,
647 * binding, and unbinding.
648 * - thermal zone devices lifecycle: registration, unregistration,
649 * binding, and unbinding.
650 */
Zhang Rui203d3d42008-01-17 15:51:08 +0800651
Zhang Rui203d3d42008-01-17 15:51:08 +0800652/**
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000653 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
654 * @tz: pointer to struct thermal_zone_device
Zhang Rui203d3d42008-01-17 15:51:08 +0800655 * @trip: indicates which trip point the cooling devices is
656 * associated with in this thermal zone.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000657 * @cdev: pointer to struct thermal_cooling_device
658 * @upper: the Maximum cooling state for this trip point.
659 * THERMAL_NO_LIMIT means no upper limit,
660 * and the cooling device can be in max_state.
661 * @lower: the Minimum cooling state can be used for this trip point.
662 * THERMAL_NO_LIMIT means no lower limit,
663 * and the cooling device can be in cooling state 0.
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000664 * @weight: The weight of the cooling device to be bound to the
665 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
666 * default value
Len Brown543a9562008-02-07 16:55:08 -0500667 *
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000668 * This interface function bind a thermal cooling device to the certain trip
669 * point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -0500670 * This function is usually called in the thermal zone device .bind callback.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000671 *
672 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +0800673 */
674int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
675 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +0800676 struct thermal_cooling_device *cdev,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000677 unsigned long upper, unsigned long lower,
678 unsigned int weight)
Zhang Rui203d3d42008-01-17 15:51:08 +0800679{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800680 struct thermal_instance *dev;
681 struct thermal_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -0500682 struct thermal_zone_device *pos1;
683 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +0800684 unsigned long max_state;
Lukasz Majewski9a3031d2014-11-18 11:16:30 +0100685 int result, ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800686
Len Brown543a9562008-02-07 16:55:08 -0500687 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +0800688 return -EINVAL;
689
Thomas Sujithc7516702008-02-15 00:58:50 -0500690 list_for_each_entry(pos1, &thermal_tz_list, node) {
691 if (pos1 == tz)
692 break;
693 }
694 list_for_each_entry(pos2, &thermal_cdev_list, node) {
695 if (pos2 == cdev)
696 break;
697 }
698
699 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +0800700 return -EINVAL;
701
Lukasz Majewski9a3031d2014-11-18 11:16:30 +0100702 ret = cdev->ops->get_max_state(cdev, &max_state);
703 if (ret)
704 return ret;
Zhang Rui9d998422012-06-26 16:35:57 +0800705
706 /* lower default 0, upper default max_state */
707 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
708 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
709
710 if (lower > upper || upper > max_state)
711 return -EINVAL;
712
Eduardo Valentin95e3ed12016-11-07 21:09:25 -0800713 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800714 if (!dev)
715 return -ENOMEM;
716 dev->tz = tz;
717 dev->cdev = cdev;
718 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +0800719 dev->upper = upper;
720 dev->lower = lower;
Zhang Ruice119f82012-06-27 14:13:04 +0800721 dev->target = THERMAL_NO_TARGET;
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000722 dev->weight = weight;
Zhang Rui74051ba2012-06-26 16:27:22 +0800723
Matthew Wilcoxb31ef822016-12-21 09:47:03 -0800724 result = ida_simple_get(&tz->ida, 0, 0, GFP_KERNEL);
725 if (result < 0)
Zhang Rui203d3d42008-01-17 15:51:08 +0800726 goto free_mem;
727
Matthew Wilcoxb31ef822016-12-21 09:47:03 -0800728 dev->id = result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800729 sprintf(dev->name, "cdev%d", dev->id);
730 result =
731 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
732 if (result)
Matthew Wilcoxb31ef822016-12-21 09:47:03 -0800733 goto release_ida;
Zhang Rui203d3d42008-01-17 15:51:08 +0800734
735 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -0700736 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800737 dev->attr.attr.name = dev->attr_name;
738 dev->attr.attr.mode = 0444;
739 dev->attr.show = thermal_cooling_device_trip_point_show;
740 result = device_create_file(&tz->device, &dev->attr);
741 if (result)
742 goto remove_symbol_link;
743
Javi Merinodb916512015-02-18 16:04:24 +0000744 sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
745 sysfs_attr_init(&dev->weight_attr.attr);
746 dev->weight_attr.attr.name = dev->weight_attr_name;
747 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
748 dev->weight_attr.show = thermal_cooling_device_weight_show;
749 dev->weight_attr.store = thermal_cooling_device_weight_store;
750 result = device_create_file(&tz->device, &dev->weight_attr);
751 if (result)
752 goto remove_trip_file;
753
Zhang Rui203d3d42008-01-17 15:51:08 +0800754 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800755 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +0800756 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
Eduardo Valentinb659a302016-11-07 21:09:23 -0800757 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
758 result = -EEXIST;
759 break;
760 }
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800761 if (!result) {
Zhang Ruicddf31b2012-06-27 10:09:36 +0800762 list_add_tail(&dev->tz_node, &tz->thermal_instances);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800763 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
Chen Yu4511f712015-10-30 16:32:10 +0800764 atomic_set(&tz->need_update, 1);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800765 }
Zhang Ruif4a821c2012-07-24 16:56:21 +0800766 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800767 mutex_unlock(&tz->lock);
768
769 if (!result)
770 return 0;
771
Javi Merinodb916512015-02-18 16:04:24 +0000772 device_remove_file(&tz->device, &dev->weight_attr);
773remove_trip_file:
Zhang Rui203d3d42008-01-17 15:51:08 +0800774 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b802012-03-21 12:55:02 -0700775remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +0800776 sysfs_remove_link(&tz->device.kobj, dev->name);
Matthew Wilcoxb31ef822016-12-21 09:47:03 -0800777release_ida:
778 ida_simple_remove(&tz->ida, dev->id);
Joe Perchescaca8b802012-03-21 12:55:02 -0700779free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +0800780 kfree(dev);
781 return result;
782}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000783EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +0800784
785/**
Eduardo Valentin9892e5d2013-04-23 21:48:17 +0000786 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
787 * thermal zone.
788 * @tz: pointer to a struct thermal_zone_device.
Zhang Rui203d3d42008-01-17 15:51:08 +0800789 * @trip: indicates which trip point the cooling devices is
790 * associated with in this thermal zone.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +0000791 * @cdev: pointer to a struct thermal_cooling_device.
Len Brown543a9562008-02-07 16:55:08 -0500792 *
Eduardo Valentin9892e5d2013-04-23 21:48:17 +0000793 * This interface function unbind a thermal cooling device from the certain
794 * trip point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -0500795 * This function is usually called in the thermal zone device .unbind callback.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +0000796 *
797 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +0800798 */
799int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
800 int trip,
801 struct thermal_cooling_device *cdev)
802{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800803 struct thermal_instance *pos, *next;
Zhang Rui203d3d42008-01-17 15:51:08 +0800804
805 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800806 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +0800807 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
Len Brown543a9562008-02-07 16:55:08 -0500808 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Ruicddf31b2012-06-27 10:09:36 +0800809 list_del(&pos->tz_node);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800810 list_del(&pos->cdev_node);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800811 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800812 mutex_unlock(&tz->lock);
813 goto unbind;
814 }
815 }
Zhang Ruif4a821c2012-07-24 16:56:21 +0800816 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800817 mutex_unlock(&tz->lock);
818
819 return -ENODEV;
820
Joe Perchescaca8b802012-03-21 12:55:02 -0700821unbind:
Viresh Kumar528464e2015-07-23 14:32:32 +0530822 device_remove_file(&tz->device, &pos->weight_attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800823 device_remove_file(&tz->device, &pos->attr);
824 sysfs_remove_link(&tz->device.kobj, pos->name);
Matthew Wilcoxb31ef822016-12-21 09:47:03 -0800825 ida_simple_remove(&tz->ida, pos->id);
Zhang Rui203d3d42008-01-17 15:51:08 +0800826 kfree(pos);
827 return 0;
828}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000829EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +0800830
831static void thermal_release(struct device *dev)
832{
833 struct thermal_zone_device *tz;
834 struct thermal_cooling_device *cdev;
835
Joe Perchescaca8b802012-03-21 12:55:02 -0700836 if (!strncmp(dev_name(dev), "thermal_zone",
837 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +0800838 tz = to_thermal_zone(dev);
Christophe Jaillet6a6cd252017-08-08 16:39:53 +0200839 thermal_zone_destroy_device_groups(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +0800840 kfree(tz);
Eduardo Valentinb659a302016-11-07 21:09:23 -0800841 } else if (!strncmp(dev_name(dev), "cooling_device",
842 sizeof("cooling_device") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +0800843 cdev = to_cooling_device(dev);
844 kfree(cdev);
845 }
846}
847
848static struct class thermal_class = {
849 .name = "thermal",
850 .dev_release = thermal_release,
851};
852
Eduardo Valentin4b0d3c22016-11-07 21:09:13 -0800853static inline
854void print_bind_err_msg(struct thermal_zone_device *tz,
855 struct thermal_cooling_device *cdev, int ret)
Eduardo Valentinf502ab82016-11-07 21:09:12 -0800856{
857 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
858 tz->type, cdev->type, ret);
859}
860
861static void __bind(struct thermal_zone_device *tz, int mask,
862 struct thermal_cooling_device *cdev,
863 unsigned long *limits,
864 unsigned int weight)
865{
866 int i, ret;
867
868 for (i = 0; i < tz->trips; i++) {
869 if (mask & (1 << i)) {
870 unsigned long upper, lower;
871
872 upper = THERMAL_NO_LIMIT;
873 lower = THERMAL_NO_LIMIT;
874 if (limits) {
875 lower = limits[i * 2];
876 upper = limits[i * 2 + 1];
877 }
878 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
879 upper, lower,
880 weight);
881 if (ret)
882 print_bind_err_msg(tz, cdev, ret);
883 }
884 }
885}
886
Eduardo Valentin949aad82016-11-07 21:09:09 -0800887static void bind_cdev(struct thermal_cooling_device *cdev)
888{
889 int i, ret;
890 const struct thermal_zone_params *tzp;
891 struct thermal_zone_device *pos = NULL;
892
893 mutex_lock(&thermal_list_lock);
894
895 list_for_each_entry(pos, &thermal_tz_list, node) {
896 if (!pos->tzp && !pos->ops->bind)
897 continue;
898
899 if (pos->ops->bind) {
900 ret = pos->ops->bind(pos, cdev);
901 if (ret)
902 print_bind_err_msg(pos, cdev, ret);
903 continue;
904 }
905
906 tzp = pos->tzp;
907 if (!tzp || !tzp->tbp)
908 continue;
909
910 for (i = 0; i < tzp->num_tbps; i++) {
911 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
912 continue;
913 if (tzp->tbp[i].match(pos, cdev))
914 continue;
915 tzp->tbp[i].cdev = cdev;
916 __bind(pos, tzp->tbp[i].trip_mask, cdev,
917 tzp->tbp[i].binding_limits,
918 tzp->tbp[i].weight);
919 }
920 }
921
922 mutex_unlock(&thermal_list_lock);
923}
924
Zhang Rui203d3d42008-01-17 15:51:08 +0800925/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400926 * __thermal_cooling_device_register() - register a new thermal cooling device
927 * @np: a pointer to a device tree node.
Zhang Rui203d3d42008-01-17 15:51:08 +0800928 * @type: the thermal cooling device type.
929 * @devdata: device private data.
930 * @ops: standard thermal cooling devices callbacks.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +0000931 *
932 * This interface function adds a new thermal cooling device (fan/processor/...)
933 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
934 * to all the thermal zone devices registered at the same time.
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400935 * It also gives the opportunity to link the cooling device to a device tree
936 * node, so that it can be bound to a thermal zone created out of device tree.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +0000937 *
938 * Return: a pointer to the created struct thermal_cooling_device or an
939 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +0800940 */
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400941static struct thermal_cooling_device *
942__thermal_cooling_device_register(struct device_node *np,
943 char *type, void *devdata,
944 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +0800945{
946 struct thermal_cooling_device *cdev;
Chen Yu4511f712015-10-30 16:32:10 +0800947 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +0800948 int result;
949
Guenter Roeck204dd1d2012-08-07 22:36:45 -0700950 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500951 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800952
953 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -0500954 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500955 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800956
Eduardo Valentin95e3ed12016-11-07 21:09:25 -0800957 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800958 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500959 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +0800960
Matthew Wilcoxb31ef822016-12-21 09:47:03 -0800961 result = ida_simple_get(&thermal_cdev_ida, 0, 0, GFP_KERNEL);
962 if (result < 0) {
Zhang Rui203d3d42008-01-17 15:51:08 +0800963 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500964 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800965 }
966
Matthew Wilcoxb31ef822016-12-21 09:47:03 -0800967 cdev->id = result;
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +0000968 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
Zhang Ruif4a821c2012-07-24 16:56:21 +0800969 mutex_init(&cdev->lock);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800970 INIT_LIST_HEAD(&cdev->thermal_instances);
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400971 cdev->np = np;
Zhang Rui203d3d42008-01-17 15:51:08 +0800972 cdev->ops = ops;
Ni Wade5ca0cce2014-02-17 11:02:55 +0800973 cdev->updated = false;
Zhang Rui203d3d42008-01-17 15:51:08 +0800974 cdev->device.class = &thermal_class;
Eduardo Valentin45cf2ec2016-11-07 21:09:02 -0800975 thermal_cooling_device_setup_sysfs(cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +0800976 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -0800977 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +0800978 result = device_register(&cdev->device);
979 if (result) {
Matthew Wilcoxb31ef822016-12-21 09:47:03 -0800980 ida_simple_remove(&thermal_cdev_ida, cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +0800981 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500982 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800983 }
984
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530985 /* Add 'this' new cdev to the global cdev list */
Zhang Rui203d3d42008-01-17 15:51:08 +0800986 mutex_lock(&thermal_list_lock);
987 list_add(&cdev->node, &thermal_cdev_list);
Zhang Rui203d3d42008-01-17 15:51:08 +0800988 mutex_unlock(&thermal_list_lock);
989
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530990 /* Update binding information for 'this' new cdev */
991 bind_cdev(cdev);
992
Chen Yu4511f712015-10-30 16:32:10 +0800993 mutex_lock(&thermal_list_lock);
994 list_for_each_entry(pos, &thermal_tz_list, node)
995 if (atomic_cmpxchg(&pos->need_update, 1, 0))
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700996 thermal_zone_device_update(pos,
997 THERMAL_EVENT_UNSPECIFIED);
Chen Yu4511f712015-10-30 16:32:10 +0800998 mutex_unlock(&thermal_list_lock);
999
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301000 return cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001001}
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001002
1003/**
1004 * thermal_cooling_device_register() - register a new thermal cooling device
1005 * @type: the thermal cooling device type.
1006 * @devdata: device private data.
1007 * @ops: standard thermal cooling devices callbacks.
1008 *
1009 * This interface function adds a new thermal cooling device (fan/processor/...)
1010 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1011 * to all the thermal zone devices registered at the same time.
1012 *
1013 * Return: a pointer to the created struct thermal_cooling_device or an
1014 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1015 */
1016struct thermal_cooling_device *
1017thermal_cooling_device_register(char *type, void *devdata,
1018 const struct thermal_cooling_device_ops *ops)
1019{
1020 return __thermal_cooling_device_register(NULL, type, devdata, ops);
1021}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001022EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001023
1024/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001025 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1026 * @np: a pointer to a device tree node.
1027 * @type: the thermal cooling device type.
1028 * @devdata: device private data.
1029 * @ops: standard thermal cooling devices callbacks.
1030 *
1031 * This function will register a cooling device with device tree node reference.
1032 * This interface function adds a new thermal cooling device (fan/processor/...)
1033 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1034 * to all the thermal zone devices registered at the same time.
1035 *
1036 * Return: a pointer to the created struct thermal_cooling_device or an
1037 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1038 */
1039struct thermal_cooling_device *
1040thermal_of_cooling_device_register(struct device_node *np,
1041 char *type, void *devdata,
1042 const struct thermal_cooling_device_ops *ops)
1043{
1044 return __thermal_cooling_device_register(np, type, devdata, ops);
1045}
1046EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1047
Eduardo Valentinf11997f2016-11-07 21:09:08 -08001048static void __unbind(struct thermal_zone_device *tz, int mask,
1049 struct thermal_cooling_device *cdev)
1050{
1051 int i;
1052
1053 for (i = 0; i < tz->trips; i++)
1054 if (mask & (1 << i))
1055 thermal_zone_unbind_cooling_device(tz, i, cdev);
1056}
1057
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001058/**
Eduardo Valentin38e7b542016-11-07 21:09:24 -08001059 * thermal_cooling_device_unregister - removes a thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001060 * @cdev: the thermal cooling device to remove.
1061 *
Eduardo Valentin38e7b542016-11-07 21:09:24 -08001062 * thermal_cooling_device_unregister() must be called when a registered
1063 * thermal cooling device is no longer needed.
Zhang Rui203d3d42008-01-17 15:51:08 +08001064 */
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301065void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
Zhang Rui203d3d42008-01-17 15:51:08 +08001066{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301067 int i;
1068 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001069 struct thermal_zone_device *tz;
1070 struct thermal_cooling_device *pos = NULL;
1071
1072 if (!cdev)
1073 return;
1074
1075 mutex_lock(&thermal_list_lock);
1076 list_for_each_entry(pos, &thermal_cdev_list, node)
Eduardo Valentinb659a302016-11-07 21:09:23 -08001077 if (pos == cdev)
1078 break;
Zhang Rui203d3d42008-01-17 15:51:08 +08001079 if (pos != cdev) {
1080 /* thermal cooling device not found */
1081 mutex_unlock(&thermal_list_lock);
1082 return;
1083 }
1084 list_del(&cdev->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301085
1086 /* Unbind all thermal zones associated with 'this' cdev */
Zhang Rui203d3d42008-01-17 15:51:08 +08001087 list_for_each_entry(tz, &thermal_tz_list, node) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301088 if (tz->ops->unbind) {
1089 tz->ops->unbind(tz, cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +08001090 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301091 }
1092
1093 if (!tz->tzp || !tz->tzp->tbp)
1094 continue;
1095
1096 tzp = tz->tzp;
1097 for (i = 0; i < tzp->num_tbps; i++) {
1098 if (tzp->tbp[i].cdev == cdev) {
1099 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1100 tzp->tbp[i].cdev = NULL;
1101 }
1102 }
Zhang Rui203d3d42008-01-17 15:51:08 +08001103 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301104
Zhang Rui203d3d42008-01-17 15:51:08 +08001105 mutex_unlock(&thermal_list_lock);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301106
Matthew Wilcoxb31ef822016-12-21 09:47:03 -08001107 ida_simple_remove(&thermal_cdev_ida, cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001108 device_unregister(&cdev->device);
Zhang Rui203d3d42008-01-17 15:51:08 +08001109}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001110EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001111
Eduardo Valentin90f5b5b2016-11-07 21:09:10 -08001112static void bind_tz(struct thermal_zone_device *tz)
Zhang Ruice119f82012-06-27 14:13:04 +08001113{
Eduardo Valentin90f5b5b2016-11-07 21:09:10 -08001114 int i, ret;
1115 struct thermal_cooling_device *pos = NULL;
1116 const struct thermal_zone_params *tzp = tz->tzp;
Zhang Ruice119f82012-06-27 14:13:04 +08001117
Eduardo Valentin90f5b5b2016-11-07 21:09:10 -08001118 if (!tzp && !tz->ops->bind)
Michele Di Giorgiod0b73062016-06-02 15:25:31 +01001119 return;
Eduardo Valentin90f5b5b2016-11-07 21:09:10 -08001120
1121 mutex_lock(&thermal_list_lock);
1122
1123 /* If there is ops->bind, try to use ops->bind */
1124 if (tz->ops->bind) {
1125 list_for_each_entry(pos, &thermal_cdev_list, node) {
1126 ret = tz->ops->bind(tz, pos);
1127 if (ret)
1128 print_bind_err_msg(tz, pos, ret);
1129 }
1130 goto exit;
Michele Di Giorgiod0b73062016-06-02 15:25:31 +01001131 }
1132
Eduardo Valentin90f5b5b2016-11-07 21:09:10 -08001133 if (!tzp || !tzp->tbp)
1134 goto exit;
Zhang Ruice119f82012-06-27 14:13:04 +08001135
Eduardo Valentin90f5b5b2016-11-07 21:09:10 -08001136 list_for_each_entry(pos, &thermal_cdev_list, node) {
1137 for (i = 0; i < tzp->num_tbps; i++) {
1138 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
1139 continue;
1140 if (tzp->tbp[i].match(tz, pos))
1141 continue;
1142 tzp->tbp[i].cdev = pos;
1143 __bind(tz, tzp->tbp[i].trip_mask, pos,
1144 tzp->tbp[i].binding_limits,
1145 tzp->tbp[i].weight);
Durgadoss R27365a62012-07-25 10:10:59 +08001146 }
1147 }
Eduardo Valentin90f5b5b2016-11-07 21:09:10 -08001148exit:
1149 mutex_unlock(&thermal_list_lock);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001150}
1151
1152/**
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001153 * thermal_zone_device_register() - register a new thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001154 * @type: the thermal zone device type
1155 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001156 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08001157 * @devdata: private device data
1158 * @ops: standard thermal zone device callbacks
Durgadoss R50125a92012-09-18 11:04:56 +05301159 * @tzp: thermal zone platform parameters
Matthew Garrettb1569e92008-12-03 17:55:32 +00001160 * @passive_delay: number of milliseconds to wait between polls when
1161 * performing passive cooling
1162 * @polling_delay: number of milliseconds to wait between polls when checking
1163 * whether trip points have been crossed (0 for interrupt
1164 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001165 *
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001166 * This interface function adds a new thermal zone device (sensor) to
1167 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1168 * thermal cooling devices registered at the same time.
Zhang Rui203d3d42008-01-17 15:51:08 +08001169 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001170 * longer needed. The passive cooling depends on the .get_trend() return value.
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001171 *
1172 * Return: a pointer to the created struct thermal_zone_device or an
1173 * in case of error, an ERR_PTR. Caller must check return value with
1174 * IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001175 */
Eduardo Valentineb7be322016-11-07 21:09:21 -08001176struct thermal_zone_device *
1177thermal_zone_device_register(const char *type, int trips, int mask,
1178 void *devdata, struct thermal_zone_device_ops *ops,
1179 struct thermal_zone_params *tzp, int passive_delay,
1180 int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001181{
1182 struct thermal_zone_device *tz;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001183 enum thermal_trip_type trip_type;
Zhang Rui81ad4272016-03-18 10:03:24 +08001184 int trip_temp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001185 int result;
1186 int count;
Javi Merinoe33df1d2015-02-26 19:00:27 +00001187 struct thermal_governor *governor;
Zhang Rui203d3d42008-01-17 15:51:08 +08001188
Eduardo Valentin54fa38c2016-11-07 21:08:39 -08001189 if (!type || strlen(type) == 0)
1190 return ERR_PTR(-EINVAL);
1191
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001192 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001193 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001194
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001195 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001196 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001197
Eduardo Valentin81bd4e12013-09-12 19:15:44 -04001198 if (!ops)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001199 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001200
Jonghwa Lee83720d02013-05-18 09:50:26 +00001201 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
Eduardo Valentin6b2aa512013-01-02 15:29:42 +00001202 return ERR_PTR(-EINVAL);
1203
Eduardo Valentin95e3ed12016-11-07 21:09:25 -08001204 tz = kzalloc(sizeof(*tz), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001205 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001206 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001207
Zhang Rui2d374132012-06-27 10:09:00 +08001208 INIT_LIST_HEAD(&tz->thermal_instances);
Matthew Wilcoxb31ef822016-12-21 09:47:03 -08001209 ida_init(&tz->ida);
Zhang Rui203d3d42008-01-17 15:51:08 +08001210 mutex_init(&tz->lock);
Matthew Wilcoxb31ef822016-12-21 09:47:03 -08001211 result = ida_simple_get(&thermal_tz_ida, 0, 0, GFP_KERNEL);
Christophe Jaillet9d9ca1f2017-08-08 16:39:54 +02001212 if (result < 0)
1213 goto free_tz;
Zhang Rui203d3d42008-01-17 15:51:08 +08001214
Matthew Wilcoxb31ef822016-12-21 09:47:03 -08001215 tz->id = result;
Eduardo Valentin54fa38c2016-11-07 21:08:39 -08001216 strlcpy(tz->type, type, sizeof(tz->type));
Zhang Rui203d3d42008-01-17 15:51:08 +08001217 tz->ops = ops;
Durgadoss R50125a92012-09-18 11:04:56 +05301218 tz->tzp = tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001219 tz->device.class = &thermal_class;
1220 tz->devdata = devdata;
1221 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001222 tz->passive_delay = passive_delay;
1223 tz->polling_delay = polling_delay;
Eduardo Valentin1c600862016-11-07 21:08:42 -08001224
Eduardo Valentin4d0fe742016-11-07 21:08:52 -08001225 /* sys I/F */
Eduardo Valentin1c600862016-11-07 21:08:42 -08001226 /* Add nodes that are always present via .groups */
Eduardo Valentin4d0fe742016-11-07 21:08:52 -08001227 result = thermal_zone_create_device_groups(tz, mask);
1228 if (result)
Christophe Jaillet9d9ca1f2017-08-08 16:39:54 +02001229 goto remove_id;
Eduardo Valentin4d0fe742016-11-07 21:08:52 -08001230
Chen Yu4511f712015-10-30 16:32:10 +08001231 /* A new thermal zone needs to be updated anyway. */
1232 atomic_set(&tz->need_update, 1);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001233
Kay Sievers354655e2009-01-06 10:44:37 -08001234 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001235 result = device_register(&tz->device);
Christophe Jaillet9d9ca1f2017-08-08 16:39:54 +02001236 if (result)
1237 goto remove_device_groups;
Zhang Rui203d3d42008-01-17 15:51:08 +08001238
Zhang Rui203d3d42008-01-17 15:51:08 +08001239 for (count = 0; count < trips; count++) {
Zhang Rui81ad4272016-03-18 10:03:24 +08001240 if (tz->ops->get_trip_type(tz, count, &trip_type))
1241 set_bit(count, &tz->trips_disabled);
Zhang Rui81ad4272016-03-18 10:03:24 +08001242 if (tz->ops->get_trip_temp(tz, count, &trip_temp))
1243 set_bit(count, &tz->trips_disabled);
1244 /* Check for bogus trip points */
1245 if (trip_temp == 0)
1246 set_bit(count, &tz->trips_disabled);
Zhang Rui203d3d42008-01-17 15:51:08 +08001247 }
1248
Durgadoss Ra4a15482012-09-18 11:04:57 +05301249 /* Update 'this' zone's governor information */
1250 mutex_lock(&thermal_governor_lock);
1251
1252 if (tz->tzp)
Javi Merinoe33df1d2015-02-26 19:00:27 +00001253 governor = __find_governor(tz->tzp->governor_name);
Durgadoss Ra4a15482012-09-18 11:04:57 +05301254 else
Javi Merinoe33df1d2015-02-26 19:00:27 +00001255 governor = def_governor;
1256
1257 result = thermal_set_governor(tz, governor);
1258 if (result) {
1259 mutex_unlock(&thermal_governor_lock);
1260 goto unregister;
1261 }
Durgadoss Ra4a15482012-09-18 11:04:57 +05301262
1263 mutex_unlock(&thermal_governor_lock);
1264
Eduardo Valentinccba4ff2013-08-15 11:34:17 -04001265 if (!tz->tzp || !tz->tzp->no_hwmon) {
1266 result = thermal_add_hwmon_sysfs(tz);
1267 if (result)
1268 goto unregister;
1269 }
Zhang Ruie68b16a2008-04-21 16:07:52 +08001270
Zhang Rui203d3d42008-01-17 15:51:08 +08001271 mutex_lock(&thermal_list_lock);
1272 list_add_tail(&tz->node, &thermal_tz_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001273 mutex_unlock(&thermal_list_lock);
1274
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301275 /* Bind cooling devices for this zone */
1276 bind_tz(tz);
1277
Eduardo Valentinb659a302016-11-07 21:09:23 -08001278 INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001279
Zhang Ruibb431ba2015-10-30 16:31:47 +08001280 thermal_zone_device_reset(tz);
Chen Yu4511f712015-10-30 16:32:10 +08001281 /* Update the new thermal zone and mark it as already updated. */
1282 if (atomic_cmpxchg(&tz->need_update, 1, 0))
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -07001283 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001284
Yao Dongdong14015862014-10-28 07:40:25 +00001285 return tz;
Zhang Rui203d3d42008-01-17 15:51:08 +08001286
Joe Perchescaca8b802012-03-21 12:55:02 -07001287unregister:
Matthew Wilcoxb31ef822016-12-21 09:47:03 -08001288 ida_simple_remove(&thermal_tz_ida, tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001289 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001290 return ERR_PTR(result);
Christophe Jaillet9d9ca1f2017-08-08 16:39:54 +02001291
1292remove_device_groups:
1293 thermal_zone_destroy_device_groups(tz);
1294remove_id:
1295 ida_simple_remove(&thermal_tz_ida, tz->id);
1296free_tz:
1297 kfree(tz);
1298 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001299}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001300EXPORT_SYMBOL_GPL(thermal_zone_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001301
1302/**
1303 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001304 * @tz: the thermal zone device to remove
1305 */
1306void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1307{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301308 int i;
1309 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001310 struct thermal_cooling_device *cdev;
1311 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001312
1313 if (!tz)
1314 return;
1315
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301316 tzp = tz->tzp;
1317
Zhang Rui203d3d42008-01-17 15:51:08 +08001318 mutex_lock(&thermal_list_lock);
1319 list_for_each_entry(pos, &thermal_tz_list, node)
Eduardo Valentinb659a302016-11-07 21:09:23 -08001320 if (pos == tz)
1321 break;
Zhang Rui203d3d42008-01-17 15:51:08 +08001322 if (pos != tz) {
1323 /* thermal zone device not found */
1324 mutex_unlock(&thermal_list_lock);
1325 return;
1326 }
1327 list_del(&tz->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301328
1329 /* Unbind all cdevs associated with 'this' thermal zone */
1330 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1331 if (tz->ops->unbind) {
1332 tz->ops->unbind(tz, cdev);
1333 continue;
1334 }
1335
1336 if (!tzp || !tzp->tbp)
1337 break;
1338
1339 for (i = 0; i < tzp->num_tbps; i++) {
1340 if (tzp->tbp[i].cdev == cdev) {
1341 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1342 tzp->tbp[i].cdev = NULL;
1343 }
1344 }
1345 }
1346
Zhang Rui203d3d42008-01-17 15:51:08 +08001347 mutex_unlock(&thermal_list_lock);
1348
Matthew Garrettb1569e92008-12-03 17:55:32 +00001349 thermal_zone_device_set_polling(tz, 0);
1350
Javi Merinoe33df1d2015-02-26 19:00:27 +00001351 thermal_set_governor(tz, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001352
Zhang Ruie68b16a2008-04-21 16:07:52 +08001353 thermal_remove_hwmon_sysfs(tz);
Matthew Wilcoxb31ef822016-12-21 09:47:03 -08001354 ida_simple_remove(&thermal_tz_ida, tz->id);
1355 ida_destroy(&tz->ida);
Zhang Rui203d3d42008-01-17 15:51:08 +08001356 mutex_destroy(&tz->lock);
1357 device_unregister(&tz->device);
Zhang Rui203d3d42008-01-17 15:51:08 +08001358}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001359EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001360
Eduardo Valentin63c4d912013-04-05 12:32:28 +00001361/**
1362 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1363 * @name: thermal zone name to fetch the temperature
1364 *
1365 * When only one zone is found with the passed name, returns a reference to it.
1366 *
1367 * Return: On success returns a reference to an unique thermal zone with
1368 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1369 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1370 */
1371struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1372{
1373 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1374 unsigned int found = 0;
1375
1376 if (!name)
1377 goto exit;
1378
1379 mutex_lock(&thermal_list_lock);
1380 list_for_each_entry(pos, &thermal_tz_list, node)
Rasmus Villemoes484ac2f2014-10-13 15:55:01 -07001381 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
Eduardo Valentin63c4d912013-04-05 12:32:28 +00001382 found++;
1383 ref = pos;
1384 }
1385 mutex_unlock(&thermal_list_lock);
1386
1387 /* nothing has been found, thus an error code for it */
1388 if (found == 0)
1389 ref = ERR_PTR(-ENODEV);
1390 else if (found > 1)
1391 /* Success only when an unique zone is found */
1392 ref = ERR_PTR(-EEXIST);
1393
1394exit:
1395 return ref;
1396}
1397EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1398
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001399#ifdef CONFIG_NET
Johannes Berg2a94fe42013-11-19 15:19:39 +01001400static const struct genl_multicast_group thermal_event_mcgrps[] = {
1401 { .name = THERMAL_GENL_MCAST_GROUP_NAME, },
1402};
1403
Johannes Berg56989f62016-10-24 14:40:05 +02001404static struct genl_family thermal_event_genl_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +02001405 .module = THIS_MODULE,
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001406 .name = THERMAL_GENL_FAMILY_NAME,
1407 .version = THERMAL_GENL_VERSION,
1408 .maxattr = THERMAL_GENL_ATTR_MAX,
Johannes Berg2a94fe42013-11-19 15:19:39 +01001409 .mcgrps = thermal_event_mcgrps,
1410 .n_mcgrps = ARRAY_SIZE(thermal_event_mcgrps),
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001411};
1412
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001413int thermal_generate_netlink_event(struct thermal_zone_device *tz,
Eduardo Valentineb7be322016-11-07 21:09:21 -08001414 enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05301415{
1416 struct sk_buff *skb;
1417 struct nlattr *attr;
1418 struct thermal_genl_event *thermal_event;
1419 void *msg_header;
1420 int size;
1421 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07001422 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301423
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001424 if (!tz)
1425 return -EINVAL;
1426
R.Durgadoss4cb18722010-10-27 03:33:29 +05301427 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07001428 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1429 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301430
1431 skb = genlmsg_new(size, GFP_ATOMIC);
1432 if (!skb)
1433 return -ENOMEM;
1434
1435 /* add the genetlink message header */
1436 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1437 &thermal_event_genl_family, 0,
1438 THERMAL_GENL_CMD_EVENT);
1439 if (!msg_header) {
1440 nlmsg_free(skb);
1441 return -ENOMEM;
1442 }
1443
1444 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07001445 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1446 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05301447
1448 if (!attr) {
1449 nlmsg_free(skb);
1450 return -EINVAL;
1451 }
1452
1453 thermal_event = nla_data(attr);
1454 if (!thermal_event) {
1455 nlmsg_free(skb);
1456 return -EINVAL;
1457 }
1458
1459 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1460
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001461 thermal_event->orig = tz->id;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301462 thermal_event->event = event;
1463
1464 /* send multicast genetlink message */
Johannes Berg053c0952015-01-16 22:09:00 +01001465 genlmsg_end(skb, msg_header);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301466
Johannes Berg68eb5502013-11-19 15:19:38 +01001467 result = genlmsg_multicast(&thermal_event_genl_family, skb, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +01001468 0, GFP_ATOMIC);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301469 if (result)
Eduardo Valentin923e0b12013-01-02 15:29:41 +00001470 dev_err(&tz->device, "Failed to send netlink event:%d", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301471
1472 return result;
1473}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001474EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301475
Johannes Berg56989f62016-10-24 14:40:05 +02001476static int __init genetlink_init(void)
R.Durgadoss4cb18722010-10-27 03:33:29 +05301477{
Johannes Berg2a94fe42013-11-19 15:19:39 +01001478 return genl_register_family(&thermal_event_genl_family);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301479}
1480
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001481static void genetlink_exit(void)
1482{
1483 genl_unregister_family(&thermal_event_genl_family);
1484}
1485#else /* !CONFIG_NET */
1486static inline int genetlink_init(void) { return 0; }
1487static inline void genetlink_exit(void) {}
1488#endif /* !CONFIG_NET */
1489
Zhang Ruiff140fe2015-10-30 16:31:58 +08001490static int thermal_pm_notify(struct notifier_block *nb,
Eduardo Valentineb7be322016-11-07 21:09:21 -08001491 unsigned long mode, void *_unused)
Zhang Ruiff140fe2015-10-30 16:31:58 +08001492{
1493 struct thermal_zone_device *tz;
1494
1495 switch (mode) {
1496 case PM_HIBERNATION_PREPARE:
1497 case PM_RESTORE_PREPARE:
1498 case PM_SUSPEND_PREPARE:
1499 atomic_set(&in_suspend, 1);
1500 break;
1501 case PM_POST_HIBERNATION:
1502 case PM_POST_RESTORE:
1503 case PM_POST_SUSPEND:
1504 atomic_set(&in_suspend, 0);
1505 list_for_each_entry(tz, &thermal_tz_list, node) {
1506 thermal_zone_device_reset(tz);
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -07001507 thermal_zone_device_update(tz,
1508 THERMAL_EVENT_UNSPECIFIED);
Zhang Ruiff140fe2015-10-30 16:31:58 +08001509 }
1510 break;
1511 default:
1512 break;
1513 }
1514 return 0;
1515}
1516
1517static struct notifier_block thermal_pm_nb = {
1518 .notifier_call = thermal_pm_notify,
1519};
1520
Zhang Rui203d3d42008-01-17 15:51:08 +08001521static int __init thermal_init(void)
1522{
Zhang Rui80a26a52013-03-26 16:38:29 +08001523 int result;
1524
Keerthye441fd62017-04-18 09:59:58 +05301525 mutex_init(&poweroff_lock);
Zhang Rui80a26a52013-03-26 16:38:29 +08001526 result = thermal_register_governors();
1527 if (result)
1528 goto error;
Zhang Rui203d3d42008-01-17 15:51:08 +08001529
1530 result = class_register(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08001531 if (result)
1532 goto unregister_governors;
1533
R.Durgadoss4cb18722010-10-27 03:33:29 +05301534 result = genetlink_init();
Zhang Rui80a26a52013-03-26 16:38:29 +08001535 if (result)
1536 goto unregister_class;
1537
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001538 result = of_parse_thermal_zones();
1539 if (result)
1540 goto exit_netlink;
1541
Zhang Ruiff140fe2015-10-30 16:31:58 +08001542 result = register_pm_notifier(&thermal_pm_nb);
1543 if (result)
1544 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
1545 result);
1546
Zhang Rui80a26a52013-03-26 16:38:29 +08001547 return 0;
1548
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001549exit_netlink:
1550 genetlink_exit();
Zhang Rui80a26a52013-03-26 16:38:29 +08001551unregister_class:
1552 class_unregister(&thermal_class);
Luis Henriques9d367e52014-12-03 21:20:21 +00001553unregister_governors:
1554 thermal_unregister_governors();
Zhang Rui80a26a52013-03-26 16:38:29 +08001555error:
Matthew Wilcoxb31ef822016-12-21 09:47:03 -08001556 ida_destroy(&thermal_tz_ida);
1557 ida_destroy(&thermal_cdev_ida);
Zhang Rui80a26a52013-03-26 16:38:29 +08001558 mutex_destroy(&thermal_list_lock);
1559 mutex_destroy(&thermal_governor_lock);
Keerthye441fd62017-04-18 09:59:58 +05301560 mutex_destroy(&poweroff_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001561 return result;
1562}
1563
1564static void __exit thermal_exit(void)
1565{
Zhang Ruiff140fe2015-10-30 16:31:58 +08001566 unregister_pm_notifier(&thermal_pm_nb);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001567 of_thermal_destroy_zones();
Zhang Rui80a26a52013-03-26 16:38:29 +08001568 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08001569 class_unregister(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08001570 thermal_unregister_governors();
Matthew Wilcoxb31ef822016-12-21 09:47:03 -08001571 ida_destroy(&thermal_tz_ida);
1572 ida_destroy(&thermal_cdev_ida);
Zhang Rui203d3d42008-01-17 15:51:08 +08001573 mutex_destroy(&thermal_list_lock);
Zhang Rui80a26a52013-03-26 16:38:29 +08001574 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001575}
1576
R.Durgadoss4cb18722010-10-27 03:33:29 +05301577fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08001578module_exit(thermal_exit);