blob: c46958846c7132c05f5af4d3630188768aedcc30 [file] [log] [blame]
wdenk7a8e9bed2003-05-31 18:35:21 +00001#include <common.h>
Graeme Russ8907b8d2009-08-23 12:59:53 +10002#include <asm/ic/ssi.h>
3#include <ds1722.h>
Jean-Christophe PLAGNIOL-VILLARD58b74b02007-10-19 00:09:05 +02004
wdenk7a8e9bed2003-05-31 18:35:21 +00005static void ds1722_select(int dev)
6{
7 ssi_set_interface(4096, 0, 0, 0);
8 ssi_chip_select(0);
9 udelay(1);
10 ssi_chip_select(dev);
11 udelay(1);
12}
13
14
15u8 ds1722_read(int dev, int addr)
16{
17 u8 res;
wdenk8bde7f72003-06-27 21:31:46 +000018
wdenk7a8e9bed2003-05-31 18:35:21 +000019 ds1722_select(dev);
wdenk8bde7f72003-06-27 21:31:46 +000020
21 ssi_tx_byte(addr);
22 res = ssi_rx_byte();
23
wdenk7a8e9bed2003-05-31 18:35:21 +000024 ssi_chip_select(0);
wdenk8bde7f72003-06-27 21:31:46 +000025
wdenk7a8e9bed2003-05-31 18:35:21 +000026 return res;
27}
28
29void ds1722_write(int dev, int addr, u8 data)
30{
31 ds1722_select(dev);
wdenk8bde7f72003-06-27 21:31:46 +000032
33 ssi_tx_byte(0x80|addr);
34 ssi_tx_byte(data);
35
wdenk7a8e9bed2003-05-31 18:35:21 +000036 ssi_chip_select(0);
37}
38
39
40u16 ds1722_temp(int dev, int resolution)
41{
42 static int useconds[] = {
wdenk8bde7f72003-06-27 21:31:46 +000043 75000, 150000, 300000, 600000, 1200000
wdenk7a8e9bed2003-05-31 18:35:21 +000044 };
45 char temp;
46 u16 res;
wdenk8bde7f72003-06-27 21:31:46 +000047
48
wdenk7a8e9bed2003-05-31 18:35:21 +000049 /* set up the desired resulotion ... */
50 ds1722_write(dev, 0, 0xe0 | (resolution << 1));
wdenk8bde7f72003-06-27 21:31:46 +000051
wdenk7a8e9bed2003-05-31 18:35:21 +000052 /* wait while the chip measures the tremperature */
wdenk8bde7f72003-06-27 21:31:46 +000053 udelay(useconds[resolution]);
54
wdenk7a8e9bed2003-05-31 18:35:21 +000055 res = (temp = ds1722_read(dev, 2)) << 8;
wdenk8bde7f72003-06-27 21:31:46 +000056
57 if (temp < 0) {
wdenk7a8e9bed2003-05-31 18:35:21 +000058 temp = (16 - (ds1722_read(dev, 1) >> 4)) & 0x0f;
59 } else {
60 temp = (ds1722_read(dev, 1) >> 4);
61 }
wdenk8bde7f72003-06-27 21:31:46 +000062
wdenk7a8e9bed2003-05-31 18:35:21 +000063 switch (temp) {
64 case 0:
65 /* .0000 */
66 break;
67 case 1:
68 /* .0625 */
69 res |=1;
70 break;
71 case 2:
72 /* .1250 */
73 res |=1;
74 break;
75 case 3:
76 /* .1875 */
77 res |=2;
78 break;
79 case 4:
80 /* .2500 */
81 res |=3;
82 break;
83 case 5:
84 /* .3125 */
85 res |=3;
86 break;
87 case 6:
88 /* .3750 */
89 res |=4;
90 break;
91 case 7:
92 /* .4375 */
93 res |=4;
94 break;
95 case 8:
96 /* .5000 */
97 res |=5;
98 break;
99 case 9:
100 /* .5625 */
101 res |=6;
102 break;
103 case 10:
104 /* .6250 */
105 res |=6;
106 break;
107 case 11:
108 /* .6875 */
109 res |=7;
110 break;
111 case 12:
112 /* .7500 */
113 res |=8;
114 break;
115 case 13:
116 /* .8125 */
117 res |=8;
118 break;
119 case 14:
120 /* .8750 */
121 res |=9;
122 break;
123 case 15:
124 /* .9375 */
125 res |=9;
126 break;
127 }
128 return res;
wdenk8bde7f72003-06-27 21:31:46 +0000129
wdenk7a8e9bed2003-05-31 18:35:21 +0000130}
131
132int ds1722_probe(int dev)
133{
134 u16 temp = ds1722_temp(dev, DS1722_RESOLUTION_12BIT);
135 printf("%d.%d deg C\n\n", (char)(temp >> 8), temp & 0xff);
136 return 0;
137}