Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 17 Jan 2020 17:57:34 +0000 (UTC)
From:      Ian Lepore <ian@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org
Subject:   svn commit: r356838 - in stable/12: share/man/man4 sys/conf sys/dev/flash sys/dev/iicbus sys/dev/iicbus/mux sys/modules/i2c sys/modules/i2c/mux
Message-ID:  <202001171757.00HHvYAe085312@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: ian
Date: Fri Jan 17 17:57:34 2020
New Revision: 356838
URL: https://svnweb.freebsd.org/changeset/base/356838

Log:
  MFC r356086, r356278, r356294, r356519, r356521-r356522, r356525-r356526
  
  r356086:
  Add comments to a couple i2c device lines in NOTES.
  
  r356278:
  Add support for i2c bus mux hardware.
  
  An i2c bus can be divided into segments which can be selectively connected
  and disconnected from the main bus. This is usually done to enable using
  multiple slave devices having the same address, by isolating the devices
  onto separate bus segments, only one of which is connected to the main bus
  at once.
  
  There are several types of i2c bus muxes, which break down into two general
  categories...
  
   - Muxes which are themselves i2c slaves. These devices respond to i2c
     commands on their upstream bus, and based on those commands, connect
     various downstream buses to the upstream. In newbus terms, they are both
     a child of an iicbus and the parent of one or more iicbus instances.
   - Muxes which are not i2c devices themselves. Such devices are part of the
     i2c bus electrically, but in newbus terms their parent is some other
     bus. The association with the upstream bus must be established by
     separate metadata (such as FDT data).
  
  In both cases, the mux driver has one or more iicbus child instances
  representing the downstream buses. The mux driver implements the iicbus_if
  interface, as if it were an iichb host bridge/i2c controller driver. It
  services the IO requests sent to it by forwarding them to the iicbus
  instance representing the upstream bus, after electrically connecting the
  upstream bus to the downstream bus that hosts the i2c slave device which
  made the IO request.
  
  The net effect is automatic mux switching which is transparent to slaves on
  the downstream buses. They just do i2c IO they way they normally do, and the
  bus is electrically connected for the duration of the IO and then idled when
  it is complete.
  
  The existing iicbus_if callback() method is enhanced so that the parameter
  passed to it can be a struct which contains a device_t for the requesting
  bus and slave devices. This change is done by adding a flag that indicates
  the extra values are present, and making the flags field the first field of
  a new args struct. If the flag is set, the iichb or mux driver can recast
  the pointer-to-flags into a pointer-to-struct and access the extra
  fields. Thus abi compatibility with older drivers is retained (but a mux
  cannot exist on the bus with the older iicbus driver in use.)
  
  A new set of core support routines exists in iicbus.c. This code will help
  implement mux drivers for any type of mux hardware by supplying all the
  boilerplate code that forwards IO requests upstream. It also has code for
  parsing metadata and instantiating the child iicbus instances based on it.
  
  Two new hardware mux drivers are added. The ltc430x driver supports the
  LTC4305/4306 mux chips which are controlled via i2c commands. The
  iic_gpiomux driver supports any mux hardware which is controlled by
  manipulating the state of one or more gpio pins.  Test Plan
  
  Tested locally using a variety of mux'd bus configurations involving both
  ltc4305 and a homebrew gpio-controlled mux. Tested configurations included
  cascaded muxes (unlikely in the real world, but useful to prove that 'it all
  just works' in terms of the automatic switching and upstream forwarding of
  IO requests).
  
  r356294:
  Explicitly include sys/rman.h instead of relying on getting it via some
  other header.
  
  r356519:
  Ensure any reserved gpio pins get released if an early exit is taken
  from the attach function.
  
  r356521:
  Init sc->maxbus to -1, not 0.  It represents the highest array index that
  has a non-NULL child bus stored in it, so the "none" value can't be zero
  since that's a valid array index.  Also, when adding all possible buses
  because there is no specific per-bus config, there's no need to reset
  sc->maxbus on each loop iteration, it can be set once after the loop.
  
  r356522:
  Change some KASSERT to device_printf + return EINVAL.  There's no need to
  bring the whole kernel down due to a configuration error detected when a
  module is loaded, it suffices to just not attach the device.
  
  r356525:
  Split the code to find and add iicbus children out to its own function.
  Move the decision to take an early exit from that function after adding
  children based on FDT data into the #ifdef FDT block, so that it doesn't
  offend coverity's notion of how the code should be written.  (What's the
  point of compilers optimizing away dead code if static analyzers won't
  let you use the feature in conjuction with an #ifdef block?)
  
  Reported by:	coverity via vangyzen@
  
  r356526:
  Remove some trailing whitespace; no functional changes.

Added:
  stable/12/share/man/man4/iic_gpiomux.4
     - copied unchanged from r356278, head/share/man/man4/iic_gpiomux.4
  stable/12/share/man/man4/iicmux.4
     - copied unchanged from r356278, head/share/man/man4/iicmux.4
  stable/12/share/man/man4/ltc430x.4
     - copied unchanged from r356278, head/share/man/man4/ltc430x.4
  stable/12/sys/dev/iicbus/mux/
     - copied from r356278, head/sys/dev/iicbus/mux/
  stable/12/sys/modules/i2c/mux/
     - copied from r356278, head/sys/modules/i2c/mux/
Modified:
  stable/12/share/man/man4/Makefile
  stable/12/sys/conf/NOTES
  stable/12/sys/conf/files
  stable/12/sys/dev/flash/cqspi.c
  stable/12/sys/dev/iicbus/iiconf.c
  stable/12/sys/dev/iicbus/iiconf.h
  stable/12/sys/dev/iicbus/mux/iic_gpiomux.c
  stable/12/sys/dev/iicbus/mux/iicmux.c
  stable/12/sys/modules/i2c/Makefile
Directory Properties:
  stable/12/   (props changed)

Modified: stable/12/share/man/man4/Makefile
==============================================================================
--- stable/12/share/man/man4/Makefile	Fri Jan 17 17:56:31 2020	(r356837)
+++ stable/12/share/man/man4/Makefile	Fri Jan 17 17:57:34 2020	(r356838)
@@ -212,8 +212,10 @@ MAN=	aac.4 \
 	ig4.4 \
 	igmp.4 \
 	iic.4 \
+	iic_gpiomux.4 \
 	iicbb.4 \
 	iicbus.4 \
+	iicmux.4 \
 	iicsmb.4 \
 	iir.4 \
 	${_imcsmb.4} \
@@ -267,6 +269,7 @@ MAN=	aac.4 \
 	lp.4 \
 	lpbb.4 \
 	lpt.4 \
+	ltc430x.4 \
 	mac.4 \
 	mac_biba.4 \
 	mac_bsdextended.4 \

Copied: stable/12/share/man/man4/iic_gpiomux.4 (from r356278, head/share/man/man4/iic_gpiomux.4)
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ stable/12/share/man/man4/iic_gpiomux.4	Fri Jan 17 17:57:34 2020	(r356838, copy of r356278, head/share/man/man4/iic_gpiomux.4)
@@ -0,0 +1,88 @@
+.\"-
+.\" SPDX-License-Identifier: BSD-2-Clause
+.\"
+.\" Copyright (c) 2019 Ian Lepore <ian@freebsd.org>
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.\" $FreeBSD$
+.\"
+.Dd January 1, 2020
+.Dt IIC_GPIOMUX 4
+.Os
+.Sh NAME
+.Nm iic_gpiomux
+.Nd driver for I2C mux hardware controlled via GPIO
+.Sh SYNOPSIS
+To compile this driver into the kernel,
+place the following line in your
+kernel configuration file:
+.Bd -ragged -offset indent
+.Cd "device iic_gpiomux"
+.Ed
+.Pp
+Alternatively, to load the driver as a
+module at boot time, place the following line in
+.Xr loader.conf 5 :
+.Bd -literal -offset indent
+iic_gpiomux_load="YES"
+.Ed
+.Sh DESCRIPTION
+The
+.Nm
+driver supports any type of I2C bus multiplexer (mux) hardware that
+is controlled by manipulating the state of one or more GPIO pins.
+It automatically connects an upstream I2C bus to one of the downstream
+buses as needed when slave devices on the downstream buses initiate I/O.
+More information on the automatic switching behavior is available in
+.Xr iicmux 4 .
+.Pp
+.Sh FDT CONFIGURATION
+On an
+.Xr fdt 4
+based system, an
+.Nm
+device node may be defined as a child node of any arbitrary bus
+in the FDT data.
+The
+.Va i2c-parent
+property indicates the connection to the upstream I2C bus.
+The children of the
+.Nm
+node are additional i2c buses, which will have their own i2c slave
+devices described in their child nodes.
+.Pp
+The
+.Nm
+driver conforms to the standard
+.Bk -words
+.Li i2c/i2c-mux-gpio.txt
+.Ek
+bindings document.
+.Sh SEE ALSO
+.Xr iicbus 4 ,
+.Xr iicmux 4 ,
+.Sh HISTORY
+The
+.Nm
+driver first appeared in
+.Fx 13.0 .

Copied: stable/12/share/man/man4/iicmux.4 (from r356278, head/share/man/man4/iicmux.4)
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ stable/12/share/man/man4/iicmux.4	Fri Jan 17 17:57:34 2020	(r356838, copy of r356278, head/share/man/man4/iicmux.4)
@@ -0,0 +1,148 @@
+.\"-
+.\" SPDX-License-Identifier: BSD-2-Clause
+.\"
+.\" Copyright (c) 2019 Ian Lepore <ian@freebsd.org>
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.\" $FreeBSD$
+.\"
+.Dd January 1, 2020
+.Dt IICMUX 4
+.Os
+.Sh NAME
+.Nm iicmux
+.Nd I2C bus mulitiplexer framework
+.Sh SYNOPSIS
+To compile this driver into the kernel,
+place the following line in your
+kernel configuration file:
+.Bd -ragged -offset indent
+.Cd "device iicmux"
+.Ed
+.Pp
+Alternatively, to load the driver as a
+module at boot time, place the following line in
+.Xr loader.conf 5 :
+.Bd -literal -offset indent
+iicmux_load="YES"
+.Ed
+.Pp
+Note that it is usually not necessary to explicitly load the
+driver module, as it will be loaded automatically along with
+the driver for the specific mux hardware in use.
+.Sh DESCRIPTION
+The
+.Nm
+framework provides support code to help implement drivers for various
+I2C bus multiplexer (mux) hardware.
+.Nm
+is not a standalone driver,
+it is a collection of support functions and driver methods which are
+used by individual mux hardware drivers.
+It will be loaded automatically when needed by a mux hardware driver.
+This manual page provides an overview of the I2C mux framework and its
+behavior.
+.Pp
+Generally speaking, an I2C mux is connected to an upstream I2C bus, and to
+one or more downstream I2C buses, and it can be commanded to connect
+any one of the downstream buses to the upstream bus.
+Some hardware may be able to connect multiple downstream buses at the
+same time, but that concept is not supported by
+.Nm .
+.Pp
+The
+.Nm
+framework operates automatically when I2C slave devices initiate I/O.
+It does not require (or even allow for) any external control to select
+the active downstream bus.
+.Pp
+When there is no I/O in progress, the mux is said to be in the
+.Dq idle
+state.
+Some mux hardware has the ability to disconnect all downstream buses
+when in an idle state.
+Other hardware must always have one of the downstream buses connected.
+Individual mux hardware drivers typically provide a way to select which
+downstream bus (if any) should be connected while in the idle state.
+In the absence of such configuration, whichever downstream bus was
+last used remains connected to the upstream bus.
+.Pp
+When an I2C slave device on a bus downstream of a mux initiates I/O,
+it first requests exclusive use of the bus by calling
+.Fn iicbus_request_bus .
+This request is communicated to the bus's parent, which is the
+.Nm
+framework
+mux driver.
+Once exclusive bus ownership is obtained, the mux driver
+connects the upstream I2C bus to the downstream bus which hosts the
+slave device that requested bus ownership.
+The mux hardware maintains that upstream-to-downstream connection until
+the slave device calls
+.Fn iicbus_release_bus .
+Before releasing ownership, the mux driver returns the mux hardware to
+the idle state.
+.Sh FDT CONFIGURATION
+On an
+.Xr fdt 4
+based system, an I2C mux device node is defined as a child node of its
+upstream I2C bus when the mux device is an I2C slave itself.
+It may be defined as a child node of any other bus or device in the
+system when it is not an I2C slave, in which case the
+.Va i2c-parent
+property indicates which upstream bus the mux is attached to.
+In either case, the children of the mux node are additional I2C buses, which
+will have one or more I2C slave devices described in their child nodes.
+.Pp
+Drivers using the
+.Nm
+framework conform to the standard
+.Bk -words
+.Li i2c/i2c-mux.txt
+.Ek
+bindings document.
+.Sh HINTS CONFIGURATION
+On a
+.Xr device.hints 5
+based system, these values are configurable for
+.Nm
+framework drivers :
+.Bl -tag -width indent
+.It Va hint.<driver>.<unit>.at
+The upstream
+.Xr iicbus 4
+the
+.Nm
+instance is attached to.
+.El
+.Pp
+When configured via hints, the driver automatically adds an iicbus
+instance for every downstream bus supported by the chip.
+There is currently no way to indicate used versus unused downstream buses.
+.Sh SEE ALSO
+.Xr iicbus 4 ,
+.Sh HISTORY
+The
+.Nm
+framework first appeared in
+.Fx 13.0 .

Copied: stable/12/share/man/man4/ltc430x.4 (from r356278, head/share/man/man4/ltc430x.4)
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ stable/12/share/man/man4/ltc430x.4	Fri Jan 17 17:57:34 2020	(r356838, copy of r356278, head/share/man/man4/ltc430x.4)
@@ -0,0 +1,112 @@
+.\"-
+.\" SPDX-License-Identifier: BSD-2-Clause
+.\"
+.\" Copyright (c) 2019 Ian Lepore <ian@freebsd.org>
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.\" $FreeBSD$
+.\"
+.Dd January 1, 2020
+.Dt LTC430X 4
+.Os
+.Sh NAME
+.Nm ltc430x
+.Nd driver for LTC4305 and LTC4306 I2C mux chips
+.Sh SYNOPSIS
+To compile this driver into the kernel,
+place the following line in your
+kernel configuration file:
+.Bd -ragged -offset indent
+.Cd "device ltc430x"
+.Ed
+.Pp
+Alternatively, to load the driver as a
+module at boot time, place the following line in
+.Xr loader.conf 5 :
+.Bd -literal -offset indent
+ltc430x_load="YES"
+.Ed
+.Sh DESCRIPTION
+The
+.Nm
+driver supports the LTC4305 and LTC4306 I2C bus multiplexer (mux) chips.
+It automatically connects an upstream I2C bus to one of several downstream
+buses as needed when slave devices on the downstream buses initiate I/O.
+More information on the automatic switching behavior is available in
+.Xr iicmux 4 .
+.Sh FDT CONFIGURATION
+On an
+.Xr fdt 4
+based system, an
+.Nm
+device node is defined as a child node of its upstream i2c bus.
+The children of the
+.Nm
+node are additional i2c buses, which will have their own i2c slave
+devices described in their child nodes.
+.Pp
+The
+.Nm
+driver conforms to the standard
+.Bk -words
+.Li i2c/i2c-mux-ltc4306.txt
+.Ek
+bindings document, except that the following optional properties
+are not currently supported and will be ignored if present:
+.Bl -bullet -compact -inset -offset indent
+.It
+enable-gpios
+.It
+gpio-controller
+.It
+#gpio-cells
+.It
+ltc,downstream-accelerators-enable
+.It
+ltc,upstream-accelerators-enable
+.El
+.Sh HINTS CONFIGURATION
+On a
+.Xr device.hints 5
+based system, these values are configurable for
+.Nm :
+.Bl -tag -width indent
+.It Va hint.ltc430x.<unit>.at
+The upstream
+.Xr iicbus 4
+the
+.Nm
+instance is attached to.
+.El
+.Pp
+When configured via hints, the driver automatically adds an iicbus
+instance for every downstream bus supported by the chip.
+There is currently no way to indicate used versus unused channels.
+.Sh SEE ALSO
+.Xr iicbus 4 ,
+.Xr iicmux 4 ,
+.Sh HISTORY
+The
+.Nm
+driver first appeared in
+.Fx 13.0 .

Modified: stable/12/sys/conf/NOTES
==============================================================================
--- stable/12/sys/conf/NOTES	Fri Jan 17 17:56:31 2020	(r356837)
+++ stable/12/sys/conf/NOTES	Fri Jan 17 17:57:34 2020	(r356838)
@@ -2515,12 +2515,17 @@ device		jedec_dimm
 # iicbb	generic I2C bit-banging code (needed by lpbb, bktr)
 #
 device		iicbus		# Bus support, required for ic/iic/iicsmb below.
-device		iicbb
+device		iicbb		# bitbang driver; implements i2c on a pair of gpio pins
 
 device		ic
-device		iic
+device		iic		# userland access to i2c slave devices via ioctl(8)
 device		iicsmb		# smb over i2c bridge
 device		iicoc		# OpenCores I2C controller support
+
+# I2C bus multiplexer (mux) devices
+device		iicmux		# i2c mux core driver
+device		iic_gpiomux	# i2c mux hardware controlled via gpio pins
+device		ltc430x		# LTC4305 and LTC4306 i2c mux chips
 
 # I2C peripheral devices
 #

Modified: stable/12/sys/conf/files
==============================================================================
--- stable/12/sys/conf/files	Fri Jan 17 17:56:31 2020	(r356837)
+++ stable/12/sys/conf/files	Fri Jan 17 17:57:34 2020	(r356838)
@@ -1864,6 +1864,10 @@ dev/iicbus/iicsmb.c		optional iicsmb				\
 dev/iicbus/iicoc.c		optional iicoc
 dev/iicbus/isl12xx.c		optional isl12xx
 dev/iicbus/lm75.c		optional lm75
+dev/iicbus/mux/iicmux.c		optional iicmux
+dev/iicbus/mux/iicmux_if.m	optional iicmux
+dev/iicbus/mux/iic_gpiomux.c	optional iic_gpiomux fdt
+dev/iicbus/mux/ltc430x.c	optional ltc430x
 dev/iicbus/nxprtc.c		optional nxprtc | pcf8563
 dev/iicbus/ofw_iicbus.c		optional fdt iicbus
 dev/iicbus/rtc8583.c		optional rtc8583

Modified: stable/12/sys/dev/flash/cqspi.c
==============================================================================
--- stable/12/sys/dev/flash/cqspi.c	Fri Jan 17 17:56:31 2020	(r356837)
+++ stable/12/sys/dev/flash/cqspi.c	Fri Jan 17 17:57:34 2020	(r356838)
@@ -50,6 +50,7 @@ __FBSDID("$FreeBSD$");
 #include <sys/malloc.h>
 #include <sys/module.h>
 #include <sys/mutex.h>
+#include <sys/rman.h>
 #include <geom/geom_disk.h>
 
 #include <machine/bus.h>

Modified: stable/12/sys/dev/iicbus/iiconf.c
==============================================================================
--- stable/12/sys/dev/iicbus/iiconf.c	Fri Jan 17 17:56:31 2020	(r356837)
+++ stable/12/sys/dev/iicbus/iiconf.c	Fri Jan 17 17:57:34 2020	(r356838)
@@ -137,6 +137,7 @@ iicbus_poll(struct iicbus_softc *sc, int how)
 int
 iicbus_request_bus(device_t bus, device_t dev, int how)
 {
+	struct iic_reqbus_data reqdata;
 	struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
 	int error = 0;
 
@@ -175,8 +176,11 @@ iicbus_request_bus(device_t bus, device_t dev, int how
 			 */
 			IICBUS_UNLOCK(sc);
 			/* Ask the underlying layers if the request is ok */
+			reqdata.dev = dev;
+			reqdata.bus = bus;
+			reqdata.flags = how | IIC_REQBUS_DEV;
 			error = IICBUS_CALLBACK(device_get_parent(bus),
-			    IIC_REQUEST_BUS, (caddr_t)&how);
+			    IIC_REQUEST_BUS, (caddr_t)&reqdata);
 			IICBUS_LOCK(sc);
 	
 			if (error != 0) {
@@ -201,6 +205,7 @@ iicbus_request_bus(device_t bus, device_t dev, int how
 int
 iicbus_release_bus(device_t bus, device_t dev)
 {
+	struct iic_reqbus_data reqdata;
 	struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
 
 	IICBUS_LOCK(sc);
@@ -213,7 +218,11 @@ iicbus_release_bus(device_t bus, device_t dev)
 	if (--sc->owncount == 0) {
 		/* Drop the lock while informing the low-level driver. */
 		IICBUS_UNLOCK(sc);
-		IICBUS_CALLBACK(device_get_parent(bus), IIC_RELEASE_BUS, NULL);
+		reqdata.dev = dev;
+		reqdata.bus = bus;
+		reqdata.flags = IIC_REQBUS_DEV;
+		IICBUS_CALLBACK(device_get_parent(bus), IIC_RELEASE_BUS,
+		    (caddr_t)&reqdata);
 		IICBUS_LOCK(sc);
 		sc->owner = NULL;
 		wakeup_one(sc);

Modified: stable/12/sys/dev/iicbus/iiconf.h
==============================================================================
--- stable/12/sys/dev/iicbus/iiconf.h	Fri Jan 17 17:56:31 2020	(r356837)
+++ stable/12/sys/dev/iicbus/iiconf.h	Fri Jan 17 17:57:34 2020	(r356838)
@@ -47,6 +47,25 @@
 #define IIC_INTR	0x2
 #define IIC_INTRWAIT	(IIC_INTR | IIC_WAIT)
 #define IIC_RECURSIVE	0x4
+#define IIC_REQBUS_DEV	0x8	/* See struct iic_reqbus_data, below. */
+
+/*
+ * The original iicbus->bridge callback api took a pointer to an int containing
+ * flags.  The new api allows a pointer to this struct, with IIC_REQBUS_DEV set
+ * in the flags to let the implementation know the pointer is actually to this
+ * struct which has the flags word first, followed by the device_t of the
+ * requesting bus and device.
+ *
+ * Note that the requesting device may not be a i2c slave device which is a
+ * child of the requested bus -- it may be a mux device which is electrically
+ * part of the bus hierarchy, but whose driver belongs to some other bus
+ * hierarchy such as gpio.
+ */
+struct iic_reqbus_data {
+	int      flags;      /* Flags from the set defined above. */
+	device_t bus;        /* The iicbus being requested. */
+	device_t dev;        /* The device requesting the bus. */
+};
 
 /*
  * i2c modes

Modified: stable/12/sys/dev/iicbus/mux/iic_gpiomux.c
==============================================================================
--- head/sys/dev/iicbus/mux/iic_gpiomux.c	Thu Jan  2 17:51:49 2020	(r356278)
+++ stable/12/sys/dev/iicbus/mux/iic_gpiomux.c	Fri Jan 17 17:57:34 2020	(r356838)
@@ -119,6 +119,15 @@ gpiomux_probe(device_t dev)
 	return (rv);
 }
 
+static void
+gpiomux_release_pins(struct gpiomux_softc *sc)
+{
+	int i;
+
+	for (i = 0; i < sc->numpins; ++i)
+		gpio_pin_release(sc->pins[i]);
+}
+
 static int
 gpiomux_attach(device_t dev)
 {
@@ -145,13 +154,16 @@ gpiomux_attach(device_t dev)
 	sc->numpins = i;
 	if (sc->numpins == 0) {
 		device_printf(dev, "cannot acquire pins listed in mux-gpios\n");
-		return ((err == 0) ? ENXIO : err);
+		if (err == 0)
+			err = ENXIO;
+		goto errexit;
 	}
 	numchannels = 1u << sc->numpins;
 	if (numchannels > IICMUX_MAX_BUSES) {
 		device_printf(dev, "too many mux-gpios pins for max %d buses\n",
 		    IICMUX_MAX_BUSES);
-		return (EINVAL);
+		err = EINVAL;
+		goto errexit;
 	}
 
 	/*
@@ -163,13 +175,15 @@ gpiomux_attach(device_t dev)
 	len = OF_getencprop(node, "i2c-parent", &propval, sizeof(propval));
 	if (len != sizeof(propval)) {
 		device_printf(dev, "cannot obtain i2c-parent property\n");
-		return (ENXIO);
+		err = ENXIO;
+		goto errexit;
 	}
 	busdev = OF_device_from_xref((phandle_t)propval);
 	if (busdev == NULL) {
 		device_printf(dev,
 		    "cannot find device referenced by i2c-parent property\n");
-		return (ENXIO);
+		err = ENXIO;
+		goto errexit;
 	}
 	device_printf(dev, "upstream bus is %s\n", device_get_nameunit(busdev));
 
@@ -202,6 +216,11 @@ gpiomux_attach(device_t dev)
 	if ((err = iicmux_attach(dev, busdev, numchannels)) == 0)
 		bus_generic_attach(dev);
 
+errexit:
+
+	if (err != 0)
+		gpiomux_release_pins(sc);
+
 	return (err);
 }
 
@@ -209,13 +228,12 @@ static int
 gpiomux_detach(device_t dev)
 {
 	struct gpiomux_softc *sc = device_get_softc(dev);
-	int err, i;
+	int err;
 
 	if ((err = iicmux_detach(dev)) != 0)
 		return (err);
 
-	for (i = 0; i < sc->numpins; ++i)
-		gpio_pin_release(sc->pins[i]);
+	gpiomux_release_pins(sc);
 
 	return (0);
 }

Modified: stable/12/sys/dev/iicbus/mux/iicmux.c
==============================================================================
--- head/sys/dev/iicbus/mux/iicmux.c	Thu Jan  2 17:51:49 2020	(r356278)
+++ stable/12/sys/dev/iicbus/mux/iicmux.c	Fri Jan 17 17:57:34 2020	(r356838)
@@ -153,7 +153,7 @@ iicmux_intr(device_t dev, int event, char *buf)
 	/* XXX iicbus_intr() in iiconf.c should return status. */
 
 	iicbus_intr(sc->busdev, event, buf);
-	return (0); 
+	return (0);
 }
 
 static int
@@ -213,7 +213,7 @@ iicmux_write(device_t dev, const char *buf, int len, i
 }
 
 /*------------------------------------------------------------------------------
- * iicmux helper functions, called by hardware-specific drivers.                
+ * iicmux helper functions, called by hardware-specific drivers.
  * All these functions return a standard errno value.
  *----------------------------------------------------------------------------*/
 
@@ -222,10 +222,16 @@ iicmux_add_child(device_t dev, device_t child, int bus
 {
 	struct iicmux_softc *sc = device_get_softc(dev);
 
-	KASSERT(busidx < sc->numbuses,
-	    ("iicmux_add_child: bus idx %d too big", busidx));
-	KASSERT(sc->childdevs[busidx] == NULL,
-	    ("iicmux_add_child: bus idx %d already added", busidx));
+	if (busidx >= sc->numbuses) {
+		device_printf(dev,
+		    "iicmux_add_child: bus idx %d too big", busidx);
+		return (EINVAL);
+	}
+	if (sc->childdevs[busidx] != NULL) {
+		device_printf(dev, "iicmux_add_child: bus idx %d already added",
+		    busidx);
+		return (EINVAL);
+	}
 
 	sc->childdevs[busidx] = child;
 	if (sc->maxbus < busidx)
@@ -234,32 +240,10 @@ iicmux_add_child(device_t dev, device_t child, int bus
 	return (0);
 }
 
-int
-iicmux_attach(device_t dev, device_t busdev, int numbuses)
+static int
+iicmux_attach_children(struct iicmux_softc *sc)
 {
-	struct iicmux_softc *sc = device_get_softc(dev);
-	int i, numadded;
-
-        /*
-         * Init the softc...
-         */
-	KASSERT(numbuses <= IICMUX_MAX_BUSES,
-		("iicmux_attach: numbuses %d exceeds max %d\n",
-		numbuses, IICMUX_MAX_BUSES));
-
-	sc->dev = dev;
-	sc->busdev = busdev;
-	sc->numbuses = numbuses;
-
-	SYSCTL_ADD_UINT(device_get_sysctl_ctx(sc->dev), 
-	    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), OID_AUTO,
-	    "debugmux", CTLFLAG_RWTUN, &sc->debugmux, 0, "debug mux operations");
-
-        /*
-         * Add children...
-         */
-	numadded = 0;
-
+	int i;
 #ifdef FDT
 	phandle_t child, node, parent;
 	pcell_t idx;
@@ -286,12 +270,12 @@ iicmux_attach(device_t dev, device_t busdev, int numbu
 	 */
 	for (child = OF_child(parent); child != 0; child = OF_peer(child)) {
 		if (OF_getencprop(child, "reg", &idx, sizeof(idx)) == -1) {
-			device_printf(dev,
+			device_printf(sc->dev,
 			    "child bus missing required 'reg' property\n");
 			continue;
 		}
 		if (idx >= sc->numbuses) {
-			device_printf(dev,
+			device_printf(sc->dev,
 			    "child bus 'reg' property %d exceeds the number "
 			    "of buses supported by the device (%d)\n",
 			    idx, sc->numbuses);
@@ -301,22 +285,48 @@ iicmux_attach(device_t dev, device_t busdev, int numbu
 		sc->childnodes[idx] = child;
 		if (sc->maxbus < idx)
 			sc->maxbus = idx;
-		++numadded;
 	}
+
+	/* If we configured anything using FDT data, we're done. */
+	if (sc->maxbus >= 0)
+		return (0);
 #endif /* FDT */
 
 	/*
-	 * If we configured anything using FDT data, we're done.  Otherwise add
-	 * an iicbus child for every downstream bus supported by the mux chip.
+	 * If we make it to here, we didn't add any children based on FDT data.
+	 * Add an iicbus child for every downstream bus supported by the mux.
 	 */
-	if (numadded > 0)
-		return (0);
-
 	for (i = 0; i < sc->numbuses; ++i) {
 		sc->childdevs[i] = device_add_child(sc->dev, "iicbus", -1);
-		if (sc->maxbus < i)
-			sc->maxbus = i;
+		sc->maxbus = i;
 	}
+
+	return (0);
+}
+
+int
+iicmux_attach(device_t dev, device_t busdev, int numbuses)
+{
+	struct iicmux_softc *sc = device_get_softc(dev);
+	int err;
+
+	if (numbuses >= IICMUX_MAX_BUSES) {
+		device_printf(dev, "iicmux_attach: numbuses %d > max %d\n",
+		    numbuses, IICMUX_MAX_BUSES);
+		return (EINVAL);
+	}
+
+	sc->dev = dev;
+	sc->busdev = busdev;
+	sc->maxbus = -1;
+	sc->numbuses = numbuses;
+
+	if ((err = iicmux_attach_children(sc)) != 0)
+		return (err);
+
+	SYSCTL_ADD_UINT(device_get_sysctl_ctx(sc->dev),
+	    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), OID_AUTO,
+	    "debugmux", CTLFLAG_RWTUN, &sc->debugmux, 0, "debug mux operations");
 
 	return (0);
 }

Modified: stable/12/sys/modules/i2c/Makefile
==============================================================================
--- stable/12/sys/modules/i2c/Makefile	Fri Jan 17 17:56:31 2020	(r356837)
+++ stable/12/sys/modules/i2c/Makefile	Fri Jan 17 17:57:34 2020	(r356838)
@@ -17,6 +17,7 @@ SUBDIR = \
 	isl \
 	isl12xx \
 	jedec_dimm \
+	mux \
 	nxprtc \
 	rtc8583 \
 	s35390a \



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202001171757.00HHvYAe085312>