1 /* $NetBSD: auxfan.c,v 1.1 2026/07/26 12:53:13 jdc Exp $ */ 2 3 /*- 4 * Copyright (c) 2026 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Julian Coleman. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: auxfan.c,v 1.1 2026/07/26 12:53:13 jdc Exp $"); 34 35 #include <sys/param.h> 36 #include <sys/kernel.h> 37 #include <sys/device.h> 38 #include <sys/proc.h> 39 #include <sys/types.h> 40 41 #include <sys/bus.h> 42 #include <machine/autoconf.h> 43 44 #include <dev/sysmon/sysmonvar.h> 45 46 #include <dev/i2c/i2cvar.h> 47 #include <arch/sparc64/dev/auxfanreg.h> 48 49 #define VAL_TO_SPEED(val) ((81680 * 60) / val) /* Could be +/- 70 */ 50 51 struct auxfan_softc { 52 device_t sc_dev; 53 54 i2c_tag_t sc_tag; 55 int sc_address; 56 57 struct sysmon_envsys *sc_sme; 58 envsys_data_t sc_sensor; 59 }; 60 61 static int auxfan_match(device_t, cfdata_t, void *); 62 static void auxfan_attach(device_t, device_t, void *); 63 void auxfan_refresh(struct sysmon_envsys *, envsys_data_t *); 64 65 CFATTACH_DECL_NEW(auxfan, sizeof(struct auxfan_softc), 66 auxfan_match, auxfan_attach, NULL, NULL); 67 68 69 static const struct device_compatible_entry compat_data[] = { 70 { .compat = "SUNW,i2c-auxfan1" }, 71 DEVICE_COMPAT_EOL 72 }; 73 74 static int 75 auxfan_match(device_t parent, cfdata_t cf, void *aux) 76 { 77 struct i2c_attach_args *ia = aux; 78 int match_result; 79 80 if (iic_use_direct_match(ia, cf, compat_data, &match_result)) 81 return match_result; 82 83 return 0; 84 } 85 86 static void 87 auxfan_attach(device_t parent, device_t self, void *aux) 88 { 89 struct auxfan_softc *sc = device_private(self); 90 struct i2c_attach_args *ia = aux; 91 92 sc->sc_tag = ia->ia_tag; 93 sc->sc_address = ia->ia_addr; 94 sc->sc_dev = self; 95 96 aprint_normal(": DIMM fan\n"); 97 98 sc->sc_sme = sysmon_envsys_create(); 99 sc->sc_sme->sme_name = device_xname(self); 100 sc->sc_sme->sme_cookie = sc; 101 102 sc->sc_sme->sme_refresh = auxfan_refresh; 103 strlcpy(sc->sc_sensor.desc, "dimm-fan", 104 sizeof(sc->sc_sensor.desc)); 105 sc->sc_sensor.units = ENVSYS_SFANRPM; 106 sc->sc_sensor.state = ENVSYS_SINVALID; 107 sc->sc_sensor.flags = ENVSYS_FMONCRITICAL; 108 if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor)) { 109 sysmon_envsys_destroy(sc->sc_sme); 110 sc->sc_sme = NULL; 111 aprint_error_dev(sc->sc_dev, 112 "unable to attach sensor to sysmon\n"); 113 return; 114 } 115 if (sysmon_envsys_register(sc->sc_sme)) { 116 aprint_error_dev(sc->sc_dev, 117 "unable to register with sysmon\n"); 118 sysmon_envsys_destroy(sc->sc_sme); 119 sc->sc_sme = NULL; 120 return; 121 } 122 123 return; 124 } 125 126 void 127 auxfan_refresh(struct sysmon_envsys *sme, envsys_data_t *edata) 128 { 129 struct auxfan_softc *sc = sme->sme_cookie; 130 int err = 0; 131 uint8_t reg, val; 132 uint16_t count; 133 134 if ((err = iic_acquire_bus(sc->sc_tag, 0)) != 0) { 135 edata->state = ENVSYS_SINVALID; 136 return; 137 } 138 139 reg = AUXFAN_DIMM_FAN_SPD_LO; 140 err = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, 141 sc->sc_address, ®, 1, &val, 1, 0); 142 if (err) { 143 iic_release_bus(sc->sc_tag, 0); 144 edata->state = ENVSYS_SINVALID; 145 return; 146 } 147 count = val; 148 149 reg = AUXFAN_DIMM_FAN_SPD_HI; 150 err = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, 151 sc->sc_address, ®, 1, &val, 1, 0); 152 iic_release_bus(sc->sc_tag, 0); 153 if (err) { 154 edata->state = ENVSYS_SINVALID; 155 return; 156 } 157 count += (val << 8); 158 159 if (count == 0xffff || count == 0x0) { /* Fan stopped */ 160 edata->value_cur = 0; 161 edata->state = ENVSYS_SCRITICAL; 162 } else { 163 edata->value_cur = VAL_TO_SPEED(count); 164 edata->state = ENVSYS_SVALID; 165 } 166 } 167