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		dev_err(&indio_dev->dev, "failed to request trigger IRQ.\n");
42		goto request_irq_error;
43	}
44
45	iio_trigger_set_drvdata(sdata->trig, indio_dev);
46	sdata->trig->ops = trigger_ops;
47	sdata->trig->dev.parent = sdata->dev;
48
49	err = iio_trigger_register(sdata->trig);
50	if (err < 0) {
51		dev_err(&indio_dev->dev, "failed to register iio trigger.\n");
52		goto iio_trigger_register_error;
53	}
54	indio_dev->trig = iio_trigger_get(sdata->trig);
55
56	return 0;
57
58iio_trigger_register_error:
59	free_irq(sdata->get_irq_data_ready(indio_dev), sdata->trig);
60request_irq_error:
61	iio_trigger_free(sdata->trig);
62iio_trigger_alloc_error:
63	return err;
64}
65EXPORT_SYMBOL(st_sensors_allocate_trigger);
66
67void st_sensors_deallocate_trigger(struct iio_dev *indio_dev)
68{
69	struct st_sensor_data *sdata = iio_priv(indio_dev);
70
71	iio_trigger_unregister(sdata->trig);
72	free_irq(sdata->get_irq_data_ready(indio_dev), sdata->trig);
73	iio_trigger_free(sdata->trig);
74}
75EXPORT_SYMBOL(st_sensors_deallocate_trigger);
76
77MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
78MODULE_DESCRIPTION("STMicroelectronics ST-sensors trigger");
79MODULE_LICENSE("GPL v2");
80