18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2015 Cogent Embedded, Inc.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/kernel.h>
78c2ecf20Sopenharmony_ci#include <linux/export.h>
88c2ecf20Sopenharmony_ci#include <linux/module.h>
98c2ecf20Sopenharmony_ci#include <linux/iio/iio.h>
108c2ecf20Sopenharmony_ci#include <linux/iio/triggered_event.h>
118c2ecf20Sopenharmony_ci#include <linux/iio/trigger_consumer.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci/**
148c2ecf20Sopenharmony_ci * iio_triggered_event_setup() - Setup pollfunc_event for triggered event
158c2ecf20Sopenharmony_ci * @indio_dev:	IIO device structure
168c2ecf20Sopenharmony_ci * @h:		Function which will be used as pollfunc_event top half
178c2ecf20Sopenharmony_ci * @thread:	Function which will be used as pollfunc_event bottom half
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci * This function combines some common tasks which will normally be performed
208c2ecf20Sopenharmony_ci * when setting up a triggered event. It will allocate the pollfunc_event and
218c2ecf20Sopenharmony_ci * set mode to use it for triggered event.
228c2ecf20Sopenharmony_ci *
238c2ecf20Sopenharmony_ci * Before calling this function the indio_dev structure should already be
248c2ecf20Sopenharmony_ci * completely initialized, but not yet registered. In practice this means that
258c2ecf20Sopenharmony_ci * this function should be called right before iio_device_register().
268c2ecf20Sopenharmony_ci *
278c2ecf20Sopenharmony_ci * To free the resources allocated by this function call
288c2ecf20Sopenharmony_ci * iio_triggered_event_cleanup().
298c2ecf20Sopenharmony_ci */
308c2ecf20Sopenharmony_ciint iio_triggered_event_setup(struct iio_dev *indio_dev,
318c2ecf20Sopenharmony_ci			      irqreturn_t (*h)(int irq, void *p),
328c2ecf20Sopenharmony_ci			      irqreturn_t (*thread)(int irq, void *p))
338c2ecf20Sopenharmony_ci{
348c2ecf20Sopenharmony_ci	indio_dev->pollfunc_event = iio_alloc_pollfunc(h,
358c2ecf20Sopenharmony_ci						       thread,
368c2ecf20Sopenharmony_ci						       IRQF_ONESHOT,
378c2ecf20Sopenharmony_ci						       indio_dev,
388c2ecf20Sopenharmony_ci						       "%s_consumer%d",
398c2ecf20Sopenharmony_ci						       indio_dev->name,
408c2ecf20Sopenharmony_ci						       indio_dev->id);
418c2ecf20Sopenharmony_ci	if (indio_dev->pollfunc_event == NULL)
428c2ecf20Sopenharmony_ci		return -ENOMEM;
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	/* Flag that events polling is possible */
458c2ecf20Sopenharmony_ci	indio_dev->modes |= INDIO_EVENT_TRIGGERED;
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	return 0;
488c2ecf20Sopenharmony_ci}
498c2ecf20Sopenharmony_ciEXPORT_SYMBOL(iio_triggered_event_setup);
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci/**
528c2ecf20Sopenharmony_ci * iio_triggered_event_cleanup() - Free resources allocated by iio_triggered_event_setup()
538c2ecf20Sopenharmony_ci * @indio_dev: IIO device structure
548c2ecf20Sopenharmony_ci */
558c2ecf20Sopenharmony_civoid iio_triggered_event_cleanup(struct iio_dev *indio_dev)
568c2ecf20Sopenharmony_ci{
578c2ecf20Sopenharmony_ci	indio_dev->modes &= ~INDIO_EVENT_TRIGGERED;
588c2ecf20Sopenharmony_ci	iio_dealloc_pollfunc(indio_dev->pollfunc_event);
598c2ecf20Sopenharmony_ci}
608c2ecf20Sopenharmony_ciEXPORT_SYMBOL(iio_triggered_event_cleanup);
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ciMODULE_AUTHOR("Vladimir Barinov");
638c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("IIO helper functions for setting up triggered events");
648c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
65