Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp262239pxb; Wed, 22 Sep 2021 01:20:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwBskfuOktjJt8sIBV++7EhvzCiOK7tf6nb9lgjGhJXblK7YDhzyjusyJq8Sw5++FOFBc+v X-Received: by 2002:a50:f197:: with SMTP id x23mr40732900edl.222.1632298830041; Wed, 22 Sep 2021 01:20:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632298830; cv=none; d=google.com; s=arc-20160816; b=mfPKlE7fSP3U0Gwj+UftjEOvXGIMXyy8YCfyVRQWqiUM91e6w2sA1nRlfTXXFj6by+ n08nwbU/NtLVo+ueMlMtisdCNQW/DvckU+/1G1wlc8yuclpCwNb9A3YEFX0mYVvEnOC1 80hxAsHaeU6sKDDGWdkNSK7wrCVt6Db8RmO8YTEUPWSwbT9imlHLY9MU7qkROSV44DgM JpHKP2RbsT7yB61UC+ke6KR7ol6jLfARPRMaC2xth7YZD5HhY9flfWsqKSOEXnckWrpy v9EDCcc75E/MZ5ouo535/EIou4S581qsUabYDlqInlQ0hLeiNh5uzzf4aUBRAr7cvChK WYsg== 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:date:subject:cc:to:from; bh=/n3s0Q6mzjuOItlrUxGG8SbwZJRPX9T+mZ3y7Ik/4ns=; b=dbhsWhkSRAk/5R1FXlkbVgqO+oERX2+lm3UUv8runuctSonRccP2EwJn/J0V5XHYmf nHxWMK8u78U76dFEv4aV4ALzz43GHcRPNZS+zfieNlFXYbsivtwV6NNmh3eSjkAKM3se V1M1TAGR2YJJ7CGCEbGVYmSw/ti0OwySH2SGJuUJWdqnDquQBGlXWTXltnacKAzhLYzU dhnJcQzEuZcsn71Pd5wyWz4si48SuCSWBtv+B5ABIqHAzqhz/rgiKv4L1xfaI4cTardR nzjjWO78kqJfuRrLNBRuE7ay3Hj2pQKgqDyd3z8K2EFmxG4F2ecuT2G88KA+0e4Cplba RuaQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x17si1828176edi.396.2021.09.22.01.20.06; Wed, 22 Sep 2021 01:20:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233766AbhIVITD (ORCPT + 99 others); Wed, 22 Sep 2021 04:19:03 -0400 Received: from twspam01.aspeedtech.com ([211.20.114.71]:53152 "EHLO twspam01.aspeedtech.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233770AbhIVIS4 (ORCPT ); Wed, 22 Sep 2021 04:18:56 -0400 Received: from mail.aspeedtech.com ([192.168.0.24]) by twspam01.aspeedtech.com with ESMTP id 18M7sdVP099796; Wed, 22 Sep 2021 15:54:39 +0800 (GMT-8) (envelope-from billy_tsai@aspeedtech.com) Received: from BillyTsai-pc.aspeed.com (192.168.2.149) by TWMBX02.aspeed.com (192.168.0.24) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Wed, 22 Sep 2021 16:14:57 +0800 From: Billy Tsai To: , , , , , , , , , , , , , CC: Subject: [v7 09/11] iio: adc: aspeed: Add compensation phase. Date: Wed, 22 Sep 2021 16:15:18 +0800 Message-ID: <20210922081520.30580-10-billy_tsai@aspeedtech.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210922081520.30580-1-billy_tsai@aspeedtech.com> References: <20210922081520.30580-1-billy_tsai@aspeedtech.com> MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [192.168.2.149] X-ClientProxiedBy: TWMBX02.aspeed.com (192.168.0.24) To TWMBX02.aspeed.com (192.168.0.24) X-DNSRBL: X-MAIL: twspam01.aspeedtech.com 18M7sdVP099796 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch adds a compensation phase to improve the accuracy of ADC measurement. This is the built-in function through input half of the reference voltage to get the ADC offset. Signed-off-by: Billy Tsai --- drivers/iio/adc/aspeed_adc.c | 54 +++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/drivers/iio/adc/aspeed_adc.c b/drivers/iio/adc/aspeed_adc.c index f2226ba3144d..efda76fa0aa1 100644 --- a/drivers/iio/adc/aspeed_adc.c +++ b/drivers/iio/adc/aspeed_adc.c @@ -103,6 +103,7 @@ struct aspeed_adc_data { struct reset_control *rst; int vref_mv; u32 sample_period_ns; + int cv; }; #define ASPEED_CHAN(_idx, _data_reg_addr) { \ @@ -112,7 +113,8 @@ struct aspeed_adc_data { .address = (_data_reg_addr), \ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ - BIT(IIO_CHAN_INFO_SAMP_FREQ), \ + BIT(IIO_CHAN_INFO_SAMP_FREQ) | \ + BIT(IIO_CHAN_INFO_OFFSET), \ } static const struct iio_chan_spec aspeed_adc_iio_channels[] = { @@ -134,6 +136,51 @@ static const struct iio_chan_spec aspeed_adc_iio_channels[] = { ASPEED_CHAN(15, 0x2E), }; +static int aspeed_adc_compensation(struct iio_dev *indio_dev) +{ + struct aspeed_adc_data *data = iio_priv(indio_dev); + u32 index, adc_raw = 0; + u32 adc_engine_control_reg_val; + + adc_engine_control_reg_val = + readl(data->base + ASPEED_REG_ENGINE_CONTROL); + adc_engine_control_reg_val &= ~ASPEED_ADC_OP_MODE; + adc_engine_control_reg_val |= + (FIELD_PREP(ASPEED_ADC_OP_MODE, ASPEED_ADC_OP_MODE_NORMAL) | + ASPEED_ADC_ENGINE_ENABLE); + /* + * Enable compensating sensing: + * After that, the input voltage of ADC will force to half of the reference + * voltage. So the expected reading raw data will become half of the max + * value. We can get compensating value = 0x200 - ADC read raw value. + * It is recommended to average at least 10 samples to get a final CV. + */ + writel(adc_engine_control_reg_val | ASPEED_ADC_CTRL_COMPENSATION | + ASPEED_ADC_CTRL_CHANNEL_ENABLE(0), + data->base + ASPEED_REG_ENGINE_CONTROL); + /* + * After enable compensating sensing mode need to wait some time for ADC stable + * Experiment result is 1ms. + */ + mdelay(1); + + for (index = 0; index < 16; index++) { + /* + * Waiting for the sampling period ensures that the value acquired + * is fresh each time. + */ + ndelay(data->sample_period_ns); + adc_raw += readw(data->base + aspeed_adc_iio_channels[0].address); + } + adc_raw >>= 4; + data->cv = BIT(ASPEED_RESOLUTION_BITS - 1) - adc_raw; + writel(adc_engine_control_reg_val, + data->base + ASPEED_REG_ENGINE_CONTROL); + dev_dbg(data->dev, "Compensating value = %d\n", data->cv); + + return 0; +} + static int aspeed_adc_set_sampling_rate(struct iio_dev *indio_dev, u32 rate) { struct aspeed_adc_data *data = iio_priv(indio_dev); @@ -163,6 +210,10 @@ static int aspeed_adc_read_raw(struct iio_dev *indio_dev, *val = readw(data->base + chan->address); return IIO_VAL_INT; + case IIO_CHAN_INFO_OFFSET: + *val = data->cv; + return IIO_VAL_INT; + case IIO_CHAN_INFO_SCALE: *val = data->vref_mv; *val2 = ASPEED_RESOLUTION_BITS; @@ -448,6 +499,7 @@ static int aspeed_adc_probe(struct platform_device *pdev) return ret; } + aspeed_adc_compensation(indio_dev); /* Start all channels in normal mode. */ adc_engine_control_reg_val = readl(data->base + ASPEED_REG_ENGINE_CONTROL); -- 2.25.1