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