1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * STMicroelectronics accelerometers driver 4 * 5 * Copyright 2012-2013 STMicroelectronics Inc. 6 * 7 * Denis Ciocca <denis.ciocca@st.com> 8 */ 9 10 #include <linux/module.h> 11 #include <linux/kernel.h> 12 #include <linux/slab.h> 13 #include <linux/stat.h> 14 #include <linux/interrupt.h> 15 #include <linux/i2c.h> 16 #include <linux/delay.h> 17 #include <linux/iio/iio.h> 18 #include <linux/iio/buffer.h> 19 #include <linux/iio/trigger_consumer.h> 20 #include <linux/iio/triggered_buffer.h> 21 22 #include <linux/iio/common/st_sensors.h> 23 #include "st_accel.h" 24 25 int st_accel_trig_set_state(struct iio_trigger *trig, bool state) 26 { 27 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); 28 29 return st_sensors_set_dataready_irq(indio_dev, state); 30 } 31 32 static int st_accel_buffer_preenable(struct iio_dev *indio_dev) 33 { 34 return st_sensors_set_enable(indio_dev, true); 35 } 36 37 static int st_accel_buffer_postenable(struct iio_dev *indio_dev) 38 { 39 int err; 40 struct st_sensor_data *adata = iio_priv(indio_dev); 41 42 adata->buffer_data = kmalloc(indio_dev->scan_bytes, GFP_KERNEL); 43 if (adata->buffer_data == NULL) { 44 err = -ENOMEM; 45 goto allocate_memory_error; 46 } 47 48 err = iio_triggered_buffer_postenable(indio_dev); 49 if (err < 0) 50 goto st_accel_buffer_postenable_error; 51 52 err = st_sensors_set_axis_enable(indio_dev, 53 (u8)indio_dev->active_scan_mask[0]); 54 if (err < 0) 55 goto st_sensors_set_axis_enable_error; 56 57 return err; 58 59 st_sensors_set_axis_enable_error: 60 iio_triggered_buffer_predisable(indio_dev); 61 st_accel_buffer_postenable_error: 62 kfree(adata->buffer_data); 63 allocate_memory_error: 64 return err; 65 } 66 67 static int st_accel_buffer_predisable(struct iio_dev *indio_dev) 68 { 69 int err, err2; 70 struct st_sensor_data *adata = iio_priv(indio_dev); 71 72 err = st_sensors_set_axis_enable(indio_dev, ST_SENSORS_ENABLE_ALL_AXIS); 73 if (err < 0) 74 goto st_accel_buffer_predisable_error; 75 76 err = st_sensors_set_enable(indio_dev, false); 77 if (err < 0) 78 goto st_accel_buffer_predisable_error; 79 80 st_accel_buffer_predisable_error: 81 err2 = iio_triggered_buffer_predisable(indio_dev); 82 if (!err) 83 err = err2; 84 85 kfree(adata->buffer_data); 86 return err; 87 } 88 89 static const struct iio_buffer_setup_ops st_accel_buffer_setup_ops = { 90 .preenable = &st_accel_buffer_preenable, 91 .postenable = &st_accel_buffer_postenable, 92 .predisable = &st_accel_buffer_predisable, 93 }; 94 95 int st_accel_allocate_ring(struct iio_dev *indio_dev) 96 { 97 return iio_triggered_buffer_setup(indio_dev, NULL, 98 &st_sensors_trigger_handler, &st_accel_buffer_setup_ops); 99 } 100 101 void st_accel_deallocate_ring(struct iio_dev *indio_dev) 102 { 103 iio_triggered_buffer_cleanup(indio_dev); 104 } 105 106 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>"); 107 MODULE_DESCRIPTION("STMicroelectronics accelerometers buffer"); 108 MODULE_LICENSE("GPL v2"); 109