blob: 34eb80ca23a5024a306009ea3cf29b0171c2052d [file] [log] [blame]
wdenk7a8e9bed2003-05-31 18:35:21 +00001
2#include <common.h>
3
4#include <ssi.h>
5
6#ifdef CONFIG_DS1722
7
8static void ds1722_select(int dev)
9{
10 ssi_set_interface(4096, 0, 0, 0);
11 ssi_chip_select(0);
12 udelay(1);
13 ssi_chip_select(dev);
14 udelay(1);
15}
16
17
18u8 ds1722_read(int dev, int addr)
19{
20 u8 res;
21
22 ds1722_select(dev);
23
24 ssi_tx_byte(addr);
25 res = ssi_rx_byte();
26
27 ssi_chip_select(0);
28
29 return res;
30}
31
32void ds1722_write(int dev, int addr, u8 data)
33{
34 ds1722_select(dev);
35
36 ssi_tx_byte(0x80|addr);
37 ssi_tx_byte(data);
38
39 ssi_chip_select(0);
40}
41
42
43u16 ds1722_temp(int dev, int resolution)
44{
45 static int useconds[] = {
46 75000, 150000, 300000, 600000, 1200000
47 };
48 char temp;
49 u16 res;
50
51
52 /* set up the desired resulotion ... */
53 ds1722_write(dev, 0, 0xe0 | (resolution << 1));
54
55 /* wait while the chip measures the tremperature */
56 udelay(useconds[resolution]);
57
58 res = (temp = ds1722_read(dev, 2)) << 8;
59
60 if (temp < 0) {
61 temp = (16 - (ds1722_read(dev, 1) >> 4)) & 0x0f;
62 } else {
63 temp = (ds1722_read(dev, 1) >> 4);
64 }
65
66 switch (temp) {
67 case 0:
68 /* .0000 */
69 break;
70 case 1:
71 /* .0625 */
72 res |=1;
73 break;
74 case 2:
75 /* .1250 */
76 res |=1;
77 break;
78 case 3:
79 /* .1875 */
80 res |=2;
81 break;
82 case 4:
83 /* .2500 */
84 res |=3;
85 break;
86 case 5:
87 /* .3125 */
88 res |=3;
89 break;
90 case 6:
91 /* .3750 */
92 res |=4;
93 break;
94 case 7:
95 /* .4375 */
96 res |=4;
97 break;
98 case 8:
99 /* .5000 */
100 res |=5;
101 break;
102 case 9:
103 /* .5625 */
104 res |=6;
105 break;
106 case 10:
107 /* .6250 */
108 res |=6;
109 break;
110 case 11:
111 /* .6875 */
112 res |=7;
113 break;
114 case 12:
115 /* .7500 */
116 res |=8;
117 break;
118 case 13:
119 /* .8125 */
120 res |=8;
121 break;
122 case 14:
123 /* .8750 */
124 res |=9;
125 break;
126 case 15:
127 /* .9375 */
128 res |=9;
129 break;
130 }
131 return res;
132
133}
134
135int ds1722_probe(int dev)
136{
137 u16 temp = ds1722_temp(dev, DS1722_RESOLUTION_12BIT);
138 printf("%d.%d deg C\n\n", (char)(temp >> 8), temp & 0xff);
139 return 0;
140}
141
142#endif