Skip to content

Commit 2017cff

Browse files
pmeerwjic23
authored andcommitted
iio:bma180: Add BMA250 chip support
the BMA250 has only 10-bit resolution; while the data readout registers have identical layout, the configuration is completely different compared to the BMA180 datasheet: http://ae-bst.resource.bosch.com/media/products/dokumente/bma250/BST-BMA250-DS002-05.pdf Signed-off-by: Peter Meerwald <pmeerw@pmeerw.net> Cc: Oleksandr Kravchenko <o.v.kravchenko@globallogic.com> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
1 parent 402a324 commit 2017cff

2 files changed

Lines changed: 128 additions & 34 deletions

File tree

‎drivers/iio/accel/Kconfig‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
menu "Accelerometers"
77

88
config BMA180
9-
tristate "Bosch BMA180 3-Axis Accelerometer Driver"
9+
tristate "Bosch BMA180/BMA250 3-Axis Accelerometer Driver"
1010
depends on I2C
1111
select IIO_BUFFER
1212
select IIO_TRIGGERED_BUFFER
1313
help
14-
Say Y here if you want to build a driver for the Bosch BMA180
15-
triaxial acceleration sensor.
14+
Say Y here if you want to build a driver for the Bosch BMA180 or
15+
BMA250 triaxial acceleration sensor.
1616

1717
To compile this driver as a module, choose M here: the
1818
module will be called bma180.

‎drivers/iio/accel/bma180.c‎

Lines changed: 125 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@
33
*
44
* Copyright 2013 Oleksandr Kravchenko <x0199363@ti.com>
55
*
6+
* Support for BMA250 (c) Peter Meerwald <pmeerw@pmeerw.net>
7+
*
68
* This file is subject to the terms and conditions of version 2 of
79
* the GNU General Public License. See the file COPYING in the main
810
* directory of this archive for more details.
11+
*
12+
* SPI is not supported by driver
13+
* BMA180: 7-bit I2C slave address 0x40 or 0x41
14+
* BMA250: 7-bit I2C slave address 0x18 or 0x19
915
*/
1016

1117
#include <linux/module.h>
@@ -28,6 +34,7 @@
2834

2935
enum {
3036
BMA180,
37+
BMA250,
3138
};
3239

3340
struct bma180_data;
@@ -39,6 +46,15 @@ struct bma180_part_info {
3946
unsigned num_scales;
4047
const int *bw_table;
4148
unsigned num_bw;
49+
50+
u8 int_reset_reg, int_reset_mask;
51+
u8 sleep_reg, sleep_mask;
52+
u8 bw_reg, bw_mask;
53+
u8 scale_reg, scale_mask;
54+
u8 power_reg, power_mask, lowpower_val;
55+
u8 int_enable_reg, int_enable_mask;
56+
u8 softreset_reg;
57+
4258
int (*chip_config)(struct bma180_data *data);
4359
void (*chip_disable)(struct bma180_data *data);
4460
};
@@ -77,13 +93,23 @@ struct bma180_part_info {
7793
#define BMA180_ID_REG_VAL 0x03
7894

7995
/* Chip power modes */
80-
#define BMA180_LOW_NOISE 0x00
8196
#define BMA180_LOW_POWER 0x03
8297

83-
/* Defaults values */
84-
#define BMA180_DEF_PMODE false
85-
#define BMA180_DEF_BW 20
86-
#define BMA180_DEF_SCALE 2452
98+
#define BMA250_RANGE_REG 0x0f
99+
#define BMA250_BW_REG 0x10
100+
#define BMA250_POWER_REG 0x11
101+
#define BMA250_RESET_REG 0x14
102+
#define BMA250_INT_ENABLE_REG 0x17
103+
#define BMA250_INT_MAP_REG 0x1a
104+
#define BMA250_INT_RESET_REG 0x21
105+
106+
#define BMA250_RANGE_MASK GENMASK(3, 0) /* Range of accel values */
107+
#define BMA250_BW_MASK GENMASK(4, 0) /* Accel bandwidth */
108+
#define BMA250_SUSPEND_MASK BIT(7) /* chip will sleep */
109+
#define BMA250_LOWPOWER_MASK BIT(6)
110+
#define BMA250_DATA_INTEN_MASK BIT(4)
111+
#define BMA250_INT1_DATA_MASK BIT(0)
112+
#define BMA250_INT_RESET_MASK BIT(7) /* Reset pending interrupts */
87113

88114
struct bma180_data {
89115
struct i2c_client *client;
@@ -107,6 +133,10 @@ enum bma180_chan {
107133
static int bma180_bw_table[] = { 10, 20, 40, 75, 150, 300 }; /* Hz */
108134
static int bma180_scale_table[] = { 1275, 1863, 2452, 3727, 4903, 9709, 19417 };
109135

136+
static int bma250_bw_table[] = { 8, 16, 31, 63, 125, 250 }; /* Hz */
137+
static int bma250_scale_table[] = { 0, 0, 0, 38344, 0, 76590, 0, 0, 153180, 0,
138+
0, 0, 306458 };
139+
110140
static int bma180_get_data_reg(struct bma180_data *data, enum bma180_chan chan)
111141
{
112142
int ret;
@@ -145,7 +175,8 @@ static int bma180_set_bits(struct bma180_data *data, u8 reg, u8 mask, u8 val)
145175

146176
static int bma180_reset_intr(struct bma180_data *data)
147177
{
148-
int ret = bma180_set_bits(data, BMA180_CTRL_REG0, BMA180_RESET_INT, 1);
178+
int ret = bma180_set_bits(data, data->part_info->int_reset_reg,
179+
data->part_info->int_reset_mask, 1);
149180

150181
if (ret)
151182
dev_err(&data->client->dev, "failed to reset interrupt\n");
@@ -155,10 +186,8 @@ static int bma180_reset_intr(struct bma180_data *data)
155186

156187
static int bma180_set_new_data_intr_state(struct bma180_data *data, bool state)
157188
{
158-
u8 reg_val = state ? BMA180_NEW_DATA_INT : 0x00;
159-
int ret = i2c_smbus_write_byte_data(data->client, BMA180_CTRL_REG3,
160-
reg_val);
161-
189+
int ret = bma180_set_bits(data, data->part_info->int_enable_reg,
190+
data->part_info->int_enable_mask, state);
162191
if (ret)
163192
goto err;
164193
ret = bma180_reset_intr(data);
@@ -175,7 +204,8 @@ static int bma180_set_new_data_intr_state(struct bma180_data *data, bool state)
175204

176205
static int bma180_set_sleep_state(struct bma180_data *data, bool state)
177206
{
178-
int ret = bma180_set_bits(data, BMA180_CTRL_REG0, BMA180_SLEEP, state);
207+
int ret = bma180_set_bits(data, data->part_info->sleep_reg,
208+
data->part_info->sleep_mask, state);
179209

180210
if (ret) {
181211
dev_err(&data->client->dev,
@@ -207,8 +237,8 @@ static int bma180_set_bw(struct bma180_data *data, int val)
207237

208238
for (i = 0; i < data->part_info->num_bw; ++i) {
209239
if (data->part_info->bw_table[i] == val) {
210-
ret = bma180_set_bits(data,
211-
BMA180_BW_TCS, BMA180_BW, i);
240+
ret = bma180_set_bits(data, data->part_info->bw_reg,
241+
data->part_info->bw_mask, i);
212242
if (ret) {
213243
dev_err(&data->client->dev,
214244
"failed to set bandwidth\n");
@@ -231,8 +261,8 @@ static int bma180_set_scale(struct bma180_data *data, int val)
231261

232262
for (i = 0; i < data->part_info->num_scales; ++i)
233263
if (data->part_info->scale_table[i] == val) {
234-
ret = bma180_set_bits(data,
235-
BMA180_OFFSET_LSB1, BMA180_RANGE, i);
264+
ret = bma180_set_bits(data, data->part_info->scale_reg,
265+
data->part_info->scale_mask, i);
236266
if (ret) {
237267
dev_err(&data->client->dev,
238268
"failed to set scale\n");
@@ -247,9 +277,9 @@ static int bma180_set_scale(struct bma180_data *data, int val)
247277

248278
static int bma180_set_pmode(struct bma180_data *data, bool mode)
249279
{
250-
u8 reg_val = mode ? BMA180_LOW_POWER : BMA180_LOW_NOISE;
251-
int ret = bma180_set_bits(data, BMA180_TCO_Z, BMA180_MODE_CONFIG,
252-
reg_val);
280+
u8 reg_val = mode ? data->part_info->lowpower_val : 0;
281+
int ret = bma180_set_bits(data, data->part_info->power_reg,
282+
data->part_info->power_mask, reg_val);
253283

254284
if (ret) {
255285
dev_err(&data->client->dev, "failed to set power mode\n");
@@ -263,7 +293,7 @@ static int bma180_set_pmode(struct bma180_data *data, bool mode)
263293
static int bma180_soft_reset(struct bma180_data *data)
264294
{
265295
int ret = i2c_smbus_write_byte_data(data->client,
266-
BMA180_RESET, BMA180_RESET_VAL);
296+
data->part_info->softreset_reg, BMA180_RESET_VAL);
267297

268298
if (ret)
269299
dev_err(&data->client->dev, "failed to reset the chip\n");
@@ -290,7 +320,11 @@ static int bma180_chip_init(struct bma180_data *data)
290320
*/
291321
msleep(20);
292322

293-
return 0;
323+
ret = bma180_set_new_data_intr_state(data, false);
324+
if (ret)
325+
return ret;
326+
327+
return bma180_set_pmode(data, false);
294328
}
295329

296330
static int bma180_chip_config(struct bma180_data *data)
@@ -305,19 +339,37 @@ static int bma180_chip_config(struct bma180_data *data)
305339
ret = bma180_set_ee_writing_state(data, true);
306340
if (ret)
307341
goto err;
308-
ret = bma180_set_new_data_intr_state(data, false);
342+
ret = bma180_set_bits(data, BMA180_OFFSET_LSB1, BMA180_SMP_SKIP, 1);
309343
if (ret)
310344
goto err;
311-
ret = bma180_set_bits(data, BMA180_OFFSET_LSB1, BMA180_SMP_SKIP, 1);
345+
ret = bma180_set_bw(data, 20); /* 20 Hz */
312346
if (ret)
313347
goto err;
314-
ret = bma180_set_pmode(data, BMA180_DEF_PMODE);
348+
ret = bma180_set_scale(data, 2452); /* 2 G */
315349
if (ret)
316350
goto err;
317-
ret = bma180_set_bw(data, BMA180_DEF_BW);
351+
352+
return 0;
353+
354+
err:
355+
dev_err(&data->client->dev, "failed to config the chip\n");
356+
return ret;
357+
}
358+
359+
static int bma250_chip_config(struct bma180_data *data)
360+
{
361+
int ret = bma180_chip_init(data);
362+
363+
if (ret)
364+
goto err;
365+
ret = bma180_set_bw(data, 16); /* 16 Hz */
318366
if (ret)
319367
goto err;
320-
ret = bma180_set_scale(data, BMA180_DEF_SCALE);
368+
ret = bma180_set_scale(data, 38344); /* 2 G */
369+
if (ret)
370+
goto err;
371+
ret = bma180_set_bits(data, BMA250_INT_MAP_REG,
372+
BMA250_INT1_DATA_MASK, 1);
321373
if (ret)
322374
goto err;
323375

@@ -343,6 +395,19 @@ static void bma180_chip_disable(struct bma180_data *data)
343395
dev_err(&data->client->dev, "failed to disable the chip\n");
344396
}
345397

398+
static void bma250_chip_disable(struct bma180_data *data)
399+
{
400+
if (bma180_set_new_data_intr_state(data, false))
401+
goto err;
402+
if (bma180_set_sleep_state(data, true))
403+
goto err;
404+
405+
return;
406+
407+
err:
408+
dev_err(&data->client->dev, "failed to disable the chip\n");
409+
}
410+
346411
static ssize_t bma180_show_avail(char *buf, const int *vals, unsigned n,
347412
bool micros)
348413
{
@@ -545,14 +610,43 @@ static const struct iio_chan_spec bma180_channels[] = {
545610
IIO_CHAN_SOFT_TIMESTAMP(4),
546611
};
547612

613+
static const struct iio_chan_spec bma250_channels[] = {
614+
BMA180_ACC_CHANNEL(X, 10),
615+
BMA180_ACC_CHANNEL(Y, 10),
616+
BMA180_ACC_CHANNEL(Z, 10),
617+
BMA180_TEMP_CHANNEL,
618+
IIO_CHAN_SOFT_TIMESTAMP(4),
619+
};
620+
548621
static const struct bma180_part_info bma180_part_info[] = {
549622
[BMA180] = {
550623
bma180_channels, ARRAY_SIZE(bma180_channels),
551624
bma180_scale_table, ARRAY_SIZE(bma180_scale_table),
552625
bma180_bw_table, ARRAY_SIZE(bma180_bw_table),
626+
BMA180_CTRL_REG0, BMA180_RESET_INT,
627+
BMA180_CTRL_REG0, BMA180_SLEEP,
628+
BMA180_BW_TCS, BMA180_BW,
629+
BMA180_OFFSET_LSB1, BMA180_RANGE,
630+
BMA180_TCO_Z, BMA180_MODE_CONFIG, BMA180_LOW_POWER,
631+
BMA180_CTRL_REG3, BMA180_NEW_DATA_INT,
632+
BMA180_RESET,
553633
bma180_chip_config,
554634
bma180_chip_disable,
555635
},
636+
[BMA250] = {
637+
bma250_channels, ARRAY_SIZE(bma250_channels),
638+
bma250_scale_table, ARRAY_SIZE(bma250_scale_table),
639+
bma250_bw_table, ARRAY_SIZE(bma250_bw_table),
640+
BMA250_INT_RESET_REG, BMA250_INT_RESET_MASK,
641+
BMA250_POWER_REG, BMA250_SUSPEND_MASK,
642+
BMA250_BW_REG, BMA250_BW_MASK,
643+
BMA250_RANGE_REG, BMA250_RANGE_MASK,
644+
BMA250_POWER_REG, BMA250_LOWPOWER_MASK, 1,
645+
BMA250_INT_ENABLE_REG, BMA250_DATA_INTEN_MASK,
646+
BMA250_RESET_REG,
647+
bma250_chip_config,
648+
bma250_chip_disable,
649+
},
556650
};
557651

558652
static irqreturn_t bma180_trigger_handler(int irq, void *p)
@@ -628,11 +722,10 @@ static int bma180_probe(struct i2c_client *client,
628722
goto err_chip_disable;
629723

630724
mutex_init(&data->mutex);
631-
632725
indio_dev->dev.parent = &client->dev;
633726
indio_dev->channels = data->part_info->channels;
634727
indio_dev->num_channels = data->part_info->num_channels;
635-
indio_dev->name = BMA180_DRV_NAME;
728+
indio_dev->name = id->name;
636729
indio_dev->modes = INDIO_DIRECT_MODE;
637730
indio_dev->info = &bma180_info;
638731

@@ -646,7 +739,7 @@ static int bma180_probe(struct i2c_client *client,
646739

647740
ret = devm_request_irq(&client->dev, client->irq,
648741
iio_trigger_generic_data_rdy_poll, IRQF_TRIGGER_RISING,
649-
BMA180_IRQ_NAME, data->trig);
742+
"bma180_event", data->trig);
650743
if (ret) {
651744
dev_err(&client->dev, "unable to request IRQ\n");
652745
goto err_trigger_free;
@@ -743,15 +836,16 @@ static SIMPLE_DEV_PM_OPS(bma180_pm_ops, bma180_suspend, bma180_resume);
743836
#endif
744837

745838
static struct i2c_device_id bma180_ids[] = {
746-
{ BMA180_DRV_NAME, BMA180 },
839+
{ "bma180", BMA180 },
840+
{ "bma250", BMA250 },
747841
{ }
748842
};
749843

750844
MODULE_DEVICE_TABLE(i2c, bma180_ids);
751845

752846
static struct i2c_driver bma180_driver = {
753847
.driver = {
754-
.name = BMA180_DRV_NAME,
848+
.name = "bma180",
755849
.owner = THIS_MODULE,
756850
.pm = BMA180_PM_OPS,
757851
},
@@ -764,5 +858,5 @@ module_i2c_driver(bma180_driver);
764858

765859
MODULE_AUTHOR("Kravchenko Oleksandr <x0199363@ti.com>");
766860
MODULE_AUTHOR("Texas Instruments, Inc.");
767-
MODULE_DESCRIPTION("Bosch BMA180 triaxial acceleration sensor");
861+
MODULE_DESCRIPTION("Bosch BMA180/BMA250 triaxial acceleration sensor");
768862
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)