Module Name:    src
Committed By:   jdc
Date:           Sun Dec 20 09:08:15 UTC 2020

Modified Files:
        src/sys/arch/sparc64/dev: pcf8591_envctrl.c

Log Message:
For the E250:
  set fans to medium speed when attaching and full speed when detaching.
  remove unused PS fan definition.
In general:
  return early from attach if the device isn't present.
  handle errors from sysmon during attach so that detach doesn't crash.
  try to handle errors when acquiring the iic bus during detach.


To generate a diff of this commit:
cvs rdiff -u -r1.14 -r1.15 src/sys/arch/sparc64/dev/pcf8591_envctrl.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/sparc64/dev/pcf8591_envctrl.c
diff -u src/sys/arch/sparc64/dev/pcf8591_envctrl.c:1.14 src/sys/arch/sparc64/dev/pcf8591_envctrl.c:1.15
--- src/sys/arch/sparc64/dev/pcf8591_envctrl.c:1.14	Mon Dec  7 13:24:15 2020
+++ src/sys/arch/sparc64/dev/pcf8591_envctrl.c	Sun Dec 20 09:08:15 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: pcf8591_envctrl.c,v 1.14 2020/12/07 13:24:15 jdc Exp $	*/
+/*	$NetBSD: pcf8591_envctrl.c,v 1.15 2020/12/20 09:08:15 jdc Exp $	*/
 /*	$OpenBSD: pcf8591_envctrl.c,v 1.6 2007/10/25 21:17:20 kettenis Exp $ */
 
 /*
@@ -19,7 +19,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: pcf8591_envctrl.c,v 1.14 2020/12/07 13:24:15 jdc Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pcf8591_envctrl.c,v 1.15 2020/12/20 09:08:15 jdc Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -55,7 +55,6 @@ __KERNEL_RCSID(0, "$NetBSD: pcf8591_envc
 
 #define PCF8591_TEMP_SENS	0x00
 #define PCF8591_CPU_FAN_CTRL	0x01
-#define PCF8591_PS_FAN_CTRL	0x02
 
 struct ecadc_channel {
 	u_int		chan_num;
@@ -89,6 +88,7 @@ static int	ecadc_detach(device_t, int);
 static void	ecadc_refresh(struct sysmon_envsys *, envsys_data_t *);
 static void	ecadc_get_limits(struct sysmon_envsys *, envsys_data_t *,
 			sysmon_envsys_lim_t *, u_int32_t *);
+static int	ecadc_set_fan_speed(struct ecadc_softc *, u_int8_t, u_int8_t);
 static void	ecadc_timeout(void *);
 static void	ecadc_fan_adjust(void *);
 
@@ -205,6 +205,21 @@ ecadc_attach(device_t parent, device_t s
 		sc->sc_nchan++;
 	}
 
+	sc->sc_tag = ia->ia_tag;
+	sc->sc_addr = ia->ia_addr;
+
+	iic_acquire_bus(sc->sc_tag, 0);
+
+	/* Try a read now, so we can fail if this component isn't present */
+	if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
+	    NULL, 0, junk, sc->sc_nchan + 1, 0)) {
+		aprint_normal(": read failed\n");
+		iic_release_bus(sc->sc_tag, 0);
+		return;
+	}
+
+	iic_release_bus(sc->sc_tag, 0);
+
 	/*
 	 * Fan speed changing information is missing from OFW
 	 * The E250 CPU fan is connected to the sensor at addr 0x4a, channel 1
@@ -214,7 +229,6 @@ ecadc_attach(device_t parent, device_t s
 	    XLATE_MAX) > 0) {
 		sc->sc_channels[sc->sc_nchan].chan_num = 1;
 		sc->sc_channels[sc->sc_nchan].chan_type = PCF8591_CPU_FAN_CTRL;
-		sc->sc_channels[sc->sc_nchan].chan_speed = 0;
 		sensor = &sc->sc_channels[sc->sc_nchan].chan_sensor;
 		sensor->units = ENVSYS_INTEGER;
 		sensor->flags = ENVSYS_FMONNOTSUPP;
@@ -225,26 +239,17 @@ ecadc_attach(device_t parent, device_t s
 		    "added CPUFAN sensor (chan %d) with cpu-fan xlate\n",
 		    device_xname(sc->sc_dev),
 		    sc->sc_channels[sc->sc_nchan].chan_num);
-		sc->sc_nchan++;
 
-		sc->sc_hastimer = 1;
-	}
+		/* Set the fan to medium speed */
+		sc->sc_channels[sc->sc_nchan].chan_speed =
+		    (sc->sc_cpu_fan_spd[0]+sc->sc_cpu_fan_spd[XLATE_MAX])/2;
+		ecadc_set_fan_speed(sc, sc->sc_channels[sc->sc_nchan].chan_num,
+		    sc->sc_channels[sc->sc_nchan].chan_speed);
 
-	sc->sc_tag = ia->ia_tag;
-	sc->sc_addr = ia->ia_addr;
-
-	iic_acquire_bus(sc->sc_tag, 0);
-
-	/* Try a read now, so we can fail if this component isn't present */
-	if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
-	    NULL, 0, junk, sc->sc_nchan + 1, 0)) {
-		aprint_normal(": read failed\n");
-		iic_release_bus(sc->sc_tag, 0);
-		return;
+		sc->sc_nchan++;
+		sc->sc_hastimer = 1;
 	}
 
-	iic_release_bus(sc->sc_tag, 0);
-
 	/* Hook us into the sysmon_envsys subsystem */
 	sc->sc_sme = sysmon_envsys_create();
 	sc->sc_sme->sme_name = device_xname(self);
@@ -280,6 +285,8 @@ static int
 ecadc_detach(device_t self, int flags)
 {
 	struct ecadc_softc *sc = device_private(self);
+	int c, i;
+
 	if (sc->sc_hastimer) {
 		callout_halt(&sc->sc_timer, NULL);
 		callout_destroy(&sc->sc_timer);
@@ -288,6 +295,23 @@ ecadc_detach(device_t self, int flags)
 	if (sc->sc_sme != NULL)
 		sysmon_envsys_unregister(sc->sc_sme);
 
+	for (i = 0; i < sc->sc_nchan; i++) {
+		struct ecadc_channel *chp = &sc->sc_channels[i];
+
+		if (chp->chan_type == PCF8591_CPU_FAN_CTRL) {
+			/* Loop in case the bus is busy */
+			for (c = 0; c < 5; c++) {
+				chp->chan_speed = sc->sc_cpu_fan_spd[0];
+				if (!ecadc_set_fan_speed(sc, chp->chan_num,
+				    chp->chan_speed))
+					return 0;
+				delay(10000);
+			}
+			aprint_error_dev(sc->sc_dev,
+			    "cannot set fan speed (chan %d)\n", chp->chan_num);
+		}
+	}
+
 	return 0;
 }
 
@@ -350,8 +374,7 @@ ecadc_refresh(struct sysmon_envsys *sme,
 			}
 			chp->chan_sensor.flags |= ENVSYS_FMONLIMITS;
 		}
-		if (chp->chan_type == PCF8591_CPU_FAN_CTRL ||
-		    chp->chan_type == PCF8591_PS_FAN_CTRL)
+		if (chp->chan_type == PCF8591_CPU_FAN_CTRL)
 			chp->chan_sensor.value_cur = data[1 + chp->chan_num];
 
 		chp->chan_sensor.state = ENVSYS_SVALID;

Reply via email to