xref: /linux/drivers/iio/accel/st_accel_buffer.c (revision 75b1a8f9d62e50f05d0e4e9f3c8bcde32527ffc1)
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/slab.h>
13 #include <linux/stat.h>
14 #include <linux/interrupt.h>
15 #include <linux/i2c.h>
16 #include <linux/delay.h>
17 #include <linux/iio/iio.h>
18 #include <linux/iio/buffer.h>
19 #include <linux/iio/trigger_consumer.h>
20 #include <linux/iio/triggered_buffer.h>
21 
22 #include <linux/iio/common/st_sensors.h>
23 #include "st_accel.h"
24 
25 int st_accel_trig_set_state(struct iio_trigger *trig, bool state)
26 {
27 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
28 
29 	return st_sensors_set_dataready_irq(indio_dev, state);
30 }
31 
32 static int st_accel_buffer_postenable(struct iio_dev *indio_dev)
33 {
34 	int err;
35 
36 	err = st_sensors_set_axis_enable(indio_dev, indio_dev->active_scan_mask[0]);
37 	if (err < 0)
38 		return err;
39 
40 	err = st_sensors_set_enable(indio_dev, true);
41 	if (err < 0)
42 		goto st_accel_buffer_enable_all_axis;
43 
44 	return 0;
45 
46 st_accel_buffer_enable_all_axis:
47 	st_sensors_set_axis_enable(indio_dev, ST_SENSORS_ENABLE_ALL_AXIS);
48 	return err;
49 }
50 
51 static int st_accel_buffer_predisable(struct iio_dev *indio_dev)
52 {
53 	int err;
54 
55 	err = st_sensors_set_enable(indio_dev, false);
56 	if (err < 0)
57 		return err;
58 
59 	return st_sensors_set_axis_enable(indio_dev,
60 					  ST_SENSORS_ENABLE_ALL_AXIS);
61 }
62 
63 static const struct iio_buffer_setup_ops st_accel_buffer_setup_ops = {
64 	.postenable = &st_accel_buffer_postenable,
65 	.predisable = &st_accel_buffer_predisable,
66 };
67 
68 int st_accel_allocate_ring(struct iio_dev *indio_dev)
69 {
70 	return iio_triggered_buffer_setup(indio_dev, NULL,
71 		&st_sensors_trigger_handler, &st_accel_buffer_setup_ops);
72 }
73 
74 void st_accel_deallocate_ring(struct iio_dev *indio_dev)
75 {
76 	iio_triggered_buffer_cleanup(indio_dev);
77 }
78 
79 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
80 MODULE_DESCRIPTION("STMicroelectronics accelerometers buffer");
81 MODULE_LICENSE("GPL v2");
82