From patchwork Fri Jun 23 08:58:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herve Codina X-Patchwork-Id: 111993 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp5630194vqr; Fri, 23 Jun 2023 02:01:43 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ40KmYYyUDQ+odv5+Sebziy3UwuyKes4PHM0ljINczVJ3aHf3SQop1j3/9Eul+6oBjGW3MV X-Received: by 2002:a92:da52:0:b0:341:c05c:2813 with SMTP id p18-20020a92da52000000b00341c05c2813mr16811102ilq.0.1687510903226; Fri, 23 Jun 2023 02:01:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687510903; cv=none; d=google.com; s=arc-20160816; b=XLvg8vMPkgZbD2dUnkepqbWntOZQSS6haS5KSxvVpE7NqD3R0rHyM2orx+tk3Bwj92 EKAYvko12ZV2g71sTE6jvmSLtJsy/LpwK7O8I4rnCJO+jJgV62MNDheAZkQsGMsfBJFJ mb2IwdNyd/JRxz4FoGaCJgdY26/KfNEk45DawT6EciQlVptBcVDwXKQBtWJrSNyS3ywv 0giY+i6KLYjOjWa6/d9WAiSGthI+cdhEf4oc7TaC38sD1jcSUJUOJ0Mp+NeNUzcY5tPA VcRxQWJkFQs0YeYJQ7UxV/23UeY6l3CHdlysZNB76UzWyjYARQJIPUC5RilpvfA9Q1nr rOeg== 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 :dkim-signature; bh=LmVBaT1XSwl45bLGn3KFVgFskQ2sa3mEJW+ejheEDG4=; b=hmEcSOpIy7oCvARQMWKE5xB8DrpSGY6fKUuHLjPqqvk2FSLg8G3VJhXPl+pSmx4nqd lHf02qPyu7PxuLExhEcvFJDTo6nl4tjVcgnD0oZZVvg6l7ne+54fh3Hd2bQ4yxXPf/Bu AXorkOb3SMqXsei9n/+/c9cZhCJi+f4nwEeuEMSSWcUoxCHqL4F2eCOGE0HsxOWnVg4k QKYJW8pv5iA2i4n9UmNAgoB2sPdsAJz2uhVIafHlJy2cA2cqfGzjJCHFqf2delJIXXRW ScHRxH+Q8dKb4fiuBqa+WNN+16Y604O+Pjic5EEQIT0Yp+dkOmX0qVVK2s/PW3cLuASb tKyQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=PT79qeZ5; 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=bootlin.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g28-20020aa79f1c000000b0065bbe39e1b3si4644200pfr.146.2023.06.23.02.01.13; Fri, 23 Jun 2023 02:01:43 -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=@bootlin.com header.s=gm1 header.b=PT79qeZ5; 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=bootlin.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231935AbjFWI7o (ORCPT + 99 others); Fri, 23 Jun 2023 04:59:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42420 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232062AbjFWI7W (ORCPT ); Fri, 23 Jun 2023 04:59:22 -0400 Received: from relay4-d.mail.gandi.net (relay4-d.mail.gandi.net [IPv6:2001:4b98:dc4:8::224]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4338A213E; Fri, 23 Jun 2023 01:59:02 -0700 (PDT) X-GND-Sasl: herve.codina@bootlin.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1687510740; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=LmVBaT1XSwl45bLGn3KFVgFskQ2sa3mEJW+ejheEDG4=; b=PT79qeZ5ATXAGei2EHtmw9Ll9SzrHrts23MAxiEs48kmV+mYIKjyqryxF5IzGnCoJIPCci Ac/kjN3O2X7biLtzVeYB1KM1qPgTvVUiWnWhJX7Cz2rjSjWoyP30cF8pm5Gdxps2CbdMgP C7T6oUahuWlo+A72o3mauFbRB2Db5SGfVLD9i+LNhlUhL+an0dn9UfWF4567Tq5eSlfh2q t67iyHTsaM9sONXuLIOM26hO8hr1aqohG72feAmtKuKIbp/jFJ8hwi1VBx0iy3tBjFU12H f6JZKcVp2KWAy3EbVI76UhOKUF4kYOL/8dPc2wbgqNTYLfaUsH4U9cLgIwYnsw== X-GND-Sasl: herve.codina@bootlin.com X-GND-Sasl: herve.codina@bootlin.com X-GND-Sasl: herve.codina@bootlin.com X-GND-Sasl: herve.codina@bootlin.com X-GND-Sasl: herve.codina@bootlin.com X-GND-Sasl: herve.codina@bootlin.com X-GND-Sasl: herve.codina@bootlin.com X-GND-Sasl: herve.codina@bootlin.com X-GND-Sasl: herve.codina@bootlin.com X-GND-Sasl: herve.codina@bootlin.com X-GND-Sasl: herve.codina@bootlin.com X-GND-Sasl: herve.codina@bootlin.com X-GND-Sasl: herve.codina@bootlin.com X-GND-Sasl: herve.codina@bootlin.com X-GND-Sasl: herve.codina@bootlin.com X-GND-Sasl: herve.codina@bootlin.com X-GND-Sasl: herve.codina@bootlin.com X-GND-Sasl: herve.codina@bootlin.com X-GND-Sasl: herve.codina@bootlin.com Received: by mail.gandi.net (Postfix) with ESMTPA id C2558E000A; Fri, 23 Jun 2023 08:58:58 +0000 (UTC) From: Herve Codina To: Herve Codina , Liam Girdwood , Mark Brown , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Jonathan Cameron , Lars-Peter Clausen , Jaroslav Kysela , Takashi Iwai , Kuninori Morimoto , Andy Shevchenko Cc: alsa-devel@alsa-project.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, Wojciech Ziemba , Christophe Leroy , Thomas Petazzoni , Jonathan Cameron Subject: [PATCH v6 10/13] iio: inkern: Add a helper to query an available minimum raw value Date: Fri, 23 Jun 2023 10:58:27 +0200 Message-Id: <20230623085830.749991-11-herve.codina@bootlin.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230623085830.749991-1-herve.codina@bootlin.com> References: <20230623085830.749991-1-herve.codina@bootlin.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,SPF_HELO_PASS, SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1769483432807260435?= X-GMAIL-MSGID: =?utf-8?q?1769483432807260435?= A helper, iio_read_max_channel_raw() exists to read the available maximum raw value of a channel but nothing similar exists to read the available minimum raw value. This new helper, iio_read_min_channel_raw(), fills the hole and can be used for reading the available minimum raw value of a channel. It is fully based on the existing iio_read_max_channel_raw(). Signed-off-by: Herve Codina Reviewed-by: Andy Shevchenko Acked-by: Jonathan Cameron Reviewed-by: Christophe Leroy --- drivers/iio/inkern.c | 63 ++++++++++++++++++++++++++++++++++++ include/linux/iio/consumer.h | 12 +++++++ 2 files changed, 75 insertions(+) diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c index 19ddd77adb11..7a1f6713318a 100644 --- a/drivers/iio/inkern.c +++ b/drivers/iio/inkern.c @@ -909,6 +909,69 @@ int iio_read_max_channel_raw(struct iio_channel *chan, int *val) } EXPORT_SYMBOL_GPL(iio_read_max_channel_raw); +static int iio_channel_read_min(struct iio_channel *chan, + int *val, int *val2, int *type, + enum iio_chan_info_enum info) +{ + const int *vals; + int length; + int ret; + + ret = iio_channel_read_avail(chan, &vals, type, &length, info); + if (ret < 0) + return ret; + + switch (ret) { + case IIO_AVAIL_RANGE: + switch (*type) { + case IIO_VAL_INT: + *val = vals[0]; + break; + default: + *val = vals[0]; + if (val2) + *val2 = vals[1]; + } + return 0; + + case IIO_AVAIL_LIST: + if (length <= 0) + return -EINVAL; + switch (*type) { + case IIO_VAL_INT: + *val = min_array(vals, length); + break; + default: + /* TODO: learn about min for other iio values */ + return -EINVAL; + } + return 0; + + default: + return -EINVAL; + } +} + +int iio_read_min_channel_raw(struct iio_channel *chan, int *val) +{ + struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); + int ret; + int type; + + mutex_lock(&iio_dev_opaque->info_exist_lock); + if (!chan->indio_dev->info) { + ret = -ENODEV; + goto err_unlock; + } + + ret = iio_channel_read_min(chan, val, NULL, &type, IIO_CHAN_INFO_RAW); +err_unlock: + mutex_unlock(&iio_dev_opaque->info_exist_lock); + + return ret; +} +EXPORT_SYMBOL_GPL(iio_read_min_channel_raw); + int iio_get_channel_type(struct iio_channel *chan, enum iio_chan_type *type) { struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); diff --git a/include/linux/iio/consumer.h b/include/linux/iio/consumer.h index f536820b9cf2..e9910b41d48e 100644 --- a/include/linux/iio/consumer.h +++ b/include/linux/iio/consumer.h @@ -301,6 +301,18 @@ int iio_write_channel_raw(struct iio_channel *chan, int val); */ int iio_read_max_channel_raw(struct iio_channel *chan, int *val); +/** + * iio_read_min_channel_raw() - read minimum available raw value from a given + * channel, i.e. the minimum possible value. + * @chan: The channel being queried. + * @val: Value read back. + * + * Note, if standard units are required, raw reads from iio channels + * need the offset (default 0) and scale (default 1) to be applied + * as (raw + offset) * scale. + */ +int iio_read_min_channel_raw(struct iio_channel *chan, int *val); + /** * iio_read_avail_channel_raw() - read available raw values from a given channel * @chan: The channel being queried.