git: 1bd3e8ba6966 - main - ofw_iicbus: Add method for manual setting of basic OFW parameters.

From: Michal Meloun <mmel_at_FreeBSD.org>
Date: Sun, 20 Feb 2022 11:39:01 UTC
The branch main has been updated by mmel:

URL: https://cgit.FreeBSD.org/src/commit/?id=1bd3e8ba696633ccd7525030d951b58ade167814

commit 1bd3e8ba696633ccd7525030d951b58ade167814
Author:     Michal Meloun <mmel@FreeBSD.org>
AuthorDate: 2022-02-20 09:45:14 +0000
Commit:     Michal Meloun <mmel@FreeBSD.org>
CommitDate: 2022-02-20 11:25:58 +0000

    ofw_iicbus: Add method for manual setting of basic OFW parameters.
    
    Some IIC multifunction devices may have multiple I2C addresses per chip, but
    only the primary address is listed in the DT (e.g. MAX776200). In this case,
    the sub-devices for the secondary addresses must be created manually with
    fixed OFW parameters (node, name, compatibility string, IIC address).
    Add a bus method to the ofw_iicbus interface that does this.
    
    MFC after:      4 weeks
---
 sys/conf/files                 |  1 +
 sys/dev/iicbus/ofw_iicbus.c    | 29 ++++++++++++++++++++++++++++
 sys/dev/iicbus/ofw_iicbus_if.m | 43 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 73 insertions(+)

diff --git a/sys/conf/files b/sys/conf/files
index f002598d1c3d..fb57567d3e87 100644
--- a/sys/conf/files
+++ b/sys/conf/files
@@ -1854,6 +1854,7 @@ dev/iicbus/mux/ltc430x.c	optional ltc430x
 dev/iicbus/mux/pca954x.c	optional pca954x
 dev/iicbus/nxprtc.c		optional nxprtc | pcf8563
 dev/iicbus/ofw_iicbus.c		optional fdt iicbus
+dev/iicbus/ofw_iicbus_if.m	optional fdt iicbus
 dev/iicbus/pcf8574.c		optional pcf8574
 dev/iicbus/pcf8591.c		optional pcf8591
 dev/iicbus/rtc8583.c		optional rtc8583
diff --git a/sys/dev/iicbus/ofw_iicbus.c b/sys/dev/iicbus/ofw_iicbus.c
index 9be05d73abde..2ccc9698e5bd 100644
--- a/sys/dev/iicbus/ofw_iicbus.c
+++ b/sys/dev/iicbus/ofw_iicbus.c
@@ -42,6 +42,7 @@ __FBSDID("$FreeBSD$");
 #include <dev/ofw/openfirm.h>
 
 #include "iicbus_if.h"
+#include "ofw_iicbus_if.h"
 
 /* Methods */
 static device_probe_t ofw_iicbus_probe;
@@ -50,6 +51,8 @@ static device_t ofw_iicbus_add_child(device_t dev, u_int order,
     const char *name, int unit);
 static const struct ofw_bus_devinfo *ofw_iicbus_get_devinfo(device_t bus,
     device_t dev);
+static int ofw_iicbus_set_devinfo(device_t bus, device_t dev,
+    phandle_t ofw_node, char *ofw_name, char *ofw_compat, int i2c_addr);
 
 static device_method_t ofw_iicbus_methods[] = {
 	/* Device interface */
@@ -68,6 +71,9 @@ static device_method_t ofw_iicbus_methods[] = {
 	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
 	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
 
+	/* ofw_iicbus interface */
+	DEVMETHOD(ofw_iicbus_set_devinfo, ofw_iicbus_set_devinfo),
+
 	DEVMETHOD_END
 };
 
@@ -238,3 +244,26 @@ ofw_iicbus_get_devinfo(device_t bus, device_t dev)
 	dinfo = device_get_ivars(dev);
 	return (&dinfo->opd_obdinfo);
 }
+
+static int
+ofw_iicbus_set_devinfo(device_t bus, device_t dev, phandle_t ofw_node,
+    char *ofw_name, char *ofw_compat, int i2c_addr)
+{
+	struct ofw_iicbus_devinfo *devi;
+
+	/*
+	 * Setup OFW-related parts of the ivars for manually
+	 * created ofw_iicbus childern.
+	 */
+	devi = device_get_ivars(dev);
+	if (devi == NULL)
+		return (ENXIO);
+
+	devi->opd_obdinfo.obd_node = ofw_node;
+	if (ofw_name != NULL)
+		devi->opd_obdinfo.obd_name = strdup(ofw_name, M_OFWPROP);
+	if (ofw_compat != NULL)
+		devi->opd_obdinfo.obd_compat = strdup(ofw_compat, M_OFWPROP);
+	devi->opd_dinfo.addr = i2c_addr;
+	return (0);
+}
diff --git a/sys/dev/iicbus/ofw_iicbus_if.m b/sys/dev/iicbus/ofw_iicbus_if.m
new file mode 100644
index 000000000000..a2b0bd2761db
--- /dev/null
+++ b/sys/dev/iicbus/ofw_iicbus_if.m
@@ -0,0 +1,43 @@
+#-
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright 2020 Michal Meloun <mmel@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/bus.h>
+#include <dev/ofw/openfirm.h>
+
+INTERFACE ofw_iicbus;
+
+#
+# Interpret interrupt
+#
+METHOD int set_devinfo {
+	device_t bus;
+	device_t dev;
+	phandle_t ofw_node;
+	char *ofw_name;
+	char *ofw_compat;
+	int i2c_addr;
+};