ssp_gyro_sensor.c 4.4 KB

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