sklm80.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. /*
  98. LM80 functions
  99. */
  100. static const char SysKonnectFileId[] =
  101. "$Id: sklm80.c,v 1.20 2002/08/13 09:16:27 rschmidt Exp $" ;
  102. #include "h/skdrv1st.h" /* Driver Specific Definitions */
  103. #include "h/lm80.h"
  104. #include "h/skdrv2nd.h" /* Adapter Control- and Driver specific Def. */
  105. #ifdef SK_DIAG
  106. #define BREAK_OR_WAIT(pAC,IoC,Event) SkI2cWait(pAC,IoC,Event)
  107. #else /* nSK_DIAG */
  108. #define BREAK_OR_WAIT(pAC,IoC,Event) break
  109. #endif /* nSK_DIAG */
  110. #ifdef SK_DIAG
  111. /*
  112. * read the register 'Reg' from the device 'Dev'
  113. *
  114. * return read error -1
  115. * success the read value
  116. */
  117. int SkLm80RcvReg(
  118. SK_IOC IoC, /* Adapter Context */
  119. int Dev, /* I2C device address */
  120. int Reg) /* register to read */
  121. {
  122. int Val = 0;
  123. int TempExt;
  124. /* Signal device number */
  125. if (SkI2cSndDev(IoC, Dev, I2C_WRITE)) {
  126. return(-1);
  127. }
  128. if (SkI2cSndByte(IoC, Reg)) {
  129. return(-1);
  130. }
  131. /* repeat start */
  132. if (SkI2cSndDev(IoC, Dev, I2C_READ)) {
  133. return(-1);
  134. }
  135. switch (Reg) {
  136. case LM80_TEMP_IN:
  137. Val = (int)SkI2cRcvByte(IoC, 1);
  138. /* First: correct the value: it might be negative */
  139. if ((Val & 0x80) != 0) {
  140. /* Value is negative */
  141. Val = Val - 256;
  142. }
  143. Val = Val * SK_LM80_TEMP_LSB;
  144. SkI2cStop(IoC);
  145. TempExt = (int)SkLm80RcvReg(IoC, LM80_ADDR, LM80_TEMP_CTRL);
  146. if (Val > 0) {
  147. Val += ((TempExt >> 7) * SK_LM80_TEMPEXT_LSB);
  148. }
  149. else {
  150. Val -= ((TempExt >> 7) * SK_LM80_TEMPEXT_LSB);
  151. }
  152. return(Val);
  153. break;
  154. case LM80_VT0_IN:
  155. case LM80_VT1_IN:
  156. case LM80_VT2_IN:
  157. case LM80_VT3_IN:
  158. Val = (int)SkI2cRcvByte(IoC, 1) * SK_LM80_VT_LSB;
  159. break;
  160. default:
  161. Val = (int)SkI2cRcvByte(IoC, 1);
  162. break;
  163. }
  164. SkI2cStop(IoC);
  165. return(Val);
  166. }
  167. #endif /* SK_DIAG */
  168. /*
  169. * read a sensors value (LM80 specific)
  170. *
  171. * This function reads a sensors value from the I2C sensor chip LM80.
  172. * The sensor is defined by its index into the sensors database in the struct
  173. * pAC points to.
  174. *
  175. * Returns 1 if the read is completed
  176. * 0 if the read must be continued (I2C Bus still allocated)
  177. */
  178. int SkLm80ReadSensor(
  179. SK_AC *pAC, /* Adapter Context */
  180. SK_IOC IoC, /* I/O Context needed in level 1 and 2 */
  181. SK_SENSOR *pSen) /* Sensor to be read */
  182. {
  183. SK_I32 Value;
  184. switch (pSen->SenState) {
  185. case SK_SEN_IDLE:
  186. /* Send address to ADDR register */
  187. SK_I2C_CTL(IoC, I2C_READ, pSen->SenDev, pSen->SenReg, 0);
  188. pSen->SenState = SK_SEN_VALUE ;
  189. BREAK_OR_WAIT(pAC, IoC, I2C_READ);
  190. case SK_SEN_VALUE:
  191. /* Read value from data register */
  192. SK_IN32(IoC, B2_I2C_DATA, ((SK_U32 *)&Value));
  193. Value &= 0xff; /* only least significant byte is valid */
  194. /* Do NOT check the Value against the thresholds */
  195. /* Checking is done in the calling instance */
  196. if (pSen->SenType == SK_SEN_VOLT) {
  197. /* Voltage sensor */
  198. pSen->SenValue = Value * SK_LM80_VT_LSB;
  199. pSen->SenState = SK_SEN_IDLE ;
  200. return(1);
  201. }
  202. if (pSen->SenType == SK_SEN_FAN) {
  203. if (Value != 0 && Value != 0xff) {
  204. /* Fan speed counter */
  205. pSen->SenValue = SK_LM80_FAN_FAKTOR/Value;
  206. }
  207. else {
  208. /* Indicate Fan error */
  209. pSen->SenValue = 0;
  210. }
  211. pSen->SenState = SK_SEN_IDLE ;
  212. return(1);
  213. }
  214. /* First: correct the value: it might be negative */
  215. if ((Value & 0x80) != 0) {
  216. /* Value is negative */
  217. Value = Value - 256;
  218. }
  219. /* We have a temperature sensor and need to get the signed extension.
  220. * For now we get the extension from the last reading, so in the normal
  221. * case we won't see flickering temperatures.
  222. */
  223. pSen->SenValue = (Value * SK_LM80_TEMP_LSB) +
  224. (pSen->SenValue % SK_LM80_TEMP_LSB);
  225. /* Send address to ADDR register */
  226. SK_I2C_CTL(IoC, I2C_READ, pSen->SenDev, LM80_TEMP_CTRL, 0);
  227. pSen->SenState = SK_SEN_VALEXT ;
  228. BREAK_OR_WAIT(pAC, IoC, I2C_READ);
  229. case SK_SEN_VALEXT:
  230. /* Read value from data register */
  231. SK_IN32(IoC, B2_I2C_DATA, ((SK_U32 *)&Value));
  232. Value &= LM80_TEMP_LSB_9; /* only bit 7 is valid */
  233. /* cut the LSB bit */
  234. pSen->SenValue = ((pSen->SenValue / SK_LM80_TEMP_LSB) *
  235. SK_LM80_TEMP_LSB);
  236. if (pSen->SenValue < 0) {
  237. /* Value negative: The bit value must be subtracted */
  238. pSen->SenValue -= ((Value >> 7) * SK_LM80_TEMPEXT_LSB);
  239. }
  240. else {
  241. /* Value positive: The bit value must be added */
  242. pSen->SenValue += ((Value >> 7) * SK_LM80_TEMPEXT_LSB);
  243. }
  244. pSen->SenState = SK_SEN_IDLE ;
  245. return(1);
  246. default:
  247. SK_ERR_LOG(pAC, SK_ERRCL_SW, SKERR_I2C_E007, SKERR_I2C_E007MSG);
  248. return(1);
  249. }
  250. /* Not completed */
  251. return(0);
  252. }