lm75.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. #include <i2c.h>
  28. #include <dtt.h>
  29. /*
  30. * Device code
  31. */
  32. #if defined(CONFIG_SYS_I2C_DTT_ADDR)
  33. #define DTT_I2C_DEV_CODE CONFIG_SYS_I2C_DTT_ADDR
  34. #else
  35. #define DTT_I2C_DEV_CODE 0x48 /* ON Semi's LM75 device */
  36. #endif
  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. /* Validate 'reg' param */
  58. if((reg < 0) || (reg > 3))
  59. return -1;
  60. /* Calculate sensor address and register. */
  61. sensor = DTT_I2C_DEV_CODE + (sensor & 0x07);
  62. /* Prepare to handle 2 byte result. */
  63. if ((reg == DTT_READ_TEMP) ||
  64. (reg == DTT_TEMP_HYST) ||
  65. (reg == DTT_TEMP_SET))
  66. dlen = 2;
  67. else
  68. dlen = 1;
  69. /* Now try to read the register. */
  70. if (i2c_read(sensor, reg, 1, data, dlen) != 0)
  71. return -1;
  72. /* Handle 2 byte result. */
  73. if (dlen == 2)
  74. return ((int)((short)data[1] + (((short)data[0]) << 8)));
  75. return (int)data[0];
  76. } /* dtt_read() */
  77. int dtt_write(int sensor, int reg, int val)
  78. {
  79. int dlen;
  80. uchar data[2];
  81. /* Validate 'reg' param */
  82. if ((reg < 0) || (reg > 3))
  83. return 1;
  84. /* Calculate sensor address and register. */
  85. sensor = DTT_I2C_DEV_CODE + (sensor & 0x07);
  86. /* Handle 2 byte values. */
  87. if ((reg == DTT_READ_TEMP) ||
  88. (reg == DTT_TEMP_HYST) ||
  89. (reg == DTT_TEMP_SET)) {
  90. dlen = 2;
  91. data[0] = (char)((val >> 8) & 0xff); /* MSB first */
  92. data[1] = (char)(val & 0xff);
  93. } else {
  94. dlen = 1;
  95. data[0] = (char)(val & 0xff);
  96. }
  97. /* Write value to register. */
  98. if (i2c_write(sensor, reg, 1, data, dlen) != 0)
  99. return 1;
  100. return 0;
  101. } /* dtt_write() */
  102. static int _dtt_init(int sensor)
  103. {
  104. int val;
  105. /* Setup TSET ( trip point ) register */
  106. val = ((CONFIG_SYS_DTT_MAX_TEMP * 2) << 7) & 0xff80; /* trip */
  107. if (dtt_write(sensor, DTT_TEMP_SET, val) != 0)
  108. return 1;
  109. /* Setup THYST ( untrip point ) register - Hysteresis */
  110. val = (((CONFIG_SYS_DTT_MAX_TEMP - CONFIG_SYS_DTT_HYSTERESIS) * 2) << 7) & 0xff80;
  111. if (dtt_write(sensor, DTT_TEMP_HYST, val) != 0)
  112. return 1;
  113. /* Setup configuraton register */
  114. #ifdef CONFIG_DTT_AD7414
  115. /* config = alert active low and disabled */
  116. val = 0x60;
  117. #else
  118. /* config = 6 sample integration, int mode, active low, and enable */
  119. val = 0x18;
  120. #endif
  121. if (dtt_write(sensor, DTT_CONFIG, val) != 0)
  122. return 1;
  123. return 0;
  124. } /* _dtt_init() */
  125. int dtt_init (void)
  126. {
  127. int i;
  128. unsigned char sensors[] = CONFIG_DTT_SENSORS;
  129. const char *const header = "DTT: ";
  130. int old_bus;
  131. /* switch to correct I2C bus */
  132. old_bus = I2C_GET_BUS();
  133. I2C_SET_BUS(CONFIG_SYS_DTT_BUS_NUM);
  134. for (i = 0; i < sizeof(sensors); i++) {
  135. if (_dtt_init(sensors[i]) != 0)
  136. printf("%s%d FAILED INIT\n", header, i+1);
  137. else
  138. printf("%s%d is %i C\n", header, i+1,
  139. dtt_get_temp(sensors[i]));
  140. }
  141. /* switch back to original I2C bus */
  142. I2C_SET_BUS(old_bus);
  143. return (0);
  144. } /* dtt_init() */
  145. int dtt_get_temp(int sensor)
  146. {
  147. int const ret = dtt_read(sensor, DTT_READ_TEMP);
  148. if (ret < 0) {
  149. printf("DTT temperature read failed.\n");
  150. return 0;
  151. }
  152. return (int)((int16_t) ret / 256);
  153. } /* dtt_get_temp() */