From patchwork Mon Jul 3 10:39:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Leonard_G=C3=B6hrs?= X-Patchwork-Id: 115310 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f45:0:b0:3ea:f831:8777 with SMTP id v5csp437459vqx; Mon, 3 Jul 2023 04:08:28 -0700 (PDT) X-Google-Smtp-Source: APBJJlH03XjWRbRWDGeSoHyFdpCzjN6TkqG+r/OTB2mj5ylC69K1YpUxOoWH7CwiPAEe8dpDZdll X-Received: by 2002:a05:6a00:26dd:b0:66d:d03e:3493 with SMTP id p29-20020a056a0026dd00b0066dd03e3493mr12549582pfw.2.1688382508427; Mon, 03 Jul 2023 04:08:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688382508; cv=none; d=google.com; s=arc-20160816; b=hf/jhQz+LuXbfNvHBCKpnSaHJeYwIbd/SucDKGJ0tJRWX61Tk6gv7+kGa/u5GAgzSq m1JtxaUqDNZNadwZDApYH0N0ktkPKRqHEW4mhJbza4/J9eqfSeCYbczh9pR1KselD/Uo sjvABNWIv2KBU/p1Uk47lYKY3tuaUY0s+7H/hWvEERyF2+nmTXJJ8o4zehPaAYs4ivVB wIQH2t4EZh5fzFO51CHZ69Cbt3TCrTvHzLzdbPICCdYfYRfhk4pHAyx03k84Gy1wCpgB XUAkuHuFZ/2dUDWsI6iv8wahSYPrnif/Z7uIWCnUdAgPKEtx41FeIUBbxmLhdSxEn6sD yIxQ== 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 :message-id:date:subject:cc:to:from; bh=jNeBglWCSy7QvuNh+PBfdi4Qo1GjsV9IAnK7dYLKoUE=; fh=Y4ZOQ1YwBzs/KZ7JU9xAz6SFF0bKk1h1cCG3crWumXk=; b=gh9mbuaqiMhdOMArzC+s/VFevLpWo0DY6ejgQeWO9r4F3ehgIc5DkRJmtTeSwseSJi S1BgMGbC0KUOajRpNQLyIjXbhZD3zcz5f24zwUoiQm02+2jXv4INd5fvpFGGDq63n7VB XpN9gyNxKfXnSzEg5+TBMP7NF5AMVQGJ+Fyr00A+0jgY9kkr053jSPWoKFR3v6L3aRRz BVBXfsXgC4zTljKJNr8hJbVe/CEgHzv+tDQoZak3Ozqk40W7AqZQ6mQ3dgj7UXmNpZRU aoBolfDdklEzAWhreUlLt2eyan2OVa55woSWXDNCome/jCUudlzs+isG+wo/d/jujj5S jKxA== ARC-Authentication-Results: i=1; mx.google.com; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u11-20020a056a00098b00b0064d28b68d8fsi16257412pfg.94.2023.07.03.04.08.15; Mon, 03 Jul 2023 04:08:28 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230507AbjGCKj4 (ORCPT + 99 others); Mon, 3 Jul 2023 06:39:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56204 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229562AbjGCKjy (ORCPT ); Mon, 3 Jul 2023 06:39:54 -0400 Received: from metis.ext.pengutronix.de (metis.ext.pengutronix.de [IPv6:2001:67c:670:201:290:27ff:fe1d:cc33]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 24623C1 for ; Mon, 3 Jul 2023 03:39:53 -0700 (PDT) Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.ext.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qGGyA-0007es-JH; Mon, 03 Jul 2023 12:39:46 +0200 Received: from [2a0a:edc0:0:1101:1d::39] (helo=dude03.red.stw.pengutronix.de) by drehscheibe.grey.stw.pengutronix.de with esmtp (Exim 4.94.2) (envelope-from ) id 1qGGy9-00BmVe-MT; Mon, 03 Jul 2023 12:39:45 +0200 Received: from lgo by dude03.red.stw.pengutronix.de with local (Exim 4.94.2) (envelope-from ) id 1qGGy8-000Nz6-Ea; Mon, 03 Jul 2023 12:39:44 +0200 From: =?utf-8?q?Leonard_G=C3=B6hrs?= To: =?utf-8?q?Leonard_G=C3=B6hrs?= , kernel@pengutronix.de, Jonathan Cameron , Lars-Peter Clausen Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3] iio: adc: add buffering support to the TI LMP92064 ADC driver Date: Mon, 3 Jul 2023 12:39:07 +0200 Message-Id: <20230703103908.27691-1-l.goehrs@pengutronix.de> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 X-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: lgo@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1770397376904664234?= X-GMAIL-MSGID: =?utf-8?q?1770397376904664234?= Enable buffered reading of samples from the LMP92064 ADC. The main benefit of this change is being able to read out current and voltage measurements in a single transfer, allowing instantaneous power measurements. Reads into the buffer can be triggered by any software triggers, e.g. the iio-trig-hrtimer: $ mkdir /sys/kernel/config/iio/triggers/hrtimer/my-trigger $ cat /sys/bus/iio/devices/iio\:device3/name lmp92064 $ iio_readdev -t my-trigger -b 16 iio:device3 | hexdump WARNING: High-speed mode not enabled 0000000 0000 0176 0101 0001 5507 abd5 7645 1768 0000010 0000 016d 0101 0001 ee1e ac6b 7645 1768 ... Signed-off-by: Leonard Göhrs --- Changes v1->v2: - Remove superfluous .shift = 0 initialization in scan_type. - Replace kmalloc buffer allocation for every read with a stack allocated structure. A struct is used to ensure correct alignment of the timestamp field. See e.g. adc/rockchip_saradc.c for other users of the same pattern. - Use available_scan_masks and always push both voltage and current measurements to the buffer. Changes v2->v3: - Handle errors returned by lmp92064_read_meas() out-of-line via a goto err; --- drivers/iio/adc/ti-lmp92064.c | 51 +++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) base-commit: 6995e2de6891c724bfeb2db33d7b87775f913ad1 diff --git a/drivers/iio/adc/ti-lmp92064.c b/drivers/iio/adc/ti-lmp92064.c index c30ed824924f3..73504a27a8e22 100644 --- a/drivers/iio/adc/ti-lmp92064.c +++ b/drivers/iio/adc/ti-lmp92064.c @@ -16,7 +16,10 @@ #include #include +#include #include +#include +#include #define TI_LMP92064_REG_CONFIG_A 0x0000 #define TI_LMP92064_REG_CONFIG_B 0x0001 @@ -91,6 +94,12 @@ static const struct iio_chan_spec lmp92064_adc_channels[] = { .address = TI_LMP92064_CHAN_INC, .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), + .scan_index = TI_LMP92064_CHAN_INC, + .scan_type = { + .sign = 'u', + .realbits = 12, + .storagebits = 16, + }, .datasheet_name = "INC", }, { @@ -98,8 +107,20 @@ static const struct iio_chan_spec lmp92064_adc_channels[] = { .address = TI_LMP92064_CHAN_INV, .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), + .scan_index = TI_LMP92064_CHAN_INV, + .scan_type = { + .sign = 'u', + .realbits = 12, + .storagebits = 16, + }, .datasheet_name = "INV", }, + IIO_CHAN_SOFT_TIMESTAMP(2), +}; + +static const unsigned long lmp92064_scan_masks[] = { + BIT(TI_LMP92064_CHAN_INC) | BIT(TI_LMP92064_CHAN_INV), + 0 }; static int lmp92064_read_meas(struct lmp92064_adc_priv *priv, u16 *res) @@ -171,6 +192,30 @@ static int lmp92064_read_raw(struct iio_dev *indio_dev, } } +static irqreturn_t lmp92064_trigger_handler(int irq, void *p) +{ + struct iio_poll_func *pf = p; + struct iio_dev *indio_dev = pf->indio_dev; + struct lmp92064_adc_priv *priv = iio_priv(indio_dev); + struct { + u16 values[2]; + int64_t timestamp; + } data; + int ret; + + ret = lmp92064_read_meas(priv, data.values); + if (ret) + goto err; + + iio_push_to_buffers_with_timestamp(indio_dev, &data, + iio_get_time_ns(indio_dev)); + +err: + iio_trigger_notify_done(indio_dev->trig); + + return IRQ_HANDLED; +} + static int lmp92064_reset(struct lmp92064_adc_priv *priv, struct gpio_desc *gpio_reset) { @@ -301,6 +346,12 @@ static int lmp92064_adc_probe(struct spi_device *spi) indio_dev->channels = lmp92064_adc_channels; indio_dev->num_channels = ARRAY_SIZE(lmp92064_adc_channels); indio_dev->info = &lmp92064_adc_info; + indio_dev->available_scan_masks = lmp92064_scan_masks; + + ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL, + lmp92064_trigger_handler, NULL); + if (ret) + return dev_err_probe(dev, ret, "Failed to setup buffered read\n"); return devm_iio_device_register(dev, indio_dev); }