aafa4757b130ac222401076676d3455f9f57ef2d
[platform/kernel/u-boot.git] / drivers / hwmon / lm75.c
1 /*
2  * (C) Copyright 2001
3  * Bill Hunter,  Wave 7 Optics, williamhunter@mediaone.net
4  *
5  * SPDX-License-Identifier:     GPL-2.0+
6  */
7
8 /*
9  * On Semiconductor's LM75 Temperature Sensor
10  */
11
12 #include <common.h>
13 #include <i2c.h>
14 #include <dtt.h>
15
16 /*
17  * Device code
18  */
19 #define DTT_I2C_DEV_CODE 0x48                   /* ON Semi's LM75 device */
20 #define DTT_READ_TEMP           0x0
21 #define DTT_CONFIG              0x1
22 #define DTT_TEMP_HYST           0x2
23 #define DTT_TEMP_SET            0x3
24
25 int dtt_read(int sensor, int reg)
26 {
27         int dlen;
28         uchar data[2];
29
30 #ifdef CONFIG_DTT_AD7414
31         /*
32          * On AD7414 the first value upon bootup is not read correctly.
33          * This is most likely because of the 800ms update time of the
34          * temp register in normal update mode. To get current values
35          * each time we issue the "dtt" command including upon powerup
36          * we switch into one-short mode.
37          *
38          * Issue one-shot mode command
39          */
40         dtt_write(sensor, DTT_CONFIG, 0x64);
41 #endif
42
43         /* Validate 'reg' param */
44         if((reg < 0) || (reg > 3))
45                 return -1;
46
47         /* Calculate sensor address and register. */
48         sensor = DTT_I2C_DEV_CODE + (sensor & 0x07);
49
50         /* Prepare to handle 2 byte result. */
51         if ((reg == DTT_READ_TEMP) ||
52                 (reg == DTT_TEMP_HYST) ||
53                 (reg == DTT_TEMP_SET))
54                         dlen = 2;
55         else
56                 dlen = 1;
57
58         /* Now try to read the register. */
59         if (i2c_read(sensor, reg, 1, data, dlen) != 0)
60                 return -1;
61
62         /* Handle 2 byte result. */
63         if (dlen == 2)
64                 return ((int)((short)data[1] + (((short)data[0]) << 8)));
65
66         return (int)data[0];
67 } /* dtt_read() */
68
69
70 int dtt_write(int sensor, int reg, int val)
71 {
72         int dlen;
73         uchar data[2];
74
75         /* Validate 'reg' param */
76         if ((reg < 0) || (reg > 3))
77                 return 1;
78
79         /* Calculate sensor address and register. */
80         sensor = DTT_I2C_DEV_CODE + (sensor & 0x07);
81
82         /* Handle 2 byte values. */
83         if ((reg == DTT_READ_TEMP) ||
84                 (reg == DTT_TEMP_HYST) ||
85                 (reg == DTT_TEMP_SET)) {
86                         dlen = 2;
87                 data[0] = (char)((val >> 8) & 0xff);    /* MSB first */
88                 data[1] = (char)(val & 0xff);
89         } else {
90                 dlen = 1;
91                 data[0] = (char)(val & 0xff);
92         }
93
94         /* Write value to register. */
95         if (i2c_write(sensor, reg, 1, data, dlen) != 0)
96                 return 1;
97
98         return 0;
99 } /* dtt_write() */
100
101
102 int dtt_init_one(int sensor)
103 {
104         int val;
105
106         /* Setup TSET ( trip point ) register */
107         val = ((CONFIG_SYS_DTT_MAX_TEMP * 2) << 7) & 0xff80; /* trip */
108         if (dtt_write(sensor, DTT_TEMP_SET, val) != 0)
109                 return 1;
110
111         /* Setup THYST ( untrip point ) register - Hysteresis */
112         val = (((CONFIG_SYS_DTT_MAX_TEMP - CONFIG_SYS_DTT_HYSTERESIS) * 2) << 7) & 0xff80;
113         if (dtt_write(sensor, DTT_TEMP_HYST, val) != 0)
114                 return 1;
115
116         /* Setup configuraton register */
117 #ifdef CONFIG_DTT_AD7414
118         /* config = alert active low and disabled */
119         val = 0x60;
120 #else
121         /* config = 6 sample integration, int mode, active low, and enable */
122         val = 0x18;
123 #endif
124         if (dtt_write(sensor, DTT_CONFIG, val) != 0)
125                 return 1;
126
127         return 0;
128 } /* dtt_init_one() */
129
130 int dtt_get_temp(int sensor)
131 {
132         int const ret = dtt_read(sensor, DTT_READ_TEMP);
133
134         if (ret < 0) {
135                 printf("DTT temperature read failed.\n");
136                 return 0;
137         }
138         return (int)((int16_t) ret / 256);
139 } /* dtt_get_temp() */