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