blob: f207e36b12cc9df59649da75fb3f120a0e604572 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Jonathan Cameron1637db42009-08-18 18:06:26 +01002/* The industrial I/O core, trigger handling functions
3 *
4 * Copyright (c) 2008 Jonathan Cameron
Jonathan Cameron1637db42009-08-18 18:06:26 +01005 */
6
7#include <linux/kernel.h>
Jonathan Cameron1637db42009-08-18 18:06:26 +01008#include <linux/idr.h>
9#include <linux/err.h>
10#include <linux/device.h>
11#include <linux/interrupt.h>
12#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Jonathan Cameron1637db42009-08-18 18:06:26 +010014
Jonathan Cameron06458e22012-04-25 15:54:58 +010015#include <linux/iio/iio.h>
Jonathan Cameron6eaf9f62021-04-26 18:49:05 +010016#include <linux/iio/iio-opaque.h>
Jonathan Cameron06458e22012-04-25 15:54:58 +010017#include <linux/iio/trigger.h>
Jonathan Camerondf9c1c42011-08-12 17:56:03 +010018#include "iio_core.h"
Jonathan Cameron6aea1c32011-08-24 17:28:38 +010019#include "iio_core_trigger.h"
Jonathan Cameron06458e22012-04-25 15:54:58 +010020#include <linux/iio/trigger_consumer.h>
Jonathan Cameron1637db42009-08-18 18:06:26 +010021
22/* RFC - Question of approach
23 * Make the common case (single sensor single trigger)
24 * simple by starting trigger capture from when first sensors
25 * is added.
26 *
27 * Complex simultaneous start requires use of 'hold' functionality
28 * of the trigger. (not implemented)
29 *
30 * Any other suggestions?
31 */
32
Jonathan Cameron47c24fd2011-08-30 12:41:07 +010033static DEFINE_IDA(iio_trigger_ida);
Jonathan Cameron1637db42009-08-18 18:06:26 +010034
35/* Single list of all available triggers */
36static LIST_HEAD(iio_trigger_list);
37static DEFINE_MUTEX(iio_trigger_list_lock);
38
39/**
Joe Simmons-Talbott9cf0b612022-06-01 14:54:14 -040040 * name_show() - retrieve useful identifying name
Cristina Opriceana8e563b02015-08-06 14:56:02 +030041 * @dev: device associated with the iio_trigger
42 * @attr: pointer to the device_attribute structure that is
43 * being processed
44 * @buf: buffer to print the name into
45 *
46 * Return: a negative number on failure or the number of written
47 * characters on success.
48 */
Joe Simmons-Talbott9cf0b612022-06-01 14:54:14 -040049static ssize_t name_show(struct device *dev, struct device_attribute *attr,
50 char *buf)
Jonathan Cameron59c85e82011-05-18 14:42:22 +010051{
Lars-Peter Clausen971ff1d2012-06-21 19:11:00 +020052 struct iio_trigger *trig = to_iio_trigger(dev);
Joe Simmons-Talbott9d9ec8d2022-07-27 14:18:54 -040053
Lars-Peter Clausen83ca56b2021-03-20 08:14:02 +010054 return sysfs_emit(buf, "%s\n", trig->name);
Jonathan Cameron59c85e82011-05-18 14:42:22 +010055}
56
Joe Simmons-Talbott9cf0b612022-06-01 14:54:14 -040057static DEVICE_ATTR_RO(name);
Jonathan Cameron59c85e82011-05-18 14:42:22 +010058
Lars-Peter Clausen6d459aa2012-07-05 10:57:06 +020059static struct attribute *iio_trig_dev_attrs[] = {
60 &dev_attr_name.attr,
61 NULL,
62};
Axel Linf59c2572013-12-02 02:57:00 +000063ATTRIBUTE_GROUPS(iio_trig_dev);
Jonathan Cameron1637db42009-08-18 18:06:26 +010064
Crestez Dan Leonard3b8e73e2016-05-23 21:40:01 +030065static struct iio_trigger *__iio_trigger_find_by_name(const char *name);
66
Dmitry Rokosovbc72d932022-06-01 17:48:32 +000067int iio_trigger_register(struct iio_trigger *trig_info)
Jonathan Cameron1637db42009-08-18 18:06:26 +010068{
69 int ret;
70
keliu319dbcd2022-05-27 09:17:39 +000071 trig_info->id = ida_alloc(&iio_trigger_ida, GFP_KERNEL);
Hartmut Knaack92825ff2014-02-16 11:53:00 +000072 if (trig_info->id < 0)
73 return trig_info->id;
74
Jonathan Cameron1637db42009-08-18 18:06:26 +010075 /* Set the name used for the sysfs directory etc */
Andy Shevchenkoebb94932021-04-02 20:49:10 +030076 dev_set_name(&trig_info->dev, "trigger%d", trig_info->id);
Jonathan Cameron1637db42009-08-18 18:06:26 +010077
78 ret = device_add(&trig_info->dev);
79 if (ret)
80 goto error_unregister_id;
81
Jonathan Cameron1637db42009-08-18 18:06:26 +010082 /* Add to list of available triggers held by the IIO core */
83 mutex_lock(&iio_trigger_list_lock);
Crestez Dan Leonard3b8e73e2016-05-23 21:40:01 +030084 if (__iio_trigger_find_by_name(trig_info->name)) {
85 pr_err("Duplicate trigger name '%s'\n", trig_info->name);
86 ret = -EEXIST;
87 goto error_device_del;
88 }
Jonathan Cameron1637db42009-08-18 18:06:26 +010089 list_add_tail(&trig_info->list, &iio_trigger_list);
90 mutex_unlock(&iio_trigger_list_lock);
91
92 return 0;
93
Crestez Dan Leonard3b8e73e2016-05-23 21:40:01 +030094error_device_del:
95 mutex_unlock(&iio_trigger_list_lock);
96 device_del(&trig_info->dev);
Jonathan Cameron1637db42009-08-18 18:06:26 +010097error_unregister_id:
keliu319dbcd2022-05-27 09:17:39 +000098 ida_free(&iio_trigger_ida, trig_info->id);
Jonathan Cameron1637db42009-08-18 18:06:26 +010099 return ret;
100}
Dmitry Rokosovbc72d932022-06-01 17:48:32 +0000101EXPORT_SYMBOL(iio_trigger_register);
Jonathan Cameron1637db42009-08-18 18:06:26 +0100102
103void iio_trigger_unregister(struct iio_trigger *trig_info)
104{
Jonathan Cameron1637db42009-08-18 18:06:26 +0100105 mutex_lock(&iio_trigger_list_lock);
Jonathan Cameron582e5482011-04-15 18:55:54 +0100106 list_del(&trig_info->list);
Jonathan Cameron1637db42009-08-18 18:06:26 +0100107 mutex_unlock(&iio_trigger_list_lock);
108
keliu319dbcd2022-05-27 09:17:39 +0000109 ida_free(&iio_trigger_ida, trig_info->id);
Jonathan Cameron1637db42009-08-18 18:06:26 +0100110 /* Possible issue in here */
Jonathan Cameron8bade402013-06-22 12:00:04 +0100111 device_del(&trig_info->dev);
Jonathan Cameron1637db42009-08-18 18:06:26 +0100112}
113EXPORT_SYMBOL(iio_trigger_unregister);
114
Matt Ranostayc8cdf702016-09-02 23:36:15 -0700115int iio_trigger_set_immutable(struct iio_dev *indio_dev, struct iio_trigger *trig)
116{
Jonathan Cameron3028e0c2021-04-26 18:49:06 +0100117 struct iio_dev_opaque *iio_dev_opaque;
118
Matt Ranostayc8cdf702016-09-02 23:36:15 -0700119 if (!indio_dev || !trig)
120 return -EINVAL;
121
Jonathan Cameron3028e0c2021-04-26 18:49:06 +0100122 iio_dev_opaque = to_iio_dev_opaque(indio_dev);
Nuno Sá16afe122022-10-12 17:16:20 +0200123 mutex_lock(&iio_dev_opaque->mlock);
Jonathan Cameron3028e0c2021-04-26 18:49:06 +0100124 WARN_ON(iio_dev_opaque->trig_readonly);
Matt Ranostayc8cdf702016-09-02 23:36:15 -0700125
126 indio_dev->trig = iio_trigger_get(trig);
Jonathan Cameron3028e0c2021-04-26 18:49:06 +0100127 iio_dev_opaque->trig_readonly = true;
Nuno Sá16afe122022-10-12 17:16:20 +0200128 mutex_unlock(&iio_dev_opaque->mlock);
Matt Ranostayc8cdf702016-09-02 23:36:15 -0700129
130 return 0;
131}
132EXPORT_SYMBOL(iio_trigger_set_immutable);
133
Crestez Dan Leonard3b8e73e2016-05-23 21:40:01 +0300134/* Search for trigger by name, assuming iio_trigger_list_lock held */
135static struct iio_trigger *__iio_trigger_find_by_name(const char *name)
136{
137 struct iio_trigger *iter;
138
139 list_for_each_entry(iter, &iio_trigger_list, list)
140 if (!strcmp(iter->name, name))
141 return iter;
142
143 return NULL;
144}
145
Alison Schofieldd5d24bc2017-01-21 19:28:52 -0800146static struct iio_trigger *iio_trigger_acquire_by_name(const char *name)
Jonathan Cameron1637db42009-08-18 18:06:26 +0100147{
Jonathan Cameronf6517f22011-04-15 18:55:53 +0100148 struct iio_trigger *trig = NULL, *iter;
Michael Hennerich7c327852010-04-26 10:49:10 +0200149
Jonathan Cameron1637db42009-08-18 18:06:26 +0100150 mutex_lock(&iio_trigger_list_lock);
Jonathan Cameronf6517f22011-04-15 18:55:53 +0100151 list_for_each_entry(iter, &iio_trigger_list, list)
152 if (sysfs_streq(iter->name, name)) {
153 trig = iter;
Alison Schofieldd5d24bc2017-01-21 19:28:52 -0800154 iio_trigger_get(trig);
Jonathan Cameron1637db42009-08-18 18:06:26 +0100155 break;
156 }
Jonathan Cameron1637db42009-08-18 18:06:26 +0100157 mutex_unlock(&iio_trigger_list_lock);
158
Jonathan Cameronf6517f22011-04-15 18:55:53 +0100159 return trig;
Jonathan Cameron5f874042010-05-04 22:20:54 +0100160}
Jonathan Cameron1637db42009-08-18 18:06:26 +0100161
Jonathan Cameron9020ef62021-10-17 18:22:09 +0100162static void iio_reenable_work_fn(struct work_struct *work)
163{
164 struct iio_trigger *trig = container_of(work, struct iio_trigger,
165 reenable_work);
166
167 /*
168 * This 'might' occur after the trigger state is set to disabled -
169 * in that case the driver should skip reenabling.
170 */
171 trig->ops->reenable(trig);
172}
173
174/*
175 * In general, reenable callbacks may need to sleep and this path is
176 * not performance sensitive, so just queue up a work item
177 * to reneable the trigger for us.
178 *
179 * Races that can cause this.
180 * 1) A handler occurs entirely in interrupt context so the counter
181 * the final decrement is still in this interrupt.
182 * 2) The trigger has been removed, but one last interrupt gets through.
183 *
184 * For (1) we must call reenable, but not in atomic context.
185 * For (2) it should be safe to call reenanble, if drivers never blindly
186 * reenable after state is off.
187 */
188static void iio_trigger_notify_done_atomic(struct iio_trigger *trig)
189{
190 if (atomic_dec_and_test(&trig->use_count) && trig->ops &&
191 trig->ops->reenable)
192 schedule_work(&trig->reenable_work);
193}
194
Mehdi Djait4ad682e2023-03-02 14:04:35 +0100195/**
196 * iio_trigger_poll() - Call the IRQ trigger handler of the consumers
197 * @trig: trigger which occurred
198 *
199 * This function should only be called from a hard IRQ context.
200 */
Peter Meerwald398fd222014-12-06 06:46:00 +0000201void iio_trigger_poll(struct iio_trigger *trig)
Jonathan Cameron1637db42009-08-18 18:06:26 +0100202{
Jonathan Camerond96d1332011-05-18 14:41:18 +0100203 int i;
Lars-Peter Clausena1a8e1d2013-07-16 15:28:00 +0100204
205 if (!atomic_read(&trig->use_count)) {
206 atomic_set(&trig->use_count, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
207
208 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
209 if (trig->subirqs[i].enabled)
Jonathan Camerond96d1332011-05-18 14:41:18 +0100210 generic_handle_irq(trig->subirq_base + i);
Lars-Peter Clausena1a8e1d2013-07-16 15:28:00 +0100211 else
Jonathan Cameron9020ef62021-10-17 18:22:09 +0100212 iio_trigger_notify_done_atomic(trig);
Lars-Peter Clausena1a8e1d2013-07-16 15:28:00 +0100213 }
214 }
Jonathan Cameron1637db42009-08-18 18:06:26 +0100215}
216EXPORT_SYMBOL(iio_trigger_poll);
217
Jonathan Cameron8384d952011-05-18 14:41:21 +0100218irqreturn_t iio_trigger_generic_data_rdy_poll(int irq, void *private)
219{
Peter Meerwald398fd222014-12-06 06:46:00 +0000220 iio_trigger_poll(private);
Jonathan Cameron8384d952011-05-18 14:41:21 +0100221 return IRQ_HANDLED;
222}
223EXPORT_SYMBOL(iio_trigger_generic_data_rdy_poll);
224
Mehdi Djaitf700e552023-03-02 14:04:36 +0100225/**
226 * iio_trigger_poll_nested() - Call the threaded trigger handler of the
227 * consumers
228 * @trig: trigger which occurred
229 *
230 * This function should only be called from a kernel thread context.
231 */
232void iio_trigger_poll_nested(struct iio_trigger *trig)
Jonathan Cameron1f785682011-05-18 14:42:11 +0100233{
234 int i;
Lars-Peter Clausena1a8e1d2013-07-16 15:28:00 +0100235
236 if (!atomic_read(&trig->use_count)) {
237 atomic_set(&trig->use_count, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
238
239 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
240 if (trig->subirqs[i].enabled)
Jonathan Cameron1f785682011-05-18 14:42:11 +0100241 handle_nested_irq(trig->subirq_base + i);
Lars-Peter Clausena1a8e1d2013-07-16 15:28:00 +0100242 else
243 iio_trigger_notify_done(trig);
244 }
245 }
Jonathan Cameron1f785682011-05-18 14:42:11 +0100246}
Mehdi Djaitf700e552023-03-02 14:04:36 +0100247EXPORT_SYMBOL(iio_trigger_poll_nested);
Jonathan Cameron1f785682011-05-18 14:42:11 +0100248
Jonathan Cameron1637db42009-08-18 18:06:26 +0100249void iio_trigger_notify_done(struct iio_trigger *trig)
250{
Jonathan Cameron04581682017-07-23 17:25:45 +0100251 if (atomic_dec_and_test(&trig->use_count) && trig->ops &&
Jonathan Cameroneca85232020-09-20 14:25:48 +0100252 trig->ops->reenable)
253 trig->ops->reenable(trig);
Jonathan Cameron1637db42009-08-18 18:06:26 +0100254}
255EXPORT_SYMBOL(iio_trigger_notify_done);
256
Jonathan Cameron1637db42009-08-18 18:06:26 +0100257/* Trigger Consumer related functions */
Jonathan Cameron208b813c2011-08-24 17:28:41 +0100258static int iio_trigger_get_irq(struct iio_trigger *trig)
259{
260 int ret;
Andy Shevchenkoaf3bac42021-04-02 20:49:11 +0300261
Jonathan Cameron208b813c2011-08-24 17:28:41 +0100262 mutex_lock(&trig->pool_lock);
263 ret = bitmap_find_free_region(trig->pool,
264 CONFIG_IIO_CONSUMERS_PER_TRIGGER,
265 ilog2(1));
266 mutex_unlock(&trig->pool_lock);
267 if (ret >= 0)
268 ret += trig->subirq_base;
269
270 return ret;
271}
272
273static void iio_trigger_put_irq(struct iio_trigger *trig, int irq)
274{
275 mutex_lock(&trig->pool_lock);
276 clear_bit(irq - trig->subirq_base, trig->pool);
277 mutex_unlock(&trig->pool_lock);
278}
Jonathan Cameron1637db42009-08-18 18:06:26 +0100279
280/* Complexity in here. With certain triggers (datardy) an acknowledgement
281 * may be needed if the pollfuncs do not include the data read for the
282 * triggering device.
283 * This is not currently handled. Alternative of not enabling trigger unless
284 * the relevant function is in there may be the best option.
285 */
Peter Meerwald860c9c52013-02-04 11:36:00 +0000286/* Worth protecting against double additions? */
Lars-Peter Clausenf11d59d2020-05-25 14:38:53 +0300287int iio_trigger_attach_poll_func(struct iio_trigger *trig,
288 struct iio_poll_func *pf)
Jonathan Cameron1637db42009-08-18 18:06:26 +0100289{
Jonathan Cameron6eaf9f62021-04-26 18:49:05 +0100290 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(pf->indio_dev);
Andy Shevchenkoaf3bac42021-04-02 20:49:11 +0300291 bool notinuse =
292 bitmap_empty(trig->pool, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
Jonathan Cameron1637db42009-08-18 18:06:26 +0100293 int ret = 0;
Jonathan Cameron1637db42009-08-18 18:06:26 +0100294
Peter Meerwald860c9c52013-02-04 11:36:00 +0000295 /* Prevent the module from being removed whilst attached to a trigger */
Jonathan Cameron6eaf9f62021-04-26 18:49:05 +0100296 __module_get(iio_dev_opaque->driver_module);
Crestez Dan Leonard99543822016-05-03 15:27:09 +0300297
298 /* Get irq number */
Jonathan Cameron51c060a2011-05-18 14:41:42 +0100299 pf->irq = iio_trigger_get_irq(trig);
Mathieu Othacehebe35d282019-02-20 17:49:10 +0100300 if (pf->irq < 0) {
301 pr_err("Could not find an available irq for trigger %s, CONFIG_IIO_CONSUMERS_PER_TRIGGER=%d limit might be exceeded\n",
302 trig->name, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
Crestez Dan Leonard99543822016-05-03 15:27:09 +0300303 goto out_put_module;
Mathieu Othacehebe35d282019-02-20 17:49:10 +0100304 }
Crestez Dan Leonard99543822016-05-03 15:27:09 +0300305
306 /* Request irq */
Jonathan Cameron51c060a2011-05-18 14:41:42 +0100307 ret = request_threaded_irq(pf->irq, pf->h, pf->thread,
308 pf->type, pf->name,
309 pf);
Crestez Dan Leonard99543822016-05-03 15:27:09 +0300310 if (ret < 0)
311 goto out_put_irq;
Jonathan Cameron5dd72ec2011-10-26 17:27:41 +0100312
Crestez Dan Leonard99543822016-05-03 15:27:09 +0300313 /* Enable trigger in driver */
Jonathan Cameron04581682017-07-23 17:25:45 +0100314 if (trig->ops && trig->ops->set_trigger_state && notinuse) {
Jonathan Camerond29f73d2011-08-12 17:08:38 +0100315 ret = trig->ops->set_trigger_state(trig, true);
Jonathan Cameron5dd72ec2011-10-26 17:27:41 +0100316 if (ret < 0)
Crestez Dan Leonard99543822016-05-03 15:27:09 +0300317 goto out_free_irq;
Jonathan Cameron5dd72ec2011-10-26 17:27:41 +0100318 }
Jonathan Camerond96d1332011-05-18 14:41:18 +0100319
Linus Walleij702a7b82016-09-01 10:27:17 +0200320 /*
321 * Check if we just registered to our own trigger: we determine that
322 * this is the case if the IIO device and the trigger device share the
323 * same parent device.
324 */
Matti Vaittinen517985e2023-05-08 13:31:17 +0300325 if (iio_validate_own_trigger(pf->indio_dev, trig))
Linus Walleij702a7b82016-09-01 10:27:17 +0200326 trig->attached_own_device = true;
327
Jonathan Cameron1637db42009-08-18 18:06:26 +0100328 return ret;
Crestez Dan Leonard99543822016-05-03 15:27:09 +0300329
330out_free_irq:
331 free_irq(pf->irq, pf);
332out_put_irq:
333 iio_trigger_put_irq(trig, pf->irq);
334out_put_module:
Jonathan Cameron6eaf9f62021-04-26 18:49:05 +0100335 module_put(iio_dev_opaque->driver_module);
Crestez Dan Leonard99543822016-05-03 15:27:09 +0300336 return ret;
Jonathan Cameron1637db42009-08-18 18:06:26 +0100337}
Jonathan Cameron1637db42009-08-18 18:06:26 +0100338
Lars-Peter Clausenf11d59d2020-05-25 14:38:53 +0300339int iio_trigger_detach_poll_func(struct iio_trigger *trig,
340 struct iio_poll_func *pf)
Jonathan Cameron1637db42009-08-18 18:06:26 +0100341{
Jonathan Cameron6eaf9f62021-04-26 18:49:05 +0100342 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(pf->indio_dev);
Andy Shevchenkoaf3bac42021-04-02 20:49:11 +0300343 bool no_other_users =
344 bitmap_weight(trig->pool, CONFIG_IIO_CONSUMERS_PER_TRIGGER) == 1;
Jonathan Cameron51c060a2011-05-18 14:41:42 +0100345 int ret = 0;
Andy Shevchenkoaf3bac42021-04-02 20:49:11 +0300346
Jonathan Cameron04581682017-07-23 17:25:45 +0100347 if (trig->ops && trig->ops->set_trigger_state && no_other_users) {
Jonathan Camerond29f73d2011-08-12 17:08:38 +0100348 ret = trig->ops->set_trigger_state(trig, false);
Jonathan Cameron51c060a2011-05-18 14:41:42 +0100349 if (ret)
Hartmut Knaack92825ff2014-02-16 11:53:00 +0000350 return ret;
Jonathan Cameron1637db42009-08-18 18:06:26 +0100351 }
Linus Walleij702a7b82016-09-01 10:27:17 +0200352 if (pf->indio_dev->dev.parent == trig->dev.parent)
353 trig->attached_own_device = false;
Jonathan Cameron51c060a2011-05-18 14:41:42 +0100354 iio_trigger_put_irq(trig, pf->irq);
355 free_irq(pf->irq, pf);
Jonathan Cameron6eaf9f62021-04-26 18:49:05 +0100356 module_put(iio_dev_opaque->driver_module);
Jonathan Cameron1637db42009-08-18 18:06:26 +0100357
Jonathan Cameron1637db42009-08-18 18:06:26 +0100358 return ret;
359}
Jonathan Cameron1637db42009-08-18 18:06:26 +0100360
Jonathan Camerond96d1332011-05-18 14:41:18 +0100361irqreturn_t iio_pollfunc_store_time(int irq, void *p)
362{
363 struct iio_poll_func *pf = p;
Andy Shevchenkoaf3bac42021-04-02 20:49:11 +0300364
Gregor Boiriebc2b7da2016-03-09 19:05:49 +0100365 pf->timestamp = iio_get_time_ns(pf->indio_dev);
Jonathan Camerond96d1332011-05-18 14:41:18 +0100366 return IRQ_WAKE_THREAD;
367}
368EXPORT_SYMBOL(iio_pollfunc_store_time);
369
Jonathan Cameron21b185f2011-05-18 14:42:32 +0100370struct iio_poll_func
371*iio_alloc_pollfunc(irqreturn_t (*h)(int irq, void *p),
372 irqreturn_t (*thread)(int irq, void *p),
373 int type,
Jonathan Camerone65bc6ac2011-08-24 17:28:36 +0100374 struct iio_dev *indio_dev,
Jonathan Cameron21b185f2011-05-18 14:42:32 +0100375 const char *fmt,
376 ...)
377{
378 va_list vargs;
379 struct iio_poll_func *pf;
380
Joe Simmons-Talbottef7cece2022-07-17 11:34:38 -0400381 pf = kmalloc(sizeof(*pf), GFP_KERNEL);
Joe Simmons-Talbott295cc422022-07-17 22:03:48 -0400382 if (!pf)
Jonathan Cameron21b185f2011-05-18 14:42:32 +0100383 return NULL;
384 va_start(vargs, fmt);
385 pf->name = kvasprintf(GFP_KERNEL, fmt, vargs);
386 va_end(vargs);
387 if (pf->name == NULL) {
388 kfree(pf);
389 return NULL;
390 }
391 pf->h = h;
392 pf->thread = thread;
393 pf->type = type;
Jonathan Camerone65bc6ac2011-08-24 17:28:36 +0100394 pf->indio_dev = indio_dev;
Jonathan Cameron21b185f2011-05-18 14:42:32 +0100395
396 return pf;
397}
398EXPORT_SYMBOL_GPL(iio_alloc_pollfunc);
399
400void iio_dealloc_pollfunc(struct iio_poll_func *pf)
401{
402 kfree(pf->name);
403 kfree(pf);
404}
405EXPORT_SYMBOL_GPL(iio_dealloc_pollfunc);
406
Jonathan Cameron1637db42009-08-18 18:06:26 +0100407/**
Joe Simmons-Talbott9cf0b612022-06-01 14:54:14 -0400408 * current_trigger_show() - trigger consumer sysfs query current trigger
Cristina Opriceana8e563b02015-08-06 14:56:02 +0300409 * @dev: device associated with an industrial I/O device
410 * @attr: pointer to the device_attribute structure that
411 * is being processed
412 * @buf: buffer where the current trigger name will be printed into
Jonathan Cameron1637db42009-08-18 18:06:26 +0100413 *
414 * For trigger consumers the current_trigger interface allows the trigger
415 * used by the device to be queried.
Cristina Opriceana8e563b02015-08-06 14:56:02 +0300416 *
417 * Return: a negative number on failure, the number of characters written
418 * on success or 0 if no trigger is available
419 */
Joe Simmons-Talbott9cf0b612022-06-01 14:54:14 -0400420static ssize_t current_trigger_show(struct device *dev,
421 struct device_attribute *attr, char *buf)
Jonathan Cameron1637db42009-08-18 18:06:26 +0100422{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200423 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameroncb6c89a2011-08-24 17:28:40 +0100424
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100425 if (indio_dev->trig)
Lars-Peter Clausen83ca56b2021-03-20 08:14:02 +0100426 return sysfs_emit(buf, "%s\n", indio_dev->trig->name);
Jonathan Cameroncb6c89a2011-08-24 17:28:40 +0100427 return 0;
Jonathan Cameron1637db42009-08-18 18:06:26 +0100428}
429
430/**
Joe Simmons-Talbott9cf0b612022-06-01 14:54:14 -0400431 * current_trigger_store() - trigger consumer sysfs set current trigger
Cristina Opriceana8e563b02015-08-06 14:56:02 +0300432 * @dev: device associated with an industrial I/O device
433 * @attr: device attribute that is being processed
434 * @buf: string buffer that holds the name of the trigger
435 * @len: length of the trigger name held by buf
Jonathan Cameron1637db42009-08-18 18:06:26 +0100436 *
437 * For trigger consumers the current_trigger interface allows the trigger
Peter Meerwald17666ef2013-11-12 21:49:00 +0000438 * used for this device to be specified at run time based on the trigger's
Jonathan Cameron1637db42009-08-18 18:06:26 +0100439 * name.
Cristina Opriceana8e563b02015-08-06 14:56:02 +0300440 *
441 * Return: negative error code on failure or length of the buffer
442 * on success
443 */
Joe Simmons-Talbott9cf0b612022-06-01 14:54:14 -0400444static ssize_t current_trigger_store(struct device *dev,
445 struct device_attribute *attr,
446 const char *buf, size_t len)
Jonathan Cameron1637db42009-08-18 18:06:26 +0100447{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200448 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron3028e0c2021-04-26 18:49:06 +0100449 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100450 struct iio_trigger *oldtrig = indio_dev->trig;
Michael Hennerich43a43602011-06-27 13:07:09 +0100451 struct iio_trigger *trig;
452 int ret;
453
Nuno Sá16afe122022-10-12 17:16:20 +0200454 mutex_lock(&iio_dev_opaque->mlock);
Miquel Raynal51570c92022-02-07 15:38:38 +0100455 if (iio_dev_opaque->currentmode == INDIO_BUFFER_TRIGGERED) {
Nuno Sá16afe122022-10-12 17:16:20 +0200456 mutex_unlock(&iio_dev_opaque->mlock);
Jonathan Cameron1637db42009-08-18 18:06:26 +0100457 return -EBUSY;
458 }
Jonathan Cameron3028e0c2021-04-26 18:49:06 +0100459 if (iio_dev_opaque->trig_readonly) {
Nuno Sá16afe122022-10-12 17:16:20 +0200460 mutex_unlock(&iio_dev_opaque->mlock);
Matt Ranostayc8cdf702016-09-02 23:36:15 -0700461 return -EPERM;
462 }
Nuno Sá16afe122022-10-12 17:16:20 +0200463 mutex_unlock(&iio_dev_opaque->mlock);
Jonathan Cameron1637db42009-08-18 18:06:26 +0100464
Alison Schofieldd5d24bc2017-01-21 19:28:52 -0800465 trig = iio_trigger_acquire_by_name(buf);
466 if (oldtrig == trig) {
467 ret = len;
468 goto out_trigger_put;
469 }
Michael Hennerich43a43602011-06-27 13:07:09 +0100470
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100471 if (trig && indio_dev->info->validate_trigger) {
472 ret = indio_dev->info->validate_trigger(indio_dev, trig);
Michael Hennerich43a43602011-06-27 13:07:09 +0100473 if (ret)
Alison Schofieldd5d24bc2017-01-21 19:28:52 -0800474 goto out_trigger_put;
Michael Hennerich43a43602011-06-27 13:07:09 +0100475 }
476
Jonathan Cameron04581682017-07-23 17:25:45 +0100477 if (trig && trig->ops && trig->ops->validate_device) {
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100478 ret = trig->ops->validate_device(trig, indio_dev);
Michael Hennerich43a43602011-06-27 13:07:09 +0100479 if (ret)
Alison Schofieldd5d24bc2017-01-21 19:28:52 -0800480 goto out_trigger_put;
Michael Hennerich43a43602011-06-27 13:07:09 +0100481 }
482
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100483 indio_dev->trig = trig;
Michael Hennerich43a43602011-06-27 13:07:09 +0100484
Vladimir Barinov735ad072015-08-20 22:37:39 +0300485 if (oldtrig) {
486 if (indio_dev->modes & INDIO_EVENT_TRIGGERED)
487 iio_trigger_detach_poll_func(oldtrig,
488 indio_dev->pollfunc_event);
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200489 iio_trigger_put(oldtrig);
Vladimir Barinov735ad072015-08-20 22:37:39 +0300490 }
491 if (indio_dev->trig) {
Vladimir Barinov735ad072015-08-20 22:37:39 +0300492 if (indio_dev->modes & INDIO_EVENT_TRIGGERED)
493 iio_trigger_attach_poll_func(indio_dev->trig,
494 indio_dev->pollfunc_event);
495 }
Jonathan Cameron1637db42009-08-18 18:06:26 +0100496
497 return len;
Alison Schofieldd5d24bc2017-01-21 19:28:52 -0800498
499out_trigger_put:
Marcin Niestroj4eecbe82017-05-18 09:12:06 +0200500 if (trig)
501 iio_trigger_put(trig);
Alison Schofieldd5d24bc2017-01-21 19:28:52 -0800502 return ret;
Jonathan Cameron1637db42009-08-18 18:06:26 +0100503}
504
Joe Simmons-Talbott9cf0b612022-06-01 14:54:14 -0400505static DEVICE_ATTR_RW(current_trigger);
Jonathan Cameron1637db42009-08-18 18:06:26 +0100506
507static struct attribute *iio_trigger_consumer_attrs[] = {
508 &dev_attr_current_trigger.attr,
509 NULL,
510};
511
512static const struct attribute_group iio_trigger_consumer_attr_group = {
513 .name = "trigger",
514 .attrs = iio_trigger_consumer_attrs,
515};
516
517static void iio_trig_release(struct device *device)
518{
519 struct iio_trigger *trig = to_iio_trigger(device);
Jonathan Camerond96d1332011-05-18 14:41:18 +0100520 int i;
521
522 if (trig->subirq_base) {
523 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
524 irq_modify_status(trig->subirq_base + i,
525 IRQ_NOAUTOEN,
526 IRQ_NOREQUEST | IRQ_NOPROBE);
527 irq_set_chip(trig->subirq_base + i,
528 NULL);
529 irq_set_handler(trig->subirq_base + i,
530 NULL);
531 }
532
533 irq_free_descs(trig->subirq_base,
534 CONFIG_IIO_CONSUMERS_PER_TRIGGER);
535 }
Jonathan Cameron59c85e82011-05-18 14:42:22 +0100536 kfree(trig->name);
Jonathan Cameron1637db42009-08-18 18:06:26 +0100537 kfree(trig);
Jonathan Cameron1637db42009-08-18 18:06:26 +0100538}
539
Bhumika Goyal3bdafc42017-01-21 22:29:49 +0530540static const struct device_type iio_trig_type = {
Jonathan Cameron1637db42009-08-18 18:06:26 +0100541 .release = iio_trig_release,
Axel Linf59c2572013-12-02 02:57:00 +0000542 .groups = iio_trig_dev_groups,
Jonathan Cameron1637db42009-08-18 18:06:26 +0100543};
544
Jonathan Camerond96d1332011-05-18 14:41:18 +0100545static void iio_trig_subirqmask(struct irq_data *d)
546{
547 struct irq_chip *chip = irq_data_get_irq_chip(d);
Andy Shevchenkoaf3bac42021-04-02 20:49:11 +0300548 struct iio_trigger *trig = container_of(chip, struct iio_trigger, subirq_chip);
549
Jonathan Camerond96d1332011-05-18 14:41:18 +0100550 trig->subirqs[d->irq - trig->subirq_base].enabled = false;
551}
552
553static void iio_trig_subirqunmask(struct irq_data *d)
554{
555 struct irq_chip *chip = irq_data_get_irq_chip(d);
Andy Shevchenkoaf3bac42021-04-02 20:49:11 +0300556 struct iio_trigger *trig = container_of(chip, struct iio_trigger, subirq_chip);
557
Jonathan Camerond96d1332011-05-18 14:41:18 +0100558 trig->subirqs[d->irq - trig->subirq_base].enabled = true;
559}
560
Dmitry Rokosovbc72d932022-06-01 17:48:32 +0000561static __printf(3, 0)
Gwendal Grignou995071d2021-03-09 11:36:13 -0800562struct iio_trigger *viio_trigger_alloc(struct device *parent,
Dmitry Rokosovbc72d932022-06-01 17:48:32 +0000563 struct module *this_mod,
Gwendal Grignou995071d2021-03-09 11:36:13 -0800564 const char *fmt,
565 va_list vargs)
Jonathan Cameron1637db42009-08-18 18:06:26 +0100566{
567 struct iio_trigger *trig;
Dan Carpenter2c99f1a2017-01-21 07:55:58 +0300568 int i;
569
Joe Simmons-Talbottef7cece2022-07-17 11:34:38 -0400570 trig = kzalloc(sizeof(*trig), GFP_KERNEL);
Dan Carpenter2c99f1a2017-01-21 07:55:58 +0300571 if (!trig)
572 return NULL;
Jonathan Camerond96d1332011-05-18 14:41:18 +0100573
Gwendal Grignou995071d2021-03-09 11:36:13 -0800574 trig->dev.parent = parent;
Dan Carpenter2c99f1a2017-01-21 07:55:58 +0300575 trig->dev.type = &iio_trig_type;
576 trig->dev.bus = &iio_bus_type;
577 device_initialize(&trig->dev);
Jonathan Cameron9020ef62021-10-17 18:22:09 +0100578 INIT_WORK(&trig->reenable_work, iio_reenable_work_fn);
Jacek Anaszewskid5363212013-08-16 14:11:00 +0100579
Dan Carpenter2c99f1a2017-01-21 07:55:58 +0300580 mutex_init(&trig->pool_lock);
581 trig->subirq_base = irq_alloc_descs(-1, 0,
582 CONFIG_IIO_CONSUMERS_PER_TRIGGER,
583 0);
584 if (trig->subirq_base < 0)
585 goto free_trig;
586
587 trig->name = kvasprintf(GFP_KERNEL, fmt, vargs);
588 if (trig->name == NULL)
589 goto free_descs;
590
Dmitry Rokosov4a080692022-06-07 18:39:18 +0000591 INIT_LIST_HEAD(&trig->list);
592
Dmitry Rokosovbc72d932022-06-01 17:48:32 +0000593 trig->owner = this_mod;
594
Dan Carpenter2c99f1a2017-01-21 07:55:58 +0300595 trig->subirq_chip.name = trig->name;
596 trig->subirq_chip.irq_mask = &iio_trig_subirqmask;
597 trig->subirq_chip.irq_unmask = &iio_trig_subirqunmask;
598 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
599 irq_set_chip(trig->subirq_base + i, &trig->subirq_chip);
600 irq_set_handler(trig->subirq_base + i, &handle_simple_irq);
601 irq_modify_status(trig->subirq_base + i,
602 IRQ_NOREQUEST | IRQ_NOAUTOEN, IRQ_NOPROBE);
Jonathan Cameron1637db42009-08-18 18:06:26 +0100603 }
Jacek Anaszewskid5363212013-08-16 14:11:00 +0100604
605 return trig;
Dan Carpenter2c99f1a2017-01-21 07:55:58 +0300606
607free_descs:
608 irq_free_descs(trig->subirq_base, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
609free_trig:
610 kfree(trig);
611 return NULL;
Jacek Anaszewskid5363212013-08-16 14:11:00 +0100612}
613
Gwendal Grignou995071d2021-03-09 11:36:13 -0800614/**
Dmitry Rokosovbc72d932022-06-01 17:48:32 +0000615 * __iio_trigger_alloc - Allocate a trigger
Gwendal Grignou995071d2021-03-09 11:36:13 -0800616 * @parent: Device to allocate iio_trigger for
Dmitry Rokosovbc72d932022-06-01 17:48:32 +0000617 * @this_mod: module allocating the trigger
Gwendal Grignou995071d2021-03-09 11:36:13 -0800618 * @fmt: trigger name format. If it includes format
619 * specifiers, the additional arguments following
620 * format are formatted and inserted in the resulting
621 * string replacing their respective specifiers.
622 * RETURNS:
623 * Pointer to allocated iio_trigger on success, NULL on failure.
624 */
Dmitry Rokosovbc72d932022-06-01 17:48:32 +0000625struct iio_trigger *__iio_trigger_alloc(struct device *parent,
626 struct module *this_mod,
627 const char *fmt, ...)
Jacek Anaszewskid5363212013-08-16 14:11:00 +0100628{
629 struct iio_trigger *trig;
630 va_list vargs;
631
632 va_start(vargs, fmt);
Dmitry Rokosovbc72d932022-06-01 17:48:32 +0000633 trig = viio_trigger_alloc(parent, this_mod, fmt, vargs);
Jacek Anaszewskid5363212013-08-16 14:11:00 +0100634 va_end(vargs);
635
Jonathan Cameron1637db42009-08-18 18:06:26 +0100636 return trig;
637}
Dmitry Rokosovbc72d932022-06-01 17:48:32 +0000638EXPORT_SYMBOL(__iio_trigger_alloc);
Jonathan Cameron1637db42009-08-18 18:06:26 +0100639
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200640void iio_trigger_free(struct iio_trigger *trig)
Jonathan Cameron1637db42009-08-18 18:06:26 +0100641{
642 if (trig)
643 put_device(&trig->dev);
644}
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200645EXPORT_SYMBOL(iio_trigger_free);
Jonathan Cameron1637db42009-08-18 18:06:26 +0100646
Jacek Anaszewskid5363212013-08-16 14:11:00 +0100647static void devm_iio_trigger_release(struct device *dev, void *res)
648{
649 iio_trigger_free(*(struct iio_trigger **)res);
650}
651
Sachin Kamata7e57dc2013-10-29 11:39:00 +0000652/**
Dmitry Rokosovbc72d932022-06-01 17:48:32 +0000653 * __devm_iio_trigger_alloc - Resource-managed iio_trigger_alloc()
Gwendal Grignou995071d2021-03-09 11:36:13 -0800654 * Managed iio_trigger_alloc. iio_trigger allocated with this function is
655 * automatically freed on driver detach.
656 * @parent: Device to allocate iio_trigger for
Dmitry Rokosovbc72d932022-06-01 17:48:32 +0000657 * @this_mod: module allocating the trigger
Sachin Kamata7e57dc2013-10-29 11:39:00 +0000658 * @fmt: trigger name format. If it includes format
659 * specifiers, the additional arguments following
660 * format are formatted and inserted in the resulting
661 * string replacing their respective specifiers.
662 *
Sachin Kamata7e57dc2013-10-29 11:39:00 +0000663 *
Sachin Kamata7e57dc2013-10-29 11:39:00 +0000664 * RETURNS:
665 * Pointer to allocated iio_trigger on success, NULL on failure.
666 */
Dmitry Rokosovbc72d932022-06-01 17:48:32 +0000667struct iio_trigger *__devm_iio_trigger_alloc(struct device *parent,
668 struct module *this_mod,
669 const char *fmt, ...)
Jacek Anaszewskid5363212013-08-16 14:11:00 +0100670{
671 struct iio_trigger **ptr, *trig;
672 va_list vargs;
673
674 ptr = devres_alloc(devm_iio_trigger_release, sizeof(*ptr),
675 GFP_KERNEL);
676 if (!ptr)
677 return NULL;
678
679 /* use raw alloc_dr for kmalloc caller tracing */
680 va_start(vargs, fmt);
Dmitry Rokosovbc72d932022-06-01 17:48:32 +0000681 trig = viio_trigger_alloc(parent, this_mod, fmt, vargs);
Jacek Anaszewskid5363212013-08-16 14:11:00 +0100682 va_end(vargs);
683 if (trig) {
684 *ptr = trig;
Gwendal Grignou995071d2021-03-09 11:36:13 -0800685 devres_add(parent, ptr);
Jacek Anaszewskid5363212013-08-16 14:11:00 +0100686 } else {
687 devres_free(ptr);
688 }
689
690 return trig;
691}
Dmitry Rokosovbc72d932022-06-01 17:48:32 +0000692EXPORT_SYMBOL_GPL(__devm_iio_trigger_alloc);
Jacek Anaszewskid5363212013-08-16 14:11:00 +0100693
Yicong Yang171a70a2021-04-08 19:38:15 +0800694static void devm_iio_trigger_unreg(void *trigger_info)
Gregor Boirie90833252016-09-02 20:47:54 +0200695{
Yicong Yang171a70a2021-04-08 19:38:15 +0800696 iio_trigger_unregister(trigger_info);
Gregor Boirie90833252016-09-02 20:47:54 +0200697}
698
699/**
Dmitry Rokosovbc72d932022-06-01 17:48:32 +0000700 * devm_iio_trigger_register - Resource-managed iio_trigger_register()
Gregor Boirie90833252016-09-02 20:47:54 +0200701 * @dev: device this trigger was allocated for
702 * @trig_info: trigger to register
703 *
704 * Managed iio_trigger_register(). The IIO trigger registered with this
705 * function is automatically unregistered on driver detach. This function
706 * calls iio_trigger_register() internally. Refer to that function for more
707 * information.
708 *
Gregor Boirie90833252016-09-02 20:47:54 +0200709 * RETURNS:
710 * 0 on success, negative error number on failure.
711 */
Dmitry Rokosovbc72d932022-06-01 17:48:32 +0000712int devm_iio_trigger_register(struct device *dev,
713 struct iio_trigger *trig_info)
Gregor Boirie90833252016-09-02 20:47:54 +0200714{
Gregor Boirie90833252016-09-02 20:47:54 +0200715 int ret;
716
Dmitry Rokosovbc72d932022-06-01 17:48:32 +0000717 ret = iio_trigger_register(trig_info);
Yicong Yang171a70a2021-04-08 19:38:15 +0800718 if (ret)
719 return ret;
Gregor Boirie90833252016-09-02 20:47:54 +0200720
Yicong Yang171a70a2021-04-08 19:38:15 +0800721 return devm_add_action_or_reset(dev, devm_iio_trigger_unreg, trig_info);
Gregor Boirie90833252016-09-02 20:47:54 +0200722}
Dmitry Rokosovbc72d932022-06-01 17:48:32 +0000723EXPORT_SYMBOL_GPL(devm_iio_trigger_register);
Gregor Boirie90833252016-09-02 20:47:54 +0200724
Linus Walleij702a7b82016-09-01 10:27:17 +0200725bool iio_trigger_using_own(struct iio_dev *indio_dev)
726{
727 return indio_dev->trig->attached_own_device;
728}
729EXPORT_SYMBOL(iio_trigger_using_own);
730
Lars-Peter Clausen43ece272016-09-23 17:19:41 +0200731/**
Matti Vaittinen517985e2023-05-08 13:31:17 +0300732 * iio_validate_own_trigger - Check if a trigger and IIO device belong to
733 * the same device
734 * @idev: the IIO device to check
735 * @trig: the IIO trigger to check
736 *
737 * This function can be used as the validate_trigger callback for triggers that
738 * can only be attached to their own device.
739 *
740 * Return: 0 if both the trigger and the IIO device belong to the same
741 * device, -EINVAL otherwise.
742 */
743int iio_validate_own_trigger(struct iio_dev *idev, struct iio_trigger *trig)
744{
745 if (idev->dev.parent != trig->dev.parent)
746 return -EINVAL;
747 return 0;
748}
749EXPORT_SYMBOL_GPL(iio_validate_own_trigger);
750
751/**
Lars-Peter Clausen43ece272016-09-23 17:19:41 +0200752 * iio_trigger_validate_own_device - Check if a trigger and IIO device belong to
753 * the same device
754 * @trig: The IIO trigger to check
755 * @indio_dev: the IIO device to check
756 *
757 * This function can be used as the validate_device callback for triggers that
758 * can only be attached to their own device.
759 *
760 * Return: 0 if both the trigger and the IIO device belong to the same
761 * device, -EINVAL otherwise.
762 */
763int iio_trigger_validate_own_device(struct iio_trigger *trig,
Andy Shevchenkoaf3bac42021-04-02 20:49:11 +0300764 struct iio_dev *indio_dev)
Lars-Peter Clausen43ece272016-09-23 17:19:41 +0200765{
766 if (indio_dev->dev.parent != trig->dev.parent)
767 return -EINVAL;
768 return 0;
769}
770EXPORT_SYMBOL(iio_trigger_validate_own_device);
771
Alexandru Ardeleane64506b2021-02-15 12:40:28 +0200772int iio_device_register_trigger_consumer(struct iio_dev *indio_dev)
Jonathan Cameron1637db42009-08-18 18:06:26 +0100773{
Alexandru Ardelean32f17172021-02-15 12:40:29 +0200774 return iio_device_register_sysfs_group(indio_dev,
775 &iio_trigger_consumer_attr_group);
Jonathan Cameron1637db42009-08-18 18:06:26 +0100776}
Jonathan Cameron1637db42009-08-18 18:06:26 +0100777
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100778void iio_device_unregister_trigger_consumer(struct iio_dev *indio_dev)
Jonathan Cameron1637db42009-08-18 18:06:26 +0100779{
Peter Meerwald860c9c52013-02-04 11:36:00 +0000780 /* Clean up an associated but not attached trigger reference */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100781 if (indio_dev->trig)
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200782 iio_trigger_put(indio_dev->trig);
Jonathan Cameron1637db42009-08-18 18:06:26 +0100783}