summaryrefslogtreecommitdiff
path: root/sys/dev/i2c/w83795g.c
blob: 1a8a7163ff6f0d61ee2823e8443f17b6d7e4c674 (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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*	$OpenBSD: w83795g.c,v 1.1 2011/07/03 21:30:20 kettenis Exp $	*/

/*
 * Copyright (c) 2011 Mark Kettenis
 *
 * 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>

/* Nuvoton W83795G Hardware Monitor */

#define NVT_BANKSELECT		0x00
#define NVT_CONFIG		0x01
#define  NVT_CONFIG_48		0x04
#define NVT_VOLT_CTRL1		0x02
#define NVT_VOLT_CTRL2		0x03
#define NVT_TEMP_CTRL1		0x04
#define NVT_TEMP_CTRL2		0x05
#define NVT_FANIN_CTRL1		0x06
#define NVT_FANIN_CTRL2		0x07
#define NVT_VSEN1		0x10
#define NVT_3VDD		0x1c
#define NVT_3VSB		0x1d
#define NVT_VBAT		0x1e
#define NVT_TR5			0x1f
#define NVT_TR6			0x20
#define NVT_TD1			0x21
#define NVT_TD2			0x22
#define NVT_TD3			0x23
#define NVT_TD4			0x24
#define NVT_FANIN1_COUNT	0x2e
#define NVT_VRLSB		0x3c

/* Voltage */
#define NVT_NUM_VOLTS	15

static const char *nvt_volt_desc[NVT_NUM_VOLTS] = {
	"", "", "", "", "", "", "", "", "", "", "",
	"VTT", "3VDD", "3VSB", "VBat"
};

/* Temperature */
#define NVT_NUM_TEMPS	6
#define NVT_NUM_TR	2
#define NVT_NUM_TD	4

/* Fan */
#define NVT_NUM_FANS	14

#define NVT_NUM_SENSORS (NVT_NUM_VOLTS + NVT_NUM_TEMPS + NVT_NUM_FANS)

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

	uint16_t	sc_vctrl;
	uint16_t	sc_tctrl1, sc_tctrl2;
	uint16_t	sc_fctrl;

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


int	nvt_match(struct device *, void *, void *);
void	nvt_attach(struct device *, struct device *, void *);
void	nvt_refresh(void *);

void	nvt_refresh_volts(struct nvt_softc *);
void	nvt_refresh_temps(struct nvt_softc *);
void	nvt_refresh_fans(struct nvt_softc *);

uint8_t	nvt_readreg(struct nvt_softc *, uint8_t);
void	nvt_writereg(struct nvt_softc *, uint8_t, uint8_t);


struct cfattach nvt_ca = {
	sizeof(struct nvt_softc), nvt_match, nvt_attach
};

struct cfdriver nvt_cd = {
	NULL, "nvt", DV_DULL
};


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

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

void
nvt_attach(struct device *parent, struct device *self, void *aux)
{
	struct nvt_softc *sc = (struct nvt_softc *)self;
	struct i2c_attach_args *ia = aux;
	uint8_t cfg, vctrl1, vctrl2;
	uint8_t tctrl1, tctrl2, fctrl1, fctrl2;
	int i, j;

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

	cfg = nvt_readreg(sc, NVT_CONFIG);
	if (cfg & NVT_CONFIG_48)
		printf(": W83795ADG");
	else
		printf(": W83795G");

	vctrl1 = nvt_readreg(sc, NVT_VOLT_CTRL1);
	vctrl2 = nvt_readreg(sc, NVT_VOLT_CTRL2);
	tctrl1 = nvt_readreg(sc, NVT_TEMP_CTRL1);
	tctrl2 = nvt_readreg(sc, NVT_TEMP_CTRL2);
	fctrl1 = nvt_readreg(sc, NVT_FANIN_CTRL1);
	fctrl2 = nvt_readreg(sc, NVT_FANIN_CTRL2);

	sc->sc_vctrl = vctrl2 << 8 | vctrl1;
	sc->sc_tctrl1 = tctrl1;
	sc->sc_tctrl2 = tctrl2;
	sc->sc_fctrl = fctrl2 << 8 | fctrl1;

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

	for (i = 0; i < NVT_NUM_VOLTS; i++) {
		strlcpy(sc->sc_sensors[i].desc, nvt_volt_desc[i],
		    sizeof(sc->sc_sensors[i].desc));
		sc->sc_sensors[i].type = SENSOR_VOLTS_DC;
	}

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

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

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

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

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

void
nvt_refresh(void *arg)
{
	struct nvt_softc *sc = arg;
	uint8_t bsr;

	iic_acquire_bus(sc->sc_tag, 0);

	bsr = nvt_readreg(sc, NVT_BANKSELECT);
	if ((bsr & 0x07) != 0x00)
		nvt_writereg(sc, NVT_BANKSELECT, bsr & 0xf8);

	nvt_refresh_volts(sc);
	nvt_refresh_temps(sc);
	nvt_refresh_fans(sc);

	if ((bsr & 0x07) != 0x00)
		nvt_writereg(sc, NVT_BANKSELECT, bsr);

	iic_release_bus(sc->sc_tag, 0);
}

void
nvt_refresh_volts(struct nvt_softc *sc)
{
	struct ksensor *s = &sc->sc_sensors[0];
	uint8_t	vrlsb, data;
	int i, reg;

	for (i = 0; i < NVT_NUM_VOLTS; i++) {
		if ((sc->sc_vctrl & (1 << i)) == 0) {
			s[i].flags |= SENSOR_FINVALID;
			s[i].value = 0;
			continue;
		}

		reg = NVT_VSEN1 + i;
		data = nvt_readreg(sc, reg);
		vrlsb = nvt_readreg(sc, NVT_VRLSB);
		if (reg != NVT_3VDD && reg != NVT_3VSB && reg != NVT_VBAT)
			s[i].value = 10000000 - ((data << 3) | (vrlsb >> 6)) * 2000;
		else
			s[i].value = 10000000 - ((data << 3) | (vrlsb >> 6)) * 6000;
		s[i].flags &= ~SENSOR_FINVALID;
	}
}

void
nvt_refresh_temps(struct nvt_softc *sc)
{
	struct ksensor *s = &sc->sc_sensors[NVT_NUM_VOLTS];
	uint8_t	vrlsb;
	int8_t data;
	int i;

	for (i = 0; i < NVT_NUM_TEMPS; i++) {
		if (i < NVT_NUM_TR
		    && (sc->sc_tctrl1 & (1 << (2 * i))) == 0) {
			s[i].flags |= SENSOR_FINVALID;
			s[i].value = 0;
			continue;
		}

		if (i >= NVT_NUM_TR
		    && (sc->sc_tctrl2 & (1 << (2 * (i - NVT_NUM_TR)))) == 0) {
			s[i].flags |= SENSOR_FINVALID;
			s[i].value = 0;
			continue;
		}

		data = nvt_readreg(sc, NVT_TR5 + i);
		vrlsb = nvt_readreg(sc, NVT_VRLSB);
		if (data == -128 && (vrlsb >> 6) == 0) {
			s[i].flags |= SENSOR_FINVALID;
			s[i].value = 0;
			continue;
		}
		s[i].value = data * 1000000 + (vrlsb >> 6) * 250000;
		s[i].value += 273150000;
		s[i].flags &= ~SENSOR_FINVALID;
	}
}

void
nvt_refresh_fans(struct nvt_softc *sc)
{
	struct ksensor *s = &sc->sc_sensors[NVT_NUM_VOLTS + NVT_NUM_TEMPS];
	uint8_t	data, vrlsb;
	uint16_t count;
	int i;

	for (i = 0; i < NVT_NUM_FANS; i++) {
		if ((sc->sc_fctrl & (1 << i)) == 0) {
			s[i].flags |= SENSOR_FINVALID;
			s[i].value = 0;
			continue;
		}

		data = nvt_readreg(sc, NVT_FANIN1_COUNT + i);
		vrlsb = nvt_readreg(sc, NVT_VRLSB);
		count = (data << 4) + (vrlsb >> 4);
		if (count == 0) {
			s[i].flags |= SENSOR_FINVALID;
			s[i].value = 0;
			continue;
		}
		s[i].value = 1350000 / (count * 2);
		s[i].flags &= ~SENSOR_FINVALID;
	}
}

uint8_t
nvt_readreg(struct nvt_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
nvt_writereg(struct nvt_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);
}