svn commit: r356278 - in head: share/man/man4 sys/conf sys/dev/iicbus sys/dev/iicbus/mux sys/modules/i2c sys/modules/i2c/mux sys/modules/i2c/mux/iic_gpiomux sys/modules/i2c/mux/iicmux sys/modules/i...

Ian Lepore ian at FreeBSD.org
Thu Jan 2 17:51:52 UTC 2020


Author: ian
Date: Thu Jan  2 17:51:49 2020
New Revision: 356278
URL: https://svnweb.freebsd.org/changeset/base/356278

Log:
  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).

Added:
  head/share/man/man4/iic_gpiomux.4   (contents, props changed)
  head/share/man/man4/iicmux.4   (contents, props changed)
  head/share/man/man4/ltc430x.4   (contents, props changed)
  head/sys/dev/iicbus/mux/
  head/sys/dev/iicbus/mux/iic_gpiomux.c   (contents, props changed)
  head/sys/dev/iicbus/mux/iicmux.c   (contents, props changed)
  head/sys/dev/iicbus/mux/iicmux.h   (contents, props changed)
  head/sys/dev/iicbus/mux/iicmux_if.m   (contents, props changed)
  head/sys/dev/iicbus/mux/ltc430x.c   (contents, props changed)
  head/sys/modules/i2c/mux/
  head/sys/modules/i2c/mux/Makefile   (contents, props changed)
  head/sys/modules/i2c/mux/iic_gpiomux/
  head/sys/modules/i2c/mux/iic_gpiomux/Makefile   (contents, props changed)
  head/sys/modules/i2c/mux/iicmux/
  head/sys/modules/i2c/mux/iicmux/Makefile   (contents, props changed)
  head/sys/modules/i2c/mux/ltc430x/
  head/sys/modules/i2c/mux/ltc430x/Makefile   (contents, props changed)
Modified:
  head/share/man/man4/Makefile
  head/sys/conf/NOTES
  head/sys/conf/files
  head/sys/dev/iicbus/iiconf.c
  head/sys/dev/iicbus/iiconf.h
  head/sys/modules/i2c/Makefile

Modified: head/share/man/man4/Makefile
==============================================================================
--- head/share/man/man4/Makefile	Thu Jan  2 17:44:41 2020	(r356277)
+++ head/share/man/man4/Makefile	Thu Jan  2 17:51:49 2020	(r356278)
@@ -204,8 +204,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} \
@@ -258,6 +260,7 @@ MAN=	aac.4 \
 	lp.4 \
 	lpbb.4 \
 	lpt.4 \
+	ltc430x.4 \
 	mac.4 \
 	mac_biba.4 \
 	mac_bsdextended.4 \

Added: head/share/man/man4/iic_gpiomux.4
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/share/man/man4/iic_gpiomux.4	Thu Jan  2 17:51:49 2020	(r356278)
@@ -0,0 +1,88 @@
+.\"-
+.\" SPDX-License-Identifier: BSD-2-Clause
+.\"
+.\" Copyright (c) 2019 Ian Lepore <ian at 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 .

Added: head/share/man/man4/iicmux.4
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/share/man/man4/iicmux.4	Thu Jan  2 17:51:49 2020	(r356278)
@@ -0,0 +1,148 @@
+.\"-
+.\" SPDX-License-Identifier: BSD-2-Clause
+.\"
+.\" Copyright (c) 2019 Ian Lepore <ian at 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 .

Added: head/share/man/man4/ltc430x.4
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/share/man/man4/ltc430x.4	Thu Jan  2 17:51:49 2020	(r356278)
@@ -0,0 +1,112 @@
+.\"-
+.\" SPDX-License-Identifier: BSD-2-Clause
+.\"
+.\" Copyright (c) 2019 Ian Lepore <ian at 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: head/sys/conf/NOTES
==============================================================================
--- head/sys/conf/NOTES	Thu Jan  2 17:44:41 2020	(r356277)
+++ head/sys/conf/NOTES	Thu Jan  2 17:51:49 2020	(r356278)
@@ -2329,6 +2329,11 @@ device		iic		# userland access to i2c slave devices vi
 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
 #
 device		ad7418		# Analog Devices temp and voltage sensor

Modified: head/sys/conf/files
==============================================================================
--- head/sys/conf/files	Thu Jan  2 17:44:41 2020	(r356277)
+++ head/sys/conf/files	Thu Jan  2 17:51:49 2020	(r356278)
@@ -1801,6 +1801,10 @@ dev/iicbus/iicoc_fdt.c		optional iicoc fdt
 dev/iicbus/iicoc_pci.c		optional iicoc pci
 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: head/sys/dev/iicbus/iiconf.c
==============================================================================
--- head/sys/dev/iicbus/iiconf.c	Thu Jan  2 17:44:41 2020	(r356277)
+++ head/sys/dev/iicbus/iiconf.c	Thu Jan  2 17:51:49 2020	(r356278)
@@ -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: head/sys/dev/iicbus/iiconf.h
==============================================================================
--- head/sys/dev/iicbus/iiconf.h	Thu Jan  2 17:44:41 2020	(r356277)
+++ head/sys/dev/iicbus/iiconf.h	Thu Jan  2 17:51:49 2020	(r356278)
@@ -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

Added: head/sys/dev/iicbus/mux/iic_gpiomux.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/sys/dev/iicbus/mux/iic_gpiomux.c	Thu Jan  2 17:51:49 2020	(r356278)
@@ -0,0 +1,249 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2019 Ian Lepore <ian at 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.
+ */
+
+/*
+ * Driver for i2c bus muxes controlled by one or more gpio pins.
+ *
+ * This driver has #ifdef FDT sections in it, as if it supports both fdt and
+ * hinted attachment, but there is currently no support for hinted attachment.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include "opt_platform.h"
+
+#include <sys/param.h>
+#include <sys/bus.h>
+#include <sys/gpio.h>
+#include <sys/kernel.h>
+#include <sys/module.h>
+#include <sys/systm.h>
+
+#include <dev/gpio/gpiobusvar.h>
+
+#include <dev/iicbus/iicbus.h>
+#include <dev/iicbus/mux/iicmux.h>
+
+#ifdef FDT
+#include <dev/ofw/ofw_bus.h>
+#include <dev/ofw/ofw_bus_subr.h>
+#include <dev/ofw/openfirm.h>
+
+static struct ofw_compat_data compat_data[] = {
+	{"i2c-mux-gpio",  true},
+	{NULL,            false}
+};
+OFWBUS_PNP_INFO(compat_data);
+SIMPLEBUS_PNP_INFO(compat_data);
+#endif /* FDT */
+
+#include <dev/iicbus/iiconf.h>
+#include "iicmux.h"
+#include "iicmux_if.h"
+
+struct gpiomux_softc {
+	struct iicmux_softc mux;
+	int	idleidx;
+	int	numpins;
+	gpio_pin_t pins[IICMUX_MAX_BUSES];
+};
+
+#define IDLE_NOOP	(-1) /* When asked to idle the bus, do nothing. */
+
+static int
+gpiomux_bus_select(device_t dev, int busidx, struct iic_reqbus_data *rd)
+{
+	struct gpiomux_softc *sc = device_get_softc(dev);
+	int i;
+
+	/*
+	 * The iicmux caller ensures busidx is between 0 and the number of buses
+	 * we passed to iicmux_init_softc(), no need for validation here.  The
+	 * bits in the index number are transcribed to the state of the pins,
+	 * except when we're asked to idle the bus.  In that case, we transcribe
+	 * sc->idleidx to the pins, unless that is IDLE_NOOP (leave the current
+	 * bus selected), in which case we just bail.
+	 */
+	if (busidx == IICMUX_SELECT_IDLE) {
+		if (sc->idleidx == IDLE_NOOP)
+			return (0);
+		busidx = sc->idleidx;
+	}
+
+	for (i = 0; i < sc->numpins; ++i)
+		gpio_pin_set_active(sc->pins[i], busidx & (1u << i));
+
+	return (0);
+}
+
+static int
+gpiomux_probe(device_t dev)
+{
+	int rv;
+
+	rv = ENXIO;
+
+#ifdef FDT
+	if (ofw_bus_status_okay(dev) &&
+	    ofw_bus_search_compatible(dev, compat_data)->ocd_data)
+		rv = BUS_PROBE_DEFAULT;
+#endif
+
+	device_set_desc(dev, "I2C GPIO Mux");
+
+	return (rv);
+}
+
+static int
+gpiomux_attach(device_t dev)
+{
+	struct gpiomux_softc *sc = device_get_softc(dev);
+	ssize_t len;
+	device_t busdev;
+	int err, i, idlebits, numchannels;
+	pcell_t propval;
+	phandle_t node;
+
+	node = ofw_bus_get_node(dev);
+
+	/*
+	 * Locate the gpio pin(s) that control the mux hardware.  There can be
+	 * multiple pins, but there must be at least one.
+	 */
+	for (i = 0; ; ++i) {
+		err = gpio_pin_get_by_ofw_propidx(dev, node, "mux-gpios", i,
+		    &sc->pins[i]);
+		if (err != 0) {
+			break;
+		}
+	}
+	sc->numpins = i;
+	if (sc->numpins == 0) {
+		device_printf(dev, "cannot acquire pins listed in mux-gpios\n");
+		return ((err == 0) ? ENXIO : err);
+	}
+	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);
+	}
+
+	/*
+	 * We don't have a parent/child relationship to the upstream bus, we
+	 * have to locate it via the i2c-parent property.  Explicitly tell the
+	 * user which upstream we're associated with, since the normal attach
+	 * message is going to mention only our actual parent.
+	 */
+	len = OF_getencprop(node, "i2c-parent", &propval, sizeof(propval));
+	if (len != sizeof(propval)) {
+		device_printf(dev, "cannot obtain i2c-parent property\n");
+		return (ENXIO);
+	}
+	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);
+	}
+	device_printf(dev, "upstream bus is %s\n", device_get_nameunit(busdev));
+
+	/*
+	 * If there is an idle-state property, that is the value we set the pins
+	 * to when the bus is idle, otherwise idling the bus is a no-op
+	 * (whichever bus was last accessed remains active).
+	 */
+	len = OF_getencprop(node, "idle-state", &propval, sizeof(propval));
+	if (len == sizeof(propval)) {
+		if ((int)propval >= numchannels) {
+			device_printf(dev,
+			    "idle-state property %d exceeds channel count\n",
+			    propval);
+		}
+		sc->idleidx = (int)propval;
+		idlebits = sc->idleidx;
+	} else {
+		sc->idleidx = IDLE_NOOP;
+		idlebits = 0;
+	}
+
+	/* Preset the mux to the idle state to get things started. */
+	for (i = 0; i < sc->numpins; ++i) {
+		gpio_pin_setflags(sc->pins[i], GPIO_PIN_OUTPUT);
+		gpio_pin_set_active(sc->pins[i], idlebits & (1u << i));
+	}
+
+	/* Init the core driver, have it add our child downstream buses. */
+	if ((err = iicmux_attach(dev, busdev, numchannels)) == 0)
+		bus_generic_attach(dev);
+
+	return (err);
+}
+
+static int
+gpiomux_detach(device_t dev)
+{
+	struct gpiomux_softc *sc = device_get_softc(dev);
+	int err, i;
+
+	if ((err = iicmux_detach(dev)) != 0)
+		return (err);
+
+	for (i = 0; i < sc->numpins; ++i)
+		gpio_pin_release(sc->pins[i]);
+
+	return (0);
+}
+
+static device_method_t gpiomux_methods[] = {
+	/* device methods */
+	DEVMETHOD(device_probe,			gpiomux_probe),
+	DEVMETHOD(device_attach,		gpiomux_attach),
+	DEVMETHOD(device_detach,		gpiomux_detach),
+
+	/* iicmux methods */
+	DEVMETHOD(iicmux_bus_select,		gpiomux_bus_select),
+
+	DEVMETHOD_END
+};
+
+static devclass_t gpiomux_devclass;
+
+DEFINE_CLASS_1(iic_gpiomux, iic_gpiomux_driver, gpiomux_methods,
+    sizeof(struct gpiomux_softc), iicmux_driver);
+DRIVER_MODULE(iic_gpiomux, simplebus, iic_gpiomux_driver, gpiomux_devclass, 0, 0);
+DRIVER_MODULE(iic_gpiomux, ofw_simplebus, iic_gpiomux_driver, gpiomux_devclass, 0, 0);
+
+#ifdef FDT
+DRIVER_MODULE(ofw_iicbus, iic_gpiomux, ofw_iicbus_driver, ofw_iicbus_devclass, 0, 0);
+#else
+DRIVER_MODULE(iicbus, iic_gpiomux, iicbus_driver, iicbus_devclass, 0, 0);
+#endif
+
+MODULE_DEPEND(iic_gpiomux, iicmux, 1, 1, 1);
+MODULE_DEPEND(iic_gpiomux, iicbus, 1, 1, 1);

Added: head/sys/dev/iicbus/mux/iicmux.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/sys/dev/iicbus/mux/iicmux.c	Thu Jan  2 17:51:49 2020	(r356278)
@@ -0,0 +1,387 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2019 Ian Lepore <ian at 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.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include "opt_platform.h"
+
+#include <sys/param.h>
+#include <sys/bus.h>
+#include <sys/kernel.h>
+#include <sys/module.h>
+#include <sys/sysctl.h>
+
+#ifdef FDT
+#include <dev/ofw/ofw_bus.h>
+#include <dev/ofw/ofw_bus_subr.h>
+#include <dev/ofw/openfirm.h>
+#endif
+
+#include <dev/iicbus/iiconf.h>
+#include "iicbus_if.h"
+#include "iicmux_if.h"
+#include "iicmux.h"
+
+/*------------------------------------------------------------------------------
+ * iicbus methods, called by the iicbus functions in iiconf.c.
+ *
+ * All these functions return an IIC adapter-layer error code (because we are
+ * pretending to be a host bridge/i2c controller).  Standard errno values
+ * returned from these must be encoded using iic2errno().
+ *----------------------------------------------------------------------------*/
+
+static int
+iicmux_callback(device_t dev, int index, caddr_t data)
+{
+	struct iicmux_softc *sc = device_get_softc(dev);
+	struct iic_reqbus_data *rd;
+	int err, i;
+
+	/* If it's not one of the operations we know about, bail early. */
+	if (index != IIC_REQUEST_BUS && index != IIC_RELEASE_BUS)
+		return (iic2errno(EOPNOTSUPP));
+
+	/*
+	 * Ensure that the data passed to us includes the device_t of the child
+	 * bus and device.  If missing, someone bypassed iicbus_request_bus()
+	 * and called this method directly using the old calling standard.  If
+	 * present, find the index of the child bus that called us.
+	 */
+	rd = (struct iic_reqbus_data *)data;
+	if (!(rd->flags & IIC_REQBUS_DEV))
+		return (iic2errno(EINVAL));
+
+	for (i = 0; i <= sc->maxbus && sc->childdevs[i] != rd->bus; ++i)
+		continue;
+	if (i > sc->maxbus)
+		return (iic2errno(ENOENT));
+
+	/*
+	 * If the operation is a release it "cannot fail".  Idle the downstream
+	 * bus, then release exclusive use of the upstream bus, and we're done.
+	 */
+	if (index == IIC_RELEASE_BUS) {
+		if (sc->debugmux > 0) {
+			device_printf(dev, "idle the bus for %s on bus %s\n",
+			    device_get_nameunit(rd->dev),
+			    device_get_nameunit(rd->bus));
+		}
+		IICMUX_BUS_SELECT(dev, IICMUX_SELECT_IDLE, rd);
+		iicbus_release_bus(sc->busdev, dev);
+		return (IIC_NOERR);
+	}
+
+	if (sc->debugmux > 0) {
+		device_printf(dev, "select bus idx %d for %s on bus %s\n", i,
+		    device_get_nameunit(rd->dev), device_get_nameunit(rd->bus));
+	}
+
+	/*
+	 * The operation is a request for exclusive use.  First we have to
+	 * request exclusive use of our upstream bus.  If multiple slave devices
+	 * from our different child buses attempt to do IO at the same time,
+	 * this is what ensures that they don't switch the bus out from under
+	 * each other. The first one in proceeds and others wait here (or get an
+	 * EWOULDBLOCK return if they're using IIC_DONTWAIT).
+	 */
+	if ((err = iicbus_request_bus(sc->busdev, dev, rd->flags)) != 0)
+		return (err); /* Already an IIC error code. */
+
+	/*
+	 * Now that we own exclusive use of the upstream bus, connect it to the
+	 * downstream bus where the request came from.
+	 */
+	if ((err = IICMUX_BUS_SELECT(dev, i, rd)) != 0)
+		iicbus_release_bus(sc->busdev, dev);
+
+	return (err);
+}
+
+static u_int
+iicmux_get_frequency(device_t dev, u_char speed)
+{
+	struct iicmux_softc *sc = device_get_softc(dev);
+
+	return (IICBUS_GET_FREQUENCY(sc->busdev, speed));
+}
+
+#ifdef FDT
+static phandle_t
+iicmux_get_node(device_t dev, device_t child)
+{
+	struct iicmux_softc *sc = device_get_softc(dev);
+	int i;
+
+	for (i = 0; i <= sc->maxbus; ++i) {
+		if (sc->childdevs[i] == child)
+			return (sc->childnodes[i]);
+	}
+	return (0); /* null handle */
+}
+#endif
+
+static int
+iicmux_intr(device_t dev, int event, char *buf)
+{
+	struct iicmux_softc *sc = device_get_softc(dev);
+
+	/* XXX iicbus_intr() in iiconf.c should return status. */
+
+	iicbus_intr(sc->busdev, event, buf);
+	return (0); 
+}
+
+static int
+iicmux_read(device_t dev, char *buf, int len, int *bytes, int last, int delay)
+{
+	struct iicmux_softc *sc = device_get_softc(dev);
+
+	return (iicbus_read(sc->busdev, buf, len, bytes, last, delay));
+}
+
+static int
+iicmux_repeated_start(device_t dev, u_char slave, int timeout)
+{
+	struct iicmux_softc *sc = device_get_softc(dev);
+
+	return (iicbus_repeated_start(sc->busdev, slave, timeout));
+}
+
+static int
+iicmux_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
+{
+	struct iicmux_softc *sc = device_get_softc(dev);
+
+	return (iicbus_reset(sc->busdev, speed, addr, oldaddr));
+}
+
+static int
+iicmux_start(device_t dev, u_char slave, int timeout)
+{
+	struct iicmux_softc *sc = device_get_softc(dev);
+
+	return (iicbus_start(sc->busdev, slave, timeout));
+}
+
+static int
+iicmux_stop(device_t dev)
+{
+	struct iicmux_softc *sc = device_get_softc(dev);
+
+	return (iicbus_stop(sc->busdev));
+}
+
+static int
+iicmux_transfer( device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
+{
+	struct iicmux_softc *sc = device_get_softc(dev);
+
+	return (iicbus_transfer(sc->busdev, msgs, nmsgs));
+}
+
+static int
+iicmux_write(device_t dev, const char *buf, int len, int *bytes, int timeout)
+{
+	struct iicmux_softc *sc = device_get_softc(dev);
+
+	return (iicbus_write(sc->busdev, buf, len, bytes, timeout));
+}
+
+/*------------------------------------------------------------------------------
+ * iicmux helper functions, called by hardware-specific drivers.                
+ * All these functions return a standard errno value.
+ *----------------------------------------------------------------------------*/
+
+int
+iicmux_add_child(device_t dev, device_t child, int busidx)
+{
+	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));
+
+	sc->childdevs[busidx] = child;
+	if (sc->maxbus < busidx)
+		sc->maxbus = busidx;
+
+	return (0);
+}
+
+int
+iicmux_attach(device_t dev, device_t busdev, int numbuses)
+{
+	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;
+
+#ifdef FDT
+	phandle_t child, node, parent;
+	pcell_t idx;
+
+	/*
+	 * Find our FDT node.  Child nodes within our node will become our

*** DIFF OUTPUT TRUNCATED AT 1000 LINES ***


More information about the svn-src-head mailing list