dbcool.c revision 1.20 1 /* $NetBSD: dbcool.c,v 1.20 2010/03/20 19:04:51 pgoyette Exp $ */
2
3 /*-
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Paul Goyette
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * a driver for the dbCool(tm) family of environmental controllers
34 *
35 * Data sheets for the various supported chips are available at
36 *
37 * http://www.onsemi.com/pub/Collateral/ADM1027-D.PDF
38 * http://www.onsemi.com/pub/Collateral/ADM1030-D.PDF
39 * http://www.onsemi.com/pub/Collateral/ADT7463-D.PDF
40 * http://www.onsemi.com/pub/Collateral/ADT7466.PDF
41 * http://www.onsemi.com/pub/Collateral/ADT7467-D.PDF
42 * http://www.onsemi.com/pub/Collateral/ADT7468-D.PDF
43 * http://www.onsemi.com/pub/Collateral/ADT7473-D.PDF
44 * http://www.onsemi.com/pub/Collateral/ADT7475-D.PDF
45 * http://www.onsemi.com/pub/Collateral/ADT7476-D.PDF
46 * http://www.onsemi.com/pub/Collateral/ADT7490-D.PDF
47 *
48 * (URLs are correct as of October 5, 2008)
49 */
50
51 #include <sys/cdefs.h>
52 __KERNEL_RCSID(0, "$NetBSD: dbcool.c,v 1.20 2010/03/20 19:04:51 pgoyette Exp $");
53
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/kernel.h>
57 #include <sys/device.h>
58 #include <sys/malloc.h>
59 #include <sys/sysctl.h>
60
61 #include <uvm/uvm_extern.h>
62
63 #include <dev/i2c/dbcool_var.h>
64 #include <dev/i2c/dbcool_reg.h>
65
66 /* Config interface */
67 static int dbcool_match(device_t, cfdata_t, void *);
68 static void dbcool_attach(device_t, device_t, void *);
69 static int dbcool_detach(device_t, int);
70
71 /* Device attributes */
72 static int dbcool_supply_voltage(struct dbcool_softc *);
73 static bool dbcool_islocked(struct dbcool_softc *);
74
75 /* Sensor read functions */
76 static void dbcool_refresh(struct sysmon_envsys *, envsys_data_t *);
77 static int dbcool_read_rpm(struct dbcool_softc *, uint8_t);
78 static int dbcool_read_temp(struct dbcool_softc *, uint8_t, bool);
79 static int dbcool_read_volt(struct dbcool_softc *, uint8_t, int, bool);
80
81 /* Sensor get/set limit functions */
82 static void dbcool_get_limits(struct sysmon_envsys *, envsys_data_t *,
83 sysmon_envsys_lim_t *, uint32_t *);
84 static void dbcool_get_temp_limits(struct dbcool_softc *, int,
85 sysmon_envsys_lim_t *, uint32_t *);
86 static void dbcool_get_volt_limits(struct dbcool_softc *, int,
87 sysmon_envsys_lim_t *, uint32_t *);
88 static void dbcool_get_fan_limits(struct dbcool_softc *, int,
89 sysmon_envsys_lim_t *, uint32_t *);
90
91 static void dbcool_set_limits(struct sysmon_envsys *, envsys_data_t *,
92 sysmon_envsys_lim_t *, uint32_t *);
93 static void dbcool_set_temp_limits(struct dbcool_softc *, int,
94 sysmon_envsys_lim_t *, uint32_t *);
95 static void dbcool_set_volt_limits(struct dbcool_softc *, int,
96 sysmon_envsys_lim_t *, uint32_t *);
97 static void dbcool_set_fan_limits(struct dbcool_softc *, int,
98 sysmon_envsys_lim_t *, uint32_t *);
99
100 /* SYSCTL Helpers */
101 static int sysctl_dbcool_temp(SYSCTLFN_PROTO);
102 static int sysctl_adm1030_temp(SYSCTLFN_PROTO);
103 static int sysctl_adm1030_trange(SYSCTLFN_PROTO);
104 static int sysctl_dbcool_duty(SYSCTLFN_PROTO);
105 static int sysctl_dbcool_behavior(SYSCTLFN_PROTO);
106 static int sysctl_dbcool_slope(SYSCTLFN_PROTO);
107 static int sysctl_dbcool_thyst(SYSCTLFN_PROTO);
108
109 /* Set-up subroutines */
110 static void dbcool_setup_controllers(struct dbcool_softc *);
111 static int dbcool_setup_sensors(struct dbcool_softc *);
112 static int dbcool_attach_sensor(struct dbcool_softc *, int);
113 static int dbcool_attach_temp_control(struct dbcool_softc *, int,
114 struct chip_id *);
115
116 #ifdef DBCOOL_DEBUG
117 static int sysctl_dbcool_reg_select(SYSCTLFN_PROTO);
118 static int sysctl_dbcool_reg_access(SYSCTLFN_PROTO);
119 #endif /* DBCOOL_DEBUG */
120
121 /*
122 * Descriptions for SYSCTL entries
123 */
124 struct dbc_sysctl_info {
125 const char *name;
126 const char *desc;
127 bool lockable;
128 int (*helper)(SYSCTLFN_PROTO);
129 };
130
131 static struct dbc_sysctl_info dbc_sysctl_table[] = {
132 /*
133 * The first several entries must remain in the same order as the
134 * corresponding entries in enum dbc_pwm_params
135 */
136 { "behavior", "operating behavior and temp selector",
137 true, sysctl_dbcool_behavior },
138 { "min_duty", "minimum fan controller PWM duty cycle",
139 true, sysctl_dbcool_duty },
140 { "max_duty", "maximum fan controller PWM duty cycle",
141 true, sysctl_dbcool_duty },
142 { "cur_duty", "current fan controller PWM duty cycle",
143 false, sysctl_dbcool_duty },
144
145 /*
146 * The rest of these should be in the order in which they
147 * are to be stored in the sysctl tree; the table index is
148 * used as the high-order bits of the sysctl_num to maintain
149 * the sequence.
150 *
151 * If you rearrange the order of these items, be sure to
152 * update the sysctl_index in the XXX_sensor_table[] for
153 * the various chips!
154 */
155 { "Trange", "temp slope/range to reach 100% duty cycle",
156 true, sysctl_dbcool_slope },
157 { "Tmin", "temp at which to start fan controller",
158 true, sysctl_dbcool_temp },
159 { "Ttherm", "temp at which THERM is asserted",
160 true, sysctl_dbcool_temp },
161 { "Thyst", "temp hysteresis for stopping fan controller",
162 true, sysctl_dbcool_thyst },
163 { "Tmin", "temp at which to start fan controller",
164 true, sysctl_adm1030_temp },
165 { "Trange", "temp slope/range to reach 100% duty cycle",
166 true, sysctl_adm1030_trange },
167 };
168
169 static const char *dbc_sensor_names[] = {
170 "l_temp", "r1_temp", "r2_temp", "Vccp", "Vcc", "fan1",
171 "fan2", "fan3", "fan4", "AIN1", "AIN2", "V2dot5",
172 "V5", "V12", "Vtt", "Imon", "VID"
173 };
174
175 /*
176 * Following table derived from product data-sheets
177 */
178 static int64_t nominal_voltages[] = {
179 -1, /* Vcc can be either 3.3 or 5.0V
180 at 3/4 scale */
181 2249939, /* Vccp 2.25V 3/4 scale */
182 2497436, /* 2.5VIN 2.5V 3/4 scale */
183 5002466, /* 5VIN 5V 3/4 scale */
184 12000000, /* 12VIN 12V 3/4 scale */
185 1690809, /* Vtt, Imon 2.25V full scale */
186 1689600, /* AIN1, AIN2 2.25V full scale */
187 0
188 };
189
190 /*
191 * Sensor-type, { val-reg, hilim-reg, lolim-reg}, name-idx, sysctl-table-idx,
192 * nom-voltage-index
193 */
194 struct dbcool_sensor ADT7490_sensor_table[] = {
195 { DBC_TEMP, { DBCOOL_LOCAL_TEMP,
196 DBCOOL_LOCAL_HIGHLIM,
197 DBCOOL_LOCAL_LOWLIM }, 0, 0, 0 },
198 { DBC_TEMP, { DBCOOL_REMOTE1_TEMP,
199 DBCOOL_REMOTE1_HIGHLIM,
200 DBCOOL_REMOTE1_LOWLIM }, 1, 0, 0 },
201 { DBC_TEMP, { DBCOOL_REMOTE2_TEMP,
202 DBCOOL_REMOTE2_HIGHLIM,
203 DBCOOL_REMOTE2_LOWLIM }, 2, 0, 0 },
204 { DBC_VOLT, { DBCOOL_VCCP,
205 DBCOOL_VCCP_HIGHLIM,
206 DBCOOL_VCCP_LOWLIM }, 3, 0, 1 },
207 { DBC_VOLT, { DBCOOL_VCC,
208 DBCOOL_VCC_HIGHLIM,
209 DBCOOL_VCC_LOWLIM }, 4, 0, 0 },
210 { DBC_VOLT, { DBCOOL_25VIN,
211 DBCOOL_25VIN_HIGHLIM,
212 DBCOOL_25VIN_LOWLIM }, 11, 0, 2 },
213 { DBC_VOLT, { DBCOOL_5VIN,
214 DBCOOL_5VIN_HIGHLIM,
215 DBCOOL_5VIN_LOWLIM }, 12, 0, 3 },
216 { DBC_VOLT, { DBCOOL_12VIN,
217 DBCOOL_12VIN_HIGHLIM,
218 DBCOOL_12VIN_LOWLIM }, 13, 0, 4 },
219 { DBC_VOLT, { DBCOOL_VTT,
220 DBCOOL_VTT_HIGHLIM,
221 DBCOOL_VTT_LOWLIM }, 14, 0, 5 },
222 { DBC_VOLT, { DBCOOL_IMON,
223 DBCOOL_IMON_HIGHLIM,
224 DBCOOL_IMON_LOWLIM }, 15, 0, 5 },
225 { DBC_FAN, { DBCOOL_FAN1_TACH_LSB,
226 DBCOOL_NO_REG,
227 DBCOOL_TACH1_MIN_LSB }, 5, 0, 0 },
228 { DBC_FAN, { DBCOOL_FAN2_TACH_LSB,
229 DBCOOL_NO_REG,
230 DBCOOL_TACH2_MIN_LSB }, 6, 0, 0 },
231 { DBC_FAN, { DBCOOL_FAN3_TACH_LSB,
232 DBCOOL_NO_REG,
233 DBCOOL_TACH3_MIN_LSB }, 7, 0, 0 },
234 { DBC_FAN, { DBCOOL_FAN4_TACH_LSB,
235 DBCOOL_NO_REG,
236 DBCOOL_TACH4_MIN_LSB }, 8, 0, 0 },
237 { DBC_VID, { DBCOOL_VID_REG,
238 DBCOOL_NO_REG,
239 DBCOOL_NO_REG }, 16, 0, 0 },
240 { DBC_CTL, { DBCOOL_LOCAL_TMIN,
241 DBCOOL_NO_REG,
242 DBCOOL_NO_REG }, 0, 5, 0 },
243 { DBC_CTL, { DBCOOL_LOCAL_TTHRESH,
244 DBCOOL_NO_REG,
245 DBCOOL_NO_REG }, 0, 6, 0 },
246 { DBC_CTL, { DBCOOL_R1_LCL_TMIN_HYST | 0x80,
247 DBCOOL_NO_REG,
248 DBCOOL_NO_REG }, 0, 7, 0 },
249 { DBC_CTL, { DBCOOL_REMOTE1_TMIN,
250 DBCOOL_NO_REG,
251 DBCOOL_NO_REG }, 1, 5, 0 },
252 { DBC_CTL, { DBCOOL_REMOTE1_TTHRESH,
253 DBCOOL_NO_REG,
254 DBCOOL_NO_REG }, 1, 6, 0 },
255 { DBC_CTL, { DBCOOL_R1_LCL_TMIN_HYST,
256 DBCOOL_NO_REG,
257 DBCOOL_NO_REG }, 1, 7, 0 },
258 { DBC_CTL, { DBCOOL_REMOTE2_TMIN,
259 DBCOOL_NO_REG,
260 DBCOOL_NO_REG }, 2, 5, 0 },
261 { DBC_CTL, { DBCOOL_REMOTE2_TTHRESH,
262 DBCOOL_NO_REG,
263 DBCOOL_NO_REG }, 2, 6, 0 },
264 { DBC_CTL, { DBCOOL_R2_TMIN_HYST,
265 DBCOOL_NO_REG,
266 DBCOOL_NO_REG }, 2, 7, 0 },
267 { DBC_EOF, { 0, 0, 0 }, 0, 0, 0 }
268 };
269
270 struct dbcool_sensor ADT7476_sensor_table[] = {
271 { DBC_TEMP, { DBCOOL_LOCAL_TEMP,
272 DBCOOL_LOCAL_HIGHLIM,
273 DBCOOL_LOCAL_LOWLIM }, 0, 0, 0 },
274 { DBC_TEMP, { DBCOOL_REMOTE1_TEMP,
275 DBCOOL_REMOTE1_HIGHLIM,
276 DBCOOL_REMOTE1_LOWLIM }, 1, 0, 0 },
277 { DBC_TEMP, { DBCOOL_REMOTE2_TEMP,
278 DBCOOL_REMOTE2_HIGHLIM,
279 DBCOOL_REMOTE2_LOWLIM }, 2, 0, 0 },
280 { DBC_VOLT, { DBCOOL_VCCP,
281 DBCOOL_VCCP_HIGHLIM,
282 DBCOOL_VCCP_LOWLIM }, 3, 0, 1 },
283 { DBC_VOLT, { DBCOOL_VCC,
284 DBCOOL_VCC_HIGHLIM,
285 DBCOOL_VCC_LOWLIM }, 4, 0, 0 },
286 { DBC_VOLT, { DBCOOL_25VIN,
287 DBCOOL_25VIN_HIGHLIM,
288 DBCOOL_25VIN_LOWLIM }, 11, 0, 2 },
289 { DBC_VOLT, { DBCOOL_5VIN,
290 DBCOOL_5VIN_HIGHLIM,
291 DBCOOL_5VIN_LOWLIM }, 12, 0, 3 },
292 { DBC_VOLT, { DBCOOL_12VIN,
293 DBCOOL_12VIN_HIGHLIM,
294 DBCOOL_12VIN_LOWLIM }, 13, 0, 4 },
295 { DBC_FAN, { DBCOOL_FAN1_TACH_LSB,
296 DBCOOL_NO_REG,
297 DBCOOL_TACH1_MIN_LSB }, 5, 0, 0 },
298 { DBC_FAN, { DBCOOL_FAN2_TACH_LSB,
299 DBCOOL_NO_REG,
300 DBCOOL_TACH2_MIN_LSB }, 6, 0, 0 },
301 { DBC_FAN, { DBCOOL_FAN3_TACH_LSB,
302 DBCOOL_NO_REG,
303 DBCOOL_TACH3_MIN_LSB }, 7, 0, 0 },
304 { DBC_FAN, { DBCOOL_FAN4_TACH_LSB,
305 DBCOOL_NO_REG,
306 DBCOOL_TACH4_MIN_LSB }, 8, 0, 0 },
307 { DBC_VID, { DBCOOL_VID_REG,
308 DBCOOL_NO_REG,
309 DBCOOL_NO_REG }, 16, 0, 0 },
310 { DBC_CTL, { DBCOOL_LOCAL_TMIN,
311 DBCOOL_NO_REG,
312 DBCOOL_NO_REG }, 0, 5, 0 },
313 { DBC_CTL, { DBCOOL_LOCAL_TTHRESH,
314 DBCOOL_NO_REG,
315 DBCOOL_NO_REG }, 0, 6, 0 },
316 { DBC_CTL, { DBCOOL_R1_LCL_TMIN_HYST | 0x80,
317 DBCOOL_NO_REG,
318 DBCOOL_NO_REG }, 0, 7, 0 },
319 { DBC_CTL, { DBCOOL_REMOTE1_TMIN,
320 DBCOOL_NO_REG,
321 DBCOOL_NO_REG }, 1, 5, 0 },
322 { DBC_CTL, { DBCOOL_REMOTE1_TTHRESH,
323 DBCOOL_NO_REG,
324 DBCOOL_NO_REG }, 1, 6, 0 },
325 { DBC_CTL, { DBCOOL_R1_LCL_TMIN_HYST,
326 DBCOOL_NO_REG,
327 DBCOOL_NO_REG }, 1, 7, 0 },
328 { DBC_CTL, { DBCOOL_REMOTE2_TMIN,
329 DBCOOL_NO_REG,
330 DBCOOL_NO_REG }, 2, 5, 0 },
331 { DBC_CTL, { DBCOOL_REMOTE2_TTHRESH,
332 DBCOOL_NO_REG,
333 DBCOOL_NO_REG }, 2, 6, 0 },
334 { DBC_CTL, { DBCOOL_R2_TMIN_HYST,
335 DBCOOL_NO_REG,
336 DBCOOL_NO_REG }, 2, 7, 0 },
337 { DBC_EOF, { 0, 0, 0 }, 0, 0, 0 }
338 };
339
340 struct dbcool_sensor ADT7475_sensor_table[] = {
341 { DBC_TEMP, { DBCOOL_LOCAL_TEMP,
342 DBCOOL_LOCAL_HIGHLIM,
343 DBCOOL_LOCAL_LOWLIM }, 0, 0, 0 },
344 { DBC_TEMP, { DBCOOL_REMOTE1_TEMP,
345 DBCOOL_REMOTE1_HIGHLIM,
346 DBCOOL_REMOTE1_LOWLIM }, 1, 0, 0 },
347 { DBC_TEMP, { DBCOOL_REMOTE2_TEMP,
348 DBCOOL_REMOTE2_HIGHLIM,
349 DBCOOL_REMOTE2_LOWLIM }, 2, 0, 0 },
350 { DBC_VOLT, { DBCOOL_VCCP,
351 DBCOOL_VCCP_HIGHLIM,
352 DBCOOL_VCCP_LOWLIM }, 3, 0, 1 },
353 { DBC_VOLT, { DBCOOL_VCC,
354 DBCOOL_VCC_HIGHLIM,
355 DBCOOL_VCC_LOWLIM }, 4, 0, 0 },
356 { DBC_FAN, { DBCOOL_FAN1_TACH_LSB,
357 DBCOOL_NO_REG,
358 DBCOOL_TACH1_MIN_LSB }, 5, 0, 0 },
359 { DBC_FAN, { DBCOOL_FAN2_TACH_LSB,
360 DBCOOL_NO_REG,
361 DBCOOL_TACH2_MIN_LSB }, 6, 0, 0 },
362 { DBC_FAN, { DBCOOL_FAN3_TACH_LSB,
363 DBCOOL_NO_REG,
364 DBCOOL_TACH3_MIN_LSB }, 7, 0, 0 },
365 { DBC_FAN, { DBCOOL_FAN4_TACH_LSB,
366 DBCOOL_NO_REG,
367 DBCOOL_TACH4_MIN_LSB }, 8, 0, 0 },
368 { DBC_CTL, { DBCOOL_LOCAL_TMIN,
369 DBCOOL_NO_REG,
370 DBCOOL_NO_REG }, 0, 5, 0 },
371 { DBC_CTL, { DBCOOL_LOCAL_TTHRESH,
372 DBCOOL_NO_REG,
373 DBCOOL_NO_REG }, 0, 6, 0 },
374 { DBC_CTL, { DBCOOL_R1_LCL_TMIN_HYST | 0x80,
375 DBCOOL_NO_REG,
376 DBCOOL_NO_REG }, 0, 7, 0 },
377 { DBC_CTL, { DBCOOL_REMOTE1_TMIN,
378 DBCOOL_NO_REG,
379 DBCOOL_NO_REG }, 1, 5, 0 },
380 { DBC_CTL, { DBCOOL_REMOTE1_TTHRESH,
381 DBCOOL_NO_REG,
382 DBCOOL_NO_REG }, 1, 6, 0 },
383 { DBC_CTL, { DBCOOL_R1_LCL_TMIN_HYST,
384 DBCOOL_NO_REG,
385 DBCOOL_NO_REG }, 1, 7, 0 },
386 { DBC_CTL, { DBCOOL_REMOTE2_TMIN,
387 DBCOOL_NO_REG,
388 DBCOOL_NO_REG }, 2, 5, 0 },
389 { DBC_CTL, { DBCOOL_REMOTE2_TTHRESH,
390 DBCOOL_NO_REG,
391 DBCOOL_NO_REG }, 2, 6, 0 },
392 { DBC_CTL, { DBCOOL_R2_TMIN_HYST,
393 DBCOOL_NO_REG,
394 DBCOOL_NO_REG }, 2, 7, 0 },
395 { DBC_EOF, { 0, 0, 0 }, 0, 0, 0 }
396 };
397
398 /*
399 * The registers of dbcool_power_control must be in the same order as
400 * in enum dbc_pwm_params
401 */
402 struct dbcool_power_control ADT7475_power_table[] = {
403 { { DBCOOL_PWM1_CTL, DBCOOL_PWM1_MINDUTY,
404 DBCOOL_PWM1_MAXDUTY, DBCOOL_PWM1_CURDUTY },
405 "fan_control_1" },
406 { { DBCOOL_PWM2_CTL, DBCOOL_PWM2_MINDUTY,
407 DBCOOL_PWM2_MAXDUTY, DBCOOL_PWM2_CURDUTY },
408 "fan_control_2" },
409 { { DBCOOL_PWM3_CTL, DBCOOL_PWM3_MINDUTY,
410 DBCOOL_PWM3_MAXDUTY, DBCOOL_PWM3_CURDUTY },
411 "fan_control_3" },
412 { { 0, 0, 0, 0 }, NULL }
413 };
414
415 struct dbcool_sensor ADT7466_sensor_table[] = {
416 { DBC_TEMP, { DBCOOL_ADT7466_LCL_TEMP_MSB,
417 DBCOOL_ADT7466_LCL_TEMP_HILIM,
418 DBCOOL_ADT7466_LCL_TEMP_LOLIM }, 0, 0, 0 },
419 { DBC_TEMP, { DBCOOL_ADT7466_REM_TEMP_MSB,
420 DBCOOL_ADT7466_REM_TEMP_HILIM,
421 DBCOOL_ADT7466_REM_TEMP_LOLIM }, 1, 0, 0 },
422 { DBC_VOLT, { DBCOOL_ADT7466_VCC,
423 DBCOOL_ADT7466_VCC_HILIM,
424 DBCOOL_ADT7466_VCC_LOLIM }, 4, 0, 0 },
425 { DBC_VOLT, { DBCOOL_ADT7466_AIN1,
426 DBCOOL_ADT7466_AIN1_HILIM,
427 DBCOOL_ADT7466_AIN1_LOLIM }, 9, 0, 6 },
428 { DBC_VOLT, { DBCOOL_ADT7466_AIN2,
429 DBCOOL_ADT7466_AIN2_HILIM,
430 DBCOOL_ADT7466_AIN2_LOLIM }, 10, 0, 6 },
431 { DBC_FAN, { DBCOOL_ADT7466_FANA_LSB,
432 DBCOOL_NO_REG,
433 DBCOOL_ADT7466_FANA_LOLIM_LSB }, 5, 0, 0 },
434 { DBC_FAN, { DBCOOL_ADT7466_FANB_LSB,
435 DBCOOL_NO_REG,
436 DBCOOL_ADT7466_FANB_LOLIM_LSB }, 6, 0, 0 },
437 { DBC_EOF, { 0, 0, 0 }, 0, 0, 0 }
438 };
439
440 struct dbcool_sensor ADM1027_sensor_table[] = {
441 { DBC_TEMP, { DBCOOL_LOCAL_TEMP,
442 DBCOOL_LOCAL_HIGHLIM,
443 DBCOOL_LOCAL_LOWLIM }, 0, 0, 0 },
444 { DBC_TEMP, { DBCOOL_REMOTE1_TEMP,
445 DBCOOL_REMOTE1_HIGHLIM,
446 DBCOOL_REMOTE1_LOWLIM }, 1, 0, 0 },
447 { DBC_TEMP, { DBCOOL_REMOTE2_TEMP,
448 DBCOOL_REMOTE2_HIGHLIM,
449 DBCOOL_REMOTE2_LOWLIM }, 2, 0, 0 },
450 { DBC_VOLT, { DBCOOL_VCCP,
451 DBCOOL_VCCP_HIGHLIM,
452 DBCOOL_VCCP_LOWLIM }, 3, 0, 1 },
453 { DBC_VOLT, { DBCOOL_VCC,
454 DBCOOL_VCC_HIGHLIM,
455 DBCOOL_VCC_LOWLIM }, 4, 0, 0 },
456 { DBC_VOLT, { DBCOOL_25VIN,
457 DBCOOL_25VIN_HIGHLIM,
458 DBCOOL_25VIN_LOWLIM }, 11, 0, 2 },
459 { DBC_VOLT, { DBCOOL_5VIN,
460 DBCOOL_5VIN_HIGHLIM,
461 DBCOOL_5VIN_LOWLIM }, 12, 0, 3 },
462 { DBC_VOLT, { DBCOOL_12VIN,
463 DBCOOL_12VIN_HIGHLIM,
464 DBCOOL_12VIN_LOWLIM }, 13, 0, 4 },
465 { DBC_FAN, { DBCOOL_FAN1_TACH_LSB,
466 DBCOOL_NO_REG,
467 DBCOOL_TACH1_MIN_LSB }, 5, 0, 0 },
468 { DBC_FAN, { DBCOOL_FAN2_TACH_LSB,
469 DBCOOL_NO_REG,
470 DBCOOL_TACH2_MIN_LSB }, 6, 0, 0 },
471 { DBC_FAN, { DBCOOL_FAN3_TACH_LSB,
472 DBCOOL_NO_REG,
473 DBCOOL_TACH3_MIN_LSB }, 7, 0, 0 },
474 { DBC_FAN, { DBCOOL_FAN4_TACH_LSB,
475 DBCOOL_NO_REG,
476 DBCOOL_TACH4_MIN_LSB }, 8, 0, 0 },
477 { DBC_VID, { DBCOOL_VID_REG,
478 DBCOOL_NO_REG,
479 DBCOOL_NO_REG }, 16, 0, 0 },
480 { DBC_CTL, { DBCOOL_LOCAL_TMIN,
481 DBCOOL_NO_REG,
482 DBCOOL_NO_REG }, 0, 5, 0 },
483 { DBC_CTL, { DBCOOL_LOCAL_TTHRESH,
484 DBCOOL_NO_REG,
485 DBCOOL_NO_REG }, 0, 6, 0 },
486 { DBC_CTL, { DBCOOL_R1_LCL_TMIN_HYST | 0x80,
487 DBCOOL_NO_REG,
488 DBCOOL_NO_REG }, 0, 7, 0 },
489 { DBC_CTL, { DBCOOL_REMOTE1_TMIN,
490 DBCOOL_NO_REG,
491 DBCOOL_NO_REG }, 1, 5, 0 },
492 { DBC_CTL, { DBCOOL_REMOTE1_TTHRESH,
493 DBCOOL_NO_REG,
494 DBCOOL_NO_REG }, 1, 6, 0 },
495 { DBC_CTL, { DBCOOL_R1_LCL_TMIN_HYST,
496 DBCOOL_NO_REG,
497 DBCOOL_NO_REG }, 1, 7, 0 },
498 { DBC_CTL, { DBCOOL_REMOTE2_TMIN,
499 DBCOOL_NO_REG,
500 DBCOOL_NO_REG }, 2, 5, 0 },
501 { DBC_CTL, { DBCOOL_REMOTE2_TTHRESH,
502 DBCOOL_NO_REG,
503 DBCOOL_NO_REG }, 2, 6, 0 },
504 { DBC_CTL, { DBCOOL_R2_TMIN_HYST,
505 DBCOOL_NO_REG,
506 DBCOOL_NO_REG }, 2, 7, 0 },
507 { DBC_EOF, { 0, 0, 0 }, 0, 0, 0 }
508 };
509
510 struct dbcool_sensor ADM1030_sensor_table[] = {
511 { DBC_TEMP, { DBCOOL_ADM1030_L_TEMP,
512 DBCOOL_ADM1030_L_HI_LIM,
513 DBCOOL_ADM1030_L_LO_LIM }, 0, 0, 0 },
514 { DBC_TEMP, { DBCOOL_ADM1030_R_TEMP,
515 DBCOOL_ADM1030_R_HI_LIM,
516 DBCOOL_ADM1030_R_LO_LIM }, 1, 0, 0 },
517 { DBC_FAN, { DBCOOL_ADM1030_FAN_TACH,
518 DBCOOL_NO_REG,
519 DBCOOL_ADM1030_FAN_LO_LIM }, 5, 0, 0 },
520 { DBC_CTL, { DBCOOL_ADM1030_L_TMIN,
521 DBCOOL_NO_REG,
522 DBCOOL_NO_REG }, 0, 8, 0 },
523 { DBC_CTL, { DBCOOL_ADM1030_L_TTHRESH,
524 DBCOOL_NO_REG,
525 DBCOOL_NO_REG }, 0, 9, 0 },
526 { DBC_CTL, { DBCOOL_ADM1030_L_TTHRESH,
527 DBCOOL_NO_REG,
528 DBCOOL_NO_REG }, 0, 6, 0 },
529 { DBC_CTL, { DBCOOL_ADM1030_R_TMIN,
530 DBCOOL_NO_REG,
531 DBCOOL_NO_REG }, 1, 8, 0 },
532 { DBC_CTL, { DBCOOL_ADM1030_L_TTHRESH,
533 DBCOOL_NO_REG,
534 DBCOOL_NO_REG }, 1, 9, 0 },
535 { DBC_CTL, { DBCOOL_ADM1030_R_TTHRESH,
536 DBCOOL_NO_REG,
537 DBCOOL_NO_REG }, 1, 6, 0 },
538 { DBC_EOF, {0, 0, 0 }, 0, 0, 0 }
539 };
540
541 struct dbcool_power_control ADM1030_power_table[] = {
542 { { DBCOOL_ADM1030_CFG1, DBCOOL_NO_REG, DBCOOL_NO_REG,
543 DBCOOL_ADM1030_FAN_SPEED_CFG },
544 "fan_control_1" },
545 { { 0, 0, 0, 0 }, NULL }
546 };
547
548 struct chip_id chip_table[] = {
549 { DBCOOL_COMPANYID, ADT7490_DEVICEID, ADT7490_REV_ID,
550 ADT7490_sensor_table, ADT7475_power_table,
551 DBCFLAG_TEMPOFFSET | DBCFLAG_HAS_MAXDUTY | DBCFLAG_HAS_PECI,
552 90000 * 60, "ADT7490" },
553 { DBCOOL_COMPANYID, ADT7476_DEVICEID, 0xff,
554 ADT7476_sensor_table, ADT7475_power_table,
555 DBCFLAG_TEMPOFFSET | DBCFLAG_HAS_MAXDUTY,
556 90000 * 60, "ADT7476" },
557 { DBCOOL_COMPANYID, ADT7475_DEVICEID, 0xff,
558 ADT7475_sensor_table, ADT7475_power_table,
559 DBCFLAG_TEMPOFFSET | DBCFLAG_HAS_MAXDUTY | DBCFLAG_HAS_SHDN,
560 90000 * 60, "ADT7475" },
561 { DBCOOL_COMPANYID, ADT7473_DEVICEID, ADT7473_REV_ID1,
562 ADT7475_sensor_table, ADT7475_power_table,
563 DBCFLAG_TEMPOFFSET | DBCFLAG_HAS_MAXDUTY | DBCFLAG_HAS_SHDN,
564 90000 * 60, "ADT7460/ADT7463" },
565 { DBCOOL_COMPANYID, ADT7473_DEVICEID, ADT7473_REV_ID2,
566 ADT7475_sensor_table, ADT7475_power_table,
567 DBCFLAG_TEMPOFFSET | DBCFLAG_HAS_MAXDUTY | DBCFLAG_HAS_SHDN,
568 90000 * 60, "ADT7463-1" },
569 { DBCOOL_COMPANYID, ADT7468_DEVICEID, 0xff,
570 ADT7476_sensor_table, ADT7475_power_table,
571 DBCFLAG_TEMPOFFSET | DBCFLAG_MULTI_VCC | DBCFLAG_HAS_MAXDUTY |
572 DBCFLAG_4BIT_VER | DBCFLAG_HAS_SHDN,
573 90000 * 60, "ADT7467/ADT7468" },
574 { DBCOOL_COMPANYID, ADT7466_DEVICEID, 0xff,
575 ADT7466_sensor_table, NULL,
576 DBCFLAG_ADT7466 | DBCFLAG_TEMPOFFSET | DBCFLAG_HAS_SHDN,
577 82000 * 60, "ADT7466" },
578 { DBCOOL_COMPANYID, ADT7463_DEVICEID, ADT7463_REV_ID1,
579 ADM1027_sensor_table, ADT7475_power_table,
580 DBCFLAG_MULTI_VCC | DBCFLAG_4BIT_VER | DBCFLAG_HAS_SHDN,
581 90000 * 60, "ADT7463" },
582 { DBCOOL_COMPANYID, ADT7463_DEVICEID, ADT7463_REV_ID2,
583 ADM1027_sensor_table, ADT7475_power_table,
584 DBCFLAG_MULTI_VCC | DBCFLAG_4BIT_VER | DBCFLAG_HAS_SHDN |
585 DBCFLAG_HAS_VID_SEL,
586 90000 * 60, "ADT7463" },
587 { DBCOOL_COMPANYID, ADM1027_DEVICEID, ADM1027_REV_ID,
588 ADM1027_sensor_table, ADT7475_power_table,
589 DBCFLAG_MULTI_VCC | DBCFLAG_4BIT_VER,
590 90000 * 60, "ADM1027" },
591 { DBCOOL_COMPANYID, ADM1030_DEVICEID, 0xff,
592 ADM1030_sensor_table, ADM1030_power_table,
593 DBCFLAG_ADM1030 | DBCFLAG_NO_READBYTE,
594 11250 * 60, "ADM1030" },
595 { 0, 0, 0, NULL, NULL, 0, 0, NULL }
596 };
597
598 static const char *behavior[] = {
599 "remote1", "local", "remote2", "full-speed",
600 "disabled", "local+remote2","all-temps", "manual"
601 };
602
603 static char dbcool_cur_behav[16];
604
605 CFATTACH_DECL_NEW(dbcool, sizeof(struct dbcool_softc),
606 dbcool_match, dbcool_attach, dbcool_detach, NULL);
607
608 int
609 dbcool_match(device_t parent, cfdata_t cf, void *aux)
610 {
611 struct i2c_attach_args *ia = aux;
612 struct dbcool_chipset dc;
613 dc.dc_tag = ia->ia_tag;
614 dc.dc_addr = ia->ia_addr;
615 dc.dc_chip = NULL;
616 dc.dc_readreg = dbcool_readreg;
617 dc.dc_writereg = dbcool_writereg;
618
619 /* no probing if we attach to iic, but verify chip id and address */
620 if ((ia->ia_addr & DBCOOL_ADDRMASK) != DBCOOL_ADDR)
621 return 0;
622 if (dbcool_chip_ident(&dc) >= 0)
623 return 1;
624
625 return 0;
626 }
627
628 void
629 dbcool_attach(device_t parent, device_t self, void *aux)
630 {
631 struct dbcool_softc *sc = device_private(self);
632 struct i2c_attach_args *args = aux;
633 uint8_t ver;
634
635 sc->sc_dc.dc_addr = args->ia_addr;
636 sc->sc_dc.dc_tag = args->ia_tag;
637 sc->sc_dc.dc_chip = NULL;
638 sc->sc_dc.dc_readreg = dbcool_readreg;
639 sc->sc_dc.dc_writereg = dbcool_writereg;
640 (void)dbcool_chip_ident(&sc->sc_dc);
641 sc->sc_dev = self;
642
643 aprint_naive("\n");
644 aprint_normal("\n");
645
646 ver = sc->sc_dc.dc_readreg(&sc->sc_dc, DBCOOL_REVISION_REG);
647 if (sc->sc_dc.dc_chip->flags & DBCFLAG_4BIT_VER)
648 aprint_normal_dev(self, "%s dBCool(tm) Controller "
649 "(rev 0x%02x, stepping 0x%02x)\n", sc->sc_dc.dc_chip->name,
650 ver >> 4, ver & 0x0f);
651 else
652 aprint_normal_dev(self, "%s dBCool(tm) Controller "
653 "(rev 0x%04x)\n", sc->sc_dc.dc_chip->name, ver);
654
655 dbcool_setup(self);
656
657 if (!pmf_device_register(self, dbcool_pmf_suspend, dbcool_pmf_resume))
658 aprint_error_dev(self, "couldn't establish power handler\n");
659 }
660
661 static int
662 dbcool_detach(device_t self, int flags)
663 {
664 struct dbcool_softc *sc = device_private(self);
665
666 sysmon_envsys_unregister(sc->sc_sme);
667 sc->sc_sme = NULL;
668 return 0;
669 }
670
671 /* On suspend, we save the state of the SHDN bit, then set it */
672 bool dbcool_pmf_suspend(device_t dev, const pmf_qual_t *qual)
673 {
674 struct dbcool_softc *sc = device_private(dev);
675 uint8_t reg, bit, cfg;
676
677 if ((sc->sc_dc.dc_chip->flags && DBCFLAG_HAS_SHDN) == 0)
678 return true;
679
680 if (sc->sc_dc.dc_chip->flags && DBCFLAG_ADT7466) {
681 reg = DBCOOL_ADT7466_CONFIG2;
682 bit = DBCOOL_ADT7466_CFG2_SHDN;
683 } else {
684 reg = DBCOOL_CONFIG2_REG;
685 bit = DBCOOL_CFG2_SHDN;
686 }
687 cfg = sc->sc_dc.dc_readreg(&sc->sc_dc, reg);
688 sc->sc_suspend = cfg & bit;
689 cfg |= bit;
690 sc->sc_dc.dc_writereg(&sc->sc_dc, reg, cfg);
691
692 return true;
693 }
694
695 /* On resume, we restore the previous state of the SHDN bit */
696 bool dbcool_pmf_resume(device_t dev, const pmf_qual_t *qual)
697 {
698 struct dbcool_softc *sc = device_private(dev);
699 uint8_t reg, bit, cfg;
700
701 if ((sc->sc_dc.dc_chip->flags && DBCFLAG_HAS_SHDN) == 0)
702 return true;
703
704 if (sc->sc_dc.dc_chip->flags && DBCFLAG_ADT7466) {
705 reg = DBCOOL_ADT7466_CONFIG2;
706 bit = DBCOOL_ADT7466_CFG2_SHDN;
707 } else {
708 reg = DBCOOL_CONFIG2_REG;
709 bit = DBCOOL_CFG2_SHDN;
710 }
711 cfg = sc->sc_dc.dc_readreg(&sc->sc_dc, reg);
712 cfg &= ~sc->sc_suspend;
713 sc->sc_dc.dc_writereg(&sc->sc_dc, reg, cfg);
714
715 return true;
716
717 }
718
719 uint8_t
720 dbcool_readreg(struct dbcool_chipset *dc, uint8_t reg)
721 {
722 uint8_t data = 0;
723
724 if (iic_acquire_bus(dc->dc_tag, 0) != 0)
725 return data;
726
727 if (dc->dc_chip == NULL || dc->dc_chip->flags & DBCFLAG_NO_READBYTE) {
728 /* ADM1027 doesn't support i2c read_byte protocol */
729 if (iic_smbus_send_byte(dc->dc_tag, dc->dc_addr, reg, 0) != 0)
730 goto bad;
731 (void)iic_smbus_receive_byte(dc->dc_tag, dc->dc_addr, &data, 0);
732 } else
733 (void)iic_smbus_read_byte(dc->dc_tag, dc->dc_addr, reg, &data,
734 0);
735
736 bad:
737 iic_release_bus(dc->dc_tag, 0);
738 return data;
739 }
740
741 void
742 dbcool_writereg(struct dbcool_chipset *dc, uint8_t reg, uint8_t val)
743 {
744 if (iic_acquire_bus(dc->dc_tag, 0) != 0)
745 return;
746
747 (void)iic_smbus_write_byte(dc->dc_tag, dc->dc_addr, reg, val, 0);
748
749 iic_release_bus(dc->dc_tag, 0);
750 }
751
752 static bool
753 dbcool_islocked(struct dbcool_softc *sc)
754 {
755 uint8_t cfg_reg;
756
757 if (sc->sc_dc.dc_chip->flags & DBCFLAG_ADM1030)
758 return 0;
759
760 if (sc->sc_dc.dc_chip->flags & DBCFLAG_ADT7466)
761 cfg_reg = DBCOOL_ADT7466_CONFIG1;
762 else
763 cfg_reg = DBCOOL_CONFIG1_REG;
764
765 if (sc->sc_dc.dc_readreg(&sc->sc_dc, cfg_reg) & DBCOOL_CFG1_LOCK)
766 return 1;
767 else
768 return 0;
769 }
770
771 static int
772 dbcool_read_temp(struct dbcool_softc *sc, uint8_t reg, bool extres)
773 {
774 uint8_t t1, t2, t3, val, ext = 0;
775 int temp;
776
777 if (sc->sc_dc.dc_chip->flags & DBCFLAG_ADT7466) {
778 /*
779 * ADT7466 temps are in strange location
780 */
781 ext = sc->sc_dc.dc_readreg(&sc->sc_dc, DBCOOL_ADT7466_CONFIG1);
782 val = sc->sc_dc.dc_readreg(&sc->sc_dc, reg);
783 if (extres)
784 ext = sc->sc_dc.dc_readreg(&sc->sc_dc, reg + 1);
785 } else if (sc->sc_dc.dc_chip->flags & DBCFLAG_ADM1030) {
786 /*
787 * ADM1030 temps are in their own special place, too
788 */
789 if (extres) {
790 ext = sc->sc_dc.dc_readreg(&sc->sc_dc, DBCOOL_ADM1030_TEMP_EXTRES);
791 if (reg == DBCOOL_ADM1030_L_TEMP)
792 ext >>= 6;
793 else
794 ext >>= 1;
795 ext &= 0x03;
796 }
797 val = sc->sc_dc.dc_readreg(&sc->sc_dc, reg);
798 } else if (extres) {
799 ext = sc->sc_dc.dc_readreg(&sc->sc_dc, DBCOOL_EXTRES2_REG);
800
801 /* Read all msb regs to unlatch them */
802 t1 = sc->sc_dc.dc_readreg(&sc->sc_dc, DBCOOL_12VIN);
803 t1 = sc->sc_dc.dc_readreg(&sc->sc_dc, DBCOOL_REMOTE1_TEMP);
804 t2 = sc->sc_dc.dc_readreg(&sc->sc_dc, DBCOOL_REMOTE2_TEMP);
805 t3 = sc->sc_dc.dc_readreg(&sc->sc_dc, DBCOOL_LOCAL_TEMP);
806 switch (reg) {
807 case DBCOOL_REMOTE1_TEMP:
808 val = t1;
809 ext >>= 2;
810 break;
811 case DBCOOL_LOCAL_TEMP:
812 val = t3;
813 ext >>= 4;
814 break;
815 case DBCOOL_REMOTE2_TEMP:
816 val = t2;
817 ext >>= 6;
818 break;
819 default:
820 val = 0;
821 break;
822 }
823 ext &= 0x03;
824 }
825 else
826 val = sc->sc_dc.dc_readreg(&sc->sc_dc, reg);
827
828 /* Check for invalid temp values */
829 if ((sc->sc_temp_offset == 0 && val == 0x80) ||
830 (sc->sc_temp_offset != 0 && val == 0))
831 return 0;
832
833 /* If using offset mode, adjust, else treat as signed */
834 if (sc->sc_temp_offset) {
835 temp = val;
836 temp -= sc->sc_temp_offset;
837 } else
838 temp = (int8_t)val;
839
840 /* Convert degC to uK and include extended precision bits */
841 temp *= 1000000;
842 temp += 250000 * (int)ext;
843 temp += 273150000U;
844
845 return temp;
846 }
847
848 static int
849 dbcool_read_rpm(struct dbcool_softc *sc, uint8_t reg)
850 {
851 int rpm;
852 uint8_t rpm_lo, rpm_hi;
853
854 rpm_lo = sc->sc_dc.dc_readreg(&sc->sc_dc, reg);
855 if (sc->sc_dc.dc_chip->flags & DBCFLAG_ADM1030)
856 rpm_hi = (rpm_lo == 0xff)?0xff:0x0;
857 else
858 rpm_hi = sc->sc_dc.dc_readreg(&sc->sc_dc, reg + 1);
859
860 rpm = (rpm_hi << 8) | rpm_lo;
861 if (rpm == 0xffff)
862 return 0; /* 0xffff indicates stalled/failed fan */
863
864 return (sc->sc_dc.dc_chip->rpm_dividend / rpm);
865 }
866
867 /* Provide chip's supply voltage, in microvolts */
868 static int
869 dbcool_supply_voltage(struct dbcool_softc *sc)
870 {
871 if (sc->sc_dc.dc_chip->flags & DBCFLAG_MULTI_VCC) {
872 if (sc->sc_dc.dc_readreg(&sc->sc_dc, DBCOOL_CONFIG1_REG) & DBCOOL_CFG1_Vcc)
873 return 5002500;
874 else
875 return 3300000;
876 } else if (sc->sc_dc.dc_chip->flags & DBCFLAG_ADT7466) {
877 if (sc->sc_dc.dc_readreg(&sc->sc_dc, DBCOOL_ADT7466_CONFIG1) &
878 DBCOOL_ADT7466_CFG1_Vcc)
879 return 5000000;
880 else
881 return 3300000;
882 } else
883 return 3300000;
884 }
885
886 /*
887 * Nominal voltages are calculated in microvolts
888 */
889 static int
890 dbcool_read_volt(struct dbcool_softc *sc, uint8_t reg, int nom_idx, bool extres)
891 {
892 uint8_t ext = 0, v1, v2, v3, v4, val;
893 int64_t ret;
894 int64_t nom;
895
896 nom = nominal_voltages[nom_idx];
897 if (nom < 0)
898 nom = sc->sc_supply_voltage;
899
900 /* ADT7466 voltages are in strange locations with only 8-bits */
901 if (sc->sc_dc.dc_chip->flags & DBCFLAG_ADT7466)
902 val = sc->sc_dc.dc_readreg(&sc->sc_dc, reg);
903 else
904 /*
905 * It's a "normal" dbCool chip - check for regs that
906 * share extended resolution bits since we have to
907 * read all the MSB registers to unlatch them.
908 */
909 if (!extres)
910 val = sc->sc_dc.dc_readreg(&sc->sc_dc, reg);
911 else if (reg == DBCOOL_12VIN) {
912 ext = sc->sc_dc.dc_readreg(&sc->sc_dc, DBCOOL_EXTRES2_REG) && 0x03;
913 val = sc->sc_dc.dc_readreg(&sc->sc_dc, reg);
914 (void)dbcool_read_temp(sc, DBCOOL_LOCAL_TEMP, true);
915 } else if (reg == DBCOOL_VTT || reg == DBCOOL_IMON) {
916 ext = sc->sc_dc.dc_readreg(&sc->sc_dc, DBCOOL_EXTRES_VTT_IMON);
917 v1 = sc->sc_dc.dc_readreg(&sc->sc_dc, DBCOOL_IMON);
918 v2 = sc->sc_dc.dc_readreg(&sc->sc_dc, DBCOOL_VTT);
919 if (reg == DBCOOL_IMON) {
920 val = v1;
921 ext >>= 6;
922 } else
923 val = v2;
924 ext >>= 4;
925 ext &= 0x0f;
926 } else {
927 ext = sc->sc_dc.dc_readreg(&sc->sc_dc, DBCOOL_EXTRES1_REG);
928 v1 = sc->sc_dc.dc_readreg(&sc->sc_dc, DBCOOL_25VIN);
929 v2 = sc->sc_dc.dc_readreg(&sc->sc_dc, DBCOOL_VCCP);
930 v3 = sc->sc_dc.dc_readreg(&sc->sc_dc, DBCOOL_VCC);
931 v4 = sc->sc_dc.dc_readreg(&sc->sc_dc, DBCOOL_5VIN);
932
933 switch (reg) {
934 case DBCOOL_25VIN:
935 val = v1;
936 break;
937 case DBCOOL_VCCP:
938 val = v2;
939 ext >>= 2;
940 break;
941 case DBCOOL_VCC:
942 val = v3;
943 ext >>= 4;
944 break;
945 case DBCOOL_5VIN:
946 val = v4;
947 ext >>= 6;
948 break;
949 default:
950 val = nom = 0;
951 }
952 ext &= 0x03;
953 }
954
955 /*
956 * Scale the nominal value by the 10-bit fraction
957 *
958 * Returned value is in microvolts.
959 */
960 ret = val;
961 ret <<= 2;
962 ret |= ext;
963 ret = (ret * nom) / 0x300;
964
965 return ret;
966 }
967
968 SYSCTL_SETUP(sysctl_dbcoolsetup, "sysctl dBCool subtree setup")
969 {
970 sysctl_createv(NULL, 0, NULL, NULL,
971 CTLFLAG_PERMANENT,
972 CTLTYPE_NODE, "hw", NULL,
973 NULL, 0, NULL, 0,
974 CTL_HW, CTL_EOL);
975 }
976
977 static int
978 sysctl_dbcool_temp(SYSCTLFN_ARGS)
979 {
980 struct sysctlnode node;
981 struct dbcool_softc *sc;
982 int reg, error;
983 uint8_t chipreg;
984 uint8_t newreg;
985
986 node = *rnode;
987 sc = (struct dbcool_softc *)node.sysctl_data;
988 chipreg = node.sysctl_num & 0xff;
989
990 if (sc->sc_temp_offset) {
991 reg = sc->sc_dc.dc_readreg(&sc->sc_dc, chipreg);
992 reg -= sc->sc_temp_offset;
993 } else
994 reg = (int8_t)sc->sc_dc.dc_readreg(&sc->sc_dc, chipreg);
995
996 node.sysctl_data = ®
997 error = sysctl_lookup(SYSCTLFN_CALL(&node));
998
999 if (error || newp == NULL)
1000 return error;
1001
1002 /* We were asked to update the value - sanity check before writing */
1003 if (*(int *)node.sysctl_data < -64 ||
1004 *(int *)node.sysctl_data > 127 + sc->sc_temp_offset)
1005 return EINVAL;
1006
1007 newreg = *(int *)node.sysctl_data;
1008 newreg += sc->sc_temp_offset;
1009 sc->sc_dc.dc_writereg(&sc->sc_dc, chipreg, newreg);
1010 return 0;
1011 }
1012
1013 static int
1014 sysctl_adm1030_temp(SYSCTLFN_ARGS)
1015 {
1016 struct sysctlnode node;
1017 struct dbcool_softc *sc;
1018 int reg, error;
1019 uint8_t chipreg, oldreg, newreg;
1020
1021 node = *rnode;
1022 sc = (struct dbcool_softc *)node.sysctl_data;
1023 chipreg = node.sysctl_num & 0xff;
1024
1025 oldreg = (int8_t)sc->sc_dc.dc_readreg(&sc->sc_dc, chipreg);
1026 reg = (oldreg >> 1) & ~0x03;
1027
1028 node.sysctl_data = ®
1029 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1030
1031 if (error || newp == NULL)
1032 return error;
1033
1034 /* We were asked to update the value - sanity check before writing */
1035 if (*(int *)node.sysctl_data < 0 || *(int *)node.sysctl_data > 127)
1036 return EINVAL;
1037
1038 newreg = *(int *)node.sysctl_data;
1039 newreg &= ~0x03;
1040 newreg <<= 1;
1041 newreg |= (oldreg & 0x07);
1042 sc->sc_dc.dc_writereg(&sc->sc_dc, chipreg, newreg);
1043 return 0;
1044 }
1045
1046 static int
1047 sysctl_adm1030_trange(SYSCTLFN_ARGS)
1048 {
1049 struct sysctlnode node;
1050 struct dbcool_softc *sc;
1051 int reg, error, newval;
1052 uint8_t chipreg, oldreg, newreg;
1053
1054 node = *rnode;
1055 sc = (struct dbcool_softc *)node.sysctl_data;
1056 chipreg = node.sysctl_num & 0xff;
1057
1058 oldreg = (int8_t)sc->sc_dc.dc_readreg(&sc->sc_dc, chipreg);
1059 reg = oldreg & 0x07;
1060
1061 node.sysctl_data = ®
1062 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1063
1064 if (error || newp == NULL)
1065 return error;
1066
1067 /* We were asked to update the value - sanity check before writing */
1068 newval = *(int *)node.sysctl_data;
1069
1070 if (newval == 5)
1071 newreg = 0;
1072 else if (newval == 10)
1073 newreg = 1;
1074 else if (newval == 20)
1075 newreg = 2;
1076 else if (newval == 40)
1077 newreg = 3;
1078 else if (newval == 80)
1079 newreg = 4;
1080 else
1081 return EINVAL;
1082
1083 newreg |= (oldreg & ~0x07);
1084 sc->sc_dc.dc_writereg(&sc->sc_dc, chipreg, newreg);
1085 return 0;
1086 }
1087
1088 static int
1089 sysctl_dbcool_duty(SYSCTLFN_ARGS)
1090 {
1091 struct sysctlnode node;
1092 struct dbcool_softc *sc;
1093 int reg, error;
1094 uint8_t chipreg, oldreg, newreg;
1095
1096 node = *rnode;
1097 sc = (struct dbcool_softc *)node.sysctl_data;
1098 chipreg = node.sysctl_num & 0xff;
1099
1100 oldreg = sc->sc_dc.dc_readreg(&sc->sc_dc, chipreg);
1101 reg = (uint32_t)oldreg;
1102 if (sc->sc_dc.dc_chip->flags & DBCFLAG_ADM1030)
1103 reg = ((reg & 0x0f) * 100) / 15;
1104 else
1105 reg = (reg * 100) / 255;
1106 node.sysctl_data = ®
1107 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1108
1109 if (error || newp == NULL)
1110 return error;
1111
1112 /* We were asked to update the value - sanity check before writing */
1113 if (*(int *)node.sysctl_data < 0 || *(int *)node.sysctl_data > 100)
1114 return EINVAL;
1115
1116 if (sc->sc_dc.dc_chip->flags & DBCFLAG_ADM1030) {
1117 newreg = *(uint8_t *)(node.sysctl_data) * 15 / 100;
1118 newreg |= oldreg & 0xf0;
1119 } else
1120 newreg = *(uint8_t *)(node.sysctl_data) * 255 / 100;
1121 sc->sc_dc.dc_writereg(&sc->sc_dc, chipreg, newreg);
1122 return 0;
1123 }
1124
1125 static int
1126 sysctl_dbcool_behavior(SYSCTLFN_ARGS)
1127 {
1128 struct sysctlnode node;
1129 struct dbcool_softc *sc;
1130 int i, reg, error;
1131 uint8_t chipreg, oldreg, newreg;
1132
1133 node = *rnode;
1134 sc = (struct dbcool_softc *)node.sysctl_data;
1135 chipreg = node.sysctl_num & 0xff;
1136
1137 oldreg = sc->sc_dc.dc_readreg(&sc->sc_dc, chipreg);
1138
1139 if (sc->sc_dc.dc_chip->flags & DBCFLAG_ADM1030) {
1140 if ((sc->sc_dc.dc_readreg(&sc->sc_dc, DBCOOL_ADM1030_CFG2) & 1) == 0)
1141 reg = 4;
1142 else if ((oldreg & 0x80) == 0)
1143 reg = 7;
1144 else if ((oldreg & 0x60) == 0)
1145 reg = 4;
1146 else
1147 reg = 6;
1148 } else
1149 reg = (oldreg >> 5) & 0x07;
1150
1151 strlcpy(dbcool_cur_behav, behavior[reg], sizeof(dbcool_cur_behav));
1152 node.sysctl_data = dbcool_cur_behav;
1153 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1154
1155 if (error || newp == NULL)
1156 return error;
1157
1158 /* We were asked to update the value - convert string to value */
1159 newreg = __arraycount(behavior);
1160 for (i = 0; i < __arraycount(behavior); i++)
1161 if (strcmp(node.sysctl_data, behavior[i]) == 0)
1162 break;
1163 if (i >= __arraycount(behavior))
1164 return EINVAL;
1165
1166 if (sc->sc_dc.dc_chip->flags & DBCFLAG_ADM1030) {
1167 /*
1168 * ADM1030 splits fan controller behavior across two
1169 * registers. We also do not support Auto-Filter mode
1170 * nor do we support Manual-RPM-feedback.
1171 */
1172 if (newreg == 4) {
1173 oldreg = sc->sc_dc.dc_readreg(&sc->sc_dc, DBCOOL_ADM1030_CFG2);
1174 oldreg &= ~0x01;
1175 sc->sc_dc.dc_writereg(&sc->sc_dc, DBCOOL_ADM1030_CFG2, oldreg);
1176 } else {
1177 if (newreg == 0)
1178 newreg = 4;
1179 else if (newreg == 6)
1180 newreg = 7;
1181 else if (newreg == 7)
1182 newreg = 0;
1183 else
1184 return EINVAL;
1185 newreg <<= 5;
1186 newreg |= (oldreg & 0x1f);
1187 sc->sc_dc.dc_writereg(&sc->sc_dc, chipreg, newreg);
1188 oldreg = sc->sc_dc.dc_readreg(&sc->sc_dc, DBCOOL_ADM1030_CFG2) | 1;
1189 sc->sc_dc.dc_writereg(&sc->sc_dc, DBCOOL_ADM1030_CFG2, oldreg);
1190 }
1191 } else {
1192 newreg = (sc->sc_dc.dc_readreg(&sc->sc_dc, chipreg) & 0x1f) | (i << 5);
1193 sc->sc_dc.dc_writereg(&sc->sc_dc, chipreg, newreg);
1194 }
1195 return 0;
1196 }
1197
1198 static int
1199 sysctl_dbcool_slope(SYSCTLFN_ARGS)
1200 {
1201 struct sysctlnode node;
1202 struct dbcool_softc *sc;
1203 int reg, error;
1204 uint8_t chipreg;
1205 uint8_t newreg;
1206
1207 node = *rnode;
1208 sc = (struct dbcool_softc *)node.sysctl_data;
1209 chipreg = node.sysctl_num & 0xff;
1210
1211 reg = (sc->sc_dc.dc_readreg(&sc->sc_dc, chipreg) >> 4) & 0x0f;
1212 node.sysctl_data = ®
1213 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1214
1215 if (error || newp == NULL)
1216 return error;
1217
1218 /* We were asked to update the value - sanity check before writing */
1219 if (*(int *)node.sysctl_data < 0 || *(int *)node.sysctl_data > 0x0f)
1220 return EINVAL;
1221
1222 newreg = (sc->sc_dc.dc_readreg(&sc->sc_dc, chipreg) & 0x0f) |
1223 (*(int *)node.sysctl_data << 4);
1224 sc->sc_dc.dc_writereg(&sc->sc_dc, chipreg, newreg);
1225 return 0;
1226 }
1227
1228 static int
1229 sysctl_dbcool_thyst(SYSCTLFN_ARGS)
1230 {
1231 struct sysctlnode node;
1232 struct dbcool_softc *sc;
1233 int reg, error;
1234 uint8_t chipreg;
1235 uint8_t newreg, newhyst;
1236
1237 node = *rnode;
1238 sc = (struct dbcool_softc *)node.sysctl_data;
1239 chipreg = node.sysctl_num & 0x7f;
1240
1241 /* retrieve 4-bit value */
1242 newreg = sc->sc_dc.dc_readreg(&sc->sc_dc, chipreg);
1243 if ((node.sysctl_num & 0x80) == 0)
1244 reg = newreg >> 4;
1245 else
1246 reg = newreg;
1247 reg = reg & 0x0f;
1248
1249 node.sysctl_data = ®
1250 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1251
1252 if (error || newp == NULL)
1253 return error;
1254
1255 /* We were asked to update the value - sanity check before writing */
1256 newhyst = *(int *)node.sysctl_data;
1257 if (newhyst > 0x0f)
1258 return EINVAL;
1259
1260 /* Insert new value into field and update register */
1261 if ((node.sysctl_num & 0x80) == 0) {
1262 newreg &= 0x0f;
1263 newreg |= (newhyst << 4);
1264 } else {
1265 newreg &= 0xf0;
1266 newreg |= newhyst;
1267 }
1268 sc->sc_dc.dc_writereg(&sc->sc_dc, chipreg, newreg);
1269 return 0;
1270 }
1271
1272 #ifdef DBCOOL_DEBUG
1273
1274 /*
1275 * These routines can be used for debugging. reg_select is used to
1276 * select any arbitrary register in the device. reg_access is used
1277 * to read (and optionally update) the selected register.
1278 *
1279 * No attempt is made to validate the data passed. If you use these
1280 * routines, you are assumed to know what you're doing!
1281 *
1282 * Caveat user
1283 */
1284 static int
1285 sysctl_dbcool_reg_select(SYSCTLFN_ARGS)
1286 {
1287 struct sysctlnode node;
1288 struct dbcool_softc *sc;
1289 int reg, error;
1290
1291 node = *rnode;
1292 sc = (struct dbcool_softc *)node.sysctl_data;
1293
1294 reg = sc->sc_user_reg;
1295 node.sysctl_data = ®
1296 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1297
1298 if (error || newp == NULL)
1299 return error;
1300
1301 sc->sc_user_reg = *(int *)node.sysctl_data;
1302 return 0;
1303 }
1304
1305 static int
1306 sysctl_dbcool_reg_access(SYSCTLFN_ARGS)
1307 {
1308 struct sysctlnode node;
1309 struct dbcool_softc *sc;
1310 int reg, error;
1311 uint8_t chipreg;
1312 uint8_t newreg;
1313
1314 node = *rnode;
1315 sc = (struct dbcool_softc *)node.sysctl_data;
1316 chipreg = sc->sc_user_reg;
1317
1318 reg = sc->sc_dc.dc_readreg(&sc->sc_dc, chipreg);
1319 node.sysctl_data = ®
1320 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1321
1322 if (error || newp == NULL)
1323 return error;
1324
1325 newreg = *(int *)node.sysctl_data;
1326 sc->sc_dc.dc_writereg(&sc->sc_dc, chipreg, newreg);
1327 return 0;
1328 }
1329 #endif /* DBCOOL_DEBUG */
1330
1331 /*
1332 * Encode an index number and register number for use as a sysctl_num
1333 * so we can select the correct device register later.
1334 */
1335 #define DBC_PWM_SYSCTL(seq, reg) ((seq << 8) | reg)
1336
1337 void
1338 dbcool_setup(device_t self)
1339 {
1340 struct dbcool_softc *sc = device_private(self);
1341 const struct sysctlnode *me = NULL;
1342 #ifdef DBCOOL_DEBUG
1343 struct sysctlnode *node = NULL;
1344 #endif
1345 uint8_t cfg_val, cfg_reg;
1346 int ret, error;
1347
1348 /*
1349 * Some chips are capable of reporting an extended temperature range
1350 * by default. On these models, config register 5 bit 0 can be set
1351 * to 1 for compatability with other chips that report 2s complement.
1352 */
1353 if (sc->sc_dc.dc_chip->flags & DBCFLAG_ADT7466) {
1354 if (sc->sc_dc.dc_readreg(&sc->sc_dc, DBCOOL_ADT7466_CONFIG1) & 0x80)
1355 sc->sc_temp_offset = 64;
1356 else
1357 sc->sc_temp_offset = 0;
1358 } else if (sc->sc_dc.dc_chip->flags & DBCFLAG_TEMPOFFSET) {
1359 if (sc->sc_dc.dc_readreg(&sc->sc_dc, DBCOOL_CONFIG5_REG) &
1360 DBCOOL_CFG5_TWOSCOMP)
1361 sc->sc_temp_offset = 0;
1362 else
1363 sc->sc_temp_offset = 64;
1364 } else
1365 sc->sc_temp_offset = 0;
1366
1367 /* Determine Vcc for this chip */
1368 sc->sc_supply_voltage = dbcool_supply_voltage(sc);
1369
1370 ret = sysctl_createv(NULL, 0, NULL, &me,
1371 CTLFLAG_READWRITE,
1372 CTLTYPE_NODE, device_xname(self), NULL,
1373 NULL, 0, NULL, 0,
1374 CTL_HW, CTL_CREATE, CTL_EOL);
1375 if (ret == 0)
1376 sc->sc_root_sysctl_num = me->sysctl_num;
1377 else
1378 sc->sc_root_sysctl_num = 0;
1379
1380 aprint_debug_dev(self,
1381 "Supply voltage %"PRId64".%06"PRId64"V, %s temp range\n",
1382 sc->sc_supply_voltage / 1000000,
1383 sc->sc_supply_voltage % 1000000,
1384 sc->sc_temp_offset ? "extended" : "normal");
1385
1386 /* Create the sensors for this device */
1387 sc->sc_sme = sysmon_envsys_create();
1388 if (dbcool_setup_sensors(sc))
1389 goto out;
1390
1391 if (sc->sc_root_sysctl_num != 0) {
1392 /* If supported, create sysctl tree for fan PWM controllers */
1393 if (sc->sc_dc.dc_chip->power != NULL)
1394 dbcool_setup_controllers(sc);
1395
1396 #ifdef DBCOOL_DEBUG
1397 ret = sysctl_createv(NULL, 0, NULL,
1398 (const struct sysctlnode **)&node,
1399 CTLFLAG_READWRITE, CTLTYPE_INT, "reg_select", NULL,
1400 sysctl_dbcool_reg_select,
1401 0, sc, sizeof(int),
1402 CTL_HW, me->sysctl_num, CTL_CREATE, CTL_EOL);
1403 if (node != NULL)
1404 node->sysctl_data = sc;
1405
1406 ret = sysctl_createv(NULL, 0, NULL,
1407 (const struct sysctlnode **)&node,
1408 CTLFLAG_READWRITE, CTLTYPE_INT, "reg_access", NULL,
1409 sysctl_dbcool_reg_access,
1410 0, sc, sizeof(int),
1411 CTL_HW, me->sysctl_num, CTL_CREATE, CTL_EOL);
1412 if (node != NULL)
1413 node->sysctl_data = sc;
1414 #endif /* DBCOOL_DEBUG */
1415 }
1416
1417 /*
1418 * Read and rewrite config register to activate device
1419 */
1420 if (sc->sc_dc.dc_chip->flags & DBCFLAG_ADM1030)
1421 cfg_reg = DBCOOL_ADM1030_CFG1;
1422 else if (sc->sc_dc.dc_chip->flags & DBCFLAG_ADT7466)
1423 cfg_reg = DBCOOL_ADT7466_CONFIG1;
1424 else
1425 cfg_reg = DBCOOL_CONFIG1_REG;
1426 cfg_val = sc->sc_dc.dc_readreg(&sc->sc_dc, DBCOOL_CONFIG1_REG);
1427 if ((cfg_val & DBCOOL_CFG1_START) == 0) {
1428 cfg_val |= DBCOOL_CFG1_START;
1429 sc->sc_dc.dc_writereg(&sc->sc_dc, cfg_reg, cfg_val);
1430 }
1431 if (dbcool_islocked(sc))
1432 aprint_normal_dev(self, "configuration locked\n");
1433
1434 sc->sc_sme->sme_name = device_xname(self);
1435 sc->sc_sme->sme_cookie = sc;
1436 sc->sc_sme->sme_refresh = dbcool_refresh;
1437 sc->sc_sme->sme_set_limits = dbcool_set_limits;
1438 sc->sc_sme->sme_get_limits = dbcool_get_limits;
1439
1440 if ((error = sysmon_envsys_register(sc->sc_sme)) != 0) {
1441 aprint_error_dev(self,
1442 "unable to register with sysmon (%d)\n", error);
1443 goto out;
1444 }
1445
1446 return;
1447
1448 out:
1449 sysmon_envsys_destroy(sc->sc_sme);
1450 }
1451
1452 static int
1453 dbcool_setup_sensors(struct dbcool_softc *sc)
1454 {
1455 int i;
1456 int error = 0;
1457 uint8_t vid_reg, vid_val;
1458 struct chip_id *chip = sc->sc_dc.dc_chip;
1459
1460 for (i=0; chip->table[i].type != DBC_EOF; i++) {
1461 if (i < DBCOOL_MAXSENSORS)
1462 sc->sc_sysctl_num[i] = -1;
1463 else if (chip->table[i].type != DBC_CTL) {
1464 aprint_normal_dev(sc->sc_dev, "chip table too big!\n");
1465 break;
1466 }
1467 switch (chip->table[i].type) {
1468 case DBC_TEMP:
1469 sc->sc_sensor[i].units = ENVSYS_STEMP;
1470 sc->sc_sensor[i].flags |= ENVSYS_FMONLIMITS;
1471 error = dbcool_attach_sensor(sc, i);
1472 break;
1473 case DBC_VOLT:
1474 /*
1475 * If 12V-In pin has been reconfigured as 6th bit
1476 * of VID code, don't create a 12V-In sensor
1477 */
1478 if ((chip->flags & DBCFLAG_HAS_VID_SEL) &&
1479 (chip->table[i].reg.val_reg == DBCOOL_12VIN) &&
1480 (sc->sc_dc.dc_readreg(&sc->sc_dc, DBCOOL_VID_REG) &
1481 0x80))
1482 break;
1483
1484 sc->sc_sensor[i].units = ENVSYS_SVOLTS_DC;
1485 sc->sc_sensor[i].flags |= ENVSYS_FMONLIMITS;
1486 error = dbcool_attach_sensor(sc, i);
1487 break;
1488 case DBC_FAN:
1489 sc->sc_sensor[i].units = ENVSYS_SFANRPM;
1490 sc->sc_sensor[i].flags |= ENVSYS_FMONLIMITS;
1491 error = dbcool_attach_sensor(sc, i);
1492 break;
1493 case DBC_VID:
1494 sc->sc_sensor[i].units = ENVSYS_INTEGER;
1495 sc->sc_sensor[i].flags |= ENVSYS_FMONNOTSUPP;
1496
1497 /* retrieve 5- or 6-bit value */
1498 vid_reg = chip->table[i].reg.val_reg;
1499 vid_val = sc->sc_dc.dc_readreg(&sc->sc_dc, vid_reg);
1500 if (chip->flags & DBCFLAG_HAS_VID_SEL)
1501 vid_val &= 0x3f;
1502 else
1503 vid_val &= 0x1f;
1504 sc->sc_sensor[i].value_cur = vid_val;
1505
1506 error = dbcool_attach_sensor(sc, i);
1507 break;
1508 case DBC_CTL:
1509 error = dbcool_attach_temp_control(sc, i, chip);
1510 if (error) {
1511 aprint_error_dev(sc->sc_dev,
1512 "attach index %d failed %d\n",
1513 i, error);
1514 error = 0;
1515 }
1516 break;
1517 default:
1518 aprint_error_dev(sc->sc_dev,
1519 "sensor_table index %d has bad type %d\n",
1520 i, chip->table[i].type);
1521 break;
1522 }
1523 if (error)
1524 break;
1525 }
1526 return error;
1527 }
1528
1529 static int
1530 dbcool_attach_sensor(struct dbcool_softc *sc, int idx)
1531 {
1532 int name_index;
1533 int error = 0;
1534
1535 name_index = sc->sc_dc.dc_chip->table[idx].name_index;
1536 strlcpy(sc->sc_sensor[idx].desc, dbc_sensor_names[name_index],
1537 sizeof(sc->sc_sensor[idx].desc));
1538 sc->sc_regs[idx] = &sc->sc_dc.dc_chip->table[idx].reg;
1539 sc->sc_nom_volt[idx] = sc->sc_dc.dc_chip->table[idx].nom_volt_index;
1540
1541 error = sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor[idx]);
1542 return error;
1543 }
1544
1545 static int
1546 dbcool_attach_temp_control(struct dbcool_softc *sc, int idx,
1547 struct chip_id *chip)
1548 {
1549 const struct sysctlnode *me2 = NULL;
1550 struct sysctlnode *node = NULL;
1551 int j, ret, sysctl_index, rw_flag;
1552 uint8_t sysctl_reg;
1553 char name[SYSCTL_NAMELEN];
1554
1555 /* Search for the corresponding temp sensor */
1556 for (j = 0; j < idx; j++) {
1557 if (j >= DBCOOL_MAXSENSORS || chip->table[j].type != DBC_TEMP)
1558 continue;
1559 if (chip->table[j].name_index == chip->table[idx].name_index)
1560 break;
1561 }
1562 if (j >= idx) /* Temp sensor not found */
1563 return ENOENT;
1564
1565 /* create sysctl node for the sensor if not one already there */
1566 if (sc->sc_sysctl_num[j] == -1) {
1567 ret = sysctl_createv(NULL, 0, NULL, &me2, CTLFLAG_READWRITE,
1568 CTLTYPE_NODE, sc->sc_sensor[j].desc, NULL,
1569 NULL, 0, NULL, 0,
1570 CTL_HW, sc->sc_root_sysctl_num, CTL_CREATE,
1571 CTL_EOL);
1572 if (me2 != NULL)
1573 sc->sc_sysctl_num[j] = me2->sysctl_num;
1574 else
1575 return ret;
1576 }
1577 /* add sysctl leaf node for this control variable */
1578 sysctl_index = chip->table[idx].sysctl_index;
1579 sysctl_reg = chip->table[idx].reg.val_reg;
1580 strlcpy(name, dbc_sysctl_table[sysctl_index].name, sizeof(name));
1581 if (dbc_sysctl_table[sysctl_index].lockable && dbcool_islocked(sc))
1582 rw_flag = CTLFLAG_READONLY | CTLFLAG_OWNDESC;
1583 else
1584 rw_flag = CTLFLAG_READWRITE | CTLFLAG_OWNDESC;
1585 ret = sysctl_createv(NULL, 0, NULL,
1586 (const struct sysctlnode **)&node, rw_flag,
1587 CTLTYPE_INT, name,
1588 dbc_sysctl_table[sysctl_index].desc,
1589 dbc_sysctl_table[sysctl_index].helper,
1590 0, sc, sizeof(int),
1591 CTL_HW, sc->sc_root_sysctl_num,
1592 sc->sc_sysctl_num[j],
1593 DBC_PWM_SYSCTL(idx, sysctl_reg), CTL_EOL);
1594 if (node != NULL)
1595 node->sysctl_data = sc;
1596
1597 return ret;
1598 }
1599
1600 static void
1601 dbcool_setup_controllers(struct dbcool_softc *sc)
1602 {
1603 int i, j, ret, rw_flag;
1604 uint8_t sysctl_reg;
1605 struct chip_id *chip = sc->sc_dc.dc_chip;
1606 const struct sysctlnode *me2 = NULL;
1607 struct sysctlnode *node = NULL;
1608 char name[SYSCTL_NAMELEN];
1609
1610 for (i = 0; chip->power[i].desc != NULL; i++) {
1611 snprintf(name, sizeof(name), "fan_ctl_%d", i);
1612 ret = sysctl_createv(NULL, 0, NULL, &me2,
1613 CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
1614 CTLTYPE_NODE, name, NULL,
1615 NULL, 0, NULL, 0,
1616 CTL_HW, sc->sc_root_sysctl_num, CTL_CREATE, CTL_EOL);
1617
1618 for (j = DBC_PWM_BEHAVIOR; j < DBC_PWM_LAST_PARAM; j++) {
1619 if (j == DBC_PWM_MAX_DUTY &&
1620 (chip->flags & DBCFLAG_HAS_MAXDUTY) == 0)
1621 continue;
1622 sysctl_reg = chip->power[i].power_regs[j];
1623 if (sysctl_reg == DBCOOL_NO_REG)
1624 continue;
1625 strlcpy(name, dbc_sysctl_table[j].name, sizeof(name));
1626 if (dbc_sysctl_table[j].lockable && dbcool_islocked(sc))
1627 rw_flag = CTLFLAG_READONLY | CTLFLAG_OWNDESC;
1628 else
1629 rw_flag = CTLFLAG_READWRITE | CTLFLAG_OWNDESC;
1630 ret = sysctl_createv(NULL, 0, NULL,
1631 (const struct sysctlnode **)&node, rw_flag,
1632 (j == DBC_PWM_BEHAVIOR)?
1633 CTLTYPE_STRING:CTLTYPE_INT,
1634 name,
1635 dbc_sysctl_table[j].desc,
1636 dbc_sysctl_table[j].helper,
1637 0, sc,
1638 ( j == DBC_PWM_BEHAVIOR)?
1639 sizeof(dbcool_cur_behav): sizeof(int),
1640 CTL_HW, sc->sc_root_sysctl_num, me2->sysctl_num,
1641 DBC_PWM_SYSCTL(j, sysctl_reg), CTL_EOL);
1642 if (node != NULL)
1643 node->sysctl_data = sc;
1644 }
1645 }
1646 }
1647
1648 static void
1649 dbcool_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
1650 {
1651 struct dbcool_softc *sc=sme->sme_cookie;
1652 int i, nom_volt_idx, cur;
1653 struct reg_list *reg;
1654
1655 i = edata->sensor;
1656 reg = sc->sc_regs[i];
1657
1658 edata->state = ENVSYS_SVALID;
1659 switch (edata->units)
1660 {
1661 case ENVSYS_STEMP:
1662 cur = dbcool_read_temp(sc, reg->val_reg, true);
1663 break;
1664 case ENVSYS_SVOLTS_DC:
1665 nom_volt_idx = sc->sc_nom_volt[i];
1666 cur = dbcool_read_volt(sc, reg->val_reg, nom_volt_idx,
1667 true);
1668 break;
1669 case ENVSYS_SFANRPM:
1670 cur = dbcool_read_rpm(sc, reg->val_reg);
1671 break;
1672 case ENVSYS_INTEGER:
1673 return;
1674 default:
1675 edata->state = ENVSYS_SINVALID;
1676 return;
1677 }
1678
1679 if (cur == 0 && (edata->units != ENVSYS_SFANRPM))
1680 edata->state = ENVSYS_SINVALID;
1681
1682 /*
1683 * If fan is "stalled" but has no low limit, treat
1684 * it as though the fan is not installed.
1685 */
1686 else if (edata->units == ENVSYS_SFANRPM && cur == 0 &&
1687 !(edata->upropset & (PROP_CRITMIN | PROP_WARNMIN)))
1688 edata->state = ENVSYS_SINVALID;
1689
1690 edata->value_cur = cur;
1691 }
1692
1693 int
1694 dbcool_chip_ident(struct dbcool_chipset *dc)
1695 {
1696 /* verify this is a supported dbCool chip */
1697 uint8_t c_id, d_id, r_id;
1698 int i;
1699
1700 c_id = dc->dc_readreg(dc, DBCOOL_COMPANYID_REG);
1701 d_id = dc->dc_readreg(dc, DBCOOL_DEVICEID_REG);
1702 r_id = dc->dc_readreg(dc, DBCOOL_REVISION_REG);
1703
1704 for (i = 0; chip_table[i].company != 0; i++)
1705 if ((c_id == chip_table[i].company) &&
1706 (d_id == chip_table[i].device ||
1707 chip_table[i].device == 0xff) &&
1708 (r_id == chip_table[i].rev ||
1709 chip_table[i].rev == 0xff)) {
1710 dc->dc_chip = &chip_table[i];
1711 return i;
1712 }
1713
1714 aprint_verbose("dbcool_chip_ident: addr 0x%02x c_id 0x%02x d_id 0x%02x"
1715 " r_id 0x%02x: No match.\n", dc->dc_addr, c_id, d_id,
1716 r_id);
1717
1718 return -1;
1719 }
1720
1721 /*
1722 * Retrieve sensor limits from the chip registers
1723 */
1724 static void
1725 dbcool_get_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
1726 sysmon_envsys_lim_t *limits, uint32_t *props)
1727 {
1728 int index = edata->sensor;
1729 struct dbcool_softc *sc = sme->sme_cookie;
1730
1731 *props &= ~(PROP_CRITMIN | PROP_CRITMAX);
1732 switch (edata->units) {
1733 case ENVSYS_STEMP:
1734 dbcool_get_temp_limits(sc, index, limits, props);
1735 break;
1736 case ENVSYS_SVOLTS_DC:
1737 dbcool_get_volt_limits(sc, index, limits, props);
1738 break;
1739 case ENVSYS_SFANRPM:
1740 dbcool_get_fan_limits(sc, index, limits, props);
1741
1742 /* FALLTHROUGH */
1743 default:
1744 break;
1745 }
1746 *props &= ~PROP_DRIVER_LIMITS;
1747
1748 /* If both limits provided, make sure they're sane */
1749 if ((*props & PROP_CRITMIN) &&
1750 (*props & PROP_CRITMAX) &&
1751 (limits->sel_critmin >= limits->sel_critmax))
1752 *props &= ~(PROP_CRITMIN | PROP_CRITMAX);
1753 }
1754
1755 static void
1756 dbcool_get_temp_limits(struct dbcool_softc *sc, int idx,
1757 sysmon_envsys_lim_t *lims, uint32_t *props)
1758 {
1759 struct reg_list *reg = sc->sc_regs[idx];
1760 uint8_t lo_lim, hi_lim;
1761
1762 lo_lim = sc->sc_dc.dc_readreg(&sc->sc_dc, reg->lo_lim_reg);
1763 hi_lim = sc->sc_dc.dc_readreg(&sc->sc_dc, reg->hi_lim_reg);
1764
1765 if (sc->sc_temp_offset) {
1766 if (lo_lim > 0x01) {
1767 lims->sel_critmin = lo_lim - sc->sc_temp_offset;
1768 *props |= PROP_CRITMIN;
1769 }
1770 if (hi_lim != 0xff) {
1771 lims->sel_critmax = hi_lim - sc->sc_temp_offset;
1772 *props |= PROP_CRITMAX;
1773 }
1774 } else {
1775 if (lo_lim != 0x80 && lo_lim != 0x81) {
1776 lims->sel_critmin = (int8_t)lo_lim;
1777 *props |= PROP_CRITMIN;
1778 }
1779
1780 if (hi_lim != 0x7f) {
1781 lims->sel_critmax = (int8_t)hi_lim;
1782 *props |= PROP_CRITMAX;
1783 }
1784 }
1785
1786 /* Convert temp limits to microKelvin */
1787 lims->sel_critmin *= 1000000;
1788 lims->sel_critmin += 273150000;
1789 lims->sel_critmax *= 1000000;
1790 lims->sel_critmax += 273150000;
1791 }
1792
1793 static void
1794 dbcool_get_volt_limits(struct dbcool_softc *sc, int idx,
1795 sysmon_envsys_lim_t *lims, uint32_t *props)
1796 {
1797 struct reg_list *reg = sc->sc_regs[idx];
1798 int64_t limit;
1799 int nom;
1800
1801 nom = nominal_voltages[sc->sc_dc.dc_chip->table[idx].nom_volt_index];
1802 if (nom < 0)
1803 nom = dbcool_supply_voltage(sc);
1804 nom *= 1000000; /* scale for microvolts */
1805
1806 limit = sc->sc_dc.dc_readreg(&sc->sc_dc, reg->lo_lim_reg);
1807 if (limit != 0x00 && limit != 0xff) {
1808 limit *= nom;
1809 limit /= 0xc0;
1810 lims->sel_critmin = limit;
1811 *props |= PROP_CRITMIN;
1812 }
1813 limit = sc->sc_dc.dc_readreg(&sc->sc_dc, reg->hi_lim_reg);
1814 if (limit != 0x00 && limit != 0xff) {
1815 limit *= nom;
1816 limit /= 0xc0;
1817 lims->sel_critmax = limit;
1818 *props |= PROP_CRITMAX;
1819 }
1820 }
1821
1822 static void
1823 dbcool_get_fan_limits(struct dbcool_softc *sc, int idx,
1824 sysmon_envsys_lim_t *lims, uint32_t *props)
1825 {
1826 struct reg_list *reg = sc->sc_regs[idx];
1827 int32_t limit;
1828
1829 limit = dbcool_read_rpm(sc, reg->lo_lim_reg);
1830 if (limit) {
1831 lims->sel_critmin = limit;
1832 *props |= PROP_CRITMIN;
1833 }
1834 }
1835
1836 /*
1837 * Update sensor limits in the chip registers
1838 */
1839 static void
1840 dbcool_set_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
1841 sysmon_envsys_lim_t *limits, uint32_t *props)
1842 {
1843 int index = edata->sensor;
1844 struct dbcool_softc *sc = sme->sme_cookie;
1845
1846 switch (edata->units) {
1847 case ENVSYS_STEMP:
1848 dbcool_set_temp_limits(sc, index, limits, props);
1849 break;
1850 case ENVSYS_SVOLTS_DC:
1851 dbcool_set_volt_limits(sc, index, limits, props);
1852 break;
1853 case ENVSYS_SFANRPM:
1854 dbcool_set_fan_limits(sc, index, limits, props);
1855
1856 /* FALLTHROUGH */
1857 default:
1858 break;
1859 }
1860 *props &= ~PROP_DRIVER_LIMITS;
1861 }
1862
1863 static void
1864 dbcool_set_temp_limits(struct dbcool_softc *sc, int idx,
1865 sysmon_envsys_lim_t *lims, uint32_t *props)
1866 {
1867 struct reg_list *reg = sc->sc_regs[idx];
1868 int32_t limit;
1869
1870 if (*props & PROP_CRITMIN) {
1871 limit = lims->sel_critmin - 273150000;
1872 limit /= 1000000;
1873 if (sc->sc_temp_offset) {
1874 limit += sc->sc_temp_offset;
1875 if (limit < 0)
1876 limit = 0;
1877 else if (limit > 255)
1878 limit = 255;
1879 } else {
1880 if (limit < -127)
1881 limit = -127;
1882 else if (limit > 127)
1883 limit = 127;
1884 }
1885 } else
1886 if (sc->sc_temp_offset)
1887 limit = 0x00;
1888 else
1889 limit = 0x80;
1890 sc->sc_dc.dc_writereg(&sc->sc_dc, reg->lo_lim_reg, (uint8_t)limit);
1891
1892 if (*props & PROP_CRITMAX) {
1893 limit = lims->sel_critmax - 273150000;
1894 limit /= 1000000;
1895 if (sc->sc_temp_offset) {
1896 limit += sc->sc_temp_offset;
1897 if (limit < 0)
1898 limit = 0;
1899 else if (limit > 255)
1900 limit = 255;
1901 } else {
1902 if (limit < -127)
1903 limit = -127;
1904 else if (limit > 127)
1905 limit = 127;
1906 }
1907 } else
1908 if (sc->sc_temp_offset)
1909 limit = 0xff;
1910 else
1911 limit = 0x7f;
1912 sc->sc_dc.dc_writereg(&sc->sc_dc, reg->hi_lim_reg, (uint8_t)limit);
1913 }
1914
1915 static void
1916 dbcool_set_volt_limits(struct dbcool_softc *sc, int idx,
1917 sysmon_envsys_lim_t *lims, uint32_t *props)
1918 {
1919 struct reg_list *reg = sc->sc_regs[idx];
1920 int64_t limit;
1921 int nom;
1922
1923 nom = nominal_voltages[sc->sc_dc.dc_chip->table[idx].nom_volt_index];
1924 if (nom < 0)
1925 nom = dbcool_supply_voltage(sc);
1926 nom *= 1000000; /* scale for microvolts */
1927
1928 if (*props & PROP_CRITMIN) {
1929 limit = lims->sel_critmin;
1930 limit *= 0xc0;
1931 limit /= nom;
1932 if (limit > 0xff)
1933 limit = 0xff;
1934 else if (limit < 0)
1935 limit = 0;
1936 } else
1937 limit = 0;
1938 sc->sc_dc.dc_writereg(&sc->sc_dc, reg->lo_lim_reg, limit);
1939
1940 if (*props & PROP_CRITMAX) {
1941 limit = lims->sel_critmax;
1942 limit *= 0xc0;
1943 limit /= nom;
1944 if (limit > 0xff)
1945 limit = 0xff;
1946 else if (limit < 0)
1947 limit = 0;
1948 } else
1949 limit = 0xff;
1950 sc->sc_dc.dc_writereg(&sc->sc_dc, reg->hi_lim_reg, limit);
1951 }
1952
1953 static void
1954 dbcool_set_fan_limits(struct dbcool_softc *sc, int idx,
1955 sysmon_envsys_lim_t *lims, uint32_t *props)
1956 {
1957 struct reg_list *reg = sc->sc_regs[idx];
1958 int32_t limit, dividend;
1959
1960 if (*props & PROP_CRITMIN) {
1961 limit = lims->sel_critmin;
1962 if (limit == 0)
1963 limit = 0xffff;
1964 else {
1965 if (sc->sc_dc.dc_chip->flags & DBCFLAG_ADM1030)
1966 dividend = 11250 * 60;
1967 else
1968 dividend = 90000 * 60;
1969 limit = limit / dividend;
1970 if (limit > 0xffff)
1971 limit = 0xffff;
1972 }
1973 } else
1974 limit = 0xffff;
1975 sc->sc_dc.dc_writereg(&sc->sc_dc, reg->lo_lim_reg, limit & 0xff);
1976 limit >>= 8;
1977 sc->sc_dc.dc_writereg(&sc->sc_dc, reg->lo_lim_reg + 1, limit & 0xff);
1978 }
1979