From patchwork Fri Nov 11 11:26:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Mitja_=C5=A0pes?= X-Patchwork-Id: 18684 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp684352wru; Fri, 11 Nov 2022 03:29:42 -0800 (PST) X-Google-Smtp-Source: AA0mqf5Ea+B1E4eyvt+p6zkkdiZspqGWgwqEexlpFhUyRlZX/I+nHMwrYkqh7thzt0mNV1ql2M/g X-Received: by 2002:a17:902:ea05:b0:179:fe95:f6d9 with SMTP id s5-20020a170902ea0500b00179fe95f6d9mr1871068plg.51.1668166181741; Fri, 11 Nov 2022 03:29:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668166181; cv=none; d=google.com; s=arc-20160816; b=BSChU5BdFrZAVDPdt3dN7ILMQiBHyErs7jSR0sFT/5CeW38SlJD28/9ZnLGON45orm znbwGP3CJf9rIMl7yZdc2RKxKEPLD/fT/70Xhuuhpt4Y6cWmObzBE8NftadXgQI1kWvb 4gbVRBblczFQm92JKZDwuHA+JSmcrIM3xEQOlqDPIWtPUizDiRxIdHYXYHTL0OL8KW66 oGCoJrAyMj9LYqFKaFKYJ2BWbXtjn8pWx5142CNs1uFVlGPOtHr43HT6vGQ4NymUYOIC MUXFTTRD75foXYrWShhwLhTfHF9ehTUUnuY0FHGNw7YwsKlnq9pbJiPcbv68pWMvlcRC JBdA== 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:to:from :dkim-signature; bh=9BmkVh6jIh/jyhyFFqsPkAGeVwmF2cJMvet6/U1c940=; b=anZMO3zMMSgrQ6zPTCxA/MPgjZiy1/+gabHEKFX17J8KzAmVBw2peenzwYt2fi8NVH BLcDSkmo6HJXBjcfphBvvj+7ibNemRrkJXZLjezmE4NVaA2WNw3HSofFI1alj4Yx7OnG S4KopFXnba5xjHm7ajHbyt+xEHVx6AntJGuX9gBZ3alE28o/6S2uFhMvCnQoF7o/QziN 5MLdVqXd220kcI0YK/DzeJ+nZDunAlafPsPvhZ4VnVDWXYmzSrLK/VCH4TH2WlI2Z4Zk 5Ch5DKMrgEDs9Orr6/+b8KYbwHv27k6aVwHZdZdYQJeCIyHLa++lNyg45hhSHJdRRXjb 9xXw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lxnav.com header.s=dhl header.b=cigeI+HJ; 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=REJECT sp=REJECT dis=NONE) header.from=lxnav.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z11-20020aa7888b000000b0055fa098c388si2519632pfe.259.2022.11.11.03.29.27; Fri, 11 Nov 2022 03:29:41 -0800 (PST) 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=@lxnav.com header.s=dhl header.b=cigeI+HJ; 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=REJECT sp=REJECT dis=NONE) header.from=lxnav.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233192AbiKKL2M (ORCPT + 99 others); Fri, 11 Nov 2022 06:28:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39198 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233087AbiKKL2D (ORCPT ); Fri, 11 Nov 2022 06:28:03 -0500 Received: from dhl.lxnav.com (dhl.lxnav.com [IPv6:2a01:4f8:c010:2f07::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C5C8A60EBB for ; Fri, 11 Nov 2022 03:28:02 -0800 (PST) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 45F504070D; Fri, 11 Nov 2022 12:27:57 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lxnav.com; s=dhl; t=1668166078; h=from:subject:date:message-id:to:mime-version: content-transfer-encoding:in-reply-to:references; bh=9BmkVh6jIh/jyhyFFqsPkAGeVwmF2cJMvet6/U1c940=; b=cigeI+HJRe6JIJ2uzZTjx0+QwIL3d2NcFzzLCU7g4uLi1ePXFF50sMtilsU0UpDeOOdkkG gbSo/GwMuqGRzKd7/fAL16JNWFIo2SYjq3eqk7JoTsUFNg4Ey4AL4xdpU1ca5jEfnP4FCx NoVJ/6F/FTicNLbic8Y45Hs7tuc3kzeRcr99xHilkL5FG7Ay0rzL/5a0rnM1OV427OWUjT EL9Zu2n6nlubE3JO/eAxbmwuY67N3o5s84c+m/ECFbu/PmD/Q+HMiJ0kQx/ZtH7NnSGjSu nyhFeeup7/c0KFjJ04iz6pPJ/93BpZ3P0kCvNtv2ZQYcCrFhRlRC+HUNn9ZHfg== From: Mitja Spes To: Jonathan Cameron , Lars-Peter Clausen , Mitja Spes , Angelo Compagnucci , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/4] iio: adc: mcp3422: fix scale read bug Date: Fri, 11 Nov 2022 12:26:53 +0100 Message-Id: <20221111112657.1521307-2-mitja@lxnav.com> In-Reply-To: <20221111112657.1521307-1-mitja@lxnav.com> References: <20221111112657.1521307-1-mitja@lxnav.com> MIME-Version: 1.0 X-Last-TLS-Session-Version: TLSv1.3 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS autolearn=unavailable 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749199021830437023?= X-GMAIL-MSGID: =?utf-8?q?1749199021830437023?= Scale was returned for currently active channel instead of the specified channel. Signed-off-by: Mitja Spes Fixes: 07914c84ba30 ("iio: adc: Add driver for Microchip MCP3422/3/4 high resolution ADC") --- drivers/iio/adc/mcp3422.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/iio/adc/mcp3422.c b/drivers/iio/adc/mcp3422.c index da353dcb1e9d..3d53de300c89 100644 --- a/drivers/iio/adc/mcp3422.c +++ b/drivers/iio/adc/mcp3422.c @@ -164,8 +164,9 @@ static int mcp3422_read_raw(struct iio_dev *iio, struct mcp3422 *adc = iio_priv(iio); int err; + u8 req_channel = channel->channel; u8 sample_rate = MCP3422_SAMPLE_RATE(adc->config); - u8 pga = MCP3422_PGA(adc->config); + u8 pga = adc->pga[req_channel]; switch (mask) { case IIO_CHAN_INFO_RAW: @@ -175,7 +176,6 @@ static int mcp3422_read_raw(struct iio_dev *iio, return IIO_VAL_INT; case IIO_CHAN_INFO_SCALE: - *val1 = 0; *val2 = mcp3422_scales[sample_rate][pga]; return IIO_VAL_INT_PLUS_NANO; From patchwork Fri Nov 11 11:26:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Mitja_=C5=A0pes?= X-Patchwork-Id: 18686 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp685051wru; Fri, 11 Nov 2022 03:31:03 -0800 (PST) X-Google-Smtp-Source: AA0mqf6VNhFdu+7yNqOZQbrxEry9qUQUjx8+DW4iW0aFcDFtXrumCpF4gEEM2hpcYkEVqTbrmfas X-Received: by 2002:a17:90a:4a8a:b0:20a:74b8:ea2a with SMTP id f10-20020a17090a4a8a00b0020a74b8ea2amr1526353pjh.73.1668166263506; Fri, 11 Nov 2022 03:31:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668166263; cv=none; d=google.com; s=arc-20160816; b=Q20NB4uGxHbbr9dt0LORxcFzVbo4rWkGlz10kRp+vLKhXhbrHG2GtjBuPPHBsHQoGc vbomyQJK173SHvMQgxhsyMRWABXHDATpFM0X+WFmmM9JKusidUrife34g5bD279Nxf8P kJqulnfiW8XmPB7jiQAoVBX1KcE5W651naN058CPY0mz0kVjqoU4/g1XYZfArt9ax0mA +VX/XD9KtgLTUPqspMEAa8MSgy/ALKgZ5Cxsnz5A8blMk+AKYnDb4Wv1tya0d0XRRhBP qQg6xftfvuvVHVP+QWztuFrLj5cF5oO2nwdQC0Tp7czRflU25JR17OniCxhvxddRE4TM cQmg== 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:to:from :dkim-signature; bh=PomnhdIpzQ+pggSr9FKY16Cho/dIxrxhSMt/zS5cSLo=; b=w3psELr5N9itZqY87KrZ6gyserNRnn257jV/s9a5OvzEcxq5o5rEvdx+eq/bjuHoPC RxiporYDOUo/cNSvqsAx+ZOgn4Nz+ZIj8X9ZR5HoQ/oeD0D13TD9T7UID458KrTDUmV7 LWi9mOS6sUwArE3A2YwpBOs/dbBJsgcLaOqQ5d3fO7SS4MoInx1cSgpL+uhlPzqoPFil 4787kHlzhQAoJlRcqS5Z9W+SoUw4xUVAqQ+DuKa4Hb2o4qSPXMJl9nFeIo5YMAdBSFA5 7bJnzokcbZUGmIYRpcf5BH42/P2TK9JjDS0yXUeej67drWI9GE6LTcSS/e6KfijmPS6l Bafw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lxnav.com header.s=dhl header.b=P+PXfovB; 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=REJECT sp=REJECT dis=NONE) header.from=lxnav.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m19-20020a170902d19300b0018010c3d7e3si1922702plb.404.2022.11.11.03.30.49; Fri, 11 Nov 2022 03:31:03 -0800 (PST) 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=@lxnav.com header.s=dhl header.b=P+PXfovB; 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=REJECT sp=REJECT dis=NONE) header.from=lxnav.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233495AbiKKL2V (ORCPT + 99 others); Fri, 11 Nov 2022 06:28:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39452 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233262AbiKKL2L (ORCPT ); Fri, 11 Nov 2022 06:28:11 -0500 Received: from dhl.lxnav.com (dhl.lxnav.com [IPv6:2a01:4f8:c010:2f07::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6F7E269DF1 for ; Fri, 11 Nov 2022 03:28:06 -0800 (PST) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 0139D40767; Fri, 11 Nov 2022 12:28:00 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lxnav.com; s=dhl; t=1668166082; h=from:subject:date:message-id:to:mime-version: content-transfer-encoding:in-reply-to:references; bh=PomnhdIpzQ+pggSr9FKY16Cho/dIxrxhSMt/zS5cSLo=; b=P+PXfovB1fwLFgoWmdrUGW8VQwclQ+LSWBa4D2wnCDpXM0Q2cWskXfTIJlLnZw9fj4G0uH 8Bs30vVh9FN0fr39UV7Ro8bcdXRbEJorK02hJYtKxTOgVbe4wmymuNNDTrG0FI45ghU6VT UN5NDnNyeHRa11zCg3q1pbPgDULfZxTkeRTNP7hHlXDivBcjLif6PzTDmnvsCZRaGNB/AI LP5xcrsBh/QJF8OyYiTCakD+F/iqDVmg32FA01xUO5EoRwWB92dy8/2RamFjS5l8/l6iuA XIM/hY04Hl+F/82Z0jSW2TGNcfw/bX7tjsrttol909mXbQ3uvYbsgPWe8PXrsA== From: Mitja Spes To: Jonathan Cameron , Lars-Peter Clausen , Mitja Spes , Angelo Compagnucci , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/4] iio: adc: mcp3422: allow setting gain and sampling per channel Date: Fri, 11 Nov 2022 12:26:54 +0100 Message-Id: <20221111112657.1521307-3-mitja@lxnav.com> In-Reply-To: <20221111112657.1521307-1-mitja@lxnav.com> References: <20221111112657.1521307-1-mitja@lxnav.com> MIME-Version: 1.0 X-Last-TLS-Session-Version: TLSv1.3 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS autolearn=unavailable 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749199108160926595?= X-GMAIL-MSGID: =?utf-8?q?1749199108160926595?= General improvements: - allow setting gain and sampling per channel - setting scale can also set sampling rate (combined setting) - use per channel config setting - do not update mcp register on setting write (we might be reading it...) instead it's updated on next value read - output all scale values (sample rates x gain) Signed-off-by: Mitja Spes --- drivers/iio/adc/mcp3422.c | 119 ++++++++++++++++++++------------------ 1 file changed, 64 insertions(+), 55 deletions(-) diff --git a/drivers/iio/adc/mcp3422.c b/drivers/iio/adc/mcp3422.c index 3d53de300c89..cfb629b964af 100644 --- a/drivers/iio/adc/mcp3422.c +++ b/drivers/iio/adc/mcp3422.c @@ -28,14 +28,19 @@ #define MCP3422_CHANNEL_MASK 0x60 #define MCP3422_PGA_MASK 0x03 #define MCP3422_SRATE_MASK 0x0C -#define MCP3422_SRATE_240 0x0 -#define MCP3422_SRATE_60 0x1 -#define MCP3422_SRATE_15 0x2 -#define MCP3422_SRATE_3 0x3 -#define MCP3422_PGA_1 0 -#define MCP3422_PGA_2 1 -#define MCP3422_PGA_4 2 -#define MCP3422_PGA_8 3 + +#define MCP3422_SRATE_240 0 +#define MCP3422_SRATE_60 1 +#define MCP3422_SRATE_15 2 +#define MCP3422_SRATE_3 3 +#define MCP3422_SRATE_COUNT 4 + +#define MCP3422_PGA_1 0 +#define MCP3422_PGA_2 1 +#define MCP3422_PGA_4 2 +#define MCP3422_PGA_8 3 +#define MCP3422_PGA_COUNT 4 + #define MCP3422_CONT_SAMPLING 0x10 #define MCP3422_CHANNEL(config) (((config) & MCP3422_CHANNEL_MASK) >> 5) @@ -52,32 +57,32 @@ .indexed = 1, \ .channel = _index, \ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) \ - | BIT(IIO_CHAN_INFO_SCALE), \ - .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ + | BIT(IIO_CHAN_INFO_SCALE) \ + | BIT(IIO_CHAN_INFO_SAMP_FREQ), \ } -static const int mcp3422_scales[4][4] = { +static const int mcp3422_scales[MCP3422_SRATE_COUNT][MCP3422_PGA_COUNT] = { { 1000000, 500000, 250000, 125000 }, { 250000, 125000, 62500, 31250 }, { 62500, 31250, 15625, 7812 }, { 15625, 7812, 3906, 1953 } }; /* Constant msleep times for data acquisitions */ -static const int mcp3422_read_times[4] = { +static const int mcp3422_read_times[MCP3422_SRATE_COUNT] = { [MCP3422_SRATE_240] = 1000 / 240, [MCP3422_SRATE_60] = 1000 / 60, [MCP3422_SRATE_15] = 1000 / 15, [MCP3422_SRATE_3] = 1000 / 3 }; /* sample rates to integer conversion table */ -static const int mcp3422_sample_rates[4] = { +static const int mcp3422_sample_rates[MCP3422_SRATE_COUNT] = { [MCP3422_SRATE_240] = 240, [MCP3422_SRATE_60] = 60, [MCP3422_SRATE_15] = 15, [MCP3422_SRATE_3] = 3 }; /* sample rates to sign extension table */ -static const int mcp3422_sign_extend[4] = { +static const int mcp3422_sign_extend[MCP3422_SRATE_COUNT] = { [MCP3422_SRATE_240] = 11, [MCP3422_SRATE_60] = 13, [MCP3422_SRATE_15] = 15, @@ -87,8 +92,8 @@ static const int mcp3422_sign_extend[4] = { struct mcp3422 { struct i2c_client *i2c; u8 id; - u8 config; - u8 pga[4]; + u8 active_config; // config currently set on mcp + u8 ch_config[4]; // per channel config struct mutex lock; }; @@ -98,7 +103,7 @@ static int mcp3422_update_config(struct mcp3422 *adc, u8 newconfig) ret = i2c_master_send(adc->i2c, &newconfig, 1); if (ret > 0) { - adc->config = newconfig; + adc->active_config = newconfig; ret = 0; } @@ -108,7 +113,7 @@ static int mcp3422_update_config(struct mcp3422 *adc, u8 newconfig) static int mcp3422_read(struct mcp3422 *adc, int *value, u8 *config) { int ret = 0; - u8 sample_rate = MCP3422_SAMPLE_RATE(adc->config); + u8 sample_rate = MCP3422_SAMPLE_RATE(adc->active_config); u8 buf[4] = {0, 0, 0, 0}; u32 temp; @@ -136,18 +141,13 @@ static int mcp3422_read_channel(struct mcp3422 *adc, mutex_lock(&adc->lock); - if (req_channel != MCP3422_CHANNEL(adc->config)) { - config = adc->config; - config &= ~MCP3422_CHANNEL_MASK; - config |= MCP3422_CHANNEL_VALUE(req_channel); - config &= ~MCP3422_PGA_MASK; - config |= MCP3422_PGA_VALUE(adc->pga[req_channel]); - ret = mcp3422_update_config(adc, config); + if (adc->ch_config[req_channel] != adc->active_config) { + ret = mcp3422_update_config(adc, adc->ch_config[req_channel]); if (ret < 0) { mutex_unlock(&adc->lock); return ret; } - msleep(mcp3422_read_times[MCP3422_SAMPLE_RATE(adc->config)]); + msleep(mcp3422_read_times[MCP3422_SAMPLE_RATE(adc->active_config)]); } ret = mcp3422_read(adc, value, &config); @@ -164,9 +164,9 @@ static int mcp3422_read_raw(struct iio_dev *iio, struct mcp3422 *adc = iio_priv(iio); int err; - u8 req_channel = channel->channel; - u8 sample_rate = MCP3422_SAMPLE_RATE(adc->config); - u8 pga = adc->pga[req_channel]; + u8 config = adc->ch_config[channel->channel]; + u8 sample_rate = MCP3422_SAMPLE_RATE(config); + u8 pga = MCP3422_PGA(config); switch (mask) { case IIO_CHAN_INFO_RAW: @@ -181,7 +181,7 @@ static int mcp3422_read_raw(struct iio_dev *iio, return IIO_VAL_INT_PLUS_NANO; case IIO_CHAN_INFO_SAMP_FREQ: - *val1 = mcp3422_sample_rates[MCP3422_SAMPLE_RATE(adc->config)]; + *val1 = mcp3422_sample_rates[sample_rate]; return IIO_VAL_INT; default: @@ -197,26 +197,25 @@ static int mcp3422_write_raw(struct iio_dev *iio, { struct mcp3422 *adc = iio_priv(iio); u8 temp; - u8 config = adc->config; u8 req_channel = channel->channel; - u8 sample_rate = MCP3422_SAMPLE_RATE(config); - u8 i; + u8 config = adc->ch_config[req_channel]; + u8 i, j; switch (mask) { case IIO_CHAN_INFO_SCALE: if (val1 != 0) return -EINVAL; - for (i = 0; i < ARRAY_SIZE(mcp3422_scales[0]); i++) { - if (val2 == mcp3422_scales[sample_rate][i]) { - adc->pga[req_channel] = i; - - config &= ~MCP3422_CHANNEL_MASK; - config |= MCP3422_CHANNEL_VALUE(req_channel); - config &= ~MCP3422_PGA_MASK; - config |= MCP3422_PGA_VALUE(adc->pga[req_channel]); - - return mcp3422_update_config(adc, config); + for (j = 0; j < MCP3422_SRATE_COUNT; j++) { + for (i = 0; i < MCP3422_PGA_COUNT; i++) { + if (val2 == mcp3422_scales[j][i]) { + config &= ~MCP3422_PGA_MASK; + config |= MCP3422_PGA_VALUE(i); + config &= ~MCP3422_SRATE_MASK; + config |= MCP3422_SAMPLE_RATE_VALUE(j); + adc->ch_config[req_channel] = config; + return 0; + } } } return -EINVAL; @@ -241,12 +240,10 @@ static int mcp3422_write_raw(struct iio_dev *iio, return -EINVAL; } - config &= ~MCP3422_CHANNEL_MASK; - config |= MCP3422_CHANNEL_VALUE(req_channel); config &= ~MCP3422_SRATE_MASK; config |= MCP3422_SAMPLE_RATE_VALUE(temp); - - return mcp3422_update_config(adc, config); + adc->ch_config[req_channel] = config; + return 0; default: break; @@ -282,14 +279,18 @@ static ssize_t mcp3422_show_samp_freqs(struct device *dev, static ssize_t mcp3422_show_scales(struct device *dev, struct device_attribute *attr, char *buf) { - struct mcp3422 *adc = iio_priv(dev_to_iio_dev(dev)); - u8 sample_rate = MCP3422_SAMPLE_RATE(adc->config); - - return sprintf(buf, "0.%09u 0.%09u 0.%09u 0.%09u\n", - mcp3422_scales[sample_rate][0], - mcp3422_scales[sample_rate][1], - mcp3422_scales[sample_rate][2], - mcp3422_scales[sample_rate][3]); + ssize_t count = 0; + int i; + + for (i = 0; i < MCP3422_SRATE_COUNT; i++) { + count += sprintf(buf + count, "0.%09u 0.%09u 0.%09u 0.%09u%s", + mcp3422_scales[i][0], + mcp3422_scales[i][1], + mcp3422_scales[i][2], + mcp3422_scales[i][3], + (i < MCP3422_SRATE_COUNT - 1 ? " " : "\n")); + } + return count; } static IIO_DEVICE_ATTR(sampling_frequency_available, S_IRUGO, @@ -336,6 +337,7 @@ static int mcp3422_probe(struct i2c_client *client, struct iio_dev *indio_dev; struct mcp3422 *adc; int err; + int i; u8 config; if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) @@ -376,6 +378,13 @@ static int mcp3422_probe(struct i2c_client *client, } /* meaningful default configuration */ + for (i = 0; i < 4; i++) { + adc->ch_config[i] = (MCP3422_CONT_SAMPLING + | MCP3422_CHANNEL_VALUE(i) + | MCP3422_PGA_VALUE(MCP3422_PGA_1) + | MCP3422_SAMPLE_RATE_VALUE(MCP3422_SRATE_240)); + } + config = (MCP3422_CONT_SAMPLING | MCP3422_CHANNEL_VALUE(0) | MCP3422_PGA_VALUE(MCP3422_PGA_1) From patchwork Fri Nov 11 11:26:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Mitja_=C5=A0pes?= X-Patchwork-Id: 18687 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp685144wru; Fri, 11 Nov 2022 03:31:13 -0800 (PST) X-Google-Smtp-Source: AA0mqf6qeCJa33dyXok9uZpiJWDfxROqoI0N8Q632PDRibO3KapMgJGkJVF/EURdV5sAFCQzprw/ X-Received: by 2002:a17:90a:1904:b0:213:22d:b2e2 with SMTP id 4-20020a17090a190400b00213022db2e2mr1431538pjg.148.1668166273599; Fri, 11 Nov 2022 03:31:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668166273; cv=none; d=google.com; s=arc-20160816; b=D6cgmlJamukapsjIj1MLRYAPjps+z4ZtlZPUpEgIiO4Gsc5qzWJd3tqlSId0mmV6A1 CPzVJ1rTMIsORbos/exEAt4dwXS5VyZYDy/OwkeuDrojnSFW/NllKkOhDUvcYfTypwU1 05N+HtTf0lZEZixrsJJ3tBkCX+RuIoa1Tfh2f1fLFEnEo3uCqaX1XikUyKt9cFyit/jB r1iEsaGHy2fPOk2wE2ndRsPZYrDe9uQpe+9v6tmOJYVoyEtXjqbACPFT2lR8aWkeAgQD 2/pDO0OsKpg0h2GRabdE7WJkvtUp8GTQ5x1i1gtel+ov3zmXTUo1raRlZ4y3NrVtybEL gx3g== 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:to:from :dkim-signature; bh=NqvSjeOULQZbj3EUjH7dknKYQjo14gTb8HTU0Fd3y2A=; b=eo3bTrcZ5xVmHqJWjMAsZGMNo04DVqgsMeVanGtRg0dJtPHnFhzw9eVRZU1DK6BVO1 PqBjQNQK72RVWMsMTcdc2+csSmhTl3pV3+PMs9wCyV4MC9Vj8AEEXyfgBaXBe4AwEOh+ 2NtASuXzQGD6BT1gbJZviiG3qaeWpTCbRWk3wenQ6BxNgaxYmKnSleo15GPTRfVNl1Yv H/6N4Tc5zCTT0SQbAcMwE8UcOJrtsHaqIv1ropl7aYDd4ezA9iyg+Gsvm9Fn13mWAMt3 KZqQDvRbjdU0Po0MoIt7kuYJTGq2UAv3jvlCQl/XJ/aMZ8DExx6rOau2BzvPfoq6vecF 9LHQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lxnav.com header.s=dhl header.b=hYtyX2wI; 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=REJECT sp=REJECT dis=NONE) header.from=lxnav.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j1-20020a170902690100b001887a1ffb17si1952657plk.223.2022.11.11.03.30.59; Fri, 11 Nov 2022 03:31:13 -0800 (PST) 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=@lxnav.com header.s=dhl header.b=hYtyX2wI; 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=REJECT sp=REJECT dis=NONE) header.from=lxnav.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233518AbiKKL2i (ORCPT + 99 others); Fri, 11 Nov 2022 06:28:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39526 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233288AbiKKL2S (ORCPT ); Fri, 11 Nov 2022 06:28:18 -0500 Received: from dhl.lxnav.com (dhl.lxnav.com [IPv6:2a01:4f8:c010:2f07::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B351569DC9 for ; Fri, 11 Nov 2022 03:28:09 -0800 (PST) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 1E82040768; Fri, 11 Nov 2022 12:28:04 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lxnav.com; s=dhl; t=1668166085; h=from:subject:date:message-id:to:mime-version: content-transfer-encoding:in-reply-to:references; bh=NqvSjeOULQZbj3EUjH7dknKYQjo14gTb8HTU0Fd3y2A=; b=hYtyX2wIlbpRse+AYLKuwh4QMTwz+d6nnySOhp5qQRVLJviQ613mUP4sJZtegxv8cMGmte nhI8wp+T8NjKNqbOm9W/KRQIwkFC94s6Rv8aXZlmOBJFoxE4Kin3W/Sr7gva9ohi5J043I ZILyXvw1M71C/kaI/gO/KksqNXI5gt1qPD9vdZqkw655oQxRs4Yk0z0ZHk0nLw2hD9nJB7 fCsRc4Lh/uj00LPqmEJVFrrRI9rjLMMO30Ecccdha44/3JURIwe5PrIOk1QrRAiYk6pCm6 1qiKhmLqrLduxacyuSXsE9KikRzdlFFeZTCw+nhw/j3b9hzw3o43lJUgQ/tEkw== From: Mitja Spes To: Jonathan Cameron , Lars-Peter Clausen , Mitja Spes , Angelo Compagnucci , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 3/4] iio: adc: mcp3422: add hardware gain attribute Date: Fri, 11 Nov 2022 12:26:55 +0100 Message-Id: <20221111112657.1521307-4-mitja@lxnav.com> In-Reply-To: <20221111112657.1521307-1-mitja@lxnav.com> References: <20221111112657.1521307-1-mitja@lxnav.com> MIME-Version: 1.0 X-Last-TLS-Session-Version: TLSv1.3 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS autolearn=unavailable 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749199118915831138?= X-GMAIL-MSGID: =?utf-8?q?1749199118915831138?= Allows setting gain separately from scale. Signed-off-by: Mitja Spes --- drivers/iio/adc/mcp3422.c | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/drivers/iio/adc/mcp3422.c b/drivers/iio/adc/mcp3422.c index cfb629b964af..eef35fb2fc22 100644 --- a/drivers/iio/adc/mcp3422.c +++ b/drivers/iio/adc/mcp3422.c @@ -58,7 +58,8 @@ .channel = _index, \ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) \ | BIT(IIO_CHAN_INFO_SCALE) \ - | BIT(IIO_CHAN_INFO_SAMP_FREQ), \ + | BIT(IIO_CHAN_INFO_SAMP_FREQ) \ + | BIT(IIO_CHAN_INFO_HARDWAREGAIN), \ } static const int mcp3422_scales[MCP3422_SRATE_COUNT][MCP3422_PGA_COUNT] = { @@ -184,6 +185,10 @@ static int mcp3422_read_raw(struct iio_dev *iio, *val1 = mcp3422_sample_rates[sample_rate]; return IIO_VAL_INT; + case IIO_CHAN_INFO_HARDWAREGAIN: + *val1 = (1 << pga); + return IIO_VAL_INT; + default: break; } @@ -245,6 +250,29 @@ static int mcp3422_write_raw(struct iio_dev *iio, adc->ch_config[req_channel] = config; return 0; + case IIO_CHAN_INFO_HARDWAREGAIN: + switch (val1) { + case 1: + temp = MCP3422_PGA_1; + break; + case 2: + temp = MCP3422_PGA_2; + break; + case 4: + temp = MCP3422_PGA_4; + break; + case 8: + temp = MCP3422_PGA_8; + break; + default: + return -EINVAL; + } + + config &= ~MCP3422_PGA_MASK; + config |= MCP3422_PGA_VALUE(temp); + adc->ch_config[req_channel] = config; + return 0; + default: break; } @@ -260,6 +288,8 @@ static int mcp3422_write_raw_get_fmt(struct iio_dev *indio_dev, return IIO_VAL_INT_PLUS_NANO; case IIO_CHAN_INFO_SAMP_FREQ: return IIO_VAL_INT_PLUS_MICRO; + case IIO_CHAN_INFO_HARDWAREGAIN: + return IIO_VAL_INT_PLUS_MICRO; default: return -EINVAL; } From patchwork Fri Nov 11 11:26:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Mitja_=C5=A0pes?= X-Patchwork-Id: 18685 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp685018wru; Fri, 11 Nov 2022 03:31:00 -0800 (PST) X-Google-Smtp-Source: AA0mqf6z9Rryo35DCL52qwWITx2LRqNEAclsoA3V/FVeKbqLhapI6fSjxjrB4heIzbwLRf7EPhKl X-Received: by 2002:a17:902:7202:b0:186:9b7d:f0c9 with SMTP id ba2-20020a170902720200b001869b7df0c9mr2142458plb.58.1668166259935; Fri, 11 Nov 2022 03:30:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668166259; cv=none; d=google.com; s=arc-20160816; b=XqtH2dRCJl9aKn1AUplkT62G7SG4FKjRqg1CIW7QDyzAeLL5JLBH3wV+q1Lzk5/KK1 wgEAZKMJD2IvdYZDIQDY5MPGNLwMtQS3I9nj1vYZc/mEPfelceEcIz7A677sU8r4X9Sq jHfjL6nrp3wi7dE9pzRobLS/sJUg6CowQrEp+siSZdZ1lBray/IeS4HOjRCvyc6hgDFp ZhaSLDA00gSqa6rnw0E/gPhGtAwY+NPuxLu7aw/+/dG2CM98Yvi1duzBZ8OCzpewqPc6 ptKpFmEH+2lfzQzSZAaCQqvoDrzvnB3eAxuDqSYZb33SbB4H9z9NaFwWfvyKQKtd0Vvk cHkQ== 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:to:from :dkim-signature; bh=sduuwDJ1cpdPo1tG/vMuBFFxLHUHN1zt6Q+P4Jawaig=; b=Z5AFRfpUQqTLyLXwyJQJZv9dyWG2PfTR2UChtdqZOrIL029Wvv1G4JGghYu3UAyv4S 30wM6H62OiodwvCS86jFYIlvnPWjukCn8fPPP9W4ArQJY+ObzfVaywSUgM6l1o5Xsug2 00MgrRpCUYwgSsK/yRxE8NNmOCYyyQYKl6uzgV69O+iWNeGjRrm1Hwc4GCvwQzLMVPf5 9PaeH+t7P/f2ZW8GRCrOVM0ZvlCpsoYGQIjdNn8+Th5ySnCpC4akcoi4aQdgbYjn3Wi1 BSkgdI2UEUMAZVFhk9mKST3Vnxj0y8q+0xHEM9Ssg1ckQs640eqm4OAvOCK/U5Rf+uy9 Ezyg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lxnav.com header.s=dhl header.b=R25EG2cP; 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=REJECT sp=REJECT dis=NONE) header.from=lxnav.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g128-20020a636b86000000b00473cd4d74a1si2137082pgc.240.2022.11.11.03.30.45; Fri, 11 Nov 2022 03:30:59 -0800 (PST) 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=@lxnav.com header.s=dhl header.b=R25EG2cP; 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=REJECT sp=REJECT dis=NONE) header.from=lxnav.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233655AbiKKL2n (ORCPT + 99 others); Fri, 11 Nov 2022 06:28:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39618 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233448AbiKKL2V (ORCPT ); Fri, 11 Nov 2022 06:28:21 -0500 Received: from dhl.lxnav.com (dhl.lxnav.com [IPv6:2a01:4f8:c010:2f07::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5E3F86A772 for ; Fri, 11 Nov 2022 03:28:12 -0800 (PST) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 982964070A; Fri, 11 Nov 2022 12:28:07 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lxnav.com; s=dhl; t=1668166088; h=from:subject:date:message-id:to:mime-version: content-transfer-encoding:in-reply-to:references; bh=sduuwDJ1cpdPo1tG/vMuBFFxLHUHN1zt6Q+P4Jawaig=; b=R25EG2cP4ELm7lYu+1KM6DyIj0ArFnNkdIqozN6tS6rHE7IFVOzk8vSoUTX7eDuwY71eTo PGJqHjaJc7bD4rQnT5gPGbAhDEAph/MMwIC70Hq4A94Y/vBTyE/raLT6xR9zbA1Zd3R7KH ELJfQrO6N8R93ZeIrjhjRlOUjlUs3zs5Q/HMf5a00aMGv292ercvOUMYrSR/evWYJaupYQ iwo9pP7Zx8Gdz00TCYvbueWxtHomDdTHaIFgehqyvJ8MNMmWeX5ORfwnP53pzvZh+bB3yf 1lb7zI5NgbGLuXpORXZMXI+tf1XZXCO9bZiNst+yOxkrQl/A8ITZqA/IgkKRcg== From: Mitja Spes To: Jonathan Cameron , Lars-Peter Clausen , Mitja Spes , Angelo Compagnucci , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 4/4] iio: adc: mcp3422: reduce sleep for fast sampling rates Date: Fri, 11 Nov 2022 12:26:56 +0100 Message-Id: <20221111112657.1521307-5-mitja@lxnav.com> In-Reply-To: <20221111112657.1521307-1-mitja@lxnav.com> References: <20221111112657.1521307-1-mitja@lxnav.com> MIME-Version: 1.0 X-Last-TLS-Session-Version: TLSv1.3 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS autolearn=unavailable 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749199103974096047?= X-GMAIL-MSGID: =?utf-8?q?1749199103974096047?= - reduced sleep time for 240 & 60 sps rates - minor roundup fix for sleep times Signed-off-by: Mitja Spes --- drivers/iio/adc/mcp3422.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/drivers/iio/adc/mcp3422.c b/drivers/iio/adc/mcp3422.c index eef35fb2fc22..dbcc8fe91aaa 100644 --- a/drivers/iio/adc/mcp3422.c +++ b/drivers/iio/adc/mcp3422.c @@ -70,10 +70,11 @@ static const int mcp3422_scales[MCP3422_SRATE_COUNT][MCP3422_PGA_COUNT] = { /* Constant msleep times for data acquisitions */ static const int mcp3422_read_times[MCP3422_SRATE_COUNT] = { - [MCP3422_SRATE_240] = 1000 / 240, - [MCP3422_SRATE_60] = 1000 / 60, - [MCP3422_SRATE_15] = 1000 / 15, - [MCP3422_SRATE_3] = 1000 / 3 }; + [MCP3422_SRATE_240] = DIV_ROUND_UP(1000, 240), + [MCP3422_SRATE_60] = DIV_ROUND_UP(1000, 60), + [MCP3422_SRATE_15] = DIV_ROUND_UP(1000, 15), + [MCP3422_SRATE_3] = (100000 + 375 - 100) / 375 /* real rate is 3.75 sps */ +}; /* sample rates to integer conversion table */ static const int mcp3422_sample_rates[MCP3422_SRATE_COUNT] = { @@ -137,6 +138,7 @@ static int mcp3422_read_channel(struct mcp3422 *adc, struct iio_chan_spec const *channel, int *value) { int ret; + int sleep_duration; u8 config; u8 req_channel = channel->channel; @@ -148,7 +150,11 @@ static int mcp3422_read_channel(struct mcp3422 *adc, mutex_unlock(&adc->lock); return ret; } - msleep(mcp3422_read_times[MCP3422_SAMPLE_RATE(adc->active_config)]); + sleep_duration = mcp3422_read_times[MCP3422_SAMPLE_RATE(adc->active_config)]; + if (sleep_duration < 20) + usleep_range(sleep_duration * 1000, sleep_duration * 1300); + else + msleep(sleep_duration); } ret = mcp3422_read(adc, value, &config);