blob: 2ff53f222352ca66d4057ff054732e6a0a581db6 [file] [log] [blame]
Denis CIOCCA217494e2013-06-03 15:58:00 +01001/*
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
26int 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
33static int st_press_buffer_preenable(struct iio_dev *indio_dev)
34{
Lars-Peter Clausen6c0743e2013-10-14 17:49:00 +010035 return st_sensors_set_enable(indio_dev, true);
Denis CIOCCA217494e2013-06-03 15:58:00 +010036}
37
38static int st_press_buffer_postenable(struct iio_dev *indio_dev)
39{
40 int err;
Denis CIOCCAa1dcf422014-10-03 17:35:40 +020041 struct st_sensor_data *press_data = iio_priv(indio_dev);
Denis CIOCCA217494e2013-06-03 15:58:00 +010042
Denis CIOCCAa1dcf422014-10-03 17:35:40 +020043 press_data->buffer_data = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
44 if (press_data->buffer_data == NULL) {
Denis CIOCCA217494e2013-06-03 15:58:00 +010045 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
55st_press_buffer_postenable_error:
Denis CIOCCAa1dcf422014-10-03 17:35:40 +020056 kfree(press_data->buffer_data);
Denis CIOCCA217494e2013-06-03 15:58:00 +010057allocate_memory_error:
58 return err;
59}
60
61static int st_press_buffer_predisable(struct iio_dev *indio_dev)
62{
63 int err;
Denis CIOCCAa1dcf422014-10-03 17:35:40 +020064 struct st_sensor_data *press_data = iio_priv(indio_dev);
Denis CIOCCA217494e2013-06-03 15:58:00 +010065
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
72st_press_buffer_predisable_error:
Denis CIOCCAa1dcf422014-10-03 17:35:40 +020073 kfree(press_data->buffer_data);
Denis CIOCCA217494e2013-06-03 15:58:00 +010074 return err;
75}
76
77static 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
83int st_press_allocate_ring(struct iio_dev *indio_dev)
84{
85 return iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
86 &st_sensors_trigger_handler, &st_press_buffer_setup_ops);
87}
88
89void st_press_deallocate_ring(struct iio_dev *indio_dev)
90{
91 iio_triggered_buffer_cleanup(indio_dev);
92}
93
94MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
95MODULE_DESCRIPTION("STMicroelectronics pressures buffer");
96MODULE_LICENSE("GPL v2");