From patchwork Fri Sep 29 17:23:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Lechner X-Patchwork-Id: 13404646 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AAA9FE728D0 for ; Fri, 29 Sep 2023 17:26:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233814AbjI2R0S (ORCPT ); Fri, 29 Sep 2023 13:26:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41934 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233717AbjI2R0P (ORCPT ); Fri, 29 Sep 2023 13:26:15 -0400 Received: from mail-oo1-xc2d.google.com (mail-oo1-xc2d.google.com [IPv6:2607:f8b0:4864:20::c2d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7CB601B8 for ; Fri, 29 Sep 2023 10:26:00 -0700 (PDT) Received: by mail-oo1-xc2d.google.com with SMTP id 006d021491bc7-57b9cb05fa3so468732eaf.1 for ; Fri, 29 Sep 2023 10:26:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1696008359; x=1696613159; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Poh3NcltZJCv8ErOckDuYAOGqnEo7FR3Od1QJodXfh4=; b=XZL5fqUy6jsM/2hgri2lGkEOLdPeJEqnQiF0vzkDquju09dgNyORdIWS3W4O6swQJz JyrZflus2mCu/KkFvGSAB7KTIQW715aAR+NATDYFE/61sJHJ+1xtbYy4dnKBzQzaGE1X V1za1w4upFvJXrBuBkenIigzXfWqPy/0sDtWcBfJSyQUcozQqHS2ay/hSy5r8denyc2S FUgHlNGjLOSOc3/To/I08mm4lSB4BJ8jc+kyrb8DTf1xb6uMSWfcCEN99HH08ljZyfay NSzmQTXVkQG00WGPvHaYtpXhAGAGReDUPbE5k2Jt1vwNTwWXqlO18DZpuzzRCwToBXzv qDcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696008359; x=1696613159; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Poh3NcltZJCv8ErOckDuYAOGqnEo7FR3Od1QJodXfh4=; b=LKZoyMBvxr+PBf9HtK4lxMjrGqhgkpXJ9sHzV/CGnfioe6nbilIeauSv3x909dynyB pdugSRXA5YE5vH7jhoj7hMHITZewzrdWZ/3nH3uc4MVBgTOXrspopYODWMOOBqE6PCSG l8NJYRe4pWnQ6JFXcZCI+c+ZE4TIpxzHRUk/VaHo3azxqjPYx/RfrR4JSQkoSSjWTqiF cL6eXDvlniJfn0rcmaDGqNG50MOky4OyT+2HE9Qu8iq/4PoC07+lMLT2ZEJCkA+niu8W 7Ud0DfLXoUHIyeFhnNiTlCEjzrOs4my/FuoC06pnke9QyfviF37Iw1sgQaX0xAyzyTE9 0ifg== X-Gm-Message-State: AOJu0YwYdfSGDR0TXhWSGrKZAd84+YPmBe0iwLXpK78lVIpDzpTbwS0r HS3wNakjF25sTF3sm0MacvqKjV92WwZf930tD9Tt8A== X-Google-Smtp-Source: AGHT+IFcKaBoCPHTdIYrdiIJME1W/aXyuWm+uUQA3E3oI36LT3glA4oafOIzM/Cwr+iVAGhbAWzh9A== X-Received: by 2002:a4a:a347:0:b0:576:bbf6:8a8e with SMTP id u7-20020a4aa347000000b00576bbf68a8emr3601045ool.2.1696008359600; Fri, 29 Sep 2023 10:25:59 -0700 (PDT) Received: from freyr.lechnology.com (ip98-183-112-25.ok.ok.cox.net. [98.183.112.25]) by smtp.gmail.com with ESMTPSA id f128-20020a4a5886000000b0057bb326cad4sm2272915oob.33.2023.09.29.10.25.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Sep 2023 10:25:59 -0700 (PDT) From: David Lechner To: linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-staging@lists.linux.dev Cc: David Lechner , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Michael Hennerich , =?utf-8?q?Nuno_S=C3=A1?= , Axel Haslam , Philip Molloy , linux-kernel@vger.kernel.org, David Lechner Subject: [PATCH v3 14/27] staging: iio: resolver: ad2s1210: implement hysteresis as channel attr Date: Fri, 29 Sep 2023 12:23:19 -0500 Message-ID: <20230929-ad2s1210-mainline-v3-14-fa4364281745@baylibre.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20230929-ad2s1210-mainline-v3-0-fa4364281745@baylibre.com> References: <20230929-ad2s1210-mainline-v3-0-fa4364281745@baylibre.com> MIME-Version: 1.0 X-Mailer: b4 0.12.3 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: David Lechner From: David Lechner The AD2S1210 resolver has a hysteresis feature that can be used to prevent flicker in the LSB of the position register. This can be either enabled or disabled. Disabling hysteresis is useful for increasing precision by oversampling. Signed-off-by: David Lechner --- v3 changes: * Refactored into more functions to reduce complexity of switch statements. * Use early return instead of break in switch statements. drivers/staging/iio/resolver/ad2s1210.c | 86 +++++++++++++++++++++++++++++++-- 1 file changed, 83 insertions(+), 3 deletions(-) diff --git a/drivers/staging/iio/resolver/ad2s1210.c b/drivers/staging/iio/resolver/ad2s1210.c index 0ec3598b600a..a82cb124a12f 100644 --- a/drivers/staging/iio/resolver/ad2s1210.c +++ b/drivers/staging/iio/resolver/ad2s1210.c @@ -78,7 +78,6 @@ struct ad2s1210_state { /** The external oscillator frequency in Hz. */ unsigned long clkin_hz; unsigned int fexcit; - bool hysteresis; u8 resolution; /** For reading raw sample value via SPI. */ __be16 sample __aligned(IIO_DMA_MINALIGN); @@ -430,6 +429,35 @@ static int ad2s1210_single_conversion(struct ad2s1210_state *st, return ret; } +static int ad2s1210_get_hysteresis(struct ad2s1210_state *st, int *val) +{ + int ret; + + mutex_lock(&st->lock); + ret = regmap_test_bits(st->regmap, AD2S1210_REG_CONTROL, + AD2S1210_ENABLE_HYSTERESIS); + mutex_unlock(&st->lock); + + if (ret < 0) + return ret; + + *val = !!ret; + return IIO_VAL_INT; +} + +static int ad2s1210_set_hysteresis(struct ad2s1210_state *st, int val) +{ + int ret; + + mutex_lock(&st->lock); + ret = regmap_update_bits(st->regmap, AD2S1210_REG_CONTROL, + AD2S1210_ENABLE_HYSTERESIS, + val ? AD2S1210_ENABLE_HYSTERESIS : 0); + mutex_unlock(&st->lock); + + return ret; +} + static const int ad2s1210_velocity_scale[] = { 17089132, /* 8.192MHz / (2*pi * 2500 / 2^15) */ 42722830, /* 8.192MHz / (2*pi * 1000 / 2^15) */ @@ -462,7 +490,55 @@ static int ad2s1210_read_raw(struct iio_dev *indio_dev, default: return -EINVAL; } + case IIO_CHAN_INFO_HYSTERESIS: + switch (chan->type) { + case IIO_ANGL: + return ad2s1210_get_hysteresis(st, val); + default: + return -EINVAL; + } + default: + return -EINVAL; + } +} + +static int ad2s1210_read_avail(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + const int **vals, int *type, + int *length, long mask) +{ + static const int hysteresis_available[] = { 0, 1 }; + + switch (mask) { + case IIO_CHAN_INFO_HYSTERESIS: + switch (chan->type) { + case IIO_ANGL: + *vals = hysteresis_available; + *type = IIO_VAL_INT; + *length = ARRAY_SIZE(hysteresis_available); + return IIO_AVAIL_LIST; + default: + return -EINVAL; + } + default: + return -EINVAL; + } +} +static int ad2s1210_write_raw(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + int val, int val2, long mask) +{ + struct ad2s1210_state *st = iio_priv(indio_dev); + + switch (mask) { + case IIO_CHAN_INFO_HYSTERESIS: + switch (chan->type) { + case IIO_ANGL: + return ad2s1210_set_hysteresis(st, val); + default: + return -EINVAL; + } default: return -EINVAL; } @@ -503,7 +579,10 @@ static const struct iio_chan_spec ad2s1210_channels[] = { .indexed = 1, .channel = 0, .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | - BIT(IIO_CHAN_INFO_SCALE), + BIT(IIO_CHAN_INFO_SCALE) | + BIT(IIO_CHAN_INFO_HYSTERESIS), + .info_mask_separate_available = + BIT(IIO_CHAN_INFO_HYSTERESIS), }, { .type = IIO_ANGL_VEL, .indexed = 1, @@ -581,6 +660,8 @@ static int ad2s1210_debugfs_reg_access(struct iio_dev *indio_dev, static const struct iio_info ad2s1210_info = { .read_raw = ad2s1210_read_raw, + .read_avail = ad2s1210_read_avail, + .write_raw = ad2s1210_write_raw, .attrs = &ad2s1210_attribute_group, .debugfs_reg_access = &ad2s1210_debugfs_reg_access, }; @@ -696,7 +777,6 @@ static int ad2s1210_probe(struct spi_device *spi) mutex_init(&st->lock); st->sdev = spi; - st->hysteresis = true; st->resolution = 12; st->fexcit = AD2S1210_DEF_EXCIT;