summaryrefslogtreecommitdiff
path: root/sys/arch/macppc/dev/psoc.c
blob: 8eaf459efb1d64b4d0e57f91bd171f7c3f746940 (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
 /* $NetBSD: psoc.c,v 1.8 2021/06/18 23:00:47 macallan Exp $ */

/*-
 * Copyright (c) 2019 Michael Lorenz
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * fan controller found in 1GHz TiBook
 *
 * register values from OF:
 * fan1 - 0x20 ( status ), 0x31 ( data )
 * fan2 - 0x26 ( status ), 0x45 ( data )
 * fan status byte 0:
 * 0x5* - fan is running, 0x6* - fan stopped
 * byte 1: unknown, 0x80 seems always set, lower bits seem to fluctuate
 * byte 2: lower 6 bit seem to indicate speed
 * fan speed may be lower 6 bit of byte 2 and lower 6 of byte 1 
 * temperature sensors start at 6, two bytes each, first appears to be
 * the temperature in degrees Celsius
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: psoc.c,v 1.8 2021/06/18 23:00:47 macallan Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/conf.h>
#include <sys/bus.h>
#include <sys/time.h>

#include <dev/ofw/openfirm.h>

#include <dev/i2c/i2cvar.h>

#include <dev/sysmon/sysmonvar.h>

#include "opt_psoc.h"
#ifdef PSOC_DEBUG
#define DPRINTF printf
#else
#define DPRINTF if (0) printf
#endif

struct psoc_softc {
	device_t	sc_dev;
	i2c_tag_t	sc_i2c;
	i2c_addr_t	sc_addr;
	int		sc_node;

	struct sysmon_envsys *sc_sme;
	envsys_data_t	sc_sensors[7];
	int		sc_nsensors;
	uint8_t		sc_temp[16];
	time_t		sc_last;
};

static int	psoc_match(device_t, cfdata_t, void *);
static void	psoc_attach(device_t, device_t, void *);

static void	psoc_sensors_refresh(struct sysmon_envsys *, envsys_data_t *);

static void	psoc_dump(struct psoc_softc *);

CFATTACH_DECL_NEW(psoc, sizeof(struct psoc_softc),
    psoc_match, psoc_attach, NULL, NULL);

static const struct device_compatible_entry compat_data[] = {
	{ .compat = "Psoc" },
	DEVICE_COMPAT_EOL
};

static int
psoc_match(device_t parent, cfdata_t match, void *aux)
{
	struct i2c_attach_args *ia = aux;
	int match_result;

	if (iic_use_direct_match(ia, match, compat_data, &match_result))
		return match_result;

	return 0;
}

static void
psoc_attach(device_t parent, device_t self, void *aux)
{
	struct psoc_softc *sc = device_private(self);
	struct i2c_attach_args *ia = aux;
	char path[256];
	envsys_data_t *s;
	int error, ih, r, i;

	sc->sc_dev = self;
	sc->sc_i2c = ia->ia_tag;
	sc->sc_addr = ia->ia_addr;
	sc->sc_node = ia->ia_cookie;
	sc->sc_last = 0;

	aprint_naive("\n");
	aprint_normal(": Psoc fan controller\n");

	error = OF_package_to_path(sc->sc_node, path, 256);
	path[error] = 0;
	DPRINTF("path [%s]\n", path);
	ih = OF_open("fan");
	OF_call_method_1("fan-init", ih, 0);
	DPRINTF("ih %08x\n", ih);

	sc->sc_sme = sysmon_envsys_create();
	sc->sc_sme->sme_name = device_xname(self);
	sc->sc_sme->sme_cookie = sc;
	sc->sc_sme->sme_refresh = psoc_sensors_refresh;
	sc->sc_nsensors = 0;

	psoc_dump(sc);

	for (i = 0; i < 4; i++) {
		r = i * 2 + 6;
		s = &sc->sc_sensors[sc->sc_nsensors];
		s->state = ENVSYS_SINVALID;
		s->units = ENVSYS_STEMP;
		snprintf(s->desc, 16, "temp%d", i);
		s->private = r;
		sysmon_envsys_sensor_attach(sc->sc_sme, s);
		sc->sc_nsensors++;
	}

	for (r = 0x20; r < 0x2b; r += 0x06) {
		s = &sc->sc_sensors[sc->sc_nsensors];
		s->state = ENVSYS_SINVALID;
		s->units = ENVSYS_SFANRPM;
		snprintf(s->desc, 16, "reg %02x", r);
		s->private = r;
		sysmon_envsys_sensor_attach(sc->sc_sme, s);
		sc->sc_nsensors++;
	}

	sysmon_envsys_register(sc->sc_sme);
}

static void
psoc_sensors_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
{
	struct psoc_softc *sc = sme->sme_cookie;
	uint8_t cmd = 6;
	uint8_t buf[0x28];
	int error = 1, data;

	if ( edata->private < 0x20) {
		cmd = 0;
		if ((time_second - sc->sc_last) > 2) {
			iic_acquire_bus(sc->sc_i2c, 0);
			error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
				    sc->sc_addr, &cmd, 1, sc->sc_temp, 16, 0);
			iic_release_bus(sc->sc_i2c, 0);
			if (error) return;
			sc->sc_last = time_second;
		}
		error = 0;
		if (edata->private > 0) {
			data = sc->sc_temp[edata->private];
			/* Celsius -> microkelvin */
			edata->value_cur = ((int)data * 1000000) + 273150000;
		}
#ifdef PSOC_DEBUG
		if (edata->private == 6)
			psoc_dump(sc);
#endif
	} else {
		cmd = edata->private;
		iic_acquire_bus(sc->sc_i2c, 0);
		error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
			    sc->sc_addr, &cmd, 1, buf, 3, 0);
		iic_release_bus(sc->sc_i2c, 0);
		if (error) return;
		switch (buf[0] & 0xf0) {
			case 0x50:
				data = buf[edata->private - 0x20];
				edata->value_cur = ((buf[2] & 0x3f) << 6) |
						    (buf[1] & 0x3f);
				break;
			case 0x60:
				edata->value_cur = 0;
				break;
			default:
				error = 0;
		}	
	}
	if (error) {
		edata->state = ENVSYS_SINVALID;
	} else {
		edata->state = ENVSYS_SVALID;
	}
}

static void
psoc_dump(struct psoc_softc *sc)
{
	int i, j;
	uint8_t data, cmd;

	iic_acquire_bus(sc->sc_i2c, 0);
	for (i = 0x20; i < 0x5f; i+= 8) {
		printf("%02x:", i);
		for (j = 0; j < 8; j++) {
			cmd = i + j;
			data = 0;
			iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
			    sc->sc_addr, &cmd, 1, &data, 1, 0);
			printf(" %02x", data);
		}
		printf("\n");
	}
	iic_release_bus(sc->sc_i2c, 0);
}