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