12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2735ad074SVladimir Barinov /*
3735ad074SVladimir Barinov  * Copyright (C) 2015 Cogent Embedded, Inc.
4735ad074SVladimir Barinov  */
5735ad074SVladimir Barinov 
6735ad074SVladimir Barinov #include <linux/kernel.h>
7735ad074SVladimir Barinov #include <linux/export.h>
8735ad074SVladimir Barinov #include <linux/module.h>
9735ad074SVladimir Barinov #include <linux/iio/iio.h>
10735ad074SVladimir Barinov #include <linux/iio/triggered_event.h>
11735ad074SVladimir Barinov #include <linux/iio/trigger_consumer.h>
12735ad074SVladimir Barinov 
13735ad074SVladimir Barinov /**
14735ad074SVladimir Barinov  * iio_triggered_event_setup() - Setup pollfunc_event for triggered event
15735ad074SVladimir Barinov  * @indio_dev:	IIO device structure
16735ad074SVladimir Barinov  * @h:		Function which will be used as pollfunc_event top half
17735ad074SVladimir Barinov  * @thread:	Function which will be used as pollfunc_event bottom half
18735ad074SVladimir Barinov  *
19735ad074SVladimir Barinov  * This function combines some common tasks which will normally be performed
20735ad074SVladimir Barinov  * when setting up a triggered event. It will allocate the pollfunc_event and
21735ad074SVladimir Barinov  * set mode to use it for triggered event.
22735ad074SVladimir Barinov  *
23735ad074SVladimir Barinov  * Before calling this function the indio_dev structure should already be
24735ad074SVladimir Barinov  * completely initialized, but not yet registered. In practice this means that
25735ad074SVladimir Barinov  * this function should be called right before iio_device_register().
26735ad074SVladimir Barinov  *
27735ad074SVladimir Barinov  * To free the resources allocated by this function call
28735ad074SVladimir Barinov  * iio_triggered_event_cleanup().
29735ad074SVladimir Barinov  */
iio_triggered_event_setup(struct iio_dev * indio_dev,irqreturn_t (* h)(int irq,void * p),irqreturn_t (* thread)(int irq,void * p))30735ad074SVladimir Barinov int iio_triggered_event_setup(struct iio_dev *indio_dev,
31735ad074SVladimir Barinov 			      irqreturn_t (*h)(int irq, void *p),
32735ad074SVladimir Barinov 			      irqreturn_t (*thread)(int irq, void *p))
33735ad074SVladimir Barinov {
34735ad074SVladimir Barinov 	indio_dev->pollfunc_event = iio_alloc_pollfunc(h,
35735ad074SVladimir Barinov 						       thread,
36735ad074SVladimir Barinov 						       IRQF_ONESHOT,
37735ad074SVladimir Barinov 						       indio_dev,
38735ad074SVladimir Barinov 						       "%s_consumer%d",
39735ad074SVladimir Barinov 						       indio_dev->name,
40*15ea2878SJonathan Cameron 						       iio_device_id(indio_dev));
41735ad074SVladimir Barinov 	if (indio_dev->pollfunc_event == NULL)
42735ad074SVladimir Barinov 		return -ENOMEM;
43735ad074SVladimir Barinov 
44735ad074SVladimir Barinov 	/* Flag that events polling is possible */
45735ad074SVladimir Barinov 	indio_dev->modes |= INDIO_EVENT_TRIGGERED;
46735ad074SVladimir Barinov 
47735ad074SVladimir Barinov 	return 0;
48735ad074SVladimir Barinov }
49735ad074SVladimir Barinov EXPORT_SYMBOL(iio_triggered_event_setup);
50735ad074SVladimir Barinov 
51735ad074SVladimir Barinov /**
52735ad074SVladimir Barinov  * iio_triggered_event_cleanup() - Free resources allocated by iio_triggered_event_setup()
53735ad074SVladimir Barinov  * @indio_dev: IIO device structure
54735ad074SVladimir Barinov  */
iio_triggered_event_cleanup(struct iio_dev * indio_dev)55735ad074SVladimir Barinov void iio_triggered_event_cleanup(struct iio_dev *indio_dev)
56735ad074SVladimir Barinov {
57735ad074SVladimir Barinov 	indio_dev->modes &= ~INDIO_EVENT_TRIGGERED;
58735ad074SVladimir Barinov 	iio_dealloc_pollfunc(indio_dev->pollfunc_event);
59735ad074SVladimir Barinov }
60735ad074SVladimir Barinov EXPORT_SYMBOL(iio_triggered_event_cleanup);
61735ad074SVladimir Barinov 
62735ad074SVladimir Barinov MODULE_AUTHOR("Vladimir Barinov");
63735ad074SVladimir Barinov MODULE_DESCRIPTION("IIO helper functions for setting up triggered events");
64735ad074SVladimir Barinov MODULE_LICENSE("GPL");
65