ssp_iio.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (C) 2014, Samsung Electronics Co. Ltd. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. */
  15. #include <linux/iio/common/ssp_sensors.h>
  16. #include <linux/iio/buffer.h>
  17. #include <linux/iio/kfifo_buf.h>
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include "ssp_iio_sensor.h"
  21. /**
  22. * ssp_common_buffer_postenable() - generic postenable callback for ssp buffer
  23. *
  24. * @indio_dev: iio device
  25. *
  26. * Returns 0 or negative value in case of error
  27. */
  28. int ssp_common_buffer_postenable(struct iio_dev *indio_dev)
  29. {
  30. struct ssp_sensor_data *spd = iio_priv(indio_dev);
  31. struct ssp_data *data = dev_get_drvdata(indio_dev->dev.parent->parent);
  32. /* the allocation is made in post because scan size is known in this
  33. * moment
  34. * */
  35. spd->buffer = kmalloc(indio_dev->scan_bytes, GFP_KERNEL | GFP_DMA);
  36. if (!spd->buffer)
  37. return -ENOMEM;
  38. return ssp_enable_sensor(data, spd->type,
  39. ssp_get_sensor_delay(data, spd->type));
  40. }
  41. EXPORT_SYMBOL(ssp_common_buffer_postenable);
  42. /**
  43. * ssp_common_buffer_postdisable() - generic postdisable callback for ssp buffer
  44. *
  45. * @indio_dev: iio device
  46. *
  47. * Returns 0 or negative value in case of error
  48. */
  49. int ssp_common_buffer_postdisable(struct iio_dev *indio_dev)
  50. {
  51. int ret;
  52. struct ssp_sensor_data *spd = iio_priv(indio_dev);
  53. struct ssp_data *data = dev_get_drvdata(indio_dev->dev.parent->parent);
  54. ret = ssp_disable_sensor(data, spd->type);
  55. if (ret < 0)
  56. return ret;
  57. kfree(spd->buffer);
  58. return ret;
  59. }
  60. EXPORT_SYMBOL(ssp_common_buffer_postdisable);
  61. /**
  62. * ssp_common_process_data() - Common process data callback for ssp sensors
  63. *
  64. * @indio_dev: iio device
  65. * @buf: source buffer
  66. * @len: sensor data length
  67. * @timestamp: system timestamp
  68. *
  69. * Returns 0 or negative value in case of error
  70. */
  71. int ssp_common_process_data(struct iio_dev *indio_dev, void *buf,
  72. unsigned int len, int64_t timestamp)
  73. {
  74. __le32 time;
  75. int64_t calculated_time = 0;
  76. struct ssp_sensor_data *spd = iio_priv(indio_dev);
  77. if (indio_dev->scan_bytes == 0)
  78. return 0;
  79. /*
  80. * it always sends full set of samples, remember about available masks
  81. */
  82. memcpy(spd->buffer, buf, len);
  83. if (indio_dev->scan_timestamp) {
  84. memcpy(&time, &((char *)buf)[len], SSP_TIME_SIZE);
  85. calculated_time =
  86. timestamp + (int64_t)le32_to_cpu(time) * 1000000;
  87. }
  88. return iio_push_to_buffers_with_timestamp(indio_dev, spd->buffer,
  89. calculated_time);
  90. }
  91. EXPORT_SYMBOL(ssp_common_process_data);
  92. MODULE_AUTHOR("Karol Wrona <k.wrona@samsung.com>");
  93. MODULE_DESCRIPTION("Samsung sensorhub commons");
  94. MODULE_LICENSE("GPL");