lm75.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * (C) Copyright 2001
  3. * Bill Hunter, Wave 7 Optics, williamhunter@mediaone.net
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. /*
  8. * On Semiconductor's LM75 Temperature Sensor
  9. */
  10. #include <common.h>
  11. #include <i2c.h>
  12. #include <dtt.h>
  13. /*
  14. * Device code
  15. */
  16. #if defined(CONFIG_SYS_I2C_DTT_ADDR)
  17. #define DTT_I2C_DEV_CODE CONFIG_SYS_I2C_DTT_ADDR
  18. #else
  19. #define DTT_I2C_DEV_CODE 0x48 /* ON Semi's LM75 device */
  20. #endif
  21. #define DTT_READ_TEMP 0x0
  22. #define DTT_CONFIG 0x1
  23. #define DTT_TEMP_HYST 0x2
  24. #define DTT_TEMP_SET 0x3
  25. int dtt_read(int sensor, int reg)
  26. {
  27. int dlen;
  28. uchar data[2];
  29. #ifdef CONFIG_DTT_AD7414
  30. /*
  31. * On AD7414 the first value upon bootup is not read correctly.
  32. * This is most likely because of the 800ms update time of the
  33. * temp register in normal update mode. To get current values
  34. * each time we issue the "dtt" command including upon powerup
  35. * we switch into one-short mode.
  36. *
  37. * Issue one-shot mode command
  38. */
  39. dtt_write(sensor, DTT_CONFIG, 0x64);
  40. #endif
  41. /* Validate 'reg' param */
  42. if((reg < 0) || (reg > 3))
  43. return -1;
  44. /* Calculate sensor address and register. */
  45. sensor = DTT_I2C_DEV_CODE + (sensor & 0x07);
  46. /* Prepare to handle 2 byte result. */
  47. if ((reg == DTT_READ_TEMP) ||
  48. (reg == DTT_TEMP_HYST) ||
  49. (reg == DTT_TEMP_SET))
  50. dlen = 2;
  51. else
  52. dlen = 1;
  53. /* Now try to read the register. */
  54. if (i2c_read(sensor, reg, 1, data, dlen) != 0)
  55. return -1;
  56. /* Handle 2 byte result. */
  57. if (dlen == 2)
  58. return ((int)((short)data[1] + (((short)data[0]) << 8)));
  59. return (int)data[0];
  60. } /* dtt_read() */
  61. int dtt_write(int sensor, int reg, int val)
  62. {
  63. int dlen;
  64. uchar data[2];
  65. /* Validate 'reg' param */
  66. if ((reg < 0) || (reg > 3))
  67. return 1;
  68. /* Calculate sensor address and register. */
  69. sensor = DTT_I2C_DEV_CODE + (sensor & 0x07);
  70. /* Handle 2 byte values. */
  71. if ((reg == DTT_READ_TEMP) ||
  72. (reg == DTT_TEMP_HYST) ||
  73. (reg == DTT_TEMP_SET)) {
  74. dlen = 2;
  75. data[0] = (char)((val >> 8) & 0xff); /* MSB first */
  76. data[1] = (char)(val & 0xff);
  77. } else {
  78. dlen = 1;
  79. data[0] = (char)(val & 0xff);
  80. }
  81. /* Write value to register. */
  82. if (i2c_write(sensor, reg, 1, data, dlen) != 0)
  83. return 1;
  84. return 0;
  85. } /* dtt_write() */
  86. int dtt_init_one(int sensor)
  87. {
  88. int val;
  89. /* Setup TSET ( trip point ) register */
  90. val = ((CONFIG_SYS_DTT_MAX_TEMP * 2) << 7) & 0xff80; /* trip */
  91. if (dtt_write(sensor, DTT_TEMP_SET, val) != 0)
  92. return 1;
  93. /* Setup THYST ( untrip point ) register - Hysteresis */
  94. val = (((CONFIG_SYS_DTT_MAX_TEMP - CONFIG_SYS_DTT_HYSTERESIS) * 2) << 7) & 0xff80;
  95. if (dtt_write(sensor, DTT_TEMP_HYST, val) != 0)
  96. return 1;
  97. /* Setup configuraton register */
  98. #ifdef CONFIG_DTT_AD7414
  99. /* config = alert active low and disabled */
  100. val = 0x60;
  101. #else
  102. /* config = 6 sample integration, int mode, active low, and enable */
  103. val = 0x18;
  104. #endif
  105. if (dtt_write(sensor, DTT_CONFIG, val) != 0)
  106. return 1;
  107. return 0;
  108. } /* dtt_init_one() */
  109. int dtt_get_temp(int sensor)
  110. {
  111. int const ret = dtt_read(sensor, DTT_READ_TEMP);
  112. if (ret < 0) {
  113. printf("DTT temperature read failed.\n");
  114. return 0;
  115. }
  116. return (int)((int16_t) ret / 256);
  117. } /* dtt_get_temp() */