2 * BMG160 Gyro Sensor driver
3 * Copyright (c) 2014, Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 #include <linux/module.h>
16 #include <linux/interrupt.h>
17 #include <linux/delay.h>
18 #include <linux/slab.h>
19 #include <linux/acpi.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/sysfs.h>
24 #include <linux/iio/buffer.h>
25 #include <linux/iio/trigger.h>
26 #include <linux/iio/events.h>
27 #include <linux/iio/trigger_consumer.h>
28 #include <linux/iio/triggered_buffer.h>
29 #include <linux/regmap.h>
32 #define BMG160_IRQ_NAME "bmg160_event"
34 #define BMG160_REG_CHIP_ID 0x00
35 #define BMG160_CHIP_ID_VAL 0x0F
37 #define BMG160_REG_PMU_LPW 0x11
38 #define BMG160_MODE_NORMAL 0x00
39 #define BMG160_MODE_DEEP_SUSPEND 0x20
40 #define BMG160_MODE_SUSPEND 0x80
42 #define BMG160_REG_RANGE 0x0F
44 #define BMG160_RANGE_2000DPS 0
45 #define BMG160_RANGE_1000DPS 1
46 #define BMG160_RANGE_500DPS 2
47 #define BMG160_RANGE_250DPS 3
48 #define BMG160_RANGE_125DPS 4
50 #define BMG160_REG_PMU_BW 0x10
51 #define BMG160_NO_FILTER 0
52 #define BMG160_DEF_BW 100
53 #define BMG160_REG_PMU_BW_RES BIT(7)
55 #define BMG160_GYRO_REG_RESET 0x14
56 #define BMG160_GYRO_RESET_VAL 0xb6
58 #define BMG160_REG_INT_MAP_0 0x17
59 #define BMG160_INT_MAP_0_BIT_ANY BIT(1)
61 #define BMG160_REG_INT_MAP_1 0x18
62 #define BMG160_INT_MAP_1_BIT_NEW_DATA BIT(0)
64 #define BMG160_REG_INT_RST_LATCH 0x21
65 #define BMG160_INT_MODE_LATCH_RESET 0x80
66 #define BMG160_INT_MODE_LATCH_INT 0x0F
67 #define BMG160_INT_MODE_NON_LATCH_INT 0x00
69 #define BMG160_REG_INT_EN_0 0x15
70 #define BMG160_DATA_ENABLE_INT BIT(7)
72 #define BMG160_REG_INT_EN_1 0x16
73 #define BMG160_INT1_BIT_OD BIT(1)
75 #define BMG160_REG_XOUT_L 0x02
76 #define BMG160_AXIS_TO_REG(axis) (BMG160_REG_XOUT_L + (axis * 2))
78 #define BMG160_REG_SLOPE_THRES 0x1B
79 #define BMG160_SLOPE_THRES_MASK 0x0F
81 #define BMG160_REG_MOTION_INTR 0x1C
82 #define BMG160_INT_MOTION_X BIT(0)
83 #define BMG160_INT_MOTION_Y BIT(1)
84 #define BMG160_INT_MOTION_Z BIT(2)
85 #define BMG160_ANY_DUR_MASK 0x30
86 #define BMG160_ANY_DUR_SHIFT 4
88 #define BMG160_REG_INT_STATUS_2 0x0B
89 #define BMG160_ANY_MOTION_MASK 0x07
90 #define BMG160_ANY_MOTION_BIT_X BIT(0)
91 #define BMG160_ANY_MOTION_BIT_Y BIT(1)
92 #define BMG160_ANY_MOTION_BIT_Z BIT(2)
94 #define BMG160_REG_TEMP 0x08
95 #define BMG160_TEMP_CENTER_VAL 23
97 #define BMG160_MAX_STARTUP_TIME_MS 80
99 #define BMG160_AUTO_SUSPEND_DELAY_MS 2000
102 struct regmap
*regmap
;
103 struct iio_trigger
*dready_trig
;
104 struct iio_trigger
*motion_trig
;
110 bool dready_trigger_on
;
111 bool motion_trigger_on
;
122 static const struct {
126 } bmg160_samp_freq_table
[] = { {100, 32, 0x07},
134 static const struct {
137 } bmg160_scale_table
[] = { { 1065, BMG160_RANGE_2000DPS
},
138 { 532, BMG160_RANGE_1000DPS
},
139 { 266, BMG160_RANGE_500DPS
},
140 { 133, BMG160_RANGE_250DPS
},
141 { 66, BMG160_RANGE_125DPS
} };
143 static int bmg160_set_mode(struct bmg160_data
*data
, u8 mode
)
145 struct device
*dev
= regmap_get_device(data
->regmap
);
148 ret
= regmap_write(data
->regmap
, BMG160_REG_PMU_LPW
, mode
);
150 dev_err(dev
, "Error writing reg_pmu_lpw\n");
157 static int bmg160_convert_freq_to_bit(int val
)
161 for (i
= 0; i
< ARRAY_SIZE(bmg160_samp_freq_table
); ++i
) {
162 if (bmg160_samp_freq_table
[i
].odr
== val
)
163 return bmg160_samp_freq_table
[i
].bw_bits
;
169 static int bmg160_set_bw(struct bmg160_data
*data
, int val
)
171 struct device
*dev
= regmap_get_device(data
->regmap
);
175 bw_bits
= bmg160_convert_freq_to_bit(val
);
179 ret
= regmap_write(data
->regmap
, BMG160_REG_PMU_BW
, bw_bits
);
181 dev_err(dev
, "Error writing reg_pmu_bw\n");
188 static int bmg160_get_filter(struct bmg160_data
*data
, int *val
)
190 struct device
*dev
= regmap_get_device(data
->regmap
);
193 unsigned int bw_bits
;
195 ret
= regmap_read(data
->regmap
, BMG160_REG_PMU_BW
, &bw_bits
);
197 dev_err(dev
, "Error reading reg_pmu_bw\n");
201 /* Ignore the readonly reserved bit. */
202 bw_bits
&= ~BMG160_REG_PMU_BW_RES
;
204 for (i
= 0; i
< ARRAY_SIZE(bmg160_samp_freq_table
); ++i
) {
205 if (bmg160_samp_freq_table
[i
].bw_bits
== bw_bits
)
209 *val
= bmg160_samp_freq_table
[i
].filter
;
211 return ret
? ret
: IIO_VAL_INT
;
215 static int bmg160_set_filter(struct bmg160_data
*data
, int val
)
217 struct device
*dev
= regmap_get_device(data
->regmap
);
221 for (i
= 0; i
< ARRAY_SIZE(bmg160_samp_freq_table
); ++i
) {
222 if (bmg160_samp_freq_table
[i
].filter
== val
)
226 ret
= regmap_write(data
->regmap
, BMG160_REG_PMU_BW
,
227 bmg160_samp_freq_table
[i
].bw_bits
);
229 dev_err(dev
, "Error writing reg_pmu_bw\n");
236 static int bmg160_chip_init(struct bmg160_data
*data
)
238 struct device
*dev
= regmap_get_device(data
->regmap
);
243 * Reset chip to get it in a known good state. A delay of 30ms after
244 * reset is required according to the datasheet.
246 regmap_write(data
->regmap
, BMG160_GYRO_REG_RESET
,
247 BMG160_GYRO_RESET_VAL
);
248 usleep_range(30000, 30700);
250 ret
= regmap_read(data
->regmap
, BMG160_REG_CHIP_ID
, &val
);
252 dev_err(dev
, "Error reading reg_chip_id\n");
256 dev_dbg(dev
, "Chip Id %x\n", val
);
257 if (val
!= BMG160_CHIP_ID_VAL
) {
258 dev_err(dev
, "invalid chip %x\n", val
);
262 ret
= bmg160_set_mode(data
, BMG160_MODE_NORMAL
);
266 /* Wait upto 500 ms to be ready after changing mode */
267 usleep_range(500, 1000);
270 ret
= bmg160_set_bw(data
, BMG160_DEF_BW
);
274 /* Set Default Range */
275 ret
= regmap_write(data
->regmap
, BMG160_REG_RANGE
, BMG160_RANGE_500DPS
);
277 dev_err(dev
, "Error writing reg_range\n");
280 data
->dps_range
= BMG160_RANGE_500DPS
;
282 ret
= regmap_read(data
->regmap
, BMG160_REG_SLOPE_THRES
, &val
);
284 dev_err(dev
, "Error reading reg_slope_thres\n");
287 data
->slope_thres
= val
;
289 /* Set default interrupt mode */
290 ret
= regmap_update_bits(data
->regmap
, BMG160_REG_INT_EN_1
,
291 BMG160_INT1_BIT_OD
, 0);
293 dev_err(dev
, "Error updating bits in reg_int_en_1\n");
297 ret
= regmap_write(data
->regmap
, BMG160_REG_INT_RST_LATCH
,
298 BMG160_INT_MODE_LATCH_INT
|
299 BMG160_INT_MODE_LATCH_RESET
);
302 "Error writing reg_motion_intr\n");
309 static int bmg160_set_power_state(struct bmg160_data
*data
, bool on
)
312 struct device
*dev
= regmap_get_device(data
->regmap
);
316 ret
= pm_runtime_get_sync(dev
);
318 pm_runtime_mark_last_busy(dev
);
319 ret
= pm_runtime_put_autosuspend(dev
);
323 dev_err(dev
, "Failed: bmg160_set_power_state for %d\n", on
);
326 pm_runtime_put_noidle(dev
);
335 static int bmg160_setup_any_motion_interrupt(struct bmg160_data
*data
,
338 struct device
*dev
= regmap_get_device(data
->regmap
);
341 /* Enable/Disable INT_MAP0 mapping */
342 ret
= regmap_update_bits(data
->regmap
, BMG160_REG_INT_MAP_0
,
343 BMG160_INT_MAP_0_BIT_ANY
,
344 (status
? BMG160_INT_MAP_0_BIT_ANY
: 0));
346 dev_err(dev
, "Error updating bits reg_int_map0\n");
350 /* Enable/Disable slope interrupts */
352 /* Update slope thres */
353 ret
= regmap_write(data
->regmap
, BMG160_REG_SLOPE_THRES
,
356 dev_err(dev
, "Error writing reg_slope_thres\n");
360 ret
= regmap_write(data
->regmap
, BMG160_REG_MOTION_INTR
,
361 BMG160_INT_MOTION_X
| BMG160_INT_MOTION_Y
|
362 BMG160_INT_MOTION_Z
);
364 dev_err(dev
, "Error writing reg_motion_intr\n");
369 * New data interrupt is always non-latched,
370 * which will have higher priority, so no need
371 * to set latched mode, we will be flooded anyway with INTR
373 if (!data
->dready_trigger_on
) {
374 ret
= regmap_write(data
->regmap
,
375 BMG160_REG_INT_RST_LATCH
,
376 BMG160_INT_MODE_LATCH_INT
|
377 BMG160_INT_MODE_LATCH_RESET
);
379 dev_err(dev
, "Error writing reg_rst_latch\n");
384 ret
= regmap_write(data
->regmap
, BMG160_REG_INT_EN_0
,
385 BMG160_DATA_ENABLE_INT
);
388 ret
= regmap_write(data
->regmap
, BMG160_REG_INT_EN_0
, 0);
392 dev_err(dev
, "Error writing reg_int_en0\n");
399 static int bmg160_setup_new_data_interrupt(struct bmg160_data
*data
,
402 struct device
*dev
= regmap_get_device(data
->regmap
);
405 /* Enable/Disable INT_MAP1 mapping */
406 ret
= regmap_update_bits(data
->regmap
, BMG160_REG_INT_MAP_1
,
407 BMG160_INT_MAP_1_BIT_NEW_DATA
,
408 (status
? BMG160_INT_MAP_1_BIT_NEW_DATA
: 0));
410 dev_err(dev
, "Error updating bits in reg_int_map1\n");
415 ret
= regmap_write(data
->regmap
, BMG160_REG_INT_RST_LATCH
,
416 BMG160_INT_MODE_NON_LATCH_INT
|
417 BMG160_INT_MODE_LATCH_RESET
);
419 dev_err(dev
, "Error writing reg_rst_latch\n");
423 ret
= regmap_write(data
->regmap
, BMG160_REG_INT_EN_0
,
424 BMG160_DATA_ENABLE_INT
);
427 /* Restore interrupt mode */
428 ret
= regmap_write(data
->regmap
, BMG160_REG_INT_RST_LATCH
,
429 BMG160_INT_MODE_LATCH_INT
|
430 BMG160_INT_MODE_LATCH_RESET
);
432 dev_err(dev
, "Error writing reg_rst_latch\n");
436 ret
= regmap_write(data
->regmap
, BMG160_REG_INT_EN_0
, 0);
440 dev_err(dev
, "Error writing reg_int_en0\n");
447 static int bmg160_get_bw(struct bmg160_data
*data
, int *val
)
449 struct device
*dev
= regmap_get_device(data
->regmap
);
451 unsigned int bw_bits
;
454 ret
= regmap_read(data
->regmap
, BMG160_REG_PMU_BW
, &bw_bits
);
456 dev_err(dev
, "Error reading reg_pmu_bw\n");
460 /* Ignore the readonly reserved bit. */
461 bw_bits
&= ~BMG160_REG_PMU_BW_RES
;
463 for (i
= 0; i
< ARRAY_SIZE(bmg160_samp_freq_table
); ++i
) {
464 if (bmg160_samp_freq_table
[i
].bw_bits
== bw_bits
) {
465 *val
= bmg160_samp_freq_table
[i
].odr
;
473 static int bmg160_set_scale(struct bmg160_data
*data
, int val
)
475 struct device
*dev
= regmap_get_device(data
->regmap
);
478 for (i
= 0; i
< ARRAY_SIZE(bmg160_scale_table
); ++i
) {
479 if (bmg160_scale_table
[i
].scale
== val
) {
480 ret
= regmap_write(data
->regmap
, BMG160_REG_RANGE
,
481 bmg160_scale_table
[i
].dps_range
);
483 dev_err(dev
, "Error writing reg_range\n");
486 data
->dps_range
= bmg160_scale_table
[i
].dps_range
;
494 static int bmg160_get_temp(struct bmg160_data
*data
, int *val
)
496 struct device
*dev
= regmap_get_device(data
->regmap
);
498 unsigned int raw_val
;
500 mutex_lock(&data
->mutex
);
501 ret
= bmg160_set_power_state(data
, true);
503 mutex_unlock(&data
->mutex
);
507 ret
= regmap_read(data
->regmap
, BMG160_REG_TEMP
, &raw_val
);
509 dev_err(dev
, "Error reading reg_temp\n");
510 bmg160_set_power_state(data
, false);
511 mutex_unlock(&data
->mutex
);
515 *val
= sign_extend32(raw_val
, 7);
516 ret
= bmg160_set_power_state(data
, false);
517 mutex_unlock(&data
->mutex
);
524 static int bmg160_get_axis(struct bmg160_data
*data
, int axis
, int *val
)
526 struct device
*dev
= regmap_get_device(data
->regmap
);
530 mutex_lock(&data
->mutex
);
531 ret
= bmg160_set_power_state(data
, true);
533 mutex_unlock(&data
->mutex
);
537 ret
= regmap_bulk_read(data
->regmap
, BMG160_AXIS_TO_REG(axis
), &raw_val
,
540 dev_err(dev
, "Error reading axis %d\n", axis
);
541 bmg160_set_power_state(data
, false);
542 mutex_unlock(&data
->mutex
);
546 *val
= sign_extend32(le16_to_cpu(raw_val
), 15);
547 ret
= bmg160_set_power_state(data
, false);
548 mutex_unlock(&data
->mutex
);
555 static int bmg160_read_raw(struct iio_dev
*indio_dev
,
556 struct iio_chan_spec
const *chan
,
557 int *val
, int *val2
, long mask
)
559 struct bmg160_data
*data
= iio_priv(indio_dev
);
563 case IIO_CHAN_INFO_RAW
:
564 switch (chan
->type
) {
566 return bmg160_get_temp(data
, val
);
568 if (iio_buffer_enabled(indio_dev
))
571 return bmg160_get_axis(data
, chan
->scan_index
,
576 case IIO_CHAN_INFO_OFFSET
:
577 if (chan
->type
== IIO_TEMP
) {
578 *val
= BMG160_TEMP_CENTER_VAL
;
582 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
:
583 return bmg160_get_filter(data
, val
);
584 case IIO_CHAN_INFO_SCALE
:
586 switch (chan
->type
) {
589 return IIO_VAL_INT_PLUS_MICRO
;
594 for (i
= 0; i
< ARRAY_SIZE(bmg160_scale_table
); ++i
) {
595 if (bmg160_scale_table
[i
].dps_range
==
597 *val2
= bmg160_scale_table
[i
].scale
;
598 return IIO_VAL_INT_PLUS_MICRO
;
606 case IIO_CHAN_INFO_SAMP_FREQ
:
608 mutex_lock(&data
->mutex
);
609 ret
= bmg160_get_bw(data
, val
);
610 mutex_unlock(&data
->mutex
);
617 static int bmg160_write_raw(struct iio_dev
*indio_dev
,
618 struct iio_chan_spec
const *chan
,
619 int val
, int val2
, long mask
)
621 struct bmg160_data
*data
= iio_priv(indio_dev
);
625 case IIO_CHAN_INFO_SAMP_FREQ
:
626 mutex_lock(&data
->mutex
);
628 * Section 4.2 of spec
629 * In suspend mode, the only supported operations are reading
630 * registers as well as writing to the (0x14) softreset
631 * register. Since we will be in suspend mode by default, change
632 * mode to power on for other writes.
634 ret
= bmg160_set_power_state(data
, true);
636 mutex_unlock(&data
->mutex
);
639 ret
= bmg160_set_bw(data
, val
);
641 bmg160_set_power_state(data
, false);
642 mutex_unlock(&data
->mutex
);
645 ret
= bmg160_set_power_state(data
, false);
646 mutex_unlock(&data
->mutex
);
648 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
:
652 mutex_lock(&data
->mutex
);
653 ret
= bmg160_set_power_state(data
, true);
655 bmg160_set_power_state(data
, false);
656 mutex_unlock(&data
->mutex
);
659 ret
= bmg160_set_filter(data
, val
);
661 bmg160_set_power_state(data
, false);
662 mutex_unlock(&data
->mutex
);
665 ret
= bmg160_set_power_state(data
, false);
666 mutex_unlock(&data
->mutex
);
668 case IIO_CHAN_INFO_SCALE
:
672 mutex_lock(&data
->mutex
);
673 /* Refer to comments above for the suspend mode ops */
674 ret
= bmg160_set_power_state(data
, true);
676 mutex_unlock(&data
->mutex
);
679 ret
= bmg160_set_scale(data
, val2
);
681 bmg160_set_power_state(data
, false);
682 mutex_unlock(&data
->mutex
);
685 ret
= bmg160_set_power_state(data
, false);
686 mutex_unlock(&data
->mutex
);
695 static int bmg160_read_event(struct iio_dev
*indio_dev
,
696 const struct iio_chan_spec
*chan
,
697 enum iio_event_type type
,
698 enum iio_event_direction dir
,
699 enum iio_event_info info
,
702 struct bmg160_data
*data
= iio_priv(indio_dev
);
706 case IIO_EV_INFO_VALUE
:
707 *val
= data
->slope_thres
& BMG160_SLOPE_THRES_MASK
;
716 static int bmg160_write_event(struct iio_dev
*indio_dev
,
717 const struct iio_chan_spec
*chan
,
718 enum iio_event_type type
,
719 enum iio_event_direction dir
,
720 enum iio_event_info info
,
723 struct bmg160_data
*data
= iio_priv(indio_dev
);
726 case IIO_EV_INFO_VALUE
:
727 if (data
->ev_enable_state
)
729 data
->slope_thres
&= ~BMG160_SLOPE_THRES_MASK
;
730 data
->slope_thres
|= (val
& BMG160_SLOPE_THRES_MASK
);
739 static int bmg160_read_event_config(struct iio_dev
*indio_dev
,
740 const struct iio_chan_spec
*chan
,
741 enum iio_event_type type
,
742 enum iio_event_direction dir
)
745 struct bmg160_data
*data
= iio_priv(indio_dev
);
747 return data
->ev_enable_state
;
750 static int bmg160_write_event_config(struct iio_dev
*indio_dev
,
751 const struct iio_chan_spec
*chan
,
752 enum iio_event_type type
,
753 enum iio_event_direction dir
,
756 struct bmg160_data
*data
= iio_priv(indio_dev
);
759 if (state
&& data
->ev_enable_state
)
762 mutex_lock(&data
->mutex
);
764 if (!state
&& data
->motion_trigger_on
) {
765 data
->ev_enable_state
= 0;
766 mutex_unlock(&data
->mutex
);
770 * We will expect the enable and disable to do operation in
771 * in reverse order. This will happen here anyway as our
772 * resume operation uses sync mode runtime pm calls, the
773 * suspend operation will be delayed by autosuspend delay
774 * So the disable operation will still happen in reverse of
775 * enable operation. When runtime pm is disabled the mode
776 * is always on so sequence doesn't matter
778 ret
= bmg160_set_power_state(data
, state
);
780 mutex_unlock(&data
->mutex
);
784 ret
= bmg160_setup_any_motion_interrupt(data
, state
);
786 bmg160_set_power_state(data
, false);
787 mutex_unlock(&data
->mutex
);
791 data
->ev_enable_state
= state
;
792 mutex_unlock(&data
->mutex
);
797 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("100 200 400 1000 2000");
799 static IIO_CONST_ATTR(in_anglvel_scale_available
,
800 "0.001065 0.000532 0.000266 0.000133 0.000066");
802 static struct attribute
*bmg160_attributes
[] = {
803 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
804 &iio_const_attr_in_anglvel_scale_available
.dev_attr
.attr
,
808 static const struct attribute_group bmg160_attrs_group
= {
809 .attrs
= bmg160_attributes
,
812 static const struct iio_event_spec bmg160_event
= {
813 .type
= IIO_EV_TYPE_ROC
,
814 .dir
= IIO_EV_DIR_EITHER
,
815 .mask_shared_by_type
= BIT(IIO_EV_INFO_VALUE
) |
816 BIT(IIO_EV_INFO_ENABLE
)
819 #define BMG160_CHANNEL(_axis) { \
820 .type = IIO_ANGL_VEL, \
822 .channel2 = IIO_MOD_##_axis, \
823 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
824 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
825 BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
826 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
827 .scan_index = AXIS_##_axis, \
832 .endianness = IIO_LE, \
834 .event_spec = &bmg160_event, \
835 .num_event_specs = 1 \
838 static const struct iio_chan_spec bmg160_channels
[] = {
841 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
842 BIT(IIO_CHAN_INFO_SCALE
) |
843 BIT(IIO_CHAN_INFO_OFFSET
),
849 IIO_CHAN_SOFT_TIMESTAMP(3),
852 static const struct iio_info bmg160_info
= {
853 .attrs
= &bmg160_attrs_group
,
854 .read_raw
= bmg160_read_raw
,
855 .write_raw
= bmg160_write_raw
,
856 .read_event_value
= bmg160_read_event
,
857 .write_event_value
= bmg160_write_event
,
858 .write_event_config
= bmg160_write_event_config
,
859 .read_event_config
= bmg160_read_event_config
,
862 static const unsigned long bmg160_accel_scan_masks
[] = {
863 BIT(AXIS_X
) | BIT(AXIS_Y
) | BIT(AXIS_Z
),
866 static irqreturn_t
bmg160_trigger_handler(int irq
, void *p
)
868 struct iio_poll_func
*pf
= p
;
869 struct iio_dev
*indio_dev
= pf
->indio_dev
;
870 struct bmg160_data
*data
= iio_priv(indio_dev
);
873 mutex_lock(&data
->mutex
);
874 ret
= regmap_bulk_read(data
->regmap
, BMG160_REG_XOUT_L
,
875 data
->buffer
, AXIS_MAX
* 2);
876 mutex_unlock(&data
->mutex
);
880 iio_push_to_buffers_with_timestamp(indio_dev
, data
->buffer
,
883 iio_trigger_notify_done(indio_dev
->trig
);
888 static int bmg160_trig_try_reen(struct iio_trigger
*trig
)
890 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
891 struct bmg160_data
*data
= iio_priv(indio_dev
);
892 struct device
*dev
= regmap_get_device(data
->regmap
);
895 /* new data interrupts don't need ack */
896 if (data
->dready_trigger_on
)
899 /* Set latched mode interrupt and clear any latched interrupt */
900 ret
= regmap_write(data
->regmap
, BMG160_REG_INT_RST_LATCH
,
901 BMG160_INT_MODE_LATCH_INT
|
902 BMG160_INT_MODE_LATCH_RESET
);
904 dev_err(dev
, "Error writing reg_rst_latch\n");
911 static int bmg160_data_rdy_trigger_set_state(struct iio_trigger
*trig
,
914 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
915 struct bmg160_data
*data
= iio_priv(indio_dev
);
918 mutex_lock(&data
->mutex
);
920 if (!state
&& data
->ev_enable_state
&& data
->motion_trigger_on
) {
921 data
->motion_trigger_on
= false;
922 mutex_unlock(&data
->mutex
);
927 * Refer to comment in bmg160_write_event_config for
928 * enable/disable operation order
930 ret
= bmg160_set_power_state(data
, state
);
932 mutex_unlock(&data
->mutex
);
935 if (data
->motion_trig
== trig
)
936 ret
= bmg160_setup_any_motion_interrupt(data
, state
);
938 ret
= bmg160_setup_new_data_interrupt(data
, state
);
940 bmg160_set_power_state(data
, false);
941 mutex_unlock(&data
->mutex
);
944 if (data
->motion_trig
== trig
)
945 data
->motion_trigger_on
= state
;
947 data
->dready_trigger_on
= state
;
949 mutex_unlock(&data
->mutex
);
954 static const struct iio_trigger_ops bmg160_trigger_ops
= {
955 .set_trigger_state
= bmg160_data_rdy_trigger_set_state
,
956 .try_reenable
= bmg160_trig_try_reen
,
959 static irqreturn_t
bmg160_event_handler(int irq
, void *private)
961 struct iio_dev
*indio_dev
= private;
962 struct bmg160_data
*data
= iio_priv(indio_dev
);
963 struct device
*dev
= regmap_get_device(data
->regmap
);
968 ret
= regmap_read(data
->regmap
, BMG160_REG_INT_STATUS_2
, &val
);
970 dev_err(dev
, "Error reading reg_int_status2\n");
971 goto ack_intr_status
;
975 dir
= IIO_EV_DIR_RISING
;
977 dir
= IIO_EV_DIR_FALLING
;
979 if (val
& BMG160_ANY_MOTION_BIT_X
)
980 iio_push_event(indio_dev
, IIO_MOD_EVENT_CODE(IIO_ANGL_VEL
,
985 iio_get_time_ns(indio_dev
));
986 if (val
& BMG160_ANY_MOTION_BIT_Y
)
987 iio_push_event(indio_dev
, IIO_MOD_EVENT_CODE(IIO_ANGL_VEL
,
992 iio_get_time_ns(indio_dev
));
993 if (val
& BMG160_ANY_MOTION_BIT_Z
)
994 iio_push_event(indio_dev
, IIO_MOD_EVENT_CODE(IIO_ANGL_VEL
,
999 iio_get_time_ns(indio_dev
));
1002 if (!data
->dready_trigger_on
) {
1003 ret
= regmap_write(data
->regmap
, BMG160_REG_INT_RST_LATCH
,
1004 BMG160_INT_MODE_LATCH_INT
|
1005 BMG160_INT_MODE_LATCH_RESET
);
1007 dev_err(dev
, "Error writing reg_rst_latch\n");
1013 static irqreturn_t
bmg160_data_rdy_trig_poll(int irq
, void *private)
1015 struct iio_dev
*indio_dev
= private;
1016 struct bmg160_data
*data
= iio_priv(indio_dev
);
1018 if (data
->dready_trigger_on
)
1019 iio_trigger_poll(data
->dready_trig
);
1020 else if (data
->motion_trigger_on
)
1021 iio_trigger_poll(data
->motion_trig
);
1023 if (data
->ev_enable_state
)
1024 return IRQ_WAKE_THREAD
;
1030 static int bmg160_buffer_preenable(struct iio_dev
*indio_dev
)
1032 struct bmg160_data
*data
= iio_priv(indio_dev
);
1034 return bmg160_set_power_state(data
, true);
1037 static int bmg160_buffer_postdisable(struct iio_dev
*indio_dev
)
1039 struct bmg160_data
*data
= iio_priv(indio_dev
);
1041 return bmg160_set_power_state(data
, false);
1044 static const struct iio_buffer_setup_ops bmg160_buffer_setup_ops
= {
1045 .preenable
= bmg160_buffer_preenable
,
1046 .postenable
= iio_triggered_buffer_postenable
,
1047 .predisable
= iio_triggered_buffer_predisable
,
1048 .postdisable
= bmg160_buffer_postdisable
,
1051 static const char *bmg160_match_acpi_device(struct device
*dev
)
1053 const struct acpi_device_id
*id
;
1055 id
= acpi_match_device(dev
->driver
->acpi_match_table
, dev
);
1059 return dev_name(dev
);
1062 int bmg160_core_probe(struct device
*dev
, struct regmap
*regmap
, int irq
,
1065 struct bmg160_data
*data
;
1066 struct iio_dev
*indio_dev
;
1069 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*data
));
1073 data
= iio_priv(indio_dev
);
1074 dev_set_drvdata(dev
, indio_dev
);
1076 data
->regmap
= regmap
;
1078 ret
= bmg160_chip_init(data
);
1082 mutex_init(&data
->mutex
);
1084 if (ACPI_HANDLE(dev
))
1085 name
= bmg160_match_acpi_device(dev
);
1087 indio_dev
->dev
.parent
= dev
;
1088 indio_dev
->channels
= bmg160_channels
;
1089 indio_dev
->num_channels
= ARRAY_SIZE(bmg160_channels
);
1090 indio_dev
->name
= name
;
1091 indio_dev
->available_scan_masks
= bmg160_accel_scan_masks
;
1092 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1093 indio_dev
->info
= &bmg160_info
;
1095 if (data
->irq
> 0) {
1096 ret
= devm_request_threaded_irq(dev
,
1098 bmg160_data_rdy_trig_poll
,
1099 bmg160_event_handler
,
1100 IRQF_TRIGGER_RISING
,
1106 data
->dready_trig
= devm_iio_trigger_alloc(dev
,
1110 if (!data
->dready_trig
)
1113 data
->motion_trig
= devm_iio_trigger_alloc(dev
,
1114 "%s-any-motion-dev%d",
1117 if (!data
->motion_trig
)
1120 data
->dready_trig
->dev
.parent
= dev
;
1121 data
->dready_trig
->ops
= &bmg160_trigger_ops
;
1122 iio_trigger_set_drvdata(data
->dready_trig
, indio_dev
);
1123 ret
= iio_trigger_register(data
->dready_trig
);
1127 data
->motion_trig
->dev
.parent
= dev
;
1128 data
->motion_trig
->ops
= &bmg160_trigger_ops
;
1129 iio_trigger_set_drvdata(data
->motion_trig
, indio_dev
);
1130 ret
= iio_trigger_register(data
->motion_trig
);
1132 data
->motion_trig
= NULL
;
1133 goto err_trigger_unregister
;
1137 ret
= iio_triggered_buffer_setup(indio_dev
,
1138 iio_pollfunc_store_time
,
1139 bmg160_trigger_handler
,
1140 &bmg160_buffer_setup_ops
);
1143 "iio triggered buffer setup failed\n");
1144 goto err_trigger_unregister
;
1147 ret
= pm_runtime_set_active(dev
);
1149 goto err_buffer_cleanup
;
1151 pm_runtime_enable(dev
);
1152 pm_runtime_set_autosuspend_delay(dev
,
1153 BMG160_AUTO_SUSPEND_DELAY_MS
);
1154 pm_runtime_use_autosuspend(dev
);
1156 ret
= iio_device_register(indio_dev
);
1158 dev_err(dev
, "unable to register iio device\n");
1159 goto err_buffer_cleanup
;
1165 iio_triggered_buffer_cleanup(indio_dev
);
1166 err_trigger_unregister
:
1167 if (data
->dready_trig
)
1168 iio_trigger_unregister(data
->dready_trig
);
1169 if (data
->motion_trig
)
1170 iio_trigger_unregister(data
->motion_trig
);
1174 EXPORT_SYMBOL_GPL(bmg160_core_probe
);
1176 void bmg160_core_remove(struct device
*dev
)
1178 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1179 struct bmg160_data
*data
= iio_priv(indio_dev
);
1181 iio_device_unregister(indio_dev
);
1183 pm_runtime_disable(dev
);
1184 pm_runtime_set_suspended(dev
);
1185 pm_runtime_put_noidle(dev
);
1187 iio_triggered_buffer_cleanup(indio_dev
);
1189 if (data
->dready_trig
) {
1190 iio_trigger_unregister(data
->dready_trig
);
1191 iio_trigger_unregister(data
->motion_trig
);
1194 mutex_lock(&data
->mutex
);
1195 bmg160_set_mode(data
, BMG160_MODE_DEEP_SUSPEND
);
1196 mutex_unlock(&data
->mutex
);
1198 EXPORT_SYMBOL_GPL(bmg160_core_remove
);
1200 #ifdef CONFIG_PM_SLEEP
1201 static int bmg160_suspend(struct device
*dev
)
1203 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1204 struct bmg160_data
*data
= iio_priv(indio_dev
);
1206 mutex_lock(&data
->mutex
);
1207 bmg160_set_mode(data
, BMG160_MODE_SUSPEND
);
1208 mutex_unlock(&data
->mutex
);
1213 static int bmg160_resume(struct device
*dev
)
1215 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1216 struct bmg160_data
*data
= iio_priv(indio_dev
);
1218 mutex_lock(&data
->mutex
);
1219 if (data
->dready_trigger_on
|| data
->motion_trigger_on
||
1220 data
->ev_enable_state
)
1221 bmg160_set_mode(data
, BMG160_MODE_NORMAL
);
1222 mutex_unlock(&data
->mutex
);
1229 static int bmg160_runtime_suspend(struct device
*dev
)
1231 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1232 struct bmg160_data
*data
= iio_priv(indio_dev
);
1235 ret
= bmg160_set_mode(data
, BMG160_MODE_SUSPEND
);
1237 dev_err(dev
, "set mode failed\n");
1244 static int bmg160_runtime_resume(struct device
*dev
)
1246 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1247 struct bmg160_data
*data
= iio_priv(indio_dev
);
1250 ret
= bmg160_set_mode(data
, BMG160_MODE_NORMAL
);
1254 msleep_interruptible(BMG160_MAX_STARTUP_TIME_MS
);
1260 const struct dev_pm_ops bmg160_pm_ops
= {
1261 SET_SYSTEM_SLEEP_PM_OPS(bmg160_suspend
, bmg160_resume
)
1262 SET_RUNTIME_PM_OPS(bmg160_runtime_suspend
,
1263 bmg160_runtime_resume
, NULL
)
1265 EXPORT_SYMBOL_GPL(bmg160_pm_ops
);
1267 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
1268 MODULE_LICENSE("GPL v2");
1269 MODULE_DESCRIPTION("BMG160 Gyro driver");