blob: 227d8169a5b36b0773f7ac1c3ddb1a1ca110ca87 [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;
wdenk8bde7f72003-06-27 21:31:46 +000021
wdenk7a8e9bed2003-05-31 18:35:21 +000022 ds1722_select(dev);
wdenk8bde7f72003-06-27 21:31:46 +000023
24 ssi_tx_byte(addr);
25 res = ssi_rx_byte();
26
wdenk7a8e9bed2003-05-31 18:35:21 +000027 ssi_chip_select(0);
wdenk8bde7f72003-06-27 21:31:46 +000028
wdenk7a8e9bed2003-05-31 18:35:21 +000029 return res;
30}
31
32void ds1722_write(int dev, int addr, u8 data)
33{
34 ds1722_select(dev);
wdenk8bde7f72003-06-27 21:31:46 +000035
36 ssi_tx_byte(0x80|addr);
37 ssi_tx_byte(data);
38
wdenk7a8e9bed2003-05-31 18:35:21 +000039 ssi_chip_select(0);
40}
41
42
43u16 ds1722_temp(int dev, int resolution)
44{
45 static int useconds[] = {
wdenk8bde7f72003-06-27 21:31:46 +000046 75000, 150000, 300000, 600000, 1200000
wdenk7a8e9bed2003-05-31 18:35:21 +000047 };
48 char temp;
49 u16 res;
wdenk8bde7f72003-06-27 21:31:46 +000050
51
wdenk7a8e9bed2003-05-31 18:35:21 +000052 /* set up the desired resulotion ... */
53 ds1722_write(dev, 0, 0xe0 | (resolution << 1));
wdenk8bde7f72003-06-27 21:31:46 +000054
wdenk7a8e9bed2003-05-31 18:35:21 +000055 /* wait while the chip measures the tremperature */
wdenk8bde7f72003-06-27 21:31:46 +000056 udelay(useconds[resolution]);
57
wdenk7a8e9bed2003-05-31 18:35:21 +000058 res = (temp = ds1722_read(dev, 2)) << 8;
wdenk8bde7f72003-06-27 21:31:46 +000059
60 if (temp < 0) {
wdenk7a8e9bed2003-05-31 18:35:21 +000061 temp = (16 - (ds1722_read(dev, 1) >> 4)) & 0x0f;
62 } else {
63 temp = (ds1722_read(dev, 1) >> 4);
64 }
wdenk8bde7f72003-06-27 21:31:46 +000065
wdenk7a8e9bed2003-05-31 18:35:21 +000066 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;
wdenk8bde7f72003-06-27 21:31:46 +0000132
wdenk7a8e9bed2003-05-31 18:35:21 +0000133}
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