adm1021.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. * See file CREDITS for list of people who contributed to this
  11. * project.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License as
  15. * published by the Free Software Foundation; either version 2 of
  16. * the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  26. * MA 02111-1307 USA
  27. */
  28. /*
  29. * Analog Devices's ADM1021
  30. * "Low Cost Microprocessor System Temperature Monitor"
  31. */
  32. #include <common.h>
  33. #include <i2c.h>
  34. #include <dtt.h>
  35. #define DTT_READ_LOC_VALUE 0x00
  36. #define DTT_READ_REM_VALUE 0x01
  37. #define DTT_READ_STATUS 0x02
  38. #define DTT_READ_CONFIG 0x03
  39. #define DTT_READ_CONVRATE 0x04
  40. #define DTT_READ_LOC_HIGHLIM 0x05
  41. #define DTT_READ_LOC_LOWLIM 0x06
  42. #define DTT_READ_REM_HIGHLIM 0x07
  43. #define DTT_READ_REM_LOWLIM 0x08
  44. #define DTT_READ_DEVID 0xfe
  45. #define DTT_WRITE_CONFIG 0x09
  46. #define DTT_WRITE_CONVRATE 0x0a
  47. #define DTT_WRITE_LOC_HIGHLIM 0x0b
  48. #define DTT_WRITE_LOC_LOWLIM 0x0c
  49. #define DTT_WRITE_REM_HIGHLIM 0x0d
  50. #define DTT_WRITE_REM_LOWLIM 0x0e
  51. #define DTT_WRITE_ONESHOT 0x0f
  52. #define DTT_STATUS_BUSY 0x80 /* 1=ADC Converting */
  53. #define DTT_STATUS_LHIGH 0x40 /* 1=Local High Temp Limit Tripped */
  54. #define DTT_STATUS_LLOW 0x20 /* 1=Local Low Temp Limit Tripped */
  55. #define DTT_STATUS_RHIGH 0x10 /* 1=Remote High Temp Limit Tripped */
  56. #define DTT_STATUS_RLOW 0x08 /* 1=Remote Low Temp Limit Tripped */
  57. #define DTT_STATUS_OPEN 0x04 /* 1=Remote Sensor Open-Circuit */
  58. #define DTT_CONFIG_ALERT_MASKED 0x80 /* 0=ALERT Enabled, 1=ALERT Masked */
  59. #define DTT_CONFIG_STANDBY 0x40 /* 0=Run, 1=Standby */
  60. #define DTT_ADM1021_DEVID 0x41
  61. typedef
  62. struct {
  63. uint i2c_addr:7; /* 7bit i2c chip address */
  64. uint conv_rate:3; /* conversion rate */
  65. uint enable_alert:1; /* enable alert output pin */
  66. uint enable_local:1; /* enable internal temp sensor */
  67. uint max_local:8; /* internal temp maximum */
  68. uint min_local:8; /* internal temp minimum */
  69. uint enable_remote:1; /* enable remote temp sensor */
  70. uint max_remote:8; /* remote temp maximum */
  71. uint min_remote:8; /* remote temp minimum */
  72. }
  73. dtt_cfg_t;
  74. dtt_cfg_t dttcfg[] = CONFIG_SYS_DTT_ADM1021;
  75. int
  76. dtt_read (int sensor, int reg)
  77. {
  78. dtt_cfg_t *dcp = &dttcfg[sensor >> 1];
  79. uchar data;
  80. if (i2c_read(dcp->i2c_addr, reg, 1, &data, 1) != 0)
  81. return -1;
  82. return (int)data;
  83. } /* dtt_read() */
  84. int
  85. dtt_write (int sensor, int reg, int val)
  86. {
  87. dtt_cfg_t *dcp = &dttcfg[sensor >> 1];
  88. uchar data;
  89. data = (uchar)(val & 0xff);
  90. if (i2c_write(dcp->i2c_addr, reg, 1, &data, 1) != 0)
  91. return 1;
  92. return 0;
  93. } /* dtt_write() */
  94. static int
  95. _dtt_init (int sensor)
  96. {
  97. dtt_cfg_t *dcp = &dttcfg[sensor >> 1];
  98. int reg, val;
  99. if (((sensor & 1) == 0 ? dcp->enable_local : dcp->enable_remote) == 0)
  100. return 1; /* sensor is disabled (or rather ignored) */
  101. /*
  102. * Setup High Limit register
  103. */
  104. if ((sensor & 1) == 0) {
  105. reg = DTT_WRITE_LOC_HIGHLIM;
  106. val = dcp->max_local;
  107. }
  108. else {
  109. reg = DTT_WRITE_REM_HIGHLIM;
  110. val = dcp->max_remote;
  111. }
  112. if (dtt_write (sensor, reg, val) != 0)
  113. return 1;
  114. /*
  115. * Setup Low Limit register
  116. */
  117. if ((sensor & 1) == 0) {
  118. reg = DTT_WRITE_LOC_LOWLIM;
  119. val = dcp->min_local;
  120. }
  121. else {
  122. reg = DTT_WRITE_REM_LOWLIM;
  123. val = dcp->min_remote;
  124. }
  125. if (dtt_write (sensor, reg, val) != 0)
  126. return 1;
  127. /* shouldn't hurt if the rest gets done twice */
  128. /*
  129. * Setup Conversion Rate register
  130. */
  131. if (dtt_write (sensor, DTT_WRITE_CONVRATE, dcp->conv_rate) != 0)
  132. return 1;
  133. /*
  134. * Setup configuraton register
  135. */
  136. val = 0; /* running */
  137. if (dcp->enable_alert == 0)
  138. val |= DTT_CONFIG_ALERT_MASKED; /* mask ALERT pin */
  139. if (dtt_write (sensor, DTT_WRITE_CONFIG, val) != 0)
  140. return 1;
  141. return 0;
  142. } /* _dtt_init() */
  143. int
  144. dtt_init (void)
  145. {
  146. int i;
  147. unsigned char sensors[] = CONFIG_DTT_SENSORS;
  148. const char *const header = "DTT: ";
  149. /* switch to correct I2C bus */
  150. I2C_SET_BUS(CONFIG_SYS_DTT_BUS_NUM);
  151. for (i = 0; i < sizeof(sensors); i++) {
  152. if (_dtt_init(sensors[i]) != 0)
  153. printf ("%s%d FAILED INIT\n", header, i+1);
  154. else
  155. printf ("%s%d is %i C\n", header, i+1,
  156. dtt_get_temp(sensors[i]));
  157. }
  158. return (0);
  159. } /* dtt_init() */
  160. int
  161. dtt_get_temp (int sensor)
  162. {
  163. signed char val;
  164. if ((sensor & 1) == 0)
  165. val = dtt_read(sensor, DTT_READ_LOC_VALUE);
  166. else
  167. val = dtt_read(sensor, DTT_READ_REM_VALUE);
  168. return (int) val;
  169. } /* dtt_get_temp() */