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
54 #define BMG160_REG_INT_MAP_0 0x17
55 #define BMG160_INT_MAP_0_BIT_ANY BIT(1)
57 #define BMG160_REG_INT_MAP_1 0x18
58 #define BMG160_INT_MAP_1_BIT_NEW_DATA BIT(0)
60 #define BMG160_REG_INT_RST_LATCH 0x21
61 #define BMG160_INT_MODE_LATCH_RESET 0x80
62 #define BMG160_INT_MODE_LATCH_INT 0x0F
63 #define BMG160_INT_MODE_NON_LATCH_INT 0x00
65 #define BMG160_REG_INT_EN_0 0x15
66 #define BMG160_DATA_ENABLE_INT BIT(7)
68 #define BMG160_REG_INT_EN_1 0x16
69 #define BMG160_INT1_BIT_OD BIT(1)
71 #define BMG160_REG_XOUT_L 0x02
72 #define BMG160_AXIS_TO_REG(axis) (BMG160_REG_XOUT_L + (axis * 2))
74 #define BMG160_REG_SLOPE_THRES 0x1B
75 #define BMG160_SLOPE_THRES_MASK 0x0F
77 #define BMG160_REG_MOTION_INTR 0x1C
78 #define BMG160_INT_MOTION_X BIT(0)
79 #define BMG160_INT_MOTION_Y BIT(1)
80 #define BMG160_INT_MOTION_Z BIT(2)
81 #define BMG160_ANY_DUR_MASK 0x30
82 #define BMG160_ANY_DUR_SHIFT 4
84 #define BMG160_REG_INT_STATUS_2 0x0B
85 #define BMG160_ANY_MOTION_MASK 0x07
86 #define BMG160_ANY_MOTION_BIT_X BIT(0)
87 #define BMG160_ANY_MOTION_BIT_Y BIT(1)
88 #define BMG160_ANY_MOTION_BIT_Z BIT(2)
90 #define BMG160_REG_TEMP 0x08
91 #define BMG160_TEMP_CENTER_VAL 23
93 #define BMG160_MAX_STARTUP_TIME_MS 80
95 #define BMG160_AUTO_SUSPEND_DELAY_MS 2000
98 struct regmap
*regmap
;
99 struct iio_trigger
*dready_trig
;
100 struct iio_trigger
*motion_trig
;
107 bool dready_trigger_on
;
108 bool motion_trigger_on
;
119 static const struct {
122 } bmg160_samp_freq_table
[] = { {100, 0x07},
128 static const struct {
131 } bmg160_scale_table
[] = { { 1065, BMG160_RANGE_2000DPS
},
132 { 532, BMG160_RANGE_1000DPS
},
133 { 266, BMG160_RANGE_500DPS
},
134 { 133, BMG160_RANGE_250DPS
},
135 { 66, BMG160_RANGE_125DPS
} };
137 static int bmg160_set_mode(struct bmg160_data
*data
, u8 mode
)
139 struct device
*dev
= regmap_get_device(data
->regmap
);
142 ret
= regmap_write(data
->regmap
, BMG160_REG_PMU_LPW
, mode
);
144 dev_err(dev
, "Error writing reg_pmu_lpw\n");
151 static int bmg160_convert_freq_to_bit(int val
)
155 for (i
= 0; i
< ARRAY_SIZE(bmg160_samp_freq_table
); ++i
) {
156 if (bmg160_samp_freq_table
[i
].val
== val
)
157 return bmg160_samp_freq_table
[i
].bw_bits
;
163 static int bmg160_set_bw(struct bmg160_data
*data
, int val
)
165 struct device
*dev
= regmap_get_device(data
->regmap
);
169 bw_bits
= bmg160_convert_freq_to_bit(val
);
173 ret
= regmap_write(data
->regmap
, BMG160_REG_PMU_BW
, bw_bits
);
175 dev_err(dev
, "Error writing reg_pmu_bw\n");
179 data
->bw_bits
= bw_bits
;
184 static int bmg160_chip_init(struct bmg160_data
*data
)
186 struct device
*dev
= regmap_get_device(data
->regmap
);
190 ret
= regmap_read(data
->regmap
, BMG160_REG_CHIP_ID
, &val
);
192 dev_err(dev
, "Error reading reg_chip_id\n");
196 dev_dbg(dev
, "Chip Id %x\n", val
);
197 if (val
!= BMG160_CHIP_ID_VAL
) {
198 dev_err(dev
, "invalid chip %x\n", val
);
202 ret
= bmg160_set_mode(data
, BMG160_MODE_NORMAL
);
206 /* Wait upto 500 ms to be ready after changing mode */
207 usleep_range(500, 1000);
210 ret
= bmg160_set_bw(data
, BMG160_DEF_BW
);
214 /* Set Default Range */
215 ret
= regmap_write(data
->regmap
, BMG160_REG_RANGE
, BMG160_RANGE_500DPS
);
217 dev_err(dev
, "Error writing reg_range\n");
220 data
->dps_range
= BMG160_RANGE_500DPS
;
222 ret
= regmap_read(data
->regmap
, BMG160_REG_SLOPE_THRES
, &val
);
224 dev_err(dev
, "Error reading reg_slope_thres\n");
227 data
->slope_thres
= val
;
229 /* Set default interrupt mode */
230 ret
= regmap_update_bits(data
->regmap
, BMG160_REG_INT_EN_1
,
231 BMG160_INT1_BIT_OD
, 0);
233 dev_err(dev
, "Error updating bits in reg_int_en_1\n");
237 ret
= regmap_write(data
->regmap
, BMG160_REG_INT_RST_LATCH
,
238 BMG160_INT_MODE_LATCH_INT
|
239 BMG160_INT_MODE_LATCH_RESET
);
242 "Error writing reg_motion_intr\n");
249 static int bmg160_set_power_state(struct bmg160_data
*data
, bool on
)
252 struct device
*dev
= regmap_get_device(data
->regmap
);
256 ret
= pm_runtime_get_sync(dev
);
258 pm_runtime_mark_last_busy(dev
);
259 ret
= pm_runtime_put_autosuspend(dev
);
263 dev_err(dev
, "Failed: bmg160_set_power_state for %d\n", on
);
266 pm_runtime_put_noidle(dev
);
275 static int bmg160_setup_any_motion_interrupt(struct bmg160_data
*data
,
278 struct device
*dev
= regmap_get_device(data
->regmap
);
281 /* Enable/Disable INT_MAP0 mapping */
282 ret
= regmap_update_bits(data
->regmap
, BMG160_REG_INT_MAP_0
,
283 BMG160_INT_MAP_0_BIT_ANY
,
284 (status
? BMG160_INT_MAP_0_BIT_ANY
: 0));
286 dev_err(dev
, "Error updating bits reg_int_map0\n");
290 /* Enable/Disable slope interrupts */
292 /* Update slope thres */
293 ret
= regmap_write(data
->regmap
, BMG160_REG_SLOPE_THRES
,
296 dev_err(dev
, "Error writing reg_slope_thres\n");
300 ret
= regmap_write(data
->regmap
, BMG160_REG_MOTION_INTR
,
301 BMG160_INT_MOTION_X
| BMG160_INT_MOTION_Y
|
302 BMG160_INT_MOTION_Z
);
304 dev_err(dev
, "Error writing reg_motion_intr\n");
309 * New data interrupt is always non-latched,
310 * which will have higher priority, so no need
311 * to set latched mode, we will be flooded anyway with INTR
313 if (!data
->dready_trigger_on
) {
314 ret
= regmap_write(data
->regmap
,
315 BMG160_REG_INT_RST_LATCH
,
316 BMG160_INT_MODE_LATCH_INT
|
317 BMG160_INT_MODE_LATCH_RESET
);
319 dev_err(dev
, "Error writing reg_rst_latch\n");
324 ret
= regmap_write(data
->regmap
, BMG160_REG_INT_EN_0
,
325 BMG160_DATA_ENABLE_INT
);
328 ret
= regmap_write(data
->regmap
, BMG160_REG_INT_EN_0
, 0);
332 dev_err(dev
, "Error writing reg_int_en0\n");
339 static int bmg160_setup_new_data_interrupt(struct bmg160_data
*data
,
342 struct device
*dev
= regmap_get_device(data
->regmap
);
345 /* Enable/Disable INT_MAP1 mapping */
346 ret
= regmap_update_bits(data
->regmap
, BMG160_REG_INT_MAP_1
,
347 BMG160_INT_MAP_1_BIT_NEW_DATA
,
348 (status
? BMG160_INT_MAP_1_BIT_NEW_DATA
: 0));
350 dev_err(dev
, "Error updating bits in reg_int_map1\n");
355 ret
= regmap_write(data
->regmap
, BMG160_REG_INT_RST_LATCH
,
356 BMG160_INT_MODE_NON_LATCH_INT
|
357 BMG160_INT_MODE_LATCH_RESET
);
359 dev_err(dev
, "Error writing reg_rst_latch\n");
363 ret
= regmap_write(data
->regmap
, BMG160_REG_INT_EN_0
,
364 BMG160_DATA_ENABLE_INT
);
367 /* Restore interrupt mode */
368 ret
= regmap_write(data
->regmap
, BMG160_REG_INT_RST_LATCH
,
369 BMG160_INT_MODE_LATCH_INT
|
370 BMG160_INT_MODE_LATCH_RESET
);
372 dev_err(dev
, "Error writing reg_rst_latch\n");
376 ret
= regmap_write(data
->regmap
, BMG160_REG_INT_EN_0
, 0);
380 dev_err(dev
, "Error writing reg_int_en0\n");
387 static int bmg160_get_bw(struct bmg160_data
*data
, int *val
)
391 for (i
= 0; i
< ARRAY_SIZE(bmg160_samp_freq_table
); ++i
) {
392 if (bmg160_samp_freq_table
[i
].bw_bits
== data
->bw_bits
) {
393 *val
= bmg160_samp_freq_table
[i
].val
;
401 static int bmg160_set_scale(struct bmg160_data
*data
, int val
)
403 struct device
*dev
= regmap_get_device(data
->regmap
);
406 for (i
= 0; i
< ARRAY_SIZE(bmg160_scale_table
); ++i
) {
407 if (bmg160_scale_table
[i
].scale
== val
) {
408 ret
= regmap_write(data
->regmap
, BMG160_REG_RANGE
,
409 bmg160_scale_table
[i
].dps_range
);
411 dev_err(dev
, "Error writing reg_range\n");
414 data
->dps_range
= bmg160_scale_table
[i
].dps_range
;
422 static int bmg160_get_temp(struct bmg160_data
*data
, int *val
)
424 struct device
*dev
= regmap_get_device(data
->regmap
);
426 unsigned int raw_val
;
428 mutex_lock(&data
->mutex
);
429 ret
= bmg160_set_power_state(data
, true);
431 mutex_unlock(&data
->mutex
);
435 ret
= regmap_read(data
->regmap
, BMG160_REG_TEMP
, &raw_val
);
437 dev_err(dev
, "Error reading reg_temp\n");
438 bmg160_set_power_state(data
, false);
439 mutex_unlock(&data
->mutex
);
443 *val
= sign_extend32(raw_val
, 7);
444 ret
= bmg160_set_power_state(data
, false);
445 mutex_unlock(&data
->mutex
);
452 static int bmg160_get_axis(struct bmg160_data
*data
, int axis
, int *val
)
454 struct device
*dev
= regmap_get_device(data
->regmap
);
458 mutex_lock(&data
->mutex
);
459 ret
= bmg160_set_power_state(data
, true);
461 mutex_unlock(&data
->mutex
);
465 ret
= regmap_bulk_read(data
->regmap
, BMG160_AXIS_TO_REG(axis
), &raw_val
,
468 dev_err(dev
, "Error reading axis %d\n", axis
);
469 bmg160_set_power_state(data
, false);
470 mutex_unlock(&data
->mutex
);
474 *val
= sign_extend32(le16_to_cpu(raw_val
), 15);
475 ret
= bmg160_set_power_state(data
, false);
476 mutex_unlock(&data
->mutex
);
483 static int bmg160_read_raw(struct iio_dev
*indio_dev
,
484 struct iio_chan_spec
const *chan
,
485 int *val
, int *val2
, long mask
)
487 struct bmg160_data
*data
= iio_priv(indio_dev
);
491 case IIO_CHAN_INFO_RAW
:
492 switch (chan
->type
) {
494 return bmg160_get_temp(data
, val
);
496 if (iio_buffer_enabled(indio_dev
))
499 return bmg160_get_axis(data
, chan
->scan_index
,
504 case IIO_CHAN_INFO_OFFSET
:
505 if (chan
->type
== IIO_TEMP
) {
506 *val
= BMG160_TEMP_CENTER_VAL
;
510 case IIO_CHAN_INFO_SCALE
:
512 switch (chan
->type
) {
515 return IIO_VAL_INT_PLUS_MICRO
;
520 for (i
= 0; i
< ARRAY_SIZE(bmg160_scale_table
); ++i
) {
521 if (bmg160_scale_table
[i
].dps_range
==
523 *val2
= bmg160_scale_table
[i
].scale
;
524 return IIO_VAL_INT_PLUS_MICRO
;
532 case IIO_CHAN_INFO_SAMP_FREQ
:
534 mutex_lock(&data
->mutex
);
535 ret
= bmg160_get_bw(data
, val
);
536 mutex_unlock(&data
->mutex
);
543 static int bmg160_write_raw(struct iio_dev
*indio_dev
,
544 struct iio_chan_spec
const *chan
,
545 int val
, int val2
, long mask
)
547 struct bmg160_data
*data
= iio_priv(indio_dev
);
551 case IIO_CHAN_INFO_SAMP_FREQ
:
552 mutex_lock(&data
->mutex
);
554 * Section 4.2 of spec
555 * In suspend mode, the only supported operations are reading
556 * registers as well as writing to the (0x14) softreset
557 * register. Since we will be in suspend mode by default, change
558 * mode to power on for other writes.
560 ret
= bmg160_set_power_state(data
, true);
562 mutex_unlock(&data
->mutex
);
565 ret
= bmg160_set_bw(data
, val
);
567 bmg160_set_power_state(data
, false);
568 mutex_unlock(&data
->mutex
);
571 ret
= bmg160_set_power_state(data
, false);
572 mutex_unlock(&data
->mutex
);
574 case IIO_CHAN_INFO_SCALE
:
578 mutex_lock(&data
->mutex
);
579 /* Refer to comments above for the suspend mode ops */
580 ret
= bmg160_set_power_state(data
, true);
582 mutex_unlock(&data
->mutex
);
585 ret
= bmg160_set_scale(data
, val2
);
587 bmg160_set_power_state(data
, false);
588 mutex_unlock(&data
->mutex
);
591 ret
= bmg160_set_power_state(data
, false);
592 mutex_unlock(&data
->mutex
);
601 static int bmg160_read_event(struct iio_dev
*indio_dev
,
602 const struct iio_chan_spec
*chan
,
603 enum iio_event_type type
,
604 enum iio_event_direction dir
,
605 enum iio_event_info info
,
608 struct bmg160_data
*data
= iio_priv(indio_dev
);
612 case IIO_EV_INFO_VALUE
:
613 *val
= data
->slope_thres
& BMG160_SLOPE_THRES_MASK
;
622 static int bmg160_write_event(struct iio_dev
*indio_dev
,
623 const struct iio_chan_spec
*chan
,
624 enum iio_event_type type
,
625 enum iio_event_direction dir
,
626 enum iio_event_info info
,
629 struct bmg160_data
*data
= iio_priv(indio_dev
);
632 case IIO_EV_INFO_VALUE
:
633 if (data
->ev_enable_state
)
635 data
->slope_thres
&= ~BMG160_SLOPE_THRES_MASK
;
636 data
->slope_thres
|= (val
& BMG160_SLOPE_THRES_MASK
);
645 static int bmg160_read_event_config(struct iio_dev
*indio_dev
,
646 const struct iio_chan_spec
*chan
,
647 enum iio_event_type type
,
648 enum iio_event_direction dir
)
651 struct bmg160_data
*data
= iio_priv(indio_dev
);
653 return data
->ev_enable_state
;
656 static int bmg160_write_event_config(struct iio_dev
*indio_dev
,
657 const struct iio_chan_spec
*chan
,
658 enum iio_event_type type
,
659 enum iio_event_direction dir
,
662 struct bmg160_data
*data
= iio_priv(indio_dev
);
665 if (state
&& data
->ev_enable_state
)
668 mutex_lock(&data
->mutex
);
670 if (!state
&& data
->motion_trigger_on
) {
671 data
->ev_enable_state
= 0;
672 mutex_unlock(&data
->mutex
);
676 * We will expect the enable and disable to do operation in
677 * in reverse order. This will happen here anyway as our
678 * resume operation uses sync mode runtime pm calls, the
679 * suspend operation will be delayed by autosuspend delay
680 * So the disable operation will still happen in reverse of
681 * enable operation. When runtime pm is disabled the mode
682 * is always on so sequence doesn't matter
684 ret
= bmg160_set_power_state(data
, state
);
686 mutex_unlock(&data
->mutex
);
690 ret
= bmg160_setup_any_motion_interrupt(data
, state
);
692 bmg160_set_power_state(data
, false);
693 mutex_unlock(&data
->mutex
);
697 data
->ev_enable_state
= state
;
698 mutex_unlock(&data
->mutex
);
703 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("100 200 400 1000 2000");
705 static IIO_CONST_ATTR(in_anglvel_scale_available
,
706 "0.001065 0.000532 0.000266 0.000133 0.000066");
708 static struct attribute
*bmg160_attributes
[] = {
709 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
710 &iio_const_attr_in_anglvel_scale_available
.dev_attr
.attr
,
714 static const struct attribute_group bmg160_attrs_group
= {
715 .attrs
= bmg160_attributes
,
718 static const struct iio_event_spec bmg160_event
= {
719 .type
= IIO_EV_TYPE_ROC
,
720 .dir
= IIO_EV_DIR_EITHER
,
721 .mask_shared_by_type
= BIT(IIO_EV_INFO_VALUE
) |
722 BIT(IIO_EV_INFO_ENABLE
)
725 #define BMG160_CHANNEL(_axis) { \
726 .type = IIO_ANGL_VEL, \
728 .channel2 = IIO_MOD_##_axis, \
729 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
730 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
731 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
732 .scan_index = AXIS_##_axis, \
737 .endianness = IIO_LE, \
739 .event_spec = &bmg160_event, \
740 .num_event_specs = 1 \
743 static const struct iio_chan_spec bmg160_channels
[] = {
746 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
747 BIT(IIO_CHAN_INFO_SCALE
) |
748 BIT(IIO_CHAN_INFO_OFFSET
),
754 IIO_CHAN_SOFT_TIMESTAMP(3),
757 static const struct iio_info bmg160_info
= {
758 .attrs
= &bmg160_attrs_group
,
759 .read_raw
= bmg160_read_raw
,
760 .write_raw
= bmg160_write_raw
,
761 .read_event_value
= bmg160_read_event
,
762 .write_event_value
= bmg160_write_event
,
763 .write_event_config
= bmg160_write_event_config
,
764 .read_event_config
= bmg160_read_event_config
,
765 .driver_module
= THIS_MODULE
,
768 static const unsigned long bmg160_accel_scan_masks
[] = {
769 BIT(AXIS_X
) | BIT(AXIS_Y
) | BIT(AXIS_Z
),
772 static irqreturn_t
bmg160_trigger_handler(int irq
, void *p
)
774 struct iio_poll_func
*pf
= p
;
775 struct iio_dev
*indio_dev
= pf
->indio_dev
;
776 struct bmg160_data
*data
= iio_priv(indio_dev
);
779 mutex_lock(&data
->mutex
);
780 ret
= regmap_bulk_read(data
->regmap
, BMG160_REG_XOUT_L
,
781 data
->buffer
, AXIS_MAX
* 2);
782 mutex_unlock(&data
->mutex
);
786 iio_push_to_buffers_with_timestamp(indio_dev
, data
->buffer
,
789 iio_trigger_notify_done(indio_dev
->trig
);
794 static int bmg160_trig_try_reen(struct iio_trigger
*trig
)
796 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
797 struct bmg160_data
*data
= iio_priv(indio_dev
);
798 struct device
*dev
= regmap_get_device(data
->regmap
);
801 /* new data interrupts don't need ack */
802 if (data
->dready_trigger_on
)
805 /* Set latched mode interrupt and clear any latched interrupt */
806 ret
= regmap_write(data
->regmap
, BMG160_REG_INT_RST_LATCH
,
807 BMG160_INT_MODE_LATCH_INT
|
808 BMG160_INT_MODE_LATCH_RESET
);
810 dev_err(dev
, "Error writing reg_rst_latch\n");
817 static int bmg160_data_rdy_trigger_set_state(struct iio_trigger
*trig
,
820 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
821 struct bmg160_data
*data
= iio_priv(indio_dev
);
824 mutex_lock(&data
->mutex
);
826 if (!state
&& data
->ev_enable_state
&& data
->motion_trigger_on
) {
827 data
->motion_trigger_on
= false;
828 mutex_unlock(&data
->mutex
);
833 * Refer to comment in bmg160_write_event_config for
834 * enable/disable operation order
836 ret
= bmg160_set_power_state(data
, state
);
838 mutex_unlock(&data
->mutex
);
841 if (data
->motion_trig
== trig
)
842 ret
= bmg160_setup_any_motion_interrupt(data
, state
);
844 ret
= bmg160_setup_new_data_interrupt(data
, state
);
846 bmg160_set_power_state(data
, false);
847 mutex_unlock(&data
->mutex
);
850 if (data
->motion_trig
== trig
)
851 data
->motion_trigger_on
= state
;
853 data
->dready_trigger_on
= state
;
855 mutex_unlock(&data
->mutex
);
860 static const struct iio_trigger_ops bmg160_trigger_ops
= {
861 .set_trigger_state
= bmg160_data_rdy_trigger_set_state
,
862 .try_reenable
= bmg160_trig_try_reen
,
863 .owner
= THIS_MODULE
,
866 static irqreturn_t
bmg160_event_handler(int irq
, void *private)
868 struct iio_dev
*indio_dev
= private;
869 struct bmg160_data
*data
= iio_priv(indio_dev
);
870 struct device
*dev
= regmap_get_device(data
->regmap
);
875 ret
= regmap_read(data
->regmap
, BMG160_REG_INT_STATUS_2
, &val
);
877 dev_err(dev
, "Error reading reg_int_status2\n");
878 goto ack_intr_status
;
882 dir
= IIO_EV_DIR_RISING
;
884 dir
= IIO_EV_DIR_FALLING
;
886 if (val
& BMG160_ANY_MOTION_BIT_X
)
887 iio_push_event(indio_dev
, IIO_MOD_EVENT_CODE(IIO_ANGL_VEL
,
893 if (val
& BMG160_ANY_MOTION_BIT_Y
)
894 iio_push_event(indio_dev
, IIO_MOD_EVENT_CODE(IIO_ANGL_VEL
,
900 if (val
& BMG160_ANY_MOTION_BIT_Z
)
901 iio_push_event(indio_dev
, IIO_MOD_EVENT_CODE(IIO_ANGL_VEL
,
909 if (!data
->dready_trigger_on
) {
910 ret
= regmap_write(data
->regmap
, BMG160_REG_INT_RST_LATCH
,
911 BMG160_INT_MODE_LATCH_INT
|
912 BMG160_INT_MODE_LATCH_RESET
);
914 dev_err(dev
, "Error writing reg_rst_latch\n");
920 static irqreturn_t
bmg160_data_rdy_trig_poll(int irq
, void *private)
922 struct iio_dev
*indio_dev
= private;
923 struct bmg160_data
*data
= iio_priv(indio_dev
);
925 if (data
->dready_trigger_on
)
926 iio_trigger_poll(data
->dready_trig
);
927 else if (data
->motion_trigger_on
)
928 iio_trigger_poll(data
->motion_trig
);
930 if (data
->ev_enable_state
)
931 return IRQ_WAKE_THREAD
;
937 static int bmg160_buffer_preenable(struct iio_dev
*indio_dev
)
939 struct bmg160_data
*data
= iio_priv(indio_dev
);
941 return bmg160_set_power_state(data
, true);
944 static int bmg160_buffer_postdisable(struct iio_dev
*indio_dev
)
946 struct bmg160_data
*data
= iio_priv(indio_dev
);
948 return bmg160_set_power_state(data
, false);
951 static const struct iio_buffer_setup_ops bmg160_buffer_setup_ops
= {
952 .preenable
= bmg160_buffer_preenable
,
953 .postenable
= iio_triggered_buffer_postenable
,
954 .predisable
= iio_triggered_buffer_predisable
,
955 .postdisable
= bmg160_buffer_postdisable
,
958 static const char *bmg160_match_acpi_device(struct device
*dev
)
960 const struct acpi_device_id
*id
;
962 id
= acpi_match_device(dev
->driver
->acpi_match_table
, dev
);
966 return dev_name(dev
);
969 int bmg160_core_probe(struct device
*dev
, struct regmap
*regmap
, int irq
,
972 struct bmg160_data
*data
;
973 struct iio_dev
*indio_dev
;
976 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*data
));
980 data
= iio_priv(indio_dev
);
981 dev_set_drvdata(dev
, indio_dev
);
983 data
->regmap
= regmap
;
985 ret
= bmg160_chip_init(data
);
989 mutex_init(&data
->mutex
);
991 if (ACPI_HANDLE(dev
))
992 name
= bmg160_match_acpi_device(dev
);
994 indio_dev
->dev
.parent
= dev
;
995 indio_dev
->channels
= bmg160_channels
;
996 indio_dev
->num_channels
= ARRAY_SIZE(bmg160_channels
);
997 indio_dev
->name
= name
;
998 indio_dev
->available_scan_masks
= bmg160_accel_scan_masks
;
999 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1000 indio_dev
->info
= &bmg160_info
;
1002 if (data
->irq
> 0) {
1003 ret
= devm_request_threaded_irq(dev
,
1005 bmg160_data_rdy_trig_poll
,
1006 bmg160_event_handler
,
1007 IRQF_TRIGGER_RISING
,
1013 data
->dready_trig
= devm_iio_trigger_alloc(dev
,
1017 if (!data
->dready_trig
)
1020 data
->motion_trig
= devm_iio_trigger_alloc(dev
,
1021 "%s-any-motion-dev%d",
1024 if (!data
->motion_trig
)
1027 data
->dready_trig
->dev
.parent
= dev
;
1028 data
->dready_trig
->ops
= &bmg160_trigger_ops
;
1029 iio_trigger_set_drvdata(data
->dready_trig
, indio_dev
);
1030 ret
= iio_trigger_register(data
->dready_trig
);
1034 data
->motion_trig
->dev
.parent
= dev
;
1035 data
->motion_trig
->ops
= &bmg160_trigger_ops
;
1036 iio_trigger_set_drvdata(data
->motion_trig
, indio_dev
);
1037 ret
= iio_trigger_register(data
->motion_trig
);
1039 data
->motion_trig
= NULL
;
1040 goto err_trigger_unregister
;
1044 ret
= iio_triggered_buffer_setup(indio_dev
,
1045 iio_pollfunc_store_time
,
1046 bmg160_trigger_handler
,
1047 &bmg160_buffer_setup_ops
);
1050 "iio triggered buffer setup failed\n");
1051 goto err_trigger_unregister
;
1054 ret
= pm_runtime_set_active(dev
);
1056 goto err_buffer_cleanup
;
1058 pm_runtime_enable(dev
);
1059 pm_runtime_set_autosuspend_delay(dev
,
1060 BMG160_AUTO_SUSPEND_DELAY_MS
);
1061 pm_runtime_use_autosuspend(dev
);
1063 ret
= iio_device_register(indio_dev
);
1065 dev_err(dev
, "unable to register iio device\n");
1066 goto err_buffer_cleanup
;
1072 iio_triggered_buffer_cleanup(indio_dev
);
1073 err_trigger_unregister
:
1074 if (data
->dready_trig
)
1075 iio_trigger_unregister(data
->dready_trig
);
1076 if (data
->motion_trig
)
1077 iio_trigger_unregister(data
->motion_trig
);
1081 EXPORT_SYMBOL_GPL(bmg160_core_probe
);
1083 void bmg160_core_remove(struct device
*dev
)
1085 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1086 struct bmg160_data
*data
= iio_priv(indio_dev
);
1088 iio_device_unregister(indio_dev
);
1090 pm_runtime_disable(dev
);
1091 pm_runtime_set_suspended(dev
);
1092 pm_runtime_put_noidle(dev
);
1094 iio_triggered_buffer_cleanup(indio_dev
);
1096 if (data
->dready_trig
) {
1097 iio_trigger_unregister(data
->dready_trig
);
1098 iio_trigger_unregister(data
->motion_trig
);
1101 mutex_lock(&data
->mutex
);
1102 bmg160_set_mode(data
, BMG160_MODE_DEEP_SUSPEND
);
1103 mutex_unlock(&data
->mutex
);
1105 EXPORT_SYMBOL_GPL(bmg160_core_remove
);
1107 #ifdef CONFIG_PM_SLEEP
1108 static int bmg160_suspend(struct device
*dev
)
1110 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1111 struct bmg160_data
*data
= iio_priv(indio_dev
);
1113 mutex_lock(&data
->mutex
);
1114 bmg160_set_mode(data
, BMG160_MODE_SUSPEND
);
1115 mutex_unlock(&data
->mutex
);
1120 static int bmg160_resume(struct device
*dev
)
1122 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1123 struct bmg160_data
*data
= iio_priv(indio_dev
);
1125 mutex_lock(&data
->mutex
);
1126 if (data
->dready_trigger_on
|| data
->motion_trigger_on
||
1127 data
->ev_enable_state
)
1128 bmg160_set_mode(data
, BMG160_MODE_NORMAL
);
1129 mutex_unlock(&data
->mutex
);
1136 static int bmg160_runtime_suspend(struct device
*dev
)
1138 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1139 struct bmg160_data
*data
= iio_priv(indio_dev
);
1142 ret
= bmg160_set_mode(data
, BMG160_MODE_SUSPEND
);
1144 dev_err(dev
, "set mode failed\n");
1151 static int bmg160_runtime_resume(struct device
*dev
)
1153 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1154 struct bmg160_data
*data
= iio_priv(indio_dev
);
1157 ret
= bmg160_set_mode(data
, BMG160_MODE_NORMAL
);
1161 msleep_interruptible(BMG160_MAX_STARTUP_TIME_MS
);
1167 const struct dev_pm_ops bmg160_pm_ops
= {
1168 SET_SYSTEM_SLEEP_PM_OPS(bmg160_suspend
, bmg160_resume
)
1169 SET_RUNTIME_PM_OPS(bmg160_runtime_suspend
,
1170 bmg160_runtime_resume
, NULL
)
1172 EXPORT_SYMBOL_GPL(bmg160_pm_ops
);
1174 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
1175 MODULE_LICENSE("GPL v2");
1176 MODULE_DESCRIPTION("BMG160 Gyro driver");