1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28#include <common.h>
29#include <i2c.h>
30#include <dtt.h>
31
32
33
34
35#if defined(CONFIG_SYS_I2C_DTT_ADDR)
36#define DTT_I2C_DEV_CODE CONFIG_SYS_I2C_DTT_ADDR
37#else
38#define DTT_I2C_DEV_CODE 0x48
39#endif
40#define DTT_READ_TEMP 0x0
41#define DTT_CONFIG 0x1
42#define DTT_TEMP_HYST 0x2
43#define DTT_TEMP_SET 0x3
44
45int dtt_read(int sensor, int reg)
46{
47 int dlen;
48 uchar data[2];
49
50#ifdef CONFIG_DTT_AD7414
51
52
53
54
55
56
57
58
59
60 dtt_write(sensor, DTT_CONFIG, 0x64);
61#endif
62
63
64 if((reg < 0) || (reg > 3))
65 return -1;
66
67
68 sensor = DTT_I2C_DEV_CODE + (sensor & 0x07);
69
70
71 if ((reg == DTT_READ_TEMP) ||
72 (reg == DTT_TEMP_HYST) ||
73 (reg == DTT_TEMP_SET))
74 dlen = 2;
75 else
76 dlen = 1;
77
78
79 if (i2c_read(sensor, reg, 1, data, dlen) != 0)
80 return -1;
81
82
83 if (dlen == 2)
84 return ((int)((short)data[1] + (((short)data[0]) << 8)));
85
86 return (int)data[0];
87}
88
89
90int dtt_write(int sensor, int reg, int val)
91{
92 int dlen;
93 uchar data[2];
94
95
96 if ((reg < 0) || (reg > 3))
97 return 1;
98
99
100 sensor = DTT_I2C_DEV_CODE + (sensor & 0x07);
101
102
103 if ((reg == DTT_READ_TEMP) ||
104 (reg == DTT_TEMP_HYST) ||
105 (reg == DTT_TEMP_SET)) {
106 dlen = 2;
107 data[0] = (char)((val >> 8) & 0xff);
108 data[1] = (char)(val & 0xff);
109 } else {
110 dlen = 1;
111 data[0] = (char)(val & 0xff);
112 }
113
114
115 if (i2c_write(sensor, reg, 1, data, dlen) != 0)
116 return 1;
117
118 return 0;
119}
120
121
122static int _dtt_init(int sensor)
123{
124 int val;
125
126
127 val = ((CONFIG_SYS_DTT_MAX_TEMP * 2) << 7) & 0xff80;
128 if (dtt_write(sensor, DTT_TEMP_SET, val) != 0)
129 return 1;
130
131
132 val = (((CONFIG_SYS_DTT_MAX_TEMP - CONFIG_SYS_DTT_HYSTERESIS) * 2) << 7) & 0xff80;
133 if (dtt_write(sensor, DTT_TEMP_HYST, val) != 0)
134 return 1;
135
136
137#ifdef CONFIG_DTT_AD7414
138
139 val = 0x60;
140#else
141
142 val = 0x18;
143#endif
144 if (dtt_write(sensor, DTT_CONFIG, val) != 0)
145 return 1;
146
147 return 0;
148}
149
150
151int dtt_init (void)
152{
153 int i;
154 unsigned char sensors[] = CONFIG_DTT_SENSORS;
155 const char *const header = "DTT: ";
156 int old_bus;
157
158
159 old_bus = I2C_GET_BUS();
160 I2C_SET_BUS(CONFIG_SYS_DTT_BUS_NUM);
161
162 for (i = 0; i < sizeof(sensors); i++) {
163 if (_dtt_init(sensors[i]) != 0)
164 printf("%s%d FAILED INIT\n", header, i+1);
165 else
166 printf("%s%d is %i C\n", header, i+1,
167 dtt_get_temp(sensors[i]));
168 }
169
170 I2C_SET_BUS(old_bus);
171
172 return (0);
173}
174
175int dtt_get_temp(int sensor)
176{
177 int const ret = dtt_read(sensor, DTT_READ_TEMP);
178
179 if (ret < 0) {
180 printf("DTT temperature read failed.\n");
181 return 0;
182 }
183 return (int)((int16_t) ret / 256);
184}
185