aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/iio/magnetometer/st_magn_buffer.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/iio/magnetometer/st_magn_buffer.c')
-rw-r--r--drivers/iio/magnetometer/st_magn_buffer.c98
1 files changed, 98 insertions, 0 deletions
diff --git a/drivers/iio/magnetometer/st_magn_buffer.c b/drivers/iio/magnetometer/st_magn_buffer.c
new file mode 100644
index 000000000000..708857bdb47d
--- /dev/null
+++ b/drivers/iio/magnetometer/st_magn_buffer.c
@@ -0,0 +1,98 @@
1/*
2 * STMicroelectronics magnetometers driver
3 *
4 * Copyright 2012-2013 STMicroelectronics Inc.
5 *
6 * Denis Ciocca <denis.ciocca@st.com>
7 *
8 * Licensed under the GPL-2.
9 */
10
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>
22
23#include <linux/iio/common/st_sensors.h>
24#include "st_magn.h"
25
26static int st_magn_buffer_preenable(struct iio_dev *indio_dev)
27{
28 int err;
29
30 err = st_sensors_set_enable(indio_dev, true);
31 if (err < 0)
32 goto st_magn_set_enable_error;
33
34 err = iio_sw_buffer_preenable(indio_dev);
35
36st_magn_set_enable_error:
37 return err;
38}
39
40static int st_magn_buffer_postenable(struct iio_dev *indio_dev)
41{
42 int err;
43 struct st_sensor_data *mdata = iio_priv(indio_dev);
44
45 mdata->buffer_data = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
46 if (mdata->buffer_data == NULL) {
47 err = -ENOMEM;
48 goto allocate_memory_error;
49 }
50
51 err = iio_triggered_buffer_postenable(indio_dev);
52 if (err < 0)
53 goto st_magn_buffer_postenable_error;
54
55 return err;
56
57st_magn_buffer_postenable_error:
58 kfree(mdata->buffer_data);
59allocate_memory_error:
60 return err;
61}
62
63static int st_magn_buffer_predisable(struct iio_dev *indio_dev)
64{
65 int err;
66 struct st_sensor_data *mdata = iio_priv(indio_dev);
67
68 err = iio_triggered_buffer_predisable(indio_dev);
69 if (err < 0)
70 goto st_magn_buffer_predisable_error;
71
72 err = st_sensors_set_enable(indio_dev, false);
73
74st_magn_buffer_predisable_error:
75 kfree(mdata->buffer_data);
76 return err;
77}
78
79static const struct iio_buffer_setup_ops st_magn_buffer_setup_ops = {
80 .preenable = &st_magn_buffer_preenable,
81 .postenable = &st_magn_buffer_postenable,
82 .predisable = &st_magn_buffer_predisable,
83};
84
85int st_magn_allocate_ring(struct iio_dev *indio_dev)
86{
87 return iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
88 &st_sensors_trigger_handler, &st_magn_buffer_setup_ops);
89}
90
91void st_magn_deallocate_ring(struct iio_dev *indio_dev)
92{
93 iio_triggered_buffer_cleanup(indio_dev);
94}
95
96MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
97MODULE_DESCRIPTION("STMicroelectronics magnetometers buffer");
98MODULE_LICENSE("GPL v2");