ds1775.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * This program is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU General Public License as
  4. * published by the Free Software Foundation; either version 2 of
  5. * the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  15. * MA 02111-1307 USA
  16. */
  17. /*
  18. * Dallas Semiconductor's DS1775 Digital Thermometer and Thermostat
  19. */
  20. #include <common.h>
  21. #ifdef CONFIG_DTT_DS1775
  22. #include <i2c.h>
  23. #include <dtt.h>
  24. #define DTT_I2C_DEV_CODE CFG_I2C_DTT_ADDR /* Dallas Semi's DS1775 device code */
  25. int dtt_read(int sensor, int reg)
  26. {
  27. int dlen;
  28. uchar data[2];
  29. /*
  30. * Calculate sensor address and command
  31. */
  32. sensor = DTT_I2C_DEV_CODE + (sensor & 0x07); /* Calculate addr of ds1775 */
  33. /*
  34. * Prepare to handle 2 byte result
  35. */
  36. if ((reg == DTT_READ_TEMP) ||
  37. (reg == DTT_TEMP_OS) || (reg == DTT_TEMP_HYST))
  38. dlen = 2;
  39. else
  40. dlen = 1;
  41. /*
  42. * Now try to read the register
  43. */
  44. if (i2c_read(sensor, reg, 1, data, dlen) != 0)
  45. return 1;
  46. /*
  47. * Handle 2 byte result
  48. */
  49. if (dlen == 2)
  50. return ((int)((short)data[1] + (((short)data[0]) << 8)));
  51. return (int) data[0];
  52. }
  53. int dtt_write(int sensor, int reg, int val)
  54. {
  55. int dlen;
  56. uchar data[2];
  57. /*
  58. * Calculate sensor address and register
  59. */
  60. sensor = DTT_I2C_DEV_CODE + (sensor & 0x07);
  61. /*
  62. * Handle various data sizes
  63. */
  64. if ((reg == DTT_READ_TEMP) ||
  65. (reg == DTT_TEMP_OS) || (reg == DTT_TEMP_HYST)) {
  66. dlen = 2;
  67. data[0] = (char)((val >> 8) & 0xff); /* MSB first */
  68. data[1] = (char)(val & 0xff);
  69. } else {
  70. dlen = 1;
  71. data[0] = (char)(val & 0xff);
  72. }
  73. /*
  74. * Write value to device
  75. */
  76. if (i2c_write(sensor, reg, 1, data, dlen) != 0)
  77. return 1;
  78. return 0;
  79. }
  80. static int _dtt_init(int sensor)
  81. {
  82. int val;
  83. /*
  84. * Setup High Temp
  85. */
  86. val = ((CFG_DTT_MAX_TEMP * 2) << 7) & 0xff80;
  87. if (dtt_write(sensor, DTT_TEMP_OS, val) != 0)
  88. return 1;
  89. udelay(50000); /* Max 50ms */
  90. /*
  91. * Setup Low Temp - hysteresis
  92. */
  93. val = (((CFG_DTT_MAX_TEMP - CFG_DTT_HYSTERESIS) * 2) << 7) & 0xff80;
  94. if (dtt_write(sensor, DTT_TEMP_HYST, val) != 0)
  95. return 1;
  96. udelay(50000); /* Max 50ms */
  97. /*
  98. * Setup configuraton register
  99. *
  100. * Fault Tolerance limits 4, Thermometer resolution bits is 9,
  101. * Polarity = Active Low,continuous conversion mode, Thermostat
  102. * mode is interrupt mode
  103. */
  104. val = 0xa;
  105. if (dtt_write(sensor, DTT_CONFIG, val) != 0)
  106. return 1;
  107. udelay(50000); /* Max 50ms */
  108. return 0;
  109. }
  110. int dtt_init (void)
  111. {
  112. int i;
  113. unsigned char sensors[] = CONFIG_DTT_SENSORS;
  114. for (i = 0; i < sizeof(sensors); i++) {
  115. if (_dtt_init(sensors[i]) != 0)
  116. printf("DTT%d: FAILED\n", i+1);
  117. else
  118. printf("DTT%d: %i C\n", i+1, dtt_get_temp(sensors[i]));
  119. }
  120. return (0);
  121. }
  122. int dtt_get_temp(int sensor)
  123. {
  124. return (dtt_read(sensor, DTT_READ_TEMP) / 256);
  125. }
  126. #endif /* CONFIG_DTT_DS1775 */