ds1621.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 Digital Thermometer and Thermostat.
  25. */
  26. #include <common.h>
  27. #ifdef CONFIG_DTT_DS1621
  28. #if !defined(CFG_EEPROM_PAGE_WRITE_ENABLE) || \
  29. (CFG_EEPROM_PAGE_WRITE_BITS < 1)
  30. # error "CFG_EEPROM_PAGE_WRITE_ENABLE must be defined and CFG_EEPROM_PAGE_WRITE_BITS must be greater than 1 to use CONFIG_DTT_DS1621"
  31. #endif
  32. #include <i2c.h>
  33. #include <dtt.h>
  34. /*
  35. * Device code
  36. */
  37. #define DTT_I2C_DEV_CODE 0x48 /* Dallas Semi's DS1621 */
  38. int dtt_read(int sensor, int reg)
  39. {
  40. int dlen;
  41. uchar data[2];
  42. /*
  43. * Calculate sensor address and command.
  44. *
  45. */
  46. sensor = DTT_I2C_DEV_CODE + (sensor & 0x07); /* Calculate addr of ds1621*/
  47. /*
  48. * Prepare to handle 2 byte result.
  49. */
  50. if ((reg == DTT_READ_TEMP) ||
  51. (reg == DTT_TEMP_HIGH) || (reg == DTT_TEMP_LOW))
  52. dlen = 2;
  53. else
  54. dlen = 1;
  55. /*
  56. * Now try to read the register.
  57. */
  58. if (i2c_read(sensor, reg, 1, data, dlen) != 0)
  59. return 1;
  60. /*
  61. * Handle 2 byte result.
  62. */
  63. if (dlen == 2)
  64. return ((int)((short)data[1] + (((short)data[0]) << 8)));
  65. return (int)data[0];
  66. } /* dtt_read() */
  67. int dtt_write(int sensor, int reg, int val)
  68. {
  69. int dlen;
  70. uchar data[2];
  71. /*
  72. * Calculate sensor address and register.
  73. *
  74. */
  75. sensor = DTT_I2C_DEV_CODE + (sensor & 0x07);
  76. /*
  77. * Handle various data sizes.
  78. */
  79. if ((reg == DTT_READ_TEMP) ||
  80. (reg == DTT_TEMP_HIGH) || (reg == DTT_TEMP_LOW)) {
  81. dlen = 2;
  82. data[0] = (char)((val >> 8) & 0xff); /* MSB first */
  83. data[1] = (char)(val & 0xff);
  84. }
  85. else if ((reg == DTT_WRITE_START_CONV) || (reg == DTT_WRITE_STOP_CONV)) {
  86. dlen = 0;
  87. data[0] = (char)0;
  88. data[1] = (char)0;
  89. }
  90. else {
  91. dlen = 1;
  92. data[0] = (char)(val & 0xff);
  93. }
  94. /*
  95. * Write value to device.
  96. */
  97. if (i2c_write(sensor, reg, 1, data, dlen) != 0)
  98. return 1;
  99. return 0;
  100. } /* dtt_write() */
  101. static int _dtt_init(int sensor)
  102. {
  103. int val;
  104. /*
  105. * Setup High Temp.
  106. */
  107. val = ((CFG_DTT_MAX_TEMP * 2) << 7) & 0xff80;
  108. if (dtt_write(sensor, DTT_TEMP_HIGH, val) != 0)
  109. return 1;
  110. udelay(50000); /* Max 50ms */
  111. /*
  112. * Setup Low Temp - hysteresis.
  113. */
  114. val = (((CFG_DTT_MAX_TEMP - CFG_DTT_HYSTERESIS) * 2) << 7) & 0xff80;
  115. if (dtt_write(sensor, DTT_TEMP_LOW, val) != 0)
  116. return 1;
  117. udelay(50000); /* Max 50ms */
  118. /*
  119. * Setup configuraton register
  120. *
  121. * Clear THF & TLF, Reserved = 1, Polarity = Active Low, One Shot = YES
  122. *
  123. * We run in polled mode, since there isn't any way to know if this
  124. * lousy device is ready to provide temperature readings on power up.
  125. */
  126. val = 0x9;
  127. if (dtt_write(sensor, DTT_CONFIG, val) != 0)
  128. return 1;
  129. udelay(50000); /* Max 50ms */
  130. return 0;
  131. } /* _dtt_init() */
  132. int dtt_init (void)
  133. {
  134. int i;
  135. unsigned char sensors[] = CONFIG_DTT_SENSORS;
  136. for (i = 0; i < sizeof(sensors); i++) {
  137. if (_dtt_init(sensors[i]) != 0)
  138. printf("DTT%d: FAILED\n", i+1);
  139. else
  140. printf("DTT%d: %i C\n", i+1, dtt_get_temp(sensors[i]));
  141. }
  142. return (0);
  143. } /* dtt_init() */
  144. int dtt_get_temp(int sensor)
  145. {
  146. int i;
  147. /*
  148. * Start a conversion, may take up to 1 second.
  149. */
  150. dtt_write(sensor, DTT_WRITE_START_CONV, 0);
  151. for (i = 0; i <= 10; i++) {
  152. udelay(100000);
  153. if (dtt_read(sensor, DTT_CONFIG) & 0x80)
  154. break;
  155. }
  156. return (dtt_read(sensor, DTT_READ_TEMP) / 256);
  157. } /* dtt_get_temp() */
  158. #endif /* CONFIG_DTT_DS1621 */