st_magn_buffer.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * STMicroelectronics magnetometers driver
  3. *
  4. * Copyright 2012-2013 STMicroelectronics Inc.
  5. *
  6. * Denis Ciocca <[email protected]>
  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_postenable(struct iio_dev *indio_dev)
  29. {
  30. int err;
  31. struct st_sensor_data *mdata = iio_priv(indio_dev);
  32. mdata->buffer_data = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
  33. if (mdata->buffer_data == NULL) {
  34. err = -ENOMEM;
  35. goto allocate_memory_error;
  36. }
  37. err = iio_triggered_buffer_postenable(indio_dev);
  38. if (err < 0)
  39. goto st_magn_buffer_postenable_error;
  40. return st_sensors_set_enable(indio_dev, true);
  41. st_magn_buffer_postenable_error:
  42. kfree(mdata->buffer_data);
  43. allocate_memory_error:
  44. return err;
  45. }
  46. static int st_magn_buffer_predisable(struct iio_dev *indio_dev)
  47. {
  48. int err;
  49. struct st_sensor_data *mdata = iio_priv(indio_dev);
  50. err = st_sensors_set_enable(indio_dev, false);
  51. if (err < 0)
  52. goto st_magn_buffer_predisable_error;
  53. err = iio_triggered_buffer_predisable(indio_dev);
  54. st_magn_buffer_predisable_error:
  55. kfree(mdata->buffer_data);
  56. return err;
  57. }
  58. static const struct iio_buffer_setup_ops st_magn_buffer_setup_ops = {
  59. .postenable = &st_magn_buffer_postenable,
  60. .predisable = &st_magn_buffer_predisable,
  61. };
  62. int st_magn_allocate_ring(struct iio_dev *indio_dev)
  63. {
  64. return iio_triggered_buffer_setup(indio_dev, NULL,
  65. &st_sensors_trigger_handler, &st_magn_buffer_setup_ops);
  66. }
  67. void st_magn_deallocate_ring(struct iio_dev *indio_dev)
  68. {
  69. iio_triggered_buffer_cleanup(indio_dev);
  70. }
  71. MODULE_AUTHOR("Denis Ciocca <[email protected]>");
  72. MODULE_DESCRIPTION("STMicroelectronics magnetometers buffer");
  73. MODULE_LICENSE("GPL v2");