inv_mpu_ring.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 <linux/module.h>
  14. #include <linux/slab.h>
  15. #include <linux/err.h>
  16. #include <linux/delay.h>
  17. #include <linux/sysfs.h>
  18. #include <linux/jiffies.h>
  19. #include <linux/irq.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/poll.h>
  22. #include <linux/math64.h>
  23. #include <asm/unaligned.h>
  24. #include "inv_mpu_iio.h"
  25. /**
  26. * inv_mpu6050_update_period() - Update chip internal period estimation
  27. *
  28. * @st: driver state
  29. * @timestamp: the interrupt timestamp
  30. * @nb: number of data set in the fifo
  31. *
  32. * This function uses interrupt timestamps to estimate the chip period and
  33. * to choose the data timestamp to come.
  34. */
  35. static void inv_mpu6050_update_period(struct inv_mpu6050_state *st,
  36. s64 timestamp, size_t nb)
  37. {
  38. /* Period boundaries for accepting timestamp */
  39. const s64 period_min =
  40. (NSEC_PER_MSEC * (100 - INV_MPU6050_TS_PERIOD_JITTER)) / 100;
  41. const s64 period_max =
  42. (NSEC_PER_MSEC * (100 + INV_MPU6050_TS_PERIOD_JITTER)) / 100;
  43. const s32 divider = INV_MPU6050_FREQ_DIVIDER(st);
  44. s64 delta, interval;
  45. bool use_it_timestamp = false;
  46. if (st->it_timestamp == 0) {
  47. /* not initialized, forced to use it_timestamp */
  48. use_it_timestamp = true;
  49. } else if (nb == 1) {
  50. /*
  51. * Validate the use of it timestamp by checking if interrupt
  52. * has been delayed.
  53. * nb > 1 means interrupt was delayed for more than 1 sample,
  54. * so it's obviously not good.
  55. * Compute the chip period between 2 interrupts for validating.
  56. */
  57. delta = div_s64(timestamp - st->it_timestamp, divider);
  58. if (delta > period_min && delta < period_max) {
  59. /* update chip period and use it timestamp */
  60. st->chip_period = (st->chip_period + delta) / 2;
  61. use_it_timestamp = true;
  62. }
  63. }
  64. if (use_it_timestamp) {
  65. /*
  66. * Manage case of multiple samples in the fifo (nb > 1):
  67. * compute timestamp corresponding to the first sample using
  68. * estimated chip period.
  69. */
  70. interval = (nb - 1) * st->chip_period * divider;
  71. st->data_timestamp = timestamp - interval;
  72. }
  73. /* save it timestamp */
  74. st->it_timestamp = timestamp;
  75. }
  76. /**
  77. * inv_mpu6050_get_timestamp() - Return the current data timestamp
  78. *
  79. * @st: driver state
  80. * @return: current data timestamp
  81. *
  82. * This function returns the current data timestamp and prepares for next one.
  83. */
  84. static s64 inv_mpu6050_get_timestamp(struct inv_mpu6050_state *st)
  85. {
  86. s64 ts;
  87. /* return current data timestamp and increment */
  88. ts = st->data_timestamp;
  89. st->data_timestamp += st->chip_period * INV_MPU6050_FREQ_DIVIDER(st);
  90. return ts;
  91. }
  92. int inv_reset_fifo(struct iio_dev *indio_dev)
  93. {
  94. int result;
  95. u8 d;
  96. struct inv_mpu6050_state *st = iio_priv(indio_dev);
  97. /* reset it timestamp validation */
  98. st->it_timestamp = 0;
  99. /* disable interrupt */
  100. result = regmap_write(st->map, st->reg->int_enable, 0);
  101. if (result) {
  102. dev_err(regmap_get_device(st->map), "int_enable failed %d\n",
  103. result);
  104. return result;
  105. }
  106. /* disable the sensor output to FIFO */
  107. result = regmap_write(st->map, st->reg->fifo_en, 0);
  108. if (result)
  109. goto reset_fifo_fail;
  110. /* disable fifo reading */
  111. result = regmap_write(st->map, st->reg->user_ctrl,
  112. st->chip_config.user_ctrl);
  113. if (result)
  114. goto reset_fifo_fail;
  115. /* reset FIFO*/
  116. d = st->chip_config.user_ctrl | INV_MPU6050_BIT_FIFO_RST;
  117. result = regmap_write(st->map, st->reg->user_ctrl, d);
  118. if (result)
  119. goto reset_fifo_fail;
  120. /* enable interrupt */
  121. if (st->chip_config.accl_fifo_enable ||
  122. st->chip_config.gyro_fifo_enable) {
  123. result = regmap_write(st->map, st->reg->int_enable,
  124. INV_MPU6050_BIT_DATA_RDY_EN);
  125. if (result)
  126. return result;
  127. }
  128. /* enable FIFO reading */
  129. d = st->chip_config.user_ctrl | INV_MPU6050_BIT_FIFO_EN;
  130. result = regmap_write(st->map, st->reg->user_ctrl, d);
  131. if (result)
  132. goto reset_fifo_fail;
  133. /* enable sensor output to FIFO */
  134. d = 0;
  135. if (st->chip_config.gyro_fifo_enable)
  136. d |= INV_MPU6050_BITS_GYRO_OUT;
  137. if (st->chip_config.accl_fifo_enable)
  138. d |= INV_MPU6050_BIT_ACCEL_OUT;
  139. result = regmap_write(st->map, st->reg->fifo_en, d);
  140. if (result)
  141. goto reset_fifo_fail;
  142. return 0;
  143. reset_fifo_fail:
  144. dev_err(regmap_get_device(st->map), "reset fifo failed %d\n", result);
  145. result = regmap_write(st->map, st->reg->int_enable,
  146. INV_MPU6050_BIT_DATA_RDY_EN);
  147. return result;
  148. }
  149. /**
  150. * inv_mpu6050_read_fifo() - Transfer data from hardware FIFO to KFIFO.
  151. */
  152. irqreturn_t inv_mpu6050_read_fifo(int irq, void *p)
  153. {
  154. struct iio_poll_func *pf = p;
  155. struct iio_dev *indio_dev = pf->indio_dev;
  156. struct inv_mpu6050_state *st = iio_priv(indio_dev);
  157. size_t bytes_per_datum;
  158. int result;
  159. u8 data[INV_MPU6050_OUTPUT_DATA_SIZE];
  160. u16 fifo_count;
  161. s64 timestamp;
  162. int int_status;
  163. size_t i, nb;
  164. mutex_lock(&st->lock);
  165. /* ack interrupt and check status */
  166. result = regmap_read(st->map, st->reg->int_status, &int_status);
  167. if (result) {
  168. dev_err(regmap_get_device(st->map),
  169. "failed to ack interrupt\n");
  170. goto flush_fifo;
  171. }
  172. if (!(int_status & INV_MPU6050_BIT_RAW_DATA_RDY_INT)) {
  173. dev_warn(regmap_get_device(st->map),
  174. "spurious interrupt with status 0x%x\n", int_status);
  175. goto end_session;
  176. }
  177. if (!(st->chip_config.accl_fifo_enable |
  178. st->chip_config.gyro_fifo_enable))
  179. goto end_session;
  180. bytes_per_datum = 0;
  181. if (st->chip_config.accl_fifo_enable)
  182. bytes_per_datum += INV_MPU6050_BYTES_PER_3AXIS_SENSOR;
  183. if (st->chip_config.gyro_fifo_enable)
  184. bytes_per_datum += INV_MPU6050_BYTES_PER_3AXIS_SENSOR;
  185. if (st->chip_type == INV_ICM20602)
  186. bytes_per_datum += INV_ICM20602_BYTES_PER_TEMP_SENSOR;
  187. /*
  188. * read fifo_count register to know how many bytes are inside the FIFO
  189. * right now
  190. */
  191. result = regmap_bulk_read(st->map, st->reg->fifo_count_h, data,
  192. INV_MPU6050_FIFO_COUNT_BYTE);
  193. if (result)
  194. goto end_session;
  195. fifo_count = get_unaligned_be16(&data[0]);
  196. /*
  197. * Handle fifo overflow by resetting fifo.
  198. * Reset if there is only 3 data set free remaining to mitigate
  199. * possible delay between reading fifo count and fifo data.
  200. */
  201. nb = 3 * bytes_per_datum;
  202. if (fifo_count >= st->hw->fifo_size - nb) {
  203. dev_warn(regmap_get_device(st->map), "fifo overflow reset\n");
  204. goto flush_fifo;
  205. }
  206. /* compute and process all complete datum */
  207. nb = fifo_count / bytes_per_datum;
  208. inv_mpu6050_update_period(st, pf->timestamp, nb);
  209. for (i = 0; i < nb; ++i) {
  210. result = regmap_bulk_read(st->map, st->reg->fifo_r_w,
  211. data, bytes_per_datum);
  212. if (result)
  213. goto flush_fifo;
  214. /* skip first samples if needed */
  215. if (st->skip_samples) {
  216. st->skip_samples--;
  217. continue;
  218. }
  219. timestamp = inv_mpu6050_get_timestamp(st);
  220. iio_push_to_buffers_with_timestamp(indio_dev, data, timestamp);
  221. }
  222. end_session:
  223. mutex_unlock(&st->lock);
  224. iio_trigger_notify_done(indio_dev->trig);
  225. return IRQ_HANDLED;
  226. flush_fifo:
  227. /* Flush HW and SW FIFOs. */
  228. inv_reset_fifo(indio_dev);
  229. mutex_unlock(&st->lock);
  230. iio_trigger_notify_done(indio_dev->trig);
  231. return IRQ_HANDLED;
  232. }