From patchwork Tue Apr 4 14:11:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: William Breathitt Gray X-Patchwork-Id: 13200264 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 EBCA0C6FD1D for ; Tue, 4 Apr 2023 14:12:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234223AbjDDOML (ORCPT ); Tue, 4 Apr 2023 10:12:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33994 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235518AbjDDOMK (ORCPT ); Tue, 4 Apr 2023 10:12:10 -0400 Received: from mail-yw1-x1131.google.com (mail-yw1-x1131.google.com [IPv6:2607:f8b0:4864:20::1131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 200AB3ABD for ; Tue, 4 Apr 2023 07:12:07 -0700 (PDT) Received: by mail-yw1-x1131.google.com with SMTP id 00721157ae682-5456249756bso615808267b3.5 for ; Tue, 04 Apr 2023 07:12:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1680617526; 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=9McelT+zWG2/DIkleGNvPOrsfCv2UT04HfKm+eOKcDU=; b=CO/hizh+011yKzU6HontJBqPWmzmJxc1gbnTM6f3+Yqc79ccNokRyR/7GJzC2qp/IW 77YiGpZEMp3k9OrXetCCxDZoZM9wCYzr5l2+OOuQKIPn3/PIpbNDDbJJoUmKz4vqSVMG C/BwkKvTishZZEqCFwmaLuryFw9oeiUKl0eDqDjW8gYpgo6jCbWZGSK87ApwJGsEUVIi QElE4bpmOf6nemwJs9CtXKHBU7onJpehB2z6ymuR+7JiP9NvqlofUCaxS96hnR5zRrTd 9mpV+6qAUyJ6Pesiy3lflzabTLEtHraF8pY2juNoHUKhTfqruKLHZWtSI8LUOaeuX6vI J4oA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680617526; 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=9McelT+zWG2/DIkleGNvPOrsfCv2UT04HfKm+eOKcDU=; b=VnL4BTFMMFNz+Z4fCcloM+e48+sQNxq4e8cHWTmgmCq7PK0XJ15bNzSEVQ36i4Efcn buB9rN/k+tj+sfoQ26FTn+gBl3n0/myeb/lYZ6fcfgO1J2jjmy3Ii1Dps22SbtEj7JWs eicfsdOequEFYSdN1+idvByvAzJ2h8iapuRmexee/myQDFuWti5YD92B7N97jFVcwxoa ZtZAkRv0gBsz8JLAvRkraTLRGZ5oyd29yjFQGYf67ZwzTQtLysNIK6Ae0gXUk1h/KEkU GHHVv1owviAxCeyLhV1E3G+jvkzMO7S78J/zzRm5lVYTKk7Xs9EFkzIo0xhT6rOjciBO hIAw== X-Gm-Message-State: AAQBX9eFd0Xf7eMXuKjP3O3Oe2bZYL5QGXk6zhZyKzszuM6kuFZPnTC5 o5QaNHBWnepA+XdTXN5kieRiPg== X-Google-Smtp-Source: AKy350a5g/bqjE02/w4LwDBmkieF0djaQVVPmgD8EpD5vIGAfgM7RY+uJOBm98yJAhCtWCOHIKb+pg== X-Received: by 2002:a81:4808:0:b0:541:a49e:6f26 with SMTP id v8-20020a814808000000b00541a49e6f26mr2459164ywa.10.1680617526309; Tue, 04 Apr 2023 07:12:06 -0700 (PDT) Received: from fedora.attlocal.net (69-109-179-158.lightspeed.dybhfl.sbcglobal.net. [69.109.179.158]) by smtp.gmail.com with ESMTPSA id d195-20020a811dcc000000b00545a08184d8sm3202750ywd.104.2023.04.04.07.12.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Apr 2023 07:12:05 -0700 (PDT) From: William Breathitt Gray To: Jonathan Cameron , Lars-Peter Clausen Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Andy Shevchenko , William Breathitt Gray Subject: [PATCH v4 1/5] iio: addac: stx104: Fix race condition for stx104_write_raw() Date: Tue, 4 Apr 2023 10:11:58 -0400 Message-Id: X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org The priv->chan_out_states array and actual DAC value can become mismatched if stx104_write_raw() is called concurrently. Prevent such a race condition by utilizing a mutex. Fixes: 97a445dad37a ("iio: Add IIO support for the DAC on the Apex Embedded Systems STX104") Signed-off-by: William Breathitt Gray --- drivers/iio/addac/stx104.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/drivers/iio/addac/stx104.c b/drivers/iio/addac/stx104.c index e45b70aa5bb7..4239aafe42fc 100644 --- a/drivers/iio/addac/stx104.c +++ b/drivers/iio/addac/stx104.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -69,10 +70,12 @@ struct stx104_reg { /** * struct stx104_iio - IIO device private data structure + * @lock: synchronization lock to prevent I/O race conditions * @chan_out_states: channels' output states * @reg: I/O address offset for the device registers */ struct stx104_iio { + struct mutex lock; unsigned int chan_out_states[STX104_NUM_OUT_CHAN]; struct stx104_reg __iomem *reg; }; @@ -178,9 +181,12 @@ static int stx104_write_raw(struct iio_dev *indio_dev, if ((unsigned int)val > 65535) return -EINVAL; + mutex_lock(&priv->lock); + priv->chan_out_states[chan->channel] = val; iowrite16(val, &priv->reg->dac[chan->channel]); + mutex_unlock(&priv->lock); return 0; } return -EINVAL; @@ -351,6 +357,8 @@ static int stx104_probe(struct device *dev, unsigned int id) indio_dev->name = dev_name(dev); + mutex_init(&priv->lock); + /* configure device for software trigger operation */ iowrite8(0, &priv->reg->acr); From patchwork Tue Apr 4 14:11:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: William Breathitt Gray X-Patchwork-Id: 13200265 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 A0F6FC6FD1D for ; Tue, 4 Apr 2023 14:12:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235693AbjDDOMQ (ORCPT ); Tue, 4 Apr 2023 10:12:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33988 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235594AbjDDOML (ORCPT ); Tue, 4 Apr 2023 10:12:11 -0400 Received: from mail-yw1-x112c.google.com (mail-yw1-x112c.google.com [IPv6:2607:f8b0:4864:20::112c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3D9EE4206 for ; Tue, 4 Apr 2023 07:12:08 -0700 (PDT) Received: by mail-yw1-x112c.google.com with SMTP id 00721157ae682-5463fa0c2bfso285263817b3.1 for ; Tue, 04 Apr 2023 07:12:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1680617527; 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=ntJEhFvPVyRwKjpX84wW6Nzwfs8RwaisnK/y3HB3QZI=; b=q4242DFwPstTgqf5Ui1GWU4QZfjJwo8dqlFgpBF1Y0R5XPVUd4yDM1Rzwf894WOFVJ 6e1xj/rMw0ZCFRKA4dzd8FtEjKCAJKd6Qon1yqC1BVmUvacoqdHv7+1ZA50uPczq6yup ewQPrYZMCzQ5ARCh8e1iMvQevFSngbCEVU+5SyRPiku+75PNHrzPh1IojCQYIGqiqD1l qE+0Z/99iGSfJgZWPwC+IqgGydOYuLRrU7F8bV3EvVZweZ98dWmbWTfzDc6KRrzE+H82 0MgfvhI9qRyAQ8Z5FZ5+0BxfvgWHFKYZ9SauqRu/mXORNcWHCWoWsxjG0+Wuz/1wEAAV eV1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680617527; 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=ntJEhFvPVyRwKjpX84wW6Nzwfs8RwaisnK/y3HB3QZI=; b=0DSo1oHcgSy/FAKMH9nUBqlYeB1rMFncUTRBFTxiyv79cPKmMbbFwRsjv8nxL6MACU LM/YRuOvyyENKVXgyQlBiNAcGeUwazFX7hjE6n0vKebAhwGfYnij/YBE0X3liJVzTaz3 gNUS9oJ2uuj8/wrhSHCX6G2tGbnbQ/dbIMRzdxM/EtaMa8BgYqcrRk9yoi6NarMXzO13 o4RGSwmVMEduzJGs4bL5pLBL1ZmaoTxppzJgp10/O7l9GOaGii1ahZfPeTLTCPL5fhSS RHPx196ghZi/6C/u61PXumpLvJv8CzbgSb+QuHwSsGjwvuilreQpK+L64Y0SWxSVEM7W 5g8g== X-Gm-Message-State: AAQBX9fjFLFPA/34CgxhI/zPsnq6+ziNV/zq1vfRm0sPKRy16O8oduFq Kkzz32mjQyjGazPk5kJEZWw/xQ== X-Google-Smtp-Source: AKy350Ydsp7NUvBJDlQJTdMtriZo5hMFkAr3fDz46ScNCXgyl5gdZJVc8UmA8llIPFg0FVx1L4dy0A== X-Received: by 2002:a0d:d88d:0:b0:541:8909:eb5a with SMTP id a135-20020a0dd88d000000b005418909eb5amr2368089ywe.18.1680617527339; Tue, 04 Apr 2023 07:12:07 -0700 (PDT) Received: from fedora.attlocal.net (69-109-179-158.lightspeed.dybhfl.sbcglobal.net. [69.109.179.158]) by smtp.gmail.com with ESMTPSA id d195-20020a811dcc000000b00545a08184d8sm3202750ywd.104.2023.04.04.07.12.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Apr 2023 07:12:06 -0700 (PDT) From: William Breathitt Gray To: Jonathan Cameron , Lars-Peter Clausen Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Andy Shevchenko , William Breathitt Gray Subject: [PATCH v4 2/5] iio: addac: stx104: Fix race condition when converting analog-to-digital Date: Tue, 4 Apr 2023 10:11:59 -0400 Message-Id: <2ae5e40eed5006ca735e4c12181a9ff5ced65547.1680564468.git.william.gray@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org The ADC conversion procedure requires several device I/O operations performed in a particular sequence. If stx104_read_raw() is called concurrently, the ADC conversion procedure could be clobbered. Prevent such a race condition by utilizing a mutex. Fixes: 4075a283ae83 ("iio: stx104: Add IIO support for the ADC channels") Signed-off-by: William Breathitt Gray --- drivers/iio/addac/stx104.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/iio/addac/stx104.c b/drivers/iio/addac/stx104.c index 4239aafe42fc..8730b79e921c 100644 --- a/drivers/iio/addac/stx104.c +++ b/drivers/iio/addac/stx104.c @@ -117,6 +117,8 @@ static int stx104_read_raw(struct iio_dev *indio_dev, return IIO_VAL_INT; } + mutex_lock(&priv->lock); + /* select ADC channel */ iowrite8(chan->channel | (chan->channel << 4), ®->achan); @@ -127,6 +129,8 @@ static int stx104_read_raw(struct iio_dev *indio_dev, while (ioread8(®->cir_asr) & BIT(7)); *val = ioread16(®->ssr_ad); + + mutex_unlock(&priv->lock); return IIO_VAL_INT; case IIO_CHAN_INFO_OFFSET: /* get ADC bipolar/unipolar configuration */ From patchwork Tue Apr 4 14:12:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: William Breathitt Gray X-Patchwork-Id: 13200266 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 C33A8C761A6 for ; Tue, 4 Apr 2023 14:12:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235594AbjDDOMQ (ORCPT ); Tue, 4 Apr 2023 10:12:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33994 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235683AbjDDOMM (ORCPT ); Tue, 4 Apr 2023 10:12:12 -0400 Received: from mail-yw1-x1130.google.com (mail-yw1-x1130.google.com [IPv6:2607:f8b0:4864:20::1130]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3529744AA for ; Tue, 4 Apr 2023 07:12:09 -0700 (PDT) Received: by mail-yw1-x1130.google.com with SMTP id 00721157ae682-54606036bb3so450306297b3.6 for ; Tue, 04 Apr 2023 07:12:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1680617528; 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=VXpkbjdlcHxE2UhGi8BVKz9oJwAUhYdEPgQCrFBUbtg=; b=ANaY3Sh6n86OcFsrMqgmdJnGPyQOIhZ6nlCWS5mYv5y2mHg71tnjhg8PZuIkR0nI6M unZ/wv8g57w6kK/GrQ1psdcxM1TOy1+Klw3LULHA4WnqMhiPtaWpDA/Io7YlTg2M49nD USSjeIFHQ2y8LagYrRuFGdDFh7j4n8ScbrPKYCzpoqYgKSBHhOeH1MkLxlv6mwSbxAh1 uM8+RycfcpMn80GWoODlWTd5XP+bzBoiWI2/wIaS/Jn15KelljV+kCmX1qrA0Fn6/aba OobIFVzVJ8yUydCZ6THMmCtCWYgvzJ37kiUhfdYBWEadHrxLK/PgSE2CsdOPHG3t70AL d2lQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680617528; 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=VXpkbjdlcHxE2UhGi8BVKz9oJwAUhYdEPgQCrFBUbtg=; b=CrQ8R1DYUIHCGjPWxzbKp20viVDEnU77QKQAkVuww+I80KIZcrjX2jJlCy8KbcSZsP d9qu7vbgIGuofSKmKwN7sfmsmVlmJmN4byThB0ghDZalyxeJ6uT0CBwzv5Pl1xERmVU/ 1mc5vb30QwKBr95lS8akzwLSMA3KM25Vns9Wq6sYYJ/hcSUIDJGhNtkeEpaJ8B6S0M9e OvuwjTznkcFBdjNizGCx8Xxff/MXZ/DyKwdOl0fOljWepc0c/bl6lXHWcaF6CUPftKUd nrh9e7Z6x9jGt/OEv0XUL+KV1o8RaRGITes3aUwU/6ftfKwz9IAKBqBy+Ck5+oclrjs9 hKEg== X-Gm-Message-State: AAQBX9eg6WukRRtNfjkCZTzJHdMHjNOJGLPVMkaayvCKC4wTk4P/7cf2 Wr5cIJR8Jz6T+e5Dh8PavJsf0A== X-Google-Smtp-Source: AKy350a+0t85IB/hhnmx7wWa+SKejsDXF74jW0q5rSx6aa+TL3DzYudacwZc2l2yk+6y1oddZZc7Yw== X-Received: by 2002:a81:1383:0:b0:541:61b1:4a59 with SMTP id 125-20020a811383000000b0054161b14a59mr19446751ywt.3.1680617528187; Tue, 04 Apr 2023 07:12:08 -0700 (PDT) Received: from fedora.attlocal.net (69-109-179-158.lightspeed.dybhfl.sbcglobal.net. [69.109.179.158]) by smtp.gmail.com with ESMTPSA id d195-20020a811dcc000000b00545a08184d8sm3202750ywd.104.2023.04.04.07.12.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Apr 2023 07:12:07 -0700 (PDT) From: William Breathitt Gray To: Jonathan Cameron , Lars-Peter Clausen Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Andy Shevchenko , William Breathitt Gray Subject: [PATCH v4 3/5] iio: addac: stx104: Improve indentation in stx104_write_raw() Date: Tue, 4 Apr 2023 10:12:00 -0400 Message-Id: X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org By bailing out early if chan->output is false for the IIO_CHAN_INFO_RAW, indentation can be decreased by a tab and code readability improved. Suggested-by: Andy Shevchenko Signed-off-by: William Breathitt Gray --- drivers/iio/addac/stx104.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/drivers/iio/addac/stx104.c b/drivers/iio/addac/stx104.c index 8730b79e921c..9cc467469dde 100644 --- a/drivers/iio/addac/stx104.c +++ b/drivers/iio/addac/stx104.c @@ -180,20 +180,20 @@ static int stx104_write_raw(struct iio_dev *indio_dev, return 0; case IIO_CHAN_INFO_RAW: - if (chan->output) { - /* DAC can only accept up to a 16-bit value */ - if ((unsigned int)val > 65535) - return -EINVAL; + if (!chan->output) + return -EINVAL; - mutex_lock(&priv->lock); + /* DAC can only accept up to a 16-bit value */ + if ((unsigned int)val > 65535) + return -EINVAL; - priv->chan_out_states[chan->channel] = val; - iowrite16(val, &priv->reg->dac[chan->channel]); + mutex_lock(&priv->lock); - mutex_unlock(&priv->lock); - return 0; - } - return -EINVAL; + priv->chan_out_states[chan->channel] = val; + iowrite16(val, &priv->reg->dac[chan->channel]); + + mutex_unlock(&priv->lock); + return 0; } return -EINVAL; From patchwork Tue Apr 4 14:12:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: William Breathitt Gray X-Patchwork-Id: 13200268 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 F0865C761A6 for ; Tue, 4 Apr 2023 14:12:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235558AbjDDOMT (ORCPT ); Tue, 4 Apr 2023 10:12:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33988 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235588AbjDDOMO (ORCPT ); Tue, 4 Apr 2023 10:12:14 -0400 Received: from mail-yb1-xb2f.google.com (mail-yb1-xb2f.google.com [IPv6:2607:f8b0:4864:20::b2f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1893EE53 for ; Tue, 4 Apr 2023 07:12:10 -0700 (PDT) Received: by mail-yb1-xb2f.google.com with SMTP id k17so38800774ybm.11 for ; Tue, 04 Apr 2023 07:12:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1680617529; 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=WpVLwoAq9qtmdGpSuk4J6AzFEWCSdDcFUFBCfkBZbDI=; b=U5L6XEdrqPn8txBQjiIjYAQtt0xd+37nLMOS1AOa5Oy695i2Nm1jg5j/AvGcfMS1IQ vssyDznKW6wQpnrNOm/l8W9Ta7fZ8eVFeRValzFGv5PSt3JzwFCviWfiHS3+hHrHCgEp x9jhCSykxYbAEKp1kYRrJTZ4Jv5AfiuUyn0AHu9oaRJXW4a6LHj12FMMYOFjwvaR6rry 91s9RN63Phr8WFVq3nK3Wn5zahXzRv9GP2si9abhldK/B1RQOlZ5VdQssTMDumlEW1hl hU79HKnNjz+J4Hbs6L8JSVqQJ5pAOq2NxxmgvYvkgmkVomRE2hN/bbuYvpZqDLmQeJUo 2iig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680617529; 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=WpVLwoAq9qtmdGpSuk4J6AzFEWCSdDcFUFBCfkBZbDI=; b=d+YG6ejVTyw7OtommzGE/h031d1GpyiScp6yiIYhIm779YgEBX4jjZ0Pdck6+Sk6gc DpbsrTpcL8ycDepE472eP54IlJ9O47KqUVgWt3XwgMdsk7SjNXIm0u4Gc6JSo46UKCj3 RsJKThbiRJI0p7hJ6Mk/r18U5GzMJb6ceUExY/SRdpjMJfUpdE8dVgBlutXxA6QxF9WN jPSXZI6oFX0CZy8ASzivH+V25vF0OHc8lO1sOAPIuv42vxSfQZKsTbPMGuebXfuT0Asx 8ch6O0hPt5bpAKURenZ7qenb7BtFP8l8H3vcSFwZBqBleB1ueCMXg+S8FatarulcdcTC dJww== X-Gm-Message-State: AAQBX9dQ06hVGnlmbwSb0EBCsFSwrY3yU8w9WZF1IIuZlTOuc8tT7lFD EOMyub1yVcC1EPo65kBuTkmtyzvVrBzuuYqZrKlLsA== X-Google-Smtp-Source: AKy350arF3/tuEBSkpG+rMEWIlbFWPUV+NDtpyJMAMAVC3ciIZE/EQ1C1lYJK6aDUIyP/naJPLLsxQ== X-Received: by 2002:a25:f40c:0:b0:b71:fe0f:3e5c with SMTP id q12-20020a25f40c000000b00b71fe0f3e5cmr4359941ybd.2.1680617529035; Tue, 04 Apr 2023 07:12:09 -0700 (PDT) Received: from fedora.attlocal.net (69-109-179-158.lightspeed.dybhfl.sbcglobal.net. [69.109.179.158]) by smtp.gmail.com with ESMTPSA id d195-20020a811dcc000000b00545a08184d8sm3202750ywd.104.2023.04.04.07.12.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Apr 2023 07:12:08 -0700 (PDT) From: William Breathitt Gray To: Jonathan Cameron , Lars-Peter Clausen Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Andy Shevchenko , William Breathitt Gray Subject: [PATCH v4 4/5] iio: addac: stx104: Migrate to the regmap API Date: Tue, 4 Apr 2023 10:12:01 -0400 Message-Id: <5799579711dad9e611f96db7ecfcd26b59338d9f.1680564468.git.william.gray@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org The regmap API supports IO port accessors so we can take advantage of regmap abstractions rather than handling access to the device registers directly in the driver. In addition, to improve code organization in stx104_probe(), the devm_iio_device_register() call is moved above GPIO configuration in order to keep relevant code closer together. Suggested-by: Andy Shevchenko Signed-off-by: William Breathitt Gray --- Changes in v4: - Remove regmap_config max_register lines as superfluous - Utilize BIT() for gain calculation in stx104_read_raw() Changes in v3: - Add STX104_ prefixes to defines to avoid potential name classes - Rename SAME_CHANNEL() to STX104_SINGLE_CHANNEL() to convey intention better - Utilize u8_encode_bits() to define STX104_SOFTWARE_TRIGGER - Adjust to utilize reg_base members in regmap_config structures - Fix off-by-one errors in aio_data_wr_ranges[], aio_data_rd_ranges[], and aio_data_regmap_config max_register - Inline gpio_config initialization to avoid zeroing it at declaration - Add blank lines between register map init blocks for clarity Changes in v2: - Relocate struct stx104_iio for the sake of a clearer patch diff - Replace FIELD_PREP() and FIELD_GET() with u8_encode_bits() and u8_get_bits() drivers/iio/addac/Kconfig | 2 + drivers/iio/addac/stx104.c | 438 ++++++++++++++++++++----------------- 2 files changed, 245 insertions(+), 195 deletions(-) diff --git a/drivers/iio/addac/Kconfig b/drivers/iio/addac/Kconfig index 2843fcb70e24..877f9124803c 100644 --- a/drivers/iio/addac/Kconfig +++ b/drivers/iio/addac/Kconfig @@ -35,7 +35,9 @@ config STX104 tristate "Apex Embedded Systems STX104 driver" depends on PC104 && X86 select ISA_BUS_API + select REGMAP_MMIO select GPIOLIB + select GPIO_REGMAP help Say yes here to build support for the Apex Embedded Systems STX104 integrated analog PC/104 card. diff --git a/drivers/iio/addac/stx104.c b/drivers/iio/addac/stx104.c index 9cc467469dde..f300cce52787 100644 --- a/drivers/iio/addac/stx104.c +++ b/drivers/iio/addac/stx104.c @@ -3,20 +3,19 @@ * IIO driver for the Apex Embedded Systems STX104 * Copyright (C) 2016 William Breathitt Gray */ +#include #include #include -#include -#include +#include +#include #include #include -#include -#include #include #include #include #include #include -#include +#include #include #define STX104_OUT_CHAN(chan) { \ @@ -46,107 +45,207 @@ static unsigned int num_stx104; module_param_hw_array(base, uint, ioport, &num_stx104, 0); MODULE_PARM_DESC(base, "Apex Embedded Systems STX104 base addresses"); -/** - * struct stx104_reg - device register structure - * @ssr_ad: Software Strobe Register and ADC Data - * @achan: ADC Channel - * @dio: Digital I/O - * @dac: DAC Channels - * @cir_asr: Clear Interrupts and ADC Status - * @acr: ADC Control - * @pccr_fsh: Pacer Clock Control and FIFO Status MSB - * @acfg: ADC Configuration - */ -struct stx104_reg { - u16 ssr_ad; - u8 achan; - u8 dio; - u16 dac[2]; - u8 cir_asr; - u8 acr; - u8 pccr_fsh; - u8 acfg; -}; +#define STX104_AIO_BASE 0x0 +#define STX104_SOFTWARE_STROBE STX104_AIO_BASE +#define STX104_ADC_DATA STX104_AIO_BASE +#define STX104_ADC_CHANNEL (STX104_AIO_BASE + 0x2) +#define STX104_DIO_REG (STX104_AIO_BASE + 0x3) +#define STX104_DAC_BASE (STX104_AIO_BASE + 0x4) +#define STX104_ADC_STATUS (STX104_AIO_BASE + 0x8) +#define STX104_ADC_CONTROL (STX104_AIO_BASE + 0x9) +#define STX104_ADC_CONFIGURATION (STX104_AIO_BASE + 0x11) + +#define STX104_AIO_DATA_STRIDE 2 +#define STX104_DAC_OFFSET(_channel) (STX104_DAC_BASE + STX104_AIO_DATA_STRIDE * (_channel)) + +/* ADC Channel */ +#define STX104_FC GENMASK(3, 0) +#define STX104_LC GENMASK(7, 4) +#define STX104_SINGLE_CHANNEL(_channel) \ + (u8_encode_bits(_channel, STX104_FC) | u8_encode_bits(_channel, STX104_LC)) + +/* ADC Status */ +#define STX104_SD BIT(5) +#define STX104_CNV BIT(7) +#define STX104_DIFFERENTIAL 1 + +/* ADC Control */ +#define STX104_ALSS GENMASK(1, 0) +#define STX104_SOFTWARE_TRIGGER u8_encode_bits(0x0, STX104_ALSS) + +/* ADC Configuration */ +#define STX104_GAIN GENMASK(1, 0) +#define STX104_ADBU BIT(2) +#define STX104_BIPOLAR 0 +#define STX104_GAIN_X1 0 +#define STX104_GAIN_X2 1 +#define STX104_GAIN_X4 2 +#define STX104_GAIN_X8 3 /** * struct stx104_iio - IIO device private data structure * @lock: synchronization lock to prevent I/O race conditions - * @chan_out_states: channels' output states - * @reg: I/O address offset for the device registers + * @aio_data_map: Regmap for analog I/O data + * @aio_ctl_map: Regmap for analog I/O control */ struct stx104_iio { struct mutex lock; - unsigned int chan_out_states[STX104_NUM_OUT_CHAN]; - struct stx104_reg __iomem *reg; + struct regmap *aio_data_map; + struct regmap *aio_ctl_map; }; -/** - * struct stx104_gpio - GPIO device private data structure - * @chip: instance of the gpio_chip - * @lock: synchronization lock to prevent I/O race conditions - * @base: base port address of the GPIO device - * @out_state: output bits state - */ -struct stx104_gpio { - struct gpio_chip chip; - spinlock_t lock; - u8 __iomem *base; - unsigned int out_state; +static const struct regmap_range aio_ctl_wr_ranges[] = { + regmap_reg_range(0x0, 0x0), regmap_reg_range(0x2, 0x2), regmap_reg_range(0x9, 0x9), + regmap_reg_range(0x11, 0x11), +}; +static const struct regmap_range aio_ctl_rd_ranges[] = { + regmap_reg_range(0x2, 0x2), regmap_reg_range(0x8, 0x9), regmap_reg_range(0x11, 0x11), +}; +static const struct regmap_range aio_ctl_volatile_ranges[] = { + regmap_reg_range(0x8, 0x8), +}; +static const struct regmap_access_table aio_ctl_wr_table = { + .yes_ranges = aio_ctl_wr_ranges, + .n_yes_ranges = ARRAY_SIZE(aio_ctl_wr_ranges), +}; +static const struct regmap_access_table aio_ctl_rd_table = { + .yes_ranges = aio_ctl_rd_ranges, + .n_yes_ranges = ARRAY_SIZE(aio_ctl_rd_ranges), +}; +static const struct regmap_access_table aio_ctl_volatile_table = { + .yes_ranges = aio_ctl_volatile_ranges, + .n_yes_ranges = ARRAY_SIZE(aio_ctl_volatile_ranges), +}; + +static const struct regmap_config aio_ctl_regmap_config = { + .name = "aio_ctl", + .reg_bits = 8, + .reg_stride = 1, + .reg_base = STX104_AIO_BASE, + .val_bits = 8, + .io_port = true, + .wr_table = &aio_ctl_wr_table, + .rd_table = &aio_ctl_rd_table, + .volatile_table = &aio_ctl_volatile_table, + .cache_type = REGCACHE_FLAT, +}; + +static const struct regmap_range aio_data_wr_ranges[] = { + regmap_reg_range(0x4, 0x6), +}; +static const struct regmap_range aio_data_rd_ranges[] = { + regmap_reg_range(0x0, 0x0), +}; +static const struct regmap_access_table aio_data_wr_table = { + .yes_ranges = aio_data_wr_ranges, + .n_yes_ranges = ARRAY_SIZE(aio_data_wr_ranges), +}; +static const struct regmap_access_table aio_data_rd_table = { + .yes_ranges = aio_data_rd_ranges, + .n_yes_ranges = ARRAY_SIZE(aio_data_rd_ranges), +}; + +static const struct regmap_config aio_data_regmap_config = { + .name = "aio_data", + .reg_bits = 16, + .reg_stride = STX104_AIO_DATA_STRIDE, + .reg_base = STX104_AIO_BASE, + .val_bits = 16, + .io_port = true, + .wr_table = &aio_data_wr_table, + .rd_table = &aio_data_rd_table, + .volatile_table = &aio_data_rd_table, + .cache_type = REGCACHE_FLAT, +}; + +static const struct regmap_config dio_regmap_config = { + .name = "dio", + .reg_bits = 8, + .reg_stride = 1, + .reg_base = STX104_DIO_REG, + .val_bits = 8, + .io_port = true, }; static int stx104_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int *val, int *val2, long mask) { struct stx104_iio *const priv = iio_priv(indio_dev); - struct stx104_reg __iomem *const reg = priv->reg; + int err; unsigned int adc_config; - int adbu; - int gain; + unsigned int value; + unsigned int adc_status; switch (mask) { case IIO_CHAN_INFO_HARDWAREGAIN: - /* get gain configuration */ - adc_config = ioread8(®->acfg); - gain = adc_config & 0x3; + err = regmap_read(priv->aio_ctl_map, STX104_ADC_CONFIGURATION, &adc_config); + if (err) + return err; - *val = 1 << gain; + *val = BIT(u8_get_bits(adc_config, STX104_GAIN)); return IIO_VAL_INT; case IIO_CHAN_INFO_RAW: if (chan->output) { - *val = priv->chan_out_states[chan->channel]; + err = regmap_read(priv->aio_data_map, STX104_DAC_OFFSET(chan->channel), + &value); + if (err) + return err; + *val = value; return IIO_VAL_INT; } mutex_lock(&priv->lock); /* select ADC channel */ - iowrite8(chan->channel | (chan->channel << 4), ®->achan); + err = regmap_write(priv->aio_ctl_map, STX104_ADC_CHANNEL, + STX104_SINGLE_CHANNEL(chan->channel)); + if (err) { + mutex_unlock(&priv->lock); + return err; + } /* trigger ADC sample capture by writing to the 8-bit * Software Strobe Register and wait for completion */ - iowrite8(0, ®->ssr_ad); - while (ioread8(®->cir_asr) & BIT(7)); - - *val = ioread16(®->ssr_ad); + err = regmap_write(priv->aio_ctl_map, STX104_SOFTWARE_STROBE, 0); + if (err) { + mutex_unlock(&priv->lock); + return err; + } + do { + err = regmap_read(priv->aio_ctl_map, STX104_ADC_STATUS, &adc_status); + if (err) { + mutex_unlock(&priv->lock); + return err; + } + } while (u8_get_bits(adc_status, STX104_CNV)); + + err = regmap_read(priv->aio_data_map, STX104_ADC_DATA, &value); + if (err) { + mutex_unlock(&priv->lock); + return err; + } + *val = value; mutex_unlock(&priv->lock); return IIO_VAL_INT; case IIO_CHAN_INFO_OFFSET: /* get ADC bipolar/unipolar configuration */ - adc_config = ioread8(®->acfg); - adbu = !(adc_config & BIT(2)); + err = regmap_read(priv->aio_ctl_map, STX104_ADC_CONFIGURATION, &adc_config); + if (err) + return err; - *val = -32768 * adbu; + *val = (u8_get_bits(adc_config, STX104_ADBU) == STX104_BIPOLAR) ? -32768 : 0; return IIO_VAL_INT; case IIO_CHAN_INFO_SCALE: /* get ADC bipolar/unipolar and gain configuration */ - adc_config = ioread8(®->acfg); - adbu = !(adc_config & BIT(2)); - gain = adc_config & 0x3; + err = regmap_read(priv->aio_ctl_map, STX104_ADC_CONFIGURATION, &adc_config); + if (err) + return err; *val = 5; - *val2 = 15 - adbu + gain; + *val2 = (u8_get_bits(adc_config, STX104_ADBU) == STX104_BIPOLAR) ? 14 : 15; + *val2 += u8_get_bits(adc_config, STX104_GAIN); return IIO_VAL_FRACTIONAL_LOG2; } @@ -157,28 +256,29 @@ static int stx104_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int val, int val2, long mask) { struct stx104_iio *const priv = iio_priv(indio_dev); + u8 gain; switch (mask) { case IIO_CHAN_INFO_HARDWAREGAIN: /* Only four gain states (x1, x2, x4, x8) */ switch (val) { case 1: - iowrite8(0, &priv->reg->acfg); + gain = STX104_GAIN_X1; break; case 2: - iowrite8(1, &priv->reg->acfg); + gain = STX104_GAIN_X2; break; case 4: - iowrite8(2, &priv->reg->acfg); + gain = STX104_GAIN_X4; break; case 8: - iowrite8(3, &priv->reg->acfg); + gain = STX104_GAIN_X8; break; default: return -EINVAL; } - return 0; + return regmap_write(priv->aio_ctl_map, STX104_ADC_CONFIGURATION, gain); case IIO_CHAN_INFO_RAW: if (!chan->output) return -EINVAL; @@ -187,13 +287,7 @@ static int stx104_write_raw(struct iio_dev *indio_dev, if ((unsigned int)val > 65535) return -EINVAL; - mutex_lock(&priv->lock); - - priv->chan_out_states[chan->channel] = val; - iowrite16(val, &priv->reg->dac[chan->channel]); - - mutex_unlock(&priv->lock); - return 0; + return regmap_write(priv->aio_data_map, STX104_DAC_OFFSET(chan->channel), val); } return -EINVAL; @@ -222,119 +316,66 @@ static const struct iio_chan_spec stx104_channels_diff[] = { STX104_IN_CHAN(6, 1), STX104_IN_CHAN(7, 1) }; -static int stx104_gpio_get_direction(struct gpio_chip *chip, - unsigned int offset) -{ - /* GPIO 0-3 are input only, while the rest are output only */ - if (offset < 4) - return 1; - - return 0; -} - -static int stx104_gpio_direction_input(struct gpio_chip *chip, - unsigned int offset) -{ - if (offset >= 4) - return -EINVAL; - - return 0; -} - -static int stx104_gpio_direction_output(struct gpio_chip *chip, - unsigned int offset, int value) +static int stx104_reg_mask_xlate(struct gpio_regmap *const gpio, const unsigned int base, + unsigned int offset, unsigned int *const reg, + unsigned int *const mask) { - if (offset < 4) - return -EINVAL; - - chip->set(chip, offset, value); - return 0; -} - -static int stx104_gpio_get(struct gpio_chip *chip, unsigned int offset) -{ - struct stx104_gpio *const stx104gpio = gpiochip_get_data(chip); - + /* Output lines are located at same register bit offsets as input lines */ if (offset >= 4) - return -EINVAL; + offset -= 4; - return !!(ioread8(stx104gpio->base) & BIT(offset)); -} - -static int stx104_gpio_get_multiple(struct gpio_chip *chip, unsigned long *mask, - unsigned long *bits) -{ - struct stx104_gpio *const stx104gpio = gpiochip_get_data(chip); - - *bits = ioread8(stx104gpio->base); + *reg = base; + *mask = BIT(offset); return 0; } -static void stx104_gpio_set(struct gpio_chip *chip, unsigned int offset, - int value) -{ - struct stx104_gpio *const stx104gpio = gpiochip_get_data(chip); - const unsigned int mask = BIT(offset) >> 4; - unsigned long flags; - - if (offset < 4) - return; - - spin_lock_irqsave(&stx104gpio->lock, flags); - - if (value) - stx104gpio->out_state |= mask; - else - stx104gpio->out_state &= ~mask; - - iowrite8(stx104gpio->out_state, stx104gpio->base); - - spin_unlock_irqrestore(&stx104gpio->lock, flags); -} - #define STX104_NGPIO 8 static const char *stx104_names[STX104_NGPIO] = { "DIN0", "DIN1", "DIN2", "DIN3", "DOUT0", "DOUT1", "DOUT2", "DOUT3" }; -static void stx104_gpio_set_multiple(struct gpio_chip *chip, - unsigned long *mask, unsigned long *bits) +static int stx104_init_hw(struct stx104_iio *const priv) { - struct stx104_gpio *const stx104gpio = gpiochip_get_data(chip); - unsigned long flags; - - /* verify masked GPIO are output */ - if (!(*mask & 0xF0)) - return; + int err; - *mask >>= 4; - *bits >>= 4; + /* configure device for software trigger operation */ + err = regmap_write(priv->aio_ctl_map, STX104_ADC_CONTROL, STX104_SOFTWARE_TRIGGER); + if (err) + return err; - spin_lock_irqsave(&stx104gpio->lock, flags); + /* initialize gain setting to x1 */ + err = regmap_write(priv->aio_ctl_map, STX104_ADC_CONFIGURATION, STX104_GAIN_X1); + if (err) + return err; - stx104gpio->out_state &= ~*mask; - stx104gpio->out_state |= *mask & *bits; - iowrite8(stx104gpio->out_state, stx104gpio->base); + /* initialize DAC outputs to 0V */ + err = regmap_write(priv->aio_data_map, STX104_DAC_BASE, 0); + if (err) + return err; + err = regmap_write(priv->aio_data_map, STX104_DAC_BASE + STX104_AIO_DATA_STRIDE, 0); + if (err) + return err; - spin_unlock_irqrestore(&stx104gpio->lock, flags); + return 0; } static int stx104_probe(struct device *dev, unsigned int id) { struct iio_dev *indio_dev; struct stx104_iio *priv; - struct stx104_gpio *stx104gpio; + struct gpio_regmap_config gpio_config; + void __iomem *stx104_base; + struct regmap *aio_ctl_map; + struct regmap *aio_data_map; + struct regmap *dio_map; int err; + unsigned int adc_status; indio_dev = devm_iio_device_alloc(dev, sizeof(*priv)); if (!indio_dev) return -ENOMEM; - stx104gpio = devm_kzalloc(dev, sizeof(*stx104gpio), GFP_KERNEL); - if (!stx104gpio) - return -ENOMEM; - if (!devm_request_region(dev, base[id], STX104_EXTENT, dev_name(dev))) { dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n", @@ -342,16 +383,37 @@ static int stx104_probe(struct device *dev, unsigned int id) return -EBUSY; } - priv = iio_priv(indio_dev); - priv->reg = devm_ioport_map(dev, base[id], STX104_EXTENT); - if (!priv->reg) + stx104_base = devm_ioport_map(dev, base[id], STX104_EXTENT); + if (!stx104_base) return -ENOMEM; + aio_ctl_map = devm_regmap_init_mmio(dev, stx104_base, &aio_ctl_regmap_config); + if (IS_ERR(aio_ctl_map)) + return dev_err_probe(dev, PTR_ERR(aio_ctl_map), + "Unable to initialize aio_ctl register map\n"); + + aio_data_map = devm_regmap_init_mmio(dev, stx104_base, &aio_data_regmap_config); + if (IS_ERR(aio_data_map)) + return dev_err_probe(dev, PTR_ERR(aio_data_map), + "Unable to initialize aio_data register map\n"); + + dio_map = devm_regmap_init_mmio(dev, stx104_base, &dio_regmap_config); + if (IS_ERR(dio_map)) + return dev_err_probe(dev, PTR_ERR(dio_map), + "Unable to initialize dio register map\n"); + + priv = iio_priv(indio_dev); + priv->aio_ctl_map = aio_ctl_map; + priv->aio_data_map = aio_data_map; + indio_dev->info = &stx104_info; indio_dev->modes = INDIO_DIRECT_MODE; - /* determine if differential inputs */ - if (ioread8(&priv->reg->cir_asr) & BIT(5)) { + err = regmap_read(aio_ctl_map, STX104_ADC_STATUS, &adc_status); + if (err) + return err; + + if (u8_get_bits(adc_status, STX104_SD) == STX104_DIFFERENTIAL) { indio_dev->num_channels = ARRAY_SIZE(stx104_channels_diff); indio_dev->channels = stx104_channels_diff; } else { @@ -363,41 +425,27 @@ static int stx104_probe(struct device *dev, unsigned int id) mutex_init(&priv->lock); - /* configure device for software trigger operation */ - iowrite8(0, &priv->reg->acr); + err = stx104_init_hw(priv); + if (err) + return err; - /* initialize gain setting to x1 */ - iowrite8(0, &priv->reg->acfg); - - /* initialize DAC output to 0V */ - iowrite16(0, &priv->reg->dac[0]); - iowrite16(0, &priv->reg->dac[1]); - - stx104gpio->chip.label = dev_name(dev); - stx104gpio->chip.parent = dev; - stx104gpio->chip.owner = THIS_MODULE; - stx104gpio->chip.base = -1; - stx104gpio->chip.ngpio = STX104_NGPIO; - stx104gpio->chip.names = stx104_names; - stx104gpio->chip.get_direction = stx104_gpio_get_direction; - stx104gpio->chip.direction_input = stx104_gpio_direction_input; - stx104gpio->chip.direction_output = stx104_gpio_direction_output; - stx104gpio->chip.get = stx104_gpio_get; - stx104gpio->chip.get_multiple = stx104_gpio_get_multiple; - stx104gpio->chip.set = stx104_gpio_set; - stx104gpio->chip.set_multiple = stx104_gpio_set_multiple; - stx104gpio->base = &priv->reg->dio; - stx104gpio->out_state = 0x0; - - spin_lock_init(&stx104gpio->lock); - - err = devm_gpiochip_add_data(dev, &stx104gpio->chip, stx104gpio); - if (err) { - dev_err(dev, "GPIO registering failed (%d)\n", err); + err = devm_iio_device_register(dev, indio_dev); + if (err) return err; - } - return devm_iio_device_register(dev, indio_dev); + gpio_config = (struct gpio_regmap_config) { + .parent = dev, + .regmap = dio_map, + .ngpio = STX104_NGPIO, + .names = stx104_names, + .reg_dat_base = GPIO_REGMAP_ADDR(STX104_DIO_REG), + .reg_set_base = GPIO_REGMAP_ADDR(STX104_DIO_REG), + .ngpio_per_reg = STX104_NGPIO, + .reg_mask_xlate = stx104_reg_mask_xlate, + .drvdata = dio_map, + }; + + return PTR_ERR_OR_ZERO(devm_gpio_regmap_register(dev, &gpio_config)); } static struct isa_driver stx104_driver = { From patchwork Tue Apr 4 14:12:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: William Breathitt Gray X-Patchwork-Id: 13200267 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 3EF9CC77B62 for ; Tue, 4 Apr 2023 14:12:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235769AbjDDOMS (ORCPT ); Tue, 4 Apr 2023 10:12:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33992 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235739AbjDDOMO (ORCPT ); Tue, 4 Apr 2023 10:12:14 -0400 Received: from mail-yb1-xb29.google.com (mail-yb1-xb29.google.com [IPv6:2607:f8b0:4864:20::b29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EA8903ABD for ; Tue, 4 Apr 2023 07:12:10 -0700 (PDT) Received: by mail-yb1-xb29.google.com with SMTP id m16so18497510ybk.0 for ; Tue, 04 Apr 2023 07:12:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1680617530; 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=qDg8gjgCK4cCzQVwhhCZvTIORe+B4i8BW2+BbUTMc8k=; b=DiobnlWTCEyBk4WPYFAHKg30bRzkskmSJ72Wcz0czZXBfiyEDWRHKO9j7HFFgmztHX xTXtdNMXXSAw/AMwjYmqyhquuzlygHqo1/eOGOM8k1x+2K95ixzBaZ3AzQVnLBYkVssL is9Dub6pRZJl9zSbNIakbfXudTP0IXFFXRKemRvFcXaYEfRE+jMAgdIu9rqNCsCKcqH+ NH2mMYvnftLWNjIjTz9qQEeVS04uRRKlmWRk2lVyO526Mp6wwBPvwvi7OOh9JxSVRmAv Dq3fsXDYkQoXuTcX4jJE2kPlWqfV+GN7mu/Hnmvpj4yC8MNr7lkMYkVANC/PFdK8DxEG jVTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680617530; 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=qDg8gjgCK4cCzQVwhhCZvTIORe+B4i8BW2+BbUTMc8k=; b=QRizWtuJI63+DxCdoH3nMGVUiHjPgcw1k++BX199yq7p4cH4qYFVZXMDaz6PRk+BmZ MDXUzs021HL85r+L7xfzxuVRMQp8Qc77R6nIVKFmUPGK83xB2sEY/q95kgHaFDff6cod K7Als+ENQidouydFd3mqclkFhctXUIu39f5QZeGkkIwBVdt0d/WWUcfKSMKDKryPywG+ 54H57f7WyKylUk5g3jMi/cZLj0cOxaypa7bWYh9WL+o3pBTeKrRy8YMG4++oHCxCAa6H ieGZwT610CbImFp6FdAyJouuARYsVkqls4q/NSEoqGotzuQxpCZmm/6G0osUUWX2DQ0k Jh9g== X-Gm-Message-State: AAQBX9cCp3X9NRJJU0WXnH5qsQNQBgTyp0OBlKeqRxuYYJtkDo4q3Khd K1J3xvf+MG7UcfLlWM1d2Vtxdw== X-Google-Smtp-Source: AKy350aJGwj3RDu4Ip5Xdz/OSHSTb1FUZElsBmHZwE/xAe13jchQE1CQ2cXbPFPEjl7DmWZ7kx4QeA== X-Received: by 2002:a25:aa53:0:b0:b05:af93:c3d7 with SMTP id s77-20020a25aa53000000b00b05af93c3d7mr2826487ybi.62.1680617529787; Tue, 04 Apr 2023 07:12:09 -0700 (PDT) Received: from fedora.attlocal.net (69-109-179-158.lightspeed.dybhfl.sbcglobal.net. [69.109.179.158]) by smtp.gmail.com with ESMTPSA id d195-20020a811dcc000000b00545a08184d8sm3202750ywd.104.2023.04.04.07.12.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Apr 2023 07:12:09 -0700 (PDT) From: William Breathitt Gray To: Jonathan Cameron , Lars-Peter Clausen Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Andy Shevchenko , William Breathitt Gray Subject: [PATCH v4 5/5] iio: addac: stx104: Use regmap_read_poll_timeout() for conversion poll Date: Tue, 4 Apr 2023 10:12:02 -0400 Message-Id: X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org ADC sample captures take a certain amount of time to complete after initiated; this conversion time range can be anywhere from 5 uSec to 53.68 Seconds depending on the configuration of the Analog Input Frame Timer register. When the conversion is in progress, the ADC Status register CNV bit is high. Utilize regmap_read_poll_timeout() to poll until the ADC conversion is completed (or timeout if more than 53.68 Seconds passes). Suggested-by: Jonathan Cameron Signed-off-by: William Breathitt Gray --- Changes in v4: none drivers/iio/addac/stx104.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/drivers/iio/addac/stx104.c b/drivers/iio/addac/stx104.c index f300cce52787..0cdb824b6bb6 100644 --- a/drivers/iio/addac/stx104.c +++ b/drivers/iio/addac/stx104.c @@ -206,19 +206,22 @@ static int stx104_read_raw(struct iio_dev *indio_dev, /* trigger ADC sample capture by writing to the 8-bit * Software Strobe Register and wait for completion + * Range is 5 uSec to 53.68 Seconds in steps of 25 nanoseconds. + * The actual Analog Input Frame Timer time interval is calculated as: + * ai_time_frame_ns = ( AIFT + 1 ) * ( 25 nSec ). + * Where 0 <= AIFT <= 2147483648. */ err = regmap_write(priv->aio_ctl_map, STX104_SOFTWARE_STROBE, 0); if (err) { mutex_unlock(&priv->lock); return err; } - do { - err = regmap_read(priv->aio_ctl_map, STX104_ADC_STATUS, &adc_status); - if (err) { - mutex_unlock(&priv->lock); - return err; - } - } while (u8_get_bits(adc_status, STX104_CNV)); + err = regmap_read_poll_timeout(priv->aio_ctl_map, STX104_ADC_STATUS, adc_status, + !u8_get_bits(adc_status, STX104_CNV), 0, 53687092); + if (err) { + mutex_unlock(&priv->lock); + return err; + } err = regmap_read(priv->aio_data_map, STX104_ADC_DATA, &value); if (err) {