Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp31501351rwd; Thu, 6 Jul 2023 23:41:55 -0700 (PDT) X-Google-Smtp-Source: APBJJlFA/XVgT6Vq/2Mm5vTEPPB1IM8+TwbcYT+otn069lv98XTG87KM3YgjMJZbDXInJB0rY09+ X-Received: by 2002:a05:6870:4253:b0:1b0:5218:ceff with SMTP id v19-20020a056870425300b001b05218ceffmr4534973oac.18.1688712115364; Thu, 06 Jul 2023 23:41:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688712115; cv=none; d=google.com; s=arc-20160816; b=x4oB1fburc5/J7FzGlD0r4IwrN5Wm03DE4I5PxgIEkox/0gca67S53XIwW5d9u+8D6 8plr7oUbJZlJovzKdhCqFiObSq6tFWfcyZ/NLG9t9BnY4cl3AW+barBwS6WyeMrR0UFR Iwkb0w4HBV82zoNOII6u54+DrNV4BLQoWAunPefslHPTKGZ57095YzveRrwiRg+pI0X3 57Mkw4pjeU3EQ49OFYZjcPh4mIwMUFIrhyi50QAwPcu8YlIMb6F5TvqJmVVmWbUjSYEC 6t5v5A1saDOEukz6DZpMGT7WoibmX8QzmQpy4G0uBQqL+SIq8EzQ0I6yNhfNclJo/0hh h8Hg== 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=Wfr1tLVVEKZmH/OUcOfBlXPETsR/q4oFb7u8MLWIths=; fh=cIeb4lFeuqc1/O8izX6NllAgdP/Y6o89OD5aBKtWiVQ=; b=pRKTBs89Z7hvnbbWUFwZXWW5ksytzcaxQLcJKSAXwcldvBviAXwzWjIkIZdHkQuGVA 6AJL7tqvOI6ZUUX7ypwMGVzGrQ4j3Me36SlyE4Lqm5eTpRZESllZvGA/JpdpmxuofM4A JgRpu3C159H5aTEqmpWRFu0fdWgkCGzRskPysWgU3DHnnwC0Bm+XQuCv2hh5WWERmX42 z51ql4nhNyCMPKoQCZiULBJgdbZI/q9UUON9gCKkDN+Vlv1xdkwWlcDm2jheYEF8D0N1 kM3Iywap9DLZTWj6Wf3kVzrdfz687+jgc2uTm57e2kHWF7bLlGjPrw5laQe14DxixWN+ FSwA== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c20-20020a6566d4000000b0053439c1712esi3091904pgw.713.2023.07.06.23.41.43; Thu, 06 Jul 2023 23:41:55 -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 S231779AbjGGGhK (ORCPT + 99 others); Fri, 7 Jul 2023 02:37:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39182 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229778AbjGGGhJ (ORCPT ); Fri, 7 Jul 2023 02:37:09 -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 E48241FC9 for ; Thu, 6 Jul 2023 23:37:07 -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 1qHf5P-0005iO-2X; Fri, 07 Jul 2023 08:36:59 +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 1qHf5N-00Cfj7-S9; Fri, 07 Jul 2023 08:36:57 +0200 Received: from lgo by dude03.red.stw.pengutronix.de with local (Exim 4.94.2) (envelope-from ) id 1qHf5N-006HOk-1v; Fri, 07 Jul 2023 08:36:57 +0200 From: =?UTF-8?q?Leonard=20G=C3=B6hrs?= To: =?UTF-8?q?Nuno=20S=C3=A1?= , =?UTF-8?q?Leonard=20G=C3=B6hrs?= , kernel@pengutronix.de, Jonathan Cameron , Lars-Peter Clausen Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4] iio: adc: add buffering support to the TI LMP92064 ADC driver Date: Fri, 7 Jul 2023 08:36:35 +0200 Message-Id: <20230707063635.1496437-1-l.goehrs@pengutronix.de> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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=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 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; Changes v3->v4: - 8-Byte align the 64 bit timestamp in the buffer structure and memset() the stack-allocated buffer to prevent leaking kernel memory to userspace. (Thanks to Nuno Sá) --- drivers/iio/adc/ti-lmp92064.c | 53 +++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/drivers/iio/adc/ti-lmp92064.c b/drivers/iio/adc/ti-lmp92064.c index c30ed824924f3..84ba5c4a0eea7 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,32 @@ 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 __aligned(8); + } data; + int ret; + + memset(&data, 0, sizeof(data)); + + 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 +348,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); } base-commit: 6995e2de6891c724bfeb2db33d7b87775f913ad1 -- 2.39.2