Module Name:    src
Committed By:   thorpej
Date:           Wed May 19 03:46:26 UTC 2021

Modified Files:
        src/sys/dev/spi [thorpej-i2c-spi-conf]: m25p.c mcp23s17.c mcp3k.c
            mcp48x1.c oj6sh.c ssdfb_spi.c tmp121.c

Log Message:
match/probe routines should not have side-effects; call spi_configure()
from the attach routine.


To generate a diff of this commit:
cvs rdiff -u -r1.17 -r1.17.4.1 src/sys/dev/spi/m25p.c
cvs rdiff -u -r1.2.2.1 -r1.2.2.2 src/sys/dev/spi/mcp23s17.c
cvs rdiff -u -r1.2.36.1 -r1.2.36.2 src/sys/dev/spi/mcp3k.c
cvs rdiff -u -r1.1.54.1 -r1.1.54.2 src/sys/dev/spi/mcp48x1.c
cvs rdiff -u -r1.8 -r1.8.2.1 src/sys/dev/spi/oj6sh.c
cvs rdiff -u -r1.5 -r1.5.4.1 src/sys/dev/spi/ssdfb_spi.c
cvs rdiff -u -r1.5.72.1 -r1.5.72.2 src/sys/dev/spi/tmp121.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/dev/spi/m25p.c
diff -u src/sys/dev/spi/m25p.c:1.17 src/sys/dev/spi/m25p.c:1.17.4.1
--- src/sys/dev/spi/m25p.c:1.17	Wed Jan 27 02:32:31 2021
+++ src/sys/dev/spi/m25p.c	Wed May 19 03:46:26 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: m25p.c,v 1.17 2021/01/27 02:32:31 thorpej Exp $ */
+/* $NetBSD: m25p.c,v 1.17.4.1 2021/05/19 03:46:26 thorpej Exp $ */
 
 /*-
  * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
@@ -42,7 +42,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: m25p.c,v 1.17 2021/01/27 02:32:31 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: m25p.c,v 1.17.4.1 2021/05/19 03:46:26 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -125,17 +125,8 @@ static int
 m25p_match(device_t parent, cfdata_t cf, void *aux)
 {
 	struct spi_attach_args *sa = aux;
-	int res;
 
-	res = spi_compatible_match(sa, cf, compat_data);
-	if (!res)
-		return res;
-
-	/* configure for 20MHz, which is the max for normal reads */
-	if (spi_configure(sa->sa_handle, SPI_MODE_0, 20000000))
-		res = 0;
-
-	return res;
+	return spi_compatible_match(sa, cf, compat_data);
 }
 
 static void
@@ -143,12 +134,21 @@ m25p_attach(device_t parent, device_t se
 {
 	struct m25p_softc *sc = device_private(self);
 	struct spi_attach_args *sa = aux;
+	int error;
 
 	sc->sc_sh = sa->sa_handle;
 
 	aprint_normal("\n");
 	aprint_naive("\n");
 
+	/* configure for 20MHz, which is the max for normal reads */
+	error = spi_configure(sa->sa_handle, SPI_MODE_0, 20000000);
+	if (error) {
+		aprint_error_dev(self, "spi_configure failed (error = %d)\n",
+		    error);
+		return;
+	}
+
 	config_interrupts(self, m25p_doattach);
 }
 

Index: src/sys/dev/spi/mcp23s17.c
diff -u src/sys/dev/spi/mcp23s17.c:1.2.2.1 src/sys/dev/spi/mcp23s17.c:1.2.2.2
--- src/sys/dev/spi/mcp23s17.c:1.2.2.1	Wed May 19 03:32:27 2021
+++ src/sys/dev/spi/mcp23s17.c	Wed May 19 03:46:26 2021
@@ -1,4 +1,4 @@
-/*      $NetBSD: mcp23s17.c,v 1.2.2.1 2021/05/19 03:32:27 thorpej Exp $ */
+/*      $NetBSD: mcp23s17.c,v 1.2.2.2 2021/05/19 03:46:26 thorpej Exp $ */
 
 /*-
  * Copyright (c) 2014 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: mcp23s17.c,v 1.2.2.1 2021/05/19 03:32:27 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: mcp23s17.c,v 1.2.2.2 2021/05/19 03:46:26 thorpej Exp $");
 
 /* 
  * Driver for Microchip MCP23S17 GPIO
@@ -105,16 +105,8 @@ static int
 mcp23s17gpio_match(device_t parent, cfdata_t cf, void *aux)
 {
 	struct spi_attach_args *sa = aux;
-	int rv;
 
-	rv = spi_compatible_match(sa, cf, compat_data);
-	if (rv != 0) {
-		/* run at 10MHz */
-		if (spi_configure(sa->sa_handle, SPI_MODE_0, 10000000))
-			return 0;
-	}
-
-	return rv;
+	return spi_compatible_match(sa, cf, compat_data);
 }
 
 static void
@@ -122,6 +114,7 @@ mcp23s17gpio_attach(device_t parent, dev
 {
 	struct mcp23s17gpio_softc *sc;
 	struct spi_attach_args *sa;
+	int error;
 #if NGPIO > 0
 	int i;
 	struct gpiobus_attach_args gba;
@@ -142,6 +135,14 @@ mcp23s17gpio_attach(device_t parent, dev
 	aprint_naive(": GPIO\n");	
 	aprint_normal(": MCP23S17 GPIO (ha=%d)\n", sc->sc_ha);
 
+	/* run at 10MHz */
+	error = spi_configure(sa->sa_handle, SPI_MODE_0, 10000000);
+	if (error) {
+		aprint_error_dev(self, "spi_configure failed (error = %d)\n",
+		    error);
+		return;
+	}
+
 	DPRINTF(1, ("%s: initialize (HAEN|SEQOP)\n", device_xname(sc->sc_dev)));
 
 	/* basic setup */

Index: src/sys/dev/spi/mcp3k.c
diff -u src/sys/dev/spi/mcp3k.c:1.2.36.1 src/sys/dev/spi/mcp3k.c:1.2.36.2
--- src/sys/dev/spi/mcp3k.c:1.2.36.1	Wed May 19 03:33:05 2021
+++ src/sys/dev/spi/mcp3k.c	Wed May 19 03:46:26 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: mcp3k.c,v 1.2.36.1 2021/05/19 03:33:05 thorpej Exp $ */
+/*	$NetBSD: mcp3k.c,v 1.2.36.2 2021/05/19 03:46:26 thorpej Exp $ */
 
 /*-
  * Copyright (c) 2015 The NetBSD Foundation, Inc.
@@ -242,10 +242,6 @@ mcp3kadc_match(device_t parent, cfdata_t
 		if (sa->sa_clist == NULL && mcp3kadc_lookup(sa, cf) == NULL) {
 			return 0;
 		}
-
-		/* configure for 1MHz */
-		if (spi_configure(sa->sa_handle, SPI_MODE_0, 1000000))
-			return 0;
 	}
 
 	return rv;
@@ -258,7 +254,7 @@ mcp3kadc_attach(device_t parent, device_
 	struct spi_attach_args *sa = aux;
 	struct mcp3kadc_softc *sc = device_private(self);
 	const struct mcp3kadc_model *model;
-	int ch, i;
+	int ch, i, error;
 
 	sc->sc_dev = self;
 	sc->sc_sh = sa->sa_handle;
@@ -273,6 +269,14 @@ mcp3kadc_attach(device_t parent, device_
 	    (unsigned)model->name, (unsigned)model->channels,
 	    (unsigned)model->bits);
 
+	/* configure for 1MHz */
+	error = spi_configure(sa->sa_handle, SPI_MODE_0, 1000000);
+	if (error) {
+		aprint_error_dev(self, "spi_configure failed (error = %d)\n",
+		    error);
+		return;
+	}
+
 	/*
 	 * XXX Get vref-supply from device tree and make the sysctl
 	 * XXX read-only in that case.

Index: src/sys/dev/spi/mcp48x1.c
diff -u src/sys/dev/spi/mcp48x1.c:1.1.54.1 src/sys/dev/spi/mcp48x1.c:1.1.54.2
--- src/sys/dev/spi/mcp48x1.c:1.1.54.1	Wed May 19 03:33:33 2021
+++ src/sys/dev/spi/mcp48x1.c	Wed May 19 03:46:26 2021
@@ -1,4 +1,4 @@
-/*      $NetBSD: mcp48x1.c,v 1.1.54.1 2021/05/19 03:33:33 thorpej Exp $ */
+/*      $NetBSD: mcp48x1.c,v 1.1.54.2 2021/05/19 03:46:26 thorpej Exp $ */
 
 /*-
  * Copyright (c) 2014 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: mcp48x1.c,v 1.1.54.1 2021/05/19 03:33:33 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: mcp48x1.c,v 1.1.54.2 2021/05/19 03:46:26 thorpej Exp $");
 
 /* 
  * Driver for Microchip MCP4801/MCP4811/MCP4821 DAC. 
@@ -158,10 +158,6 @@ mcp48x1dac_match(device_t parent, cfdata
 		if (sa->sa_clist == NULL && mcp48x1dac_lookup(sa, cf) == NULL) {
 			return 0;
 		}
-
-		/* configure for 20MHz */
-		if (spi_configure(sa->sa_handle, SPI_MODE_0, 20000000))
-			return 0;
 	}
 
 	return rv;
@@ -173,6 +169,7 @@ mcp48x1dac_attach(device_t parent, devic
 	struct mcp48x1dac_softc *sc = device_private(self);
 	struct spi_attach_args *sa = aux;
 	const struct mcp48x1dac_model *model;
+	int error;
 
 	sc->sc_dev = self;
 	sc->sc_sh = sa->sa_handle;
@@ -185,6 +182,14 @@ mcp48x1dac_attach(device_t parent, devic
 	aprint_naive(": Digital to Analog converter\n");	
 	aprint_normal(": MCP%u DAC\n", model->name);
 
+	/* configure for 20MHz */
+	error = spi_configure(sa->sa_handle, SPI_MODE_0, 20000000);
+	if (error) {
+		aprint_error_dev(self, "spi_configure failed (error = %d)\n",
+		    error);
+		return;
+	}
+
 	if(!mcp48x1dac_envsys_attach(sc)) {
 		aprint_error_dev(sc->sc_dev, "failed to attach envsys\n");
 		return;

Index: src/sys/dev/spi/oj6sh.c
diff -u src/sys/dev/spi/oj6sh.c:1.8 src/sys/dev/spi/oj6sh.c:1.8.2.1
--- src/sys/dev/spi/oj6sh.c:1.8	Sat Apr 24 23:36:59 2021
+++ src/sys/dev/spi/oj6sh.c	Wed May 19 03:46:26 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: oj6sh.c,v 1.8 2021/04/24 23:36:59 thorpej Exp $	*/
+/*	$NetBSD: oj6sh.c,v 1.8.2.1 2021/05/19 03:46:26 thorpej Exp $	*/
 
 /*
  * Copyright (c) 2014  Genetec Corporation.  All rights reserved.
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: oj6sh.c,v 1.8 2021/04/24 23:36:59 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: oj6sh.c,v 1.8.2.1 2021/05/19 03:46:26 thorpej Exp $");
 
 #include "opt_oj6sh.h"
 
@@ -138,8 +138,6 @@ oj6sh_match(device_t parent, cfdata_t cf
 
 	if (spi_compatible_match(sa, cf, compat_data) == 0)
 		return 0;
-	if (spi_configure(sa->sa_handle, SPI_MODE_0, 2500000))
-		return 0;
 
 	return 2;
 }
@@ -184,10 +182,18 @@ oj6sh_attach(device_t parent, device_t s
 	struct oj6sh_softc *sc = device_private(self);
 	struct spi_attach_args *sa = aux;
 	struct wsmousedev_attach_args a;
+	int errir;
 
 	aprint_naive("\n");
 	aprint_normal(": OJ6SH-T25 Optical Joystick\n");
 
+	error = spi_configure(sa->sa_handle, SPI_MODE_0, 2500000);
+	if (error) {
+		aprint_error_dev(self, "spi_configure failed (error = %d)\n",
+		    error);
+		return;
+	}
+
 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
 
 	sc->sc_dev = self;

Index: src/sys/dev/spi/ssdfb_spi.c
diff -u src/sys/dev/spi/ssdfb_spi.c:1.5 src/sys/dev/spi/ssdfb_spi.c:1.5.4.1
--- src/sys/dev/spi/ssdfb_spi.c:1.5	Wed Jan 27 02:32:31 2021
+++ src/sys/dev/spi/ssdfb_spi.c	Wed May 19 03:46:26 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: ssdfb_spi.c,v 1.5 2021/01/27 02:32:31 thorpej Exp $ */
+/* $NetBSD: ssdfb_spi.c,v 1.5.4.1 2021/05/19 03:46:26 thorpej Exp $ */
 
 /*
  * Copyright (c) 2019 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ssdfb_spi.c,v 1.5 2021/01/27 02:32:31 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ssdfb_spi.c,v 1.5.4.1 2021/05/19 03:46:26 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/device.h>
@@ -82,19 +82,8 @@ static int
 ssdfb_spi_match(device_t parent, cfdata_t match, void *aux)
 {
 	struct spi_attach_args *sa = aux;
-	int res;
 
-	res = spi_compatible_match(sa, match, compat_data);
-	if (!res)
-		return res;
-
-	/*
-	 * SSD1306 and SSD1322 data sheets specify 100ns cycle time.
-	 */
-	if (spi_configure(sa->sa_handle, SPI_MODE_0, 10000000))
-		res = 0;
-
-	return res;
+	return spi_compatible_match(sa, match, compat_data);
 }
 
 static void
@@ -104,12 +93,24 @@ ssdfb_spi_attach(device_t parent, device
 	struct cfdata *cf = device_cfdata(self);
 	struct spi_attach_args *sa = aux;
 	int flags = cf->cf_flags;
+	int error;
 
 	sc->sc.sc_dev = self;
 	sc->sc_sh = sa->sa_handle;
 	sc->sc.sc_cookie = (void *)sc;
 	if ((flags & SSDFB_ATTACH_FLAG_PRODUCT_MASK) == SSDFB_PRODUCT_UNKNOWN)
 		flags |= SSDFB_PRODUCT_SSD1322_GENERIC;
+
+	/*
+	 * SSD1306 and SSD1322 data sheets specify 100ns cycle time.
+	 */
+	error = spi_configure(sa->sa_handle, SPI_MODE_0, 10000000);
+	if (error) {
+		aprint_error(": spi_configure failed (error = %d)\n",
+		    error);
+		return;
+	}
+
 	/*
 	 * Note on interface modes.
 	 *

Index: src/sys/dev/spi/tmp121.c
diff -u src/sys/dev/spi/tmp121.c:1.5.72.1 src/sys/dev/spi/tmp121.c:1.5.72.2
--- src/sys/dev/spi/tmp121.c:1.5.72.1	Wed May 19 03:34:11 2021
+++ src/sys/dev/spi/tmp121.c	Wed May 19 03:46:26 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: tmp121.c,v 1.5.72.1 2021/05/19 03:34:11 thorpej Exp $ */
+/* $NetBSD: tmp121.c,v 1.5.72.2 2021/05/19 03:46:26 thorpej Exp $ */
 
 /*-
  * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
@@ -42,7 +42,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: tmp121.c,v 1.5.72.1 2021/05/19 03:34:11 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: tmp121.c,v 1.5.72.2 2021/05/19 03:46:26 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -91,16 +91,8 @@ static int
 tmp121temp_match(device_t parent, cfdata_t cf, void *aux)
 {
 	struct spi_attach_args *sa = aux;
-	int rv;
 
-	rv = spi_compatible_match(sa, cf, compat_data);
-	if (rv != 0) {
-		/* configure for 10MHz */
-		if (spi_configure(sa->sa_handle, SPI_MODE_0, 1000000))
-			return 0;
-	}
-
-	return rv;
+	return spi_compatible_match(sa, cf, compat_data);
 }
 
 static void
@@ -108,10 +100,19 @@ tmp121temp_attach(device_t parent, devic
 {
 	struct tmp121temp_softc *sc = device_private(self);
 	struct spi_attach_args *sa = aux;
+	int error;
 
 	aprint_naive(": Temperature Sensor\n");	
 	aprint_normal(": TI TMP121 Temperature Sensor\n");
 
+	/* configure for 10MHz */
+	error = spi_configure(sa->sa_handle, SPI_MODE_0, 1000000);
+	if (error) {
+		aprint_error_dev(self, "spi_configure failed (error = %d)\n",
+		    error);
+		return;
+	}
+
 	sc->sc_sh = sa->sa_handle;
 
 	sc->sc_sme = sysmon_envsys_create();

Reply via email to