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>
20 #include <linux/gpio/consumer.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/iio/iio.h>
24 #include <linux/iio/sysfs.h>
25 #include <linux/iio/buffer.h>
26 #include <linux/iio/trigger.h>
27 #include <linux/iio/events.h>
28 #include <linux/iio/trigger_consumer.h>
29 #include <linux/iio/triggered_buffer.h>
30 #include <linux/regmap.h>
33 #define BMG160_IRQ_NAME "bmg160_event"
34 #define BMG160_GPIO_NAME "gpio_int"
36 #define BMG160_REG_CHIP_ID 0x00
37 #define BMG160_CHIP_ID_VAL 0x0F
39 #define BMG160_REG_PMU_LPW 0x11
40 #define BMG160_MODE_NORMAL 0x00
41 #define BMG160_MODE_DEEP_SUSPEND 0x20
42 #define BMG160_MODE_SUSPEND 0x80
44 #define BMG160_REG_RANGE 0x0F
46 #define BMG160_RANGE_2000DPS 0
47 #define BMG160_RANGE_1000DPS 1
48 #define BMG160_RANGE_500DPS 2
49 #define BMG160_RANGE_250DPS 3
50 #define BMG160_RANGE_125DPS 4
52 #define BMG160_REG_PMU_BW 0x10
53 #define BMG160_NO_FILTER 0
54 #define BMG160_DEF_BW 100
56 #define BMG160_REG_INT_MAP_0 0x17
57 #define BMG160_INT_MAP_0_BIT_ANY BIT(1)
59 #define BMG160_REG_INT_MAP_1 0x18
60 #define BMG160_INT_MAP_1_BIT_NEW_DATA BIT(0)
62 #define BMG160_REG_INT_RST_LATCH 0x21
63 #define BMG160_INT_MODE_LATCH_RESET 0x80
64 #define BMG160_INT_MODE_LATCH_INT 0x0F
65 #define BMG160_INT_MODE_NON_LATCH_INT 0x00
67 #define BMG160_REG_INT_EN_0 0x15
68 #define BMG160_DATA_ENABLE_INT BIT(7)
70 #define BMG160_REG_INT_EN_1 0x16
71 #define BMG160_INT1_BIT_OD BIT(1)
73 #define BMG160_REG_XOUT_L 0x02
74 #define BMG160_AXIS_TO_REG(axis) (BMG160_REG_XOUT_L + (axis * 2))
76 #define BMG160_REG_SLOPE_THRES 0x1B
77 #define BMG160_SLOPE_THRES_MASK 0x0F
79 #define BMG160_REG_MOTION_INTR 0x1C
80 #define BMG160_INT_MOTION_X BIT(0)
81 #define BMG160_INT_MOTION_Y BIT(1)
82 #define BMG160_INT_MOTION_Z BIT(2)
83 #define BMG160_ANY_DUR_MASK 0x30
84 #define BMG160_ANY_DUR_SHIFT 4
86 #define BMG160_REG_INT_STATUS_2 0x0B
87 #define BMG160_ANY_MOTION_MASK 0x07
88 #define BMG160_ANY_MOTION_BIT_X BIT(0)
89 #define BMG160_ANY_MOTION_BIT_Y BIT(1)
90 #define BMG160_ANY_MOTION_BIT_Z BIT(2)
92 #define BMG160_REG_TEMP 0x08
93 #define BMG160_TEMP_CENTER_VAL 23
95 #define BMG160_MAX_STARTUP_TIME_MS 80
97 #define BMG160_AUTO_SUSPEND_DELAY_MS 2000
101 struct regmap
*regmap
;
102 struct iio_trigger
*dready_trig
;
103 struct iio_trigger
*motion_trig
;
110 bool dready_trigger_on
;
111 bool motion_trigger_on
;
121 static const struct {
124 } bmg160_samp_freq_table
[] = { {100, 0x07},
130 static const struct {
133 } bmg160_scale_table
[] = { { 1065, BMG160_RANGE_2000DPS
},
134 { 532, BMG160_RANGE_1000DPS
},
135 { 266, BMG160_RANGE_500DPS
},
136 { 133, BMG160_RANGE_250DPS
},
137 { 66, BMG160_RANGE_125DPS
} };
139 static int bmg160_set_mode(struct bmg160_data
*data
, u8 mode
)
143 ret
= regmap_write(data
->regmap
, BMG160_REG_PMU_LPW
, mode
);
145 dev_err(data
->dev
, "Error writing reg_pmu_lpw\n");
152 static int bmg160_convert_freq_to_bit(int val
)
156 for (i
= 0; i
< ARRAY_SIZE(bmg160_samp_freq_table
); ++i
) {
157 if (bmg160_samp_freq_table
[i
].val
== val
)
158 return bmg160_samp_freq_table
[i
].bw_bits
;
164 static int bmg160_set_bw(struct bmg160_data
*data
, int val
)
169 bw_bits
= bmg160_convert_freq_to_bit(val
);
173 ret
= regmap_write(data
->regmap
, BMG160_REG_PMU_BW
, bw_bits
);
175 dev_err(data
->dev
, "Error writing reg_pmu_bw\n");
179 data
->bw_bits
= bw_bits
;
184 static int bmg160_chip_init(struct bmg160_data
*data
)
189 ret
= regmap_read(data
->regmap
, BMG160_REG_CHIP_ID
, &val
);
191 dev_err(data
->dev
, "Error reading reg_chip_id\n");
195 dev_dbg(data
->dev
, "Chip Id %x\n", val
);
196 if (val
!= BMG160_CHIP_ID_VAL
) {
197 dev_err(data
->dev
, "invalid chip %x\n", val
);
201 ret
= bmg160_set_mode(data
, BMG160_MODE_NORMAL
);
205 /* Wait upto 500 ms to be ready after changing mode */
206 usleep_range(500, 1000);
209 ret
= bmg160_set_bw(data
, BMG160_DEF_BW
);
213 /* Set Default Range */
214 ret
= regmap_write(data
->regmap
, BMG160_REG_RANGE
, BMG160_RANGE_500DPS
);
216 dev_err(data
->dev
, "Error writing reg_range\n");
219 data
->dps_range
= BMG160_RANGE_500DPS
;
221 ret
= regmap_read(data
->regmap
, BMG160_REG_SLOPE_THRES
, &val
);
223 dev_err(data
->dev
, "Error reading reg_slope_thres\n");
226 data
->slope_thres
= val
;
228 /* Set default interrupt mode */
229 ret
= regmap_update_bits(data
->regmap
, BMG160_REG_INT_EN_1
,
230 BMG160_INT1_BIT_OD
, 0);
232 dev_err(data
->dev
, "Error updating bits in reg_int_en_1\n");
236 ret
= regmap_write(data
->regmap
, BMG160_REG_INT_RST_LATCH
,
237 BMG160_INT_MODE_LATCH_INT
|
238 BMG160_INT_MODE_LATCH_RESET
);
241 "Error writing reg_motion_intr\n");
248 static int bmg160_set_power_state(struct bmg160_data
*data
, bool on
)
254 ret
= pm_runtime_get_sync(data
->dev
);
256 pm_runtime_mark_last_busy(data
->dev
);
257 ret
= pm_runtime_put_autosuspend(data
->dev
);
262 "Failed: bmg160_set_power_state for %d\n", on
);
264 pm_runtime_put_noidle(data
->dev
);
273 static int bmg160_setup_any_motion_interrupt(struct bmg160_data
*data
,
278 /* Enable/Disable INT_MAP0 mapping */
279 ret
= regmap_update_bits(data
->regmap
, BMG160_REG_INT_MAP_0
,
280 BMG160_INT_MAP_0_BIT_ANY
,
281 (status
? BMG160_INT_MAP_0_BIT_ANY
: 0));
283 dev_err(data
->dev
, "Error updating bits reg_int_map0\n");
287 /* Enable/Disable slope interrupts */
289 /* Update slope thres */
290 ret
= regmap_write(data
->regmap
, BMG160_REG_SLOPE_THRES
,
294 "Error writing reg_slope_thres\n");
298 ret
= regmap_write(data
->regmap
, BMG160_REG_MOTION_INTR
,
299 BMG160_INT_MOTION_X
| BMG160_INT_MOTION_Y
|
300 BMG160_INT_MOTION_Z
);
303 "Error writing reg_motion_intr\n");
308 * New data interrupt is always non-latched,
309 * which will have higher priority, so no need
310 * to set latched mode, we will be flooded anyway with INTR
312 if (!data
->dready_trigger_on
) {
313 ret
= regmap_write(data
->regmap
,
314 BMG160_REG_INT_RST_LATCH
,
315 BMG160_INT_MODE_LATCH_INT
|
316 BMG160_INT_MODE_LATCH_RESET
);
319 "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(data
->dev
, "Error writing reg_int_en0\n");
339 static int bmg160_setup_new_data_interrupt(struct bmg160_data
*data
,
344 /* Enable/Disable INT_MAP1 mapping */
345 ret
= regmap_update_bits(data
->regmap
, BMG160_REG_INT_MAP_1
,
346 BMG160_INT_MAP_1_BIT_NEW_DATA
,
347 (status
? BMG160_INT_MAP_1_BIT_NEW_DATA
: 0));
349 dev_err(data
->dev
, "Error updating bits in reg_int_map1\n");
354 ret
= regmap_write(data
->regmap
, BMG160_REG_INT_RST_LATCH
,
355 BMG160_INT_MODE_NON_LATCH_INT
|
356 BMG160_INT_MODE_LATCH_RESET
);
359 "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
);
373 "Error writing reg_rst_latch\n");
377 ret
= regmap_write(data
->regmap
, BMG160_REG_INT_EN_0
, 0);
381 dev_err(data
->dev
, "Error writing reg_int_en0\n");
388 static int bmg160_get_bw(struct bmg160_data
*data
, int *val
)
392 for (i
= 0; i
< ARRAY_SIZE(bmg160_samp_freq_table
); ++i
) {
393 if (bmg160_samp_freq_table
[i
].bw_bits
== data
->bw_bits
) {
394 *val
= bmg160_samp_freq_table
[i
].val
;
402 static int bmg160_set_scale(struct bmg160_data
*data
, int val
)
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
);
412 "Error writing reg_range\n");
415 data
->dps_range
= bmg160_scale_table
[i
].dps_range
;
423 static int bmg160_get_temp(struct bmg160_data
*data
, int *val
)
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(data
->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
)
455 unsigned int raw_val
;
457 mutex_lock(&data
->mutex
);
458 ret
= bmg160_set_power_state(data
, true);
460 mutex_unlock(&data
->mutex
);
464 ret
= regmap_bulk_read(data
->regmap
, BMG160_AXIS_TO_REG(axis
), &raw_val
,
467 dev_err(data
->dev
, "Error reading axis %d\n", axis
);
468 bmg160_set_power_state(data
, false);
469 mutex_unlock(&data
->mutex
);
473 *val
= sign_extend32(raw_val
, 15);
474 ret
= bmg160_set_power_state(data
, false);
475 mutex_unlock(&data
->mutex
);
482 static int bmg160_read_raw(struct iio_dev
*indio_dev
,
483 struct iio_chan_spec
const *chan
,
484 int *val
, int *val2
, long mask
)
486 struct bmg160_data
*data
= iio_priv(indio_dev
);
490 case IIO_CHAN_INFO_RAW
:
491 switch (chan
->type
) {
493 return bmg160_get_temp(data
, val
);
495 if (iio_buffer_enabled(indio_dev
))
498 return bmg160_get_axis(data
, chan
->scan_index
,
503 case IIO_CHAN_INFO_OFFSET
:
504 if (chan
->type
== IIO_TEMP
) {
505 *val
= BMG160_TEMP_CENTER_VAL
;
509 case IIO_CHAN_INFO_SCALE
:
511 switch (chan
->type
) {
514 return IIO_VAL_INT_PLUS_MICRO
;
519 for (i
= 0; i
< ARRAY_SIZE(bmg160_scale_table
); ++i
) {
520 if (bmg160_scale_table
[i
].dps_range
==
522 *val2
= bmg160_scale_table
[i
].scale
;
523 return IIO_VAL_INT_PLUS_MICRO
;
531 case IIO_CHAN_INFO_SAMP_FREQ
:
533 mutex_lock(&data
->mutex
);
534 ret
= bmg160_get_bw(data
, val
);
535 mutex_unlock(&data
->mutex
);
542 static int bmg160_write_raw(struct iio_dev
*indio_dev
,
543 struct iio_chan_spec
const *chan
,
544 int val
, int val2
, long mask
)
546 struct bmg160_data
*data
= iio_priv(indio_dev
);
550 case IIO_CHAN_INFO_SAMP_FREQ
:
551 mutex_lock(&data
->mutex
);
553 * Section 4.2 of spec
554 * In suspend mode, the only supported operations are reading
555 * registers as well as writing to the (0x14) softreset
556 * register. Since we will be in suspend mode by default, change
557 * mode to power on for other writes.
559 ret
= bmg160_set_power_state(data
, true);
561 mutex_unlock(&data
->mutex
);
564 ret
= bmg160_set_bw(data
, val
);
566 bmg160_set_power_state(data
, false);
567 mutex_unlock(&data
->mutex
);
570 ret
= bmg160_set_power_state(data
, false);
571 mutex_unlock(&data
->mutex
);
573 case IIO_CHAN_INFO_SCALE
:
577 mutex_lock(&data
->mutex
);
578 /* Refer to comments above for the suspend mode ops */
579 ret
= bmg160_set_power_state(data
, true);
581 mutex_unlock(&data
->mutex
);
584 ret
= bmg160_set_scale(data
, val2
);
586 bmg160_set_power_state(data
, false);
587 mutex_unlock(&data
->mutex
);
590 ret
= bmg160_set_power_state(data
, false);
591 mutex_unlock(&data
->mutex
);
600 static int bmg160_read_event(struct iio_dev
*indio_dev
,
601 const struct iio_chan_spec
*chan
,
602 enum iio_event_type type
,
603 enum iio_event_direction dir
,
604 enum iio_event_info info
,
607 struct bmg160_data
*data
= iio_priv(indio_dev
);
611 case IIO_EV_INFO_VALUE
:
612 *val
= data
->slope_thres
& BMG160_SLOPE_THRES_MASK
;
621 static int bmg160_write_event(struct iio_dev
*indio_dev
,
622 const struct iio_chan_spec
*chan
,
623 enum iio_event_type type
,
624 enum iio_event_direction dir
,
625 enum iio_event_info info
,
628 struct bmg160_data
*data
= iio_priv(indio_dev
);
631 case IIO_EV_INFO_VALUE
:
632 if (data
->ev_enable_state
)
634 data
->slope_thres
&= ~BMG160_SLOPE_THRES_MASK
;
635 data
->slope_thres
|= (val
& BMG160_SLOPE_THRES_MASK
);
644 static int bmg160_read_event_config(struct iio_dev
*indio_dev
,
645 const struct iio_chan_spec
*chan
,
646 enum iio_event_type type
,
647 enum iio_event_direction dir
)
650 struct bmg160_data
*data
= iio_priv(indio_dev
);
652 return data
->ev_enable_state
;
655 static int bmg160_write_event_config(struct iio_dev
*indio_dev
,
656 const struct iio_chan_spec
*chan
,
657 enum iio_event_type type
,
658 enum iio_event_direction dir
,
661 struct bmg160_data
*data
= iio_priv(indio_dev
);
664 if (state
&& data
->ev_enable_state
)
667 mutex_lock(&data
->mutex
);
669 if (!state
&& data
->motion_trigger_on
) {
670 data
->ev_enable_state
= 0;
671 mutex_unlock(&data
->mutex
);
675 * We will expect the enable and disable to do operation in
676 * in reverse order. This will happen here anyway as our
677 * resume operation uses sync mode runtime pm calls, the
678 * suspend operation will be delayed by autosuspend delay
679 * So the disable operation will still happen in reverse of
680 * enable operation. When runtime pm is disabled the mode
681 * is always on so sequence doesn't matter
683 ret
= bmg160_set_power_state(data
, state
);
685 mutex_unlock(&data
->mutex
);
689 ret
= bmg160_setup_any_motion_interrupt(data
, state
);
691 bmg160_set_power_state(data
, false);
692 mutex_unlock(&data
->mutex
);
696 data
->ev_enable_state
= state
;
697 mutex_unlock(&data
->mutex
);
702 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("100 200 400 1000 2000");
704 static IIO_CONST_ATTR(in_anglvel_scale_available
,
705 "0.001065 0.000532 0.000266 0.000133 0.000066");
707 static struct attribute
*bmg160_attributes
[] = {
708 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
709 &iio_const_attr_in_anglvel_scale_available
.dev_attr
.attr
,
713 static const struct attribute_group bmg160_attrs_group
= {
714 .attrs
= bmg160_attributes
,
717 static const struct iio_event_spec bmg160_event
= {
718 .type
= IIO_EV_TYPE_ROC
,
719 .dir
= IIO_EV_DIR_EITHER
,
720 .mask_shared_by_type
= BIT(IIO_EV_INFO_VALUE
) |
721 BIT(IIO_EV_INFO_ENABLE
)
724 #define BMG160_CHANNEL(_axis) { \
725 .type = IIO_ANGL_VEL, \
727 .channel2 = IIO_MOD_##_axis, \
728 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
729 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
730 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
731 .scan_index = AXIS_##_axis, \
737 .event_spec = &bmg160_event, \
738 .num_event_specs = 1 \
741 static const struct iio_chan_spec bmg160_channels
[] = {
744 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
745 BIT(IIO_CHAN_INFO_SCALE
) |
746 BIT(IIO_CHAN_INFO_OFFSET
),
752 IIO_CHAN_SOFT_TIMESTAMP(3),
755 static const struct iio_info bmg160_info
= {
756 .attrs
= &bmg160_attrs_group
,
757 .read_raw
= bmg160_read_raw
,
758 .write_raw
= bmg160_write_raw
,
759 .read_event_value
= bmg160_read_event
,
760 .write_event_value
= bmg160_write_event
,
761 .write_event_config
= bmg160_write_event_config
,
762 .read_event_config
= bmg160_read_event_config
,
763 .driver_module
= THIS_MODULE
,
766 static irqreturn_t
bmg160_trigger_handler(int irq
, void *p
)
768 struct iio_poll_func
*pf
= p
;
769 struct iio_dev
*indio_dev
= pf
->indio_dev
;
770 struct bmg160_data
*data
= iio_priv(indio_dev
);
774 mutex_lock(&data
->mutex
);
775 for_each_set_bit(bit
, indio_dev
->active_scan_mask
,
776 indio_dev
->masklength
) {
777 ret
= regmap_bulk_read(data
->regmap
, BMG160_AXIS_TO_REG(bit
),
780 mutex_unlock(&data
->mutex
);
783 data
->buffer
[i
++] = ret
;
785 mutex_unlock(&data
->mutex
);
787 iio_push_to_buffers_with_timestamp(indio_dev
, data
->buffer
,
790 iio_trigger_notify_done(indio_dev
->trig
);
795 static int bmg160_trig_try_reen(struct iio_trigger
*trig
)
797 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
798 struct bmg160_data
*data
= iio_priv(indio_dev
);
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(data
->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
);
874 ret
= regmap_read(data
->regmap
, BMG160_REG_INT_STATUS_2
, &val
);
876 dev_err(data
->dev
, "Error reading reg_int_status2\n");
877 goto ack_intr_status
;
881 dir
= IIO_EV_DIR_RISING
;
883 dir
= IIO_EV_DIR_FALLING
;
885 if (val
& BMG160_ANY_MOTION_BIT_X
)
886 iio_push_event(indio_dev
, IIO_MOD_EVENT_CODE(IIO_ANGL_VEL
,
892 if (val
& BMG160_ANY_MOTION_BIT_Y
)
893 iio_push_event(indio_dev
, IIO_MOD_EVENT_CODE(IIO_ANGL_VEL
,
899 if (val
& BMG160_ANY_MOTION_BIT_Z
)
900 iio_push_event(indio_dev
, IIO_MOD_EVENT_CODE(IIO_ANGL_VEL
,
908 if (!data
->dready_trigger_on
) {
909 ret
= regmap_write(data
->regmap
, BMG160_REG_INT_RST_LATCH
,
910 BMG160_INT_MODE_LATCH_INT
|
911 BMG160_INT_MODE_LATCH_RESET
);
914 "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 int bmg160_gpio_probe(struct bmg160_data
*data
)
962 struct gpio_desc
*gpio
;
966 /* data ready gpio interrupt pin */
967 gpio
= devm_gpiod_get_index(dev
, BMG160_GPIO_NAME
, 0, GPIOD_IN
);
969 dev_err(dev
, "acpi gpio get index failed\n");
970 return PTR_ERR(gpio
);
973 data
->irq
= gpiod_to_irq(gpio
);
975 dev_dbg(dev
, "GPIO resource, no:%d irq:%d\n", desc_to_gpio(gpio
),
981 static const char *bmg160_match_acpi_device(struct device
*dev
)
983 const struct acpi_device_id
*id
;
985 id
= acpi_match_device(dev
->driver
->acpi_match_table
, dev
);
989 return dev_name(dev
);
992 int bmg160_core_probe(struct device
*dev
, struct regmap
*regmap
, int irq
,
995 struct bmg160_data
*data
;
996 struct iio_dev
*indio_dev
;
999 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*data
));
1003 data
= iio_priv(indio_dev
);
1004 dev_set_drvdata(dev
, indio_dev
);
1007 data
->regmap
= regmap
;
1009 ret
= bmg160_chip_init(data
);
1013 mutex_init(&data
->mutex
);
1015 if (ACPI_HANDLE(dev
))
1016 name
= bmg160_match_acpi_device(dev
);
1018 indio_dev
->dev
.parent
= dev
;
1019 indio_dev
->channels
= bmg160_channels
;
1020 indio_dev
->num_channels
= ARRAY_SIZE(bmg160_channels
);
1021 indio_dev
->name
= name
;
1022 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1023 indio_dev
->info
= &bmg160_info
;
1026 bmg160_gpio_probe(data
);
1028 if (data
->irq
> 0) {
1029 ret
= devm_request_threaded_irq(dev
,
1031 bmg160_data_rdy_trig_poll
,
1032 bmg160_event_handler
,
1033 IRQF_TRIGGER_RISING
,
1039 data
->dready_trig
= devm_iio_trigger_alloc(dev
,
1043 if (!data
->dready_trig
)
1046 data
->motion_trig
= devm_iio_trigger_alloc(dev
,
1047 "%s-any-motion-dev%d",
1050 if (!data
->motion_trig
)
1053 data
->dready_trig
->dev
.parent
= dev
;
1054 data
->dready_trig
->ops
= &bmg160_trigger_ops
;
1055 iio_trigger_set_drvdata(data
->dready_trig
, indio_dev
);
1056 ret
= iio_trigger_register(data
->dready_trig
);
1060 data
->motion_trig
->dev
.parent
= dev
;
1061 data
->motion_trig
->ops
= &bmg160_trigger_ops
;
1062 iio_trigger_set_drvdata(data
->motion_trig
, indio_dev
);
1063 ret
= iio_trigger_register(data
->motion_trig
);
1065 data
->motion_trig
= NULL
;
1066 goto err_trigger_unregister
;
1070 ret
= iio_triggered_buffer_setup(indio_dev
,
1071 iio_pollfunc_store_time
,
1072 bmg160_trigger_handler
,
1073 &bmg160_buffer_setup_ops
);
1076 "iio triggered buffer setup failed\n");
1077 goto err_trigger_unregister
;
1080 ret
= iio_device_register(indio_dev
);
1082 dev_err(dev
, "unable to register iio device\n");
1083 goto err_buffer_cleanup
;
1086 ret
= pm_runtime_set_active(dev
);
1088 goto err_iio_unregister
;
1090 pm_runtime_enable(dev
);
1091 pm_runtime_set_autosuspend_delay(dev
,
1092 BMG160_AUTO_SUSPEND_DELAY_MS
);
1093 pm_runtime_use_autosuspend(dev
);
1098 iio_device_unregister(indio_dev
);
1100 iio_triggered_buffer_cleanup(indio_dev
);
1101 err_trigger_unregister
:
1102 if (data
->dready_trig
)
1103 iio_trigger_unregister(data
->dready_trig
);
1104 if (data
->motion_trig
)
1105 iio_trigger_unregister(data
->motion_trig
);
1109 EXPORT_SYMBOL_GPL(bmg160_core_probe
);
1111 void bmg160_core_remove(struct device
*dev
)
1113 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1114 struct bmg160_data
*data
= iio_priv(indio_dev
);
1116 pm_runtime_disable(dev
);
1117 pm_runtime_set_suspended(dev
);
1118 pm_runtime_put_noidle(dev
);
1120 iio_device_unregister(indio_dev
);
1121 iio_triggered_buffer_cleanup(indio_dev
);
1123 if (data
->dready_trig
) {
1124 iio_trigger_unregister(data
->dready_trig
);
1125 iio_trigger_unregister(data
->motion_trig
);
1128 mutex_lock(&data
->mutex
);
1129 bmg160_set_mode(data
, BMG160_MODE_DEEP_SUSPEND
);
1130 mutex_unlock(&data
->mutex
);
1132 EXPORT_SYMBOL_GPL(bmg160_core_remove
);
1134 #ifdef CONFIG_PM_SLEEP
1135 static int bmg160_suspend(struct device
*dev
)
1137 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1138 struct bmg160_data
*data
= iio_priv(indio_dev
);
1140 mutex_lock(&data
->mutex
);
1141 bmg160_set_mode(data
, BMG160_MODE_SUSPEND
);
1142 mutex_unlock(&data
->mutex
);
1147 static int bmg160_resume(struct device
*dev
)
1149 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1150 struct bmg160_data
*data
= iio_priv(indio_dev
);
1152 mutex_lock(&data
->mutex
);
1153 if (data
->dready_trigger_on
|| data
->motion_trigger_on
||
1154 data
->ev_enable_state
)
1155 bmg160_set_mode(data
, BMG160_MODE_NORMAL
);
1156 mutex_unlock(&data
->mutex
);
1163 static int bmg160_runtime_suspend(struct device
*dev
)
1165 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1166 struct bmg160_data
*data
= iio_priv(indio_dev
);
1169 ret
= bmg160_set_mode(data
, BMG160_MODE_SUSPEND
);
1171 dev_err(data
->dev
, "set mode failed\n");
1178 static int bmg160_runtime_resume(struct device
*dev
)
1180 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1181 struct bmg160_data
*data
= iio_priv(indio_dev
);
1184 ret
= bmg160_set_mode(data
, BMG160_MODE_NORMAL
);
1188 msleep_interruptible(BMG160_MAX_STARTUP_TIME_MS
);
1194 const struct dev_pm_ops bmg160_pm_ops
= {
1195 SET_SYSTEM_SLEEP_PM_OPS(bmg160_suspend
, bmg160_resume
)
1196 SET_RUNTIME_PM_OPS(bmg160_runtime_suspend
,
1197 bmg160_runtime_resume
, NULL
)
1199 EXPORT_SYMBOL_GPL(bmg160_pm_ops
);
1201 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
1202 MODULE_LICENSE("GPL v2");
1203 MODULE_DESCRIPTION("BMG160 Gyro driver");