sklm80.c 7.4 KB

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