Heiko Schocher | acb4f4a | 2006-12-21 16:14:48 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2006 |
| 3 | * Heiko Schocher, DENX Software Enginnering <hs@denx.de> |
| 4 | * |
| 5 | * based on dtt/lm75.c which is ... |
| 6 | * |
| 7 | * (C) Copyright 2001 |
| 8 | * Bill Hunter, Wave 7 Optics, williamhunter@mediaone.net |
| 9 | * |
| 10 | * See file CREDITS for list of people who contributed to this |
| 11 | * project. |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or |
| 14 | * modify it under the terms of the GNU General Public License as |
| 15 | * published by the Free Software Foundation; either version 2 of |
| 16 | * the License, or (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License |
| 24 | * along with this program; if not, write to the Free Software |
| 25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 26 | * MA 02111-1307 USA |
| 27 | */ |
| 28 | |
| 29 | /* |
| 30 | * On Semiconductor's LM81 Temperature Sensor |
| 31 | */ |
| 32 | |
| 33 | #include <common.h> |
| 34 | |
| 35 | #ifdef CONFIG_DTT_LM81 |
| 36 | #if !defined(CFG_EEPROM_PAGE_WRITE_ENABLE) || \ |
| 37 | (CFG_EEPROM_PAGE_WRITE_BITS < 1) |
| 38 | # error "CFG_EEPROM_PAGE_WRITE_ENABLE must be defined and CFG_EEPROM_PAGE_WRITE_BITS must be greater than 1 to use CONFIG_DTT_LM81" |
| 39 | #endif |
| 40 | |
| 41 | #include <i2c.h> |
| 42 | #include <dtt.h> |
| 43 | |
| 44 | /* |
| 45 | * Device code |
| 46 | */ |
| 47 | #define DTT_I2C_DEV_CODE 0x2c /* ON Semi's LM81 device */ |
| 48 | |
| 49 | int dtt_read(int sensor, int reg) |
| 50 | { |
| 51 | int dlen = 1; |
| 52 | uchar data[2]; |
| 53 | |
| 54 | /* |
| 55 | * Calculate sensor address and register. |
| 56 | */ |
| 57 | sensor = DTT_I2C_DEV_CODE + (sensor & 0x03); /* calculate address of lm81 */ |
| 58 | |
| 59 | /* |
| 60 | * Now try to read the register. |
| 61 | */ |
| 62 | if (i2c_read(sensor, reg, 1, data, dlen) != 0) |
| 63 | return -1; |
| 64 | |
| 65 | return (int)data[0]; |
| 66 | } /* dtt_read() */ |
| 67 | |
| 68 | |
| 69 | int dtt_write(int sensor, int reg, int val) |
| 70 | { |
| 71 | uchar data; |
| 72 | |
| 73 | /* |
| 74 | * Calculate sensor address and register. |
| 75 | */ |
| 76 | sensor = DTT_I2C_DEV_CODE + (sensor & 0x03); /* calculate address of lm81 */ |
| 77 | |
| 78 | data = (char)(val & 0xff); |
| 79 | |
| 80 | /* |
| 81 | * Write value to register. |
| 82 | */ |
Heiko Schocher | 6f2798d | 2007-01-14 13:35:31 +0100 | [diff] [blame] | 83 | if (i2c_write(sensor, reg, 1, &data, 1) != 0) |
Heiko Schocher | acb4f4a | 2006-12-21 16:14:48 +0100 | [diff] [blame] | 84 | return 1; |
| 85 | |
| 86 | return 0; |
| 87 | } /* dtt_write() */ |
| 88 | |
| 89 | #define DTT_MANU 0x3e |
| 90 | #define DTT_REV 0x3f |
Heiko Schocher | 6f2798d | 2007-01-14 13:35:31 +0100 | [diff] [blame] | 91 | #define DTT_CONFIG 0x40 |
Heiko Schocher | acb4f4a | 2006-12-21 16:14:48 +0100 | [diff] [blame] | 92 | #define DTT_ADR 0x48 |
| 93 | |
| 94 | static int _dtt_init(int sensor) |
| 95 | { |
| 96 | int man; |
| 97 | int adr; |
| 98 | int rev; |
| 99 | |
| 100 | if (dtt_write (sensor, DTT_CONFIG, 0x01) < 0) |
| 101 | return 1; |
Heiko Schocher | 6f2798d | 2007-01-14 13:35:31 +0100 | [diff] [blame] | 102 | /* The LM81 needs 400ms to get the correct values ... */ |
| 103 | udelay (400000); |
Heiko Schocher | acb4f4a | 2006-12-21 16:14:48 +0100 | [diff] [blame] | 104 | man = dtt_read (sensor, DTT_MANU); |
| 105 | if (man != 0x01) |
| 106 | return 1; |
| 107 | adr = dtt_read (sensor, DTT_ADR); |
| 108 | if (adr < 0) |
| 109 | return 1; |
| 110 | rev = dtt_read (sensor, DTT_REV); |
| 111 | if (adr < 0) |
| 112 | return 1; |
| 113 | |
| 114 | printf ("DTT: Found LM81@%x Rev: %d\n", adr, rev); |
Heiko Schocher | acb4f4a | 2006-12-21 16:14:48 +0100 | [diff] [blame] | 115 | return 0; |
| 116 | } /* _dtt_init() */ |
| 117 | |
| 118 | |
| 119 | int dtt_init (void) |
| 120 | { |
| 121 | int i; |
| 122 | unsigned char sensors[] = CONFIG_DTT_SENSORS; |
| 123 | const char *const header = "DTT: "; |
| 124 | |
| 125 | for (i = 0; i < sizeof(sensors); i++) { |
| 126 | if (_dtt_init(sensors[i]) != 0) |
| 127 | printf("%s%d FAILED INIT\n", header, i+1); |
| 128 | else |
| 129 | printf("%s%d is %i C\n", header, i+1, |
| 130 | dtt_get_temp(sensors[i])); |
| 131 | } |
| 132 | |
| 133 | return (0); |
| 134 | } /* dtt_init() */ |
| 135 | |
| 136 | #define TEMP_FROM_REG(temp) \ |
Wolfgang Denk | 8637071 | 2007-01-15 13:41:04 +0100 | [diff] [blame] | 137 | ((temp)<256?((((temp)&0x1fe) >> 1) * 10) + ((temp) & 1) * 5: \ |
| 138 | ((((temp)&0x1fe) >> 1) -255) * 10 - ((temp) & 1) * 5) \ |
Heiko Schocher | acb4f4a | 2006-12-21 16:14:48 +0100 | [diff] [blame] | 139 | |
| 140 | int dtt_get_temp(int sensor) |
| 141 | { |
| 142 | int val = dtt_read (sensor, DTT_READ_TEMP); |
| 143 | int tmpcnf = dtt_read (sensor, DTT_CONFIG_TEMP); |
| 144 | |
| 145 | return (TEMP_FROM_REG((val << 1) + ((tmpcnf & 0x80) >> 7))) / 10; |
| 146 | } /* dtt_get_temp() */ |
| 147 | |
| 148 | #endif /* CONFIG_DTT_LM81 */ |