Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp1348994iog; Tue, 14 Jun 2022 04:29:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwjvtJAFh+G4qRbtxpGDVsRSL0vtzgi9GqgkpS6G6ZotE1MwciWxKjUch79iKz2RJ1UWc02 X-Received: by 2002:aa7:c952:0:b0:434:edcc:f247 with SMTP id h18-20020aa7c952000000b00434edccf247mr5388622edt.412.1655206146725; Tue, 14 Jun 2022 04:29:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655206146; cv=none; d=google.com; s=arc-20160816; b=yIijS+wpFBN7bNOD7tOQcKNIn+So8OrxMzztniwkv2b80g3zEdeEcMCQlkfoosaStK QOI3LYiWHNP3Tx1NMBGtrJsIRWnmiJjjin0uHdT3bRZGpRzHZmxCQbRyPE94KdEe2CPi rMjLkUg1CWk9Hy9JJZqV00l5i3Kw0PpkFMsYCKZV80gV8+rAZC/3YWDfnnu/0UtMZRIu 09cTh4oEWSGFGPZ8qzPkLujZZpPGecM6vU2m1aUCqmmGpOTzzvpDVIomWyIha8ao0Su3 WQE9e4VFo21gCxKEwTjTk7nfoe99o4oWfghJLT+2FflS6UToQtPbvefwWtXLdHdHBhOH 22lg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=v/577Y7IJ8Xz9VQq2RMiRbkj4WuM1JYMY8TQO/GSHvM=; b=zmaKJJ+CxRvlsMNtlGJ6nK3q6q0veklwt7/1fEhdPuIJndzAe8nNUKF2Vdn+Aao1o1 xlp3lbnWKBV2AY24l5p4sJOUfhnwgRCciNV/kWGwE02B7nhbrvkwGJHeOES3I30Ox2aI UGnNCopdGB2kDwbXVoLpJ5BqlT68kkUlCKHk1796W3VOD9XaGqUWlxJt0J0Eszl5PuXw JGpoctTvFNHxu5bp3OQL6UpANGm4rBWc1T04wL01THA0VYaXK4MSU7UNx2fMy2v9ewK/ If8uf+qJFRQjSB5y49QisOZo5lNGR5CrFfsACIizmjdLpuO9s+jlca9ORDMrq4356+ko qVRg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="Hh/l9xxP"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id fc19-20020a1709073a5300b007121c1dc158si8410145ejc.518.2022.06.14.04.28.40; Tue, 14 Jun 2022 04:29:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="Hh/l9xxP"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242990AbiFNLN7 (ORCPT + 99 others); Tue, 14 Jun 2022 07:13:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38770 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356294AbiFNLNn (ORCPT ); Tue, 14 Jun 2022 07:13:43 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EBB9720BDA; Tue, 14 Jun 2022 04:13:40 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id AA1D8B817DD; Tue, 14 Jun 2022 11:13:39 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 87FAEC3411B; Tue, 14 Jun 2022 11:13:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1655205218; bh=ENxCaP9Rk2nwCR/0WD0QuKmb7cfrw4GmQwwV0iSrjDU=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=Hh/l9xxP/UpxZHcucnAZAY/2RPjW7ZhykKXFR6o3nwKC/acvwOOH4YnGjj4s73SNd wUSW17MZVdj57vBp2pyVAGWMxf+qqdnBXm9a0WRI8wvxclb8y5xTasUfiNLsgoe1LL 7W58vY00SoFalY3Jut9SMdvcEkwp8zTLXgohjLIuHTH9UNwoUs0E4z4z9jMc6lvZXl ZZSXFa8KUxzcY5AEN4764+vN/Nt/L784GhIgknu/WlMqKpNSxYPdZEIxgQnpL/aqxK rF5lb+Vql3JtQTeQBGrALUAHnn2im5cZfX8Ge/1t6HpSaGRzmWt2hZX202OXaDsR/J uCh1vslFlNAiw== Date: Tue, 14 Jun 2022 12:22:48 +0100 From: Jonathan Cameron To: William Breathitt Gray Cc: lars@metafoo.de, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 1/2] iio: adc: stx104: Implement and utilize register structures Message-ID: <20220614122248.7f257556@jic23-huawei> In-Reply-To: References: X-Mailer: Claws Mail 4.1.0 (GTK 3.24.34; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-8.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 6 Jun 2022 10:15:17 -0400 William Breathitt Gray wrote: > Reduce magic numbers and improve code readability by implementing and > utilizing named register data structures. > > Signed-off-by: William Breathitt Gray A few comments inline, but looks fine to me otherwise. Jonathan > --- > 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 > > #define STX104_OUT_CHAN(chan) { \ > .type = 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"); > > +/** > + * 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; > }; > > /** > @@ -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; > }; > > @@ -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 = iio_priv(indio_dev); > + struct stx104_reg __iomem *const reg = 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 = ioread8(priv->base + 11); > + adc_config = ioread8(®->acfg); > gain = adc_config & 0x3; > > *val = 1 << gain; > @@ -91,24 +115,24 @@ static int stx104_read_raw(struct iio_dev *indio_dev, > } > > /* select ADC channel */ > - iowrite8(chan->channel | (chan->channel << 4), priv->base + 2); > + iowrite8(chan->channel | (chan->channel << 4), ®->achan); > > /* trigger ADC sample capture and wait for completion */ > - iowrite8(0, priv->base); > - while (ioread8(priv->base + 8) & BIT(7)); > + iowrite8(0, ®->ad); Curious - 8 bit write to a 16 bit address? Maybe worth a comment on why. > + while (ioread8(®->cir_asr) & BIT(7)); > > - *val = ioread16(priv->base); > + *val = ioread16(®->ad); > return IIO_VAL_INT; > case IIO_CHAN_INFO_OFFSET: > /* get ADC bipolar/unipolar configuration */ > - adc_config = ioread8(priv->base + 11); > + adc_config = ioread8(®->acfg); > adbu = !(adc_config & BIT(2)); > > *val = -32768 * adbu; > return IIO_VAL_INT; > case IIO_CHAN_INFO_SCALE: > /* get ADC bipolar/unipolar and gain configuration */ > - adc_config = ioread8(priv->base + 11); > + adc_config = ioread8(®->acfg); > adbu = !(adc_config & BIT(2)); > gain = adc_config & 0x3; > > @@ -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; > > priv->chan_out_states[chan->channel] = val; > - iowrite16(val, priv->base + 4 + 2 * chan->channel); > + iowrite16(val, priv->reg->dac + chan->channel); Perhaps for consistency with below go with &priv->reg->dac[chan->channels]; > > return 0; > } > @@ -307,15 +331,15 @@ static int stx104_probe(struct device *dev, unsigned int id) > } > > priv = iio_priv(indio_dev); > - priv->base = devm_ioport_map(dev, base[id], STX104_EXTENT); > - if (!priv->base) > + priv->reg = devm_ioport_map(dev, base[id], STX104_EXTENT); > + if (!priv->reg) > return -ENOMEM; > > indio_dev->info = &stx104_info; > indio_dev->modes = INDIO_DIRECT_MODE; > > /* determine if differential inputs */ > - if (ioread8(priv->base + 8) & BIT(5)) { > + if (ioread8(&priv->reg->cir_asr) & BIT(5)) { > indio_dev->num_channels = ARRAY_SIZE(stx104_channels_diff); > indio_dev->channels = stx104_channels_diff; > } else { > @@ -326,14 +350,14 @@ static int stx104_probe(struct device *dev, unsigned int id) > indio_dev->name = dev_name(dev); > > /* configure device for software trigger operation */ > - iowrite8(0, priv->base + 9); > + iowrite8(0, &priv->reg->acr); > > /* initialize gain setting to x1 */ > - iowrite8(0, priv->base + 11); > + iowrite8(0, &priv->reg->acfg); > > /* 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]); > > stx104gpio->chip.label = dev_name(dev); > stx104gpio->chip.parent = dev; > @@ -348,7 +372,7 @@ static int stx104_probe(struct device *dev, unsigned int id) > 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->base + 3; > + stx104gpio->base = &priv->reg->dio; > stx104gpio->out_state = 0x0; > > spin_lock_init(&stx104gpio->lock);