1 /* 2 * STMicroelectronics magnetometers 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_magn.h" 25 26 int st_magn_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 33 static int st_magn_buffer_postenable(struct iio_dev *indio_dev) 34 { 35 int err; 36 struct st_sensor_data *mdata = iio_priv(indio_dev); 37 38 mdata->buffer_data = kmalloc(indio_dev->scan_bytes, GFP_KERNEL); 39 if (mdata->buffer_data == NULL) { 40 err = -ENOMEM; 41 goto allocate_memory_error; 42 } 43 44 err = iio_triggered_buffer_postenable(indio_dev); 45 if (err < 0) 46 goto st_magn_buffer_postenable_error; 47 48 return st_sensors_set_enable(indio_dev, true); 49 50 st_magn_buffer_postenable_error: 51 kfree(mdata->buffer_data); 52 allocate_memory_error: 53 return err; 54 } 55 56 static int st_magn_buffer_predisable(struct iio_dev *indio_dev) 57 { 58 int err; 59 struct st_sensor_data *mdata = iio_priv(indio_dev); 60 61 err = st_sensors_set_enable(indio_dev, false); 62 if (err < 0) 63 goto st_magn_buffer_predisable_error; 64 65 err = iio_triggered_buffer_predisable(indio_dev); 66 67 st_magn_buffer_predisable_error: 68 kfree(mdata->buffer_data); 69 return err; 70 } 71 72 static const struct iio_buffer_setup_ops st_magn_buffer_setup_ops = { 73 .postenable = &st_magn_buffer_postenable, 74 .predisable = &st_magn_buffer_predisable, 75 }; 76 77 int st_magn_allocate_ring(struct iio_dev *indio_dev) 78 { 79 return iio_triggered_buffer_setup(indio_dev, NULL, 80 &st_sensors_trigger_handler, &st_magn_buffer_setup_ops); 81 } 82 83 void st_magn_deallocate_ring(struct iio_dev *indio_dev) 84 { 85 iio_triggered_buffer_cleanup(indio_dev); 86 } 87 88 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>"); 89 MODULE_DESCRIPTION("STMicroelectronics magnetometers buffer"); 90 MODULE_LICENSE("GPL v2"); 91