adm1021.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * (C) Copyright 2003
  3. * Murray Jensen, CSIRO-MIT, Murray.Jensen@csiro.au
  4. *
  5. * based on dtt/lm75.c which is ...
  6. *
  7. * (C) Copyright 2001
  8. * Bill Hunter, Wave 7 Optics, williamhunter@mediaone.net
  9. *
  10. * SPDX-License-Identifier: GPL-2.0+
  11. */
  12. /*
  13. * Analog Devices's ADM1021
  14. * "Low Cost Microprocessor System Temperature Monitor"
  15. */
  16. #include <common.h>
  17. #include <i2c.h>
  18. #include <dtt.h>
  19. #define DTT_READ_LOC_VALUE 0x00
  20. #define DTT_READ_REM_VALUE 0x01
  21. #define DTT_READ_STATUS 0x02
  22. #define DTT_READ_CONFIG 0x03
  23. #define DTT_READ_CONVRATE 0x04
  24. #define DTT_READ_LOC_HIGHLIM 0x05
  25. #define DTT_READ_LOC_LOWLIM 0x06
  26. #define DTT_READ_REM_HIGHLIM 0x07
  27. #define DTT_READ_REM_LOWLIM 0x08
  28. #define DTT_READ_DEVID 0xfe
  29. #define DTT_WRITE_CONFIG 0x09
  30. #define DTT_WRITE_CONVRATE 0x0a
  31. #define DTT_WRITE_LOC_HIGHLIM 0x0b
  32. #define DTT_WRITE_LOC_LOWLIM 0x0c
  33. #define DTT_WRITE_REM_HIGHLIM 0x0d
  34. #define DTT_WRITE_REM_LOWLIM 0x0e
  35. #define DTT_WRITE_ONESHOT 0x0f
  36. #define DTT_STATUS_BUSY 0x80 /* 1=ADC Converting */
  37. #define DTT_STATUS_LHIGH 0x40 /* 1=Local High Temp Limit Tripped */
  38. #define DTT_STATUS_LLOW 0x20 /* 1=Local Low Temp Limit Tripped */
  39. #define DTT_STATUS_RHIGH 0x10 /* 1=Remote High Temp Limit Tripped */
  40. #define DTT_STATUS_RLOW 0x08 /* 1=Remote Low Temp Limit Tripped */
  41. #define DTT_STATUS_OPEN 0x04 /* 1=Remote Sensor Open-Circuit */
  42. #define DTT_CONFIG_ALERT_MASKED 0x80 /* 0=ALERT Enabled, 1=ALERT Masked */
  43. #define DTT_CONFIG_STANDBY 0x40 /* 0=Run, 1=Standby */
  44. #define DTT_ADM1021_DEVID 0x41
  45. typedef
  46. struct {
  47. uint i2c_addr:7; /* 7bit i2c chip address */
  48. uint conv_rate:3; /* conversion rate */
  49. uint enable_alert:1; /* enable alert output pin */
  50. uint enable_local:1; /* enable internal temp sensor */
  51. uint max_local:8; /* internal temp maximum */
  52. uint min_local:8; /* internal temp minimum */
  53. uint enable_remote:1; /* enable remote temp sensor */
  54. uint max_remote:8; /* remote temp maximum */
  55. uint min_remote:8; /* remote temp minimum */
  56. }
  57. dtt_cfg_t;
  58. dtt_cfg_t dttcfg[] = CONFIG_SYS_DTT_ADM1021;
  59. int
  60. dtt_read (int sensor, int reg)
  61. {
  62. dtt_cfg_t *dcp = &dttcfg[sensor >> 1];
  63. uchar data;
  64. if (i2c_read(dcp->i2c_addr, reg, 1, &data, 1) != 0)
  65. return -1;
  66. return (int)data;
  67. } /* dtt_read() */
  68. int
  69. dtt_write (int sensor, int reg, int val)
  70. {
  71. dtt_cfg_t *dcp = &dttcfg[sensor >> 1];
  72. uchar data;
  73. data = (uchar)(val & 0xff);
  74. if (i2c_write(dcp->i2c_addr, reg, 1, &data, 1) != 0)
  75. return 1;
  76. return 0;
  77. } /* dtt_write() */
  78. int
  79. dtt_init_one(int sensor)
  80. {
  81. dtt_cfg_t *dcp = &dttcfg[sensor >> 1];
  82. int reg, val;
  83. if (((sensor & 1) == 0 ? dcp->enable_local : dcp->enable_remote) == 0)
  84. return 1; /* sensor is disabled (or rather ignored) */
  85. /*
  86. * Setup High Limit register
  87. */
  88. if ((sensor & 1) == 0) {
  89. reg = DTT_WRITE_LOC_HIGHLIM;
  90. val = dcp->max_local;
  91. }
  92. else {
  93. reg = DTT_WRITE_REM_HIGHLIM;
  94. val = dcp->max_remote;
  95. }
  96. if (dtt_write (sensor, reg, val) != 0)
  97. return 1;
  98. /*
  99. * Setup Low Limit register
  100. */
  101. if ((sensor & 1) == 0) {
  102. reg = DTT_WRITE_LOC_LOWLIM;
  103. val = dcp->min_local;
  104. }
  105. else {
  106. reg = DTT_WRITE_REM_LOWLIM;
  107. val = dcp->min_remote;
  108. }
  109. if (dtt_write (sensor, reg, val) != 0)
  110. return 1;
  111. /* shouldn't hurt if the rest gets done twice */
  112. /*
  113. * Setup Conversion Rate register
  114. */
  115. if (dtt_write (sensor, DTT_WRITE_CONVRATE, dcp->conv_rate) != 0)
  116. return 1;
  117. /*
  118. * Setup configuraton register
  119. */
  120. val = 0; /* running */
  121. if (dcp->enable_alert == 0)
  122. val |= DTT_CONFIG_ALERT_MASKED; /* mask ALERT pin */
  123. if (dtt_write (sensor, DTT_WRITE_CONFIG, val) != 0)
  124. return 1;
  125. return 0;
  126. } /* dtt_init_one() */
  127. int
  128. dtt_get_temp (int sensor)
  129. {
  130. signed char val;
  131. if ((sensor & 1) == 0)
  132. val = dtt_read(sensor, DTT_READ_LOC_VALUE);
  133. else
  134. val = dtt_read(sensor, DTT_READ_REM_VALUE);
  135. return (int) val;
  136. } /* dtt_get_temp() */