]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/thermal/thermal_core.c
thermal: remove get_mode() operation of drivers
[mirror_ubuntu-hirsute-kernel.git] / drivers / thermal / thermal_core.c
CommitLineData
7e3c0381 1// SPDX-License-Identifier: GPL-2.0
203d3d4a
ZR
2/*
3 * thermal.c - Generic Thermal Management Sysfs support.
4 *
5 * Copyright (C) 2008 Intel Corp
6 * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
7 * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
203d3d4a
ZR
8 */
9
c5a01dd5
JP
10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
203d3d4a
ZR
12#include <linux/device.h>
13#include <linux/err.h>
3f0cfea3 14#include <linux/export.h>
5a0e3ad6 15#include <linux/slab.h>
203d3d4a
ZR
16#include <linux/kdev_t.h>
17#include <linux/idr.h>
18#include <linux/thermal.h>
b1569e99 19#include <linux/reboot.h>
42a5bf50 20#include <linux/string.h>
a116b5d4 21#include <linux/of.h>
ff140fea 22#include <linux/suspend.h>
203d3d4a 23
100a8fdb
PA
24#define CREATE_TRACE_POINTS
25#include <trace/events/thermal.h>
26
71350db4 27#include "thermal_core.h"
0dd88793 28#include "thermal_hwmon.h"
71350db4 29
b31ef828
MW
30static DEFINE_IDA(thermal_tz_ida);
31static DEFINE_IDA(thermal_cdev_ida);
203d3d4a
ZR
32
33static LIST_HEAD(thermal_tz_list);
34static LIST_HEAD(thermal_cdev_list);
a4a15485
D
35static LIST_HEAD(thermal_governor_list);
36
203d3d4a 37static DEFINE_MUTEX(thermal_list_lock);
a4a15485 38static DEFINE_MUTEX(thermal_governor_lock);
e441fd68 39static DEFINE_MUTEX(poweroff_lock);
a4a15485 40
ff140fea 41static atomic_t in_suspend;
e441fd68 42static bool power_off_triggered;
ff140fea 43
f2234bcd
ZR
44static struct thermal_governor *def_governor;
45
1b4f4849
EV
46/*
47 * Governor section: set of functions to handle thermal governors
48 *
49 * Functions to help in the life cycle of thermal governors within
50 * the thermal core and by the thermal governor code.
51 */
52
a4a15485
D
53static struct thermal_governor *__find_governor(const char *name)
54{
55 struct thermal_governor *pos;
56
f2234bcd
ZR
57 if (!name || !name[0])
58 return def_governor;
59
a4a15485 60 list_for_each_entry(pos, &thermal_governor_list, governor_list)
484ac2f3 61 if (!strncasecmp(name, pos->name, THERMAL_NAME_LENGTH))
a4a15485
D
62 return pos;
63
64 return NULL;
65}
66
e33df1d2
JM
67/**
68 * bind_previous_governor() - bind the previous governor of the thermal zone
69 * @tz: a valid pointer to a struct thermal_zone_device
70 * @failed_gov_name: the name of the governor that failed to register
71 *
72 * Register the previous governor of the thermal zone after a new
73 * governor has failed to be bound.
74 */
75static void bind_previous_governor(struct thermal_zone_device *tz,
76 const char *failed_gov_name)
77{
78 if (tz->governor && tz->governor->bind_to_tz) {
79 if (tz->governor->bind_to_tz(tz)) {
80 dev_err(&tz->device,
81 "governor %s failed to bind and the previous one (%s) failed to bind again, thermal zone %s has no governor\n",
82 failed_gov_name, tz->governor->name, tz->type);
83 tz->governor = NULL;
84 }
85 }
86}
87
88/**
89 * thermal_set_governor() - Switch to another governor
90 * @tz: a valid pointer to a struct thermal_zone_device
91 * @new_gov: pointer to the new governor
92 *
93 * Change the governor of thermal zone @tz.
94 *
95 * Return: 0 on success, an error if the new governor's bind_to_tz() failed.
96 */
97static int thermal_set_governor(struct thermal_zone_device *tz,
98 struct thermal_governor *new_gov)
99{
100 int ret = 0;
101
102 if (tz->governor && tz->governor->unbind_from_tz)
103 tz->governor->unbind_from_tz(tz);
104
105 if (new_gov && new_gov->bind_to_tz) {
106 ret = new_gov->bind_to_tz(tz);
107 if (ret) {
108 bind_previous_governor(tz, new_gov->name);
109
110 return ret;
111 }
112 }
113
114 tz->governor = new_gov;
115
116 return ret;
117}
118
a4a15485
D
119int thermal_register_governor(struct thermal_governor *governor)
120{
121 int err;
122 const char *name;
123 struct thermal_zone_device *pos;
124
125 if (!governor)
126 return -EINVAL;
127
128 mutex_lock(&thermal_governor_lock);
129
130 err = -EBUSY;
5027ba36 131 if (!__find_governor(governor->name)) {
eb7be329
EV
132 bool match_default;
133
a4a15485
D
134 err = 0;
135 list_add(&governor->governor_list, &thermal_governor_list);
eb7be329
EV
136 match_default = !strncmp(governor->name,
137 DEFAULT_THERMAL_GOVERNOR,
138 THERMAL_NAME_LENGTH);
139
140 if (!def_governor && match_default)
f2234bcd 141 def_governor = governor;
a4a15485
D
142 }
143
144 mutex_lock(&thermal_list_lock);
145
146 list_for_each_entry(pos, &thermal_tz_list, node) {
f2234bcd
ZR
147 /*
148 * only thermal zones with specified tz->tzp->governor_name
149 * may run with tz->govenor unset
150 */
a4a15485
D
151 if (pos->governor)
152 continue;
f2234bcd
ZR
153
154 name = pos->tzp->governor_name;
155
e33df1d2
JM
156 if (!strncasecmp(name, governor->name, THERMAL_NAME_LENGTH)) {
157 int ret;
158
159 ret = thermal_set_governor(pos, governor);
160 if (ret)
161 dev_err(&pos->device,
162 "Failed to set governor %s for thermal zone %s: %d\n",
163 governor->name, pos->type, ret);
164 }
a4a15485
D
165 }
166
167 mutex_unlock(&thermal_list_lock);
168 mutex_unlock(&thermal_governor_lock);
169
170 return err;
171}
a4a15485
D
172
173void thermal_unregister_governor(struct thermal_governor *governor)
174{
175 struct thermal_zone_device *pos;
176
177 if (!governor)
178 return;
179
180 mutex_lock(&thermal_governor_lock);
181
5027ba36 182 if (!__find_governor(governor->name))
a4a15485
D
183 goto exit;
184
185 mutex_lock(&thermal_list_lock);
186
187 list_for_each_entry(pos, &thermal_tz_list, node) {
484ac2f3 188 if (!strncasecmp(pos->governor->name, governor->name,
eb7be329 189 THERMAL_NAME_LENGTH))
e33df1d2 190 thermal_set_governor(pos, NULL);
a4a15485
D
191 }
192
193 mutex_unlock(&thermal_list_lock);
194 list_del(&governor->governor_list);
195exit:
196 mutex_unlock(&thermal_governor_lock);
9b4298a0 197}
9b4298a0 198
1b4f4849
EV
199int thermal_zone_device_set_policy(struct thermal_zone_device *tz,
200 char *policy)
9b4298a0 201{
1b4f4849
EV
202 struct thermal_governor *gov;
203 int ret = -EINVAL;
9b4298a0 204
1b4f4849 205 mutex_lock(&thermal_governor_lock);
9b4298a0 206 mutex_lock(&tz->lock);
9b4298a0 207
1b4f4849
EV
208 gov = __find_governor(strim(policy));
209 if (!gov)
210 goto exit;
9b4298a0 211
1b4f4849 212 ret = thermal_set_governor(tz, gov);
9b4298a0 213
1b4f4849
EV
214exit:
215 mutex_unlock(&tz->lock);
216 mutex_unlock(&thermal_governor_lock);
9b4298a0 217
1b4f4849 218 return ret;
7e8ee1e9
D
219}
220
1b4f4849 221int thermal_build_list_of_policies(char *buf)
7e8ee1e9 222{
1b4f4849
EV
223 struct thermal_governor *pos;
224 ssize_t count = 0;
225 ssize_t size = PAGE_SIZE;
7e8ee1e9 226
1b4f4849 227 mutex_lock(&thermal_governor_lock);
a8892d83 228
1b4f4849
EV
229 list_for_each_entry(pos, &thermal_governor_list, governor_list) {
230 size = PAGE_SIZE - count;
231 count += scnprintf(buf + count, size, "%s ", pos->name);
7e8ee1e9 232 }
1b4f4849 233 count += scnprintf(buf + count, size, "\n");
7e8ee1e9 234
1b4f4849 235 mutex_unlock(&thermal_governor_lock);
7e8ee1e9 236
1b4f4849 237 return count;
7e8ee1e9
D
238}
239
57c5b2ec 240static void __init thermal_unregister_governors(void)
7e8ee1e9 241{
57c5b2ec
DL
242 struct thermal_governor **governor;
243
244 for_each_governor_table(governor)
245 thermal_unregister_governor(*governor);
246}
7e8ee1e9 247
57c5b2ec
DL
248static int __init thermal_register_governors(void)
249{
250 int ret = 0;
251 struct thermal_governor **governor;
7e8ee1e9 252
57c5b2ec
DL
253 for_each_governor_table(governor) {
254 ret = thermal_register_governor(*governor);
255 if (ret) {
256 pr_err("Failed to register governor: '%s'",
257 (*governor)->name);
258 break;
259 }
7e8ee1e9 260
57c5b2ec
DL
261 pr_info("Registered thermal governor '%s'",
262 (*governor)->name);
263 }
7e8ee1e9 264
57c5b2ec
DL
265 if (ret) {
266 struct thermal_governor **gov;
7e8ee1e9 267
57c5b2ec
DL
268 for_each_governor_table(gov) {
269 if (gov == governor)
270 break;
271 thermal_unregister_governor(*gov);
272 }
273 }
7e8ee1e9 274
57c5b2ec 275 return ret;
7e8ee1e9
D
276}
277
8772e185
EV
278/*
279 * Zone update section: main control loop applied to each zone while monitoring
280 *
281 * in polling mode. The monitoring is done using a workqueue.
282 * Same update may be done on a zone by calling thermal_zone_device_update().
283 *
284 * An update means:
285 * - Non-critical trips will invoke the governor responsible for that zone;
286 * - Hot trips will produce a notification to userspace;
287 * - Critical trip point will cause a system shutdown.
288 */
0c01ebbf
D
289static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
290 int delay)
291{
292 if (delay > 1000)
c2b59d27
JG
293 mod_delayed_work(system_freezable_power_efficient_wq,
294 &tz->poll_queue,
0c01ebbf
D
295 round_jiffies(msecs_to_jiffies(delay)));
296 else if (delay)
c2b59d27
JG
297 mod_delayed_work(system_freezable_power_efficient_wq,
298 &tz->poll_queue,
0c01ebbf
D
299 msecs_to_jiffies(delay));
300 else
163b00cd 301 cancel_delayed_work(&tz->poll_queue);
0c01ebbf
D
302}
303
304static void monitor_thermal_zone(struct thermal_zone_device *tz)
305{
306 mutex_lock(&tz->lock);
307
308 if (tz->passive)
309 thermal_zone_device_set_polling(tz, tz->passive_delay);
310 else if (tz->polling_delay)
311 thermal_zone_device_set_polling(tz, tz->polling_delay);
312 else
313 thermal_zone_device_set_polling(tz, 0);
314
315 mutex_unlock(&tz->lock);
316}
317
5be52fcc 318static void handle_non_critical_trips(struct thermal_zone_device *tz, int trip)
0c01ebbf 319{
f2234bcd
ZR
320 tz->governor ? tz->governor->throttle(tz, trip) :
321 def_governor->throttle(tz, trip);
0c01ebbf
D
322}
323
ef1d87e0
K
324/**
325 * thermal_emergency_poweroff_func - emergency poweroff work after a known delay
326 * @work: work_struct associated with the emergency poweroff function
327 *
328 * This function is called in very critical situations to force
329 * a kernel poweroff after a configurable timeout value.
330 */
331static void thermal_emergency_poweroff_func(struct work_struct *work)
332{
333 /*
334 * We have reached here after the emergency thermal shutdown
335 * Waiting period has expired. This means orderly_poweroff has
336 * not been able to shut off the system for some reason.
337 * Try to shut down the system immediately using kernel_power_off
338 * if populated
339 */
340 WARN(1, "Attempting kernel_power_off: Temperature too high\n");
341 kernel_power_off();
342
343 /*
344 * Worst of the worst case trigger emergency restart
345 */
346 WARN(1, "Attempting emergency_restart: Temperature too high\n");
347 emergency_restart();
348}
349
350static DECLARE_DELAYED_WORK(thermal_emergency_poweroff_work,
351 thermal_emergency_poweroff_func);
352
353/**
354 * thermal_emergency_poweroff - Trigger an emergency system poweroff
355 *
356 * This may be called from any critical situation to trigger a system shutdown
357 * after a known period of time. By default this is not scheduled.
358 */
c4b379d0 359static void thermal_emergency_poweroff(void)
ef1d87e0
K
360{
361 int poweroff_delay_ms = CONFIG_THERMAL_EMERGENCY_POWEROFF_DELAY_MS;
362 /*
363 * poweroff_delay_ms must be a carefully profiled positive value.
364 * Its a must for thermal_emergency_poweroff_work to be scheduled
365 */
366 if (poweroff_delay_ms <= 0)
367 return;
368 schedule_delayed_work(&thermal_emergency_poweroff_work,
369 msecs_to_jiffies(poweroff_delay_ms));
370}
371
0c01ebbf 372static void handle_critical_trips(struct thermal_zone_device *tz,
eb7be329 373 int trip, enum thermal_trip_type trip_type)
0c01ebbf 374{
17e8351a 375 int trip_temp;
0c01ebbf
D
376
377 tz->ops->get_trip_temp(tz, trip, &trip_temp);
378
379 /* If we have not crossed the trip_temp, we do not care. */
84ffe3ec 380 if (trip_temp <= 0 || tz->temperature < trip_temp)
0c01ebbf
D
381 return;
382
208cd822
PA
383 trace_thermal_zone_trip(tz, trip, trip_type);
384
0c01ebbf
D
385 if (tz->ops->notify)
386 tz->ops->notify(tz, trip, trip_type);
387
388 if (trip_type == THERMAL_TRIP_CRITICAL) {
923e0b1e 389 dev_emerg(&tz->device,
039f6cf5 390 "critical temperature reached (%d C), shutting down\n",
923e0b1e 391 tz->temperature / 1000);
e441fd68
K
392 mutex_lock(&poweroff_lock);
393 if (!power_off_triggered) {
ef1d87e0
K
394 /*
395 * Queue a backup emergency shutdown in the event of
396 * orderly_poweroff failure
397 */
398 thermal_emergency_poweroff();
e441fd68
K
399 orderly_poweroff(true);
400 power_off_triggered = true;
401 }
402 mutex_unlock(&poweroff_lock);
0c01ebbf
D
403 }
404}
405
406static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
407{
408 enum thermal_trip_type type;
409
81ad4276
ZR
410 /* Ignore disabled trip points */
411 if (test_bit(trip, &tz->trips_disabled))
412 return;
413
0c01ebbf
D
414 tz->ops->get_trip_type(tz, trip, &type);
415
416 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
417 handle_critical_trips(tz, trip, type);
418 else
5be52fcc 419 handle_non_critical_trips(tz, trip);
0c01ebbf
D
420 /*
421 * Alright, we handled this trip successfully.
422 * So, start monitoring again.
423 */
424 monitor_thermal_zone(tz);
425}
426
427static void update_temperature(struct thermal_zone_device *tz)
428{
17e8351a 429 int temp, ret;
0c01ebbf 430
e6e238c3 431 ret = thermal_zone_get_temp(tz, &temp);
0c01ebbf 432 if (ret) {
7e497a73
HG
433 if (ret != -EAGAIN)
434 dev_warn(&tz->device,
435 "failed to read out thermal zone (%d)\n",
436 ret);
e6e238c3 437 return;
0c01ebbf
D
438 }
439
e6e238c3 440 mutex_lock(&tz->lock);
0c01ebbf
D
441 tz->last_temperature = tz->temperature;
442 tz->temperature = temp;
0c01ebbf 443 mutex_unlock(&tz->lock);
06475b55 444
100a8fdb 445 trace_thermal_temperature(tz);
bb431ba2
ZR
446}
447
964f4843 448static void thermal_zone_device_init(struct thermal_zone_device *tz)
bb431ba2
ZR
449{
450 struct thermal_instance *pos;
bb431ba2 451 tz->temperature = THERMAL_TEMP_INVALID;
bb431ba2
ZR
452 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
453 pos->initialized = false;
0c01ebbf
D
454}
455
964f4843
WW
456static void thermal_zone_device_reset(struct thermal_zone_device *tz)
457{
458 tz->passive = 0;
459 thermal_zone_device_init(tz);
460}
461
0e70f466
SP
462void thermal_zone_device_update(struct thermal_zone_device *tz,
463 enum thermal_notify_event event)
0c01ebbf
D
464{
465 int count;
466
ff140fea
ZR
467 if (atomic_read(&in_suspend))
468 return;
469
81bd4e1c
EV
470 if (!tz->ops->get_temp)
471 return;
472
0c01ebbf
D
473 update_temperature(tz);
474
060c034a
SH
475 thermal_zone_set_trips(tz);
476
0e70f466
SP
477 tz->notify_event = event;
478
0c01ebbf
D
479 for (count = 0; count < tz->trips; count++)
480 handle_thermal_trip(tz, count);
481}
910cb1e3 482EXPORT_SYMBOL_GPL(thermal_zone_device_update);
0c01ebbf 483
106339ab
EV
484/**
485 * thermal_notify_framework - Sensor drivers use this API to notify framework
486 * @tz: thermal zone device
487 * @trip: indicates which trip point has been crossed
488 *
489 * This function handles the trip events from sensor drivers. It starts
490 * throttling the cooling devices according to the policy configured.
491 * For CRITICAL and HOT trip points, this notifies the respective drivers,
492 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
493 * The throttling policy is based on the configured platform data; if no
494 * platform data is provided, this uses the step_wise throttling policy.
495 */
496void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
497{
498 handle_thermal_trip(tz, trip);
499}
500EXPORT_SYMBOL_GPL(thermal_notify_framework);
501
0c01ebbf
D
502static void thermal_zone_device_check(struct work_struct *work)
503{
504 struct thermal_zone_device *tz = container_of(work, struct
505 thermal_zone_device,
506 poll_queue.work);
0e70f466 507 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
0c01ebbf
D
508}
509
712afbdf
EV
510/*
511 * Power actor section: interface to power actors to estimate power
512 *
513 * Set of functions used to interact to cooling devices that know
514 * how to estimate their devices power consumption.
515 */
9f38271c 516
35b11d2e
JM
517/**
518 * power_actor_get_max_power() - get the maximum power that a cdev can consume
519 * @cdev: pointer to &thermal_cooling_device
520 * @tz: a valid thermal zone device pointer
521 * @max_power: pointer in which to store the maximum power
522 *
523 * Calculate the maximum power consumption in milliwats that the
524 * cooling device can currently consume and store it in @max_power.
525 *
526 * Return: 0 on success, -EINVAL if @cdev doesn't support the
527 * power_actor API or -E* on other error.
528 */
529int power_actor_get_max_power(struct thermal_cooling_device *cdev,
530 struct thermal_zone_device *tz, u32 *max_power)
531{
532 if (!cdev_is_power_actor(cdev))
533 return -EINVAL;
534
535 return cdev->ops->state2power(cdev, tz, 0, max_power);
536}
537
c973c3bc
JM
538/**
539 * power_actor_get_min_power() - get the mainimum power that a cdev can consume
540 * @cdev: pointer to &thermal_cooling_device
541 * @tz: a valid thermal zone device pointer
542 * @min_power: pointer in which to store the minimum power
543 *
544 * Calculate the minimum power consumption in milliwatts that the
545 * cooling device can currently consume and store it in @min_power.
546 *
547 * Return: 0 on success, -EINVAL if @cdev doesn't support the
548 * power_actor API or -E* on other error.
549 */
550int power_actor_get_min_power(struct thermal_cooling_device *cdev,
551 struct thermal_zone_device *tz, u32 *min_power)
552{
553 unsigned long max_state;
554 int ret;
555
556 if (!cdev_is_power_actor(cdev))
557 return -EINVAL;
558
559 ret = cdev->ops->get_max_state(cdev, &max_state);
560 if (ret)
561 return ret;
562
563 return cdev->ops->state2power(cdev, tz, max_state, min_power);
564}
565
35b11d2e 566/**
1a7e7cc0 567 * power_actor_set_power() - limit the maximum power a cooling device consumes
35b11d2e
JM
568 * @cdev: pointer to &thermal_cooling_device
569 * @instance: thermal instance to update
570 * @power: the power in milliwatts
571 *
1a7e7cc0
EV
572 * Set the cooling device to consume at most @power milliwatts. The limit is
573 * expected to be a cap at the maximum power consumption.
35b11d2e
JM
574 *
575 * Return: 0 on success, -EINVAL if the cooling device does not
576 * implement the power actor API or -E* for other failures.
577 */
578int power_actor_set_power(struct thermal_cooling_device *cdev,
579 struct thermal_instance *instance, u32 power)
580{
581 unsigned long state;
582 int ret;
583
584 if (!cdev_is_power_actor(cdev))
585 return -EINVAL;
586
587 ret = cdev->ops->power2state(cdev, instance->tz, power, &state);
588 if (ret)
589 return ret;
590
591 instance->target = state;
d0b7306d 592 mutex_lock(&cdev->lock);
35b11d2e 593 cdev->updated = false;
d0b7306d 594 mutex_unlock(&cdev->lock);
35b11d2e
JM
595 thermal_cdev_update(cdev);
596
597 return 0;
598}
599
3d0055d2
EV
600void thermal_zone_device_rebind_exception(struct thermal_zone_device *tz,
601 const char *cdev_type, size_t size)
203d3d4a 602{
3d0055d2 603 struct thermal_cooling_device *cdev = NULL;
203d3d4a 604
3d0055d2
EV
605 mutex_lock(&thermal_list_lock);
606 list_for_each_entry(cdev, &thermal_cdev_list, node) {
607 /* skip non matching cdevs */
608 if (strncmp(cdev_type, cdev->type, size))
609 continue;
610
611 /* re binding the exception matching the type pattern */
612 thermal_zone_bind_cooling_device(tz, THERMAL_TRIPS_NONE, cdev,
613 THERMAL_NO_LIMIT,
614 THERMAL_NO_LIMIT,
615 THERMAL_WEIGHT_DEFAULT);
616 }
617 mutex_unlock(&thermal_list_lock);
203d3d4a
ZR
618}
619
3d0055d2
EV
620void thermal_zone_device_unbind_exception(struct thermal_zone_device *tz,
621 const char *cdev_type, size_t size)
203d3d4a 622{
3d0055d2 623 struct thermal_cooling_device *cdev = NULL;
203d3d4a 624
3d0055d2
EV
625 mutex_lock(&thermal_list_lock);
626 list_for_each_entry(cdev, &thermal_cdev_list, node) {
627 /* skip non matching cdevs */
628 if (strncmp(cdev_type, cdev->type, size))
629 continue;
630 /* unbinding the exception matching the type pattern */
631 thermal_zone_unbind_cooling_device(tz, THERMAL_TRIPS_NONE,
632 cdev);
633 }
634 mutex_unlock(&thermal_list_lock);
203d3d4a
ZR
635}
636
81193e2e
EV
637/*
638 * Device management section: cooling devices, zones devices, and binding
639 *
640 * Set of functions provided by the thermal core for:
641 * - cooling devices lifecycle: registration, unregistration,
642 * binding, and unbinding.
643 * - thermal zone devices lifecycle: registration, unregistration,
644 * binding, and unbinding.
645 */
203d3d4a
ZR
646
647/**
d2e4eb83
EV
648 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
649 * @tz: pointer to struct thermal_zone_device
203d3d4a
ZR
650 * @trip: indicates which trip point the cooling devices is
651 * associated with in this thermal zone.
d2e4eb83
EV
652 * @cdev: pointer to struct thermal_cooling_device
653 * @upper: the Maximum cooling state for this trip point.
654 * THERMAL_NO_LIMIT means no upper limit,
655 * and the cooling device can be in max_state.
656 * @lower: the Minimum cooling state can be used for this trip point.
657 * THERMAL_NO_LIMIT means no lower limit,
658 * and the cooling device can be in cooling state 0.
6cd9e9f6
KS
659 * @weight: The weight of the cooling device to be bound to the
660 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
661 * default value
543a9561 662 *
d2e4eb83
EV
663 * This interface function bind a thermal cooling device to the certain trip
664 * point of a thermal zone device.
543a9561 665 * This function is usually called in the thermal zone device .bind callback.
d2e4eb83
EV
666 *
667 * Return: 0 on success, the proper error value otherwise.
203d3d4a
ZR
668 */
669int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
670 int trip,
9d99842f 671 struct thermal_cooling_device *cdev,
6cd9e9f6
KS
672 unsigned long upper, unsigned long lower,
673 unsigned int weight)
203d3d4a 674{
b81b6ba3
ZR
675 struct thermal_instance *dev;
676 struct thermal_instance *pos;
c7516709
TS
677 struct thermal_zone_device *pos1;
678 struct thermal_cooling_device *pos2;
74051ba5 679 unsigned long max_state;
9a3031dc 680 int result, ret;
203d3d4a 681
543a9561 682 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
203d3d4a
ZR
683 return -EINVAL;
684
c7516709
TS
685 list_for_each_entry(pos1, &thermal_tz_list, node) {
686 if (pos1 == tz)
687 break;
688 }
689 list_for_each_entry(pos2, &thermal_cdev_list, node) {
690 if (pos2 == cdev)
691 break;
692 }
693
694 if (tz != pos1 || cdev != pos2)
203d3d4a
ZR
695 return -EINVAL;
696
9a3031dc
LM
697 ret = cdev->ops->get_max_state(cdev, &max_state);
698 if (ret)
699 return ret;
9d99842f
ZR
700
701 /* lower default 0, upper default max_state */
702 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
703 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
704
705 if (lower > upper || upper > max_state)
706 return -EINVAL;
707
95e3ed15 708 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
203d3d4a
ZR
709 if (!dev)
710 return -ENOMEM;
711 dev->tz = tz;
712 dev->cdev = cdev;
713 dev->trip = trip;
9d99842f
ZR
714 dev->upper = upper;
715 dev->lower = lower;
ce119f83 716 dev->target = THERMAL_NO_TARGET;
6cd9e9f6 717 dev->weight = weight;
74051ba5 718
b31ef828
MW
719 result = ida_simple_get(&tz->ida, 0, 0, GFP_KERNEL);
720 if (result < 0)
203d3d4a
ZR
721 goto free_mem;
722
b31ef828 723 dev->id = result;
203d3d4a
ZR
724 sprintf(dev->name, "cdev%d", dev->id);
725 result =
726 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
727 if (result)
b31ef828 728 goto release_ida;
203d3d4a
ZR
729
730 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
975f8c56 731 sysfs_attr_init(&dev->attr.attr);
203d3d4a
ZR
732 dev->attr.attr.name = dev->attr_name;
733 dev->attr.attr.mode = 0444;
33e678d4 734 dev->attr.show = trip_point_show;
203d3d4a
ZR
735 result = device_create_file(&tz->device, &dev->attr);
736 if (result)
737 goto remove_symbol_link;
738
db916513
JM
739 sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
740 sysfs_attr_init(&dev->weight_attr.attr);
741 dev->weight_attr.attr.name = dev->weight_attr_name;
742 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
33e678d4
VK
743 dev->weight_attr.show = weight_show;
744 dev->weight_attr.store = weight_store;
db916513
JM
745 result = device_create_file(&tz->device, &dev->weight_attr);
746 if (result)
747 goto remove_trip_file;
748
203d3d4a 749 mutex_lock(&tz->lock);
f4a821ce 750 mutex_lock(&cdev->lock);
cddf31b3 751 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
b659a30d
EV
752 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
753 result = -EEXIST;
754 break;
755 }
b5e4ae62 756 if (!result) {
cddf31b3 757 list_add_tail(&dev->tz_node, &tz->thermal_instances);
b5e4ae62 758 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
4511f716 759 atomic_set(&tz->need_update, 1);
b5e4ae62 760 }
f4a821ce 761 mutex_unlock(&cdev->lock);
203d3d4a
ZR
762 mutex_unlock(&tz->lock);
763
764 if (!result)
765 return 0;
766
db916513
JM
767 device_remove_file(&tz->device, &dev->weight_attr);
768remove_trip_file:
203d3d4a 769 device_remove_file(&tz->device, &dev->attr);
caca8b80 770remove_symbol_link:
203d3d4a 771 sysfs_remove_link(&tz->device.kobj, dev->name);
b31ef828
MW
772release_ida:
773 ida_simple_remove(&tz->ida, dev->id);
caca8b80 774free_mem:
203d3d4a
ZR
775 kfree(dev);
776 return result;
777}
910cb1e3 778EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
203d3d4a
ZR
779
780/**
9892e5dc
EV
781 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
782 * thermal zone.
783 * @tz: pointer to a struct thermal_zone_device.
203d3d4a
ZR
784 * @trip: indicates which trip point the cooling devices is
785 * associated with in this thermal zone.
9892e5dc 786 * @cdev: pointer to a struct thermal_cooling_device.
543a9561 787 *
9892e5dc
EV
788 * This interface function unbind a thermal cooling device from the certain
789 * trip point of a thermal zone device.
543a9561 790 * This function is usually called in the thermal zone device .unbind callback.
9892e5dc
EV
791 *
792 * Return: 0 on success, the proper error value otherwise.
203d3d4a
ZR
793 */
794int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
795 int trip,
796 struct thermal_cooling_device *cdev)
797{
b81b6ba3 798 struct thermal_instance *pos, *next;
203d3d4a
ZR
799
800 mutex_lock(&tz->lock);
f4a821ce 801 mutex_lock(&cdev->lock);
cddf31b3 802 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
543a9561 803 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
cddf31b3 804 list_del(&pos->tz_node);
b5e4ae62 805 list_del(&pos->cdev_node);
f4a821ce 806 mutex_unlock(&cdev->lock);
203d3d4a
ZR
807 mutex_unlock(&tz->lock);
808 goto unbind;
809 }
810 }
f4a821ce 811 mutex_unlock(&cdev->lock);
203d3d4a
ZR
812 mutex_unlock(&tz->lock);
813
814 return -ENODEV;
815
caca8b80 816unbind:
528464ea 817 device_remove_file(&tz->device, &pos->weight_attr);
203d3d4a
ZR
818 device_remove_file(&tz->device, &pos->attr);
819 sysfs_remove_link(&tz->device.kobj, pos->name);
b31ef828 820 ida_simple_remove(&tz->ida, pos->id);
203d3d4a
ZR
821 kfree(pos);
822 return 0;
823}
910cb1e3 824EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
203d3d4a
ZR
825
826static void thermal_release(struct device *dev)
827{
828 struct thermal_zone_device *tz;
829 struct thermal_cooling_device *cdev;
830
caca8b80
JP
831 if (!strncmp(dev_name(dev), "thermal_zone",
832 sizeof("thermal_zone") - 1)) {
203d3d4a 833 tz = to_thermal_zone(dev);
6a6cd25b 834 thermal_zone_destroy_device_groups(tz);
203d3d4a 835 kfree(tz);
b659a30d
EV
836 } else if (!strncmp(dev_name(dev), "cooling_device",
837 sizeof("cooling_device") - 1)) {
203d3d4a
ZR
838 cdev = to_cooling_device(dev);
839 kfree(cdev);
840 }
841}
842
843static struct class thermal_class = {
844 .name = "thermal",
845 .dev_release = thermal_release,
846};
847
4b0d3c2d
EV
848static inline
849void print_bind_err_msg(struct thermal_zone_device *tz,
850 struct thermal_cooling_device *cdev, int ret)
f502ab84
EV
851{
852 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
853 tz->type, cdev->type, ret);
854}
855
856static void __bind(struct thermal_zone_device *tz, int mask,
857 struct thermal_cooling_device *cdev,
858 unsigned long *limits,
859 unsigned int weight)
860{
861 int i, ret;
862
863 for (i = 0; i < tz->trips; i++) {
864 if (mask & (1 << i)) {
865 unsigned long upper, lower;
866
867 upper = THERMAL_NO_LIMIT;
868 lower = THERMAL_NO_LIMIT;
869 if (limits) {
870 lower = limits[i * 2];
871 upper = limits[i * 2 + 1];
872 }
873 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
874 upper, lower,
875 weight);
876 if (ret)
877 print_bind_err_msg(tz, cdev, ret);
878 }
879 }
880}
881
949aad83
EV
882static void bind_cdev(struct thermal_cooling_device *cdev)
883{
884 int i, ret;
885 const struct thermal_zone_params *tzp;
886 struct thermal_zone_device *pos = NULL;
887
888 mutex_lock(&thermal_list_lock);
889
890 list_for_each_entry(pos, &thermal_tz_list, node) {
891 if (!pos->tzp && !pos->ops->bind)
892 continue;
893
894 if (pos->ops->bind) {
895 ret = pos->ops->bind(pos, cdev);
896 if (ret)
897 print_bind_err_msg(pos, cdev, ret);
898 continue;
899 }
900
901 tzp = pos->tzp;
902 if (!tzp || !tzp->tbp)
903 continue;
904
905 for (i = 0; i < tzp->num_tbps; i++) {
906 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
907 continue;
908 if (tzp->tbp[i].match(pos, cdev))
909 continue;
910 tzp->tbp[i].cdev = cdev;
911 __bind(pos, tzp->tbp[i].trip_mask, cdev,
912 tzp->tbp[i].binding_limits,
913 tzp->tbp[i].weight);
914 }
915 }
916
917 mutex_unlock(&thermal_list_lock);
918}
919
203d3d4a 920/**
a116b5d4
EV
921 * __thermal_cooling_device_register() - register a new thermal cooling device
922 * @np: a pointer to a device tree node.
203d3d4a
ZR
923 * @type: the thermal cooling device type.
924 * @devdata: device private data.
925 * @ops: standard thermal cooling devices callbacks.
3a6eccb3
EV
926 *
927 * This interface function adds a new thermal cooling device (fan/processor/...)
928 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
929 * to all the thermal zone devices registered at the same time.
a116b5d4
EV
930 * It also gives the opportunity to link the cooling device to a device tree
931 * node, so that it can be bound to a thermal zone created out of device tree.
3a6eccb3
EV
932 *
933 * Return: a pointer to the created struct thermal_cooling_device or an
934 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
203d3d4a 935 */
a116b5d4
EV
936static struct thermal_cooling_device *
937__thermal_cooling_device_register(struct device_node *np,
f991de53 938 const char *type, void *devdata,
a116b5d4 939 const struct thermal_cooling_device_ops *ops)
203d3d4a
ZR
940{
941 struct thermal_cooling_device *cdev;
4511f716 942 struct thermal_zone_device *pos = NULL;
203d3d4a
ZR
943 int result;
944
204dd1d3 945 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
3e6fda5c 946 return ERR_PTR(-EINVAL);
203d3d4a
ZR
947
948 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
543a9561 949 !ops->set_cur_state)
3e6fda5c 950 return ERR_PTR(-EINVAL);
203d3d4a 951
95e3ed15 952 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
203d3d4a 953 if (!cdev)
3e6fda5c 954 return ERR_PTR(-ENOMEM);
203d3d4a 955
b31ef828
MW
956 result = ida_simple_get(&thermal_cdev_ida, 0, 0, GFP_KERNEL);
957 if (result < 0) {
203d3d4a 958 kfree(cdev);
3e6fda5c 959 return ERR_PTR(result);
203d3d4a
ZR
960 }
961
b31ef828 962 cdev->id = result;
c7a8b9d9 963 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
f4a821ce 964 mutex_init(&cdev->lock);
b5e4ae62 965 INIT_LIST_HEAD(&cdev->thermal_instances);
a116b5d4 966 cdev->np = np;
203d3d4a 967 cdev->ops = ops;
5ca0cce5 968 cdev->updated = false;
203d3d4a
ZR
969 cdev->device.class = &thermal_class;
970 cdev->devdata = devdata;
8ea22951 971 thermal_cooling_device_setup_sysfs(cdev);
354655ea 972 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
203d3d4a
ZR
973 result = device_register(&cdev->device);
974 if (result) {
b31ef828 975 ida_simple_remove(&thermal_cdev_ida, cdev->id);
adc8749b 976 put_device(&cdev->device);
3e6fda5c 977 return ERR_PTR(result);
203d3d4a
ZR
978 }
979
7e8ee1e9 980 /* Add 'this' new cdev to the global cdev list */
203d3d4a
ZR
981 mutex_lock(&thermal_list_lock);
982 list_add(&cdev->node, &thermal_cdev_list);
203d3d4a
ZR
983 mutex_unlock(&thermal_list_lock);
984
7e8ee1e9
D
985 /* Update binding information for 'this' new cdev */
986 bind_cdev(cdev);
987
4511f716
CY
988 mutex_lock(&thermal_list_lock);
989 list_for_each_entry(pos, &thermal_tz_list, node)
990 if (atomic_cmpxchg(&pos->need_update, 1, 0))
0e70f466
SP
991 thermal_zone_device_update(pos,
992 THERMAL_EVENT_UNSPECIFIED);
4511f716
CY
993 mutex_unlock(&thermal_list_lock);
994
7e8ee1e9 995 return cdev;
203d3d4a 996}
a116b5d4
EV
997
998/**
999 * thermal_cooling_device_register() - register a new thermal cooling device
1000 * @type: the thermal cooling device type.
1001 * @devdata: device private data.
1002 * @ops: standard thermal cooling devices callbacks.
1003 *
1004 * This interface function adds a new thermal cooling device (fan/processor/...)
1005 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1006 * to all the thermal zone devices registered at the same time.
1007 *
1008 * Return: a pointer to the created struct thermal_cooling_device or an
1009 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1010 */
1011struct thermal_cooling_device *
f991de53 1012thermal_cooling_device_register(const char *type, void *devdata,
a116b5d4
EV
1013 const struct thermal_cooling_device_ops *ops)
1014{
1015 return __thermal_cooling_device_register(NULL, type, devdata, ops);
1016}
910cb1e3 1017EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
203d3d4a 1018
a116b5d4
EV
1019/**
1020 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1021 * @np: a pointer to a device tree node.
1022 * @type: the thermal cooling device type.
1023 * @devdata: device private data.
1024 * @ops: standard thermal cooling devices callbacks.
1025 *
1026 * This function will register a cooling device with device tree node reference.
1027 * This interface function adds a new thermal cooling device (fan/processor/...)
1028 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1029 * to all the thermal zone devices registered at the same time.
1030 *
1031 * Return: a pointer to the created struct thermal_cooling_device or an
1032 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1033 */
1034struct thermal_cooling_device *
1035thermal_of_cooling_device_register(struct device_node *np,
f991de53 1036 const char *type, void *devdata,
a116b5d4
EV
1037 const struct thermal_cooling_device_ops *ops)
1038{
1039 return __thermal_cooling_device_register(np, type, devdata, ops);
1040}
1041EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1042
b4ab114c
GR
1043static void thermal_cooling_device_release(struct device *dev, void *res)
1044{
1045 thermal_cooling_device_unregister(
1046 *(struct thermal_cooling_device **)res);
1047}
1048
1049/**
1050 * devm_thermal_of_cooling_device_register() - register an OF thermal cooling
1051 * device
1052 * @dev: a valid struct device pointer of a sensor device.
1053 * @np: a pointer to a device tree node.
1054 * @type: the thermal cooling device type.
1055 * @devdata: device private data.
1056 * @ops: standard thermal cooling devices callbacks.
1057 *
1058 * This function will register a cooling device with device tree node reference.
1059 * This interface function adds a new thermal cooling device (fan/processor/...)
1060 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1061 * to all the thermal zone devices registered at the same time.
1062 *
1063 * Return: a pointer to the created struct thermal_cooling_device or an
1064 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1065 */
1066struct thermal_cooling_device *
1067devm_thermal_of_cooling_device_register(struct device *dev,
1068 struct device_node *np,
1069 char *type, void *devdata,
1070 const struct thermal_cooling_device_ops *ops)
1071{
1072 struct thermal_cooling_device **ptr, *tcd;
1073
1074 ptr = devres_alloc(thermal_cooling_device_release, sizeof(*ptr),
1075 GFP_KERNEL);
1076 if (!ptr)
1077 return ERR_PTR(-ENOMEM);
1078
1079 tcd = __thermal_cooling_device_register(np, type, devdata, ops);
1080 if (IS_ERR(tcd)) {
1081 devres_free(ptr);
1082 return tcd;
1083 }
1084
1085 *ptr = tcd;
1086 devres_add(dev, ptr);
1087
1088 return tcd;
1089}
1090EXPORT_SYMBOL_GPL(devm_thermal_of_cooling_device_register);
1091
f11997fa
EV
1092static void __unbind(struct thermal_zone_device *tz, int mask,
1093 struct thermal_cooling_device *cdev)
1094{
1095 int i;
1096
1097 for (i = 0; i < tz->trips; i++)
1098 if (mask & (1 << i))
1099 thermal_zone_unbind_cooling_device(tz, i, cdev);
1100}
1101
203d3d4a 1102/**
38e7b549 1103 * thermal_cooling_device_unregister - removes a thermal cooling device
203d3d4a
ZR
1104 * @cdev: the thermal cooling device to remove.
1105 *
38e7b549
EV
1106 * thermal_cooling_device_unregister() must be called when a registered
1107 * thermal cooling device is no longer needed.
203d3d4a 1108 */
7e8ee1e9 1109void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
203d3d4a 1110{
7e8ee1e9
D
1111 int i;
1112 const struct thermal_zone_params *tzp;
203d3d4a
ZR
1113 struct thermal_zone_device *tz;
1114 struct thermal_cooling_device *pos = NULL;
1115
1116 if (!cdev)
1117 return;
1118
1119 mutex_lock(&thermal_list_lock);
1120 list_for_each_entry(pos, &thermal_cdev_list, node)
b659a30d
EV
1121 if (pos == cdev)
1122 break;
203d3d4a
ZR
1123 if (pos != cdev) {
1124 /* thermal cooling device not found */
1125 mutex_unlock(&thermal_list_lock);
1126 return;
1127 }
1128 list_del(&cdev->node);
7e8ee1e9
D
1129
1130 /* Unbind all thermal zones associated with 'this' cdev */
203d3d4a 1131 list_for_each_entry(tz, &thermal_tz_list, node) {
7e8ee1e9
D
1132 if (tz->ops->unbind) {
1133 tz->ops->unbind(tz, cdev);
1134 continue;
1135 }
1136
1137 if (!tz->tzp || !tz->tzp->tbp)
203d3d4a 1138 continue;
7e8ee1e9
D
1139
1140 tzp = tz->tzp;
1141 for (i = 0; i < tzp->num_tbps; i++) {
1142 if (tzp->tbp[i].cdev == cdev) {
1143 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1144 tzp->tbp[i].cdev = NULL;
1145 }
1146 }
203d3d4a 1147 }
7e8ee1e9 1148
203d3d4a 1149 mutex_unlock(&thermal_list_lock);
7e8ee1e9 1150
b31ef828 1151 ida_simple_remove(&thermal_cdev_ida, cdev->id);
3c587768 1152 device_del(&cdev->device);
8ea22951 1153 thermal_cooling_device_destroy_sysfs(cdev);
3c587768 1154 put_device(&cdev->device);
203d3d4a 1155}
910cb1e3 1156EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
203d3d4a 1157
90f5b5bb 1158static void bind_tz(struct thermal_zone_device *tz)
ce119f83 1159{
90f5b5bb
EV
1160 int i, ret;
1161 struct thermal_cooling_device *pos = NULL;
1162 const struct thermal_zone_params *tzp = tz->tzp;
ce119f83 1163
90f5b5bb 1164 if (!tzp && !tz->ops->bind)
ce119f83 1165 return;
c56f5c03 1166
90f5b5bb 1167 mutex_lock(&thermal_list_lock);
c56f5c03 1168
90f5b5bb
EV
1169 /* If there is ops->bind, try to use ops->bind */
1170 if (tz->ops->bind) {
1171 list_for_each_entry(pos, &thermal_cdev_list, node) {
1172 ret = tz->ops->bind(tz, pos);
1173 if (ret)
1174 print_bind_err_msg(tz, pos, ret);
27365a6c 1175 }
90f5b5bb 1176 goto exit;
27365a6c 1177 }
c56f5c03 1178
90f5b5bb
EV
1179 if (!tzp || !tzp->tbp)
1180 goto exit;
27365a6c 1181
90f5b5bb
EV
1182 list_for_each_entry(pos, &thermal_cdev_list, node) {
1183 for (i = 0; i < tzp->num_tbps; i++) {
1184 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
1185 continue;
1186 if (tzp->tbp[i].match(tz, pos))
1187 continue;
1188 tzp->tbp[i].cdev = pos;
1189 __bind(tz, tzp->tbp[i].trip_mask, pos,
1190 tzp->tbp[i].binding_limits,
1191 tzp->tbp[i].weight);
27365a6c 1192 }
c56f5c03 1193 }
90f5b5bb
EV
1194exit:
1195 mutex_unlock(&thermal_list_lock);
c56f5c03
D
1196}
1197
203d3d4a 1198/**
a00e55f9 1199 * thermal_zone_device_register() - register a new thermal zone device
203d3d4a
ZR
1200 * @type: the thermal zone device type
1201 * @trips: the number of trip points the thermal zone support
c56f5c03 1202 * @mask: a bit string indicating the writeablility of trip points
203d3d4a
ZR
1203 * @devdata: private device data
1204 * @ops: standard thermal zone device callbacks
50125a9b 1205 * @tzp: thermal zone platform parameters
b1569e99
MG
1206 * @passive_delay: number of milliseconds to wait between polls when
1207 * performing passive cooling
1208 * @polling_delay: number of milliseconds to wait between polls when checking
1209 * whether trip points have been crossed (0 for interrupt
1210 * driven systems)
203d3d4a 1211 *
a00e55f9
EV
1212 * This interface function adds a new thermal zone device (sensor) to
1213 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1214 * thermal cooling devices registered at the same time.
203d3d4a 1215 * thermal_zone_device_unregister() must be called when the device is no
1b7ddb84 1216 * longer needed. The passive cooling depends on the .get_trend() return value.
a00e55f9
EV
1217 *
1218 * Return: a pointer to the created struct thermal_zone_device or an
1219 * in case of error, an ERR_PTR. Caller must check return value with
1220 * IS_ERR*() helpers.
203d3d4a 1221 */
eb7be329
EV
1222struct thermal_zone_device *
1223thermal_zone_device_register(const char *type, int trips, int mask,
1224 void *devdata, struct thermal_zone_device_ops *ops,
1225 struct thermal_zone_params *tzp, int passive_delay,
1226 int polling_delay)
203d3d4a
ZR
1227{
1228 struct thermal_zone_device *tz;
03a971a2 1229 enum thermal_trip_type trip_type;
81ad4276 1230 int trip_temp;
adc8749b 1231 int id;
203d3d4a
ZR
1232 int result;
1233 int count;
e33df1d2 1234 struct thermal_governor *governor;
203d3d4a 1235
67eed44b
AK
1236 if (!type || strlen(type) == 0) {
1237 pr_err("Error: No thermal zone type defined\n");
54fa38cc 1238 return ERR_PTR(-EINVAL);
67eed44b 1239 }
54fa38cc 1240
67eed44b
AK
1241 if (type && strlen(type) >= THERMAL_NAME_LENGTH) {
1242 pr_err("Error: Thermal zone name (%s) too long, should be under %d chars\n",
1243 type, THERMAL_NAME_LENGTH);
3e6fda5c 1244 return ERR_PTR(-EINVAL);
67eed44b 1245 }
203d3d4a 1246
67eed44b
AK
1247 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips) {
1248 pr_err("Error: Incorrect number of thermal trips\n");
3e6fda5c 1249 return ERR_PTR(-EINVAL);
67eed44b 1250 }
203d3d4a 1251
67eed44b
AK
1252 if (!ops) {
1253 pr_err("Error: Thermal zone device ops not defined\n");
3e6fda5c 1254 return ERR_PTR(-EINVAL);
67eed44b 1255 }
203d3d4a 1256
83720d0b 1257 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
6b2aa51d
EV
1258 return ERR_PTR(-EINVAL);
1259
95e3ed15 1260 tz = kzalloc(sizeof(*tz), GFP_KERNEL);
203d3d4a 1261 if (!tz)
3e6fda5c 1262 return ERR_PTR(-ENOMEM);
203d3d4a 1263
2d374139 1264 INIT_LIST_HEAD(&tz->thermal_instances);
b31ef828 1265 ida_init(&tz->ida);
203d3d4a 1266 mutex_init(&tz->lock);
adc8749b
YH
1267 id = ida_simple_get(&thermal_tz_ida, 0, 0, GFP_KERNEL);
1268 if (id < 0) {
1269 result = id;
9d9ca1f9 1270 goto free_tz;
adc8749b 1271 }
203d3d4a 1272
adc8749b 1273 tz->id = id;
54fa38cc 1274 strlcpy(tz->type, type, sizeof(tz->type));
203d3d4a 1275 tz->ops = ops;
50125a9b 1276 tz->tzp = tzp;
203d3d4a
ZR
1277 tz->device.class = &thermal_class;
1278 tz->devdata = devdata;
1279 tz->trips = trips;
b1569e99
MG
1280 tz->passive_delay = passive_delay;
1281 tz->polling_delay = polling_delay;
1c600861 1282
4d0fe749 1283 /* sys I/F */
1c600861 1284 /* Add nodes that are always present via .groups */
4d0fe749
EV
1285 result = thermal_zone_create_device_groups(tz, mask);
1286 if (result)
9d9ca1f9 1287 goto remove_id;
4d0fe749 1288
4511f716
CY
1289 /* A new thermal zone needs to be updated anyway. */
1290 atomic_set(&tz->need_update, 1);
b1569e99 1291
354655ea 1292 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
203d3d4a 1293 result = device_register(&tz->device);
9d9ca1f9 1294 if (result)
adc8749b 1295 goto release_device;
203d3d4a 1296
203d3d4a 1297 for (count = 0; count < trips; count++) {
81ad4276
ZR
1298 if (tz->ops->get_trip_type(tz, count, &trip_type))
1299 set_bit(count, &tz->trips_disabled);
81ad4276
ZR
1300 if (tz->ops->get_trip_temp(tz, count, &trip_temp))
1301 set_bit(count, &tz->trips_disabled);
1302 /* Check for bogus trip points */
1303 if (trip_temp == 0)
1304 set_bit(count, &tz->trips_disabled);
203d3d4a
ZR
1305 }
1306
a4a15485
D
1307 /* Update 'this' zone's governor information */
1308 mutex_lock(&thermal_governor_lock);
1309
1310 if (tz->tzp)
e33df1d2 1311 governor = __find_governor(tz->tzp->governor_name);
a4a15485 1312 else
e33df1d2
JM
1313 governor = def_governor;
1314
1315 result = thermal_set_governor(tz, governor);
1316 if (result) {
1317 mutex_unlock(&thermal_governor_lock);
1318 goto unregister;
1319 }
a4a15485
D
1320
1321 mutex_unlock(&thermal_governor_lock);
1322
ccba4ffd
EV
1323 if (!tz->tzp || !tz->tzp->no_hwmon) {
1324 result = thermal_add_hwmon_sysfs(tz);
1325 if (result)
1326 goto unregister;
1327 }
e68b16ab 1328
203d3d4a
ZR
1329 mutex_lock(&thermal_list_lock);
1330 list_add_tail(&tz->node, &thermal_tz_list);
203d3d4a
ZR
1331 mutex_unlock(&thermal_list_lock);
1332
7e8ee1e9
D
1333 /* Bind cooling devices for this zone */
1334 bind_tz(tz);
1335
b659a30d 1336 INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check);
b1569e99 1337
bb431ba2 1338 thermal_zone_device_reset(tz);
4511f716
CY
1339 /* Update the new thermal zone and mark it as already updated. */
1340 if (atomic_cmpxchg(&tz->need_update, 1, 0))
0e70f466 1341 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
b1569e99 1342
14015860 1343 return tz;
203d3d4a 1344
caca8b80 1345unregister:
adc8749b
YH
1346 device_del(&tz->device);
1347release_device:
1348 put_device(&tz->device);
1349 tz = NULL;
9d9ca1f9 1350remove_id:
adc8749b 1351 ida_simple_remove(&thermal_tz_ida, id);
9d9ca1f9
CJ
1352free_tz:
1353 kfree(tz);
1354 return ERR_PTR(result);
203d3d4a 1355}
910cb1e3 1356EXPORT_SYMBOL_GPL(thermal_zone_device_register);
203d3d4a
ZR
1357
1358/**
1359 * thermal_device_unregister - removes the registered thermal zone device
203d3d4a
ZR
1360 * @tz: the thermal zone device to remove
1361 */
1362void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1363{
7e8ee1e9
D
1364 int i;
1365 const struct thermal_zone_params *tzp;
203d3d4a
ZR
1366 struct thermal_cooling_device *cdev;
1367 struct thermal_zone_device *pos = NULL;
203d3d4a
ZR
1368
1369 if (!tz)
1370 return;
1371
7e8ee1e9
D
1372 tzp = tz->tzp;
1373
203d3d4a
ZR
1374 mutex_lock(&thermal_list_lock);
1375 list_for_each_entry(pos, &thermal_tz_list, node)
b659a30d
EV
1376 if (pos == tz)
1377 break;
203d3d4a
ZR
1378 if (pos != tz) {
1379 /* thermal zone device not found */
1380 mutex_unlock(&thermal_list_lock);
1381 return;
1382 }
1383 list_del(&tz->node);
7e8ee1e9
D
1384
1385 /* Unbind all cdevs associated with 'this' thermal zone */
1386 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1387 if (tz->ops->unbind) {
1388 tz->ops->unbind(tz, cdev);
1389 continue;
1390 }
1391
1392 if (!tzp || !tzp->tbp)
1393 break;
1394
1395 for (i = 0; i < tzp->num_tbps; i++) {
1396 if (tzp->tbp[i].cdev == cdev) {
1397 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1398 tzp->tbp[i].cdev = NULL;
1399 }
1400 }
1401 }
1402
203d3d4a
ZR
1403 mutex_unlock(&thermal_list_lock);
1404
163b00cd 1405 cancel_delayed_work_sync(&tz->poll_queue);
b1569e99 1406
e33df1d2 1407 thermal_set_governor(tz, NULL);
203d3d4a 1408
e68b16ab 1409 thermal_remove_hwmon_sysfs(tz);
b31ef828
MW
1410 ida_simple_remove(&thermal_tz_ida, tz->id);
1411 ida_destroy(&tz->ida);
203d3d4a
ZR
1412 mutex_destroy(&tz->lock);
1413 device_unregister(&tz->device);
203d3d4a 1414}
910cb1e3 1415EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
203d3d4a 1416
63c4d919
EV
1417/**
1418 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1419 * @name: thermal zone name to fetch the temperature
1420 *
1421 * When only one zone is found with the passed name, returns a reference to it.
1422 *
1423 * Return: On success returns a reference to an unique thermal zone with
1424 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1425 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1426 */
1427struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1428{
1429 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1430 unsigned int found = 0;
1431
1432 if (!name)
1433 goto exit;
1434
1435 mutex_lock(&thermal_list_lock);
1436 list_for_each_entry(pos, &thermal_tz_list, node)
484ac2f3 1437 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
63c4d919
EV
1438 found++;
1439 ref = pos;
1440 }
1441 mutex_unlock(&thermal_list_lock);
1442
1443 /* nothing has been found, thus an error code for it */
1444 if (found == 0)
1445 ref = ERR_PTR(-ENODEV);
1446 else if (found > 1)
1447 /* Success only when an unique zone is found */
1448 ref = ERR_PTR(-EEXIST);
1449
1450exit:
1451 return ref;
1452}
1453EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1454
ff140fea 1455static int thermal_pm_notify(struct notifier_block *nb,
eb7be329 1456 unsigned long mode, void *_unused)
ff140fea
ZR
1457{
1458 struct thermal_zone_device *tz;
1459
1460 switch (mode) {
1461 case PM_HIBERNATION_PREPARE:
1462 case PM_RESTORE_PREPARE:
1463 case PM_SUSPEND_PREPARE:
1464 atomic_set(&in_suspend, 1);
1465 break;
1466 case PM_POST_HIBERNATION:
1467 case PM_POST_RESTORE:
1468 case PM_POST_SUSPEND:
1469 atomic_set(&in_suspend, 0);
1470 list_for_each_entry(tz, &thermal_tz_list, node) {
1ee14820 1471 if (tz->mode == THERMAL_DEVICE_DISABLED)
ff54bbd1
WW
1472 continue;
1473
964f4843 1474 thermal_zone_device_init(tz);
0e70f466
SP
1475 thermal_zone_device_update(tz,
1476 THERMAL_EVENT_UNSPECIFIED);
ff140fea
ZR
1477 }
1478 break;
1479 default:
1480 break;
1481 }
1482 return 0;
1483}
1484
1485static struct notifier_block thermal_pm_nb = {
1486 .notifier_call = thermal_pm_notify,
1487};
1488
203d3d4a
ZR
1489static int __init thermal_init(void)
1490{
80a26a5c
ZR
1491 int result;
1492
e441fd68 1493 mutex_init(&poweroff_lock);
80a26a5c
ZR
1494 result = thermal_register_governors();
1495 if (result)
1496 goto error;
203d3d4a
ZR
1497
1498 result = class_register(&thermal_class);
80a26a5c
ZR
1499 if (result)
1500 goto unregister_governors;
1501
4e5e4705
EV
1502 result = of_parse_thermal_zones();
1503 if (result)
f96c8e50 1504 goto unregister_class;
4e5e4705 1505
ff140fea
ZR
1506 result = register_pm_notifier(&thermal_pm_nb);
1507 if (result)
1508 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
1509 result);
1510
80a26a5c
ZR
1511 return 0;
1512
80a26a5c
ZR
1513unregister_class:
1514 class_unregister(&thermal_class);
9d367e5e
LH
1515unregister_governors:
1516 thermal_unregister_governors();
80a26a5c 1517error:
b31ef828
MW
1518 ida_destroy(&thermal_tz_ida);
1519 ida_destroy(&thermal_cdev_ida);
80a26a5c
ZR
1520 mutex_destroy(&thermal_list_lock);
1521 mutex_destroy(&thermal_governor_lock);
e441fd68 1522 mutex_destroy(&poweroff_lock);
203d3d4a
ZR
1523 return result;
1524}
ae16a688 1525core_initcall(thermal_init);