ssp_iio.c 2.9 KB

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