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