to make u-boot work for fat32 filesystem
[jz_uboot.git] / dtt / adm1021.c
blob14c38f0a83df2cba55b9fc428066d43e014b1d6b
1 /*
2 * (C) Copyright 2003
3 * Murray Jensen, CSIRO-MIT, Murray.Jensen@csiro.au
5 * based on dtt/lm75.c which is ...
7 * (C) Copyright 2001
8 * Bill Hunter, Wave 7 Optics, williamhunter@mediaone.net
10 * See file CREDITS for list of people who contributed to this
11 * project.
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.
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.
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
30 * Analog Devices's ADM1021
31 * "Low Cost Microprocessor System Temperature Monitor"
34 #include <common.h>
36 #ifdef CONFIG_DTT_ADM1021
38 #include <i2c.h>
39 #include <dtt.h>
41 typedef
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 */
53 dtt_cfg_t;
55 dtt_cfg_t dttcfg[] = CFG_DTT_ADM1021;
57 int
58 dtt_read (int sensor, int reg)
60 dtt_cfg_t *dcp = &dttcfg[sensor >> 1];
61 uchar data;
63 if (i2c_read(dcp->i2c_addr, reg, 1, &data, 1) != 0)
64 return -1;
66 return (int)data;
67 } /* dtt_read() */
69 int
70 dtt_write (int sensor, int reg, int val)
72 dtt_cfg_t *dcp = &dttcfg[sensor >> 1];
73 uchar data;
75 data = (uchar)(val & 0xff);
77 if (i2c_write(dcp->i2c_addr, reg, 1, &data, 1) != 0)
78 return 1;
80 return 0;
81 } /* dtt_write() */
83 static int
84 _dtt_init (int sensor)
86 dtt_cfg_t *dcp = &dttcfg[sensor >> 1];
87 int reg, val;
89 if (((sensor & 1) == 0 ? dcp->enable_local : dcp->enable_remote) == 0)
90 return 1; /* sensor is disabled (or rather ignored) */
93 * Setup High Limit register
95 if ((sensor & 1) == 0) {
96 reg = DTT_WRITE_LOC_HIGHLIM;
97 val = dcp->max_local;
99 else {
100 reg = DTT_WRITE_REM_HIGHLIM;
101 val = dcp->max_remote;
103 if (dtt_write (sensor, reg, val) != 0)
104 return 1;
107 * Setup Low Limit register
109 if ((sensor & 1) == 0) {
110 reg = DTT_WRITE_LOC_LOWLIM;
111 val = dcp->min_local;
113 else {
114 reg = DTT_WRITE_REM_LOWLIM;
115 val = dcp->min_remote;
117 if (dtt_write (sensor, reg, val) != 0)
118 return 1;
120 /* shouldn't hurt if the rest gets done twice */
123 * Setup Conversion Rate register
125 if (dtt_write (sensor, DTT_WRITE_CONVRATE, dcp->conv_rate) != 0)
126 return 1;
129 * Setup configuraton register
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;
137 return 0;
138 } /* _dtt_init() */
141 dtt_init (void)
143 int i;
144 unsigned char sensors[] = CONFIG_DTT_SENSORS;
145 const char *const header = "DTT: ";
147 for (i = 0; i < sizeof(sensors); i++) {
148 if (_dtt_init(sensors[i]) != 0)
149 printf ("%s%d FAILED INIT\n", header, i+1);
150 else
151 printf ("%s%d is %i C\n", header, i+1,
152 dtt_get_temp(sensors[i]));
155 return (0);
156 } /* dtt_init() */
159 dtt_get_temp (int sensor)
161 signed char val;
163 if ((sensor & 1) == 0)
164 val = dtt_read(sensor, DTT_READ_LOC_VALUE);
165 else
166 val = dtt_read(sensor, DTT_READ_REM_VALUE);
168 return (int) val;
169 } /* dtt_get_temp() */
171 #endif /* CONFIG_DTT_ADM1021 */