psoc.c revision 1.1 1 /* $NetBSD: psoc.c,v 1.1 2019/11/01 17:51:56 macallan Exp $ */
2
3 /*-
4 * Copyright (c) 2018 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 * fan3 - 0x59
36 * temperature sensors start at 6, two bytes each, first appears to be
37 * the temperature in degrees Celsius
38 */
39
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: psoc.c,v 1.1 2019/11/01 17:51:56 macallan Exp $");
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/device.h>
46 #include <sys/conf.h>
47 #include <sys/bus.h>
48 #include <sys/time.h>
49
50 #include <dev/ofw/openfirm.h>
51
52 #include <dev/i2c/i2cvar.h>
53
54 #include <dev/sysmon/sysmonvar.h>
55
56 struct psoc_softc {
57 device_t sc_dev;
58 i2c_tag_t sc_i2c;
59 i2c_addr_t sc_addr;
60 int sc_node;
61
62 struct sysmon_envsys *sc_sme;
63 envsys_data_t sc_sensors[7];
64 int sc_nsensors;
65 uint8_t sc_temp[16];
66 time_t sc_last;
67 };
68
69 static int psoc_match(device_t, cfdata_t, void *);
70 static void psoc_attach(device_t, device_t, void *);
71
72 static void psoc_sensors_refresh(struct sysmon_envsys *, envsys_data_t *);
73
74 CFATTACH_DECL_NEW(psoc, sizeof(struct psoc_softc),
75 psoc_match, psoc_attach, NULL, NULL);
76
77 static const struct device_compatible_entry compat_data[] = {
78 { "Psoc", 0 },
79 { NULL, 0 }
80 };
81
82 static int
83 psoc_match(device_t parent, cfdata_t match, void *aux)
84 {
85 struct i2c_attach_args *ia = aux;
86 int match_result;
87
88 if (iic_use_direct_match(ia, match, compat_data, &match_result))
89 return match_result;
90
91 return 0;
92 }
93
94 static void
95 psoc_attach(device_t parent, device_t self, void *aux)
96 {
97 struct psoc_softc *sc = device_private(self);
98 struct i2c_attach_args *ia = aux;
99 char path[256];
100 envsys_data_t *s;
101 int error, ih, r;
102
103 sc->sc_dev = self;
104 sc->sc_i2c = ia->ia_tag;
105 sc->sc_addr = ia->ia_addr;
106 sc->sc_node = ia->ia_cookie;
107 sc->sc_last = 0;
108
109 aprint_naive("\n");
110 aprint_normal(": Psoc fan controller\n");
111
112 error = OF_package_to_path(sc->sc_node, path, 256);
113 path[error] = 0;
114 printf("path [%s]\n", path);
115 ih = OF_open("fan");
116 OF_call_method_1("fan-init", ih, 0);
117 printf("ih %08x\n", ih);
118
119 sc->sc_sme = sysmon_envsys_create();
120 sc->sc_sme->sme_name = device_xname(self);
121 sc->sc_sme->sme_cookie = sc;
122 sc->sc_sme->sme_refresh = psoc_sensors_refresh;
123 sc->sc_nsensors = 0;
124
125 int i, j;
126 uint8_t data, cmd;
127 for (i = 0; i < 0x7f; i+= 8) {
128 printf("%02x:", i);
129 for (j = 0; j < 8; j++) {
130 cmd = i + j;
131 data = 0;
132 iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
133 sc->sc_addr, &cmd, 1, &data, 1, 0);
134 printf(" %02x", data);
135 }
136 printf("\n");
137 }
138 for (i = 0; i < 4; i++) {
139 r = i * 2 + 6;
140 s = &sc->sc_sensors[sc->sc_nsensors];
141 s->state = ENVSYS_SINVALID;
142 s->units = ENVSYS_STEMP;
143 snprintf(s->desc, 16, "temp%d", i);
144 s->private = r;
145 sysmon_envsys_sensor_attach(sc->sc_sme, s);
146 sc->sc_nsensors++;
147 }
148 #if 0
149 for (r = 0x31; r < 0x50; r += 0x14) {
150 s = &sc->sc_sensors[sc->sc_nsensors];
151 s->state = ENVSYS_SINVALID;
152 s->units = ENVSYS_SFANRPM;
153 snprintf(s->desc, 16, "reg %02x", r);
154 s->private = r;
155 sysmon_envsys_sensor_attach(sc->sc_sme, s);
156 sc->sc_nsensors++;
157 }
158 #endif
159 sysmon_envsys_register(sc->sc_sme);
160 }
161
162 static void
163 psoc_sensors_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
164 {
165 struct psoc_softc *sc = sme->sme_cookie;
166 uint8_t cmd = 6;
167 uint8_t buf[0x28];
168 int error = 1, data, i;
169
170 if ( edata->private < 0x20) {
171 cmd = 0;
172 if ((time_second - sc->sc_last) > 2) {
173 iic_acquire_bus(sc->sc_i2c, 0);
174 error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
175 sc->sc_addr, &cmd, 1, sc->sc_temp, 16, 0);
176 iic_release_bus(sc->sc_i2c, 0);
177 if (error) return;
178 sc->sc_last = time_second;
179 }
180 error = 0;
181 if (edata->private > 0) {
182 data = sc->sc_temp[edata->private];
183 /* Celsius -> microkelvin */
184 edata->value_cur = ((int)data * 1000000) + 273150000;
185 }
186 } else {
187 cmd = 0x31;
188 iic_acquire_bus(sc->sc_i2c, 0);
189 error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
190 sc->sc_addr, &cmd, 1, buf, 0x28, 0);
191 iic_release_bus(sc->sc_i2c, 0);
192 if (error) return;
193 if (edata->private > 0) {
194 data = buf[edata->private - 0x20];
195 edata->value_cur = data;
196 for (i = 0; i < 14; i++)
197 printf(" %02x", buf[edata->private - 0x31 + i]);
198 printf("\n");
199 }
200 }
201 if (error) {
202 edata->state = ENVSYS_SINVALID;
203 } else {
204 edata->state = ENVSYS_SVALID;
205 }
206 }
207