psoc.c revision 1.5 1 /* $NetBSD: psoc.c,v 1.5 2021/01/25 14:20:39 thorpej 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.5 2021/01/25 14:20:39 thorpej 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 struct psoc_softc {
61 device_t sc_dev;
62 i2c_tag_t sc_i2c;
63 i2c_addr_t sc_addr;
64 int sc_node;
65
66 struct sysmon_envsys *sc_sme;
67 envsys_data_t sc_sensors[7];
68 int sc_nsensors;
69 uint8_t sc_temp[16];
70 time_t sc_last;
71 };
72
73 static int psoc_match(device_t, cfdata_t, void *);
74 static void psoc_attach(device_t, device_t, void *);
75
76 static void psoc_sensors_refresh(struct sysmon_envsys *, envsys_data_t *);
77
78 static void psoc_dump(struct psoc_softc *);
79
80 CFATTACH_DECL_NEW(psoc, sizeof(struct psoc_softc),
81 psoc_match, psoc_attach, NULL, NULL);
82
83 static const struct device_compatible_entry compat_data[] = {
84 { .compat = "Psoc" },
85 { }
86 };
87
88 static int
89 psoc_match(device_t parent, cfdata_t match, void *aux)
90 {
91 struct i2c_attach_args *ia = aux;
92 int match_result;
93
94 if (iic_use_direct_match(ia, match, compat_data, &match_result))
95 return match_result;
96
97 return 0;
98 }
99
100 static void
101 psoc_attach(device_t parent, device_t self, void *aux)
102 {
103 struct psoc_softc *sc = device_private(self);
104 struct i2c_attach_args *ia = aux;
105 char path[256];
106 envsys_data_t *s;
107 int error, ih, r, i;
108
109 sc->sc_dev = self;
110 sc->sc_i2c = ia->ia_tag;
111 sc->sc_addr = ia->ia_addr;
112 sc->sc_node = ia->ia_cookie;
113 sc->sc_last = 0;
114
115 aprint_naive("\n");
116 aprint_normal(": Psoc fan controller\n");
117
118 error = OF_package_to_path(sc->sc_node, path, 256);
119 path[error] = 0;
120 printf("path [%s]\n", path);
121 ih = OF_open("fan");
122 OF_call_method_1("fan-init", ih, 0);
123 printf("ih %08x\n", ih);
124
125 sc->sc_sme = sysmon_envsys_create();
126 sc->sc_sme->sme_name = device_xname(self);
127 sc->sc_sme->sme_cookie = sc;
128 sc->sc_sme->sme_refresh = psoc_sensors_refresh;
129 sc->sc_nsensors = 0;
130
131 psoc_dump(sc);
132
133 for (i = 0; i < 4; i++) {
134 r = i * 2 + 6;
135 s = &sc->sc_sensors[sc->sc_nsensors];
136 s->state = ENVSYS_SINVALID;
137 s->units = ENVSYS_STEMP;
138 snprintf(s->desc, 16, "temp%d", i);
139 s->private = r;
140 sysmon_envsys_sensor_attach(sc->sc_sme, s);
141 sc->sc_nsensors++;
142 }
143
144 for (r = 0x20; r < 0x2b; r += 0x06) {
145 s = &sc->sc_sensors[sc->sc_nsensors];
146 s->state = ENVSYS_SINVALID;
147 s->units = ENVSYS_SFANRPM;
148 snprintf(s->desc, 16, "reg %02x", r);
149 s->private = r;
150 sysmon_envsys_sensor_attach(sc->sc_sme, s);
151 sc->sc_nsensors++;
152 }
153
154 sysmon_envsys_register(sc->sc_sme);
155 }
156
157 static void
158 psoc_sensors_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
159 {
160 struct psoc_softc *sc = sme->sme_cookie;
161 uint8_t cmd = 6;
162 uint8_t buf[0x28];
163 int error = 1, data;
164
165 if ( edata->private < 0x20) {
166 cmd = 0;
167 if ((time_second - sc->sc_last) > 2) {
168 iic_acquire_bus(sc->sc_i2c, 0);
169 error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
170 sc->sc_addr, &cmd, 1, sc->sc_temp, 16, 0);
171 iic_release_bus(sc->sc_i2c, 0);
172 if (error) return;
173 sc->sc_last = time_second;
174 }
175 error = 0;
176 if (edata->private > 0) {
177 data = sc->sc_temp[edata->private];
178 /* Celsius -> microkelvin */
179 edata->value_cur = ((int)data * 1000000) + 273150000;
180 }
181 #ifdef PSOC_DEBUG
182 if (edata->private == 6)
183 psoc_dump(sc);
184 #endif
185 } else {
186 cmd = edata->private;
187 iic_acquire_bus(sc->sc_i2c, 0);
188 error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
189 sc->sc_addr, &cmd, 1, buf, 3, 0);
190 iic_release_bus(sc->sc_i2c, 0);
191 if (error) return;
192 switch (buf[0] & 0xf0) {
193 case 0x50:
194 data = buf[edata->private - 0x20];
195 edata->value_cur = ((buf[2] & 0x3f) << 6) |
196 (buf[1] & 0x3f);
197 break;
198 case 0x60:
199 edata->value_cur = 0;
200 break;
201 default:
202 error = 0;
203 }
204 }
205 if (error) {
206 edata->state = ENVSYS_SINVALID;
207 } else {
208 edata->state = ENVSYS_SVALID;
209 }
210 }
211
212 static void
213 psoc_dump(struct psoc_softc *sc)
214 {
215 int i, j;
216 uint8_t data, cmd;
217 for (i = 0x20; i < 0x5f; i+= 8) {
218 printf("%02x:", i);
219 for (j = 0; j < 8; j++) {
220 cmd = i + j;
221 data = 0;
222 iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
223 sc->sc_addr, &cmd, 1, &data, 1, 0);
224 printf(" %02x", data);
225 }
226 printf("\n");
227 }
228 }
229