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
13 * BMA254: 7-bit I2C slave address 0x18 or 0x19
16 #include <linux/module.h>
17 #include <linux/i2c.h>
18 #include <linux/interrupt.h>
19 #include <linux/delay.h>
20 #include <linux/of_device.h>
22 #include <linux/bitops.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/slab.h>
25 #include <linux/string.h>
26 #include <linux/iio/iio.h>
27 #include <linux/iio/sysfs.h>
28 #include <linux/iio/buffer.h>
29 #include <linux/iio/trigger.h>
30 #include <linux/iio/trigger_consumer.h>
31 #include <linux/iio/triggered_buffer.h>
33 #define BMA180_DRV_NAME "bma180"
34 #define BMA180_IRQ_NAME "bma180_event"
46 struct bma180_part_info {
48 const struct iio_chan_spec *channels;
49 unsigned int num_channels;
50 const int *scale_table;
51 unsigned int num_scales;
56 u8 int_reset_reg, int_reset_mask;
57 u8 sleep_reg, sleep_mask;
59 u8 scale_reg, scale_mask;
60 u8 power_reg, power_mask, lowpower_val;
61 u8 int_enable_reg, int_enable_mask;
62 u8 int_map_reg, int_enable_dataready_int1_mask;
63 u8 softreset_reg, softreset_val;
65 int (*chip_config)(struct bma180_data *data);
66 void (*chip_disable)(struct bma180_data *data);
70 #define BMA023_CTRL_REG0 0x0a
71 #define BMA023_CTRL_REG1 0x0b
72 #define BMA023_CTRL_REG2 0x14
73 #define BMA023_CTRL_REG3 0x15
75 #define BMA023_RANGE_MASK GENMASK(4, 3) /* Range of accel values */
76 #define BMA023_BW_MASK GENMASK(2, 0) /* Accel bandwidth */
77 #define BMA023_SLEEP BIT(0)
78 #define BMA023_INT_RESET_MASK BIT(6)
79 #define BMA023_NEW_DATA_INT BIT(5) /* Intr every new accel data is ready */
80 #define BMA023_RESET_VAL BIT(1)
82 #define BMA180_CHIP_ID 0x00 /* Need to distinguish BMA180 from other */
83 #define BMA180_ACC_X_LSB 0x02 /* First of 6 registers of accel data */
84 #define BMA180_TEMP 0x08
85 #define BMA180_CTRL_REG0 0x0d
86 #define BMA180_RESET 0x10
87 #define BMA180_BW_TCS 0x20
88 #define BMA180_CTRL_REG3 0x21
89 #define BMA180_TCO_Z 0x30
90 #define BMA180_OFFSET_LSB1 0x35
92 /* BMA180_CTRL_REG0 bits */
93 #define BMA180_DIS_WAKE_UP BIT(0) /* Disable wake up mode */
94 #define BMA180_SLEEP BIT(1) /* 1 - chip will sleep */
95 #define BMA180_EE_W BIT(4) /* Unlock writing to addr from 0x20 */
96 #define BMA180_RESET_INT BIT(6) /* Reset pending interrupts */
98 /* BMA180_CTRL_REG3 bits */
99 #define BMA180_NEW_DATA_INT BIT(1) /* Intr every new accel data is ready */
101 /* BMA180_OFFSET_LSB1 skipping mode bit */
102 #define BMA180_SMP_SKIP BIT(0)
104 /* Bit masks for registers bit fields */
105 #define BMA180_RANGE 0x0e /* Range of measured accel values */
106 #define BMA180_BW 0xf0 /* Accel bandwidth */
107 #define BMA180_MODE_CONFIG 0x03 /* Config operation modes */
109 /* We have to write this value in reset register to do soft reset */
110 #define BMA180_RESET_VAL 0xb6
112 #define BMA023_ID_REG_VAL 0x02
113 #define BMA180_ID_REG_VAL 0x03
114 #define BMA250_ID_REG_VAL 0x03
115 #define BMA254_ID_REG_VAL 0xfa /* 250 decimal */
117 /* Chip power modes */
118 #define BMA180_LOW_POWER 0x03
120 #define BMA250_RANGE_REG 0x0f
121 #define BMA250_BW_REG 0x10
122 #define BMA250_POWER_REG 0x11
123 #define BMA250_RESET_REG 0x14
124 #define BMA250_INT_ENABLE_REG 0x17
125 #define BMA250_INT_MAP_REG 0x1a
126 #define BMA250_INT_RESET_REG 0x21
128 #define BMA250_RANGE_MASK GENMASK(3, 0) /* Range of accel values */
129 #define BMA250_BW_MASK GENMASK(4, 0) /* Accel bandwidth */
130 #define BMA250_SUSPEND_MASK BIT(7) /* chip will sleep */
131 #define BMA250_LOWPOWER_MASK BIT(6)
132 #define BMA250_DATA_INTEN_MASK BIT(4)
133 #define BMA250_INT1_DATA_MASK BIT(0)
134 #define BMA250_INT_RESET_MASK BIT(7) /* Reset pending interrupts */
136 #define BMA254_RANGE_REG 0x0f
137 #define BMA254_BW_REG 0x10
138 #define BMA254_POWER_REG 0x11
139 #define BMA254_RESET_REG 0x14
140 #define BMA254_INT_ENABLE_REG 0x17
141 #define BMA254_INT_MAP_REG 0x1a
142 #define BMA254_INT_RESET_REG 0x21
144 #define BMA254_RANGE_MASK GENMASK(3, 0) /* Range of accel values */
145 #define BMA254_BW_MASK GENMASK(4, 0) /* Accel bandwidth */
146 #define BMA254_SUSPEND_MASK BIT(7) /* chip will sleep */
147 #define BMA254_LOWPOWER_MASK BIT(6)
148 #define BMA254_DATA_INTEN_MASK BIT(4)
149 #define BMA254_INT2_DATA_MASK BIT(7)
150 #define BMA254_INT1_DATA_MASK BIT(0)
151 #define BMA254_INT_RESET_MASK BIT(7) /* Reset pending interrupts */
154 struct regulator *vdd_supply;
155 struct regulator *vddio_supply;
156 struct i2c_client *client;
157 struct iio_trigger *trig;
158 const struct bma180_part_info *part_info;
159 struct iio_mount_matrix orientation;
165 u8 buff[16]; /* 3x 16-bit + 8-bit + padding + timestamp */
175 static int bma023_bw_table[] = { 25, 50, 100, 190, 375, 750, 1500 }; /* Hz */
176 static int bma023_scale_table[] = { 2452, 4903, 9709, };
178 static int bma180_bw_table[] = { 10, 20, 40, 75, 150, 300 }; /* Hz */
179 static int bma180_scale_table[] = { 1275, 1863, 2452, 3727, 4903, 9709, 19417 };
181 static int bma25x_bw_table[] = { 8, 16, 31, 63, 125, 250 }; /* Hz */
182 static int bma25x_scale_table[] = { 0, 0, 0, 38344, 0, 76590, 0, 0, 153180, 0,
185 static int bma180_get_data_reg(struct bma180_data *data, enum bma180_chan chan)
189 if (data->sleep_state)
194 ret = i2c_smbus_read_byte_data(data->client, BMA180_TEMP);
196 dev_err(&data->client->dev, "failed to read temp register\n");
199 ret = i2c_smbus_read_word_data(data->client,
200 BMA180_ACC_X_LSB + chan * 2);
202 dev_err(&data->client->dev,
203 "failed to read accel_%c register\n",
210 static int bma180_set_bits(struct bma180_data *data, u8 reg, u8 mask, u8 val)
212 int ret = i2c_smbus_read_byte_data(data->client, reg);
213 u8 reg_val = (ret & ~mask) | (val << (ffs(mask) - 1));
218 return i2c_smbus_write_byte_data(data->client, reg, reg_val);
221 static int bma180_reset_intr(struct bma180_data *data)
223 int ret = bma180_set_bits(data, data->part_info->int_reset_reg,
224 data->part_info->int_reset_mask, 1);
227 dev_err(&data->client->dev, "failed to reset interrupt\n");
232 static int bma180_set_new_data_intr_state(struct bma180_data *data, bool state)
234 int ret = bma180_set_bits(data, data->part_info->int_enable_reg,
235 data->part_info->int_enable_mask, state);
238 ret = bma180_reset_intr(data);
245 dev_err(&data->client->dev,
246 "failed to set new data interrupt state %d\n", state);
250 static int bma180_set_sleep_state(struct bma180_data *data, bool state)
252 int ret = bma180_set_bits(data, data->part_info->sleep_reg,
253 data->part_info->sleep_mask, state);
256 dev_err(&data->client->dev,
257 "failed to set sleep state %d\n", state);
260 data->sleep_state = state;
265 static int bma180_set_ee_writing_state(struct bma180_data *data, bool state)
267 int ret = bma180_set_bits(data, BMA180_CTRL_REG0, BMA180_EE_W, state);
270 dev_err(&data->client->dev,
271 "failed to set ee writing state %d\n", state);
276 static int bma180_set_bw(struct bma180_data *data, int val)
280 if (data->sleep_state)
283 for (i = 0; i < data->part_info->num_bw; ++i) {
284 if (data->part_info->bw_table[i] == val) {
285 ret = bma180_set_bits(data, data->part_info->bw_reg,
286 data->part_info->bw_mask, i);
288 dev_err(&data->client->dev,
289 "failed to set bandwidth\n");
300 static int bma180_set_scale(struct bma180_data *data, int val)
304 if (data->sleep_state)
307 for (i = 0; i < data->part_info->num_scales; ++i)
308 if (data->part_info->scale_table[i] == val) {
309 ret = bma180_set_bits(data, data->part_info->scale_reg,
310 data->part_info->scale_mask, i);
312 dev_err(&data->client->dev,
313 "failed to set scale\n");
323 static int bma180_set_pmode(struct bma180_data *data, bool mode)
325 u8 reg_val = mode ? data->part_info->lowpower_val : 0;
326 int ret = bma180_set_bits(data, data->part_info->power_reg,
327 data->part_info->power_mask, reg_val);
330 dev_err(&data->client->dev, "failed to set power mode\n");
338 static int bma180_soft_reset(struct bma180_data *data)
340 int ret = i2c_smbus_write_byte_data(data->client,
341 data->part_info->softreset_reg,
342 data->part_info->softreset_val);
345 dev_err(&data->client->dev, "failed to reset the chip\n");
350 static int bma180_chip_init(struct bma180_data *data)
352 /* Try to read chip_id register. It must return 0x03. */
353 int ret = i2c_smbus_read_byte_data(data->client, BMA180_CHIP_ID);
357 if (ret != data->part_info->chip_id) {
358 dev_err(&data->client->dev, "wrong chip ID %d expected %d\n",
359 ret, data->part_info->chip_id);
363 ret = bma180_soft_reset(data);
367 * No serial transaction should occur within minimum 10 us
368 * after soft_reset command
372 return bma180_set_new_data_intr_state(data, false);
375 static int bma023_chip_config(struct bma180_data *data)
377 int ret = bma180_chip_init(data);
382 ret = bma180_set_bw(data, 50); /* 50 Hz */
385 ret = bma180_set_scale(data, 2452); /* 2 G */
392 dev_err(&data->client->dev, "failed to config the chip\n");
396 static int bma180_chip_config(struct bma180_data *data)
398 int ret = bma180_chip_init(data);
402 ret = bma180_set_pmode(data, false);
405 ret = bma180_set_bits(data, BMA180_CTRL_REG0, BMA180_DIS_WAKE_UP, 1);
408 ret = bma180_set_ee_writing_state(data, true);
411 ret = bma180_set_bits(data, BMA180_OFFSET_LSB1, BMA180_SMP_SKIP, 1);
414 ret = bma180_set_bw(data, 20); /* 20 Hz */
417 ret = bma180_set_scale(data, 2452); /* 2 G */
424 dev_err(&data->client->dev, "failed to config the chip\n");
428 static int bma25x_chip_config(struct bma180_data *data)
430 int ret = bma180_chip_init(data);
434 ret = bma180_set_pmode(data, false);
437 ret = bma180_set_bw(data, 16); /* 16 Hz */
440 ret = bma180_set_scale(data, 38344); /* 2 G */
444 * This enables dataready interrupt on the INT1 pin
445 * FIXME: support using the INT2 pin
447 ret = bma180_set_bits(data, data->part_info->int_map_reg,
448 data->part_info->int_enable_dataready_int1_mask, 1);
455 dev_err(&data->client->dev, "failed to config the chip\n");
459 static void bma023_chip_disable(struct bma180_data *data)
461 if (bma180_set_sleep_state(data, true))
467 dev_err(&data->client->dev, "failed to disable the chip\n");
470 static void bma180_chip_disable(struct bma180_data *data)
472 if (bma180_set_new_data_intr_state(data, false))
474 if (bma180_set_ee_writing_state(data, false))
476 if (bma180_set_sleep_state(data, true))
482 dev_err(&data->client->dev, "failed to disable the chip\n");
485 static void bma25x_chip_disable(struct bma180_data *data)
487 if (bma180_set_new_data_intr_state(data, false))
489 if (bma180_set_sleep_state(data, true))
495 dev_err(&data->client->dev, "failed to disable the chip\n");
498 static ssize_t bma180_show_avail(char *buf, const int *vals, unsigned int n,
504 for (i = 0; i < n; i++) {
507 len += scnprintf(buf + len, PAGE_SIZE - len,
508 micros ? "0.%06d " : "%d ", vals[i]);
515 static ssize_t bma180_show_filter_freq_avail(struct device *dev,
516 struct device_attribute *attr, char *buf)
518 struct bma180_data *data = iio_priv(dev_to_iio_dev(dev));
520 return bma180_show_avail(buf, data->part_info->bw_table,
521 data->part_info->num_bw, false);
524 static ssize_t bma180_show_scale_avail(struct device *dev,
525 struct device_attribute *attr, char *buf)
527 struct bma180_data *data = iio_priv(dev_to_iio_dev(dev));
529 return bma180_show_avail(buf, data->part_info->scale_table,
530 data->part_info->num_scales, true);
533 static IIO_DEVICE_ATTR(in_accel_filter_low_pass_3db_frequency_available,
534 S_IRUGO, bma180_show_filter_freq_avail, NULL, 0);
536 static IIO_DEVICE_ATTR(in_accel_scale_available,
537 S_IRUGO, bma180_show_scale_avail, NULL, 0);
539 static struct attribute *bma180_attributes[] = {
540 &iio_dev_attr_in_accel_filter_low_pass_3db_frequency_available.
542 &iio_dev_attr_in_accel_scale_available.dev_attr.attr,
546 static const struct attribute_group bma180_attrs_group = {
547 .attrs = bma180_attributes,
550 static int bma180_read_raw(struct iio_dev *indio_dev,
551 struct iio_chan_spec const *chan, int *val, int *val2,
554 struct bma180_data *data = iio_priv(indio_dev);
558 case IIO_CHAN_INFO_RAW:
559 ret = iio_device_claim_direct_mode(indio_dev);
563 mutex_lock(&data->mutex);
564 ret = bma180_get_data_reg(data, chan->scan_index);
565 mutex_unlock(&data->mutex);
566 iio_device_release_direct_mode(indio_dev);
569 if (chan->scan_type.sign == 's') {
570 *val = sign_extend32(ret >> chan->scan_type.shift,
571 chan->scan_type.realbits - 1);
576 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
579 case IIO_CHAN_INFO_SCALE:
580 switch (chan->type) {
584 return IIO_VAL_INT_PLUS_MICRO;
591 case IIO_CHAN_INFO_OFFSET:
592 *val = data->part_info->temp_offset;
599 static int bma180_write_raw(struct iio_dev *indio_dev,
600 struct iio_chan_spec const *chan, int val, int val2, long mask)
602 struct bma180_data *data = iio_priv(indio_dev);
606 case IIO_CHAN_INFO_SCALE:
609 mutex_lock(&data->mutex);
610 ret = bma180_set_scale(data, val2);
611 mutex_unlock(&data->mutex);
613 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
616 mutex_lock(&data->mutex);
617 ret = bma180_set_bw(data, val);
618 mutex_unlock(&data->mutex);
625 static const struct iio_info bma180_info = {
626 .attrs = &bma180_attrs_group,
627 .read_raw = bma180_read_raw,
628 .write_raw = bma180_write_raw,
631 static const char * const bma180_power_modes[] = { "low_noise", "low_power" };
633 static int bma180_get_power_mode(struct iio_dev *indio_dev,
634 const struct iio_chan_spec *chan)
636 struct bma180_data *data = iio_priv(indio_dev);
641 static int bma180_set_power_mode(struct iio_dev *indio_dev,
642 const struct iio_chan_spec *chan, unsigned int mode)
644 struct bma180_data *data = iio_priv(indio_dev);
647 mutex_lock(&data->mutex);
648 ret = bma180_set_pmode(data, mode);
649 mutex_unlock(&data->mutex);
654 static const struct iio_mount_matrix *
655 bma180_accel_get_mount_matrix(const struct iio_dev *indio_dev,
656 const struct iio_chan_spec *chan)
658 struct bma180_data *data = iio_priv(indio_dev);
660 return &data->orientation;
663 static const struct iio_enum bma180_power_mode_enum = {
664 .items = bma180_power_modes,
665 .num_items = ARRAY_SIZE(bma180_power_modes),
666 .get = bma180_get_power_mode,
667 .set = bma180_set_power_mode,
670 static const struct iio_chan_spec_ext_info bma023_ext_info[] = {
671 IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, bma180_accel_get_mount_matrix),
675 static const struct iio_chan_spec_ext_info bma180_ext_info[] = {
676 IIO_ENUM("power_mode", IIO_SHARED_BY_TYPE, &bma180_power_mode_enum),
677 IIO_ENUM_AVAILABLE("power_mode", &bma180_power_mode_enum),
678 IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, bma180_accel_get_mount_matrix),
682 #define BMA023_ACC_CHANNEL(_axis, _bits) { \
685 .channel2 = IIO_MOD_##_axis, \
686 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
687 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
688 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
689 .scan_index = AXIS_##_axis, \
694 .shift = 16 - _bits, \
696 .ext_info = bma023_ext_info, \
699 #define BMA150_TEMP_CHANNEL { \
701 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
702 BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET), \
703 .scan_index = TEMP, \
711 #define BMA180_ACC_CHANNEL(_axis, _bits) { \
714 .channel2 = IIO_MOD_##_axis, \
715 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
716 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
717 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
718 .scan_index = AXIS_##_axis, \
723 .shift = 16 - _bits, \
725 .ext_info = bma180_ext_info, \
728 #define BMA180_TEMP_CHANNEL { \
730 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
731 BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET), \
732 .scan_index = TEMP, \
740 static const struct iio_chan_spec bma023_channels[] = {
741 BMA023_ACC_CHANNEL(X, 10),
742 BMA023_ACC_CHANNEL(Y, 10),
743 BMA023_ACC_CHANNEL(Z, 10),
744 IIO_CHAN_SOFT_TIMESTAMP(4),
747 static const struct iio_chan_spec bma150_channels[] = {
748 BMA023_ACC_CHANNEL(X, 10),
749 BMA023_ACC_CHANNEL(Y, 10),
750 BMA023_ACC_CHANNEL(Z, 10),
752 IIO_CHAN_SOFT_TIMESTAMP(4),
755 static const struct iio_chan_spec bma180_channels[] = {
756 BMA180_ACC_CHANNEL(X, 14),
757 BMA180_ACC_CHANNEL(Y, 14),
758 BMA180_ACC_CHANNEL(Z, 14),
760 IIO_CHAN_SOFT_TIMESTAMP(4),
763 static const struct iio_chan_spec bma250_channels[] = {
764 BMA180_ACC_CHANNEL(X, 10),
765 BMA180_ACC_CHANNEL(Y, 10),
766 BMA180_ACC_CHANNEL(Z, 10),
768 IIO_CHAN_SOFT_TIMESTAMP(4),
771 static const struct iio_chan_spec bma254_channels[] = {
772 BMA180_ACC_CHANNEL(X, 12),
773 BMA180_ACC_CHANNEL(Y, 12),
774 BMA180_ACC_CHANNEL(Z, 12),
776 IIO_CHAN_SOFT_TIMESTAMP(4),
779 static const struct bma180_part_info bma180_part_info[] = {
781 .chip_id = BMA023_ID_REG_VAL,
782 .channels = bma023_channels,
783 .num_channels = ARRAY_SIZE(bma023_channels),
784 .scale_table = bma023_scale_table,
785 .num_scales = ARRAY_SIZE(bma023_scale_table),
786 .bw_table = bma023_bw_table,
787 .num_bw = ARRAY_SIZE(bma023_bw_table),
788 /* No temperature channel */
790 .int_reset_reg = BMA023_CTRL_REG0,
791 .int_reset_mask = BMA023_INT_RESET_MASK,
792 .sleep_reg = BMA023_CTRL_REG0,
793 .sleep_mask = BMA023_SLEEP,
794 .bw_reg = BMA023_CTRL_REG2,
795 .bw_mask = BMA023_BW_MASK,
796 .scale_reg = BMA023_CTRL_REG2,
797 .scale_mask = BMA023_RANGE_MASK,
798 /* No power mode on bma023 */
802 .int_enable_reg = BMA023_CTRL_REG3,
803 .int_enable_mask = BMA023_NEW_DATA_INT,
804 .softreset_reg = BMA023_CTRL_REG0,
805 .softreset_val = BMA023_RESET_VAL,
806 .chip_config = bma023_chip_config,
807 .chip_disable = bma023_chip_disable,
810 .chip_id = BMA023_ID_REG_VAL,
811 .channels = bma150_channels,
812 .num_channels = ARRAY_SIZE(bma150_channels),
813 .scale_table = bma023_scale_table,
814 .num_scales = ARRAY_SIZE(bma023_scale_table),
815 .bw_table = bma023_bw_table,
816 .num_bw = ARRAY_SIZE(bma023_bw_table),
817 .temp_offset = -60, /* 0 LSB @ -30 degree C */
818 .int_reset_reg = BMA023_CTRL_REG0,
819 .int_reset_mask = BMA023_INT_RESET_MASK,
820 .sleep_reg = BMA023_CTRL_REG0,
821 .sleep_mask = BMA023_SLEEP,
822 .bw_reg = BMA023_CTRL_REG2,
823 .bw_mask = BMA023_BW_MASK,
824 .scale_reg = BMA023_CTRL_REG2,
825 .scale_mask = BMA023_RANGE_MASK,
826 /* No power mode on bma150 */
830 .int_enable_reg = BMA023_CTRL_REG3,
831 .int_enable_mask = BMA023_NEW_DATA_INT,
832 .softreset_reg = BMA023_CTRL_REG0,
833 .softreset_val = BMA023_RESET_VAL,
834 .chip_config = bma023_chip_config,
835 .chip_disable = bma023_chip_disable,
838 .chip_id = BMA180_ID_REG_VAL,
839 .channels = bma180_channels,
840 .num_channels = ARRAY_SIZE(bma180_channels),
841 .scale_table = bma180_scale_table,
842 .num_scales = ARRAY_SIZE(bma180_scale_table),
843 .bw_table = bma180_bw_table,
844 .num_bw = ARRAY_SIZE(bma180_bw_table),
845 .temp_offset = 48, /* 0 LSB @ 24 degree C */
846 .int_reset_reg = BMA180_CTRL_REG0,
847 .int_reset_mask = BMA180_RESET_INT,
848 .sleep_reg = BMA180_CTRL_REG0,
849 .sleep_mask = BMA180_SLEEP,
850 .bw_reg = BMA180_BW_TCS,
851 .bw_mask = BMA180_BW,
852 .scale_reg = BMA180_OFFSET_LSB1,
853 .scale_mask = BMA180_RANGE,
854 .power_reg = BMA180_TCO_Z,
855 .power_mask = BMA180_MODE_CONFIG,
856 .lowpower_val = BMA180_LOW_POWER,
857 .int_enable_reg = BMA180_CTRL_REG3,
858 .int_enable_mask = BMA180_NEW_DATA_INT,
859 .softreset_reg = BMA180_RESET,
860 .softreset_val = BMA180_RESET_VAL,
861 .chip_config = bma180_chip_config,
862 .chip_disable = bma180_chip_disable,
865 .chip_id = BMA250_ID_REG_VAL,
866 .channels = bma250_channels,
867 .num_channels = ARRAY_SIZE(bma250_channels),
868 .scale_table = bma25x_scale_table,
869 .num_scales = ARRAY_SIZE(bma25x_scale_table),
870 .bw_table = bma25x_bw_table,
871 .num_bw = ARRAY_SIZE(bma25x_bw_table),
872 .temp_offset = 48, /* 0 LSB @ 24 degree C */
873 .int_reset_reg = BMA250_INT_RESET_REG,
874 .int_reset_mask = BMA250_INT_RESET_MASK,
875 .sleep_reg = BMA250_POWER_REG,
876 .sleep_mask = BMA250_SUSPEND_MASK,
877 .bw_reg = BMA250_BW_REG,
878 .bw_mask = BMA250_BW_MASK,
879 .scale_reg = BMA250_RANGE_REG,
880 .scale_mask = BMA250_RANGE_MASK,
881 .power_reg = BMA250_POWER_REG,
882 .power_mask = BMA250_LOWPOWER_MASK,
884 .int_enable_reg = BMA250_INT_ENABLE_REG,
885 .int_enable_mask = BMA250_DATA_INTEN_MASK,
886 .int_map_reg = BMA250_INT_MAP_REG,
887 .int_enable_dataready_int1_mask = BMA250_INT1_DATA_MASK,
888 .softreset_reg = BMA250_RESET_REG,
889 .softreset_val = BMA180_RESET_VAL,
890 .chip_config = bma25x_chip_config,
891 .chip_disable = bma25x_chip_disable,
894 .chip_id = BMA254_ID_REG_VAL,
895 .channels = bma254_channels,
896 .num_channels = ARRAY_SIZE(bma254_channels),
897 .scale_table = bma25x_scale_table,
898 .num_scales = ARRAY_SIZE(bma25x_scale_table),
899 .bw_table = bma25x_bw_table,
900 .num_bw = ARRAY_SIZE(bma25x_bw_table),
901 .temp_offset = 46, /* 0 LSB @ 23 degree C */
902 .int_reset_reg = BMA254_INT_RESET_REG,
903 .int_reset_mask = BMA254_INT_RESET_MASK,
904 .sleep_reg = BMA254_POWER_REG,
905 .sleep_mask = BMA254_SUSPEND_MASK,
906 .bw_reg = BMA254_BW_REG,
907 .bw_mask = BMA254_BW_MASK,
908 .scale_reg = BMA254_RANGE_REG,
909 .scale_mask = BMA254_RANGE_MASK,
910 .power_reg = BMA254_POWER_REG,
911 .power_mask = BMA254_LOWPOWER_MASK,
913 .int_enable_reg = BMA254_INT_ENABLE_REG,
914 .int_enable_mask = BMA254_DATA_INTEN_MASK,
915 .int_map_reg = BMA254_INT_MAP_REG,
916 .int_enable_dataready_int1_mask = BMA254_INT1_DATA_MASK,
917 .softreset_reg = BMA254_RESET_REG,
918 .softreset_val = BMA180_RESET_VAL,
919 .chip_config = bma25x_chip_config,
920 .chip_disable = bma25x_chip_disable,
924 static irqreturn_t bma180_trigger_handler(int irq, void *p)
926 struct iio_poll_func *pf = p;
927 struct iio_dev *indio_dev = pf->indio_dev;
928 struct bma180_data *data = iio_priv(indio_dev);
929 s64 time_ns = iio_get_time_ns(indio_dev);
932 mutex_lock(&data->mutex);
934 for_each_set_bit(bit, indio_dev->active_scan_mask,
935 indio_dev->masklength) {
936 ret = bma180_get_data_reg(data, bit);
938 mutex_unlock(&data->mutex);
941 ((s16 *)data->buff)[i++] = ret;
944 mutex_unlock(&data->mutex);
946 iio_push_to_buffers_with_timestamp(indio_dev, data->buff, time_ns);
948 iio_trigger_notify_done(indio_dev->trig);
953 static int bma180_data_rdy_trigger_set_state(struct iio_trigger *trig,
956 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
957 struct bma180_data *data = iio_priv(indio_dev);
959 return bma180_set_new_data_intr_state(data, state);
962 static void bma180_trig_reen(struct iio_trigger *trig)
964 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
965 struct bma180_data *data = iio_priv(indio_dev);
968 ret = bma180_reset_intr(data);
970 dev_err(&data->client->dev, "failed to reset interrupt\n");
973 static const struct iio_trigger_ops bma180_trigger_ops = {
974 .set_trigger_state = bma180_data_rdy_trigger_set_state,
975 .reenable = bma180_trig_reen,
978 static int bma180_probe(struct i2c_client *client,
979 const struct i2c_device_id *id)
981 struct device *dev = &client->dev;
982 struct bma180_data *data;
983 struct iio_dev *indio_dev;
987 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
991 data = iio_priv(indio_dev);
992 i2c_set_clientdata(client, indio_dev);
993 data->client = client;
994 if (client->dev.of_node)
995 chip = (enum chip_ids)of_device_get_match_data(dev);
997 chip = id->driver_data;
998 data->part_info = &bma180_part_info[chip];
1000 ret = iio_read_mount_matrix(dev, "mount-matrix",
1001 &data->orientation);
1005 data->vdd_supply = devm_regulator_get(dev, "vdd");
1006 if (IS_ERR(data->vdd_supply))
1007 return dev_err_probe(dev, PTR_ERR(data->vdd_supply),
1008 "Failed to get vdd regulator\n");
1010 data->vddio_supply = devm_regulator_get(dev, "vddio");
1011 if (IS_ERR(data->vddio_supply))
1012 return dev_err_probe(dev, PTR_ERR(data->vddio_supply),
1013 "Failed to get vddio regulator\n");
1015 /* Typical voltage 2.4V these are min and max */
1016 ret = regulator_set_voltage(data->vdd_supply, 1620000, 3600000);
1019 ret = regulator_set_voltage(data->vddio_supply, 1200000, 3600000);
1022 ret = regulator_enable(data->vdd_supply);
1024 dev_err(dev, "Failed to enable vdd regulator: %d\n", ret);
1027 ret = regulator_enable(data->vddio_supply);
1029 dev_err(dev, "Failed to enable vddio regulator: %d\n", ret);
1030 goto err_disable_vdd;
1032 /* Wait to make sure we started up properly (3 ms at least) */
1033 usleep_range(3000, 5000);
1035 ret = data->part_info->chip_config(data);
1037 goto err_chip_disable;
1039 mutex_init(&data->mutex);
1040 indio_dev->channels = data->part_info->channels;
1041 indio_dev->num_channels = data->part_info->num_channels;
1042 indio_dev->name = id->name;
1043 indio_dev->modes = INDIO_DIRECT_MODE;
1044 indio_dev->info = &bma180_info;
1046 if (client->irq > 0) {
1047 data->trig = iio_trigger_alloc(dev, "%s-dev%d", indio_dev->name,
1048 iio_device_id(indio_dev));
1051 goto err_chip_disable;
1054 ret = devm_request_irq(dev, client->irq,
1055 iio_trigger_generic_data_rdy_poll, IRQF_TRIGGER_RISING,
1056 "bma180_event", data->trig);
1058 dev_err(dev, "unable to request IRQ\n");
1059 goto err_trigger_free;
1062 data->trig->ops = &bma180_trigger_ops;
1063 iio_trigger_set_drvdata(data->trig, indio_dev);
1064 indio_dev->trig = iio_trigger_get(data->trig);
1066 ret = iio_trigger_register(data->trig);
1068 goto err_trigger_free;
1071 ret = iio_triggered_buffer_setup(indio_dev, NULL,
1072 bma180_trigger_handler, NULL);
1074 dev_err(dev, "unable to setup iio triggered buffer\n");
1075 goto err_trigger_unregister;
1078 ret = iio_device_register(indio_dev);
1080 dev_err(dev, "unable to register iio device\n");
1081 goto err_buffer_cleanup;
1087 iio_triggered_buffer_cleanup(indio_dev);
1088 err_trigger_unregister:
1090 iio_trigger_unregister(data->trig);
1092 iio_trigger_free(data->trig);
1094 data->part_info->chip_disable(data);
1095 regulator_disable(data->vddio_supply);
1097 regulator_disable(data->vdd_supply);
1102 static int bma180_remove(struct i2c_client *client)
1104 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1105 struct bma180_data *data = iio_priv(indio_dev);
1107 iio_device_unregister(indio_dev);
1108 iio_triggered_buffer_cleanup(indio_dev);
1110 iio_trigger_unregister(data->trig);
1111 iio_trigger_free(data->trig);
1114 mutex_lock(&data->mutex);
1115 data->part_info->chip_disable(data);
1116 mutex_unlock(&data->mutex);
1117 regulator_disable(data->vddio_supply);
1118 regulator_disable(data->vdd_supply);
1123 #ifdef CONFIG_PM_SLEEP
1124 static int bma180_suspend(struct device *dev)
1126 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1127 struct bma180_data *data = iio_priv(indio_dev);
1130 mutex_lock(&data->mutex);
1131 ret = bma180_set_sleep_state(data, true);
1132 mutex_unlock(&data->mutex);
1137 static int bma180_resume(struct device *dev)
1139 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1140 struct bma180_data *data = iio_priv(indio_dev);
1143 mutex_lock(&data->mutex);
1144 ret = bma180_set_sleep_state(data, false);
1145 mutex_unlock(&data->mutex);
1150 static SIMPLE_DEV_PM_OPS(bma180_pm_ops, bma180_suspend, bma180_resume);
1151 #define BMA180_PM_OPS (&bma180_pm_ops)
1153 #define BMA180_PM_OPS NULL
1156 static const struct i2c_device_id bma180_ids[] = {
1157 { "bma023", BMA023 },
1158 { "bma150", BMA150 },
1159 { "bma180", BMA180 },
1160 { "bma250", BMA250 },
1161 { "bma254", BMA254 },
1162 { "smb380", BMA150 },
1166 MODULE_DEVICE_TABLE(i2c, bma180_ids);
1168 static const struct of_device_id bma180_of_match[] = {
1170 .compatible = "bosch,bma023",
1171 .data = (void *)BMA023
1174 .compatible = "bosch,bma150",
1175 .data = (void *)BMA150
1178 .compatible = "bosch,bma180",
1179 .data = (void *)BMA180
1182 .compatible = "bosch,bma250",
1183 .data = (void *)BMA250
1186 .compatible = "bosch,bma254",
1187 .data = (void *)BMA254
1190 .compatible = "bosch,smb380",
1191 .data = (void *)BMA150
1195 MODULE_DEVICE_TABLE(of, bma180_of_match);
1197 static struct i2c_driver bma180_driver = {
1200 .pm = BMA180_PM_OPS,
1201 .of_match_table = bma180_of_match,
1203 .probe = bma180_probe,
1204 .remove = bma180_remove,
1205 .id_table = bma180_ids,
1208 module_i2c_driver(bma180_driver);
1210 MODULE_AUTHOR("Kravchenko Oleksandr <x0199363@ti.com>");
1211 MODULE_AUTHOR("Texas Instruments, Inc.");
1212 MODULE_DESCRIPTION("Bosch BMA023/BMA1x0/BMA25x triaxial acceleration sensor");
1213 MODULE_LICENSE("GPL");