wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2001 |
| 3 | * Erik Theisen, Wave 7 Optics, etheisen@mindspring.com. |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * Dallas Semiconductor's DS1621 Digital Thermometer and Thermostat. |
| 26 | */ |
| 27 | |
| 28 | #include <common.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 29 | #include <i2c.h> |
| 30 | #include <dtt.h> |
| 31 | |
| 32 | /* |
| 33 | * Device code |
| 34 | */ |
| 35 | #define DTT_I2C_DEV_CODE 0x48 /* Dallas Semi's DS1621 */ |
Michal Simek | 10de36d | 2008-07-11 11:50:53 +0200 | [diff] [blame] | 36 | #define DTT_READ_TEMP 0xAA |
| 37 | #define DTT_READ_COUNTER 0xA8 |
| 38 | #define DTT_READ_SLOPE 0xA9 |
| 39 | #define DTT_WRITE_START_CONV 0xEE |
| 40 | #define DTT_WRITE_STOP_CONV 0x22 |
| 41 | #define DTT_TEMP_HIGH 0xA1 |
| 42 | #define DTT_TEMP_LOW 0xA2 |
| 43 | #define DTT_CONFIG 0xAC |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 44 | |
| 45 | int dtt_read(int sensor, int reg) |
| 46 | { |
| 47 | int dlen; |
| 48 | uchar data[2]; |
| 49 | |
| 50 | /* |
| 51 | * Calculate sensor address and command. |
| 52 | * |
| 53 | */ |
| 54 | sensor = DTT_I2C_DEV_CODE + (sensor & 0x07); /* Calculate addr of ds1621*/ |
| 55 | |
| 56 | /* |
| 57 | * Prepare to handle 2 byte result. |
| 58 | */ |
| 59 | if ((reg == DTT_READ_TEMP) || |
| 60 | (reg == DTT_TEMP_HIGH) || (reg == DTT_TEMP_LOW)) |
| 61 | dlen = 2; |
| 62 | else |
| 63 | dlen = 1; |
| 64 | |
| 65 | /* |
| 66 | * Now try to read the register. |
| 67 | */ |
| 68 | if (i2c_read(sensor, reg, 1, data, dlen) != 0) |
| 69 | return 1; |
| 70 | |
| 71 | /* |
| 72 | * Handle 2 byte result. |
| 73 | */ |
| 74 | if (dlen == 2) |
| 75 | return ((int)((short)data[1] + (((short)data[0]) << 8))); |
| 76 | |
| 77 | return (int)data[0]; |
| 78 | } /* dtt_read() */ |
| 79 | |
| 80 | |
| 81 | int dtt_write(int sensor, int reg, int val) |
| 82 | { |
| 83 | int dlen; |
| 84 | uchar data[2]; |
| 85 | |
| 86 | /* |
| 87 | * Calculate sensor address and register. |
| 88 | * |
| 89 | */ |
wdenk | dccbda0 | 2003-07-14 22:13:32 +0000 | [diff] [blame] | 90 | sensor = DTT_I2C_DEV_CODE + (sensor & 0x07); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 91 | |
| 92 | /* |
| 93 | * Handle various data sizes. |
| 94 | */ |
| 95 | if ((reg == DTT_READ_TEMP) || |
| 96 | (reg == DTT_TEMP_HIGH) || (reg == DTT_TEMP_LOW)) { |
| 97 | dlen = 2; |
| 98 | data[0] = (char)((val >> 8) & 0xff); /* MSB first */ |
| 99 | data[1] = (char)(val & 0xff); |
| 100 | } |
| 101 | else if ((reg == DTT_WRITE_START_CONV) || (reg == DTT_WRITE_STOP_CONV)) { |
| 102 | dlen = 0; |
| 103 | data[0] = (char)0; |
| 104 | data[1] = (char)0; |
| 105 | } |
| 106 | else { |
| 107 | dlen = 1; |
| 108 | data[0] = (char)(val & 0xff); |
| 109 | } |
| 110 | |
| 111 | /* |
| 112 | * Write value to device. |
| 113 | */ |
| 114 | if (i2c_write(sensor, reg, 1, data, dlen) != 0) |
| 115 | return 1; |
| 116 | |
| 117 | return 0; |
| 118 | } /* dtt_write() */ |
| 119 | |
| 120 | |
| 121 | static int _dtt_init(int sensor) |
| 122 | { |
| 123 | int val; |
| 124 | |
| 125 | /* |
| 126 | * Setup High Temp. |
| 127 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 128 | val = ((CONFIG_SYS_DTT_MAX_TEMP * 2) << 7) & 0xff80; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 129 | if (dtt_write(sensor, DTT_TEMP_HIGH, val) != 0) |
| 130 | return 1; |
| 131 | udelay(50000); /* Max 50ms */ |
| 132 | |
| 133 | /* |
| 134 | * Setup Low Temp - hysteresis. |
| 135 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 136 | val = (((CONFIG_SYS_DTT_MAX_TEMP - CONFIG_SYS_DTT_HYSTERESIS) * 2) << 7) & 0xff80; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 137 | if (dtt_write(sensor, DTT_TEMP_LOW, val) != 0) |
| 138 | return 1; |
| 139 | udelay(50000); /* Max 50ms */ |
| 140 | |
| 141 | /* |
| 142 | * Setup configuraton register |
| 143 | * |
| 144 | * Clear THF & TLF, Reserved = 1, Polarity = Active Low, One Shot = YES |
| 145 | * |
| 146 | * We run in polled mode, since there isn't any way to know if this |
| 147 | * lousy device is ready to provide temperature readings on power up. |
| 148 | */ |
| 149 | val = 0x9; |
| 150 | if (dtt_write(sensor, DTT_CONFIG, val) != 0) |
| 151 | return 1; |
| 152 | udelay(50000); /* Max 50ms */ |
| 153 | |
| 154 | return 0; |
| 155 | } /* _dtt_init() */ |
| 156 | |
| 157 | |
| 158 | int dtt_init (void) |
| 159 | { |
| 160 | int i; |
| 161 | unsigned char sensors[] = CONFIG_DTT_SENSORS; |
| 162 | |
| 163 | for (i = 0; i < sizeof(sensors); i++) { |
| 164 | if (_dtt_init(sensors[i]) != 0) |
| 165 | printf("DTT%d: FAILED\n", i+1); |
| 166 | else |
| 167 | printf("DTT%d: %i C\n", i+1, dtt_get_temp(sensors[i])); |
| 168 | } |
| 169 | |
| 170 | return (0); |
| 171 | } /* dtt_init() */ |
| 172 | |
| 173 | |
| 174 | int dtt_get_temp(int sensor) |
| 175 | { |
| 176 | int i; |
| 177 | |
| 178 | /* |
| 179 | * Start a conversion, may take up to 1 second. |
| 180 | */ |
| 181 | dtt_write(sensor, DTT_WRITE_START_CONV, 0); |
| 182 | for (i = 0; i <= 10; i++) { |
| 183 | udelay(100000); |
| 184 | if (dtt_read(sensor, DTT_CONFIG) & 0x80) |
| 185 | break; |
| 186 | } |
| 187 | |
| 188 | return (dtt_read(sensor, DTT_READ_TEMP) / 256); |
| 189 | } /* dtt_get_temp() */ |