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