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