sklm80.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. #define BREAK_OR_WAIT(pAC,IoC,Event) break
  34. /*
  35. * read a sensors value (LM80 specific)
  36. *
  37. * This function reads a sensors value from the I2C sensor chip LM80.
  38. * The sensor is defined by its index into the sensors database in the struct
  39. * pAC points to.
  40. *
  41. * Returns 1 if the read is completed
  42. * 0 if the read must be continued (I2C Bus still allocated)
  43. */
  44. int SkLm80ReadSensor(
  45. SK_AC *pAC, /* Adapter Context */
  46. SK_IOC IoC, /* I/O Context needed in level 1 and 2 */
  47. SK_SENSOR *pSen) /* Sensor to be read */
  48. {
  49. SK_I32 Value;
  50. switch (pSen->SenState) {
  51. case SK_SEN_IDLE:
  52. /* Send address to ADDR register */
  53. SK_I2C_CTL(IoC, I2C_READ, pSen->SenDev, I2C_025K_DEV, pSen->SenReg, 0);
  54. pSen->SenState = SK_SEN_VALUE ;
  55. BREAK_OR_WAIT(pAC, IoC, I2C_READ);
  56. case SK_SEN_VALUE:
  57. /* Read value from data register */
  58. SK_IN32(IoC, B2_I2C_DATA, ((SK_U32 *)&Value));
  59. Value &= 0xff; /* only least significant byte is valid */
  60. /* Do NOT check the Value against the thresholds */
  61. /* Checking is done in the calling instance */
  62. if (pSen->SenType == SK_SEN_VOLT) {
  63. /* Voltage sensor */
  64. pSen->SenValue = Value * SK_LM80_VT_LSB;
  65. pSen->SenState = SK_SEN_IDLE ;
  66. return(1);
  67. }
  68. if (pSen->SenType == SK_SEN_FAN) {
  69. if (Value != 0 && Value != 0xff) {
  70. /* Fan speed counter */
  71. pSen->SenValue = SK_LM80_FAN_FAKTOR/Value;
  72. }
  73. else {
  74. /* Indicate Fan error */
  75. pSen->SenValue = 0;
  76. }
  77. pSen->SenState = SK_SEN_IDLE ;
  78. return(1);
  79. }
  80. /* First: correct the value: it might be negative */
  81. if ((Value & 0x80) != 0) {
  82. /* Value is negative */
  83. Value = Value - 256;
  84. }
  85. /* We have a temperature sensor and need to get the signed extension.
  86. * For now we get the extension from the last reading, so in the normal
  87. * case we won't see flickering temperatures.
  88. */
  89. pSen->SenValue = (Value * SK_LM80_TEMP_LSB) +
  90. (pSen->SenValue % SK_LM80_TEMP_LSB);
  91. /* Send address to ADDR register */
  92. SK_I2C_CTL(IoC, I2C_READ, pSen->SenDev, I2C_025K_DEV, LM80_TEMP_CTRL, 0);
  93. pSen->SenState = SK_SEN_VALEXT ;
  94. BREAK_OR_WAIT(pAC, IoC, I2C_READ);
  95. case SK_SEN_VALEXT:
  96. /* Read value from data register */
  97. SK_IN32(IoC, B2_I2C_DATA, ((SK_U32 *)&Value));
  98. Value &= LM80_TEMP_LSB_9; /* only bit 7 is valid */
  99. /* cut the LSB bit */
  100. pSen->SenValue = ((pSen->SenValue / SK_LM80_TEMP_LSB) *
  101. SK_LM80_TEMP_LSB);
  102. if (pSen->SenValue < 0) {
  103. /* Value negative: The bit value must be subtracted */
  104. pSen->SenValue -= ((Value >> 7) * SK_LM80_TEMPEXT_LSB);
  105. }
  106. else {
  107. /* Value positive: The bit value must be added */
  108. pSen->SenValue += ((Value >> 7) * SK_LM80_TEMPEXT_LSB);
  109. }
  110. pSen->SenState = SK_SEN_IDLE ;
  111. return(1);
  112. default:
  113. SK_ERR_LOG(pAC, SK_ERRCL_SW, SKERR_I2C_E007, SKERR_I2C_E007MSG);
  114. return(1);
  115. }
  116. /* Not completed */
  117. return(0);
  118. }