blob: a1e642ee13d6a4f8af5eb0f6a9c92bafe51460d9 [file] [log] [blame]
Denis Cioccad6251162013-01-25 23:44:00 +00001/*
2 * STMicroelectronics accelerometers 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_accel.h"
25
26int st_accel_trig_set_state(struct iio_trigger *trig, bool state)
27{
Lars-Peter Clausen1e9663c2013-03-25 08:58:00 +000028 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
Denis Cioccad6251162013-01-25 23:44:00 +000029
30 return st_sensors_set_dataready_irq(indio_dev, state);
31}
32
33static int st_accel_buffer_preenable(struct iio_dev *indio_dev)
34{
Lars-Peter Clausend0894cab2013-10-14 17:49:00 +010035 return st_sensors_set_enable(indio_dev, true);
Denis Cioccad6251162013-01-25 23:44:00 +000036}
37
38static int st_accel_buffer_postenable(struct iio_dev *indio_dev)
39{
40 int err;
41 struct st_sensor_data *adata = iio_priv(indio_dev);
42
43 adata->buffer_data = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
44 if (adata->buffer_data == NULL) {
45 err = -ENOMEM;
46 goto allocate_memory_error;
47 }
48
49 err = st_sensors_set_axis_enable(indio_dev,
50 (u8)indio_dev->active_scan_mask[0]);
51 if (err < 0)
52 goto st_accel_buffer_postenable_error;
53
54 err = iio_triggered_buffer_postenable(indio_dev);
55 if (err < 0)
56 goto st_accel_buffer_postenable_error;
57
58 return err;
59
60st_accel_buffer_postenable_error:
61 kfree(adata->buffer_data);
62allocate_memory_error:
63 return err;
64}
65
66static int st_accel_buffer_predisable(struct iio_dev *indio_dev)
67{
68 int err;
69 struct st_sensor_data *adata = iio_priv(indio_dev);
70
71 err = iio_triggered_buffer_predisable(indio_dev);
72 if (err < 0)
73 goto st_accel_buffer_predisable_error;
74
75 err = st_sensors_set_axis_enable(indio_dev, ST_SENSORS_ENABLE_ALL_AXIS);
76 if (err < 0)
77 goto st_accel_buffer_predisable_error;
78
79 err = st_sensors_set_enable(indio_dev, false);
80
81st_accel_buffer_predisable_error:
82 kfree(adata->buffer_data);
83 return err;
84}
85
86static const struct iio_buffer_setup_ops st_accel_buffer_setup_ops = {
87 .preenable = &st_accel_buffer_preenable,
88 .postenable = &st_accel_buffer_postenable,
89 .predisable = &st_accel_buffer_predisable,
90};
91
92int st_accel_allocate_ring(struct iio_dev *indio_dev)
93{
94 return iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
95 &st_sensors_trigger_handler, &st_accel_buffer_setup_ops);
96}
97
98void st_accel_deallocate_ring(struct iio_dev *indio_dev)
99{
100 iio_triggered_buffer_cleanup(indio_dev);
101}
102
103MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
104MODULE_DESCRIPTION("STMicroelectronics accelerometers buffer");
105MODULE_LICENSE("GPL v2");