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