blob: 9349eb6ff685e71f1b341a4b44cb1db13a32bd53 [file] [log] [blame]
Heiko Schocher6dedf3d2006-12-21 16:14:48 +01001/*
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
Heiko Schocher6dedf3d2006-12-21 16:14:48 +010035#if !defined(CFG_EEPROM_PAGE_WRITE_ENABLE) || \
36 (CFG_EEPROM_PAGE_WRITE_BITS < 1)
37# 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"
38#endif
39
40#include <i2c.h>
41#include <dtt.h>
42
43/*
44 * Device code
45 */
46#define DTT_I2C_DEV_CODE 0x2c /* ON Semi's LM81 device */
Michal Simek6ecbb452008-07-11 11:50:53 +020047#define DTT_READ_TEMP 0x27
48#define DTT_CONFIG_TEMP 0x4b
49#define DTT_TEMP_MAX 0x39
50#define DTT_TEMP_HYST 0x3a
51#define DTT_CONFIG 0x40
Heiko Schocher6dedf3d2006-12-21 16:14:48 +010052
53int dtt_read(int sensor, int reg)
54{
55 int dlen = 1;
56 uchar data[2];
57
58 /*
59 * Calculate sensor address and register.
60 */
61 sensor = DTT_I2C_DEV_CODE + (sensor & 0x03); /* calculate address of lm81 */
62
63 /*
64 * Now try to read the register.
65 */
66 if (i2c_read(sensor, reg, 1, data, dlen) != 0)
67 return -1;
68
69 return (int)data[0];
70} /* dtt_read() */
71
72
73int dtt_write(int sensor, int reg, int val)
74{
75 uchar data;
76
77 /*
78 * Calculate sensor address and register.
79 */
80 sensor = DTT_I2C_DEV_CODE + (sensor & 0x03); /* calculate address of lm81 */
81
82 data = (char)(val & 0xff);
83
84 /*
85 * Write value to register.
86 */
Heiko Schochera443d312007-01-14 13:35:31 +010087 if (i2c_write(sensor, reg, 1, &data, 1) != 0)
Heiko Schocher6dedf3d2006-12-21 16:14:48 +010088 return 1;
89
90 return 0;
91} /* dtt_write() */
92
93#define DTT_MANU 0x3e
94#define DTT_REV 0x3f
Heiko Schochera443d312007-01-14 13:35:31 +010095#define DTT_CONFIG 0x40
Heiko Schocher6dedf3d2006-12-21 16:14:48 +010096#define DTT_ADR 0x48
97
98static int _dtt_init(int sensor)
99{
100 int man;
101 int adr;
102 int rev;
103
104 if (dtt_write (sensor, DTT_CONFIG, 0x01) < 0)
105 return 1;
Heiko Schochera443d312007-01-14 13:35:31 +0100106 /* The LM81 needs 400ms to get the correct values ... */
107 udelay (400000);
Heiko Schocher6dedf3d2006-12-21 16:14:48 +0100108 man = dtt_read (sensor, DTT_MANU);
109 if (man != 0x01)
110 return 1;
111 adr = dtt_read (sensor, DTT_ADR);
112 if (adr < 0)
113 return 1;
114 rev = dtt_read (sensor, DTT_REV);
115 if (adr < 0)
116 return 1;
117
118 printf ("DTT: Found LM81@%x Rev: %d\n", adr, rev);
Heiko Schocher6dedf3d2006-12-21 16:14:48 +0100119 return 0;
120} /* _dtt_init() */
121
122
123int dtt_init (void)
124{
125 int i;
126 unsigned char sensors[] = CONFIG_DTT_SENSORS;
127 const char *const header = "DTT: ";
128
129 for (i = 0; i < sizeof(sensors); i++) {
130 if (_dtt_init(sensors[i]) != 0)
131 printf("%s%d FAILED INIT\n", header, i+1);
132 else
133 printf("%s%d is %i C\n", header, i+1,
134 dtt_get_temp(sensors[i]));
135 }
136
137 return (0);
138} /* dtt_init() */
139
140#define TEMP_FROM_REG(temp) \
Wolfgang Denkf11033e2007-01-15 13:41:04 +0100141 ((temp)<256?((((temp)&0x1fe) >> 1) * 10) + ((temp) & 1) * 5: \
142 ((((temp)&0x1fe) >> 1) -255) * 10 - ((temp) & 1) * 5) \
Heiko Schocher6dedf3d2006-12-21 16:14:48 +0100143
144int dtt_get_temp(int sensor)
145{
146 int val = dtt_read (sensor, DTT_READ_TEMP);
147 int tmpcnf = dtt_read (sensor, DTT_CONFIG_TEMP);
148
149 return (TEMP_FROM_REG((val << 1) + ((tmpcnf & 0x80) >> 7))) / 10;
150} /* dtt_get_temp() */