lm75.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * (C) Copyright 2001
  3. * Bill Hunter, Wave 7 Optics, williamhunter@mediaone.net
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /*
  24. * On Semiconductor's LM75 Temperature Sensor
  25. */
  26. #include <common.h>
  27. #ifdef CONFIG_DTT_LM75
  28. #if !defined(CFG_EEPROM_PAGE_WRITE_ENABLE) || \
  29. (CFG_EEPROM_PAGE_WRITE_BITS < 1)
  30. # error "CFG_EEPROM_PAGE_WRITE_ENABLE must be defined and CFG_EEPROM_PAGE_WRITE_BITS must be greater than 1 to use CONFIG_DTT_LM75"
  31. #endif
  32. #include <i2c.h>
  33. #include <dtt.h>
  34. /*
  35. * Device code
  36. */
  37. #define DTT_I2C_DEV_CODE 0x48 /* ON Semi's LM75 device */
  38. int dtt_read(int sensor, int reg)
  39. {
  40. int dlen;
  41. uchar data[2];
  42. /*
  43. * Validate 'reg' param
  44. */
  45. if((reg < 0) || (reg > 3))
  46. return -1;
  47. /*
  48. * Calculate sensor address and register.
  49. */
  50. sensor = DTT_I2C_DEV_CODE + (sensor & 0x07); /* calculate address of lm75 */
  51. /*
  52. * Prepare to handle 2 byte result.
  53. */
  54. if ((reg == DTT_READ_TEMP) ||
  55. (reg == DTT_TEMP_HYST) ||
  56. (reg == DTT_TEMP_SET))
  57. dlen = 2;
  58. else
  59. dlen = 1;
  60. /*
  61. * Now try to read the register.
  62. */
  63. if (i2c_read(sensor, reg, 1, data, dlen) != 0)
  64. return -1;
  65. /*
  66. * Handle 2 byte result.
  67. */
  68. if (dlen == 2)
  69. return ((int)((short)data[1] + (((short)data[0]) << 8)));
  70. return (int)data[0];
  71. } /* dtt_read() */
  72. int dtt_write(int sensor, int reg, int val)
  73. {
  74. int dlen;
  75. uchar data[2];
  76. /*
  77. * Validate 'reg' param
  78. */
  79. if ((reg < 0) || (reg > 3))
  80. return 1;
  81. /*
  82. * Calculate sensor address and register.
  83. */
  84. sensor = DTT_I2C_DEV_CODE + (sensor & 0x07); /* calculate address of lm75 */
  85. /*
  86. * Handle 2 byte values.
  87. */
  88. if ((reg == DTT_READ_TEMP) ||
  89. (reg == DTT_TEMP_HYST) ||
  90. (reg == DTT_TEMP_SET)) {
  91. dlen = 2;
  92. data[0] = (char)((val >> 8) & 0xff); /* MSB first */
  93. data[1] = (char)(val & 0xff);
  94. } else {
  95. dlen = 1;
  96. data[0] = (char)(val & 0xff);
  97. }
  98. /*
  99. * Write value to register.
  100. */
  101. if (i2c_write(sensor, reg, 1, data, dlen) != 0)
  102. return 1;
  103. return 0;
  104. } /* dtt_write() */
  105. static int _dtt_init(int sensor)
  106. {
  107. int val;
  108. /*
  109. * Setup TSET ( trip point ) register
  110. */
  111. val = ((CFG_DTT_MAX_TEMP * 2) << 7) & 0xff80; /* trip */
  112. if (dtt_write(sensor, DTT_TEMP_SET, val) != 0)
  113. return 1;
  114. /*
  115. * Setup THYST ( untrip point ) register - Hysteresis
  116. */
  117. val = (((CFG_DTT_MAX_TEMP - CFG_DTT_HYSTERESIS) * 2) << 7) & 0xff80;
  118. if (dtt_write(sensor, DTT_TEMP_HYST, val) != 0)
  119. return 1;
  120. /*
  121. * Setup configuraton register
  122. */
  123. #ifdef CONFIG_DTT_AD7414
  124. /* config = alert active low and disabled */
  125. val = 0x60;
  126. #else
  127. /* config = 6 sample integration, int mode, active low, and enable */
  128. val = 0x18;
  129. #endif
  130. if (dtt_write(sensor, DTT_CONFIG, val) != 0)
  131. return 1;
  132. return 0;
  133. } /* _dtt_init() */
  134. int dtt_init (void)
  135. {
  136. int i;
  137. unsigned char sensors[] = CONFIG_DTT_SENSORS;
  138. const char *const header = "DTT: ";
  139. for (i = 0; i < sizeof(sensors); i++) {
  140. if (_dtt_init(sensors[i]) != 0)
  141. printf("%s%d FAILED INIT\n", header, i+1);
  142. else
  143. printf("%s%d is %i C\n", header, i+1,
  144. dtt_get_temp(sensors[i]));
  145. }
  146. return (0);
  147. } /* dtt_init() */
  148. int dtt_get_temp(int sensor)
  149. {
  150. return (dtt_read(sensor, DTT_READ_TEMP) / 256);
  151. } /* dtt_get_temp() */
  152. #endif /* CONFIG_DTT_LM75 */