.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * bma180.c - IIO driver for Bosch BMA180 triaxial acceleration sensor |
---|
3 | 4 | * |
---|
.. | .. |
---|
5 | 6 | * |
---|
6 | 7 | * Support for BMA250 (c) Peter Meerwald <pmeerw@pmeerw.net> |
---|
7 | 8 | * |
---|
8 | | - * This file is subject to the terms and conditions of version 2 of |
---|
9 | | - * the GNU General Public License. See the file COPYING in the main |
---|
10 | | - * directory of this archive for more details. |
---|
11 | | - * |
---|
12 | 9 | * SPI is not supported by driver |
---|
| 10 | + * BMA023/BMA150/SMB380: 7-bit I2C slave address 0x38 |
---|
13 | 11 | * BMA180: 7-bit I2C slave address 0x40 or 0x41 |
---|
14 | 12 | * BMA250: 7-bit I2C slave address 0x18 or 0x19 |
---|
| 13 | + * BMA254: 7-bit I2C slave address 0x18 or 0x19 |
---|
15 | 14 | */ |
---|
16 | 15 | |
---|
17 | 16 | #include <linux/module.h> |
---|
.. | .. |
---|
21 | 20 | #include <linux/of_device.h> |
---|
22 | 21 | #include <linux/of.h> |
---|
23 | 22 | #include <linux/bitops.h> |
---|
| 23 | +#include <linux/regulator/consumer.h> |
---|
24 | 24 | #include <linux/slab.h> |
---|
25 | 25 | #include <linux/string.h> |
---|
26 | 26 | #include <linux/iio/iio.h> |
---|
.. | .. |
---|
34 | 34 | #define BMA180_IRQ_NAME "bma180_event" |
---|
35 | 35 | |
---|
36 | 36 | enum chip_ids { |
---|
| 37 | + BMA023, |
---|
| 38 | + BMA150, |
---|
37 | 39 | BMA180, |
---|
38 | 40 | BMA250, |
---|
| 41 | + BMA254, |
---|
39 | 42 | }; |
---|
40 | 43 | |
---|
41 | 44 | struct bma180_data; |
---|
42 | 45 | |
---|
43 | 46 | struct bma180_part_info { |
---|
| 47 | + u8 chip_id; |
---|
44 | 48 | const struct iio_chan_spec *channels; |
---|
45 | 49 | unsigned int num_channels; |
---|
46 | 50 | const int *scale_table; |
---|
47 | 51 | unsigned int num_scales; |
---|
48 | 52 | const int *bw_table; |
---|
49 | 53 | unsigned int num_bw; |
---|
| 54 | + int temp_offset; |
---|
50 | 55 | |
---|
51 | 56 | u8 int_reset_reg, int_reset_mask; |
---|
52 | 57 | u8 sleep_reg, sleep_mask; |
---|
.. | .. |
---|
54 | 59 | u8 scale_reg, scale_mask; |
---|
55 | 60 | u8 power_reg, power_mask, lowpower_val; |
---|
56 | 61 | u8 int_enable_reg, int_enable_mask; |
---|
57 | | - u8 softreset_reg; |
---|
| 62 | + u8 int_map_reg, int_enable_dataready_int1_mask; |
---|
| 63 | + u8 softreset_reg, softreset_val; |
---|
58 | 64 | |
---|
59 | 65 | int (*chip_config)(struct bma180_data *data); |
---|
60 | 66 | void (*chip_disable)(struct bma180_data *data); |
---|
61 | 67 | }; |
---|
62 | 68 | |
---|
63 | 69 | /* Register set */ |
---|
| 70 | +#define BMA023_CTRL_REG0 0x0a |
---|
| 71 | +#define BMA023_CTRL_REG1 0x0b |
---|
| 72 | +#define BMA023_CTRL_REG2 0x14 |
---|
| 73 | +#define BMA023_CTRL_REG3 0x15 |
---|
| 74 | + |
---|
| 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) |
---|
| 81 | + |
---|
64 | 82 | #define BMA180_CHIP_ID 0x00 /* Need to distinguish BMA180 from other */ |
---|
65 | 83 | #define BMA180_ACC_X_LSB 0x02 /* First of 6 registers of accel data */ |
---|
66 | 84 | #define BMA180_TEMP 0x08 |
---|
.. | .. |
---|
91 | 109 | /* We have to write this value in reset register to do soft reset */ |
---|
92 | 110 | #define BMA180_RESET_VAL 0xb6 |
---|
93 | 111 | |
---|
| 112 | +#define BMA023_ID_REG_VAL 0x02 |
---|
94 | 113 | #define BMA180_ID_REG_VAL 0x03 |
---|
| 114 | +#define BMA250_ID_REG_VAL 0x03 |
---|
| 115 | +#define BMA254_ID_REG_VAL 0xfa /* 250 decimal */ |
---|
95 | 116 | |
---|
96 | 117 | /* Chip power modes */ |
---|
97 | 118 | #define BMA180_LOW_POWER 0x03 |
---|
.. | .. |
---|
113 | 134 | #define BMA250_INT1_DATA_MASK BIT(0) |
---|
114 | 135 | #define BMA250_INT_RESET_MASK BIT(7) /* Reset pending interrupts */ |
---|
115 | 136 | |
---|
| 137 | +#define BMA254_RANGE_REG 0x0f |
---|
| 138 | +#define BMA254_BW_REG 0x10 |
---|
| 139 | +#define BMA254_POWER_REG 0x11 |
---|
| 140 | +#define BMA254_RESET_REG 0x14 |
---|
| 141 | +#define BMA254_INT_ENABLE_REG 0x17 |
---|
| 142 | +#define BMA254_INT_MAP_REG 0x1a |
---|
| 143 | +#define BMA254_INT_RESET_REG 0x21 |
---|
| 144 | + |
---|
| 145 | +#define BMA254_RANGE_MASK GENMASK(3, 0) /* Range of accel values */ |
---|
| 146 | +#define BMA254_BW_MASK GENMASK(4, 0) /* Accel bandwidth */ |
---|
| 147 | +#define BMA254_BW_OFFSET 8 |
---|
| 148 | +#define BMA254_SUSPEND_MASK BIT(7) /* chip will sleep */ |
---|
| 149 | +#define BMA254_LOWPOWER_MASK BIT(6) |
---|
| 150 | +#define BMA254_DATA_INTEN_MASK BIT(4) |
---|
| 151 | +#define BMA254_INT2_DATA_MASK BIT(7) |
---|
| 152 | +#define BMA254_INT1_DATA_MASK BIT(0) |
---|
| 153 | +#define BMA254_INT_RESET_MASK BIT(7) /* Reset pending interrupts */ |
---|
| 154 | + |
---|
116 | 155 | struct bma180_data { |
---|
| 156 | + struct regulator *vdd_supply; |
---|
| 157 | + struct regulator *vddio_supply; |
---|
117 | 158 | struct i2c_client *client; |
---|
118 | 159 | struct iio_trigger *trig; |
---|
119 | 160 | const struct bma180_part_info *part_info; |
---|
| 161 | + struct iio_mount_matrix orientation; |
---|
120 | 162 | struct mutex mutex; |
---|
121 | 163 | bool sleep_state; |
---|
122 | 164 | int scale; |
---|
.. | .. |
---|
136 | 178 | TEMP |
---|
137 | 179 | }; |
---|
138 | 180 | |
---|
| 181 | +static int bma023_bw_table[] = { 25, 50, 100, 190, 375, 750, 1500 }; /* Hz */ |
---|
| 182 | +static int bma023_scale_table[] = { 2452, 4903, 9709, }; |
---|
| 183 | + |
---|
139 | 184 | static int bma180_bw_table[] = { 10, 20, 40, 75, 150, 300 }; /* Hz */ |
---|
140 | 185 | static int bma180_scale_table[] = { 1275, 1863, 2452, 3727, 4903, 9709, 19417 }; |
---|
141 | 186 | |
---|
142 | | -static int bma250_bw_table[] = { 8, 16, 31, 63, 125, 250 }; /* Hz */ |
---|
143 | | -static int bma250_scale_table[] = { 0, 0, 0, 38344, 0, 76590, 0, 0, 153180, 0, |
---|
| 187 | +static int bma25x_bw_table[] = { 8, 16, 31, 63, 125, 250 }; /* Hz */ |
---|
| 188 | +static int bma25x_scale_table[] = { 0, 0, 0, 38344, 0, 76590, 0, 0, 153180, 0, |
---|
144 | 189 | 0, 0, 306458 }; |
---|
145 | 190 | |
---|
146 | 191 | static int bma180_get_data_reg(struct bma180_data *data, enum bma180_chan chan) |
---|
.. | .. |
---|
300 | 345 | static int bma180_soft_reset(struct bma180_data *data) |
---|
301 | 346 | { |
---|
302 | 347 | int ret = i2c_smbus_write_byte_data(data->client, |
---|
303 | | - data->part_info->softreset_reg, BMA180_RESET_VAL); |
---|
| 348 | + data->part_info->softreset_reg, |
---|
| 349 | + data->part_info->softreset_val); |
---|
304 | 350 | |
---|
305 | 351 | if (ret) |
---|
306 | 352 | dev_err(&data->client->dev, "failed to reset the chip\n"); |
---|
.. | .. |
---|
315 | 361 | |
---|
316 | 362 | if (ret < 0) |
---|
317 | 363 | return ret; |
---|
318 | | - if (ret != BMA180_ID_REG_VAL) |
---|
| 364 | + if (ret != data->part_info->chip_id) { |
---|
| 365 | + dev_err(&data->client->dev, "wrong chip ID %d expected %d\n", |
---|
| 366 | + ret, data->part_info->chip_id); |
---|
319 | 367 | return -ENODEV; |
---|
| 368 | + } |
---|
320 | 369 | |
---|
321 | 370 | ret = bma180_soft_reset(data); |
---|
322 | 371 | if (ret) |
---|
.. | .. |
---|
327 | 376 | */ |
---|
328 | 377 | msleep(20); |
---|
329 | 378 | |
---|
330 | | - ret = bma180_set_new_data_intr_state(data, false); |
---|
331 | | - if (ret) |
---|
332 | | - return ret; |
---|
| 379 | + return bma180_set_new_data_intr_state(data, false); |
---|
| 380 | +} |
---|
333 | 381 | |
---|
334 | | - return bma180_set_pmode(data, false); |
---|
| 382 | +static int bma023_chip_config(struct bma180_data *data) |
---|
| 383 | +{ |
---|
| 384 | + int ret = bma180_chip_init(data); |
---|
| 385 | + |
---|
| 386 | + if (ret) |
---|
| 387 | + goto err; |
---|
| 388 | + |
---|
| 389 | + ret = bma180_set_bw(data, 50); /* 50 Hz */ |
---|
| 390 | + if (ret) |
---|
| 391 | + goto err; |
---|
| 392 | + ret = bma180_set_scale(data, 2452); /* 2 G */ |
---|
| 393 | + if (ret) |
---|
| 394 | + goto err; |
---|
| 395 | + |
---|
| 396 | + return 0; |
---|
| 397 | + |
---|
| 398 | +err: |
---|
| 399 | + dev_err(&data->client->dev, "failed to config the chip\n"); |
---|
| 400 | + return ret; |
---|
335 | 401 | } |
---|
336 | 402 | |
---|
337 | 403 | static int bma180_chip_config(struct bma180_data *data) |
---|
338 | 404 | { |
---|
339 | 405 | int ret = bma180_chip_init(data); |
---|
340 | 406 | |
---|
| 407 | + if (ret) |
---|
| 408 | + goto err; |
---|
| 409 | + ret = bma180_set_pmode(data, false); |
---|
341 | 410 | if (ret) |
---|
342 | 411 | goto err; |
---|
343 | 412 | ret = bma180_set_bits(data, BMA180_CTRL_REG0, BMA180_DIS_WAKE_UP, 1); |
---|
.. | .. |
---|
363 | 432 | return ret; |
---|
364 | 433 | } |
---|
365 | 434 | |
---|
366 | | -static int bma250_chip_config(struct bma180_data *data) |
---|
| 435 | +static int bma25x_chip_config(struct bma180_data *data) |
---|
367 | 436 | { |
---|
368 | 437 | int ret = bma180_chip_init(data); |
---|
369 | 438 | |
---|
| 439 | + if (ret) |
---|
| 440 | + goto err; |
---|
| 441 | + ret = bma180_set_pmode(data, false); |
---|
370 | 442 | if (ret) |
---|
371 | 443 | goto err; |
---|
372 | 444 | ret = bma180_set_bw(data, 16); /* 16 Hz */ |
---|
.. | .. |
---|
375 | 447 | ret = bma180_set_scale(data, 38344); /* 2 G */ |
---|
376 | 448 | if (ret) |
---|
377 | 449 | goto err; |
---|
378 | | - ret = bma180_set_bits(data, BMA250_INT_MAP_REG, |
---|
379 | | - BMA250_INT1_DATA_MASK, 1); |
---|
| 450 | + /* |
---|
| 451 | + * This enables dataready interrupt on the INT1 pin |
---|
| 452 | + * FIXME: support using the INT2 pin |
---|
| 453 | + */ |
---|
| 454 | + ret = bma180_set_bits(data, data->part_info->int_map_reg, |
---|
| 455 | + data->part_info->int_enable_dataready_int1_mask, 1); |
---|
380 | 456 | if (ret) |
---|
381 | 457 | goto err; |
---|
382 | 458 | |
---|
.. | .. |
---|
385 | 461 | err: |
---|
386 | 462 | dev_err(&data->client->dev, "failed to config the chip\n"); |
---|
387 | 463 | return ret; |
---|
| 464 | +} |
---|
| 465 | + |
---|
| 466 | +static void bma023_chip_disable(struct bma180_data *data) |
---|
| 467 | +{ |
---|
| 468 | + if (bma180_set_sleep_state(data, true)) |
---|
| 469 | + goto err; |
---|
| 470 | + |
---|
| 471 | + return; |
---|
| 472 | + |
---|
| 473 | +err: |
---|
| 474 | + dev_err(&data->client->dev, "failed to disable the chip\n"); |
---|
388 | 475 | } |
---|
389 | 476 | |
---|
390 | 477 | static void bma180_chip_disable(struct bma180_data *data) |
---|
.. | .. |
---|
402 | 489 | dev_err(&data->client->dev, "failed to disable the chip\n"); |
---|
403 | 490 | } |
---|
404 | 491 | |
---|
405 | | -static void bma250_chip_disable(struct bma180_data *data) |
---|
| 492 | +static void bma25x_chip_disable(struct bma180_data *data) |
---|
406 | 493 | { |
---|
407 | 494 | if (bma180_set_new_data_intr_state(data, false)) |
---|
408 | 495 | goto err; |
---|
.. | .. |
---|
486 | 573 | iio_device_release_direct_mode(indio_dev); |
---|
487 | 574 | if (ret < 0) |
---|
488 | 575 | return ret; |
---|
489 | | - *val = sign_extend32(ret >> chan->scan_type.shift, |
---|
490 | | - chan->scan_type.realbits - 1); |
---|
| 576 | + if (chan->scan_type.sign == 's') { |
---|
| 577 | + *val = sign_extend32(ret >> chan->scan_type.shift, |
---|
| 578 | + chan->scan_type.realbits - 1); |
---|
| 579 | + } else { |
---|
| 580 | + *val = ret; |
---|
| 581 | + } |
---|
491 | 582 | return IIO_VAL_INT; |
---|
492 | 583 | case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: |
---|
493 | 584 | *val = data->bw; |
---|
.. | .. |
---|
505 | 596 | return -EINVAL; |
---|
506 | 597 | } |
---|
507 | 598 | case IIO_CHAN_INFO_OFFSET: |
---|
508 | | - *val = 48; /* 0 LSB @ 24 degree C */ |
---|
| 599 | + *val = data->part_info->temp_offset; |
---|
509 | 600 | return IIO_VAL_INT; |
---|
510 | 601 | default: |
---|
511 | 602 | return -EINVAL; |
---|
.. | .. |
---|
567 | 658 | return ret; |
---|
568 | 659 | } |
---|
569 | 660 | |
---|
| 661 | +static const struct iio_mount_matrix * |
---|
| 662 | +bma180_accel_get_mount_matrix(const struct iio_dev *indio_dev, |
---|
| 663 | + const struct iio_chan_spec *chan) |
---|
| 664 | +{ |
---|
| 665 | + struct bma180_data *data = iio_priv(indio_dev); |
---|
| 666 | + |
---|
| 667 | + return &data->orientation; |
---|
| 668 | +} |
---|
| 669 | + |
---|
570 | 670 | static const struct iio_enum bma180_power_mode_enum = { |
---|
571 | 671 | .items = bma180_power_modes, |
---|
572 | 672 | .num_items = ARRAY_SIZE(bma180_power_modes), |
---|
.. | .. |
---|
574 | 674 | .set = bma180_set_power_mode, |
---|
575 | 675 | }; |
---|
576 | 676 | |
---|
577 | | -static const struct iio_chan_spec_ext_info bma180_ext_info[] = { |
---|
578 | | - IIO_ENUM("power_mode", true, &bma180_power_mode_enum), |
---|
579 | | - IIO_ENUM_AVAILABLE("power_mode", &bma180_power_mode_enum), |
---|
580 | | - { }, |
---|
| 677 | +static const struct iio_chan_spec_ext_info bma023_ext_info[] = { |
---|
| 678 | + IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, bma180_accel_get_mount_matrix), |
---|
| 679 | + { } |
---|
581 | 680 | }; |
---|
| 681 | + |
---|
| 682 | +static const struct iio_chan_spec_ext_info bma180_ext_info[] = { |
---|
| 683 | + IIO_ENUM("power_mode", IIO_SHARED_BY_TYPE, &bma180_power_mode_enum), |
---|
| 684 | + IIO_ENUM_AVAILABLE("power_mode", &bma180_power_mode_enum), |
---|
| 685 | + IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, bma180_accel_get_mount_matrix), |
---|
| 686 | + { } |
---|
| 687 | +}; |
---|
| 688 | + |
---|
| 689 | +#define BMA023_ACC_CHANNEL(_axis, _bits) { \ |
---|
| 690 | + .type = IIO_ACCEL, \ |
---|
| 691 | + .modified = 1, \ |
---|
| 692 | + .channel2 = IIO_MOD_##_axis, \ |
---|
| 693 | + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
---|
| 694 | + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ |
---|
| 695 | + BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \ |
---|
| 696 | + .scan_index = AXIS_##_axis, \ |
---|
| 697 | + .scan_type = { \ |
---|
| 698 | + .sign = 's', \ |
---|
| 699 | + .realbits = _bits, \ |
---|
| 700 | + .storagebits = 16, \ |
---|
| 701 | + .shift = 16 - _bits, \ |
---|
| 702 | + }, \ |
---|
| 703 | + .ext_info = bma023_ext_info, \ |
---|
| 704 | +} |
---|
| 705 | + |
---|
| 706 | +#define BMA150_TEMP_CHANNEL { \ |
---|
| 707 | + .type = IIO_TEMP, \ |
---|
| 708 | + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ |
---|
| 709 | + BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET), \ |
---|
| 710 | + .scan_index = TEMP, \ |
---|
| 711 | + .scan_type = { \ |
---|
| 712 | + .sign = 'u', \ |
---|
| 713 | + .realbits = 8, \ |
---|
| 714 | + .storagebits = 16, \ |
---|
| 715 | + }, \ |
---|
| 716 | +} |
---|
582 | 717 | |
---|
583 | 718 | #define BMA180_ACC_CHANNEL(_axis, _bits) { \ |
---|
584 | 719 | .type = IIO_ACCEL, \ |
---|
.. | .. |
---|
609 | 744 | }, \ |
---|
610 | 745 | } |
---|
611 | 746 | |
---|
| 747 | +static const struct iio_chan_spec bma023_channels[] = { |
---|
| 748 | + BMA023_ACC_CHANNEL(X, 10), |
---|
| 749 | + BMA023_ACC_CHANNEL(Y, 10), |
---|
| 750 | + BMA023_ACC_CHANNEL(Z, 10), |
---|
| 751 | + IIO_CHAN_SOFT_TIMESTAMP(4), |
---|
| 752 | +}; |
---|
| 753 | + |
---|
| 754 | +static const struct iio_chan_spec bma150_channels[] = { |
---|
| 755 | + BMA023_ACC_CHANNEL(X, 10), |
---|
| 756 | + BMA023_ACC_CHANNEL(Y, 10), |
---|
| 757 | + BMA023_ACC_CHANNEL(Z, 10), |
---|
| 758 | + BMA150_TEMP_CHANNEL, |
---|
| 759 | + IIO_CHAN_SOFT_TIMESTAMP(4), |
---|
| 760 | +}; |
---|
| 761 | + |
---|
612 | 762 | static const struct iio_chan_spec bma180_channels[] = { |
---|
613 | 763 | BMA180_ACC_CHANNEL(X, 14), |
---|
614 | 764 | BMA180_ACC_CHANNEL(Y, 14), |
---|
.. | .. |
---|
625 | 775 | IIO_CHAN_SOFT_TIMESTAMP(4), |
---|
626 | 776 | }; |
---|
627 | 777 | |
---|
| 778 | +static const struct iio_chan_spec bma254_channels[] = { |
---|
| 779 | + BMA180_ACC_CHANNEL(X, 12), |
---|
| 780 | + BMA180_ACC_CHANNEL(Y, 12), |
---|
| 781 | + BMA180_ACC_CHANNEL(Z, 12), |
---|
| 782 | + BMA180_TEMP_CHANNEL, |
---|
| 783 | + IIO_CHAN_SOFT_TIMESTAMP(4), |
---|
| 784 | +}; |
---|
| 785 | + |
---|
628 | 786 | static const struct bma180_part_info bma180_part_info[] = { |
---|
| 787 | + [BMA023] = { |
---|
| 788 | + .chip_id = BMA023_ID_REG_VAL, |
---|
| 789 | + .channels = bma023_channels, |
---|
| 790 | + .num_channels = ARRAY_SIZE(bma023_channels), |
---|
| 791 | + .scale_table = bma023_scale_table, |
---|
| 792 | + .num_scales = ARRAY_SIZE(bma023_scale_table), |
---|
| 793 | + .bw_table = bma023_bw_table, |
---|
| 794 | + .num_bw = ARRAY_SIZE(bma023_bw_table), |
---|
| 795 | + /* No temperature channel */ |
---|
| 796 | + .temp_offset = 0, |
---|
| 797 | + .int_reset_reg = BMA023_CTRL_REG0, |
---|
| 798 | + .int_reset_mask = BMA023_INT_RESET_MASK, |
---|
| 799 | + .sleep_reg = BMA023_CTRL_REG0, |
---|
| 800 | + .sleep_mask = BMA023_SLEEP, |
---|
| 801 | + .bw_reg = BMA023_CTRL_REG2, |
---|
| 802 | + .bw_mask = BMA023_BW_MASK, |
---|
| 803 | + .scale_reg = BMA023_CTRL_REG2, |
---|
| 804 | + .scale_mask = BMA023_RANGE_MASK, |
---|
| 805 | + /* No power mode on bma023 */ |
---|
| 806 | + .power_reg = 0, |
---|
| 807 | + .power_mask = 0, |
---|
| 808 | + .lowpower_val = 0, |
---|
| 809 | + .int_enable_reg = BMA023_CTRL_REG3, |
---|
| 810 | + .int_enable_mask = BMA023_NEW_DATA_INT, |
---|
| 811 | + .softreset_reg = BMA023_CTRL_REG0, |
---|
| 812 | + .softreset_val = BMA023_RESET_VAL, |
---|
| 813 | + .chip_config = bma023_chip_config, |
---|
| 814 | + .chip_disable = bma023_chip_disable, |
---|
| 815 | + }, |
---|
| 816 | + [BMA150] = { |
---|
| 817 | + .chip_id = BMA023_ID_REG_VAL, |
---|
| 818 | + .channels = bma150_channels, |
---|
| 819 | + .num_channels = ARRAY_SIZE(bma150_channels), |
---|
| 820 | + .scale_table = bma023_scale_table, |
---|
| 821 | + .num_scales = ARRAY_SIZE(bma023_scale_table), |
---|
| 822 | + .bw_table = bma023_bw_table, |
---|
| 823 | + .num_bw = ARRAY_SIZE(bma023_bw_table), |
---|
| 824 | + .temp_offset = -60, /* 0 LSB @ -30 degree C */ |
---|
| 825 | + .int_reset_reg = BMA023_CTRL_REG0, |
---|
| 826 | + .int_reset_mask = BMA023_INT_RESET_MASK, |
---|
| 827 | + .sleep_reg = BMA023_CTRL_REG0, |
---|
| 828 | + .sleep_mask = BMA023_SLEEP, |
---|
| 829 | + .bw_reg = BMA023_CTRL_REG2, |
---|
| 830 | + .bw_mask = BMA023_BW_MASK, |
---|
| 831 | + .scale_reg = BMA023_CTRL_REG2, |
---|
| 832 | + .scale_mask = BMA023_RANGE_MASK, |
---|
| 833 | + /* No power mode on bma150 */ |
---|
| 834 | + .power_reg = 0, |
---|
| 835 | + .power_mask = 0, |
---|
| 836 | + .lowpower_val = 0, |
---|
| 837 | + .int_enable_reg = BMA023_CTRL_REG3, |
---|
| 838 | + .int_enable_mask = BMA023_NEW_DATA_INT, |
---|
| 839 | + .softreset_reg = BMA023_CTRL_REG0, |
---|
| 840 | + .softreset_val = BMA023_RESET_VAL, |
---|
| 841 | + .chip_config = bma023_chip_config, |
---|
| 842 | + .chip_disable = bma023_chip_disable, |
---|
| 843 | + }, |
---|
629 | 844 | [BMA180] = { |
---|
| 845 | + .chip_id = BMA180_ID_REG_VAL, |
---|
630 | 846 | .channels = bma180_channels, |
---|
631 | 847 | .num_channels = ARRAY_SIZE(bma180_channels), |
---|
632 | 848 | .scale_table = bma180_scale_table, |
---|
633 | 849 | .num_scales = ARRAY_SIZE(bma180_scale_table), |
---|
634 | 850 | .bw_table = bma180_bw_table, |
---|
635 | 851 | .num_bw = ARRAY_SIZE(bma180_bw_table), |
---|
| 852 | + .temp_offset = 48, /* 0 LSB @ 24 degree C */ |
---|
636 | 853 | .int_reset_reg = BMA180_CTRL_REG0, |
---|
637 | 854 | .int_reset_mask = BMA180_RESET_INT, |
---|
638 | 855 | .sleep_reg = BMA180_CTRL_REG0, |
---|
.. | .. |
---|
647 | 864 | .int_enable_reg = BMA180_CTRL_REG3, |
---|
648 | 865 | .int_enable_mask = BMA180_NEW_DATA_INT, |
---|
649 | 866 | .softreset_reg = BMA180_RESET, |
---|
| 867 | + .softreset_val = BMA180_RESET_VAL, |
---|
650 | 868 | .chip_config = bma180_chip_config, |
---|
651 | 869 | .chip_disable = bma180_chip_disable, |
---|
652 | 870 | }, |
---|
653 | 871 | [BMA250] = { |
---|
| 872 | + .chip_id = BMA250_ID_REG_VAL, |
---|
654 | 873 | .channels = bma250_channels, |
---|
655 | 874 | .num_channels = ARRAY_SIZE(bma250_channels), |
---|
656 | | - .scale_table = bma250_scale_table, |
---|
657 | | - .num_scales = ARRAY_SIZE(bma250_scale_table), |
---|
658 | | - .bw_table = bma250_bw_table, |
---|
659 | | - .num_bw = ARRAY_SIZE(bma250_bw_table), |
---|
| 875 | + .scale_table = bma25x_scale_table, |
---|
| 876 | + .num_scales = ARRAY_SIZE(bma25x_scale_table), |
---|
| 877 | + .bw_table = bma25x_bw_table, |
---|
| 878 | + .num_bw = ARRAY_SIZE(bma25x_bw_table), |
---|
| 879 | + .temp_offset = 48, /* 0 LSB @ 24 degree C */ |
---|
660 | 880 | .int_reset_reg = BMA250_INT_RESET_REG, |
---|
661 | 881 | .int_reset_mask = BMA250_INT_RESET_MASK, |
---|
662 | 882 | .sleep_reg = BMA250_POWER_REG, |
---|
.. | .. |
---|
671 | 891 | .lowpower_val = 1, |
---|
672 | 892 | .int_enable_reg = BMA250_INT_ENABLE_REG, |
---|
673 | 893 | .int_enable_mask = BMA250_DATA_INTEN_MASK, |
---|
| 894 | + .int_map_reg = BMA250_INT_MAP_REG, |
---|
| 895 | + .int_enable_dataready_int1_mask = BMA250_INT1_DATA_MASK, |
---|
674 | 896 | .softreset_reg = BMA250_RESET_REG, |
---|
675 | | - .chip_config = bma250_chip_config, |
---|
676 | | - .chip_disable = bma250_chip_disable, |
---|
| 897 | + .softreset_val = BMA180_RESET_VAL, |
---|
| 898 | + .chip_config = bma25x_chip_config, |
---|
| 899 | + .chip_disable = bma25x_chip_disable, |
---|
| 900 | + }, |
---|
| 901 | + [BMA254] = { |
---|
| 902 | + .chip_id = BMA254_ID_REG_VAL, |
---|
| 903 | + .channels = bma254_channels, |
---|
| 904 | + .num_channels = ARRAY_SIZE(bma254_channels), |
---|
| 905 | + .scale_table = bma25x_scale_table, |
---|
| 906 | + .num_scales = ARRAY_SIZE(bma25x_scale_table), |
---|
| 907 | + .bw_table = bma25x_bw_table, |
---|
| 908 | + .num_bw = ARRAY_SIZE(bma25x_bw_table), |
---|
| 909 | + .temp_offset = 46, /* 0 LSB @ 23 degree C */ |
---|
| 910 | + .int_reset_reg = BMA254_INT_RESET_REG, |
---|
| 911 | + .int_reset_mask = BMA254_INT_RESET_MASK, |
---|
| 912 | + .sleep_reg = BMA254_POWER_REG, |
---|
| 913 | + .sleep_mask = BMA254_SUSPEND_MASK, |
---|
| 914 | + .bw_reg = BMA254_BW_REG, |
---|
| 915 | + .bw_mask = BMA254_BW_MASK, |
---|
| 916 | + .bw_offset = BMA254_BW_OFFSET, |
---|
| 917 | + .scale_reg = BMA254_RANGE_REG, |
---|
| 918 | + .scale_mask = BMA254_RANGE_MASK, |
---|
| 919 | + .power_reg = BMA254_POWER_REG, |
---|
| 920 | + .power_mask = BMA254_LOWPOWER_MASK, |
---|
| 921 | + .lowpower_val = 1, |
---|
| 922 | + .int_enable_reg = BMA254_INT_ENABLE_REG, |
---|
| 923 | + .int_enable_mask = BMA254_DATA_INTEN_MASK, |
---|
| 924 | + .int_map_reg = BMA254_INT_MAP_REG, |
---|
| 925 | + .int_enable_dataready_int1_mask = BMA254_INT1_DATA_MASK, |
---|
| 926 | + .softreset_reg = BMA254_RESET_REG, |
---|
| 927 | + .softreset_val = BMA180_RESET_VAL, |
---|
| 928 | + .chip_config = bma25x_chip_config, |
---|
| 929 | + .chip_disable = bma25x_chip_disable, |
---|
677 | 930 | }, |
---|
678 | 931 | }; |
---|
679 | 932 | |
---|
.. | .. |
---|
731 | 984 | static int bma180_probe(struct i2c_client *client, |
---|
732 | 985 | const struct i2c_device_id *id) |
---|
733 | 986 | { |
---|
| 987 | + struct device *dev = &client->dev; |
---|
734 | 988 | struct bma180_data *data; |
---|
735 | 989 | struct iio_dev *indio_dev; |
---|
736 | 990 | enum chip_ids chip; |
---|
737 | 991 | int ret; |
---|
738 | 992 | |
---|
739 | | - indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); |
---|
| 993 | + indio_dev = devm_iio_device_alloc(dev, sizeof(*data)); |
---|
740 | 994 | if (!indio_dev) |
---|
741 | 995 | return -ENOMEM; |
---|
742 | 996 | |
---|
.. | .. |
---|
744 | 998 | i2c_set_clientdata(client, indio_dev); |
---|
745 | 999 | data->client = client; |
---|
746 | 1000 | if (client->dev.of_node) |
---|
747 | | - chip = (enum chip_ids)of_device_get_match_data(&client->dev); |
---|
| 1001 | + chip = (enum chip_ids)of_device_get_match_data(dev); |
---|
748 | 1002 | else |
---|
749 | 1003 | chip = id->driver_data; |
---|
750 | 1004 | data->part_info = &bma180_part_info[chip]; |
---|
| 1005 | + |
---|
| 1006 | + ret = iio_read_mount_matrix(dev, "mount-matrix", |
---|
| 1007 | + &data->orientation); |
---|
| 1008 | + if (ret) |
---|
| 1009 | + return ret; |
---|
| 1010 | + |
---|
| 1011 | + data->vdd_supply = devm_regulator_get(dev, "vdd"); |
---|
| 1012 | + if (IS_ERR(data->vdd_supply)) |
---|
| 1013 | + return dev_err_probe(dev, PTR_ERR(data->vdd_supply), |
---|
| 1014 | + "Failed to get vdd regulator\n"); |
---|
| 1015 | + |
---|
| 1016 | + data->vddio_supply = devm_regulator_get(dev, "vddio"); |
---|
| 1017 | + if (IS_ERR(data->vddio_supply)) |
---|
| 1018 | + return dev_err_probe(dev, PTR_ERR(data->vddio_supply), |
---|
| 1019 | + "Failed to get vddio regulator\n"); |
---|
| 1020 | + |
---|
| 1021 | + /* Typical voltage 2.4V these are min and max */ |
---|
| 1022 | + ret = regulator_set_voltage(data->vdd_supply, 1620000, 3600000); |
---|
| 1023 | + if (ret) |
---|
| 1024 | + return ret; |
---|
| 1025 | + ret = regulator_set_voltage(data->vddio_supply, 1200000, 3600000); |
---|
| 1026 | + if (ret) |
---|
| 1027 | + return ret; |
---|
| 1028 | + ret = regulator_enable(data->vdd_supply); |
---|
| 1029 | + if (ret) { |
---|
| 1030 | + dev_err(dev, "Failed to enable vdd regulator: %d\n", ret); |
---|
| 1031 | + return ret; |
---|
| 1032 | + } |
---|
| 1033 | + ret = regulator_enable(data->vddio_supply); |
---|
| 1034 | + if (ret) { |
---|
| 1035 | + dev_err(dev, "Failed to enable vddio regulator: %d\n", ret); |
---|
| 1036 | + goto err_disable_vdd; |
---|
| 1037 | + } |
---|
| 1038 | + /* Wait to make sure we started up properly (3 ms at least) */ |
---|
| 1039 | + usleep_range(3000, 5000); |
---|
751 | 1040 | |
---|
752 | 1041 | ret = data->part_info->chip_config(data); |
---|
753 | 1042 | if (ret < 0) |
---|
754 | 1043 | goto err_chip_disable; |
---|
755 | 1044 | |
---|
756 | 1045 | mutex_init(&data->mutex); |
---|
757 | | - indio_dev->dev.parent = &client->dev; |
---|
758 | 1046 | indio_dev->channels = data->part_info->channels; |
---|
759 | 1047 | indio_dev->num_channels = data->part_info->num_channels; |
---|
760 | 1048 | indio_dev->name = id->name; |
---|
.. | .. |
---|
769 | 1057 | goto err_chip_disable; |
---|
770 | 1058 | } |
---|
771 | 1059 | |
---|
772 | | - ret = devm_request_irq(&client->dev, client->irq, |
---|
| 1060 | + ret = devm_request_irq(dev, client->irq, |
---|
773 | 1061 | iio_trigger_generic_data_rdy_poll, IRQF_TRIGGER_RISING, |
---|
774 | 1062 | "bma180_event", data->trig); |
---|
775 | 1063 | if (ret) { |
---|
776 | | - dev_err(&client->dev, "unable to request IRQ\n"); |
---|
| 1064 | + dev_err(dev, "unable to request IRQ\n"); |
---|
777 | 1065 | goto err_trigger_free; |
---|
778 | 1066 | } |
---|
779 | 1067 | |
---|
780 | | - data->trig->dev.parent = &client->dev; |
---|
| 1068 | + data->trig->dev.parent = dev; |
---|
781 | 1069 | data->trig->ops = &bma180_trigger_ops; |
---|
782 | 1070 | iio_trigger_set_drvdata(data->trig, indio_dev); |
---|
783 | | - indio_dev->trig = iio_trigger_get(data->trig); |
---|
784 | 1071 | |
---|
785 | 1072 | ret = iio_trigger_register(data->trig); |
---|
786 | 1073 | if (ret) |
---|
787 | 1074 | goto err_trigger_free; |
---|
| 1075 | + |
---|
| 1076 | + indio_dev->trig = iio_trigger_get(data->trig); |
---|
788 | 1077 | } |
---|
789 | 1078 | |
---|
790 | 1079 | ret = iio_triggered_buffer_setup(indio_dev, NULL, |
---|
791 | 1080 | bma180_trigger_handler, NULL); |
---|
792 | 1081 | if (ret < 0) { |
---|
793 | | - dev_err(&client->dev, "unable to setup iio triggered buffer\n"); |
---|
| 1082 | + dev_err(dev, "unable to setup iio triggered buffer\n"); |
---|
794 | 1083 | goto err_trigger_unregister; |
---|
795 | 1084 | } |
---|
796 | 1085 | |
---|
797 | 1086 | ret = iio_device_register(indio_dev); |
---|
798 | 1087 | if (ret < 0) { |
---|
799 | | - dev_err(&client->dev, "unable to register iio device\n"); |
---|
| 1088 | + dev_err(dev, "unable to register iio device\n"); |
---|
800 | 1089 | goto err_buffer_cleanup; |
---|
801 | 1090 | } |
---|
802 | 1091 | |
---|
.. | .. |
---|
811 | 1100 | iio_trigger_free(data->trig); |
---|
812 | 1101 | err_chip_disable: |
---|
813 | 1102 | data->part_info->chip_disable(data); |
---|
| 1103 | + regulator_disable(data->vddio_supply); |
---|
| 1104 | +err_disable_vdd: |
---|
| 1105 | + regulator_disable(data->vdd_supply); |
---|
814 | 1106 | |
---|
815 | 1107 | return ret; |
---|
816 | 1108 | } |
---|
.. | .. |
---|
830 | 1122 | mutex_lock(&data->mutex); |
---|
831 | 1123 | data->part_info->chip_disable(data); |
---|
832 | 1124 | mutex_unlock(&data->mutex); |
---|
| 1125 | + regulator_disable(data->vddio_supply); |
---|
| 1126 | + regulator_disable(data->vdd_supply); |
---|
833 | 1127 | |
---|
834 | 1128 | return 0; |
---|
835 | 1129 | } |
---|
.. | .. |
---|
868 | 1162 | #endif |
---|
869 | 1163 | |
---|
870 | 1164 | static const struct i2c_device_id bma180_ids[] = { |
---|
| 1165 | + { "bma023", BMA023 }, |
---|
| 1166 | + { "bma150", BMA150 }, |
---|
871 | 1167 | { "bma180", BMA180 }, |
---|
872 | 1168 | { "bma250", BMA250 }, |
---|
| 1169 | + { "bma254", BMA254 }, |
---|
| 1170 | + { "smb380", BMA150 }, |
---|
873 | 1171 | { } |
---|
874 | 1172 | }; |
---|
875 | 1173 | |
---|
.. | .. |
---|
877 | 1175 | |
---|
878 | 1176 | static const struct of_device_id bma180_of_match[] = { |
---|
879 | 1177 | { |
---|
| 1178 | + .compatible = "bosch,bma023", |
---|
| 1179 | + .data = (void *)BMA023 |
---|
| 1180 | + }, |
---|
| 1181 | + { |
---|
| 1182 | + .compatible = "bosch,bma150", |
---|
| 1183 | + .data = (void *)BMA150 |
---|
| 1184 | + }, |
---|
| 1185 | + { |
---|
880 | 1186 | .compatible = "bosch,bma180", |
---|
881 | 1187 | .data = (void *)BMA180 |
---|
882 | 1188 | }, |
---|
883 | 1189 | { |
---|
884 | 1190 | .compatible = "bosch,bma250", |
---|
885 | 1191 | .data = (void *)BMA250 |
---|
| 1192 | + }, |
---|
| 1193 | + { |
---|
| 1194 | + .compatible = "bosch,bma254", |
---|
| 1195 | + .data = (void *)BMA254 |
---|
| 1196 | + }, |
---|
| 1197 | + { |
---|
| 1198 | + .compatible = "bosch,smb380", |
---|
| 1199 | + .data = (void *)BMA150 |
---|
886 | 1200 | }, |
---|
887 | 1201 | { } |
---|
888 | 1202 | }; |
---|
.. | .. |
---|
903 | 1217 | |
---|
904 | 1218 | MODULE_AUTHOR("Kravchenko Oleksandr <x0199363@ti.com>"); |
---|
905 | 1219 | MODULE_AUTHOR("Texas Instruments, Inc."); |
---|
906 | | -MODULE_DESCRIPTION("Bosch BMA180/BMA250 triaxial acceleration sensor"); |
---|
| 1220 | +MODULE_DESCRIPTION("Bosch BMA023/BMA1x0/BMA25x triaxial acceleration sensor"); |
---|
907 | 1221 | MODULE_LICENSE("GPL"); |
---|