lm81.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * (C) Copyright 2006
  3. * Heiko Schocher, DENX Software Enginnering <hs@denx.de>
  4. *
  5. * based on dtt/lm75.c which is ...
  6. *
  7. * (C) Copyright 2001
  8. * Bill Hunter, Wave 7 Optics, williamhunter@mediaone.net
  9. *
  10. * See file CREDITS for list of people who contributed to this
  11. * project.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License as
  15. * published by the Free Software Foundation; either version 2 of
  16. * the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  26. * MA 02111-1307 USA
  27. */
  28. /*
  29. * On Semiconductor's LM81 Temperature Sensor
  30. */
  31. #include <common.h>
  32. #if !defined(CFG_EEPROM_PAGE_WRITE_ENABLE) || \
  33. (CFG_EEPROM_PAGE_WRITE_BITS < 1)
  34. # error "CFG_EEPROM_PAGE_WRITE_ENABLE must be defined and CFG_EEPROM_PAGE_WRITE_BITS must be greater than 1 to use CONFIG_DTT_LM81"
  35. #endif
  36. #include <i2c.h>
  37. #include <dtt.h>
  38. /*
  39. * Device code
  40. */
  41. #define DTT_I2C_DEV_CODE 0x2c /* ON Semi's LM81 device */
  42. #define DTT_READ_TEMP 0x27
  43. #define DTT_CONFIG_TEMP 0x4b
  44. #define DTT_TEMP_MAX 0x39
  45. #define DTT_TEMP_HYST 0x3a
  46. #define DTT_CONFIG 0x40
  47. int dtt_read(int sensor, int reg)
  48. {
  49. int dlen = 1;
  50. uchar data[2];
  51. /*
  52. * Calculate sensor address and register.
  53. */
  54. sensor = DTT_I2C_DEV_CODE + (sensor & 0x03); /* calculate address of lm81 */
  55. /*
  56. * Now try to read the register.
  57. */
  58. if (i2c_read(sensor, reg, 1, data, dlen) != 0)
  59. return -1;
  60. return (int)data[0];
  61. } /* dtt_read() */
  62. int dtt_write(int sensor, int reg, int val)
  63. {
  64. uchar data;
  65. /*
  66. * Calculate sensor address and register.
  67. */
  68. sensor = DTT_I2C_DEV_CODE + (sensor & 0x03); /* calculate address of lm81 */
  69. data = (char)(val & 0xff);
  70. /*
  71. * Write value to register.
  72. */
  73. if (i2c_write(sensor, reg, 1, &data, 1) != 0)
  74. return 1;
  75. return 0;
  76. } /* dtt_write() */
  77. #define DTT_MANU 0x3e
  78. #define DTT_REV 0x3f
  79. #define DTT_CONFIG 0x40
  80. #define DTT_ADR 0x48
  81. static int _dtt_init(int sensor)
  82. {
  83. int man;
  84. int adr;
  85. int rev;
  86. if (dtt_write (sensor, DTT_CONFIG, 0x01) < 0)
  87. return 1;
  88. /* The LM81 needs 400ms to get the correct values ... */
  89. udelay (400000);
  90. man = dtt_read (sensor, DTT_MANU);
  91. if (man != 0x01)
  92. return 1;
  93. adr = dtt_read (sensor, DTT_ADR);
  94. if (adr < 0)
  95. return 1;
  96. rev = dtt_read (sensor, DTT_REV);
  97. if (adr < 0)
  98. return 1;
  99. printf ("DTT: Found LM81@%x Rev: %d\n", adr, rev);
  100. return 0;
  101. } /* _dtt_init() */
  102. int dtt_init (void)
  103. {
  104. int i;
  105. unsigned char sensors[] = CONFIG_DTT_SENSORS;
  106. const char *const header = "DTT: ";
  107. for (i = 0; i < sizeof(sensors); i++) {
  108. if (_dtt_init(sensors[i]) != 0)
  109. printf("%s%d FAILED INIT\n", header, i+1);
  110. else
  111. printf("%s%d is %i C\n", header, i+1,
  112. dtt_get_temp(sensors[i]));
  113. }
  114. return (0);
  115. } /* dtt_init() */
  116. #define TEMP_FROM_REG(temp) \
  117. ((temp)<256?((((temp)&0x1fe) >> 1) * 10) + ((temp) & 1) * 5: \
  118. ((((temp)&0x1fe) >> 1) -255) * 10 - ((temp) & 1) * 5) \
  119. int dtt_get_temp(int sensor)
  120. {
  121. int val = dtt_read (sensor, DTT_READ_TEMP);
  122. int tmpcnf = dtt_read (sensor, DTT_CONFIG_TEMP);
  123. return (TEMP_FROM_REG((val << 1) + ((tmpcnf & 0x80) >> 7))) / 10;
  124. } /* dtt_get_temp() */