Lines Matching full:env

79 static int envelope_detector_comp_latch(struct envelope *env)  in envelope_detector_comp_latch()  argument
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()
117 struct envelope *env = ctx; in envelope_detector_comp_isr() local
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()
127 static void envelope_detector_setup_compare(struct envelope *env) in envelope_detector_setup_compare() argument
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()
149 ret = iio_write_channel_raw(env->dac, env->invert ? 0 : env->dac_max); in envelope_detector_setup_compare()
154 envelope_detector_comp_latch(env); 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()
173 struct envelope *env = container_of(work, struct envelope, in envelope_detector_timeout() local
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()
183 envelope_detector_setup_compare(env); in envelope_detector_timeout()
190 struct envelope *env = iio_priv(indio_dev); in envelope_detector_read_raw() local
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()
208 envelope_detector_setup_compare(env); 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()
226 mutex_unlock(&env->read_lock); in envelope_detector_read_raw()
234 struct envelope *env = iio_priv(indio_dev); in envelope_show_invert() local
236 return sprintf(buf, "%u\n", env->invert); in envelope_show_invert()
244 struct envelope *env = iio_priv(indio_dev); in envelope_store_invert() local
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()
274 struct envelope *env = iio_priv(indio_dev); in envelope_show_comp_interval() local
276 return sprintf(buf, "%u\n", env->comp_interval); in envelope_show_comp_interval()
284 struct envelope *env = iio_priv(indio_dev); in envelope_store_comp_interval() local
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()
327 struct envelope *env; in envelope_detector_probe() local
331 indio_dev = devm_iio_device_alloc(dev, sizeof(*env)); in envelope_detector_probe()
336 env = iio_priv(indio_dev); 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()
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()
382 ret = iio_read_max_channel_raw(env->dac, &env->dac_max); in envelope_detector_probe()