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