1
2
3
4
5
6
7
8
9
10
11
12#include <common.h>
13#include <i2c.h>
14#include <dtt.h>
15
16
17
18
19#if defined(CONFIG_SYS_I2C_DTT_ADDR)
20#define DTT_I2C_DEV_CODE CONFIG_SYS_I2C_DTT_ADDR
21#else
22#define DTT_I2C_DEV_CODE 0x48
23#endif
24#define DTT_READ_TEMP 0x0
25#define DTT_CONFIG 0x1
26#define DTT_TEMP_HYST 0x2
27#define DTT_TEMP_SET 0x3
28
29int dtt_read(int sensor, int reg)
30{
31 int dlen;
32 uchar data[2];
33
34#ifdef CONFIG_DTT_AD7414
35
36
37
38
39
40
41
42
43
44 dtt_write(sensor, DTT_CONFIG, 0x64);
45#endif
46
47
48 if((reg < 0) || (reg > 3))
49 return -1;
50
51
52 sensor = DTT_I2C_DEV_CODE + (sensor & 0x07);
53
54
55 if ((reg == DTT_READ_TEMP) ||
56 (reg == DTT_TEMP_HYST) ||
57 (reg == DTT_TEMP_SET))
58 dlen = 2;
59 else
60 dlen = 1;
61
62
63 if (i2c_read(sensor, reg, 1, data, dlen) != 0)
64 return -1;
65
66
67 if (dlen == 2)
68 return ((int)((short)data[1] + (((short)data[0]) << 8)));
69
70 return (int)data[0];
71}
72
73
74int dtt_write(int sensor, int reg, int val)
75{
76 int dlen;
77 uchar data[2];
78
79
80 if ((reg < 0) || (reg > 3))
81 return 1;
82
83
84 sensor = DTT_I2C_DEV_CODE + (sensor & 0x07);
85
86
87 if ((reg == DTT_READ_TEMP) ||
88 (reg == DTT_TEMP_HYST) ||
89 (reg == DTT_TEMP_SET)) {
90 dlen = 2;
91 data[0] = (char)((val >> 8) & 0xff);
92 data[1] = (char)(val & 0xff);
93 } else {
94 dlen = 1;
95 data[0] = (char)(val & 0xff);
96 }
97
98
99 if (i2c_write(sensor, reg, 1, data, dlen) != 0)
100 return 1;
101
102 return 0;
103}
104
105
106int dtt_init_one(int sensor)
107{
108 int val;
109
110
111 val = ((CONFIG_SYS_DTT_MAX_TEMP * 2) << 7) & 0xff80;
112 if (dtt_write(sensor, DTT_TEMP_SET, val) != 0)
113 return 1;
114
115
116 val = (((CONFIG_SYS_DTT_MAX_TEMP - CONFIG_SYS_DTT_HYSTERESIS) * 2) << 7) & 0xff80;
117 if (dtt_write(sensor, DTT_TEMP_HYST, val) != 0)
118 return 1;
119
120
121#ifdef CONFIG_DTT_AD7414
122
123 val = 0x60;
124#else
125
126 val = 0x18;
127#endif
128 if (dtt_write(sensor, DTT_CONFIG, val) != 0)
129 return 1;
130
131 return 0;
132}
133
134int dtt_get_temp(int sensor)
135{
136 int const ret = dtt_read(sensor, DTT_READ_TEMP);
137
138 if (ret < 0) {
139 printf("DTT temperature read failed.\n");
140 return 0;
141 }
142 return (int)((int16_t) ret / 256);
143}
144