1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * STMicroelectronics accelerometers driver
4  *
5  * Copyright 2012-2013 STMicroelectronics Inc.
6  *
7  * Denis Ciocca <denis.ciocca@st.com>
8  */
9 
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/iio/iio.h>
13 #include <linux/iio/buffer.h>
14 #include <linux/iio/trigger.h>
15 #include <linux/iio/triggered_buffer.h>
16 
17 #include <linux/iio/common/st_sensors.h>
18 #include "st_accel.h"
19 
20 int st_accel_trig_set_state(struct iio_trigger *trig, bool state)
21 {
22 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
23 
24 	return st_sensors_set_dataready_irq(indio_dev, state);
25 }
26 
27 static int st_accel_buffer_postenable(struct iio_dev *indio_dev)
28 {
29 	int err;
30 
31 	err = st_sensors_set_axis_enable(indio_dev, indio_dev->active_scan_mask[0]);
32 	if (err < 0)
33 		return err;
34 
35 	err = st_sensors_set_enable(indio_dev, true);
36 	if (err < 0)
37 		goto st_accel_buffer_enable_all_axis;
38 
39 	return 0;
40 
41 st_accel_buffer_enable_all_axis:
42 	st_sensors_set_axis_enable(indio_dev, ST_SENSORS_ENABLE_ALL_AXIS);
43 	return err;
44 }
45 
46 static int st_accel_buffer_predisable(struct iio_dev *indio_dev)
47 {
48 	int err;
49 
50 	err = st_sensors_set_enable(indio_dev, false);
51 	if (err < 0)
52 		return err;
53 
54 	return st_sensors_set_axis_enable(indio_dev,
55 					  ST_SENSORS_ENABLE_ALL_AXIS);
56 }
57 
58 static const struct iio_buffer_setup_ops st_accel_buffer_setup_ops = {
59 	.postenable = &st_accel_buffer_postenable,
60 	.predisable = &st_accel_buffer_predisable,
61 };
62 
63 int st_accel_allocate_ring(struct iio_dev *indio_dev)
64 {
65 	return devm_iio_triggered_buffer_setup(indio_dev->dev.parent, indio_dev,
66 		NULL, &st_sensors_trigger_handler, &st_accel_buffer_setup_ops);
67 }
68 
69 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
70 MODULE_DESCRIPTION("STMicroelectronics accelerometers buffer");
71 MODULE_LICENSE("GPL v2");
72