inv_mpu_trigger.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. if (st->chip_config.gyro_fifo_enable) {
  47. result = inv_mpu6050_switch_engine(st, true,
  48. INV_MPU6050_BIT_PWR_GYRO_STBY);
  49. if (result)
  50. return result;
  51. }
  52. if (st->chip_config.accl_fifo_enable) {
  53. result = inv_mpu6050_switch_engine(st, true,
  54. INV_MPU6050_BIT_PWR_ACCL_STBY);
  55. if (result)
  56. return result;
  57. }
  58. result = inv_reset_fifo(indio_dev);
  59. if (result)
  60. return result;
  61. } else {
  62. result = regmap_write(st->map, st->reg->fifo_en, 0);
  63. if (result)
  64. return result;
  65. result = regmap_write(st->map, st->reg->int_enable, 0);
  66. if (result)
  67. return result;
  68. result = regmap_write(st->map, st->reg->user_ctrl, 0);
  69. if (result)
  70. return result;
  71. result = inv_mpu6050_switch_engine(st, false,
  72. INV_MPU6050_BIT_PWR_GYRO_STBY);
  73. if (result)
  74. return result;
  75. result = inv_mpu6050_switch_engine(st, false,
  76. INV_MPU6050_BIT_PWR_ACCL_STBY);
  77. if (result)
  78. return result;
  79. result = inv_mpu6050_set_power_itg(st, false);
  80. if (result)
  81. return result;
  82. }
  83. return 0;
  84. }
  85. /**
  86. * inv_mpu_data_rdy_trigger_set_state() - set data ready interrupt state
  87. * @trig: Trigger instance
  88. * @state: Desired trigger state
  89. */
  90. static int inv_mpu_data_rdy_trigger_set_state(struct iio_trigger *trig,
  91. bool state)
  92. {
  93. struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
  94. struct inv_mpu6050_state *st = iio_priv(indio_dev);
  95. int result;
  96. mutex_lock(&st->lock);
  97. result = inv_mpu6050_set_enable(indio_dev, state);
  98. mutex_unlock(&st->lock);
  99. return result;
  100. }
  101. static const struct iio_trigger_ops inv_mpu_trigger_ops = {
  102. .owner = THIS_MODULE,
  103. .set_trigger_state = &inv_mpu_data_rdy_trigger_set_state,
  104. };
  105. int inv_mpu6050_probe_trigger(struct iio_dev *indio_dev)
  106. {
  107. int ret;
  108. struct inv_mpu6050_state *st = iio_priv(indio_dev);
  109. st->trig = devm_iio_trigger_alloc(&indio_dev->dev,
  110. "%s-dev%d",
  111. indio_dev->name,
  112. indio_dev->id);
  113. if (!st->trig)
  114. return -ENOMEM;
  115. ret = devm_request_irq(&indio_dev->dev, st->irq,
  116. &iio_trigger_generic_data_rdy_poll,
  117. IRQF_TRIGGER_RISING,
  118. "inv_mpu",
  119. st->trig);
  120. if (ret)
  121. return ret;
  122. st->trig->dev.parent = regmap_get_device(st->map);
  123. st->trig->ops = &inv_mpu_trigger_ops;
  124. iio_trigger_set_drvdata(st->trig, indio_dev);
  125. ret = iio_trigger_register(st->trig);
  126. if (ret)
  127. return ret;
  128. indio_dev->trig = iio_trigger_get(st->trig);
  129. return 0;
  130. }
  131. void inv_mpu6050_remove_trigger(struct inv_mpu6050_state *st)
  132. {
  133. iio_trigger_unregister(st->trig);
  134. }