2 * Copyright (C) 2012 Invensense, Inc.
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.
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.
17 * @brief Hardware drivers.
20 * @file inv_mpu_ring.c
21 * @brief A sysfs device driver for Invensense gyroscopes.
22 * @details This file is part of inv mpu iio driver code
25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <linux/i2c.h>
31 #include <linux/err.h>
32 #include <linux/delay.h>
33 #include <linux/sysfs.h>
34 #include <linux/jiffies.h>
35 #include <linux/irq.h>
36 #include <linux/interrupt.h>
37 #include <linux/kfifo.h>
38 #include <linux/poll.h>
39 #include <linux/miscdevice.h>
40 #ifdef INV_KERNEL_3_10
41 #include <linux/iio/iio.h>
42 #include <linux/iio/kfifo_buf.h>
43 #include <linux/iio/trigger_consumer.h>
44 #include <linux/iio/sysfs.h>
47 #include "kfifo_buf.h"
48 #include "trigger_consumer.h"
52 #include "inv_mpu_iio.h"
55 * reset_fifo_mpu3050() - Reset FIFO related registers
56 * @st: Device driver instance.
58 static int reset_fifo_mpu3050(struct iio_dev *indio_dev)
60 struct inv_reg_map_s *reg;
63 struct inv_mpu_iio_s *st = iio_priv(indio_dev);
66 /* disable interrupt */
67 result = inv_plat_single_write(st, reg->int_enable,
68 st->plat_data.int_config);
71 /* disable the sensor output to FIFO */
72 result = inv_plat_single_write(st, reg->fifo_en, 0);
75 result = inv_plat_read(st, reg->user_ctrl, 1, &user_ctrl);
78 /* disable fifo reading */
79 user_ctrl &= ~BIT_FIFO_EN;
80 st->chip_config.has_footer = 0;
82 val = (BIT_3050_FIFO_RST | user_ctrl);
83 result = inv_plat_single_write(st, reg->user_ctrl, val | st->i2c_dis);
86 st->last_isr_time = get_time_ns();
87 if (st->chip_config.dmp_on) {
88 /* enable interrupt when DMP is done */
89 result = inv_plat_single_write(st, reg->int_enable,
90 st->plat_data.int_config | BIT_DMP_INT_EN);
94 result = inv_plat_single_write(st, reg->user_ctrl,
95 BIT_FIFO_EN | user_ctrl | st->i2c_dis);
99 /* enable interrupt */
100 if (st->chip_config.accl_fifo_enable ||
101 st->chip_config.gyro_fifo_enable) {
102 result = inv_plat_single_write(st, reg->int_enable,
103 st->plat_data.int_config | BIT_DATA_RDY_EN);
107 /* enable FIFO reading and I2C master interface*/
108 result = inv_plat_single_write(st, reg->user_ctrl,
109 BIT_FIFO_EN | user_ctrl | st->i2c_dis);
112 /* enable sensor output to FIFO and FIFO footer*/
114 if (st->chip_config.accl_fifo_enable)
115 val |= BITS_3050_ACCL_OUT;
116 if (st->chip_config.gyro_fifo_enable)
117 val |= BITS_GYRO_OUT;
118 result = inv_plat_single_write(st, reg->fifo_en, val);
125 if (st->chip_config.dmp_on)
126 val = BIT_DMP_INT_EN;
128 val = BIT_DATA_RDY_EN;
129 inv_plat_single_write(st, reg->int_enable,
130 st->plat_data.int_config | val);
131 pr_err("reset fifo failed\n");
137 * inv_set_lpf() - set low pass filer based on fifo rate.
139 static int inv_set_lpf(struct inv_mpu_iio_s *st, int rate)
141 const short hz[] = {188, 98, 42, 20, 10, 5};
142 const int d[] = {INV_FILTER_188HZ, INV_FILTER_98HZ,
143 INV_FILTER_42HZ, INV_FILTER_20HZ,
144 INV_FILTER_10HZ, INV_FILTER_5HZ};
145 int i, h, data, result;
146 struct inv_reg_map_s *reg;
150 while ((h < hz[i]) && (i < ARRAY_SIZE(d) - 1))
153 if (INV_MPU3050 == st->chip_type) {
154 if (st->mpu_slave != NULL) {
155 result = st->mpu_slave->set_lpf(st, rate);
159 result = inv_plat_single_write(st, reg->lpf, data |
160 (st->chip_config.fsr << GYRO_CONFIG_FSR_SHIFT));
162 result = inv_plat_single_write(st, reg->lpf, data);
166 st->chip_config.lpf = data;
172 * set_fifo_rate_reg() - Set fifo rate in hardware register
174 static int set_fifo_rate_reg(struct inv_mpu_iio_s *st)
179 struct inv_reg_map_s *reg;
182 fifo_rate = st->chip_config.new_fifo_rate;
183 data = ONE_K_HZ / fifo_rate - 1;
184 result = inv_plat_single_write(st, reg->sample_rate_div, data);
187 result = inv_set_lpf(st, fifo_rate);
190 st->chip_config.fifo_rate = fifo_rate;
196 * inv_lpa_mode() - store current low power mode settings
198 static int inv_lpa_mode(struct inv_mpu_iio_s *st, int lpa_mode)
200 unsigned long result;
202 struct inv_reg_map_s *reg;
205 result = inv_plat_read(st, reg->pwr_mgmt_1, 1, &d);
213 result = inv_plat_single_write(st, reg->pwr_mgmt_1, d);
216 if (INV_MPU6500 == st->chip_type) {
218 d = BIT_ACCEL_FCHOCIE_B;
221 result = inv_plat_single_write(st, REG_6500_ACCEL_CONFIG2, d);
230 * reset_fifo_itg() - Reset FIFO related registers.
231 * @st: Device driver instance.
233 static int reset_fifo_itg(struct iio_dev *indio_dev)
235 struct inv_reg_map_s *reg;
238 struct inv_mpu_iio_s *st = iio_priv(indio_dev);
241 if (st->chip_config.lpa_mode) {
242 result = inv_lpa_mode(st, 0);
244 pr_err("reset lpa mode failed\n");
248 /* disable interrupt */
249 result = inv_plat_single_write(st, reg->int_enable, 0);
251 pr_err("int_enable write failed\n");
254 /* disable the sensor output to FIFO */
255 result = inv_plat_single_write(st, reg->fifo_en, 0);
257 goto reset_fifo_fail;
258 /* disable fifo reading */
259 result = inv_plat_single_write(st, reg->user_ctrl, st->i2c_dis);
261 goto reset_fifo_fail;
264 /* MPU6500's BIT_6500_WOM_EN is the same as BIT_MOT_EN */
265 if (st->mot_int.mot_on)
266 int_word |= BIT_MOT_EN;
268 if (st->chip_config.dmp_on) {
269 val = (BIT_FIFO_RST | BIT_DMP_RST);
270 result = inv_plat_single_write(st, reg->user_ctrl, val | st->i2c_dis);
272 goto reset_fifo_fail;
273 st->last_isr_time = get_time_ns();
274 if (st->chip_config.dmp_int_on) {
275 int_word |= BIT_DMP_INT_EN;
276 result = inv_plat_single_write(st, reg->int_enable,
281 val = (BIT_DMP_EN | BIT_FIFO_EN);
282 if (st->chip_config.compass_enable &
283 (!st->chip_config.dmp_event_int_on))
284 val |= BIT_I2C_MST_EN;
285 result = inv_plat_single_write(st, reg->user_ctrl, val | st->i2c_dis);
287 goto reset_fifo_fail;
289 if (st->chip_config.compass_enable) {
290 /* I2C_MST_DLY is set according to sample rate,
291 slow down the power*/
292 data = max(COMPASS_RATE_SCALE *
293 st->chip_config.new_fifo_rate / ONE_K_HZ,
294 st->chip_config.new_fifo_rate /
295 st->chip_config.dmp_output_rate);
298 result = inv_plat_single_write(st, REG_I2C_SLV4_CTRL,
304 if (st->chip_config.accl_fifo_enable)
305 val |= INV_ACCL_MASK;
306 if (st->chip_config.gyro_fifo_enable)
307 val |= INV_GYRO_MASK;
308 result = inv_send_sensor_data(st, val);
311 if (st->chip_config.display_orient_on || st->chip_config.tap_on)
312 result = inv_send_interrupt_word(st, true);
314 result = inv_send_interrupt_word(st, false);
316 /* reset FIFO and possibly reset I2C*/
318 result = inv_plat_single_write(st, reg->user_ctrl, val | st->i2c_dis);
320 goto reset_fifo_fail;
321 st->last_isr_time = get_time_ns();
322 /* enable interrupt */
323 if (st->chip_config.accl_fifo_enable ||
324 st->chip_config.gyro_fifo_enable ||
325 st->chip_config.compass_enable) {
326 int_word |= BIT_DATA_RDY_EN;
328 result = inv_plat_single_write(st, reg->int_enable, int_word);
331 /* enable FIFO reading and I2C master interface*/
333 if (st->chip_config.compass_enable)
334 val |= BIT_I2C_MST_EN;
335 result = inv_plat_single_write(st, reg->user_ctrl, val | st->i2c_dis);
337 goto reset_fifo_fail;
338 if (st->chip_config.compass_enable) {
339 /* I2C_MST_DLY is set according to sample rate,
340 slow down the power*/
341 data = COMPASS_RATE_SCALE *
342 st->chip_config.new_fifo_rate / ONE_K_HZ;
345 result = inv_plat_single_write(st, REG_I2C_SLV4_CTRL,
350 /* enable sensor output to FIFO */
352 if (st->chip_config.gyro_fifo_enable)
353 val |= BITS_GYRO_OUT;
354 if (st->chip_config.accl_fifo_enable)
355 val |= BIT_ACCEL_OUT;
356 result = inv_plat_single_write(st, reg->fifo_en, val);
358 goto reset_fifo_fail;
360 st->chip_config.normal_compass_measure = 0;
361 result = inv_lpa_mode(st, st->chip_config.lpa_mode);
363 goto reset_fifo_fail;
368 if (st->chip_config.dmp_on)
369 val = BIT_DMP_INT_EN;
371 val = BIT_DATA_RDY_EN;
372 inv_plat_single_write(st, reg->int_enable, val);
373 pr_err("reset fifo failed\n");
379 * inv_clear_kfifo() - clear time stamp fifo
380 * @st: Device driver instance.
382 static void inv_clear_kfifo(struct inv_mpu_iio_s *st)
386 spin_lock_irqsave(&st->time_stamp_lock, flags);
387 kfifo_reset(&st->timestamps);
388 spin_unlock_irqrestore(&st->time_stamp_lock, flags);
392 * inv_reset_fifo() - Reset FIFO related registers.
393 * @st: Device driver instance.
395 static int inv_reset_fifo(struct iio_dev *indio_dev)
397 struct inv_mpu_iio_s *st = iio_priv(indio_dev);
400 if (INV_MPU3050 == st->chip_type)
401 return reset_fifo_mpu3050(indio_dev);
403 return reset_fifo_itg(indio_dev);
406 static int inv_set_dmp_sysfs(struct inv_mpu_iio_s *st)
410 result = inv_set_fifo_rate(st, st->chip_config.dmp_output_rate);
413 result = inv_set_interrupt_on_gesture_event(st,
414 st->chip_config.dmp_event_int_on);
420 * set_inv_enable() - Reset FIFO related registers.
421 * This also powers on the chip if needed.
422 * @st: Device driver instance.
423 * @fifo_enable: enable/disable
425 static int set_inv_enable(struct iio_dev *indio_dev,
427 struct inv_mpu_iio_s *st = iio_priv(indio_dev);
428 struct inv_reg_map_s *reg;
433 if (st->chip_config.new_fifo_rate !=
434 st->chip_config.fifo_rate) {
435 result = set_fifo_rate_reg(st);
439 if (st->chip_config.dmp_on) {
440 result = inv_set_dmp_sysfs(st);
445 if (st->chip_config.gyro_enable) {
446 result = st->switch_gyro_engine(st, true);
450 if (st->chip_config.accl_enable) {
451 result = st->switch_accl_engine(st, true);
456 result = inv_reset_fifo(indio_dev);
460 if ((INV_MPU3050 != st->chip_type)
461 && st->chip_config.lpa_mode) {
462 /* if the chip is in low power mode,
463 register write/read could fail */
464 result = inv_lpa_mode(st, 0);
468 result = inv_plat_single_write(st, reg->fifo_en, 0);
471 /* disable fifo reading */
472 if (INV_MPU3050 != st->chip_type) {
473 result = inv_plat_single_write(st, reg->int_enable, 0);
476 result = inv_plat_single_write(st, reg->user_ctrl, st->i2c_dis);
478 result = inv_plat_single_write(st, reg->int_enable,
479 st->plat_data.int_config);
483 /* turn off the gyro/accl engine during disable phase */
484 result = st->switch_gyro_engine(st, false);
487 result = st->switch_accl_engine(st, false);
490 result = st->set_power_state(st, false);
494 st->chip_config.enable = enable;
499 /* #define _RATE_DEBUG */
502 static s64 tm_irq_min = -1;
503 static s64 tm_irq_max = -1;
504 static s64 tm_irq_sum = -1;
505 static s64 tm_irq_last_print = -1;
506 static s64 tm_irq_count = -1;
510 * inv_irq_handler() - Cache a timestamp at each data ready interrupt.
512 static irqreturn_t inv_irq_handler(int irq, void *dev_id)
514 struct inv_mpu_iio_s *st;
517 u64 time_since_last_irq;
519 st = (struct inv_mpu_iio_s *)dev_id;
520 timestamp = get_time_ns();
521 time_since_last_irq = timestamp - st->last_isr_time;
522 spin_lock(&st->time_stamp_lock);
525 if (tm_irq_min <= 0 && tm_irq_max <= 0) {
526 tm_irq_min = time_since_last_irq;
527 tm_irq_max = time_since_last_irq;
530 } else if (time_since_last_irq < tm_irq_min) {
531 tm_irq_min = time_since_last_irq;
532 } else if (time_since_last_irq > tm_irq_max) {
533 tm_irq_max = time_since_last_irq;
535 tm_irq_sum += time_since_last_irq;
538 if (unlikely((timestamp - tm_irq_last_print) >= 1000000000)) {
539 pr_info("MPU Interrupt: %lld,%d,%lld\n", tm_irq_min, (u32)tm_irq_sum / (u32)tm_irq_count, tm_irq_max);
540 tm_irq_last_print = timestamp;
549 while ((time_since_last_irq > st->irq_dur_ns * 2) &&
550 (catch_up < MAX_CATCH_UP) &&
551 (!st->chip_config.lpa_mode) &&
552 (!st->chip_config.dmp_on)) {
553 st->last_isr_time += st->irq_dur_ns;
554 kfifo_in(&st->timestamps,
555 &st->last_isr_time, 1);
556 time_since_last_irq = timestamp - st->last_isr_time;
559 kfifo_in(&st->timestamps, ×tamp, 1);
560 st->last_isr_time = timestamp;
561 spin_unlock(&st->time_stamp_lock);
563 return IRQ_WAKE_THREAD;
566 static int put_scan_to_buf(struct iio_dev *indio_dev, u8 *d,
567 short *s, int scan_index, int d_ind) {
568 struct iio_buffer *ring = indio_dev->buffer;
571 for (i = 0; i < 3; i++) {
572 st = iio_scan_mask_query(indio_dev, ring, scan_index + i);
574 memcpy(&d[d_ind], &s[i], sizeof(s[i]));
575 d_ind += sizeof(s[i]);
581 static void inv_report_data_3050(struct iio_dev *indio_dev, s64 t,
582 int has_footer, u8 *data)
584 struct inv_mpu_iio_s *st = iio_priv(indio_dev);
585 struct iio_buffer *ring = indio_dev->buffer;
587 struct inv_chip_config_s *conf;
588 short g[THREE_AXIS], a[THREE_AXIS];
591 int bytes_per_datum, scan_count;
592 conf = &st->chip_config;
594 scan_count = bitmap_weight(indio_dev->active_scan_mask,
595 indio_dev->masklength);
596 bytes_per_datum = scan_count * 2;
604 if (conf->gyro_fifo_enable) {
605 for (i = 0; i < ARRAY_SIZE(g); i++) {
606 g[i] = be16_to_cpup((__be16 *)(&data[ind + i * 2]));
607 st->raw_gyro[i] = g[i];
609 ind += BYTES_PER_SENSOR;
611 if (conf->accl_fifo_enable) {
612 st->mpu_slave->combine_data(&data[ind], a);
613 for (i = 0; i < ARRAY_SIZE(a); i++)
614 st->raw_accel[i] = a[i];
616 ind += BYTES_PER_SENSOR;
617 d_ind = put_scan_to_buf(indio_dev, tmp, a,
618 INV_MPU_SCAN_ACCL_X, d_ind);
620 if (conf->gyro_fifo_enable)
621 d_ind = put_scan_to_buf(indio_dev, tmp, g,
622 INV_MPU_SCAN_GYRO_X, d_ind);
624 i = (bytes_per_datum + 7) / 8;
625 if (ring->scan_timestamp)
627 #ifdef INV_KERNEL_3_10
628 ring->access->store_to(indio_dev->buffer, (u8 *)buf);
630 ring->access->store_to(indio_dev->buffer, (u8 *)buf, t);
635 * inv_read_fifo_mpu3050() - Transfer data from FIFO to ring buffer for
638 irqreturn_t inv_read_fifo_mpu3050(int irq, void *dev_id)
641 struct inv_mpu_iio_s *st = (struct inv_mpu_iio_s *)dev_id;
642 struct iio_dev *indio_dev = iio_priv_to_dev(st);
646 short fifo_count, byte_read;
649 struct inv_reg_map_s *reg;
651 /* It is impossible that chip is asleep or enable is
652 zero when interrupt is on
653 * because interrupt is now connected with enable */
654 if (st->chip_config.dmp_on)
655 bytes_per_datum = BYTES_FOR_DMP;
657 bytes_per_datum = (st->chip_config.accl_fifo_enable +
658 st->chip_config.gyro_fifo_enable)*BYTES_PER_SENSOR;
659 if (st->chip_config.has_footer)
660 byte_read = bytes_per_datum + MPU3050_FOOTER_SIZE;
662 byte_read = bytes_per_datum;
665 if (byte_read != 0) {
666 result = inv_plat_read(st, reg->fifo_count_h,
667 FIFO_COUNT_BYTE, data);
670 fifo_count = be16_to_cpup((__be16 *)(&data[0]));
671 if (fifo_count < byte_read)
675 if (fifo_count > FIFO_THRESHOLD)
677 /* Timestamp mismatch. */
678 if (kfifo_len(&st->timestamps) <
679 fifo_count / byte_read)
681 if (kfifo_len(&st->timestamps) >
682 fifo_count / byte_read + TIME_STAMP_TOR) {
683 if (st->chip_config.dmp_on) {
684 result = kfifo_to_user(&st->timestamps,
685 ×tamp, sizeof(timestamp), &copied);
693 while ((bytes_per_datum != 0) && (fifo_count >= byte_read)) {
694 result = inv_plat_read(st, reg->fifo_r_w, byte_read, data);
698 result = kfifo_to_user(&st->timestamps,
699 ×tamp, sizeof(timestamp), &copied);
702 inv_report_data_3050(indio_dev, timestamp,
703 st->chip_config.has_footer, data);
704 fifo_count -= byte_read;
705 if (st->chip_config.has_footer == 0) {
706 st->chip_config.has_footer = 1;
707 byte_read = bytes_per_datum + MPU3050_FOOTER_SIZE;
715 /* Flush HW and SW FIFOs. */
716 inv_reset_fifo(indio_dev);
721 static int inv_report_gyro_accl_compass(struct iio_dev *indio_dev,
724 struct inv_mpu_iio_s *st = iio_priv(indio_dev);
725 short g[THREE_AXIS], a[THREE_AXIS], c[THREE_AXIS];
729 u8 d[8], compass_divider;
733 struct inv_chip_config_s *conf;
735 conf = &st->chip_config;
738 if (conf->quaternion_on & conf->dmp_on) {
739 for (i = 0; i < ARRAY_SIZE(q); i++) {
740 q[i] = be32_to_cpup((__be32 *)(&data[ind + i * 4]));
741 st->raw_quaternion[i] = q[i];
742 memcpy(&buf[ind + i * sizeof(q[i])], &q[i],
745 ind += QUATERNION_BYTES;
748 if (conf->accl_fifo_enable) {
749 for (i = 0; i < ARRAY_SIZE(a); i++) {
750 a[i] = be16_to_cpup((__be16 *)(&data[ind + i * 2]));
751 memcpy(&buf[ind + i * sizeof(a[i])], &a[i],
754 ind += BYTES_PER_SENSOR;
757 if (conf->gyro_fifo_enable) {
758 for (i = 0; i < ARRAY_SIZE(g); i++) {
759 g[i] = be16_to_cpup((__be16 *)(&data[ind + i * 2]));
760 memcpy(&buf[ind + i * sizeof(g[i])], &g[i],
763 ind += BYTES_PER_SENSOR;
766 if (conf->dmp_on && (conf->tap_on || conf->display_orient_on)) {
767 word = (u32)(be32_to_cpup((u32 *)&data[ind]));
768 source = ((word >> 16) & 0xff);
770 st->tap_data = (DMP_MASK_TAP & (word & 0xff));
771 st->display_orient_data =
772 ((DMP_MASK_DIS_ORIEN & (word & 0xff)) >>
773 DMP_DIS_ORIEN_SHIFT);
776 /* report tap information */
777 if (source & INT_SRC_TAP)
778 sysfs_notify(&indio_dev->dev.kobj, NULL, "event_tap");
779 /* report orientation information */
780 if (source & INT_SRC_DISPLAY_ORIENT)
781 sysfs_notify(&indio_dev->dev.kobj, NULL,
782 "event_display_orientation");
784 /*divider and counter is used to decrease the speed of read in
785 high frequency sample rate*/
786 if (conf->compass_fifo_enable) {
791 compass_divider = st->compass_dmp_divider;
793 compass_divider = st->compass_divider;
794 if (compass_divider <= st->compass_counter) {
795 /*read from external sensor data register */
796 result = inv_plat_read(st, REG_EXT_SENS_DATA_00,
797 NUM_BYTES_COMPASS_SLAVE, d);
798 /* d[7] is status 2 register */
799 /*for AKM8975, bit 2 and 3 should be all be zero*/
800 /* for AMK8963, bit 3 should be zero*/
801 if ((DATA_AKM_DRDY == d[0]) &&
802 (0 == (d[7] & DATA_AKM_STAT_MASK)) &&
805 sens = st->chip_info.compass_sens;
806 c[0] = (short)((d[2] << 8) | d[1]);
807 c[1] = (short)((d[4] << 8) | d[3]);
808 c[2] = (short)((d[6] << 8) | d[5]);
809 c[0] = (short)(((int)c[0] *
810 (sens[0] + 128)) >> 8);
811 c[1] = (short)(((int)c[1] *
812 (sens[1] + 128)) >> 8);
813 c[2] = (short)(((int)c[2] *
814 (sens[2] + 128)) >> 8);
815 st->raw_compass[0] = c[0];
816 st->raw_compass[1] = c[1];
817 st->raw_compass[2] = c[2];
819 st->compass_counter = 0;
820 } else if (compass_divider != 0) {
821 st->compass_counter++;
823 if (!conf->normal_compass_measure) {
827 conf->normal_compass_measure = 1;
829 for (i = 0; i < 3; i++)
830 memcpy(&buf[ind + i * sizeof(c[i])], &c[i],
832 ind += BYTES_PER_SENSOR;
835 tmp[DIV_ROUND_UP(ind, 8)] = t;
838 #ifdef INV_KERNEL_3_10
839 iio_push_to_buffers(indio_dev, buf);
841 iio_push_to_buffer(indio_dev->buffer, buf, t);
848 static void inv_process_motion(struct inv_mpu_iio_s *st)
850 struct iio_dev *indio_dev = iio_priv_to_dev(st);
851 s32 diff, true_motion;
856 /* motion interrupt */
857 result = inv_plat_read(st, REG_INT_STATUS, 1, data);
861 if (data[0] & BIT_MOT_INT) {
862 timestamp = get_time_ns();
863 diff = (int)(((timestamp - st->mpu6500_last_motion_time) >>
865 if (diff > st->mot_int.mot_dur) {
866 st->mpu6500_last_motion_time = timestamp;
872 sysfs_notify(&indio_dev->dev.kobj, NULL,
873 "event_accel_motion");
877 static int get_bytes_per_datum(struct inv_mpu_iio_s *st)
882 if (st->chip_config.dmp_on) {
883 if (st->chip_config.quaternion_on)
884 bytes_per_datum += QUATERNION_BYTES;
885 if (st->chip_config.tap_on ||
886 st->chip_config.display_orient_on)
887 bytes_per_datum += BYTES_FOR_EVENTS;
889 if (st->chip_config.accl_fifo_enable)
890 bytes_per_datum += BYTES_PER_SENSOR;
891 if (st->chip_config.gyro_fifo_enable)
892 bytes_per_datum += BYTES_PER_SENSOR;
894 return bytes_per_datum;
898 static s64 tm_end_min = -1;
899 static s64 tm_end_max = -1;
900 static s64 tm_end_sum = -1;
901 static s64 tm_end_last_print = -1;
902 static s64 tm_end_count = -1;
905 * inv_read_fifo() - Transfer data from FIFO to ring buffer.
907 irqreturn_t inv_read_fifo(int irq, void *dev_id)
910 struct inv_mpu_iio_s *st = (struct inv_mpu_iio_s *)dev_id;
911 struct iio_dev *indio_dev = iio_priv_to_dev(st);
912 size_t bytes_per_datum;
914 u8 data[BYTES_FOR_DMP + QUATERNION_BYTES];
918 struct inv_reg_map_s *reg;
922 mutex_lock(&indio_dev->mlock);
923 if (!(iio_buffer_enabled(indio_dev)))
927 if (!(st->chip_config.accl_fifo_enable |
928 st->chip_config.gyro_fifo_enable |
929 st->chip_config.dmp_on |
930 st->chip_config.compass_fifo_enable |
933 if (st->mot_int.mot_on)
934 inv_process_motion(st);
935 if (st->chip_config.dmp_on && st->chip_config.smd_enable) {
936 /* dmp interrupt status */
937 result = inv_plat_read(st, REG_DMP_INT_STATUS, 1, data);
939 if (data[0] & SMD_INT_ON) {
940 sysfs_notify(&indio_dev->dev.kobj, NULL,
942 st->chip_config.smd_enable = 0;
945 if (st->chip_config.lpa_mode) {
946 result = inv_plat_read(st, reg->raw_accl,
947 BYTES_PER_SENSOR, data);
950 inv_report_gyro_accl_compass(indio_dev, data,
954 bytes_per_datum = get_bytes_per_datum(st);
956 if (bytes_per_datum != 0) {
957 result = inv_plat_read(st, reg->fifo_count_h,
958 FIFO_COUNT_BYTE, data);
961 fifo_count = be16_to_cpup((__be16 *)(&data[0]));
962 if ((fifo_count == 0) && (kfifo_len(&st->timestamps) > 0))
964 if (fifo_count < bytes_per_datum)
966 /* fifo count can't be odd number */
969 if (fifo_count > FIFO_THRESHOLD)
971 /* timestamp mismatch. */
972 if (kfifo_len(&st->timestamps) <
973 fifo_count / bytes_per_datum)
975 if (kfifo_len(&st->timestamps) >
976 fifo_count / bytes_per_datum + TIME_STAMP_TOR) {
977 if (st->chip_config.dmp_on) {
978 result = kfifo_to_user(&st->timestamps,
979 ×tamp, sizeof(timestamp), &copied);
987 result = kfifo_to_user(&st->timestamps,
988 ×tamp, sizeof(timestamp), &copied);
993 while ((bytes_per_datum != 0) && (fifo_count >= bytes_per_datum)) {
994 result = inv_plat_read(st, reg->fifo_r_w, bytes_per_datum,
999 result = kfifo_to_user(&st->timestamps,
1000 ×tamp, sizeof(timestamp), &copied);
1003 inv_report_gyro_accl_compass(indio_dev, data, timestamp);
1004 fifo_count -= bytes_per_datum;
1006 if (bytes_per_datum == 0 && st->chip_config.compass_fifo_enable)
1007 inv_report_gyro_accl_compass(indio_dev, data, timestamp);
1011 s64 tm_end_cur = get_time_ns();
1012 s64 tm_end_delta = tm_end_cur - timestamp;
1014 if (tm_end_min <= 0 && tm_end_max <= 0) {
1015 tm_end_min = tm_end_delta;
1016 tm_end_max = tm_end_delta;
1019 } else if (tm_end_delta < tm_end_min) {
1020 tm_end_min = tm_end_delta;
1021 } else if (tm_end_delta > tm_end_max) {
1022 tm_end_max = tm_end_delta;
1024 tm_end_sum += tm_end_delta;
1027 if (unlikely((tm_end_cur - tm_end_last_print) >= 1000000000)) {
1028 pr_info("MPU KRNL report rate[%4lld]: %8lld, %8d, %8lld\n",
1029 tm_end_count, tm_end_min, (u32)tm_end_sum / (u32)tm_end_count, tm_end_max);
1030 tm_end_last_print = tm_end_cur;
1040 mutex_unlock(&indio_dev->mlock);
1044 /* Flush HW and SW FIFOs. */
1045 pr_info("fifo_count = %d, fifo_len = %d\n", fifo_count, kfifo_len(&st->timestamps));
1046 inv_reset_fifo(indio_dev);
1047 inv_clear_kfifo(st);
1048 mutex_unlock(&indio_dev->mlock);
1053 void inv_mpu_unconfigure_ring(struct iio_dev *indio_dev)
1055 struct inv_mpu_iio_s *st = iio_priv(indio_dev);
1056 free_irq(st->irq, st);
1057 iio_kfifo_free(indio_dev->buffer);
1060 static int inv_postenable(struct iio_dev *indio_dev)
1062 return set_inv_enable(indio_dev, true);
1065 static int inv_predisable(struct iio_dev *indio_dev)
1067 return set_inv_enable(indio_dev, false);
1070 static void inv_scan_query(struct iio_dev *indio_dev)
1072 struct inv_mpu_iio_s *st = iio_priv(indio_dev);
1073 struct iio_buffer *ring = indio_dev->buffer;
1076 if (iio_scan_mask_query(indio_dev, ring, INV_MPU_SCAN_GYRO_X) ||
1077 iio_scan_mask_query(indio_dev, ring, INV_MPU_SCAN_GYRO_Y) ||
1078 iio_scan_mask_query(indio_dev, ring, INV_MPU_SCAN_GYRO_Z))
1079 st->chip_config.gyro_fifo_enable = 1;
1081 st->chip_config.gyro_fifo_enable = 0;
1083 if (iio_scan_mask_query(indio_dev, ring, INV_MPU_SCAN_ACCL_X) ||
1084 iio_scan_mask_query(indio_dev, ring, INV_MPU_SCAN_ACCL_Y) ||
1085 iio_scan_mask_query(indio_dev, ring, INV_MPU_SCAN_ACCL_Z))
1086 st->chip_config.accl_fifo_enable = 1;
1088 st->chip_config.accl_fifo_enable = 0;
1090 if (iio_scan_mask_query(indio_dev, ring, INV_MPU_SCAN_MAGN_X) ||
1091 iio_scan_mask_query(indio_dev, ring, INV_MPU_SCAN_MAGN_Y) ||
1092 iio_scan_mask_query(indio_dev, ring, INV_MPU_SCAN_MAGN_Z))
1093 st->chip_config.compass_fifo_enable = 1;
1095 st->chip_config.compass_fifo_enable = 0;
1097 /* check to make sure engine is turned on if fifo is turned on */
1098 if (st->chip_config.gyro_fifo_enable &&
1099 (!st->chip_config.gyro_enable)) {
1100 result = st->switch_gyro_engine(st, true);
1103 st->chip_config.gyro_enable = true;
1105 if (st->chip_config.accl_fifo_enable &&
1106 (!st->chip_config.accl_enable)) {
1107 result = st->switch_accl_engine(st, true);
1110 st->chip_config.accl_enable = true;
1114 static int inv_check_quaternion(struct iio_dev *indio_dev)
1116 struct inv_mpu_iio_s *st = iio_priv(indio_dev);
1117 struct iio_buffer *ring = indio_dev->buffer;
1120 if (st->chip_config.dmp_on) {
1122 iio_scan_mask_query(indio_dev, ring, INV_MPU_SCAN_QUAT_R) ||
1123 iio_scan_mask_query(indio_dev, ring, INV_MPU_SCAN_QUAT_X) ||
1124 iio_scan_mask_query(indio_dev, ring, INV_MPU_SCAN_QUAT_Y) ||
1125 iio_scan_mask_query(indio_dev, ring, INV_MPU_SCAN_QUAT_Z))
1126 st->chip_config.quaternion_on = 1;
1128 st->chip_config.quaternion_on = 0;
1130 result = inv_send_quaternion(st,
1131 st->chip_config.quaternion_on);
1135 st->chip_config.quaternion_on = 0;
1136 clear_bit(INV_MPU_SCAN_QUAT_R, ring->scan_mask);
1137 clear_bit(INV_MPU_SCAN_QUAT_X, ring->scan_mask);
1138 clear_bit(INV_MPU_SCAN_QUAT_Y, ring->scan_mask);
1139 clear_bit(INV_MPU_SCAN_QUAT_Z, ring->scan_mask);
1145 static int inv_check_conflict_sysfs(struct iio_dev *indio_dev)
1147 struct inv_mpu_iio_s *st = iio_priv(indio_dev);
1148 struct iio_buffer *ring = indio_dev->buffer;
1151 if (st->chip_config.lpa_mode) {
1152 /* dmp cannot run with low power mode on */
1153 st->chip_config.dmp_on = 0;
1154 result = st->gyro_en(st, ring, false);
1157 result = st->compass_en(st, ring, false);
1160 result = st->quaternion_en(st, ring, false);
1164 result = st->accl_en(st, ring, true);
1168 result = inv_check_quaternion(indio_dev);
1175 static int inv_preenable(struct iio_dev *indio_dev)
1178 struct inv_mpu_iio_s *st = iio_priv(indio_dev);
1180 result = st->set_power_state(st, true);
1184 result = inv_check_conflict_sysfs(indio_dev);
1187 inv_scan_query(indio_dev);
1192 static const struct iio_buffer_setup_ops inv_mpu_ring_setup_ops = {
1193 .preenable = &inv_preenable,
1194 .postenable = &inv_postenable,
1195 .predisable = &inv_predisable,
1198 int inv_mpu_configure_ring(struct iio_dev *indio_dev)
1201 struct inv_mpu_iio_s *st = iio_priv(indio_dev);
1202 struct iio_buffer *ring;
1204 ring = iio_kfifo_allocate();
1207 indio_dev->buffer = ring;
1208 /* setup ring buffer */
1209 ring->scan_timestamp = true;
1210 indio_dev->setup_ops = &inv_mpu_ring_setup_ops;
1211 /*scan count double count timestamp. should subtract 1. but
1212 number of channels still includes timestamp*/
1213 if (INV_MPU3050 == st->chip_type)
1214 ret = request_threaded_irq(st->irq, inv_irq_handler,
1215 inv_read_fifo_mpu3050,
1216 IRQF_TRIGGER_RISING | IRQF_SHARED, "inv_irq", st);
1218 ret = request_threaded_irq(st->irq, inv_irq_handler,
1220 IRQF_TRIGGER_RISING | IRQF_SHARED, "inv_irq", st);
1222 goto error_iio_sw_rb_free;
1224 indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
1226 error_iio_sw_rb_free:
1227 iio_kfifo_free(indio_dev->buffer);