From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 3436C31A7F6; Tue, 28 Apr 2026 17:02:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777395739; cv=none; b=svZ1JrBMB2anIyL5ZGcQzYXflhGsIBugT5EYRpygsa304WvlKbvnFfSoXAWHSCtWUUhASrNBG8CWwibhShxa82qjZJixA3c0NYyDfS/J+HJssZJoPziKVfKdZmxAoJj8i5AULnBle/+xvjp9VTlpf6oXq8m/Vh5xlXhnqNZQ5VE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777395739; c=relaxed/simple; bh=d6bxSpQnPzAhUfpXFkl4ed0Xj2EaJ5VoyGZHGKMSqXU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=kbosh3v9v3Y6Nhbo+X0u0+i6gf4g0th2lBnoKp4uxYqFLYE/vFFC7f+uOfWdIAFU8wKxTjUYshXhdQVr9+Pslb3iUT+JZTUGDZ/bcain/dZNFptwqK0bK9w3htzlGcLeJB/EDalRGdMtHD4BkaeMRqGel0TtluDDsGOTCEfCqyA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=S14sxr+w; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="S14sxr+w" Received: by smtp.kernel.org (Postfix) with ESMTPS id 09931C2BCC9; Tue, 28 Apr 2026 17:02:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1777395739; bh=d6bxSpQnPzAhUfpXFkl4ed0Xj2EaJ5VoyGZHGKMSqXU=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=S14sxr+wFwMUBNTnwmCAoH8fTKvx4AaZjx8B6U+JTVq63Rp+q8Z1ZBJ4Dvm6bfjxp 2LH47qWJyjwTGvgVS+Rb1LBVi5cKXqX6ye0aGgVTGkOls78UYjCgoPaOpiK+xAGQM9 JWfdg4B8VHt1GoNJtGtytceB8GXu1LSCbijcUOmFHyDQWRVvcG6GS9BlyjJ8E+sJT+ XDEVMVzNhQGNgB6/ql8hTwu+DeLH6NWvdArdqUmmTkoBnOCCiKkYr81a4+51IpE8cX q2tqHgCPH7PG9vo6waMzy9i79XsTHzIjYvAJnEnygMaZRXyyXhNJf67Fa6C9Q+exF0 ThbtFqoQ8L8Aw== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id F3C30FF8878; Tue, 28 Apr 2026 17:02:18 +0000 (UTC) From: Rodrigo Alencar via B4 Relay Date: Tue, 28 Apr 2026 18:02:19 +0100 Subject: [PATCH v3 04/11] iio: dac: ad5686: fix powerdown control on dual-channel devices Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260428-ad5686-fixes-v3-4-9cff7bd67a15@analog.com> References: <20260428-ad5686-fixes-v3-0-9cff7bd67a15@analog.com> In-Reply-To: <20260428-ad5686-fixes-v3-0-9cff7bd67a15@analog.com> To: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Stefan Popa , Jonathan Cameron , Greg Kroah-Hartman , Michael Auchter , Jonathan Cameron Cc: Lars-Peter Clausen , Michael Hennerich , Jonathan Cameron , David Lechner , Andy Shevchenko , Rodrigo Alencar X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1777395737; l=4274; i=rodrigo.alencar@analog.com; s=default; h=from:subject:message-id; bh=swK7HWPRTiDnxfKdBPmwx4vSDCtY2pSzF0OqooQD2i4=; b=RfpQ/Hwv6XLTi5jXXmp3SBUwviWtkJprrU6TDNoohQYhakp7uwzDs9ROpKxA89c494XHyFa4z dhQArrBm8hiCp7BUPEccfj9Tm03ZsAUW68aGCBfk7IPaFWf4g3uENdD X-Developer-Key: i=rodrigo.alencar@analog.com; a=ed25519; pk=ULeHbgU/OYh/PG/4anHDfLgldFItQHAhOktYRVLMFRo= X-Endpoint-Received: by B4 Relay for rodrigo.alencar@analog.com/default with auth_id=561 X-Original-From: Rodrigo Alencar Reply-To: rodrigo.alencar@analog.com From: Rodrigo Alencar 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. 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 --- drivers/iio/dac/ad5686.c | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/drivers/iio/dac/ad5686.c b/drivers/iio/dac/ad5686.c index 69358dd66cbc..b31d0ec5415c 100644 --- a/drivers/iio/dac/ad5686.c +++ b/drivers/iio/dac/ad5686.c @@ -25,24 +25,35 @@ 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; + return ((st->pwr_down_mode >> shift) & 0x3) - 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); guard(mutex)(&st->lock); - st->pwr_down_mode &= ~(0x3 << (chan->channel * 2)); - st->pwr_down_mode |= ((mode + 1) << (chan->channel * 2)); + st->pwr_down_mode &= ~(0x3 << shift); + st->pwr_down_mode |= (mode + 1) << shift; return 0; } @@ -57,10 +68,10 @@ 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)))); + return sysfs_emit(buf, "%d\n", !!(st->pwr_down_mask & (0x3 << shift))); } static ssize_t ad5686_write_dac_powerdown(struct iio_dev *indio_dev, @@ -82,9 +93,9 @@ static ssize_t ad5686_write_dac_powerdown(struct iio_dev *indio_dev, guard(mutex)(&st->lock); if (readin) - st->pwr_down_mask |= (0x3 << (chan->channel * 2)); + st->pwr_down_mask |= 0x3 << ad5686_pd_mask_shift(chan); else - st->pwr_down_mask &= ~(0x3 << (chan->channel * 2)); + st->pwr_down_mask &= ~(0x3 << ad5686_pd_mask_shift(chan)); switch (st->chip_info->regmap_type) { case AD5310_REGMAP: @@ -464,7 +475,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; bool has_external_vref; u8 cmd; int ret, i; @@ -489,8 +500,14 @@ int ad5686_probe(struct device *dev, st->vref_mv = has_external_vref ? ret / 1000 : st->chip_info->int_vref_mv; /* 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)); + st->pwr_down_mode = ~0U; + st->pwr_down_mask = ~0U; + for (i = 0; i < st->chip_info->num_channels; i++) { + shift = ad5686_pd_mask_shift(&st->chip_info->channels[i]); + st->pwr_down_mask &= ~(0x3 << shift); /* powered up state */ + st->pwr_down_mode &= ~(0x3 << shift); + st->pwr_down_mode |= 0x01 << shift; + } indio_dev->name = name; indio_dev->info = &ad5686_info; -- 2.43.0