st_gyro_buffer.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. #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. #include <linux/iio/common/st_sensors.h>
  22. #include "st_gyro.h"
  23. int st_gyro_trig_set_state(struct iio_trigger *trig, bool state)
  24. {
  25. struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
  26. return st_sensors_set_dataready_irq(indio_dev, state);
  27. }
  28. static int st_gyro_buffer_preenable(struct iio_dev *indio_dev)
  29. {
  30. return st_sensors_set_enable(indio_dev, true);
  31. }
  32. static int st_gyro_buffer_postenable(struct iio_dev *indio_dev)
  33. {
  34. int err;
  35. struct st_sensor_data *gdata = iio_priv(indio_dev);
  36. gdata->buffer_data = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
  37. if (gdata->buffer_data == NULL) {
  38. err = -ENOMEM;
  39. goto allocate_memory_error;
  40. }
  41. err = st_sensors_set_axis_enable(indio_dev,
  42. (u8)indio_dev->active_scan_mask[0]);
  43. if (err < 0)
  44. goto st_gyro_buffer_postenable_error;
  45. err = iio_triggered_buffer_postenable(indio_dev);
  46. if (err < 0)
  47. goto st_gyro_buffer_postenable_error;
  48. return err;
  49. st_gyro_buffer_postenable_error:
  50. kfree(gdata->buffer_data);
  51. allocate_memory_error:
  52. return err;
  53. }
  54. static int st_gyro_buffer_predisable(struct iio_dev *indio_dev)
  55. {
  56. int err;
  57. struct st_sensor_data *gdata = iio_priv(indio_dev);
  58. err = iio_triggered_buffer_predisable(indio_dev);
  59. if (err < 0)
  60. goto st_gyro_buffer_predisable_error;
  61. err = st_sensors_set_axis_enable(indio_dev, ST_SENSORS_ENABLE_ALL_AXIS);
  62. if (err < 0)
  63. goto st_gyro_buffer_predisable_error;
  64. err = st_sensors_set_enable(indio_dev, false);
  65. st_gyro_buffer_predisable_error:
  66. kfree(gdata->buffer_data);
  67. return err;
  68. }
  69. static const struct iio_buffer_setup_ops st_gyro_buffer_setup_ops = {
  70. .preenable = &st_gyro_buffer_preenable,
  71. .postenable = &st_gyro_buffer_postenable,
  72. .predisable = &st_gyro_buffer_predisable,
  73. };
  74. int st_gyro_allocate_ring(struct iio_dev *indio_dev)
  75. {
  76. return iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
  77. &st_sensors_trigger_handler, &st_gyro_buffer_setup_ops);
  78. }
  79. void st_gyro_deallocate_ring(struct iio_dev *indio_dev)
  80. {
  81. iio_triggered_buffer_cleanup(indio_dev);
  82. }
  83. MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
  84. MODULE_DESCRIPTION("STMicroelectronics gyroscopes buffer");
  85. MODULE_LICENSE("GPL v2");