ds1621.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * (C) Copyright 2001
  3. * Erik Theisen, Wave 7 Optics, etheisen@mindspring.com.
  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. * Dallas Semiconductor's DS1621/1631 Digital Thermometer and Thermostat.
  25. */
  26. #include <common.h>
  27. #include <i2c.h>
  28. #include <dtt.h>
  29. /*
  30. * Device code
  31. */
  32. #define DTT_I2C_DEV_CODE 0x48 /* Dallas Semi's DS1621 */
  33. #define DTT_READ_TEMP 0xAA
  34. #define DTT_READ_COUNTER 0xA8
  35. #define DTT_READ_SLOPE 0xA9
  36. #define DTT_WRITE_START_CONV 0xEE
  37. #define DTT_WRITE_STOP_CONV 0x22
  38. #define DTT_TEMP_HIGH 0xA1
  39. #define DTT_TEMP_LOW 0xA2
  40. #define DTT_CONFIG 0xAC
  41. /*
  42. * Config register bits
  43. */
  44. #define DTT_CONFIG_1SHOT 0x01
  45. #define DTT_CONFIG_POLARITY 0x02
  46. #define DTT_CONFIG_R0 0x04 /* ds1631 only */
  47. #define DTT_CONFIG_R1 0x08 /* ds1631 only */
  48. #define DTT_CONFIG_NVB 0x10
  49. #define DTT_CONFIG_TLF 0x20
  50. #define DTT_CONFIG_THF 0x40
  51. #define DTT_CONFIG_DONE 0x80
  52. int dtt_read(int sensor, int reg)
  53. {
  54. int dlen;
  55. uchar data[2];
  56. /* Calculate sensor address and command */
  57. sensor = DTT_I2C_DEV_CODE + (sensor & 0x07); /* Calculate addr of ds1621*/
  58. /* Prepare to handle 2 byte result */
  59. switch(reg) {
  60. case DTT_READ_TEMP:
  61. case DTT_TEMP_HIGH:
  62. case DTT_TEMP_LOW:
  63. dlen = 2;
  64. break;
  65. default:
  66. dlen = 1;
  67. }
  68. /* Now try to read the register */
  69. if (i2c_read(sensor, reg, 1, data, dlen) != 0)
  70. return 1;
  71. /* Handle 2 byte result */
  72. if (dlen == 2)
  73. return (short)((data[0] << 8) | data[1]);
  74. return (int)data[0];
  75. }
  76. int dtt_write(int sensor, int reg, int val)
  77. {
  78. int dlen;
  79. uchar data[2];
  80. /* Calculate sensor address and register */
  81. sensor = DTT_I2C_DEV_CODE + (sensor & 0x07);
  82. /* Handle various data sizes. */
  83. switch(reg) {
  84. case DTT_READ_TEMP:
  85. case DTT_TEMP_HIGH:
  86. case DTT_TEMP_LOW:
  87. dlen = 2;
  88. data[0] = (char)((val >> 8) & 0xff); /* MSB first */
  89. data[1] = (char)(val & 0xff);
  90. break;
  91. case DTT_WRITE_START_CONV:
  92. case DTT_WRITE_STOP_CONV:
  93. dlen = 0;
  94. data[0] = (char)0;
  95. data[1] = (char)0;
  96. break;
  97. default:
  98. dlen = 1;
  99. data[0] = (char)(val & 0xff);
  100. }
  101. /* Write value to device */
  102. if (i2c_write(sensor, reg, 1, data, dlen) != 0)
  103. return 1;
  104. /* Poll NV memory busy bit in case write was to register stored in EEPROM */
  105. while(i2c_reg_read(sensor, DTT_CONFIG) & DTT_CONFIG_NVB)
  106. ;
  107. return 0;
  108. }
  109. static int _dtt_init(int sensor)
  110. {
  111. int val;
  112. /* Setup High Temp */
  113. val = ((CONFIG_SYS_DTT_MAX_TEMP * 2) << 7) & 0xff80;
  114. if (dtt_write(sensor, DTT_TEMP_HIGH, val) != 0)
  115. return 1;
  116. /* Setup Low Temp - hysteresis */
  117. val = (((CONFIG_SYS_DTT_MAX_TEMP - CONFIG_SYS_DTT_HYSTERESIS) * 2) << 7) & 0xff80;
  118. if (dtt_write(sensor, DTT_TEMP_LOW, val) != 0)
  119. return 1;
  120. /*
  121. * Setup configuraton register
  122. *
  123. * Clear THF & TLF, Reserved = 1, Polarity = Active Low, One Shot = YES
  124. *
  125. * We run in polled mode, since there isn't any way to know if this
  126. * lousy device is ready to provide temperature readings on power up.
  127. */
  128. val = 0x9;
  129. if (dtt_write(sensor, DTT_CONFIG, val) != 0)
  130. return 1;
  131. return 0;
  132. }
  133. int dtt_init (void)
  134. {
  135. int i;
  136. unsigned char sensors[] = CONFIG_DTT_SENSORS;
  137. for (i = 0; i < sizeof(sensors); i++) {
  138. if (_dtt_init(sensors[i]) != 0)
  139. printf("DTT%d: FAILED\n", i + 1);
  140. else
  141. printf("DTT%d: %i C\n", i + 1, dtt_get_temp(sensors[i]));
  142. }
  143. return (0);
  144. }
  145. int dtt_get_temp(int sensor)
  146. {
  147. int i;
  148. /* Start a conversion, may take up to 1 second. */
  149. dtt_write(sensor, DTT_WRITE_START_CONV, 0);
  150. for (i = 0; i <= 10; i++) {
  151. udelay(100000);
  152. if (dtt_read(sensor, DTT_CONFIG) & DTT_CONFIG_DONE)
  153. break;
  154. }
  155. return (dtt_read(sensor, DTT_READ_TEMP) / 256);
  156. }