1 // SPDX-License-Identifier: GPL-2.0-only
3 * 3-axis accelerometer driver supporting following Bosch-Sensortec chips:
11 * Copyright (c) 2014, Intel Corporation.
14 #include <linux/module.h>
15 #include <linux/i2c.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/events.h>
26 #include <linux/iio/trigger.h>
27 #include <linux/iio/trigger_consumer.h>
28 #include <linux/iio/triggered_buffer.h>
29 #include <linux/regmap.h>
31 #include "bmc150-accel.h"
33 #define BMC150_ACCEL_DRV_NAME "bmc150_accel"
34 #define BMC150_ACCEL_IRQ_NAME "bmc150_accel_event"
36 #define BMC150_ACCEL_REG_CHIP_ID 0x00
38 #define BMC150_ACCEL_REG_INT_STATUS_2 0x0B
39 #define BMC150_ACCEL_ANY_MOTION_MASK 0x07
40 #define BMC150_ACCEL_ANY_MOTION_BIT_X BIT(0)
41 #define BMC150_ACCEL_ANY_MOTION_BIT_Y BIT(1)
42 #define BMC150_ACCEL_ANY_MOTION_BIT_Z BIT(2)
43 #define BMC150_ACCEL_ANY_MOTION_BIT_SIGN BIT(3)
45 #define BMC150_ACCEL_REG_PMU_LPW 0x11
46 #define BMC150_ACCEL_PMU_MODE_MASK 0xE0
47 #define BMC150_ACCEL_PMU_MODE_SHIFT 5
48 #define BMC150_ACCEL_PMU_BIT_SLEEP_DUR_MASK 0x17
49 #define BMC150_ACCEL_PMU_BIT_SLEEP_DUR_SHIFT 1
51 #define BMC150_ACCEL_REG_PMU_RANGE 0x0F
53 #define BMC150_ACCEL_DEF_RANGE_2G 0x03
54 #define BMC150_ACCEL_DEF_RANGE_4G 0x05
55 #define BMC150_ACCEL_DEF_RANGE_8G 0x08
56 #define BMC150_ACCEL_DEF_RANGE_16G 0x0C
58 /* Default BW: 125Hz */
59 #define BMC150_ACCEL_REG_PMU_BW 0x10
60 #define BMC150_ACCEL_DEF_BW 125
62 #define BMC150_ACCEL_REG_RESET 0x14
63 #define BMC150_ACCEL_RESET_VAL 0xB6
65 #define BMC150_ACCEL_REG_INT_MAP_0 0x19
66 #define BMC150_ACCEL_INT_MAP_0_BIT_SLOPE BIT(2)
68 #define BMC150_ACCEL_REG_INT_MAP_1 0x1A
69 #define BMC150_ACCEL_INT_MAP_1_BIT_DATA BIT(0)
70 #define BMC150_ACCEL_INT_MAP_1_BIT_FWM BIT(1)
71 #define BMC150_ACCEL_INT_MAP_1_BIT_FFULL BIT(2)
73 #define BMC150_ACCEL_REG_INT_RST_LATCH 0x21
74 #define BMC150_ACCEL_INT_MODE_LATCH_RESET 0x80
75 #define BMC150_ACCEL_INT_MODE_LATCH_INT 0x0F
76 #define BMC150_ACCEL_INT_MODE_NON_LATCH_INT 0x00
78 #define BMC150_ACCEL_REG_INT_EN_0 0x16
79 #define BMC150_ACCEL_INT_EN_BIT_SLP_X BIT(0)
80 #define BMC150_ACCEL_INT_EN_BIT_SLP_Y BIT(1)
81 #define BMC150_ACCEL_INT_EN_BIT_SLP_Z BIT(2)
83 #define BMC150_ACCEL_REG_INT_EN_1 0x17
84 #define BMC150_ACCEL_INT_EN_BIT_DATA_EN BIT(4)
85 #define BMC150_ACCEL_INT_EN_BIT_FFULL_EN BIT(5)
86 #define BMC150_ACCEL_INT_EN_BIT_FWM_EN BIT(6)
88 #define BMC150_ACCEL_REG_INT_OUT_CTRL 0x20
89 #define BMC150_ACCEL_INT_OUT_CTRL_INT1_LVL BIT(0)
91 #define BMC150_ACCEL_REG_INT_5 0x27
92 #define BMC150_ACCEL_SLOPE_DUR_MASK 0x03
94 #define BMC150_ACCEL_REG_INT_6 0x28
95 #define BMC150_ACCEL_SLOPE_THRES_MASK 0xFF
97 /* Slope duration in terms of number of samples */
98 #define BMC150_ACCEL_DEF_SLOPE_DURATION 1
99 /* in terms of multiples of g's/LSB, based on range */
100 #define BMC150_ACCEL_DEF_SLOPE_THRESHOLD 1
102 #define BMC150_ACCEL_REG_XOUT_L 0x02
104 #define BMC150_ACCEL_MAX_STARTUP_TIME_MS 100
106 /* Sleep Duration values */
107 #define BMC150_ACCEL_SLEEP_500_MICRO 0x05
108 #define BMC150_ACCEL_SLEEP_1_MS 0x06
109 #define BMC150_ACCEL_SLEEP_2_MS 0x07
110 #define BMC150_ACCEL_SLEEP_4_MS 0x08
111 #define BMC150_ACCEL_SLEEP_6_MS 0x09
112 #define BMC150_ACCEL_SLEEP_10_MS 0x0A
113 #define BMC150_ACCEL_SLEEP_25_MS 0x0B
114 #define BMC150_ACCEL_SLEEP_50_MS 0x0C
115 #define BMC150_ACCEL_SLEEP_100_MS 0x0D
116 #define BMC150_ACCEL_SLEEP_500_MS 0x0E
117 #define BMC150_ACCEL_SLEEP_1_SEC 0x0F
119 #define BMC150_ACCEL_REG_TEMP 0x08
120 #define BMC150_ACCEL_TEMP_CENTER_VAL 23
122 #define BMC150_ACCEL_AXIS_TO_REG(axis) (BMC150_ACCEL_REG_XOUT_L + (axis * 2))
123 #define BMC150_AUTO_SUSPEND_DELAY_MS 2000
125 #define BMC150_ACCEL_REG_FIFO_STATUS 0x0E
126 #define BMC150_ACCEL_REG_FIFO_CONFIG0 0x30
127 #define BMC150_ACCEL_REG_FIFO_CONFIG1 0x3E
128 #define BMC150_ACCEL_REG_FIFO_DATA 0x3F
129 #define BMC150_ACCEL_FIFO_LENGTH 32
131 enum bmc150_accel_axis
{
138 enum bmc150_power_modes
{
139 BMC150_ACCEL_SLEEP_MODE_NORMAL
,
140 BMC150_ACCEL_SLEEP_MODE_DEEP_SUSPEND
,
141 BMC150_ACCEL_SLEEP_MODE_LPM
,
142 BMC150_ACCEL_SLEEP_MODE_SUSPEND
= 0x04,
145 struct bmc150_scale_info
{
150 struct bmc150_accel_chip_info
{
153 const struct iio_chan_spec
*channels
;
155 const struct bmc150_scale_info scale_table
[4];
158 struct bmc150_accel_interrupt
{
159 const struct bmc150_accel_interrupt_info
*info
;
163 struct bmc150_accel_trigger
{
164 struct bmc150_accel_data
*data
;
165 struct iio_trigger
*indio_trig
;
166 int (*setup
)(struct bmc150_accel_trigger
*t
, bool state
);
171 enum bmc150_accel_interrupt_id
{
172 BMC150_ACCEL_INT_DATA_READY
,
173 BMC150_ACCEL_INT_ANY_MOTION
,
174 BMC150_ACCEL_INT_WATERMARK
,
175 BMC150_ACCEL_INTERRUPTS
,
178 enum bmc150_accel_trigger_id
{
179 BMC150_ACCEL_TRIGGER_DATA_READY
,
180 BMC150_ACCEL_TRIGGER_ANY_MOTION
,
181 BMC150_ACCEL_TRIGGERS
,
184 struct bmc150_accel_data
{
185 struct regmap
*regmap
;
187 struct bmc150_accel_interrupt interrupts
[BMC150_ACCEL_INTERRUPTS
];
188 struct bmc150_accel_trigger triggers
[BMC150_ACCEL_TRIGGERS
];
190 u8 fifo_mode
, watermark
;
197 int64_t timestamp
, old_timestamp
; /* Only used in hw fifo mode. */
198 const struct bmc150_accel_chip_info
*chip_info
;
199 struct iio_mount_matrix orientation
;
202 static const struct {
206 } bmc150_accel_samp_freq_table
[] = { {15, 620000, 0x08},
215 static const struct {
218 } bmc150_accel_sample_upd_time
[] = { {0x08, 64},
227 static const struct {
230 } bmc150_accel_sleep_value_table
[] = { {0, 0},
231 {500, BMC150_ACCEL_SLEEP_500_MICRO
},
232 {1000, BMC150_ACCEL_SLEEP_1_MS
},
233 {2000, BMC150_ACCEL_SLEEP_2_MS
},
234 {4000, BMC150_ACCEL_SLEEP_4_MS
},
235 {6000, BMC150_ACCEL_SLEEP_6_MS
},
236 {10000, BMC150_ACCEL_SLEEP_10_MS
},
237 {25000, BMC150_ACCEL_SLEEP_25_MS
},
238 {50000, BMC150_ACCEL_SLEEP_50_MS
},
239 {100000, BMC150_ACCEL_SLEEP_100_MS
},
240 {500000, BMC150_ACCEL_SLEEP_500_MS
},
241 {1000000, BMC150_ACCEL_SLEEP_1_SEC
} };
243 const struct regmap_config bmc150_regmap_conf
= {
246 .max_register
= 0x3f,
248 EXPORT_SYMBOL_GPL(bmc150_regmap_conf
);
250 static int bmc150_accel_set_mode(struct bmc150_accel_data
*data
,
251 enum bmc150_power_modes mode
,
254 struct device
*dev
= regmap_get_device(data
->regmap
);
261 for (i
= 0; i
< ARRAY_SIZE(bmc150_accel_sleep_value_table
);
263 if (bmc150_accel_sleep_value_table
[i
].sleep_dur
==
266 bmc150_accel_sleep_value_table
[i
].reg_value
;
275 lpw_bits
= mode
<< BMC150_ACCEL_PMU_MODE_SHIFT
;
276 lpw_bits
|= (dur_val
<< BMC150_ACCEL_PMU_BIT_SLEEP_DUR_SHIFT
);
278 dev_dbg(dev
, "Set Mode bits %x\n", lpw_bits
);
280 ret
= regmap_write(data
->regmap
, BMC150_ACCEL_REG_PMU_LPW
, lpw_bits
);
282 dev_err(dev
, "Error writing reg_pmu_lpw\n");
289 static int bmc150_accel_set_bw(struct bmc150_accel_data
*data
, int val
,
295 for (i
= 0; i
< ARRAY_SIZE(bmc150_accel_samp_freq_table
); ++i
) {
296 if (bmc150_accel_samp_freq_table
[i
].val
== val
&&
297 bmc150_accel_samp_freq_table
[i
].val2
== val2
) {
298 ret
= regmap_write(data
->regmap
,
299 BMC150_ACCEL_REG_PMU_BW
,
300 bmc150_accel_samp_freq_table
[i
].bw_bits
);
305 bmc150_accel_samp_freq_table
[i
].bw_bits
;
313 static int bmc150_accel_update_slope(struct bmc150_accel_data
*data
)
315 struct device
*dev
= regmap_get_device(data
->regmap
);
318 ret
= regmap_write(data
->regmap
, BMC150_ACCEL_REG_INT_6
,
321 dev_err(dev
, "Error writing reg_int_6\n");
325 ret
= regmap_update_bits(data
->regmap
, BMC150_ACCEL_REG_INT_5
,
326 BMC150_ACCEL_SLOPE_DUR_MASK
, data
->slope_dur
);
328 dev_err(dev
, "Error updating reg_int_5\n");
332 dev_dbg(dev
, "%x %x\n", data
->slope_thres
, data
->slope_dur
);
337 static int bmc150_accel_any_motion_setup(struct bmc150_accel_trigger
*t
,
341 return bmc150_accel_update_slope(t
->data
);
346 static int bmc150_accel_get_bw(struct bmc150_accel_data
*data
, int *val
,
351 for (i
= 0; i
< ARRAY_SIZE(bmc150_accel_samp_freq_table
); ++i
) {
352 if (bmc150_accel_samp_freq_table
[i
].bw_bits
== data
->bw_bits
) {
353 *val
= bmc150_accel_samp_freq_table
[i
].val
;
354 *val2
= bmc150_accel_samp_freq_table
[i
].val2
;
355 return IIO_VAL_INT_PLUS_MICRO
;
363 static int bmc150_accel_get_startup_times(struct bmc150_accel_data
*data
)
367 for (i
= 0; i
< ARRAY_SIZE(bmc150_accel_sample_upd_time
); ++i
) {
368 if (bmc150_accel_sample_upd_time
[i
].bw_bits
== data
->bw_bits
)
369 return bmc150_accel_sample_upd_time
[i
].msec
;
372 return BMC150_ACCEL_MAX_STARTUP_TIME_MS
;
375 static int bmc150_accel_set_power_state(struct bmc150_accel_data
*data
, bool on
)
377 struct device
*dev
= regmap_get_device(data
->regmap
);
381 ret
= pm_runtime_get_sync(dev
);
383 pm_runtime_mark_last_busy(dev
);
384 ret
= pm_runtime_put_autosuspend(dev
);
389 "Failed: %s for %d\n", __func__
, on
);
391 pm_runtime_put_noidle(dev
);
399 static int bmc150_accel_set_power_state(struct bmc150_accel_data
*data
, bool on
)
405 static const struct bmc150_accel_interrupt_info
{
410 } bmc150_accel_interrupts
[BMC150_ACCEL_INTERRUPTS
] = {
411 { /* data ready interrupt */
412 .map_reg
= BMC150_ACCEL_REG_INT_MAP_1
,
413 .map_bitmask
= BMC150_ACCEL_INT_MAP_1_BIT_DATA
,
414 .en_reg
= BMC150_ACCEL_REG_INT_EN_1
,
415 .en_bitmask
= BMC150_ACCEL_INT_EN_BIT_DATA_EN
,
417 { /* motion interrupt */
418 .map_reg
= BMC150_ACCEL_REG_INT_MAP_0
,
419 .map_bitmask
= BMC150_ACCEL_INT_MAP_0_BIT_SLOPE
,
420 .en_reg
= BMC150_ACCEL_REG_INT_EN_0
,
421 .en_bitmask
= BMC150_ACCEL_INT_EN_BIT_SLP_X
|
422 BMC150_ACCEL_INT_EN_BIT_SLP_Y
|
423 BMC150_ACCEL_INT_EN_BIT_SLP_Z
425 { /* fifo watermark interrupt */
426 .map_reg
= BMC150_ACCEL_REG_INT_MAP_1
,
427 .map_bitmask
= BMC150_ACCEL_INT_MAP_1_BIT_FWM
,
428 .en_reg
= BMC150_ACCEL_REG_INT_EN_1
,
429 .en_bitmask
= BMC150_ACCEL_INT_EN_BIT_FWM_EN
,
433 static void bmc150_accel_interrupts_setup(struct iio_dev
*indio_dev
,
434 struct bmc150_accel_data
*data
)
438 for (i
= 0; i
< BMC150_ACCEL_INTERRUPTS
; i
++)
439 data
->interrupts
[i
].info
= &bmc150_accel_interrupts
[i
];
442 static int bmc150_accel_set_interrupt(struct bmc150_accel_data
*data
, int i
,
445 struct device
*dev
= regmap_get_device(data
->regmap
);
446 struct bmc150_accel_interrupt
*intr
= &data
->interrupts
[i
];
447 const struct bmc150_accel_interrupt_info
*info
= intr
->info
;
451 if (atomic_inc_return(&intr
->users
) > 1)
454 if (atomic_dec_return(&intr
->users
) > 0)
459 * We will expect the enable and disable to do operation in reverse
460 * order. This will happen here anyway, as our resume operation uses
461 * sync mode runtime pm calls. The suspend operation will be delayed
462 * by autosuspend delay.
463 * So the disable operation will still happen in reverse order of
464 * enable operation. When runtime pm is disabled the mode is always on,
465 * so sequence doesn't matter.
467 ret
= bmc150_accel_set_power_state(data
, state
);
471 /* map the interrupt to the appropriate pins */
472 ret
= regmap_update_bits(data
->regmap
, info
->map_reg
, info
->map_bitmask
,
473 (state
? info
->map_bitmask
: 0));
475 dev_err(dev
, "Error updating reg_int_map\n");
476 goto out_fix_power_state
;
479 /* enable/disable the interrupt */
480 ret
= regmap_update_bits(data
->regmap
, info
->en_reg
, info
->en_bitmask
,
481 (state
? info
->en_bitmask
: 0));
483 dev_err(dev
, "Error updating reg_int_en\n");
484 goto out_fix_power_state
;
490 bmc150_accel_set_power_state(data
, false);
494 static int bmc150_accel_set_scale(struct bmc150_accel_data
*data
, int val
)
496 struct device
*dev
= regmap_get_device(data
->regmap
);
499 for (i
= 0; i
< ARRAY_SIZE(data
->chip_info
->scale_table
); ++i
) {
500 if (data
->chip_info
->scale_table
[i
].scale
== val
) {
501 ret
= regmap_write(data
->regmap
,
502 BMC150_ACCEL_REG_PMU_RANGE
,
503 data
->chip_info
->scale_table
[i
].reg_range
);
505 dev_err(dev
, "Error writing pmu_range\n");
509 data
->range
= data
->chip_info
->scale_table
[i
].reg_range
;
517 static int bmc150_accel_get_temp(struct bmc150_accel_data
*data
, int *val
)
519 struct device
*dev
= regmap_get_device(data
->regmap
);
523 mutex_lock(&data
->mutex
);
525 ret
= regmap_read(data
->regmap
, BMC150_ACCEL_REG_TEMP
, &value
);
527 dev_err(dev
, "Error reading reg_temp\n");
528 mutex_unlock(&data
->mutex
);
531 *val
= sign_extend32(value
, 7);
533 mutex_unlock(&data
->mutex
);
538 static int bmc150_accel_get_axis(struct bmc150_accel_data
*data
,
539 struct iio_chan_spec
const *chan
,
542 struct device
*dev
= regmap_get_device(data
->regmap
);
544 int axis
= chan
->scan_index
;
547 mutex_lock(&data
->mutex
);
548 ret
= bmc150_accel_set_power_state(data
, true);
550 mutex_unlock(&data
->mutex
);
554 ret
= regmap_bulk_read(data
->regmap
, BMC150_ACCEL_AXIS_TO_REG(axis
),
555 &raw_val
, sizeof(raw_val
));
557 dev_err(dev
, "Error reading axis %d\n", axis
);
558 bmc150_accel_set_power_state(data
, false);
559 mutex_unlock(&data
->mutex
);
562 *val
= sign_extend32(le16_to_cpu(raw_val
) >> chan
->scan_type
.shift
,
563 chan
->scan_type
.realbits
- 1);
564 ret
= bmc150_accel_set_power_state(data
, false);
565 mutex_unlock(&data
->mutex
);
572 static int bmc150_accel_read_raw(struct iio_dev
*indio_dev
,
573 struct iio_chan_spec
const *chan
,
574 int *val
, int *val2
, long mask
)
576 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
580 case IIO_CHAN_INFO_RAW
:
581 switch (chan
->type
) {
583 return bmc150_accel_get_temp(data
, val
);
585 if (iio_buffer_enabled(indio_dev
))
588 return bmc150_accel_get_axis(data
, chan
, val
);
592 case IIO_CHAN_INFO_OFFSET
:
593 if (chan
->type
== IIO_TEMP
) {
594 *val
= BMC150_ACCEL_TEMP_CENTER_VAL
;
599 case IIO_CHAN_INFO_SCALE
:
601 switch (chan
->type
) {
604 return IIO_VAL_INT_PLUS_MICRO
;
608 const struct bmc150_scale_info
*si
;
609 int st_size
= ARRAY_SIZE(data
->chip_info
->scale_table
);
611 for (i
= 0; i
< st_size
; ++i
) {
612 si
= &data
->chip_info
->scale_table
[i
];
613 if (si
->reg_range
== data
->range
) {
615 return IIO_VAL_INT_PLUS_MICRO
;
623 case IIO_CHAN_INFO_SAMP_FREQ
:
624 mutex_lock(&data
->mutex
);
625 ret
= bmc150_accel_get_bw(data
, val
, val2
);
626 mutex_unlock(&data
->mutex
);
633 static int bmc150_accel_write_raw(struct iio_dev
*indio_dev
,
634 struct iio_chan_spec
const *chan
,
635 int val
, int val2
, long mask
)
637 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
641 case IIO_CHAN_INFO_SAMP_FREQ
:
642 mutex_lock(&data
->mutex
);
643 ret
= bmc150_accel_set_bw(data
, val
, val2
);
644 mutex_unlock(&data
->mutex
);
646 case IIO_CHAN_INFO_SCALE
:
650 mutex_lock(&data
->mutex
);
651 ret
= bmc150_accel_set_scale(data
, val2
);
652 mutex_unlock(&data
->mutex
);
661 static int bmc150_accel_read_event(struct iio_dev
*indio_dev
,
662 const struct iio_chan_spec
*chan
,
663 enum iio_event_type type
,
664 enum iio_event_direction dir
,
665 enum iio_event_info info
,
668 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
672 case IIO_EV_INFO_VALUE
:
673 *val
= data
->slope_thres
;
675 case IIO_EV_INFO_PERIOD
:
676 *val
= data
->slope_dur
;
685 static int bmc150_accel_write_event(struct iio_dev
*indio_dev
,
686 const struct iio_chan_spec
*chan
,
687 enum iio_event_type type
,
688 enum iio_event_direction dir
,
689 enum iio_event_info info
,
692 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
694 if (data
->ev_enable_state
)
698 case IIO_EV_INFO_VALUE
:
699 data
->slope_thres
= val
& BMC150_ACCEL_SLOPE_THRES_MASK
;
701 case IIO_EV_INFO_PERIOD
:
702 data
->slope_dur
= val
& BMC150_ACCEL_SLOPE_DUR_MASK
;
711 static int bmc150_accel_read_event_config(struct iio_dev
*indio_dev
,
712 const struct iio_chan_spec
*chan
,
713 enum iio_event_type type
,
714 enum iio_event_direction dir
)
716 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
718 return data
->ev_enable_state
;
721 static int bmc150_accel_write_event_config(struct iio_dev
*indio_dev
,
722 const struct iio_chan_spec
*chan
,
723 enum iio_event_type type
,
724 enum iio_event_direction dir
,
727 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
730 if (state
== data
->ev_enable_state
)
733 mutex_lock(&data
->mutex
);
735 ret
= bmc150_accel_set_interrupt(data
, BMC150_ACCEL_INT_ANY_MOTION
,
738 mutex_unlock(&data
->mutex
);
742 data
->ev_enable_state
= state
;
743 mutex_unlock(&data
->mutex
);
748 static int bmc150_accel_validate_trigger(struct iio_dev
*indio_dev
,
749 struct iio_trigger
*trig
)
751 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
754 for (i
= 0; i
< BMC150_ACCEL_TRIGGERS
; i
++) {
755 if (data
->triggers
[i
].indio_trig
== trig
)
762 static ssize_t
bmc150_accel_get_fifo_watermark(struct device
*dev
,
763 struct device_attribute
*attr
,
766 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
767 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
770 mutex_lock(&data
->mutex
);
771 wm
= data
->watermark
;
772 mutex_unlock(&data
->mutex
);
774 return sprintf(buf
, "%d\n", wm
);
777 static ssize_t
bmc150_accel_get_fifo_state(struct device
*dev
,
778 struct device_attribute
*attr
,
781 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
782 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
785 mutex_lock(&data
->mutex
);
786 state
= data
->fifo_mode
;
787 mutex_unlock(&data
->mutex
);
789 return sprintf(buf
, "%d\n", state
);
792 static const struct iio_mount_matrix
*
793 bmc150_accel_get_mount_matrix(const struct iio_dev
*indio_dev
,
794 const struct iio_chan_spec
*chan
)
796 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
798 return &data
->orientation
;
801 static const struct iio_chan_spec_ext_info bmc150_accel_ext_info
[] = {
802 IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR
, bmc150_accel_get_mount_matrix
),
806 static IIO_CONST_ATTR(hwfifo_watermark_min
, "1");
807 static IIO_CONST_ATTR(hwfifo_watermark_max
,
808 __stringify(BMC150_ACCEL_FIFO_LENGTH
));
809 static IIO_DEVICE_ATTR(hwfifo_enabled
, S_IRUGO
,
810 bmc150_accel_get_fifo_state
, NULL
, 0);
811 static IIO_DEVICE_ATTR(hwfifo_watermark
, S_IRUGO
,
812 bmc150_accel_get_fifo_watermark
, NULL
, 0);
814 static const struct attribute
*bmc150_accel_fifo_attributes
[] = {
815 &iio_const_attr_hwfifo_watermark_min
.dev_attr
.attr
,
816 &iio_const_attr_hwfifo_watermark_max
.dev_attr
.attr
,
817 &iio_dev_attr_hwfifo_watermark
.dev_attr
.attr
,
818 &iio_dev_attr_hwfifo_enabled
.dev_attr
.attr
,
822 static int bmc150_accel_set_watermark(struct iio_dev
*indio_dev
, unsigned val
)
824 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
826 if (val
> BMC150_ACCEL_FIFO_LENGTH
)
827 val
= BMC150_ACCEL_FIFO_LENGTH
;
829 mutex_lock(&data
->mutex
);
830 data
->watermark
= val
;
831 mutex_unlock(&data
->mutex
);
837 * We must read at least one full frame in one burst, otherwise the rest of the
838 * frame data is discarded.
840 static int bmc150_accel_fifo_transfer(struct bmc150_accel_data
*data
,
841 char *buffer
, int samples
)
843 struct device
*dev
= regmap_get_device(data
->regmap
);
844 int sample_length
= 3 * 2;
846 int total_length
= samples
* sample_length
;
848 ret
= regmap_raw_read(data
->regmap
, BMC150_ACCEL_REG_FIFO_DATA
,
849 buffer
, total_length
);
852 "Error transferring data from fifo: %d\n", ret
);
857 static int __bmc150_accel_fifo_flush(struct iio_dev
*indio_dev
,
858 unsigned samples
, bool irq
)
860 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
861 struct device
*dev
= regmap_get_device(data
->regmap
);
864 u16 buffer
[BMC150_ACCEL_FIFO_LENGTH
* 3];
866 uint64_t sample_period
;
869 ret
= regmap_read(data
->regmap
, BMC150_ACCEL_REG_FIFO_STATUS
, &val
);
871 dev_err(dev
, "Error reading reg_fifo_status\n");
881 * If we getting called from IRQ handler we know the stored timestamp is
882 * fairly accurate for the last stored sample. Otherwise, if we are
883 * called as a result of a read operation from userspace and hence
884 * before the watermark interrupt was triggered, take a timestamp
885 * now. We can fall anywhere in between two samples so the error in this
886 * case is at most one sample period.
889 data
->old_timestamp
= data
->timestamp
;
890 data
->timestamp
= iio_get_time_ns(indio_dev
);
894 * Approximate timestamps for each of the sample based on the sampling
895 * frequency, timestamp for last sample and number of samples.
897 * Note that we can't use the current bandwidth settings to compute the
898 * sample period because the sample rate varies with the device
899 * (e.g. between 31.70ms to 32.20ms for a bandwidth of 15.63HZ). That
900 * small variation adds when we store a large number of samples and
901 * creates significant jitter between the last and first samples in
902 * different batches (e.g. 32ms vs 21ms).
904 * To avoid this issue we compute the actual sample period ourselves
905 * based on the timestamp delta between the last two flush operations.
907 sample_period
= (data
->timestamp
- data
->old_timestamp
);
908 do_div(sample_period
, count
);
909 tstamp
= data
->timestamp
- (count
- 1) * sample_period
;
911 if (samples
&& count
> samples
)
914 ret
= bmc150_accel_fifo_transfer(data
, (u8
*)buffer
, count
);
919 * Ideally we want the IIO core to handle the demux when running in fifo
920 * mode but not when running in triggered buffer mode. Unfortunately
921 * this does not seem to be possible, so stick with driver demux for
924 for (i
= 0; i
< count
; i
++) {
929 for_each_set_bit(bit
, indio_dev
->active_scan_mask
,
930 indio_dev
->masklength
)
931 memcpy(&sample
[j
++], &buffer
[i
* 3 + bit
], 2);
933 iio_push_to_buffers_with_timestamp(indio_dev
, sample
, tstamp
);
935 tstamp
+= sample_period
;
941 static int bmc150_accel_fifo_flush(struct iio_dev
*indio_dev
, unsigned samples
)
943 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
946 mutex_lock(&data
->mutex
);
947 ret
= __bmc150_accel_fifo_flush(indio_dev
, samples
, false);
948 mutex_unlock(&data
->mutex
);
953 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
954 "15.620000 31.260000 62.50000 125 250 500 1000 2000");
956 static struct attribute
*bmc150_accel_attributes
[] = {
957 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
961 static const struct attribute_group bmc150_accel_attrs_group
= {
962 .attrs
= bmc150_accel_attributes
,
965 static const struct iio_event_spec bmc150_accel_event
= {
966 .type
= IIO_EV_TYPE_ROC
,
967 .dir
= IIO_EV_DIR_EITHER
,
968 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
969 BIT(IIO_EV_INFO_ENABLE
) |
970 BIT(IIO_EV_INFO_PERIOD
)
973 #define BMC150_ACCEL_CHANNEL(_axis, bits) { \
976 .channel2 = IIO_MOD_##_axis, \
977 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
978 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
979 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
980 .scan_index = AXIS_##_axis, \
983 .realbits = (bits), \
985 .shift = 16 - (bits), \
986 .endianness = IIO_LE, \
988 .ext_info = bmc150_accel_ext_info, \
989 .event_spec = &bmc150_accel_event, \
990 .num_event_specs = 1 \
993 #define BMC150_ACCEL_CHANNELS(bits) { \
996 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
997 BIT(IIO_CHAN_INFO_SCALE) | \
998 BIT(IIO_CHAN_INFO_OFFSET), \
1001 BMC150_ACCEL_CHANNEL(X, bits), \
1002 BMC150_ACCEL_CHANNEL(Y, bits), \
1003 BMC150_ACCEL_CHANNEL(Z, bits), \
1004 IIO_CHAN_SOFT_TIMESTAMP(3), \
1007 static const struct iio_chan_spec bma222e_accel_channels
[] =
1008 BMC150_ACCEL_CHANNELS(8);
1009 static const struct iio_chan_spec bma250e_accel_channels
[] =
1010 BMC150_ACCEL_CHANNELS(10);
1011 static const struct iio_chan_spec bmc150_accel_channels
[] =
1012 BMC150_ACCEL_CHANNELS(12);
1013 static const struct iio_chan_spec bma280_accel_channels
[] =
1014 BMC150_ACCEL_CHANNELS(14);
1016 static const struct bmc150_accel_chip_info bmc150_accel_chip_info_tbl
[] = {
1020 .channels
= bmc150_accel_channels
,
1021 .num_channels
= ARRAY_SIZE(bmc150_accel_channels
),
1022 .scale_table
= { {9610, BMC150_ACCEL_DEF_RANGE_2G
},
1023 {19122, BMC150_ACCEL_DEF_RANGE_4G
},
1024 {38344, BMC150_ACCEL_DEF_RANGE_8G
},
1025 {76590, BMC150_ACCEL_DEF_RANGE_16G
} },
1030 .channels
= bmc150_accel_channels
,
1031 .num_channels
= ARRAY_SIZE(bmc150_accel_channels
),
1032 .scale_table
= { {9610, BMC150_ACCEL_DEF_RANGE_2G
},
1033 {19122, BMC150_ACCEL_DEF_RANGE_4G
},
1034 {38344, BMC150_ACCEL_DEF_RANGE_8G
},
1035 {76590, BMC150_ACCEL_DEF_RANGE_16G
} },
1040 .channels
= bmc150_accel_channels
,
1041 .num_channels
= ARRAY_SIZE(bmc150_accel_channels
),
1042 .scale_table
= { {9610, BMC150_ACCEL_DEF_RANGE_2G
},
1043 {19122, BMC150_ACCEL_DEF_RANGE_4G
},
1044 {38344, BMC150_ACCEL_DEF_RANGE_8G
},
1045 {76590, BMC150_ACCEL_DEF_RANGE_16G
} },
1050 .channels
= bma250e_accel_channels
,
1051 .num_channels
= ARRAY_SIZE(bma250e_accel_channels
),
1052 .scale_table
= { {38344, BMC150_ACCEL_DEF_RANGE_2G
},
1053 {76590, BMC150_ACCEL_DEF_RANGE_4G
},
1054 {153277, BMC150_ACCEL_DEF_RANGE_8G
},
1055 {306457, BMC150_ACCEL_DEF_RANGE_16G
} },
1060 .channels
= bma222e_accel_channels
,
1061 .num_channels
= ARRAY_SIZE(bma222e_accel_channels
),
1062 .scale_table
= { {153277, BMC150_ACCEL_DEF_RANGE_2G
},
1063 {306457, BMC150_ACCEL_DEF_RANGE_4G
},
1064 {612915, BMC150_ACCEL_DEF_RANGE_8G
},
1065 {1225831, BMC150_ACCEL_DEF_RANGE_16G
} },
1070 .channels
= bma280_accel_channels
,
1071 .num_channels
= ARRAY_SIZE(bma280_accel_channels
),
1072 .scale_table
= { {2392, BMC150_ACCEL_DEF_RANGE_2G
},
1073 {4785, BMC150_ACCEL_DEF_RANGE_4G
},
1074 {9581, BMC150_ACCEL_DEF_RANGE_8G
},
1075 {19152, BMC150_ACCEL_DEF_RANGE_16G
} },
1079 static const struct iio_info bmc150_accel_info
= {
1080 .attrs
= &bmc150_accel_attrs_group
,
1081 .read_raw
= bmc150_accel_read_raw
,
1082 .write_raw
= bmc150_accel_write_raw
,
1083 .read_event_value
= bmc150_accel_read_event
,
1084 .write_event_value
= bmc150_accel_write_event
,
1085 .write_event_config
= bmc150_accel_write_event_config
,
1086 .read_event_config
= bmc150_accel_read_event_config
,
1089 static const struct iio_info bmc150_accel_info_fifo
= {
1090 .attrs
= &bmc150_accel_attrs_group
,
1091 .read_raw
= bmc150_accel_read_raw
,
1092 .write_raw
= bmc150_accel_write_raw
,
1093 .read_event_value
= bmc150_accel_read_event
,
1094 .write_event_value
= bmc150_accel_write_event
,
1095 .write_event_config
= bmc150_accel_write_event_config
,
1096 .read_event_config
= bmc150_accel_read_event_config
,
1097 .validate_trigger
= bmc150_accel_validate_trigger
,
1098 .hwfifo_set_watermark
= bmc150_accel_set_watermark
,
1099 .hwfifo_flush_to_buffer
= bmc150_accel_fifo_flush
,
1102 static const unsigned long bmc150_accel_scan_masks
[] = {
1103 BIT(AXIS_X
) | BIT(AXIS_Y
) | BIT(AXIS_Z
),
1106 static irqreturn_t
bmc150_accel_trigger_handler(int irq
, void *p
)
1108 struct iio_poll_func
*pf
= p
;
1109 struct iio_dev
*indio_dev
= pf
->indio_dev
;
1110 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
1113 mutex_lock(&data
->mutex
);
1114 ret
= regmap_bulk_read(data
->regmap
, BMC150_ACCEL_REG_XOUT_L
,
1115 data
->buffer
, AXIS_MAX
* 2);
1116 mutex_unlock(&data
->mutex
);
1120 iio_push_to_buffers_with_timestamp(indio_dev
, data
->buffer
,
1123 iio_trigger_notify_done(indio_dev
->trig
);
1128 static int bmc150_accel_trig_try_reen(struct iio_trigger
*trig
)
1130 struct bmc150_accel_trigger
*t
= iio_trigger_get_drvdata(trig
);
1131 struct bmc150_accel_data
*data
= t
->data
;
1132 struct device
*dev
= regmap_get_device(data
->regmap
);
1135 /* new data interrupts don't need ack */
1136 if (t
== &t
->data
->triggers
[BMC150_ACCEL_TRIGGER_DATA_READY
])
1139 mutex_lock(&data
->mutex
);
1140 /* clear any latched interrupt */
1141 ret
= regmap_write(data
->regmap
, BMC150_ACCEL_REG_INT_RST_LATCH
,
1142 BMC150_ACCEL_INT_MODE_LATCH_INT
|
1143 BMC150_ACCEL_INT_MODE_LATCH_RESET
);
1144 mutex_unlock(&data
->mutex
);
1146 dev_err(dev
, "Error writing reg_int_rst_latch\n");
1153 static int bmc150_accel_trigger_set_state(struct iio_trigger
*trig
,
1156 struct bmc150_accel_trigger
*t
= iio_trigger_get_drvdata(trig
);
1157 struct bmc150_accel_data
*data
= t
->data
;
1160 mutex_lock(&data
->mutex
);
1162 if (t
->enabled
== state
) {
1163 mutex_unlock(&data
->mutex
);
1168 ret
= t
->setup(t
, state
);
1170 mutex_unlock(&data
->mutex
);
1175 ret
= bmc150_accel_set_interrupt(data
, t
->intr
, state
);
1177 mutex_unlock(&data
->mutex
);
1183 mutex_unlock(&data
->mutex
);
1188 static const struct iio_trigger_ops bmc150_accel_trigger_ops
= {
1189 .set_trigger_state
= bmc150_accel_trigger_set_state
,
1190 .try_reenable
= bmc150_accel_trig_try_reen
,
1193 static int bmc150_accel_handle_roc_event(struct iio_dev
*indio_dev
)
1195 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
1196 struct device
*dev
= regmap_get_device(data
->regmap
);
1201 ret
= regmap_read(data
->regmap
, BMC150_ACCEL_REG_INT_STATUS_2
, &val
);
1203 dev_err(dev
, "Error reading reg_int_status_2\n");
1207 if (val
& BMC150_ACCEL_ANY_MOTION_BIT_SIGN
)
1208 dir
= IIO_EV_DIR_FALLING
;
1210 dir
= IIO_EV_DIR_RISING
;
1212 if (val
& BMC150_ACCEL_ANY_MOTION_BIT_X
)
1213 iio_push_event(indio_dev
,
1214 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1221 if (val
& BMC150_ACCEL_ANY_MOTION_BIT_Y
)
1222 iio_push_event(indio_dev
,
1223 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1230 if (val
& BMC150_ACCEL_ANY_MOTION_BIT_Z
)
1231 iio_push_event(indio_dev
,
1232 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
1242 static irqreturn_t
bmc150_accel_irq_thread_handler(int irq
, void *private)
1244 struct iio_dev
*indio_dev
= private;
1245 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
1246 struct device
*dev
= regmap_get_device(data
->regmap
);
1250 mutex_lock(&data
->mutex
);
1252 if (data
->fifo_mode
) {
1253 ret
= __bmc150_accel_fifo_flush(indio_dev
,
1254 BMC150_ACCEL_FIFO_LENGTH
, true);
1259 if (data
->ev_enable_state
) {
1260 ret
= bmc150_accel_handle_roc_event(indio_dev
);
1266 ret
= regmap_write(data
->regmap
, BMC150_ACCEL_REG_INT_RST_LATCH
,
1267 BMC150_ACCEL_INT_MODE_LATCH_INT
|
1268 BMC150_ACCEL_INT_MODE_LATCH_RESET
);
1270 dev_err(dev
, "Error writing reg_int_rst_latch\n");
1277 mutex_unlock(&data
->mutex
);
1282 static irqreturn_t
bmc150_accel_irq_handler(int irq
, void *private)
1284 struct iio_dev
*indio_dev
= private;
1285 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
1289 data
->old_timestamp
= data
->timestamp
;
1290 data
->timestamp
= iio_get_time_ns(indio_dev
);
1292 for (i
= 0; i
< BMC150_ACCEL_TRIGGERS
; i
++) {
1293 if (data
->triggers
[i
].enabled
) {
1294 iio_trigger_poll(data
->triggers
[i
].indio_trig
);
1300 if (data
->ev_enable_state
|| data
->fifo_mode
)
1301 return IRQ_WAKE_THREAD
;
1309 static const struct {
1312 int (*setup
)(struct bmc150_accel_trigger
*t
, bool state
);
1313 } bmc150_accel_triggers
[BMC150_ACCEL_TRIGGERS
] = {
1320 .name
= "%s-any-motion-dev%d",
1321 .setup
= bmc150_accel_any_motion_setup
,
1325 static void bmc150_accel_unregister_triggers(struct bmc150_accel_data
*data
,
1330 for (i
= from
; i
>= 0; i
--) {
1331 if (data
->triggers
[i
].indio_trig
) {
1332 iio_trigger_unregister(data
->triggers
[i
].indio_trig
);
1333 data
->triggers
[i
].indio_trig
= NULL
;
1338 static int bmc150_accel_triggers_setup(struct iio_dev
*indio_dev
,
1339 struct bmc150_accel_data
*data
)
1341 struct device
*dev
= regmap_get_device(data
->regmap
);
1344 for (i
= 0; i
< BMC150_ACCEL_TRIGGERS
; i
++) {
1345 struct bmc150_accel_trigger
*t
= &data
->triggers
[i
];
1347 t
->indio_trig
= devm_iio_trigger_alloc(dev
,
1348 bmc150_accel_triggers
[i
].name
,
1351 if (!t
->indio_trig
) {
1356 t
->indio_trig
->dev
.parent
= dev
;
1357 t
->indio_trig
->ops
= &bmc150_accel_trigger_ops
;
1358 t
->intr
= bmc150_accel_triggers
[i
].intr
;
1360 t
->setup
= bmc150_accel_triggers
[i
].setup
;
1361 iio_trigger_set_drvdata(t
->indio_trig
, t
);
1363 ret
= iio_trigger_register(t
->indio_trig
);
1369 bmc150_accel_unregister_triggers(data
, i
- 1);
1374 #define BMC150_ACCEL_FIFO_MODE_STREAM 0x80
1375 #define BMC150_ACCEL_FIFO_MODE_FIFO 0x40
1376 #define BMC150_ACCEL_FIFO_MODE_BYPASS 0x00
1378 static int bmc150_accel_fifo_set_mode(struct bmc150_accel_data
*data
)
1380 struct device
*dev
= regmap_get_device(data
->regmap
);
1381 u8 reg
= BMC150_ACCEL_REG_FIFO_CONFIG1
;
1384 ret
= regmap_write(data
->regmap
, reg
, data
->fifo_mode
);
1386 dev_err(dev
, "Error writing reg_fifo_config1\n");
1390 if (!data
->fifo_mode
)
1393 ret
= regmap_write(data
->regmap
, BMC150_ACCEL_REG_FIFO_CONFIG0
,
1396 dev_err(dev
, "Error writing reg_fifo_config0\n");
1401 static int bmc150_accel_buffer_preenable(struct iio_dev
*indio_dev
)
1403 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
1405 return bmc150_accel_set_power_state(data
, true);
1408 static int bmc150_accel_buffer_postenable(struct iio_dev
*indio_dev
)
1410 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
1413 if (indio_dev
->currentmode
== INDIO_BUFFER_TRIGGERED
)
1414 return iio_triggered_buffer_postenable(indio_dev
);
1416 mutex_lock(&data
->mutex
);
1418 if (!data
->watermark
)
1421 ret
= bmc150_accel_set_interrupt(data
, BMC150_ACCEL_INT_WATERMARK
,
1426 data
->fifo_mode
= BMC150_ACCEL_FIFO_MODE_FIFO
;
1428 ret
= bmc150_accel_fifo_set_mode(data
);
1430 data
->fifo_mode
= 0;
1431 bmc150_accel_set_interrupt(data
, BMC150_ACCEL_INT_WATERMARK
,
1436 mutex_unlock(&data
->mutex
);
1441 static int bmc150_accel_buffer_predisable(struct iio_dev
*indio_dev
)
1443 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
1445 if (indio_dev
->currentmode
== INDIO_BUFFER_TRIGGERED
)
1446 return iio_triggered_buffer_predisable(indio_dev
);
1448 mutex_lock(&data
->mutex
);
1450 if (!data
->fifo_mode
)
1453 bmc150_accel_set_interrupt(data
, BMC150_ACCEL_INT_WATERMARK
, false);
1454 __bmc150_accel_fifo_flush(indio_dev
, BMC150_ACCEL_FIFO_LENGTH
, false);
1455 data
->fifo_mode
= 0;
1456 bmc150_accel_fifo_set_mode(data
);
1459 mutex_unlock(&data
->mutex
);
1464 static int bmc150_accel_buffer_postdisable(struct iio_dev
*indio_dev
)
1466 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
1468 return bmc150_accel_set_power_state(data
, false);
1471 static const struct iio_buffer_setup_ops bmc150_accel_buffer_ops
= {
1472 .preenable
= bmc150_accel_buffer_preenable
,
1473 .postenable
= bmc150_accel_buffer_postenable
,
1474 .predisable
= bmc150_accel_buffer_predisable
,
1475 .postdisable
= bmc150_accel_buffer_postdisable
,
1478 static int bmc150_accel_chip_init(struct bmc150_accel_data
*data
)
1480 struct device
*dev
= regmap_get_device(data
->regmap
);
1485 * Reset chip to get it in a known good state. A delay of 1.8ms after
1486 * reset is required according to the data sheets of supported chips.
1488 regmap_write(data
->regmap
, BMC150_ACCEL_REG_RESET
,
1489 BMC150_ACCEL_RESET_VAL
);
1490 usleep_range(1800, 2500);
1492 ret
= regmap_read(data
->regmap
, BMC150_ACCEL_REG_CHIP_ID
, &val
);
1494 dev_err(dev
, "Error: Reading chip id\n");
1498 dev_dbg(dev
, "Chip Id %x\n", val
);
1499 for (i
= 0; i
< ARRAY_SIZE(bmc150_accel_chip_info_tbl
); i
++) {
1500 if (bmc150_accel_chip_info_tbl
[i
].chip_id
== val
) {
1501 data
->chip_info
= &bmc150_accel_chip_info_tbl
[i
];
1506 if (!data
->chip_info
) {
1507 dev_err(dev
, "Invalid chip %x\n", val
);
1511 ret
= bmc150_accel_set_mode(data
, BMC150_ACCEL_SLEEP_MODE_NORMAL
, 0);
1516 ret
= bmc150_accel_set_bw(data
, BMC150_ACCEL_DEF_BW
, 0);
1520 /* Set Default Range */
1521 ret
= regmap_write(data
->regmap
, BMC150_ACCEL_REG_PMU_RANGE
,
1522 BMC150_ACCEL_DEF_RANGE_4G
);
1524 dev_err(dev
, "Error writing reg_pmu_range\n");
1528 data
->range
= BMC150_ACCEL_DEF_RANGE_4G
;
1530 /* Set default slope duration and thresholds */
1531 data
->slope_thres
= BMC150_ACCEL_DEF_SLOPE_THRESHOLD
;
1532 data
->slope_dur
= BMC150_ACCEL_DEF_SLOPE_DURATION
;
1533 ret
= bmc150_accel_update_slope(data
);
1537 /* Set default as latched interrupts */
1538 ret
= regmap_write(data
->regmap
, BMC150_ACCEL_REG_INT_RST_LATCH
,
1539 BMC150_ACCEL_INT_MODE_LATCH_INT
|
1540 BMC150_ACCEL_INT_MODE_LATCH_RESET
);
1542 dev_err(dev
, "Error writing reg_int_rst_latch\n");
1549 int bmc150_accel_core_probe(struct device
*dev
, struct regmap
*regmap
, int irq
,
1550 const char *name
, bool block_supported
)
1552 struct bmc150_accel_data
*data
;
1553 struct iio_dev
*indio_dev
;
1556 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*data
));
1560 data
= iio_priv(indio_dev
);
1561 dev_set_drvdata(dev
, indio_dev
);
1564 data
->regmap
= regmap
;
1566 ret
= iio_read_mount_matrix(dev
, "mount-matrix",
1567 &data
->orientation
);
1571 ret
= bmc150_accel_chip_init(data
);
1575 mutex_init(&data
->mutex
);
1577 indio_dev
->dev
.parent
= dev
;
1578 indio_dev
->channels
= data
->chip_info
->channels
;
1579 indio_dev
->num_channels
= data
->chip_info
->num_channels
;
1580 indio_dev
->name
= name
? name
: data
->chip_info
->name
;
1581 indio_dev
->available_scan_masks
= bmc150_accel_scan_masks
;
1582 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1583 indio_dev
->info
= &bmc150_accel_info
;
1585 ret
= iio_triggered_buffer_setup(indio_dev
,
1586 &iio_pollfunc_store_time
,
1587 bmc150_accel_trigger_handler
,
1588 &bmc150_accel_buffer_ops
);
1590 dev_err(dev
, "Failed: iio triggered buffer setup\n");
1594 if (data
->irq
> 0) {
1595 ret
= devm_request_threaded_irq(
1597 bmc150_accel_irq_handler
,
1598 bmc150_accel_irq_thread_handler
,
1599 IRQF_TRIGGER_RISING
,
1600 BMC150_ACCEL_IRQ_NAME
,
1603 goto err_buffer_cleanup
;
1606 * Set latched mode interrupt. While certain interrupts are
1607 * non-latched regardless of this settings (e.g. new data) we
1608 * want to use latch mode when we can to prevent interrupt
1611 ret
= regmap_write(data
->regmap
, BMC150_ACCEL_REG_INT_RST_LATCH
,
1612 BMC150_ACCEL_INT_MODE_LATCH_RESET
);
1614 dev_err(dev
, "Error writing reg_int_rst_latch\n");
1615 goto err_buffer_cleanup
;
1618 bmc150_accel_interrupts_setup(indio_dev
, data
);
1620 ret
= bmc150_accel_triggers_setup(indio_dev
, data
);
1622 goto err_buffer_cleanup
;
1624 if (block_supported
) {
1625 indio_dev
->modes
|= INDIO_BUFFER_SOFTWARE
;
1626 indio_dev
->info
= &bmc150_accel_info_fifo
;
1627 iio_buffer_set_attrs(indio_dev
->buffer
,
1628 bmc150_accel_fifo_attributes
);
1632 ret
= pm_runtime_set_active(dev
);
1634 goto err_trigger_unregister
;
1636 pm_runtime_enable(dev
);
1637 pm_runtime_set_autosuspend_delay(dev
, BMC150_AUTO_SUSPEND_DELAY_MS
);
1638 pm_runtime_use_autosuspend(dev
);
1640 ret
= iio_device_register(indio_dev
);
1642 dev_err(dev
, "Unable to register iio device\n");
1643 goto err_trigger_unregister
;
1648 err_trigger_unregister
:
1649 bmc150_accel_unregister_triggers(data
, BMC150_ACCEL_TRIGGERS
- 1);
1651 iio_triggered_buffer_cleanup(indio_dev
);
1655 EXPORT_SYMBOL_GPL(bmc150_accel_core_probe
);
1657 int bmc150_accel_core_remove(struct device
*dev
)
1659 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1660 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
1662 iio_device_unregister(indio_dev
);
1664 pm_runtime_disable(dev
);
1665 pm_runtime_set_suspended(dev
);
1666 pm_runtime_put_noidle(dev
);
1668 bmc150_accel_unregister_triggers(data
, BMC150_ACCEL_TRIGGERS
- 1);
1670 iio_triggered_buffer_cleanup(indio_dev
);
1672 mutex_lock(&data
->mutex
);
1673 bmc150_accel_set_mode(data
, BMC150_ACCEL_SLEEP_MODE_DEEP_SUSPEND
, 0);
1674 mutex_unlock(&data
->mutex
);
1678 EXPORT_SYMBOL_GPL(bmc150_accel_core_remove
);
1680 #ifdef CONFIG_PM_SLEEP
1681 static int bmc150_accel_suspend(struct device
*dev
)
1683 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1684 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
1686 mutex_lock(&data
->mutex
);
1687 bmc150_accel_set_mode(data
, BMC150_ACCEL_SLEEP_MODE_SUSPEND
, 0);
1688 mutex_unlock(&data
->mutex
);
1693 static int bmc150_accel_resume(struct device
*dev
)
1695 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1696 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
1698 mutex_lock(&data
->mutex
);
1699 bmc150_accel_set_mode(data
, BMC150_ACCEL_SLEEP_MODE_NORMAL
, 0);
1700 bmc150_accel_fifo_set_mode(data
);
1701 mutex_unlock(&data
->mutex
);
1708 static int bmc150_accel_runtime_suspend(struct device
*dev
)
1710 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1711 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
1714 ret
= bmc150_accel_set_mode(data
, BMC150_ACCEL_SLEEP_MODE_SUSPEND
, 0);
1721 static int bmc150_accel_runtime_resume(struct device
*dev
)
1723 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1724 struct bmc150_accel_data
*data
= iio_priv(indio_dev
);
1728 ret
= bmc150_accel_set_mode(data
, BMC150_ACCEL_SLEEP_MODE_NORMAL
, 0);
1731 ret
= bmc150_accel_fifo_set_mode(data
);
1735 sleep_val
= bmc150_accel_get_startup_times(data
);
1737 usleep_range(sleep_val
* 1000, 20000);
1739 msleep_interruptible(sleep_val
);
1745 const struct dev_pm_ops bmc150_accel_pm_ops
= {
1746 SET_SYSTEM_SLEEP_PM_OPS(bmc150_accel_suspend
, bmc150_accel_resume
)
1747 SET_RUNTIME_PM_OPS(bmc150_accel_runtime_suspend
,
1748 bmc150_accel_runtime_resume
, NULL
)
1750 EXPORT_SYMBOL_GPL(bmc150_accel_pm_ops
);
1752 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
1753 MODULE_LICENSE("GPL v2");
1754 MODULE_DESCRIPTION("BMC150 accelerometer driver");