Received: by 2002:ac8:45c5:0:b0:405:464a:c27a with SMTP id e5csp1265380qto; Thu, 27 Jul 2023 09:17:16 -0700 (PDT) X-Google-Smtp-Source: APBJJlHQaFNSgbwe7mfVpzGlK/+EFqgHR7jHvUGKZ0QhCbLVtVnB33YsQWPz72uc9du4XVnnEI1r X-Received: by 2002:a17:90b:14b:b0:25b:d292:d9a3 with SMTP id em11-20020a17090b014b00b0025bd292d9a3mr5004127pjb.37.1690474636081; Thu, 27 Jul 2023 09:17:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690474636; cv=none; d=google.com; s=arc-20160816; b=dgultn8/T5x7Z93hXyhalq0LvyyFszRlzZ9sY+gpoE/0nbYj/MprtScIiaEY7rwfWB pZdFAWQkM8I8KjF8zxgw6U5jS+ejIQO0g3LF6dZ+t4Hh0GjYx+j9bzEacwIeaGb/yr8B groWPHiMZy9k6KMYsN2dstiXRaS/7fMquLX03Bz/Ef3fPrjK042jVErDWP5YekSouMwK v8UNZAlQaWdQqCyEOSmcN2W2amSIiFbldqk/AnviTfb/lxbYeG7D+bV0b57iLjx68y+h Vx1EyIOlJae3t8zexOJ/lexz5PfAiww22QtvKJmZjmDArDtbqAf49BcKD1BZe/O12OHv qoZQ== 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=Pt5jUqqdSOoqnjMTkcAHM3VXQXCIBKFvR/ivoinfamw=; fh=vilCLOZi4yx25FrNNONIv48CG1ps2oXYOAPQR4WJ75Y=; b=lk5qjOX/5gupQBau5Bh+blfGNcTwsNLMMC2zA5sTlIf4/Ktv6EPp67niPspm+6jN05 x/4MEEmXJJUFic9aiXaUI2Q/lz0VJfPZzIqhY34yJN2nLM5vP2YbXejjC47dvjHaiEbV 8z59PRB8cgcSqWp/rtyWnPI1Y8nRfZJXmCbLgpuP8LqlImxgRq+3R+J8PEZbfc0QC3e6 hoRE8v6vZwV3dlesM8I4tQSM209q2TAKoNhYJ4/aaWHZY01ZO6gJA2UsxfKcGEP5J2ej Pk3hb3sjqkVKYUC1tjayjjrPPoWInjfeQDwffZ29939wUW24LS0o/g4X1YZjGRGHohbN R68g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@foss.st.com header.s=selector1 header.b=zymx3FJu; 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=foss.st.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v10-20020a63f20a000000b0055bd8c2b3bdsi1383403pgh.153.2023.07.27.09.16.53; Thu, 27 Jul 2023 09:17: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=@foss.st.com header.s=selector1 header.b=zymx3FJu; 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=foss.st.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234356AbjG0PG1 (ORCPT + 99 others); Thu, 27 Jul 2023 11:06:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52284 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234050AbjG0PGX (ORCPT ); Thu, 27 Jul 2023 11:06:23 -0400 Received: from mx07-00178001.pphosted.com (mx07-00178001.pphosted.com [185.132.182.106]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5ABF33A99; Thu, 27 Jul 2023 08:06:02 -0700 (PDT) Received: from pps.filterd (m0288072.ppops.net [127.0.0.1]) by mx07-00178001.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 36RE2Egv003290; Thu, 27 Jul 2023 17:05:47 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foss.st.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=selector1; bh=Pt5jUqqdSOoqnjMTkcAHM3VXQXCIBKFvR/ivoinfamw=; b=zymx3FJupVczr0LBeD96k642Kl+8JvVTWEb6uyhk5jRRlfIBW/x+Nh70E3455exT2npq 4BQnfQptu3kS6UynsE+t30eo8dIY5JwzxZnU04dmu8kzntl8tV4sE8Ivk3fRyk6jrb96 bxXZr3F6b9R1jWz2r4KdooUDAdMdnWeIMQW9NEuz6g3R8fM2mZN/QKB9TzklCmuq5k/z VAwm2vUp6SGttvIwFeCRjiAmuRKg5S8nyk2soBsdjhQpyEsGfkPYpHY8mQS6Oru+lO4f lOqdSYWoqiJMkdyNGwJ9PIq95tbyz8MyxPOcsNClxW/JSUZSkheZJ8y0ZJ2qI740mbmu zg== Received: from beta.dmz-eu.st.com (beta.dmz-eu.st.com [164.129.1.35]) by mx07-00178001.pphosted.com (PPS) with ESMTPS id 3s3nyg9xc8-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 27 Jul 2023 17:05:46 +0200 Received: from euls16034.sgp.st.com (euls16034.sgp.st.com [10.75.44.20]) by beta.dmz-eu.st.com (STMicroelectronics) with ESMTP id 6F00410002A; Thu, 27 Jul 2023 17:05:46 +0200 (CEST) Received: from Webmail-eu.st.com (shfdag1node1.st.com [10.75.129.69]) by euls16034.sgp.st.com (STMicroelectronics) with ESMTP id 67EFE209BA7; Thu, 27 Jul 2023 17:05:46 +0200 (CEST) Received: from localhost (10.201.20.178) by SHFDAG1NODE1.st.com (10.75.129.69) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.21; Thu, 27 Jul 2023 17:05:46 +0200 From: Olivier Moysan To: Jonathan Cameron , Lars-Peter Clausen , Liam Girdwood , Mark Brown CC: Olivier Moysan , , Subject: [RFC v2 08/11] iio: adc: sd modulator: add scale and offset support Date: Thu, 27 Jul 2023 17:03:19 +0200 Message-ID: <20230727150324.1157933-9-olivier.moysan@foss.st.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230727150324.1157933-1-olivier.moysan@foss.st.com> References: <20230727150324.1157933-1-olivier.moysan@foss.st.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [10.201.20.178] X-ClientProxiedBy: EQNCAS1NODE3.st.com (10.75.129.80) To SHFDAG1NODE1.st.com (10.75.129.69) X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.957,Hydra:6.0.591,FMLib:17.11.176.26 definitions=2023-07-27_07,2023-07-26_01,2023-05-22_02 X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,RCVD_IN_DNSWL_LOW,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 Register the SD modulator as an IIO backend device instead of a standard IIO device. Expose scale and offset information to IIO backend consumer. Signed-off-by: Olivier Moysan --- drivers/iio/adc/sd_adc_modulator.c | 106 +++++++++++++++++++++++------ 1 file changed, 86 insertions(+), 20 deletions(-) diff --git a/drivers/iio/adc/sd_adc_modulator.c b/drivers/iio/adc/sd_adc_modulator.c index 327cc2097f6c..e77e7884c403 100644 --- a/drivers/iio/adc/sd_adc_modulator.c +++ b/drivers/iio/adc/sd_adc_modulator.c @@ -6,44 +6,110 @@ * Author: Arnaud Pouliquen . */ +#include #include #include #include #include #include +#include -static const struct iio_info iio_sd_mod_iio_info; +struct iio_sd_mod_priv { + struct regulator *vref; + int vref_mv; +}; -static const struct iio_chan_spec iio_sd_mod_ch = { - .type = IIO_VOLTAGE, - .indexed = 1, - .scan_type = { - .sign = 'u', - .realbits = 1, - .shift = 0, - }, +static int sd_mod_enable(struct iio_backend *backend) +{ + struct iio_sd_mod_priv *priv = backend->priv; + int ret; + + ret = regulator_enable(priv->vref); + if (ret) { + dev_err(&backend->dev, "Failed to enable regulator: %d\n", ret); + return ret; + } + + ret = regulator_get_voltage(priv->vref); + priv->vref_mv = ret / 1000; + + return 0; +}; + +static int sd_mod_disable(struct iio_backend *backend) +{ + struct iio_sd_mod_priv *priv = backend->priv; + + return regulator_disable(priv->vref); +}; + +static int sd_mod_read(struct iio_backend *backend, int *val, int *val2, long mask) +{ + struct iio_sd_mod_priv *priv = backend->priv; + + switch (mask) { + case IIO_CHAN_INFO_SCALE: + *val = priv->vref_mv; + *val2 = 0; + return IIO_VAL_INT; + + case IIO_CHAN_INFO_OFFSET: + *val = 0; + *val2 = 0; + return IIO_VAL_INT; + } + + return -EINVAL; +}; + +static const struct iio_backend_ops sd_mod_ops = { + .enable = sd_mod_enable, + .disable = sd_mod_disable, + .read_raw = sd_mod_read, }; static int iio_sd_mod_probe(struct platform_device *pdev) { struct device *dev = &pdev->dev; - struct iio_dev *iio; + struct regulator *vref; + struct iio_backend *backend; + struct iio_sd_mod_priv *priv; + int ret; - iio = devm_iio_device_alloc(dev, 0); - if (!iio) + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); + if (!priv) return -ENOMEM; - iio->name = dev_name(dev); - iio->info = &iio_sd_mod_iio_info; - iio->modes = INDIO_BUFFER_HARDWARE; + vref = devm_regulator_get_optional(dev, "vref"); + if (IS_ERR(vref)) { + if (PTR_ERR(vref) != -ENODEV) + return dev_err_probe(dev, PTR_ERR(vref), "Failed to get vref\n"); + } else { + priv->vref = vref; + } - iio->num_channels = 1; - iio->channels = &iio_sd_mod_ch; + backend = iio_backend_alloc(dev); + if (!backend) { + dev_err(dev, "Failed to allocate sd modulator backend data\n"); + return -ENOMEM; + } + + backend->priv = priv; + backend->ops = &sd_mod_ops; + + ret = iio_backend_register(backend); + if (ret) { + dev_err(dev, "Failed to register backend: %d\n", ret); + goto err_free_backend; + } - platform_set_drvdata(pdev, iio); + return 0; - return devm_iio_device_register(&pdev->dev, iio); -} +err_free_backend: + iio_backend_free(&backend->dev); + + return ret; +}; static const struct of_device_id sd_adc_of_match[] = { { .compatible = "sd-modulator" }, -- 2.25.1