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