1/*
2 * STMicroelectronics sensors trigger library driver
3 *
4 * Copyright 2012-2013 STMicroelectronics Inc.
5 *
6 * Denis Ciocca <denis.ciocca@st.com>
7 *
8 * Licensed under the GPL-2.
9 */
10
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/slab.h>
14#include <linux/iio/iio.h>
15#include <linux/iio/trigger.h>
16#include <linux/interrupt.h>
17
18#include <linux/iio/common/st_sensors.h>
19
20
21int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
22				const struct iio_trigger_ops *trigger_ops)
23{
24	int err;
25	struct st_sensor_data *sdata = iio_priv(indio_dev);
26
27	sdata->trig = iio_trigger_alloc("%s-trigger", indio_dev->name);
28	if (sdata->trig == NULL) {
29		err = -ENOMEM;
30		dev_err(&indio_dev->dev, "failed to allocate iio trigger.\n");
31		goto iio_trigger_alloc_error;
32	}
33
34	err = request_threaded_irq(sdata->get_irq_data_ready(indio_dev),
35			iio_trigger_generic_data_rdy_poll,
36			NULL,
37			IRQF_TRIGGER_RISING,
38			sdata->trig->name,
39			sdata->trig);
40	if (err)
41		goto request_irq_error;
42
43	iio_trigger_set_drvdata(sdata->trig, indio_dev);
44	sdata->trig->ops = trigger_ops;
45	sdata->trig->dev.parent = sdata->dev;
46
47	err = iio_trigger_register(sdata->trig);
48	if (err < 0) {
49		dev_err(&indio_dev->dev, "failed to register iio trigger.\n");
50		goto iio_trigger_register_error;
51	}
52	indio_dev->trig = iio_trigger_get(sdata->trig);
53
54	return 0;
55
56iio_trigger_register_error:
57	free_irq(sdata->get_irq_data_ready(indio_dev), sdata->trig);
58request_irq_error:
59	iio_trigger_free(sdata->trig);
60iio_trigger_alloc_error:
61	return err;
62}
63EXPORT_SYMBOL(st_sensors_allocate_trigger);
64
65void st_sensors_deallocate_trigger(struct iio_dev *indio_dev)
66{
67	struct st_sensor_data *sdata = iio_priv(indio_dev);
68
69	iio_trigger_unregister(sdata->trig);
70	free_irq(sdata->get_irq_data_ready(indio_dev), sdata->trig);
71	iio_trigger_free(sdata->trig);
72}
73EXPORT_SYMBOL(st_sensors_deallocate_trigger);
74
75MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
76MODULE_DESCRIPTION("STMicroelectronics ST-sensors trigger");
77MODULE_LICENSE("GPL v2");
78