xref: /netbsd-src/sys/arch/macppc/dev/psoc.c (revision 8f6fb76507c4e4917cf1aa63b3ca3817038c9543)
1  /* $NetBSD: psoc.c,v 1.8 2021/06/18 23:00:47 macallan Exp $ */
2 
3 /*-
4  * Copyright (c) 2019 Michael Lorenz
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*
30  * fan controller found in 1GHz TiBook
31  *
32  * register values from OF:
33  * fan1 - 0x20 ( status ), 0x31 ( data )
34  * fan2 - 0x26 ( status ), 0x45 ( data )
35  * fan status byte 0:
36  * 0x5* - fan is running, 0x6* - fan stopped
37  * byte 1: unknown, 0x80 seems always set, lower bits seem to fluctuate
38  * byte 2: lower 6 bit seem to indicate speed
39  * fan speed may be lower 6 bit of byte 2 and lower 6 of byte 1
40  * temperature sensors start at 6, two bytes each, first appears to be
41  * the temperature in degrees Celsius
42  */
43 
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: psoc.c,v 1.8 2021/06/18 23:00:47 macallan Exp $");
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/device.h>
50 #include <sys/conf.h>
51 #include <sys/bus.h>
52 #include <sys/time.h>
53 
54 #include <dev/ofw/openfirm.h>
55 
56 #include <dev/i2c/i2cvar.h>
57 
58 #include <dev/sysmon/sysmonvar.h>
59 
60 #include "opt_psoc.h"
61 #ifdef PSOC_DEBUG
62 #define DPRINTF printf
63 #else
64 #define DPRINTF if (0) printf
65 #endif
66 
67 struct psoc_softc {
68 	device_t	sc_dev;
69 	i2c_tag_t	sc_i2c;
70 	i2c_addr_t	sc_addr;
71 	int		sc_node;
72 
73 	struct sysmon_envsys *sc_sme;
74 	envsys_data_t	sc_sensors[7];
75 	int		sc_nsensors;
76 	uint8_t		sc_temp[16];
77 	time_t		sc_last;
78 };
79 
80 static int	psoc_match(device_t, cfdata_t, void *);
81 static void	psoc_attach(device_t, device_t, void *);
82 
83 static void	psoc_sensors_refresh(struct sysmon_envsys *, envsys_data_t *);
84 
85 static void	psoc_dump(struct psoc_softc *);
86 
87 CFATTACH_DECL_NEW(psoc, sizeof(struct psoc_softc),
88     psoc_match, psoc_attach, NULL, NULL);
89 
90 static const struct device_compatible_entry compat_data[] = {
91 	{ .compat = "Psoc" },
92 	DEVICE_COMPAT_EOL
93 };
94 
95 static int
psoc_match(device_t parent,cfdata_t match,void * aux)96 psoc_match(device_t parent, cfdata_t match, void *aux)
97 {
98 	struct i2c_attach_args *ia = aux;
99 	int match_result;
100 
101 	if (iic_use_direct_match(ia, match, compat_data, &match_result))
102 		return match_result;
103 
104 	return 0;
105 }
106 
107 static void
psoc_attach(device_t parent,device_t self,void * aux)108 psoc_attach(device_t parent, device_t self, void *aux)
109 {
110 	struct psoc_softc *sc = device_private(self);
111 	struct i2c_attach_args *ia = aux;
112 	char path[256];
113 	envsys_data_t *s;
114 	int error, ih, r, i;
115 
116 	sc->sc_dev = self;
117 	sc->sc_i2c = ia->ia_tag;
118 	sc->sc_addr = ia->ia_addr;
119 	sc->sc_node = ia->ia_cookie;
120 	sc->sc_last = 0;
121 
122 	aprint_naive("\n");
123 	aprint_normal(": Psoc fan controller\n");
124 
125 	error = OF_package_to_path(sc->sc_node, path, 256);
126 	path[error] = 0;
127 	DPRINTF("path [%s]\n", path);
128 	ih = OF_open("fan");
129 	OF_call_method_1("fan-init", ih, 0);
130 	DPRINTF("ih %08x\n", ih);
131 
132 	sc->sc_sme = sysmon_envsys_create();
133 	sc->sc_sme->sme_name = device_xname(self);
134 	sc->sc_sme->sme_cookie = sc;
135 	sc->sc_sme->sme_refresh = psoc_sensors_refresh;
136 	sc->sc_nsensors = 0;
137 
138 	psoc_dump(sc);
139 
140 	for (i = 0; i < 4; i++) {
141 		r = i * 2 + 6;
142 		s = &sc->sc_sensors[sc->sc_nsensors];
143 		s->state = ENVSYS_SINVALID;
144 		s->units = ENVSYS_STEMP;
145 		snprintf(s->desc, 16, "temp%d", i);
146 		s->private = r;
147 		sysmon_envsys_sensor_attach(sc->sc_sme, s);
148 		sc->sc_nsensors++;
149 	}
150 
151 	for (r = 0x20; r < 0x2b; r += 0x06) {
152 		s = &sc->sc_sensors[sc->sc_nsensors];
153 		s->state = ENVSYS_SINVALID;
154 		s->units = ENVSYS_SFANRPM;
155 		snprintf(s->desc, 16, "reg %02x", r);
156 		s->private = r;
157 		sysmon_envsys_sensor_attach(sc->sc_sme, s);
158 		sc->sc_nsensors++;
159 	}
160 
161 	sysmon_envsys_register(sc->sc_sme);
162 }
163 
164 static void
psoc_sensors_refresh(struct sysmon_envsys * sme,envsys_data_t * edata)165 psoc_sensors_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
166 {
167 	struct psoc_softc *sc = sme->sme_cookie;
168 	uint8_t cmd = 6;
169 	uint8_t buf[0x28];
170 	int error = 1, data;
171 
172 	if ( edata->private < 0x20) {
173 		cmd = 0;
174 		if ((time_second - sc->sc_last) > 2) {
175 			iic_acquire_bus(sc->sc_i2c, 0);
176 			error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
177 				    sc->sc_addr, &cmd, 1, sc->sc_temp, 16, 0);
178 			iic_release_bus(sc->sc_i2c, 0);
179 			if (error) return;
180 			sc->sc_last = time_second;
181 		}
182 		error = 0;
183 		if (edata->private > 0) {
184 			data = sc->sc_temp[edata->private];
185 			/* Celsius -> microkelvin */
186 			edata->value_cur = ((int)data * 1000000) + 273150000;
187 		}
188 #ifdef PSOC_DEBUG
189 		if (edata->private == 6)
190 			psoc_dump(sc);
191 #endif
192 	} else {
193 		cmd = edata->private;
194 		iic_acquire_bus(sc->sc_i2c, 0);
195 		error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
196 			    sc->sc_addr, &cmd, 1, buf, 3, 0);
197 		iic_release_bus(sc->sc_i2c, 0);
198 		if (error) return;
199 		switch (buf[0] & 0xf0) {
200 			case 0x50:
201 				data = buf[edata->private - 0x20];
202 				edata->value_cur = ((buf[2] & 0x3f) << 6) |
203 						    (buf[1] & 0x3f);
204 				break;
205 			case 0x60:
206 				edata->value_cur = 0;
207 				break;
208 			default:
209 				error = 0;
210 		}
211 	}
212 	if (error) {
213 		edata->state = ENVSYS_SINVALID;
214 	} else {
215 		edata->state = ENVSYS_SVALID;
216 	}
217 }
218 
219 static void
psoc_dump(struct psoc_softc * sc)220 psoc_dump(struct psoc_softc *sc)
221 {
222 	int i, j;
223 	uint8_t data, cmd;
224 
225 	iic_acquire_bus(sc->sc_i2c, 0);
226 	for (i = 0x20; i < 0x5f; i+= 8) {
227 		printf("%02x:", i);
228 		for (j = 0; j < 8; j++) {
229 			cmd = i + j;
230 			data = 0;
231 			iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
232 			    sc->sc_addr, &cmd, 1, &data, 1, 0);
233 			printf(" %02x", data);
234 		}
235 		printf("\n");
236 	}
237 	iic_release_bus(sc->sc_i2c, 0);
238 }
239