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