st_magn_buffer.c 2.3 KB

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