blob: b95b130d926195b0efda75cab81aa84c9a4d1eed [file] [log] [blame]
John Otken9aa36772007-07-26 17:49:11 +02001/*
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02002 * SPDX-License-Identifier: GPL-2.0+
John Otken9aa36772007-07-26 17:49:11 +02003 */
4
5/*
6 * Dallas Semiconductor's DS1775 Digital Thermometer and Thermostat
7 */
8
9#include <common.h>
10
John Otken9aa36772007-07-26 17:49:11 +020011#include <i2c.h>
12#include <dtt.h>
13
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020014#define DTT_I2C_DEV_CODE CONFIG_SYS_I2C_DTT_ADDR /* Dallas Semi's DS1775 device code */
Michal Simek10de36d2008-07-11 11:50:53 +020015#define DTT_READ_TEMP 0x0
16#define DTT_CONFIG 0x1
17#define DTT_TEMP_HYST 0x2
18#define DTT_TEMP_OS 0x3
John Otken9aa36772007-07-26 17:49:11 +020019
20int dtt_read(int sensor, int reg)
21{
22 int dlen;
23 uchar data[2];
24
25 /*
26 * Calculate sensor address and command
27 */
28 sensor = DTT_I2C_DEV_CODE + (sensor & 0x07); /* Calculate addr of ds1775 */
29
30 /*
31 * Prepare to handle 2 byte result
32 */
33 if ((reg == DTT_READ_TEMP) ||
34 (reg == DTT_TEMP_OS) || (reg == DTT_TEMP_HYST))
35 dlen = 2;
36 else
37 dlen = 1;
38
39 /*
40 * Now try to read the register
41 */
42 if (i2c_read(sensor, reg, 1, data, dlen) != 0)
43 return 1;
44
45 /*
46 * Handle 2 byte result
47 */
48 if (dlen == 2)
49 return ((int)((short)data[1] + (((short)data[0]) << 8)));
50
51 return (int) data[0];
52}
53
54
55int dtt_write(int sensor, int reg, int val)
56{
57 int dlen;
58 uchar data[2];
59
60 /*
61 * Calculate sensor address and register
62 */
63 sensor = DTT_I2C_DEV_CODE + (sensor & 0x07);
64
65 /*
66 * Handle various data sizes
67 */
68 if ((reg == DTT_READ_TEMP) ||
69 (reg == DTT_TEMP_OS) || (reg == DTT_TEMP_HYST)) {
70 dlen = 2;
71 data[0] = (char)((val >> 8) & 0xff); /* MSB first */
72 data[1] = (char)(val & 0xff);
73 } else {
74 dlen = 1;
75 data[0] = (char)(val & 0xff);
76 }
77
78 /*
79 * Write value to device
80 */
81 if (i2c_write(sensor, reg, 1, data, dlen) != 0)
82 return 1;
83
84 return 0;
85}
86
87
Heiko Schocher8df768c2011-08-01 04:01:43 +000088int dtt_init_one(int sensor)
John Otken9aa36772007-07-26 17:49:11 +020089{
90 int val;
91
92 /*
93 * Setup High Temp
94 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020095 val = ((CONFIG_SYS_DTT_MAX_TEMP * 2) << 7) & 0xff80;
John Otken9aa36772007-07-26 17:49:11 +020096 if (dtt_write(sensor, DTT_TEMP_OS, val) != 0)
97 return 1;
98 udelay(50000); /* Max 50ms */
99
100 /*
101 * Setup Low Temp - hysteresis
102 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200103 val = (((CONFIG_SYS_DTT_MAX_TEMP - CONFIG_SYS_DTT_HYSTERESIS) * 2) << 7) & 0xff80;
John Otken9aa36772007-07-26 17:49:11 +0200104 if (dtt_write(sensor, DTT_TEMP_HYST, val) != 0)
105 return 1;
106 udelay(50000); /* Max 50ms */
107
108 /*
109 * Setup configuraton register
110 *
111 * Fault Tolerance limits 4, Thermometer resolution bits is 9,
112 * Polarity = Active Low,continuous conversion mode, Thermostat
113 * mode is interrupt mode
114 */
115 val = 0xa;
116 if (dtt_write(sensor, DTT_CONFIG, val) != 0)
117 return 1;
118 udelay(50000); /* Max 50ms */
119
120 return 0;
121}
122
John Otken9aa36772007-07-26 17:49:11 +0200123int dtt_get_temp(int sensor)
124{
125 return (dtt_read(sensor, DTT_READ_TEMP) / 256);
126}