ds1621.c 3.2 KB

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