wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2001 |
| 3 | * Bill Hunter, Wave 7 Optics, williamhunter@mediaone.net |
| 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 | * On Semiconductor's LM75 Temperature Sensor |
| 26 | */ |
| 27 | |
| 28 | #include <common.h> |
| 29 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 30 | #if !defined(CFG_EEPROM_PAGE_WRITE_ENABLE) || \ |
| 31 | (CFG_EEPROM_PAGE_WRITE_BITS < 1) |
| 32 | # error "CFG_EEPROM_PAGE_WRITE_ENABLE must be defined and CFG_EEPROM_PAGE_WRITE_BITS must be greater than 1 to use CONFIG_DTT_LM75" |
| 33 | #endif |
| 34 | |
| 35 | #include <i2c.h> |
| 36 | #include <dtt.h> |
| 37 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 38 | /* |
| 39 | * Device code |
| 40 | */ |
Victor Gallardo | 31856dd | 2008-09-09 15:13:29 -0700 | [diff] [blame] | 41 | #if defined(CFG_I2C_DTT_ADDR) |
| 42 | #define DTT_I2C_DEV_CODE CFG_I2C_DTT_ADDR |
| 43 | #else |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 44 | #define DTT_I2C_DEV_CODE 0x48 /* ON Semi's LM75 device */ |
Victor Gallardo | 31856dd | 2008-09-09 15:13:29 -0700 | [diff] [blame] | 45 | #endif |
Michal Simek | 10de36d | 2008-07-11 11:50:53 +0200 | [diff] [blame] | 46 | #define DTT_READ_TEMP 0x0 |
| 47 | #define DTT_CONFIG 0x1 |
| 48 | #define DTT_TEMP_HYST 0x2 |
| 49 | #define DTT_TEMP_SET 0x3 |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 50 | |
| 51 | int dtt_read(int sensor, int reg) |
| 52 | { |
| 53 | int dlen; |
| 54 | uchar data[2]; |
| 55 | |
Stefan Roese | 84db66c | 2008-05-16 11:06:06 +0200 | [diff] [blame] | 56 | #ifdef CONFIG_DTT_AD7414 |
| 57 | /* |
| 58 | * On AD7414 the first value upon bootup is not read correctly. |
| 59 | * This is most likely because of the 800ms update time of the |
| 60 | * temp register in normal update mode. To get current values |
| 61 | * each time we issue the "dtt" command including upon powerup |
| 62 | * we switch into one-short mode. |
| 63 | * |
| 64 | * Issue one-shot mode command |
| 65 | */ |
| 66 | dtt_write(sensor, DTT_CONFIG, 0x64); |
| 67 | #endif |
| 68 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 69 | /* |
| 70 | * Validate 'reg' param |
| 71 | */ |
| 72 | if((reg < 0) || (reg > 3)) |
| 73 | return -1; |
| 74 | |
| 75 | /* |
| 76 | * Calculate sensor address and register. |
| 77 | */ |
| 78 | sensor = DTT_I2C_DEV_CODE + (sensor & 0x07); /* calculate address of lm75 */ |
| 79 | |
| 80 | /* |
| 81 | * Prepare to handle 2 byte result. |
| 82 | */ |
| 83 | if ((reg == DTT_READ_TEMP) || |
| 84 | (reg == DTT_TEMP_HYST) || |
| 85 | (reg == DTT_TEMP_SET)) |
| 86 | dlen = 2; |
| 87 | else |
| 88 | dlen = 1; |
| 89 | |
| 90 | /* |
| 91 | * Now try to read the register. |
| 92 | */ |
| 93 | if (i2c_read(sensor, reg, 1, data, dlen) != 0) |
| 94 | return -1; |
| 95 | |
| 96 | /* |
| 97 | * Handle 2 byte result. |
| 98 | */ |
| 99 | if (dlen == 2) |
| 100 | return ((int)((short)data[1] + (((short)data[0]) << 8))); |
| 101 | |
| 102 | |
| 103 | return (int)data[0]; |
| 104 | } /* dtt_read() */ |
| 105 | |
| 106 | |
| 107 | int dtt_write(int sensor, int reg, int val) |
| 108 | { |
| 109 | int dlen; |
| 110 | uchar data[2]; |
| 111 | |
| 112 | /* |
| 113 | * Validate 'reg' param |
| 114 | */ |
| 115 | if ((reg < 0) || (reg > 3)) |
| 116 | return 1; |
| 117 | |
| 118 | /* |
| 119 | * Calculate sensor address and register. |
| 120 | */ |
| 121 | sensor = DTT_I2C_DEV_CODE + (sensor & 0x07); /* calculate address of lm75 */ |
| 122 | |
| 123 | /* |
| 124 | * Handle 2 byte values. |
| 125 | */ |
| 126 | if ((reg == DTT_READ_TEMP) || |
| 127 | (reg == DTT_TEMP_HYST) || |
| 128 | (reg == DTT_TEMP_SET)) { |
| 129 | dlen = 2; |
| 130 | data[0] = (char)((val >> 8) & 0xff); /* MSB first */ |
| 131 | data[1] = (char)(val & 0xff); |
| 132 | } else { |
| 133 | dlen = 1; |
| 134 | data[0] = (char)(val & 0xff); |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | * Write value to register. |
| 139 | */ |
| 140 | if (i2c_write(sensor, reg, 1, data, dlen) != 0) |
| 141 | return 1; |
| 142 | |
| 143 | return 0; |
| 144 | } /* dtt_write() */ |
| 145 | |
| 146 | |
| 147 | static int _dtt_init(int sensor) |
| 148 | { |
| 149 | int val; |
| 150 | |
| 151 | /* |
| 152 | * Setup TSET ( trip point ) register |
| 153 | */ |
| 154 | val = ((CFG_DTT_MAX_TEMP * 2) << 7) & 0xff80; /* trip */ |
| 155 | if (dtt_write(sensor, DTT_TEMP_SET, val) != 0) |
| 156 | return 1; |
| 157 | |
| 158 | /* |
| 159 | * Setup THYST ( untrip point ) register - Hysteresis |
| 160 | */ |
| 161 | val = (((CFG_DTT_MAX_TEMP - CFG_DTT_HYSTERESIS) * 2) << 7) & 0xff80; |
| 162 | if (dtt_write(sensor, DTT_TEMP_HYST, val) != 0) |
| 163 | return 1; |
| 164 | |
| 165 | /* |
| 166 | * Setup configuraton register |
| 167 | */ |
Stefan Roese | 42fbddd | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 168 | #ifdef CONFIG_DTT_AD7414 |
| 169 | /* config = alert active low and disabled */ |
| 170 | val = 0x60; |
| 171 | #else |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 172 | /* config = 6 sample integration, int mode, active low, and enable */ |
| 173 | val = 0x18; |
Stefan Roese | 42fbddd | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 174 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 175 | if (dtt_write(sensor, DTT_CONFIG, val) != 0) |
| 176 | return 1; |
| 177 | |
| 178 | return 0; |
| 179 | } /* _dtt_init() */ |
| 180 | |
| 181 | |
| 182 | int dtt_init (void) |
| 183 | { |
| 184 | int i; |
| 185 | unsigned char sensors[] = CONFIG_DTT_SENSORS; |
| 186 | const char *const header = "DTT: "; |
| 187 | |
| 188 | for (i = 0; i < sizeof(sensors); i++) { |
| 189 | if (_dtt_init(sensors[i]) != 0) |
| 190 | printf("%s%d FAILED INIT\n", header, i+1); |
| 191 | else |
| 192 | printf("%s%d is %i C\n", header, i+1, |
| 193 | dtt_get_temp(sensors[i])); |
| 194 | } |
| 195 | |
| 196 | return (0); |
| 197 | } /* dtt_init() */ |
| 198 | |
| 199 | int dtt_get_temp(int sensor) |
| 200 | { |
Larry Johnson | 8f9b6d2 | 2008-02-21 13:58:16 -0500 | [diff] [blame] | 201 | int const ret = dtt_read(sensor, DTT_READ_TEMP); |
| 202 | |
| 203 | if (ret < 0) { |
| 204 | printf("DTT temperature read failed.\n"); |
| 205 | return 0; |
| 206 | } |
| 207 | return (int)((int16_t) ret / 256); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 208 | } /* dtt_get_temp() */ |