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>
30 #include <linux/delay.h>
33 #define BMG160_IRQ_NAME "bmg160_event"
35 #define BMG160_REG_CHIP_ID 0x00
36 #define BMG160_CHIP_ID_VAL 0x0F
38 #define BMG160_REG_PMU_LPW 0x11
39 #define BMG160_MODE_NORMAL 0x00
40 #define BMG160_MODE_DEEP_SUSPEND 0x20
41 #define BMG160_MODE_SUSPEND 0x80
43 #define BMG160_REG_RANGE 0x0F
45 #define BMG160_RANGE_2000DPS 0
46 #define BMG160_RANGE_1000DPS 1
47 #define BMG160_RANGE_500DPS 2
48 #define BMG160_RANGE_250DPS 3
49 #define BMG160_RANGE_125DPS 4
51 #define BMG160_REG_PMU_BW 0x10
52 #define BMG160_NO_FILTER 0
53 #define BMG160_DEF_BW 100
54 #define BMG160_REG_PMU_BW_RES BIT(7)
56 #define BMG160_GYRO_REG_RESET 0x14
57 #define BMG160_GYRO_RESET_VAL 0xb6
59 #define BMG160_REG_INT_MAP_0 0x17
60 #define BMG160_INT_MAP_0_BIT_ANY BIT(1)
62 #define BMG160_REG_INT_MAP_1 0x18
63 #define BMG160_INT_MAP_1_BIT_NEW_DATA BIT(0)
65 #define BMG160_REG_INT_RST_LATCH 0x21
66 #define BMG160_INT_MODE_LATCH_RESET 0x80
67 #define BMG160_INT_MODE_LATCH_INT 0x0F
68 #define BMG160_INT_MODE_NON_LATCH_INT 0x00
70 #define BMG160_REG_INT_EN_0 0x15
71 #define BMG160_DATA_ENABLE_INT BIT(7)
73 #define BMG160_REG_INT_EN_1 0x16
74 #define BMG160_INT1_BIT_OD BIT(1)
76 #define BMG160_REG_XOUT_L 0x02
77 #define BMG160_AXIS_TO_REG(axis) (BMG160_REG_XOUT_L + (axis * 2))
79 #define BMG160_REG_SLOPE_THRES 0x1B
80 #define BMG160_SLOPE_THRES_MASK 0x0F
82 #define BMG160_REG_MOTION_INTR 0x1C
83 #define BMG160_INT_MOTION_X BIT(0)
84 #define BMG160_INT_MOTION_Y BIT(1)
85 #define BMG160_INT_MOTION_Z BIT(2)
86 #define BMG160_ANY_DUR_MASK 0x30
87 #define BMG160_ANY_DUR_SHIFT 4
89 #define BMG160_REG_INT_STATUS_2 0x0B
90 #define BMG160_ANY_MOTION_MASK 0x07
91 #define BMG160_ANY_MOTION_BIT_X BIT(0)
92 #define BMG160_ANY_MOTION_BIT_Y BIT(1)
93 #define BMG160_ANY_MOTION_BIT_Z BIT(2)
95 #define BMG160_REG_TEMP 0x08
96 #define BMG160_TEMP_CENTER_VAL 23
98 #define BMG160_MAX_STARTUP_TIME_MS 80
100 #define BMG160_AUTO_SUSPEND_DELAY_MS 2000
103 struct regmap *regmap;
104 struct iio_trigger *dready_trig;
105 struct iio_trigger *motion_trig;
107 /* Ensure naturally aligned timestamp */
110 s64 timestamp __aligned(8);
115 bool dready_trigger_on;
116 bool motion_trigger_on;
127 static const struct {
131 } bmg160_samp_freq_table[] = { {100, 32, 0x07},
139 static const struct {
142 } bmg160_scale_table[] = { { 1065, BMG160_RANGE_2000DPS},
143 { 532, BMG160_RANGE_1000DPS},
144 { 266, BMG160_RANGE_500DPS},
145 { 133, BMG160_RANGE_250DPS},
146 { 66, BMG160_RANGE_125DPS} };
148 static int bmg160_set_mode(struct bmg160_data *data, u8 mode)
150 struct device *dev = regmap_get_device(data->regmap);
153 ret = regmap_write(data->regmap, BMG160_REG_PMU_LPW, mode);
155 dev_err(dev, "Error writing reg_pmu_lpw\n");
162 static int bmg160_convert_freq_to_bit(int val)
166 for (i = 0; i < ARRAY_SIZE(bmg160_samp_freq_table); ++i) {
167 if (bmg160_samp_freq_table[i].odr == val)
168 return bmg160_samp_freq_table[i].bw_bits;
174 static int bmg160_set_bw(struct bmg160_data *data, int val)
176 struct device *dev = regmap_get_device(data->regmap);
180 bw_bits = bmg160_convert_freq_to_bit(val);
184 ret = regmap_write(data->regmap, BMG160_REG_PMU_BW, bw_bits);
186 dev_err(dev, "Error writing reg_pmu_bw\n");
193 static int bmg160_get_filter(struct bmg160_data *data, int *val)
195 struct device *dev = regmap_get_device(data->regmap);
198 unsigned int bw_bits;
200 ret = regmap_read(data->regmap, BMG160_REG_PMU_BW, &bw_bits);
202 dev_err(dev, "Error reading reg_pmu_bw\n");
206 /* Ignore the readonly reserved bit. */
207 bw_bits &= ~BMG160_REG_PMU_BW_RES;
209 for (i = 0; i < ARRAY_SIZE(bmg160_samp_freq_table); ++i) {
210 if (bmg160_samp_freq_table[i].bw_bits == bw_bits)
214 *val = bmg160_samp_freq_table[i].filter;
216 return ret ? ret : IIO_VAL_INT;
220 static int bmg160_set_filter(struct bmg160_data *data, int val)
222 struct device *dev = regmap_get_device(data->regmap);
226 for (i = 0; i < ARRAY_SIZE(bmg160_samp_freq_table); ++i) {
227 if (bmg160_samp_freq_table[i].filter == val)
231 ret = regmap_write(data->regmap, BMG160_REG_PMU_BW,
232 bmg160_samp_freq_table[i].bw_bits);
234 dev_err(dev, "Error writing reg_pmu_bw\n");
241 static int bmg160_chip_init(struct bmg160_data *data)
243 struct device *dev = regmap_get_device(data->regmap);
248 * Reset chip to get it in a known good state. A delay of 30ms after
249 * reset is required according to the datasheet.
251 regmap_write(data->regmap, BMG160_GYRO_REG_RESET,
252 BMG160_GYRO_RESET_VAL);
253 usleep_range(30000, 30700);
255 ret = regmap_read(data->regmap, BMG160_REG_CHIP_ID, &val);
257 dev_err(dev, "Error reading reg_chip_id\n");
261 dev_dbg(dev, "Chip Id %x\n", val);
262 if (val != BMG160_CHIP_ID_VAL) {
263 dev_err(dev, "invalid chip %x\n", val);
267 ret = bmg160_set_mode(data, BMG160_MODE_NORMAL);
271 /* Wait upto 500 ms to be ready after changing mode */
272 usleep_range(500, 1000);
275 ret = bmg160_set_bw(data, BMG160_DEF_BW);
279 /* Set Default Range */
280 ret = regmap_write(data->regmap, BMG160_REG_RANGE, BMG160_RANGE_500DPS);
282 dev_err(dev, "Error writing reg_range\n");
285 data->dps_range = BMG160_RANGE_500DPS;
287 ret = regmap_read(data->regmap, BMG160_REG_SLOPE_THRES, &val);
289 dev_err(dev, "Error reading reg_slope_thres\n");
292 data->slope_thres = val;
294 /* Set default interrupt mode */
295 ret = regmap_update_bits(data->regmap, BMG160_REG_INT_EN_1,
296 BMG160_INT1_BIT_OD, 0);
298 dev_err(dev, "Error updating bits in reg_int_en_1\n");
302 ret = regmap_write(data->regmap, BMG160_REG_INT_RST_LATCH,
303 BMG160_INT_MODE_LATCH_INT |
304 BMG160_INT_MODE_LATCH_RESET);
307 "Error writing reg_motion_intr\n");
314 static int bmg160_set_power_state(struct bmg160_data *data, bool on)
317 struct device *dev = regmap_get_device(data->regmap);
321 ret = pm_runtime_get_sync(dev);
323 pm_runtime_mark_last_busy(dev);
324 ret = pm_runtime_put_autosuspend(dev);
328 dev_err(dev, "Failed: bmg160_set_power_state for %d\n", on);
331 pm_runtime_put_noidle(dev);
340 static int bmg160_setup_any_motion_interrupt(struct bmg160_data *data,
343 struct device *dev = regmap_get_device(data->regmap);
346 /* Enable/Disable INT_MAP0 mapping */
347 ret = regmap_update_bits(data->regmap, BMG160_REG_INT_MAP_0,
348 BMG160_INT_MAP_0_BIT_ANY,
349 (status ? BMG160_INT_MAP_0_BIT_ANY : 0));
351 dev_err(dev, "Error updating bits reg_int_map0\n");
355 /* Enable/Disable slope interrupts */
357 /* Update slope thres */
358 ret = regmap_write(data->regmap, BMG160_REG_SLOPE_THRES,
361 dev_err(dev, "Error writing reg_slope_thres\n");
365 ret = regmap_write(data->regmap, BMG160_REG_MOTION_INTR,
366 BMG160_INT_MOTION_X | BMG160_INT_MOTION_Y |
367 BMG160_INT_MOTION_Z);
369 dev_err(dev, "Error writing reg_motion_intr\n");
374 * New data interrupt is always non-latched,
375 * which will have higher priority, so no need
376 * to set latched mode, we will be flooded anyway with INTR
378 if (!data->dready_trigger_on) {
379 ret = regmap_write(data->regmap,
380 BMG160_REG_INT_RST_LATCH,
381 BMG160_INT_MODE_LATCH_INT |
382 BMG160_INT_MODE_LATCH_RESET);
384 dev_err(dev, "Error writing reg_rst_latch\n");
389 ret = regmap_write(data->regmap, BMG160_REG_INT_EN_0,
390 BMG160_DATA_ENABLE_INT);
393 ret = regmap_write(data->regmap, BMG160_REG_INT_EN_0, 0);
397 dev_err(dev, "Error writing reg_int_en0\n");
404 static int bmg160_setup_new_data_interrupt(struct bmg160_data *data,
407 struct device *dev = regmap_get_device(data->regmap);
410 /* Enable/Disable INT_MAP1 mapping */
411 ret = regmap_update_bits(data->regmap, BMG160_REG_INT_MAP_1,
412 BMG160_INT_MAP_1_BIT_NEW_DATA,
413 (status ? BMG160_INT_MAP_1_BIT_NEW_DATA : 0));
415 dev_err(dev, "Error updating bits in reg_int_map1\n");
420 ret = regmap_write(data->regmap, BMG160_REG_INT_RST_LATCH,
421 BMG160_INT_MODE_NON_LATCH_INT |
422 BMG160_INT_MODE_LATCH_RESET);
424 dev_err(dev, "Error writing reg_rst_latch\n");
428 ret = regmap_write(data->regmap, BMG160_REG_INT_EN_0,
429 BMG160_DATA_ENABLE_INT);
432 /* Restore interrupt mode */
433 ret = regmap_write(data->regmap, BMG160_REG_INT_RST_LATCH,
434 BMG160_INT_MODE_LATCH_INT |
435 BMG160_INT_MODE_LATCH_RESET);
437 dev_err(dev, "Error writing reg_rst_latch\n");
441 ret = regmap_write(data->regmap, BMG160_REG_INT_EN_0, 0);
445 dev_err(dev, "Error writing reg_int_en0\n");
452 static int bmg160_get_bw(struct bmg160_data *data, int *val)
454 struct device *dev = regmap_get_device(data->regmap);
456 unsigned int bw_bits;
459 ret = regmap_read(data->regmap, BMG160_REG_PMU_BW, &bw_bits);
461 dev_err(dev, "Error reading reg_pmu_bw\n");
465 /* Ignore the readonly reserved bit. */
466 bw_bits &= ~BMG160_REG_PMU_BW_RES;
468 for (i = 0; i < ARRAY_SIZE(bmg160_samp_freq_table); ++i) {
469 if (bmg160_samp_freq_table[i].bw_bits == bw_bits) {
470 *val = bmg160_samp_freq_table[i].odr;
478 static int bmg160_set_scale(struct bmg160_data *data, int val)
480 struct device *dev = regmap_get_device(data->regmap);
483 for (i = 0; i < ARRAY_SIZE(bmg160_scale_table); ++i) {
484 if (bmg160_scale_table[i].scale == val) {
485 ret = regmap_write(data->regmap, BMG160_REG_RANGE,
486 bmg160_scale_table[i].dps_range);
488 dev_err(dev, "Error writing reg_range\n");
491 data->dps_range = bmg160_scale_table[i].dps_range;
499 static int bmg160_get_temp(struct bmg160_data *data, int *val)
501 struct device *dev = regmap_get_device(data->regmap);
503 unsigned int raw_val;
505 mutex_lock(&data->mutex);
506 ret = bmg160_set_power_state(data, true);
508 mutex_unlock(&data->mutex);
512 ret = regmap_read(data->regmap, BMG160_REG_TEMP, &raw_val);
514 dev_err(dev, "Error reading reg_temp\n");
515 bmg160_set_power_state(data, false);
516 mutex_unlock(&data->mutex);
520 *val = sign_extend32(raw_val, 7);
521 ret = bmg160_set_power_state(data, false);
522 mutex_unlock(&data->mutex);
529 static int bmg160_get_axis(struct bmg160_data *data, int axis, int *val)
531 struct device *dev = regmap_get_device(data->regmap);
535 mutex_lock(&data->mutex);
536 ret = bmg160_set_power_state(data, true);
538 mutex_unlock(&data->mutex);
542 ret = regmap_bulk_read(data->regmap, BMG160_AXIS_TO_REG(axis), &raw_val,
545 dev_err(dev, "Error reading axis %d\n", axis);
546 bmg160_set_power_state(data, false);
547 mutex_unlock(&data->mutex);
551 *val = sign_extend32(le16_to_cpu(raw_val), 15);
552 ret = bmg160_set_power_state(data, false);
553 mutex_unlock(&data->mutex);
560 static int bmg160_read_raw(struct iio_dev *indio_dev,
561 struct iio_chan_spec const *chan,
562 int *val, int *val2, long mask)
564 struct bmg160_data *data = iio_priv(indio_dev);
568 case IIO_CHAN_INFO_RAW:
569 switch (chan->type) {
571 return bmg160_get_temp(data, val);
573 if (iio_buffer_enabled(indio_dev))
576 return bmg160_get_axis(data, chan->scan_index,
581 case IIO_CHAN_INFO_OFFSET:
582 if (chan->type == IIO_TEMP) {
583 *val = BMG160_TEMP_CENTER_VAL;
587 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
588 return bmg160_get_filter(data, val);
589 case IIO_CHAN_INFO_SCALE:
590 switch (chan->type) {
598 for (i = 0; i < ARRAY_SIZE(bmg160_scale_table); ++i) {
599 if (bmg160_scale_table[i].dps_range ==
602 *val2 = bmg160_scale_table[i].scale;
603 return IIO_VAL_INT_PLUS_MICRO;
611 case IIO_CHAN_INFO_SAMP_FREQ:
613 mutex_lock(&data->mutex);
614 ret = bmg160_get_bw(data, val);
615 mutex_unlock(&data->mutex);
622 static int bmg160_write_raw(struct iio_dev *indio_dev,
623 struct iio_chan_spec const *chan,
624 int val, int val2, long mask)
626 struct bmg160_data *data = iio_priv(indio_dev);
630 case IIO_CHAN_INFO_SAMP_FREQ:
631 mutex_lock(&data->mutex);
633 * Section 4.2 of spec
634 * In suspend mode, the only supported operations are reading
635 * registers as well as writing to the (0x14) softreset
636 * register. Since we will be in suspend mode by default, change
637 * mode to power on for other writes.
639 ret = bmg160_set_power_state(data, true);
641 mutex_unlock(&data->mutex);
644 ret = bmg160_set_bw(data, val);
646 bmg160_set_power_state(data, false);
647 mutex_unlock(&data->mutex);
650 ret = bmg160_set_power_state(data, false);
651 mutex_unlock(&data->mutex);
653 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
657 mutex_lock(&data->mutex);
658 ret = bmg160_set_power_state(data, true);
660 bmg160_set_power_state(data, false);
661 mutex_unlock(&data->mutex);
664 ret = bmg160_set_filter(data, val);
666 bmg160_set_power_state(data, false);
667 mutex_unlock(&data->mutex);
670 ret = bmg160_set_power_state(data, false);
671 mutex_unlock(&data->mutex);
673 case IIO_CHAN_INFO_SCALE:
677 mutex_lock(&data->mutex);
678 /* Refer to comments above for the suspend mode ops */
679 ret = bmg160_set_power_state(data, true);
681 mutex_unlock(&data->mutex);
684 ret = bmg160_set_scale(data, val2);
686 bmg160_set_power_state(data, false);
687 mutex_unlock(&data->mutex);
690 ret = bmg160_set_power_state(data, false);
691 mutex_unlock(&data->mutex);
700 static int bmg160_read_event(struct iio_dev *indio_dev,
701 const struct iio_chan_spec *chan,
702 enum iio_event_type type,
703 enum iio_event_direction dir,
704 enum iio_event_info info,
707 struct bmg160_data *data = iio_priv(indio_dev);
711 case IIO_EV_INFO_VALUE:
712 *val = data->slope_thres & BMG160_SLOPE_THRES_MASK;
721 static int bmg160_write_event(struct iio_dev *indio_dev,
722 const struct iio_chan_spec *chan,
723 enum iio_event_type type,
724 enum iio_event_direction dir,
725 enum iio_event_info info,
728 struct bmg160_data *data = iio_priv(indio_dev);
731 case IIO_EV_INFO_VALUE:
732 if (data->ev_enable_state)
734 data->slope_thres &= ~BMG160_SLOPE_THRES_MASK;
735 data->slope_thres |= (val & BMG160_SLOPE_THRES_MASK);
744 static int bmg160_read_event_config(struct iio_dev *indio_dev,
745 const struct iio_chan_spec *chan,
746 enum iio_event_type type,
747 enum iio_event_direction dir)
750 struct bmg160_data *data = iio_priv(indio_dev);
752 return data->ev_enable_state;
755 static int bmg160_write_event_config(struct iio_dev *indio_dev,
756 const struct iio_chan_spec *chan,
757 enum iio_event_type type,
758 enum iio_event_direction dir,
761 struct bmg160_data *data = iio_priv(indio_dev);
764 if (state && data->ev_enable_state)
767 mutex_lock(&data->mutex);
769 if (!state && data->motion_trigger_on) {
770 data->ev_enable_state = 0;
771 mutex_unlock(&data->mutex);
775 * We will expect the enable and disable to do operation in
776 * in reverse order. This will happen here anyway as our
777 * resume operation uses sync mode runtime pm calls, the
778 * suspend operation will be delayed by autosuspend delay
779 * So the disable operation will still happen in reverse of
780 * enable operation. When runtime pm is disabled the mode
781 * is always on so sequence doesn't matter
783 ret = bmg160_set_power_state(data, state);
785 mutex_unlock(&data->mutex);
789 ret = bmg160_setup_any_motion_interrupt(data, state);
791 bmg160_set_power_state(data, false);
792 mutex_unlock(&data->mutex);
796 data->ev_enable_state = state;
797 mutex_unlock(&data->mutex);
802 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("100 200 400 1000 2000");
804 static IIO_CONST_ATTR(in_anglvel_scale_available,
805 "0.001065 0.000532 0.000266 0.000133 0.000066");
807 static struct attribute *bmg160_attributes[] = {
808 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
809 &iio_const_attr_in_anglvel_scale_available.dev_attr.attr,
813 static const struct attribute_group bmg160_attrs_group = {
814 .attrs = bmg160_attributes,
817 static const struct iio_event_spec bmg160_event = {
818 .type = IIO_EV_TYPE_ROC,
819 .dir = IIO_EV_DIR_EITHER,
820 .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) |
821 BIT(IIO_EV_INFO_ENABLE)
824 #define BMG160_CHANNEL(_axis) { \
825 .type = IIO_ANGL_VEL, \
827 .channel2 = IIO_MOD_##_axis, \
828 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
829 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
830 BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
831 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
832 .scan_index = AXIS_##_axis, \
837 .endianness = IIO_LE, \
839 .event_spec = &bmg160_event, \
840 .num_event_specs = 1 \
843 static const struct iio_chan_spec bmg160_channels[] = {
846 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
847 BIT(IIO_CHAN_INFO_SCALE) |
848 BIT(IIO_CHAN_INFO_OFFSET),
854 IIO_CHAN_SOFT_TIMESTAMP(3),
857 static const struct iio_info bmg160_info = {
858 .attrs = &bmg160_attrs_group,
859 .read_raw = bmg160_read_raw,
860 .write_raw = bmg160_write_raw,
861 .read_event_value = bmg160_read_event,
862 .write_event_value = bmg160_write_event,
863 .write_event_config = bmg160_write_event_config,
864 .read_event_config = bmg160_read_event_config,
865 .driver_module = THIS_MODULE,
868 static const unsigned long bmg160_accel_scan_masks[] = {
869 BIT(AXIS_X) | BIT(AXIS_Y) | BIT(AXIS_Z),
872 static irqreturn_t bmg160_trigger_handler(int irq, void *p)
874 struct iio_poll_func *pf = p;
875 struct iio_dev *indio_dev = pf->indio_dev;
876 struct bmg160_data *data = iio_priv(indio_dev);
879 mutex_lock(&data->mutex);
880 ret = regmap_bulk_read(data->regmap, BMG160_REG_XOUT_L,
881 data->scan.chans, AXIS_MAX * 2);
882 mutex_unlock(&data->mutex);
886 iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
889 iio_trigger_notify_done(indio_dev->trig);
894 static int bmg160_trig_try_reen(struct iio_trigger *trig)
896 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
897 struct bmg160_data *data = iio_priv(indio_dev);
898 struct device *dev = regmap_get_device(data->regmap);
901 /* new data interrupts don't need ack */
902 if (data->dready_trigger_on)
905 /* Set latched mode interrupt and clear any latched interrupt */
906 ret = regmap_write(data->regmap, BMG160_REG_INT_RST_LATCH,
907 BMG160_INT_MODE_LATCH_INT |
908 BMG160_INT_MODE_LATCH_RESET);
910 dev_err(dev, "Error writing reg_rst_latch\n");
917 static int bmg160_data_rdy_trigger_set_state(struct iio_trigger *trig,
920 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
921 struct bmg160_data *data = iio_priv(indio_dev);
924 mutex_lock(&data->mutex);
926 if (!state && data->ev_enable_state && data->motion_trigger_on) {
927 data->motion_trigger_on = false;
928 mutex_unlock(&data->mutex);
933 * Refer to comment in bmg160_write_event_config for
934 * enable/disable operation order
936 ret = bmg160_set_power_state(data, state);
938 mutex_unlock(&data->mutex);
941 if (data->motion_trig == trig)
942 ret = bmg160_setup_any_motion_interrupt(data, state);
944 ret = bmg160_setup_new_data_interrupt(data, state);
946 bmg160_set_power_state(data, false);
947 mutex_unlock(&data->mutex);
950 if (data->motion_trig == trig)
951 data->motion_trigger_on = state;
953 data->dready_trigger_on = state;
955 mutex_unlock(&data->mutex);
960 static const struct iio_trigger_ops bmg160_trigger_ops = {
961 .set_trigger_state = bmg160_data_rdy_trigger_set_state,
962 .try_reenable = bmg160_trig_try_reen,
963 .owner = THIS_MODULE,
966 static irqreturn_t bmg160_event_handler(int irq, void *private)
968 struct iio_dev *indio_dev = private;
969 struct bmg160_data *data = iio_priv(indio_dev);
970 struct device *dev = regmap_get_device(data->regmap);
975 ret = regmap_read(data->regmap, BMG160_REG_INT_STATUS_2, &val);
977 dev_err(dev, "Error reading reg_int_status2\n");
978 goto ack_intr_status;
982 dir = IIO_EV_DIR_RISING;
984 dir = IIO_EV_DIR_FALLING;
986 if (val & BMG160_ANY_MOTION_BIT_X)
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_Y)
994 iio_push_event(indio_dev, IIO_MOD_EVENT_CODE(IIO_ANGL_VEL,
999 iio_get_time_ns(indio_dev));
1000 if (val & BMG160_ANY_MOTION_BIT_Z)
1001 iio_push_event(indio_dev, IIO_MOD_EVENT_CODE(IIO_ANGL_VEL,
1006 iio_get_time_ns(indio_dev));
1009 if (!data->dready_trigger_on) {
1010 ret = regmap_write(data->regmap, BMG160_REG_INT_RST_LATCH,
1011 BMG160_INT_MODE_LATCH_INT |
1012 BMG160_INT_MODE_LATCH_RESET);
1014 dev_err(dev, "Error writing reg_rst_latch\n");
1020 static irqreturn_t bmg160_data_rdy_trig_poll(int irq, void *private)
1022 struct iio_dev *indio_dev = private;
1023 struct bmg160_data *data = iio_priv(indio_dev);
1025 if (data->dready_trigger_on)
1026 iio_trigger_poll(data->dready_trig);
1027 else if (data->motion_trigger_on)
1028 iio_trigger_poll(data->motion_trig);
1030 if (data->ev_enable_state)
1031 return IRQ_WAKE_THREAD;
1037 static int bmg160_buffer_preenable(struct iio_dev *indio_dev)
1039 struct bmg160_data *data = iio_priv(indio_dev);
1041 return bmg160_set_power_state(data, true);
1044 static int bmg160_buffer_postdisable(struct iio_dev *indio_dev)
1046 struct bmg160_data *data = iio_priv(indio_dev);
1048 return bmg160_set_power_state(data, false);
1051 static const struct iio_buffer_setup_ops bmg160_buffer_setup_ops = {
1052 .preenable = bmg160_buffer_preenable,
1053 .postenable = iio_triggered_buffer_postenable,
1054 .predisable = iio_triggered_buffer_predisable,
1055 .postdisable = bmg160_buffer_postdisable,
1058 static const char *bmg160_match_acpi_device(struct device *dev)
1060 const struct acpi_device_id *id;
1062 id = acpi_match_device(dev->driver->acpi_match_table, dev);
1066 return dev_name(dev);
1069 int bmg160_core_probe(struct device *dev, struct regmap *regmap, int irq,
1072 struct bmg160_data *data;
1073 struct iio_dev *indio_dev;
1076 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
1080 data = iio_priv(indio_dev);
1081 dev_set_drvdata(dev, indio_dev);
1083 data->regmap = regmap;
1085 ret = bmg160_chip_init(data);
1089 mutex_init(&data->mutex);
1091 if (ACPI_HANDLE(dev))
1092 name = bmg160_match_acpi_device(dev);
1094 indio_dev->dev.parent = dev;
1095 indio_dev->channels = bmg160_channels;
1096 indio_dev->num_channels = ARRAY_SIZE(bmg160_channels);
1097 indio_dev->name = name;
1098 indio_dev->available_scan_masks = bmg160_accel_scan_masks;
1099 indio_dev->modes = INDIO_DIRECT_MODE;
1100 indio_dev->info = &bmg160_info;
1102 if (data->irq > 0) {
1103 ret = devm_request_threaded_irq(dev,
1105 bmg160_data_rdy_trig_poll,
1106 bmg160_event_handler,
1107 IRQF_TRIGGER_RISING,
1113 data->dready_trig = devm_iio_trigger_alloc(dev,
1117 if (!data->dready_trig)
1120 data->motion_trig = devm_iio_trigger_alloc(dev,
1121 "%s-any-motion-dev%d",
1124 if (!data->motion_trig)
1127 data->dready_trig->dev.parent = dev;
1128 data->dready_trig->ops = &bmg160_trigger_ops;
1129 iio_trigger_set_drvdata(data->dready_trig, indio_dev);
1130 ret = iio_trigger_register(data->dready_trig);
1134 data->motion_trig->dev.parent = dev;
1135 data->motion_trig->ops = &bmg160_trigger_ops;
1136 iio_trigger_set_drvdata(data->motion_trig, indio_dev);
1137 ret = iio_trigger_register(data->motion_trig);
1139 data->motion_trig = NULL;
1140 goto err_trigger_unregister;
1144 ret = iio_triggered_buffer_setup(indio_dev,
1145 iio_pollfunc_store_time,
1146 bmg160_trigger_handler,
1147 &bmg160_buffer_setup_ops);
1150 "iio triggered buffer setup failed\n");
1151 goto err_trigger_unregister;
1154 ret = pm_runtime_set_active(dev);
1156 goto err_buffer_cleanup;
1158 pm_runtime_enable(dev);
1159 pm_runtime_set_autosuspend_delay(dev,
1160 BMG160_AUTO_SUSPEND_DELAY_MS);
1161 pm_runtime_use_autosuspend(dev);
1163 ret = iio_device_register(indio_dev);
1165 dev_err(dev, "unable to register iio device\n");
1166 goto err_buffer_cleanup;
1172 iio_triggered_buffer_cleanup(indio_dev);
1173 err_trigger_unregister:
1174 if (data->dready_trig)
1175 iio_trigger_unregister(data->dready_trig);
1176 if (data->motion_trig)
1177 iio_trigger_unregister(data->motion_trig);
1181 EXPORT_SYMBOL_GPL(bmg160_core_probe);
1183 void bmg160_core_remove(struct device *dev)
1185 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1186 struct bmg160_data *data = iio_priv(indio_dev);
1188 iio_device_unregister(indio_dev);
1190 pm_runtime_disable(dev);
1191 pm_runtime_set_suspended(dev);
1192 pm_runtime_put_noidle(dev);
1194 iio_triggered_buffer_cleanup(indio_dev);
1196 if (data->dready_trig) {
1197 iio_trigger_unregister(data->dready_trig);
1198 iio_trigger_unregister(data->motion_trig);
1201 mutex_lock(&data->mutex);
1202 bmg160_set_mode(data, BMG160_MODE_DEEP_SUSPEND);
1203 mutex_unlock(&data->mutex);
1205 EXPORT_SYMBOL_GPL(bmg160_core_remove);
1207 #ifdef CONFIG_PM_SLEEP
1208 static int bmg160_suspend(struct device *dev)
1210 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1211 struct bmg160_data *data = iio_priv(indio_dev);
1213 mutex_lock(&data->mutex);
1214 bmg160_set_mode(data, BMG160_MODE_SUSPEND);
1215 mutex_unlock(&data->mutex);
1220 static int bmg160_resume(struct device *dev)
1222 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1223 struct bmg160_data *data = iio_priv(indio_dev);
1225 mutex_lock(&data->mutex);
1226 if (data->dready_trigger_on || data->motion_trigger_on ||
1227 data->ev_enable_state)
1228 bmg160_set_mode(data, BMG160_MODE_NORMAL);
1229 mutex_unlock(&data->mutex);
1236 static int bmg160_runtime_suspend(struct device *dev)
1238 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1239 struct bmg160_data *data = iio_priv(indio_dev);
1242 ret = bmg160_set_mode(data, BMG160_MODE_SUSPEND);
1244 dev_err(dev, "set mode failed\n");
1251 static int bmg160_runtime_resume(struct device *dev)
1253 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1254 struct bmg160_data *data = iio_priv(indio_dev);
1257 ret = bmg160_set_mode(data, BMG160_MODE_NORMAL);
1261 msleep_interruptible(BMG160_MAX_STARTUP_TIME_MS);
1267 const struct dev_pm_ops bmg160_pm_ops = {
1268 SET_SYSTEM_SLEEP_PM_OPS(bmg160_suspend, bmg160_resume)
1269 SET_RUNTIME_PM_OPS(bmg160_runtime_suspend,
1270 bmg160_runtime_resume, NULL)
1272 EXPORT_SYMBOL_GPL(bmg160_pm_ops);
1274 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
1275 MODULE_LICENSE("GPL v2");
1276 MODULE_DESCRIPTION("BMG160 Gyro driver");