From nobody Sat Nov 13 09:12:18 2021
X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1])
	by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 7FEAF18597B3;
	Sat, 13 Nov 2021 09:12:18 +0000 (UTC)
	(envelope-from git@FreeBSD.org)
Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256
	 client-signature RSA-PSS (4096 bits) client-digest SHA256)
	(Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK))
	by mx1.freebsd.org (Postfix) with ESMTPS id 4HrqTt34hMz3vkf;
	Sat, 13 Nov 2021 09:12:18 +0000 (UTC)
	(envelope-from git@FreeBSD.org)
Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256)
	(Client did not present a certificate)
	by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4A8EC10CA8;
	Sat, 13 Nov 2021 09:12:18 +0000 (UTC)
	(envelope-from git@FreeBSD.org)
Received: from gitrepo.freebsd.org ([127.0.1.44])
	by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 1AD9CIsv078369;
	Sat, 13 Nov 2021 09:12:18 GMT
	(envelope-from git@gitrepo.freebsd.org)
Received: (from git@localhost)
	by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 1AD9CIAw078368;
	Sat, 13 Nov 2021 09:12:18 GMT
	(envelope-from git)
Date: Sat, 13 Nov 2021 09:12:18 GMT
Message-Id: <202111130912.1AD9CIAw078368@gitrepo.freebsd.org>
To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org,
        dev-commits-src-branches@FreeBSD.org
From: Andriy Gapon <avg@FreeBSD.org>
Subject: git: 8f57430de139 - stable/13 - driver for MAX44009 I2C illuminance sensor
List-Id: Commits to the stable branches of the FreeBSD src repository <dev-commits-src-branches.freebsd.org>
List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches
List-Help: <mailto:dev-commits-src-branches+help@freebsd.org>
List-Post: <mailto:dev-commits-src-branches@freebsd.org>
List-Subscribe: <mailto:dev-commits-src-branches+subscribe@freebsd.org>
List-Unsubscribe: <mailto:dev-commits-src-branches+unsubscribe@freebsd.org>
Sender: owner-dev-commits-src-branches@freebsd.org
X-BeenThere: dev-commits-src-branches@freebsd.org
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
X-Git-Committer: avg
X-Git-Repository: src
X-Git-Refname: refs/heads/stable/13
X-Git-Reftype: branch
X-Git-Commit: 8f57430de13961726e106d6aab5fa56ff877df03
Auto-Submitted: auto-generated
X-ThisMailContainsUnwantedMimeParts: N

The branch stable/13 has been updated by avg:

URL: https://cgit.FreeBSD.org/src/commit/?id=8f57430de13961726e106d6aab5fa56ff877df03

commit 8f57430de13961726e106d6aab5fa56ff877df03
Author:     Andriy Gapon <avg@FreeBSD.org>
AuthorDate: 2020-08-20 08:11:31 +0000
Commit:     Andriy Gapon <avg@FreeBSD.org>
CommitDate: 2021-11-13 09:12:05 +0000

    driver for MAX44009 I2C illuminance sensor
    
    (cherry picked from commit ff6fe2983550aeab0852e982e7fe5ac4c672c205)
---
 share/man/man4/Makefile           |   1 +
 share/man/man4/max44009.4         |  92 ++++++++++++++++++
 sys/conf/files                    |   1 +
 sys/dev/iicbus/max44009.c         | 193 ++++++++++++++++++++++++++++++++++++++
 sys/modules/i2c/Makefile          |   1 +
 sys/modules/i2c/max44009/Makefile |  17 ++++
 6 files changed, 305 insertions(+)

diff --git a/share/man/man4/Makefile b/share/man/man4/Makefile
index e80b451f20b2..f5aaa2b616a6 100644
--- a/share/man/man4/Makefile
+++ b/share/man/man4/Makefile
@@ -293,6 +293,7 @@ MAN=	aac.4 \
 	mac_stub.4 \
 	mac_test.4 \
 	malo.4 \
+	max44009.4 \
 	md.4 \
 	mdio.4 \
 	me.4 \
diff --git a/share/man/man4/max44009.4 b/share/man/man4/max44009.4
new file mode 100644
index 000000000000..fa399c52cec3
--- /dev/null
+++ b/share/man/man4/max44009.4
@@ -0,0 +1,92 @@
+.\"
+.\" SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+.\"
+.\" Copyright (c) 2020 Andriy Gapon <avg@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 November 6, 2021
+.Dt MAX44009 4
+.Os
+.Sh NAME
+.Nm max44009
+.Nd driver for MAX44009 Ambient Light Sensor
+.Sh SYNOPSIS
+To compile this driver into the kernel,
+place the following lines in your
+kernel configuration file:
+.Bd -ragged -offset indent
+.Cd "device max44009"
+.Cd "device iicbus"
+.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
+max44009_load="YES"
+.Ed
+.Sh DESCRIPTION
+The
+.Nm
+driver reports illuminance data from MAX44009 Ambient Light Sensor.
+.Pp
+The
+.Nm
+driver reports data via
+.Xr sysctl 8
+entry in the device's node in the
+.Xr sysctl 8
+tree:
+.Bl -tag -width illuminance
+.It Va illuminance
+The illuminance, in lux units.
+.El
+.Pp
+On an
+.Xr FDT 4
+based system the following properties must be set:
+.Bl -tag -width "compatible"
+.It Va compatible
+Must be set to
+.Qq maxim,max44009 .
+.It Va reg
+The I2C address of
+.Nm
+which can be either 0x4a or 0x4b.
+.El
+.Sh SEE ALSO
+.Xr fdt 4 ,
+.Xr iicbus 4 ,
+.Xr sysctl 8
+.Sh HISTORY
+The
+.Nm
+driver and this manual page was written by
+.An Andriy Gapon Aq Mt avg@FreeBSD.org .
+.Sh BUGS
+The
+.Nm
+driver does not support illuminance thresholds and the corresponding
+interrupt.
diff --git a/sys/conf/files b/sys/conf/files
index 92e68a0b103e..a9f67bcf46f7 100644
--- a/sys/conf/files
+++ b/sys/conf/files
@@ -1878,6 +1878,7 @@ dev/iicbus/iicoc_fdt.c		optional iicoc ext_resources fdt
 dev/iicbus/iicoc_pci.c		optional iicoc pci
 dev/iicbus/isl12xx.c		optional isl12xx
 dev/iicbus/lm75.c		optional lm75
+dev/iicbus/max44009.c		optional max44009
 dev/iicbus/mux/pca9547.c	optional pca9547 iicmux fdt
 dev/iicbus/mux/iicmux.c		optional iicmux
 dev/iicbus/mux/iicmux_if.m	optional iicmux
diff --git a/sys/dev/iicbus/max44009.c b/sys/dev/iicbus/max44009.c
new file mode 100644
index 000000000000..739b43ed9055
--- /dev/null
+++ b/sys/dev/iicbus/max44009.c
@@ -0,0 +1,193 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) Andriy Gapon
+ *
+ * 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>
+#include <sys/systm.h>
+
+#include <machine/bus.h>
+
+#include <dev/iicbus/iicbus.h>
+#include <dev/iicbus/iiconf.h>
+
+#ifdef FDT
+#include <dev/ofw/ofw_bus.h>
+#include <dev/ofw/ofw_bus_subr.h>
+#endif
+
+/*
+ * Driver for MAX44009 Ambient Light Sensor with ADC.
+ */
+#define	REG_LUX_HIGH			0x03
+#define	REG_LUX_LOW			0x04
+
+struct max44009_softc {
+	device_t		sc_dev;
+	uint8_t			sc_addr;
+};
+
+#ifdef FDT
+static const struct ofw_compat_data compat_data[] = {
+        { "maxim,max44009",		true },
+	{ NULL,				false },
+};
+#endif
+
+static int
+max44009_get_reading(device_t dev, u_int *reading)
+{
+	struct iic_msg msgs[4];
+	struct max44009_softc *sc;
+	u_int val;
+	uint8_t reghi, reglo, valhi, vallo;
+	int error;
+
+	sc = device_get_softc(dev);
+
+	reghi = REG_LUX_HIGH;
+	reglo = REG_LUX_LOW;
+	msgs[0].slave = sc->sc_addr;
+	msgs[0].flags = IIC_M_WR | IIC_M_NOSTOP;
+	msgs[0].len = 1;
+	msgs[0].buf = &reghi;
+	msgs[1].slave = sc->sc_addr;
+	msgs[1].flags = IIC_M_RD | IIC_M_NOSTOP;
+	msgs[1].len = 1;
+	msgs[1].buf = &valhi;
+	msgs[2].slave = sc->sc_addr;
+	msgs[2].flags = IIC_M_WR | IIC_M_NOSTOP;
+	msgs[2].len = 1;
+	msgs[2].buf = &reglo;
+	msgs[3].slave = sc->sc_addr;
+	msgs[3].flags = IIC_M_RD;
+	msgs[3].len = 1;
+	msgs[3].buf = &vallo;
+
+	error = iicbus_transfer_excl(dev, msgs, nitems(msgs), IIC_INTRWAIT);
+	if (error != 0)
+		return (error);
+
+	val = ((valhi & 0x0f) << 4) | (vallo & 0x0f);
+	val <<= (valhi & 0xf0) >> 4;
+	val = val * 72 / 100;
+	*reading = val;
+	return (0);
+}
+
+static int
+max44009_lux_sysctl(SYSCTL_HANDLER_ARGS)
+{
+	device_t dev;
+	u_int reading;
+	int error, val;
+
+	if (req->oldptr != NULL) {
+		dev = arg1;
+		error = max44009_get_reading(dev, &reading);
+		if (error != 0)
+			return (EIO);
+		val = reading;
+	}
+	error = sysctl_handle_int(oidp, &val, 0, req);
+	return (error);
+}
+
+static int
+max44009_probe(device_t dev)
+{
+	int rc;
+
+#ifdef FDT
+	if (!ofw_bus_status_okay(dev))
+		return (ENXIO);
+	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data)
+		rc = BUS_PROBE_GENERIC;
+	else
+#endif
+		rc = BUS_PROBE_NOWILDCARD;
+	device_set_desc(dev, "MAX44009 light intensity sensor");
+	return (rc);
+}
+
+static int
+max44009_attach(device_t dev)
+{
+	struct max44009_softc *sc;
+	struct sysctl_ctx_list *ctx;
+	struct sysctl_oid *tree_node;
+	struct sysctl_oid_list *tree;
+
+	sc = device_get_softc(dev);
+	sc->sc_dev = dev;
+	sc->sc_addr = iicbus_get_addr(dev);
+
+	ctx = device_get_sysctl_ctx(dev);
+	tree_node = device_get_sysctl_tree(dev);
+	tree = SYSCTL_CHILDREN(tree_node);
+
+	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "illuminance",
+	    CTLTYPE_INT | CTLFLAG_RD, dev, 0,
+	    max44009_lux_sysctl, "I", "Light intensity, lux");
+	return (0);
+}
+
+static int
+max44009_detach(device_t dev)
+{
+	return (0);
+}
+
+static device_method_t  max44009_methods[] = {
+	/* Device interface */
+	DEVMETHOD(device_probe,		max44009_probe),
+	DEVMETHOD(device_attach,	max44009_attach),
+	DEVMETHOD(device_detach,	max44009_detach),
+
+	DEVMETHOD_END
+};
+
+static driver_t max44009_driver = {
+	"max44009",
+	max44009_methods,
+	sizeof(struct max44009_softc)
+};
+
+static devclass_t max44009_devclass;
+
+DRIVER_MODULE(max44009, iicbus, max44009_driver, max44009_devclass, 0, 0);
+MODULE_DEPEND(max44009, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
+MODULE_VERSION(max44009, 1);
+IICBUS_FDT_PNP_INFO(compat_data);
diff --git a/sys/modules/i2c/Makefile b/sys/modules/i2c/Makefile
index b79e2a98fc7d..483eb012bb95 100644
--- a/sys/modules/i2c/Makefile
+++ b/sys/modules/i2c/Makefile
@@ -18,6 +18,7 @@ SUBDIR = \
 	isl \
 	isl12xx \
 	jedec_dimm \
+	max44009 \
 	mux \
 	nxprtc \
 	pcf8591 \
diff --git a/sys/modules/i2c/max44009/Makefile b/sys/modules/i2c/max44009/Makefile
new file mode 100644
index 000000000000..1e201c5aaba4
--- /dev/null
+++ b/sys/modules/i2c/max44009/Makefile
@@ -0,0 +1,17 @@
+# $FreeBSD$
+
+.PATH:	${SRCTOP}/sys/dev/iicbus
+KMOD=	max44009
+SRCS=	max44009.c
+
+SRCS+=	\
+	bus_if.h \
+	device_if.h \
+	iicbus_if.h \
+	opt_platform.h
+
+.if !empty(OPT_FDT)
+SRCS+=	ofw_bus_if.h
+.endif
+
+.include <bsd.kmod.mk>