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