Lines Matching +full:level +full:- +full:high

1 // SPDX-License-Identifier: GPL-2.0
11 * The DAC is used to find the peak level of an alternating voltage input
16 * input +------>-------|+ \
18 * .-------. | }---.
20 * | dac|-->--|- / |
24 * | irq|------<-------'
26 * '-------'
62 int high; member
63 int level; member
72 * (one-bit memory) for if the interrupt has triggered since last calling
75 * need to service a possible interrupt flood from the comparator when no-one
83 spin_lock_irq(&env->comp_lock); in envelope_detector_comp_latch()
84 comp = env->comp; in envelope_detector_comp_latch()
85 env->comp = 0; in envelope_detector_comp_latch()
86 spin_unlock_irq(&env->comp_lock); in envelope_detector_comp_latch()
98 enable_irq(env->comp_irq); in envelope_detector_comp_latch()
101 synchronize_irq(env->comp_irq); in envelope_detector_comp_latch()
104 spin_lock_irq(&env->comp_lock); in envelope_detector_comp_latch()
105 comp = env->comp; in envelope_detector_comp_latch()
106 env->comp = 0; in envelope_detector_comp_latch()
107 spin_unlock_irq(&env->comp_lock); in envelope_detector_comp_latch()
110 enable_irq(env->comp_irq); in envelope_detector_comp_latch()
119 spin_lock(&env->comp_lock); in envelope_detector_comp_isr()
120 env->comp = 1; in envelope_detector_comp_isr()
121 disable_irq_nosync(env->comp_irq); in envelope_detector_comp_isr()
122 spin_unlock(&env->comp_lock); in envelope_detector_comp_isr()
132 * Do a binary search for the peak input level, and stop in envelope_detector_setup_compare()
133 * when that level is "trapped" between two adjacent DAC in envelope_detector_setup_compare()
136 * env->level ends up as env->low when the termination in envelope_detector_setup_compare()
138 * ceiling when invert is not active so that env->level in envelope_detector_setup_compare()
139 * ends up as env->high in that case. in envelope_detector_setup_compare()
141 env->level = (env->high + env->low + !env->invert) / 2; in envelope_detector_setup_compare()
143 if (env->high == env->low + 1) { in envelope_detector_setup_compare()
144 complete(&env->done); in envelope_detector_setup_compare()
148 /* Set a "safe" DAC level (if there is such a thing)... */ in envelope_detector_setup_compare()
149 ret = iio_write_channel_raw(env->dac, env->invert ? 0 : env->dac_max); in envelope_detector_setup_compare()
156 /* ...set the real DAC level... */ in envelope_detector_setup_compare()
157 ret = iio_write_channel_raw(env->dac, env->level); in envelope_detector_setup_compare()
162 schedule_delayed_work(&env->comp_timeout, in envelope_detector_setup_compare()
163 msecs_to_jiffies(env->comp_interval)); in envelope_detector_setup_compare()
167 env->level = ret; in envelope_detector_setup_compare()
168 complete(&env->done); in envelope_detector_setup_compare()
176 /* Adjust low/high depending on the latch content... */ in envelope_detector_timeout()
177 if (!envelope_detector_comp_latch(env) ^ !env->invert) in envelope_detector_timeout()
178 env->low = env->level; in envelope_detector_timeout()
180 env->high = env->level; in envelope_detector_timeout()
196 * When invert is active, start with high=max+1 and low=0 in envelope_detector_read_raw()
199 * start with high=max and low=-1 when invert is not active in envelope_detector_read_raw()
200 * since we will end up with the high value in that case. in envelope_detector_read_raw()
205 mutex_lock(&env->read_lock); in envelope_detector_read_raw()
206 env->high = env->dac_max + env->invert; in envelope_detector_read_raw()
207 env->low = -1 + env->invert; in envelope_detector_read_raw()
209 wait_for_completion(&env->done); in envelope_detector_read_raw()
210 if (env->level < 0) { in envelope_detector_read_raw()
211 ret = env->level; in envelope_detector_read_raw()
214 *val = env->invert ? env->dac_max - env->level : env->level; in envelope_detector_read_raw()
215 mutex_unlock(&env->read_lock); in envelope_detector_read_raw()
220 return iio_read_channel_scale(env->dac, val, val2); in envelope_detector_read_raw()
223 return -EINVAL; in envelope_detector_read_raw()
226 mutex_unlock(&env->read_lock); in envelope_detector_read_raw()
236 return sprintf(buf, "%u\n", env->invert); in envelope_show_invert()
253 return -EINVAL; in envelope_store_invert()
255 trigger = invert ? env->comp_irq_trigger_inv : env->comp_irq_trigger; in envelope_store_invert()
257 mutex_lock(&env->read_lock); in envelope_store_invert()
258 if (invert != env->invert) in envelope_store_invert()
259 ret = irq_set_irq_type(env->comp_irq, trigger); in envelope_store_invert()
261 env->invert = invert; in envelope_store_invert()
264 mutex_unlock(&env->read_lock); in envelope_store_invert()
276 return sprintf(buf, "%u\n", env->comp_interval); in envelope_show_comp_interval()
292 return -EINVAL; in envelope_store_comp_interval()
294 mutex_lock(&env->read_lock); in envelope_store_comp_interval()
295 env->comp_interval = interval; in envelope_store_comp_interval()
296 mutex_unlock(&env->read_lock); in envelope_store_comp_interval()
325 struct device *dev = &pdev->dev; in envelope_detector_probe()
333 return -ENOMEM; in envelope_detector_probe()
337 env->comp_interval = 50; /* some sensible default? */ in envelope_detector_probe()
339 spin_lock_init(&env->comp_lock); in envelope_detector_probe()
340 mutex_init(&env->read_lock); in envelope_detector_probe()
341 init_completion(&env->done); in envelope_detector_probe()
342 INIT_DELAYED_WORK(&env->comp_timeout, envelope_detector_timeout); in envelope_detector_probe()
344 indio_dev->name = dev_name(dev); in envelope_detector_probe()
345 indio_dev->info = &envelope_detector_info; in envelope_detector_probe()
346 indio_dev->channels = &envelope_detector_iio_channel; in envelope_detector_probe()
347 indio_dev->num_channels = 1; in envelope_detector_probe()
349 env->dac = devm_iio_channel_get(dev, "dac"); in envelope_detector_probe()
350 if (IS_ERR(env->dac)) in envelope_detector_probe()
351 return dev_err_probe(dev, PTR_ERR(env->dac), in envelope_detector_probe()
354 env->comp_irq = platform_get_irq_byname(pdev, "comp"); in envelope_detector_probe()
355 if (env->comp_irq < 0) in envelope_detector_probe()
356 return env->comp_irq; in envelope_detector_probe()
358 ret = devm_request_irq(dev, env->comp_irq, envelope_detector_comp_isr, in envelope_detector_probe()
359 0, "envelope-detector", env); in envelope_detector_probe()
363 env->comp_irq_trigger = irq_get_trigger_type(env->comp_irq); in envelope_detector_probe()
364 if (env->comp_irq_trigger & IRQF_TRIGGER_RISING) in envelope_detector_probe()
365 env->comp_irq_trigger_inv |= IRQF_TRIGGER_FALLING; in envelope_detector_probe()
366 if (env->comp_irq_trigger & IRQF_TRIGGER_FALLING) in envelope_detector_probe()
367 env->comp_irq_trigger_inv |= IRQF_TRIGGER_RISING; in envelope_detector_probe()
368 if (env->comp_irq_trigger & IRQF_TRIGGER_HIGH) in envelope_detector_probe()
369 env->comp_irq_trigger_inv |= IRQF_TRIGGER_LOW; in envelope_detector_probe()
370 if (env->comp_irq_trigger & IRQF_TRIGGER_LOW) in envelope_detector_probe()
371 env->comp_irq_trigger_inv |= IRQF_TRIGGER_HIGH; in envelope_detector_probe()
373 ret = iio_get_channel_type(env->dac, &type); in envelope_detector_probe()
379 return -EINVAL; in envelope_detector_probe()
382 ret = iio_read_max_channel_raw(env->dac, &env->dac_max); in envelope_detector_probe()
392 { .compatible = "axentia,tse850-envelope-detector", },
400 .name = "iio-envelope-detector",