Line data Source code
1 : /* $OpenBSD: adm1024.c,v 1.14 2007/06/24 05:34:35 dlg Exp $ */
2 :
3 : /*
4 : * Copyright (c) 2005 Theo de Raadt
5 : *
6 : * Permission to use, copy, modify, and distribute this software for any
7 : * purpose with or without fee is hereby granted, provided that the above
8 : * copyright notice and this permission notice appear in all copies.
9 : *
10 : * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 : * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 : * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 : * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 : * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 : * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 : * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 : */
18 :
19 : #include <sys/param.h>
20 : #include <sys/systm.h>
21 : #include <sys/device.h>
22 : #include <sys/sensors.h>
23 :
24 : #include <dev/i2c/i2cvar.h>
25 :
26 : /* ADM 1024 registers */
27 : #define ADM1024_V2_5 0x20
28 : #define ADM1024_Vccp 0x21
29 : #define ADM1024_Vcc 0x22
30 : #define ADM1024_V5 0x23
31 : #define ADM1024_V12 0x24
32 : #define ADM1024_Vccp2 0x25
33 : #define ADM1024_EXT_TEMP 0x26
34 : #define ADM1024_INT_TEMP 0x27
35 : #define ADM1024_FAN1 0x28
36 : #define ADM1024_FAN2 0x29
37 : #define ADM1024_STATUS2 0x42
38 : #define ADM1024_FANC 0x47
39 : #define ADM1024_STATUS2_EXT 0x40
40 : #define ADM1024_COMPANY 0x3e /* contains 0x41 */
41 : #define ADM1024_STEPPING 0x3f /* contains 0x2? */
42 : #define ADM1024_CONFIG1 0x40
43 : #define ADM1024_CONFIG1_START 0x01
44 : #define ADM1024_CONFIG1_INTCLR 0x08
45 :
46 : /* Sensors */
47 : #define ADMLC_INT 0
48 : #define ADMLC_EXT 1
49 : #define ADMLC_V2_5 2
50 : #define ADMLC_Vccp 3
51 : #define ADMLC_Vcc 4
52 : #define ADMLC_V5 5
53 : #define ADMLC_V12 6
54 : #define ADMLC_Vccp2 7
55 : #define ADMLC_FAN1 8
56 : #define ADMLC_FAN2 9
57 : #define ADMLC_NUM_SENSORS 10
58 :
59 : struct admlc_softc {
60 : struct device sc_dev;
61 : i2c_tag_t sc_tag;
62 : i2c_addr_t sc_addr;
63 :
64 : struct ksensor sc_sensor[ADMLC_NUM_SENSORS];
65 : struct ksensordev sc_sensordev;
66 : int sc_fan1mul;
67 : int sc_fan2mul;
68 : };
69 :
70 : int admlc_match(struct device *, void *, void *);
71 : void admlc_attach(struct device *, struct device *, void *);
72 : void admlc_refresh(void *);
73 :
74 : struct cfattach admlc_ca = {
75 : sizeof(struct admlc_softc), admlc_match, admlc_attach
76 : };
77 :
78 : struct cfdriver admlc_cd = {
79 : NULL, "admlc", DV_DULL
80 : };
81 :
82 : int
83 0 : admlc_match(struct device *parent, void *match, void *aux)
84 : {
85 0 : struct i2c_attach_args *ia = aux;
86 :
87 0 : if (strcmp(ia->ia_name, "adm1024") == 0)
88 0 : return (1);
89 0 : return (0);
90 0 : }
91 :
92 : void
93 0 : admlc_attach(struct device *parent, struct device *self, void *aux)
94 : {
95 0 : struct admlc_softc *sc = (struct admlc_softc *)self;
96 0 : struct i2c_attach_args *ia = aux;
97 0 : u_int8_t cmd, data, data2;
98 : int i;
99 :
100 0 : sc->sc_tag = ia->ia_tag;
101 0 : sc->sc_addr = ia->ia_addr;
102 :
103 0 : iic_acquire_bus(sc->sc_tag, 0);
104 0 : cmd = ADM1024_CONFIG1;
105 0 : if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
106 0 : sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0)) {
107 0 : iic_release_bus(sc->sc_tag, 0);
108 0 : printf(": cannot get control register\n");
109 0 : return;
110 : }
111 0 : data2 = data | ADM1024_CONFIG1_START;
112 0 : data2 = data2 & ~ADM1024_CONFIG1_INTCLR;
113 0 : if (data != data2) {
114 0 : if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
115 0 : sc->sc_addr, &cmd, sizeof cmd, &data2, sizeof data2, 0)) {
116 0 : iic_release_bus(sc->sc_tag, 0);
117 0 : printf(": cannot set control register\n");
118 0 : return;
119 : }
120 : }
121 :
122 0 : cmd = ADM1024_FANC;
123 0 : if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
124 0 : sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0)) {
125 0 : printf(", unable to read fan setting\n");
126 0 : return;
127 : }
128 0 : sc->sc_fan1mul = (1 << (data >> 4) & 0x3);
129 0 : sc->sc_fan2mul = (1 << (data >> 6) & 0x3);
130 :
131 0 : iic_release_bus(sc->sc_tag, 0);
132 :
133 : /* Initialize sensor data. */
134 0 : strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname,
135 : sizeof(sc->sc_sensordev.xname));
136 :
137 0 : sc->sc_sensor[ADMLC_INT].type = SENSOR_TEMP;
138 0 : strlcpy(sc->sc_sensor[ADMLC_INT].desc, "Internal",
139 : sizeof(sc->sc_sensor[ADMLC_INT].desc));
140 :
141 0 : sc->sc_sensor[ADMLC_EXT].type = SENSOR_TEMP;
142 0 : strlcpy(sc->sc_sensor[ADMLC_EXT].desc, "External",
143 : sizeof(sc->sc_sensor[ADMLC_EXT].desc));
144 :
145 0 : sc->sc_sensor[ADMLC_V2_5].type = SENSOR_VOLTS_DC;
146 0 : strlcpy(sc->sc_sensor[ADMLC_V2_5].desc, "2.5 V",
147 : sizeof(sc->sc_sensor[ADMLC_V2_5].desc));
148 :
149 0 : sc->sc_sensor[ADMLC_Vccp].type = SENSOR_VOLTS_DC;
150 0 : strlcpy(sc->sc_sensor[ADMLC_Vccp].desc, "Vccp",
151 : sizeof(sc->sc_sensor[ADMLC_Vccp].desc));
152 :
153 0 : sc->sc_sensor[ADMLC_Vcc].type = SENSOR_VOLTS_DC;
154 0 : strlcpy(sc->sc_sensor[ADMLC_Vcc].desc, "Vcc",
155 : sizeof(sc->sc_sensor[ADMLC_Vcc].desc));
156 :
157 0 : sc->sc_sensor[ADMLC_V5].type = SENSOR_VOLTS_DC;
158 0 : strlcpy(sc->sc_sensor[ADMLC_V5].desc, "5 V",
159 : sizeof(sc->sc_sensor[ADMLC_V5].desc));
160 :
161 0 : sc->sc_sensor[ADMLC_V12].type = SENSOR_VOLTS_DC;
162 0 : strlcpy(sc->sc_sensor[ADMLC_V12].desc, "12 V",
163 : sizeof(sc->sc_sensor[ADMLC_V12].desc));
164 :
165 0 : sc->sc_sensor[ADMLC_Vccp2].type = SENSOR_VOLTS_DC;
166 0 : strlcpy(sc->sc_sensor[ADMLC_Vccp2].desc, "Vccp2",
167 : sizeof(sc->sc_sensor[ADMLC_Vccp2].desc));
168 :
169 0 : sc->sc_sensor[ADMLC_FAN1].type = SENSOR_FANRPM;
170 :
171 0 : sc->sc_sensor[ADMLC_FAN2].type = SENSOR_FANRPM;
172 :
173 :
174 0 : if (sensor_task_register(sc, admlc_refresh, 5) == NULL) {
175 0 : printf(", unable to register update task\n");
176 0 : return;
177 : }
178 :
179 0 : for (i = 0; i < ADMLC_NUM_SENSORS; i++)
180 0 : sensor_attach(&sc->sc_sensordev, &sc->sc_sensor[i]);
181 0 : sensordev_install(&sc->sc_sensordev);
182 :
183 0 : printf("\n");
184 0 : }
185 :
186 : static void
187 0 : fanval(struct ksensor *sens, int mul, u_int8_t data)
188 : {
189 0 : int tmp = data * mul;
190 :
191 0 : if (tmp == 0)
192 0 : sens->flags |= SENSOR_FINVALID;
193 : else
194 0 : sens->value = 1350000 / tmp;
195 0 : }
196 :
197 : void
198 0 : admlc_refresh(void *arg)
199 : {
200 0 : struct admlc_softc *sc = arg;
201 0 : u_int8_t cmd, data;
202 0 : int8_t sdata;
203 :
204 0 : iic_acquire_bus(sc->sc_tag, 0);
205 :
206 0 : cmd = ADM1024_INT_TEMP;
207 0 : if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
208 0 : sc->sc_addr, &cmd, sizeof cmd, &sdata, sizeof sdata, 0) == 0)
209 0 : sc->sc_sensor[ADMLC_INT].value = 273150000 + 1000000 * sdata;
210 :
211 0 : cmd = ADM1024_EXT_TEMP;
212 0 : if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
213 0 : sc->sc_addr, &cmd, sizeof cmd, &sdata, sizeof sdata, 0) == 0)
214 0 : sc->sc_sensor[ADMLC_EXT].value = 273150000 + 1000000 * sdata;
215 :
216 0 : cmd = ADM1024_STATUS2;
217 0 : if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
218 0 : sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0) == 0) {
219 0 : if (data & ADM1024_STATUS2_EXT)
220 0 : sc->sc_sensor[ADMLC_EXT].flags |= SENSOR_FINVALID;
221 : else
222 0 : sc->sc_sensor[ADMLC_EXT].flags &= ~SENSOR_FINVALID;
223 : }
224 :
225 0 : cmd = ADM1024_V2_5;
226 0 : if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
227 0 : sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0) == 0)
228 0 : sc->sc_sensor[ADMLC_V2_5].value = 2500000 * data / 192;
229 :
230 0 : cmd = ADM1024_Vccp;
231 0 : if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
232 0 : sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0) == 0)
233 0 : sc->sc_sensor[ADMLC_Vcc].value = 2249000 * data / 192;
234 :
235 0 : cmd = ADM1024_Vcc;
236 0 : if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
237 0 : sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0) == 0)
238 0 : sc->sc_sensor[ADMLC_Vcc].value = 3300000 * data / 192;
239 :
240 0 : cmd = ADM1024_V5;
241 0 : if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
242 0 : sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0) == 0)
243 0 : sc->sc_sensor[ADMLC_V5].value = 5000000 * data / 192;
244 :
245 0 : cmd = ADM1024_V12;
246 0 : if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
247 0 : sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0) == 0)
248 0 : sc->sc_sensor[ADMLC_V12].value = 12000000 * data / 192;
249 :
250 0 : cmd = ADM1024_Vccp2;
251 0 : if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
252 0 : sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0) == 0)
253 0 : sc->sc_sensor[ADMLC_Vccp2].value = 2700000 * data / 192;
254 :
255 0 : cmd = ADM1024_FAN1;
256 0 : if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
257 0 : sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0) == 0)
258 0 : fanval(&sc->sc_sensor[ADMLC_FAN1], sc->sc_fan1mul, data);
259 :
260 0 : cmd = ADM1024_FAN2;
261 0 : if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
262 0 : sc->sc_addr, &cmd, sizeof cmd, &data, sizeof data, 0) == 0)
263 0 : fanval(&sc->sc_sensor[ADMLC_FAN2], sc->sc_fan2mul, data);
264 0 : iic_release_bus(sc->sc_tag, 0);
265 0 : }
|