wdenk | 6dd652f | 2003-06-19 23:40:20 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2003 |
| 3 | * Murray Jensen, CSIRO-MIT, Murray.Jensen@csiro.au |
| 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 | * Analog Devices's ADM1021 |
| 31 | * "Low Cost Microprocessor System Temperature Monitor" |
| 32 | */ |
| 33 | |
| 34 | #include <common.h> |
| 35 | |
| 36 | #ifdef CONFIG_DTT_ADM1021 |
| 37 | |
| 38 | #include <i2c.h> |
| 39 | #include <dtt.h> |
| 40 | |
| 41 | typedef |
| 42 | struct { |
| 43 | uint i2c_addr:7; /* 7bit i2c chip address */ |
| 44 | uint conv_rate:3; /* conversion rate */ |
| 45 | uint enable_alert:1; /* enable alert output pin */ |
| 46 | uint enable_local:1; /* enable internal temp sensor */ |
| 47 | uint max_local:8; /* internal temp maximum */ |
| 48 | uint min_local:8; /* internal temp minimum */ |
| 49 | uint enable_remote:1; /* enable remote temp sensor */ |
| 50 | uint max_remote:8; /* remote temp maximum */ |
| 51 | uint min_remote:8; /* remote temp minimum */ |
| 52 | } |
| 53 | dtt_cfg_t; |
| 54 | |
| 55 | dtt_cfg_t dttcfg[] = CFG_DTT_ADM1021; |
| 56 | |
| 57 | int |
| 58 | dtt_read (int sensor, int reg) |
| 59 | { |
| 60 | dtt_cfg_t *dcp = &dttcfg[sensor >> 1]; |
| 61 | uchar data; |
| 62 | |
| 63 | if (i2c_read(dcp->i2c_addr, reg, 1, &data, 1) != 0) |
| 64 | return -1; |
| 65 | |
| 66 | return (int)data; |
| 67 | } /* dtt_read() */ |
| 68 | |
| 69 | int |
| 70 | dtt_write (int sensor, int reg, int val) |
| 71 | { |
| 72 | dtt_cfg_t *dcp = &dttcfg[sensor >> 1]; |
| 73 | uchar data; |
| 74 | |
| 75 | data = (uchar)(val & 0xff); |
| 76 | |
| 77 | if (i2c_write(dcp->i2c_addr, reg, 1, &data, 1) != 0) |
| 78 | return 1; |
| 79 | |
| 80 | return 0; |
| 81 | } /* dtt_write() */ |
| 82 | |
| 83 | static int |
| 84 | _dtt_init (int sensor) |
| 85 | { |
| 86 | dtt_cfg_t *dcp = &dttcfg[sensor >> 1]; |
| 87 | int reg, val; |
| 88 | |
| 89 | if (((sensor & 1) == 0 ? dcp->enable_local : dcp->enable_remote) == 0) |
| 90 | return 1; /* sensor is disabled (or rather ignored) */ |
| 91 | |
| 92 | /* |
| 93 | * Setup High Limit register |
| 94 | */ |
| 95 | if ((sensor & 1) == 0) { |
| 96 | reg = DTT_WRITE_LOC_HIGHLIM; |
| 97 | val = dcp->max_local; |
| 98 | } |
| 99 | else { |
| 100 | reg = DTT_WRITE_REM_HIGHLIM; |
| 101 | val = dcp->max_remote; |
| 102 | } |
| 103 | if (dtt_write (sensor, reg, val) != 0) |
| 104 | return 1; |
| 105 | |
| 106 | /* |
| 107 | * Setup Low Limit register |
| 108 | */ |
| 109 | if ((sensor & 1) == 0) { |
| 110 | reg = DTT_WRITE_LOC_LOWLIM; |
| 111 | val = dcp->min_local; |
| 112 | } |
| 113 | else { |
| 114 | reg = DTT_WRITE_REM_LOWLIM; |
| 115 | val = dcp->min_remote; |
| 116 | } |
| 117 | if (dtt_write (sensor, reg, val) != 0) |
| 118 | return 1; |
| 119 | |
| 120 | /* shouldn't hurt if the rest gets done twice */ |
| 121 | |
| 122 | /* |
| 123 | * Setup Conversion Rate register |
| 124 | */ |
| 125 | if (dtt_write (sensor, DTT_WRITE_CONVRATE, dcp->conv_rate) != 0) |
| 126 | return 1; |
| 127 | |
| 128 | /* |
| 129 | * Setup configuraton register |
| 130 | */ |
| 131 | val = 0; /* running */ |
| 132 | if (dcp->enable_alert == 0) |
| 133 | val |= DTT_CONFIG_ALERT_MASKED; /* mask ALERT pin */ |
| 134 | if (dtt_write (sensor, DTT_WRITE_CONFIG, val) != 0) |
| 135 | return 1; |
| 136 | |
| 137 | return 0; |
| 138 | } /* _dtt_init() */ |
| 139 | |
| 140 | int |
| 141 | dtt_init (void) |
| 142 | { |
| 143 | int i; |
| 144 | unsigned char sensors[] = CONFIG_DTT_SENSORS; |
| 145 | const char *const header = "DTT: "; |
| 146 | |
| 147 | for (i = 0; i < sizeof(sensors); i++) { |
| 148 | if (_dtt_init(sensors[i]) != 0) |
| 149 | printf ("%s%d FAILED INIT\n", header, i+1); |
| 150 | else |
| 151 | printf ("%s%d is %i C\n", header, i+1, |
| 152 | dtt_get_temp(sensors[i])); |
| 153 | } |
| 154 | |
| 155 | return (0); |
| 156 | } /* dtt_init() */ |
| 157 | |
| 158 | int |
| 159 | dtt_get_temp (int sensor) |
| 160 | { |
| 161 | signed char val; |
| 162 | |
| 163 | if ((sensor & 1) == 0) |
| 164 | val = dtt_read(sensor, DTT_READ_LOC_VALUE); |
| 165 | else |
| 166 | val = dtt_read(sensor, DTT_READ_REM_VALUE); |
| 167 | |
| 168 | return (int) val; |
| 169 | } /* dtt_get_temp() */ |
| 170 | |
| 171 | #endif /* CONFIG_DTT_ADM1021 */ |