Probe / match routines should not have side-effects: do the spi_configure()

calls in the attach routines.
This commit is contained in:
thorpej 2022-01-19 05:05:45 +00:00
parent da1006fc7e
commit c86942858d
9 changed files with 100 additions and 72 deletions

View File

@ -1,5 +1,5 @@
/* $Id: mpcsa_leds.c,v 1.7 2021/08/07 16:18:50 thorpej Exp $ */
/* $NetBSD: mpcsa_leds.c,v 1.7 2021/08/07 16:18:50 thorpej Exp $ */
/* $Id: mpcsa_leds.c,v 1.8 2022/01/19 05:05:45 thorpej Exp $ */
/* $NetBSD: mpcsa_leds.c,v 1.8 2022/01/19 05:05:45 thorpej Exp $ */
/*
* Copyright (c) 2007 Embedtronics Oy. All rights reserved.
@ -30,7 +30,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: mpcsa_leds.c,v 1.7 2021/08/07 16:18:50 thorpej Exp $");
__KERNEL_RCSID(0, "$NetBSD: mpcsa_leds.c,v 1.8 2022/01/19 05:05:45 thorpej Exp $");
#include <sys/param.h>
#include <sys/systm.h>
@ -118,14 +118,10 @@ static struct mpcsa_leds_softc *mpcsa_leds_sc;
static int
mpcsa_leds_match(device_t parent, cfdata_t match, void *aux)
{
struct spi_attach_args *sa = aux;
if (strcmp(match->cf_name, "mpcsa_leds"))
return 0;
if (spi_configure(sa->sa_handle, SPI_MODE_0, 10000000))
return 0;
return 2;
}
@ -138,10 +134,18 @@ mpcsa_leds_attach(device_t parent, device_t self, void *aux)
struct gpiobus_attach_args gba;
#endif
int n;
int error;
aprint_naive(": output buffer\n");
aprint_normal(": 74HC595 or compatible shift register(s)\n");
error = spi_configure(sa->sa_handle, SPI_MODE_0, 10000000);
if (error) {
aprint_error_dev(self,
"failed to set Mode 0 @ 10MHz, error=%d\n", error);
return;
}
sc->sc_sh = sa->sa_handle;
sc->sc_pinstate = 0xffff;
callout_init(&sc->sc_c, 0);

View File

@ -1,4 +1,4 @@
/* $NetBSD: m25p.c,v 1.18 2021/05/14 09:25:14 jmcneill Exp $ */
/* $NetBSD: m25p.c,v 1.19 2022/01/19 05:05:45 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.18 2021/05/14 09:25:14 jmcneill Exp $");
__KERNEL_RCSID(0, "$NetBSD: m25p.c,v 1.19 2022/01/19 05:05:45 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 self, void *aux)
{
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,
"failed to set Mode 0 @ 20MHz, error=%d\n", error);
return;
}
config_interrupts(self, m25p_doattach);
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: mcp23xxxgpio_spi.c,v 1.2 2022/01/17 19:36:54 thorpej Exp $ */
/* $NetBSD: mcp23xxxgpio_spi.c,v 1.3 2022/01/19 05:05:45 thorpej Exp $ */
/*-
* Copyright (c) 2014, 2022 The NetBSD Foundation, Inc.
@ -30,7 +30,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: mcp23xxxgpio_spi.c,v 1.2 2022/01/17 19:36:54 thorpej Exp $");
__KERNEL_RCSID(0, "$NetBSD: mcp23xxxgpio_spi.c,v 1.3 2022/01/19 05:05:45 thorpej Exp $");
/*
* Driver for Microchip serial I/O expanders:
@ -175,14 +175,9 @@ static const struct mcpgpio_accessops mcpgpio_spi_accessops = {
static int
mcpgpio_spi_match(device_t parent, cfdata_t cf, void *aux)
{
struct spi_attach_args *sa = aux;
/* MCP23S17 has no way to detect it! */
/* run at 10MHz */
if (spi_configure(sa->sa_handle, SPI_MODE_0, 10000000))
return 0;
return 1;
}
@ -207,6 +202,14 @@ mcpgpio_spi_attach(device_t parent, device_t self, void *aux)
aprint_naive("\n");
aprint_normal(": %s I/O Expander\n", sc->sc_variant->name);
/* run at 10MHz */
error = spi_configure(sa->sa_handle, SPI_MODE_0, 10000000);
if (error) {
aprint_error_dev(self,
"failed to set Mode 0 @ 10MHz, error=%d\n", error);
return;
}
/*
* Before we decode the topology information, ensure each
* chip has IOCON.HAEN set so that it will actually decode

View File

@ -1,4 +1,4 @@
/* $NetBSD: mcp3k.c,v 1.2 2016/11/20 12:38:04 phx Exp $ */
/* $NetBSD: mcp3k.c,v 1.3 2022/01/19 05:05:45 thorpej Exp $ */
/*-
* Copyright (c) 2015 The NetBSD Foundation, Inc.
@ -174,15 +174,10 @@ static struct mcp3kadc_model mcp3k_models[] = {
static int
mcp3kadc_match(device_t parent, cfdata_t cf, void *aux)
{
struct spi_attach_args *sa = aux;
if (strcmp(cf->cf_name, "mcp3kadc") != 0)
return 0;
/* configure for 1MHz */
if (spi_configure(sa->sa_handle, SPI_MODE_0, 1000000))
return 0;
return 1;
}
@ -193,7 +188,7 @@ mcp3kadc_attach(device_t parent, device_t self, void *aux)
struct spi_attach_args *sa;
struct mcp3kadc_softc *sc;
struct mcp3kadc_model *model;
int ch, i;
int error, ch, i;
sa = aux;
sc = device_private(self);
@ -209,6 +204,14 @@ mcp3kadc_attach(device_t parent, device_t self, void *aux)
(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,
"failed to set Mode 0 @ 1MHz, error=%d\n", error);
return;
}
/* set a default Vref in mV according to the chip's ADC resolution */
sc->sc_vref_mv = 1 << ((model->flags & M3K_SIGNED) ?
model->bits - 1 : model->bits);

View File

@ -1,4 +1,4 @@
/* $NetBSD: mcp48x1.c,v 1.1 2014/02/25 20:09:37 rkujawa Exp $ */
/* $NetBSD: mcp48x1.c,v 1.2 2022/01/19 05:05:45 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 2014/02/25 20:09:37 rkujawa Exp $");
__KERNEL_RCSID(0, "$NetBSD: mcp48x1.c,v 1.2 2022/01/19 05:05:45 thorpej Exp $");
/*
* Driver for Microchip MCP4801/MCP4811/MCP4821 DAC.
@ -115,13 +115,9 @@ static struct mcp48x1dac_model mcp48x1_models[] = {
static int
mcp48x1dac_match(device_t parent, cfdata_t cf, void *aux)
{
struct spi_attach_args *sa = aux;
/* MCP48x1 is a write-only device, so no way to detect it! */
if (spi_configure(sa->sa_handle, SPI_MODE_0, 20000000))
return 0;
return 1;
}
@ -130,7 +126,7 @@ mcp48x1dac_attach(device_t parent, device_t self, void *aux)
{
struct mcp48x1dac_softc *sc;
struct spi_attach_args *sa;
int cf_flags;
int error, cf_flags;
aprint_naive(": Digital to Analog converter\n");
aprint_normal(": MCP48x1 DAC\n");
@ -143,6 +139,13 @@ mcp48x1dac_attach(device_t parent, device_t self, void *aux)
sc->sc_dm = &mcp48x1_models[cf_flags]; /* flag value defines model */
error = spi_configure(sa->sa_handle, SPI_MODE_0, 20000000);
if (error) {
aprint_error_dev(self,
"failed to set Mode 0 @ 20MHz, error=%d\n", error);
return;
}
if(!mcp48x1dac_envsys_attach(sc)) {
aprint_error_dev(sc->sc_dev, "failed to attach envsys\n");
return;

View File

@ -1,4 +1,4 @@
/* $NetBSD: oj6sh.c,v 1.9 2021/08/07 16:19:16 thorpej Exp $ */
/* $NetBSD: oj6sh.c,v 1.10 2022/01/19 05:05:45 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.9 2021/08/07 16:19:16 thorpej Exp $");
__KERNEL_RCSID(0, "$NetBSD: oj6sh.c,v 1.10 2022/01/19 05:05:45 thorpej Exp $");
#include "opt_oj6sh.h"
@ -138,8 +138,6 @@ oj6sh_match(device_t parent, cfdata_t cf, void *aux)
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 self, void *aux)
struct oj6sh_softc *sc = device_private(self);
struct spi_attach_args *sa = aux;
struct wsmousedev_attach_args a;
int error;
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,
"failed to set Mode 0 @ 2.5MHz, error=%d\n", error);
return;
}
mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
sc->sc_dev = self;

View File

@ -1,5 +1,5 @@
/* $NetBSD: scmdspi.c,v 1.1 2021/12/07 17:39:54 brad Exp $ */
/* $NetBSD: scmdspi.c,v 1.2 2022/01/19 05:05:45 thorpej Exp $ */
/*
* Copyright (c) 2021 Brad Spencer <brad@anduin.eldar.org>
@ -18,7 +18,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: scmdspi.c,v 1.1 2021/12/07 17:39:54 brad Exp $");
__KERNEL_RCSID(0, "$NetBSD: scmdspi.c,v 1.2 2022/01/19 05:05:45 thorpej Exp $");
/*
* SPI driver for the Sparkfun Serial motor controller.
@ -174,10 +174,6 @@ scmdspi_match(device_t parent, cfdata_t match, void *aux)
printf("Trying to match\n");
}
/* configure for 1MHz and SPI mode 0 according to the data sheet */
if (spi_configure(sa->sa_handle, SPI_MODE_0, 1000000))
return 0;
return 1;
}
@ -186,6 +182,7 @@ scmdspi_attach(device_t parent, device_t self, void *aux)
{
struct scmd_sc *sc;
struct spi_attach_args *sa;
int error;
sa = aux;
sc = device_private(self);
@ -207,6 +204,14 @@ scmdspi_attach(device_t parent, device_t self, void *aux)
cv_init(&sc->sc_condvar, "scmdspicv");
cv_init(&sc->sc_cond_dying, "scmdspidc");
/* configure for 1MHz and SPI mode 0 according to the data sheet */
error = spi_configure(sa->sa_handle, SPI_MODE_0, 1000000);
if (error) {
aprint_error(": failed to set Mode 0 @ 1MHz, error=%d\n",
error);
return;
}
/* Please note that if the pins are not set up for SPI, the attachment
* will work, but it will not figure out that there are slave modules.
* It is likely required that a re-enumeration be performed after the pins

View File

@ -1,4 +1,4 @@
/* $NetBSD: ssdfb_spi.c,v 1.11 2021/08/19 17:50:18 tnn Exp $ */
/* $NetBSD: ssdfb_spi.c,v 1.12 2022/01/19 05:05:45 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.11 2021/08/19 17:50:18 tnn Exp $");
__KERNEL_RCSID(0, "$NetBSD: ssdfb_spi.c,v 1.12 2022/01/19 05:05:45 thorpej Exp $");
#include <sys/param.h>
#include <sys/device.h>
@ -102,19 +102,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
@ -124,6 +113,7 @@ ssdfb_spi_attach(device_t parent, device_t self, void *aux)
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;
@ -136,6 +126,17 @@ ssdfb_spi_attach(device_t parent, device_t self, void *aux)
else
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(": failed to set Mode 0 @ 10MHz, error=%d\n",
error);
return;
}
/*
* Note on interface modes.
*

View File

@ -1,4 +1,4 @@
/* $NetBSD: tmp121.c,v 1.5 2011/06/20 17:31:37 pgoyette Exp $ */
/* $NetBSD: tmp121.c,v 1.6 2022/01/19 05:05:45 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 2011/06/20 17:31:37 pgoyette Exp $");
__KERNEL_RCSID(0, "$NetBSD: tmp121.c,v 1.6 2022/01/19 05:05:45 thorpej Exp $");
#include <sys/param.h>
#include <sys/systm.h>
@ -71,12 +71,6 @@ CFATTACH_DECL_NEW(tmp121temp, sizeof(struct tmp121temp_softc),
static int
tmp121temp_match(device_t parent, cfdata_t cf, void *aux)
{
struct spi_attach_args *sa = aux;
/* configure for 10MHz */
if (spi_configure(sa->sa_handle, SPI_MODE_0, 1000000))
return 0;
return 1;
}
@ -85,10 +79,19 @@ tmp121temp_attach(device_t parent, device_t self, void *aux)
{
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,
"failed to set Mode 0 @ 10MHz, error=%d\n", error);
return;
}
sc->sc_sh = sa->sa_handle;
sc->sc_sme = sysmon_envsys_create();