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