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