]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/hwmon/lm63.c
Merge branch 'u-boot/master' into u-boot-arm/master
[karo-tx-uboot.git] / drivers / hwmon / lm63.c
1 /*
2  * (C) Copyright 2007-2008
3  * Dirk Eibach,  Guntermann & Drunck GmbH, eibach@gdsys.de
4  * based on lm75.c by Bill Hunter
5  *
6  * SPDX-License-Identifier:     GPL-2.0+
7  */
8
9 /*
10  * National LM63/LM64 Temperature Sensor
11  * Main difference: LM 64 has -16 Kelvin temperature offset
12  */
13
14 #include <common.h>
15 #include <i2c.h>
16 #include <dtt.h>
17
18 #define DTT_I2C_LM63_ADDR       0x4C    /* National LM63 device */
19
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
29 struct pwm_lookup_entry {
30         u8 temp;
31         u8 pwm;
32 };
33
34 /*
35  * Device code
36  */
37
38 int dtt_read(int sensor, int reg)
39 {
40         int dlen;
41         uchar data[2];
42
43         /*
44          * Calculate sensor address and register.
45          */
46         if (!sensor)
47                 sensor = DTT_I2C_LM63_ADDR;     /* legacy config */
48
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
60 int 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          */
68         if (!sensor)
69                 sensor = DTT_I2C_LM63_ADDR;     /* legacy config */
70
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
83 static int is_lm64(int sensor)
84 {
85         return sensor && (sensor != DTT_I2C_LM63_ADDR);
86 }
87
88 int dtt_init_one(int sensor)
89 {
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         /*
112          * Make sure PWM Lookup-Table is writeable
113          */
114         if (dtt_write(sensor, DTT_FAN_CONFIG, 0x20) != 0)
115                 return 1;
116
117         /*
118          * Setup PWM Lookup-Table
119          */
120         for (i = 0; i < ARRAY_SIZE(pwm_lookup); i++) {
121                 int address = DTT_PWM_LOOKUP_BASE + 2 * i;
122                 val = pwm_lookup[i].temp;
123                 if (is_lm64(sensor))
124                         val -= 16;
125                 if (dtt_write(sensor, address, val) != 0)
126                         return 1;
127                 val = dtt_read(sensor, address);
128                 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
150 int 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
155         if (is_lm64(sensor))
156                 temp += 16 << 8;
157
158         /* Ignore LSB for now, U-Boot only prints natural numbers */
159         return temp >> 8;
160 }