summaryrefslogtreecommitdiff
path: root/sys/dev/i2c/asc7611.c
blob: 2ebb2b32792a816a8064b992494a6d19c6d08022 (plain)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*	$OpenBSD: asc7611.c,v 1.2 2009/01/26 15:07:49 kettenis Exp $	*/

/*
 * Copyright (c) 2008 Constantine A. Murenin <cnst+openbsd@bugmail.mojo.ru>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/sensors.h>

#include <dev/i2c/i2cvar.h>

/*
 * Andigilog aSC7611
 *	Hardware Monitor with Integrated Fan Control
 * http://www.andigilog.com/downloads/aSC7611_70A05007.pdf
 * October 2006
 */

/* Temperature */
#define ANDL_NUM_TEMPS	3
static const struct {
	const char	*name;
	const uint8_t	mreg;
	const uint8_t	lreg;
} andl_temp[ANDL_NUM_TEMPS] = {
	{ "External", 0x25, 0x10 },
	{ "Internal", 0x26, 0x15 },
	{ "External", 0x27, 0x0e }
};

/* Voltage */
#define ANDL_NUM_VOLTS	5
static const struct {
	const char	*name;
	const short	nominal;
	const uint8_t	mreg;
} andl_volt[ANDL_NUM_VOLTS] = {
	{ "+2.5V", 2500, 0x20 },
	{ "Vccp", 2250, 0x21 },
	{ "+3.3V", 3300, 0x22 },
	{ "+5V", 5000, 0x23 },
	{ "+12V", 12000, 0x24 }
};

/* Fan */
#define ANDL_NUM_TACHS	4
#define ANDL_TACH_START	0x28

#define ANDL_NUM_TOTAL	(ANDL_NUM_TEMPS + ANDL_NUM_VOLTS + ANDL_NUM_TACHS)

struct andl_softc {
	struct device	sc_dev;
	i2c_tag_t	sc_tag;
	i2c_addr_t	sc_addr;

	struct ksensor	sc_sensors[ANDL_NUM_TOTAL];
	struct ksensordev sc_sensordev;
};


int	andl_match(struct device *, void *, void *);
void	andl_attach(struct device *, struct device *, void *);
void	andl_refresh(void *);

int	andl_refresh_temps(struct andl_softc *, struct ksensor *);
int	andl_refresh_volts(struct andl_softc *, struct ksensor *);
int	andl_refresh_tachs(struct andl_softc *, struct ksensor *);

uint8_t	andl_readreg(struct andl_softc *, uint8_t);
void	andl_writereg(struct andl_softc *, uint8_t, uint8_t);


struct cfattach andl_ca = {
	sizeof(struct andl_softc), andl_match, andl_attach
};

struct cfdriver andl_cd = {
	NULL, "andl", DV_DULL
};


int
andl_match(struct device *parent, void *match, void *aux)
{
	struct i2c_attach_args *ia = aux;

	if (strcmp(ia->ia_name, "asc7611") == 0)
		return 1;
	return 0;
}

void
andl_attach(struct device *parent, struct device *self, void *aux)
{
	struct andl_softc *sc = (struct andl_softc *)self;
	struct i2c_attach_args *ia = aux;
	int i, j;

	sc->sc_tag = ia->ia_tag;
	sc->sc_addr = ia->ia_addr;

	printf(": %s", ia->ia_name);

	strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname,
	    sizeof(sc->sc_sensordev.xname));

	for (i = 0; i < ANDL_NUM_TEMPS; i++) {
		strlcpy(sc->sc_sensors[i].desc, andl_temp[i].name,
		    sizeof(sc->sc_sensors[i].desc));
		sc->sc_sensors[i].type = SENSOR_TEMP;
	}

	for (j = i; i < j + ANDL_NUM_VOLTS; i++) {
		strlcpy(sc->sc_sensors[i].desc, andl_volt[i - j].name,
		    sizeof(sc->sc_sensors[i].desc));
		sc->sc_sensors[i].type = SENSOR_VOLTS_DC;
	}

	for (j = i + ANDL_NUM_TACHS; i < j; i++)
		sc->sc_sensors[i].type = SENSOR_FANRPM;

	for (i = 0; i < j; i++)
		sensor_attach(&sc->sc_sensordev, &sc->sc_sensors[i]);

	if (sensor_task_register(sc, andl_refresh, 5) == NULL) {
		printf(", unable to register update task\n");
		return;
	}

	sensordev_install(&sc->sc_sensordev);
	printf("\n");
}

void
andl_refresh(void *arg)
{
	struct andl_softc *sc = arg;
	struct ksensor *s = sc->sc_sensors;

	iic_acquire_bus(sc->sc_tag, 0);

	s += andl_refresh_temps(sc, s);
	s += andl_refresh_volts(sc, s);
	s += andl_refresh_tachs(sc, s);

	iic_release_bus(sc->sc_tag, 0);
}

int
andl_refresh_temps(struct andl_softc *sc, struct ksensor *s)
{
	int i;

	for (i = 0; i < ANDL_NUM_TEMPS; i++) {
		uint8_t m = andl_readreg(sc, andl_temp[i].mreg);
		uint8_t l = andl_readreg(sc, andl_temp[i].lreg);
		int32_t t = (m << 8 | l) >> (16 - 10);

		if (t & 0x200)
			t -= 0x400;
		t *= 250;
		if (t < -55000 || t > 125000) {
			s[i].flags |= SENSOR_FINVALID;
			s[i].value = 0;
		} else {
			s[i].value = t * 1000 + 273150000;
			s[i].flags &= ~SENSOR_FINVALID;
		}
	}
	return i;
}

int
andl_refresh_volts(struct andl_softc *sc, struct ksensor *s)
{
	int i;

	for (i = 0; i < ANDL_NUM_VOLTS; i++)
		s[i].value = 1000ll * andl_readreg(sc, andl_volt[i].mreg) *
		    andl_volt[i].nominal / 0xc0;
	return i;
}

int
andl_refresh_tachs(struct andl_softc *sc, struct ksensor *s)
{
	int i;

	for (i = 0; i < ANDL_NUM_TACHS; i++) {
		uint8_t l = andl_readreg(sc, ANDL_TACH_START + i * 2);
		uint8_t m = andl_readreg(sc, ANDL_TACH_START + i * 2 + 1);
		uint16_t b = m << 8 | l;

		if (b >= 0xfffc || b == 0) {
			s[i].flags |= SENSOR_FINVALID;
			s[i].value = 0;
		} else {
			s[i].value = (90000 * 60) / b;
			s[i].flags &= ~SENSOR_FINVALID;
		}
	}
	return i;
}

uint8_t
andl_readreg(struct andl_softc *sc, uint8_t reg)
{
	uint8_t data;

	iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
	    sc->sc_addr, &reg, sizeof reg, &data, sizeof data, 0);

	return data;
}

void
andl_writereg(struct andl_softc *sc, uint8_t reg, uint8_t data)
{
	iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
	    sc->sc_addr, &reg, sizeof reg, &data, sizeof data, 0);
}