ACPI / thermal: Remove unused macros in the driver/acpi/thermal.c
[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, -1, cdev,
847                                                  THERMAL_NO_LIMIT,
848                                                  THERMAL_NO_LIMIT);
849                         else
850                                 result = thermal_zone_unbind_cooling_device
851                                                 (thermal, -1, cdev);
852                         if (result)
853                                 goto failed;
854                 }
855         }
856
857 failed:
858         return result;
859 }
860
861 static int
862 acpi_thermal_bind_cooling_device(struct thermal_zone_device *thermal,
863                                         struct thermal_cooling_device *cdev)
864 {
865         return acpi_thermal_cooling_device_cb(thermal, cdev, true);
866 }
867
868 static int
869 acpi_thermal_unbind_cooling_device(struct thermal_zone_device *thermal,
870                                         struct thermal_cooling_device *cdev)
871 {
872         return acpi_thermal_cooling_device_cb(thermal, cdev, false);
873 }
874
875 static const struct thermal_zone_device_ops acpi_thermal_zone_ops = {
876         .bind = acpi_thermal_bind_cooling_device,
877         .unbind = acpi_thermal_unbind_cooling_device,
878         .get_temp = thermal_get_temp,
879         .get_mode = thermal_get_mode,
880         .set_mode = thermal_set_mode,
881         .get_trip_type = thermal_get_trip_type,
882         .get_trip_temp = thermal_get_trip_temp,
883         .get_crit_temp = thermal_get_crit_temp,
884         .get_trend = thermal_get_trend,
885         .notify = thermal_notify,
886 };
887
888 static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz)
889 {
890         int trips = 0;
891         int result;
892         acpi_status status;
893         int i;
894
895         if (tz->trips.critical.flags.valid)
896                 trips++;
897
898         if (tz->trips.hot.flags.valid)
899                 trips++;
900
901         if (tz->trips.passive.flags.valid)
902                 trips++;
903
904         for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
905                         tz->trips.active[i].flags.valid; i++, trips++);
906
907         if (tz->trips.passive.flags.valid)
908                 tz->thermal_zone =
909                         thermal_zone_device_register("acpitz", trips, 0, tz,
910                                                 &acpi_thermal_zone_ops, NULL,
911                                                      tz->trips.passive.tsp*100,
912                                                      tz->polling_frequency*100);
913         else
914                 tz->thermal_zone =
915                         thermal_zone_device_register("acpitz", trips, 0, tz,
916                                                 &acpi_thermal_zone_ops, NULL,
917                                                 0, tz->polling_frequency*100);
918         if (IS_ERR(tz->thermal_zone))
919                 return -ENODEV;
920
921         result = sysfs_create_link(&tz->device->dev.kobj,
922                                    &tz->thermal_zone->device.kobj, "thermal_zone");
923         if (result)
924                 return result;
925
926         result = sysfs_create_link(&tz->thermal_zone->device.kobj,
927                                    &tz->device->dev.kobj, "device");
928         if (result)
929                 return result;
930
931         status = acpi_attach_data(tz->device->handle,
932                                   acpi_bus_private_data_handler,
933                                   tz->thermal_zone);
934         if (ACPI_FAILURE(status)) {
935                 printk(KERN_ERR PREFIX
936                                 "Error attaching device data\n");
937                 return -ENODEV;
938         }
939
940         tz->tz_enabled = 1;
941
942         dev_info(&tz->device->dev, "registered as thermal_zone%d\n",
943                  tz->thermal_zone->id);
944         return 0;
945 }
946
947 static void acpi_thermal_unregister_thermal_zone(struct acpi_thermal *tz)
948 {
949         sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
950         sysfs_remove_link(&tz->thermal_zone->device.kobj, "device");
951         thermal_zone_device_unregister(tz->thermal_zone);
952         tz->thermal_zone = NULL;
953         acpi_detach_data(tz->device->handle, acpi_bus_private_data_handler);
954 }
955
956
957 /* --------------------------------------------------------------------------
958                                  Driver Interface
959    -------------------------------------------------------------------------- */
960
961 static void acpi_thermal_notify(struct acpi_device *device, u32 event)
962 {
963         struct acpi_thermal *tz = acpi_driver_data(device);
964
965
966         if (!tz)
967                 return;
968
969         switch (event) {
970         case ACPI_THERMAL_NOTIFY_TEMPERATURE:
971                 acpi_thermal_check(tz);
972                 break;
973         case ACPI_THERMAL_NOTIFY_THRESHOLDS:
974                 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_THRESHOLDS);
975                 acpi_thermal_check(tz);
976                 acpi_bus_generate_netlink_event(device->pnp.device_class,
977                                                   dev_name(&device->dev), event, 0);
978                 break;
979         case ACPI_THERMAL_NOTIFY_DEVICES:
980                 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_DEVICES);
981                 acpi_thermal_check(tz);
982                 acpi_bus_generate_netlink_event(device->pnp.device_class,
983                                                   dev_name(&device->dev), event, 0);
984                 break;
985         default:
986                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
987                                   "Unsupported event [0x%x]\n", event));
988                 break;
989         }
990 }
991
992 /*
993  * On some platforms, the AML code has dependency about
994  * the evaluating order of _TMP and _CRT/_HOT/_PSV/_ACx.
995  * 1. On HP Pavilion G4-1016tx, _TMP must be invoked after
996  *    /_CRT/_HOT/_PSV/_ACx, or else system will be power off.
997  * 2. On HP Compaq 6715b/6715s, the return value of _PSV is 0
998  *    if _TMP has never been evaluated.
999  *
1000  * As this dependency is totally transparent to OS, evaluate
1001  * all of them once, in the order of _CRT/_HOT/_PSV/_ACx,
1002  * _TMP, before they are actually used.
1003  */
1004 static void acpi_thermal_aml_dependency_fix(struct acpi_thermal *tz)
1005 {
1006         acpi_handle handle = tz->device->handle;
1007         unsigned long long value;
1008         int i;
1009
1010         acpi_evaluate_integer(handle, "_CRT", NULL, &value);
1011         acpi_evaluate_integer(handle, "_HOT", NULL, &value);
1012         acpi_evaluate_integer(handle, "_PSV", NULL, &value);
1013         for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1014                 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
1015                 acpi_status status;
1016
1017                 status = acpi_evaluate_integer(handle, name, NULL, &value);
1018                 if (status == AE_NOT_FOUND)
1019                         break;
1020         }
1021         acpi_evaluate_integer(handle, "_TMP", NULL, &value);
1022 }
1023
1024 static int acpi_thermal_get_info(struct acpi_thermal *tz)
1025 {
1026         int result = 0;
1027
1028
1029         if (!tz)
1030                 return -EINVAL;
1031
1032         acpi_thermal_aml_dependency_fix(tz);
1033
1034         /* Get trip points [_CRT, _PSV, etc.] (required) */
1035         result = acpi_thermal_get_trip_points(tz);
1036         if (result)
1037                 return result;
1038
1039         /* Get temperature [_TMP] (required) */
1040         result = acpi_thermal_get_temperature(tz);
1041         if (result)
1042                 return result;
1043
1044         /* Set the cooling mode [_SCP] to active cooling (default) */
1045         result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
1046         if (!result)
1047                 tz->flags.cooling_mode = 1;
1048
1049         /* Get default polling frequency [_TZP] (optional) */
1050         if (tzp)
1051                 tz->polling_frequency = tzp;
1052         else
1053                 acpi_thermal_get_polling_frequency(tz);
1054
1055         return 0;
1056 }
1057
1058 /*
1059  * The exact offset between Kelvin and degree Celsius is 273.15. However ACPI
1060  * handles temperature values with a single decimal place. As a consequence,
1061  * some implementations use an offset of 273.1 and others use an offset of
1062  * 273.2. Try to find out which one is being used, to present the most
1063  * accurate and visually appealing number.
1064  *
1065  * The heuristic below should work for all ACPI thermal zones which have a
1066  * critical trip point with a value being a multiple of 0.5 degree Celsius.
1067  */
1068 static void acpi_thermal_guess_offset(struct acpi_thermal *tz)
1069 {
1070         if (tz->trips.critical.flags.valid &&
1071             (tz->trips.critical.temperature % 5) == 1)
1072                 tz->kelvin_offset = 2731;
1073         else
1074                 tz->kelvin_offset = 2732;
1075 }
1076
1077 static int acpi_thermal_add(struct acpi_device *device)
1078 {
1079         int result = 0;
1080         struct acpi_thermal *tz = NULL;
1081
1082
1083         if (!device)
1084                 return -EINVAL;
1085
1086         tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
1087         if (!tz)
1088                 return -ENOMEM;
1089
1090         tz->device = device;
1091         strcpy(tz->name, device->pnp.bus_id);
1092         strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1093         strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
1094         device->driver_data = tz;
1095
1096         result = acpi_thermal_get_info(tz);
1097         if (result)
1098                 goto free_memory;
1099
1100         acpi_thermal_guess_offset(tz);
1101
1102         result = acpi_thermal_register_thermal_zone(tz);
1103         if (result)
1104                 goto free_memory;
1105
1106         printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
1107                acpi_device_name(device), acpi_device_bid(device),
1108                KELVIN_TO_CELSIUS(tz->temperature));
1109         goto end;
1110
1111 free_memory:
1112         kfree(tz);
1113 end:
1114         return result;
1115 }
1116
1117 static int acpi_thermal_remove(struct acpi_device *device)
1118 {
1119         struct acpi_thermal *tz = NULL;
1120
1121         if (!device || !acpi_driver_data(device))
1122                 return -EINVAL;
1123
1124         tz = acpi_driver_data(device);
1125
1126         acpi_thermal_unregister_thermal_zone(tz);
1127         kfree(tz);
1128         return 0;
1129 }
1130
1131 #ifdef CONFIG_PM_SLEEP
1132 static int acpi_thermal_resume(struct device *dev)
1133 {
1134         struct acpi_thermal *tz;
1135         int i, j, power_state, result;
1136
1137         if (!dev)
1138                 return -EINVAL;
1139
1140         tz = acpi_driver_data(to_acpi_device(dev));
1141         if (!tz)
1142                 return -EINVAL;
1143
1144         for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1145                 if (!(&tz->trips.active[i]))
1146                         break;
1147                 if (!tz->trips.active[i].flags.valid)
1148                         break;
1149                 tz->trips.active[i].flags.enabled = 1;
1150                 for (j = 0; j < tz->trips.active[i].devices.count; j++) {
1151                         result = acpi_bus_update_power(
1152                                         tz->trips.active[i].devices.handles[j],
1153                                         &power_state);
1154                         if (result || (power_state != ACPI_STATE_D0)) {
1155                                 tz->trips.active[i].flags.enabled = 0;
1156                                 break;
1157                         }
1158                 }
1159                 tz->state.active |= tz->trips.active[i].flags.enabled;
1160         }
1161
1162         acpi_thermal_check(tz);
1163
1164         return AE_OK;
1165 }
1166 #endif
1167
1168 static int thermal_act(const struct dmi_system_id *d) {
1169
1170         if (act == 0) {
1171                 printk(KERN_NOTICE "ACPI: %s detected: "
1172                         "disabling all active thermal trip points\n", d->ident);
1173                 act = -1;
1174         }
1175         return 0;
1176 }
1177 static int thermal_nocrt(const struct dmi_system_id *d) {
1178
1179         printk(KERN_NOTICE "ACPI: %s detected: "
1180                 "disabling all critical thermal trip point actions.\n", d->ident);
1181         nocrt = 1;
1182         return 0;
1183 }
1184 static int thermal_tzp(const struct dmi_system_id *d) {
1185
1186         if (tzp == 0) {
1187                 printk(KERN_NOTICE "ACPI: %s detected: "
1188                         "enabling thermal zone polling\n", d->ident);
1189                 tzp = 300;      /* 300 dS = 30 Seconds */
1190         }
1191         return 0;
1192 }
1193 static int thermal_psv(const struct dmi_system_id *d) {
1194
1195         if (psv == 0) {
1196                 printk(KERN_NOTICE "ACPI: %s detected: "
1197                         "disabling all passive thermal trip points\n", d->ident);
1198                 psv = -1;
1199         }
1200         return 0;
1201 }
1202
1203 static struct dmi_system_id thermal_dmi_table[] __initdata = {
1204         /*
1205          * Award BIOS on this AOpen makes thermal control almost worthless.
1206          * http://bugzilla.kernel.org/show_bug.cgi?id=8842
1207          */
1208         {
1209          .callback = thermal_act,
1210          .ident = "AOpen i915GMm-HFS",
1211          .matches = {
1212                 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1213                 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1214                 },
1215         },
1216         {
1217          .callback = thermal_psv,
1218          .ident = "AOpen i915GMm-HFS",
1219          .matches = {
1220                 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1221                 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1222                 },
1223         },
1224         {
1225          .callback = thermal_tzp,
1226          .ident = "AOpen i915GMm-HFS",
1227          .matches = {
1228                 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1229                 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1230                 },
1231         },
1232         {
1233          .callback = thermal_nocrt,
1234          .ident = "Gigabyte GA-7ZX",
1235          .matches = {
1236                 DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
1237                 DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
1238                 },
1239         },
1240         {}
1241 };
1242
1243 static int __init acpi_thermal_init(void)
1244 {
1245         int result = 0;
1246
1247         dmi_check_system(thermal_dmi_table);
1248
1249         if (off) {
1250                 printk(KERN_NOTICE "ACPI: thermal control disabled\n");
1251                 return -ENODEV;
1252         }
1253
1254         result = acpi_bus_register_driver(&acpi_thermal_driver);
1255         if (result < 0)
1256                 return -ENODEV;
1257
1258         return 0;
1259 }
1260
1261 static void __exit acpi_thermal_exit(void)
1262 {
1263
1264         acpi_bus_unregister_driver(&acpi_thermal_driver);
1265
1266         return;
1267 }
1268
1269 module_init(acpi_thermal_init);
1270 module_exit(acpi_thermal_exit);