tda.c revision 1.5.2.1 1 /* $NetBSD: tda.c,v 1.5.2.1 2012/11/20 03:01:45 tls Exp $ */
2 /* $OpenBSD: tda.c,v 1.4 2008/02/27 17:25:00 robert Exp $ */
3
4 /*
5 * Copyright (c) 2008 Robert Nagy <robert (at) openbsd.org>
6 * Copyright (c) 2008 Mark Kettenis <kettenis (at) openbsd.org>
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 */
20
21 #include <sys/cdefs.h>
22 __KERNEL_RCSID(0, "$NetBSD: tda.c,v 1.5.2.1 2012/11/20 03:01:45 tls Exp $");
23
24 #include <sys/param.h>
25 #include <sys/systm.h>
26 #include <sys/kernel.h>
27 #include <sys/device.h>
28 #include <dev/sysmon/sysmonvar.h>
29 #include <dev/sysmon/sysmon_taskq.h>
30
31 #include <machine/autoconf.h>
32 #include <machine/openfirm.h>
33
34 #include <dev/i2c/i2cvar.h>
35
36 /* fan control registers */
37 #define TDA_SYSFAN_REG 0xf0
38 #define TDA_CPUFAN_REG 0xf2
39 #define TDA_PSFAN_REG 0xf4
40
41 #define TDA_FANSPEED_MIN 0x0c
42 #define TDA_FANSPEED_MAX 0x3f
43
44 #define TDA_PSFAN_ON 0x1f
45 #define TDA_PSFAN_OFF 0x00
46
47 /* Internal and External temperature senor numbers */
48 #define SENSOR_TEMP_EXT 0
49 #define SENSOR_TEMP_INT 1
50
51 #define CPU_TEMP_MAX (67 * 1000000 + 273150000)
52 #define CPU_TEMP_MIN (57 * 1000000 + 273150000)
53 #define SYS_TEMP_MAX (30 * 1000000 + 273150000)
54 #define SYS_TEMP_MIN (20 * 1000000 + 273150000)
55
56 struct tda_softc {
57 device_t sc_dev;
58 i2c_tag_t sc_tag;
59 i2c_addr_t sc_addr;
60
61 u_int16_t sc_cfan_speed; /* current CPU fan speed */
62 u_int16_t sc_sfan_speed; /* current SYS fan speed */
63
64 callout_t sc_timer;
65 };
66
67 int tda_match(device_t, cfdata_t, void *);
68 void tda_attach(device_t, device_t, void *);
69
70 void tda_setspeed(struct tda_softc *);
71 static void tda_adjust(void *);
72 static void tda_timeout(void *);
73
74
75 CFATTACH_DECL_NEW(tda, sizeof(struct tda_softc),
76 tda_match, tda_attach, NULL, NULL);
77
78 int
79 tda_match(device_t parent, cfdata_t match, void *aux)
80 {
81 struct i2c_attach_args *ia = aux;
82 char name[32];
83
84 /* Only attach on the Sun Blade 1000/2000. */
85 if (OF_getprop(findroot(), "name", name, sizeof(name)) <= 0)
86 return (0);
87 if (strcmp(name, "SUNW,Sun-Blade-1000") != 0)
88 return (0);
89
90 /*
91 * No need for "compatible" matching, we know exactly what
92 * firmware calls us.
93 */
94 if (ia->ia_name == NULL)
95 return(0);
96 return strcmp(ia->ia_name, "fan-control") == 0;
97 }
98
99 void
100 tda_attach(device_t parent, device_t self, void *aux)
101 {
102 struct tda_softc *sc = device_private(self);
103 struct i2c_attach_args *ia = aux;
104
105 sc->sc_dev = self;
106 sc->sc_tag = ia->ia_tag;
107 sc->sc_addr = ia->ia_addr;
108
109 aprint_normal(": %s\n", ia->ia_name);
110 aprint_naive(": Environment sensor\n");
111
112 /*
113 * Set the fans to maximum speed and save the power levels;
114 * the controller is write-only.
115 */
116 sc->sc_cfan_speed = sc->sc_sfan_speed = (TDA_FANSPEED_MAX+TDA_FANSPEED_MIN)/2;
117 tda_setspeed(sc);
118
119 callout_init(&sc->sc_timer, CALLOUT_MPSAFE);
120 callout_reset(&sc->sc_timer, hz*20, tda_timeout, sc);
121 }
122
123 static void
124 tda_timeout(void *v)
125 {
126 struct tda_softc *sc = v;
127
128 sysmon_task_queue_sched(0, tda_adjust, sc);
129 callout_reset(&sc->sc_timer, hz*60, tda_timeout, sc);
130 }
131
132 void
133 tda_setspeed(struct tda_softc *sc)
134 {
135 u_int8_t cmd[2];
136
137 if (sc->sc_cfan_speed < TDA_FANSPEED_MIN)
138 sc->sc_cfan_speed = TDA_FANSPEED_MIN;
139 if (sc->sc_sfan_speed < TDA_FANSPEED_MIN)
140 sc->sc_sfan_speed = TDA_FANSPEED_MIN;
141 if (sc->sc_cfan_speed > TDA_FANSPEED_MAX)
142 sc->sc_cfan_speed = TDA_FANSPEED_MAX;
143 if (sc->sc_sfan_speed > TDA_FANSPEED_MAX)
144 sc->sc_sfan_speed = TDA_FANSPEED_MAX;
145
146 iic_acquire_bus(sc->sc_tag, 0);
147
148 cmd[0] = TDA_CPUFAN_REG;
149 cmd[1] = sc->sc_cfan_speed;
150 if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
151 sc->sc_addr, &cmd, sizeof(cmd), NULL, 0, 0)) {
152 aprint_error_dev(sc->sc_dev, "cannot write cpu-fan register\n");
153 iic_release_bus(sc->sc_tag, 0);
154 return;
155 }
156
157 cmd[0] = TDA_SYSFAN_REG;
158 cmd[1] = sc->sc_sfan_speed;
159 if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
160 sc->sc_addr, &cmd, sizeof(cmd), NULL, 0, 0)) {
161 aprint_error_dev(sc->sc_dev, "cannot write system-fan register\n");
162 iic_release_bus(sc->sc_tag, 0);
163 return;
164 }
165
166 iic_release_bus(sc->sc_tag, 0);
167
168 aprint_debug_dev(sc->sc_dev, "changed fan speed to cpu=%d system=%d\n",
169 sc->sc_cfan_speed, sc->sc_sfan_speed);
170 }
171
172 static bool
173 is_cpu_sensor(const envsys_data_t *edata)
174 {
175 if (edata->units != ENVSYS_STEMP)
176 return false;
177 return strcmp(edata->desc, "external") == 0;
178 }
179
180 static bool
181 is_system_sensor(const envsys_data_t *edata)
182 {
183 if (edata->units != ENVSYS_STEMP)
184 return false;
185 return strcmp(edata->desc, "internal") == 0;
186 }
187
188 static void
189 tda_adjust(void *v)
190 {
191 struct tda_softc *sc = v;
192 u_int64_t ctemp, stemp;
193 u_int16_t cspeed, sspeed;
194
195 /* Default to running the fans at maximum speed. */
196 sspeed = cspeed = TDA_FANSPEED_MAX;
197
198 /* fetch maximum current temperature */
199 ctemp = sysmon_envsys_get_max_value(is_cpu_sensor, true);
200 stemp = sysmon_envsys_get_max_value(is_system_sensor, true);
201
202 /* the predicates for selecting sensors must have gone wrong */
203 if (ctemp == 0 || stemp == 0) {
204 aprint_error_dev(sc->sc_dev, "skipping temp adjustment"
205 " - no sensor values\n");
206 return;
207 }
208
209 aprint_debug_dev(sc->sc_dev, "current temperature: cpu %" PRIu64
210 " system %" PRIu64 "\n",
211 ctemp, stemp);
212
213 if (ctemp < CPU_TEMP_MIN)
214 cspeed = TDA_FANSPEED_MIN;
215 else if (ctemp < CPU_TEMP_MAX)
216 cspeed = TDA_FANSPEED_MIN +
217 (ctemp - CPU_TEMP_MIN) *
218 (TDA_FANSPEED_MAX - TDA_FANSPEED_MIN) /
219 (CPU_TEMP_MAX - CPU_TEMP_MIN);
220
221 if (stemp < SYS_TEMP_MIN)
222 sspeed = TDA_FANSPEED_MIN;
223 else if (stemp < SYS_TEMP_MAX)
224 sc->sc_sfan_speed = TDA_FANSPEED_MIN +
225 (stemp - SYS_TEMP_MIN) *
226 (TDA_FANSPEED_MAX - TDA_FANSPEED_MIN) /
227 (SYS_TEMP_MAX - SYS_TEMP_MIN);
228
229 if (sspeed == sc->sc_sfan_speed && cspeed == sc->sc_cfan_speed)
230 return;
231
232 sc->sc_sfan_speed = sspeed;
233 sc->sc_cfan_speed = cspeed;
234 tda_setspeed(sc);
235 }
236