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