2 * STMicroelectronics accelerometers driver
4 * Copyright 2012-2013 STMicroelectronics Inc.
6 * Denis Ciocca <denis.ciocca@st.com>
8 * Licensed under the GPL-2.
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/stat.h>
15 #include <linux/interrupt.h>
16 #include <linux/i2c.h>
17 #include <linux/delay.h>
18 #include <linux/iio/iio.h>
19 #include <linux/iio/buffer.h>
20 #include <linux/iio/trigger_consumer.h>
21 #include <linux/iio/triggered_buffer.h>
23 #include <linux/iio/common/st_sensors.h>
26 int st_accel_trig_set_state(struct iio_trigger
*trig
, bool state
)
28 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
30 return st_sensors_set_dataready_irq(indio_dev
, state
);
33 static int st_accel_buffer_preenable(struct iio_dev
*indio_dev
)
37 err
= st_sensors_set_enable(indio_dev
, true);
39 goto st_accel_set_enable_error
;
41 err
= iio_sw_buffer_preenable(indio_dev
);
43 st_accel_set_enable_error
:
47 static int st_accel_buffer_postenable(struct iio_dev
*indio_dev
)
50 struct st_sensor_data
*adata
= iio_priv(indio_dev
);
52 adata
->buffer_data
= kmalloc(indio_dev
->scan_bytes
, GFP_KERNEL
);
53 if (adata
->buffer_data
== NULL
) {
55 goto allocate_memory_error
;
58 err
= st_sensors_set_axis_enable(indio_dev
,
59 (u8
)indio_dev
->active_scan_mask
[0]);
61 goto st_accel_buffer_postenable_error
;
63 err
= iio_triggered_buffer_postenable(indio_dev
);
65 goto st_accel_buffer_postenable_error
;
69 st_accel_buffer_postenable_error
:
70 kfree(adata
->buffer_data
);
71 allocate_memory_error
:
75 static int st_accel_buffer_predisable(struct iio_dev
*indio_dev
)
78 struct st_sensor_data
*adata
= iio_priv(indio_dev
);
80 err
= iio_triggered_buffer_predisable(indio_dev
);
82 goto st_accel_buffer_predisable_error
;
84 err
= st_sensors_set_axis_enable(indio_dev
, ST_SENSORS_ENABLE_ALL_AXIS
);
86 goto st_accel_buffer_predisable_error
;
88 err
= st_sensors_set_enable(indio_dev
, false);
90 st_accel_buffer_predisable_error
:
91 kfree(adata
->buffer_data
);
95 static const struct iio_buffer_setup_ops st_accel_buffer_setup_ops
= {
96 .preenable
= &st_accel_buffer_preenable
,
97 .postenable
= &st_accel_buffer_postenable
,
98 .predisable
= &st_accel_buffer_predisable
,
101 int st_accel_allocate_ring(struct iio_dev
*indio_dev
)
103 return iio_triggered_buffer_setup(indio_dev
, &iio_pollfunc_store_time
,
104 &st_sensors_trigger_handler
, &st_accel_buffer_setup_ops
);
107 void st_accel_deallocate_ring(struct iio_dev
*indio_dev
)
109 iio_triggered_buffer_cleanup(indio_dev
);
112 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
113 MODULE_DESCRIPTION("STMicroelectronics accelerometers buffer");
114 MODULE_LICENSE("GPL v2");