treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / iio / imu / inv_mpu6050 / inv_mpu_ring.c
blobf9fdf4302a91d7e2f35ff3298629ed1234404ebc
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2012 Invensense, Inc.
4 */
6 #include <linux/module.h>
7 #include <linux/slab.h>
8 #include <linux/err.h>
9 #include <linux/delay.h>
10 #include <linux/sysfs.h>
11 #include <linux/jiffies.h>
12 #include <linux/irq.h>
13 #include <linux/interrupt.h>
14 #include <linux/poll.h>
15 #include <linux/math64.h>
16 #include <asm/unaligned.h>
17 #include "inv_mpu_iio.h"
19 /**
20 * inv_mpu6050_update_period() - Update chip internal period estimation
22 * @st: driver state
23 * @timestamp: the interrupt timestamp
24 * @nb: number of data set in the fifo
26 * This function uses interrupt timestamps to estimate the chip period and
27 * to choose the data timestamp to come.
29 static void inv_mpu6050_update_period(struct inv_mpu6050_state *st,
30 s64 timestamp, size_t nb)
32 /* Period boundaries for accepting timestamp */
33 const s64 period_min =
34 (NSEC_PER_MSEC * (100 - INV_MPU6050_TS_PERIOD_JITTER)) / 100;
35 const s64 period_max =
36 (NSEC_PER_MSEC * (100 + INV_MPU6050_TS_PERIOD_JITTER)) / 100;
37 const s32 divider = INV_MPU6050_FREQ_DIVIDER(st);
38 s64 delta, interval;
39 bool use_it_timestamp = false;
41 if (st->it_timestamp == 0) {
42 /* not initialized, forced to use it_timestamp */
43 use_it_timestamp = true;
44 } else if (nb == 1) {
46 * Validate the use of it timestamp by checking if interrupt
47 * has been delayed.
48 * nb > 1 means interrupt was delayed for more than 1 sample,
49 * so it's obviously not good.
50 * Compute the chip period between 2 interrupts for validating.
52 delta = div_s64(timestamp - st->it_timestamp, divider);
53 if (delta > period_min && delta < period_max) {
54 /* update chip period and use it timestamp */
55 st->chip_period = (st->chip_period + delta) / 2;
56 use_it_timestamp = true;
60 if (use_it_timestamp) {
62 * Manage case of multiple samples in the fifo (nb > 1):
63 * compute timestamp corresponding to the first sample using
64 * estimated chip period.
66 interval = (nb - 1) * st->chip_period * divider;
67 st->data_timestamp = timestamp - interval;
70 /* save it timestamp */
71 st->it_timestamp = timestamp;
74 /**
75 * inv_mpu6050_get_timestamp() - Return the current data timestamp
77 * @st: driver state
78 * @return: current data timestamp
80 * This function returns the current data timestamp and prepares for next one.
82 static s64 inv_mpu6050_get_timestamp(struct inv_mpu6050_state *st)
84 s64 ts;
86 /* return current data timestamp and increment */
87 ts = st->data_timestamp;
88 st->data_timestamp += st->chip_period * INV_MPU6050_FREQ_DIVIDER(st);
90 return ts;
93 int inv_reset_fifo(struct iio_dev *indio_dev)
95 int result;
96 u8 d;
97 struct inv_mpu6050_state *st = iio_priv(indio_dev);
99 /* reset it timestamp validation */
100 st->it_timestamp = 0;
102 /* disable interrupt */
103 result = regmap_write(st->map, st->reg->int_enable, 0);
104 if (result) {
105 dev_err(regmap_get_device(st->map), "int_enable failed %d\n",
106 result);
107 return result;
109 /* disable the sensor output to FIFO */
110 result = regmap_write(st->map, st->reg->fifo_en, 0);
111 if (result)
112 goto reset_fifo_fail;
113 /* disable fifo reading */
114 result = regmap_write(st->map, st->reg->user_ctrl,
115 st->chip_config.user_ctrl);
116 if (result)
117 goto reset_fifo_fail;
119 /* reset FIFO*/
120 d = st->chip_config.user_ctrl | INV_MPU6050_BIT_FIFO_RST;
121 result = regmap_write(st->map, st->reg->user_ctrl, d);
122 if (result)
123 goto reset_fifo_fail;
125 /* enable interrupt */
126 if (st->chip_config.accl_fifo_enable ||
127 st->chip_config.gyro_fifo_enable ||
128 st->chip_config.magn_fifo_enable) {
129 result = regmap_write(st->map, st->reg->int_enable,
130 INV_MPU6050_BIT_DATA_RDY_EN);
131 if (result)
132 return result;
134 /* enable FIFO reading */
135 d = st->chip_config.user_ctrl | INV_MPU6050_BIT_FIFO_EN;
136 result = regmap_write(st->map, st->reg->user_ctrl, d);
137 if (result)
138 goto reset_fifo_fail;
139 /* enable sensor output to FIFO */
140 d = 0;
141 if (st->chip_config.gyro_fifo_enable)
142 d |= INV_MPU6050_BITS_GYRO_OUT;
143 if (st->chip_config.accl_fifo_enable)
144 d |= INV_MPU6050_BIT_ACCEL_OUT;
145 if (st->chip_config.temp_fifo_enable)
146 d |= INV_MPU6050_BIT_TEMP_OUT;
147 if (st->chip_config.magn_fifo_enable)
148 d |= INV_MPU6050_BIT_SLAVE_0;
149 result = regmap_write(st->map, st->reg->fifo_en, d);
150 if (result)
151 goto reset_fifo_fail;
153 return 0;
155 reset_fifo_fail:
156 dev_err(regmap_get_device(st->map), "reset fifo failed %d\n", result);
157 result = regmap_write(st->map, st->reg->int_enable,
158 INV_MPU6050_BIT_DATA_RDY_EN);
160 return result;
164 * inv_mpu6050_read_fifo() - Transfer data from hardware FIFO to KFIFO.
166 irqreturn_t inv_mpu6050_read_fifo(int irq, void *p)
168 struct iio_poll_func *pf = p;
169 struct iio_dev *indio_dev = pf->indio_dev;
170 struct inv_mpu6050_state *st = iio_priv(indio_dev);
171 size_t bytes_per_datum;
172 int result;
173 u8 data[INV_MPU6050_OUTPUT_DATA_SIZE];
174 u16 fifo_count;
175 s64 timestamp;
176 int int_status;
177 size_t i, nb;
179 mutex_lock(&st->lock);
181 /* ack interrupt and check status */
182 result = regmap_read(st->map, st->reg->int_status, &int_status);
183 if (result) {
184 dev_err(regmap_get_device(st->map),
185 "failed to ack interrupt\n");
186 goto flush_fifo;
188 if (!(int_status & INV_MPU6050_BIT_RAW_DATA_RDY_INT))
189 goto end_session;
191 if (!(st->chip_config.accl_fifo_enable |
192 st->chip_config.gyro_fifo_enable |
193 st->chip_config.magn_fifo_enable))
194 goto end_session;
195 bytes_per_datum = 0;
196 if (st->chip_config.accl_fifo_enable)
197 bytes_per_datum += INV_MPU6050_BYTES_PER_3AXIS_SENSOR;
199 if (st->chip_config.gyro_fifo_enable)
200 bytes_per_datum += INV_MPU6050_BYTES_PER_3AXIS_SENSOR;
202 if (st->chip_config.temp_fifo_enable)
203 bytes_per_datum += INV_MPU6050_BYTES_PER_TEMP_SENSOR;
205 if (st->chip_config.magn_fifo_enable)
206 bytes_per_datum += INV_MPU9X50_BYTES_MAGN;
209 * read fifo_count register to know how many bytes are inside the FIFO
210 * right now
212 result = regmap_bulk_read(st->map, st->reg->fifo_count_h, data,
213 INV_MPU6050_FIFO_COUNT_BYTE);
214 if (result)
215 goto end_session;
216 fifo_count = get_unaligned_be16(&data[0]);
219 * Handle fifo overflow by resetting fifo.
220 * Reset if there is only 3 data set free remaining to mitigate
221 * possible delay between reading fifo count and fifo data.
223 nb = 3 * bytes_per_datum;
224 if (fifo_count >= st->hw->fifo_size - nb) {
225 dev_warn(regmap_get_device(st->map), "fifo overflow reset\n");
226 goto flush_fifo;
229 /* compute and process all complete datum */
230 nb = fifo_count / bytes_per_datum;
231 inv_mpu6050_update_period(st, pf->timestamp, nb);
232 for (i = 0; i < nb; ++i) {
233 result = regmap_bulk_read(st->map, st->reg->fifo_r_w,
234 data, bytes_per_datum);
235 if (result)
236 goto flush_fifo;
237 /* skip first samples if needed */
238 if (st->skip_samples) {
239 st->skip_samples--;
240 continue;
242 timestamp = inv_mpu6050_get_timestamp(st);
243 iio_push_to_buffers_with_timestamp(indio_dev, data, timestamp);
246 end_session:
247 mutex_unlock(&st->lock);
248 iio_trigger_notify_done(indio_dev->trig);
250 return IRQ_HANDLED;
252 flush_fifo:
253 /* Flush HW and SW FIFOs. */
254 inv_reset_fifo(indio_dev);
255 mutex_unlock(&st->lock);
256 iio_trigger_notify_done(indio_dev->trig);
258 return IRQ_HANDLED;