From patchwork Tue Nov 28 12:57:33 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Klinger X-Patchwork-Id: 10080257 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id DD7B460353 for ; Tue, 28 Nov 2017 12:57:57 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0444A28438 for ; Tue, 28 Nov 2017 12:57:58 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EC6C32881A; Tue, 28 Nov 2017 12:57:57 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5F17828438 for ; Tue, 28 Nov 2017 12:57:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752534AbdK1M5h (ORCPT ); Tue, 28 Nov 2017 07:57:37 -0500 Received: from mout.kundenserver.de ([212.227.17.10]:50705 "EHLO mout.kundenserver.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752483AbdK1M5f (ORCPT ); Tue, 28 Nov 2017 07:57:35 -0500 Received: from localhost ([46.78.234.13]) by mrelayeu.kundenserver.de (mreue103 [212.227.15.183]) with ESMTPSA (Nemesis) id 0MRB2F-1egzkQ1wpr-00UcXi; Tue, 28 Nov 2017 13:57:17 +0100 Date: Tue, 28 Nov 2017 13:57:33 +0100 From: Andreas Klinger To: jic23@kernel.org, knaack.h@gmx.de, lars@metafoo.de, pmeerw@pmeerw.net, singhalsimran0@gmail.com, linux-iio@vger.kernel.org Cc: linux-kernel@vger.kernel.org Subject: [PATCH 1/2] iio: hx711: add triggered buffer support Message-ID: <20171128125733.GA31831@arbeit> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) X-Provags-ID: V03:K0:qXSw5EbTy9XfXoreSoKDzb+4JI3wVh9FLQSzLuvzhBTFdgpjjyb Vv7qUbvXCN1PmCzxfu8fBbFm2waV8Z4IuSnf+ZaRS+9vvkLkbapm4BoYX1UuvQT5aGpDpAQ WHE/y3Kh4fAognpoiljSyF2ppMPiAf5v+uAZTeREBOGz2gXnkN0On0wg56bFd94Dq34hFsc 0STBU1hsVXY58mBpm45kw== X-UI-Out-Filterresults: notjunk:1; V01:K0:Lo2iEuHp1Pw=:y8Wb2BbNjx821dMQo8TTya Is3SmEQUScICnqzhbPMoSqV/KKDSNDfl3nirpS2CzB3buHLwm4aq96R1dPB/BkZYVOeqI4DHr agpp/HnK+B1dAHNYSONHi/8i4lXbttFmXkoOSbKKc5G/4qyej0gur6L2oBYH9pgnxjn7H4Knk 43sf0hedHWRKZaDanHPrgQDPQVhGYOgUf1NjtyTrPCySt+NyfnMtwdXshLPzaTHtFh195kW7d 5hk6wryrKrvEY6FXgVIP7BhIEd+c67mXk3a7+RrGmvN59pcXWU21TpXq+sAQqB+0oMKqUcr+g vNm/jogVnSNZNr9wxKGAK3hmG6FtKW0ME6zcrr9rjC9NeblMQov1oEgSxJDO13hK/+r4mqOD+ nWXJ6zfcG8F9LJNeozVMG0hb+gNBt1POtatAyXDIM/OJ3FXODBRGUo+1EyaDl6ColICZveqf1 W3Dsd88AsuQ9am3L4nJ7y3p9/zX/vW3lvfj6UP4A5iat9QGo2r4RTygsnpD50SFsm5QkVo545 /I/Fsg2BpyxIZQhusRDL+LUVVmhF632zVE539Lu465Y4tmPzWpMub5kxqT4AlunM5SHoRYsQM C4DNEV7tMJGOo+zm04Pu/6DY+DqdDOsCTTZW3h7C+MIYLZO9NFdFUQRiIC+ea3UF83ZvtiUtE uXJ8TSagqVeFjIAA9IQUzNkWssNUs24KwiIMe+qBpnsVQBpFwpTBwsRT9rp3mGqbZFjCFOivZ YUWZY2UyKbGhRlfkaXCPPU4poH19QJR5B0C3ww== Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Add buffer to device data struct and add trigger function Data format is quite simple: voltage - channel 0 32 Bit voltage - channel 1 32 Bit timestamp 64 Bit Using both channels at the same time is working quite slow because of changing the channel which needs a dummy read. Signed-off-by: Andreas Klinger --- drivers/iio/adc/hx711.c | 106 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 88 insertions(+), 18 deletions(-) diff --git a/drivers/iio/adc/hx711.c b/drivers/iio/adc/hx711.c index d10b9f13d557..d773fd69a76d 100644 --- a/drivers/iio/adc/hx711.c +++ b/drivers/iio/adc/hx711.c @@ -24,6 +24,9 @@ #include #include #include +#include +#include +#include #include #include @@ -89,6 +92,11 @@ struct hx711_data { int gain_set; /* gain set on device */ int gain_chan_a; /* gain for channel A */ struct mutex lock; + /* + * triggered buffer + * 2x32-bit channel + 2x32-bit timestamp + */ + u32 buffer[4]; }; static int hx711_cycle(struct hx711_data *hx711_data) @@ -236,34 +244,43 @@ static int hx711_set_gain_for_channel(struct hx711_data *hx711_data, int chan) return 0; } +static int hx711_reset_read(struct hx711_data *hx711_data, int chan) +{ + int ret; + int val; + + /* + * hx711_reset() must be called from here + * because it could be calling hx711_read() by itself + */ + if (hx711_reset(hx711_data)) { + mutex_unlock(&hx711_data->lock); + dev_err(hx711_data->dev, "reset failed!"); + return -EIO; + } + + ret = hx711_set_gain_for_channel(hx711_data, chan); + if (ret < 0) { + mutex_unlock(&hx711_data->lock); + return ret; + } + + val = hx711_read(hx711_data); + + return val; +} + static int hx711_read_raw(struct iio_dev *indio_dev, const struct iio_chan_spec *chan, int *val, int *val2, long mask) { struct hx711_data *hx711_data = iio_priv(indio_dev); - int ret; switch (mask) { case IIO_CHAN_INFO_RAW: mutex_lock(&hx711_data->lock); - /* - * hx711_reset() must be called from here - * because it could be calling hx711_read() by itself - */ - if (hx711_reset(hx711_data)) { - mutex_unlock(&hx711_data->lock); - dev_err(hx711_data->dev, "reset failed!"); - return -EIO; - } - - ret = hx711_set_gain_for_channel(hx711_data, chan->channel); - if (ret < 0) { - mutex_unlock(&hx711_data->lock); - return ret; - } - - *val = hx711_read(hx711_data); + *val = hx711_reset_read(hx711_data, chan->channel); mutex_unlock(&hx711_data->lock); @@ -339,6 +356,35 @@ static int hx711_write_raw_get_fmt(struct iio_dev *indio_dev, return IIO_VAL_INT_PLUS_NANO; } +static irqreturn_t hx711_trigger(int irq, void *p) +{ + struct iio_poll_func *pf = p; + struct iio_dev *indio_dev = pf->indio_dev; + struct hx711_data *hx711_data = iio_priv(indio_dev); + int i; + + mutex_lock(&hx711_data->lock); + + memset(hx711_data->buffer, 0, sizeof(hx711_data->buffer)); + + for (i = 0; i < indio_dev->masklength; i++) { + if (!test_bit(i, indio_dev->active_scan_mask)) + continue; + + hx711_data->buffer[i] = hx711_reset_read(hx711_data, + indio_dev->channels[i].channel); + } + + iio_push_to_buffers_with_timestamp(indio_dev, hx711_data->buffer, + pf->timestamp); + + mutex_unlock(&hx711_data->lock); + + iio_trigger_notify_done(indio_dev->trig); + + return IRQ_HANDLED; +} + static ssize_t hx711_scale_available_show(struct device *dev, struct device_attribute *attr, char *buf) @@ -387,6 +433,14 @@ static const struct iio_chan_spec hx711_chan_spec[] = { .indexed = 1, .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), + .scan_index = 0, + .scan_type = { + .sign = 'u', + .realbits = 24, + .storagebits = 32, + .shift = 0, + .endianness = IIO_CPU, + }, }, { .type = IIO_VOLTAGE, @@ -394,7 +448,16 @@ static const struct iio_chan_spec hx711_chan_spec[] = { .indexed = 1, .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), + .scan_index = 1, + .scan_type = { + .sign = 'u', + .realbits = 24, + .storagebits = 32, + .shift = 0, + .endianness = IIO_CPU, + }, }, + IIO_CHAN_SOFT_TIMESTAMP(2), }; static int hx711_probe(struct platform_device *pdev) @@ -482,6 +545,13 @@ static int hx711_probe(struct platform_device *pdev) indio_dev->channels = hx711_chan_spec; indio_dev->num_channels = ARRAY_SIZE(hx711_chan_spec); + ret = devm_iio_triggered_buffer_setup(dev, indio_dev, + iio_pollfunc_store_time, hx711_trigger, NULL); + if (ret < 0) { + dev_err(dev, "setup of iio triggered buffer failed\n"); + return ret; + } + ret = iio_device_register(indio_dev); if (ret < 0) { dev_err(dev, "Couldn't register the device\n");