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