From patchwork Fri Apr 21 12:41:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herve Codina X-Patchwork-Id: 86286 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1038608vqo; Fri, 21 Apr 2023 05:46:57 -0700 (PDT) X-Google-Smtp-Source: AKy350ZxdtIv/Hg4UQ8f3R9Tv/uat7BED2kEbRBLVK/RN3tdkktNYmuxFzt2nmg7rLkBEBUyuIkG X-Received: by 2002:a05:6a00:1582:b0:63d:3ddb:5f3f with SMTP id u2-20020a056a00158200b0063d3ddb5f3fmr6947085pfk.0.1682081217209; Fri, 21 Apr 2023 05:46:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682081217; cv=none; d=google.com; s=arc-20160816; b=XAfzN3sDnh8BSgROAPS9nAeiTNjvWIB4X0vDN2pRFIZzrOIvUfZAwpkkzIYoJW8O1v ZiL290UmlWiqqwUfFEqwRT4uSZFKcVVceeBIm+Ei1dqpysOpJTeCVjLEcsDS+CjgQ3IK NZsDw+Z6CW61LePnauQ4/PMiccuqPlMycHRtmMhbCP8lk0aUdSUt/bSrmgdymI3Y1BUB tCW10tiKMxXwOl4JTDqfUkF/FGGLhWmy6L1WBEfCDsvFpyB15TEUc3dWupCJImSPKVuI jDU+YkPJJX7WLTg9PWrWR+qb7MEEBQxhWFEysxfPkb5/VEoRysxam30o0GOJd2FL9fuP 9XeA== 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=eNsCq0s00cdcExGMd2Rls3HSwLDqQCPjKQ7JzV6nFDI=; b=wki0TtI3HiJbINScqPx2cYP9F1fVtbuDDKK+H+snGcpu04NeFXpRuDtIbAnlgApq+o 0EfXEr+AM6aDyVgVwmMNsR6L9b7DI5lw0zU5L/XqMIYs8gqXPsx6NIKN1yOsF9R/7cbT HmuRFRBVblOJW8mh8P6/KY+QYV/rtcEVOek8PwNi8e6XJceEhEs8zrbzl7NsJdfIE5WW pkYTZMw+3BED/pZZ09J9Nu9NcV/PdY8mYJI19EWLFu+LVzeYTZ5X3gID1u4BZZ+wsOcx G70qUNo5A8w0yqSDjxvQck2BngmLLj1ZIpe30U7XJ+ncbZiU4Y97XLnejMf4uLpiS7As ejdQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=QClt+BCD; 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 m127-20020a625885000000b0063b652ff9cdsi3075520pfb.404.2023.04.21.05.46.44; Fri, 21 Apr 2023 05:46:57 -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=QClt+BCD; 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 S229575AbjDUMmZ (ORCPT + 99 others); Fri, 21 Apr 2023 08:42:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44908 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231921AbjDUMmW (ORCPT ); Fri, 21 Apr 2023 08:42:22 -0400 Received: from relay2-d.mail.gandi.net (relay2-d.mail.gandi.net [IPv6:2001:4b98:dc4:8::222]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CDA4C10240; Fri, 21 Apr 2023 05:41:47 -0700 (PDT) Received: (Authenticated sender: herve.codina@bootlin.com) by mail.gandi.net (Postfix) with ESMTPA id AE9A740007; Fri, 21 Apr 2023 12:41:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1682080904; 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=eNsCq0s00cdcExGMd2Rls3HSwLDqQCPjKQ7JzV6nFDI=; b=QClt+BCDWwhb08qssJkO5Mnz+mTsc0qr/vnqlDJjJEEnPe741wrKNRdxSsaP97cjKQTq8k V/t7Ho58TgZ/3/rfOvyfx2K2zkrC2idEVmE770AjuXb762FnlLP9MAkTTviN5KZagfNwAN oK/4u1tuS1uddfmahTeg3M9d4IGuyEO6ORp5N2H3V+c/xyAAJAakmemGJQWLKhzwP5Nj2A r0/rkKgSUcWd9noVnD5iPj5ExEAS+Obu6XGQxQyMI2zzCgglewV1/4fBWnR8hpL4Sfb89t 8QMtFhBIq0TcfacVoQzq9qX3RRkxehIi/xuD2p8Anq0GkyVxQaCQhDSjBum9iw== From: Herve Codina To: Herve Codina , Liam Girdwood , Mark Brown , Rob Herring , Krzysztof Kozlowski , Jonathan Cameron , Lars-Peter Clausen , Jaroslav Kysela , Takashi Iwai Cc: alsa-devel@alsa-project.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, Christophe Leroy , Thomas Petazzoni Subject: [PATCH 1/4] dt-bindings: sound: Add simple-iio-aux Date: Fri, 21 Apr 2023 14:41:19 +0200 Message-Id: <20230421124122.324820-2-herve.codina@bootlin.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230421124122.324820-1-herve.codina@bootlin.com> References: <20230421124122.324820-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_NONE, 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?1763789994567689631?= X-GMAIL-MSGID: =?utf-8?q?1763789994567689631?= Industrial I/O devices can be present in the audio path. These devices needs to be viewed as audio components in order to be fully integrated in the audio path. simple-iio-aux allows to consider these Industrial I/O devices as auxliary audio devices. Signed-off-by: Herve Codina --- .../bindings/sound/simple-iio-aux.yaml | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 Documentation/devicetree/bindings/sound/simple-iio-aux.yaml diff --git a/Documentation/devicetree/bindings/sound/simple-iio-aux.yaml b/Documentation/devicetree/bindings/sound/simple-iio-aux.yaml new file mode 100644 index 000000000000..fab128fce4fc --- /dev/null +++ b/Documentation/devicetree/bindings/sound/simple-iio-aux.yaml @@ -0,0 +1,65 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/sound/simple-iio-aux.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Simple IIO auxiliary + +maintainers: + - Herve Codina + +description: | + Auxiliary device based on Industrial I/O device channels + +allOf: + - $ref: /schemas/iio/iio-consumer.yaml + - $ref: dai-common.yaml# + +properties: + compatible: + const: simple-iio-aux + + io-channels: + description: + Industrial I/O device channels used + + io-channel-names: + description: + Industrial I/O channel names related to io-channels. + These names are used to provides sound controls, widgets and routes names. + + invert: + $ref: /schemas/types.yaml#/definitions/uint32-array + description: | + A list of 0/1 flags defining whether or not the related channel is + inverted + items: + enum: [0, 1] + default: 0 + description: | + Invert the sound control value compared to the IIO channel raw value. + - 1: The related sound control value is inverted meaning that the + minimum sound control value correspond to the maximum IIO channel + raw value and the maximum sound control value correspond to the + minimum IIO channel raw value. + - 0: The related sound control value is not inverted meaning that the + minimum (resp maximum) sound control value correspond to the + minimum (resp maximum) IIO channel raw value. + +required: + - compatible + - io-channels + - io-channel-names + +unevaluatedProperties: false + +examples: + - | + aux { + compatible = "simple-iio-aux"; + io-channels = <&iio 0>, <&iio 1>, <&iio 2>, <&iio 3>; + io-channel-names = "CH0", "CH1", "CH2", "CH3"; + /* Invert CH1 and CH2 */ + invert = <0 1 1>; + }; From patchwork Fri Apr 21 12:41:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herve Codina X-Patchwork-Id: 86289 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1040692vqo; Fri, 21 Apr 2023 05:50:45 -0700 (PDT) X-Google-Smtp-Source: AKy350aPjSP/z4175NP5ugjQkxk76Jh+NqWTCCKnjM156wmwJyDzWwlEDgnKHLFysoPU55xyMuIF X-Received: by 2002:a17:90b:3e8b:b0:246:c3e1:c931 with SMTP id rj11-20020a17090b3e8b00b00246c3e1c931mr4551974pjb.23.1682081445054; Fri, 21 Apr 2023 05:50:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682081445; cv=none; d=google.com; s=arc-20160816; b=RXgWl7Arzw3XzyqJ62hLKvrqPhyWpsViNV7vR9J4vYMwAR8FPq1iiEu1B/sHnpLRJb 6ji2QcvqgxTb5DBDBw5ZlNLnCVAL6PYL7WZX28tPZCyAq/1EFp/mw24B6R5ARu7/r7wf V8ck3IWFNYSF6da3CqKbvqLhiSLM1OH4YiTq0yGbr3dD4sPL2vXmRDgzpcgIHNIdj11Q BAF5UbnSDVHD74mfKlBEDKgUXtl8Ia/8++ruMyeDY1pEFG878TFoNxRGqXylUNbucMa2 2F4oCKcRhLUxBh5UhTnDvUSptJ+0xZYRtoVqdKS/UCNn3gyKZ5aQia84LNrpHpa970Gs Zogg== 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=ecGQES+IctsZxeHGmbZiv99zRKwWx/U6QMun/YsCg54=; b=LTIPTRCWo2K0EBGu1AH2wEMVPXybM/4SRAc8+ZNI1bY5Hag6oiJXiJ+zZhlFgMS4tu CFRuDfKlM5giZXwm5vyNxEIfdf0y2IL+LXj7nIr70fC/1xaCSSebseDkb/t1BV/k8vzC EK/boZiqGRoV0MPmkwlqUyhhtodDaKEheoO1xZ61EwTutSaI9IyZKbEy1vOgFuy3pfrJ vVcqHZtOLLUhSooBrYz+gWoW9qLz1BEMWRTRJvCKeYCr3MfK3KxRWHKNjI7dnQeckYBc 0EFLw6l9zjvsYa3wga6WcpOAfIEMwZQ2YutloZ57cuclOUtF432RlKOl6DbCzN488UUU OZgg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=Y2Uy4aX0; 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 x62-20020a638641000000b00520a879b9cesi4326349pgd.3.2023.04.21.05.50.31; Fri, 21 Apr 2023 05:50:45 -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=Y2Uy4aX0; 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 S231936AbjDUMme (ORCPT + 99 others); Fri, 21 Apr 2023 08:42:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45148 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232135AbjDUMmb (ORCPT ); Fri, 21 Apr 2023 08:42:31 -0400 Received: from relay2-d.mail.gandi.net (relay2-d.mail.gandi.net [IPv6:2001:4b98:dc4:8::222]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BA63FD309; Fri, 21 Apr 2023 05:42:01 -0700 (PDT) Received: (Authenticated sender: herve.codina@bootlin.com) by mail.gandi.net (Postfix) with ESMTPA id 036DE40004; Fri, 21 Apr 2023 12:41:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1682080915; 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=ecGQES+IctsZxeHGmbZiv99zRKwWx/U6QMun/YsCg54=; b=Y2Uy4aX0fJLJin3UbJuHlp6xKyONaXP+q/4E6traJCaV4jgmMdm3TsvfcgIBmqlsoee0CF zT9VC+AnT8ScfxXu35DaKXKyN6XZvWtUjwQULTMwh1h5C0fRNf2CwmQWLmbfleahnmYjXX SiI8pO1SgPpnwt3yS6RAHsotNbZlJdp3oPLXK5KZudsMH/cQGQy27Yy5X0sWHIZ/BZMdIV h3GITU6Nxe+bCB+QzISMmVKb1l6g/nrEiMkI0RPMX6YxPeQ3ClFV9qjERP4OZmjJCKbKd9 DtCX5vjcjqW7EPRdRpqZLWwK6U9FfWcsg03OZmma3d+DiL2V/zpDZMehsrVBvw== From: Herve Codina To: Herve Codina , Liam Girdwood , Mark Brown , Rob Herring , Krzysztof Kozlowski , Jonathan Cameron , Lars-Peter Clausen , Jaroslav Kysela , Takashi Iwai Cc: alsa-devel@alsa-project.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, Christophe Leroy , Thomas Petazzoni Subject: [PATCH 2/4] iio: inkern: Add a helper to query an available minimum raw value Date: Fri, 21 Apr 2023 14:41:20 +0200 Message-Id: <20230421124122.324820-3-herve.codina@bootlin.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230421124122.324820-1-herve.codina@bootlin.com> References: <20230421124122.324820-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_NONE, 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?1763790233222601062?= X-GMAIL-MSGID: =?utf-8?q?1763790233222601062?= 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 --- drivers/iio/inkern.c | 67 ++++++++++++++++++++++++++++++++++++ include/linux/iio/consumer.h | 11 ++++++ 2 files changed, 78 insertions(+) diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c index 872fd5c24147..914fc69c718a 100644 --- a/drivers/iio/inkern.c +++ b/drivers/iio/inkern.c @@ -912,6 +912,73 @@ 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) +{ + int unused; + const int *vals; + int length; + int ret; + + if (!val2) + val2 = &unused; + + ret = iio_channel_read_avail(chan, &vals, type, &length, info); + switch (ret) { + case IIO_AVAIL_RANGE: + switch (*type) { + case IIO_VAL_INT: + *val = vals[0]; + break; + default: + *val = vals[0]; + *val2 = vals[1]; + } + return 0; + + case IIO_AVAIL_LIST: + if (length <= 0) + return -EINVAL; + switch (*type) { + case IIO_VAL_INT: + *val = vals[--length]; + while (length) { + if (vals[--length] < *val) + *val = vals[length]; + } + break; + default: + /* FIXME: learn about min for other iio values */ + return -EINVAL; + } + return 0; + + default: + return ret; + } +} + +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 6802596b017c..956120d8b5a3 100644 --- a/include/linux/iio/consumer.h +++ b/include/linux/iio/consumer.h @@ -297,6 +297,17 @@ 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 raw reads from iio channels are in adc counts and hence + * scale will need to be applied if standard units are required. + */ +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. From patchwork Fri Apr 21 12:41:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herve Codina X-Patchwork-Id: 86292 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1040932vqo; Fri, 21 Apr 2023 05:51:17 -0700 (PDT) X-Google-Smtp-Source: AKy350asetiFSUHsA7Dw7/L51t7OKHBXoImwI5wr5lfYltjp+Qp/IbZSYKX+s2qRGLKx3NVX/lE5 X-Received: by 2002:a17:903:2447:b0:1a6:fe25:4129 with SMTP id l7-20020a170903244700b001a6fe254129mr5431800pls.0.1682081476948; Fri, 21 Apr 2023 05:51:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682081476; cv=none; d=google.com; s=arc-20160816; b=QZbF7Ldd5pJGpuej0cLFtfA2MHULYeiM075IV4T9NxsiuXQtKJuYWzds0Ce7EI7LRg NmiWo1toZqGJhAmIKW8qgAaGWruUxkpb56IO9dAZyOXH7XCzhBeqL13xBDPVY2kWXvKS Snaj58sQz7IWO8ceaE+x03ikL6aOAdo5WdzsaVu0JnEig6UConH/21VL15U0hojcbBRO /ymnHCDpkCxp+65npDEXHP09FsU6C7R8fpSm8aljOkiC8Zc4f7s+JdpjdI4tiRAGe+yE Ym5pO9zLgglDt4fRTBQ0d8Psl7PmDnq6dABd6aNz2jK6+mKbY4cKNITo1uGiNM8N1bow 4zcg== 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=ReUlzkjE4Wu5oxkDFDACc0QGLsw/liPYYYIVObV0csA=; b=tFoXOY8IskYecWfUo+6XPTEJDbUlGIAABNZeCm1SD58ug6iyiu6F7PERFJODy88kAE C0JLiynlmYlt8yWxxp13gk0zMKmuM8PqOPGvl4O++jMoA1e6oy9qvJ7AowDv78juI5QQ vndoeVUlRM+Igukk9FMpV9p5moDApk2Wq0VPI0truTL3ERbG/6MpuSxhWho6p7TP3us8 zK9Gv0bQ47eHjNky7rer/D/Rb8tAT8oG18JbSCNb0z5kZun48t1PSzkwJMsxXHcsaOme zqqJ++cOuXp+WESpAmyNX1CW4W6t/tI8MuBFtvdiPCFnhBg1dXrQZhPwn5RXdKQR49cr KcWA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=EBy4SLN3; 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 k2-20020a170902d58200b001a69cb5f7besi4375095plh.525.2023.04.21.05.51.04; Fri, 21 Apr 2023 05:51:16 -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=EBy4SLN3; 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 S232268AbjDUMmo (ORCPT + 99 others); Fri, 21 Apr 2023 08:42:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45152 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232109AbjDUMmj (ORCPT ); Fri, 21 Apr 2023 08:42:39 -0400 Received: from relay2-d.mail.gandi.net (relay2-d.mail.gandi.net [217.70.183.194]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D17F8902A; Fri, 21 Apr 2023 05:42:12 -0700 (PDT) Received: (Authenticated sender: herve.codina@bootlin.com) by mail.gandi.net (Postfix) with ESMTPA id AFC324000A; Fri, 21 Apr 2023 12:42:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1682080921; 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=ReUlzkjE4Wu5oxkDFDACc0QGLsw/liPYYYIVObV0csA=; b=EBy4SLN3K1xBNjJvOdgJT6PUfEQL9bFvtJnkKZY46KL21NgKdoRGRLuXY2Ng5RVJCaQUsK GUAjgQdTVTG41iA642TMsVC4zRI8weITdr7eKSHZvuCV1Q5xCkm8Jbl77IjpdbpUulDUiX AsR9HA/raMQtAMUCJOFuJPfaI0RECjhMyOz7Albr5ZYlsRCx1iz32soaxO3gFjAiprgSuv cIxyMyYohgaG33z6C43NJ8liKsqhp27aY2E8che4fAx5gExva8v9hS1l3cwoZzOoA0YZ43 4sasYv521gJLU5JtIUz/jn78fj2GCk6QXZiocudJcA47ux7IaLWyQXpxTA88AQ== From: Herve Codina To: Herve Codina , Liam Girdwood , Mark Brown , Rob Herring , Krzysztof Kozlowski , Jonathan Cameron , Lars-Peter Clausen , Jaroslav Kysela , Takashi Iwai Cc: alsa-devel@alsa-project.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, Christophe Leroy , Thomas Petazzoni Subject: [PATCH 3/4] ASoC: soc-dapm.h: Add a helper to build a DAPM widget dynamically Date: Fri, 21 Apr 2023 14:41:21 +0200 Message-Id: <20230421124122.324820-4-herve.codina@bootlin.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230421124122.324820-1-herve.codina@bootlin.com> References: <20230421124122.324820-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, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,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?1763790266860529636?= X-GMAIL-MSGID: =?utf-8?q?1763790266860529636?= The SND_SOC_DAPM_* helpers family are used to build widgets array in a static way. Introduce SND_SOC_DAPM_WIDGET() in order to use the SND_SOC_DAPM_* helpers family in a dynamic way. The different SND_SOC_DAPM_* parameters can be computed by the code and the widget can be built based on this parameter computation. For instance: static int create_widget(char *input_name) { struct snd_soc_dapm_widget widget; char name*; ... name = input_name; if (!name) name = "default"; widget = SND_SOC_DAPM_WIDGET(SND_SOC_DAPM_INPUT(name)); ... } Signed-off-by: Herve Codina --- include/sound/soc-dapm.h | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/include/sound/soc-dapm.h b/include/sound/soc-dapm.h index 64915ebd641e..d6bb97fba8c9 100644 --- a/include/sound/soc-dapm.h +++ b/include/sound/soc-dapm.h @@ -276,7 +276,17 @@ struct soc_enum; .reg = SND_SOC_NOPM, .event = dapm_pinctrl_event, \ .event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD } - +/* + * Helper to create a widget 'dynamically' + * This can be used with any of the SND_SOC_DAPM_* widget helper. + * For instance: + * struct snd_soc_dapm_widget widget; + * ... + * widget = SND_SOC_DAPM_WIDGET(SND_SOC_DAPM_INPUT(input_name)); + */ +#define SND_SOC_DAPM_WIDGET(_widget)({ \ + struct snd_soc_dapm_widget _w[1] = { _widget }; \ + _w[0]; }) /* dapm kcontrol types */ #define SOC_DAPM_DOUBLE(xname, reg, lshift, rshift, max, invert) \ From patchwork Fri Apr 21 12:41:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herve Codina X-Patchwork-Id: 86293 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1041074vqo; Fri, 21 Apr 2023 05:51:34 -0700 (PDT) X-Google-Smtp-Source: AKy350ZbzqbrqOQLXm4HhqLnhkzHECNC3uYgLw4n1OSZETmc3zTIG2pwpnthZSU+PPlAzlR3Mu+i X-Received: by 2002:a17:90a:c90d:b0:240:1014:a1b3 with SMTP id v13-20020a17090ac90d00b002401014a1b3mr4797329pjt.35.1682081494197; Fri, 21 Apr 2023 05:51:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682081494; cv=none; d=google.com; s=arc-20160816; b=hUV7D6+Fqm4f434vH7rBK32boGNlqsI71ZGNWluFUi0W3LdRt+9BHksS9UQHkBiPKb wRfQfuKHHTSn0YruodKIxG3E2q0bqyAebT/GTRKlvfJLRU6pZLMi7cV76ofOVY/nG1FW C7QhttYcNGy0C/ooH1PJy/gfXIsVn6NTZPq82Ux72EomTF5GtEhinQ06CaoLkJfMlKDL SGkaRLsQnHzeWfywvaxmRoVlDfig7dns6H+s9Ados6vQKbJZ8CZlyZkQMDikUge/e1od aLFCHK72DxzGuMhPWZYatcKwgUunJvED/eDoFaRl+zcWajOBsVQ4+osr3Ep0O+xYZAcj 8qsw== 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=169cNx1n7ZwFZR/MsassqaqgZLC71GGKyN2GetFRRIU=; b=S+aBQbtM2YQWLjt/1ShG/nBkHXM+V7eosWJB9UtHRxVPqSx11c6SNDzwb+o4ORykky XHb3lfKmGfNMSTzwK2eemV6fKYV0DT7DGU7wu/uposQMS09+1TvYSloNy2ye5Qy49hsK eKwObgWMUMi/KHI8wDgX93003kISt87CWhST7kKAjZiHMBZOzgz0IVrVlQOnoid3QCVt rAerLn4vm45d2OAQW8qvV9QKhk3UUWRkYH+uejRx/JE+aSZUsEuf9+B72Q4fTwz2Qnhj 6nYcXa1EbN5sTk6TTIefU4CRPLB2RNXPRuPqRaOjIlBOegIr7UEjng5GmsX8x5Q5wdIU wIew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=LugssXjr; 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 ne5-20020a17090b374500b0024709ef2225si4413910pjb.84.2023.04.21.05.51.21; Fri, 21 Apr 2023 05:51:34 -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=LugssXjr; 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 S232344AbjDUMn2 (ORCPT + 99 others); Fri, 21 Apr 2023 08:43:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45154 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232351AbjDUMnP (ORCPT ); Fri, 21 Apr 2023 08:43:15 -0400 Received: from relay2-d.mail.gandi.net (relay2-d.mail.gandi.net [217.70.183.194]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D2DA8CC16; Fri, 21 Apr 2023 05:42:45 -0700 (PDT) Received: (Authenticated sender: herve.codina@bootlin.com) by mail.gandi.net (Postfix) with ESMTPA id D1B2940002; Fri, 21 Apr 2023 12:42:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1682080963; 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=169cNx1n7ZwFZR/MsassqaqgZLC71GGKyN2GetFRRIU=; b=LugssXjr9Rhn0BhOfTceS1X+dPtcN7Fy/iBAVZrAT1BBweYuSNVmaU2HYy58w4qYKOQtFs sytRpTW18QdAm3IQny91Lq5Qle9WkA81VKKvR8MMHflc3UYhg/5RxLSRrjw1kwrXfgEzOh 07K44MwrFpOHaQr1kwsOeZxUD3IJV7rnxVe4qdVSo9KIgZe2dNEJlFb7CYHoJy2Hr7Mgx9 0xWy3r75PmbFiYf4xsKF2Gr5N3e+J81WKGSV41LXDEB/OBg9PmAeRYcihxRkBZtlgxJ1Rz F/RXzj7lAnRxMb/ENX1QV9DPOv6zUdmz0+NVDED3v1NHw1QpDmvIcXHj9sSuJQ== From: Herve Codina To: Herve Codina , Liam Girdwood , Mark Brown , Rob Herring , Krzysztof Kozlowski , Jonathan Cameron , Lars-Peter Clausen , Jaroslav Kysela , Takashi Iwai Cc: alsa-devel@alsa-project.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, Christophe Leroy , Thomas Petazzoni Subject: [PATCH 4/4] ASoC: codecs: Add support for the generic IIO auxiliary devices Date: Fri, 21 Apr 2023 14:41:22 +0200 Message-Id: <20230421124122.324820-5-herve.codina@bootlin.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230421124122.324820-1-herve.codina@bootlin.com> References: <20230421124122.324820-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, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,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?1763790284982640246?= X-GMAIL-MSGID: =?utf-8?q?1763790284982640246?= Industrial I/O devices can be present in the audio path. These devices needs to be used as audio components in order to be fully integrated in the audio path. This support allows to consider these Industrial I/O devices as auxliary audio devices and allows to control them using mixer controls. Signed-off-by: Herve Codina --- sound/soc/codecs/Kconfig | 12 ++ sound/soc/codecs/Makefile | 2 + sound/soc/codecs/simple-iio-aux.c | 307 ++++++++++++++++++++++++++++++ 3 files changed, 321 insertions(+) create mode 100644 sound/soc/codecs/simple-iio-aux.c diff --git a/sound/soc/codecs/Kconfig b/sound/soc/codecs/Kconfig index 4f78da914fc7..ee87e0125bfd 100644 --- a/sound/soc/codecs/Kconfig +++ b/sound/soc/codecs/Kconfig @@ -211,6 +211,7 @@ config SND_SOC_ALL_CODECS imply SND_SOC_SGTL5000 imply SND_SOC_SI476X imply SND_SOC_SIMPLE_AMPLIFIER + imply SND_SOC_SIMPLE_IIO_AUX imply SND_SOC_SIMPLE_MUX imply SND_SOC_SMA1303 imply SND_SOC_SPDIF @@ -1555,6 +1556,17 @@ config SND_SOC_SIGMADSP_REGMAP config SND_SOC_SIMPLE_AMPLIFIER tristate "Simple Audio Amplifier" +config SND_SOC_SIMPLE_IIO_AUX + tristate "Simple IIO Auxiliary device" + depends on IIO + help + Enable support for Industrial I/O devices as audio auxiliary devices. + This allows to have an IIO device present in the audio path and + controlled using mixer controls. + + To compile this driver as a module, choose M here: the module + will be called snd-soc-simple-iio-aux. + config SND_SOC_SIMPLE_MUX tristate "Simple Audio Mux" depends on GPIOLIB diff --git a/sound/soc/codecs/Makefile b/sound/soc/codecs/Makefile index 11bd66d46f7b..f2b1ee22b57c 100644 --- a/sound/soc/codecs/Makefile +++ b/sound/soc/codecs/Makefile @@ -359,6 +359,7 @@ snd-soc-zl38060-objs := zl38060.o snd-soc-max9877-objs := max9877.o snd-soc-max98504-objs := max98504.o snd-soc-simple-amplifier-objs := simple-amplifier.o +snd-soc-simple-iio-aux-objs := simple-iio-aux.o snd-soc-tpa6130a2-objs := tpa6130a2.o snd-soc-tas2552-objs := tas2552.o snd-soc-tas2562-objs := tas2562.o @@ -729,6 +730,7 @@ obj-$(CONFIG_SND_SOC_ZL38060) += snd-soc-zl38060.o obj-$(CONFIG_SND_SOC_MAX9877) += snd-soc-max9877.o obj-$(CONFIG_SND_SOC_MAX98504) += snd-soc-max98504.o obj-$(CONFIG_SND_SOC_SIMPLE_AMPLIFIER) += snd-soc-simple-amplifier.o +obj-$(CONFIG_SND_SOC_SIMPLE_IIO_AUX) += snd-soc-simple-iio-aux.o obj-$(CONFIG_SND_SOC_TPA6130A2) += snd-soc-tpa6130a2.o obj-$(CONFIG_SND_SOC_LPASS_MACRO_COMMON) += snd-soc-lpass-macro-common.o obj-$(CONFIG_SND_SOC_LPASS_WSA_MACRO) += snd-soc-lpass-wsa-macro.o diff --git a/sound/soc/codecs/simple-iio-aux.c b/sound/soc/codecs/simple-iio-aux.c new file mode 100644 index 000000000000..77f7fb11e416 --- /dev/null +++ b/sound/soc/codecs/simple-iio-aux.c @@ -0,0 +1,307 @@ +// SPDX-License-Identifier: GPL-2.0-only +// +// simple-iio-aux.c -- ALSA SoC glue to use IIO devices as audio components +// +// Copyright 2023 CS GROUP France +// +// Author: Herve Codina + +#include +#include +#include +#include +#include + +struct simple_iio_aux_chan { + struct iio_channel *iio_chan; + const char *name; + bool is_inverted; + int max; + int min; +}; + +struct simple_iio_aux { + struct device *dev; + struct simple_iio_aux_chan *chans; + unsigned int num_chans; +}; + +static int simple_iio_aux_info_volsw(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_info *uinfo) +{ + struct simple_iio_aux_chan *chan = + (struct simple_iio_aux_chan *)kcontrol->private_value; + + uinfo->count = 1; + uinfo->value.integer.min = 0; + uinfo->value.integer.max = chan->max - chan->min; + uinfo->type = (uinfo->value.integer.max == 1) ? + SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER; + return 0; +} + +static int simple_iio_aux_get_volsw(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct simple_iio_aux_chan *chan = (struct simple_iio_aux_chan *)kcontrol->private_value; + int max = chan->max; + int min = chan->min; + unsigned int mask = (1 << fls(max)) - 1; + unsigned int invert = chan->is_inverted; + int ret; + int val; + + ret = iio_read_channel_raw(chan->iio_chan, &val); + if (ret < 0) + return ret; + + ucontrol->value.integer.value[0] = (val & mask) - min; + if (invert) + ucontrol->value.integer.value[0] = max - ucontrol->value.integer.value[0]; + + return 0; +} + +static int simple_iio_aux_put_volsw(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct simple_iio_aux_chan *chan = (struct simple_iio_aux_chan *)kcontrol->private_value; + int max = chan->max; + int min = chan->min; + unsigned int mask = (1 << fls(max)) - 1; + unsigned int invert = chan->is_inverted; + int val; + int ret; + int tmp; + + val = ucontrol->value.integer.value[0]; + if (val < 0) + return -EINVAL; + if (val > max - min) + return -EINVAL; + + val = (val + min) & mask; + if (invert) + val = max - val; + + ret = iio_read_channel_raw(chan->iio_chan, &tmp); + if (ret < 0) + return ret; + + if (tmp == val) + return 0; + + ret = iio_write_channel_raw(chan->iio_chan, val); + if (ret) + return ret; + + return 1; /* The value changed */ +} + +static int simple_iio_aux_add_controls(struct snd_soc_component *component, + struct simple_iio_aux_chan *chan) +{ + struct snd_kcontrol_new control = {0}; + + control.iface = SNDRV_CTL_ELEM_IFACE_MIXER; + control.name = chan->name; + control.info = simple_iio_aux_info_volsw; + control.get = simple_iio_aux_get_volsw; + control.put = simple_iio_aux_put_volsw; + control.private_value = (unsigned long)chan; + + return snd_soc_add_component_controls(component, &control, 1); +} + +/* + * These data could be on stack but they are pretty big. + * As ASoC internally copy them and protect them against concurrent accesses + * (snd_soc_bind_card() protects using client_mutex), keep them in the global + * data area. + */ +static struct snd_soc_dapm_widget widgets[3] = {0}; +static struct snd_soc_dapm_route routes[2] = {0}; + +static int simple_iio_aux_add_dapms(struct snd_soc_component *component, + struct simple_iio_aux_chan *chan) +{ + struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component); + char *input_name = NULL; + char *output_name = NULL; + char *pga_name = NULL; + int ret; + + input_name = kasprintf(GFP_KERNEL, "%s IN", chan->name); + if (!input_name) { + ret = -ENOMEM; + goto end; + } + output_name = kasprintf(GFP_KERNEL, "%s OUT", chan->name); + if (!output_name) { + ret = -ENOMEM; + goto end; + } + pga_name = kasprintf(GFP_KERNEL, "%s PGA", chan->name); + if (!pga_name) { + ret = -ENOMEM; + goto end; + } + + BUILD_BUG_ON(ARRAY_SIZE(widgets) < 3); + widgets[0] = SND_SOC_DAPM_WIDGET(SND_SOC_DAPM_INPUT(input_name)); + widgets[1] = SND_SOC_DAPM_WIDGET(SND_SOC_DAPM_OUTPUT(output_name)); + widgets[2] = SND_SOC_DAPM_WIDGET(SND_SOC_DAPM_PGA(pga_name, SND_SOC_NOPM, 0, 0, NULL, 0)); + ret = snd_soc_dapm_new_controls(dapm, widgets, 3); + if (ret) + goto end; + + BUILD_BUG_ON(ARRAY_SIZE(routes) < 2); + routes[0].sink = pga_name; + routes[0].control = NULL; + routes[0].source = input_name; + routes[1].sink = output_name; + routes[1].control = NULL; + routes[1].source = pga_name; + ret = snd_soc_dapm_add_routes(dapm, routes, 2); + +end: + /* Allocated names are no more needed (duplicated in ASoC internals) */ + kfree(pga_name); + kfree(output_name); + kfree(input_name); + + return ret; +} + +static int simple_iio_aux_component_probe(struct snd_soc_component *component) +{ + struct simple_iio_aux *iio_aux = snd_soc_component_get_drvdata(component); + struct simple_iio_aux_chan *chan; + int ret; + int i; + + for (i = 0; i < iio_aux->num_chans; i++) { + chan = iio_aux->chans + i; + + ret = iio_read_max_channel_raw(chan->iio_chan, &chan->max); + if (ret) { + dev_err(component->dev, "chan[%d] %s: Cannot get max raw value (%d)\n", + i, chan->name, ret); + return ret; + } + + ret = iio_read_min_channel_raw(chan->iio_chan, &chan->min); + if (ret) { + dev_err(component->dev, "chan[%d] %s: Cannot get min raw value (%d)\n", + i, chan->name, ret); + return ret; + } + + /* Set initial value */ + ret = iio_write_channel_raw(chan->iio_chan, + chan->is_inverted ? chan->max : chan->min); + if (ret) { + dev_err(component->dev, "chan[%d] %s: Cannot set initial value (%d)\n", + i, chan->name, ret); + return ret; + } + + ret = simple_iio_aux_add_controls(component, chan); + if (ret) + return ret; + + ret = simple_iio_aux_add_dapms(component, chan); + if (ret) + return ret; + + dev_dbg(component->dev, "chan[%d]: Added %s (min=%d, max=%d, invert=%s)\n", + i, chan->name, chan->min, chan->max, + chan->is_inverted ? "on" : "off"); + } + + return 0; +} + +static const struct snd_soc_component_driver simple_iio_aux_component_driver = { + .probe = simple_iio_aux_component_probe, +}; + +static int simple_iio_aux_probe(struct platform_device *pdev) +{ + struct device_node *np = pdev->dev.of_node; + struct simple_iio_aux_chan *iio_aux_chan; + struct simple_iio_aux *iio_aux; + int count; + u32 tmp; + int ret; + int i; + + iio_aux = devm_kzalloc(&pdev->dev, sizeof(*iio_aux), GFP_KERNEL); + if (!iio_aux) + return -ENOMEM; + + iio_aux->dev = &pdev->dev; + + count = of_property_count_strings(np, "io-channel-names"); + if (count < 0) { + dev_err(iio_aux->dev, "%pOF: failed to read io-channel-names\n", np); + return count; + } + + iio_aux->chans = devm_kmalloc_array(&pdev->dev, count, + sizeof(*iio_aux->chans), GFP_KERNEL); + if (!iio_aux->chans) + return -ENOMEM; + iio_aux->num_chans = count; + + for (i = 0; i < iio_aux->num_chans; i++) { + iio_aux_chan = iio_aux->chans + i; + + ret = of_property_read_string_index(np, "io-channel-names", i, + &iio_aux_chan->name); + if (ret < 0) { + dev_err(iio_aux->dev, "%pOF: failed to read io-channel-names[%d]\n", np, i); + return ret; + } + + iio_aux_chan->iio_chan = devm_iio_channel_get(iio_aux->dev, iio_aux_chan->name); + if (IS_ERR(iio_aux_chan->iio_chan)) { + ret = PTR_ERR(iio_aux_chan->iio_chan); + return dev_err_probe(iio_aux->dev, ret, + "get IIO channel '%s' failed (%d)\n", + iio_aux_chan->name, ret); + } + + tmp = 0; + of_property_read_u32_index(np, "invert", i, &tmp); + iio_aux_chan->is_inverted = !!tmp; + } + + platform_set_drvdata(pdev, iio_aux); + + return devm_snd_soc_register_component(iio_aux->dev, + &simple_iio_aux_component_driver, + NULL, 0); +} + +#ifdef CONFIG_OF +static const struct of_device_id simple_iio_aux_ids[] = { + { .compatible = "simple-iio-aux", }, + { } +}; +MODULE_DEVICE_TABLE(of, simple_iio_aux_ids); +#endif + +static struct platform_driver simple_iio_aux_driver = { + .driver = { + .name = "simple-iio-aux", + .of_match_table = of_match_ptr(simple_iio_aux_ids), + }, + .probe = simple_iio_aux_probe, +}; + +module_platform_driver(simple_iio_aux_driver); + +MODULE_AUTHOR("Herve Codina "); +MODULE_DESCRIPTION("IIO ALSA SoC aux driver"); +MODULE_LICENSE("GPL");