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