blob: 053c785fc56268b443631ef81002a405d93de782 [file] [log] [blame]
Dirk Eibach8fdffec2008-10-08 13:44:27 +02001/*
2 * (C) Copyright 2007-2008
3 * Dirk Eibach, Guntermann & Drunck GmbH, eibach@gdsys.de
4 * based on lm75.c by Bill Hunter
5 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Dirk Eibach8fdffec2008-10-08 13:44:27 +02007 */
8
9/*
Dirk Eibach4a9d3b12011-04-18 10:43:14 +020010 * National LM63/LM64 Temperature Sensor
11 * Main difference: LM 64 has -16 Kelvin temperature offset
Dirk Eibach8fdffec2008-10-08 13:44:27 +020012 */
13
14#include <common.h>
15#include <i2c.h>
16#include <dtt.h>
17
Dirk Eibach4a9d3b12011-04-18 10:43:14 +020018#define DTT_I2C_LM63_ADDR 0x4C /* National LM63 device */
Dirk Eibach8fdffec2008-10-08 13:44:27 +020019
20#define DTT_READ_TEMP_RMT_MSB 0x01
21#define DTT_CONFIG 0x03
22#define DTT_READ_TEMP_RMT_LSB 0x10
23#define DTT_TACHLIM_LSB 0x48
24#define DTT_TACHLIM_MSB 0x49
25#define DTT_FAN_CONFIG 0x4A
26#define DTT_PWM_FREQ 0x4D
27#define DTT_PWM_LOOKUP_BASE 0x50
28
29struct pwm_lookup_entry {
30 u8 temp;
31 u8 pwm;
32};
33
34/*
35 * Device code
36 */
37
38int dtt_read(int sensor, int reg)
39{
40 int dlen;
41 uchar data[2];
42
43 /*
44 * Calculate sensor address and register.
45 */
Dirk Eibach4a9d3b12011-04-18 10:43:14 +020046 if (!sensor)
47 sensor = DTT_I2C_LM63_ADDR; /* legacy config */
Dirk Eibach8fdffec2008-10-08 13:44:27 +020048
49 dlen = 1;
50
51 /*
52 * Now try to read the register.
53 */
54 if (i2c_read(sensor, reg, 1, data, dlen) != 0)
55 return -1;
56
57 return (int)data[0];
58} /* dtt_read() */
59
60int dtt_write(int sensor, int reg, int val)
61{
62 int dlen;
63 uchar data[2];
64
65 /*
66 * Calculate sensor address and register.
67 */
Dirk Eibach4a9d3b12011-04-18 10:43:14 +020068 if (!sensor)
69 sensor = DTT_I2C_LM63_ADDR; /* legacy config */
Dirk Eibach8fdffec2008-10-08 13:44:27 +020070
71 dlen = 1;
72 data[0] = (char)(val & 0xff);
73
74 /*
75 * Write value to register.
76 */
77 if (i2c_write(sensor, reg, 1, data, dlen) != 0)
78 return 1;
79
80 return 0;
81} /* dtt_write() */
82
Dirk Eibach4a9d3b12011-04-18 10:43:14 +020083static int is_lm64(int sensor)
84{
85 return sensor && (sensor != DTT_I2C_LM63_ADDR);
86}
87
Heiko Schocher8df768c2011-08-01 04:01:43 +000088int dtt_init_one(int sensor)
Dirk Eibach8fdffec2008-10-08 13:44:27 +020089{
90 int i;
91 int val;
92
93 struct pwm_lookup_entry pwm_lookup[] = CONFIG_DTT_PWM_LOOKUPTABLE;
94
95 /*
96 * Set PWM Frequency to 2.5% resolution
97 */
98 val = 20;
99 if (dtt_write(sensor, DTT_PWM_FREQ, val) != 0)
100 return 1;
101
102 /*
103 * Set Tachometer Limit
104 */
105 val = CONFIG_DTT_TACH_LIMIT;
106 if (dtt_write(sensor, DTT_TACHLIM_LSB, val & 0xff) != 0)
107 return 1;
108 if (dtt_write(sensor, DTT_TACHLIM_MSB, (val >> 8) & 0xff) != 0)
109 return 1;
110
111 /*
Dirk Eibach4a9d3b12011-04-18 10:43:14 +0200112 * Make sure PWM Lookup-Table is writeable
113 */
114 if (dtt_write(sensor, DTT_FAN_CONFIG, 0x20) != 0)
115 return 1;
116
117 /*
Dirk Eibach8fdffec2008-10-08 13:44:27 +0200118 * Setup PWM Lookup-Table
119 */
Axel Lindd5d4a12013-06-23 00:55:14 +0800120 for (i = 0; i < ARRAY_SIZE(pwm_lookup); i++) {
Dirk Eibach8fdffec2008-10-08 13:44:27 +0200121 int address = DTT_PWM_LOOKUP_BASE + 2 * i;
122 val = pwm_lookup[i].temp;
Dirk Eibach4a9d3b12011-04-18 10:43:14 +0200123 if (is_lm64(sensor))
124 val -= 16;
Dirk Eibach8fdffec2008-10-08 13:44:27 +0200125 if (dtt_write(sensor, address, val) != 0)
126 return 1;
Dirk Eibach4a9d3b12011-04-18 10:43:14 +0200127 val = dtt_read(sensor, address);
Dirk Eibach8fdffec2008-10-08 13:44:27 +0200128 val = pwm_lookup[i].pwm;
129 if (dtt_write(sensor, address + 1, val) != 0)
130 return 1;
131 }
132
133 /*
134 * Enable PWM Lookup-Table, PWM Clock 360 kHz, Tachometer Mode 2
135 */
136 val = 0x02;
137 if (dtt_write(sensor, DTT_FAN_CONFIG, val) != 0)
138 return 1;
139
140 /*
141 * Enable Tach input
142 */
143 val = dtt_read(sensor, DTT_CONFIG) | 0x04;
144 if (dtt_write(sensor, DTT_CONFIG, val) != 0)
145 return 1;
146
147 return 0;
148}
149
150int dtt_get_temp(int sensor)
151{
152 s16 temp = (dtt_read(sensor, DTT_READ_TEMP_RMT_MSB) << 8)
153 | (dtt_read(sensor, DTT_READ_TEMP_RMT_LSB));
154
Dirk Eibach4a9d3b12011-04-18 10:43:14 +0200155 if (is_lm64(sensor))
156 temp += 16 << 8;
157
Dirk Eibach8fdffec2008-10-08 13:44:27 +0200158 /* Ignore LSB for now, U-Boot only prints natural numbers */
159 return temp >> 8;
160}