blob: 668ee9fb090aa250cf35c09e136f9659a3a16b83 [file] [log] [blame]
Heiko Schocheracb4f4a2006-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>
Heiko Schocheracb4f4a2006-12-21 16:14:48 +010034#include <i2c.h>
35#include <dtt.h>
36
37/*
38 * Device code
39 */
40#define DTT_I2C_DEV_CODE 0x2c /* ON Semi's LM81 device */
Michal Simek10de36d2008-07-11 11:50:53 +020041#define DTT_READ_TEMP 0x27
42#define DTT_CONFIG_TEMP 0x4b
43#define DTT_TEMP_MAX 0x39
44#define DTT_TEMP_HYST 0x3a
45#define DTT_CONFIG 0x40
Heiko Schocheracb4f4a2006-12-21 16:14:48 +010046
47int dtt_read(int sensor, int reg)
48{
49 int dlen = 1;
50 uchar data[2];
51
52 /*
53 * Calculate sensor address and register.
54 */
55 sensor = DTT_I2C_DEV_CODE + (sensor & 0x03); /* calculate address of lm81 */
56
57 /*
58 * Now try to read the register.
59 */
60 if (i2c_read(sensor, reg, 1, data, dlen) != 0)
61 return -1;
62
63 return (int)data[0];
64} /* dtt_read() */
65
66
67int dtt_write(int sensor, int reg, int val)
68{
69 uchar data;
70
71 /*
72 * Calculate sensor address and register.
73 */
74 sensor = DTT_I2C_DEV_CODE + (sensor & 0x03); /* calculate address of lm81 */
75
76 data = (char)(val & 0xff);
77
78 /*
79 * Write value to register.
80 */
Heiko Schocher6f2798d2007-01-14 13:35:31 +010081 if (i2c_write(sensor, reg, 1, &data, 1) != 0)
Heiko Schocheracb4f4a2006-12-21 16:14:48 +010082 return 1;
83
84 return 0;
85} /* dtt_write() */
86
87#define DTT_MANU 0x3e
88#define DTT_REV 0x3f
Heiko Schocher6f2798d2007-01-14 13:35:31 +010089#define DTT_CONFIG 0x40
Heiko Schocheracb4f4a2006-12-21 16:14:48 +010090#define DTT_ADR 0x48
91
92static int _dtt_init(int sensor)
93{
94 int man;
95 int adr;
96 int rev;
97
98 if (dtt_write (sensor, DTT_CONFIG, 0x01) < 0)
99 return 1;
Heiko Schocher6f2798d2007-01-14 13:35:31 +0100100 /* The LM81 needs 400ms to get the correct values ... */
101 udelay (400000);
Heiko Schocheracb4f4a2006-12-21 16:14:48 +0100102 man = dtt_read (sensor, DTT_MANU);
103 if (man != 0x01)
104 return 1;
105 adr = dtt_read (sensor, DTT_ADR);
106 if (adr < 0)
107 return 1;
108 rev = dtt_read (sensor, DTT_REV);
109 if (adr < 0)
110 return 1;
111
112 printf ("DTT: Found LM81@%x Rev: %d\n", adr, rev);
Heiko Schocheracb4f4a2006-12-21 16:14:48 +0100113 return 0;
114} /* _dtt_init() */
115
116
117int dtt_init (void)
118{
119 int i;
120 unsigned char sensors[] = CONFIG_DTT_SENSORS;
121 const char *const header = "DTT: ";
122
123 for (i = 0; i < sizeof(sensors); i++) {
124 if (_dtt_init(sensors[i]) != 0)
125 printf("%s%d FAILED INIT\n", header, i+1);
126 else
127 printf("%s%d is %i C\n", header, i+1,
128 dtt_get_temp(sensors[i]));
129 }
130
131 return (0);
132} /* dtt_init() */
133
134#define TEMP_FROM_REG(temp) \
Wolfgang Denk86370712007-01-15 13:41:04 +0100135 ((temp)<256?((((temp)&0x1fe) >> 1) * 10) + ((temp) & 1) * 5: \
136 ((((temp)&0x1fe) >> 1) -255) * 10 - ((temp) & 1) * 5) \
Heiko Schocheracb4f4a2006-12-21 16:14:48 +0100137
138int dtt_get_temp(int sensor)
139{
140 int val = dtt_read (sensor, DTT_READ_TEMP);
141 int tmpcnf = dtt_read (sensor, DTT_CONFIG_TEMP);
142
143 return (TEMP_FROM_REG((val << 1) + ((tmpcnf & 0x80) >> 7))) / 10;
144} /* dtt_get_temp() */