lm73.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * (C) Copyright 2007
  3. * Larry Johnson, lrj@acm.org
  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. * National Semiconductor LM73 Temperature Sensor
  30. */
  31. #include <common.h>
  32. #ifdef CONFIG_DTT_LM73
  33. #if !defined(CFG_EEPROM_PAGE_WRITE_ENABLE) || \
  34. (CFG_EEPROM_PAGE_WRITE_BITS < 1)
  35. # error "CFG_EEPROM_PAGE_WRITE_ENABLE must be defined and CFG_EEPROM_PAGE_WRITE_BITS must be greater than 1 to use CONFIG_DTT_LM73"
  36. #endif
  37. #include <i2c.h>
  38. #include <dtt.h>
  39. /*
  40. * Device code
  41. */
  42. #define DTT_I2C_DEV_CODE 0x48 /* National Semi's LM73 device */
  43. int dtt_read(int sensor, int reg)
  44. {
  45. int dlen;
  46. uchar data[2];
  47. /*
  48. * Validate 'reg' param and get register size.
  49. */
  50. switch (reg) {
  51. case DTT_CONFIG:
  52. case DTT_CONTROL:
  53. dlen = 1;
  54. break;
  55. case DTT_READ_TEMP:
  56. case DTT_TEMP_HIGH:
  57. case DTT_TEMP_LOW:
  58. case DTT_ID:
  59. dlen = 2;
  60. break;
  61. default:
  62. return -1;
  63. }
  64. /*
  65. * Calculate sensor address and register.
  66. */
  67. sensor = DTT_I2C_DEV_CODE + (sensor & 0x07); /* calculate LM73 addr */
  68. /*
  69. * Now try to read the register.
  70. */
  71. if (i2c_read(sensor, reg, 1, data, dlen) != 0)
  72. return -1;
  73. /*
  74. * Handle 2 byte result.
  75. */
  76. if (2 == dlen)
  77. return ((int)((short)data[1] + (((short)data[0]) << 8)));
  78. return (int)data[0];
  79. } /* dtt_read() */
  80. int dtt_write(int sensor, int reg, int val)
  81. {
  82. int dlen;
  83. uchar data[2];
  84. /*
  85. * Validate 'reg' param and handle register size
  86. */
  87. switch (reg) {
  88. case DTT_CONFIG:
  89. case DTT_CONTROL:
  90. dlen = 1;
  91. data[0] = (char)(val & 0xff);
  92. break;
  93. case DTT_TEMP_HIGH:
  94. case DTT_TEMP_LOW:
  95. dlen = 2;
  96. data[0] = (char)((val >> 8) & 0xff); /* MSB first */
  97. data[1] = (char)(val & 0xff);
  98. break;
  99. default:
  100. return -1;
  101. }
  102. /*
  103. * Calculate sensor address and register.
  104. */
  105. sensor = DTT_I2C_DEV_CODE + (sensor & 0x07); /* calculate LM73 addr */
  106. /*
  107. * Write value to register.
  108. */
  109. return i2c_write(sensor, reg, 1, data, dlen) != 0;
  110. } /* dtt_write() */
  111. static int _dtt_init(int sensor)
  112. {
  113. int val;
  114. /*
  115. * Validate the Identification register
  116. */
  117. if (0x0190 != dtt_read(sensor, DTT_ID))
  118. return 1;
  119. /*
  120. * Setup THIGH (upper-limit) and TLOW (lower-limit) registers
  121. */
  122. val = CFG_DTT_MAX_TEMP << 7;
  123. if (dtt_write(sensor, DTT_TEMP_HIGH, val))
  124. return 1;
  125. val = CFG_DTT_MIN_TEMP << 7;
  126. if (dtt_write(sensor, DTT_TEMP_LOW, val))
  127. return 1;
  128. /*
  129. * Setup configuraton register
  130. */
  131. /* config = alert active low, disabled, and reset */
  132. val = 0x64;
  133. if (dtt_write(sensor, DTT_CONFIG, val))
  134. return 1;
  135. /*
  136. * Setup control/status register
  137. */
  138. /* control = temp resolution 0.25C */
  139. val = 0x00;
  140. if (dtt_write(sensor, DTT_CONTROL, val))
  141. return 1;
  142. dtt_read(sensor, DTT_CONTROL); /* clear temperature flags */
  143. return 0;
  144. } /* _dtt_init() */
  145. int dtt_init(void)
  146. {
  147. int i;
  148. unsigned char sensors[] = CONFIG_DTT_SENSORS;
  149. const char *const header = "DTT: ";
  150. for (i = 0; i < sizeof(sensors); i++) {
  151. if (_dtt_init(sensors[i]) != 0)
  152. printf("%s%d FAILED INIT\n", header, i + 1);
  153. else
  154. printf("%s%d is %i C\n", header, i + 1,
  155. dtt_get_temp(sensors[i]));
  156. }
  157. return 0;
  158. } /* dtt_init() */
  159. int dtt_get_temp(int sensor)
  160. {
  161. return (dtt_read(sensor, DTT_READ_TEMP) + 0x0040) >> 7;
  162. } /* dtt_get_temp() */
  163. #endif /* CONFIG_DTT_LM73 */