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