lm75.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. #if !defined(CFG_EEPROM_PAGE_WRITE_ENABLE) || \
  28. (CFG_EEPROM_PAGE_WRITE_BITS < 1)
  29. # 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"
  30. #endif
  31. #include <i2c.h>
  32. #include <dtt.h>
  33. /*
  34. * Device code
  35. */
  36. #define DTT_I2C_DEV_CODE 0x48 /* ON Semi's LM75 device */
  37. #define DTT_READ_TEMP 0x0
  38. #define DTT_CONFIG 0x1
  39. #define DTT_TEMP_HYST 0x2
  40. #define DTT_TEMP_SET 0x3
  41. int dtt_read(int sensor, int reg)
  42. {
  43. int dlen;
  44. uchar data[2];
  45. #ifdef CONFIG_DTT_AD7414
  46. /*
  47. * On AD7414 the first value upon bootup is not read correctly.
  48. * This is most likely because of the 800ms update time of the
  49. * temp register in normal update mode. To get current values
  50. * each time we issue the "dtt" command including upon powerup
  51. * we switch into one-short mode.
  52. *
  53. * Issue one-shot mode command
  54. */
  55. dtt_write(sensor, DTT_CONFIG, 0x64);
  56. #endif
  57. /*
  58. * Validate 'reg' param
  59. */
  60. if((reg < 0) || (reg > 3))
  61. return -1;
  62. /*
  63. * Calculate sensor address and register.
  64. */
  65. sensor = DTT_I2C_DEV_CODE + (sensor & 0x07); /* calculate address of lm75 */
  66. /*
  67. * Prepare to handle 2 byte result.
  68. */
  69. if ((reg == DTT_READ_TEMP) ||
  70. (reg == DTT_TEMP_HYST) ||
  71. (reg == DTT_TEMP_SET))
  72. dlen = 2;
  73. else
  74. dlen = 1;
  75. /*
  76. * Now try to read the register.
  77. */
  78. if (i2c_read(sensor, reg, 1, data, dlen) != 0)
  79. return -1;
  80. /*
  81. * Handle 2 byte result.
  82. */
  83. if (dlen == 2)
  84. return ((int)((short)data[1] + (((short)data[0]) << 8)));
  85. return (int)data[0];
  86. } /* dtt_read() */
  87. int dtt_write(int sensor, int reg, int val)
  88. {
  89. int dlen;
  90. uchar data[2];
  91. /*
  92. * Validate 'reg' param
  93. */
  94. if ((reg < 0) || (reg > 3))
  95. return 1;
  96. /*
  97. * Calculate sensor address and register.
  98. */
  99. sensor = DTT_I2C_DEV_CODE + (sensor & 0x07); /* calculate address of lm75 */
  100. /*
  101. * Handle 2 byte values.
  102. */
  103. if ((reg == DTT_READ_TEMP) ||
  104. (reg == DTT_TEMP_HYST) ||
  105. (reg == DTT_TEMP_SET)) {
  106. dlen = 2;
  107. data[0] = (char)((val >> 8) & 0xff); /* MSB first */
  108. data[1] = (char)(val & 0xff);
  109. } else {
  110. dlen = 1;
  111. data[0] = (char)(val & 0xff);
  112. }
  113. /*
  114. * Write value to register.
  115. */
  116. if (i2c_write(sensor, reg, 1, data, dlen) != 0)
  117. return 1;
  118. return 0;
  119. } /* dtt_write() */
  120. static int _dtt_init(int sensor)
  121. {
  122. int val;
  123. /*
  124. * Setup TSET ( trip point ) register
  125. */
  126. val = ((CFG_DTT_MAX_TEMP * 2) << 7) & 0xff80; /* trip */
  127. if (dtt_write(sensor, DTT_TEMP_SET, val) != 0)
  128. return 1;
  129. /*
  130. * Setup THYST ( untrip point ) register - Hysteresis
  131. */
  132. val = (((CFG_DTT_MAX_TEMP - CFG_DTT_HYSTERESIS) * 2) << 7) & 0xff80;
  133. if (dtt_write(sensor, DTT_TEMP_HYST, val) != 0)
  134. return 1;
  135. /*
  136. * Setup configuraton register
  137. */
  138. #ifdef CONFIG_DTT_AD7414
  139. /* config = alert active low and disabled */
  140. val = 0x60;
  141. #else
  142. /* config = 6 sample integration, int mode, active low, and enable */
  143. val = 0x18;
  144. #endif
  145. if (dtt_write(sensor, DTT_CONFIG, val) != 0)
  146. return 1;
  147. return 0;
  148. } /* _dtt_init() */
  149. int dtt_init (void)
  150. {
  151. int i;
  152. unsigned char sensors[] = CONFIG_DTT_SENSORS;
  153. const char *const header = "DTT: ";
  154. for (i = 0; i < sizeof(sensors); i++) {
  155. if (_dtt_init(sensors[i]) != 0)
  156. printf("%s%d FAILED INIT\n", header, i+1);
  157. else
  158. printf("%s%d is %i C\n", header, i+1,
  159. dtt_get_temp(sensors[i]));
  160. }
  161. return (0);
  162. } /* dtt_init() */
  163. int dtt_get_temp(int sensor)
  164. {
  165. int const ret = dtt_read(sensor, DTT_READ_TEMP);
  166. if (ret < 0) {
  167. printf("DTT temperature read failed.\n");
  168. return 0;
  169. }
  170. return (int)((int16_t) ret / 256);
  171. } /* dtt_get_temp() */