From nobody Tue Apr 28 05:05:46 2026 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 15FC7C433EF for ; Mon, 6 Jun 2022 14:15:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239465AbiFFOPk (ORCPT ); Mon, 6 Jun 2022 10:15:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54026 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239445AbiFFOPd (ORCPT ); Mon, 6 Jun 2022 10:15:33 -0400 Received: from mail-qk1-x730.google.com (mail-qk1-x730.google.com [IPv6:2607:f8b0:4864:20::730]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BDF382CDCE for ; Mon, 6 Jun 2022 07:15:28 -0700 (PDT) Received: by mail-qk1-x730.google.com with SMTP id bi27so4363426qkb.10 for ; Mon, 06 Jun 2022 07:15:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=kHombqk3VYPwKde0y6W4IYyIm10RdpNnxO/ZPqQ0Sxs=; b=jtwcDJB56TkN8p8A7JM8buPO6gQUo/XgKrVsHo4iM2y+ufJaPxfc4ArTRJI9jbhIXz qI3phv7gG9K76q4/HkPcDEwerfmS5T/+Bk9KMjRH5XDJZ9mNYQxH2WUHEsgX/cFliVu6 y3gn9FazsXJ7dUGvt27M0LtpJMQUGQx3Bs/fCuJRhzyTOSsxP5rV4RNIeOvy+LprHxJS wSWcp5s3hKVUajPANqKtkC4HLSU5ILaKLFHe+W4IdVmN5O3rKkBNLI/mi3VCRBvmU+MW SRjGi9yT5AQBQA+w1TMIHmpMw0t4SNjDqMwWXftLVZb/7bNAB/l3QM13+rGArmrTUs1O M89Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=kHombqk3VYPwKde0y6W4IYyIm10RdpNnxO/ZPqQ0Sxs=; b=EWW2AdVyIv2pdQY3QC0PQTlqIBGYLRgNlQd2NJxK54Urh4cdur3FA3kFWMK0FiYJjh 4bdNv+GCrQ2jqmkzT3VO/loZ2vgyvUs9b1cXvsPwMMPUQLR8bQlRXLeoHvje2jaKlW4g oBWGw+Li61aE40h7JXGbUFd0j1psTvakN7jj/8D2aF9jgbriAH3s6mw/ZKaM+e1he2MI H1XP94/su8GW6xGVqNqiQxX/MltM6JDGwxsQ/XTqFxU7QLNU/SlRoEE4AlhAViK5g3xw Q3YzEF9ebN8ld6eu7AanguoqVV53aapWpw5uOkVEAa/bYVe5pjkOoocLdIWBDlRlwK0M Lx6Q== X-Gm-Message-State: AOAM5338b6NZ3Erj+ds8/4WCzg8of3W1jm2Ed/+397grBGl5nOZvDICM fDZlg5PDu+fVDenLgpsM5RQXiQ== X-Google-Smtp-Source: ABdhPJzxFp5PvNMb/dA5xI22yRoY4ap7OODLm7toYUJxtZU5yxPmVLHG7YjDJzfEE4uI1G1hHLM1sQ== X-Received: by 2002:a05:620a:1532:b0:6a6:9647:8d6d with SMTP id n18-20020a05620a153200b006a696478d6dmr11729144qkk.413.1654524927744; Mon, 06 Jun 2022 07:15:27 -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 c15-20020a05620a268f00b006a3af1bd183sm10335086qkp.127.2022.06.06.07.15.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Jun 2022 07:15:27 -0700 (PDT) From: William Breathitt Gray To: jic23@kernel.org Cc: lars@metafoo.de, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, William Breathitt Gray Subject: [PATCH 1/2] iio: adc: stx104: Implement and utilize register structures Date: Mon, 6 Jun 2022 10:15:17 -0400 Message-Id: X-Mailer: git-send-email 2.36.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Reduce magic numbers and improve code readability by implementing and utilizing named register data structures. Signed-off-by: William Breathitt Gray --- drivers/iio/adc/stx104.c | 70 +++++++++++++++++++++++++++------------- 1 file changed, 47 insertions(+), 23 deletions(-) diff --git a/drivers/iio/adc/stx104.c b/drivers/iio/adc/stx104.c index 7552351bfed9..7656b363e281 100644 --- a/drivers/iio/adc/stx104.c +++ b/drivers/iio/adc/stx104.c @@ -16,6 +16,7 @@ #include #include #include +#include =20 #define STX104_OUT_CHAN(chan) { \ .type =3D IIO_VOLTAGE, \ @@ -44,14 +45,36 @@ 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"); =20 +/** + * struct stx104_reg - device register structure + * @ad: 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 ad; + u8 achan; + u8 dio; + u16 dac[2]; + u8 cir_asr; + u8 acr; + u8 pccr_fsh; + u8 acfg; +}; + /** * struct stx104_iio - IIO device private data structure * @chan_out_states: channels' output states - * @base: base port address of the IIO device + * @reg: I/O address offset for the device registers */ struct stx104_iio { unsigned int chan_out_states[STX104_NUM_OUT_CHAN]; - void __iomem *base; + struct stx104_reg __iomem *reg; }; =20 /** @@ -64,7 +87,7 @@ struct stx104_iio { struct stx104_gpio { struct gpio_chip chip; spinlock_t lock; - void __iomem *base; + u8 __iomem *base; unsigned int out_state; }; =20 @@ -72,6 +95,7 @@ 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 =3D iio_priv(indio_dev); + struct stx104_reg __iomem *const reg =3D priv->reg; unsigned int adc_config; int adbu; int gain; @@ -79,7 +103,7 @@ static int stx104_read_raw(struct iio_dev *indio_dev, switch (mask) { case IIO_CHAN_INFO_HARDWAREGAIN: /* get gain configuration */ - adc_config =3D ioread8(priv->base + 11); + adc_config =3D ioread8(®->acfg); gain =3D adc_config & 0x3; =20 *val =3D 1 << gain; @@ -91,24 +115,24 @@ static int stx104_read_raw(struct iio_dev *indio_dev, } =20 /* select ADC channel */ - iowrite8(chan->channel | (chan->channel << 4), priv->base + 2); + iowrite8(chan->channel | (chan->channel << 4), ®->achan); =20 /* trigger ADC sample capture and wait for completion */ - iowrite8(0, priv->base); - while (ioread8(priv->base + 8) & BIT(7)); + iowrite8(0, ®->ad); + while (ioread8(®->cir_asr) & BIT(7)); =20 - *val =3D ioread16(priv->base); + *val =3D ioread16(®->ad); return IIO_VAL_INT; case IIO_CHAN_INFO_OFFSET: /* get ADC bipolar/unipolar configuration */ - adc_config =3D ioread8(priv->base + 11); + adc_config =3D ioread8(®->acfg); adbu =3D !(adc_config & BIT(2)); =20 *val =3D -32768 * adbu; return IIO_VAL_INT; case IIO_CHAN_INFO_SCALE: /* get ADC bipolar/unipolar and gain configuration */ - adc_config =3D ioread8(priv->base + 11); + adc_config =3D ioread8(®->acfg); adbu =3D !(adc_config & BIT(2)); gain =3D adc_config & 0x3; =20 @@ -130,16 +154,16 @@ static int stx104_write_raw(struct iio_dev *indio_dev, /* Only four gain states (x1, x2, x4, x8) */ switch (val) { case 1: - iowrite8(0, priv->base + 11); + iowrite8(0, &priv->reg->acfg); break; case 2: - iowrite8(1, priv->base + 11); + iowrite8(1, &priv->reg->acfg); break; case 4: - iowrite8(2, priv->base + 11); + iowrite8(2, &priv->reg->acfg); break; case 8: - iowrite8(3, priv->base + 11); + iowrite8(3, &priv->reg->acfg); break; default: return -EINVAL; @@ -153,7 +177,7 @@ static int stx104_write_raw(struct iio_dev *indio_dev, return -EINVAL; =20 priv->chan_out_states[chan->channel] =3D val; - iowrite16(val, priv->base + 4 + 2 * chan->channel); + iowrite16(val, priv->reg->dac + chan->channel); =20 return 0; } @@ -307,15 +331,15 @@ static int stx104_probe(struct device *dev, unsigned = int id) } =20 priv =3D iio_priv(indio_dev); - priv->base =3D devm_ioport_map(dev, base[id], STX104_EXTENT); - if (!priv->base) + priv->reg =3D devm_ioport_map(dev, base[id], STX104_EXTENT); + if (!priv->reg) return -ENOMEM; =20 indio_dev->info =3D &stx104_info; indio_dev->modes =3D INDIO_DIRECT_MODE; =20 /* determine if differential inputs */ - if (ioread8(priv->base + 8) & BIT(5)) { + if (ioread8(&priv->reg->cir_asr) & BIT(5)) { indio_dev->num_channels =3D ARRAY_SIZE(stx104_channels_diff); indio_dev->channels =3D stx104_channels_diff; } else { @@ -326,14 +350,14 @@ static int stx104_probe(struct device *dev, unsigned = int id) indio_dev->name =3D dev_name(dev); =20 /* configure device for software trigger operation */ - iowrite8(0, priv->base + 9); + iowrite8(0, &priv->reg->acr); =20 /* initialize gain setting to x1 */ - iowrite8(0, priv->base + 11); + iowrite8(0, &priv->reg->acfg); =20 /* initialize DAC output to 0V */ - iowrite16(0, priv->base + 4); - iowrite16(0, priv->base + 6); + iowrite16(0, &priv->reg->dac[0]); + iowrite16(0, &priv->reg->dac[1]); =20 stx104gpio->chip.label =3D dev_name(dev); stx104gpio->chip.parent =3D dev; @@ -348,7 +372,7 @@ static int stx104_probe(struct device *dev, unsigned in= t id) stx104gpio->chip.get_multiple =3D stx104_gpio_get_multiple; stx104gpio->chip.set =3D stx104_gpio_set; stx104gpio->chip.set_multiple =3D stx104_gpio_set_multiple; - stx104gpio->base =3D priv->base + 3; + stx104gpio->base =3D &priv->reg->dio; stx104gpio->out_state =3D 0x0; =20 spin_lock_init(&stx104gpio->lock); --=20 2.36.1 From nobody Tue Apr 28 05:05:46 2026 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 A82E7C433EF for ; Mon, 6 Jun 2022 14:16:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239490AbiFFOPq (ORCPT ); Mon, 6 Jun 2022 10:15:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54792 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239467AbiFFOPg (ORCPT ); Mon, 6 Jun 2022 10:15:36 -0400 Received: from mail-qt1-x82d.google.com (mail-qt1-x82d.google.com [IPv6:2607:f8b0:4864:20::82d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 116F92D1C3 for ; Mon, 6 Jun 2022 07:15:30 -0700 (PDT) Received: by mail-qt1-x82d.google.com with SMTP id k4so3814611qth.8 for ; Mon, 06 Jun 2022 07:15:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=wh/GNYrTUWQwS2LY1J4/sKG7u3eGX16BxrYgrkihQrQ=; b=QlWo3+198cSiHHs+hq+/W8YxainPD4zxjEMFT1ARc2tIYSNGvNt3Czp56VcDcE7xXE Mg9hV9nupxeWMeN8L15uWBA49GKTqKuODxtAKZ8ls1sWXnw8gRfOijNfjGpikRw3zFG3 OtO3GEyuWJRJ0OZow3PTL5Icch4gcYIgkS7kagTPOxjkzm/c7tf++I7UrCKi0gxztNHd uxToyHm2I2Olw6nL38SZkEMvRxaaWq9rxJkAQy29RlTRhsk9DKxdQI1YNqLC7tyxm8v2 9C3aQ3mf0EJr/U32mY9jmhbAWIEISh09sGC7+4Aso3hgirf3UcNkpZtUfttrrVvDyclv hZCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=wh/GNYrTUWQwS2LY1J4/sKG7u3eGX16BxrYgrkihQrQ=; b=y1K93ksi2yoFwZbGWfZSyi+vftEhyvg9OL8JstMkX84B1E7MSaCAXsfhL4IivRqBKs fHA2nT+iJuVfwbIgt0h1fjaFLETRMKkYYKXiY6bA+gUulPYivR1BEN1MIm7EtpOh2fKm /tl9OTGlDbZSJ17h0ntae8K/A97GRW10lza/TK5zibivKoLvqbTNveJiwJQmSP9CsEx2 S47GtPQiW2ZRO0W7YrH/GXE4/8ONjX6ziEF49gmFMBP4WZEVB12w/4s3MNl51XCMUwkl yFRnVZYJI9FmXEBDdghrmaEfNiTZc1+CqawzGFpgm2j9HTTJwxpPhkMCjuvxjHeJiQdd Iepg== X-Gm-Message-State: AOAM531pHexa9+cmI5C6IGIIMIveGI9CD5Q2HguEDeOYU9LIu3O9siBE YScNNr+YhnhAYluDFhogmpxiVg== X-Google-Smtp-Source: ABdhPJxQRDEeEokM9Fo2uYlhzVajgwwVOYqYmd5G8pZo9R7X+FPuurNmOePJdMhP9bv+SVtjr62Ymw== X-Received: by 2002:ac8:4e54:0:b0:304:bf64:1356 with SMTP id e20-20020ac84e54000000b00304bf641356mr18126504qtw.547.1654524929070; Mon, 06 Jun 2022 07:15:29 -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 c15-20020a05620a268f00b006a3af1bd183sm10335086qkp.127.2022.06.06.07.15.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Jun 2022 07:15:28 -0700 (PDT) From: William Breathitt Gray To: jic23@kernel.org Cc: lars@metafoo.de, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, William Breathitt Gray Subject: [PATCH 2/2] iio: dac: cio-dac: Implement and utilize register structures Date: Mon, 6 Jun 2022 10:15:18 -0400 Message-Id: <44aec703753f930cceff448babd1c8e2959eebb0.1654118389.git.william.gray@linaro.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Reduce magic numbers and improve code readability by implementing and utilizing named register data structures. Signed-off-by: William Breathitt Gray --- drivers/iio/dac/cio-dac.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/drivers/iio/dac/cio-dac.c b/drivers/iio/dac/cio-dac.c index 8080984dcb03..7860450ceaf3 100644 --- a/drivers/iio/dac/cio-dac.c +++ b/drivers/iio/dac/cio-dac.c @@ -16,6 +16,7 @@ #include #include #include +#include =20 #define CIO_DAC_NUM_CHAN 16 =20 @@ -34,14 +35,22 @@ static unsigned int num_cio_dac; module_param_hw_array(base, uint, ioport, &num_cio_dac, 0); MODULE_PARM_DESC(base, "Measurement Computing CIO-DAC base addresses"); =20 +/** + * struct cio_dac_reg - device register structure + * @da: D/A data + */ +struct cio_dac_reg { + u16 da[CIO_DAC_NUM_CHAN]; +}; + /** * struct cio_dac_iio - IIO device private data structure * @chan_out_states: channels' output states - * @base: base port address of the IIO device + * @reg: I/O address offset for the device registers */ struct cio_dac_iio { int chan_out_states[CIO_DAC_NUM_CHAN]; - void __iomem *base; + struct cio_dac_reg __iomem *reg; }; =20 static int cio_dac_read_raw(struct iio_dev *indio_dev, @@ -61,7 +70,6 @@ static int cio_dac_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int val, int val2, long mask) { struct cio_dac_iio *const priv =3D iio_priv(indio_dev); - const unsigned int chan_addr_offset =3D 2 * chan->channel; =20 if (mask !=3D IIO_CHAN_INFO_RAW) return -EINVAL; @@ -71,7 +79,7 @@ static int cio_dac_write_raw(struct iio_dev *indio_dev, return -EINVAL; =20 priv->chan_out_states[chan->channel] =3D val; - iowrite16(val, priv->base + chan_addr_offset); + iowrite16(val, priv->reg->da + chan->channel); =20 return 0; } @@ -106,8 +114,8 @@ static int cio_dac_probe(struct device *dev, unsigned i= nt id) } =20 priv =3D iio_priv(indio_dev); - priv->base =3D devm_ioport_map(dev, base[id], CIO_DAC_EXTENT); - if (!priv->base) + priv->reg =3D devm_ioport_map(dev, base[id], CIO_DAC_EXTENT); + if (!priv->reg) return -ENOMEM; =20 indio_dev->info =3D &cio_dac_info; @@ -117,8 +125,8 @@ static int cio_dac_probe(struct device *dev, unsigned i= nt id) indio_dev->name =3D dev_name(dev); =20 /* initialize DAC outputs to 0V */ - for (i =3D 0; i < 32; i +=3D 2) - iowrite16(0, priv->base + i); + for (i =3D 0; i < CIO_DAC_NUM_CHAN; i++) + iowrite16(0, priv->reg->da + i); =20 return devm_iio_device_register(dev, indio_dev); } --=20 2.36.1