fancontrol.c revision 1.3.4.2 1 1.3.4.2 thorpej /* $NetBSD: fancontrol.c,v 1.3.4.2 2021/08/01 22:42:12 thorpej Exp $ */
2 1.3.4.2 thorpej
3 1.3.4.2 thorpej /*-
4 1.3.4.2 thorpej * Copyright (c) 2021 Michael Lorenz
5 1.3.4.2 thorpej * All rights reserved.
6 1.3.4.2 thorpej *
7 1.3.4.2 thorpej * Redistribution and use in source and binary forms, with or without
8 1.3.4.2 thorpej * modification, are permitted provided that the following conditions
9 1.3.4.2 thorpej * are met:
10 1.3.4.2 thorpej * 1. Redistributions of source code must retain the above copyright
11 1.3.4.2 thorpej * notice, this list of conditions and the following disclaimer.
12 1.3.4.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
13 1.3.4.2 thorpej * notice, this list of conditions and the following disclaimer in the
14 1.3.4.2 thorpej * documentation and/or other materials provided with the distribution.
15 1.3.4.2 thorpej *
16 1.3.4.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.3.4.2 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.3.4.2 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.3.4.2 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.3.4.2 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.3.4.2 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.3.4.2 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.3.4.2 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.3.4.2 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.3.4.2 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.3.4.2 thorpej * POSSIBILITY OF SUCH DAMAGE.
27 1.3.4.2 thorpej */
28 1.3.4.2 thorpej
29 1.3.4.2 thorpej #include <sys/cdefs.h>
30 1.3.4.2 thorpej __KERNEL_RCSID(0, "$NetBSD: fancontrol.c,v 1.3.4.2 2021/08/01 22:42:12 thorpej Exp $");
31 1.3.4.2 thorpej
32 1.3.4.2 thorpej #include <sys/param.h>
33 1.3.4.2 thorpej #include <sys/systm.h>
34 1.3.4.2 thorpej #include <sys/device.h>
35 1.3.4.2 thorpej #include <sys/conf.h>
36 1.3.4.2 thorpej #include <sys/bus.h>
37 1.3.4.2 thorpej #include <sys/sysctl.h>
38 1.3.4.2 thorpej #include <dev/sysmon/sysmonvar.h>
39 1.3.4.2 thorpej
40 1.3.4.2 thorpej #include <macppc/dev/fancontrolvar.h>
41 1.3.4.2 thorpej #include "opt_fancontrol.h"
42 1.3.4.2 thorpej
43 1.3.4.2 thorpej #ifdef FANCONTROL_DEBUG
44 1.3.4.2 thorpej #define DPRINTF printf
45 1.3.4.2 thorpej #else
46 1.3.4.2 thorpej #define DPRINTF while (0) printf
47 1.3.4.2 thorpej #endif
48 1.3.4.2 thorpej
49 1.3.4.2 thorpej int
50 1.3.4.2 thorpej fancontrol_adjust_zone(fancontrol_zone_t *z)
51 1.3.4.2 thorpej {
52 1.3.4.2 thorpej int temp, i, speed, diff, step;
53 1.3.4.2 thorpej
54 1.3.4.2 thorpej if (z->nfans <= 0)
55 1.3.4.2 thorpej return -1;
56 1.3.4.2 thorpej
57 1.3.4.2 thorpej temp = sysmon_envsys_get_max_value(z->filter, true);
58 1.3.4.2 thorpej if (temp == 0) {
59 1.3.4.2 thorpej /* no sensor data - leave fan alone */
60 1.3.4.2 thorpej DPRINTF("nodata\n");
61 1.3.4.2 thorpej return -1;
62 1.3.4.2 thorpej }
63 1.3.4.2 thorpej
64 1.3.4.2 thorpej if (z->Tmin < 30) z->Tmin = 30;
65 1.3.4.2 thorpej if (z->Tmin > 60) z->Tmin = 60;
66 1.3.4.2 thorpej if (z->Tmax > 95) z->Tmax = 95;
67 1.3.4.2 thorpej if (z->Tmax < (z->Tmin + 10)) z->Tmax = z->Tmin + 10;
68 1.3.4.2 thorpej temp = (temp - 273150000) / 1000000;
69 1.3.4.2 thorpej diff = temp - z->Tmin;
70 1.3.4.2 thorpej DPRINTF("%s %d %d\n", z->name, temp, z->Tmin);
71 1.3.4.2 thorpej if (diff < 0) diff = 0;
72 1.3.4.2 thorpej diff = (100 * diff) / (z->Tmax - z->Tmin);
73 1.3.4.2 thorpej
74 1.3.4.2 thorpej /* now adjust each fan to the new speed */
75 1.3.4.2 thorpej for (i = 0; i < z->nfans; i++) {
76 1.3.4.2 thorpej step = (z->fans[i].max_rpm - z->fans[i].min_rpm) / 100;
77 1.3.4.2 thorpej speed = z->fans[i].min_rpm + diff * step;
78 1.3.4.2 thorpej DPRINTF("diff %d base %d %d sp %d\n",
79 1.3.4.2 thorpej diff, z->fans[i].min_rpm, z->fans[i].max_rpm, speed);
80 1.3.4.2 thorpej z->set_rpm(z->cookie, z->fans[i].num, speed);
81 1.3.4.2 thorpej }
82 1.3.4.2 thorpej return 0;
83 1.3.4.2 thorpej }
84 1.3.4.2 thorpej
85 1.3.4.2 thorpej int
86 1.3.4.2 thorpej fancontrol_init_zone(fancontrol_zone_t *z, struct sysctlnode *me)
87 1.3.4.2 thorpej {
88 1.3.4.2 thorpej struct sysctlnode *zone_node, *node;
89 1.3.4.2 thorpej
90 1.3.4.2 thorpej if (z->nfans <= 0) return 0;
91 1.3.4.2 thorpej
92 1.3.4.2 thorpej sysctl_createv(NULL, 0, NULL, (void *) &zone_node,
93 1.3.4.2 thorpej CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
94 1.3.4.2 thorpej CTLTYPE_NODE, z->name, NULL,
95 1.3.4.2 thorpej NULL, 0, NULL, 0,
96 1.3.4.2 thorpej CTL_MACHDEP,
97 1.3.4.2 thorpej me->sysctl_num,
98 1.3.4.2 thorpej CTL_CREATE, CTL_EOL);
99 1.3.4.2 thorpej
100 1.3.4.2 thorpej sysctl_createv(NULL, 0, NULL, (void *) &node,
101 1.3.4.2 thorpej CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
102 1.3.4.2 thorpej CTLTYPE_INT, "Tmin", "minimum temperature",
103 1.3.4.2 thorpej NULL, 0, (void *)&z->Tmin, 0,
104 1.3.4.2 thorpej CTL_MACHDEP,
105 1.3.4.2 thorpej me->sysctl_num,
106 1.3.4.2 thorpej zone_node->sysctl_num,
107 1.3.4.2 thorpej CTL_CREATE, CTL_EOL);
108 1.3.4.2 thorpej
109 1.3.4.2 thorpej sysctl_createv(NULL, 0, NULL, (void *) &node,
110 1.3.4.2 thorpej CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
111 1.3.4.2 thorpej CTLTYPE_INT, "Tmax", "maximum temperature",
112 1.3.4.2 thorpej NULL, 0, (void *)&z->Tmax, 0,
113 1.3.4.2 thorpej CTL_MACHDEP,
114 1.3.4.2 thorpej me->sysctl_num,
115 1.3.4.2 thorpej zone_node->sysctl_num,
116 1.3.4.2 thorpej CTL_CREATE, CTL_EOL);
117 1.3.4.2 thorpej
118 1.3.4.2 thorpej return 0;
119 1.3.4.2 thorpej }
120