inv_mpu_trigger.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (C) 2012 Invensense, Inc.
  3. *
  4. * This software is licensed under the terms of the GNU General Public
  5. * License version 2, as published by the Free Software Foundation, and
  6. * may be copied, distributed, and modified under those terms.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include "inv_mpu_iio.h"
  14. static void inv_scan_query(struct iio_dev *indio_dev)
  15. {
  16. struct inv_mpu6050_state *st = iio_priv(indio_dev);
  17. st->chip_config.gyro_fifo_enable =
  18. test_bit(INV_MPU6050_SCAN_GYRO_X,
  19. indio_dev->active_scan_mask) ||
  20. test_bit(INV_MPU6050_SCAN_GYRO_Y,
  21. indio_dev->active_scan_mask) ||
  22. test_bit(INV_MPU6050_SCAN_GYRO_Z,
  23. indio_dev->active_scan_mask);
  24. st->chip_config.accl_fifo_enable =
  25. test_bit(INV_MPU6050_SCAN_ACCL_X,
  26. indio_dev->active_scan_mask) ||
  27. test_bit(INV_MPU6050_SCAN_ACCL_Y,
  28. indio_dev->active_scan_mask) ||
  29. test_bit(INV_MPU6050_SCAN_ACCL_Z,
  30. indio_dev->active_scan_mask);
  31. }
  32. /**
  33. * inv_mpu6050_set_enable() - enable chip functions.
  34. * @indio_dev: Device driver instance.
  35. * @enable: enable/disable
  36. */
  37. static int inv_mpu6050_set_enable(struct iio_dev *indio_dev, bool enable)
  38. {
  39. struct inv_mpu6050_state *st = iio_priv(indio_dev);
  40. int result;
  41. if (enable) {
  42. result = inv_mpu6050_set_power_itg(st, true);
  43. if (result)
  44. return result;
  45. inv_scan_query(indio_dev);
  46. st->skip_samples = 0;
  47. if (st->chip_config.gyro_fifo_enable) {
  48. result = inv_mpu6050_switch_engine(st, true,
  49. INV_MPU6050_BIT_PWR_GYRO_STBY);
  50. if (result)
  51. goto error_power_off;
  52. /* gyro first sample is out of specs, skip it */
  53. st->skip_samples = 1;
  54. }
  55. if (st->chip_config.accl_fifo_enable) {
  56. result = inv_mpu6050_switch_engine(st, true,
  57. INV_MPU6050_BIT_PWR_ACCL_STBY);
  58. if (result)
  59. goto error_gyro_off;
  60. }
  61. result = inv_reset_fifo(indio_dev);
  62. if (result)
  63. goto error_accl_off;
  64. } else {
  65. result = regmap_write(st->map, st->reg->fifo_en, 0);
  66. if (result)
  67. goto error_accl_off;
  68. result = regmap_write(st->map, st->reg->int_enable, 0);
  69. if (result)
  70. goto error_accl_off;
  71. result = regmap_write(st->map, st->reg->user_ctrl,
  72. st->chip_config.user_ctrl);
  73. if (result)
  74. goto error_accl_off;
  75. result = inv_mpu6050_switch_engine(st, false,
  76. INV_MPU6050_BIT_PWR_ACCL_STBY);
  77. if (result)
  78. goto error_accl_off;
  79. result = inv_mpu6050_switch_engine(st, false,
  80. INV_MPU6050_BIT_PWR_GYRO_STBY);
  81. if (result)
  82. goto error_gyro_off;
  83. result = inv_mpu6050_set_power_itg(st, false);
  84. if (result)
  85. goto error_power_off;
  86. }
  87. return 0;
  88. error_accl_off:
  89. if (st->chip_config.accl_fifo_enable)
  90. inv_mpu6050_switch_engine(st, false,
  91. INV_MPU6050_BIT_PWR_ACCL_STBY);
  92. error_gyro_off:
  93. if (st->chip_config.gyro_fifo_enable)
  94. inv_mpu6050_switch_engine(st, false,
  95. INV_MPU6050_BIT_PWR_GYRO_STBY);
  96. error_power_off:
  97. inv_mpu6050_set_power_itg(st, false);
  98. return result;
  99. }
  100. /**
  101. * inv_mpu_data_rdy_trigger_set_state() - set data ready interrupt state
  102. * @trig: Trigger instance
  103. * @state: Desired trigger state
  104. */
  105. static int inv_mpu_data_rdy_trigger_set_state(struct iio_trigger *trig,
  106. bool state)
  107. {
  108. struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
  109. struct inv_mpu6050_state *st = iio_priv(indio_dev);
  110. int result;
  111. mutex_lock(&st->lock);
  112. result = inv_mpu6050_set_enable(indio_dev, state);
  113. mutex_unlock(&st->lock);
  114. return result;
  115. }
  116. static const struct iio_trigger_ops inv_mpu_trigger_ops = {
  117. .set_trigger_state = &inv_mpu_data_rdy_trigger_set_state,
  118. };
  119. int inv_mpu6050_probe_trigger(struct iio_dev *indio_dev, int irq_type)
  120. {
  121. int ret;
  122. struct inv_mpu6050_state *st = iio_priv(indio_dev);
  123. st->trig = devm_iio_trigger_alloc(&indio_dev->dev,
  124. "%s-dev%d",
  125. indio_dev->name,
  126. indio_dev->id);
  127. if (!st->trig)
  128. return -ENOMEM;
  129. ret = devm_request_irq(&indio_dev->dev, st->irq,
  130. &iio_trigger_generic_data_rdy_poll,
  131. irq_type,
  132. "inv_mpu",
  133. st->trig);
  134. if (ret)
  135. return ret;
  136. st->trig->dev.parent = regmap_get_device(st->map);
  137. st->trig->ops = &inv_mpu_trigger_ops;
  138. iio_trigger_set_drvdata(st->trig, indio_dev);
  139. ret = devm_iio_trigger_register(&indio_dev->dev, st->trig);
  140. if (ret)
  141. return ret;
  142. indio_dev->trig = iio_trigger_get(st->trig);
  143. return 0;
  144. }