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