Thermal: Allow first cooling device update
[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 passive_delay_store(struct device *dev, struct device_attribute *attr,
677                     const char *buf, size_t count)
678 {
679         struct thermal_zone_device *tz = to_thermal_zone(dev);
680         int passive_delay;
681
682         if (!sscanf(buf, "%d\n", &passive_delay))
683                 return -EINVAL;
684
685         tz->passive_delay = passive_delay;
686         thermal_zone_device_update(tz);
687         return count;
688 }
689
690 static ssize_t
691 passive_delay_show(struct device *dev, struct device_attribute *attr,
692                    char *buf)
693 {
694         struct thermal_zone_device *tz = to_thermal_zone(dev);
695
696         return sprintf(buf, "%d\n", tz->passive_delay);
697 }
698
699 static ssize_t
700 policy_store(struct device *dev, struct device_attribute *attr,
701                     const char *buf, size_t count)
702 {
703         int ret = -EINVAL;
704         struct thermal_zone_device *tz = to_thermal_zone(dev);
705         struct thermal_governor *gov;
706         char name[THERMAL_NAME_LENGTH];
707
708         mutex_lock(&thermal_governor_lock);
709
710         if ((strlen(buf) >= THERMAL_NAME_LENGTH) && !sscanf(buf, "%s\n", name))
711                 goto exit;
712
713         gov = __find_governor((const char *)name);
714         if (!gov)
715                 goto exit;
716
717         tz->governor = gov;
718         ret = count;
719
720 exit:
721         mutex_unlock(&thermal_governor_lock);
722         return ret;
723 }
724
725 static ssize_t
726 policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
727 {
728         struct thermal_zone_device *tz = to_thermal_zone(dev);
729
730         return sprintf(buf, "%s\n", tz->governor->name);
731 }
732
733 static ssize_t
734 available_policies_show(struct device *dev, struct device_attribute *devattr,
735                         char *buf)
736 {
737         struct thermal_governor *pos;
738         ssize_t count = 0;
739
740         mutex_lock(&thermal_governor_lock);
741
742         list_for_each_entry(pos, &thermal_governor_list, governor_list)
743                 count += sprintf(buf + count, "%s ", pos->name);
744         count += sprintf(buf + count, "\n");
745
746         mutex_unlock(&thermal_governor_lock);
747         return count;
748 }
749
750 static DEVICE_ATTR(type, 0444, type_show, NULL);
751 static DEVICE_ATTR(temp, 0444, temp_show, NULL);
752 static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
753 static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
754 static DEVICE_ATTR(passive_delay, S_IRUGO | S_IWUSR,
755                    passive_delay_show, passive_delay_store);
756 static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
757 static DEVICE_ATTR(available_policies, S_IRUGO, available_policies_show, NULL);
758
759 /* sys I/F for cooling device */
760 #define to_cooling_device(_dev) \
761         container_of(_dev, struct thermal_cooling_device, device)
762
763 static ssize_t
764 thermal_cooling_device_type_show(struct device *dev,
765                                  struct device_attribute *attr, char *buf)
766 {
767         struct thermal_cooling_device *cdev = to_cooling_device(dev);
768
769         return sprintf(buf, "%s\n", cdev->type);
770 }
771
772 static ssize_t
773 thermal_cooling_device_max_state_show(struct device *dev,
774                                       struct device_attribute *attr, char *buf)
775 {
776         struct thermal_cooling_device *cdev = to_cooling_device(dev);
777         unsigned long state;
778         int ret;
779
780         ret = cdev->ops->get_max_state(cdev, &state);
781         if (ret)
782                 return ret;
783         return sprintf(buf, "%ld\n", state);
784 }
785
786 static ssize_t
787 thermal_cooling_device_cur_state_show(struct device *dev,
788                                       struct device_attribute *attr, char *buf)
789 {
790         struct thermal_cooling_device *cdev = to_cooling_device(dev);
791         unsigned long state;
792         int ret;
793
794         ret = cdev->ops->get_cur_state(cdev, &state);
795         if (ret)
796                 return ret;
797         return sprintf(buf, "%ld\n", state);
798 }
799
800 static ssize_t
801 thermal_cooling_device_cur_state_store(struct device *dev,
802                                        struct device_attribute *attr,
803                                        const char *buf, size_t count)
804 {
805         struct thermal_cooling_device *cdev = to_cooling_device(dev);
806         unsigned long state;
807         int result;
808
809         if (!sscanf(buf, "%ld\n", &state))
810                 return -EINVAL;
811
812         if ((long)state < 0)
813                 return -EINVAL;
814
815         result = cdev->ops->set_cur_state(cdev, state);
816         if (result)
817                 return result;
818         return count;
819 }
820
821 static struct device_attribute dev_attr_cdev_type =
822 __ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
823 static DEVICE_ATTR(max_state, 0444,
824                    thermal_cooling_device_max_state_show, NULL);
825 static DEVICE_ATTR(cur_state, 0644,
826                    thermal_cooling_device_cur_state_show,
827                    thermal_cooling_device_cur_state_store);
828
829 static ssize_t
830 thermal_cooling_device_trip_point_show(struct device *dev,
831                                        struct device_attribute *attr, char *buf)
832 {
833         struct thermal_instance *instance;
834
835         instance =
836             container_of(attr, struct thermal_instance, attr);
837
838         if (instance->trip == THERMAL_TRIPS_NONE)
839                 return sprintf(buf, "-1\n");
840         else
841                 return sprintf(buf, "%d\n", instance->trip);
842 }
843
844 /* Device management */
845
846 #if defined(CONFIG_THERMAL_HWMON)
847
848 /* hwmon sys I/F */
849 #include <linux/hwmon.h>
850
851 /* thermal zone devices with the same type share one hwmon device */
852 struct thermal_hwmon_device {
853         char type[THERMAL_NAME_LENGTH];
854         struct device *device;
855         int count;
856         struct list_head tz_list;
857         struct list_head node;
858 };
859
860 struct thermal_hwmon_attr {
861         struct device_attribute attr;
862         char name[16];
863 };
864
865 /* one temperature input for each thermal zone */
866 struct thermal_hwmon_temp {
867         struct list_head hwmon_node;
868         struct thermal_zone_device *tz;
869         struct thermal_hwmon_attr temp_input;   /* hwmon sys attr */
870         struct thermal_hwmon_attr temp_crit;    /* hwmon sys attr */
871 };
872
873 static LIST_HEAD(thermal_hwmon_list);
874
875 static ssize_t
876 name_show(struct device *dev, struct device_attribute *attr, char *buf)
877 {
878         struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
879         return sprintf(buf, "%s\n", hwmon->type);
880 }
881 static DEVICE_ATTR(name, 0444, name_show, NULL);
882
883 static ssize_t
884 temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
885 {
886         long temperature;
887         int ret;
888         struct thermal_hwmon_attr *hwmon_attr
889                         = container_of(attr, struct thermal_hwmon_attr, attr);
890         struct thermal_hwmon_temp *temp
891                         = container_of(hwmon_attr, struct thermal_hwmon_temp,
892                                        temp_input);
893         struct thermal_zone_device *tz = temp->tz;
894
895         ret = tz->ops->get_temp(tz, &temperature);
896
897         if (ret)
898                 return ret;
899
900         return sprintf(buf, "%ld\n", temperature);
901 }
902
903 static ssize_t
904 temp_crit_show(struct device *dev, struct device_attribute *attr,
905                 char *buf)
906 {
907         struct thermal_hwmon_attr *hwmon_attr
908                         = container_of(attr, struct thermal_hwmon_attr, attr);
909         struct thermal_hwmon_temp *temp
910                         = container_of(hwmon_attr, struct thermal_hwmon_temp,
911                                        temp_crit);
912         struct thermal_zone_device *tz = temp->tz;
913         long temperature;
914         int ret;
915
916         ret = tz->ops->get_trip_temp(tz, 0, &temperature);
917         if (ret)
918                 return ret;
919
920         return sprintf(buf, "%ld\n", temperature);
921 }
922
923
924 static struct thermal_hwmon_device *
925 thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
926 {
927         struct thermal_hwmon_device *hwmon;
928
929         mutex_lock(&thermal_list_lock);
930         list_for_each_entry(hwmon, &thermal_hwmon_list, node)
931                 if (!strcmp(hwmon->type, tz->type)) {
932                         mutex_unlock(&thermal_list_lock);
933                         return hwmon;
934                 }
935         mutex_unlock(&thermal_list_lock);
936
937         return NULL;
938 }
939
940 /* Find the temperature input matching a given thermal zone */
941 static struct thermal_hwmon_temp *
942 thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
943                           const struct thermal_zone_device *tz)
944 {
945         struct thermal_hwmon_temp *temp;
946
947         mutex_lock(&thermal_list_lock);
948         list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
949                 if (temp->tz == tz) {
950                         mutex_unlock(&thermal_list_lock);
951                         return temp;
952                 }
953         mutex_unlock(&thermal_list_lock);
954
955         return NULL;
956 }
957
958 static int
959 thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
960 {
961         struct thermal_hwmon_device *hwmon;
962         struct thermal_hwmon_temp *temp;
963         int new_hwmon_device = 1;
964         int result;
965
966         hwmon = thermal_hwmon_lookup_by_type(tz);
967         if (hwmon) {
968                 new_hwmon_device = 0;
969                 goto register_sys_interface;
970         }
971
972         hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
973         if (!hwmon)
974                 return -ENOMEM;
975
976         INIT_LIST_HEAD(&hwmon->tz_list);
977         strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
978         hwmon->device = hwmon_device_register(NULL);
979         if (IS_ERR(hwmon->device)) {
980                 result = PTR_ERR(hwmon->device);
981                 goto free_mem;
982         }
983         dev_set_drvdata(hwmon->device, hwmon);
984         result = device_create_file(hwmon->device, &dev_attr_name);
985         if (result)
986                 goto free_mem;
987
988  register_sys_interface:
989         temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL);
990         if (!temp) {
991                 result = -ENOMEM;
992                 goto unregister_name;
993         }
994
995         temp->tz = tz;
996         hwmon->count++;
997
998         snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
999                  "temp%d_input", hwmon->count);
1000         temp->temp_input.attr.attr.name = temp->temp_input.name;
1001         temp->temp_input.attr.attr.mode = 0444;
1002         temp->temp_input.attr.show = temp_input_show;
1003         sysfs_attr_init(&temp->temp_input.attr.attr);
1004         result = device_create_file(hwmon->device, &temp->temp_input.attr);
1005         if (result)
1006                 goto free_temp_mem;
1007
1008         if (tz->ops->get_crit_temp) {
1009                 unsigned long temperature;
1010                 if (!tz->ops->get_crit_temp(tz, &temperature)) {
1011                         snprintf(temp->temp_crit.name,
1012                                  sizeof(temp->temp_crit.name),
1013                                 "temp%d_crit", hwmon->count);
1014                         temp->temp_crit.attr.attr.name = temp->temp_crit.name;
1015                         temp->temp_crit.attr.attr.mode = 0444;
1016                         temp->temp_crit.attr.show = temp_crit_show;
1017                         sysfs_attr_init(&temp->temp_crit.attr.attr);
1018                         result = device_create_file(hwmon->device,
1019                                                     &temp->temp_crit.attr);
1020                         if (result)
1021                                 goto unregister_input;
1022                 }
1023         }
1024
1025         mutex_lock(&thermal_list_lock);
1026         if (new_hwmon_device)
1027                 list_add_tail(&hwmon->node, &thermal_hwmon_list);
1028         list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
1029         mutex_unlock(&thermal_list_lock);
1030
1031         return 0;
1032
1033  unregister_input:
1034         device_remove_file(hwmon->device, &temp->temp_input.attr);
1035  free_temp_mem:
1036         kfree(temp);
1037  unregister_name:
1038         if (new_hwmon_device) {
1039                 device_remove_file(hwmon->device, &dev_attr_name);
1040                 hwmon_device_unregister(hwmon->device);
1041         }
1042  free_mem:
1043         if (new_hwmon_device)
1044                 kfree(hwmon);
1045
1046         return result;
1047 }
1048
1049 static void
1050 thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
1051 {
1052         struct thermal_hwmon_device *hwmon;
1053         struct thermal_hwmon_temp *temp;
1054
1055         hwmon = thermal_hwmon_lookup_by_type(tz);
1056         if (unlikely(!hwmon)) {
1057                 /* Should never happen... */
1058                 dev_dbg(&tz->device, "hwmon device lookup failed!\n");
1059                 return;
1060         }
1061
1062         temp = thermal_hwmon_lookup_temp(hwmon, tz);
1063         if (unlikely(!temp)) {
1064                 /* Should never happen... */
1065                 dev_dbg(&tz->device, "temperature input lookup failed!\n");
1066                 return;
1067         }
1068
1069         device_remove_file(hwmon->device, &temp->temp_input.attr);
1070         if (tz->ops->get_crit_temp)
1071                 device_remove_file(hwmon->device, &temp->temp_crit.attr);
1072
1073         mutex_lock(&thermal_list_lock);
1074         list_del(&temp->hwmon_node);
1075         kfree(temp);
1076         if (!list_empty(&hwmon->tz_list)) {
1077                 mutex_unlock(&thermal_list_lock);
1078                 return;
1079         }
1080         list_del(&hwmon->node);
1081         mutex_unlock(&thermal_list_lock);
1082
1083         device_remove_file(hwmon->device, &dev_attr_name);
1084         hwmon_device_unregister(hwmon->device);
1085         kfree(hwmon);
1086 }
1087 #else
1088 static int
1089 thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
1090 {
1091         return 0;
1092 }
1093
1094 static void
1095 thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
1096 {
1097 }
1098 #endif
1099
1100 /**
1101  * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
1102  * @tz:         thermal zone device
1103  * @trip:       indicates which trip point the cooling devices is
1104  *              associated with in this thermal zone.
1105  * @cdev:       thermal cooling device
1106  *
1107  * This function is usually called in the thermal zone device .bind callback.
1108  */
1109 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
1110                                      int trip,
1111                                      struct thermal_cooling_device *cdev,
1112                                      unsigned long upper, unsigned long lower)
1113 {
1114         struct thermal_instance *dev;
1115         struct thermal_instance *pos;
1116         struct thermal_zone_device *pos1;
1117         struct thermal_cooling_device *pos2;
1118         unsigned long max_state;
1119         int result;
1120
1121         if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
1122                 return -EINVAL;
1123
1124         list_for_each_entry(pos1, &thermal_tz_list, node) {
1125                 if (pos1 == tz)
1126                         break;
1127         }
1128         list_for_each_entry(pos2, &thermal_cdev_list, node) {
1129                 if (pos2 == cdev)
1130                         break;
1131         }
1132
1133         if (tz != pos1 || cdev != pos2)
1134                 return -EINVAL;
1135
1136         cdev->ops->get_max_state(cdev, &max_state);
1137
1138         /* lower default 0, upper default max_state */
1139         lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
1140         upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
1141
1142         if (lower > upper || upper > max_state)
1143                 return -EINVAL;
1144
1145         dev =
1146             kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
1147         if (!dev)
1148                 return -ENOMEM;
1149         dev->tz = tz;
1150         dev->cdev = cdev;
1151         dev->trip = trip;
1152         dev->upper = upper;
1153         dev->lower = lower;
1154         dev->target = THERMAL_NO_TARGET;
1155
1156         result = get_idr(&tz->idr, &tz->lock, &dev->id);
1157         if (result)
1158                 goto free_mem;
1159
1160         sprintf(dev->name, "cdev%d", dev->id);
1161         result =
1162             sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
1163         if (result)
1164                 goto release_idr;
1165
1166         sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
1167         sysfs_attr_init(&dev->attr.attr);
1168         dev->attr.attr.name = dev->attr_name;
1169         dev->attr.attr.mode = 0444;
1170         dev->attr.show = thermal_cooling_device_trip_point_show;
1171         result = device_create_file(&tz->device, &dev->attr);
1172         if (result)
1173                 goto remove_symbol_link;
1174
1175         mutex_lock(&tz->lock);
1176         mutex_lock(&cdev->lock);
1177         list_for_each_entry(pos, &tz->thermal_instances, tz_node)
1178             if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1179                 result = -EEXIST;
1180                 break;
1181         }
1182         if (!result) {
1183                 list_add_tail(&dev->tz_node, &tz->thermal_instances);
1184                 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
1185         }
1186         mutex_unlock(&cdev->lock);
1187         mutex_unlock(&tz->lock);
1188
1189         if (!result)
1190                 return 0;
1191
1192         device_remove_file(&tz->device, &dev->attr);
1193 remove_symbol_link:
1194         sysfs_remove_link(&tz->device.kobj, dev->name);
1195 release_idr:
1196         release_idr(&tz->idr, &tz->lock, dev->id);
1197 free_mem:
1198         kfree(dev);
1199         return result;
1200 }
1201 EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
1202
1203 /**
1204  * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
1205  * @tz:         thermal zone device
1206  * @trip:       indicates which trip point the cooling devices is
1207  *              associated with in this thermal zone.
1208  * @cdev:       thermal cooling device
1209  *
1210  * This function is usually called in the thermal zone device .unbind callback.
1211  */
1212 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1213                                        int trip,
1214                                        struct thermal_cooling_device *cdev)
1215 {
1216         struct thermal_instance *pos, *next;
1217
1218         mutex_lock(&tz->lock);
1219         mutex_lock(&cdev->lock);
1220         list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
1221                 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1222                         list_del(&pos->tz_node);
1223                         list_del(&pos->cdev_node);
1224                         mutex_unlock(&cdev->lock);
1225                         mutex_unlock(&tz->lock);
1226                         goto unbind;
1227                 }
1228         }
1229         mutex_unlock(&cdev->lock);
1230         mutex_unlock(&tz->lock);
1231
1232         return -ENODEV;
1233
1234 unbind:
1235         device_remove_file(&tz->device, &pos->attr);
1236         sysfs_remove_link(&tz->device.kobj, pos->name);
1237         release_idr(&tz->idr, &tz->lock, pos->id);
1238         kfree(pos);
1239         return 0;
1240 }
1241 EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
1242
1243 static void thermal_release(struct device *dev)
1244 {
1245         struct thermal_zone_device *tz;
1246         struct thermal_cooling_device *cdev;
1247
1248         if (!strncmp(dev_name(dev), "thermal_zone",
1249                      sizeof("thermal_zone") - 1)) {
1250                 tz = to_thermal_zone(dev);
1251                 kfree(tz);
1252         } else {
1253                 cdev = to_cooling_device(dev);
1254                 kfree(cdev);
1255         }
1256 }
1257
1258 static struct class thermal_class = {
1259         .name = "thermal",
1260         .dev_release = thermal_release,
1261 };
1262
1263 /**
1264  * thermal_cooling_device_register - register a new thermal cooling device
1265  * @type:       the thermal cooling device type.
1266  * @devdata:    device private data.
1267  * @ops:                standard thermal cooling devices callbacks.
1268  */
1269 struct thermal_cooling_device *
1270 thermal_cooling_device_register(char *type, void *devdata,
1271                                 const struct thermal_cooling_device_ops *ops)
1272 {
1273         struct thermal_cooling_device *cdev;
1274         int result;
1275
1276         if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1277                 return ERR_PTR(-EINVAL);
1278
1279         if (!ops || !ops->get_max_state || !ops->get_cur_state ||
1280             !ops->set_cur_state)
1281                 return ERR_PTR(-EINVAL);
1282
1283         cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1284         if (!cdev)
1285                 return ERR_PTR(-ENOMEM);
1286
1287         result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1288         if (result) {
1289                 kfree(cdev);
1290                 return ERR_PTR(result);
1291         }
1292
1293         strcpy(cdev->type, type ? : "");
1294         mutex_init(&cdev->lock);
1295         INIT_LIST_HEAD(&cdev->thermal_instances);
1296         cdev->ops = ops;
1297         cdev->updated = false;
1298         cdev->device.class = &thermal_class;
1299         cdev->devdata = devdata;
1300         dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
1301         result = device_register(&cdev->device);
1302         if (result) {
1303                 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1304                 kfree(cdev);
1305                 return ERR_PTR(result);
1306         }
1307
1308         /* sys I/F */
1309         if (type) {
1310                 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
1311                 if (result)
1312                         goto unregister;
1313         }
1314
1315         result = device_create_file(&cdev->device, &dev_attr_max_state);
1316         if (result)
1317                 goto unregister;
1318
1319         result = device_create_file(&cdev->device, &dev_attr_cur_state);
1320         if (result)
1321                 goto unregister;
1322
1323         /* Add 'this' new cdev to the global cdev list */
1324         mutex_lock(&thermal_list_lock);
1325         list_add(&cdev->node, &thermal_cdev_list);
1326         mutex_unlock(&thermal_list_lock);
1327
1328         /* Update binding information for 'this' new cdev */
1329         bind_cdev(cdev);
1330
1331         return cdev;
1332
1333 unregister:
1334         release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1335         device_unregister(&cdev->device);
1336         return ERR_PTR(result);
1337 }
1338 EXPORT_SYMBOL(thermal_cooling_device_register);
1339
1340 /**
1341  * thermal_cooling_device_unregister - removes the registered thermal cooling device
1342  * @cdev:       the thermal cooling device to remove.
1343  *
1344  * thermal_cooling_device_unregister() must be called when the device is no
1345  * longer needed.
1346  */
1347 void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
1348 {
1349         int i;
1350         const struct thermal_zone_params *tzp;
1351         struct thermal_zone_device *tz;
1352         struct thermal_cooling_device *pos = NULL;
1353
1354         if (!cdev)
1355                 return;
1356
1357         mutex_lock(&thermal_list_lock);
1358         list_for_each_entry(pos, &thermal_cdev_list, node)
1359             if (pos == cdev)
1360                 break;
1361         if (pos != cdev) {
1362                 /* thermal cooling device not found */
1363                 mutex_unlock(&thermal_list_lock);
1364                 return;
1365         }
1366         list_del(&cdev->node);
1367
1368         /* Unbind all thermal zones associated with 'this' cdev */
1369         list_for_each_entry(tz, &thermal_tz_list, node) {
1370                 if (tz->ops->unbind) {
1371                         tz->ops->unbind(tz, cdev);
1372                         continue;
1373                 }
1374
1375                 if (!tz->tzp || !tz->tzp->tbp)
1376                         continue;
1377
1378                 tzp = tz->tzp;
1379                 for (i = 0; i < tzp->num_tbps; i++) {
1380                         if (tzp->tbp[i].cdev == cdev) {
1381                                 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1382                                 tzp->tbp[i].cdev = NULL;
1383                         }
1384                 }
1385         }
1386
1387         mutex_unlock(&thermal_list_lock);
1388
1389         if (cdev->type[0])
1390                 device_remove_file(&cdev->device, &dev_attr_cdev_type);
1391         device_remove_file(&cdev->device, &dev_attr_max_state);
1392         device_remove_file(&cdev->device, &dev_attr_cur_state);
1393
1394         release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1395         device_unregister(&cdev->device);
1396         return;
1397 }
1398 EXPORT_SYMBOL(thermal_cooling_device_unregister);
1399
1400 void thermal_cdev_update(struct thermal_cooling_device *cdev)
1401 {
1402         struct thermal_instance *instance;
1403         unsigned long target = 0;
1404
1405         /* cooling device is updated*/
1406         if (cdev->updated)
1407                 return;
1408
1409         mutex_lock(&cdev->lock);
1410         /* Make sure cdev enters the deepest cooling state */
1411         list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1412                 if (instance->target == THERMAL_NO_TARGET)
1413                         continue;
1414                 if (instance->target > target)
1415                         target = instance->target;
1416         }
1417         mutex_unlock(&cdev->lock);
1418         cdev->ops->set_cur_state(cdev, target);
1419         cdev->updated = true;
1420 }
1421 EXPORT_SYMBOL(thermal_cdev_update);
1422
1423 /**
1424  * notify_thermal_framework - Sensor drivers use this API to notify framework
1425  * @tz:         thermal zone device
1426  * @trip:       indicates which trip point has been crossed
1427  *
1428  * This function handles the trip events from sensor drivers. It starts
1429  * throttling the cooling devices according to the policy configured.
1430  * For CRITICAL and HOT trip points, this notifies the respective drivers,
1431  * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1432  * The throttling policy is based on the configured platform data; if no
1433  * platform data is provided, this uses the step_wise throttling policy.
1434  */
1435 void notify_thermal_framework(struct thermal_zone_device *tz, int trip)
1436 {
1437         handle_thermal_trip(tz, trip);
1438 }
1439 EXPORT_SYMBOL(notify_thermal_framework);
1440
1441 struct thermal_zone_device *thermal_zone_device_find(void *data,
1442         int (*match)(struct thermal_zone_device *, void *))
1443 {
1444         struct thermal_zone_device *thz;
1445
1446         mutex_lock(&thermal_list_lock);
1447         list_for_each_entry(thz, &thermal_tz_list, node)
1448                 if (match(thz, data)) {
1449                         mutex_unlock(&thermal_list_lock);
1450                         return thz;
1451                 }
1452
1453         mutex_unlock(&thermal_list_lock);
1454         return NULL;
1455 }
1456 EXPORT_SYMBOL(thermal_zone_device_find);
1457
1458 /**
1459  * create_trip_attrs - create attributes for trip points
1460  * @tz:         the thermal zone device
1461  * @mask:       Writeable trip point bitmap.
1462  */
1463 static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1464 {
1465         int indx;
1466         int size = sizeof(struct thermal_attr) * tz->trips;
1467
1468         tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
1469         if (!tz->trip_type_attrs)
1470                 return -ENOMEM;
1471
1472         tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
1473         if (!tz->trip_temp_attrs) {
1474                 kfree(tz->trip_type_attrs);
1475                 return -ENOMEM;
1476         }
1477
1478         if (tz->ops->get_trip_hyst) {
1479                 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1480                 if (!tz->trip_hyst_attrs) {
1481                         kfree(tz->trip_type_attrs);
1482                         kfree(tz->trip_temp_attrs);
1483                         return -ENOMEM;
1484                 }
1485         }
1486
1487
1488         for (indx = 0; indx < tz->trips; indx++) {
1489                 /* create trip type attribute */
1490                 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1491                          "trip_point_%d_type", indx);
1492
1493                 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1494                 tz->trip_type_attrs[indx].attr.attr.name =
1495                                                 tz->trip_type_attrs[indx].name;
1496                 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1497                 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1498
1499                 device_create_file(&tz->device,
1500                                    &tz->trip_type_attrs[indx].attr);
1501
1502                 /* create trip temp attribute */
1503                 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1504                          "trip_point_%d_temp", indx);
1505
1506                 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1507                 tz->trip_temp_attrs[indx].attr.attr.name =
1508                                                 tz->trip_temp_attrs[indx].name;
1509                 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1510                 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1511                 if (mask & (1 << indx)) {
1512                         tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1513                         tz->trip_temp_attrs[indx].attr.store =
1514                                                         trip_point_temp_store;
1515                 }
1516
1517                 device_create_file(&tz->device,
1518                                    &tz->trip_temp_attrs[indx].attr);
1519
1520                 /* create Optional trip hyst attribute */
1521                 if (!tz->ops->get_trip_hyst)
1522                         continue;
1523                 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1524                          "trip_point_%d_hyst", indx);
1525
1526                 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1527                 tz->trip_hyst_attrs[indx].attr.attr.name =
1528                                         tz->trip_hyst_attrs[indx].name;
1529                 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1530                 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1531                 if (tz->ops->set_trip_hyst) {
1532                         tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1533                         tz->trip_hyst_attrs[indx].attr.store =
1534                                         trip_point_hyst_store;
1535                 }
1536
1537                 device_create_file(&tz->device,
1538                                    &tz->trip_hyst_attrs[indx].attr);
1539         }
1540         return 0;
1541 }
1542
1543 static void remove_trip_attrs(struct thermal_zone_device *tz)
1544 {
1545         int indx;
1546
1547         for (indx = 0; indx < tz->trips; indx++) {
1548                 device_remove_file(&tz->device,
1549                                    &tz->trip_type_attrs[indx].attr);
1550                 device_remove_file(&tz->device,
1551                                    &tz->trip_temp_attrs[indx].attr);
1552                 if (tz->ops->get_trip_hyst)
1553                         device_remove_file(&tz->device,
1554                                   &tz->trip_hyst_attrs[indx].attr);
1555         }
1556         kfree(tz->trip_type_attrs);
1557         kfree(tz->trip_temp_attrs);
1558         kfree(tz->trip_hyst_attrs);
1559 }
1560
1561 /**
1562  * thermal_zone_device_register - register a new thermal zone device
1563  * @type:       the thermal zone device type
1564  * @trips:      the number of trip points the thermal zone support
1565  * @mask:       a bit string indicating the writeablility of trip points
1566  * @devdata:    private device data
1567  * @ops:        standard thermal zone device callbacks
1568  * @tzp:        thermal zone platform parameters
1569  * @passive_delay: number of milliseconds to wait between polls when
1570  *                 performing passive cooling
1571  * @polling_delay: number of milliseconds to wait between polls when checking
1572  *                 whether trip points have been crossed (0 for interrupt
1573  *                 driven systems)
1574  *
1575  * thermal_zone_device_unregister() must be called when the device is no
1576  * longer needed. The passive cooling depends on the .get_trend() return value.
1577  */
1578 struct thermal_zone_device *thermal_zone_device_register(const char *type,
1579         int trips, int mask, void *devdata,
1580         const struct thermal_zone_device_ops *ops,
1581         const struct thermal_zone_params *tzp,
1582         int passive_delay, int polling_delay)
1583 {
1584         struct thermal_zone_device *tz;
1585         enum thermal_trip_type trip_type;
1586         int result;
1587         int count;
1588         int passive = 0;
1589
1590         if (type && strlen(type) >= THERMAL_NAME_LENGTH)
1591                 return ERR_PTR(-EINVAL);
1592
1593         if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
1594                 return ERR_PTR(-EINVAL);
1595
1596         if (!ops || !ops->get_temp)
1597                 return ERR_PTR(-EINVAL);
1598
1599         tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1600         if (!tz)
1601                 return ERR_PTR(-ENOMEM);
1602
1603         INIT_LIST_HEAD(&tz->thermal_instances);
1604         idr_init(&tz->idr);
1605         mutex_init(&tz->lock);
1606         result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1607         if (result) {
1608                 kfree(tz);
1609                 return ERR_PTR(result);
1610         }
1611
1612         strcpy(tz->type, type ? : "");
1613         tz->ops = ops;
1614         tz->tzp = tzp;
1615         tz->device.class = &thermal_class;
1616         tz->devdata = devdata;
1617         tz->trips = trips;
1618         tz->passive_delay = passive_delay;
1619         tz->polling_delay = polling_delay;
1620
1621         dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1622         result = device_register(&tz->device);
1623         if (result) {
1624                 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1625                 kfree(tz);
1626                 return ERR_PTR(result);
1627         }
1628
1629         /* sys I/F */
1630         if (type) {
1631                 result = device_create_file(&tz->device, &dev_attr_type);
1632                 if (result)
1633                         goto unregister;
1634         }
1635
1636         result = device_create_file(&tz->device, &dev_attr_temp);
1637         if (result)
1638                 goto unregister;
1639
1640         if (ops->get_mode) {
1641                 result = device_create_file(&tz->device, &dev_attr_mode);
1642                 if (result)
1643                         goto unregister;
1644         }
1645
1646         result = create_trip_attrs(tz, mask);
1647         if (result)
1648                 goto unregister;
1649
1650         for (count = 0; count < trips; count++) {
1651                 tz->ops->get_trip_type(tz, count, &trip_type);
1652                 if (trip_type == THERMAL_TRIP_PASSIVE)
1653                         passive = 1;
1654         }
1655
1656         if (!passive) {
1657                 result = device_create_file(&tz->device, &dev_attr_passive);
1658                 if (result)
1659                         goto unregister;
1660         }
1661
1662         /* Create policy attribute */
1663         result = device_create_file(&tz->device, &dev_attr_policy);
1664         if (result)
1665                 goto unregister;
1666
1667         /* Create passive_delay attribute */
1668         result = device_create_file(&tz->device, &dev_attr_passive_delay);
1669         if (result)
1670                 goto unregister;
1671
1672         /* Create available_policies attribute */
1673         result = device_create_file(&tz->device, &dev_attr_available_policies);
1674         if (result)
1675                 goto unregister;
1676
1677         /* Update 'this' zone's governor information */
1678         mutex_lock(&thermal_governor_lock);
1679
1680         if (tz->tzp)
1681                 tz->governor = __find_governor(tz->tzp->governor_name);
1682         else
1683                 tz->governor = __find_governor(DEFAULT_THERMAL_GOVERNOR);
1684
1685         mutex_unlock(&thermal_governor_lock);
1686
1687         result = thermal_add_hwmon_sysfs(tz);
1688         if (result)
1689                 goto unregister;
1690
1691         mutex_lock(&thermal_list_lock);
1692         list_add_tail(&tz->node, &thermal_tz_list);
1693         mutex_unlock(&thermal_list_lock);
1694
1695         /* Bind cooling devices for this zone */
1696         bind_tz(tz);
1697
1698         INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1699
1700         thermal_zone_device_update(tz);
1701
1702         if (!result)
1703                 return tz;
1704
1705 unregister:
1706         release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1707         device_unregister(&tz->device);
1708         return ERR_PTR(result);
1709 }
1710 EXPORT_SYMBOL(thermal_zone_device_register);
1711
1712 /**
1713  * thermal_device_unregister - removes the registered thermal zone device
1714  * @tz: the thermal zone device to remove
1715  */
1716 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1717 {
1718         int i;
1719         const struct thermal_zone_params *tzp;
1720         struct thermal_cooling_device *cdev;
1721         struct thermal_zone_device *pos = NULL;
1722
1723         if (!tz)
1724                 return;
1725
1726         tzp = tz->tzp;
1727
1728         mutex_lock(&thermal_list_lock);
1729         list_for_each_entry(pos, &thermal_tz_list, node)
1730             if (pos == tz)
1731                 break;
1732         if (pos != tz) {
1733                 /* thermal zone device not found */
1734                 mutex_unlock(&thermal_list_lock);
1735                 return;
1736         }
1737         list_del(&tz->node);
1738
1739         /* Unbind all cdevs associated with 'this' thermal zone */
1740         list_for_each_entry(cdev, &thermal_cdev_list, node) {
1741                 if (tz->ops->unbind) {
1742                         tz->ops->unbind(tz, cdev);
1743                         continue;
1744                 }
1745
1746                 if (!tzp || !tzp->tbp)
1747                         break;
1748
1749                 for (i = 0; i < tzp->num_tbps; i++) {
1750                         if (tzp->tbp[i].cdev == cdev) {
1751                                 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1752                                 tzp->tbp[i].cdev = NULL;
1753                         }
1754                 }
1755         }
1756
1757         mutex_unlock(&thermal_list_lock);
1758
1759         thermal_zone_device_set_polling(tz, 0);
1760
1761         if (tz->type[0])
1762                 device_remove_file(&tz->device, &dev_attr_type);
1763         device_remove_file(&tz->device, &dev_attr_temp);
1764         if (tz->ops->get_mode)
1765                 device_remove_file(&tz->device, &dev_attr_mode);
1766         device_remove_file(&tz->device, &dev_attr_passive_delay);
1767         device_remove_file(&tz->device, &dev_attr_policy);
1768         device_remove_file(&tz->device, &dev_attr_available_policies);
1769         remove_trip_attrs(tz);
1770         tz->governor = NULL;
1771
1772         thermal_remove_hwmon_sysfs(tz);
1773         release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1774         idr_destroy(&tz->idr);
1775         mutex_destroy(&tz->lock);
1776         device_unregister(&tz->device);
1777         return;
1778 }
1779 EXPORT_SYMBOL(thermal_zone_device_unregister);
1780
1781 #ifdef CONFIG_NET
1782 static struct genl_family thermal_event_genl_family = {
1783         .id = GENL_ID_GENERATE,
1784         .name = THERMAL_GENL_FAMILY_NAME,
1785         .version = THERMAL_GENL_VERSION,
1786         .maxattr = THERMAL_GENL_ATTR_MAX,
1787 };
1788
1789 static struct genl_multicast_group thermal_event_mcgrp = {
1790         .name = THERMAL_GENL_MCAST_GROUP_NAME,
1791 };
1792
1793 int thermal_generate_netlink_event(u32 orig, enum events event)
1794 {
1795         struct sk_buff *skb;
1796         struct nlattr *attr;
1797         struct thermal_genl_event *thermal_event;
1798         void *msg_header;
1799         int size;
1800         int result;
1801         static unsigned int thermal_event_seqnum;
1802
1803         /* allocate memory */
1804         size = nla_total_size(sizeof(struct thermal_genl_event)) +
1805                nla_total_size(0);
1806
1807         skb = genlmsg_new(size, GFP_ATOMIC);
1808         if (!skb)
1809                 return -ENOMEM;
1810
1811         /* add the genetlink message header */
1812         msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1813                                  &thermal_event_genl_family, 0,
1814                                  THERMAL_GENL_CMD_EVENT);
1815         if (!msg_header) {
1816                 nlmsg_free(skb);
1817                 return -ENOMEM;
1818         }
1819
1820         /* fill the data */
1821         attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1822                            sizeof(struct thermal_genl_event));
1823
1824         if (!attr) {
1825                 nlmsg_free(skb);
1826                 return -EINVAL;
1827         }
1828
1829         thermal_event = nla_data(attr);
1830         if (!thermal_event) {
1831                 nlmsg_free(skb);
1832                 return -EINVAL;
1833         }
1834
1835         memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1836
1837         thermal_event->orig = orig;
1838         thermal_event->event = event;
1839
1840         /* send multicast genetlink message */
1841         result = genlmsg_end(skb, msg_header);
1842         if (result < 0) {
1843                 nlmsg_free(skb);
1844                 return result;
1845         }
1846
1847         result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1848         if (result)
1849                 pr_info("failed to send netlink event:%d\n", result);
1850
1851         return result;
1852 }
1853 EXPORT_SYMBOL(thermal_generate_netlink_event);
1854
1855 static int genetlink_init(void)
1856 {
1857         int result;
1858
1859         result = genl_register_family(&thermal_event_genl_family);
1860         if (result)
1861                 return result;
1862
1863         result = genl_register_mc_group(&thermal_event_genl_family,
1864                                         &thermal_event_mcgrp);
1865         if (result)
1866                 genl_unregister_family(&thermal_event_genl_family);
1867         return result;
1868 }
1869
1870 static void genetlink_exit(void)
1871 {
1872         genl_unregister_family(&thermal_event_genl_family);
1873 }
1874 #else /* !CONFIG_NET */
1875 static inline int genetlink_init(void) { return 0; }
1876 static inline void genetlink_exit(void) {}
1877 #endif /* !CONFIG_NET */
1878
1879 static int __init thermal_init(void)
1880 {
1881         int result = 0;
1882
1883         result = class_register(&thermal_class);
1884         if (result) {
1885                 idr_destroy(&thermal_tz_idr);
1886                 idr_destroy(&thermal_cdev_idr);
1887                 mutex_destroy(&thermal_idr_lock);
1888                 mutex_destroy(&thermal_list_lock);
1889         }
1890         result = genetlink_init();
1891         return result;
1892 }
1893
1894 static void __exit thermal_exit(void)
1895 {
1896         class_unregister(&thermal_class);
1897         idr_destroy(&thermal_tz_idr);
1898         idr_destroy(&thermal_cdev_idr);
1899         mutex_destroy(&thermal_idr_lock);
1900         mutex_destroy(&thermal_list_lock);
1901         genetlink_exit();
1902 }
1903
1904 fs_initcall(thermal_init);
1905 module_exit(thermal_exit);