^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Driver for an envelope detector using a DAC and a comparator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2016 Axentia Technologies AB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Author: Peter Rosin <peda@axentia.se>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * The DAC is used to find the peak level of an alternating voltage input
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * signal by a binary search using the output of a comparator wired to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * an interrupt pin. Like so:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * _
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * input +------>-------|+ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * .-------. | }---.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * | | | / |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * | dac|-->--|- / |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * | | |_/ |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * | | |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * | | |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * | irq|------<-------'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * | |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * '-------'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/completion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <linux/iio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include <linux/iio/iio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include <linux/iio/sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #include <linux/irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #include <linux/of_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct envelope {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) spinlock_t comp_lock; /* protects comp */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) int comp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct mutex read_lock; /* protects everything else */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) int comp_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) u32 comp_irq_trigger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) u32 comp_irq_trigger_inv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) struct iio_channel *dac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) struct delayed_work comp_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) unsigned int comp_interval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) bool invert;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) u32 dac_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) int high;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) int level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) int low;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct completion done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * The envelope_detector_comp_latch function works together with the compare
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * interrupt service routine below (envelope_detector_comp_isr) as a latch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * (one-bit memory) for if the interrupt has triggered since last calling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * The ..._comp_isr function disables the interrupt so that the cpu does not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * need to service a possible interrupt flood from the comparator when no-one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * cares anyway, and this ..._comp_latch function reenables them again if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) static int envelope_detector_comp_latch(struct envelope *env)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) int comp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) spin_lock_irq(&env->comp_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) comp = env->comp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) env->comp = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) spin_unlock_irq(&env->comp_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (!comp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * The irq was disabled, and is reenabled just now.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * But there might have been a pending irq that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) * happened while the irq was disabled that fires
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * just as the irq is reenabled. That is not what
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * is desired.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) enable_irq(env->comp_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) /* So, synchronize this possibly pending irq... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) synchronize_irq(env->comp_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) /* ...and redo the whole dance. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) spin_lock_irq(&env->comp_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) comp = env->comp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) env->comp = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) spin_unlock_irq(&env->comp_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (comp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) enable_irq(env->comp_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static irqreturn_t envelope_detector_comp_isr(int irq, void *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) struct envelope *env = ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) spin_lock(&env->comp_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) env->comp = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) disable_irq_nosync(env->comp_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) spin_unlock(&env->comp_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) static void envelope_detector_setup_compare(struct envelope *env)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * Do a binary search for the peak input level, and stop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * when that level is "trapped" between two adjacent DAC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * values.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * When invert is active, use the midpoint floor so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * env->level ends up as env->low when the termination
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * criteria below is fulfilled, and use the midpoint
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * ceiling when invert is not active so that env->level
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * ends up as env->high in that case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) env->level = (env->high + env->low + !env->invert) / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) if (env->high == env->low + 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) complete(&env->done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) /* Set a "safe" DAC level (if there is such a thing)... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) ret = iio_write_channel_raw(env->dac, env->invert ? 0 : env->dac_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) /* ...clear the comparison result... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) envelope_detector_comp_latch(env);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) /* ...set the real DAC level... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) ret = iio_write_channel_raw(env->dac, env->level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) /* ...and wait for a bit to see if the latch catches anything. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) schedule_delayed_work(&env->comp_timeout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) msecs_to_jiffies(env->comp_interval));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) env->level = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) complete(&env->done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) static void envelope_detector_timeout(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) struct envelope *env = container_of(work, struct envelope,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) comp_timeout.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) /* Adjust low/high depending on the latch content... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (!envelope_detector_comp_latch(env) ^ !env->invert)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) env->low = env->level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) env->high = env->level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) /* ...and continue the search. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) envelope_detector_setup_compare(env);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) static int envelope_detector_read_raw(struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) struct iio_chan_spec const *chan,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) int *val, int *val2, long mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) struct envelope *env = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) switch (mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) case IIO_CHAN_INFO_RAW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * When invert is active, start with high=max+1 and low=0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) * since we will end up with the low value when the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) * termination criteria is fulfilled (rounding down). And
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) * start with high=max and low=-1 when invert is not active
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) * since we will end up with the high value in that case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) * This ensures that the returned value in both cases are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) * in the same range as the DAC and is a value that has not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) * triggered the comparator.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) mutex_lock(&env->read_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) env->high = env->dac_max + env->invert;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) env->low = -1 + env->invert;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) envelope_detector_setup_compare(env);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) wait_for_completion(&env->done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) if (env->level < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) ret = env->level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) goto err_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) *val = env->invert ? env->dac_max - env->level : env->level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) mutex_unlock(&env->read_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) return IIO_VAL_INT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) case IIO_CHAN_INFO_SCALE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) return iio_read_channel_scale(env->dac, val, val2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) err_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) mutex_unlock(&env->read_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) static ssize_t envelope_show_invert(struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) uintptr_t private,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) struct iio_chan_spec const *ch, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) struct envelope *env = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) return sprintf(buf, "%u\n", env->invert);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) static ssize_t envelope_store_invert(struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) uintptr_t private,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) struct iio_chan_spec const *ch,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) const char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) struct envelope *env = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) unsigned long invert;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) u32 trigger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) ret = kstrtoul(buf, 0, &invert);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) if (invert > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) trigger = invert ? env->comp_irq_trigger_inv : env->comp_irq_trigger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) mutex_lock(&env->read_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) if (invert != env->invert)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) ret = irq_set_irq_type(env->comp_irq, trigger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) env->invert = invert;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) ret = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) mutex_unlock(&env->read_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) static ssize_t envelope_show_comp_interval(struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) uintptr_t private,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) struct iio_chan_spec const *ch,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) struct envelope *env = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) return sprintf(buf, "%u\n", env->comp_interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) static ssize_t envelope_store_comp_interval(struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) uintptr_t private,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) struct iio_chan_spec const *ch,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) const char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) struct envelope *env = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) unsigned long interval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) ret = kstrtoul(buf, 0, &interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) if (interval > 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) mutex_lock(&env->read_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) env->comp_interval = interval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) mutex_unlock(&env->read_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) static const struct iio_chan_spec_ext_info envelope_detector_ext_info[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) { .name = "invert",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) .read = envelope_show_invert,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) .write = envelope_store_invert, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) { .name = "compare_interval",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) .read = envelope_show_comp_interval,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) .write = envelope_store_comp_interval, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) { /* sentinel */ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) static const struct iio_chan_spec envelope_detector_iio_channel = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) .type = IIO_ALTVOLTAGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) .info_mask_separate = BIT(IIO_CHAN_INFO_RAW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) | BIT(IIO_CHAN_INFO_SCALE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) .ext_info = envelope_detector_ext_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) .indexed = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) static const struct iio_info envelope_detector_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) .read_raw = &envelope_detector_read_raw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) static int envelope_detector_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) struct iio_dev *indio_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) struct envelope *env;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) enum iio_chan_type type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) indio_dev = devm_iio_device_alloc(dev, sizeof(*env));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) if (!indio_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) platform_set_drvdata(pdev, indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) env = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) env->comp_interval = 50; /* some sensible default? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) spin_lock_init(&env->comp_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) mutex_init(&env->read_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) init_completion(&env->done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) INIT_DELAYED_WORK(&env->comp_timeout, envelope_detector_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) indio_dev->name = dev_name(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) indio_dev->info = &envelope_detector_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) indio_dev->channels = &envelope_detector_iio_channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) indio_dev->num_channels = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) env->dac = devm_iio_channel_get(dev, "dac");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) if (IS_ERR(env->dac))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) return dev_err_probe(dev, PTR_ERR(env->dac),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) "failed to get dac input channel\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) env->comp_irq = platform_get_irq_byname(pdev, "comp");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) if (env->comp_irq < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) return env->comp_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) ret = devm_request_irq(dev, env->comp_irq, envelope_detector_comp_isr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 0, "envelope-detector", env);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) return dev_err_probe(dev, ret, "failed to request interrupt\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) env->comp_irq_trigger = irq_get_trigger_type(env->comp_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) if (env->comp_irq_trigger & IRQF_TRIGGER_RISING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) env->comp_irq_trigger_inv |= IRQF_TRIGGER_FALLING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) if (env->comp_irq_trigger & IRQF_TRIGGER_FALLING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) env->comp_irq_trigger_inv |= IRQF_TRIGGER_RISING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) if (env->comp_irq_trigger & IRQF_TRIGGER_HIGH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) env->comp_irq_trigger_inv |= IRQF_TRIGGER_LOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) if (env->comp_irq_trigger & IRQF_TRIGGER_LOW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) env->comp_irq_trigger_inv |= IRQF_TRIGGER_HIGH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) ret = iio_get_channel_type(env->dac, &type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) if (type != IIO_VOLTAGE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) dev_err(dev, "dac is of the wrong type\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) ret = iio_read_max_channel_raw(env->dac, &env->dac_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) dev_err(dev, "dac does not indicate its raw maximum value\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) return devm_iio_device_register(dev, indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) static const struct of_device_id envelope_detector_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) { .compatible = "axentia,tse850-envelope-detector", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) { /* sentinel */ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) MODULE_DEVICE_TABLE(of, envelope_detector_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) static struct platform_driver envelope_detector_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) .probe = envelope_detector_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) .name = "iio-envelope-detector",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) .of_match_table = envelope_detector_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) module_platform_driver(envelope_detector_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) MODULE_DESCRIPTION("Envelope detector using a DAC and a comparator");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) MODULE_LICENSE("GPL v2");