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