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