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