• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * STMicroelectronics gyroscopes 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_gyro.h"
25 
st_gyro_trig_set_state(struct iio_trigger * trig,bool state)26 int st_gyro_trig_set_state(struct iio_trigger *trig, bool state)
27 {
28 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
29 
30 	return st_sensors_set_dataready_irq(indio_dev, state);
31 }
32 
st_gyro_buffer_preenable(struct iio_dev * indio_dev)33 static int st_gyro_buffer_preenable(struct iio_dev *indio_dev)
34 {
35 	int err;
36 
37 	err = st_sensors_set_enable(indio_dev, true);
38 	if (err < 0)
39 		goto st_gyro_set_enable_error;
40 
41 	err = iio_sw_buffer_preenable(indio_dev);
42 
43 st_gyro_set_enable_error:
44 	return err;
45 }
46 
st_gyro_buffer_postenable(struct iio_dev * indio_dev)47 static int st_gyro_buffer_postenable(struct iio_dev *indio_dev)
48 {
49 	int err;
50 	struct st_sensor_data *gdata = iio_priv(indio_dev);
51 
52 	gdata->buffer_data = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
53 	if (gdata->buffer_data == NULL) {
54 		err = -ENOMEM;
55 		goto allocate_memory_error;
56 	}
57 
58 	err = st_sensors_set_axis_enable(indio_dev,
59 					(u8)indio_dev->active_scan_mask[0]);
60 	if (err < 0)
61 		goto st_gyro_buffer_postenable_error;
62 
63 	err = iio_triggered_buffer_postenable(indio_dev);
64 	if (err < 0)
65 		goto st_gyro_buffer_postenable_error;
66 
67 	return err;
68 
69 st_gyro_buffer_postenable_error:
70 	kfree(gdata->buffer_data);
71 allocate_memory_error:
72 	return err;
73 }
74 
st_gyro_buffer_predisable(struct iio_dev * indio_dev)75 static int st_gyro_buffer_predisable(struct iio_dev *indio_dev)
76 {
77 	int err;
78 	struct st_sensor_data *gdata = iio_priv(indio_dev);
79 
80 	err = iio_triggered_buffer_predisable(indio_dev);
81 	if (err < 0)
82 		goto st_gyro_buffer_predisable_error;
83 
84 	err = st_sensors_set_axis_enable(indio_dev, ST_SENSORS_ENABLE_ALL_AXIS);
85 	if (err < 0)
86 		goto st_gyro_buffer_predisable_error;
87 
88 	err = st_sensors_set_enable(indio_dev, false);
89 
90 st_gyro_buffer_predisable_error:
91 	kfree(gdata->buffer_data);
92 	return err;
93 }
94 
95 static const struct iio_buffer_setup_ops st_gyro_buffer_setup_ops = {
96 	.preenable = &st_gyro_buffer_preenable,
97 	.postenable = &st_gyro_buffer_postenable,
98 	.predisable = &st_gyro_buffer_predisable,
99 };
100 
st_gyro_allocate_ring(struct iio_dev * indio_dev)101 int st_gyro_allocate_ring(struct iio_dev *indio_dev)
102 {
103 	return iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
104 		&st_sensors_trigger_handler, &st_gyro_buffer_setup_ops);
105 }
106 
st_gyro_deallocate_ring(struct iio_dev * indio_dev)107 void st_gyro_deallocate_ring(struct iio_dev *indio_dev)
108 {
109 	iio_triggered_buffer_cleanup(indio_dev);
110 }
111 
112 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
113 MODULE_DESCRIPTION("STMicroelectronics gyroscopes buffer");
114 MODULE_LICENSE("GPL v2");
115