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