ds1775.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * SPDX-License-Identifier: GPL-2.0+
  3. */
  4. /*
  5. * Dallas Semiconductor's DS1775 Digital Thermometer and Thermostat
  6. */
  7. #include <common.h>
  8. #include <i2c.h>
  9. #include <dtt.h>
  10. #define DTT_I2C_DEV_CODE CONFIG_SYS_I2C_DTT_ADDR /* Dallas Semi's DS1775 device code */
  11. #define DTT_READ_TEMP 0x0
  12. #define DTT_CONFIG 0x1
  13. #define DTT_TEMP_HYST 0x2
  14. #define DTT_TEMP_OS 0x3
  15. int dtt_read(int sensor, int reg)
  16. {
  17. int dlen;
  18. uchar data[2];
  19. /*
  20. * Calculate sensor address and command
  21. */
  22. sensor = DTT_I2C_DEV_CODE + (sensor & 0x07); /* Calculate addr of ds1775 */
  23. /*
  24. * Prepare to handle 2 byte result
  25. */
  26. if ((reg == DTT_READ_TEMP) ||
  27. (reg == DTT_TEMP_OS) || (reg == DTT_TEMP_HYST))
  28. dlen = 2;
  29. else
  30. dlen = 1;
  31. /*
  32. * Now try to read the register
  33. */
  34. if (i2c_read(sensor, reg, 1, data, dlen) != 0)
  35. return 1;
  36. /*
  37. * Handle 2 byte result
  38. */
  39. if (dlen == 2)
  40. return ((int)((short)data[1] + (((short)data[0]) << 8)));
  41. return (int) data[0];
  42. }
  43. int dtt_write(int sensor, int reg, int val)
  44. {
  45. int dlen;
  46. uchar data[2];
  47. /*
  48. * Calculate sensor address and register
  49. */
  50. sensor = DTT_I2C_DEV_CODE + (sensor & 0x07);
  51. /*
  52. * Handle various data sizes
  53. */
  54. if ((reg == DTT_READ_TEMP) ||
  55. (reg == DTT_TEMP_OS) || (reg == DTT_TEMP_HYST)) {
  56. dlen = 2;
  57. data[0] = (char)((val >> 8) & 0xff); /* MSB first */
  58. data[1] = (char)(val & 0xff);
  59. } else {
  60. dlen = 1;
  61. data[0] = (char)(val & 0xff);
  62. }
  63. /*
  64. * Write value to device
  65. */
  66. if (i2c_write(sensor, reg, 1, data, dlen) != 0)
  67. return 1;
  68. return 0;
  69. }
  70. int dtt_init_one(int sensor)
  71. {
  72. int val;
  73. /*
  74. * Setup High Temp
  75. */
  76. val = ((CONFIG_SYS_DTT_MAX_TEMP * 2) << 7) & 0xff80;
  77. if (dtt_write(sensor, DTT_TEMP_OS, val) != 0)
  78. return 1;
  79. udelay(50000); /* Max 50ms */
  80. /*
  81. * Setup Low Temp - hysteresis
  82. */
  83. val = (((CONFIG_SYS_DTT_MAX_TEMP - CONFIG_SYS_DTT_HYSTERESIS) * 2) << 7) & 0xff80;
  84. if (dtt_write(sensor, DTT_TEMP_HYST, val) != 0)
  85. return 1;
  86. udelay(50000); /* Max 50ms */
  87. /*
  88. * Setup configuraton register
  89. *
  90. * Fault Tolerance limits 4, Thermometer resolution bits is 9,
  91. * Polarity = Active Low,continuous conversion mode, Thermostat
  92. * mode is interrupt mode
  93. */
  94. val = 0xa;
  95. if (dtt_write(sensor, DTT_CONFIG, val) != 0)
  96. return 1;
  97. udelay(50000); /* Max 50ms */
  98. return 0;
  99. }
  100. int dtt_get_temp(int sensor)
  101. {
  102. return (dtt_read(sensor, DTT_READ_TEMP) / 256);
  103. }