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