Added support for the Hynix HY27US08121A 64MB Flash chip.
[u-boot-openmoko/mini2440.git] / drivers / hwmon / lm75.c
blobe29b29440f237aa537321bc388abce9c86014822
1 /*
2 * (C) Copyright 2001
3 * Bill Hunter, Wave 7 Optics, williamhunter@mediaone.net
5 * See file CREDITS for list of people who contributed to this
6 * project.
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.
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.
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
25 * On Semiconductor's LM75 Temperature Sensor
28 #include <common.h>
30 #ifdef CONFIG_DTT_LM75
31 #if !defined(CFG_EEPROM_PAGE_WRITE_ENABLE) || \
32 (CFG_EEPROM_PAGE_WRITE_BITS < 1)
33 # 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"
34 #endif
36 #include <i2c.h>
37 #include <dtt.h>
41 * Device code
43 #define DTT_I2C_DEV_CODE 0x48 /* ON Semi's LM75 device */
45 int dtt_read(int sensor, int reg)
47 int dlen;
48 uchar data[2];
51 * Validate 'reg' param
53 if((reg < 0) || (reg > 3))
54 return -1;
57 * Calculate sensor address and register.
59 sensor = DTT_I2C_DEV_CODE + (sensor & 0x07); /* calculate address of lm75 */
62 * Prepare to handle 2 byte result.
64 if ((reg == DTT_READ_TEMP) ||
65 (reg == DTT_TEMP_HYST) ||
66 (reg == DTT_TEMP_SET))
67 dlen = 2;
68 else
69 dlen = 1;
72 * Now try to read the register.
74 if (i2c_read(sensor, reg, 1, data, dlen) != 0)
75 return -1;
78 * Handle 2 byte result.
80 if (dlen == 2)
81 return ((int)((short)data[1] + (((short)data[0]) << 8)));
84 return (int)data[0];
85 } /* dtt_read() */
88 int dtt_write(int sensor, int reg, int val)
90 int dlen;
91 uchar data[2];
94 * Validate 'reg' param
96 if ((reg < 0) || (reg > 3))
97 return 1;
100 * Calculate sensor address and register.
102 sensor = DTT_I2C_DEV_CODE + (sensor & 0x07); /* calculate address of lm75 */
105 * Handle 2 byte values.
107 if ((reg == DTT_READ_TEMP) ||
108 (reg == DTT_TEMP_HYST) ||
109 (reg == DTT_TEMP_SET)) {
110 dlen = 2;
111 data[0] = (char)((val >> 8) & 0xff); /* MSB first */
112 data[1] = (char)(val & 0xff);
113 } else {
114 dlen = 1;
115 data[0] = (char)(val & 0xff);
119 * Write value to register.
121 if (i2c_write(sensor, reg, 1, data, dlen) != 0)
122 return 1;
124 return 0;
125 } /* dtt_write() */
128 static int _dtt_init(int sensor)
130 int val;
133 * Setup TSET ( trip point ) register
135 val = ((CFG_DTT_MAX_TEMP * 2) << 7) & 0xff80; /* trip */
136 if (dtt_write(sensor, DTT_TEMP_SET, val) != 0)
137 return 1;
140 * Setup THYST ( untrip point ) register - Hysteresis
142 val = (((CFG_DTT_MAX_TEMP - CFG_DTT_HYSTERESIS) * 2) << 7) & 0xff80;
143 if (dtt_write(sensor, DTT_TEMP_HYST, val) != 0)
144 return 1;
147 * Setup configuraton register
149 #ifdef CONFIG_DTT_AD7414
150 /* config = alert active low and disabled */
151 val = 0x60;
152 #else
153 /* config = 6 sample integration, int mode, active low, and enable */
154 val = 0x18;
155 #endif
156 if (dtt_write(sensor, DTT_CONFIG, val) != 0)
157 return 1;
159 return 0;
160 } /* _dtt_init() */
163 int dtt_init (void)
165 int i;
166 unsigned char sensors[] = CONFIG_DTT_SENSORS;
167 const char *const header = "DTT: ";
169 for (i = 0; i < sizeof(sensors); i++) {
170 if (_dtt_init(sensors[i]) != 0)
171 printf("%s%d FAILED INIT\n", header, i+1);
172 else
173 printf("%s%d is %i C\n", header, i+1,
174 dtt_get_temp(sensors[i]));
177 return (0);
178 } /* dtt_init() */
180 int dtt_get_temp(int sensor)
182 int const ret = dtt_read(sensor, DTT_READ_TEMP);
184 if (ret < 0) {
185 printf("DTT temperature read failed.\n");
186 return 0;
188 return (int)((int16_t) ret / 256);
189 } /* dtt_get_temp() */
191 #endif /* CONFIG_DTT_LM75 */