lm73.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * (C) Copyright 2007-2008
  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. #define DTT_READ_TEMP 0x0
  39. #define DTT_CONFIG 0x1
  40. #define DTT_TEMP_HIGH 0x2
  41. #define DTT_TEMP_LOW 0x3
  42. #define DTT_CONTROL 0x4
  43. #define DTT_ID 0x7
  44. int dtt_read(int const sensor, int const reg)
  45. {
  46. int dlen;
  47. uint8_t data[2];
  48. /*
  49. * Validate 'reg' param and get register size.
  50. */
  51. switch (reg) {
  52. case DTT_CONFIG:
  53. case DTT_CONTROL:
  54. dlen = 1;
  55. break;
  56. case DTT_READ_TEMP:
  57. case DTT_TEMP_HIGH:
  58. case DTT_TEMP_LOW:
  59. case DTT_ID:
  60. dlen = 2;
  61. break;
  62. default:
  63. return -1;
  64. }
  65. /*
  66. * Try to read the register at the calculated sensor address.
  67. */
  68. if (0 !=
  69. i2c_read(DTT_I2C_DEV_CODE + (sensor & 0x07), reg, 1, data, dlen))
  70. return -1;
  71. /*
  72. * Handle 2 byte result.
  73. */
  74. if (2 == dlen)
  75. return (int)((unsigned)data[0] << 8 | (unsigned)data[1]);
  76. return (int)data[0];
  77. } /* dtt_read() */
  78. int dtt_write(int const sensor, int const reg, int const val)
  79. {
  80. int dlen;
  81. uint8_t data[2];
  82. /*
  83. * Validate 'reg' param and handle register size
  84. */
  85. switch (reg) {
  86. case DTT_CONFIG:
  87. case DTT_CONTROL:
  88. dlen = 1;
  89. data[0] = (uint8_t) val;
  90. break;
  91. case DTT_TEMP_HIGH:
  92. case DTT_TEMP_LOW:
  93. dlen = 2;
  94. data[0] = (uint8_t) (val >> 8); /* MSB first */
  95. data[1] = (uint8_t) val;
  96. break;
  97. default:
  98. return -1;
  99. }
  100. /*
  101. * Write value to register at the calculated sensor address.
  102. */
  103. return 0 != i2c_write(DTT_I2C_DEV_CODE + (sensor & 0x07), reg, 1, data,
  104. dlen);
  105. } /* dtt_write() */
  106. static int _dtt_init(int const 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 = CONFIG_SYS_DTT_MAX_TEMP << 7;
  118. if (dtt_write(sensor, DTT_TEMP_HIGH, val))
  119. return -1;
  120. val = CONFIG_SYS_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 (0 != _dtt_init(sensors[i]))
  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 const sensor)
  155. {
  156. int const ret = dtt_read(sensor, DTT_READ_TEMP);
  157. if (ret < 0) {
  158. printf("DTT temperature read failed.\n");
  159. return 0;
  160. }
  161. return (int)((int16_t) ret + 0x0040) >> 7;
  162. } /* dtt_get_temp() */