2 * 3-axis accelerometer driver supporting following Bosch-Sensortec chips:
10 * Copyright (c) 2014, Intel Corporation.
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms and conditions of the GNU General Public License,
14 * version 2, as published by the Free Software Foundation.
16 * This program is distributed in the hope it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
22 #include <linux/module.h>
23 #include <linux/i2c.h>
24 #include <linux/interrupt.h>
25 #include <linux/delay.h>
26 #include <linux/slab.h>
27 #include <linux/acpi.h>
28 #include <linux/gpio/consumer.h>
30 #include <linux/pm_runtime.h>
31 #include <linux/iio/iio.h>
32 #include <linux/iio/sysfs.h>
33 #include <linux/iio/buffer.h>
34 #include <linux/iio/events.h>
35 #include <linux/iio/trigger.h>
36 #include <linux/iio/trigger_consumer.h>
37 #include <linux/iio/triggered_buffer.h>
38 #include <linux/regmap.h>
40 #include "bmc150-accel.h"
42 #define BMC150_ACCEL_DRV_NAME "bmc150_accel"
43 #define BMC150_ACCEL_IRQ_NAME "bmc150_accel_event"
45 #define BMC150_ACCEL_REG_CHIP_ID 0x00
47 #define BMC150_ACCEL_REG_INT_STATUS_2 0x0B
48 #define BMC150_ACCEL_ANY_MOTION_MASK 0x07
49 #define BMC150_ACCEL_ANY_MOTION_BIT_X BIT(0)
50 #define BMC150_ACCEL_ANY_MOTION_BIT_Y BIT(1)
51 #define BMC150_ACCEL_ANY_MOTION_BIT_Z BIT(2)
52 #define BMC150_ACCEL_ANY_MOTION_BIT_SIGN BIT(3)
54 #define BMC150_ACCEL_REG_PMU_LPW 0x11
55 #define BMC150_ACCEL_PMU_MODE_MASK 0xE0
56 #define BMC150_ACCEL_PMU_MODE_SHIFT 5
57 #define BMC150_ACCEL_PMU_BIT_SLEEP_DUR_MASK 0x17
58 #define BMC150_ACCEL_PMU_BIT_SLEEP_DUR_SHIFT 1
60 #define BMC150_ACCEL_REG_PMU_RANGE 0x0F
62 #define BMC150_ACCEL_DEF_RANGE_2G 0x03
63 #define BMC150_ACCEL_DEF_RANGE_4G 0x05
64 #define BMC150_ACCEL_DEF_RANGE_8G 0x08
65 #define BMC150_ACCEL_DEF_RANGE_16G 0x0C
67 /* Default BW: 125Hz */
68 #define BMC150_ACCEL_REG_PMU_BW 0x10
69 #define BMC150_ACCEL_DEF_BW 125
71 #define BMC150_ACCEL_REG_RESET 0x14
72 #define BMC150_ACCEL_RESET_VAL 0xB6
74 #define BMC150_ACCEL_REG_INT_MAP_0 0x19
75 #define BMC150_ACCEL_INT_MAP_0_BIT_SLOPE BIT(2)
77 #define BMC150_ACCEL_REG_INT_MAP_1 0x1A
78 #define BMC150_ACCEL_INT_MAP_1_BIT_DATA BIT(0)
79 #define BMC150_ACCEL_INT_MAP_1_BIT_FWM BIT(1)
80 #define BMC150_ACCEL_INT_MAP_1_BIT_FFULL BIT(2)
82 #define BMC150_ACCEL_REG_INT_RST_LATCH 0x21
83 #define BMC150_ACCEL_INT_MODE_LATCH_RESET 0x80
84 #define BMC150_ACCEL_INT_MODE_LATCH_INT 0x0F
85 #define BMC150_ACCEL_INT_MODE_NON_LATCH_INT 0x00
87 #define BMC150_ACCEL_REG_INT_EN_0 0x16
88 #define BMC150_ACCEL_INT_EN_BIT_SLP_X BIT(0)
89 #define BMC150_ACCEL_INT_EN_BIT_SLP_Y BIT(1)
90 #define BMC150_ACCEL_INT_EN_BIT_SLP_Z BIT(2)
92 #define BMC150_ACCEL_REG_INT_EN_1 0x17
93 #define BMC150_ACCEL_INT_EN_BIT_DATA_EN BIT(4)
94 #define BMC150_ACCEL_INT_EN_BIT_FFULL_EN BIT(5)
95 #define BMC150_ACCEL_INT_EN_BIT_FWM_EN BIT(6)
97 #define BMC150_ACCEL_REG_INT_OUT_CTRL 0x20
98 #define BMC150_ACCEL_INT_OUT_CTRL_INT1_LVL BIT(0)
100 #define BMC150_ACCEL_REG_INT_5 0x27
101 #define BMC150_ACCEL_SLOPE_DUR_MASK 0x03
103 #define BMC150_ACCEL_REG_INT_6 0x28
104 #define BMC150_ACCEL_SLOPE_THRES_MASK 0xFF
106 /* Slope duration in terms of number of samples */
107 #define BMC150_ACCEL_DEF_SLOPE_DURATION 1
108 /* in terms of multiples of g's/LSB, based on range */
109 #define BMC150_ACCEL_DEF_SLOPE_THRESHOLD 1
111 #define BMC150_ACCEL_REG_XOUT_L 0x02
113 #define BMC150_ACCEL_MAX_STARTUP_TIME_MS 100
115 /* Sleep Duration values */
116 #define BMC150_ACCEL_SLEEP_500_MICRO 0x05
117 #define BMC150_ACCEL_SLEEP_1_MS 0x06
118 #define BMC150_ACCEL_SLEEP_2_MS 0x07
119 #define BMC150_ACCEL_SLEEP_4_MS 0x08
120 #define BMC150_ACCEL_SLEEP_6_MS 0x09
121 #define BMC150_ACCEL_SLEEP_10_MS 0x0A
122 #define BMC150_ACCEL_SLEEP_25_MS 0x0B
123 #define BMC150_ACCEL_SLEEP_50_MS 0x0C
124 #define BMC150_ACCEL_SLEEP_100_MS 0x0D
125 #define BMC150_ACCEL_SLEEP_500_MS 0x0E
126 #define BMC150_ACCEL_SLEEP_1_SEC 0x0F
128 #define BMC150_ACCEL_REG_TEMP 0x08
129 #define BMC150_ACCEL_TEMP_CENTER_VAL 24
131 #define BMC150_ACCEL_AXIS_TO_REG(axis) (BMC150_ACCEL_REG_XOUT_L + (axis * 2))
132 #define BMC150_AUTO_SUSPEND_DELAY_MS 2000
134 #define BMC150_ACCEL_REG_FIFO_STATUS 0x0E
135 #define BMC150_ACCEL_REG_FIFO_CONFIG0 0x30
136 #define BMC150_ACCEL_REG_FIFO_CONFIG1 0x3E
137 #define BMC150_ACCEL_REG_FIFO_DATA 0x3F
138 #define BMC150_ACCEL_FIFO_LENGTH 32
140 enum bmc150_accel_axis
{
146 enum bmc150_power_modes
{
147 BMC150_ACCEL_SLEEP_MODE_NORMAL
,
148 BMC150_ACCEL_SLEEP_MODE_DEEP_SUSPEND
,
149 BMC150_ACCEL_SLEEP_MODE_LPM
,
150 BMC150_ACCEL_SLEEP_MODE_SUSPEND
= 0x04,
153 struct bmc150_scale_info
{
158 struct bmc150_accel_chip_info
{
161 const struct iio_chan_spec
*channels
;
163 const struct bmc150_scale_info scale_table
[4];
166 struct bmc150_accel_interrupt
{
167 const struct bmc150_accel_interrupt_info
*info
;
171 struct bmc150_accel_trigger
{
172 struct bmc150_accel_data
*data
;
173 struct iio_trigger
*indio_trig
;
174 int (*setup
)(struct bmc150_accel_trigger
*t
, bool state
);
179 enum bmc150_accel_interrupt_id
{
180 BMC150_ACCEL_INT_DATA_READY
,
181 BMC150_ACCEL_INT_ANY_MOTION
,
182 BMC150_ACCEL_INT_WATERMARK
,
183 BMC150_ACCEL_INTERRUPTS
,
186 enum bmc150_accel_trigger_id
{
187 BMC150_ACCEL_TRIGGER_DATA_READY
,
188 BMC150_ACCEL_TRIGGER_ANY_MOTION
,
189 BMC150_ACCEL_TRIGGERS
,
192 struct bmc150_accel_data
{
193 struct regmap
*regmap
;
196 struct bmc150_accel_interrupt interrupts
[BMC150_ACCEL_INTERRUPTS
];
197 struct bmc150_accel_trigger triggers
[BMC150_ACCEL_TRIGGERS
];
199 u8 fifo_mode
, watermark
;
206 int64_t timestamp
, old_timestamp
; /* Only used in hw fifo mode. */
207 const struct bmc150_accel_chip_info
*chip_info
;
210 static const struct {
214 } bmc150_accel_samp_freq_table
[] = { {15, 620000, 0x08},
223 static const struct {
226 } bmc150_accel_sample_upd_time
[] = { {0x08, 64},
235 static const struct {
238 } bmc150_accel_sleep_value_table
[] = { {0, 0},
239 {500, BMC150_ACCEL_SLEEP_500_MICRO
},
240 {1000, BMC150_ACCEL_SLEEP_1_MS
},
241 {2000, BMC150_ACCEL_SLEEP_2_MS
},
242 {4000, BMC150_ACCEL_SLEEP_4_MS
},
243 {6000, BMC150_ACCEL_SLEEP_6_MS
},
244 {10000, BMC150_ACCEL_SLEEP_10_MS
},
245 {25000, BMC150_ACCEL_SLEEP_25_MS
},
246 {50000, BMC150_ACCEL_SLEEP_50_MS
},
247 {100000, BMC150_ACCEL_SLEEP_100_MS
},
248 {500000, BMC150_ACCEL_SLEEP_500_MS
},
249 {1000000, BMC150_ACCEL_SLEEP_1_SEC
} };
251 static const struct regmap_config bmc150_i2c_regmap_conf
= {
254 .max_register
= 0x3f,
257 static int bmc150_accel_set_mode(struct bmc150_accel_data
*data
,
258 enum bmc150_power_modes mode
,
267 for (i
= 0; i
< ARRAY_SIZE(bmc150_accel_sleep_value_table
);
269 if (bmc150_accel_sleep_value_table
[i
].sleep_dur
==
272 bmc150_accel_sleep_value_table
[i
].reg_value
;
281 lpw_bits
= mode
<< BMC150_ACCEL_PMU_MODE_SHIFT
;
282 lpw_bits
|= (dur_val
<< BMC150_ACCEL_PMU_BIT_SLEEP_DUR_SHIFT
);
284 dev_dbg(data
->dev
, "Set Mode bits %x\n", lpw_bits
);
286 ret
= regmap_write(data
->regmap
, BMC150_ACCEL_REG_PMU_LPW
, lpw_bits
);
288 dev_err(data
->dev
, "Error writing reg_pmu_lpw\n");
295 static int bmc150_accel_set_bw(struct bmc150_accel_data
*data
, int val
,
301 for (i
= 0; i
< ARRAY_SIZE(bmc150_accel_samp_freq_table
); ++i
) {
302 if (bmc150_accel_samp_freq_table
[i
].val
== val
&&
303 bmc150_accel_samp_freq_table
[i
].val2
== val2
) {
304 ret
= regmap_write(data
->regmap
,
305 BMC150_ACCEL_REG_PMU_BW
,
306 bmc150_accel_samp_freq_table
[i
].bw_bits
);
311 bmc150_accel_samp_freq_table
[i
].bw_bits
;
319 static int bmc150_accel_update_slope(struct bmc150_accel_data
*data
)
323 ret
= regmap_write(data
->regmap
, BMC150_ACCEL_REG_INT_6
,
326 dev_err(data
->dev
, "Error writing reg_int_6\n");
330 ret
= regmap_update_bits(data
->regmap
, BMC150_ACCEL_REG_INT_5
,
331 BMC150_ACCEL_SLOPE_DUR_MASK
, data
->slope_dur
);
333 dev_err(data
->dev
, "Error updating reg_int_5\n");
337 dev_dbg(data
->dev
, "%s: %x %x\n", __func__
, data
->slope_thres
,
343 static int bmc150_accel_any_motion_setup(struct bmc150_accel_trigger
*t
,
347 return bmc150_accel_update_slope(t
->data
);
352 static int bmc150_accel_get_bw(struct bmc150_accel_data
*data
, int *val
,
357 for (i
= 0; i
< ARRAY_SIZE(bmc150_accel_samp_freq_table
); ++i
) {
358 if (bmc150_accel_samp_freq_table
[i
].bw_bits
== data
->bw_bits
) {
359 *val
= bmc150_accel_samp_freq_table
[i
].val
;
360 *val2
= bmc150_accel_samp_freq_table
[i
].val2
;
361 return IIO_VAL_INT_PLUS_MICRO
;
369 static int bmc150_accel_get_startup_times(struct bmc150_accel_data
*data
)
373 for (i
= 0; i
< ARRAY_SIZE(bmc150_accel_sample_upd_time
); ++i
) {
374 if (bmc150_accel_sample_upd_time
[i
].bw_bits
== data
->bw_bits
)
375 return bmc150_accel_sample_upd_time
[i
].msec
;
378 return BMC150_ACCEL_MAX_STARTUP_TIME_MS
;
381 static int bmc150_accel_set_power_state(struct bmc150_accel_data
*data
, bool on
)
386 ret
= pm_runtime_get_sync(data
->dev
);
388 pm_runtime_mark_last_busy(data
->dev
);
389 ret
= pm_runtime_put_autosuspend(data
->dev
);
394 "Failed: bmc150_accel_set_power_state for %d\n", on
);
396 pm_runtime_put_noidle(data
->dev
);
404 static int bmc150_accel_set_power_state(struct bmc150_accel_data
*data
, bool on
)
410 static const struct bmc150_accel_interrupt_info
{
415 } bmc150_accel_interrupts
[BMC150_ACCEL_INTERRUPTS
] = {
416 { /* data ready interrupt */
417 .map_reg
= BMC150_ACCEL_REG_INT_MAP_1
,
418 .map_bitmask
= BMC150_ACCEL_INT_MAP_1_BIT_DATA
,
419 .en_reg
= BMC150_ACCEL_REG_INT_EN_1
,
420 .en_bitmask
= BMC150_ACCEL_INT_EN_BIT_DATA_EN
,
422 { /* motion interrupt */
423 .map_reg
= BMC150_ACCEL_REG_INT_MAP_0
,
424 .map_bitmask
= BMC150_ACCEL_INT_MAP_0_BIT_SLOPE
,
425 .en_reg
= BMC150_ACCEL_REG_INT_EN_0
,
426 .en_bitmask
= BMC150_ACCEL_INT_EN_BIT_SLP_X
|
427 BMC150_ACCEL_INT_EN_BIT_SLP_Y
|
428 BMC150_ACCEL_INT_EN_BIT_SLP_Z
430 { /* fifo watermark interrupt */
431 .map_reg
= BMC150_ACCEL_REG_INT_MAP_1
,
432 .map_bitmask
= BMC150_ACCEL_INT_MAP_1_BIT_FWM
,
433 .en_reg
= BMC150_ACCEL_REG_INT_EN_1
,
434 .en_bitmask
= BMC150_ACCEL_INT_EN_BIT_FWM_EN
,
438 static void bmc150_accel_interrupts_setup(struct iio_dev
*indio_dev
,
439 struct bmc150_accel_data
*data
)
443 for (i
= 0; i
< BMC150_ACCEL_INTERRUPTS
; i
++)
444 data
->interrupts
[i
].info
= &bmc150_accel_interrupts
[i
];
447 static int bmc150_accel_set_interrupt(struct bmc150_accel_data
*data
, int i
,
450 struct bmc150_accel_interrupt
*intr
= &data
->interrupts
[i
];
451 const struct bmc150_accel_interrupt_info
*info
= intr
->info
;
455 if (atomic_inc_return(&intr
->users
) > 1)
458 if (atomic_dec_return(&intr
->users
) > 0)
463 * We will expect the enable and disable to do operation in reverse
464 * order. This will happen here anyway, as our resume operation uses
465 * sync mode runtime pm calls. The suspend operation will be delayed
466 * by autosuspend delay.
467 * So the disable operation will still happen in reverse order of
468 * enable operation. When runtime pm is disabled the mode is always on,
469 * so sequence doesn't matter.
471 ret
= bmc150_accel_set_power_state(data
, state
);
475 /* map the interrupt to the appropriate pins */
476 ret
= regmap_update_bits(data
->regmap
, info
->map_reg
, info
->map_bitmask
,
477 (state
? info
->map_bitmask
: 0));
479 dev_err(data
->dev
, "Error updating reg_int_map\n");
480 goto out_fix_power_state
;
483 /* enable/disable the interrupt */
484 ret
= regmap_update_bits(data
->regmap
, info
->en_reg
, info
->en_bitmask
,
485 (state
? info
->en_bitmask
: 0));
487 dev_err(data
->dev
, "Error updating reg_int_en\n");
488 goto out_fix_power_state
;
494 bmc150_accel_set_power_state(data
, false);
498 static int bmc150_accel_set_scale(struct bmc150_accel_data
*data
, int val
)
502 for (i
= 0; i
< ARRAY_SIZE(data
->chip_info
->scale_table
); ++i
) {
503 if (data
->chip_info
->scale_table
[i
].scale
== val
) {
504 ret
= regmap_write(data
->regmap
,
505 BMC150_ACCEL_REG_PMU_RANGE
,
506 data
->chip_info
->scale_table
[i
].reg_range
);
509 "Error writing pmu_range\n");
513 data
->range
= data
->chip_info
->scale_table
[i
].reg_range
;
521 static int bmc150_accel_get_temp(struct bmc150_accel_data
*data
, int *val
)
526 mutex_lock(&data
->mutex
);
528 ret
= regmap_read(data
->regmap
, BMC150_ACCEL_REG_TEMP
, &value
);
530 dev_err(data
->dev
, "Error reading reg_temp\n");
531 mutex_unlock(&data
->mutex
);
534 *val
= sign_extend32(value
, 7);
536 mutex_unlock(&data
->mutex
);
541 static int bmc150_accel_get_axis(struct bmc150_accel_data
*data
,
542 struct iio_chan_spec
const *chan
,
546 int axis
= chan
->scan_index
;
549 mutex_lock(&data
->mutex
);
550 ret
= bmc150_accel_set_power_state(data
, true);
552 mutex_unlock(&data
->mutex
);
556 ret
= regmap_bulk_read(data
->regmap
, BMC150_ACCEL_AXIS_TO_REG(axis
),
557 &raw_val
, sizeof(raw_val
));
559 dev_err(data
->dev
, "Error reading axis %d\n", axis
);
560 bmc150_accel_set_power_state(data
, false);
561 mutex_unlock(&data
->mutex
);
564 *val
= sign_extend32(le16_to_cpu(raw_val
) >> chan
->scan_type
.shift
,
565 chan
->scan_type
.realbits
- 1);
566 ret
= bmc150_accel_set_power_state(data
, false);
567 mutex_unlock(&data
->mutex
);
574 static int bmc150_accel_read_raw(struct iio_dev
*indio_dev
,
575 struct iio_chan_spec
const *chan
,
576 int *val
, int *val2
, long mask
)
578 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
582 case IIO_CHAN_INFO_RAW
:
583 switch (chan
->type
) {
585 return bmc150_accel_get_temp(data
, val
);
587 if (iio_buffer_enabled(indio_dev
))
590 return bmc150_accel_get_axis(data
, chan
, val
);
594 case IIO_CHAN_INFO_OFFSET
:
595 if (chan
->type
== IIO_TEMP
) {
596 *val
= BMC150_ACCEL_TEMP_CENTER_VAL
;
601 case IIO_CHAN_INFO_SCALE
:
603 switch (chan
->type
) {
606 return IIO_VAL_INT_PLUS_MICRO
;
610 const struct bmc150_scale_info
*si
;
611 int st_size
= ARRAY_SIZE(data
->chip_info
->scale_table
);
613 for (i
= 0; i
< st_size
; ++i
) {
614 si
= &data
->chip_info
->scale_table
[i
];
615 if (si
->reg_range
== data
->range
) {
617 return IIO_VAL_INT_PLUS_MICRO
;
625 case IIO_CHAN_INFO_SAMP_FREQ
:
626 mutex_lock(&data
->mutex
);
627 ret
= bmc150_accel_get_bw(data
, val
, val2
);
628 mutex_unlock(&data
->mutex
);
635 static int bmc150_accel_write_raw(struct iio_dev
*indio_dev
,
636 struct iio_chan_spec
const *chan
,
637 int val
, int val2
, long mask
)
639 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
643 case IIO_CHAN_INFO_SAMP_FREQ
:
644 mutex_lock(&data
->mutex
);
645 ret
= bmc150_accel_set_bw(data
, val
, val2
);
646 mutex_unlock(&data
->mutex
);
648 case IIO_CHAN_INFO_SCALE
:
652 mutex_lock(&data
->mutex
);
653 ret
= bmc150_accel_set_scale(data
, val2
);
654 mutex_unlock(&data
->mutex
);
663 static int bmc150_accel_read_event(struct iio_dev
*indio_dev
,
664 const struct iio_chan_spec
*chan
,
665 enum iio_event_type type
,
666 enum iio_event_direction dir
,
667 enum iio_event_info info
,
670 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
674 case IIO_EV_INFO_VALUE
:
675 *val
= data
->slope_thres
;
677 case IIO_EV_INFO_PERIOD
:
678 *val
= data
->slope_dur
;
687 static int bmc150_accel_write_event(struct iio_dev
*indio_dev
,
688 const struct iio_chan_spec
*chan
,
689 enum iio_event_type type
,
690 enum iio_event_direction dir
,
691 enum iio_event_info info
,
694 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
696 if (data
->ev_enable_state
)
700 case IIO_EV_INFO_VALUE
:
701 data
->slope_thres
= val
& BMC150_ACCEL_SLOPE_THRES_MASK
;
703 case IIO_EV_INFO_PERIOD
:
704 data
->slope_dur
= val
& BMC150_ACCEL_SLOPE_DUR_MASK
;
713 static int bmc150_accel_read_event_config(struct iio_dev
*indio_dev
,
714 const struct iio_chan_spec
*chan
,
715 enum iio_event_type type
,
716 enum iio_event_direction dir
)
718 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
720 return data
->ev_enable_state
;
723 static int bmc150_accel_write_event_config(struct iio_dev
*indio_dev
,
724 const struct iio_chan_spec
*chan
,
725 enum iio_event_type type
,
726 enum iio_event_direction dir
,
729 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
732 if (state
== data
->ev_enable_state
)
735 mutex_lock(&data
->mutex
);
737 ret
= bmc150_accel_set_interrupt(data
, BMC150_ACCEL_INT_ANY_MOTION
,
740 mutex_unlock(&data
->mutex
);
744 data
->ev_enable_state
= state
;
745 mutex_unlock(&data
->mutex
);
750 static int bmc150_accel_validate_trigger(struct iio_dev
*indio_dev
,
751 struct iio_trigger
*trig
)
753 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
756 for (i
= 0; i
< BMC150_ACCEL_TRIGGERS
; i
++) {
757 if (data
->triggers
[i
].indio_trig
== trig
)
764 static ssize_t
bmc150_accel_get_fifo_watermark(struct device
*dev
,
765 struct device_attribute
*attr
,
768 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
769 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
772 mutex_lock(&data
->mutex
);
773 wm
= data
->watermark
;
774 mutex_unlock(&data
->mutex
);
776 return sprintf(buf
, "%d\n", wm
);
779 static ssize_t
bmc150_accel_get_fifo_state(struct device
*dev
,
780 struct device_attribute
*attr
,
783 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
784 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
787 mutex_lock(&data
->mutex
);
788 state
= data
->fifo_mode
;
789 mutex_unlock(&data
->mutex
);
791 return sprintf(buf
, "%d\n", state
);
794 static IIO_CONST_ATTR(hwfifo_watermark_min
, "1");
795 static IIO_CONST_ATTR(hwfifo_watermark_max
,
796 __stringify(BMC150_ACCEL_FIFO_LENGTH
));
797 static IIO_DEVICE_ATTR(hwfifo_enabled
, S_IRUGO
,
798 bmc150_accel_get_fifo_state
, NULL
, 0);
799 static IIO_DEVICE_ATTR(hwfifo_watermark
, S_IRUGO
,
800 bmc150_accel_get_fifo_watermark
, NULL
, 0);
802 static const struct attribute
*bmc150_accel_fifo_attributes
[] = {
803 &iio_const_attr_hwfifo_watermark_min
.dev_attr
.attr
,
804 &iio_const_attr_hwfifo_watermark_max
.dev_attr
.attr
,
805 &iio_dev_attr_hwfifo_watermark
.dev_attr
.attr
,
806 &iio_dev_attr_hwfifo_enabled
.dev_attr
.attr
,
810 static int bmc150_accel_set_watermark(struct iio_dev
*indio_dev
, unsigned val
)
812 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
814 if (val
> BMC150_ACCEL_FIFO_LENGTH
)
815 val
= BMC150_ACCEL_FIFO_LENGTH
;
817 mutex_lock(&data
->mutex
);
818 data
->watermark
= val
;
819 mutex_unlock(&data
->mutex
);
825 * We must read at least one full frame in one burst, otherwise the rest of the
826 * frame data is discarded.
828 static int bmc150_accel_fifo_transfer(struct bmc150_accel_data
*data
,
829 char *buffer
, int samples
)
831 int sample_length
= 3 * 2;
833 int total_length
= samples
* sample_length
;
835 size_t step
= regmap_get_raw_read_max(data
->regmap
);
837 if (!step
|| step
> total_length
)
839 else if (step
< total_length
)
840 step
= sample_length
;
843 * Seems we have a bus with size limitation so we have to execute
846 for (i
= 0; i
< total_length
; i
+= step
) {
847 ret
= regmap_raw_read(data
->regmap
, BMC150_ACCEL_REG_FIFO_DATA
,
854 dev_err(data
->dev
, "Error transferring data from fifo in single steps of %zu\n",
860 static int __bmc150_accel_fifo_flush(struct iio_dev
*indio_dev
,
861 unsigned samples
, bool irq
)
863 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
866 u16 buffer
[BMC150_ACCEL_FIFO_LENGTH
* 3];
868 uint64_t sample_period
;
871 ret
= regmap_read(data
->regmap
, BMC150_ACCEL_REG_FIFO_STATUS
, &val
);
873 dev_err(data
->dev
, "Error reading reg_fifo_status\n");
883 * If we getting called from IRQ handler we know the stored timestamp is
884 * fairly accurate for the last stored sample. Otherwise, if we are
885 * called as a result of a read operation from userspace and hence
886 * before the watermark interrupt was triggered, take a timestamp
887 * now. We can fall anywhere in between two samples so the error in this
888 * case is at most one sample period.
891 data
->old_timestamp
= data
->timestamp
;
892 data
->timestamp
= iio_get_time_ns();
896 * Approximate timestamps for each of the sample based on the sampling
897 * frequency, timestamp for last sample and number of samples.
899 * Note that we can't use the current bandwidth settings to compute the
900 * sample period because the sample rate varies with the device
901 * (e.g. between 31.70ms to 32.20ms for a bandwidth of 15.63HZ). That
902 * small variation adds when we store a large number of samples and
903 * creates significant jitter between the last and first samples in
904 * different batches (e.g. 32ms vs 21ms).
906 * To avoid this issue we compute the actual sample period ourselves
907 * based on the timestamp delta between the last two flush operations.
909 sample_period
= (data
->timestamp
- data
->old_timestamp
);
910 do_div(sample_period
, count
);
911 tstamp
= data
->timestamp
- (count
- 1) * sample_period
;
913 if (samples
&& count
> samples
)
916 ret
= bmc150_accel_fifo_transfer(data
, (u8
*)buffer
, count
);
921 * Ideally we want the IIO core to handle the demux when running in fifo
922 * mode but not when running in triggered buffer mode. Unfortunately
923 * this does not seem to be possible, so stick with driver demux for
926 for (i
= 0; i
< count
; i
++) {
931 for_each_set_bit(bit
, indio_dev
->active_scan_mask
,
932 indio_dev
->masklength
)
933 memcpy(&sample
[j
++], &buffer
[i
* 3 + bit
], 2);
935 iio_push_to_buffers_with_timestamp(indio_dev
, sample
, tstamp
);
937 tstamp
+= sample_period
;
943 static int bmc150_accel_fifo_flush(struct iio_dev
*indio_dev
, unsigned samples
)
945 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
948 mutex_lock(&data
->mutex
);
949 ret
= __bmc150_accel_fifo_flush(indio_dev
, samples
, false);
950 mutex_unlock(&data
->mutex
);
955 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
956 "15.620000 31.260000 62.50000 125 250 500 1000 2000");
958 static struct attribute
*bmc150_accel_attributes
[] = {
959 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
963 static const struct attribute_group bmc150_accel_attrs_group
= {
964 .attrs
= bmc150_accel_attributes
,
967 static const struct iio_event_spec bmc150_accel_event
= {
968 .type
= IIO_EV_TYPE_ROC
,
969 .dir
= IIO_EV_DIR_EITHER
,
970 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
971 BIT(IIO_EV_INFO_ENABLE
) |
972 BIT(IIO_EV_INFO_PERIOD
)
975 #define BMC150_ACCEL_CHANNEL(_axis, bits) { \
978 .channel2 = IIO_MOD_##_axis, \
979 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
980 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
981 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
982 .scan_index = AXIS_##_axis, \
985 .realbits = (bits), \
987 .shift = 16 - (bits), \
988 .endianness = IIO_LE, \
990 .event_spec = &bmc150_accel_event, \
991 .num_event_specs = 1 \
994 #define BMC150_ACCEL_CHANNELS(bits) { \
997 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
998 BIT(IIO_CHAN_INFO_SCALE) | \
999 BIT(IIO_CHAN_INFO_OFFSET), \
1002 BMC150_ACCEL_CHANNEL(X, bits), \
1003 BMC150_ACCEL_CHANNEL(Y, bits), \
1004 BMC150_ACCEL_CHANNEL(Z, bits), \
1005 IIO_CHAN_SOFT_TIMESTAMP(3), \
1008 static const struct iio_chan_spec bma222e_accel_channels
[] =
1009 BMC150_ACCEL_CHANNELS(8);
1010 static const struct iio_chan_spec bma250e_accel_channels
[] =
1011 BMC150_ACCEL_CHANNELS(10);
1012 static const struct iio_chan_spec bmc150_accel_channels
[] =
1013 BMC150_ACCEL_CHANNELS(12);
1014 static const struct iio_chan_spec bma280_accel_channels
[] =
1015 BMC150_ACCEL_CHANNELS(14);
1017 static const struct bmc150_accel_chip_info bmc150_accel_chip_info_tbl
[] = {
1021 .channels
= bmc150_accel_channels
,
1022 .num_channels
= ARRAY_SIZE(bmc150_accel_channels
),
1023 .scale_table
= { {9610, BMC150_ACCEL_DEF_RANGE_2G
},
1024 {19122, BMC150_ACCEL_DEF_RANGE_4G
},
1025 {38344, BMC150_ACCEL_DEF_RANGE_8G
},
1026 {76590, BMC150_ACCEL_DEF_RANGE_16G
} },
1031 .channels
= bmc150_accel_channels
,
1032 .num_channels
= ARRAY_SIZE(bmc150_accel_channels
),
1033 .scale_table
= { {9610, BMC150_ACCEL_DEF_RANGE_2G
},
1034 {19122, BMC150_ACCEL_DEF_RANGE_4G
},
1035 {38344, BMC150_ACCEL_DEF_RANGE_8G
},
1036 {76590, BMC150_ACCEL_DEF_RANGE_16G
} },
1041 .channels
= bmc150_accel_channels
,
1042 .num_channels
= ARRAY_SIZE(bmc150_accel_channels
),
1043 .scale_table
= { {9610, BMC150_ACCEL_DEF_RANGE_2G
},
1044 {19122, BMC150_ACCEL_DEF_RANGE_4G
},
1045 {38344, BMC150_ACCEL_DEF_RANGE_8G
},
1046 {76590, BMC150_ACCEL_DEF_RANGE_16G
} },
1051 .channels
= bma250e_accel_channels
,
1052 .num_channels
= ARRAY_SIZE(bma250e_accel_channels
),
1053 .scale_table
= { {38344, BMC150_ACCEL_DEF_RANGE_2G
},
1054 {76590, BMC150_ACCEL_DEF_RANGE_4G
},
1055 {153277, BMC150_ACCEL_DEF_RANGE_8G
},
1056 {306457, BMC150_ACCEL_DEF_RANGE_16G
} },
1061 .channels
= bma222e_accel_channels
,
1062 .num_channels
= ARRAY_SIZE(bma222e_accel_channels
),
1063 .scale_table
= { {153277, BMC150_ACCEL_DEF_RANGE_2G
},
1064 {306457, BMC150_ACCEL_DEF_RANGE_4G
},
1065 {612915, BMC150_ACCEL_DEF_RANGE_8G
},
1066 {1225831, BMC150_ACCEL_DEF_RANGE_16G
} },
1071 .channels
= bma280_accel_channels
,
1072 .num_channels
= ARRAY_SIZE(bma280_accel_channels
),
1073 .scale_table
= { {2392, BMC150_ACCEL_DEF_RANGE_2G
},
1074 {4785, BMC150_ACCEL_DEF_RANGE_4G
},
1075 {9581, BMC150_ACCEL_DEF_RANGE_8G
},
1076 {19152, BMC150_ACCEL_DEF_RANGE_16G
} },
1080 static const struct iio_info bmc150_accel_info
= {
1081 .attrs
= &bmc150_accel_attrs_group
,
1082 .read_raw
= bmc150_accel_read_raw
,
1083 .write_raw
= bmc150_accel_write_raw
,
1084 .read_event_value
= bmc150_accel_read_event
,
1085 .write_event_value
= bmc150_accel_write_event
,
1086 .write_event_config
= bmc150_accel_write_event_config
,
1087 .read_event_config
= bmc150_accel_read_event_config
,
1088 .driver_module
= THIS_MODULE
,
1091 static const struct iio_info bmc150_accel_info_fifo
= {
1092 .attrs
= &bmc150_accel_attrs_group
,
1093 .read_raw
= bmc150_accel_read_raw
,
1094 .write_raw
= bmc150_accel_write_raw
,
1095 .read_event_value
= bmc150_accel_read_event
,
1096 .write_event_value
= bmc150_accel_write_event
,
1097 .write_event_config
= bmc150_accel_write_event_config
,
1098 .read_event_config
= bmc150_accel_read_event_config
,
1099 .validate_trigger
= bmc150_accel_validate_trigger
,
1100 .hwfifo_set_watermark
= bmc150_accel_set_watermark
,
1101 .hwfifo_flush_to_buffer
= bmc150_accel_fifo_flush
,
1102 .driver_module
= THIS_MODULE
,
1105 static irqreturn_t
bmc150_accel_trigger_handler(int irq
, void *p
)
1107 struct iio_poll_func
*pf
= p
;
1108 struct iio_dev
*indio_dev
= pf
->indio_dev
;
1109 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
1110 int bit
, ret
, i
= 0;
1111 unsigned int raw_val
;
1113 mutex_lock(&data
->mutex
);
1114 for_each_set_bit(bit
, indio_dev
->active_scan_mask
,
1115 indio_dev
->masklength
) {
1116 ret
= regmap_bulk_read(data
->regmap
,
1117 BMC150_ACCEL_AXIS_TO_REG(bit
), &raw_val
,
1120 mutex_unlock(&data
->mutex
);
1123 data
->buffer
[i
++] = raw_val
;
1125 mutex_unlock(&data
->mutex
);
1127 iio_push_to_buffers_with_timestamp(indio_dev
, data
->buffer
,
1130 iio_trigger_notify_done(indio_dev
->trig
);
1135 static int bmc150_accel_trig_try_reen(struct iio_trigger
*trig
)
1137 struct bmc150_accel_trigger
*t
= iio_trigger_get_drvdata(trig
);
1138 struct bmc150_accel_data
*data
= t
->data
;
1141 /* new data interrupts don't need ack */
1142 if (t
== &t
->data
->triggers
[BMC150_ACCEL_TRIGGER_DATA_READY
])
1145 mutex_lock(&data
->mutex
);
1146 /* clear any latched interrupt */
1147 ret
= regmap_write(data
->regmap
, BMC150_ACCEL_REG_INT_RST_LATCH
,
1148 BMC150_ACCEL_INT_MODE_LATCH_INT
|
1149 BMC150_ACCEL_INT_MODE_LATCH_RESET
);
1150 mutex_unlock(&data
->mutex
);
1153 "Error writing reg_int_rst_latch\n");
1160 static int bmc150_accel_trigger_set_state(struct iio_trigger
*trig
,
1163 struct bmc150_accel_trigger
*t
= iio_trigger_get_drvdata(trig
);
1164 struct bmc150_accel_data
*data
= t
->data
;
1167 mutex_lock(&data
->mutex
);
1169 if (t
->enabled
== state
) {
1170 mutex_unlock(&data
->mutex
);
1175 ret
= t
->setup(t
, state
);
1177 mutex_unlock(&data
->mutex
);
1182 ret
= bmc150_accel_set_interrupt(data
, t
->intr
, state
);
1184 mutex_unlock(&data
->mutex
);
1190 mutex_unlock(&data
->mutex
);
1195 static const struct iio_trigger_ops bmc150_accel_trigger_ops
= {
1196 .set_trigger_state
= bmc150_accel_trigger_set_state
,
1197 .try_reenable
= bmc150_accel_trig_try_reen
,
1198 .owner
= THIS_MODULE
,
1201 static int bmc150_accel_handle_roc_event(struct iio_dev
*indio_dev
)
1203 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
1208 ret
= regmap_read(data
->regmap
, BMC150_ACCEL_REG_INT_STATUS_2
, &val
);
1210 dev_err(data
->dev
, "Error reading reg_int_status_2\n");
1214 if (val
& BMC150_ACCEL_ANY_MOTION_BIT_SIGN
)
1215 dir
= IIO_EV_DIR_FALLING
;
1217 dir
= IIO_EV_DIR_RISING
;
1219 if (val
& BMC150_ACCEL_ANY_MOTION_BIT_X
)
1220 iio_push_event(indio_dev
,
1221 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1228 if (val
& BMC150_ACCEL_ANY_MOTION_BIT_Y
)
1229 iio_push_event(indio_dev
,
1230 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1237 if (val
& BMC150_ACCEL_ANY_MOTION_BIT_Z
)
1238 iio_push_event(indio_dev
,
1239 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1249 static irqreturn_t
bmc150_accel_irq_thread_handler(int irq
, void *private)
1251 struct iio_dev
*indio_dev
= private;
1252 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
1256 mutex_lock(&data
->mutex
);
1258 if (data
->fifo_mode
) {
1259 ret
= __bmc150_accel_fifo_flush(indio_dev
,
1260 BMC150_ACCEL_FIFO_LENGTH
, true);
1265 if (data
->ev_enable_state
) {
1266 ret
= bmc150_accel_handle_roc_event(indio_dev
);
1272 ret
= regmap_write(data
->regmap
, BMC150_ACCEL_REG_INT_RST_LATCH
,
1273 BMC150_ACCEL_INT_MODE_LATCH_INT
|
1274 BMC150_ACCEL_INT_MODE_LATCH_RESET
);
1276 dev_err(data
->dev
, "Error writing reg_int_rst_latch\n");
1283 mutex_unlock(&data
->mutex
);
1288 static irqreturn_t
bmc150_accel_irq_handler(int irq
, void *private)
1290 struct iio_dev
*indio_dev
= private;
1291 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
1295 data
->old_timestamp
= data
->timestamp
;
1296 data
->timestamp
= iio_get_time_ns();
1298 for (i
= 0; i
< BMC150_ACCEL_TRIGGERS
; i
++) {
1299 if (data
->triggers
[i
].enabled
) {
1300 iio_trigger_poll(data
->triggers
[i
].indio_trig
);
1306 if (data
->ev_enable_state
|| data
->fifo_mode
)
1307 return IRQ_WAKE_THREAD
;
1315 static const struct {
1318 int (*setup
)(struct bmc150_accel_trigger
*t
, bool state
);
1319 } bmc150_accel_triggers
[BMC150_ACCEL_TRIGGERS
] = {
1326 .name
= "%s-any-motion-dev%d",
1327 .setup
= bmc150_accel_any_motion_setup
,
1331 static void bmc150_accel_unregister_triggers(struct bmc150_accel_data
*data
,
1336 for (i
= from
; i
>= 0; i
--) {
1337 if (data
->triggers
[i
].indio_trig
) {
1338 iio_trigger_unregister(data
->triggers
[i
].indio_trig
);
1339 data
->triggers
[i
].indio_trig
= NULL
;
1344 static int bmc150_accel_triggers_setup(struct iio_dev
*indio_dev
,
1345 struct bmc150_accel_data
*data
)
1349 for (i
= 0; i
< BMC150_ACCEL_TRIGGERS
; i
++) {
1350 struct bmc150_accel_trigger
*t
= &data
->triggers
[i
];
1352 t
->indio_trig
= devm_iio_trigger_alloc(data
->dev
,
1353 bmc150_accel_triggers
[i
].name
,
1356 if (!t
->indio_trig
) {
1361 t
->indio_trig
->dev
.parent
= data
->dev
;
1362 t
->indio_trig
->ops
= &bmc150_accel_trigger_ops
;
1363 t
->intr
= bmc150_accel_triggers
[i
].intr
;
1365 t
->setup
= bmc150_accel_triggers
[i
].setup
;
1366 iio_trigger_set_drvdata(t
->indio_trig
, t
);
1368 ret
= iio_trigger_register(t
->indio_trig
);
1374 bmc150_accel_unregister_triggers(data
, i
- 1);
1379 #define BMC150_ACCEL_FIFO_MODE_STREAM 0x80
1380 #define BMC150_ACCEL_FIFO_MODE_FIFO 0x40
1381 #define BMC150_ACCEL_FIFO_MODE_BYPASS 0x00
1383 static int bmc150_accel_fifo_set_mode(struct bmc150_accel_data
*data
)
1385 u8 reg
= BMC150_ACCEL_REG_FIFO_CONFIG1
;
1388 ret
= regmap_write(data
->regmap
, reg
, data
->fifo_mode
);
1390 dev_err(data
->dev
, "Error writing reg_fifo_config1\n");
1394 if (!data
->fifo_mode
)
1397 ret
= regmap_write(data
->regmap
, BMC150_ACCEL_REG_FIFO_CONFIG0
,
1400 dev_err(data
->dev
, "Error writing reg_fifo_config0\n");
1405 static int bmc150_accel_buffer_preenable(struct iio_dev
*indio_dev
)
1407 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
1409 return bmc150_accel_set_power_state(data
, true);
1412 static int bmc150_accel_buffer_postenable(struct iio_dev
*indio_dev
)
1414 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
1417 if (indio_dev
->currentmode
== INDIO_BUFFER_TRIGGERED
)
1418 return iio_triggered_buffer_postenable(indio_dev
);
1420 mutex_lock(&data
->mutex
);
1422 if (!data
->watermark
)
1425 ret
= bmc150_accel_set_interrupt(data
, BMC150_ACCEL_INT_WATERMARK
,
1430 data
->fifo_mode
= BMC150_ACCEL_FIFO_MODE_FIFO
;
1432 ret
= bmc150_accel_fifo_set_mode(data
);
1434 data
->fifo_mode
= 0;
1435 bmc150_accel_set_interrupt(data
, BMC150_ACCEL_INT_WATERMARK
,
1440 mutex_unlock(&data
->mutex
);
1445 static int bmc150_accel_buffer_predisable(struct iio_dev
*indio_dev
)
1447 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
1449 if (indio_dev
->currentmode
== INDIO_BUFFER_TRIGGERED
)
1450 return iio_triggered_buffer_predisable(indio_dev
);
1452 mutex_lock(&data
->mutex
);
1454 if (!data
->fifo_mode
)
1457 bmc150_accel_set_interrupt(data
, BMC150_ACCEL_INT_WATERMARK
, false);
1458 __bmc150_accel_fifo_flush(indio_dev
, BMC150_ACCEL_FIFO_LENGTH
, false);
1459 data
->fifo_mode
= 0;
1460 bmc150_accel_fifo_set_mode(data
);
1463 mutex_unlock(&data
->mutex
);
1468 static int bmc150_accel_buffer_postdisable(struct iio_dev
*indio_dev
)
1470 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
1472 return bmc150_accel_set_power_state(data
, false);
1475 static const struct iio_buffer_setup_ops bmc150_accel_buffer_ops
= {
1476 .preenable
= bmc150_accel_buffer_preenable
,
1477 .postenable
= bmc150_accel_buffer_postenable
,
1478 .predisable
= bmc150_accel_buffer_predisable
,
1479 .postdisable
= bmc150_accel_buffer_postdisable
,
1482 static int bmc150_accel_chip_init(struct bmc150_accel_data
*data
)
1488 * Reset chip to get it in a known good state. A delay of 1.8ms after
1489 * reset is required according to the data sheets of supported chips.
1491 regmap_write(data
->regmap
, BMC150_ACCEL_REG_RESET
,
1492 BMC150_ACCEL_RESET_VAL
);
1493 usleep_range(1800, 2500);
1495 ret
= regmap_read(data
->regmap
, BMC150_ACCEL_REG_CHIP_ID
, &val
);
1498 "Error: Reading chip id\n");
1502 dev_dbg(data
->dev
, "Chip Id %x\n", val
);
1503 for (i
= 0; i
< ARRAY_SIZE(bmc150_accel_chip_info_tbl
); i
++) {
1504 if (bmc150_accel_chip_info_tbl
[i
].chip_id
== val
) {
1505 data
->chip_info
= &bmc150_accel_chip_info_tbl
[i
];
1510 if (!data
->chip_info
) {
1511 dev_err(data
->dev
, "Invalid chip %x\n", val
);
1515 ret
= bmc150_accel_set_mode(data
, BMC150_ACCEL_SLEEP_MODE_NORMAL
, 0);
1520 ret
= bmc150_accel_set_bw(data
, BMC150_ACCEL_DEF_BW
, 0);
1524 /* Set Default Range */
1525 ret
= regmap_write(data
->regmap
, BMC150_ACCEL_REG_PMU_RANGE
,
1526 BMC150_ACCEL_DEF_RANGE_4G
);
1529 "Error writing reg_pmu_range\n");
1533 data
->range
= BMC150_ACCEL_DEF_RANGE_4G
;
1535 /* Set default slope duration and thresholds */
1536 data
->slope_thres
= BMC150_ACCEL_DEF_SLOPE_THRESHOLD
;
1537 data
->slope_dur
= BMC150_ACCEL_DEF_SLOPE_DURATION
;
1538 ret
= bmc150_accel_update_slope(data
);
1542 /* Set default as latched interrupts */
1543 ret
= regmap_write(data
->regmap
, BMC150_ACCEL_REG_INT_RST_LATCH
,
1544 BMC150_ACCEL_INT_MODE_LATCH_INT
|
1545 BMC150_ACCEL_INT_MODE_LATCH_RESET
);
1548 "Error writing reg_int_rst_latch\n");
1555 int bmc150_accel_core_probe(struct device
*dev
, struct regmap
*regmap
, int irq
,
1556 const char *name
, bool block_supported
)
1558 struct bmc150_accel_data
*data
;
1559 struct iio_dev
*indio_dev
;
1562 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*data
));
1566 data
= iio_priv(indio_dev
);
1567 dev_set_drvdata(dev
, indio_dev
);
1571 data
->regmap
= regmap
;
1573 ret
= bmc150_accel_chip_init(data
);
1577 mutex_init(&data
->mutex
);
1579 indio_dev
->dev
.parent
= dev
;
1580 indio_dev
->channels
= data
->chip_info
->channels
;
1581 indio_dev
->num_channels
= data
->chip_info
->num_channels
;
1582 indio_dev
->name
= name
? name
: data
->chip_info
->name
;
1583 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1584 indio_dev
->info
= &bmc150_accel_info
;
1586 ret
= iio_triggered_buffer_setup(indio_dev
,
1587 &iio_pollfunc_store_time
,
1588 bmc150_accel_trigger_handler
,
1589 &bmc150_accel_buffer_ops
);
1591 dev_err(data
->dev
, "Failed: iio triggered buffer setup\n");
1595 if (data
->irq
> 0) {
1596 ret
= devm_request_threaded_irq(
1597 data
->dev
, data
->irq
,
1598 bmc150_accel_irq_handler
,
1599 bmc150_accel_irq_thread_handler
,
1600 IRQF_TRIGGER_RISING
,
1601 BMC150_ACCEL_IRQ_NAME
,
1604 goto err_buffer_cleanup
;
1607 * Set latched mode interrupt. While certain interrupts are
1608 * non-latched regardless of this settings (e.g. new data) we
1609 * want to use latch mode when we can to prevent interrupt
1612 ret
= regmap_write(data
->regmap
, BMC150_ACCEL_REG_INT_RST_LATCH
,
1613 BMC150_ACCEL_INT_MODE_LATCH_RESET
);
1615 dev_err(data
->dev
, "Error writing reg_int_rst_latch\n");
1616 goto err_buffer_cleanup
;
1619 bmc150_accel_interrupts_setup(indio_dev
, data
);
1621 ret
= bmc150_accel_triggers_setup(indio_dev
, data
);
1623 goto err_buffer_cleanup
;
1625 if (block_supported
) {
1626 indio_dev
->modes
|= INDIO_BUFFER_SOFTWARE
;
1627 indio_dev
->info
= &bmc150_accel_info_fifo
;
1628 indio_dev
->buffer
->attrs
= bmc150_accel_fifo_attributes
;
1632 ret
= iio_device_register(indio_dev
);
1634 dev_err(dev
, "Unable to register iio device\n");
1635 goto err_trigger_unregister
;
1638 ret
= pm_runtime_set_active(dev
);
1640 goto err_iio_unregister
;
1642 pm_runtime_enable(dev
);
1643 pm_runtime_set_autosuspend_delay(dev
, BMC150_AUTO_SUSPEND_DELAY_MS
);
1644 pm_runtime_use_autosuspend(dev
);
1649 iio_device_unregister(indio_dev
);
1650 err_trigger_unregister
:
1651 bmc150_accel_unregister_triggers(data
, BMC150_ACCEL_TRIGGERS
- 1);
1653 iio_triggered_buffer_cleanup(indio_dev
);
1657 EXPORT_SYMBOL_GPL(bmc150_accel_core_probe
);
1659 int bmc150_accel_core_remove(struct device
*dev
)
1661 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1662 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
1664 pm_runtime_disable(data
->dev
);
1665 pm_runtime_set_suspended(data
->dev
);
1666 pm_runtime_put_noidle(data
->dev
);
1668 iio_device_unregister(indio_dev
);
1670 bmc150_accel_unregister_triggers(data
, BMC150_ACCEL_TRIGGERS
- 1);
1672 iio_triggered_buffer_cleanup(indio_dev
);
1674 mutex_lock(&data
->mutex
);
1675 bmc150_accel_set_mode(data
, BMC150_ACCEL_SLEEP_MODE_DEEP_SUSPEND
, 0);
1676 mutex_unlock(&data
->mutex
);
1680 EXPORT_SYMBOL_GPL(bmc150_accel_core_remove
);
1682 #ifdef CONFIG_PM_SLEEP
1683 static int bmc150_accel_suspend(struct device
*dev
)
1685 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1686 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
1688 mutex_lock(&data
->mutex
);
1689 bmc150_accel_set_mode(data
, BMC150_ACCEL_SLEEP_MODE_SUSPEND
, 0);
1690 mutex_unlock(&data
->mutex
);
1695 static int bmc150_accel_resume(struct device
*dev
)
1697 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1698 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
1700 mutex_lock(&data
->mutex
);
1701 bmc150_accel_set_mode(data
, BMC150_ACCEL_SLEEP_MODE_NORMAL
, 0);
1702 bmc150_accel_fifo_set_mode(data
);
1703 mutex_unlock(&data
->mutex
);
1710 static int bmc150_accel_runtime_suspend(struct device
*dev
)
1712 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1713 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
1716 dev_dbg(data
->dev
, __func__
);
1717 ret
= bmc150_accel_set_mode(data
, BMC150_ACCEL_SLEEP_MODE_SUSPEND
, 0);
1724 static int bmc150_accel_runtime_resume(struct device
*dev
)
1726 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1727 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
1731 dev_dbg(data
->dev
, __func__
);
1733 ret
= bmc150_accel_set_mode(data
, BMC150_ACCEL_SLEEP_MODE_NORMAL
, 0);
1736 ret
= bmc150_accel_fifo_set_mode(data
);
1740 sleep_val
= bmc150_accel_get_startup_times(data
);
1742 usleep_range(sleep_val
* 1000, 20000);
1744 msleep_interruptible(sleep_val
);
1750 const struct dev_pm_ops bmc150_accel_pm_ops
= {
1751 SET_SYSTEM_SLEEP_PM_OPS(bmc150_accel_suspend
, bmc150_accel_resume
)
1752 SET_RUNTIME_PM_OPS(bmc150_accel_runtime_suspend
,
1753 bmc150_accel_runtime_resume
, NULL
)
1755 EXPORT_SYMBOL_GPL(bmc150_accel_pm_ops
);
1757 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
1758 MODULE_LICENSE("GPL v2");
1759 MODULE_DESCRIPTION("BMC150 accelerometer driver");