Home | History | Annotate | Line # | Download | only in ic
nslm7x.c revision 1.31
      1  1.31   xtraeme /*	$NetBSD: nslm7x.c,v 1.31 2007/03/11 21:23:22 xtraeme Exp $ */
      2   1.1      groo 
      3   1.1      groo /*-
      4   1.1      groo  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5   1.1      groo  * All rights reserved.
      6   1.1      groo  *
      7   1.1      groo  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1      groo  * by Bill Squier.
      9   1.1      groo  *
     10   1.1      groo  * Redistribution and use in source and binary forms, with or without
     11   1.1      groo  * modification, are permitted provided that the following conditions
     12   1.1      groo  * are met:
     13   1.1      groo  * 1. Redistributions of source code must retain the above copyright
     14   1.1      groo  *    notice, this list of conditions and the following disclaimer.
     15   1.1      groo  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1      groo  *    notice, this list of conditions and the following disclaimer in the
     17   1.1      groo  *    documentation and/or other materials provided with the distribution.
     18   1.1      groo  * 3. All advertising materials mentioning features or use of this software
     19   1.1      groo  *    must display the following acknowledgement:
     20   1.1      groo  *        This product includes software developed by the NetBSD
     21   1.1      groo  *        Foundation, Inc. and its contributors.
     22   1.1      groo  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.1      groo  *    contributors may be used to endorse or promote products derived
     24   1.1      groo  *    from this software without specific prior written permission.
     25   1.1      groo  *
     26   1.1      groo  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.1      groo  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.1      groo  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.1      groo  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.1      groo  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.1      groo  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.1      groo  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.1      groo  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.1      groo  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.1      groo  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.1      groo  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1      groo  */
     38  1.13     lukem 
     39  1.13     lukem #include <sys/cdefs.h>
     40  1.31   xtraeme __KERNEL_RCSID(0, "$NetBSD: nslm7x.c,v 1.31 2007/03/11 21:23:22 xtraeme Exp $");
     41   1.1      groo 
     42   1.1      groo #include <sys/param.h>
     43   1.1      groo #include <sys/systm.h>
     44   1.1      groo #include <sys/kernel.h>
     45   1.1      groo #include <sys/proc.h>
     46   1.1      groo #include <sys/device.h>
     47   1.1      groo #include <sys/conf.h>
     48   1.1      groo #include <sys/time.h>
     49   1.1      groo 
     50   1.1      groo #include <machine/bus.h>
     51   1.1      groo 
     52   1.1      groo #include <dev/isa/isareg.h>
     53   1.1      groo #include <dev/isa/isavar.h>
     54   1.1      groo 
     55   1.4   thorpej #include <dev/sysmon/sysmonvar.h>
     56   1.4   thorpej 
     57   1.1      groo #include <dev/ic/nslm7xvar.h>
     58   1.1      groo 
     59   1.1      groo #include <machine/intr.h>
     60   1.1      groo 
     61   1.1      groo #if defined(LMDEBUG)
     62  1.30   xtraeme #define DPRINTF(x)	do { printf x; } while (0)
     63   1.1      groo #else
     64   1.1      groo #define DPRINTF(x)
     65   1.1      groo #endif
     66   1.1      groo 
     67  1.30   xtraeme /*
     68  1.30   xtraeme  * LM78-compatible chips can typically measure voltages up to 4.096 V.
     69  1.30   xtraeme  * To measure higher voltages the input is attenuated with (external)
     70  1.30   xtraeme  * resistors.  Negative voltages are measured using inverting op amps
     71  1.30   xtraeme  * and resistors.  So we have to convert the sensor values back to
     72  1.30   xtraeme  * real voltages by applying the appropriate resistor factor.
     73  1.30   xtraeme  */
     74  1.30   xtraeme #define RFACT_NONE	10000
     75  1.30   xtraeme #define RFACT(x, y)	(RFACT_NONE * ((x) + (y)) / (y))
     76  1.30   xtraeme #define NRFACT(x, y)	(-RFACT_NONE * (x) / (y))
     77  1.30   xtraeme 
     78   1.4   thorpej const struct envsys_range lm_ranges[] = {	/* sc->sensors sub-intervals */
     79  1.30   xtraeme 						/* for each unit type */
     80   1.1      groo 	{ 7, 7,    ENVSYS_STEMP   },
     81   1.1      groo 	{ 8, 10,   ENVSYS_SFANRPM },
     82   1.1      groo 	{ 1, 0,    ENVSYS_SVOLTS_AC },	/* None */
     83   1.1      groo 	{ 0, 6,    ENVSYS_SVOLTS_DC },
     84   1.1      groo 	{ 1, 0,    ENVSYS_SOHMS },	/* None */
     85   1.1      groo 	{ 1, 0,    ENVSYS_SWATTS },	/* None */
     86   1.1      groo 	{ 1, 0,    ENVSYS_SAMPS }	/* None */
     87   1.1      groo };
     88   1.1      groo 
     89  1.30   xtraeme static int lm_match(struct lm_softc *);
     90  1.30   xtraeme static int wb_match(struct lm_softc *);
     91  1.30   xtraeme static int def_match(struct lm_softc *);
     92  1.30   xtraeme 
     93  1.30   xtraeme static void lm_generic_banksel(struct lm_softc *, int);
     94  1.30   xtraeme static void lm_setup_sensors(struct lm_softc *, struct lm_sensor *);
     95  1.30   xtraeme 
     96  1.30   xtraeme static void lm_refresh_sensor_data(struct lm_softc *);
     97  1.30   xtraeme static void lm_refresh_volt(struct lm_softc *, int);
     98  1.30   xtraeme static void lm_refresh_temp(struct lm_softc *, int);
     99  1.30   xtraeme static void lm_refresh_fanrpm(struct lm_softc *, int);
    100  1.30   xtraeme 
    101  1.30   xtraeme static void wb_refresh_sensor_data(struct lm_softc *);
    102  1.30   xtraeme static void wb_w83637hf_refresh_vcore(struct lm_softc *, int);
    103  1.30   xtraeme static void wb_refresh_nvolt(struct lm_softc *, int);
    104  1.30   xtraeme static void wb_w83627ehf_refresh_nvolt(struct lm_softc *, int);
    105  1.30   xtraeme static void wb_refresh_temp(struct lm_softc *, int);
    106  1.30   xtraeme static void wb_refresh_fanrpm(struct lm_softc *, int);
    107  1.30   xtraeme static void wb_w83792d_refresh_fanrpm(struct lm_softc *, int);
    108   1.5    bouyer 
    109  1.30   xtraeme static void as_refresh_temp(struct lm_softc *, int);
    110  1.20     perry 
    111  1.30   xtraeme static int lm_gtredata(struct sysmon_envsys *, struct envsys_tre_data *);
    112  1.30   xtraeme static int generic_streinfo_fan(struct lm_softc *, struct envsys_basic_info *,
    113  1.20     perry            int, struct envsys_basic_info *);
    114  1.30   xtraeme static int lm_streinfo(struct sysmon_envsys *, struct envsys_basic_info *);
    115  1.30   xtraeme static int wb781_streinfo(struct sysmon_envsys *, struct envsys_basic_info *);
    116  1.30   xtraeme static int wb782_streinfo(struct sysmon_envsys *, struct envsys_basic_info *);
    117   1.5    bouyer 
    118   1.5    bouyer struct lm_chip {
    119  1.20     perry 	int (*chip_match)(struct lm_softc *);
    120   1.5    bouyer };
    121   1.5    bouyer 
    122  1.30   xtraeme static struct lm_chip lm_chips[] = {
    123   1.8    bouyer 	{ wb_match },
    124   1.8    bouyer 	{ lm_match },
    125   1.8    bouyer 	{ def_match } /* Must be last */
    126   1.5    bouyer };
    127   1.5    bouyer 
    128  1.30   xtraeme static struct lm_sensor lm78_sensors[] = {
    129  1.30   xtraeme 	/* Voltage */
    130  1.30   xtraeme 	{ "VCore A", ENVSYS_SVOLTS_DC, 0, 0x20, lm_refresh_volt, RFACT_NONE },
    131  1.30   xtraeme 	{ "VCore B", ENVSYS_SVOLTS_DC, 0, 0x21, lm_refresh_volt, RFACT_NONE },
    132  1.30   xtraeme 	{ "+3.3V", ENVSYS_SVOLTS_DC, 0, 0x22, lm_refresh_volt, RFACT_NONE },
    133  1.30   xtraeme 	{ "+5V", ENVSYS_SVOLTS_DC, 0, 0x23, lm_refresh_volt, RFACT(68, 100) },
    134  1.30   xtraeme 	{ "+12V", ENVSYS_SVOLTS_DC, 0, 0x24, lm_refresh_volt, RFACT(30, 10) },
    135  1.30   xtraeme 	{ "-12V", ENVSYS_SVOLTS_DC, 0, 0x25, lm_refresh_volt, NRFACT(240, 60) },
    136  1.30   xtraeme 	{ "-5V", ENVSYS_SVOLTS_DC, 0, 0x26, lm_refresh_volt, NRFACT(100, 60) },
    137  1.30   xtraeme 
    138  1.30   xtraeme 	/* Temperature */
    139  1.31   xtraeme 	{ "Temp0", ENVSYS_STEMP, 0, 0x27, lm_refresh_temp, 0 },
    140  1.30   xtraeme 
    141  1.30   xtraeme 	/* Fans */
    142  1.31   xtraeme 	{ "Fan0", ENVSYS_SFANRPM, 0, 0x28, lm_refresh_fanrpm, 0 },
    143  1.31   xtraeme 	{ "Fan1", ENVSYS_SFANRPM, 0, 0x29, lm_refresh_fanrpm, 0 },
    144  1.31   xtraeme 	{ "Fan2", ENVSYS_SFANRPM, 0, 0x2a, lm_refresh_fanrpm, 0 },
    145  1.30   xtraeme 
    146  1.30   xtraeme 	{ NULL }
    147  1.30   xtraeme };
    148  1.30   xtraeme 
    149  1.30   xtraeme static struct lm_sensor w83627hf_sensors[] = {
    150  1.30   xtraeme 	/* Voltage */
    151  1.30   xtraeme 	{ "VCore A", ENVSYS_SVOLTS_DC, 0, 0x20, lm_refresh_volt, RFACT_NONE },
    152  1.30   xtraeme 	{ "VCore B", ENVSYS_SVOLTS_DC, 0, 0x21, lm_refresh_volt, RFACT_NONE },
    153  1.30   xtraeme 	{ "+3.3V", ENVSYS_SVOLTS_DC, 0, 0x22, lm_refresh_volt, RFACT_NONE },
    154  1.30   xtraeme 	{ "+5V", ENVSYS_SVOLTS_DC, 0, 0x23, lm_refresh_volt, RFACT(34, 50) },
    155  1.30   xtraeme 	{ "+12V", ENVSYS_SVOLTS_DC, 0, 0x24, lm_refresh_volt, RFACT(28, 10) },
    156  1.30   xtraeme 	{ "-12V", ENVSYS_SVOLTS_DC, 0, 0x25, wb_refresh_nvolt, RFACT(232, 56) },
    157  1.30   xtraeme 	{ "-5V", ENVSYS_SVOLTS_DC, 0, 0x26, wb_refresh_nvolt, RFACT(120, 56) },
    158  1.30   xtraeme 	{ "5VSB", ENVSYS_SVOLTS_DC, 5, 0x50, lm_refresh_volt, RFACT(17, 33) },
    159  1.30   xtraeme 	{ "VBAT", ENVSYS_SVOLTS_DC, 5, 0x51, lm_refresh_volt, RFACT_NONE },
    160  1.30   xtraeme 
    161  1.30   xtraeme 	/* Temperature */
    162  1.31   xtraeme 	{ "Temp0", ENVSYS_STEMP, 0, 0x27, lm_refresh_temp, 0 },
    163  1.31   xtraeme 	{ "Temp1", ENVSYS_STEMP, 1, 0x50, wb_refresh_temp, 0 },
    164  1.31   xtraeme 	{ "Temp2", ENVSYS_STEMP, 2, 0x50, wb_refresh_temp, 0 },
    165  1.30   xtraeme 
    166  1.30   xtraeme 	/* Fans */
    167  1.31   xtraeme 	{ "Fan0", ENVSYS_SFANRPM, 0, 0x28, wb_refresh_fanrpm, 0 },
    168  1.31   xtraeme 	{ "Fan1", ENVSYS_SFANRPM, 0, 0x29, wb_refresh_fanrpm, 0 },
    169  1.31   xtraeme 	{ "Fan2", ENVSYS_SFANRPM, 0, 0x2a, wb_refresh_fanrpm, 0 },
    170  1.30   xtraeme 
    171  1.30   xtraeme 	{ NULL }
    172  1.30   xtraeme };
    173  1.30   xtraeme 
    174  1.30   xtraeme /*
    175  1.30   xtraeme  * The W83627EHF can measure voltages up to 2.048 V instead of the
    176  1.30   xtraeme  * traditional 4.096 V.  For measuring positive voltages, this can be
    177  1.30   xtraeme  * accounted for by halving the resistor factor.  Negative voltages
    178  1.30   xtraeme  * need special treatment, also because the reference voltage is 2.048 V
    179  1.30   xtraeme  * instead of the traditional 3.6 V.
    180  1.30   xtraeme  */
    181  1.30   xtraeme static struct lm_sensor w83627ehf_sensors[] = {
    182  1.30   xtraeme 	/* Voltage */
    183  1.30   xtraeme 	{ "VCore", ENVSYS_SVOLTS_DC, 0, 0x20, lm_refresh_volt, RFACT_NONE / 2},
    184  1.30   xtraeme 	{ "+12V", ENVSYS_SVOLTS_DC, 0, 0x21, lm_refresh_volt, RFACT(56, 10) / 2 },
    185  1.30   xtraeme 	{ "+3.3V", ENVSYS_SVOLTS_DC, 0, 0x22, lm_refresh_volt, RFACT(34, 34) / 2 },
    186  1.30   xtraeme 	{ "+3.3V", ENVSYS_SVOLTS_DC, 0, 0x23, lm_refresh_volt, RFACT(34, 24) / 2 },
    187  1.31   xtraeme 	{ "-12V", ENVSYS_SVOLTS_DC, 0, 0x24, wb_w83627ehf_refresh_nvolt, 0 },
    188  1.30   xtraeme 	{ "Unknown", ENVSYS_SVOLTS_DC, 0, 0x25, lm_refresh_volt, RFACT_NONE / 2 },
    189  1.30   xtraeme 	{ "Unknown", ENVSYS_SVOLTS_DC, 0, 0x26, lm_refresh_volt, RFACT_NONE / 2 },
    190  1.30   xtraeme 	{ "3.3VSB", ENVSYS_SVOLTS_DC, 5, 0x50, lm_refresh_volt, RFACT(34, 34) / 2 },
    191  1.30   xtraeme 	{ "VBAT", ENVSYS_SVOLTS_DC, 5, 0x51, lm_refresh_volt, RFACT_NONE / 2 },
    192  1.30   xtraeme 	{ "Unknown", ENVSYS_SVOLTS_DC, 5, 0x52, lm_refresh_volt, RFACT_NONE / 2 },
    193  1.30   xtraeme 
    194  1.30   xtraeme 	/* Temperature */
    195  1.31   xtraeme 	{ "Temp0", ENVSYS_STEMP, 0, 0x27, lm_refresh_temp, 0 },
    196  1.31   xtraeme 	{ "Temp1", ENVSYS_STEMP, 1, 0x50, wb_refresh_temp, 0 },
    197  1.31   xtraeme 	{ "Temp2", ENVSYS_STEMP, 2, 0x50, wb_refresh_temp, 0 },
    198  1.30   xtraeme 
    199  1.30   xtraeme 	/* Fans */
    200  1.31   xtraeme 	{ "Fan0", ENVSYS_SFANRPM, 0, 0x28, wb_refresh_fanrpm, 0 },
    201  1.31   xtraeme 	{ "Fan1", ENVSYS_SFANRPM, 0, 0x29, wb_refresh_fanrpm, 0 },
    202  1.31   xtraeme 	{ "Fan2", ENVSYS_SFANRPM, 0, 0x2a, wb_refresh_fanrpm, 0 },
    203  1.30   xtraeme 
    204  1.30   xtraeme 	{ NULL }
    205  1.30   xtraeme };
    206  1.30   xtraeme 
    207  1.30   xtraeme static struct lm_sensor w83627dhg_sensors[] = {
    208  1.30   xtraeme 	/* Voltage */
    209  1.31   xtraeme 	{ "VCore", ENVSYS_SVOLTS_DC, 0, 0x20, lm_refresh_volt, RFACT_NONE/2 },
    210  1.31   xtraeme 	{ "+12V", ENVSYS_SVOLTS_DC, 0, 0x21, lm_refresh_volt, RFACT(56,10)/2 },
    211  1.30   xtraeme 	{ "+3.3V", ENVSYS_SVOLTS_DC, 0, 0x22, lm_refresh_volt, RFACT_NONE },
    212  1.31   xtraeme 	{ "AVCC", ENVSYS_SVOLTS_DC, 0, 0x23, lm_refresh_volt, RFACT_NONE },
    213  1.31   xtraeme 	{ "+5V", ENVSYS_SVOLTS_DC, 0, 0x25, lm_refresh_volt, RFACT(32, 56) },
    214  1.30   xtraeme 	/*
    215  1.31   xtraeme 	 * I'm not sure about which one is -12V or -5V.
    216  1.30   xtraeme 	 */
    217  1.30   xtraeme #if 0
    218  1.31   xtraeme 	{ "-12V", ENVSYS_SVOLTS_DC, 0, 0x24, wb_refresh_nvolt, RFACT(232, 60) },
    219  1.31   xtraeme 	{ "-5V", ENVSYS_SVOLTS_DC, 0, 0x26, wb_w83627ehf_refresh_nvolt },
    220  1.30   xtraeme #endif
    221  1.31   xtraeme 	{ "+3.3VSB", ENVSYS_SVOLTS_DC, 5, 0x50, lm_refresh_volt, RFACT_NONE },
    222  1.30   xtraeme 	{ "VBAT", ENVSYS_SVOLTS_DC, 5, 0x51, lm_refresh_volt, RFACT_NONE },
    223  1.30   xtraeme 
    224  1.30   xtraeme 	/* Temperature */
    225  1.31   xtraeme 	{ "System Temp", ENVSYS_STEMP, 0, 0x27, lm_refresh_temp, 0 },
    226  1.31   xtraeme 	{ "CPU Temp", ENVSYS_STEMP, 1, 0x50, wb_refresh_temp, 0 },
    227  1.31   xtraeme 	{ "Aux Temp", ENVSYS_STEMP, 2, 0x50, wb_refresh_temp, 0 },
    228  1.30   xtraeme 
    229  1.30   xtraeme 	/* Fans */
    230  1.31   xtraeme 	{ "System Fan", ENVSYS_SFANRPM, 0, 0x28, wb_refresh_fanrpm, 0 },
    231  1.31   xtraeme 	{ "CPU Fan", ENVSYS_SFANRPM, 0, 0x29, wb_refresh_fanrpm, 0 },
    232  1.31   xtraeme 	{ "Aux Fan", ENVSYS_SFANRPM, 0, 0x2a, wb_refresh_fanrpm, 0 },
    233  1.30   xtraeme 
    234  1.30   xtraeme 	{ NULL }
    235  1.30   xtraeme };
    236  1.30   xtraeme 
    237  1.30   xtraeme static struct lm_sensor w83637hf_sensors[] = {
    238  1.30   xtraeme 	/* Voltage */
    239  1.31   xtraeme 	{ "VCore", ENVSYS_SVOLTS_DC, 0, 0x20, wb_w83637hf_refresh_vcore, 0 },
    240  1.30   xtraeme 	{ "+12V", ENVSYS_SVOLTS_DC, 0, 0x21, lm_refresh_volt, RFACT(28, 10) },
    241  1.30   xtraeme 	{ "+3.3V", ENVSYS_SVOLTS_DC, 0, 0x22, lm_refresh_volt, RFACT_NONE },
    242  1.30   xtraeme 	{ "+5V", ENVSYS_SVOLTS_DC, 0, 0x23, lm_refresh_volt, RFACT(34, 51) },
    243  1.30   xtraeme 	{ "-12V", ENVSYS_SVOLTS_DC, 0, 0x24, wb_refresh_nvolt, RFACT(232, 56) },
    244  1.30   xtraeme 	{ "5VSB", ENVSYS_SVOLTS_DC, 5, 0x50, lm_refresh_volt, RFACT(34, 51) },
    245  1.30   xtraeme 	{ "VBAT", ENVSYS_SVOLTS_DC, 5, 0x51, lm_refresh_volt, RFACT_NONE },
    246  1.30   xtraeme 
    247  1.30   xtraeme 	/* Temperature */
    248  1.31   xtraeme 	{ "Temp0", ENVSYS_STEMP, 0, 0x27, lm_refresh_temp, 0 },
    249  1.31   xtraeme 	{ "Temp1", ENVSYS_STEMP, 1, 0x50, wb_refresh_temp, 0 },
    250  1.31   xtraeme 	{ "Temp2", ENVSYS_STEMP, 2, 0x50, wb_refresh_temp, 0 },
    251  1.30   xtraeme 
    252  1.30   xtraeme 	/* Fans */
    253  1.31   xtraeme 	{ "Fan0", ENVSYS_SFANRPM, 0, 0x28, wb_refresh_fanrpm, 0 },
    254  1.31   xtraeme 	{ "Fan1", ENVSYS_SFANRPM, 0, 0x29, wb_refresh_fanrpm, 0 },
    255  1.31   xtraeme 	{ "Fan2", ENVSYS_SFANRPM, 0, 0x2a, wb_refresh_fanrpm, 0 },
    256  1.30   xtraeme 
    257  1.30   xtraeme 	{ NULL }
    258  1.30   xtraeme };
    259  1.30   xtraeme 
    260  1.30   xtraeme static struct lm_sensor w83697hf_sensors[] = {
    261  1.30   xtraeme 	/* Voltage */
    262  1.30   xtraeme 	{ "VCore", ENVSYS_SVOLTS_DC, 0, 0x20, lm_refresh_volt, RFACT_NONE },
    263  1.30   xtraeme 	{ "+3.3V", ENVSYS_SVOLTS_DC, 0, 0x22, lm_refresh_volt, RFACT_NONE },
    264  1.30   xtraeme 	{ "+5V", ENVSYS_SVOLTS_DC, 0, 0x23, lm_refresh_volt, RFACT(34, 50) },
    265  1.30   xtraeme 	{ "+12V", ENVSYS_SVOLTS_DC, 0, 0x24, lm_refresh_volt, RFACT(28, 10) },
    266  1.30   xtraeme 	{ "-12V", ENVSYS_SVOLTS_DC, 0, 0x25, wb_refresh_nvolt, RFACT(232, 56) },
    267  1.30   xtraeme 	{ "-5V", ENVSYS_SVOLTS_DC, 0, 0x26, wb_refresh_nvolt, RFACT(120, 56) },
    268  1.30   xtraeme 	{ "5VSB", ENVSYS_SVOLTS_DC, 5, 0x50, lm_refresh_volt, RFACT(17, 33) },
    269  1.30   xtraeme 	{ "VBAT", ENVSYS_SVOLTS_DC, 5, 0x51, lm_refresh_volt, RFACT_NONE },
    270  1.30   xtraeme 
    271  1.30   xtraeme 	/* Temperature */
    272  1.31   xtraeme 	{ "Temp0", ENVSYS_STEMP, 0, 0x27, lm_refresh_temp, 0 },
    273  1.31   xtraeme 	{ "Temp1", ENVSYS_STEMP, 1, 0x50, wb_refresh_temp, 0 },
    274  1.30   xtraeme 
    275  1.30   xtraeme 	/* Fans */
    276  1.31   xtraeme 	{ "Fan0", ENVSYS_SFANRPM, 0, 0x28, wb_refresh_fanrpm, 0 },
    277  1.31   xtraeme 	{ "Fan1", ENVSYS_SFANRPM, 0, 0x29, wb_refresh_fanrpm, 0 },
    278  1.30   xtraeme 
    279  1.30   xtraeme 	{ NULL }
    280  1.30   xtraeme };
    281  1.30   xtraeme 
    282  1.30   xtraeme /*
    283  1.30   xtraeme  * The datasheet doesn't mention the (internal) resistors used for the
    284  1.30   xtraeme  * +5V, but using the values from the W83782D datasheets seems to
    285  1.30   xtraeme  * provide sensible results.
    286  1.30   xtraeme  */
    287  1.30   xtraeme static struct lm_sensor w83781d_sensors[] = {
    288  1.30   xtraeme 	/* Voltage */
    289  1.30   xtraeme 	{ "VCore A", ENVSYS_SVOLTS_DC, 0, 0x20, lm_refresh_volt, RFACT_NONE },
    290  1.30   xtraeme 	{ "VCore B", ENVSYS_SVOLTS_DC, 0, 0x21, lm_refresh_volt, RFACT_NONE },
    291  1.30   xtraeme 	{ "+3.3V", ENVSYS_SVOLTS_DC, 0, 0x22, lm_refresh_volt, RFACT_NONE },
    292  1.30   xtraeme 	{ "+5V", ENVSYS_SVOLTS_DC, 0, 0x23, lm_refresh_volt, RFACT(34, 50) },
    293  1.30   xtraeme 	{ "+12V", ENVSYS_SVOLTS_DC, 0, 0x24, lm_refresh_volt, RFACT(28, 10) },
    294  1.30   xtraeme 	{ "-12V", ENVSYS_SVOLTS_DC, 0, 0x25, lm_refresh_volt, NRFACT(2100, 604) },
    295  1.30   xtraeme 	{ "-5V", ENVSYS_SVOLTS_DC, 0, 0x26, lm_refresh_volt, NRFACT(909, 604) },
    296  1.30   xtraeme 
    297  1.30   xtraeme 	/* Temperature */
    298  1.31   xtraeme 	{ "Temp0", ENVSYS_STEMP, 0, 0x27, lm_refresh_temp, 0 },
    299  1.31   xtraeme 	{ "Temp1", ENVSYS_STEMP, 1, 0x50, wb_refresh_temp, 0 },
    300  1.31   xtraeme 	{ "Temp2", ENVSYS_STEMP, 2, 0x50, wb_refresh_temp, 0 },
    301  1.30   xtraeme 
    302  1.30   xtraeme 	/* Fans */
    303  1.31   xtraeme 	{ "Fan0", ENVSYS_SFANRPM, 0, 0x28, lm_refresh_fanrpm, 0 },
    304  1.31   xtraeme 	{ "Fan1", ENVSYS_SFANRPM, 0, 0x29, lm_refresh_fanrpm, 0 },
    305  1.31   xtraeme 	{ "Fan2", ENVSYS_SFANRPM, 0, 0x2a, lm_refresh_fanrpm, 0 },
    306  1.30   xtraeme 
    307  1.30   xtraeme 	{ NULL }
    308  1.30   xtraeme };
    309  1.30   xtraeme 
    310  1.30   xtraeme static struct lm_sensor w83782d_sensors[] = {
    311  1.30   xtraeme 	/* Voltage */
    312  1.30   xtraeme 	{ "VCore", ENVSYS_SVOLTS_DC, 0, 0x20, lm_refresh_volt, RFACT_NONE },
    313  1.30   xtraeme 	{ "VINR0", ENVSYS_SVOLTS_DC, 0, 0x21, lm_refresh_volt, RFACT_NONE },
    314  1.30   xtraeme 	{ "+3.3V", ENVSYS_SVOLTS_DC, 0, 0x22, lm_refresh_volt, RFACT_NONE },
    315  1.30   xtraeme 	{ "+5V", ENVSYS_SVOLTS_DC, 0, 0x23, lm_refresh_volt, RFACT(34, 50) },
    316  1.30   xtraeme 	{ "+12V", ENVSYS_SVOLTS_DC, 0, 0x24, lm_refresh_volt, RFACT(28, 10) },
    317  1.30   xtraeme 	{ "-12V", ENVSYS_SVOLTS_DC, 0, 0x25, wb_refresh_nvolt, RFACT(232, 56) },
    318  1.30   xtraeme 	{ "-5V", ENVSYS_SVOLTS_DC, 0, 0x26, wb_refresh_nvolt, RFACT(120, 56) },
    319  1.30   xtraeme 	{ "5VSB", ENVSYS_SVOLTS_DC, 5, 0x50, lm_refresh_volt, RFACT(17, 33) },
    320  1.30   xtraeme 	{ "VBAT", ENVSYS_SVOLTS_DC, 5, 0x51, lm_refresh_volt, RFACT_NONE },
    321  1.30   xtraeme 
    322  1.30   xtraeme 	/* Temperature */
    323  1.31   xtraeme 	{ "Temp0", ENVSYS_STEMP, 0, 0x27, lm_refresh_temp, 0 },
    324  1.31   xtraeme 	{ "Temp1", ENVSYS_STEMP, 1, 0x50, wb_refresh_temp, 0 },
    325  1.31   xtraeme 	{ "Temp2", ENVSYS_STEMP, 2, 0x50, wb_refresh_temp, 0 },
    326  1.30   xtraeme 
    327  1.30   xtraeme 	/* Fans */
    328  1.31   xtraeme 	{ "Fan0", ENVSYS_SFANRPM, 0, 0x28, wb_refresh_fanrpm, 0 },
    329  1.31   xtraeme 	{ "Fan1", ENVSYS_SFANRPM, 0, 0x29, wb_refresh_fanrpm, 0 },
    330  1.31   xtraeme 	{ "Fan2", ENVSYS_SFANRPM, 0, 0x2a, wb_refresh_fanrpm, 0 },
    331  1.30   xtraeme 
    332  1.30   xtraeme 	{ NULL }
    333  1.30   xtraeme };
    334  1.30   xtraeme 
    335  1.30   xtraeme static struct lm_sensor w83783s_sensors[] = {
    336  1.30   xtraeme 	/* Voltage */
    337  1.30   xtraeme 	{ "VCore", ENVSYS_SVOLTS_DC, 0, 0x20, lm_refresh_volt, RFACT_NONE },
    338  1.30   xtraeme 	{ "+3.3V", ENVSYS_SVOLTS_DC, 0, 0x22, lm_refresh_volt, RFACT_NONE },
    339  1.30   xtraeme 	{ "+5V", ENVSYS_SVOLTS_DC, 0, 0x23, lm_refresh_volt, RFACT(34, 50) },
    340  1.30   xtraeme 	{ "+12V", ENVSYS_SVOLTS_DC, 0, 0x24, lm_refresh_volt, RFACT(28, 10) },
    341  1.30   xtraeme 	{ "-12V", ENVSYS_SVOLTS_DC, 0, 0x25, wb_refresh_nvolt, RFACT(232, 56) },
    342  1.30   xtraeme 	{ "-5V", ENVSYS_SVOLTS_DC, 0, 0x26, wb_refresh_nvolt, RFACT(120, 56) },
    343  1.30   xtraeme 
    344  1.30   xtraeme 	/* Temperature */
    345  1.31   xtraeme 	{ "Temp0", ENVSYS_STEMP, 0, 0x27, lm_refresh_temp, 0 },
    346  1.31   xtraeme 	{ "Temp1", ENVSYS_STEMP, 1, 0x50, wb_refresh_temp, 0 },
    347  1.30   xtraeme 
    348  1.30   xtraeme 	/* Fans */
    349  1.31   xtraeme 	{ "Fan0", ENVSYS_SFANRPM, 0, 0x28, wb_refresh_fanrpm, 0 },
    350  1.31   xtraeme 	{ "Fan1", ENVSYS_SFANRPM, 0, 0x29, wb_refresh_fanrpm, 0 },
    351  1.31   xtraeme 	{ "Fan2", ENVSYS_SFANRPM, 0, 0x2a, wb_refresh_fanrpm, 0 },
    352  1.30   xtraeme 
    353  1.30   xtraeme 	{ NULL }
    354  1.30   xtraeme };
    355  1.30   xtraeme 
    356  1.30   xtraeme static struct lm_sensor w83791d_sensors[] = {
    357  1.30   xtraeme 	/* Voltage */
    358  1.30   xtraeme 	{ "VCore", ENVSYS_SVOLTS_DC, 0, 0x20, lm_refresh_volt, 10000 },
    359  1.30   xtraeme 	{ "VINR0", ENVSYS_SVOLTS_DC, 0, 0x21, lm_refresh_volt, 10000 },
    360  1.30   xtraeme 	{ "+3.3V", ENVSYS_SVOLTS_DC, 0, 0x22, lm_refresh_volt, 10000 },
    361  1.30   xtraeme 	{ "+5V", ENVSYS_SVOLTS_DC, 0, 0x23, lm_refresh_volt, RFACT(34, 50) },
    362  1.30   xtraeme 	{ "+12V", ENVSYS_SVOLTS_DC, 0, 0x24, lm_refresh_volt, RFACT(28, 10) },
    363  1.30   xtraeme 	{ "-12V", ENVSYS_SVOLTS_DC, 0, 0x25, wb_refresh_nvolt, RFACT(232, 56) },
    364  1.30   xtraeme 	{ "-5V", ENVSYS_SVOLTS_DC, 0, 0x26, wb_refresh_nvolt, RFACT(120, 56) },
    365  1.30   xtraeme 	{ "5VSB", ENVSYS_SVOLTS_DC, 0, 0xb0, lm_refresh_volt, RFACT(17, 33) },
    366  1.30   xtraeme 	{ "VBAT", ENVSYS_SVOLTS_DC, 0, 0xb1, lm_refresh_volt, RFACT_NONE },
    367  1.30   xtraeme 	{ "VINR1", ENVSYS_SVOLTS_DC, 0, 0xb2, lm_refresh_volt, RFACT_NONE },
    368  1.30   xtraeme 
    369  1.30   xtraeme 	/* Temperature */
    370  1.31   xtraeme 	{ "Temp0", ENVSYS_STEMP, 0, 0x27, lm_refresh_temp, 0 },
    371  1.31   xtraeme 	{ "Temp1", ENVSYS_STEMP, 0, 0xc0, wb_refresh_temp, 0 },
    372  1.31   xtraeme 	{ "Temp2", ENVSYS_STEMP, 0, 0xc8, wb_refresh_temp, 0 },
    373  1.30   xtraeme 
    374  1.30   xtraeme 	/* Fans */
    375  1.31   xtraeme 	{ "Fan0", ENVSYS_SFANRPM, 0, 0x28, wb_refresh_fanrpm, 0 },
    376  1.31   xtraeme 	{ "Fan1", ENVSYS_SFANRPM, 0, 0x29, wb_refresh_fanrpm, 0 },
    377  1.31   xtraeme 	{ "Fan2", ENVSYS_SFANRPM, 0, 0x2a, wb_refresh_fanrpm, 0 },
    378  1.31   xtraeme 	{ "Fan3", ENVSYS_SFANRPM, 0, 0xba, wb_refresh_fanrpm, 0 },
    379  1.31   xtraeme 	{ "Fan4", ENVSYS_SFANRPM, 0, 0xbb, wb_refresh_fanrpm, 0 },
    380  1.30   xtraeme 
    381  1.30   xtraeme         { NULL }
    382  1.30   xtraeme };
    383  1.30   xtraeme 
    384  1.30   xtraeme static struct lm_sensor w83792d_sensors[] = {
    385  1.30   xtraeme 	/* Voltage */
    386  1.30   xtraeme 	{ "VCore A", ENVSYS_SVOLTS_DC, 0, 0x20, lm_refresh_volt, RFACT_NONE },
    387  1.30   xtraeme 	{ "VCore B", ENVSYS_SVOLTS_DC, 0, 0x21, lm_refresh_volt, RFACT_NONE },
    388  1.30   xtraeme 	{ "+3.3V", ENVSYS_SVOLTS_DC, 0, 0x22, lm_refresh_volt, RFACT_NONE },
    389  1.30   xtraeme 	{ "-5V", ENVSYS_SVOLTS_DC, 0, 0x23, wb_refresh_nvolt, RFACT(120, 56) },
    390  1.30   xtraeme 	{ "+12V", ENVSYS_SVOLTS_DC, 0, 0x24, lm_refresh_volt, RFACT(28, 10) },
    391  1.30   xtraeme 	{ "-12V", ENVSYS_SVOLTS_DC, 0, 0x25, wb_refresh_nvolt, RFACT(232, 56) },
    392  1.30   xtraeme 	{ "+5V", ENVSYS_SVOLTS_DC, 0, 0x26, lm_refresh_volt, RFACT(34, 50) },
    393  1.30   xtraeme 	{ "5VSB", ENVSYS_SVOLTS_DC, 0, 0xb0, lm_refresh_volt, RFACT(17, 33) },
    394  1.30   xtraeme 	{ "VBAT", ENVSYS_SVOLTS_DC, 0, 0xb1, lm_refresh_volt, RFACT_NONE },
    395  1.30   xtraeme 
    396  1.30   xtraeme 	/* Temperature */
    397  1.31   xtraeme 	{ "Temp0", ENVSYS_STEMP, 0, 0x27, lm_refresh_temp, 0 },
    398  1.31   xtraeme 	{ "Temp1", ENVSYS_STEMP, 0, 0xc0, wb_refresh_temp, 0 },
    399  1.31   xtraeme 	{ "Temp2", ENVSYS_STEMP, 0, 0xc8, wb_refresh_temp, 0 },
    400  1.30   xtraeme 
    401  1.30   xtraeme 	/* Fans */
    402  1.31   xtraeme 	{ "Fan0", ENVSYS_SFANRPM, 0, 0x28, wb_w83792d_refresh_fanrpm, 0 },
    403  1.31   xtraeme 	{ "Fan1", ENVSYS_SFANRPM, 0, 0x29, wb_w83792d_refresh_fanrpm, 0 },
    404  1.31   xtraeme 	{ "Fan2", ENVSYS_SFANRPM, 0, 0x2a, wb_w83792d_refresh_fanrpm, 0 },
    405  1.31   xtraeme 	{ "Fan3", ENVSYS_SFANRPM, 0, 0xb8, wb_w83792d_refresh_fanrpm, 0 },
    406  1.31   xtraeme 	{ "Fan4", ENVSYS_SFANRPM, 0, 0xb9, wb_w83792d_refresh_fanrpm, 0 },
    407  1.31   xtraeme 	{ "Fan5", ENVSYS_SFANRPM, 0, 0xba, wb_w83792d_refresh_fanrpm, 0 },
    408  1.31   xtraeme 	{ "Fan6", ENVSYS_SFANRPM, 0, 0xbe, wb_w83792d_refresh_fanrpm, 0 },
    409  1.30   xtraeme 
    410  1.30   xtraeme 	{ NULL }
    411  1.30   xtraeme };
    412   1.1      groo 
    413  1.30   xtraeme static struct lm_sensor as99127f_sensors[] = {
    414  1.30   xtraeme 	/* Voltage */
    415  1.30   xtraeme 	{ "VCore A", ENVSYS_SVOLTS_DC, 0, 0x20, lm_refresh_volt, RFACT_NONE },
    416  1.30   xtraeme 	{ "VCore B", ENVSYS_SVOLTS_DC, 0, 0x21, lm_refresh_volt, RFACT_NONE },
    417  1.30   xtraeme 	{ "+3.3V", ENVSYS_SVOLTS_DC, 0, 0x22, lm_refresh_volt, RFACT_NONE },
    418  1.30   xtraeme 	{ "+5V", ENVSYS_SVOLTS_DC, 0, 0x23, lm_refresh_volt, RFACT(34, 50) },
    419  1.30   xtraeme 	{ "+12V", ENVSYS_SVOLTS_DC, 0, 0x24, lm_refresh_volt, RFACT(28, 10) },
    420  1.30   xtraeme 	{ "-12V", ENVSYS_SVOLTS_DC, 0, 0x25, wb_refresh_nvolt, RFACT(232, 56) },
    421  1.30   xtraeme 	{ "-5V", ENVSYS_SVOLTS_DC, 0, 0x26, wb_refresh_nvolt, RFACT(120, 56) },
    422  1.30   xtraeme 
    423  1.30   xtraeme 	/* Temperature */
    424  1.31   xtraeme 	{ "Temp0", ENVSYS_STEMP, 0, 0x27, lm_refresh_temp, 0 },
    425  1.31   xtraeme 	{ "Temp1", ENVSYS_STEMP, 1, 0x50, as_refresh_temp, 0 },
    426  1.31   xtraeme 	{ "Temp2", ENVSYS_STEMP, 2, 0x50, as_refresh_temp, 0 },
    427  1.30   xtraeme 
    428  1.30   xtraeme 	/* Fans */
    429  1.31   xtraeme 	{ "Fan0", ENVSYS_SFANRPM, 0, 0x28, lm_refresh_fanrpm, 0 },
    430  1.31   xtraeme 	{ "Fan1", ENVSYS_SFANRPM, 0, 0x29, lm_refresh_fanrpm, 0 },
    431  1.31   xtraeme 	{ "Fan2", ENVSYS_SFANRPM, 0, 0x2a, lm_refresh_fanrpm, 0 },
    432  1.30   xtraeme 
    433  1.30   xtraeme 	{ NULL }
    434  1.30   xtraeme };
    435  1.30   xtraeme 
    436  1.30   xtraeme static void
    437  1.30   xtraeme lm_generic_banksel(struct lm_softc *lmsc, int bank)
    438   1.1      groo {
    439  1.17        ad 	(*lmsc->lm_writereg)(lmsc, WB_BANKSEL, bank);
    440   1.1      groo }
    441   1.1      groo 
    442   1.1      groo /*
    443   1.2      groo  * bus independent probe
    444   1.2      groo  */
    445   1.2      groo int
    446  1.30   xtraeme lm_probe(bus_space_tag_t iot, bus_space_handle_t ioh)
    447   1.2      groo {
    448  1.30   xtraeme 	uint8_t cr;
    449   1.2      groo 	int rv;
    450   1.2      groo 
    451   1.2      groo 	/* Check for some power-on defaults */
    452   1.2      groo 	bus_space_write_1(iot, ioh, LMC_ADDR, LMD_CONFIG);
    453   1.2      groo 
    454   1.2      groo 	/* Perform LM78 reset */
    455   1.2      groo 	bus_space_write_1(iot, ioh, LMC_DATA, 0x80);
    456   1.2      groo 
    457   1.2      groo 	/* XXX - Why do I have to reselect the register? */
    458   1.2      groo 	bus_space_write_1(iot, ioh, LMC_ADDR, LMD_CONFIG);
    459   1.2      groo 	cr = bus_space_read_1(iot, ioh, LMC_DATA);
    460   1.2      groo 
    461   1.2      groo 	/* XXX - spec says *only* 0x08! */
    462  1.23   xtraeme 	if ((cr == 0x08) || (cr == 0x01) || (cr == 0x03))
    463   1.2      groo 		rv = 1;
    464   1.2      groo 	else
    465   1.2      groo 		rv = 0;
    466   1.2      groo 
    467   1.2      groo 	DPRINTF(("lm: rv = %d, cr = %x\n", rv, cr));
    468   1.2      groo 
    469  1.30   xtraeme 	return rv;
    470   1.2      groo }
    471   1.2      groo 
    472   1.2      groo 
    473   1.2      groo /*
    474   1.1      groo  * pre:  lmsc contains valid busspace tag and handle
    475   1.1      groo  */
    476   1.1      groo void
    477  1.30   xtraeme lm_attach(struct lm_softc *lmsc)
    478   1.1      groo {
    479  1.30   xtraeme 	uint32_t i;
    480   1.1      groo 
    481  1.30   xtraeme 	for (i = 0; i < __arraycount(lm_chips); i++)
    482   1.5    bouyer 		if (lm_chips[i].chip_match(lmsc))
    483   1.5    bouyer 			break;
    484   1.1      groo 
    485   1.1      groo 	/* Start the monitoring loop */
    486  1.17        ad 	(*lmsc->lm_writereg)(lmsc, LMD_CONFIG, 0x01);
    487   1.1      groo 
    488   1.1      groo 	/* Indicate we have never read the registers */
    489   1.1      groo 	timerclear(&lmsc->lastread);
    490   1.1      groo 
    491   1.1      groo 	/* Initialize sensors */
    492   1.5    bouyer 	for (i = 0; i < lmsc->numsensors; ++i) {
    493   1.1      groo 		lmsc->sensors[i].sensor = lmsc->info[i].sensor = i;
    494   1.1      groo 		lmsc->sensors[i].validflags = (ENVSYS_FVALID|ENVSYS_FCURVALID);
    495   1.1      groo 		lmsc->info[i].validflags = ENVSYS_FVALID;
    496   1.1      groo 		lmsc->sensors[i].warnflags = ENVSYS_WARN_OK;
    497   1.1      groo 	}
    498   1.4   thorpej 	/*
    499   1.4   thorpej 	 * Hook into the System Monitor.
    500   1.4   thorpej 	 */
    501   1.4   thorpej 	lmsc->sc_sysmon.sme_ranges = lm_ranges;
    502   1.4   thorpej 	lmsc->sc_sysmon.sme_sensor_info = lmsc->info;
    503   1.4   thorpej 	lmsc->sc_sysmon.sme_sensor_data = lmsc->sensors;
    504   1.4   thorpej 	lmsc->sc_sysmon.sme_cookie = lmsc;
    505   1.4   thorpej 
    506   1.4   thorpej 	lmsc->sc_sysmon.sme_gtredata = lm_gtredata;
    507   1.5    bouyer 	/* sme_streinfo set in chip-specific attach */
    508   1.4   thorpej 
    509   1.5    bouyer 	lmsc->sc_sysmon.sme_nsensors = lmsc->numsensors;
    510   1.4   thorpej 	lmsc->sc_sysmon.sme_envsys_version = 1000;
    511   1.4   thorpej 
    512   1.4   thorpej 	if (sysmon_envsys_register(&lmsc->sc_sysmon))
    513  1.30   xtraeme 		aprint_error("%s: unable to register with sysmon\n",
    514   1.4   thorpej 		    lmsc->sc_dev.dv_xname);
    515   1.1      groo }
    516   1.1      groo 
    517  1.30   xtraeme static int
    518  1.30   xtraeme lm_match(struct lm_softc *sc)
    519   1.5    bouyer {
    520  1.30   xtraeme 	const char *model = NULL;
    521  1.30   xtraeme 	int chipid;
    522   1.5    bouyer 
    523  1.30   xtraeme 	/* See if we have an LM78/LM78J/LM79 or LM81 */
    524  1.30   xtraeme 	chipid = (*sc->lm_readreg)(sc, LMD_CHIPID) & LM_ID_MASK;
    525  1.30   xtraeme 	switch(chipid) {
    526   1.5    bouyer 	case LM_ID_LM78:
    527  1.30   xtraeme 		model = "LM78";
    528   1.5    bouyer 		break;
    529   1.5    bouyer 	case LM_ID_LM78J:
    530  1.30   xtraeme 		model = "LM78J";
    531   1.5    bouyer 		break;
    532   1.5    bouyer 	case LM_ID_LM79:
    533  1.30   xtraeme 		model = "LM79";
    534  1.15    bouyer 		break;
    535  1.15    bouyer 	case LM_ID_LM81:
    536  1.30   xtraeme 		model = "LM81";
    537   1.5    bouyer 		break;
    538   1.5    bouyer 	default:
    539   1.5    bouyer 		return 0;
    540   1.5    bouyer 	}
    541   1.1      groo 
    542  1.30   xtraeme 	aprint_normal(": National Semiconductor %s Hardware monitor\n", model);
    543   1.5    bouyer 
    544  1.30   xtraeme 	lm_setup_sensors(sc, lm78_sensors);
    545  1.30   xtraeme 	sc->sc_sysmon.sme_streinfo = lm_streinfo;
    546  1.30   xtraeme 	sc->refresh_sensor_data = lm_refresh_sensor_data;
    547   1.5    bouyer 	return 1;
    548   1.5    bouyer }
    549   1.5    bouyer 
    550  1.30   xtraeme static int
    551  1.30   xtraeme def_match(struct lm_softc *sc)
    552   1.1      groo {
    553  1.30   xtraeme 	int chipid;
    554   1.5    bouyer 
    555  1.30   xtraeme 	chipid = (*sc->lm_readreg)(sc, LMD_CHIPID) & LM_ID_MASK;
    556  1.30   xtraeme 	aprint_error(": Unknown chip (ID %d)\n", chipid);
    557   1.5    bouyer 
    558  1.30   xtraeme 	lm_setup_sensors(sc, lm78_sensors);
    559   1.5    bouyer 	sc->sc_sysmon.sme_streinfo = lm_streinfo;
    560  1.30   xtraeme 	sc->refresh_sensor_data = lm_refresh_sensor_data;
    561  1.30   xtraeme 	return 1;
    562   1.5    bouyer }
    563   1.1      groo 
    564  1.30   xtraeme static int
    565  1.30   xtraeme wb_match(struct lm_softc *sc)
    566   1.5    bouyer {
    567  1.30   xtraeme 	const char *model;
    568  1.30   xtraeme 	int banksel, vendid, devid;
    569   1.1      groo 
    570  1.30   xtraeme 	model = NULL;
    571  1.30   xtraeme 
    572  1.30   xtraeme 	/* Read vendor ID */
    573  1.30   xtraeme 	banksel = (*sc->lm_readreg)(sc, WB_BANKSEL);
    574  1.30   xtraeme 	lm_generic_banksel(sc, WB_BANKSEL_HBAC);
    575  1.30   xtraeme 
    576  1.30   xtraeme 	vendid = (*sc->lm_readreg)(sc, WB_VENDID) << 8;
    577  1.30   xtraeme 	lm_generic_banksel(sc, 0);
    578  1.30   xtraeme 	vendid |= (*sc->lm_readreg)(sc, WB_VENDID);
    579  1.30   xtraeme 	DPRINTF(("winbond vend id 0x%x\n", vendid));
    580  1.30   xtraeme 	if (vendid != WB_VENDID_WINBOND && vendid != WB_VENDID_ASUS)
    581   1.5    bouyer 		return 0;
    582   1.7    bouyer 
    583  1.30   xtraeme 	/* Read device/chip ID */
    584  1.30   xtraeme 	lm_generic_banksel(sc, WB_BANKSEL_B0);
    585  1.30   xtraeme 	devid = (*sc->lm_readreg)(sc, LMD_CHIPID);
    586  1.30   xtraeme 	sc->chipid = (*sc->lm_readreg)(sc, WB_BANK0_CHIPID);
    587  1.30   xtraeme 	lm_generic_banksel(sc, banksel);
    588  1.30   xtraeme 	DPRINTF(("winbond chip id 0x%x\n", sc->chipid));
    589  1.30   xtraeme 
    590  1.30   xtraeme 	switch(sc->chipid) {
    591  1.30   xtraeme 	case WB_CHIPID_W83627HF:
    592  1.30   xtraeme 		model = "W83627HF";
    593  1.30   xtraeme 		lm_setup_sensors(sc, w83627hf_sensors);
    594  1.30   xtraeme 		break;
    595  1.30   xtraeme 	case WB_CHIPID_W83627THF:
    596  1.30   xtraeme 		model = "W83627THF";
    597  1.30   xtraeme 		lm_setup_sensors(sc, w83637hf_sensors);
    598  1.30   xtraeme 		break;
    599  1.30   xtraeme 	case WB_CHIPID_W83627EHF:
    600  1.30   xtraeme 		model = "W83627EHF";
    601  1.30   xtraeme 		lm_setup_sensors(sc, w83627ehf_sensors);
    602  1.30   xtraeme 		break;
    603  1.30   xtraeme 	case WB_CHIPID_W83627DHG:
    604  1.30   xtraeme 		model = "W83627DHG";
    605  1.30   xtraeme 		lm_setup_sensors(sc, w83627dhg_sensors);
    606  1.30   xtraeme 		break;
    607  1.30   xtraeme 	case WB_CHIPID_W83637HF:
    608  1.30   xtraeme 		model = "W83637HF";
    609  1.30   xtraeme 		lm_generic_banksel(sc, WB_BANKSEL_B0);
    610  1.30   xtraeme 		if ((*sc->lm_readreg)(sc, WB_BANK0_CONFIG) & WB_CONFIG_VMR9)
    611  1.30   xtraeme 			sc->vrm9 = 1;
    612  1.30   xtraeme 		lm_generic_banksel(sc, banksel);
    613  1.30   xtraeme 		lm_setup_sensors(sc, w83637hf_sensors);
    614  1.30   xtraeme 		break;
    615  1.30   xtraeme 	case WB_CHIPID_W83697HF:
    616  1.30   xtraeme 		model = "W83697HF";
    617  1.30   xtraeme 		lm_setup_sensors(sc, w83697hf_sensors);
    618  1.30   xtraeme 		break;
    619  1.30   xtraeme 	case WB_CHIPID_W83781D:
    620  1.30   xtraeme 	case WB_CHIPID_W83781D_2:
    621  1.30   xtraeme 		model = "W83781D";
    622  1.30   xtraeme 		lm_setup_sensors(sc, w83781d_sensors);
    623   1.7    bouyer 		sc->sc_sysmon.sme_streinfo = wb781_streinfo;
    624  1.30   xtraeme 		break;
    625  1.30   xtraeme 	case WB_CHIPID_W83782D:
    626  1.30   xtraeme 		model = "W83782D";
    627  1.30   xtraeme 		lm_setup_sensors(sc, w83782d_sensors);
    628   1.8    bouyer 		sc->sc_sysmon.sme_streinfo = wb782_streinfo;
    629   1.7    bouyer 		break;
    630  1.30   xtraeme 	case WB_CHIPID_W83783S:
    631  1.30   xtraeme 		model = "W83783S";
    632  1.30   xtraeme 		lm_setup_sensors(sc, w83783s_sensors);
    633  1.30   xtraeme 		break;
    634  1.30   xtraeme 	case WB_CHIPID_W83791D:
    635  1.30   xtraeme 		model = "W83791D";
    636  1.30   xtraeme 		lm_setup_sensors(sc, w83791d_sensors);
    637  1.30   xtraeme 		break;
    638  1.30   xtraeme 	case WB_CHIPID_W83791SD:
    639  1.30   xtraeme 		model = "W83791SD";
    640  1.30   xtraeme 		break;
    641  1.30   xtraeme 	case WB_CHIPID_W83792D:
    642  1.30   xtraeme 		model = "W83792D";
    643  1.30   xtraeme 		lm_setup_sensors(sc, w83792d_sensors);
    644   1.7    bouyer 		break;
    645  1.30   xtraeme 	case WB_CHIPID_AS99127F:
    646  1.30   xtraeme 		if (vendid == WB_VENDID_ASUS) {
    647  1.30   xtraeme 			model = "AS99127F";
    648  1.30   xtraeme 			lm_setup_sensors(sc, w83781d_sensors);
    649  1.30   xtraeme 		} else {
    650  1.30   xtraeme 			model = "AS99127F rev 2";
    651  1.30   xtraeme 			lm_setup_sensors(sc, as99127f_sensors);
    652  1.30   xtraeme 		}
    653  1.23   xtraeme 		break;
    654   1.7    bouyer 	default:
    655  1.30   xtraeme 		aprint_normal(": unknown Winbond chip (ID 0x%x)\n",
    656  1.30   xtraeme 		    sc->chipid);
    657  1.30   xtraeme 		/* Handle as a standard LM78. */
    658  1.30   xtraeme 		lm_setup_sensors(sc, lm78_sensors);
    659  1.30   xtraeme 		sc->refresh_sensor_data = lm_refresh_sensor_data;
    660   1.7    bouyer 		return 1;
    661   1.7    bouyer 	}
    662  1.30   xtraeme 
    663  1.30   xtraeme 	aprint_normal(": Winbond %s Hardware monitor\n", model);
    664  1.30   xtraeme 
    665  1.30   xtraeme 	sc->sc_sysmon.sme_streinfo = lm_streinfo;
    666  1.30   xtraeme 	sc->refresh_sensor_data = wb_refresh_sensor_data;
    667   1.8    bouyer 	return 1;
    668   1.8    bouyer }
    669   1.5    bouyer 
    670   1.8    bouyer static void
    671  1.30   xtraeme lm_setup_sensors(struct lm_softc *sc, struct lm_sensor *sensors)
    672   1.8    bouyer {
    673  1.30   xtraeme 	int i;
    674  1.30   xtraeme 
    675  1.30   xtraeme 	for (i = 0; sensors[i].desc; i++) {
    676  1.30   xtraeme 		sc->sensors[i].units = sc->info[i].units = sensors[i].type;
    677  1.30   xtraeme 		strlcpy(sc->info[i].desc, sensors[i].desc,
    678  1.30   xtraeme 		    sizeof(sc->info[i].desc));
    679  1.30   xtraeme 		sc->numsensors++;
    680  1.30   xtraeme 	}
    681  1.30   xtraeme 	sc->lm_sensors = sensors;
    682   1.8    bouyer }
    683   1.8    bouyer 
    684   1.8    bouyer static void
    685  1.30   xtraeme lm_refresh_sensor_data(struct lm_softc *sc)
    686   1.8    bouyer {
    687   1.8    bouyer 	int i;
    688   1.5    bouyer 
    689  1.30   xtraeme 	/* Refresh our stored data for every sensor */
    690  1.30   xtraeme 	for (i = 0; i < sc->numsensors; i++)
    691  1.30   xtraeme 		sc->lm_sensors[i].refresh(sc, i);
    692  1.30   xtraeme }
    693  1.30   xtraeme 
    694  1.30   xtraeme static void
    695  1.30   xtraeme lm_refresh_volt(struct lm_softc *sc, int n)
    696  1.30   xtraeme {
    697  1.30   xtraeme 	int data;
    698  1.30   xtraeme 
    699  1.30   xtraeme 	data = (*sc->lm_readreg)(sc, sc->lm_sensors[n].reg);
    700  1.30   xtraeme 	DPRINTF(("%s: volt[%d] 0x%x\n", __func__, n, data));
    701  1.30   xtraeme 	sc->sensors[n].cur.data_s = (data << 4);
    702  1.30   xtraeme 	sc->sensors[n].cur.data_s *= sc->lm_sensors[n].rfact;
    703  1.30   xtraeme 	sc->sensors[n].cur.data_s /= 10;
    704  1.30   xtraeme 	sc->info[n].rfact = sc->lm_sensors[n].rfact;
    705  1.30   xtraeme }
    706  1.30   xtraeme 
    707  1.30   xtraeme #define INVALIDATE_SENSOR(x)						\
    708  1.30   xtraeme 	do {								\
    709  1.30   xtraeme 		sc->sensors[(x)].validflags &= ~ENVSYS_FCURVALID;	\
    710  1.30   xtraeme 		sc->sensors[(x)].cur.data_us = 0;			\
    711  1.30   xtraeme 	} while (/* CONSTCOND */ 0)
    712  1.30   xtraeme 
    713  1.30   xtraeme static void
    714  1.30   xtraeme lm_refresh_temp(struct lm_softc *sc, int n)
    715  1.30   xtraeme {
    716  1.30   xtraeme 	int sdata;
    717  1.30   xtraeme 
    718  1.30   xtraeme 	/*
    719  1.30   xtraeme 	 * The data sheet suggests that the range of the temperature
    720  1.30   xtraeme 	 * sensor is between -55 degC and +125 degC.
    721  1.30   xtraeme 	 */
    722  1.30   xtraeme 	sdata = (*sc->lm_readreg)(sc, sc->lm_sensors[n].reg);
    723  1.30   xtraeme 	if (sdata > 0x7d && sdata < 0xc9) {
    724  1.30   xtraeme 		INVALIDATE_SENSOR(n);
    725  1.30   xtraeme 	} else {
    726  1.30   xtraeme 		if (sdata & 0x80)
    727  1.30   xtraeme 			sdata -= 0x100;
    728  1.30   xtraeme 		sc->sensors[n].validflags |= (ENVSYS_FVALID|ENVSYS_FCURVALID);
    729  1.30   xtraeme 		sc->sensors[n].cur.data_us = sdata * 1000000 + 273150000;
    730   1.8    bouyer 	}
    731   1.8    bouyer }
    732   1.8    bouyer 
    733  1.30   xtraeme static void
    734  1.30   xtraeme lm_refresh_fanrpm(struct lm_softc *sc, int n)
    735  1.30   xtraeme {
    736  1.30   xtraeme 	int data, divisor = 1;
    737  1.30   xtraeme 
    738  1.30   xtraeme 	/*
    739  1.30   xtraeme 	 * We might get more accurate fan readings by adjusting the
    740  1.30   xtraeme 	 * divisor, but that might interfere with APM or other SMM
    741  1.30   xtraeme 	 * BIOS code reading the fan speeds.
    742  1.30   xtraeme 	 */
    743  1.30   xtraeme 
    744  1.30   xtraeme 	/* FAN3 has a fixed fan divisor. */
    745  1.30   xtraeme 	if (sc->lm_sensors[n].reg == LMD_FAN1 ||
    746  1.30   xtraeme 	    sc->lm_sensors[n].reg == LMD_FAN2) {
    747  1.30   xtraeme 		data = (*sc->lm_readreg)(sc, LMD_VIDFAN);
    748  1.30   xtraeme 		if (sc->lm_sensors[n].reg == LMD_FAN1)
    749  1.30   xtraeme 			divisor = (data >> 4) & 0x03;
    750  1.30   xtraeme 		else
    751  1.30   xtraeme 			divisor = (data >> 6) & 0x03;
    752  1.30   xtraeme 	}
    753  1.30   xtraeme 
    754  1.30   xtraeme 	data = (*sc->lm_readreg)(sc, sc->lm_sensors[n].reg);
    755  1.30   xtraeme 	if (data == 0xff || data == 0x00) {
    756  1.30   xtraeme 		INVALIDATE_SENSOR(n);
    757  1.30   xtraeme 	} else {
    758  1.30   xtraeme 		sc->sensors[n].validflags |= (ENVSYS_FVALID|ENVSYS_FCURVALID);
    759  1.30   xtraeme 		sc->sensors[n].cur.data_us = 1350000 / (data << divisor);
    760  1.30   xtraeme 	}
    761  1.30   xtraeme }
    762  1.30   xtraeme 
    763  1.30   xtraeme static void
    764  1.30   xtraeme wb_refresh_sensor_data(struct lm_softc *sc)
    765  1.30   xtraeme {
    766  1.30   xtraeme 	int banksel, bank, i;
    767  1.30   xtraeme 
    768  1.30   xtraeme 	/*
    769  1.30   xtraeme 	 * Properly save and restore bank selection register.
    770  1.30   xtraeme 	 */
    771  1.30   xtraeme 
    772  1.30   xtraeme 	banksel = bank = sc->lm_readreg(sc, WB_BANKSEL);
    773  1.30   xtraeme 	for (i = 0; i < sc->numsensors; i++) {
    774  1.30   xtraeme 		if (bank != sc->lm_sensors[i].bank) {
    775  1.30   xtraeme 			bank = sc->lm_sensors[i].bank;
    776  1.30   xtraeme 			lm_generic_banksel(sc, bank);
    777  1.30   xtraeme 		}
    778  1.30   xtraeme 		sc->lm_sensors[i].refresh(sc, i);
    779  1.30   xtraeme 	}
    780  1.30   xtraeme 	lm_generic_banksel(sc, banksel);
    781  1.30   xtraeme }
    782  1.30   xtraeme 
    783  1.30   xtraeme static void
    784  1.30   xtraeme wb_w83637hf_refresh_vcore(struct lm_softc *sc, int n)
    785  1.30   xtraeme {
    786  1.30   xtraeme 	int data;
    787  1.30   xtraeme 
    788  1.30   xtraeme 	data = (*sc->lm_readreg)(sc, sc->lm_sensors[n].reg);
    789  1.30   xtraeme 
    790  1.30   xtraeme 	/*
    791  1.30   xtraeme 	 * Depending on the voltage detection method,
    792  1.30   xtraeme 	 * one of the following formulas is used:
    793  1.30   xtraeme 	 *	VRM8 method: value = raw * 0.016V
    794  1.30   xtraeme 	 *	VRM9 method: value = raw * 0.00488V + 0.70V
    795  1.30   xtraeme 	 */
    796  1.30   xtraeme 	if (sc->vrm9)
    797  1.30   xtraeme 		sc->sensors[n].cur.data_s = (data * 4880) + 700000;
    798  1.30   xtraeme 	else
    799  1.30   xtraeme 		sc->sensors[n].cur.data_s = (data * 16000);
    800  1.30   xtraeme }
    801   1.8    bouyer 
    802   1.8    bouyer static void
    803  1.30   xtraeme wb_refresh_nvolt(struct lm_softc *sc, int n)
    804   1.8    bouyer {
    805  1.30   xtraeme 	int data;
    806  1.30   xtraeme 
    807  1.30   xtraeme 	data = (*sc->lm_readreg)(sc, sc->lm_sensors[n].reg);
    808  1.30   xtraeme 	sc->sensors[n].cur.data_s = ((data << 4) - WB_VREF);
    809  1.30   xtraeme 	sc->sensors[n].cur.data_s *= sc->lm_sensors[n].rfact;
    810  1.30   xtraeme 	sc->sensors[n].cur.data_s /= 10;
    811  1.30   xtraeme 	sc->sensors[n].cur.data_s += WB_VREF * 1000;
    812  1.30   xtraeme }
    813  1.30   xtraeme 
    814  1.30   xtraeme static void
    815  1.30   xtraeme wb_w83627ehf_refresh_nvolt(struct lm_softc *sc, int n)
    816  1.30   xtraeme {
    817  1.30   xtraeme 	int data;
    818  1.30   xtraeme 
    819  1.30   xtraeme 	data = (*sc->lm_readreg)(sc, sc->lm_sensors[n].reg);
    820  1.30   xtraeme 	sc->sensors[n].cur.data_s = ((data << 3) - WB_W83627EHF_VREF);
    821  1.30   xtraeme 	sc->sensors[n].cur.data_s *= RFACT(232, 10);
    822  1.30   xtraeme 	sc->sensors[n].cur.data_s /= 10;
    823  1.30   xtraeme 	sc->sensors[n].cur.data_s += WB_W83627EHF_VREF * 1000;
    824  1.30   xtraeme }
    825  1.30   xtraeme 
    826  1.30   xtraeme static void
    827  1.30   xtraeme wb_refresh_temp(struct lm_softc *sc, int n)
    828  1.30   xtraeme {
    829  1.30   xtraeme 	int sdata;
    830  1.30   xtraeme 
    831  1.30   xtraeme 	/*
    832  1.30   xtraeme 	 * The data sheet suggests that the range of the temperature
    833  1.30   xtraeme 	 * sensor is between -55 degC and +125 degC.  However, values
    834  1.30   xtraeme 	 * around -48 degC seem to be a very common bogus values.
    835  1.30   xtraeme 	 * Since such values are unreasonably low, we use -45 degC for
    836  1.30   xtraeme 	 * the lower limit instead.
    837  1.30   xtraeme 	 */
    838  1.30   xtraeme 	sdata = (*sc->lm_readreg)(sc, sc->lm_sensors[n].reg) << 1;
    839  1.30   xtraeme 	sdata += (*sc->lm_readreg)(sc, sc->lm_sensors[n].reg + 1) >> 7;
    840  1.30   xtraeme 	if (sdata > 0x0fa && sdata < 0x1a6) {
    841  1.30   xtraeme 		INVALIDATE_SENSOR(n);
    842  1.30   xtraeme 	} else {
    843  1.30   xtraeme 		if (sdata & 0x100)
    844  1.30   xtraeme 			sdata -= 0x200;
    845  1.30   xtraeme 		sc->sensors[n].validflags |= (ENVSYS_FVALID|ENVSYS_FCURVALID);
    846  1.30   xtraeme 		sc->sensors[n].cur.data_us = sdata * 500000 + 273150000;
    847  1.30   xtraeme 	}
    848  1.30   xtraeme }
    849  1.30   xtraeme 
    850  1.30   xtraeme static void
    851  1.30   xtraeme wb_refresh_fanrpm(struct lm_softc *sc, int n)
    852  1.30   xtraeme {
    853  1.30   xtraeme 	int fan, data, divisor = 0;
    854  1.30   xtraeme 
    855  1.30   xtraeme 	/*
    856  1.30   xtraeme 	 * This is madness; the fan divisor bits are scattered all
    857  1.30   xtraeme 	 * over the place.
    858  1.30   xtraeme 	 */
    859  1.30   xtraeme 
    860  1.30   xtraeme 	if (sc->lm_sensors[n].reg == LMD_FAN1 ||
    861  1.30   xtraeme 	    sc->lm_sensors[n].reg == LMD_FAN2 ||
    862  1.30   xtraeme 	    sc->lm_sensors[n].reg == LMD_FAN3) {
    863  1.30   xtraeme 		data = (*sc->lm_readreg)(sc, WB_BANK0_VBAT);
    864  1.30   xtraeme 		fan = (sc->lm_sensors[n].reg - LMD_FAN1);
    865  1.30   xtraeme 		if ((data >> 5) & (1 << fan))
    866  1.30   xtraeme 			divisor |= 0x04;
    867  1.30   xtraeme 	}
    868  1.30   xtraeme 
    869  1.30   xtraeme 	if (sc->lm_sensors[n].reg == LMD_FAN1 ||
    870  1.30   xtraeme 	    sc->lm_sensors[n].reg == LMD_FAN2) {
    871  1.30   xtraeme 		data = (*sc->lm_readreg)(sc, LMD_VIDFAN);
    872  1.30   xtraeme 		if (sc->lm_sensors[n].reg == LMD_FAN1)
    873  1.30   xtraeme 			divisor |= (data >> 4) & 0x03;
    874  1.30   xtraeme 		else
    875  1.30   xtraeme 			divisor |= (data >> 6) & 0x03;
    876  1.30   xtraeme 	} else if (sc->lm_sensors[n].reg == LMD_FAN3) {
    877  1.30   xtraeme 		data = (*sc->lm_readreg)(sc, WB_PIN);
    878  1.30   xtraeme 		divisor |= (data >> 6) & 0x03;
    879  1.30   xtraeme 	} else if (sc->lm_sensors[n].reg == WB_BANK0_FAN4 ||
    880  1.30   xtraeme 		   sc->lm_sensors[n].reg == WB_BANK0_FAN5) {
    881  1.30   xtraeme 		data = (*sc->lm_readreg)(sc, WB_BANK0_FAN45);
    882  1.30   xtraeme 		if (sc->lm_sensors[n].reg == WB_BANK0_FAN4)
    883  1.30   xtraeme 			divisor |= (data >> 0) & 0x07;
    884  1.30   xtraeme 		else
    885  1.30   xtraeme 			divisor |= (data >> 4) & 0x07;
    886  1.30   xtraeme 	}
    887  1.30   xtraeme 
    888  1.30   xtraeme 	data = (*sc->lm_readreg)(sc, sc->lm_sensors[n].reg);
    889  1.30   xtraeme 	if (data == 0xff || data == 0x00) {
    890  1.30   xtraeme 		INVALIDATE_SENSOR(n);
    891  1.30   xtraeme 	} else {
    892  1.30   xtraeme 		sc->sensors[n].validflags |= (ENVSYS_FVALID|ENVSYS_FCURVALID);
    893  1.30   xtraeme 		sc->sensors[n].cur.data_us = 1350000 / (data << divisor);
    894  1.30   xtraeme 	}
    895  1.30   xtraeme }
    896  1.30   xtraeme 
    897  1.30   xtraeme static void
    898  1.30   xtraeme wb_w83792d_refresh_fanrpm(struct lm_softc *sc, int n)
    899  1.30   xtraeme {
    900  1.30   xtraeme 	int reg, shift, data, divisor = 1;
    901  1.30   xtraeme 
    902  1.30   xtraeme 	shift = 0;
    903  1.30   xtraeme 
    904  1.30   xtraeme 	switch (sc->lm_sensors[n].reg) {
    905  1.30   xtraeme 	case 0x28:
    906  1.30   xtraeme 		reg = 0x47; shift = 0;
    907  1.30   xtraeme 		break;
    908  1.30   xtraeme 	case 0x29:
    909  1.30   xtraeme 		reg = 0x47; shift = 4;
    910  1.30   xtraeme 		break;
    911  1.30   xtraeme 	case 0x2a:
    912  1.30   xtraeme 		reg = 0x5b; shift = 0;
    913  1.30   xtraeme 		break;
    914  1.30   xtraeme 	case 0xb8:
    915  1.30   xtraeme 		reg = 0x5b; shift = 4;
    916  1.30   xtraeme 		break;
    917  1.30   xtraeme 	case 0xb9:
    918  1.30   xtraeme 		reg = 0x5c; shift = 0;
    919  1.30   xtraeme 		break;
    920  1.30   xtraeme 	case 0xba:
    921  1.30   xtraeme 		reg = 0x5c; shift = 4;
    922  1.30   xtraeme 		break;
    923  1.30   xtraeme 	case 0xbe:
    924  1.30   xtraeme 		reg = 0x9e; shift = 0;
    925  1.30   xtraeme 		break;
    926  1.30   xtraeme 	default:
    927  1.30   xtraeme 		reg = 0;
    928  1.30   xtraeme 		break;
    929  1.30   xtraeme 	}
    930  1.30   xtraeme 
    931  1.30   xtraeme 	data = (*sc->lm_readreg)(sc, sc->lm_sensors[n].reg);
    932  1.30   xtraeme 	if (data == 0xff || data == 0x00) {
    933  1.30   xtraeme 		INVALIDATE_SENSOR(n);
    934  1.30   xtraeme 	} else {
    935  1.30   xtraeme 		if (reg != 0)
    936  1.30   xtraeme 			divisor = ((*sc->lm_readreg)(sc, reg) >> shift) & 0x7;
    937  1.30   xtraeme 		sc->sensors[n].validflags |= (ENVSYS_FVALID|ENVSYS_FCURVALID);
    938  1.30   xtraeme 		sc->sensors[n].cur.data_us = 1350000 / (data << divisor);
    939  1.30   xtraeme 	}
    940  1.30   xtraeme }
    941  1.30   xtraeme 
    942  1.30   xtraeme static void
    943  1.30   xtraeme as_refresh_temp(struct lm_softc *sc, int n)
    944  1.30   xtraeme {
    945  1.30   xtraeme 	int sdata;
    946  1.30   xtraeme 
    947  1.30   xtraeme 	/*
    948  1.30   xtraeme 	 * It seems a shorted temperature diode produces an all-ones
    949  1.30   xtraeme 	 * bit pattern.
    950  1.30   xtraeme 	 */
    951  1.30   xtraeme 	sdata = (*sc->lm_readreg)(sc, sc->lm_sensors[n].reg) << 1;
    952  1.30   xtraeme 	sdata += (*sc->lm_readreg)(sc, sc->lm_sensors[n].reg + 1) >> 7;
    953  1.30   xtraeme 	if (sdata == 0x1ff) {
    954  1.30   xtraeme 		INVALIDATE_SENSOR(n);
    955  1.30   xtraeme 	} else {
    956  1.30   xtraeme 		if (sdata & 0x100)
    957  1.30   xtraeme 			sdata -= 0x200;
    958  1.30   xtraeme 		sc->sensors[n].validflags |= (ENVSYS_FVALID|ENVSYS_FCURVALID);
    959  1.30   xtraeme 		sc->sensors[n].cur.data_us = sdata * 500000 + 273150000;
    960   1.5    bouyer 	}
    961   1.1      groo }
    962   1.1      groo 
    963  1.30   xtraeme #undef INVALIDATE_SENSOR
    964  1.30   xtraeme 
    965  1.30   xtraeme static int
    966  1.30   xtraeme lm_gtredata(struct sysmon_envsys *sme, envsys_tre_data_t *tred)
    967   1.5    bouyer {
    968  1.26    kardel 	static const struct timeval onepointfive = { 1, 500000 };
    969  1.26    kardel 	struct timeval t, utv;
    970  1.26    kardel 	struct lm_softc *sc = sme->sme_cookie;
    971  1.26    kardel 
    972  1.26    kardel 	/* read new values at most once every 1.5 seconds */
    973  1.26    kardel 	getmicrouptime(&utv);
    974  1.26    kardel 	timeradd(&sc->lastread, &onepointfive, &t);
    975  1.26    kardel 	if (timercmp(&utv, &t, >)) {
    976  1.26    kardel 		sc->lastread = utv;
    977  1.26    kardel 		sc->refresh_sensor_data(sc);
    978  1.27   hannken 	}
    979   1.5    bouyer 
    980  1.26    kardel 	*tred = sc->sensors[tred->sensor];
    981   1.5    bouyer 
    982  1.26    kardel 	return 0;
    983   1.5    bouyer }
    984   1.1      groo 
    985  1.30   xtraeme static int
    986  1.30   xtraeme generic_streinfo_fan(struct lm_softc *sc, envsys_basic_info_t *info, int n,
    987  1.30   xtraeme 	envsys_basic_info_t *binfo)
    988   1.7    bouyer {
    989  1.30   xtraeme 	uint8_t sdata;
    990   1.7    bouyer 	int divisor;
    991   1.7    bouyer 
    992   1.7    bouyer 	/* FAN1 and FAN2 can have divisors set, but not FAN3 */
    993   1.7    bouyer 	if ((sc->info[binfo->sensor].units == ENVSYS_SFANRPM)
    994  1.14      tron 	    && (n < 2)) {
    995   1.7    bouyer 		if (binfo->rpms == 0) {
    996   1.7    bouyer 			binfo->validflags = 0;
    997  1.19  christos 			return 0;
    998   1.7    bouyer 		}
    999   1.7    bouyer 
   1000  1.14      tron 		/* write back the nominal FAN speed  */
   1001  1.14      tron 		info->rpms = binfo->rpms;
   1002  1.14      tron 
   1003   1.7    bouyer 		/* 153 is the nominal FAN speed value */
   1004   1.7    bouyer 		divisor = 1350000 / (binfo->rpms * 153);
   1005   1.7    bouyer 
   1006   1.7    bouyer 		/* ...but we need lg(divisor) */
   1007   1.7    bouyer 		if (divisor <= 1)
   1008   1.7    bouyer 		    divisor = 0;
   1009   1.7    bouyer 		else if (divisor <= 2)
   1010   1.7    bouyer 		    divisor = 1;
   1011   1.7    bouyer 		else if (divisor <= 4)
   1012   1.7    bouyer 		    divisor = 2;
   1013   1.7    bouyer 		else
   1014   1.7    bouyer 		    divisor = 3;
   1015   1.7    bouyer 
   1016   1.7    bouyer 		/*
   1017   1.7    bouyer 		 * FAN1 div is in bits <5:4>, FAN2 div is
   1018   1.7    bouyer 		 * in <7:6>
   1019   1.7    bouyer 		 */
   1020  1.17        ad 		sdata = (*sc->lm_readreg)(sc, LMD_VIDFAN);
   1021  1.14      tron 		if ( n == 0 ) {  /* FAN1 */
   1022   1.7    bouyer 		    divisor <<= 4;
   1023   1.7    bouyer 		    sdata = (sdata & 0xCF) | divisor;
   1024   1.7    bouyer 		} else { /* FAN2 */
   1025   1.7    bouyer 		    divisor <<= 6;
   1026   1.7    bouyer 		    sdata = (sdata & 0x3F) | divisor;
   1027   1.7    bouyer 		}
   1028   1.7    bouyer 
   1029  1.17        ad 		(*sc->lm_writereg)(sc, LMD_VIDFAN, sdata);
   1030   1.7    bouyer 	}
   1031  1.19  christos 	return 0;
   1032   1.7    bouyer 
   1033   1.7    bouyer }
   1034   1.7    bouyer 
   1035  1.30   xtraeme static int
   1036  1.30   xtraeme lm_streinfo(struct sysmon_envsys *sme, envsys_basic_info_t *binfo)
   1037   1.1      groo {
   1038   1.5    bouyer 	 struct lm_softc *sc = sme->sme_cookie;
   1039   1.5    bouyer 
   1040   1.5    bouyer 	 if (sc->info[binfo->sensor].units == ENVSYS_SVOLTS_DC)
   1041   1.5    bouyer 		  sc->info[binfo->sensor].rfact = binfo->rfact;
   1042   1.5    bouyer 	 else {
   1043   1.7    bouyer 		if (sc->info[binfo->sensor].units == ENVSYS_SFANRPM) {
   1044   1.7    bouyer 			generic_streinfo_fan(sc, &sc->info[binfo->sensor],
   1045   1.7    bouyer 			    binfo->sensor - 8, binfo);
   1046   1.7    bouyer 		}
   1047  1.19  christos 		strlcpy(sc->info[binfo->sensor].desc, binfo->desc,
   1048   1.7    bouyer 		    sizeof(sc->info[binfo->sensor].desc));
   1049   1.7    bouyer 		binfo->validflags = ENVSYS_FVALID;
   1050   1.7    bouyer 	 }
   1051  1.19  christos 	 return 0;
   1052   1.7    bouyer }
   1053   1.5    bouyer 
   1054  1.30   xtraeme static int
   1055  1.30   xtraeme wb781_streinfo(struct sysmon_envsys *sme, envsys_basic_info_t *binfo)
   1056   1.7    bouyer {
   1057   1.7    bouyer 	 struct lm_softc *sc = sme->sme_cookie;
   1058  1.14      tron 	 int divisor;
   1059  1.30   xtraeme 	 uint8_t sdata;
   1060  1.14      tron 	 int i;
   1061   1.5    bouyer 
   1062   1.7    bouyer 	 if (sc->info[binfo->sensor].units == ENVSYS_SVOLTS_DC)
   1063   1.7    bouyer 		  sc->info[binfo->sensor].rfact = binfo->rfact;
   1064   1.7    bouyer 	 else {
   1065   1.7    bouyer 		if (sc->info[binfo->sensor].units == ENVSYS_SFANRPM) {
   1066  1.14      tron 			if (binfo->rpms == 0) {
   1067  1.14      tron 				binfo->validflags = 0;
   1068  1.19  christos 				return 0;
   1069  1.14      tron 			}
   1070  1.14      tron 
   1071  1.14      tron 			/* write back the nominal FAN speed  */
   1072  1.14      tron 			sc->info[binfo->sensor].rpms = binfo->rpms;
   1073  1.14      tron 
   1074  1.14      tron 			/* 153 is the nominal FAN speed value */
   1075  1.14      tron 			divisor = 1350000 / (binfo->rpms * 153);
   1076  1.14      tron 
   1077  1.14      tron 			/* ...but we need lg(divisor) */
   1078  1.14      tron 			for (i = 0; i < 7; i++) {
   1079  1.14      tron 				if (divisor <= (1 << i))
   1080  1.14      tron 				 	break;
   1081  1.14      tron 			}
   1082  1.14      tron 			divisor = i;
   1083  1.14      tron 
   1084  1.14      tron 			if (binfo->sensor == 10 || binfo->sensor == 11) {
   1085  1.14      tron 				/*
   1086  1.14      tron 				 * FAN1 div is in bits <5:4>, FAN2 div
   1087  1.14      tron 				 * is in <7:6>
   1088  1.14      tron 				 */
   1089  1.17        ad 				sdata = (*sc->lm_readreg)(sc, LMD_VIDFAN);
   1090  1.14      tron 				if ( binfo->sensor == 10 ) {  /* FAN1 */
   1091  1.14      tron 					 sdata = (sdata & 0xCF) |
   1092  1.14      tron 					     ((divisor & 0x3) << 4);
   1093  1.14      tron 				} else { /* FAN2 */
   1094  1.14      tron 					 sdata = (sdata & 0x3F) |
   1095  1.14      tron 					     ((divisor & 0x3) << 6);
   1096  1.14      tron 				}
   1097  1.17        ad 				(*sc->lm_writereg)(sc, LMD_VIDFAN, sdata);
   1098  1.14      tron 			} else {
   1099  1.14      tron 				/* FAN3 is in WB_PIN <7:6> */
   1100  1.17        ad 				sdata = (*sc->lm_readreg)(sc, WB_PIN);
   1101  1.14      tron 				sdata = (sdata & 0x3F) |
   1102  1.14      tron 				     ((divisor & 0x3) << 6);
   1103  1.17        ad 				(*sc->lm_writereg)(sc, WB_PIN, sdata);
   1104  1.14      tron 			}
   1105   1.7    bouyer 		}
   1106  1.19  christos 		strlcpy(sc->info[binfo->sensor].desc, binfo->desc,
   1107   1.7    bouyer 		    sizeof(sc->info[binfo->sensor].desc));
   1108   1.7    bouyer 		binfo->validflags = ENVSYS_FVALID;
   1109   1.5    bouyer 	 }
   1110  1.19  christos 	 return 0;
   1111   1.5    bouyer }
   1112   1.5    bouyer 
   1113  1.30   xtraeme static int
   1114  1.30   xtraeme wb782_streinfo(struct sysmon_envsys *sme, envsys_basic_info_t *binfo)
   1115   1.5    bouyer {
   1116   1.5    bouyer 	 struct lm_softc *sc = sme->sme_cookie;
   1117   1.5    bouyer 	 int divisor;
   1118  1.30   xtraeme 	 uint8_t sdata;
   1119   1.5    bouyer 	 int i;
   1120   1.5    bouyer 
   1121   1.5    bouyer 	 if (sc->info[binfo->sensor].units == ENVSYS_SVOLTS_DC)
   1122   1.5    bouyer 		  sc->info[binfo->sensor].rfact = binfo->rfact;
   1123   1.5    bouyer 	 else {
   1124   1.5    bouyer 	 	if (sc->info[binfo->sensor].units == ENVSYS_SFANRPM) {
   1125   1.4   thorpej 			if (binfo->rpms == 0) {
   1126   1.4   thorpej 				binfo->validflags = 0;
   1127  1.19  christos 				return 0;
   1128   1.1      groo 			}
   1129   1.1      groo 
   1130  1.14      tron 			/* write back the nominal FAN speed  */
   1131  1.14      tron 			sc->info[binfo->sensor].rpms = binfo->rpms;
   1132  1.14      tron 
   1133   1.4   thorpej 			/* 153 is the nominal FAN speed value */
   1134   1.4   thorpej 			divisor = 1350000 / (binfo->rpms * 153);
   1135   1.1      groo 
   1136   1.4   thorpej 			/* ...but we need lg(divisor) */
   1137   1.5    bouyer 			for (i = 0; i < 7; i++) {
   1138   1.5    bouyer 				if (divisor <= (1 << i))
   1139   1.5    bouyer 				 	break;
   1140   1.5    bouyer 			}
   1141   1.5    bouyer 			divisor = i;
   1142   1.4   thorpej 
   1143   1.5    bouyer 			if (binfo->sensor == 12 || binfo->sensor == 13) {
   1144   1.5    bouyer 				/*
   1145   1.5    bouyer 				 * FAN1 div is in bits <5:4>, FAN2 div
   1146   1.5    bouyer 				 * is in <7:6>
   1147   1.5    bouyer 				 */
   1148  1.17        ad 				sdata = (*sc->lm_readreg)(sc, LMD_VIDFAN);
   1149   1.5    bouyer 				if ( binfo->sensor == 12 ) {  /* FAN1 */
   1150   1.5    bouyer 					 sdata = (sdata & 0xCF) |
   1151   1.5    bouyer 					     ((divisor & 0x3) << 4);
   1152   1.5    bouyer 				} else { /* FAN2 */
   1153   1.5    bouyer 					 sdata = (sdata & 0x3F) |
   1154   1.5    bouyer 					     ((divisor & 0x3) << 6);
   1155   1.5    bouyer 				}
   1156  1.17        ad 				(*sc->lm_writereg)(sc, LMD_VIDFAN, sdata);
   1157   1.5    bouyer 			} else {
   1158   1.5    bouyer 				/* FAN3 is in WB_PIN <7:6> */
   1159  1.17        ad 				sdata = (*sc->lm_readreg)(sc, WB_PIN);
   1160   1.5    bouyer 				sdata = (sdata & 0x3F) |
   1161   1.5    bouyer 				     ((divisor & 0x3) << 6);
   1162  1.17        ad 				(*sc->lm_writereg)(sc, WB_PIN, sdata);
   1163   1.1      groo 			}
   1164  1.30   xtraeme 			/* Bit 2 of divisor is in WB_BANK0_VBAT */
   1165  1.30   xtraeme 			lm_generic_banksel(sc, WB_BANKSEL_B0);
   1166  1.30   xtraeme 			sdata = (*sc->lm_readreg)(sc, WB_BANK0_VBAT);
   1167   1.5    bouyer 			sdata &= ~(0x20 << (binfo->sensor - 12));
   1168   1.5    bouyer 			sdata |= (divisor & 0x4) << (binfo->sensor - 9);
   1169  1.30   xtraeme 			(*sc->lm_writereg)(sc, WB_BANK0_VBAT, sdata);
   1170   1.1      groo 		}
   1171   1.1      groo 
   1172  1.19  christos 		strlcpy(sc->info[binfo->sensor].desc, binfo->desc,
   1173   1.4   thorpej 		    sizeof(sc->info[binfo->sensor].desc));
   1174  1.19  christos 		binfo->validflags = ENVSYS_FVALID;
   1175  1.19  christos 	}
   1176  1.19  christos 	return 0;
   1177  1.19  christos }
   1178