sklm80.c 7.4 KB

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