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