ssp_accel_sensor.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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/iio.h>
  17. #include <linux/iio/buffer.h>
  18. #include <linux/iio/kfifo_buf.h>
  19. #include <linux/module.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/slab.h>
  22. #include "../common/ssp_sensors/ssp_iio_sensor.h"
  23. #define SSP_CHANNEL_COUNT 3
  24. #define SSP_ACCEL_NAME "ssp-accelerometer"
  25. static const char ssp_accel_device_name[] = SSP_ACCEL_NAME;
  26. enum ssp_accel_3d_channel {
  27. SSP_CHANNEL_SCAN_INDEX_X,
  28. SSP_CHANNEL_SCAN_INDEX_Y,
  29. SSP_CHANNEL_SCAN_INDEX_Z,
  30. SSP_CHANNEL_SCAN_INDEX_TIME,
  31. };
  32. static int ssp_accel_read_raw(struct iio_dev *indio_dev,
  33. struct iio_chan_spec const *chan, int *val,
  34. int *val2, long mask)
  35. {
  36. u32 t;
  37. struct ssp_data *data = dev_get_drvdata(indio_dev->dev.parent->parent);
  38. switch (mask) {
  39. case IIO_CHAN_INFO_SAMP_FREQ:
  40. t = ssp_get_sensor_delay(data, SSP_ACCELEROMETER_SENSOR);
  41. ssp_convert_to_freq(t, val, val2);
  42. return IIO_VAL_INT_PLUS_MICRO;
  43. default:
  44. break;
  45. }
  46. return -EINVAL;
  47. }
  48. static int ssp_accel_write_raw(struct iio_dev *indio_dev,
  49. struct iio_chan_spec const *chan, int val,
  50. int val2, long mask)
  51. {
  52. int ret;
  53. struct ssp_data *data = dev_get_drvdata(indio_dev->dev.parent->parent);
  54. switch (mask) {
  55. case IIO_CHAN_INFO_SAMP_FREQ:
  56. ret = ssp_convert_to_time(val, val2);
  57. ret = ssp_change_delay(data, SSP_ACCELEROMETER_SENSOR, ret);
  58. if (ret < 0)
  59. dev_err(&indio_dev->dev, "accel sensor enable fail\n");
  60. return ret;
  61. default:
  62. break;
  63. }
  64. return -EINVAL;
  65. }
  66. static const struct iio_info ssp_accel_iio_info = {
  67. .read_raw = &ssp_accel_read_raw,
  68. .write_raw = &ssp_accel_write_raw,
  69. };
  70. static const unsigned long ssp_accel_scan_mask[] = { 0x7, 0, };
  71. static const struct iio_chan_spec ssp_acc_channels[] = {
  72. SSP_CHANNEL_AG(IIO_ACCEL, IIO_MOD_X, SSP_CHANNEL_SCAN_INDEX_X),
  73. SSP_CHANNEL_AG(IIO_ACCEL, IIO_MOD_Y, SSP_CHANNEL_SCAN_INDEX_Y),
  74. SSP_CHANNEL_AG(IIO_ACCEL, IIO_MOD_Z, SSP_CHANNEL_SCAN_INDEX_Z),
  75. SSP_CHAN_TIMESTAMP(SSP_CHANNEL_SCAN_INDEX_TIME),
  76. };
  77. static int ssp_process_accel_data(struct iio_dev *indio_dev, void *buf,
  78. int64_t timestamp)
  79. {
  80. return ssp_common_process_data(indio_dev, buf, SSP_ACCELEROMETER_SIZE,
  81. timestamp);
  82. }
  83. static const struct iio_buffer_setup_ops ssp_accel_buffer_ops = {
  84. .postenable = &ssp_common_buffer_postenable,
  85. .postdisable = &ssp_common_buffer_postdisable,
  86. };
  87. static int ssp_accel_probe(struct platform_device *pdev)
  88. {
  89. int ret;
  90. struct iio_dev *indio_dev;
  91. struct ssp_sensor_data *spd;
  92. struct iio_buffer *buffer;
  93. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*spd));
  94. if (!indio_dev)
  95. return -ENOMEM;
  96. spd = iio_priv(indio_dev);
  97. spd->process_data = ssp_process_accel_data;
  98. spd->type = SSP_ACCELEROMETER_SENSOR;
  99. indio_dev->name = ssp_accel_device_name;
  100. indio_dev->dev.parent = &pdev->dev;
  101. indio_dev->dev.of_node = pdev->dev.of_node;
  102. indio_dev->info = &ssp_accel_iio_info;
  103. indio_dev->modes = INDIO_BUFFER_SOFTWARE;
  104. indio_dev->channels = ssp_acc_channels;
  105. indio_dev->num_channels = ARRAY_SIZE(ssp_acc_channels);
  106. indio_dev->available_scan_masks = ssp_accel_scan_mask;
  107. buffer = devm_iio_kfifo_allocate(&pdev->dev);
  108. if (!buffer)
  109. return -ENOMEM;
  110. iio_device_attach_buffer(indio_dev, buffer);
  111. indio_dev->setup_ops = &ssp_accel_buffer_ops;
  112. platform_set_drvdata(pdev, indio_dev);
  113. ret = devm_iio_device_register(&pdev->dev, indio_dev);
  114. if (ret < 0)
  115. return ret;
  116. /* ssp registering should be done after all iio setup */
  117. ssp_register_consumer(indio_dev, SSP_ACCELEROMETER_SENSOR);
  118. return 0;
  119. }
  120. static struct platform_driver ssp_accel_driver = {
  121. .driver = {
  122. .name = SSP_ACCEL_NAME,
  123. },
  124. .probe = ssp_accel_probe,
  125. };
  126. module_platform_driver(ssp_accel_driver);
  127. MODULE_AUTHOR("Karol Wrona <k.wrona@samsung.com>");
  128. MODULE_DESCRIPTION("Samsung sensorhub accelerometers driver");
  129. MODULE_LICENSE("GPL");