ACPI / thermal: Use THERMAL_TRIPS_NONE macro to replace number
[firefly-linux-kernel-4.4.55.git] / drivers / acpi / thermal.c
1 /*
2  *  acpi_thermal.c - ACPI Thermal Zone Driver ($Revision: 41 $)
3  *
4  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6  *
7  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or (at
12  *  your option) any later version.
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  *  This driver fully implements the ACPI thermal policy as described in the
26  *  ACPI 2.0 Specification.
27  *
28  *  TBD: 1. Implement passive cooling hysteresis.
29  *       2. Enhance passive cooling (CPU) states/limit interface to support
30  *          concepts of 'multiple limiters', upper/lower limits, etc.
31  *
32  */
33
34 #include <linux/kernel.h>
35 #include <linux/module.h>
36 #include <linux/dmi.h>
37 #include <linux/init.h>
38 #include <linux/slab.h>
39 #include <linux/types.h>
40 #include <linux/jiffies.h>
41 #include <linux/kmod.h>
42 #include <linux/reboot.h>
43 #include <linux/device.h>
44 #include <asm/uaccess.h>
45 #include <linux/thermal.h>
46 #include <acpi/acpi_bus.h>
47 #include <acpi/acpi_drivers.h>
48
49 #define PREFIX "ACPI: "
50
51 #define ACPI_THERMAL_CLASS              "thermal_zone"
52 #define ACPI_THERMAL_DEVICE_NAME        "Thermal Zone"
53 #define ACPI_THERMAL_NOTIFY_TEMPERATURE 0x80
54 #define ACPI_THERMAL_NOTIFY_THRESHOLDS  0x81
55 #define ACPI_THERMAL_NOTIFY_DEVICES     0x82
56 #define ACPI_THERMAL_NOTIFY_CRITICAL    0xF0
57 #define ACPI_THERMAL_NOTIFY_HOT         0xF1
58 #define ACPI_THERMAL_MODE_ACTIVE        0x00
59
60 #define ACPI_THERMAL_MAX_ACTIVE 10
61 #define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65
62
63 #define _COMPONENT              ACPI_THERMAL_COMPONENT
64 ACPI_MODULE_NAME("thermal");
65
66 MODULE_AUTHOR("Paul Diefenbaugh");
67 MODULE_DESCRIPTION("ACPI Thermal Zone Driver");
68 MODULE_LICENSE("GPL");
69
70 static int act;
71 module_param(act, int, 0644);
72 MODULE_PARM_DESC(act, "Disable or override all lowest active trip points.");
73
74 static int crt;
75 module_param(crt, int, 0644);
76 MODULE_PARM_DESC(crt, "Disable or lower all critical trip points.");
77
78 static int tzp;
79 module_param(tzp, int, 0444);
80 MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.");
81
82 static int nocrt;
83 module_param(nocrt, int, 0);
84 MODULE_PARM_DESC(nocrt, "Set to take no action upon ACPI thermal zone critical trips points.");
85
86 static int off;
87 module_param(off, int, 0);
88 MODULE_PARM_DESC(off, "Set to disable ACPI thermal support.");
89
90 static int psv;
91 module_param(psv, int, 0644);
92 MODULE_PARM_DESC(psv, "Disable or override all passive trip points.");
93
94 static int acpi_thermal_add(struct acpi_device *device);
95 static int acpi_thermal_remove(struct acpi_device *device);
96 static void acpi_thermal_notify(struct acpi_device *device, u32 event);
97
98 static const struct acpi_device_id  thermal_device_ids[] = {
99         {ACPI_THERMAL_HID, 0},
100         {"", 0},
101 };
102 MODULE_DEVICE_TABLE(acpi, thermal_device_ids);
103
104 #ifdef CONFIG_PM_SLEEP
105 static int acpi_thermal_resume(struct device *dev);
106 #endif
107 static SIMPLE_DEV_PM_OPS(acpi_thermal_pm, NULL, acpi_thermal_resume);
108
109 static struct acpi_driver acpi_thermal_driver = {
110         .name = "thermal",
111         .class = ACPI_THERMAL_CLASS,
112         .ids = thermal_device_ids,
113         .ops = {
114                 .add = acpi_thermal_add,
115                 .remove = acpi_thermal_remove,
116                 .notify = acpi_thermal_notify,
117                 },
118         .drv.pm = &acpi_thermal_pm,
119 };
120
121 struct acpi_thermal_state {
122         u8 critical:1;
123         u8 hot:1;
124         u8 passive:1;
125         u8 active:1;
126         u8 reserved:4;
127         int active_index;
128 };
129
130 struct acpi_thermal_state_flags {
131         u8 valid:1;
132         u8 enabled:1;
133         u8 reserved:6;
134 };
135
136 struct acpi_thermal_critical {
137         struct acpi_thermal_state_flags flags;
138         unsigned long temperature;
139 };
140
141 struct acpi_thermal_hot {
142         struct acpi_thermal_state_flags flags;
143         unsigned long temperature;
144 };
145
146 struct acpi_thermal_passive {
147         struct acpi_thermal_state_flags flags;
148         unsigned long temperature;
149         unsigned long tc1;
150         unsigned long tc2;
151         unsigned long tsp;
152         struct acpi_handle_list devices;
153 };
154
155 struct acpi_thermal_active {
156         struct acpi_thermal_state_flags flags;
157         unsigned long temperature;
158         struct acpi_handle_list devices;
159 };
160
161 struct acpi_thermal_trips {
162         struct acpi_thermal_critical critical;
163         struct acpi_thermal_hot hot;
164         struct acpi_thermal_passive passive;
165         struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
166 };
167
168 struct acpi_thermal_flags {
169         u8 cooling_mode:1;      /* _SCP */
170         u8 devices:1;           /* _TZD */
171         u8 reserved:6;
172 };
173
174 struct acpi_thermal {
175         struct acpi_device * device;
176         acpi_bus_id name;
177         unsigned long temperature;
178         unsigned long last_temperature;
179         unsigned long polling_frequency;
180         volatile u8 zombie;
181         struct acpi_thermal_flags flags;
182         struct acpi_thermal_state state;
183         struct acpi_thermal_trips trips;
184         struct acpi_handle_list devices;
185         struct thermal_zone_device *thermal_zone;
186         int tz_enabled;
187         int kelvin_offset;
188 };
189
190 /* --------------------------------------------------------------------------
191                              Thermal Zone Management
192    -------------------------------------------------------------------------- */
193
194 static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
195 {
196         acpi_status status = AE_OK;
197         unsigned long long tmp;
198
199         if (!tz)
200                 return -EINVAL;
201
202         tz->last_temperature = tz->temperature;
203
204         status = acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tmp);
205         if (ACPI_FAILURE(status))
206                 return -ENODEV;
207
208         tz->temperature = tmp;
209         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
210                           tz->temperature));
211
212         return 0;
213 }
214
215 static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
216 {
217         acpi_status status = AE_OK;
218         unsigned long long tmp;
219
220         if (!tz)
221                 return -EINVAL;
222
223         status = acpi_evaluate_integer(tz->device->handle, "_TZP", NULL, &tmp);
224         if (ACPI_FAILURE(status))
225                 return -ENODEV;
226
227         tz->polling_frequency = tmp;
228         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
229                           tz->polling_frequency));
230
231         return 0;
232 }
233
234 static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
235 {
236         acpi_status status = AE_OK;
237         union acpi_object arg0 = { ACPI_TYPE_INTEGER };
238         struct acpi_object_list arg_list = { 1, &arg0 };
239         acpi_handle handle = NULL;
240
241
242         if (!tz)
243                 return -EINVAL;
244
245         status = acpi_get_handle(tz->device->handle, "_SCP", &handle);
246         if (ACPI_FAILURE(status)) {
247                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
248                 return -ENODEV;
249         }
250
251         arg0.integer.value = mode;
252
253         status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
254         if (ACPI_FAILURE(status))
255                 return -ENODEV;
256
257         return 0;
258 }
259
260 #define ACPI_TRIPS_CRITICAL     0x01
261 #define ACPI_TRIPS_HOT          0x02
262 #define ACPI_TRIPS_PASSIVE      0x04
263 #define ACPI_TRIPS_ACTIVE       0x08
264 #define ACPI_TRIPS_DEVICES      0x10
265
266 #define ACPI_TRIPS_REFRESH_THRESHOLDS   (ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE)
267 #define ACPI_TRIPS_REFRESH_DEVICES      ACPI_TRIPS_DEVICES
268
269 #define ACPI_TRIPS_INIT      (ACPI_TRIPS_CRITICAL | ACPI_TRIPS_HOT |    \
270                               ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE |  \
271                               ACPI_TRIPS_DEVICES)
272
273 /*
274  * This exception is thrown out in two cases:
275  * 1.An invalid trip point becomes invalid or a valid trip point becomes invalid
276  *   when re-evaluating the AML code.
277  * 2.TODO: Devices listed in _PSL, _ALx, _TZD may change.
278  *   We need to re-bind the cooling devices of a thermal zone when this occurs.
279  */
280 #define ACPI_THERMAL_TRIPS_EXCEPTION(flags, str)        \
281 do {    \
282         if (flags != ACPI_TRIPS_INIT)   \
283                 ACPI_EXCEPTION((AE_INFO, AE_ERROR,      \
284                 "ACPI thermal trip point %s changed\n"  \
285                 "Please send acpidump to linux-acpi@vger.kernel.org", str)); \
286 } while (0)
287
288 static int acpi_thermal_trips_update(struct acpi_thermal *tz, int flag)
289 {
290         acpi_status status = AE_OK;
291         unsigned long long tmp;
292         struct acpi_handle_list devices;
293         int valid = 0;
294         int i;
295
296         /* Critical Shutdown */
297         if (flag & ACPI_TRIPS_CRITICAL) {
298                 status = acpi_evaluate_integer(tz->device->handle,
299                                 "_CRT", NULL, &tmp);
300                 tz->trips.critical.temperature = tmp;
301                 /*
302                  * Treat freezing temperatures as invalid as well; some
303                  * BIOSes return really low values and cause reboots at startup.
304                  * Below zero (Celsius) values clearly aren't right for sure..
305                  * ... so lets discard those as invalid.
306                  */
307                 if (ACPI_FAILURE(status)) {
308                         tz->trips.critical.flags.valid = 0;
309                         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
310                                           "No critical threshold\n"));
311                 } else if (tmp <= 2732) {
312                         printk(KERN_WARNING FW_BUG "Invalid critical threshold "
313                                "(%llu)\n", tmp);
314                         tz->trips.critical.flags.valid = 0;
315                 } else {
316                         tz->trips.critical.flags.valid = 1;
317                         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
318                                           "Found critical threshold [%lu]\n",
319                                           tz->trips.critical.temperature));
320                 }
321                 if (tz->trips.critical.flags.valid == 1) {
322                         if (crt == -1) {
323                                 tz->trips.critical.flags.valid = 0;
324                         } else if (crt > 0) {
325                                 unsigned long crt_k = CELSIUS_TO_KELVIN(crt);
326                                 /*
327                                  * Allow override critical threshold
328                                  */
329                                 if (crt_k > tz->trips.critical.temperature)
330                                         printk(KERN_WARNING PREFIX
331                                                 "Critical threshold %d C\n", crt);
332                                 tz->trips.critical.temperature = crt_k;
333                         }
334                 }
335         }
336
337         /* Critical Sleep (optional) */
338         if (flag & ACPI_TRIPS_HOT) {
339                 status = acpi_evaluate_integer(tz->device->handle,
340                                 "_HOT", NULL, &tmp);
341                 if (ACPI_FAILURE(status)) {
342                         tz->trips.hot.flags.valid = 0;
343                         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
344                                         "No hot threshold\n"));
345                 } else {
346                         tz->trips.hot.temperature = tmp;
347                         tz->trips.hot.flags.valid = 1;
348                         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
349                                         "Found hot threshold [%lu]\n",
350                                         tz->trips.critical.temperature));
351                 }
352         }
353
354         /* Passive (optional) */
355         if (((flag & ACPI_TRIPS_PASSIVE) && tz->trips.passive.flags.valid) ||
356                 (flag == ACPI_TRIPS_INIT)) {
357                 valid = tz->trips.passive.flags.valid;
358                 if (psv == -1) {
359                         status = AE_SUPPORT;
360                 } else if (psv > 0) {
361                         tmp = CELSIUS_TO_KELVIN(psv);
362                         status = AE_OK;
363                 } else {
364                         status = acpi_evaluate_integer(tz->device->handle,
365                                 "_PSV", NULL, &tmp);
366                 }
367
368                 if (ACPI_FAILURE(status))
369                         tz->trips.passive.flags.valid = 0;
370                 else {
371                         tz->trips.passive.temperature = tmp;
372                         tz->trips.passive.flags.valid = 1;
373                         if (flag == ACPI_TRIPS_INIT) {
374                                 status = acpi_evaluate_integer(
375                                                 tz->device->handle, "_TC1",
376                                                 NULL, &tmp);
377                                 if (ACPI_FAILURE(status))
378                                         tz->trips.passive.flags.valid = 0;
379                                 else
380                                         tz->trips.passive.tc1 = tmp;
381                                 status = acpi_evaluate_integer(
382                                                 tz->device->handle, "_TC2",
383                                                 NULL, &tmp);
384                                 if (ACPI_FAILURE(status))
385                                         tz->trips.passive.flags.valid = 0;
386                                 else
387                                         tz->trips.passive.tc2 = tmp;
388                                 status = acpi_evaluate_integer(
389                                                 tz->device->handle, "_TSP",
390                                                 NULL, &tmp);
391                                 if (ACPI_FAILURE(status))
392                                         tz->trips.passive.flags.valid = 0;
393                                 else
394                                         tz->trips.passive.tsp = tmp;
395                         }
396                 }
397         }
398         if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.passive.flags.valid) {
399                 memset(&devices, 0, sizeof(struct acpi_handle_list));
400                 status = acpi_evaluate_reference(tz->device->handle, "_PSL",
401                                                         NULL, &devices);
402                 if (ACPI_FAILURE(status)) {
403                         printk(KERN_WARNING PREFIX
404                                 "Invalid passive threshold\n");
405                         tz->trips.passive.flags.valid = 0;
406                 }
407                 else
408                         tz->trips.passive.flags.valid = 1;
409
410                 if (memcmp(&tz->trips.passive.devices, &devices,
411                                 sizeof(struct acpi_handle_list))) {
412                         memcpy(&tz->trips.passive.devices, &devices,
413                                 sizeof(struct acpi_handle_list));
414                         ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
415                 }
416         }
417         if ((flag & ACPI_TRIPS_PASSIVE) || (flag & ACPI_TRIPS_DEVICES)) {
418                 if (valid != tz->trips.passive.flags.valid)
419                                 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
420         }
421
422         /* Active (optional) */
423         for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
424                 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
425                 valid = tz->trips.active[i].flags.valid;
426
427                 if (act == -1)
428                         break; /* disable all active trip points */
429
430                 if ((flag == ACPI_TRIPS_INIT) || ((flag & ACPI_TRIPS_ACTIVE) &&
431                         tz->trips.active[i].flags.valid)) {
432                         status = acpi_evaluate_integer(tz->device->handle,
433                                                         name, NULL, &tmp);
434                         if (ACPI_FAILURE(status)) {
435                                 tz->trips.active[i].flags.valid = 0;
436                                 if (i == 0)
437                                         break;
438                                 if (act <= 0)
439                                         break;
440                                 if (i == 1)
441                                         tz->trips.active[0].temperature =
442                                                 CELSIUS_TO_KELVIN(act);
443                                 else
444                                         /*
445                                          * Don't allow override higher than
446                                          * the next higher trip point
447                                          */
448                                         tz->trips.active[i - 1].temperature =
449                                                 (tz->trips.active[i - 2].temperature <
450                                                 CELSIUS_TO_KELVIN(act) ?
451                                                 tz->trips.active[i - 2].temperature :
452                                                 CELSIUS_TO_KELVIN(act));
453                                 break;
454                         } else {
455                                 tz->trips.active[i].temperature = tmp;
456                                 tz->trips.active[i].flags.valid = 1;
457                         }
458                 }
459
460                 name[2] = 'L';
461                 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.active[i].flags.valid ) {
462                         memset(&devices, 0, sizeof(struct acpi_handle_list));
463                         status = acpi_evaluate_reference(tz->device->handle,
464                                                 name, NULL, &devices);
465                         if (ACPI_FAILURE(status)) {
466                                 printk(KERN_WARNING PREFIX
467                                         "Invalid active%d threshold\n", i);
468                                 tz->trips.active[i].flags.valid = 0;
469                         }
470                         else
471                                 tz->trips.active[i].flags.valid = 1;
472
473                         if (memcmp(&tz->trips.active[i].devices, &devices,
474                                         sizeof(struct acpi_handle_list))) {
475                                 memcpy(&tz->trips.active[i].devices, &devices,
476                                         sizeof(struct acpi_handle_list));
477                                 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
478                         }
479                 }
480                 if ((flag & ACPI_TRIPS_ACTIVE) || (flag & ACPI_TRIPS_DEVICES))
481                         if (valid != tz->trips.active[i].flags.valid)
482                                 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
483
484                 if (!tz->trips.active[i].flags.valid)
485                         break;
486         }
487
488         if (flag & ACPI_TRIPS_DEVICES) {
489                 memset(&devices, 0, sizeof(struct acpi_handle_list));
490                 status = acpi_evaluate_reference(tz->device->handle, "_TZD",
491                                                 NULL, &devices);
492                 if (memcmp(&tz->devices, &devices,
493                                 sizeof(struct acpi_handle_list))) {
494                         memcpy(&tz->devices, &devices,
495                                 sizeof(struct acpi_handle_list));
496                         ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
497                 }
498         }
499
500         return 0;
501 }
502
503 static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
504 {
505         int i, valid, ret = acpi_thermal_trips_update(tz, ACPI_TRIPS_INIT);
506
507         if (ret)
508                 return ret;
509
510         valid = tz->trips.critical.flags.valid |
511                 tz->trips.hot.flags.valid |
512                 tz->trips.passive.flags.valid;
513
514         for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
515                 valid |= tz->trips.active[i].flags.valid;
516
517         if (!valid) {
518                 printk(KERN_WARNING FW_BUG "No valid trip found\n");
519                 return -ENODEV;
520         }
521         return 0;
522 }
523
524 static void acpi_thermal_check(void *data)
525 {
526         struct acpi_thermal *tz = data;
527
528         if (!tz->tz_enabled) {
529                 pr_warn("thermal zone is disabled \n");
530                 return;
531         }
532         thermal_zone_device_update(tz->thermal_zone);
533 }
534
535 /* sys I/F for generic thermal sysfs support */
536 #define KELVIN_TO_MILLICELSIUS(t, off) (((t) - (off)) * 100)
537
538 static int thermal_get_temp(struct thermal_zone_device *thermal,
539                             unsigned long *temp)
540 {
541         struct acpi_thermal *tz = thermal->devdata;
542         int result;
543
544         if (!tz)
545                 return -EINVAL;
546
547         result = acpi_thermal_get_temperature(tz);
548         if (result)
549                 return result;
550
551         *temp = KELVIN_TO_MILLICELSIUS(tz->temperature, tz->kelvin_offset);
552         return 0;
553 }
554
555 static int thermal_get_mode(struct thermal_zone_device *thermal,
556                                 enum thermal_device_mode *mode)
557 {
558         struct acpi_thermal *tz = thermal->devdata;
559
560         if (!tz)
561                 return -EINVAL;
562
563         *mode = tz->tz_enabled ? THERMAL_DEVICE_ENABLED :
564                 THERMAL_DEVICE_DISABLED;
565
566         return 0;
567 }
568
569 static int thermal_set_mode(struct thermal_zone_device *thermal,
570                                 enum thermal_device_mode mode)
571 {
572         struct acpi_thermal *tz = thermal->devdata;
573         int enable;
574
575         if (!tz)
576                 return -EINVAL;
577
578         /*
579          * enable/disable thermal management from ACPI thermal driver
580          */
581         if (mode == THERMAL_DEVICE_ENABLED)
582                 enable = 1;
583         else if (mode == THERMAL_DEVICE_DISABLED)
584                 enable = 0;
585         else
586                 return -EINVAL;
587
588         if (enable != tz->tz_enabled) {
589                 tz->tz_enabled = enable;
590                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
591                         "%s kernel ACPI thermal control\n",
592                         tz->tz_enabled ? "Enable" : "Disable"));
593                 acpi_thermal_check(tz);
594         }
595         return 0;
596 }
597
598 static int thermal_get_trip_type(struct thermal_zone_device *thermal,
599                                  int trip, enum thermal_trip_type *type)
600 {
601         struct acpi_thermal *tz = thermal->devdata;
602         int i;
603
604         if (!tz || trip < 0)
605                 return -EINVAL;
606
607         if (tz->trips.critical.flags.valid) {
608                 if (!trip) {
609                         *type = THERMAL_TRIP_CRITICAL;
610                         return 0;
611                 }
612                 trip--;
613         }
614
615         if (tz->trips.hot.flags.valid) {
616                 if (!trip) {
617                         *type = THERMAL_TRIP_HOT;
618                         return 0;
619                 }
620                 trip--;
621         }
622
623         if (tz->trips.passive.flags.valid) {
624                 if (!trip) {
625                         *type = THERMAL_TRIP_PASSIVE;
626                         return 0;
627                 }
628                 trip--;
629         }
630
631         for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
632                 tz->trips.active[i].flags.valid; i++) {
633                 if (!trip) {
634                         *type = THERMAL_TRIP_ACTIVE;
635                         return 0;
636                 }
637                 trip--;
638         }
639
640         return -EINVAL;
641 }
642
643 static int thermal_get_trip_temp(struct thermal_zone_device *thermal,
644                                  int trip, unsigned long *temp)
645 {
646         struct acpi_thermal *tz = thermal->devdata;
647         int i;
648
649         if (!tz || trip < 0)
650                 return -EINVAL;
651
652         if (tz->trips.critical.flags.valid) {
653                 if (!trip) {
654                         *temp = KELVIN_TO_MILLICELSIUS(
655                                 tz->trips.critical.temperature,
656                                 tz->kelvin_offset);
657                         return 0;
658                 }
659                 trip--;
660         }
661
662         if (tz->trips.hot.flags.valid) {
663                 if (!trip) {
664                         *temp = KELVIN_TO_MILLICELSIUS(
665                                 tz->trips.hot.temperature,
666                                 tz->kelvin_offset);
667                         return 0;
668                 }
669                 trip--;
670         }
671
672         if (tz->trips.passive.flags.valid) {
673                 if (!trip) {
674                         *temp = KELVIN_TO_MILLICELSIUS(
675                                 tz->trips.passive.temperature,
676                                 tz->kelvin_offset);
677                         return 0;
678                 }
679                 trip--;
680         }
681
682         for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
683                 tz->trips.active[i].flags.valid; i++) {
684                 if (!trip) {
685                         *temp = KELVIN_TO_MILLICELSIUS(
686                                 tz->trips.active[i].temperature,
687                                 tz->kelvin_offset);
688                         return 0;
689                 }
690                 trip--;
691         }
692
693         return -EINVAL;
694 }
695
696 static int thermal_get_crit_temp(struct thermal_zone_device *thermal,
697                                 unsigned long *temperature) {
698         struct acpi_thermal *tz = thermal->devdata;
699
700         if (tz->trips.critical.flags.valid) {
701                 *temperature = KELVIN_TO_MILLICELSIUS(
702                                 tz->trips.critical.temperature,
703                                 tz->kelvin_offset);
704                 return 0;
705         } else
706                 return -EINVAL;
707 }
708
709 static int thermal_get_trend(struct thermal_zone_device *thermal,
710                                 int trip, enum thermal_trend *trend)
711 {
712         struct acpi_thermal *tz = thermal->devdata;
713         enum thermal_trip_type type;
714         int i;
715
716         if (thermal_get_trip_type(thermal, trip, &type))
717                 return -EINVAL;
718
719         if (type == THERMAL_TRIP_ACTIVE) {
720                 unsigned long trip_temp;
721                 unsigned long temp = KELVIN_TO_MILLICELSIUS(tz->temperature,
722                                                         tz->kelvin_offset);
723                 if (thermal_get_trip_temp(thermal, trip, &trip_temp))
724                         return -EINVAL;
725
726                 if (temp > trip_temp) {
727                         *trend = THERMAL_TREND_RAISING;
728                         return 0;
729                 } else {
730                         /* Fall back on default trend */
731                         return -EINVAL;
732                 }
733         }
734
735         /*
736          * tz->temperature has already been updated by generic thermal layer,
737          * before this callback being invoked
738          */
739         i = (tz->trips.passive.tc1 * (tz->temperature - tz->last_temperature))
740                 + (tz->trips.passive.tc2
741                 * (tz->temperature - tz->trips.passive.temperature));
742
743         if (i > 0)
744                 *trend = THERMAL_TREND_RAISING;
745         else if (i < 0)
746                 *trend = THERMAL_TREND_DROPPING;
747         else
748                 *trend = THERMAL_TREND_STABLE;
749         return 0;
750 }
751
752
753 static int thermal_notify(struct thermal_zone_device *thermal, int trip,
754                            enum thermal_trip_type trip_type)
755 {
756         u8 type = 0;
757         struct acpi_thermal *tz = thermal->devdata;
758
759         if (trip_type == THERMAL_TRIP_CRITICAL)
760                 type = ACPI_THERMAL_NOTIFY_CRITICAL;
761         else if (trip_type == THERMAL_TRIP_HOT)
762                 type = ACPI_THERMAL_NOTIFY_HOT;
763         else
764                 return 0;
765
766         acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
767                                         dev_name(&tz->device->dev), type, 1);
768
769         if (trip_type == THERMAL_TRIP_CRITICAL && nocrt)
770                 return 1;
771
772         return 0;
773 }
774
775 static int acpi_thermal_cooling_device_cb(struct thermal_zone_device *thermal,
776                                         struct thermal_cooling_device *cdev,
777                                         bool bind)
778 {
779         struct acpi_device *device = cdev->devdata;
780         struct acpi_thermal *tz = thermal->devdata;
781         struct acpi_device *dev;
782         acpi_status status;
783         acpi_handle handle;
784         int i;
785         int j;
786         int trip = -1;
787         int result = 0;
788
789         if (tz->trips.critical.flags.valid)
790                 trip++;
791
792         if (tz->trips.hot.flags.valid)
793                 trip++;
794
795         if (tz->trips.passive.flags.valid) {
796                 trip++;
797                 for (i = 0; i < tz->trips.passive.devices.count;
798                     i++) {
799                         handle = tz->trips.passive.devices.handles[i];
800                         status = acpi_bus_get_device(handle, &dev);
801                         if (ACPI_FAILURE(status) || dev != device)
802                                 continue;
803                         if (bind)
804                                 result =
805                                         thermal_zone_bind_cooling_device
806                                         (thermal, trip, cdev,
807                                          THERMAL_NO_LIMIT, THERMAL_NO_LIMIT);
808                         else
809                                 result =
810                                         thermal_zone_unbind_cooling_device
811                                         (thermal, trip, cdev);
812                         if (result)
813                                 goto failed;
814                 }
815         }
816
817         for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
818                 if (!tz->trips.active[i].flags.valid)
819                         break;
820                 trip++;
821                 for (j = 0;
822                     j < tz->trips.active[i].devices.count;
823                     j++) {
824                         handle = tz->trips.active[i].devices.handles[j];
825                         status = acpi_bus_get_device(handle, &dev);
826                         if (ACPI_FAILURE(status) || dev != device)
827                                 continue;
828                         if (bind)
829                                 result = thermal_zone_bind_cooling_device
830                                         (thermal, trip, cdev,
831                                          THERMAL_NO_LIMIT, THERMAL_NO_LIMIT);
832                         else
833                                 result = thermal_zone_unbind_cooling_device
834                                         (thermal, trip, cdev);
835                         if (result)
836                                 goto failed;
837                 }
838         }
839
840         for (i = 0; i < tz->devices.count; i++) {
841                 handle = tz->devices.handles[i];
842                 status = acpi_bus_get_device(handle, &dev);
843                 if (ACPI_SUCCESS(status) && (dev == device)) {
844                         if (bind)
845                                 result = thermal_zone_bind_cooling_device
846                                                 (thermal, THERMAL_TRIPS_NONE,
847                                                  cdev, THERMAL_NO_LIMIT,
848                                                  THERMAL_NO_LIMIT);
849                         else
850                                 result = thermal_zone_unbind_cooling_device
851                                                 (thermal, THERMAL_TRIPS_NONE,
852                                                  cdev);
853                         if (result)
854                                 goto failed;
855                 }
856         }
857
858 failed:
859         return result;
860 }
861
862 static int
863 acpi_thermal_bind_cooling_device(struct thermal_zone_device *thermal,
864                                         struct thermal_cooling_device *cdev)
865 {
866         return acpi_thermal_cooling_device_cb(thermal, cdev, true);
867 }
868
869 static int
870 acpi_thermal_unbind_cooling_device(struct thermal_zone_device *thermal,
871                                         struct thermal_cooling_device *cdev)
872 {
873         return acpi_thermal_cooling_device_cb(thermal, cdev, false);
874 }
875
876 static const struct thermal_zone_device_ops acpi_thermal_zone_ops = {
877         .bind = acpi_thermal_bind_cooling_device,
878         .unbind = acpi_thermal_unbind_cooling_device,
879         .get_temp = thermal_get_temp,
880         .get_mode = thermal_get_mode,
881         .set_mode = thermal_set_mode,
882         .get_trip_type = thermal_get_trip_type,
883         .get_trip_temp = thermal_get_trip_temp,
884         .get_crit_temp = thermal_get_crit_temp,
885         .get_trend = thermal_get_trend,
886         .notify = thermal_notify,
887 };
888
889 static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz)
890 {
891         int trips = 0;
892         int result;
893         acpi_status status;
894         int i;
895
896         if (tz->trips.critical.flags.valid)
897                 trips++;
898
899         if (tz->trips.hot.flags.valid)
900                 trips++;
901
902         if (tz->trips.passive.flags.valid)
903                 trips++;
904
905         for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
906                         tz->trips.active[i].flags.valid; i++, trips++);
907
908         if (tz->trips.passive.flags.valid)
909                 tz->thermal_zone =
910                         thermal_zone_device_register("acpitz", trips, 0, tz,
911                                                 &acpi_thermal_zone_ops, NULL,
912                                                      tz->trips.passive.tsp*100,
913                                                      tz->polling_frequency*100);
914         else
915                 tz->thermal_zone =
916                         thermal_zone_device_register("acpitz", trips, 0, tz,
917                                                 &acpi_thermal_zone_ops, NULL,
918                                                 0, tz->polling_frequency*100);
919         if (IS_ERR(tz->thermal_zone))
920                 return -ENODEV;
921
922         result = sysfs_create_link(&tz->device->dev.kobj,
923                                    &tz->thermal_zone->device.kobj, "thermal_zone");
924         if (result)
925                 return result;
926
927         result = sysfs_create_link(&tz->thermal_zone->device.kobj,
928                                    &tz->device->dev.kobj, "device");
929         if (result)
930                 return result;
931
932         status = acpi_attach_data(tz->device->handle,
933                                   acpi_bus_private_data_handler,
934                                   tz->thermal_zone);
935         if (ACPI_FAILURE(status)) {
936                 printk(KERN_ERR PREFIX
937                                 "Error attaching device data\n");
938                 return -ENODEV;
939         }
940
941         tz->tz_enabled = 1;
942
943         dev_info(&tz->device->dev, "registered as thermal_zone%d\n",
944                  tz->thermal_zone->id);
945         return 0;
946 }
947
948 static void acpi_thermal_unregister_thermal_zone(struct acpi_thermal *tz)
949 {
950         sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
951         sysfs_remove_link(&tz->thermal_zone->device.kobj, "device");
952         thermal_zone_device_unregister(tz->thermal_zone);
953         tz->thermal_zone = NULL;
954         acpi_detach_data(tz->device->handle, acpi_bus_private_data_handler);
955 }
956
957
958 /* --------------------------------------------------------------------------
959                                  Driver Interface
960    -------------------------------------------------------------------------- */
961
962 static void acpi_thermal_notify(struct acpi_device *device, u32 event)
963 {
964         struct acpi_thermal *tz = acpi_driver_data(device);
965
966
967         if (!tz)
968                 return;
969
970         switch (event) {
971         case ACPI_THERMAL_NOTIFY_TEMPERATURE:
972                 acpi_thermal_check(tz);
973                 break;
974         case ACPI_THERMAL_NOTIFY_THRESHOLDS:
975                 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_THRESHOLDS);
976                 acpi_thermal_check(tz);
977                 acpi_bus_generate_netlink_event(device->pnp.device_class,
978                                                   dev_name(&device->dev), event, 0);
979                 break;
980         case ACPI_THERMAL_NOTIFY_DEVICES:
981                 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_DEVICES);
982                 acpi_thermal_check(tz);
983                 acpi_bus_generate_netlink_event(device->pnp.device_class,
984                                                   dev_name(&device->dev), event, 0);
985                 break;
986         default:
987                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
988                                   "Unsupported event [0x%x]\n", event));
989                 break;
990         }
991 }
992
993 /*
994  * On some platforms, the AML code has dependency about
995  * the evaluating order of _TMP and _CRT/_HOT/_PSV/_ACx.
996  * 1. On HP Pavilion G4-1016tx, _TMP must be invoked after
997  *    /_CRT/_HOT/_PSV/_ACx, or else system will be power off.
998  * 2. On HP Compaq 6715b/6715s, the return value of _PSV is 0
999  *    if _TMP has never been evaluated.
1000  *
1001  * As this dependency is totally transparent to OS, evaluate
1002  * all of them once, in the order of _CRT/_HOT/_PSV/_ACx,
1003  * _TMP, before they are actually used.
1004  */
1005 static void acpi_thermal_aml_dependency_fix(struct acpi_thermal *tz)
1006 {
1007         acpi_handle handle = tz->device->handle;
1008         unsigned long long value;
1009         int i;
1010
1011         acpi_evaluate_integer(handle, "_CRT", NULL, &value);
1012         acpi_evaluate_integer(handle, "_HOT", NULL, &value);
1013         acpi_evaluate_integer(handle, "_PSV", NULL, &value);
1014         for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1015                 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
1016                 acpi_status status;
1017
1018                 status = acpi_evaluate_integer(handle, name, NULL, &value);
1019                 if (status == AE_NOT_FOUND)
1020                         break;
1021         }
1022         acpi_evaluate_integer(handle, "_TMP", NULL, &value);
1023 }
1024
1025 static int acpi_thermal_get_info(struct acpi_thermal *tz)
1026 {
1027         int result = 0;
1028
1029
1030         if (!tz)
1031                 return -EINVAL;
1032
1033         acpi_thermal_aml_dependency_fix(tz);
1034
1035         /* Get trip points [_CRT, _PSV, etc.] (required) */
1036         result = acpi_thermal_get_trip_points(tz);
1037         if (result)
1038                 return result;
1039
1040         /* Get temperature [_TMP] (required) */
1041         result = acpi_thermal_get_temperature(tz);
1042         if (result)
1043                 return result;
1044
1045         /* Set the cooling mode [_SCP] to active cooling (default) */
1046         result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
1047         if (!result)
1048                 tz->flags.cooling_mode = 1;
1049
1050         /* Get default polling frequency [_TZP] (optional) */
1051         if (tzp)
1052                 tz->polling_frequency = tzp;
1053         else
1054                 acpi_thermal_get_polling_frequency(tz);
1055
1056         return 0;
1057 }
1058
1059 /*
1060  * The exact offset between Kelvin and degree Celsius is 273.15. However ACPI
1061  * handles temperature values with a single decimal place. As a consequence,
1062  * some implementations use an offset of 273.1 and others use an offset of
1063  * 273.2. Try to find out which one is being used, to present the most
1064  * accurate and visually appealing number.
1065  *
1066  * The heuristic below should work for all ACPI thermal zones which have a
1067  * critical trip point with a value being a multiple of 0.5 degree Celsius.
1068  */
1069 static void acpi_thermal_guess_offset(struct acpi_thermal *tz)
1070 {
1071         if (tz->trips.critical.flags.valid &&
1072             (tz->trips.critical.temperature % 5) == 1)
1073                 tz->kelvin_offset = 2731;
1074         else
1075                 tz->kelvin_offset = 2732;
1076 }
1077
1078 static int acpi_thermal_add(struct acpi_device *device)
1079 {
1080         int result = 0;
1081         struct acpi_thermal *tz = NULL;
1082
1083
1084         if (!device)
1085                 return -EINVAL;
1086
1087         tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
1088         if (!tz)
1089                 return -ENOMEM;
1090
1091         tz->device = device;
1092         strcpy(tz->name, device->pnp.bus_id);
1093         strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1094         strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
1095         device->driver_data = tz;
1096
1097         result = acpi_thermal_get_info(tz);
1098         if (result)
1099                 goto free_memory;
1100
1101         acpi_thermal_guess_offset(tz);
1102
1103         result = acpi_thermal_register_thermal_zone(tz);
1104         if (result)
1105                 goto free_memory;
1106
1107         printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
1108                acpi_device_name(device), acpi_device_bid(device),
1109                KELVIN_TO_CELSIUS(tz->temperature));
1110         goto end;
1111
1112 free_memory:
1113         kfree(tz);
1114 end:
1115         return result;
1116 }
1117
1118 static int acpi_thermal_remove(struct acpi_device *device)
1119 {
1120         struct acpi_thermal *tz = NULL;
1121
1122         if (!device || !acpi_driver_data(device))
1123                 return -EINVAL;
1124
1125         tz = acpi_driver_data(device);
1126
1127         acpi_thermal_unregister_thermal_zone(tz);
1128         kfree(tz);
1129         return 0;
1130 }
1131
1132 #ifdef CONFIG_PM_SLEEP
1133 static int acpi_thermal_resume(struct device *dev)
1134 {
1135         struct acpi_thermal *tz;
1136         int i, j, power_state, result;
1137
1138         if (!dev)
1139                 return -EINVAL;
1140
1141         tz = acpi_driver_data(to_acpi_device(dev));
1142         if (!tz)
1143                 return -EINVAL;
1144
1145         for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1146                 if (!(&tz->trips.active[i]))
1147                         break;
1148                 if (!tz->trips.active[i].flags.valid)
1149                         break;
1150                 tz->trips.active[i].flags.enabled = 1;
1151                 for (j = 0; j < tz->trips.active[i].devices.count; j++) {
1152                         result = acpi_bus_update_power(
1153                                         tz->trips.active[i].devices.handles[j],
1154                                         &power_state);
1155                         if (result || (power_state != ACPI_STATE_D0)) {
1156                                 tz->trips.active[i].flags.enabled = 0;
1157                                 break;
1158                         }
1159                 }
1160                 tz->state.active |= tz->trips.active[i].flags.enabled;
1161         }
1162
1163         acpi_thermal_check(tz);
1164
1165         return AE_OK;
1166 }
1167 #endif
1168
1169 static int thermal_act(const struct dmi_system_id *d) {
1170
1171         if (act == 0) {
1172                 printk(KERN_NOTICE "ACPI: %s detected: "
1173                         "disabling all active thermal trip points\n", d->ident);
1174                 act = -1;
1175         }
1176         return 0;
1177 }
1178 static int thermal_nocrt(const struct dmi_system_id *d) {
1179
1180         printk(KERN_NOTICE "ACPI: %s detected: "
1181                 "disabling all critical thermal trip point actions.\n", d->ident);
1182         nocrt = 1;
1183         return 0;
1184 }
1185 static int thermal_tzp(const struct dmi_system_id *d) {
1186
1187         if (tzp == 0) {
1188                 printk(KERN_NOTICE "ACPI: %s detected: "
1189                         "enabling thermal zone polling\n", d->ident);
1190                 tzp = 300;      /* 300 dS = 30 Seconds */
1191         }
1192         return 0;
1193 }
1194 static int thermal_psv(const struct dmi_system_id *d) {
1195
1196         if (psv == 0) {
1197                 printk(KERN_NOTICE "ACPI: %s detected: "
1198                         "disabling all passive thermal trip points\n", d->ident);
1199                 psv = -1;
1200         }
1201         return 0;
1202 }
1203
1204 static struct dmi_system_id thermal_dmi_table[] __initdata = {
1205         /*
1206          * Award BIOS on this AOpen makes thermal control almost worthless.
1207          * http://bugzilla.kernel.org/show_bug.cgi?id=8842
1208          */
1209         {
1210          .callback = thermal_act,
1211          .ident = "AOpen i915GMm-HFS",
1212          .matches = {
1213                 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1214                 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1215                 },
1216         },
1217         {
1218          .callback = thermal_psv,
1219          .ident = "AOpen i915GMm-HFS",
1220          .matches = {
1221                 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1222                 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1223                 },
1224         },
1225         {
1226          .callback = thermal_tzp,
1227          .ident = "AOpen i915GMm-HFS",
1228          .matches = {
1229                 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1230                 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1231                 },
1232         },
1233         {
1234          .callback = thermal_nocrt,
1235          .ident = "Gigabyte GA-7ZX",
1236          .matches = {
1237                 DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
1238                 DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
1239                 },
1240         },
1241         {}
1242 };
1243
1244 static int __init acpi_thermal_init(void)
1245 {
1246         int result = 0;
1247
1248         dmi_check_system(thermal_dmi_table);
1249
1250         if (off) {
1251                 printk(KERN_NOTICE "ACPI: thermal control disabled\n");
1252                 return -ENODEV;
1253         }
1254
1255         result = acpi_bus_register_driver(&acpi_thermal_driver);
1256         if (result < 0)
1257                 return -ENODEV;
1258
1259         return 0;
1260 }
1261
1262 static void __exit acpi_thermal_exit(void)
1263 {
1264
1265         acpi_bus_unregister_driver(&acpi_thermal_driver);
1266
1267         return;
1268 }
1269
1270 module_init(acpi_thermal_init);
1271 module_exit(acpi_thermal_exit);