1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <common.h>
19
20#include <i2c.h>
21#include <dtt.h>
22
23#define DTT_READ_LOC_VALUE 0x00
24#define DTT_READ_REM_VALUE 0x01
25#define DTT_READ_STATUS 0x02
26#define DTT_READ_CONFIG 0x03
27#define DTT_READ_CONVRATE 0x04
28#define DTT_READ_LOC_HIGHLIM 0x05
29#define DTT_READ_LOC_LOWLIM 0x06
30#define DTT_READ_REM_HIGHLIM 0x07
31#define DTT_READ_REM_LOWLIM 0x08
32#define DTT_READ_DEVID 0xfe
33
34#define DTT_WRITE_CONFIG 0x09
35#define DTT_WRITE_CONVRATE 0x0a
36#define DTT_WRITE_LOC_HIGHLIM 0x0b
37#define DTT_WRITE_LOC_LOWLIM 0x0c
38#define DTT_WRITE_REM_HIGHLIM 0x0d
39#define DTT_WRITE_REM_LOWLIM 0x0e
40#define DTT_WRITE_ONESHOT 0x0f
41
42#define DTT_STATUS_BUSY 0x80
43#define DTT_STATUS_LHIGH 0x40
44#define DTT_STATUS_LLOW 0x20
45#define DTT_STATUS_RHIGH 0x10
46#define DTT_STATUS_RLOW 0x08
47#define DTT_STATUS_OPEN 0x04
48
49#define DTT_CONFIG_ALERT_MASKED 0x80
50#define DTT_CONFIG_STANDBY 0x40
51
52#define DTT_ADM1021_DEVID 0x41
53
54typedef
55 struct {
56 uint i2c_addr:7;
57 uint conv_rate:3;
58 uint enable_alert:1;
59 uint enable_local:1;
60 uint max_local:8;
61 uint min_local:8;
62 uint enable_remote:1;
63 uint max_remote:8;
64 uint min_remote:8;
65 }
66dtt_cfg_t;
67
68dtt_cfg_t dttcfg[] = CONFIG_SYS_DTT_ADM1021;
69
70int
71dtt_read (int sensor, int reg)
72{
73 dtt_cfg_t *dcp = &dttcfg[sensor >> 1];
74 uchar data;
75
76 if (i2c_read(dcp->i2c_addr, reg, 1, &data, 1) != 0)
77 return -1;
78
79 return (int)data;
80}
81
82int
83dtt_write (int sensor, int reg, int val)
84{
85 dtt_cfg_t *dcp = &dttcfg[sensor >> 1];
86 uchar data;
87
88 data = (uchar)(val & 0xff);
89
90 if (i2c_write(dcp->i2c_addr, reg, 1, &data, 1) != 0)
91 return 1;
92
93 return 0;
94}
95
96int
97dtt_init_one(int sensor)
98{
99 dtt_cfg_t *dcp = &dttcfg[sensor >> 1];
100 int reg, val;
101
102 if (((sensor & 1) == 0 ? dcp->enable_local : dcp->enable_remote) == 0)
103 return 1;
104
105
106
107
108 if ((sensor & 1) == 0) {
109 reg = DTT_WRITE_LOC_HIGHLIM;
110 val = dcp->max_local;
111 }
112 else {
113 reg = DTT_WRITE_REM_HIGHLIM;
114 val = dcp->max_remote;
115 }
116 if (dtt_write (sensor, reg, val) != 0)
117 return 1;
118
119
120
121
122 if ((sensor & 1) == 0) {
123 reg = DTT_WRITE_LOC_LOWLIM;
124 val = dcp->min_local;
125 }
126 else {
127 reg = DTT_WRITE_REM_LOWLIM;
128 val = dcp->min_remote;
129 }
130 if (dtt_write (sensor, reg, val) != 0)
131 return 1;
132
133
134
135
136
137
138 if (dtt_write (sensor, DTT_WRITE_CONVRATE, dcp->conv_rate) != 0)
139 return 1;
140
141
142
143
144 val = 0;
145 if (dcp->enable_alert == 0)
146 val |= DTT_CONFIG_ALERT_MASKED;
147 if (dtt_write (sensor, DTT_WRITE_CONFIG, val) != 0)
148 return 1;
149
150 return 0;
151}
152
153int
154dtt_get_temp (int sensor)
155{
156 signed char val;
157
158 if ((sensor & 1) == 0)
159 val = dtt_read(sensor, DTT_READ_LOC_VALUE);
160 else
161 val = dtt_read(sensor, DTT_READ_REM_VALUE);
162
163 return (int) val;
164}
165