wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 1 | #include <common.h> |
Graeme Russ | 8907b8d | 2009-08-23 12:59:53 +1000 | [diff] [blame] | 2 | #include <asm/ic/ssi.h> |
| 3 | #include <ds1722.h> |
Jean-Christophe PLAGNIOL-VILLARD | 58b74b0 | 2007-10-19 00:09:05 +0200 | [diff] [blame] | 4 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 5 | static 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 | |
| 15 | u8 ds1722_read(int dev, int addr) |
| 16 | { |
| 17 | u8 res; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 18 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 19 | ds1722_select(dev); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 20 | |
| 21 | ssi_tx_byte(addr); |
| 22 | res = ssi_rx_byte(); |
| 23 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 24 | ssi_chip_select(0); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 25 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 26 | return res; |
| 27 | } |
| 28 | |
| 29 | void ds1722_write(int dev, int addr, u8 data) |
| 30 | { |
| 31 | ds1722_select(dev); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 32 | |
| 33 | ssi_tx_byte(0x80|addr); |
| 34 | ssi_tx_byte(data); |
| 35 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 36 | ssi_chip_select(0); |
| 37 | } |
| 38 | |
| 39 | |
| 40 | u16 ds1722_temp(int dev, int resolution) |
| 41 | { |
| 42 | static int useconds[] = { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 43 | 75000, 150000, 300000, 600000, 1200000 |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 44 | }; |
| 45 | char temp; |
| 46 | u16 res; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 47 | |
| 48 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 49 | /* set up the desired resulotion ... */ |
| 50 | ds1722_write(dev, 0, 0xe0 | (resolution << 1)); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 51 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 52 | /* wait while the chip measures the tremperature */ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 53 | udelay(useconds[resolution]); |
| 54 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 55 | res = (temp = ds1722_read(dev, 2)) << 8; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 56 | |
| 57 | if (temp < 0) { |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 58 | temp = (16 - (ds1722_read(dev, 1) >> 4)) & 0x0f; |
| 59 | } else { |
| 60 | temp = (ds1722_read(dev, 1) >> 4); |
| 61 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 62 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 63 | 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; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 129 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | int 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 | } |