From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from metis.whiteo.stw.pengutronix.de (metis.whiteo.stw.pengutronix.de [185.203.201.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0F9584B8DF6 for ; Fri, 15 May 2026 16:05:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.201.7 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778861161; cv=none; b=uNfXNReqIjJwcJYdkwE7fjS5PVkkddHNgAL6zeLN7zXMIejhhGcU2Ayqy+yoj1qQK6Q0f+0+sl0+O0Y9mGL/00drr7QW68dlCTDlZ6ctIPiCYjaIrtMGRqYZJJ04xZYWFwb2TY4Fun7wOMhWoEPhRHLbGfxCM+ysN/qRCjY1Erg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778861161; c=relaxed/simple; bh=RPAaHTRPl8V3JmhjoT27j/U3hayKrychHGmuxLnwTFc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KOhCnNCeZiaBItqSx84EfNLei2AGg5b5SarA7W8C0EFLe3bJv/r7Y8Sg4YlzXR0b1DF6d42Xa0k/NVMUBwVDtbXcY5sF5W7CkkpPY8zjIm4IXf/LTIxOo2YhWeLSCGm4KTiMzHYoJMfZYb0N3FcL66oQCZW1K4QaXRpEtznADOM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.201.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.whiteo.stw.pengutronix.de with esmtp (Exim 4.92) (envelope-from ) id 1wNv2h-00023d-2h; Fri, 15 May 2026 18:05:39 +0200 Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac] helo=dude04) by drehscheibe.grey.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wNv2g-000wnK-2A; Fri, 15 May 2026 18:05:38 +0200 Received: from ore by dude04 with local (Exim 4.98.2) (envelope-from ) id 1wNv2g-00000000UA1-2VOS; Fri, 15 May 2026 18:05:38 +0200 From: Oleksij Rempel To: Guenter Roeck , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Lee Jones , Peter Rosin , Linus Walleij Cc: Oleksij Rempel , kernel@pengutronix.de, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-hwmon@vger.kernel.org, linux-gpio@vger.kernel.org, David Jander Subject: [PATCH v12 6/6] mux: add NXP MC33978/MC34978 AMUX driver Date: Fri, 15 May 2026 18:05:34 +0200 Message-ID: <20260515160537.115808-7-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260515160537.115808-1-o.rempel@pengutronix.de> References: <20260515160537.115808-1-o.rempel@pengutronix.de> Precedence: bulk X-Mailing-List: devicetree@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: ore@pengutronix.de X-SA-Exim-Scanned: No (on metis.whiteo.stw.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: devicetree@vger.kernel.org Add a mux-control driver for the 24-to-1 analog multiplexer (AMUX) embedded in the NXP MC33978/MC34978 Multiple Switch Detection Interface (MSDI) devices. Assisted-by: Gemini:gemini-3.1-pro Sashiko Signed-off-by: Oleksij Rempel --- changes v12: - Convert platform_device_id to named initializers (`.name =`) https://lore.kernel.org/all/cover.1778582187.git.u.kleine-koenig@baylibre.com changes v11: - no changes changes v10: - no changes changes v9: - rename mc33978-mux to mux-mc33978 in the Kconfig help - fail if fwnode is NULL changes v8: - no changes changes v7: - Simplify the return path and local variable assignment in mc33978_mux_set(). - Change idle_state to a signed integer to properly handle negative MUX subsystem constants. - Default to MUX_IDLE_AS_IS when the "idle-state" device tree property is missing. - Explicitly reject MUX_IDLE_DISCONNECT since the hardware does not support disconnecting the multiplexer. changes v6: - parse optional idle-state property - validate idle-state against available AMUX channels - lower-case probe error messages changes v5: - no changes changes v4: - no changes changes v3: - no changes changes v2: - Add missing include. - Add platform_device_id table --- drivers/mux/Kconfig | 14 ++++ drivers/mux/Makefile | 2 + drivers/mux/mc33978-mux.c | 141 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 157 insertions(+) create mode 100644 drivers/mux/mc33978-mux.c diff --git a/drivers/mux/Kconfig b/drivers/mux/Kconfig index 6d17dfa25dad..51510a74e632 100644 --- a/drivers/mux/Kconfig +++ b/drivers/mux/Kconfig @@ -56,6 +56,20 @@ config MUX_GPIO To compile the driver as a module, choose M here: the module will be called mux-gpio. +config MUX_MC33978 + tristate "NXP MC33978/MC34978 Analog Multiplexer" + depends on MFD_MC33978 + help + MC33978/MC34978 24-to-1 analog multiplexer (AMUX) driver. + + This driver provides mux-control for the analog multiplexer, + which can route switch voltages, temperature, and battery voltage + to an external ADC. Typically used with IIO ADC drivers to measure + analog values from the 22 switch inputs plus temperature and VBATP. + + To compile the driver as a module, choose M here: the module will + be called mux-mc33978. + config MUX_MMIO tristate "MMIO/Regmap register bitfield-controlled Multiplexer" depends on OF diff --git a/drivers/mux/Makefile b/drivers/mux/Makefile index 6e9fa47daf56..339c44b4d4f4 100644 --- a/drivers/mux/Makefile +++ b/drivers/mux/Makefile @@ -7,10 +7,12 @@ mux-core-objs := core.o mux-adg792a-objs := adg792a.o mux-adgs1408-objs := adgs1408.o mux-gpio-objs := gpio.o +mux-mc33978-objs := mc33978-mux.o mux-mmio-objs := mmio.o obj-$(CONFIG_MULTIPLEXER) += mux-core.o obj-$(CONFIG_MUX_ADG792A) += mux-adg792a.o obj-$(CONFIG_MUX_ADGS1408) += mux-adgs1408.o obj-$(CONFIG_MUX_GPIO) += mux-gpio.o +obj-$(CONFIG_MUX_MC33978) += mux-mc33978.o obj-$(CONFIG_MUX_MMIO) += mux-mmio.o diff --git a/drivers/mux/mc33978-mux.c b/drivers/mux/mc33978-mux.c new file mode 100644 index 000000000000..9034d7c3778d --- /dev/null +++ b/drivers/mux/mc33978-mux.c @@ -0,0 +1,141 @@ +// SPDX-License-Identifier: GPL-2.0-only +// Copyright (c) 2026 Pengutronix, Oleksij Rempel +/* + * MC33978/MC34978 Analog Multiplexer (AMUX) Driver + * + * This driver provides mux-control for the 24-to-1 analog multiplexer. + * The AMUX routes one of the following signals to the external AMUX pin: + * - Channels 0-13: SG0-SG13 switch voltages + * - Channels 14-21: SP0-SP7 switch voltages + * - Channel 22: Internal temperature diode + * - Channel 23: Battery voltage (VBATP) + * + * Consumer drivers (typically IIO ADC drivers) use the mux-control + * subsystem to select which signal to measure. + * + * Architecture: + * The MC33978 does not have an internal ADC. Instead, it routes analog + * signals to an external AMUX pin that must be connected to an external + * ADC (such as the SoC's internal ADC). The IIO subsystem is responsible + * for coordinating the mux selection and ADC sampling. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +/* AMUX_CTRL register field definitions */ +#define MC33978_AMUX_CTRL_MASK GENMASK(5, 0) /* 6-bit channel select */ + +struct mc33978_mux_priv { + struct device *dev; + struct regmap *map; +}; + +static int mc33978_mux_set(struct mux_control *mux, int state) +{ + struct mux_chip *mux_chip = mux->chip; + struct mc33978_mux_priv *priv = mux_chip_priv(mux_chip); + int ret; + + if (state < 0 || state >= MC33978_NUM_AMUX_CH) + return -EINVAL; + + ret = regmap_update_bits(priv->map, MC33978_REG_AMUX_CTRL, + MC33978_AMUX_CTRL_MASK, state); + if (ret) + dev_err(priv->dev, "failed to set AMUX channel %d: %d\n", + state, ret); + + return ret; +} + +static const struct mux_control_ops mc33978_mux_ops = { + .set = mc33978_mux_set, +}; + +static int mc33978_mux_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct mc33978_mux_priv *priv; + struct fwnode_handle *fwnode; + struct mux_chip *mux_chip; + struct mux_control *mux; + s32 idle_state; + int ret; + + mux_chip = devm_mux_chip_alloc(dev, 1, sizeof(*priv)); + if (IS_ERR(mux_chip)) + return dev_err_probe(dev, PTR_ERR(mux_chip), "failed to allocate mux chip\n"); + + fwnode = dev_fwnode(dev->parent); + if (!fwnode) + return dev_err_probe(dev, -ENODEV, "missing parent firmware node\n"); + + /* Borrow the parent's firmware node so consumers can find this mux chip */ + device_set_node(&mux_chip->dev, fwnode); + + priv = mux_chip_priv(mux_chip); + priv->dev = dev; + + priv->map = dev_get_regmap(dev->parent, NULL); + if (!priv->map) + return dev_err_probe(dev, -ENODEV, "failed to get parent regmap\n"); + + mux_chip->ops = &mc33978_mux_ops; + + mux = &mux_chip->mux[0]; + mux->states = MC33978_NUM_AMUX_CH; + + ret = device_property_read_u32(&mux_chip->dev, "idle-state", + (u32 *)&idle_state); + if (ret < 0 && ret != -EINVAL) { + return dev_err_probe(dev, ret, "failed to parse idle-state\n"); + } else if (ret == -EINVAL) { + mux->idle_state = MUX_IDLE_AS_IS; + } else { + if (idle_state == MUX_IDLE_DISCONNECT) + return dev_err_probe(dev, -EINVAL, + "idle-disconnect not supported by hardware\n"); + if (idle_state != MUX_IDLE_AS_IS && + (idle_state < 0 || idle_state >= MC33978_NUM_AMUX_CH)) + return dev_err_probe(dev, -EINVAL, "invalid idle-state %d\n", + idle_state); + mux->idle_state = idle_state; + } + + ret = devm_mux_chip_register(dev, mux_chip); + if (ret) + return dev_err_probe(dev, ret, "failed to register mux chip\n"); + + platform_set_drvdata(pdev, mux_chip); + + return 0; +} + +static const struct platform_device_id mc33978_mux_id[] = { + { .name = "mc33978-mux" }, + { .name = "mc34978-mux" }, + { } +}; +MODULE_DEVICE_TABLE(platform, mc33978_mux_id); + +static struct platform_driver mc33978_mux_driver = { + .driver = { + .name = "mc33978-mux", + }, + .probe = mc33978_mux_probe, + .id_table = mc33978_mux_id, +}; +module_platform_driver(mc33978_mux_driver); + +MODULE_AUTHOR("Oleksij Rempel "); +MODULE_DESCRIPTION("NXP MC33978/MC34978 Analog Multiplexer Driver"); +MODULE_LICENSE("GPL"); -- 2.47.3