inv_mpu_trigger.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 = inv_mpu6050_write_reg(st, st->reg->fifo_en, 0);
  63. if (result)
  64. return result;
  65. result = inv_mpu6050_write_reg(st, st->reg->int_enable, 0);
  66. if (result)
  67. return result;
  68. result = inv_mpu6050_write_reg(st, 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. st->chip_config.enable = enable;
  84. return 0;
  85. }
  86. /**
  87. * inv_mpu_data_rdy_trigger_set_state() - set data ready interrupt state
  88. * @trig: Trigger instance
  89. * @state: Desired trigger state
  90. */
  91. static int inv_mpu_data_rdy_trigger_set_state(struct iio_trigger *trig,
  92. bool state)
  93. {
  94. return inv_mpu6050_set_enable(iio_trigger_get_drvdata(trig), state);
  95. }
  96. static const struct iio_trigger_ops inv_mpu_trigger_ops = {
  97. .owner = THIS_MODULE,
  98. .set_trigger_state = &inv_mpu_data_rdy_trigger_set_state,
  99. };
  100. int inv_mpu6050_probe_trigger(struct iio_dev *indio_dev)
  101. {
  102. int ret;
  103. struct inv_mpu6050_state *st = iio_priv(indio_dev);
  104. st->trig = devm_iio_trigger_alloc(&indio_dev->dev,
  105. "%s-dev%d",
  106. indio_dev->name,
  107. indio_dev->id);
  108. if (!st->trig)
  109. return -ENOMEM;
  110. ret = devm_request_irq(&indio_dev->dev, st->client->irq,
  111. &iio_trigger_generic_data_rdy_poll,
  112. IRQF_TRIGGER_RISING,
  113. "inv_mpu",
  114. st->trig);
  115. if (ret)
  116. return ret;
  117. st->trig->dev.parent = &st->client->dev;
  118. st->trig->ops = &inv_mpu_trigger_ops;
  119. iio_trigger_set_drvdata(st->trig, indio_dev);
  120. ret = iio_trigger_register(st->trig);
  121. if (ret)
  122. return ret;
  123. indio_dev->trig = iio_trigger_get(st->trig);
  124. return 0;
  125. }
  126. void inv_mpu6050_remove_trigger(struct inv_mpu6050_state *st)
  127. {
  128. iio_trigger_unregister(st->trig);
  129. }