tda.c revision 1.3.2.2 1 /* $NetBSD: tda.c,v 1.3.2.2 2010/03/11 15:03:00 yamt 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/param.h>
22 #include <sys/systm.h>
23 #include <sys/kernel.h>
24 #include <sys/device.h>
25 #include <dev/sysmon/sysmonvar.h>
26 #include <dev/sysmon/sysmon_taskq.h>
27
28 #include <machine/autoconf.h>
29 #include <machine/openfirm.h>
30
31 #include <dev/i2c/i2cvar.h>
32
33 /* fan control registers */
34 #define TDA_SYSFAN_REG 0xf0
35 #define TDA_CPUFAN_REG 0xf2
36 #define TDA_PSFAN_REG 0xf4
37
38 #define TDA_FANSPEED_MIN 0x0c
39 #define TDA_FANSPEED_MAX 0x3f
40
41 #define TDA_PSFAN_ON 0x1f
42 #define TDA_PSFAN_OFF 0x00
43
44 /* Internal and External temperature senor numbers */
45 #define SENSOR_TEMP_EXT 0
46 #define SENSOR_TEMP_INT 1
47
48 #define CPU_TEMP_MAX (67 * 1000000 + 273150000)
49 #define CPU_TEMP_MIN (57 * 1000000 + 273150000)
50 #define SYS_TEMP_MAX (30 * 1000000 + 273150000)
51 #define SYS_TEMP_MIN (20 * 1000000 + 273150000)
52
53 struct tda_softc {
54 device_t sc_dev;
55 i2c_tag_t sc_tag;
56 i2c_addr_t sc_addr;
57
58 u_int16_t sc_cfan_speed; /* current CPU fan speed */
59 u_int16_t sc_sfan_speed; /* current SYS fan speed */
60
61 callout_t sc_timer;
62 };
63
64 int tda_match(struct device *, struct cfdata *, void *);
65 void tda_attach(struct device *, struct device *, void *);
66
67 void tda_setspeed(struct tda_softc *);
68 static void tda_adjust(void *);
69 static void tda_timeout(void *);
70
71
72 CFATTACH_DECL_NEW(tda, sizeof(struct tda_softc),
73 tda_match, tda_attach, NULL, NULL);
74
75 int
76 tda_match(struct device *parent, struct cfdata *match, void *aux)
77 {
78 struct i2c_attach_args *ia = aux;
79 char name[32];
80
81 /* Only attach on the Sun Blade 1000/2000. */
82 if (OF_getprop(findroot(), "name", name, sizeof(name)) <= 0)
83 return (0);
84 if (strcmp(name, "SUNW,Sun-Blade-1000") != 0)
85 return (0);
86
87 /*
88 * No need for "compatible" matching, we know exactly what
89 * firmware calls us.
90 */
91 return strcmp(ia->ia_name, "fan-control") == 0;
92 }
93
94 void
95 tda_attach(struct device *parent, struct device *self, void *aux)
96 {
97 struct tda_softc *sc = device_private(self);
98 struct i2c_attach_args *ia = aux;
99
100 sc->sc_dev = self;
101 sc->sc_tag = ia->ia_tag;
102 sc->sc_addr = ia->ia_addr;
103
104 aprint_normal(": %s\n", ia->ia_name);
105
106 /*
107 * Set the fans to maximum speed and save the power levels;
108 * the controller is write-only.
109 */
110 sc->sc_cfan_speed = sc->sc_sfan_speed = (TDA_FANSPEED_MAX+TDA_FANSPEED_MIN)/2;
111 tda_setspeed(sc);
112
113 callout_init(&sc->sc_timer, CALLOUT_MPSAFE);
114 callout_reset(&sc->sc_timer, hz*20, tda_timeout, sc);
115 }
116
117 static void
118 tda_timeout(void *v)
119 {
120 struct tda_softc *sc = v;
121
122 sysmon_task_queue_sched(0, tda_adjust, sc);
123 callout_reset(&sc->sc_timer, hz*60, tda_timeout, sc);
124 }
125
126 void
127 tda_setspeed(struct tda_softc *sc)
128 {
129 u_int8_t cmd[2];
130
131 if (sc->sc_cfan_speed < TDA_FANSPEED_MIN)
132 sc->sc_cfan_speed = TDA_FANSPEED_MIN;
133 if (sc->sc_sfan_speed < TDA_FANSPEED_MIN)
134 sc->sc_sfan_speed = TDA_FANSPEED_MIN;
135 if (sc->sc_cfan_speed > TDA_FANSPEED_MAX)
136 sc->sc_cfan_speed = TDA_FANSPEED_MAX;
137 if (sc->sc_sfan_speed > TDA_FANSPEED_MAX)
138 sc->sc_sfan_speed = TDA_FANSPEED_MAX;
139
140 iic_acquire_bus(sc->sc_tag, 0);
141
142 cmd[0] = TDA_CPUFAN_REG;
143 cmd[1] = sc->sc_cfan_speed;
144 if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
145 sc->sc_addr, &cmd, sizeof(cmd), NULL, 0, 0)) {
146 aprint_error_dev(sc->sc_dev, "cannot write cpu-fan register\n");
147 iic_release_bus(sc->sc_tag, 0);
148 return;
149 }
150
151 cmd[0] = TDA_SYSFAN_REG;
152 cmd[1] = sc->sc_sfan_speed;
153 if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
154 sc->sc_addr, &cmd, sizeof(cmd), NULL, 0, 0)) {
155 aprint_error_dev(sc->sc_dev, "cannot write system-fan register\n");
156 iic_release_bus(sc->sc_tag, 0);
157 return;
158 }
159
160 iic_release_bus(sc->sc_tag, 0);
161
162 aprint_debug_dev(sc->sc_dev, "changed fan speed to cpu=%d system=%d\n",
163 sc->sc_cfan_speed, sc->sc_sfan_speed);
164 }
165
166 static bool
167 is_cpu_sensor(const envsys_data_t *edata)
168 {
169 if (edata->units != ENVSYS_STEMP)
170 return false;
171 return strcmp(edata->desc, "external") == 0;
172 }
173
174 static bool
175 is_system_sensor(const envsys_data_t *edata)
176 {
177 if (edata->units != ENVSYS_STEMP)
178 return false;
179 return strcmp(edata->desc, "internal") == 0;
180 }
181
182 static void
183 tda_adjust(void *v)
184 {
185 struct tda_softc *sc = v;
186 u_int64_t ctemp, stemp;
187 u_int16_t cspeed, sspeed;
188
189 /* Default to running the fans at maximum speed. */
190 sspeed = cspeed = TDA_FANSPEED_MAX;
191
192 /* fetch maximum current temperature */
193 ctemp = sysmon_envsys_get_max_value(is_cpu_sensor, true);
194 stemp = sysmon_envsys_get_max_value(is_system_sensor, true);
195
196 /* the predicates for selecting sensors must have gone wrong */
197 if (ctemp == 0 || stemp == 0) {
198 aprint_error_dev(sc->sc_dev, "skipping temp adjustment"
199 " - no sensor values\n");
200 return;
201 }
202
203 aprint_debug_dev(sc->sc_dev, "current temperature: cpu %" PRIu64
204 " system %" PRIu64 "\n",
205 ctemp, stemp);
206
207 if (ctemp < CPU_TEMP_MIN)
208 cspeed = TDA_FANSPEED_MIN;
209 else if (ctemp < CPU_TEMP_MAX)
210 cspeed = TDA_FANSPEED_MIN +
211 (ctemp - CPU_TEMP_MIN) *
212 (TDA_FANSPEED_MAX - TDA_FANSPEED_MIN) /
213 (CPU_TEMP_MAX - CPU_TEMP_MIN);
214
215 if (stemp < SYS_TEMP_MIN)
216 sspeed = TDA_FANSPEED_MIN;
217 else if (stemp < SYS_TEMP_MAX)
218 sc->sc_sfan_speed = TDA_FANSPEED_MIN +
219 (stemp - SYS_TEMP_MIN) *
220 (TDA_FANSPEED_MAX - TDA_FANSPEED_MIN) /
221 (SYS_TEMP_MAX - SYS_TEMP_MIN);
222
223 if (sspeed == sc->sc_sfan_speed && cspeed == sc->sc_cfan_speed)
224 return;
225
226 sc->sc_sfan_speed = sspeed;
227 sc->sc_cfan_speed = cspeed;
228 tda_setspeed(sc);
229 }
230