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