1 // SPDX-License-Identifier: GPL-2.0-only
2 /* The industrial I/O core, trigger handling functions
3  *
4  * Copyright (c) 2008 Jonathan Cameron
5  */
6 
7 #include <linux/kernel.h>
8 #include <linux/idr.h>
9 #include <linux/err.h>
10 #include <linux/device.h>
11 #include <linux/interrupt.h>
12 #include <linux/list.h>
13 #include <linux/slab.h>
14 
15 #include <linux/iio/iio.h>
16 #include <linux/iio/iio-opaque.h>
17 #include <linux/iio/trigger.h>
18 #include "iio_core.h"
19 #include "iio_core_trigger.h"
20 #include <linux/iio/trigger_consumer.h>
21 
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 
33 static DEFINE_IDA(iio_trigger_ida);
34 
35 /* Single list of all available triggers */
36 static LIST_HEAD(iio_trigger_list);
37 static DEFINE_MUTEX(iio_trigger_list_lock);
38 
39 /**
40  * iio_trigger_read_name() - retrieve useful identifying name
41  * @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  */
49 static ssize_t iio_trigger_read_name(struct device *dev,
50 				     struct device_attribute *attr,
51 				     char *buf)
52 {
53 	struct iio_trigger *trig = to_iio_trigger(dev);
54 	return sysfs_emit(buf, "%s\n", trig->name);
55 }
56 
57 static DEVICE_ATTR(name, S_IRUGO, iio_trigger_read_name, NULL);
58 
59 static struct attribute *iio_trig_dev_attrs[] = {
60 	&dev_attr_name.attr,
61 	NULL,
62 };
63 ATTRIBUTE_GROUPS(iio_trig_dev);
64 
65 static struct iio_trigger *__iio_trigger_find_by_name(const char *name);
66 
67 int __iio_trigger_register(struct iio_trigger *trig_info,
68 			   struct module *this_mod)
69 {
70 	int ret;
71 
72 	trig_info->owner = this_mod;
73 
74 	trig_info->id = ida_simple_get(&iio_trigger_ida, 0, 0, GFP_KERNEL);
75 	if (trig_info->id < 0)
76 		return trig_info->id;
77 
78 	/* Set the name used for the sysfs directory etc */
79 	dev_set_name(&trig_info->dev, "trigger%d", trig_info->id);
80 
81 	ret = device_add(&trig_info->dev);
82 	if (ret)
83 		goto error_unregister_id;
84 
85 	/* Add to list of available triggers held by the IIO core */
86 	mutex_lock(&iio_trigger_list_lock);
87 	if (__iio_trigger_find_by_name(trig_info->name)) {
88 		pr_err("Duplicate trigger name '%s'\n", trig_info->name);
89 		ret = -EEXIST;
90 		goto error_device_del;
91 	}
92 	list_add_tail(&trig_info->list, &iio_trigger_list);
93 	mutex_unlock(&iio_trigger_list_lock);
94 
95 	return 0;
96 
97 error_device_del:
98 	mutex_unlock(&iio_trigger_list_lock);
99 	device_del(&trig_info->dev);
100 error_unregister_id:
101 	ida_simple_remove(&iio_trigger_ida, trig_info->id);
102 	return ret;
103 }
104 EXPORT_SYMBOL(__iio_trigger_register);
105 
106 void iio_trigger_unregister(struct iio_trigger *trig_info)
107 {
108 	mutex_lock(&iio_trigger_list_lock);
109 	list_del(&trig_info->list);
110 	mutex_unlock(&iio_trigger_list_lock);
111 
112 	ida_simple_remove(&iio_trigger_ida, trig_info->id);
113 	/* Possible issue in here */
114 	device_del(&trig_info->dev);
115 }
116 EXPORT_SYMBOL(iio_trigger_unregister);
117 
118 int iio_trigger_set_immutable(struct iio_dev *indio_dev, struct iio_trigger *trig)
119 {
120 	struct iio_dev_opaque *iio_dev_opaque;
121 
122 	if (!indio_dev || !trig)
123 		return -EINVAL;
124 
125 	iio_dev_opaque = to_iio_dev_opaque(indio_dev);
126 	mutex_lock(&indio_dev->mlock);
127 	WARN_ON(iio_dev_opaque->trig_readonly);
128 
129 	indio_dev->trig = iio_trigger_get(trig);
130 	iio_dev_opaque->trig_readonly = true;
131 	mutex_unlock(&indio_dev->mlock);
132 
133 	return 0;
134 }
135 EXPORT_SYMBOL(iio_trigger_set_immutable);
136 
137 /* Search for trigger by name, assuming iio_trigger_list_lock held */
138 static struct iio_trigger *__iio_trigger_find_by_name(const char *name)
139 {
140 	struct iio_trigger *iter;
141 
142 	list_for_each_entry(iter, &iio_trigger_list, list)
143 		if (!strcmp(iter->name, name))
144 			return iter;
145 
146 	return NULL;
147 }
148 
149 static struct iio_trigger *iio_trigger_acquire_by_name(const char *name)
150 {
151 	struct iio_trigger *trig = NULL, *iter;
152 
153 	mutex_lock(&iio_trigger_list_lock);
154 	list_for_each_entry(iter, &iio_trigger_list, list)
155 		if (sysfs_streq(iter->name, name)) {
156 			trig = iter;
157 			iio_trigger_get(trig);
158 			break;
159 		}
160 	mutex_unlock(&iio_trigger_list_lock);
161 
162 	return trig;
163 }
164 
165 void iio_trigger_poll(struct iio_trigger *trig)
166 {
167 	int i;
168 
169 	if (!atomic_read(&trig->use_count)) {
170 		atomic_set(&trig->use_count, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
171 
172 		for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
173 			if (trig->subirqs[i].enabled)
174 				generic_handle_irq(trig->subirq_base + i);
175 			else
176 				iio_trigger_notify_done(trig);
177 		}
178 	}
179 }
180 EXPORT_SYMBOL(iio_trigger_poll);
181 
182 irqreturn_t iio_trigger_generic_data_rdy_poll(int irq, void *private)
183 {
184 	iio_trigger_poll(private);
185 	return IRQ_HANDLED;
186 }
187 EXPORT_SYMBOL(iio_trigger_generic_data_rdy_poll);
188 
189 void iio_trigger_poll_chained(struct iio_trigger *trig)
190 {
191 	int i;
192 
193 	if (!atomic_read(&trig->use_count)) {
194 		atomic_set(&trig->use_count, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
195 
196 		for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
197 			if (trig->subirqs[i].enabled)
198 				handle_nested_irq(trig->subirq_base + i);
199 			else
200 				iio_trigger_notify_done(trig);
201 		}
202 	}
203 }
204 EXPORT_SYMBOL(iio_trigger_poll_chained);
205 
206 void iio_trigger_notify_done(struct iio_trigger *trig)
207 {
208 	if (atomic_dec_and_test(&trig->use_count) && trig->ops &&
209 	    trig->ops->reenable)
210 		trig->ops->reenable(trig);
211 }
212 EXPORT_SYMBOL(iio_trigger_notify_done);
213 
214 /* Trigger Consumer related functions */
215 static int iio_trigger_get_irq(struct iio_trigger *trig)
216 {
217 	int ret;
218 
219 	mutex_lock(&trig->pool_lock);
220 	ret = bitmap_find_free_region(trig->pool,
221 				      CONFIG_IIO_CONSUMERS_PER_TRIGGER,
222 				      ilog2(1));
223 	mutex_unlock(&trig->pool_lock);
224 	if (ret >= 0)
225 		ret += trig->subirq_base;
226 
227 	return ret;
228 }
229 
230 static void iio_trigger_put_irq(struct iio_trigger *trig, int irq)
231 {
232 	mutex_lock(&trig->pool_lock);
233 	clear_bit(irq - trig->subirq_base, trig->pool);
234 	mutex_unlock(&trig->pool_lock);
235 }
236 
237 /* Complexity in here.  With certain triggers (datardy) an acknowledgement
238  * may be needed if the pollfuncs do not include the data read for the
239  * triggering device.
240  * This is not currently handled.  Alternative of not enabling trigger unless
241  * the relevant function is in there may be the best option.
242  */
243 /* Worth protecting against double additions? */
244 int iio_trigger_attach_poll_func(struct iio_trigger *trig,
245 				 struct iio_poll_func *pf)
246 {
247 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(pf->indio_dev);
248 	bool notinuse =
249 		bitmap_empty(trig->pool, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
250 	int ret = 0;
251 
252 	/* Prevent the module from being removed whilst attached to a trigger */
253 	__module_get(iio_dev_opaque->driver_module);
254 
255 	/* Get irq number */
256 	pf->irq = iio_trigger_get_irq(trig);
257 	if (pf->irq < 0) {
258 		pr_err("Could not find an available irq for trigger %s, CONFIG_IIO_CONSUMERS_PER_TRIGGER=%d limit might be exceeded\n",
259 			trig->name, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
260 		goto out_put_module;
261 	}
262 
263 	/* Request irq */
264 	ret = request_threaded_irq(pf->irq, pf->h, pf->thread,
265 				   pf->type, pf->name,
266 				   pf);
267 	if (ret < 0)
268 		goto out_put_irq;
269 
270 	/* Enable trigger in driver */
271 	if (trig->ops && trig->ops->set_trigger_state && notinuse) {
272 		ret = trig->ops->set_trigger_state(trig, true);
273 		if (ret < 0)
274 			goto out_free_irq;
275 	}
276 
277 	/*
278 	 * Check if we just registered to our own trigger: we determine that
279 	 * this is the case if the IIO device and the trigger device share the
280 	 * same parent device.
281 	 */
282 	if (pf->indio_dev->dev.parent == trig->dev.parent)
283 		trig->attached_own_device = true;
284 
285 	return ret;
286 
287 out_free_irq:
288 	free_irq(pf->irq, pf);
289 out_put_irq:
290 	iio_trigger_put_irq(trig, pf->irq);
291 out_put_module:
292 	module_put(iio_dev_opaque->driver_module);
293 	return ret;
294 }
295 
296 int iio_trigger_detach_poll_func(struct iio_trigger *trig,
297 				 struct iio_poll_func *pf)
298 {
299 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(pf->indio_dev);
300 	bool no_other_users =
301 		bitmap_weight(trig->pool, CONFIG_IIO_CONSUMERS_PER_TRIGGER) == 1;
302 	int ret = 0;
303 
304 	if (trig->ops && trig->ops->set_trigger_state && no_other_users) {
305 		ret = trig->ops->set_trigger_state(trig, false);
306 		if (ret)
307 			return ret;
308 	}
309 	if (pf->indio_dev->dev.parent == trig->dev.parent)
310 		trig->attached_own_device = false;
311 	iio_trigger_put_irq(trig, pf->irq);
312 	free_irq(pf->irq, pf);
313 	module_put(iio_dev_opaque->driver_module);
314 
315 	return ret;
316 }
317 
318 irqreturn_t iio_pollfunc_store_time(int irq, void *p)
319 {
320 	struct iio_poll_func *pf = p;
321 
322 	pf->timestamp = iio_get_time_ns(pf->indio_dev);
323 	return IRQ_WAKE_THREAD;
324 }
325 EXPORT_SYMBOL(iio_pollfunc_store_time);
326 
327 struct iio_poll_func
328 *iio_alloc_pollfunc(irqreturn_t (*h)(int irq, void *p),
329 		    irqreturn_t (*thread)(int irq, void *p),
330 		    int type,
331 		    struct iio_dev *indio_dev,
332 		    const char *fmt,
333 		    ...)
334 {
335 	va_list vargs;
336 	struct iio_poll_func *pf;
337 
338 	pf = kmalloc(sizeof *pf, GFP_KERNEL);
339 	if (pf == NULL)
340 		return NULL;
341 	va_start(vargs, fmt);
342 	pf->name = kvasprintf(GFP_KERNEL, fmt, vargs);
343 	va_end(vargs);
344 	if (pf->name == NULL) {
345 		kfree(pf);
346 		return NULL;
347 	}
348 	pf->h = h;
349 	pf->thread = thread;
350 	pf->type = type;
351 	pf->indio_dev = indio_dev;
352 
353 	return pf;
354 }
355 EXPORT_SYMBOL_GPL(iio_alloc_pollfunc);
356 
357 void iio_dealloc_pollfunc(struct iio_poll_func *pf)
358 {
359 	kfree(pf->name);
360 	kfree(pf);
361 }
362 EXPORT_SYMBOL_GPL(iio_dealloc_pollfunc);
363 
364 /**
365  * iio_trigger_read_current() - trigger consumer sysfs query current trigger
366  * @dev:	device associated with an industrial I/O device
367  * @attr:	pointer to the device_attribute structure that
368  *		is being processed
369  * @buf:	buffer where the current trigger name will be printed into
370  *
371  * For trigger consumers the current_trigger interface allows the trigger
372  * used by the device to be queried.
373  *
374  * Return: a negative number on failure, the number of characters written
375  *	   on success or 0 if no trigger is available
376  */
377 static ssize_t iio_trigger_read_current(struct device *dev,
378 					struct device_attribute *attr,
379 					char *buf)
380 {
381 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
382 
383 	if (indio_dev->trig)
384 		return sysfs_emit(buf, "%s\n", indio_dev->trig->name);
385 	return 0;
386 }
387 
388 /**
389  * iio_trigger_write_current() - trigger consumer sysfs set current trigger
390  * @dev:	device associated with an industrial I/O device
391  * @attr:	device attribute that is being processed
392  * @buf:	string buffer that holds the name of the trigger
393  * @len:	length of the trigger name held by buf
394  *
395  * For trigger consumers the current_trigger interface allows the trigger
396  * used for this device to be specified at run time based on the trigger's
397  * name.
398  *
399  * Return: negative error code on failure or length of the buffer
400  *	   on success
401  */
402 static ssize_t iio_trigger_write_current(struct device *dev,
403 					 struct device_attribute *attr,
404 					 const char *buf,
405 					 size_t len)
406 {
407 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
408 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
409 	struct iio_trigger *oldtrig = indio_dev->trig;
410 	struct iio_trigger *trig;
411 	int ret;
412 
413 	mutex_lock(&indio_dev->mlock);
414 	if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
415 		mutex_unlock(&indio_dev->mlock);
416 		return -EBUSY;
417 	}
418 	if (iio_dev_opaque->trig_readonly) {
419 		mutex_unlock(&indio_dev->mlock);
420 		return -EPERM;
421 	}
422 	mutex_unlock(&indio_dev->mlock);
423 
424 	trig = iio_trigger_acquire_by_name(buf);
425 	if (oldtrig == trig) {
426 		ret = len;
427 		goto out_trigger_put;
428 	}
429 
430 	if (trig && indio_dev->info->validate_trigger) {
431 		ret = indio_dev->info->validate_trigger(indio_dev, trig);
432 		if (ret)
433 			goto out_trigger_put;
434 	}
435 
436 	if (trig && trig->ops && trig->ops->validate_device) {
437 		ret = trig->ops->validate_device(trig, indio_dev);
438 		if (ret)
439 			goto out_trigger_put;
440 	}
441 
442 	indio_dev->trig = trig;
443 
444 	if (oldtrig) {
445 		if (indio_dev->modes & INDIO_EVENT_TRIGGERED)
446 			iio_trigger_detach_poll_func(oldtrig,
447 						     indio_dev->pollfunc_event);
448 		iio_trigger_put(oldtrig);
449 	}
450 	if (indio_dev->trig) {
451 		if (indio_dev->modes & INDIO_EVENT_TRIGGERED)
452 			iio_trigger_attach_poll_func(indio_dev->trig,
453 						     indio_dev->pollfunc_event);
454 	}
455 
456 	return len;
457 
458 out_trigger_put:
459 	if (trig)
460 		iio_trigger_put(trig);
461 	return ret;
462 }
463 
464 static DEVICE_ATTR(current_trigger, S_IRUGO | S_IWUSR,
465 		   iio_trigger_read_current,
466 		   iio_trigger_write_current);
467 
468 static struct attribute *iio_trigger_consumer_attrs[] = {
469 	&dev_attr_current_trigger.attr,
470 	NULL,
471 };
472 
473 static const struct attribute_group iio_trigger_consumer_attr_group = {
474 	.name = "trigger",
475 	.attrs = iio_trigger_consumer_attrs,
476 };
477 
478 static void iio_trig_release(struct device *device)
479 {
480 	struct iio_trigger *trig = to_iio_trigger(device);
481 	int i;
482 
483 	if (trig->subirq_base) {
484 		for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
485 			irq_modify_status(trig->subirq_base + i,
486 					  IRQ_NOAUTOEN,
487 					  IRQ_NOREQUEST | IRQ_NOPROBE);
488 			irq_set_chip(trig->subirq_base + i,
489 				     NULL);
490 			irq_set_handler(trig->subirq_base + i,
491 					NULL);
492 		}
493 
494 		irq_free_descs(trig->subirq_base,
495 			       CONFIG_IIO_CONSUMERS_PER_TRIGGER);
496 	}
497 	kfree(trig->name);
498 	kfree(trig);
499 }
500 
501 static const struct device_type iio_trig_type = {
502 	.release = iio_trig_release,
503 	.groups = iio_trig_dev_groups,
504 };
505 
506 static void iio_trig_subirqmask(struct irq_data *d)
507 {
508 	struct irq_chip *chip = irq_data_get_irq_chip(d);
509 	struct iio_trigger *trig = container_of(chip, struct iio_trigger, subirq_chip);
510 
511 	trig->subirqs[d->irq - trig->subirq_base].enabled = false;
512 }
513 
514 static void iio_trig_subirqunmask(struct irq_data *d)
515 {
516 	struct irq_chip *chip = irq_data_get_irq_chip(d);
517 	struct iio_trigger *trig = container_of(chip, struct iio_trigger, subirq_chip);
518 
519 	trig->subirqs[d->irq - trig->subirq_base].enabled = true;
520 }
521 
522 static __printf(2, 0)
523 struct iio_trigger *viio_trigger_alloc(struct device *parent,
524 				       const char *fmt,
525 				       va_list vargs)
526 {
527 	struct iio_trigger *trig;
528 	int i;
529 
530 	trig = kzalloc(sizeof *trig, GFP_KERNEL);
531 	if (!trig)
532 		return NULL;
533 
534 	trig->dev.parent = parent;
535 	trig->dev.type = &iio_trig_type;
536 	trig->dev.bus = &iio_bus_type;
537 	device_initialize(&trig->dev);
538 
539 	mutex_init(&trig->pool_lock);
540 	trig->subirq_base = irq_alloc_descs(-1, 0,
541 					    CONFIG_IIO_CONSUMERS_PER_TRIGGER,
542 					    0);
543 	if (trig->subirq_base < 0)
544 		goto free_trig;
545 
546 	trig->name = kvasprintf(GFP_KERNEL, fmt, vargs);
547 	if (trig->name == NULL)
548 		goto free_descs;
549 
550 	trig->subirq_chip.name = trig->name;
551 	trig->subirq_chip.irq_mask = &iio_trig_subirqmask;
552 	trig->subirq_chip.irq_unmask = &iio_trig_subirqunmask;
553 	for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
554 		irq_set_chip(trig->subirq_base + i, &trig->subirq_chip);
555 		irq_set_handler(trig->subirq_base + i, &handle_simple_irq);
556 		irq_modify_status(trig->subirq_base + i,
557 				  IRQ_NOREQUEST | IRQ_NOAUTOEN, IRQ_NOPROBE);
558 	}
559 	get_device(&trig->dev);
560 
561 	return trig;
562 
563 free_descs:
564 	irq_free_descs(trig->subirq_base, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
565 free_trig:
566 	kfree(trig);
567 	return NULL;
568 }
569 
570 /**
571  * iio_trigger_alloc - Allocate a trigger
572  * @parent:		Device to allocate iio_trigger for
573  * @fmt:		trigger name format. If it includes format
574  *			specifiers, the additional arguments following
575  *			format are formatted and inserted in the resulting
576  *			string replacing their respective specifiers.
577  * RETURNS:
578  * Pointer to allocated iio_trigger on success, NULL on failure.
579  */
580 struct iio_trigger *iio_trigger_alloc(struct device *parent, const char *fmt, ...)
581 {
582 	struct iio_trigger *trig;
583 	va_list vargs;
584 
585 	va_start(vargs, fmt);
586 	trig = viio_trigger_alloc(parent, fmt, vargs);
587 	va_end(vargs);
588 
589 	return trig;
590 }
591 EXPORT_SYMBOL(iio_trigger_alloc);
592 
593 void iio_trigger_free(struct iio_trigger *trig)
594 {
595 	if (trig)
596 		put_device(&trig->dev);
597 }
598 EXPORT_SYMBOL(iio_trigger_free);
599 
600 static void devm_iio_trigger_release(struct device *dev, void *res)
601 {
602 	iio_trigger_free(*(struct iio_trigger **)res);
603 }
604 
605 /**
606  * devm_iio_trigger_alloc - Resource-managed iio_trigger_alloc()
607  * Managed iio_trigger_alloc.  iio_trigger allocated with this function is
608  * automatically freed on driver detach.
609  * @parent:		Device to allocate iio_trigger for
610  * @fmt:		trigger name format. If it includes format
611  *			specifiers, the additional arguments following
612  *			format are formatted and inserted in the resulting
613  *			string replacing their respective specifiers.
614  *
615  *
616  * RETURNS:
617  * Pointer to allocated iio_trigger on success, NULL on failure.
618  */
619 struct iio_trigger *devm_iio_trigger_alloc(struct device *parent, const char *fmt, ...)
620 {
621 	struct iio_trigger **ptr, *trig;
622 	va_list vargs;
623 
624 	ptr = devres_alloc(devm_iio_trigger_release, sizeof(*ptr),
625 			   GFP_KERNEL);
626 	if (!ptr)
627 		return NULL;
628 
629 	/* use raw alloc_dr for kmalloc caller tracing */
630 	va_start(vargs, fmt);
631 	trig = viio_trigger_alloc(parent, fmt, vargs);
632 	va_end(vargs);
633 	if (trig) {
634 		*ptr = trig;
635 		devres_add(parent, ptr);
636 	} else {
637 		devres_free(ptr);
638 	}
639 
640 	return trig;
641 }
642 EXPORT_SYMBOL_GPL(devm_iio_trigger_alloc);
643 
644 static void devm_iio_trigger_unreg(void *trigger_info)
645 {
646 	iio_trigger_unregister(trigger_info);
647 }
648 
649 /**
650  * __devm_iio_trigger_register - Resource-managed iio_trigger_register()
651  * @dev:	device this trigger was allocated for
652  * @trig_info:	trigger to register
653  * @this_mod:   module registering the trigger
654  *
655  * Managed iio_trigger_register().  The IIO trigger registered with this
656  * function is automatically unregistered on driver detach. This function
657  * calls iio_trigger_register() internally. Refer to that function for more
658  * information.
659  *
660  * RETURNS:
661  * 0 on success, negative error number on failure.
662  */
663 int __devm_iio_trigger_register(struct device *dev,
664 				struct iio_trigger *trig_info,
665 				struct module *this_mod)
666 {
667 	int ret;
668 
669 	ret = __iio_trigger_register(trig_info, this_mod);
670 	if (ret)
671 		return ret;
672 
673 	return devm_add_action_or_reset(dev, devm_iio_trigger_unreg, trig_info);
674 }
675 EXPORT_SYMBOL_GPL(__devm_iio_trigger_register);
676 
677 bool iio_trigger_using_own(struct iio_dev *indio_dev)
678 {
679 	return indio_dev->trig->attached_own_device;
680 }
681 EXPORT_SYMBOL(iio_trigger_using_own);
682 
683 /**
684  * iio_trigger_validate_own_device - Check if a trigger and IIO device belong to
685  *  the same device
686  * @trig: The IIO trigger to check
687  * @indio_dev: the IIO device to check
688  *
689  * This function can be used as the validate_device callback for triggers that
690  * can only be attached to their own device.
691  *
692  * Return: 0 if both the trigger and the IIO device belong to the same
693  * device, -EINVAL otherwise.
694  */
695 int iio_trigger_validate_own_device(struct iio_trigger *trig,
696 				    struct iio_dev *indio_dev)
697 {
698 	if (indio_dev->dev.parent != trig->dev.parent)
699 		return -EINVAL;
700 	return 0;
701 }
702 EXPORT_SYMBOL(iio_trigger_validate_own_device);
703 
704 int iio_device_register_trigger_consumer(struct iio_dev *indio_dev)
705 {
706 	return iio_device_register_sysfs_group(indio_dev,
707 					       &iio_trigger_consumer_attr_group);
708 }
709 
710 void iio_device_unregister_trigger_consumer(struct iio_dev *indio_dev)
711 {
712 	/* Clean up an associated but not attached trigger reference */
713 	if (indio_dev->trig)
714 		iio_trigger_put(indio_dev->trig);
715 }
716