adm1021.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. #ifdef CONFIG_DTT_ADM1021
  34. #include <i2c.h>
  35. #include <dtt.h>
  36. typedef
  37. struct {
  38. uint i2c_addr:7; /* 7bit i2c chip address */
  39. uint conv_rate:3; /* conversion rate */
  40. uint enable_alert:1; /* enable alert output pin */
  41. uint enable_local:1; /* enable internal temp sensor */
  42. uint max_local:8; /* internal temp maximum */
  43. uint min_local:8; /* internal temp minimum */
  44. uint enable_remote:1; /* enable remote temp sensor */
  45. uint max_remote:8; /* remote temp maximum */
  46. uint min_remote:8; /* remote temp minimum */
  47. }
  48. dtt_cfg_t;
  49. dtt_cfg_t dttcfg[] = CFG_DTT_ADM1021;
  50. int
  51. dtt_read (int sensor, int reg)
  52. {
  53. dtt_cfg_t *dcp = &dttcfg[sensor >> 1];
  54. uchar data;
  55. if (i2c_read(dcp->i2c_addr, reg, 1, &data, 1) != 0)
  56. return -1;
  57. return (int)data;
  58. } /* dtt_read() */
  59. int
  60. dtt_write (int sensor, int reg, int val)
  61. {
  62. dtt_cfg_t *dcp = &dttcfg[sensor >> 1];
  63. uchar data;
  64. data = (uchar)(val & 0xff);
  65. if (i2c_write(dcp->i2c_addr, reg, 1, &data, 1) != 0)
  66. return 1;
  67. return 0;
  68. } /* dtt_write() */
  69. static int
  70. _dtt_init (int sensor)
  71. {
  72. dtt_cfg_t *dcp = &dttcfg[sensor >> 1];
  73. int reg, val;
  74. if (((sensor & 1) == 0 ? dcp->enable_local : dcp->enable_remote) == 0)
  75. return 1; /* sensor is disabled (or rather ignored) */
  76. /*
  77. * Setup High Limit register
  78. */
  79. if ((sensor & 1) == 0) {
  80. reg = DTT_WRITE_LOC_HIGHLIM;
  81. val = dcp->max_local;
  82. }
  83. else {
  84. reg = DTT_WRITE_REM_HIGHLIM;
  85. val = dcp->max_remote;
  86. }
  87. if (dtt_write (sensor, reg, val) != 0)
  88. return 1;
  89. /*
  90. * Setup Low Limit register
  91. */
  92. if ((sensor & 1) == 0) {
  93. reg = DTT_WRITE_LOC_LOWLIM;
  94. val = dcp->min_local;
  95. }
  96. else {
  97. reg = DTT_WRITE_REM_LOWLIM;
  98. val = dcp->min_remote;
  99. }
  100. if (dtt_write (sensor, reg, val) != 0)
  101. return 1;
  102. /* shouldn't hurt if the rest gets done twice */
  103. /*
  104. * Setup Conversion Rate register
  105. */
  106. if (dtt_write (sensor, DTT_WRITE_CONVRATE, dcp->conv_rate) != 0)
  107. return 1;
  108. /*
  109. * Setup configuraton register
  110. */
  111. val = 0; /* running */
  112. if (dcp->enable_alert == 0)
  113. val |= DTT_CONFIG_ALERT_MASKED; /* mask ALERT pin */
  114. if (dtt_write (sensor, DTT_WRITE_CONFIG, val) != 0)
  115. return 1;
  116. return 0;
  117. } /* _dtt_init() */
  118. int
  119. dtt_init (void)
  120. {
  121. int i;
  122. unsigned char sensors[] = CONFIG_DTT_SENSORS;
  123. const char *const header = "DTT: ";
  124. /* switch to correct I2C bus */
  125. I2C_SET_BUS(CFG_DTT_BUS_NUM);
  126. for (i = 0; i < sizeof(sensors); i++) {
  127. if (_dtt_init(sensors[i]) != 0)
  128. printf ("%s%d FAILED INIT\n", header, i+1);
  129. else
  130. printf ("%s%d is %i C\n", header, i+1,
  131. dtt_get_temp(sensors[i]));
  132. }
  133. return (0);
  134. } /* dtt_init() */
  135. int
  136. dtt_get_temp (int sensor)
  137. {
  138. signed char val;
  139. if ((sensor & 1) == 0)
  140. val = dtt_read(sensor, DTT_READ_LOC_VALUE);
  141. else
  142. val = dtt_read(sensor, DTT_READ_REM_VALUE);
  143. return (int) val;
  144. } /* dtt_get_temp() */
  145. #endif /* CONFIG_DTT_ADM1021 */