Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 1 | /* |
Larry Johnson | 7f8b130 | 2008-02-21 13:58:11 -0500 | [diff] [blame^] | 2 | * (C) Copyright 2007-2008 |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 3 | * Larry Johnson, lrj@acm.org |
| 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 | * National Semiconductor LM73 Temperature Sensor |
| 31 | */ |
| 32 | |
| 33 | #include <common.h> |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 34 | #include <i2c.h> |
| 35 | #include <dtt.h> |
| 36 | |
| 37 | /* |
| 38 | * Device code |
| 39 | */ |
| 40 | #define DTT_I2C_DEV_CODE 0x48 /* National Semi's LM73 device */ |
| 41 | |
Larry Johnson | 7f8b130 | 2008-02-21 13:58:11 -0500 | [diff] [blame^] | 42 | int dtt_read(int const sensor, int const reg) |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 43 | { |
| 44 | int dlen; |
Larry Johnson | 7f8b130 | 2008-02-21 13:58:11 -0500 | [diff] [blame^] | 45 | uint8_t data[2]; |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 46 | |
| 47 | /* |
| 48 | * Validate 'reg' param and get register size. |
| 49 | */ |
| 50 | switch (reg) { |
| 51 | case DTT_CONFIG: |
| 52 | case DTT_CONTROL: |
| 53 | dlen = 1; |
| 54 | break; |
| 55 | case DTT_READ_TEMP: |
| 56 | case DTT_TEMP_HIGH: |
| 57 | case DTT_TEMP_LOW: |
| 58 | case DTT_ID: |
| 59 | dlen = 2; |
| 60 | break; |
| 61 | default: |
| 62 | return -1; |
| 63 | } |
| 64 | /* |
Larry Johnson | 7f8b130 | 2008-02-21 13:58:11 -0500 | [diff] [blame^] | 65 | * Try to read the register at the calculated sensor address. |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 66 | */ |
Larry Johnson | 7f8b130 | 2008-02-21 13:58:11 -0500 | [diff] [blame^] | 67 | if (0 != |
| 68 | i2c_read(DTT_I2C_DEV_CODE + (sensor & 0x07), reg, 1, data, dlen)) |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 69 | return -1; |
| 70 | /* |
| 71 | * Handle 2 byte result. |
| 72 | */ |
| 73 | if (2 == dlen) |
Larry Johnson | 7f8b130 | 2008-02-21 13:58:11 -0500 | [diff] [blame^] | 74 | return (int)((unsigned)data[0] << 8 | (unsigned)data[1]); |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 75 | |
| 76 | return (int)data[0]; |
| 77 | } /* dtt_read() */ |
| 78 | |
Larry Johnson | 7f8b130 | 2008-02-21 13:58:11 -0500 | [diff] [blame^] | 79 | int dtt_write(int const sensor, int const reg, int const val) |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 80 | { |
| 81 | int dlen; |
Larry Johnson | 7f8b130 | 2008-02-21 13:58:11 -0500 | [diff] [blame^] | 82 | uint8_t data[2]; |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 83 | |
| 84 | /* |
| 85 | * Validate 'reg' param and handle register size |
| 86 | */ |
| 87 | switch (reg) { |
| 88 | case DTT_CONFIG: |
| 89 | case DTT_CONTROL: |
| 90 | dlen = 1; |
Larry Johnson | 7f8b130 | 2008-02-21 13:58:11 -0500 | [diff] [blame^] | 91 | data[0] = (uint8_t) val; |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 92 | break; |
| 93 | case DTT_TEMP_HIGH: |
| 94 | case DTT_TEMP_LOW: |
| 95 | dlen = 2; |
Larry Johnson | 7f8b130 | 2008-02-21 13:58:11 -0500 | [diff] [blame^] | 96 | data[0] = (uint8_t) (val >> 8); /* MSB first */ |
| 97 | data[1] = (uint8_t) val; |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 98 | break; |
| 99 | default: |
| 100 | return -1; |
| 101 | } |
| 102 | /* |
Larry Johnson | 7f8b130 | 2008-02-21 13:58:11 -0500 | [diff] [blame^] | 103 | * Write value to register at the calculated sensor address. |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 104 | */ |
Larry Johnson | 7f8b130 | 2008-02-21 13:58:11 -0500 | [diff] [blame^] | 105 | return 0 != i2c_write(DTT_I2C_DEV_CODE + (sensor & 0x07), reg, 1, data, |
| 106 | dlen); |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 107 | } /* dtt_write() */ |
| 108 | |
Larry Johnson | 7f8b130 | 2008-02-21 13:58:11 -0500 | [diff] [blame^] | 109 | static int _dtt_init(int const sensor) |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 110 | { |
| 111 | int val; |
| 112 | |
| 113 | /* |
| 114 | * Validate the Identification register |
| 115 | */ |
| 116 | if (0x0190 != dtt_read(sensor, DTT_ID)) |
Larry Johnson | 7f8b130 | 2008-02-21 13:58:11 -0500 | [diff] [blame^] | 117 | return -1; |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 118 | /* |
| 119 | * Setup THIGH (upper-limit) and TLOW (lower-limit) registers |
| 120 | */ |
| 121 | val = CFG_DTT_MAX_TEMP << 7; |
| 122 | if (dtt_write(sensor, DTT_TEMP_HIGH, val)) |
Larry Johnson | 7f8b130 | 2008-02-21 13:58:11 -0500 | [diff] [blame^] | 123 | return -1; |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 124 | |
| 125 | val = CFG_DTT_MIN_TEMP << 7; |
| 126 | if (dtt_write(sensor, DTT_TEMP_LOW, val)) |
Larry Johnson | 7f8b130 | 2008-02-21 13:58:11 -0500 | [diff] [blame^] | 127 | return -1; |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 128 | /* |
| 129 | * Setup configuraton register |
| 130 | */ |
| 131 | /* config = alert active low, disabled, and reset */ |
| 132 | val = 0x64; |
| 133 | if (dtt_write(sensor, DTT_CONFIG, val)) |
Larry Johnson | 7f8b130 | 2008-02-21 13:58:11 -0500 | [diff] [blame^] | 134 | return -1; |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 135 | /* |
| 136 | * Setup control/status register |
| 137 | */ |
| 138 | /* control = temp resolution 0.25C */ |
| 139 | val = 0x00; |
| 140 | if (dtt_write(sensor, DTT_CONTROL, val)) |
Larry Johnson | 7f8b130 | 2008-02-21 13:58:11 -0500 | [diff] [blame^] | 141 | return -1; |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 142 | |
| 143 | dtt_read(sensor, DTT_CONTROL); /* clear temperature flags */ |
| 144 | return 0; |
| 145 | } /* _dtt_init() */ |
| 146 | |
| 147 | int dtt_init(void) |
| 148 | { |
| 149 | int i; |
| 150 | unsigned char sensors[] = CONFIG_DTT_SENSORS; |
| 151 | const char *const header = "DTT: "; |
| 152 | |
| 153 | for (i = 0; i < sizeof(sensors); i++) { |
Larry Johnson | 7f8b130 | 2008-02-21 13:58:11 -0500 | [diff] [blame^] | 154 | if (0 != _dtt_init(sensors[i])) |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 155 | printf("%s%d FAILED INIT\n", header, i + 1); |
| 156 | else |
| 157 | printf("%s%d is %i C\n", header, i + 1, |
| 158 | dtt_get_temp(sensors[i])); |
| 159 | } |
| 160 | return 0; |
| 161 | } /* dtt_init() */ |
| 162 | |
Larry Johnson | 7f8b130 | 2008-02-21 13:58:11 -0500 | [diff] [blame^] | 163 | int dtt_get_temp(int const sensor) |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 164 | { |
Larry Johnson | 7f8b130 | 2008-02-21 13:58:11 -0500 | [diff] [blame^] | 165 | int const ret = dtt_read(sensor, DTT_READ_TEMP); |
| 166 | |
| 167 | if (ret < 0) { |
| 168 | printf("DTT temperature read failed.\n"); |
| 169 | return 0; |
| 170 | } |
| 171 | return (int)((int16_t) ret + 0x0040) >> 7; |
Larry Johnson | 2528338 | 2007-12-27 09:52:17 -0500 | [diff] [blame] | 172 | } /* dtt_get_temp() */ |