lm73.c 3.7 KB

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