From patchwork Thu Sep 21 14:19:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Lechner X-Patchwork-Id: 13394663 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 9037DE7D0AA for ; Thu, 21 Sep 2023 21:20:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231497AbjIUVUt (ORCPT ); Thu, 21 Sep 2023 17:20:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46074 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232637AbjIUVOc (ORCPT ); Thu, 21 Sep 2023 17:14:32 -0400 Received: from mail-ej1-x635.google.com (mail-ej1-x635.google.com [IPv6:2a00:1450:4864:20::635]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9CFB95BBE for ; Thu, 21 Sep 2023 10:08:54 -0700 (PDT) Received: by mail-ej1-x635.google.com with SMTP id a640c23a62f3a-99de884ad25so148392666b.3 for ; Thu, 21 Sep 2023 10:08:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1695316132; x=1695920932; 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=lWgDNsrrXF5syWEsVgdKbxr1ASE09oFAZQly/Ty6i74=; b=q07ZfFYdIk2e4t8Wt6QxjCcWOHY/5NXBDrj0IqELgZeFv/5aeQul9OGw6rl5N8iqVq 3q9o/D3vOQ6bPLY9t524V16rdY+/SXw55dK3D+aDB4PqL2Sf6cNadQLU9yxIvW21Gzkr 96a0Tw0OYo9NVXhOGMGNDjEGjJx5QNUO8/VyTDpwmPbAPzb9BMBOfDqpEnTbLt/DJa2N fFntFFbv30dSy46BYAr27K04yTEVq2L8tUsC1omC2SomPT/8XRV+4PvwxJDok+c9ULtc oyaEgr1h40l1gWiXB4Td7blafx9hL+63T9PLtcVfdsJ50AsJk3fZjpvBcs9TbRTfBBcz +s0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695316132; x=1695920932; 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=lWgDNsrrXF5syWEsVgdKbxr1ASE09oFAZQly/Ty6i74=; b=Ld1vauOCps4KcyzVsOFrBULqH5zr4fLs2ASUtGGsP7cn91v6WkIrf+jF6M+VQ2w4ef yD52dg+VhCnlWN2S0uHL3O95W5AN1cXrOEH4hyQsnPfkzDjkPzsVRZsep2HWs/pantjk zZL05oSUO2pFVz0rcW4ihAokQOYe43tzdw4Qv0g33TJixXKhTmKcaEROwEmTRktbJktr AYu1dB2SxxvWjTLYhjzzEFOTchDzEMpxuxc321n0vVBoOgPNzTj0QMb+9+F/3cDicIHY K5l5Nyd9+Cs7hOlo+cgCr9AStIlQoVoKjV03dFc1Ju/+tEWMtXmXNCxK10W+KxkZdjIr 520g== X-Gm-Message-State: AOJu0Ywwf8hG3bOsQxKgY3bXjM9MlYmgazE8Mn5J1Wo59ZD3u4riloDI OKXj1za2Tg5wWf5hlYsMlBN15pCoqzvZDLhVNbrpJhkt X-Google-Smtp-Source: AGHT+IGRjZhp2R5sxkFWDdwbOa90jJSgYK4eOmJzrC4HUjAJ3wveQwXoDeexC2vjkJhYZNOJkJ9DkA== X-Received: by 2002:a5d:5192:0:b0:320:9e2:b3a2 with SMTP id k18-20020a5d5192000000b0032009e2b3a2mr4841480wrv.33.1695306164753; Thu, 21 Sep 2023 07:22:44 -0700 (PDT) Received: from localhost.localdomain (abordeaux-655-1-129-86.w90-5.abo.wanadoo.fr. [90.5.10.86]) by smtp.gmail.com with ESMTPSA id n11-20020a5d4c4b000000b0031fbbe347ebsm1901426wrt.22.2023.09.21.07.22.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Sep 2023 07:22:44 -0700 (PDT) From: David Lechner To: linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-staging@lists.linux.dev Cc: linux-kernel@vger.kernel.org, Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Michael Hennerich , =?utf-8?q?Nuno_S=C3=A1?= , Axel Haslam , Philip Molloy , David Lechner Subject: [v2 14/19] staging: iio: resolver: ad2s1210: refactor setting excitation frequency Date: Thu, 21 Sep 2023 09:19:42 -0500 Message-Id: <20230921141947.57784-17-dlechner@baylibre.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230921141947.57784-1-dlechner@baylibre.com> References: <20230921141947.57784-1-dlechner@baylibre.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org This combines the ad2s1210_update_frequency_control_word() and ad2s1210_soft_reset() functions into a single function since they both have to be called together. Also clean up a few things while touching this: - move AD2S1210_DEF_EXCIT macro with similar macros - remove unnecessary dev_err() calls Signed-off-by: David Lechner --- drivers/staging/iio/resolver/ad2s1210.c | 63 ++++++++++++------------- 1 file changed, 31 insertions(+), 32 deletions(-) diff --git a/drivers/staging/iio/resolver/ad2s1210.c b/drivers/staging/iio/resolver/ad2s1210.c index fe413759deb9..f1ffee34ebbc 100644 --- a/drivers/staging/iio/resolver/ad2s1210.c +++ b/drivers/staging/iio/resolver/ad2s1210.c @@ -53,12 +53,11 @@ #define AD2S1210_MIN_CLKIN 6144000 #define AD2S1210_MAX_CLKIN 10240000 #define AD2S1210_MIN_EXCIT 2000 +#define AD2S1210_DEF_EXCIT 10000 #define AD2S1210_MAX_EXCIT 20000 #define AD2S1210_MIN_FCW 0x4 #define AD2S1210_MAX_FCW 0x50 -#define AD2S1210_DEF_EXCIT 10000 - enum ad2s1210_mode { MOD_POS = 0b00, MOD_VEL = 0b01, @@ -184,18 +183,29 @@ static int ad2s1210_regmap_reg_read(void *context, unsigned int reg, return 0; } -static inline -int ad2s1210_update_frequency_control_word(struct ad2s1210_state *st) +/* + * Sets the excitation frequency and performs software reset. + * + * Must be called with lock held. + */ +static int ad2s1210_set_excitation_frequency(struct ad2s1210_state *st, + u16 fexcit) { - unsigned char fcw; + int ret; + u8 fcw; - fcw = (unsigned char)(st->fexcit * (1 << 15) / st->fclkin); - if (fcw < AD2S1210_MIN_FCW || fcw > AD2S1210_MAX_FCW) { - dev_err(&st->sdev->dev, "ad2s1210: FCW out of range\n"); + fcw = fexcit * (1 << 15) / st->fclkin; + if (fcw < AD2S1210_MIN_FCW || fcw > AD2S1210_MAX_FCW) return -ERANGE; - } - return regmap_write(st->regmap, AD2S1210_REG_EXCIT_FREQ, fcw); + ret = regmap_write(st->regmap, AD2S1210_REG_EXCIT_FREQ, fcw); + if (ret < 0) + return ret; + + st->fexcit = fexcit; + + /* software reset reinitializes the excitation frequency output */ + return regmap_write(st->regmap, AD2S1210_REG_SOFT_RESET, 0); } static int ad2s1210_set_resolution_gpios(struct ad2s1210_state *st, @@ -210,11 +220,6 @@ static int ad2s1210_set_resolution_gpios(struct ad2s1210_state *st, bitmap); } -static inline int ad2s1210_soft_reset(struct ad2s1210_state *st) -{ - return regmap_write(st->regmap, AD2S1210_REG_SOFT_RESET, 0); -} - static ssize_t ad2s1210_show_fexcit(struct device *dev, struct device_attribute *attr, char *buf) @@ -229,27 +234,24 @@ static ssize_t ad2s1210_store_fexcit(struct device *dev, const char *buf, size_t len) { struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev)); - unsigned int fexcit; + u16 fexcit; int ret; - ret = kstrtouint(buf, 10, &fexcit); - if (ret < 0) - return ret; - if (fexcit < AD2S1210_MIN_EXCIT || fexcit > AD2S1210_MAX_EXCIT) { - dev_err(dev, - "ad2s1210: excitation frequency out of range\n"); + ret = kstrtou16(buf, 10, &fexcit); + if (ret < 0 || fexcit < AD2S1210_MIN_EXCIT || fexcit > AD2S1210_MAX_EXCIT) return -EINVAL; - } + mutex_lock(&st->lock); - st->fexcit = fexcit; - ret = ad2s1210_update_frequency_control_word(st); + ret = ad2s1210_set_excitation_frequency(st, fexcit); if (ret < 0) goto error_ret; - ret = ad2s1210_soft_reset(st); + + ret = len; + error_ret: mutex_unlock(&st->lock); - return ret < 0 ? ret : len; + return ret; } static ssize_t ad2s1210_show_resolution(struct device *dev, @@ -624,10 +626,8 @@ static int ad2s1210_initial(struct ad2s1210_state *st) if (ret < 0) goto error_ret; - ret = ad2s1210_update_frequency_control_word(st); - if (ret < 0) - goto error_ret; - ret = ad2s1210_soft_reset(st); + ret = ad2s1210_set_excitation_frequency(st, AD2S1210_DEF_EXCIT); + error_ret: mutex_unlock(&st->lock); return ret; @@ -773,7 +773,6 @@ static int ad2s1210_probe(struct spi_device *spi) mutex_init(&st->lock); st->sdev = spi; st->resolution = 12; - st->fexcit = AD2S1210_DEF_EXCIT; ret = ad2s1210_setup_clocks(st); if (ret < 0)