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