Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756937AbcJ3QBU (ORCPT ); Sun, 30 Oct 2016 12:01:20 -0400 Received: from mail-ve1eur01on0139.outbound.protection.outlook.com ([104.47.1.139]:33395 "EHLO EUR01-VE1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1754090AbcJ3QBR (ORCPT ); Sun, 30 Oct 2016 12:01:17 -0400 X-Greylist: delayed 3664 seconds by postgrey-1.27 at vger.kernel.org; Sun, 30 Oct 2016 12:01:16 EDT Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=peda@axentia.se; Subject: Re: [PATCH v3 8/8] iio: envelope-detector: ADC driver based on a DAC and a comparator To: Jonathan Cameron , References: <1477262381-7800-1-git-send-email-peda@axentia.se> <1477262381-7800-9-git-send-email-peda@axentia.se> <61cd873a-b6ff-50d5-1161-e9b1ad90af88@kernel.org> CC: Hartmut Knaack , Lars-Peter Clausen , Peter Meerwald-Stadler , Rob Herring , Mark Rutland , , , Thomas Gleixner From: Peter Rosin Organization: Axentia Technologies AB Message-ID: Date: Sun, 30 Oct 2016 15:26:45 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 In-Reply-To: <61cd873a-b6ff-50d5-1161-e9b1ad90af88@kernel.org> Content-Type: text/plain; charset="windows-1252" Content-Transfer-Encoding: 7bit X-Originating-IP: [217.210.101.82] X-ClientProxiedBy: DB3PR01CA0069.eurprd01.prod.exchangelabs.com (10.242.133.172) To VI1PR0201MB2319.eurprd02.prod.outlook.com (10.168.63.21) X-MS-Office365-Filtering-Correlation-Id: 9d00a364-f58d-4098-ee80-08d400d0cb1c X-Microsoft-Exchange-Diagnostics: 1;VI1PR0201MB2319;2:RNzq6tBFGptVa5xrQqGJbpUYPKCLnkkvVOgD4CH+bMVQZsXrdTGIzPfsp55CRcLUPEg69sYfO6Ukj5L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iKy11mp1IuRGClM272Szciyi9mVfRNv0n7DyMtKkBeCZCQ==;4:8LqY4EB/T99SQDOyJ+Qqi25yNLsAiMKkaZ2wnwSCr0kSkSzLzB3FV3+SzfGBpDWb5OXV0to9uIYCYYvS/av7p85UGPtoNmHSHeT9NUjSZQuM37iLo4E9cvMxGsutY1snTLwSmQizsHvIVGBYoml4QEbUhuLc/ulfGMaHl+7slifR2XPKYhxo+87C4AmAV8FLuw9dBoAHEicIi1eepfSSR+x18SI38OfWfGrSJcGqDdyeyNVKoeS7QzKIJ/+yUppFB3u/YrUmMqo3KrUW3jRRWHyUlegnxk4c4pli5RjIeh/0klY9YNy8862T5Xz6S21MSD7+ggM35fmSLeHnXi+a+gQPPTd6QXG09Um5YTxG1knlLAEqT5pLSgPRU7kdzQzRHydVG3bMDW1pF/dKwa/owh0rwqAAE7uiN0A+wVsU0DsUIncJ6y21p9AEbEUgAYNeMZQCJOigzOVsyDwEvOs5zW44ho1+BmPk/seD3vfChI0= X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(9452136761055); X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(6040176)(601004)(2401047)(5005006)(8121501046)(3002001)(10201501046)(6043046)(6042046);SRVR:VI1PR0201MB2319;BCL:0;PCL:0;RULEID:;SRVR:VI1PR0201MB2319; X-Forefront-PRVS: 01110342A5 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10019020)(4630300001)(6009001)(6049001)(7916002)(377424004)(189002)(199003)(52314003)(24454002)(50944005)(81166006)(97736004)(81156014)(230700001)(54356999)(31686004)(65826007)(2906002)(42186005)(36756003)(23746002)(19580405001)(4001350100001)(50986999)(65956001)(47776003)(83506001)(5660300001)(66066001)(117156001)(19580395003)(65806001)(4326007)(8676002)(76176999)(50466002)(105586002)(6116002)(31696002)(68736007)(7846002)(7736002)(2950100002)(64126003)(86362001)(106356001)(4001150100001)(3846002)(575784001)(74482002)(189998001)(7416002)(8666005)(586003)(305945005)(5001770100001)(77096005)(33646002)(101416001)(6666003)(92566002)(7059030)(2004002)(42262002);DIR:OUT;SFP:1102;SCL:1;SRVR:VI1PR0201MB2319;H:[192.168.0.125];FPR:;SPF:None;PTR:InfoNoRecords;MX:1;A:1;LANG:en; X-Microsoft-Exchange-Diagnostics: =?Windows-1252?Q?1;VI1PR0201MB2319;23:GqudGyPZJ86OXz9rRPL6JfrmDPapz0VqXR1?= =?Windows-1252?Q?YKlSwmypRdlMpRvZkkZUynuiZG5j6Wac1Tgnyerkul3f5LsxWe8r/YKF?= =?Windows-1252?Q?4DTSoaOeutFJ19SBGOM8VpZOJE4urWuIniqOL2+gCvU1PSeBmz1og5SD?= =?Windows-1252?Q?XNZ+DuiX011bms+QSehOpUv4YjKqoSL90ZQUV/JPzeCEQClbFgLzfGPm?= =?Windows-1252?Q?VV9kW66O3J66GrM/XAyrzqjOCS0QPcbMC9IqWsRg5brEUuypzIgbzAu2?= =?Windows-1252?Q?nu/sGCFbLVopW2+3Y7VFAV3bCHVbPON5+P3OASIu5AJeKRZWOSOYKCcm?= =?Windows-1252?Q?en/FWpWjubjNPElXo8RxMp9Avz0XBt33gndGqwIbXEdksnEJzcgzILPE?= =?Windows-1252?Q?4emsSU4VmasJbmNySqHdrY915fH7d1yKgSr19PB5m98h+dOGZcVrBKfc?= =?Windows-1252?Q?XFY4iQhsqWLczuuy3WwKEAIEO2cyLkN+x4duuvnAEVL0EI7bKXyTaEpg?= =?Windows-1252?Q?gnfKVUEfsO2DgA2ky7Q5zC4dR3a2x9eFDz506l3lTu6scS6l4SkTbdgy?= =?Windows-1252?Q?/D04xxGIHsqoyd1eSBvPkoVYfll+7pZhgHo/E2jdIXuB50fF2rS/nti1?= =?Windows-1252?Q?VefI0xns1LhEexTBl63sH33fmL6FQD+JUmfSrSkVeV55HZDO1KvoQ8WJ?= =?Windows-1252?Q?uMf7hiVxGTz2mwWpiuZqQVs26aWih3b9kEYXHXxXVG1D71LlR/cGQRuT?= =?Windows-1252?Q?HJLtjybfKoxNioB0j0u9tT8V33pVM3x2Szyh1E+jkB2Fsaohj+CthTtC?= =?Windows-1252?Q?tlZCOynmCIcYH8Eu5Xn2eMkVzlyyONK2CyJhTjOtieQ+cNQwuyUBP9yB?= =?Windows-1252?Q?2ByTFxdzY91MxBSz1CIlVlzoUqnLDXTmD2j/KltXznjS//QD9r9fkG4J?= =?Windows-1252?Q?xOVee3+HR+vUo3qTgH/3ia13NQW6kQOsCG1omZFxgwiSdbEIiMLhguDn?= =?Windows-1252?Q?eeoZJRuMQwZfJyGRKuIXXLBG7IOsimySiSCthWeqRbxB0YiZBDNHIMNy?= =?Windows-1252?Q?aFA56yUCTsv553UJer/ap2//YktOJUV3I7mZS74N9+LuZ07HO9NXmwgR?= =?Windows-1252?Q?zITf4EuJ4DsVcGuH9MoEyxWOVKt50zSGG9TUM/rnMV/V9l2QZLxgp4q1?= =?Windows-1252?Q?JWgn3kNGaKSsfCBA7Huo/b7prMQmJasz1DvJj19eFysVfnMsJ9h7t1/q?= =?Windows-1252?Q?vcaEVDGbIc6eS4tFudBwymfzrbWCZJcdyjE7ZWdVZLU/khsF9wHMprDe?= =?Windows-1252?Q?uh9r+nsG920JJnPv1zbmeOC31BcVANOqTyF+jeoveSMm6MFP3GGbvwZM?= =?Windows-1252?Q?kv9AFqI4Y67OXFmnhNPD32sYCd8XtRM6akd+VGAucTeSJFtvW3M/lu5o?= =?Windows-1252?Q?q96oNQbN8LF3fl0uV8Lji6GOwQzMschNW9lUbUk7YEwMjxJdQ/M6tFyB?= =?Windows-1252?Q?uXv69d+WXn2BwE1v2TFGSky5rfeTMD26bG5D2zN4aJpxduTr99O3kRbG?= =?Windows-1252?Q?vl2G0dZVIUHcnxww1WQ0ipS9FsEIcfN7jJByMvWrgO68mGkyNG2sBDhR?= =?Windows-1252?Q?0XOANSPNldaGnfwvXVgutyNKzIb4kGCIixyqKFT+kCGWT?= X-Microsoft-Exchange-Diagnostics: 1;VI1PR0201MB2319;6:WhDpnfR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change-CrossTenant-OriginalArrivalTime: 30 Oct 2016 14:26:50.3081 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR0201MB2319 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 18770 Lines: 566 On 2016-10-30 14:44, Jonathan Cameron wrote: > On 23/10/16 23:39, Peter Rosin wrote: >> The DAC is used to find the peak level of an alternating voltage input >> signal by a binary search using the output of a comparator wired to >> an interrupt pin. Like so: >> _ >> | \ >> input +------>-------|+ \ >> | \ >> .-------. | }---. >> | | | / | >> | dac|-->--|- / | >> | | |_/ | >> | | | >> | | | >> | irq|------<-------' >> | | >> '-------' >> >> Signed-off-by: Peter Rosin > Looks good to me. I've cc'd Thomas Gleixner as the irq usage is obviously > a bit unusual and I thought he may have some thoughts on it. > > Jonathan >> --- >> .../testing/sysfs-bus-iio-adc-envelope-detector | 36 ++ >> MAINTAINERS | 2 + >> drivers/iio/adc/Kconfig | 10 + >> drivers/iio/adc/Makefile | 1 + >> drivers/iio/adc/envelope-detector.c | 422 +++++++++++++++++++++ >> 5 files changed, 471 insertions(+) >> create mode 100644 Documentation/ABI/testing/sysfs-bus-iio-adc-envelope-detector >> create mode 100644 drivers/iio/adc/envelope-detector.c >> >> diff --git a/Documentation/ABI/testing/sysfs-bus-iio-adc-envelope-detector b/Documentation/ABI/testing/sysfs-bus-iio-adc-envelope-detector >> new file mode 100644 >> index 000000000000..2071f9bcfaa5 >> --- /dev/null >> +++ b/Documentation/ABI/testing/sysfs-bus-iio-adc-envelope-detector >> @@ -0,0 +1,36 @@ >> +What: /sys/bus/iio/devices/iio:deviceX/in_altvoltageY_invert >> +Date: October 2016 >> +KernelVersion: 4.9 >> +Contact: Peter Rosin >> +Description: >> + The DAC is used to find the peak level of an alternating >> + voltage input signal by a binary search using the output >> + of a comparator wired to an interrupt pin. Like so: >> + _ >> + | \ >> + input +------>-------|+ \ >> + | \ >> + .-------. | }---. >> + | | | / | >> + | dac|-->--|- / | >> + | | |_/ | >> + | | | >> + | | | >> + | irq|------<-------' >> + | | >> + '-------' >> + The boolean invert attribute (0/1) should be set when the >> + input signal is centered around the maximum value of the >> + dac instead of zero. The envelope detector will search >> + from below in this case and will also invert the result. >> + The edge/level of the interrupt is also switched to its >> + opposite value. >> + >> +What: /sys/bus/iio/devices/iio:deviceX/in_altvoltageY_compare_interval >> +Date: October 2016 >> +KernelVersion: 4.9 >> +Contact: Peter Rosin >> +Description: >> + Number of milliseconds to wait for the comparator in each >> + step of the binary search for the input peak level. Needs >> + to relate to the frequency of the input signal. >> diff --git a/MAINTAINERS b/MAINTAINERS >> index fca35d16037d..0cf3549e05e7 100644 >> --- a/MAINTAINERS >> +++ b/MAINTAINERS >> @@ -6123,7 +6123,9 @@ IIO ENVELOPE DETECTOR >> M: Peter Rosin >> L: linux-iio@vger.kernel.org >> S: Maintained >> +F: Documentation/ABI/testing/sysfs-bus-iio-adc-envelope-detector >> F: Documentation/devicetree/bindings/iio/adc/envelope-detector.txt >> +F: drivers/iio/adc/envelope-detector.c >> >> IIO SUBSYSTEM AND DRIVERS >> M: Jonathan Cameron >> diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig >> index 7edcf3238620..d5c4a95855c2 100644 >> --- a/drivers/iio/adc/Kconfig >> +++ b/drivers/iio/adc/Kconfig >> @@ -195,6 +195,16 @@ config DA9150_GPADC >> To compile this driver as a module, choose M here: the module will be >> called berlin2-adc. >> >> +config ENVELOPE_DETECTOR >> + tristate "Envelope detector using a DAC and a comparator" >> + depends on OF >> + help >> + Say yes here to build support for an envelope detector using a DAC >> + and a comparator. >> + >> + To compile this driver as a module, choose M here: the module will be >> + called iio-envelope-detector. While reading through your replies (thanks for your time!) I noticed this; the module name should obviously be s/iio-envelope-detector/envelope-detector/ I fixed that locally so if there is some other reason to respin, it'll be included, but if not please squash that on the way in. Ok? If you think I should respin instead, let me know... Cheers, Peter >> + >> config EXYNOS_ADC >> tristate "Exynos ADC driver support" >> depends on ARCH_EXYNOS || ARCH_S3C24XX || ARCH_S3C64XX || (OF && COMPILE_TEST) >> diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile >> index 7a40c04c311f..0d773c6a0578 100644 >> --- a/drivers/iio/adc/Makefile >> +++ b/drivers/iio/adc/Makefile >> @@ -20,6 +20,7 @@ obj-$(CONFIG_BCM_IPROC_ADC) += bcm_iproc_adc.o >> obj-$(CONFIG_BERLIN2_ADC) += berlin2-adc.o >> obj-$(CONFIG_CC10001_ADC) += cc10001_adc.o >> obj-$(CONFIG_DA9150_GPADC) += da9150-gpadc.o >> +obj-$(CONFIG_ENVELOPE_DETECTOR) += envelope-detector.o >> obj-$(CONFIG_EXYNOS_ADC) += exynos_adc.o >> obj-$(CONFIG_FSL_MX25_ADC) += fsl-imx25-gcq.o >> obj-$(CONFIG_HI8435) += hi8435.o >> diff --git a/drivers/iio/adc/envelope-detector.c b/drivers/iio/adc/envelope-detector.c >> new file mode 100644 >> index 000000000000..fef15c0d7c9c >> --- /dev/null >> +++ b/drivers/iio/adc/envelope-detector.c >> @@ -0,0 +1,422 @@ >> +/* >> + * Driver for an envelope detector using a DAC and a comparator >> + * >> + * Copyright (C) 2016 Axentia Technologies AB >> + * >> + * Author: Peter Rosin >> + * >> + * This program is free software; you can redistribute it and/or modify >> + * it under the terms of the GNU General Public License version 2 as >> + * published by the Free Software Foundation. >> + */ >> + >> +/* >> + * The DAC is used to find the peak level of an alternating voltage input >> + * signal by a binary search using the output of a comparator wired to >> + * an interrupt pin. Like so: >> + * _ >> + * | \ >> + * input +------>-------|+ \ >> + * | \ >> + * .-------. | }---. >> + * | | | / | >> + * | dac|-->--|- / | >> + * | | |_/ | >> + * | | | >> + * | | | >> + * | irq|------<-------' >> + * | | >> + * '-------' >> + */ >> + >> +#include >> +#include >> +#include >> +#include >> +#include >> +#include >> +#include >> +#include >> +#include >> +#include >> +#include >> +#include >> +#include >> +#include >> +#include >> +#include >> + >> +struct envelope { >> + spinlock_t comp_lock; /* protects comp */ >> + int comp; >> + >> + struct mutex read_lock; /* protects everything else */ >> + >> + int comp_irq; >> + u32 comp_irq_trigger; >> + u32 comp_irq_trigger_inv; >> + >> + struct iio_channel *dac; >> + struct delayed_work comp_timeout; >> + >> + unsigned int comp_interval; >> + bool invert; >> + u32 dac_max; >> + >> + int high; >> + int level; >> + int low; >> + >> + struct completion done; >> +}; >> + >> +/* >> + * The envelope_detector_comp_latch function works together with the compare >> + * interrupt service routine below (envelope_detector_comp_isr) as a latch >> + * (one-bit memory) for if the interrupt has triggered since last calling >> + * this function. >> + * The ..._comp_isr function disables the interrupt so that the cpu does not >> + * need to service a possible interrupt flood from the comparator when no-one >> + * cares anyway, and this ..._comp_latch function reenables them again if >> + * needed. >> + */ >> +static int envelope_detector_comp_latch(struct envelope *env) >> +{ >> + int comp; >> + >> + spin_lock_irq(&env->comp_lock); >> + comp = env->comp; >> + env->comp = 0; >> + spin_unlock_irq(&env->comp_lock); >> + >> + if (!comp) >> + return 0; >> + >> + /* >> + * The irq was disabled, and is reenabled just now. >> + * But there might have been a pending irq that >> + * happened while the irq was disabled that fires >> + * just as the irq is reenabled. That is not what >> + * is desired. >> + */ >> + enable_irq(env->comp_irq); >> + >> + /* So, synchronize this possibly pending irq... */ >> + synchronize_irq(env->comp_irq); >> + >> + /* ...and redo the whole dance. */ >> + spin_lock_irq(&env->comp_lock); >> + comp = env->comp; >> + env->comp = 0; >> + spin_unlock_irq(&env->comp_lock); >> + >> + if (comp) >> + enable_irq(env->comp_irq); >> + >> + return 1; >> +} >> + >> +static irqreturn_t envelope_detector_comp_isr(int irq, void *ctx) >> +{ >> + struct envelope *env = ctx; >> + >> + spin_lock(&env->comp_lock); >> + env->comp = 1; >> + disable_irq_nosync(env->comp_irq); >> + spin_unlock(&env->comp_lock); >> + >> + return IRQ_HANDLED; >> +} >> + >> +static void envelope_detector_setup_compare(struct envelope *env) >> +{ >> + int ret; >> + >> + /* >> + * Do a binary search for the peak input level, and stop >> + * when that level is "trapped" between two adjacent DAC >> + * values. >> + * When invert is active, use the midpoint floor so that >> + * env->level ends up as env->low when the termination >> + * criteria below is fulfilled, and use the midpoint >> + * ceiling when invert is not active so that env->level >> + * ends up as env->high in that case. >> + */ >> + env->level = (env->high + env->low + !env->invert) / 2; >> + >> + if (env->high == env->low + 1) { >> + complete(&env->done); >> + return; >> + } >> + >> + /* Set a "safe" DAC level (if there is such a thing)... */ >> + ret = iio_write_channel_raw(env->dac, env->invert ? 0 : env->dac_max); >> + if (ret < 0) >> + goto err; >> + >> + /* ...clear the comparison result... */ >> + envelope_detector_comp_latch(env); >> + >> + /* ...set the real DAC level... */ >> + ret = iio_write_channel_raw(env->dac, env->level); >> + if (ret < 0) >> + goto err; >> + >> + /* ...and wait for a bit to see if the latch catches anything. */ >> + schedule_delayed_work(&env->comp_timeout, >> + msecs_to_jiffies(env->comp_interval)); >> + return; >> + >> +err: >> + env->level = ret; >> + complete(&env->done); >> +} >> + >> +static void envelope_detector_timeout(struct work_struct *work) >> +{ >> + struct envelope *env = container_of(work, struct envelope, >> + comp_timeout.work); >> + >> + /* Adjust low/high depending on the latch content... */ >> + if (!envelope_detector_comp_latch(env) ^ !env->invert) >> + env->low = env->level; >> + else >> + env->high = env->level; >> + >> + /* ...and continue the search. */ >> + envelope_detector_setup_compare(env); >> +} >> + >> +static int envelope_detector_read_raw(struct iio_dev *indio_dev, >> + struct iio_chan_spec const *chan, >> + int *val, int *val2, long mask) >> +{ >> + struct envelope *env = iio_priv(indio_dev); >> + int ret; >> + >> + switch (mask) { >> + case IIO_CHAN_INFO_RAW: >> + /* >> + * When invert is active, start with high=max+1 and low=0 >> + * since we will end up with the low value when the >> + * termination criteria is fulfilled (rounding down). And >> + * start with high=max and low=-1 when invert is not active >> + * since we will end up with the high value in that case. >> + * This ensures that the returned value in both cases are >> + * in the same range as the DAC and is a value that has not >> + * triggered the comparator. >> + */ >> + mutex_lock(&env->read_lock); >> + env->high = env->dac_max + env->invert; >> + env->low = -1 + env->invert; >> + envelope_detector_setup_compare(env); >> + wait_for_completion(&env->done); >> + if (env->level < 0) { >> + ret = env->level; >> + goto err_unlock; >> + } >> + *val = env->invert ? env->dac_max - env->level : env->level; >> + mutex_unlock(&env->read_lock); >> + >> + return IIO_VAL_INT; >> + >> + case IIO_CHAN_INFO_SCALE: >> + return iio_read_channel_scale(env->dac, val, val2); >> + } >> + >> + return -EINVAL; >> + >> +err_unlock: >> + mutex_unlock(&env->read_lock); >> + return ret; >> +} >> + >> +static ssize_t envelope_show_invert(struct iio_dev *indio_dev, >> + uintptr_t private, >> + struct iio_chan_spec const *ch, char *buf) >> +{ >> + struct envelope *env = iio_priv(indio_dev); >> + >> + return sprintf(buf, "%u\n", env->invert); >> +} >> + >> +static ssize_t envelope_store_invert(struct iio_dev *indio_dev, >> + uintptr_t private, >> + struct iio_chan_spec const *ch, >> + const char *buf, size_t len) >> +{ >> + struct envelope *env = iio_priv(indio_dev); >> + unsigned long invert; >> + int ret; >> + u32 trigger; >> + >> + ret = kstrtoul(buf, 0, &invert); >> + if (ret < 0) >> + return ret; >> + if (invert > 1) >> + return -EINVAL; >> + >> + trigger = invert ? env->comp_irq_trigger_inv : env->comp_irq_trigger; >> + >> + mutex_lock(&env->read_lock); >> + if (invert != env->invert) >> + ret = irq_set_irq_type(env->comp_irq, trigger); >> + if (!ret) { >> + env->invert = invert; >> + ret = len; >> + } >> + mutex_unlock(&env->read_lock); >> + >> + return ret; >> +} >> + >> +static ssize_t envelope_show_comp_interval(struct iio_dev *indio_dev, >> + uintptr_t private, >> + struct iio_chan_spec const *ch, >> + char *buf) >> +{ >> + struct envelope *env = iio_priv(indio_dev); >> + >> + return sprintf(buf, "%u\n", env->comp_interval); >> +} >> + >> +static ssize_t envelope_store_comp_interval(struct iio_dev *indio_dev, >> + uintptr_t private, >> + struct iio_chan_spec const *ch, >> + const char *buf, size_t len) >> +{ >> + struct envelope *env = iio_priv(indio_dev); >> + unsigned long interval; >> + int ret; >> + >> + ret = kstrtoul(buf, 0, &interval); >> + if (ret < 0) >> + return ret; >> + if (interval > 1000) >> + return -EINVAL; >> + >> + mutex_lock(&env->read_lock); >> + env->comp_interval = interval; >> + mutex_unlock(&env->read_lock); >> + >> + return len; >> +} >> + >> +static const struct iio_chan_spec_ext_info envelope_detector_ext_info[] = { >> + { .name = "invert", >> + .read = envelope_show_invert, >> + .write = envelope_store_invert, }, >> + { .name = "compare_interval", >> + .read = envelope_show_comp_interval, >> + .write = envelope_store_comp_interval, }, >> + { /* sentinel */ } >> +}; >> + >> +static const struct iio_chan_spec envelope_detector_iio_channel = { >> + .type = IIO_ALTVOLTAGE, >> + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) >> + | BIT(IIO_CHAN_INFO_SCALE), >> + .ext_info = envelope_detector_ext_info, >> + .indexed = 1, >> +}; >> + >> +static const struct iio_info envelope_detector_info = { >> + .read_raw = &envelope_detector_read_raw, >> + .driver_module = THIS_MODULE, >> +}; >> + >> +static int envelope_detector_probe(struct platform_device *pdev) >> +{ >> + struct device *dev = &pdev->dev; >> + struct iio_dev *indio_dev; >> + struct envelope *env; >> + enum iio_chan_type type; >> + int ret; >> + >> + indio_dev = devm_iio_device_alloc(dev, sizeof(*env)); >> + if (!indio_dev) >> + return -ENOMEM; >> + >> + platform_set_drvdata(pdev, indio_dev); >> + env = iio_priv(indio_dev); >> + env->comp_interval = 50; /* some sensible default? */ >> + >> + spin_lock_init(&env->comp_lock); >> + mutex_init(&env->read_lock); >> + init_completion(&env->done); >> + INIT_DELAYED_WORK(&env->comp_timeout, envelope_detector_timeout); >> + >> + indio_dev->name = dev_name(dev); >> + indio_dev->dev.parent = dev; >> + indio_dev->dev.of_node = dev->of_node; >> + indio_dev->info = &envelope_detector_info; >> + indio_dev->channels = &envelope_detector_iio_channel; >> + indio_dev->num_channels = 1; >> + >> + env->dac = devm_iio_channel_get(dev, "dac"); >> + if (IS_ERR(env->dac)) { >> + if (PTR_ERR(env->dac) != -EPROBE_DEFER) >> + dev_err(dev, "failed to get dac input channel\n"); >> + return PTR_ERR(env->dac); >> + } >> + >> + env->comp_irq = platform_get_irq_byname(pdev, "comp"); >> + if (env->comp_irq < 0) { >> + if (env->comp_irq != -EPROBE_DEFER) >> + dev_err(dev, "failed to get compare interrupt\n"); >> + return env->comp_irq; >> + } >> + >> + ret = devm_request_irq(dev, env->comp_irq, envelope_detector_comp_isr, >> + 0, "envelope-detector", env); >> + if (ret) { >> + if (ret != -EPROBE_DEFER) >> + dev_err(dev, "failed to request interrupt\n"); >> + return ret; >> + } >> + env->comp_irq_trigger = irq_get_trigger_type(env->comp_irq); >> + if (env->comp_irq_trigger & IRQF_TRIGGER_RISING) >> + env->comp_irq_trigger_inv |= IRQF_TRIGGER_FALLING; >> + if (env->comp_irq_trigger & IRQF_TRIGGER_FALLING) >> + env->comp_irq_trigger_inv |= IRQF_TRIGGER_RISING; >> + if (env->comp_irq_trigger & IRQF_TRIGGER_HIGH) >> + env->comp_irq_trigger_inv |= IRQF_TRIGGER_LOW; >> + if (env->comp_irq_trigger & IRQF_TRIGGER_LOW) >> + env->comp_irq_trigger_inv |= IRQF_TRIGGER_HIGH; >> + >> + ret = iio_get_channel_type(env->dac, &type); >> + if (ret < 0) >> + return ret; >> + >> + if (type != IIO_VOLTAGE) { >> + dev_err(dev, "dac is of the wrong type\n"); >> + return -EINVAL; >> + } >> + >> + ret = iio_read_max_channel_raw(env->dac, &env->dac_max); >> + if (ret < 0) { >> + dev_err(dev, "dac does not indicate its raw maximum value\n"); >> + return ret; >> + } >> + >> + return devm_iio_device_register(dev, indio_dev); >> +} >> + >> +static const struct of_device_id envelope_detector_match[] = { >> + { .compatible = "axentia,tse850-envelope-detector", }, >> + { /* sentinel */ } >> +}; >> +MODULE_DEVICE_TABLE(of, envelope_detector_match); >> + >> +static struct platform_driver envelope_detector_driver = { >> + .probe = envelope_detector_probe, >> + .driver = { >> + .name = "iio-envelope-detector", >> + .of_match_table = envelope_detector_match, >> + }, >> +}; >> +module_platform_driver(envelope_detector_driver); >> + >> +MODULE_DESCRIPTION("Envelope detector using a DAC and a comparator"); >> +MODULE_AUTHOR("Peter Rosin "); >> +MODULE_LICENSE("GPL v2"); >> >