Lines Matching refs:env

80 static int envelope_detector_comp_latch(struct envelope *env)  in envelope_detector_comp_latch()  argument
84 spin_lock_irq(&env->comp_lock); in envelope_detector_comp_latch()
85 comp = env->comp; in envelope_detector_comp_latch()
86 env->comp = 0; in envelope_detector_comp_latch()
87 spin_unlock_irq(&env->comp_lock); in envelope_detector_comp_latch()
99 enable_irq(env->comp_irq); in envelope_detector_comp_latch()
102 synchronize_irq(env->comp_irq); in envelope_detector_comp_latch()
105 spin_lock_irq(&env->comp_lock); in envelope_detector_comp_latch()
106 comp = env->comp; in envelope_detector_comp_latch()
107 env->comp = 0; in envelope_detector_comp_latch()
108 spin_unlock_irq(&env->comp_lock); in envelope_detector_comp_latch()
111 enable_irq(env->comp_irq); in envelope_detector_comp_latch()
118 struct envelope *env = ctx; in envelope_detector_comp_isr() local
120 spin_lock(&env->comp_lock); in envelope_detector_comp_isr()
121 env->comp = 1; in envelope_detector_comp_isr()
122 disable_irq_nosync(env->comp_irq); in envelope_detector_comp_isr()
123 spin_unlock(&env->comp_lock); in envelope_detector_comp_isr()
128 static void envelope_detector_setup_compare(struct envelope *env) in envelope_detector_setup_compare() argument
142 env->level = (env->high + env->low + !env->invert) / 2; in envelope_detector_setup_compare()
144 if (env->high == env->low + 1) { in envelope_detector_setup_compare()
145 complete(&env->done); in envelope_detector_setup_compare()
150 ret = iio_write_channel_raw(env->dac, env->invert ? 0 : env->dac_max); in envelope_detector_setup_compare()
155 envelope_detector_comp_latch(env); in envelope_detector_setup_compare()
158 ret = iio_write_channel_raw(env->dac, env->level); in envelope_detector_setup_compare()
163 schedule_delayed_work(&env->comp_timeout, in envelope_detector_setup_compare()
164 msecs_to_jiffies(env->comp_interval)); in envelope_detector_setup_compare()
168 env->level = ret; in envelope_detector_setup_compare()
169 complete(&env->done); in envelope_detector_setup_compare()
174 struct envelope *env = container_of(work, struct envelope, in envelope_detector_timeout() local
178 if (!envelope_detector_comp_latch(env) ^ !env->invert) in envelope_detector_timeout()
179 env->low = env->level; in envelope_detector_timeout()
181 env->high = env->level; in envelope_detector_timeout()
184 envelope_detector_setup_compare(env); in envelope_detector_timeout()
191 struct envelope *env = iio_priv(indio_dev); in envelope_detector_read_raw() local
206 mutex_lock(&env->read_lock); in envelope_detector_read_raw()
207 env->high = env->dac_max + env->invert; in envelope_detector_read_raw()
208 env->low = -1 + env->invert; in envelope_detector_read_raw()
209 envelope_detector_setup_compare(env); in envelope_detector_read_raw()
210 wait_for_completion(&env->done); in envelope_detector_read_raw()
211 if (env->level < 0) { in envelope_detector_read_raw()
212 ret = env->level; in envelope_detector_read_raw()
215 *val = env->invert ? env->dac_max - env->level : env->level; in envelope_detector_read_raw()
216 mutex_unlock(&env->read_lock); in envelope_detector_read_raw()
221 return iio_read_channel_scale(env->dac, val, val2); in envelope_detector_read_raw()
227 mutex_unlock(&env->read_lock); in envelope_detector_read_raw()
235 struct envelope *env = iio_priv(indio_dev); in envelope_show_invert() local
237 return sprintf(buf, "%u\n", env->invert); in envelope_show_invert()
245 struct envelope *env = iio_priv(indio_dev); in envelope_store_invert() local
256 trigger = invert ? env->comp_irq_trigger_inv : env->comp_irq_trigger; in envelope_store_invert()
258 mutex_lock(&env->read_lock); in envelope_store_invert()
259 if (invert != env->invert) in envelope_store_invert()
260 ret = irq_set_irq_type(env->comp_irq, trigger); in envelope_store_invert()
262 env->invert = invert; in envelope_store_invert()
265 mutex_unlock(&env->read_lock); in envelope_store_invert()
275 struct envelope *env = iio_priv(indio_dev); in envelope_show_comp_interval() local
277 return sprintf(buf, "%u\n", env->comp_interval); in envelope_show_comp_interval()
285 struct envelope *env = iio_priv(indio_dev); in envelope_store_comp_interval() local
295 mutex_lock(&env->read_lock); in envelope_store_comp_interval()
296 env->comp_interval = interval; in envelope_store_comp_interval()
297 mutex_unlock(&env->read_lock); in envelope_store_comp_interval()
328 struct envelope *env; in envelope_detector_probe() local
332 indio_dev = devm_iio_device_alloc(dev, sizeof(*env)); in envelope_detector_probe()
337 env = iio_priv(indio_dev); in envelope_detector_probe()
338 env->comp_interval = 50; /* some sensible default? */ in envelope_detector_probe()
340 spin_lock_init(&env->comp_lock); in envelope_detector_probe()
341 mutex_init(&env->read_lock); in envelope_detector_probe()
342 init_completion(&env->done); in envelope_detector_probe()
343 INIT_DELAYED_WORK(&env->comp_timeout, envelope_detector_timeout); in envelope_detector_probe()
350 env->dac = devm_iio_channel_get(dev, "dac"); in envelope_detector_probe()
351 if (IS_ERR(env->dac)) in envelope_detector_probe()
352 return dev_err_probe(dev, PTR_ERR(env->dac), in envelope_detector_probe()
355 env->comp_irq = platform_get_irq_byname(pdev, "comp"); in envelope_detector_probe()
356 if (env->comp_irq < 0) in envelope_detector_probe()
357 return env->comp_irq; in envelope_detector_probe()
359 ret = devm_request_irq(dev, env->comp_irq, envelope_detector_comp_isr, in envelope_detector_probe()
360 0, "envelope-detector", env); in envelope_detector_probe()
364 env->comp_irq_trigger = irq_get_trigger_type(env->comp_irq); in envelope_detector_probe()
365 if (env->comp_irq_trigger & IRQF_TRIGGER_RISING) in envelope_detector_probe()
366 env->comp_irq_trigger_inv |= IRQF_TRIGGER_FALLING; in envelope_detector_probe()
367 if (env->comp_irq_trigger & IRQF_TRIGGER_FALLING) in envelope_detector_probe()
368 env->comp_irq_trigger_inv |= IRQF_TRIGGER_RISING; in envelope_detector_probe()
369 if (env->comp_irq_trigger & IRQF_TRIGGER_HIGH) in envelope_detector_probe()
370 env->comp_irq_trigger_inv |= IRQF_TRIGGER_LOW; in envelope_detector_probe()
371 if (env->comp_irq_trigger & IRQF_TRIGGER_LOW) in envelope_detector_probe()
372 env->comp_irq_trigger_inv |= IRQF_TRIGGER_HIGH; in envelope_detector_probe()
374 ret = iio_get_channel_type(env->dac, &type); in envelope_detector_probe()
383 ret = iio_read_max_channel_raw(env->dac, &env->dac_max); in envelope_detector_probe()