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