File: sens_lm90.c

package info (click to toggle)
xmbmon 2.05-8
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 928 kB
  • ctags: 910
  • sloc: ansic: 6,089; sh: 2,151; perl: 87; makefile: 79
file content (191 lines) | stat: -rw-r--r-- 3,727 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 * Natinal Semiconductor LM75 temperature sensor chip
 *
 ***************************************************************
 * Before calling these routines, one must call method->Open() *
 * After calling these routines, one must call method->Close() *
 ***************************************************************
 *

National Semiconductor
         Chip         Temp    Volt    Fan     SMBus   IOport
        lm90           2       -       -       yes     no

Analog Devices
         Chip         Temp    Volt    Fan     SMBus   IOport
        ADM1020        2       -       -       yes     no
        ADM1021        2       -       -       yes     no
        ADM1023        2       -       -       yes     no

 *
 * by YRS
 */


#include	<stdio.h>
#include	<string.h>
#include	"sensors.h"

/* external (global) data */
extern int smb_slave;
extern LM_METHODS method_smb;


#define	LM90_ADDR_START		0x90	/*0x90-0x9E*/
#define	LM90_ADDR_END		0x9E

#define LM90_LTEMP		0x00
#define LM90_RTEMPH		0x01
#define LM90_RTEMPL		0x10
#define LM90_OFFSTH		0x11
#define LM90_OFFSTL		0x12
#define LM90_VENDID		0xFE
#define LM90_DEVID		0xFF

static int lm90chipid = 0;

static	int		lm90_probe(LM_METHODS *);
static	int		lm90_ident(LM_METHODS *);
static	float	lm90_temp(LM_METHODS *, int);
static	int		lm90_fanrpm(LM_METHODS *, int);
static	float	lm90_volt(LM_METHODS *, int);

#define BUFF_LEN 128
static char buff[BUFF_LEN];

SENSOR lm90 = {
	buff,
	lm90_probe,
	lm90_temp,
	lm90_volt,
	lm90_fanrpm
};

enum lm90_chips {
	NOSENSER,
	LM90,
	ADM1020,
	ADM1023
};

static char *lm90chip[] = {
	"No Sensor",
	"Nat.Semi.Con. Chip LM90",
	"Analog Dev. Chip ADM1020",
	"Analog Dev. Chip ADM1021/1023",
	NULL };


#define LM90_chkRegNum 8

/* Register checked for probing */
static int chkReg[] = {
	0x00, 0x01, 0x02, 0x03,
	0x04, 0x05, 0x06, 0x07,
	0x08, 0x10, 0x11, 0x12,
	0x13, 0x14, 0x19, 0x20,
	0x21, 0xBF, 0xFE, 0xFF,
	-1 };


/*
 *  return 0 if not probed
 */
static	int     lm90_probe(LM_METHODS *method)
{
	int n, save;

	if (method != &method_smb)
		return 0;

	save = smb_slave;

	for (n = LM90_ADDR_START; n <= LM90_ADDR_END; ) {
		if (!(smb_slave = get_smb_slave(n, LM90_ADDR_END)))
			goto ret0;
		else {
			if (lm90_ident(method)
					&& chkReg_Probe(smb_slave, "Probing LM90 chip:\n",
							chkReg, method) >= LM90_chkRegNum)
				goto ret1;
			else
				n = smb_slave + 2;
		}
	}

ret0:
	smb_slave = save;
	return 0;
ret1:
	kill_smb_slave(smb_slave);
	strcpy(buff, lm90chip[lm90chipid]);
	return lm90chipid;
}

static int lm90_ident(LM_METHODS *method)
{
	int vend, revd;

	vend = method->Read(LM90_VENDID);
	revd = method->Read(LM90_DEVID);
	if (vend == 0x01 && revd == 0x21)
		lm90chipid = LM90;
	else if (vend == 0x41) {
		if ((revd && 0xF0) == 0x30)
			lm90chipid = ADM1023;
		else
			lm90chipid = ADM1020;
	}

	return lm90chipid;
}

/*
 *	\retval	0xFFFF	no sensor
 *	\retval	other	temperature
 *  no = 0,1,2,...
 */
static	float	lm90_temp( LM_METHODS *method, int no )
{
	int n = 0, k = 0;
	float offset = 0.0;

	if (no < 0 || 2 < no)
		return 0xFFFF;

	if (no == 0)
		n = method->Read(LM90_LTEMP);
	else if (no == 1) {
		if (lm90chipid != ADM1020) {
			if ((n = method->Read(LM90_OFFSTH)) == 0xFF)
				n = 0;
			if ((k = method->Read(LM90_OFFSTL)) == 0xFF)
				k = 0;
			else
				k >>= 5;
			offset = (float) n + 0.125 * (float) k;
		}
		n = method->Read(LM90_RTEMPH);
		if ((k = method->Read(LM90_RTEMPL)) == 0xFF)
			k = 0;
		else
			k >>= 5;
	}
	if (n >= 0x80)
		n = k = 0;

	return ((float) n + 0.125 * (float) k - offset);
}

/* lm90 is only for temperature sensor */

static	float	lm90_volt(LM_METHODS *method, int no)
{
	return 0xFFFF;
}

static	int		lm90_fanrpm(LM_METHODS *method, int no)
{
	return 0xFFFF;
}