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