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/i2c.h>
17 #include <linux/interrupt.h>
18 #include <linux/delay.h>
19 #include <linux/slab.h>
20 #include <linux/acpi.h>
21 #include <linux/gpio/consumer.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/iio/iio.h>
25 #include <linux/iio/sysfs.h>
26 #include <linux/iio/buffer.h>
27 #include <linux/iio/trigger.h>
28 #include <linux/iio/events.h>
29 #include <linux/iio/trigger_consumer.h>
30 #include <linux/iio/triggered_buffer.h>
32 #define BMG160_DRV_NAME "bmg160"
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
100 struct i2c_client
*client
;
101 struct iio_trigger
*dready_trig
;
102 struct iio_trigger
*motion_trig
;
109 bool dready_trigger_on
;
110 bool motion_trigger_on
;
120 static const struct {
123 } bmg160_samp_freq_table
[] = { {100, 0x07},
129 static const struct {
132 } bmg160_scale_table
[] = { { 1065, BMG160_RANGE_2000DPS
},
133 { 532, BMG160_RANGE_1000DPS
},
134 { 266, BMG160_RANGE_500DPS
},
135 { 133, BMG160_RANGE_250DPS
},
136 { 66, BMG160_RANGE_125DPS
} };
138 static int bmg160_set_mode(struct bmg160_data
*data
, u8 mode
)
142 ret
= i2c_smbus_write_byte_data(data
->client
,
143 BMG160_REG_PMU_LPW
, mode
);
145 dev_err(&data
->client
->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
= i2c_smbus_write_byte_data(data
->client
, BMG160_REG_PMU_BW
,
176 dev_err(&data
->client
->dev
, "Error writing reg_pmu_bw\n");
180 data
->bw_bits
= bw_bits
;
185 static int bmg160_chip_init(struct bmg160_data
*data
)
189 ret
= i2c_smbus_read_byte_data(data
->client
, BMG160_REG_CHIP_ID
);
191 dev_err(&data
->client
->dev
, "Error reading reg_chip_id\n");
195 dev_dbg(&data
->client
->dev
, "Chip Id %x\n", ret
);
196 if (ret
!= BMG160_CHIP_ID_VAL
) {
197 dev_err(&data
->client
->dev
, "invalid chip %x\n", ret
);
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
= i2c_smbus_write_byte_data(data
->client
,
216 BMG160_RANGE_500DPS
);
218 dev_err(&data
->client
->dev
, "Error writing reg_range\n");
221 data
->dps_range
= BMG160_RANGE_500DPS
;
223 ret
= i2c_smbus_read_byte_data(data
->client
, BMG160_REG_SLOPE_THRES
);
225 dev_err(&data
->client
->dev
, "Error reading reg_slope_thres\n");
228 data
->slope_thres
= ret
;
230 /* Set default interrupt mode */
231 ret
= i2c_smbus_read_byte_data(data
->client
, BMG160_REG_INT_EN_1
);
233 dev_err(&data
->client
->dev
, "Error reading reg_int_en_1\n");
236 ret
&= ~BMG160_INT1_BIT_OD
;
237 ret
= i2c_smbus_write_byte_data(data
->client
,
238 BMG160_REG_INT_EN_1
, ret
);
240 dev_err(&data
->client
->dev
, "Error writing reg_int_en_1\n");
244 ret
= i2c_smbus_write_byte_data(data
->client
,
245 BMG160_REG_INT_RST_LATCH
,
246 BMG160_INT_MODE_LATCH_INT
|
247 BMG160_INT_MODE_LATCH_RESET
);
249 dev_err(&data
->client
->dev
,
250 "Error writing reg_motion_intr\n");
257 static int bmg160_set_power_state(struct bmg160_data
*data
, bool on
)
263 ret
= pm_runtime_get_sync(&data
->client
->dev
);
265 pm_runtime_mark_last_busy(&data
->client
->dev
);
266 ret
= pm_runtime_put_autosuspend(&data
->client
->dev
);
270 dev_err(&data
->client
->dev
,
271 "Failed: bmg160_set_power_state for %d\n", on
);
273 pm_runtime_put_noidle(&data
->client
->dev
);
282 static int bmg160_setup_any_motion_interrupt(struct bmg160_data
*data
,
287 /* Enable/Disable INT_MAP0 mapping */
288 ret
= i2c_smbus_read_byte_data(data
->client
, BMG160_REG_INT_MAP_0
);
290 dev_err(&data
->client
->dev
, "Error reading reg_int_map0\n");
294 ret
|= BMG160_INT_MAP_0_BIT_ANY
;
296 ret
&= ~BMG160_INT_MAP_0_BIT_ANY
;
298 ret
= i2c_smbus_write_byte_data(data
->client
,
299 BMG160_REG_INT_MAP_0
,
302 dev_err(&data
->client
->dev
, "Error writing reg_int_map0\n");
306 /* Enable/Disable slope interrupts */
308 /* Update slope thres */
309 ret
= i2c_smbus_write_byte_data(data
->client
,
310 BMG160_REG_SLOPE_THRES
,
313 dev_err(&data
->client
->dev
,
314 "Error writing reg_slope_thres\n");
318 ret
= i2c_smbus_write_byte_data(data
->client
,
319 BMG160_REG_MOTION_INTR
,
320 BMG160_INT_MOTION_X
|
321 BMG160_INT_MOTION_Y
|
322 BMG160_INT_MOTION_Z
);
324 dev_err(&data
->client
->dev
,
325 "Error writing reg_motion_intr\n");
330 * New data interrupt is always non-latched,
331 * which will have higher priority, so no need
332 * to set latched mode, we will be flooded anyway with INTR
334 if (!data
->dready_trigger_on
) {
335 ret
= i2c_smbus_write_byte_data(data
->client
,
336 BMG160_REG_INT_RST_LATCH
,
337 BMG160_INT_MODE_LATCH_INT
|
338 BMG160_INT_MODE_LATCH_RESET
);
340 dev_err(&data
->client
->dev
,
341 "Error writing reg_rst_latch\n");
346 ret
= i2c_smbus_write_byte_data(data
->client
,
348 BMG160_DATA_ENABLE_INT
);
351 ret
= i2c_smbus_write_byte_data(data
->client
,
356 dev_err(&data
->client
->dev
, "Error writing reg_int_en0\n");
363 static int bmg160_setup_new_data_interrupt(struct bmg160_data
*data
,
368 /* Enable/Disable INT_MAP1 mapping */
369 ret
= i2c_smbus_read_byte_data(data
->client
, BMG160_REG_INT_MAP_1
);
371 dev_err(&data
->client
->dev
, "Error reading reg_int_map1\n");
376 ret
|= BMG160_INT_MAP_1_BIT_NEW_DATA
;
378 ret
&= ~BMG160_INT_MAP_1_BIT_NEW_DATA
;
380 ret
= i2c_smbus_write_byte_data(data
->client
,
381 BMG160_REG_INT_MAP_1
,
384 dev_err(&data
->client
->dev
, "Error writing reg_int_map1\n");
389 ret
= i2c_smbus_write_byte_data(data
->client
,
390 BMG160_REG_INT_RST_LATCH
,
391 BMG160_INT_MODE_NON_LATCH_INT
|
392 BMG160_INT_MODE_LATCH_RESET
);
394 dev_err(&data
->client
->dev
,
395 "Error writing reg_rst_latch\n");
399 ret
= i2c_smbus_write_byte_data(data
->client
,
401 BMG160_DATA_ENABLE_INT
);
404 /* Restore interrupt mode */
405 ret
= i2c_smbus_write_byte_data(data
->client
,
406 BMG160_REG_INT_RST_LATCH
,
407 BMG160_INT_MODE_LATCH_INT
|
408 BMG160_INT_MODE_LATCH_RESET
);
410 dev_err(&data
->client
->dev
,
411 "Error writing reg_rst_latch\n");
415 ret
= i2c_smbus_write_byte_data(data
->client
,
421 dev_err(&data
->client
->dev
, "Error writing reg_int_en0\n");
428 static int bmg160_get_bw(struct bmg160_data
*data
, int *val
)
432 for (i
= 0; i
< ARRAY_SIZE(bmg160_samp_freq_table
); ++i
) {
433 if (bmg160_samp_freq_table
[i
].bw_bits
== data
->bw_bits
) {
434 *val
= bmg160_samp_freq_table
[i
].val
;
442 static int bmg160_set_scale(struct bmg160_data
*data
, int val
)
446 for (i
= 0; i
< ARRAY_SIZE(bmg160_scale_table
); ++i
) {
447 if (bmg160_scale_table
[i
].scale
== val
) {
448 ret
= i2c_smbus_write_byte_data(
451 bmg160_scale_table
[i
].dps_range
);
453 dev_err(&data
->client
->dev
,
454 "Error writing reg_range\n");
457 data
->dps_range
= bmg160_scale_table
[i
].dps_range
;
465 static int bmg160_get_temp(struct bmg160_data
*data
, int *val
)
469 mutex_lock(&data
->mutex
);
470 ret
= bmg160_set_power_state(data
, true);
472 mutex_unlock(&data
->mutex
);
476 ret
= i2c_smbus_read_byte_data(data
->client
, BMG160_REG_TEMP
);
478 dev_err(&data
->client
->dev
, "Error reading reg_temp\n");
479 bmg160_set_power_state(data
, false);
480 mutex_unlock(&data
->mutex
);
484 *val
= sign_extend32(ret
, 7);
485 ret
= bmg160_set_power_state(data
, false);
486 mutex_unlock(&data
->mutex
);
493 static int bmg160_get_axis(struct bmg160_data
*data
, int axis
, int *val
)
497 mutex_lock(&data
->mutex
);
498 ret
= bmg160_set_power_state(data
, true);
500 mutex_unlock(&data
->mutex
);
504 ret
= i2c_smbus_read_word_data(data
->client
, BMG160_AXIS_TO_REG(axis
));
506 dev_err(&data
->client
->dev
, "Error reading axis %d\n", axis
);
507 bmg160_set_power_state(data
, false);
508 mutex_unlock(&data
->mutex
);
512 *val
= sign_extend32(ret
, 15);
513 ret
= bmg160_set_power_state(data
, false);
514 mutex_unlock(&data
->mutex
);
521 static int bmg160_read_raw(struct iio_dev
*indio_dev
,
522 struct iio_chan_spec
const *chan
,
523 int *val
, int *val2
, long mask
)
525 struct bmg160_data
*data
= iio_priv(indio_dev
);
529 case IIO_CHAN_INFO_RAW
:
530 switch (chan
->type
) {
532 return bmg160_get_temp(data
, val
);
534 if (iio_buffer_enabled(indio_dev
))
537 return bmg160_get_axis(data
, chan
->scan_index
,
542 case IIO_CHAN_INFO_OFFSET
:
543 if (chan
->type
== IIO_TEMP
) {
544 *val
= BMG160_TEMP_CENTER_VAL
;
548 case IIO_CHAN_INFO_SCALE
:
550 switch (chan
->type
) {
553 return IIO_VAL_INT_PLUS_MICRO
;
558 for (i
= 0; i
< ARRAY_SIZE(bmg160_scale_table
); ++i
) {
559 if (bmg160_scale_table
[i
].dps_range
==
561 *val2
= bmg160_scale_table
[i
].scale
;
562 return IIO_VAL_INT_PLUS_MICRO
;
570 case IIO_CHAN_INFO_SAMP_FREQ
:
572 mutex_lock(&data
->mutex
);
573 ret
= bmg160_get_bw(data
, val
);
574 mutex_unlock(&data
->mutex
);
581 static int bmg160_write_raw(struct iio_dev
*indio_dev
,
582 struct iio_chan_spec
const *chan
,
583 int val
, int val2
, long mask
)
585 struct bmg160_data
*data
= iio_priv(indio_dev
);
589 case IIO_CHAN_INFO_SAMP_FREQ
:
590 mutex_lock(&data
->mutex
);
592 * Section 4.2 of spec
593 * In suspend mode, the only supported operations are reading
594 * registers as well as writing to the (0x14) softreset
595 * register. Since we will be in suspend mode by default, change
596 * mode to power on for other writes.
598 ret
= bmg160_set_power_state(data
, true);
600 mutex_unlock(&data
->mutex
);
603 ret
= bmg160_set_bw(data
, val
);
605 bmg160_set_power_state(data
, false);
606 mutex_unlock(&data
->mutex
);
609 ret
= bmg160_set_power_state(data
, false);
610 mutex_unlock(&data
->mutex
);
612 case IIO_CHAN_INFO_SCALE
:
616 mutex_lock(&data
->mutex
);
617 /* Refer to comments above for the suspend mode ops */
618 ret
= bmg160_set_power_state(data
, true);
620 mutex_unlock(&data
->mutex
);
623 ret
= bmg160_set_scale(data
, val2
);
625 bmg160_set_power_state(data
, false);
626 mutex_unlock(&data
->mutex
);
629 ret
= bmg160_set_power_state(data
, false);
630 mutex_unlock(&data
->mutex
);
639 static int bmg160_read_event(struct iio_dev
*indio_dev
,
640 const struct iio_chan_spec
*chan
,
641 enum iio_event_type type
,
642 enum iio_event_direction dir
,
643 enum iio_event_info info
,
646 struct bmg160_data
*data
= iio_priv(indio_dev
);
650 case IIO_EV_INFO_VALUE
:
651 *val
= data
->slope_thres
& BMG160_SLOPE_THRES_MASK
;
660 static int bmg160_write_event(struct iio_dev
*indio_dev
,
661 const struct iio_chan_spec
*chan
,
662 enum iio_event_type type
,
663 enum iio_event_direction dir
,
664 enum iio_event_info info
,
667 struct bmg160_data
*data
= iio_priv(indio_dev
);
670 case IIO_EV_INFO_VALUE
:
671 if (data
->ev_enable_state
)
673 data
->slope_thres
&= ~BMG160_SLOPE_THRES_MASK
;
674 data
->slope_thres
|= (val
& BMG160_SLOPE_THRES_MASK
);
683 static int bmg160_read_event_config(struct iio_dev
*indio_dev
,
684 const struct iio_chan_spec
*chan
,
685 enum iio_event_type type
,
686 enum iio_event_direction dir
)
689 struct bmg160_data
*data
= iio_priv(indio_dev
);
691 return data
->ev_enable_state
;
694 static int bmg160_write_event_config(struct iio_dev
*indio_dev
,
695 const struct iio_chan_spec
*chan
,
696 enum iio_event_type type
,
697 enum iio_event_direction dir
,
700 struct bmg160_data
*data
= iio_priv(indio_dev
);
703 if (state
&& data
->ev_enable_state
)
706 mutex_lock(&data
->mutex
);
708 if (!state
&& data
->motion_trigger_on
) {
709 data
->ev_enable_state
= 0;
710 mutex_unlock(&data
->mutex
);
714 * We will expect the enable and disable to do operation in
715 * in reverse order. This will happen here anyway as our
716 * resume operation uses sync mode runtime pm calls, the
717 * suspend operation will be delayed by autosuspend delay
718 * So the disable operation will still happen in reverse of
719 * enable operation. When runtime pm is disabled the mode
720 * is always on so sequence doesn't matter
722 ret
= bmg160_set_power_state(data
, state
);
724 mutex_unlock(&data
->mutex
);
728 ret
= bmg160_setup_any_motion_interrupt(data
, state
);
730 bmg160_set_power_state(data
, false);
731 mutex_unlock(&data
->mutex
);
735 data
->ev_enable_state
= state
;
736 mutex_unlock(&data
->mutex
);
741 static int bmg160_validate_trigger(struct iio_dev
*indio_dev
,
742 struct iio_trigger
*trig
)
744 struct bmg160_data
*data
= iio_priv(indio_dev
);
746 if (data
->dready_trig
!= trig
&& data
->motion_trig
!= trig
)
752 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("100 200 400 1000 2000");
754 static IIO_CONST_ATTR(in_anglvel_scale_available
,
755 "0.001065 0.000532 0.000266 0.000133 0.000066");
757 static struct attribute
*bmg160_attributes
[] = {
758 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
759 &iio_const_attr_in_anglvel_scale_available
.dev_attr
.attr
,
763 static const struct attribute_group bmg160_attrs_group
= {
764 .attrs
= bmg160_attributes
,
767 static const struct iio_event_spec bmg160_event
= {
768 .type
= IIO_EV_TYPE_ROC
,
769 .dir
= IIO_EV_DIR_EITHER
,
770 .mask_shared_by_type
= BIT(IIO_EV_INFO_VALUE
) |
771 BIT(IIO_EV_INFO_ENABLE
)
774 #define BMG160_CHANNEL(_axis) { \
775 .type = IIO_ANGL_VEL, \
777 .channel2 = IIO_MOD_##_axis, \
778 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
779 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
780 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
781 .scan_index = AXIS_##_axis, \
787 .event_spec = &bmg160_event, \
788 .num_event_specs = 1 \
791 static const struct iio_chan_spec bmg160_channels
[] = {
794 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
795 BIT(IIO_CHAN_INFO_SCALE
) |
796 BIT(IIO_CHAN_INFO_OFFSET
),
802 IIO_CHAN_SOFT_TIMESTAMP(3),
805 static const struct iio_info bmg160_info
= {
806 .attrs
= &bmg160_attrs_group
,
807 .read_raw
= bmg160_read_raw
,
808 .write_raw
= bmg160_write_raw
,
809 .read_event_value
= bmg160_read_event
,
810 .write_event_value
= bmg160_write_event
,
811 .write_event_config
= bmg160_write_event_config
,
812 .read_event_config
= bmg160_read_event_config
,
813 .validate_trigger
= bmg160_validate_trigger
,
814 .driver_module
= THIS_MODULE
,
817 static irqreturn_t
bmg160_trigger_handler(int irq
, void *p
)
819 struct iio_poll_func
*pf
= p
;
820 struct iio_dev
*indio_dev
= pf
->indio_dev
;
821 struct bmg160_data
*data
= iio_priv(indio_dev
);
824 mutex_lock(&data
->mutex
);
825 for_each_set_bit(bit
, indio_dev
->buffer
->scan_mask
,
826 indio_dev
->masklength
) {
827 ret
= i2c_smbus_read_word_data(data
->client
,
828 BMG160_AXIS_TO_REG(bit
));
830 mutex_unlock(&data
->mutex
);
833 data
->buffer
[i
++] = ret
;
835 mutex_unlock(&data
->mutex
);
837 iio_push_to_buffers_with_timestamp(indio_dev
, data
->buffer
,
840 iio_trigger_notify_done(indio_dev
->trig
);
845 static int bmg160_trig_try_reen(struct iio_trigger
*trig
)
847 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
848 struct bmg160_data
*data
= iio_priv(indio_dev
);
851 /* new data interrupts don't need ack */
852 if (data
->dready_trigger_on
)
855 /* Set latched mode interrupt and clear any latched interrupt */
856 ret
= i2c_smbus_write_byte_data(data
->client
,
857 BMG160_REG_INT_RST_LATCH
,
858 BMG160_INT_MODE_LATCH_INT
|
859 BMG160_INT_MODE_LATCH_RESET
);
861 dev_err(&data
->client
->dev
, "Error writing reg_rst_latch\n");
868 static int bmg160_data_rdy_trigger_set_state(struct iio_trigger
*trig
,
871 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
872 struct bmg160_data
*data
= iio_priv(indio_dev
);
875 mutex_lock(&data
->mutex
);
877 if (!state
&& data
->ev_enable_state
&& data
->motion_trigger_on
) {
878 data
->motion_trigger_on
= false;
879 mutex_unlock(&data
->mutex
);
884 * Refer to comment in bmg160_write_event_config for
885 * enable/disable operation order
887 ret
= bmg160_set_power_state(data
, state
);
889 mutex_unlock(&data
->mutex
);
892 if (data
->motion_trig
== trig
)
893 ret
= bmg160_setup_any_motion_interrupt(data
, state
);
895 ret
= bmg160_setup_new_data_interrupt(data
, state
);
897 bmg160_set_power_state(data
, false);
898 mutex_unlock(&data
->mutex
);
901 if (data
->motion_trig
== trig
)
902 data
->motion_trigger_on
= state
;
904 data
->dready_trigger_on
= state
;
906 mutex_unlock(&data
->mutex
);
911 static const struct iio_trigger_ops bmg160_trigger_ops
= {
912 .set_trigger_state
= bmg160_data_rdy_trigger_set_state
,
913 .try_reenable
= bmg160_trig_try_reen
,
914 .owner
= THIS_MODULE
,
917 static irqreturn_t
bmg160_event_handler(int irq
, void *private)
919 struct iio_dev
*indio_dev
= private;
920 struct bmg160_data
*data
= iio_priv(indio_dev
);
924 ret
= i2c_smbus_read_byte_data(data
->client
, BMG160_REG_INT_STATUS_2
);
926 dev_err(&data
->client
->dev
, "Error reading reg_int_status2\n");
927 goto ack_intr_status
;
931 dir
= IIO_EV_DIR_RISING
;
933 dir
= IIO_EV_DIR_FALLING
;
935 if (ret
& BMG160_ANY_MOTION_BIT_X
)
936 iio_push_event(indio_dev
, IIO_MOD_EVENT_CODE(IIO_ANGL_VEL
,
942 if (ret
& BMG160_ANY_MOTION_BIT_Y
)
943 iio_push_event(indio_dev
, IIO_MOD_EVENT_CODE(IIO_ANGL_VEL
,
949 if (ret
& BMG160_ANY_MOTION_BIT_Z
)
950 iio_push_event(indio_dev
, IIO_MOD_EVENT_CODE(IIO_ANGL_VEL
,
958 if (!data
->dready_trigger_on
) {
959 ret
= i2c_smbus_write_byte_data(data
->client
,
960 BMG160_REG_INT_RST_LATCH
,
961 BMG160_INT_MODE_LATCH_INT
|
962 BMG160_INT_MODE_LATCH_RESET
);
964 dev_err(&data
->client
->dev
,
965 "Error writing reg_rst_latch\n");
971 static irqreturn_t
bmg160_data_rdy_trig_poll(int irq
, void *private)
973 struct iio_dev
*indio_dev
= private;
974 struct bmg160_data
*data
= iio_priv(indio_dev
);
976 data
->timestamp
= iio_get_time_ns();
978 if (data
->dready_trigger_on
)
979 iio_trigger_poll(data
->dready_trig
);
980 else if (data
->motion_trigger_on
)
981 iio_trigger_poll(data
->motion_trig
);
983 if (data
->ev_enable_state
)
984 return IRQ_WAKE_THREAD
;
990 static int bmg160_gpio_probe(struct i2c_client
*client
,
991 struct bmg160_data
*data
)
995 struct gpio_desc
*gpio
;
1003 /* data ready gpio interrupt pin */
1004 gpio
= devm_gpiod_get_index(dev
, BMG160_GPIO_NAME
, 0);
1006 dev_err(dev
, "acpi gpio get index failed\n");
1007 return PTR_ERR(gpio
);
1010 ret
= gpiod_direction_input(gpio
);
1014 ret
= gpiod_to_irq(gpio
);
1016 dev_dbg(dev
, "GPIO resource, no:%d irq:%d\n", desc_to_gpio(gpio
), ret
);
1021 static const char *bmg160_match_acpi_device(struct device
*dev
)
1023 const struct acpi_device_id
*id
;
1025 id
= acpi_match_device(dev
->driver
->acpi_match_table
, dev
);
1029 return dev_name(dev
);
1032 static int bmg160_probe(struct i2c_client
*client
,
1033 const struct i2c_device_id
*id
)
1035 struct bmg160_data
*data
;
1036 struct iio_dev
*indio_dev
;
1038 const char *name
= NULL
;
1040 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
1044 data
= iio_priv(indio_dev
);
1045 i2c_set_clientdata(client
, indio_dev
);
1046 data
->client
= client
;
1048 ret
= bmg160_chip_init(data
);
1052 mutex_init(&data
->mutex
);
1057 if (ACPI_HANDLE(&client
->dev
))
1058 name
= bmg160_match_acpi_device(&client
->dev
);
1060 indio_dev
->dev
.parent
= &client
->dev
;
1061 indio_dev
->channels
= bmg160_channels
;
1062 indio_dev
->num_channels
= ARRAY_SIZE(bmg160_channels
);
1063 indio_dev
->name
= name
;
1064 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1065 indio_dev
->info
= &bmg160_info
;
1067 if (client
->irq
<= 0)
1068 client
->irq
= bmg160_gpio_probe(client
, data
);
1070 if (client
->irq
> 0) {
1071 ret
= devm_request_threaded_irq(&client
->dev
,
1073 bmg160_data_rdy_trig_poll
,
1074 bmg160_event_handler
,
1075 IRQF_TRIGGER_RISING
,
1081 data
->dready_trig
= devm_iio_trigger_alloc(&client
->dev
,
1085 if (!data
->dready_trig
)
1088 data
->motion_trig
= devm_iio_trigger_alloc(&client
->dev
,
1089 "%s-any-motion-dev%d",
1092 if (!data
->motion_trig
)
1095 data
->dready_trig
->dev
.parent
= &client
->dev
;
1096 data
->dready_trig
->ops
= &bmg160_trigger_ops
;
1097 iio_trigger_set_drvdata(data
->dready_trig
, indio_dev
);
1098 ret
= iio_trigger_register(data
->dready_trig
);
1102 data
->motion_trig
->dev
.parent
= &client
->dev
;
1103 data
->motion_trig
->ops
= &bmg160_trigger_ops
;
1104 iio_trigger_set_drvdata(data
->motion_trig
, indio_dev
);
1105 ret
= iio_trigger_register(data
->motion_trig
);
1107 data
->motion_trig
= NULL
;
1108 goto err_trigger_unregister
;
1111 ret
= iio_triggered_buffer_setup(indio_dev
,
1113 bmg160_trigger_handler
,
1116 dev_err(&client
->dev
,
1117 "iio triggered buffer setup failed\n");
1118 goto err_trigger_unregister
;
1122 ret
= iio_device_register(indio_dev
);
1124 dev_err(&client
->dev
, "unable to register iio device\n");
1125 goto err_buffer_cleanup
;
1128 ret
= pm_runtime_set_active(&client
->dev
);
1130 goto err_iio_unregister
;
1132 pm_runtime_enable(&client
->dev
);
1133 pm_runtime_set_autosuspend_delay(&client
->dev
,
1134 BMG160_AUTO_SUSPEND_DELAY_MS
);
1135 pm_runtime_use_autosuspend(&client
->dev
);
1140 iio_device_unregister(indio_dev
);
1142 if (data
->dready_trig
)
1143 iio_triggered_buffer_cleanup(indio_dev
);
1144 err_trigger_unregister
:
1145 if (data
->dready_trig
)
1146 iio_trigger_unregister(data
->dready_trig
);
1147 if (data
->motion_trig
)
1148 iio_trigger_unregister(data
->motion_trig
);
1153 static int bmg160_remove(struct i2c_client
*client
)
1155 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
1156 struct bmg160_data
*data
= iio_priv(indio_dev
);
1158 pm_runtime_disable(&client
->dev
);
1159 pm_runtime_set_suspended(&client
->dev
);
1160 pm_runtime_put_noidle(&client
->dev
);
1162 iio_device_unregister(indio_dev
);
1164 if (data
->dready_trig
) {
1165 iio_triggered_buffer_cleanup(indio_dev
);
1166 iio_trigger_unregister(data
->dready_trig
);
1167 iio_trigger_unregister(data
->motion_trig
);
1170 mutex_lock(&data
->mutex
);
1171 bmg160_set_mode(data
, BMG160_MODE_DEEP_SUSPEND
);
1172 mutex_unlock(&data
->mutex
);
1177 #ifdef CONFIG_PM_SLEEP
1178 static int bmg160_suspend(struct device
*dev
)
1180 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
1181 struct bmg160_data
*data
= iio_priv(indio_dev
);
1183 mutex_lock(&data
->mutex
);
1184 bmg160_set_mode(data
, BMG160_MODE_SUSPEND
);
1185 mutex_unlock(&data
->mutex
);
1190 static int bmg160_resume(struct device
*dev
)
1192 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
1193 struct bmg160_data
*data
= iio_priv(indio_dev
);
1195 mutex_lock(&data
->mutex
);
1196 if (data
->dready_trigger_on
|| data
->motion_trigger_on
||
1197 data
->ev_enable_state
)
1198 bmg160_set_mode(data
, BMG160_MODE_NORMAL
);
1199 mutex_unlock(&data
->mutex
);
1206 static int bmg160_runtime_suspend(struct device
*dev
)
1208 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
1209 struct bmg160_data
*data
= iio_priv(indio_dev
);
1212 ret
= bmg160_set_mode(data
, BMG160_MODE_SUSPEND
);
1214 dev_err(&data
->client
->dev
, "set mode failed\n");
1221 static int bmg160_runtime_resume(struct device
*dev
)
1223 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
1224 struct bmg160_data
*data
= iio_priv(indio_dev
);
1227 ret
= bmg160_set_mode(data
, BMG160_MODE_NORMAL
);
1231 msleep_interruptible(BMG160_MAX_STARTUP_TIME_MS
);
1237 static const struct dev_pm_ops bmg160_pm_ops
= {
1238 SET_SYSTEM_SLEEP_PM_OPS(bmg160_suspend
, bmg160_resume
)
1239 SET_RUNTIME_PM_OPS(bmg160_runtime_suspend
,
1240 bmg160_runtime_resume
, NULL
)
1243 static const struct acpi_device_id bmg160_acpi_match
[] = {
1249 MODULE_DEVICE_TABLE(acpi
, bmg160_acpi_match
);
1251 static const struct i2c_device_id bmg160_id
[] = {
1257 MODULE_DEVICE_TABLE(i2c
, bmg160_id
);
1259 static struct i2c_driver bmg160_driver
= {
1261 .name
= BMG160_DRV_NAME
,
1262 .acpi_match_table
= ACPI_PTR(bmg160_acpi_match
),
1263 .pm
= &bmg160_pm_ops
,
1265 .probe
= bmg160_probe
,
1266 .remove
= bmg160_remove
,
1267 .id_table
= bmg160_id
,
1269 module_i2c_driver(bmg160_driver
);
1271 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
1272 MODULE_LICENSE("GPL v2");
1273 MODULE_DESCRIPTION("BMG160 Gyro driver");