xref: /linux/drivers/iio/accel/st_accel_buffer.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1fda8d26eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2d6251168SDenis Ciocca /*
3d6251168SDenis Ciocca  * STMicroelectronics accelerometers driver
4d6251168SDenis Ciocca  *
5d6251168SDenis Ciocca  * Copyright 2012-2013 STMicroelectronics Inc.
6d6251168SDenis Ciocca  *
7d6251168SDenis Ciocca  * Denis Ciocca <denis.ciocca@st.com>
8d6251168SDenis Ciocca  */
9d6251168SDenis Ciocca 
10d6251168SDenis Ciocca #include <linux/kernel.h>
11d6251168SDenis Ciocca #include <linux/iio/iio.h>
12d6251168SDenis Ciocca #include <linux/iio/buffer.h>
139c6cd755SJonathan Cameron #include <linux/iio/trigger.h>
14d6251168SDenis Ciocca #include <linux/iio/triggered_buffer.h>
15d6251168SDenis Ciocca 
16d6251168SDenis Ciocca #include <linux/iio/common/st_sensors.h>
17d6251168SDenis Ciocca #include "st_accel.h"
18d6251168SDenis Ciocca 
st_accel_trig_set_state(struct iio_trigger * trig,bool state)19d6251168SDenis Ciocca int st_accel_trig_set_state(struct iio_trigger *trig, bool state)
20d6251168SDenis Ciocca {
211e9663c6SLars-Peter Clausen 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
22d6251168SDenis Ciocca 
23d6251168SDenis Ciocca 	return st_sensors_set_dataready_irq(indio_dev, state);
24d6251168SDenis Ciocca }
25d6251168SDenis Ciocca 
st_accel_buffer_postenable(struct iio_dev * indio_dev)26d6251168SDenis Ciocca static int st_accel_buffer_postenable(struct iio_dev *indio_dev)
27d6251168SDenis Ciocca {
28d4e85198SDenis Ciocca 	int err;
29d6251168SDenis Ciocca 
301f38527dSAndy Shevchenko 	err = st_sensors_set_axis_enable(indio_dev, indio_dev->active_scan_mask[0]);
3105b8bcc9SAlexandru Ardelean 	if (err < 0)
32f11d59d8SLars-Peter Clausen 		return err;
3305b8bcc9SAlexandru Ardelean 
34d4e85198SDenis Ciocca 	err = st_sensors_set_enable(indio_dev, true);
35d4e85198SDenis Ciocca 	if (err < 0)
36d4e85198SDenis Ciocca 		goto st_accel_buffer_enable_all_axis;
37d6251168SDenis Ciocca 
38d4e85198SDenis Ciocca 	return 0;
39d4e85198SDenis Ciocca 
40d4e85198SDenis Ciocca st_accel_buffer_enable_all_axis:
41d4e85198SDenis Ciocca 	st_sensors_set_axis_enable(indio_dev, ST_SENSORS_ENABLE_ALL_AXIS);
42d6251168SDenis Ciocca 	return err;
43d6251168SDenis Ciocca }
44d6251168SDenis Ciocca 
st_accel_buffer_predisable(struct iio_dev * indio_dev)45d6251168SDenis Ciocca static int st_accel_buffer_predisable(struct iio_dev *indio_dev)
46d6251168SDenis Ciocca {
47f11d59d8SLars-Peter Clausen 	int err;
48d6251168SDenis Ciocca 
49d6251168SDenis Ciocca 	err = st_sensors_set_enable(indio_dev, false);
5005b8bcc9SAlexandru Ardelean 	if (err < 0)
51d6251168SDenis Ciocca 		return err;
52f11d59d8SLars-Peter Clausen 
53f11d59d8SLars-Peter Clausen 	return st_sensors_set_axis_enable(indio_dev,
54f11d59d8SLars-Peter Clausen 					  ST_SENSORS_ENABLE_ALL_AXIS);
55d6251168SDenis Ciocca }
56d6251168SDenis Ciocca 
57d6251168SDenis Ciocca static const struct iio_buffer_setup_ops st_accel_buffer_setup_ops = {
58d6251168SDenis Ciocca 	.postenable = &st_accel_buffer_postenable,
59d6251168SDenis Ciocca 	.predisable = &st_accel_buffer_predisable,
60d6251168SDenis Ciocca };
61d6251168SDenis Ciocca 
st_accel_allocate_ring(struct iio_dev * indio_dev)62d6251168SDenis Ciocca int st_accel_allocate_ring(struct iio_dev *indio_dev)
63d6251168SDenis Ciocca {
64*a442673bSAlexandru Ardelean 	return devm_iio_triggered_buffer_setup(indio_dev->dev.parent, indio_dev,
65*a442673bSAlexandru Ardelean 		NULL, &st_sensors_trigger_handler, &st_accel_buffer_setup_ops);
66d6251168SDenis Ciocca }
67