From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (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 1D3102116F4 for ; Thu, 4 Jun 2026 16:51:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780591864; cv=none; b=dEtQ8qlLUsqz+pQwzuAUduOBcEQ/iGYPGBYO/APs9F7fS+0AJWEqdquOY2t2LRK2e8ULixUQOt7F3hZD5I2k/Ke9N96Oyqcwgwz+itofjZHHc0IZiwMFEGy4spXuRonu+kiuArWHwgQuuL8yRa9qmpE+kLTMGR6cabz6TqKj5BE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780591864; c=relaxed/simple; bh=a8UgwlT7jOTTnC3dSUlx2lm/6Uw0OW4vtIE2FnXl3bg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=joRv5fccaJkwcA7Ys0NiZiTRTsxZmOSRJF2apnzHfnDSGlhhY3EbKtqXpol8C3FTjgivQmnolfOcgAM0+MMjpU5I2KADVOALS9uRPnaxU1XF16h1wTq6PYEUxfLuiNjC8aYwW+4ORrgoYEO/uMcJNXWGN5fR4ZWMx/JN1w72MBk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=kpvrUHG2; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="kpvrUHG2" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 350211F00893; Thu, 4 Jun 2026 16:51:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1780591862; bh=wFyUds3bgA+9cGOjxE3fbAze7Ygq6iX6jXKUQH9YGRI=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=kpvrUHG2FvLdf3Oi1HiwI5EAzBNHJNefVf830bsOivdDpIg0rparguMfWAdxo88Jo ln6ZawwMu2bVdSZaVxDuSEAWJRVmprlTnoZNAJmjjw6bzSlh1gLuix5RR3nkhqOvVi 3LjkoLENA5Hf+pDogZYYb3mZUVIuH3r7v5VNQAtqmEomtiYldm7YfBPQFbARZRxzGA NolaRgaJd+lop9rbxrbweaeVGBOxXmKQanAOLx/GAAS2sll27G9OqoMfPjkUFwL5oQ FHlHSCo5ZDImQEpibzU9xOzgOZle4YCqh0FXlkqo3cRQv5SHECJ+wdiG98/ScWfRy1 8qSIkR1sKKcFg== From: Sasha Levin To: stable@vger.kernel.org Cc: Rodrigo Alencar , Stable@vger.kernel.org, Jonathan Cameron , Sasha Levin Subject: [PATCH 6.1.y] iio: dac: ad5686: fix powerdown control on dual-channel devices Date: Thu, 4 Jun 2026 12:50:59 -0400 Message-ID: <20260604165100.3876351-1-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <2026060442-rink-repose-7887@gregkh> References: <2026060442-rink-repose-7887@gregkh> Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Rodrigo Alencar [ Upstream commit 8aeaf25a85263a7a43357e16ad78ab969f6f8aeb ] Fix powerdown control by using a proper bit shift for the powerdown mask values. During initialization, powerdown bits are initialized so that unused bits are set to 1 and the correct bit shift is used. Dual-channel devices use one-hot encoding in the address and that reflects on the position of the powerdown bits, which are not channel-index based for that case. Quad-channel devices also use one-hot encoding for the channel address but the result of log2(address) coincides with the channel index value. Mask as 0x3U is used rather than 0x3, because shift can reach value of 30 (last channel of a 16-channel device), which would mess with the sign bit. The issue was introduced when first adding support for dual-channel devices, which overlooked powerdown control differences. Fixes: 7dc8faeab3e3 ("iio: dac: ad5686: add support for AD5338R") Signed-off-by: Rodrigo Alencar Cc: Signed-off-by: Jonathan Cameron Signed-off-by: Sasha Levin --- drivers/iio/dac/ad5686.c | 46 +++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/drivers/iio/dac/ad5686.c b/drivers/iio/dac/ad5686.c index 7d1b2b83172de9..7e41fa03d72dec 100644 --- a/drivers/iio/dac/ad5686.c +++ b/drivers/iio/dac/ad5686.c @@ -25,22 +25,37 @@ static const char * const ad5686_powerdown_modes[] = { "three_state" }; +static inline unsigned int ad5686_pd_mask_shift(const struct iio_chan_spec *chan) +{ + if (chan->channel == chan->address) + return chan->channel * 2; + + /* one-hot encoding is used in dual/quad channel devices */ + return __ffs(chan->address) * 2; +} + static int ad5686_get_powerdown_mode(struct iio_dev *indio_dev, const struct iio_chan_spec *chan) { + unsigned int shift = ad5686_pd_mask_shift(chan); struct ad5686_state *st = iio_priv(indio_dev); - return ((st->pwr_down_mode >> (chan->channel * 2)) & 0x3) - 1; + guard(mutex)(&st->lock); + + return ((st->pwr_down_mode >> shift) & 0x3U) - 1; } static int ad5686_set_powerdown_mode(struct iio_dev *indio_dev, const struct iio_chan_spec *chan, unsigned int mode) { + unsigned int shift = ad5686_pd_mask_shift(chan); struct ad5686_state *st = iio_priv(indio_dev); - st->pwr_down_mode &= ~(0x3 << (chan->channel * 2)); - st->pwr_down_mode |= ((mode + 1) << (chan->channel * 2)); + guard(mutex)(&st->lock); + + st->pwr_down_mode &= ~(0x3U << shift); + st->pwr_down_mode |= (mode + 1) << shift; return 0; } @@ -55,10 +70,12 @@ static const struct iio_enum ad5686_powerdown_mode_enum = { static ssize_t ad5686_read_dac_powerdown(struct iio_dev *indio_dev, uintptr_t private, const struct iio_chan_spec *chan, char *buf) { + unsigned int shift = ad5686_pd_mask_shift(chan); struct ad5686_state *st = iio_priv(indio_dev); - return sysfs_emit(buf, "%d\n", !!(st->pwr_down_mask & - (0x3 << (chan->channel * 2)))); + guard(mutex)(&st->lock); + + return sysfs_emit(buf, "%d\n", !!(st->pwr_down_mask & (0x3U << shift))); } static ssize_t ad5686_write_dac_powerdown(struct iio_dev *indio_dev, @@ -78,9 +95,9 @@ static ssize_t ad5686_write_dac_powerdown(struct iio_dev *indio_dev, return ret; if (readin) - st->pwr_down_mask |= (0x3 << (chan->channel * 2)); + st->pwr_down_mask |= 0x3U << ad5686_pd_mask_shift(chan); else - st->pwr_down_mask &= ~(0x3 << (chan->channel * 2)); + st->pwr_down_mask &= ~(0x3U << ad5686_pd_mask_shift(chan)); switch (st->chip_info->regmap_type) { case AD5310_REGMAP: @@ -453,7 +470,7 @@ int ad5686_probe(struct device *dev, { struct ad5686_state *st; struct iio_dev *indio_dev; - unsigned int val, ref_bit_msk; + unsigned int val, ref_bit_msk, shift; u8 cmd; int ret, i, voltage_uv = 0; @@ -488,9 +505,18 @@ int ad5686_probe(struct device *dev, else st->vref_mv = st->chip_info->int_vref_mv; + /* Initialize masks to all ones provided the max shift (last channel) */ + shift = ad5686_pd_mask_shift(&st->chip_info->channels[st->chip_info->num_channels - 1]); + st->pwr_down_mask = GENMASK(shift + 1, 0); + st->pwr_down_mode = GENMASK(shift + 1, 0); + /* Set all the power down mode for all channels to 1K pulldown */ - for (i = 0; i < st->chip_info->num_channels; i++) - st->pwr_down_mode |= (0x01 << (i * 2)); + for (i = 0; i < st->chip_info->num_channels; i++) { + shift = ad5686_pd_mask_shift(&st->chip_info->channels[i]); + st->pwr_down_mask &= ~(0x3U << shift); /* powered up state */ + st->pwr_down_mode &= ~(0x3U << shift); + st->pwr_down_mode |= 0x01U << shift; + } indio_dev->name = name; indio_dev->info = &ad5686_info; -- 2.53.0