ssp_iio.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2014, Samsung Electronics Co. Ltd. All Rights Reserved.
  4. */
  5. #include <linux/iio/common/ssp_sensors.h>
  6. #include <linux/iio/buffer.h>
  7. #include <linux/iio/kfifo_buf.h>
  8. #include <linux/module.h>
  9. #include <linux/slab.h>
  10. #include "ssp_iio_sensor.h"
  11. /**
  12. * ssp_common_buffer_postenable() - generic postenable callback for ssp buffer
  13. *
  14. * @indio_dev: iio device
  15. *
  16. * Returns 0 or negative value in case of error
  17. */
  18. int ssp_common_buffer_postenable(struct iio_dev *indio_dev)
  19. {
  20. struct ssp_sensor_data *spd = iio_priv(indio_dev);
  21. struct ssp_data *data = dev_get_drvdata(indio_dev->dev.parent->parent);
  22. /* the allocation is made in post because scan size is known in this
  23. * moment
  24. * */
  25. spd->buffer = kmalloc(indio_dev->scan_bytes, GFP_KERNEL | GFP_DMA);
  26. if (!spd->buffer)
  27. return -ENOMEM;
  28. return ssp_enable_sensor(data, spd->type,
  29. ssp_get_sensor_delay(data, spd->type));
  30. }
  31. EXPORT_SYMBOL_NS(ssp_common_buffer_postenable, IIO_SSP_SENSORS);
  32. /**
  33. * ssp_common_buffer_postdisable() - generic postdisable callback for ssp buffer
  34. *
  35. * @indio_dev: iio device
  36. *
  37. * Returns 0 or negative value in case of error
  38. */
  39. int ssp_common_buffer_postdisable(struct iio_dev *indio_dev)
  40. {
  41. int ret;
  42. struct ssp_sensor_data *spd = iio_priv(indio_dev);
  43. struct ssp_data *data = dev_get_drvdata(indio_dev->dev.parent->parent);
  44. ret = ssp_disable_sensor(data, spd->type);
  45. if (ret < 0)
  46. return ret;
  47. kfree(spd->buffer);
  48. return ret;
  49. }
  50. EXPORT_SYMBOL_NS(ssp_common_buffer_postdisable, IIO_SSP_SENSORS);
  51. /**
  52. * ssp_common_process_data() - Common process data callback for ssp sensors
  53. *
  54. * @indio_dev: iio device
  55. * @buf: source buffer
  56. * @len: sensor data length
  57. * @timestamp: system timestamp
  58. *
  59. * Returns 0 or negative value in case of error
  60. */
  61. int ssp_common_process_data(struct iio_dev *indio_dev, void *buf,
  62. unsigned int len, int64_t timestamp)
  63. {
  64. __le32 time;
  65. int64_t calculated_time = 0;
  66. struct ssp_sensor_data *spd = iio_priv(indio_dev);
  67. if (indio_dev->scan_bytes == 0)
  68. return 0;
  69. /*
  70. * it always sends full set of samples, remember about available masks
  71. */
  72. memcpy(spd->buffer, buf, len);
  73. if (indio_dev->scan_timestamp) {
  74. memcpy(&time, &((char *)buf)[len], SSP_TIME_SIZE);
  75. calculated_time =
  76. timestamp + (int64_t)le32_to_cpu(time) * 1000000;
  77. }
  78. return iio_push_to_buffers_with_timestamp(indio_dev, spd->buffer,
  79. calculated_time);
  80. }
  81. EXPORT_SYMBOL_NS(ssp_common_process_data, IIO_SSP_SENSORS);
  82. MODULE_AUTHOR("Karol Wrona <k.wrona@samsung.com>");
  83. MODULE_DESCRIPTION("Samsung sensorhub commons");
  84. MODULE_LICENSE("GPL");
  85. MODULE_IMPORT_NS(IIO_SSP_SENSORS);