OSDN Git Service

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