5aef16f3384144b9e281ad7939c7b7bdff1036c0
[firefly-linux-kernel-4.4.55.git] / drivers / platform / x86 / acerhdf.c
1 /*
2  * acerhdf - A driver which monitors the temperature
3  *           of the aspire one netbook, turns on/off the fan
4  *           as soon as the upper/lower threshold is reached.
5  *
6  * (C) 2009 - Peter Feuerer     peter (a) piie.net
7  *                              http://piie.net
8  *     2009 Borislav Petkov <petkovbb@gmail.com>
9  *
10  * Inspired by and many thanks to:
11  *  o acerfand   - Rachel Greenham
12  *  o acer_ec.pl - Michael Kurz     michi.kurz (at) googlemail.com
13  *               - Petr Tomasek     tomasek (#) etf,cuni,cz
14  *               - Carlos Corbacho  cathectic (at) gmail.com
15  *  o lkml       - Matthew Garrett
16  *               - Borislav Petkov
17  *               - Andreas Mohr
18  *
19  *  This program is free software; you can redistribute it and/or modify
20  *  it under the terms of the GNU General Public License as published by
21  *  the Free Software Foundation; either version 2 of the License, or
22  *  (at your option) any later version.
23  *
24  *  This program is distributed in the hope that it will be useful,
25  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
26  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  *  GNU General Public License for more details.
28  *
29  *  You should have received a copy of the GNU General Public License
30  *  along with this program; if not, write to the Free Software
31  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
32  */
33
34 #define pr_fmt(fmt) "acerhdf: " fmt
35
36 #include <linux/kernel.h>
37 #include <linux/module.h>
38 #include <linux/fs.h>
39 #include <linux/dmi.h>
40 #include <acpi/acpi_drivers.h>
41 #include <linux/sched.h>
42 #include <linux/thermal.h>
43 #include <linux/platform_device.h>
44
45 /*
46  * The driver is started with "kernel mode off" by default. That means, the BIOS
47  * is still in control of the fan. In this mode the driver allows to read the
48  * temperature of the cpu and a userspace tool may take over control of the fan.
49  * If the driver is switched to "kernel mode" (e.g. via module parameter) the
50  * driver is in full control of the fan. If you want the module to be started in
51  * kernel mode by default, define the following:
52  */
53 #undef START_IN_KERNEL_MODE
54
55 #define DRV_VER "0.5.17"
56
57 /*
58  * According to the Atom N270 datasheet,
59  * (http://download.intel.com/design/processor/datashts/320032.pdf) the
60  * CPU's optimal operating limits denoted in junction temperature as
61  * measured by the on-die thermal monitor are within 0 <= Tj <= 90. So,
62  * assume 89°C is critical temperature.
63  */
64 #define ACERHDF_TEMP_CRIT 89
65 #define ACERHDF_FAN_OFF 0
66 #define ACERHDF_FAN_AUTO 1
67
68 /*
69  * No matter what value the user puts into the fanon variable, turn on the fan
70  * at 80 degree Celsius to prevent hardware damage
71  */
72 #define ACERHDF_MAX_FANON 80
73
74 /*
75  * Maximum interval between two temperature checks is 15 seconds, as the die
76  * can get hot really fast under heavy load (plus we shouldn't forget about
77  * possible impact of _external_ aggressive sources such as heaters, sun etc.)
78  */
79 #define ACERHDF_MAX_INTERVAL 15
80
81 #ifdef START_IN_KERNEL_MODE
82 static int kernelmode = 1;
83 #else
84 static int kernelmode;
85 #endif
86
87 static unsigned int interval = 10;
88 static unsigned int fanon = 63;
89 static unsigned int fanoff = 58;
90 static unsigned int verbose;
91 static unsigned int fanstate = ACERHDF_FAN_AUTO;
92 static char force_bios[16];
93 static char force_product[16];
94 static unsigned int prev_interval;
95 struct thermal_zone_device *thz_dev;
96 struct thermal_cooling_device *cl_dev;
97 struct platform_device *acerhdf_dev;
98
99 module_param(kernelmode, uint, 0);
100 MODULE_PARM_DESC(kernelmode, "Kernel mode fan control on / off");
101 module_param(interval, uint, 0600);
102 MODULE_PARM_DESC(interval, "Polling interval of temperature check");
103 module_param(fanon, uint, 0600);
104 MODULE_PARM_DESC(fanon, "Turn the fan on above this temperature");
105 module_param(fanoff, uint, 0600);
106 MODULE_PARM_DESC(fanoff, "Turn the fan off below this temperature");
107 module_param(verbose, uint, 0600);
108 MODULE_PARM_DESC(verbose, "Enable verbose dmesg output");
109 module_param_string(force_bios, force_bios, 16, 0);
110 MODULE_PARM_DESC(force_bios, "Force BIOS version and omit BIOS check");
111 module_param_string(force_product, force_product, 16, 0);
112 MODULE_PARM_DESC(force_product, "Force BIOS product and omit BIOS check");
113
114 /*
115  * cmd_off: to switch the fan completely off / to check if the fan is off
116  *      cmd_auto: to set the BIOS in control of the fan. The BIOS regulates then
117  *              the fan speed depending on the temperature
118  */
119 struct fancmd {
120         u8 cmd_off;
121         u8 cmd_auto;
122 };
123
124 /* BIOS settings */
125 struct bios_settings_t {
126         const char *vendor;
127         const char *product;
128         const char *version;
129         unsigned char fanreg;
130         unsigned char tempreg;
131         struct fancmd cmd;
132 };
133
134 /* Register addresses and values for different BIOS versions */
135 static const struct bios_settings_t bios_tbl[] = {
136         /* AOA110 */
137         {"Acer", "AOA110", "v0.3109", 0x55, 0x58, {0x1f, 0x00} },
138         {"Acer", "AOA110", "v0.3114", 0x55, 0x58, {0x1f, 0x00} },
139         {"Acer", "AOA110", "v0.3301", 0x55, 0x58, {0xaf, 0x00} },
140         {"Acer", "AOA110", "v0.3304", 0x55, 0x58, {0xaf, 0x00} },
141         {"Acer", "AOA110", "v0.3305", 0x55, 0x58, {0xaf, 0x00} },
142         {"Acer", "AOA110", "v0.3308", 0x55, 0x58, {0x21, 0x00} },
143         {"Acer", "AOA110", "v0.3309", 0x55, 0x58, {0x21, 0x00} },
144         {"Acer", "AOA110", "v0.3310", 0x55, 0x58, {0x21, 0x00} },
145         /* AOA150 */
146         {"Acer", "AOA150", "v0.3114", 0x55, 0x58, {0x20, 0x00} },
147         {"Acer", "AOA150", "v0.3304", 0x55, 0x58, {0x20, 0x00} },
148         {"Acer", "AOA150", "v0.3305", 0x55, 0x58, {0x20, 0x00} },
149         {"Acer", "AOA150", "v0.3308", 0x55, 0x58, {0x20, 0x00} },
150         {"Acer", "AOA150", "v0.3309", 0x55, 0x58, {0x20, 0x00} },
151         {"Acer", "AOA150", "v0.3310", 0x55, 0x58, {0x20, 0x00} },
152         /* special BIOS / other */
153         {"Gateway", "AOA110", "v0.3103", 0x55, 0x58, {0x21, 0x00} },
154         {"Gateway", "AOA150", "v0.3103", 0x55, 0x58, {0x20, 0x00} },
155         {"Packard Bell", "DOA150", "v0.3104", 0x55, 0x58, {0x21, 0x00} },
156         {"Packard Bell", "AOA110", "v0.3105", 0x55, 0x58, {0x21, 0x00} },
157         /* pewpew-terminator */
158         {"", "", "", 0, 0, {0, 0} }
159 };
160
161 static const struct bios_settings_t *bios_cfg __read_mostly;
162
163 static int acerhdf_get_temp(int *temp)
164 {
165         u8 read_temp;
166
167         if (ec_read(bios_cfg->tempreg, &read_temp))
168                 return -EINVAL;
169
170         *temp = read_temp;
171
172         return 0;
173 }
174
175 static int acerhdf_get_fanstate(int *state)
176 {
177         u8 fan;
178
179         if (ec_read(bios_cfg->fanreg, &fan))
180                 return -EINVAL;
181
182         if (fan != bios_cfg->cmd.cmd_off)
183                 *state = ACERHDF_FAN_AUTO;
184         else
185                 *state = ACERHDF_FAN_OFF;
186
187         return 0;
188 }
189
190 static void acerhdf_change_fanstate(int state)
191 {
192         unsigned char cmd;
193
194         if (verbose)
195                 pr_notice("fan %s\n", (state == ACERHDF_FAN_OFF) ?
196                                 "OFF" : "ON");
197
198         if ((state != ACERHDF_FAN_OFF) && (state != ACERHDF_FAN_AUTO)) {
199                 pr_err("invalid fan state %d requested, setting to auto!\n",
200                         state);
201                 state = ACERHDF_FAN_AUTO;
202         }
203
204         cmd = (state == ACERHDF_FAN_OFF) ? bios_cfg->cmd.cmd_off
205                                          : bios_cfg->cmd.cmd_auto;
206         fanstate = state;
207
208         ec_write(bios_cfg->fanreg, cmd);
209 }
210
211 static void acerhdf_check_param(struct thermal_zone_device *thermal)
212 {
213         if (fanon > ACERHDF_MAX_FANON) {
214                 pr_err("fanon temperature too high, set to %d\n",
215                                 ACERHDF_MAX_FANON);
216                 fanon = ACERHDF_MAX_FANON;
217         }
218
219         if (kernelmode && prev_interval != interval) {
220                 if (interval > ACERHDF_MAX_INTERVAL) {
221                         pr_err("interval too high, set to %d\n",
222                                 ACERHDF_MAX_INTERVAL);
223                         interval = ACERHDF_MAX_INTERVAL;
224                 }
225                 if (verbose)
226                         pr_notice("interval changed to: %d\n",
227                                         interval);
228                 thermal->polling_delay = interval*1000;
229                 prev_interval = interval;
230         }
231 }
232
233 /*
234  * This is the thermal zone callback which does the delayed polling of the fan
235  * state. We do check /sysfs-originating settings here in acerhdf_check_param()
236  * as late as the polling interval is since we can't do that in the respective
237  * accessors of the module parameters.
238  */
239 static int acerhdf_get_ec_temp(struct thermal_zone_device *thermal,
240                                unsigned long *t)
241 {
242         int temp, err = 0;
243
244         acerhdf_check_param(thermal);
245
246         err = acerhdf_get_temp(&temp);
247         if (err)
248                 return err;
249
250         if (verbose)
251                 pr_notice("temp %d\n", temp);
252
253         *t = temp;
254         return 0;
255 }
256
257 static int acerhdf_bind(struct thermal_zone_device *thermal,
258                         struct thermal_cooling_device *cdev)
259 {
260         /* if the cooling device is the one from acerhdf bind it */
261         if (cdev != cl_dev)
262                 return 0;
263
264         if (thermal_zone_bind_cooling_device(thermal, 0, cdev)) {
265                 pr_err("error binding cooling dev\n");
266                 return -EINVAL;
267         }
268         return 0;
269 }
270
271 static int acerhdf_unbind(struct thermal_zone_device *thermal,
272                           struct thermal_cooling_device *cdev)
273 {
274         if (cdev != cl_dev)
275                 return 0;
276
277         if (thermal_zone_unbind_cooling_device(thermal, 0, cdev)) {
278                 pr_err("error unbinding cooling dev\n");
279                 return -EINVAL;
280         }
281         return 0;
282 }
283
284 static inline void acerhdf_revert_to_bios_mode(void)
285 {
286         acerhdf_change_fanstate(ACERHDF_FAN_AUTO);
287         kernelmode = 0;
288         if (thz_dev)
289                 thz_dev->polling_delay = 0;
290         pr_notice("kernel mode fan control OFF\n");
291 }
292 static inline void acerhdf_enable_kernelmode(void)
293 {
294         kernelmode = 1;
295
296         thz_dev->polling_delay = interval*1000;
297         thermal_zone_device_update(thz_dev);
298         pr_notice("kernel mode fan control ON\n");
299 }
300
301 static int acerhdf_get_mode(struct thermal_zone_device *thermal,
302                             enum thermal_device_mode *mode)
303 {
304         if (verbose)
305                 pr_notice("kernel mode fan control %d\n", kernelmode);
306
307         *mode = (kernelmode) ? THERMAL_DEVICE_ENABLED
308                              : THERMAL_DEVICE_DISABLED;
309
310         return 0;
311 }
312
313 /*
314  * set operation mode;
315  * enabled: the thermal layer of the kernel takes care about
316  *          the temperature and the fan.
317  * disabled: the BIOS takes control of the fan.
318  */
319 static int acerhdf_set_mode(struct thermal_zone_device *thermal,
320                             enum thermal_device_mode mode)
321 {
322         if (mode == THERMAL_DEVICE_DISABLED && kernelmode)
323                 acerhdf_revert_to_bios_mode();
324         else if (mode == THERMAL_DEVICE_ENABLED && !kernelmode)
325                 acerhdf_enable_kernelmode();
326
327         return 0;
328 }
329
330 static int acerhdf_get_trip_type(struct thermal_zone_device *thermal, int trip,
331                                  enum thermal_trip_type *type)
332 {
333         if (trip == 0)
334                 *type = THERMAL_TRIP_ACTIVE;
335
336         return 0;
337 }
338
339 static int acerhdf_get_trip_temp(struct thermal_zone_device *thermal, int trip,
340                                  unsigned long *temp)
341 {
342         if (trip == 0)
343                 *temp = fanon;
344
345         return 0;
346 }
347
348 static int acerhdf_get_crit_temp(struct thermal_zone_device *thermal,
349                                  unsigned long *temperature)
350 {
351         *temperature = ACERHDF_TEMP_CRIT;
352         return 0;
353 }
354
355 /* bind callback functions to thermalzone */
356 struct thermal_zone_device_ops acerhdf_dev_ops = {
357         .bind = acerhdf_bind,
358         .unbind = acerhdf_unbind,
359         .get_temp = acerhdf_get_ec_temp,
360         .get_mode = acerhdf_get_mode,
361         .set_mode = acerhdf_set_mode,
362         .get_trip_type = acerhdf_get_trip_type,
363         .get_trip_temp = acerhdf_get_trip_temp,
364         .get_crit_temp = acerhdf_get_crit_temp,
365 };
366
367
368 /*
369  * cooling device callback functions
370  * get maximal fan cooling state
371  */
372 static int acerhdf_get_max_state(struct thermal_cooling_device *cdev,
373                                  unsigned long *state)
374 {
375         *state = 1;
376
377         return 0;
378 }
379
380 static int acerhdf_get_cur_state(struct thermal_cooling_device *cdev,
381                                  unsigned long *state)
382 {
383         int err = 0, tmp;
384
385         err = acerhdf_get_fanstate(&tmp);
386         if (err)
387                 return err;
388
389         *state = (tmp == ACERHDF_FAN_AUTO) ? 1 : 0;
390         return 0;
391 }
392
393 /* change current fan state - is overwritten when running in kernel mode */
394 static int acerhdf_set_cur_state(struct thermal_cooling_device *cdev,
395                                  unsigned long state)
396 {
397         int cur_temp, cur_state, err = 0;
398
399         if (!kernelmode)
400                 return 0;
401
402         err = acerhdf_get_temp(&cur_temp);
403         if (err) {
404                 pr_err("error reading temperature, hand off control to BIOS\n");
405                 goto err_out;
406         }
407
408         err = acerhdf_get_fanstate(&cur_state);
409         if (err) {
410                 pr_err("error reading fan state, hand off control to BIOS\n");
411                 goto err_out;
412         }
413
414         if (state == 0) {
415                 /* turn fan off only if below fanoff temperature */
416                 if ((cur_state == ACERHDF_FAN_AUTO) &&
417                     (cur_temp < fanoff))
418                         acerhdf_change_fanstate(ACERHDF_FAN_OFF);
419         } else {
420                 if (cur_state == ACERHDF_FAN_OFF)
421                         acerhdf_change_fanstate(ACERHDF_FAN_AUTO);
422         }
423         return 0;
424
425 err_out:
426         acerhdf_revert_to_bios_mode();
427         return -EINVAL;
428 }
429
430 /* bind fan callbacks to fan device */
431 struct thermal_cooling_device_ops acerhdf_cooling_ops = {
432         .get_max_state = acerhdf_get_max_state,
433         .get_cur_state = acerhdf_get_cur_state,
434         .set_cur_state = acerhdf_set_cur_state,
435 };
436
437 /* suspend / resume functionality */
438 static int acerhdf_suspend(struct device *dev)
439 {
440         if (kernelmode)
441                 acerhdf_change_fanstate(ACERHDF_FAN_AUTO);
442
443         if (verbose)
444                 pr_notice("going suspend\n");
445
446         return 0;
447 }
448
449 static int __devinit acerhdf_probe(struct platform_device *device)
450 {
451         return 0;
452 }
453
454 static int acerhdf_remove(struct platform_device *device)
455 {
456         return 0;
457 }
458
459 static struct dev_pm_ops acerhdf_pm_ops = {
460         .suspend = acerhdf_suspend,
461         .freeze  = acerhdf_suspend,
462 };
463
464 static struct platform_driver acerhdf_driver = {
465         .driver = {
466                 .name  = "acerhdf",
467                 .owner = THIS_MODULE,
468                 .pm    = &acerhdf_pm_ops,
469         },
470         .probe = acerhdf_probe,
471         .remove = acerhdf_remove,
472 };
473
474
475 /* check hardware */
476 static int acerhdf_check_hardware(void)
477 {
478         char const *vendor, *version, *product;
479         int i;
480         unsigned long prod_len = 0;
481
482         /* get BIOS data */
483         vendor  = dmi_get_system_info(DMI_SYS_VENDOR);
484         version = dmi_get_system_info(DMI_BIOS_VERSION);
485         product = dmi_get_system_info(DMI_PRODUCT_NAME);
486
487
488         pr_info("Acer Aspire One Fan driver, v.%s\n", DRV_VER);
489
490         if (force_bios[0]) {
491                 version = force_bios;
492                 pr_info("forcing BIOS version: %s\n", version);
493                 kernelmode = 0;
494         }
495
496         if (force_product[0]) {
497                 product = force_product;
498                 pr_info("forcing BIOS product: %s\n", product);
499                 kernelmode = 0;
500         }
501
502         prod_len = strlen(product);
503
504         if (verbose)
505                 pr_info("BIOS info: %s %s, product: %s\n",
506                         vendor, version, product);
507
508         /* search BIOS version and vendor in BIOS settings table */
509         for (i = 0; bios_tbl[i].version[0]; i++) {
510                 if (strlen(bios_tbl[i].product) >= prod_len &&
511                     !strncmp(bios_tbl[i].product, product,
512                            strlen(bios_tbl[i].product)) &&
513                     !strcmp(bios_tbl[i].vendor, vendor) &&
514                     !strcmp(bios_tbl[i].version, version)) {
515                         bios_cfg = &bios_tbl[i];
516                         break;
517                 }
518         }
519
520         if (!bios_cfg) {
521                 pr_err("unknown (unsupported) BIOS version %s/%s/%s, "
522                         "please report, aborting!\n", vendor, product, version);
523                 return -EINVAL;
524         }
525
526         /*
527          * if started with kernel mode off, prevent the kernel from switching
528          * off the fan
529          */
530         if (!kernelmode) {
531                 pr_notice("Fan control off, to enable do:\n");
532                 pr_notice("echo -n \"enabled\" > "
533                         "/sys/class/thermal/thermal_zone0/mode\n");
534         }
535
536         return 0;
537 }
538
539 static int acerhdf_register_platform(void)
540 {
541         int err = 0;
542
543         err = platform_driver_register(&acerhdf_driver);
544         if (err)
545                 return err;
546
547         acerhdf_dev = platform_device_alloc("acerhdf", -1);
548         platform_device_add(acerhdf_dev);
549
550         return 0;
551 }
552
553 static void acerhdf_unregister_platform(void)
554 {
555         if (!acerhdf_dev)
556                 return;
557
558         platform_device_del(acerhdf_dev);
559         platform_driver_unregister(&acerhdf_driver);
560 }
561
562 static int acerhdf_register_thermal(void)
563 {
564         cl_dev = thermal_cooling_device_register("acerhdf-fan", NULL,
565                                                  &acerhdf_cooling_ops);
566
567         if (IS_ERR(cl_dev))
568                 return -EINVAL;
569
570         thz_dev = thermal_zone_device_register("acerhdf", 1, NULL,
571                                               &acerhdf_dev_ops, 0, 0, 0,
572                                               (kernelmode) ? interval*1000 : 0);
573         if (IS_ERR(thz_dev))
574                 return -EINVAL;
575
576         return 0;
577 }
578
579 static void acerhdf_unregister_thermal(void)
580 {
581         if (cl_dev) {
582                 thermal_cooling_device_unregister(cl_dev);
583                 cl_dev = NULL;
584         }
585
586         if (thz_dev) {
587                 thermal_zone_device_unregister(thz_dev);
588                 thz_dev = NULL;
589         }
590 }
591
592 static int __init acerhdf_init(void)
593 {
594         int err = 0;
595
596         err = acerhdf_check_hardware();
597         if (err)
598                 goto out_err;
599
600         err = acerhdf_register_platform();
601         if (err)
602                 goto err_unreg;
603
604         err = acerhdf_register_thermal();
605         if (err)
606                 goto err_unreg;
607
608         return 0;
609
610 err_unreg:
611         acerhdf_unregister_thermal();
612         acerhdf_unregister_platform();
613
614 out_err:
615         return -ENODEV;
616 }
617
618 static void __exit acerhdf_exit(void)
619 {
620         acerhdf_change_fanstate(ACERHDF_FAN_AUTO);
621         acerhdf_unregister_thermal();
622         acerhdf_unregister_platform();
623 }
624
625 MODULE_LICENSE("GPL");
626 MODULE_AUTHOR("Peter Feuerer");
627 MODULE_DESCRIPTION("Aspire One temperature and fan driver");
628 MODULE_ALIAS("dmi:*:*Acer*:*:");
629 MODULE_ALIAS("dmi:*:*Gateway*:*:");
630 MODULE_ALIAS("dmi:*:*Packard Bell*:*:");
631
632 module_init(acerhdf_init);
633 module_exit(acerhdf_exit);