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