blob: a46cd4dfb5a03a2d3642863382c6c787dbaaed0f [file] [log] [blame]
wdenkabda5ca2003-05-31 18:35:21 +00001
2#include <common.h>
Graeme Russ0c3f6492009-08-23 12:59:53 +10003#include <asm/ic/ssi.h>
4#include <ds1722.h>
Jean-Christophe PLAGNIOL-VILLARD71dd93a2007-10-19 00:09:05 +02005
wdenkabda5ca2003-05-31 18:35:21 +00006static void ds1722_select(int dev)
7{
8 ssi_set_interface(4096, 0, 0, 0);
9 ssi_chip_select(0);
10 udelay(1);
11 ssi_chip_select(dev);
12 udelay(1);
13}
14
15
16u8 ds1722_read(int dev, int addr)
17{
18 u8 res;
wdenk57b2d802003-06-27 21:31:46 +000019
wdenkabda5ca2003-05-31 18:35:21 +000020 ds1722_select(dev);
wdenk57b2d802003-06-27 21:31:46 +000021
22 ssi_tx_byte(addr);
23 res = ssi_rx_byte();
24
wdenkabda5ca2003-05-31 18:35:21 +000025 ssi_chip_select(0);
wdenk57b2d802003-06-27 21:31:46 +000026
wdenkabda5ca2003-05-31 18:35:21 +000027 return res;
28}
29
30void ds1722_write(int dev, int addr, u8 data)
31{
32 ds1722_select(dev);
wdenk57b2d802003-06-27 21:31:46 +000033
34 ssi_tx_byte(0x80|addr);
35 ssi_tx_byte(data);
36
wdenkabda5ca2003-05-31 18:35:21 +000037 ssi_chip_select(0);
38}
39
40
41u16 ds1722_temp(int dev, int resolution)
42{
43 static int useconds[] = {
wdenk57b2d802003-06-27 21:31:46 +000044 75000, 150000, 300000, 600000, 1200000
wdenkabda5ca2003-05-31 18:35:21 +000045 };
46 char temp;
47 u16 res;
wdenk57b2d802003-06-27 21:31:46 +000048
49
wdenkabda5ca2003-05-31 18:35:21 +000050 /* set up the desired resulotion ... */
51 ds1722_write(dev, 0, 0xe0 | (resolution << 1));
wdenk57b2d802003-06-27 21:31:46 +000052
wdenkabda5ca2003-05-31 18:35:21 +000053 /* wait while the chip measures the tremperature */
wdenk57b2d802003-06-27 21:31:46 +000054 udelay(useconds[resolution]);
55
wdenkabda5ca2003-05-31 18:35:21 +000056 res = (temp = ds1722_read(dev, 2)) << 8;
wdenk57b2d802003-06-27 21:31:46 +000057
58 if (temp < 0) {
wdenkabda5ca2003-05-31 18:35:21 +000059 temp = (16 - (ds1722_read(dev, 1) >> 4)) & 0x0f;
60 } else {
61 temp = (ds1722_read(dev, 1) >> 4);
62 }
wdenk57b2d802003-06-27 21:31:46 +000063
wdenkabda5ca2003-05-31 18:35:21 +000064 switch (temp) {
65 case 0:
66 /* .0000 */
67 break;
68 case 1:
69 /* .0625 */
70 res |=1;
71 break;
72 case 2:
73 /* .1250 */
74 res |=1;
75 break;
76 case 3:
77 /* .1875 */
78 res |=2;
79 break;
80 case 4:
81 /* .2500 */
82 res |=3;
83 break;
84 case 5:
85 /* .3125 */
86 res |=3;
87 break;
88 case 6:
89 /* .3750 */
90 res |=4;
91 break;
92 case 7:
93 /* .4375 */
94 res |=4;
95 break;
96 case 8:
97 /* .5000 */
98 res |=5;
99 break;
100 case 9:
101 /* .5625 */
102 res |=6;
103 break;
104 case 10:
105 /* .6250 */
106 res |=6;
107 break;
108 case 11:
109 /* .6875 */
110 res |=7;
111 break;
112 case 12:
113 /* .7500 */
114 res |=8;
115 break;
116 case 13:
117 /* .8125 */
118 res |=8;
119 break;
120 case 14:
121 /* .8750 */
122 res |=9;
123 break;
124 case 15:
125 /* .9375 */
126 res |=9;
127 break;
128 }
129 return res;
wdenk57b2d802003-06-27 21:31:46 +0000130
wdenkabda5ca2003-05-31 18:35:21 +0000131}
132
133int ds1722_probe(int dev)
134{
135 u16 temp = ds1722_temp(dev, DS1722_RESOLUTION_12BIT);
136 printf("%d.%d deg C\n\n", (char)(temp >> 8), temp & 0xff);
137 return 0;
138}