1 // SPDX-License-Identifier: GPL-2.0-only
3 * bma180.c - IIO driver for Bosch BMA180 triaxial acceleration sensor
5 * Copyright 2013 Oleksandr Kravchenko <x0199363@ti.com>
7 * Support for BMA250 (c) Peter Meerwald <pmeerw@pmeerw.net>
9 * SPI is not supported by driver
10 * BMA023/BMA150/SMB380: 7-bit I2C slave address 0x38
11 * BMA180: 7-bit I2C slave address 0x40 or 0x41
12 * BMA250: 7-bit I2C slave address 0x18 or 0x19
15 #include <linux/module.h>
16 #include <linux/mod_devicetable.h>
17 #include <linux/i2c.h>
18 #include <linux/interrupt.h>
19 #include <linux/delay.h>
20 #include <linux/bitops.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/slab.h>
23 #include <linux/string.h>
24 #include <linux/iio/iio.h>
25 #include <linux/iio/sysfs.h>
26 #include <linux/iio/buffer.h>
27 #include <linux/iio/trigger.h>
28 #include <linux/iio/trigger_consumer.h>
29 #include <linux/iio/triggered_buffer.h>
31 #define BMA180_DRV_NAME "bma180"
32 #define BMA180_IRQ_NAME "bma180_event"
43 struct bma180_part_info
{
45 const struct iio_chan_spec
*channels
;
46 unsigned int num_channels
;
47 const int *scale_table
;
48 unsigned int num_scales
;
53 u8 int_reset_reg
, int_reset_mask
;
54 u8 sleep_reg
, sleep_mask
;
55 u8 bw_reg
, bw_mask
, bw_offset
;
56 u8 scale_reg
, scale_mask
;
57 u8 power_reg
, power_mask
, lowpower_val
;
58 u8 int_enable_reg
, int_enable_mask
;
59 u8 softreset_reg
, softreset_val
;
61 int (*chip_config
)(struct bma180_data
*data
);
62 void (*chip_disable
)(struct bma180_data
*data
);
66 #define BMA023_CTRL_REG0 0x0a
67 #define BMA023_CTRL_REG1 0x0b
68 #define BMA023_CTRL_REG2 0x14
69 #define BMA023_CTRL_REG3 0x15
71 #define BMA023_RANGE_MASK GENMASK(4, 3) /* Range of accel values */
72 #define BMA023_BW_MASK GENMASK(2, 0) /* Accel bandwidth */
73 #define BMA023_SLEEP BIT(0)
74 #define BMA023_INT_RESET_MASK BIT(6)
75 #define BMA023_NEW_DATA_INT BIT(5) /* Intr every new accel data is ready */
76 #define BMA023_RESET_VAL BIT(1)
78 #define BMA180_CHIP_ID 0x00 /* Need to distinguish BMA180 from other */
79 #define BMA180_ACC_X_LSB 0x02 /* First of 6 registers of accel data */
80 #define BMA180_TEMP 0x08
81 #define BMA180_CTRL_REG0 0x0d
82 #define BMA180_RESET 0x10
83 #define BMA180_BW_TCS 0x20
84 #define BMA180_CTRL_REG3 0x21
85 #define BMA180_TCO_Z 0x30
86 #define BMA180_OFFSET_LSB1 0x35
88 /* BMA180_CTRL_REG0 bits */
89 #define BMA180_DIS_WAKE_UP BIT(0) /* Disable wake up mode */
90 #define BMA180_SLEEP BIT(1) /* 1 - chip will sleep */
91 #define BMA180_EE_W BIT(4) /* Unlock writing to addr from 0x20 */
92 #define BMA180_RESET_INT BIT(6) /* Reset pending interrupts */
94 /* BMA180_CTRL_REG3 bits */
95 #define BMA180_NEW_DATA_INT BIT(1) /* Intr every new accel data is ready */
97 /* BMA180_OFFSET_LSB1 skipping mode bit */
98 #define BMA180_SMP_SKIP BIT(0)
100 /* Bit masks for registers bit fields */
101 #define BMA180_RANGE 0x0e /* Range of measured accel values */
102 #define BMA180_BW 0xf0 /* Accel bandwidth */
103 #define BMA180_MODE_CONFIG 0x03 /* Config operation modes */
105 /* We have to write this value in reset register to do soft reset */
106 #define BMA180_RESET_VAL 0xb6
108 #define BMA023_ID_REG_VAL 0x02
109 #define BMA180_ID_REG_VAL 0x03
110 #define BMA250_ID_REG_VAL 0x03
112 /* Chip power modes */
113 #define BMA180_LOW_POWER 0x03
115 #define BMA250_RANGE_REG 0x0f
116 #define BMA250_BW_REG 0x10
117 #define BMA250_POWER_REG 0x11
118 #define BMA250_RESET_REG 0x14
119 #define BMA250_INT_ENABLE_REG 0x17
120 #define BMA250_INT_MAP_REG 0x1a
121 #define BMA250_INT_RESET_REG 0x21
123 #define BMA250_RANGE_MASK GENMASK(3, 0) /* Range of accel values */
124 #define BMA250_BW_MASK GENMASK(4, 0) /* Accel bandwidth */
125 #define BMA250_BW_OFFSET 8
126 #define BMA250_SUSPEND_MASK BIT(7) /* chip will sleep */
127 #define BMA250_LOWPOWER_MASK BIT(6)
128 #define BMA250_DATA_INTEN_MASK BIT(4)
129 #define BMA250_INT1_DATA_MASK BIT(0)
130 #define BMA250_INT_RESET_MASK BIT(7) /* Reset pending interrupts */
133 struct regulator
*vdd_supply
;
134 struct regulator
*vddio_supply
;
135 struct i2c_client
*client
;
136 struct iio_trigger
*trig
;
137 const struct bma180_part_info
*part_info
;
138 struct iio_mount_matrix orientation
;
144 /* Ensure timestamp is naturally aligned */
147 s64 timestamp
__aligned(8);
158 static int bma023_bw_table
[] = { 25, 50, 100, 190, 375, 750, 1500 }; /* Hz */
159 static int bma023_scale_table
[] = { 2452, 4903, 9709, };
161 static int bma180_bw_table
[] = { 10, 20, 40, 75, 150, 300 }; /* Hz */
162 static int bma180_scale_table
[] = { 1275, 1863, 2452, 3727, 4903, 9709, 19417 };
164 static int bma250_bw_table
[] = { 8, 16, 31, 63, 125, 250, 500, 1000 }; /* Hz */
165 static int bma250_scale_table
[] = { 0, 0, 0, 38344, 0, 76590, 0, 0, 153180, 0,
168 static int bma180_get_data_reg(struct bma180_data
*data
, enum bma180_chan chan
)
172 if (data
->sleep_state
)
177 ret
= i2c_smbus_read_byte_data(data
->client
, BMA180_TEMP
);
179 dev_err(&data
->client
->dev
, "failed to read temp register\n");
182 ret
= i2c_smbus_read_word_data(data
->client
,
183 BMA180_ACC_X_LSB
+ chan
* 2);
185 dev_err(&data
->client
->dev
,
186 "failed to read accel_%c register\n",
193 static int bma180_set_bits(struct bma180_data
*data
, u8 reg
, u8 mask
, u8 val
)
195 int ret
= i2c_smbus_read_byte_data(data
->client
, reg
);
196 u8 reg_val
= (ret
& ~mask
) | (val
<< (ffs(mask
) - 1));
201 return i2c_smbus_write_byte_data(data
->client
, reg
, reg_val
);
204 static int bma180_reset_intr(struct bma180_data
*data
)
206 int ret
= bma180_set_bits(data
, data
->part_info
->int_reset_reg
,
207 data
->part_info
->int_reset_mask
, 1);
210 dev_err(&data
->client
->dev
, "failed to reset interrupt\n");
215 static int bma180_set_new_data_intr_state(struct bma180_data
*data
, bool state
)
217 int ret
= bma180_set_bits(data
, data
->part_info
->int_enable_reg
,
218 data
->part_info
->int_enable_mask
, state
);
221 ret
= bma180_reset_intr(data
);
228 dev_err(&data
->client
->dev
,
229 "failed to set new data interrupt state %d\n", state
);
233 static int bma180_set_sleep_state(struct bma180_data
*data
, bool state
)
235 int ret
= bma180_set_bits(data
, data
->part_info
->sleep_reg
,
236 data
->part_info
->sleep_mask
, state
);
239 dev_err(&data
->client
->dev
,
240 "failed to set sleep state %d\n", state
);
243 data
->sleep_state
= state
;
248 static int bma180_set_ee_writing_state(struct bma180_data
*data
, bool state
)
250 int ret
= bma180_set_bits(data
, BMA180_CTRL_REG0
, BMA180_EE_W
, state
);
253 dev_err(&data
->client
->dev
,
254 "failed to set ee writing state %d\n", state
);
259 static int bma180_set_bw(struct bma180_data
*data
, int val
)
263 if (data
->sleep_state
)
266 for (i
= 0; i
< data
->part_info
->num_bw
; ++i
) {
267 if (data
->part_info
->bw_table
[i
] == val
) {
268 ret
= bma180_set_bits(data
, data
->part_info
->bw_reg
,
269 data
->part_info
->bw_mask
,
270 i
+ data
->part_info
->bw_offset
);
272 dev_err(&data
->client
->dev
,
273 "failed to set bandwidth\n");
284 static int bma180_set_scale(struct bma180_data
*data
, int val
)
288 if (data
->sleep_state
)
291 for (i
= 0; i
< data
->part_info
->num_scales
; ++i
)
292 if (data
->part_info
->scale_table
[i
] == val
) {
293 ret
= bma180_set_bits(data
, data
->part_info
->scale_reg
,
294 data
->part_info
->scale_mask
, i
);
296 dev_err(&data
->client
->dev
,
297 "failed to set scale\n");
307 static int bma180_set_pmode(struct bma180_data
*data
, bool mode
)
309 u8 reg_val
= mode
? data
->part_info
->lowpower_val
: 0;
310 int ret
= bma180_set_bits(data
, data
->part_info
->power_reg
,
311 data
->part_info
->power_mask
, reg_val
);
314 dev_err(&data
->client
->dev
, "failed to set power mode\n");
322 static int bma180_soft_reset(struct bma180_data
*data
)
324 int ret
= i2c_smbus_write_byte_data(data
->client
,
325 data
->part_info
->softreset_reg
,
326 data
->part_info
->softreset_val
);
329 dev_err(&data
->client
->dev
, "failed to reset the chip\n");
334 static int bma180_chip_init(struct bma180_data
*data
)
336 /* Try to read chip_id register. It must return 0x03. */
337 int ret
= i2c_smbus_read_byte_data(data
->client
, BMA180_CHIP_ID
);
341 if (ret
!= data
->part_info
->chip_id
) {
342 dev_err(&data
->client
->dev
, "wrong chip ID %d expected %d\n",
343 ret
, data
->part_info
->chip_id
);
347 ret
= bma180_soft_reset(data
);
351 * No serial transaction should occur within minimum 10 us
352 * after soft_reset command
356 return bma180_set_new_data_intr_state(data
, false);
359 static int bma023_chip_config(struct bma180_data
*data
)
361 int ret
= bma180_chip_init(data
);
366 ret
= bma180_set_bw(data
, 50); /* 50 Hz */
369 ret
= bma180_set_scale(data
, 2452); /* 2 G */
376 dev_err(&data
->client
->dev
, "failed to config the chip\n");
380 static int bma180_chip_config(struct bma180_data
*data
)
382 int ret
= bma180_chip_init(data
);
386 ret
= bma180_set_pmode(data
, false);
389 ret
= bma180_set_bits(data
, BMA180_CTRL_REG0
, BMA180_DIS_WAKE_UP
, 1);
392 ret
= bma180_set_ee_writing_state(data
, true);
395 ret
= bma180_set_bits(data
, BMA180_OFFSET_LSB1
, BMA180_SMP_SKIP
, 1);
398 ret
= bma180_set_bw(data
, 20); /* 20 Hz */
401 ret
= bma180_set_scale(data
, 2452); /* 2 G */
408 dev_err(&data
->client
->dev
, "failed to config the chip\n");
412 static int bma250_chip_config(struct bma180_data
*data
)
414 int ret
= bma180_chip_init(data
);
418 ret
= bma180_set_pmode(data
, false);
421 ret
= bma180_set_bw(data
, 16); /* 16 Hz */
424 ret
= bma180_set_scale(data
, 38344); /* 2 G */
428 * This enables dataready interrupt on the INT1 pin
429 * FIXME: support using the INT2 pin
431 ret
= bma180_set_bits(data
, BMA250_INT_MAP_REG
, BMA250_INT1_DATA_MASK
, 1);
438 dev_err(&data
->client
->dev
, "failed to config the chip\n");
442 static void bma023_chip_disable(struct bma180_data
*data
)
444 if (bma180_set_sleep_state(data
, true))
450 dev_err(&data
->client
->dev
, "failed to disable the chip\n");
453 static void bma180_chip_disable(struct bma180_data
*data
)
455 if (bma180_set_new_data_intr_state(data
, false))
457 if (bma180_set_ee_writing_state(data
, false))
459 if (bma180_set_sleep_state(data
, true))
465 dev_err(&data
->client
->dev
, "failed to disable the chip\n");
468 static void bma250_chip_disable(struct bma180_data
*data
)
470 if (bma180_set_new_data_intr_state(data
, false))
472 if (bma180_set_sleep_state(data
, true))
478 dev_err(&data
->client
->dev
, "failed to disable the chip\n");
481 static ssize_t
bma180_show_avail(char *buf
, const int *vals
, unsigned int n
,
487 for (i
= 0; i
< n
; i
++) {
490 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
,
491 micros
? "0.%06d " : "%d ", vals
[i
]);
498 static ssize_t
bma180_show_filter_freq_avail(struct device
*dev
,
499 struct device_attribute
*attr
, char *buf
)
501 struct bma180_data
*data
= iio_priv(dev_to_iio_dev(dev
));
503 return bma180_show_avail(buf
, data
->part_info
->bw_table
,
504 data
->part_info
->num_bw
, false);
507 static ssize_t
bma180_show_scale_avail(struct device
*dev
,
508 struct device_attribute
*attr
, char *buf
)
510 struct bma180_data
*data
= iio_priv(dev_to_iio_dev(dev
));
512 return bma180_show_avail(buf
, data
->part_info
->scale_table
,
513 data
->part_info
->num_scales
, true);
516 static IIO_DEVICE_ATTR(in_accel_filter_low_pass_3db_frequency_available
,
517 S_IRUGO
, bma180_show_filter_freq_avail
, NULL
, 0);
519 static IIO_DEVICE_ATTR(in_accel_scale_available
,
520 S_IRUGO
, bma180_show_scale_avail
, NULL
, 0);
522 static struct attribute
*bma180_attributes
[] = {
523 &iio_dev_attr_in_accel_filter_low_pass_3db_frequency_available
.
525 &iio_dev_attr_in_accel_scale_available
.dev_attr
.attr
,
529 static const struct attribute_group bma180_attrs_group
= {
530 .attrs
= bma180_attributes
,
533 static int bma180_read_raw(struct iio_dev
*indio_dev
,
534 struct iio_chan_spec
const *chan
, int *val
, int *val2
,
537 struct bma180_data
*data
= iio_priv(indio_dev
);
541 case IIO_CHAN_INFO_RAW
:
542 ret
= iio_device_claim_direct_mode(indio_dev
);
546 mutex_lock(&data
->mutex
);
547 ret
= bma180_get_data_reg(data
, chan
->scan_index
);
548 mutex_unlock(&data
->mutex
);
549 iio_device_release_direct_mode(indio_dev
);
552 if (chan
->scan_type
.sign
== 's') {
553 *val
= sign_extend32(ret
>> chan
->scan_type
.shift
,
554 chan
->scan_type
.realbits
- 1);
559 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
:
562 case IIO_CHAN_INFO_SCALE
:
563 switch (chan
->type
) {
567 return IIO_VAL_INT_PLUS_MICRO
;
574 case IIO_CHAN_INFO_OFFSET
:
575 *val
= data
->part_info
->temp_offset
;
582 static int bma180_write_raw(struct iio_dev
*indio_dev
,
583 struct iio_chan_spec
const *chan
, int val
, int val2
, long mask
)
585 struct bma180_data
*data
= iio_priv(indio_dev
);
589 case IIO_CHAN_INFO_SCALE
:
592 mutex_lock(&data
->mutex
);
593 ret
= bma180_set_scale(data
, val2
);
594 mutex_unlock(&data
->mutex
);
596 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
:
599 mutex_lock(&data
->mutex
);
600 ret
= bma180_set_bw(data
, val
);
601 mutex_unlock(&data
->mutex
);
608 static const struct iio_info bma180_info
= {
609 .attrs
= &bma180_attrs_group
,
610 .read_raw
= bma180_read_raw
,
611 .write_raw
= bma180_write_raw
,
614 static const char * const bma180_power_modes
[] = { "low_noise", "low_power" };
616 static int bma180_get_power_mode(struct iio_dev
*indio_dev
,
617 const struct iio_chan_spec
*chan
)
619 struct bma180_data
*data
= iio_priv(indio_dev
);
624 static int bma180_set_power_mode(struct iio_dev
*indio_dev
,
625 const struct iio_chan_spec
*chan
, unsigned int mode
)
627 struct bma180_data
*data
= iio_priv(indio_dev
);
630 mutex_lock(&data
->mutex
);
631 ret
= bma180_set_pmode(data
, mode
);
632 mutex_unlock(&data
->mutex
);
637 static const struct iio_mount_matrix
*
638 bma180_accel_get_mount_matrix(const struct iio_dev
*indio_dev
,
639 const struct iio_chan_spec
*chan
)
641 struct bma180_data
*data
= iio_priv(indio_dev
);
643 return &data
->orientation
;
646 static const struct iio_enum bma180_power_mode_enum
= {
647 .items
= bma180_power_modes
,
648 .num_items
= ARRAY_SIZE(bma180_power_modes
),
649 .get
= bma180_get_power_mode
,
650 .set
= bma180_set_power_mode
,
653 static const struct iio_chan_spec_ext_info bma023_ext_info
[] = {
654 IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR
, bma180_accel_get_mount_matrix
),
658 static const struct iio_chan_spec_ext_info bma180_ext_info
[] = {
659 IIO_ENUM("power_mode", IIO_SHARED_BY_TYPE
, &bma180_power_mode_enum
),
660 IIO_ENUM_AVAILABLE("power_mode", IIO_SHARED_BY_TYPE
, &bma180_power_mode_enum
),
661 IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR
, bma180_accel_get_mount_matrix
),
665 #define BMA023_ACC_CHANNEL(_axis, _bits) { \
668 .channel2 = IIO_MOD_##_axis, \
669 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
670 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
671 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
672 .scan_index = AXIS_##_axis, \
677 .shift = 16 - _bits, \
679 .ext_info = bma023_ext_info, \
682 #define BMA150_TEMP_CHANNEL { \
684 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
685 BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET), \
686 .scan_index = TEMP, \
694 #define BMA180_ACC_CHANNEL(_axis, _bits) { \
697 .channel2 = IIO_MOD_##_axis, \
698 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
699 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
700 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
701 .scan_index = AXIS_##_axis, \
706 .shift = 16 - _bits, \
708 .ext_info = bma180_ext_info, \
711 #define BMA180_TEMP_CHANNEL { \
713 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
714 BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET), \
715 .scan_index = TEMP, \
723 static const struct iio_chan_spec bma023_channels
[] = {
724 BMA023_ACC_CHANNEL(X
, 10),
725 BMA023_ACC_CHANNEL(Y
, 10),
726 BMA023_ACC_CHANNEL(Z
, 10),
727 IIO_CHAN_SOFT_TIMESTAMP(4),
730 static const struct iio_chan_spec bma150_channels
[] = {
731 BMA023_ACC_CHANNEL(X
, 10),
732 BMA023_ACC_CHANNEL(Y
, 10),
733 BMA023_ACC_CHANNEL(Z
, 10),
735 IIO_CHAN_SOFT_TIMESTAMP(4),
738 static const struct iio_chan_spec bma180_channels
[] = {
739 BMA180_ACC_CHANNEL(X
, 14),
740 BMA180_ACC_CHANNEL(Y
, 14),
741 BMA180_ACC_CHANNEL(Z
, 14),
743 IIO_CHAN_SOFT_TIMESTAMP(4),
746 static const struct iio_chan_spec bma250_channels
[] = {
747 BMA180_ACC_CHANNEL(X
, 10),
748 BMA180_ACC_CHANNEL(Y
, 10),
749 BMA180_ACC_CHANNEL(Z
, 10),
751 IIO_CHAN_SOFT_TIMESTAMP(4),
754 static const struct bma180_part_info bma180_part_info
[] = {
756 .chip_id
= BMA023_ID_REG_VAL
,
757 .channels
= bma023_channels
,
758 .num_channels
= ARRAY_SIZE(bma023_channels
),
759 .scale_table
= bma023_scale_table
,
760 .num_scales
= ARRAY_SIZE(bma023_scale_table
),
761 .bw_table
= bma023_bw_table
,
762 .num_bw
= ARRAY_SIZE(bma023_bw_table
),
763 /* No temperature channel */
765 .int_reset_reg
= BMA023_CTRL_REG0
,
766 .int_reset_mask
= BMA023_INT_RESET_MASK
,
767 .sleep_reg
= BMA023_CTRL_REG0
,
768 .sleep_mask
= BMA023_SLEEP
,
769 .bw_reg
= BMA023_CTRL_REG2
,
770 .bw_mask
= BMA023_BW_MASK
,
771 .scale_reg
= BMA023_CTRL_REG2
,
772 .scale_mask
= BMA023_RANGE_MASK
,
773 /* No power mode on bma023 */
777 .int_enable_reg
= BMA023_CTRL_REG3
,
778 .int_enable_mask
= BMA023_NEW_DATA_INT
,
779 .softreset_reg
= BMA023_CTRL_REG0
,
780 .softreset_val
= BMA023_RESET_VAL
,
781 .chip_config
= bma023_chip_config
,
782 .chip_disable
= bma023_chip_disable
,
785 .chip_id
= BMA023_ID_REG_VAL
,
786 .channels
= bma150_channels
,
787 .num_channels
= ARRAY_SIZE(bma150_channels
),
788 .scale_table
= bma023_scale_table
,
789 .num_scales
= ARRAY_SIZE(bma023_scale_table
),
790 .bw_table
= bma023_bw_table
,
791 .num_bw
= ARRAY_SIZE(bma023_bw_table
),
792 .temp_offset
= -60, /* 0 LSB @ -30 degree C */
793 .int_reset_reg
= BMA023_CTRL_REG0
,
794 .int_reset_mask
= BMA023_INT_RESET_MASK
,
795 .sleep_reg
= BMA023_CTRL_REG0
,
796 .sleep_mask
= BMA023_SLEEP
,
797 .bw_reg
= BMA023_CTRL_REG2
,
798 .bw_mask
= BMA023_BW_MASK
,
799 .scale_reg
= BMA023_CTRL_REG2
,
800 .scale_mask
= BMA023_RANGE_MASK
,
801 /* No power mode on bma150 */
805 .int_enable_reg
= BMA023_CTRL_REG3
,
806 .int_enable_mask
= BMA023_NEW_DATA_INT
,
807 .softreset_reg
= BMA023_CTRL_REG0
,
808 .softreset_val
= BMA023_RESET_VAL
,
809 .chip_config
= bma023_chip_config
,
810 .chip_disable
= bma023_chip_disable
,
813 .chip_id
= BMA180_ID_REG_VAL
,
814 .channels
= bma180_channels
,
815 .num_channels
= ARRAY_SIZE(bma180_channels
),
816 .scale_table
= bma180_scale_table
,
817 .num_scales
= ARRAY_SIZE(bma180_scale_table
),
818 .bw_table
= bma180_bw_table
,
819 .num_bw
= ARRAY_SIZE(bma180_bw_table
),
820 .temp_offset
= 48, /* 0 LSB @ 24 degree C */
821 .int_reset_reg
= BMA180_CTRL_REG0
,
822 .int_reset_mask
= BMA180_RESET_INT
,
823 .sleep_reg
= BMA180_CTRL_REG0
,
824 .sleep_mask
= BMA180_SLEEP
,
825 .bw_reg
= BMA180_BW_TCS
,
826 .bw_mask
= BMA180_BW
,
827 .scale_reg
= BMA180_OFFSET_LSB1
,
828 .scale_mask
= BMA180_RANGE
,
829 .power_reg
= BMA180_TCO_Z
,
830 .power_mask
= BMA180_MODE_CONFIG
,
831 .lowpower_val
= BMA180_LOW_POWER
,
832 .int_enable_reg
= BMA180_CTRL_REG3
,
833 .int_enable_mask
= BMA180_NEW_DATA_INT
,
834 .softreset_reg
= BMA180_RESET
,
835 .softreset_val
= BMA180_RESET_VAL
,
836 .chip_config
= bma180_chip_config
,
837 .chip_disable
= bma180_chip_disable
,
840 .chip_id
= BMA250_ID_REG_VAL
,
841 .channels
= bma250_channels
,
842 .num_channels
= ARRAY_SIZE(bma250_channels
),
843 .scale_table
= bma250_scale_table
,
844 .num_scales
= ARRAY_SIZE(bma250_scale_table
),
845 .bw_table
= bma250_bw_table
,
846 .num_bw
= ARRAY_SIZE(bma250_bw_table
),
847 .temp_offset
= 48, /* 0 LSB @ 24 degree C */
848 .int_reset_reg
= BMA250_INT_RESET_REG
,
849 .int_reset_mask
= BMA250_INT_RESET_MASK
,
850 .sleep_reg
= BMA250_POWER_REG
,
851 .sleep_mask
= BMA250_SUSPEND_MASK
,
852 .bw_reg
= BMA250_BW_REG
,
853 .bw_mask
= BMA250_BW_MASK
,
854 .bw_offset
= BMA250_BW_OFFSET
,
855 .scale_reg
= BMA250_RANGE_REG
,
856 .scale_mask
= BMA250_RANGE_MASK
,
857 .power_reg
= BMA250_POWER_REG
,
858 .power_mask
= BMA250_LOWPOWER_MASK
,
860 .int_enable_reg
= BMA250_INT_ENABLE_REG
,
861 .int_enable_mask
= BMA250_DATA_INTEN_MASK
,
862 .softreset_reg
= BMA250_RESET_REG
,
863 .softreset_val
= BMA180_RESET_VAL
,
864 .chip_config
= bma250_chip_config
,
865 .chip_disable
= bma250_chip_disable
,
869 static irqreturn_t
bma180_trigger_handler(int irq
, void *p
)
871 struct iio_poll_func
*pf
= p
;
872 struct iio_dev
*indio_dev
= pf
->indio_dev
;
873 struct bma180_data
*data
= iio_priv(indio_dev
);
874 s64 time_ns
= iio_get_time_ns(indio_dev
);
877 mutex_lock(&data
->mutex
);
879 iio_for_each_active_channel(indio_dev
, bit
) {
880 ret
= bma180_get_data_reg(data
, bit
);
882 mutex_unlock(&data
->mutex
);
885 data
->scan
.chan
[i
++] = ret
;
888 mutex_unlock(&data
->mutex
);
890 iio_push_to_buffers_with_timestamp(indio_dev
, &data
->scan
, time_ns
);
892 iio_trigger_notify_done(indio_dev
->trig
);
897 static int bma180_data_rdy_trigger_set_state(struct iio_trigger
*trig
,
900 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
901 struct bma180_data
*data
= iio_priv(indio_dev
);
903 return bma180_set_new_data_intr_state(data
, state
);
906 static void bma180_trig_reen(struct iio_trigger
*trig
)
908 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
909 struct bma180_data
*data
= iio_priv(indio_dev
);
912 ret
= bma180_reset_intr(data
);
914 dev_err(&data
->client
->dev
, "failed to reset interrupt\n");
917 static const struct iio_trigger_ops bma180_trigger_ops
= {
918 .set_trigger_state
= bma180_data_rdy_trigger_set_state
,
919 .reenable
= bma180_trig_reen
,
922 static int bma180_probe(struct i2c_client
*client
)
924 const struct i2c_device_id
*id
= i2c_client_get_device_id(client
);
925 struct device
*dev
= &client
->dev
;
926 struct bma180_data
*data
;
927 struct iio_dev
*indio_dev
;
930 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*data
));
934 data
= iio_priv(indio_dev
);
935 i2c_set_clientdata(client
, indio_dev
);
936 data
->client
= client
;
937 data
->part_info
= i2c_get_match_data(client
);
939 ret
= iio_read_mount_matrix(dev
, &data
->orientation
);
943 data
->vdd_supply
= devm_regulator_get(dev
, "vdd");
944 if (IS_ERR(data
->vdd_supply
))
945 return dev_err_probe(dev
, PTR_ERR(data
->vdd_supply
),
946 "Failed to get vdd regulator\n");
948 data
->vddio_supply
= devm_regulator_get(dev
, "vddio");
949 if (IS_ERR(data
->vddio_supply
))
950 return dev_err_probe(dev
, PTR_ERR(data
->vddio_supply
),
951 "Failed to get vddio regulator\n");
953 /* Typical voltage 2.4V these are min and max */
954 ret
= regulator_set_voltage(data
->vdd_supply
, 1620000, 3600000);
957 ret
= regulator_set_voltage(data
->vddio_supply
, 1200000, 3600000);
960 ret
= regulator_enable(data
->vdd_supply
);
962 dev_err(dev
, "Failed to enable vdd regulator: %d\n", ret
);
965 ret
= regulator_enable(data
->vddio_supply
);
967 dev_err(dev
, "Failed to enable vddio regulator: %d\n", ret
);
968 goto err_disable_vdd
;
970 /* Wait to make sure we started up properly (3 ms at least) */
971 usleep_range(3000, 5000);
973 ret
= data
->part_info
->chip_config(data
);
975 goto err_chip_disable
;
977 mutex_init(&data
->mutex
);
978 indio_dev
->channels
= data
->part_info
->channels
;
979 indio_dev
->num_channels
= data
->part_info
->num_channels
;
980 indio_dev
->name
= id
->name
;
981 indio_dev
->modes
= INDIO_DIRECT_MODE
;
982 indio_dev
->info
= &bma180_info
;
984 if (client
->irq
> 0) {
985 data
->trig
= iio_trigger_alloc(dev
, "%s-dev%d", indio_dev
->name
,
986 iio_device_id(indio_dev
));
989 goto err_chip_disable
;
992 ret
= devm_request_irq(dev
, client
->irq
,
993 iio_trigger_generic_data_rdy_poll
, IRQF_TRIGGER_RISING
,
994 "bma180_event", data
->trig
);
996 dev_err(dev
, "unable to request IRQ\n");
997 goto err_trigger_free
;
1000 data
->trig
->ops
= &bma180_trigger_ops
;
1001 iio_trigger_set_drvdata(data
->trig
, indio_dev
);
1003 ret
= iio_trigger_register(data
->trig
);
1005 goto err_trigger_free
;
1007 indio_dev
->trig
= iio_trigger_get(data
->trig
);
1010 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
1011 bma180_trigger_handler
, NULL
);
1013 dev_err(dev
, "unable to setup iio triggered buffer\n");
1014 goto err_trigger_unregister
;
1017 ret
= iio_device_register(indio_dev
);
1019 dev_err(dev
, "unable to register iio device\n");
1020 goto err_buffer_cleanup
;
1026 iio_triggered_buffer_cleanup(indio_dev
);
1027 err_trigger_unregister
:
1029 iio_trigger_unregister(data
->trig
);
1031 iio_trigger_free(data
->trig
);
1033 data
->part_info
->chip_disable(data
);
1034 regulator_disable(data
->vddio_supply
);
1036 regulator_disable(data
->vdd_supply
);
1041 static void bma180_remove(struct i2c_client
*client
)
1043 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
1044 struct bma180_data
*data
= iio_priv(indio_dev
);
1046 iio_device_unregister(indio_dev
);
1047 iio_triggered_buffer_cleanup(indio_dev
);
1049 iio_trigger_unregister(data
->trig
);
1050 iio_trigger_free(data
->trig
);
1053 mutex_lock(&data
->mutex
);
1054 data
->part_info
->chip_disable(data
);
1055 mutex_unlock(&data
->mutex
);
1056 regulator_disable(data
->vddio_supply
);
1057 regulator_disable(data
->vdd_supply
);
1060 static int bma180_suspend(struct device
*dev
)
1062 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
1063 struct bma180_data
*data
= iio_priv(indio_dev
);
1066 mutex_lock(&data
->mutex
);
1067 ret
= bma180_set_sleep_state(data
, true);
1068 mutex_unlock(&data
->mutex
);
1073 static int bma180_resume(struct device
*dev
)
1075 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
1076 struct bma180_data
*data
= iio_priv(indio_dev
);
1079 mutex_lock(&data
->mutex
);
1080 ret
= bma180_set_sleep_state(data
, false);
1081 mutex_unlock(&data
->mutex
);
1086 static DEFINE_SIMPLE_DEV_PM_OPS(bma180_pm_ops
, bma180_suspend
, bma180_resume
);
1088 static const struct i2c_device_id bma180_ids
[] = {
1089 { "bma023", (kernel_ulong_t
)&bma180_part_info
[BMA023
] },
1090 { "bma150", (kernel_ulong_t
)&bma180_part_info
[BMA150
] },
1091 { "bma180", (kernel_ulong_t
)&bma180_part_info
[BMA180
] },
1092 { "bma250", (kernel_ulong_t
)&bma180_part_info
[BMA250
] },
1093 { "smb380", (kernel_ulong_t
)&bma180_part_info
[BMA150
] },
1097 MODULE_DEVICE_TABLE(i2c
, bma180_ids
);
1099 static const struct of_device_id bma180_of_match
[] = {
1101 .compatible
= "bosch,bma023",
1102 .data
= &bma180_part_info
[BMA023
]
1105 .compatible
= "bosch,bma150",
1106 .data
= &bma180_part_info
[BMA150
]
1109 .compatible
= "bosch,bma180",
1110 .data
= &bma180_part_info
[BMA180
]
1113 .compatible
= "bosch,bma250",
1114 .data
= &bma180_part_info
[BMA250
]
1117 .compatible
= "bosch,smb380",
1118 .data
= &bma180_part_info
[BMA150
]
1122 MODULE_DEVICE_TABLE(of
, bma180_of_match
);
1124 static struct i2c_driver bma180_driver
= {
1127 .pm
= pm_sleep_ptr(&bma180_pm_ops
),
1128 .of_match_table
= bma180_of_match
,
1130 .probe
= bma180_probe
,
1131 .remove
= bma180_remove
,
1132 .id_table
= bma180_ids
,
1135 module_i2c_driver(bma180_driver
);
1137 MODULE_AUTHOR("Kravchenko Oleksandr <x0199363@ti.com>");
1138 MODULE_AUTHOR("Texas Instruments, Inc.");
1139 MODULE_DESCRIPTION("Bosch BMA023/BMA1x0/BMA250 triaxial acceleration sensor");
1140 MODULE_LICENSE("GPL");