git: ff6fe2983550 - main - driver for MAX44009 I2C illuminance sensor
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Sat, 06 Nov 2021 17:19:22 UTC
The branch main has been updated by avg: URL: https://cgit.FreeBSD.org/src/commit/?id=ff6fe2983550aeab0852e982e7fe5ac4c672c205 commit ff6fe2983550aeab0852e982e7fe5ac4c672c205 Author: Andriy Gapon <avg@FreeBSD.org> AuthorDate: 2020-08-20 08:11:31 +0000 Commit: Andriy Gapon <avg@FreeBSD.org> CommitDate: 2021-11-06 17:19:04 +0000 driver for MAX44009 I2C illuminance sensor --- 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 bf7f265e7551..f19e11ba62aa 100644 --- a/share/man/man4/Makefile +++ b/share/man/man4/Makefile @@ -292,6 +292,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 434a783ebbbe..5229484dc076 100644 --- a/sys/conf/files +++ b/sys/conf/files @@ -1848,6 +1848,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 = ®hi; + 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 = ®lo; + 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>