sklm80.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /******************************************************************************
  2. *
  3. * Name: sklm80.c
  4. * Project: Gigabit Ethernet Adapters, TWSI-Module
  5. * Version: $Revision: 1.22 $
  6. * Date: $Date: 2003/10/20 09:08:21 $
  7. * Purpose: Functions to access Voltage and Temperature Sensor (LM80)
  8. *
  9. ******************************************************************************/
  10. /******************************************************************************
  11. *
  12. * (C)Copyright 1998-2002 SysKonnect.
  13. * (C)Copyright 2002-2003 Marvell.
  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. LM80 functions
  25. */
  26. #if (defined(DEBUG) || ((!defined(LINT)) && (!defined(SK_SLIM))))
  27. static const char SysKonnectFileId[] =
  28. "@(#) $Id: sklm80.c,v 1.22 2003/10/20 09:08:21 rschmidt Exp $ (C) Marvell. ";
  29. #endif
  30. #include "h/skdrv1st.h" /* Driver Specific Definitions */
  31. #include "h/lm80.h"
  32. #include "h/skdrv2nd.h" /* Adapter Control- and Driver specific Def. */
  33. #ifdef SK_DIAG
  34. #define BREAK_OR_WAIT(pAC,IoC,Event) SkI2cWait(pAC,IoC,Event)
  35. #else /* nSK_DIAG */
  36. #define BREAK_OR_WAIT(pAC,IoC,Event) break
  37. #endif /* nSK_DIAG */
  38. #ifdef SK_DIAG
  39. /*
  40. * read the register 'Reg' from the device 'Dev'
  41. *
  42. * return read error -1
  43. * success the read value
  44. */
  45. int SkLm80RcvReg(
  46. SK_IOC IoC, /* Adapter Context */
  47. int Dev, /* I2C device address */
  48. int Reg) /* register to read */
  49. {
  50. int Val = 0;
  51. int TempExt;
  52. /* Signal device number */
  53. if (SkI2cSndDev(IoC, Dev, I2C_WRITE)) {
  54. return(-1);
  55. }
  56. if (SkI2cSndByte(IoC, Reg)) {
  57. return(-1);
  58. }
  59. /* repeat start */
  60. if (SkI2cSndDev(IoC, Dev, I2C_READ)) {
  61. return(-1);
  62. }
  63. switch (Reg) {
  64. case LM80_TEMP_IN:
  65. Val = (int)SkI2cRcvByte(IoC, 1);
  66. /* First: correct the value: it might be negative */
  67. if ((Val & 0x80) != 0) {
  68. /* Value is negative */
  69. Val = Val - 256;
  70. }
  71. Val = Val * SK_LM80_TEMP_LSB;
  72. SkI2cStop(IoC);
  73. TempExt = (int)SkLm80RcvReg(IoC, LM80_ADDR, LM80_TEMP_CTRL);
  74. if (Val > 0) {
  75. Val += ((TempExt >> 7) * SK_LM80_TEMPEXT_LSB);
  76. }
  77. else {
  78. Val -= ((TempExt >> 7) * SK_LM80_TEMPEXT_LSB);
  79. }
  80. return(Val);
  81. break;
  82. case LM80_VT0_IN:
  83. case LM80_VT1_IN:
  84. case LM80_VT2_IN:
  85. case LM80_VT3_IN:
  86. Val = (int)SkI2cRcvByte(IoC, 1) * SK_LM80_VT_LSB;
  87. break;
  88. default:
  89. Val = (int)SkI2cRcvByte(IoC, 1);
  90. break;
  91. }
  92. SkI2cStop(IoC);
  93. return(Val);
  94. }
  95. #endif /* SK_DIAG */
  96. /*
  97. * read a sensors value (LM80 specific)
  98. *
  99. * This function reads a sensors value from the I2C sensor chip LM80.
  100. * The sensor is defined by its index into the sensors database in the struct
  101. * pAC points to.
  102. *
  103. * Returns 1 if the read is completed
  104. * 0 if the read must be continued (I2C Bus still allocated)
  105. */
  106. int SkLm80ReadSensor(
  107. SK_AC *pAC, /* Adapter Context */
  108. SK_IOC IoC, /* I/O Context needed in level 1 and 2 */
  109. SK_SENSOR *pSen) /* Sensor to be read */
  110. {
  111. SK_I32 Value;
  112. switch (pSen->SenState) {
  113. case SK_SEN_IDLE:
  114. /* Send address to ADDR register */
  115. SK_I2C_CTL(IoC, I2C_READ, pSen->SenDev, I2C_025K_DEV, pSen->SenReg, 0);
  116. pSen->SenState = SK_SEN_VALUE ;
  117. BREAK_OR_WAIT(pAC, IoC, I2C_READ);
  118. case SK_SEN_VALUE:
  119. /* Read value from data register */
  120. SK_IN32(IoC, B2_I2C_DATA, ((SK_U32 *)&Value));
  121. Value &= 0xff; /* only least significant byte is valid */
  122. /* Do NOT check the Value against the thresholds */
  123. /* Checking is done in the calling instance */
  124. if (pSen->SenType == SK_SEN_VOLT) {
  125. /* Voltage sensor */
  126. pSen->SenValue = Value * SK_LM80_VT_LSB;
  127. pSen->SenState = SK_SEN_IDLE ;
  128. return(1);
  129. }
  130. if (pSen->SenType == SK_SEN_FAN) {
  131. if (Value != 0 && Value != 0xff) {
  132. /* Fan speed counter */
  133. pSen->SenValue = SK_LM80_FAN_FAKTOR/Value;
  134. }
  135. else {
  136. /* Indicate Fan error */
  137. pSen->SenValue = 0;
  138. }
  139. pSen->SenState = SK_SEN_IDLE ;
  140. return(1);
  141. }
  142. /* First: correct the value: it might be negative */
  143. if ((Value & 0x80) != 0) {
  144. /* Value is negative */
  145. Value = Value - 256;
  146. }
  147. /* We have a temperature sensor and need to get the signed extension.
  148. * For now we get the extension from the last reading, so in the normal
  149. * case we won't see flickering temperatures.
  150. */
  151. pSen->SenValue = (Value * SK_LM80_TEMP_LSB) +
  152. (pSen->SenValue % SK_LM80_TEMP_LSB);
  153. /* Send address to ADDR register */
  154. SK_I2C_CTL(IoC, I2C_READ, pSen->SenDev, I2C_025K_DEV, LM80_TEMP_CTRL, 0);
  155. pSen->SenState = SK_SEN_VALEXT ;
  156. BREAK_OR_WAIT(pAC, IoC, I2C_READ);
  157. case SK_SEN_VALEXT:
  158. /* Read value from data register */
  159. SK_IN32(IoC, B2_I2C_DATA, ((SK_U32 *)&Value));
  160. Value &= LM80_TEMP_LSB_9; /* only bit 7 is valid */
  161. /* cut the LSB bit */
  162. pSen->SenValue = ((pSen->SenValue / SK_LM80_TEMP_LSB) *
  163. SK_LM80_TEMP_LSB);
  164. if (pSen->SenValue < 0) {
  165. /* Value negative: The bit value must be subtracted */
  166. pSen->SenValue -= ((Value >> 7) * SK_LM80_TEMPEXT_LSB);
  167. }
  168. else {
  169. /* Value positive: The bit value must be added */
  170. pSen->SenValue += ((Value >> 7) * SK_LM80_TEMPEXT_LSB);
  171. }
  172. pSen->SenState = SK_SEN_IDLE ;
  173. return(1);
  174. default:
  175. SK_ERR_LOG(pAC, SK_ERRCL_SW, SKERR_I2C_E007, SKERR_I2C_E007MSG);
  176. return(1);
  177. }
  178. /* Not completed */
  179. return(0);
  180. }