1 /*
2  * STMicroelectronics pressures driver
3  *
4  * Copyright 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_pressure.h"
25 
st_press_trig_set_state(struct iio_trigger * trig,bool state)26 int st_press_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_press_buffer_preenable(struct iio_dev * indio_dev)33 static int st_press_buffer_preenable(struct iio_dev *indio_dev)
34 {
35 	return st_sensors_set_enable(indio_dev, true);
36 }
37 
st_press_buffer_postenable(struct iio_dev * indio_dev)38 static int st_press_buffer_postenable(struct iio_dev *indio_dev)
39 {
40 	int err;
41 	struct st_sensor_data *press_data = iio_priv(indio_dev);
42 
43 	press_data->buffer_data = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
44 	if (press_data->buffer_data == NULL) {
45 		err = -ENOMEM;
46 		goto allocate_memory_error;
47 	}
48 
49 	err = iio_triggered_buffer_postenable(indio_dev);
50 	if (err < 0)
51 		goto st_press_buffer_postenable_error;
52 
53 	return err;
54 
55 st_press_buffer_postenable_error:
56 	kfree(press_data->buffer_data);
57 allocate_memory_error:
58 	return err;
59 }
60 
st_press_buffer_predisable(struct iio_dev * indio_dev)61 static int st_press_buffer_predisable(struct iio_dev *indio_dev)
62 {
63 	int err;
64 	struct st_sensor_data *press_data = iio_priv(indio_dev);
65 
66 	err = iio_triggered_buffer_predisable(indio_dev);
67 	if (err < 0)
68 		goto st_press_buffer_predisable_error;
69 
70 	err = st_sensors_set_enable(indio_dev, false);
71 
72 st_press_buffer_predisable_error:
73 	kfree(press_data->buffer_data);
74 	return err;
75 }
76 
77 static const struct iio_buffer_setup_ops st_press_buffer_setup_ops = {
78 	.preenable = &st_press_buffer_preenable,
79 	.postenable = &st_press_buffer_postenable,
80 	.predisable = &st_press_buffer_predisable,
81 };
82 
st_press_allocate_ring(struct iio_dev * indio_dev)83 int st_press_allocate_ring(struct iio_dev *indio_dev)
84 {
85 	return iio_triggered_buffer_setup(indio_dev, NULL,
86 		&st_sensors_trigger_handler, &st_press_buffer_setup_ops);
87 }
88 
st_press_deallocate_ring(struct iio_dev * indio_dev)89 void st_press_deallocate_ring(struct iio_dev *indio_dev)
90 {
91 	iio_triggered_buffer_cleanup(indio_dev);
92 }
93 
94 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
95 MODULE_DESCRIPTION("STMicroelectronics pressures buffer");
96 MODULE_LICENSE("GPL v2");
97