blob: 66947a664e5ab9787a5d01e4cf6b879cd842466e [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
2 * (C) Copyright 2001
3 * Erik Theisen, Wave 7 Optics, etheisen@mindspring.com.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenkc6097192002-11-03 00:24:07 +00006 */
7
8/*
Peter Tyserc3e5fa02010-09-29 13:46:17 -05009 * Dallas Semiconductor's DS1621/1631 Digital Thermometer and Thermostat.
wdenkc6097192002-11-03 00:24:07 +000010 */
11
12#include <common.h>
wdenkc6097192002-11-03 00:24:07 +000013#include <i2c.h>
14#include <dtt.h>
15
16/*
17 * Device code
18 */
Peter Tyser9a6c80b2010-09-29 13:46:18 -050019#define DTT_I2C_DEV_CODE 0x48 /* Dallas Semi's DS1621 */
Michal Simek6ecbb452008-07-11 11:50:53 +020020#define DTT_READ_TEMP 0xAA
21#define DTT_READ_COUNTER 0xA8
22#define DTT_READ_SLOPE 0xA9
23#define DTT_WRITE_START_CONV 0xEE
24#define DTT_WRITE_STOP_CONV 0x22
25#define DTT_TEMP_HIGH 0xA1
26#define DTT_TEMP_LOW 0xA2
27#define DTT_CONFIG 0xAC
wdenkc6097192002-11-03 00:24:07 +000028
Peter Tyserc3e5fa02010-09-29 13:46:17 -050029/*
30 * Config register bits
31 */
32#define DTT_CONFIG_1SHOT 0x01
33#define DTT_CONFIG_POLARITY 0x02
Peter Tyser9a6c80b2010-09-29 13:46:18 -050034#define DTT_CONFIG_R0 0x04 /* ds1631 only */
35#define DTT_CONFIG_R1 0x08 /* ds1631 only */
Peter Tyserc3e5fa02010-09-29 13:46:17 -050036#define DTT_CONFIG_NVB 0x10
37#define DTT_CONFIG_TLF 0x20
38#define DTT_CONFIG_THF 0x40
39#define DTT_CONFIG_DONE 0x80
40
41
wdenkc6097192002-11-03 00:24:07 +000042int dtt_read(int sensor, int reg)
43{
Peter Tyser9a6c80b2010-09-29 13:46:18 -050044 int dlen;
45 uchar data[2];
wdenkc6097192002-11-03 00:24:07 +000046
Peter Tyser9a6c80b2010-09-29 13:46:18 -050047 /* Calculate sensor address and command */
48 sensor = DTT_I2C_DEV_CODE + (sensor & 0x07); /* Calculate addr of ds1621*/
wdenkc6097192002-11-03 00:24:07 +000049
Peter Tyser9a6c80b2010-09-29 13:46:18 -050050 /* Prepare to handle 2 byte result */
51 switch(reg) {
52 case DTT_READ_TEMP:
53 case DTT_TEMP_HIGH:
54 case DTT_TEMP_LOW:
55 dlen = 2;
56 break;
57 default:
58 dlen = 1;
59 }
wdenkc6097192002-11-03 00:24:07 +000060
Peter Tyser9a6c80b2010-09-29 13:46:18 -050061 /* Now try to read the register */
62 if (i2c_read(sensor, reg, 1, data, dlen) != 0)
63 return 1;
wdenkc6097192002-11-03 00:24:07 +000064
Peter Tyser9a6c80b2010-09-29 13:46:18 -050065 /* Handle 2 byte result */
66 if (dlen == 2)
Jeff Dischler7d85f1d2010-09-29 13:46:19 -050067 return (short)((data[0] << 8) | data[1]);
wdenkc6097192002-11-03 00:24:07 +000068
Peter Tyser9a6c80b2010-09-29 13:46:18 -050069 return (int)data[0];
70}
wdenkc6097192002-11-03 00:24:07 +000071
72
73int dtt_write(int sensor, int reg, int val)
74{
Peter Tyser9a6c80b2010-09-29 13:46:18 -050075 int dlen;
76 uchar data[2];
wdenkc6097192002-11-03 00:24:07 +000077
Peter Tyser9a6c80b2010-09-29 13:46:18 -050078 /* Calculate sensor address and register */
79 sensor = DTT_I2C_DEV_CODE + (sensor & 0x07);
wdenkc6097192002-11-03 00:24:07 +000080
Peter Tyser9a6c80b2010-09-29 13:46:18 -050081 /* Handle various data sizes. */
82 switch(reg) {
83 case DTT_READ_TEMP:
84 case DTT_TEMP_HIGH:
85 case DTT_TEMP_LOW:
86 dlen = 2;
87 data[0] = (char)((val >> 8) & 0xff); /* MSB first */
88 data[1] = (char)(val & 0xff);
89 break;
90 case DTT_WRITE_START_CONV:
91 case DTT_WRITE_STOP_CONV:
92 dlen = 0;
93 data[0] = (char)0;
94 data[1] = (char)0;
95 break;
96 default:
97 dlen = 1;
98 data[0] = (char)(val & 0xff);
99 }
wdenkc6097192002-11-03 00:24:07 +0000100
Peter Tyser9a6c80b2010-09-29 13:46:18 -0500101 /* Write value to device */
102 if (i2c_write(sensor, reg, 1, data, dlen) != 0)
103 return 1;
wdenkc6097192002-11-03 00:24:07 +0000104
Peter Tyser9a6c80b2010-09-29 13:46:18 -0500105 /* Poll NV memory busy bit in case write was to register stored in EEPROM */
106 while(i2c_reg_read(sensor, DTT_CONFIG) & DTT_CONFIG_NVB)
107 ;
Peter Tyserc3e5fa02010-09-29 13:46:17 -0500108
Peter Tyser9a6c80b2010-09-29 13:46:18 -0500109 return 0;
110}
wdenkc6097192002-11-03 00:24:07 +0000111
112
Heiko Schocher780f13a2011-08-01 04:01:43 +0000113int dtt_init_one(int sensor)
wdenkc6097192002-11-03 00:24:07 +0000114{
Peter Tyser9a6c80b2010-09-29 13:46:18 -0500115 int val;
wdenkc6097192002-11-03 00:24:07 +0000116
Peter Tyser9a6c80b2010-09-29 13:46:18 -0500117 /* Setup High Temp */
118 val = ((CONFIG_SYS_DTT_MAX_TEMP * 2) << 7) & 0xff80;
119 if (dtt_write(sensor, DTT_TEMP_HIGH, val) != 0)
120 return 1;
wdenkc6097192002-11-03 00:24:07 +0000121
Peter Tyser9a6c80b2010-09-29 13:46:18 -0500122 /* Setup Low Temp - hysteresis */
123 val = (((CONFIG_SYS_DTT_MAX_TEMP - CONFIG_SYS_DTT_HYSTERESIS) * 2) << 7) & 0xff80;
124 if (dtt_write(sensor, DTT_TEMP_LOW, val) != 0)
125 return 1;
wdenkc6097192002-11-03 00:24:07 +0000126
Peter Tyser9a6c80b2010-09-29 13:46:18 -0500127 /*
128 * Setup configuraton register
129 *
130 * Clear THF & TLF, Reserved = 1, Polarity = Active Low, One Shot = YES
131 *
132 * We run in polled mode, since there isn't any way to know if this
133 * lousy device is ready to provide temperature readings on power up.
134 */
135 val = 0x9;
136 if (dtt_write(sensor, DTT_CONFIG, val) != 0)
137 return 1;
wdenkc6097192002-11-03 00:24:07 +0000138
Peter Tyser9a6c80b2010-09-29 13:46:18 -0500139 return 0;
140}
wdenkc6097192002-11-03 00:24:07 +0000141
wdenkc6097192002-11-03 00:24:07 +0000142int dtt_get_temp(int sensor)
143{
Peter Tyser9a6c80b2010-09-29 13:46:18 -0500144 int i;
wdenkc6097192002-11-03 00:24:07 +0000145
Peter Tyser9a6c80b2010-09-29 13:46:18 -0500146 /* Start a conversion, may take up to 1 second. */
147 dtt_write(sensor, DTT_WRITE_START_CONV, 0);
148 for (i = 0; i <= 10; i++) {
149 udelay(100000);
150 if (dtt_read(sensor, DTT_CONFIG) & DTT_CONFIG_DONE)
151 break;
152 }
wdenkc6097192002-11-03 00:24:07 +0000153
Peter Tyser9a6c80b2010-09-29 13:46:18 -0500154 return (dtt_read(sensor, DTT_READ_TEMP) / 256);
155}