blob: 687572b1d24af9c9fd822f64c34da446597146a0 [file] [log] [blame]
wdenkeb20ad32003-09-05 23:19:14 +00001/******************************************************************************
2 *
3 * Name: sklm80.c
4 * Project: GEnesis, PCI Gigabit Ethernet Adapter
5 * Version: $Revision: 1.20 $
6 * Date: $Date: 2002/08/13 09:16:27 $
7 * Purpose: Funktions to access Voltage and Temperature Sensor (LM80)
8 *
9 ******************************************************************************/
10
11/******************************************************************************
12 *
13 * (C)Copyright 1998-2002 SysKonnect GmbH.
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * The information in this file is provided "AS IS" without warranty.
21 *
22 ******************************************************************************/
23
24/******************************************************************************
25 *
26 * History:
27 *
28 * $Log: sklm80.c,v $
29 * Revision 1.20 2002/08/13 09:16:27 rschmidt
30 * Changed return value for SkLm80ReadSensor() back to 'int'
31 * Editorial changes
wdenk9c53f402003-10-15 23:53:47 +000032 *
wdenkeb20ad32003-09-05 23:19:14 +000033 * Revision 1.19 2002/08/06 09:43:31 jschmalz
34 * Extensions and changes for Yukon
wdenk9c53f402003-10-15 23:53:47 +000035 *
wdenkeb20ad32003-09-05 23:19:14 +000036 * Revision 1.18 2002/08/02 12:26:57 rschmidt
37 * Editorial changes
wdenk9c53f402003-10-15 23:53:47 +000038 *
wdenkeb20ad32003-09-05 23:19:14 +000039 * Revision 1.17 1999/11/22 13:35:51 cgoos
40 * Changed license header to GPL.
wdenk9c53f402003-10-15 23:53:47 +000041 *
wdenkeb20ad32003-09-05 23:19:14 +000042 * Revision 1.16 1999/05/27 14:05:47 malthoff
43 * Fans: Set SenVal to 0 if the fan value is 0 or 0xff. Both values
44 * are outside the limits (0: div zero error, 0xff: value not in
45 * range, assume 0).
wdenk9c53f402003-10-15 23:53:47 +000046 *
wdenkeb20ad32003-09-05 23:19:14 +000047 * Revision 1.15 1999/05/27 13:38:51 malthoff
48 * Pervent from Division by zero errors.
wdenk9c53f402003-10-15 23:53:47 +000049 *
wdenkeb20ad32003-09-05 23:19:14 +000050 * Revision 1.14 1999/05/20 09:20:01 cgoos
51 * Changes for 1000Base-T (Fan sensors).
wdenk9c53f402003-10-15 23:53:47 +000052 *
wdenkeb20ad32003-09-05 23:19:14 +000053 * Revision 1.13 1998/10/22 09:48:14 gklug
54 * fix: SysKonnectFileId typo
wdenk9c53f402003-10-15 23:53:47 +000055 *
wdenkeb20ad32003-09-05 23:19:14 +000056 * Revision 1.12 1998/10/09 06:12:06 malthoff
57 * Remove ID_sccs by SysKonnectFileId.
wdenk9c53f402003-10-15 23:53:47 +000058 *
wdenkeb20ad32003-09-05 23:19:14 +000059 * Revision 1.11 1998/09/04 08:33:48 malthoff
60 * bug fix: SenState = SK_SEN_IDLE when
61 * leaving SK_SEN_VALEXT state
wdenk9c53f402003-10-15 23:53:47 +000062 *
wdenkeb20ad32003-09-05 23:19:14 +000063 * Revision 1.10 1998/08/20 12:02:10 gklug
64 * fix: compiler warnings type mismatch
65 *
66 * Revision 1.9 1998/08/20 11:37:38 gklug
67 * chg: change Ioc to IoC
wdenk9c53f402003-10-15 23:53:47 +000068 *
wdenkeb20ad32003-09-05 23:19:14 +000069 * Revision 1.8 1998/08/19 12:20:58 gklug
70 * fix: remove struct from C files (see CCC)
wdenk9c53f402003-10-15 23:53:47 +000071 *
wdenkeb20ad32003-09-05 23:19:14 +000072 * Revision 1.7 1998/08/17 07:04:57 malthoff
73 * Take SkLm80RcvReg() function from ski2c.c.
74 * Add IoC parameter to BREAK_OR_WAIT() macro.
wdenk9c53f402003-10-15 23:53:47 +000075 *
wdenkeb20ad32003-09-05 23:19:14 +000076 * Revision 1.6 1998/08/14 07:11:28 malthoff
77 * remove pAc with pAC.
78 *
79 * Revision 1.5 1998/08/14 06:46:55 gklug
80 * fix: temperature can get negative
81 *
82 * Revision 1.4 1998/08/13 08:27:04 gklug
83 * add: temperature reading now o.k.
84 * fix: pSen declaration, SK_ERR_LOG call, ADDR macro
85 *
86 * Revision 1.3 1998/08/13 07:28:21 gklug
87 * fix: pSen was wrong initialized
88 * add: correct conversion for voltage readings
89 *
90 * Revision 1.2 1998/08/11 07:52:14 gklug
91 * add: Lm80 read sensor function
92 *
93 * Revision 1.1 1998/07/17 09:57:12 gklug
94 * initial version
95 *
96 *
97 *
98 ******************************************************************************/
99
100
wdenkde887eb2003-09-10 18:20:28 +0000101#include <config.h>
102
103#ifdef CONFIG_SK98
104
wdenkeb20ad32003-09-05 23:19:14 +0000105/*
106 LM80 functions
107*/
108static const char SysKonnectFileId[] =
109 "$Id: sklm80.c,v 1.20 2002/08/13 09:16:27 rschmidt Exp $" ;
110
111#include "h/skdrv1st.h" /* Driver Specific Definitions */
112#include "h/lm80.h"
113#include "h/skdrv2nd.h" /* Adapter Control- and Driver specific Def. */
114
115#ifdef SK_DIAG
116#define BREAK_OR_WAIT(pAC,IoC,Event) SkI2cWait(pAC,IoC,Event)
117#else /* nSK_DIAG */
118#define BREAK_OR_WAIT(pAC,IoC,Event) break
119#endif /* nSK_DIAG */
120
121#ifdef SK_DIAG
122/*
123 * read the register 'Reg' from the device 'Dev'
124 *
125 * return read error -1
126 * success the read value
127 */
128int SkLm80RcvReg(
129SK_IOC IoC, /* Adapter Context */
130int Dev, /* I2C device address */
131int Reg) /* register to read */
132{
133 int Val = 0;
134 int TempExt;
135
136 /* Signal device number */
137 if (SkI2cSndDev(IoC, Dev, I2C_WRITE)) {
138 return(-1);
139 }
140
141 if (SkI2cSndByte(IoC, Reg)) {
142 return(-1);
143 }
144
145 /* repeat start */
146 if (SkI2cSndDev(IoC, Dev, I2C_READ)) {
147 return(-1);
148 }
149
150 switch (Reg) {
151 case LM80_TEMP_IN:
152 Val = (int)SkI2cRcvByte(IoC, 1);
153
154 /* First: correct the value: it might be negative */
155 if ((Val & 0x80) != 0) {
156 /* Value is negative */
157 Val = Val - 256;
158 }
159 Val = Val * SK_LM80_TEMP_LSB;
160 SkI2cStop(IoC);
wdenk9c53f402003-10-15 23:53:47 +0000161
wdenkeb20ad32003-09-05 23:19:14 +0000162 TempExt = (int)SkLm80RcvReg(IoC, LM80_ADDR, LM80_TEMP_CTRL);
wdenk9c53f402003-10-15 23:53:47 +0000163
wdenkeb20ad32003-09-05 23:19:14 +0000164 if (Val > 0) {
165 Val += ((TempExt >> 7) * SK_LM80_TEMPEXT_LSB);
166 }
167 else {
168 Val -= ((TempExt >> 7) * SK_LM80_TEMPEXT_LSB);
169 }
170 return(Val);
171 break;
172 case LM80_VT0_IN:
173 case LM80_VT1_IN:
174 case LM80_VT2_IN:
175 case LM80_VT3_IN:
176 Val = (int)SkI2cRcvByte(IoC, 1) * SK_LM80_VT_LSB;
177 break;
wdenk9c53f402003-10-15 23:53:47 +0000178
wdenkeb20ad32003-09-05 23:19:14 +0000179 default:
180 Val = (int)SkI2cRcvByte(IoC, 1);
181 break;
182 }
183
184 SkI2cStop(IoC);
185 return(Val);
186}
187#endif /* SK_DIAG */
188
189/*
190 * read a sensors value (LM80 specific)
191 *
192 * This function reads a sensors value from the I2C sensor chip LM80.
193 * The sensor is defined by its index into the sensors database in the struct
194 * pAC points to.
195 *
196 * Returns 1 if the read is completed
197 * 0 if the read must be continued (I2C Bus still allocated)
198 */
199int SkLm80ReadSensor(
200SK_AC *pAC, /* Adapter Context */
201SK_IOC IoC, /* I/O Context needed in level 1 and 2 */
202SK_SENSOR *pSen) /* Sensor to be read */
203{
204 SK_I32 Value;
205
206 switch (pSen->SenState) {
207 case SK_SEN_IDLE:
208 /* Send address to ADDR register */
209 SK_I2C_CTL(IoC, I2C_READ, pSen->SenDev, pSen->SenReg, 0);
210
211 pSen->SenState = SK_SEN_VALUE ;
212 BREAK_OR_WAIT(pAC, IoC, I2C_READ);
wdenk9c53f402003-10-15 23:53:47 +0000213
wdenkeb20ad32003-09-05 23:19:14 +0000214 case SK_SEN_VALUE:
215 /* Read value from data register */
216 SK_IN32(IoC, B2_I2C_DATA, ((SK_U32 *)&Value));
wdenk9c53f402003-10-15 23:53:47 +0000217
wdenkeb20ad32003-09-05 23:19:14 +0000218 Value &= 0xff; /* only least significant byte is valid */
219
220 /* Do NOT check the Value against the thresholds */
221 /* Checking is done in the calling instance */
222
223 if (pSen->SenType == SK_SEN_VOLT) {
224 /* Voltage sensor */
225 pSen->SenValue = Value * SK_LM80_VT_LSB;
226 pSen->SenState = SK_SEN_IDLE ;
227 return(1);
228 }
229
230 if (pSen->SenType == SK_SEN_FAN) {
231 if (Value != 0 && Value != 0xff) {
232 /* Fan speed counter */
233 pSen->SenValue = SK_LM80_FAN_FAKTOR/Value;
234 }
235 else {
236 /* Indicate Fan error */
237 pSen->SenValue = 0;
238 }
239 pSen->SenState = SK_SEN_IDLE ;
240 return(1);
241 }
242
243 /* First: correct the value: it might be negative */
244 if ((Value & 0x80) != 0) {
245 /* Value is negative */
246 Value = Value - 256;
247 }
248
249 /* We have a temperature sensor and need to get the signed extension.
250 * For now we get the extension from the last reading, so in the normal
251 * case we won't see flickering temperatures.
252 */
253 pSen->SenValue = (Value * SK_LM80_TEMP_LSB) +
254 (pSen->SenValue % SK_LM80_TEMP_LSB);
255
256 /* Send address to ADDR register */
257 SK_I2C_CTL(IoC, I2C_READ, pSen->SenDev, LM80_TEMP_CTRL, 0);
258
259 pSen->SenState = SK_SEN_VALEXT ;
260 BREAK_OR_WAIT(pAC, IoC, I2C_READ);
wdenk9c53f402003-10-15 23:53:47 +0000261
wdenkeb20ad32003-09-05 23:19:14 +0000262 case SK_SEN_VALEXT:
263 /* Read value from data register */
264 SK_IN32(IoC, B2_I2C_DATA, ((SK_U32 *)&Value));
265 Value &= LM80_TEMP_LSB_9; /* only bit 7 is valid */
266
267 /* cut the LSB bit */
268 pSen->SenValue = ((pSen->SenValue / SK_LM80_TEMP_LSB) *
269 SK_LM80_TEMP_LSB);
270
271 if (pSen->SenValue < 0) {
272 /* Value negative: The bit value must be subtracted */
273 pSen->SenValue -= ((Value >> 7) * SK_LM80_TEMPEXT_LSB);
274 }
275 else {
276 /* Value positive: The bit value must be added */
277 pSen->SenValue += ((Value >> 7) * SK_LM80_TEMPEXT_LSB);
278 }
279
280 pSen->SenState = SK_SEN_IDLE ;
281 return(1);
wdenk9c53f402003-10-15 23:53:47 +0000282
wdenkeb20ad32003-09-05 23:19:14 +0000283 default:
284 SK_ERR_LOG(pAC, SK_ERRCL_SW, SKERR_I2C_E007, SKERR_I2C_E007MSG);
285 return(1);
286 }
287
288 /* Not completed */
289 return(0);
290}
wdenkde887eb2003-09-10 18:20:28 +0000291
292#endif /* CONFIG_SK98 */