aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/iio
diff options
context:
space:
mode:
authorPeter Meerwald <pmeerw@pmeerw.net>2014-08-19 18:43:00 -0400
committerJonathan Cameron <jic23@kernel.org>2014-09-14 15:52:25 -0400
commit2017cff24cc08b145bff7256dd6b0ef99e7e8a01 (patch)
tree7a950e36cd8bfd8d4a07a8c43769706673e38ce9 /drivers/iio
parent402a324e6103c234f73564a3a611766414b6325b (diff)
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>
Diffstat (limited to 'drivers/iio')
-rw-r--r--drivers/iio/accel/Kconfig6
-rw-r--r--drivers/iio/accel/bma180.c156
2 files changed, 128 insertions, 34 deletions
diff --git a/drivers/iio/accel/Kconfig b/drivers/iio/accel/Kconfig
index f71efc8d3f3e..9b9be8725e9d 100644
--- a/drivers/iio/accel/Kconfig
+++ b/drivers/iio/accel/Kconfig
@@ -6,13 +6,13 @@
6menu "Accelerometers" 6menu "Accelerometers"
7 7
8config BMA180 8config BMA180
9 tristate "Bosch BMA180 3-Axis Accelerometer Driver" 9 tristate "Bosch BMA180/BMA250 3-Axis Accelerometer Driver"
10 depends on I2C 10 depends on I2C
11 select IIO_BUFFER 11 select IIO_BUFFER
12 select IIO_TRIGGERED_BUFFER 12 select IIO_TRIGGERED_BUFFER
13 help 13 help
14 Say Y here if you want to build a driver for the Bosch BMA180 14 Say Y here if you want to build a driver for the Bosch BMA180 or
15 triaxial acceleration sensor. 15 BMA250 triaxial acceleration sensor.
16 16
17 To compile this driver as a module, choose M here: the 17 To compile this driver as a module, choose M here: the
18 module will be called bma180. 18 module will be called bma180.
diff --git a/drivers/iio/accel/bma180.c b/drivers/iio/accel/bma180.c
index a543cffb4ec3..6ef19641457c 100644
--- a/drivers/iio/accel/bma180.c
+++ b/drivers/iio/accel/bma180.c
@@ -3,9 +3,15 @@
3 * 3 *
4 * Copyright 2013 Oleksandr Kravchenko <x0199363@ti.com> 4 * Copyright 2013 Oleksandr Kravchenko <x0199363@ti.com>
5 * 5 *
6 * Support for BMA250 (c) Peter Meerwald <pmeerw@pmeerw.net>
7 *
6 * This file is subject to the terms and conditions of version 2 of 8 * This file is subject to the terms and conditions of version 2 of
7 * the GNU General Public License. See the file COPYING in the main 9 * the GNU General Public License. See the file COPYING in the main
8 * directory of this archive for more details. 10 * 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
9 */ 15 */
10 16
11#include <linux/module.h> 17#include <linux/module.h>
@@ -28,6 +34,7 @@
28 34
29enum { 35enum {
30 BMA180, 36 BMA180,
37 BMA250,
31}; 38};
32 39
33struct bma180_data; 40struct bma180_data;
@@ -39,6 +46,15 @@ struct bma180_part_info {
39 unsigned num_scales; 46 unsigned num_scales;
40 const int *bw_table; 47 const int *bw_table;
41 unsigned num_bw; 48 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
42 int (*chip_config)(struct bma180_data *data); 58 int (*chip_config)(struct bma180_data *data);
43 void (*chip_disable)(struct bma180_data *data); 59 void (*chip_disable)(struct bma180_data *data);
44}; 60};
@@ -77,13 +93,23 @@ struct bma180_part_info {
77#define BMA180_ID_REG_VAL 0x03 93#define BMA180_ID_REG_VAL 0x03
78 94
79/* Chip power modes */ 95/* Chip power modes */
80#define BMA180_LOW_NOISE 0x00
81#define BMA180_LOW_POWER 0x03 96#define BMA180_LOW_POWER 0x03
82 97
83/* Defaults values */ 98#define BMA250_RANGE_REG 0x0f
84#define BMA180_DEF_PMODE false 99#define BMA250_BW_REG 0x10
85#define BMA180_DEF_BW 20 100#define BMA250_POWER_REG 0x11
86#define BMA180_DEF_SCALE 2452 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 */
87 113
88struct bma180_data { 114struct bma180_data {
89 struct i2c_client *client; 115 struct i2c_client *client;
@@ -107,6 +133,10 @@ enum bma180_chan {
107static int bma180_bw_table[] = { 10, 20, 40, 75, 150, 300 }; /* Hz */ 133static int bma180_bw_table[] = { 10, 20, 40, 75, 150, 300 }; /* Hz */
108static int bma180_scale_table[] = { 1275, 1863, 2452, 3727, 4903, 9709, 19417 }; 134static int bma180_scale_table[] = { 1275, 1863, 2452, 3727, 4903, 9709, 19417 };
109 135
136static int bma250_bw_table[] = { 8, 16, 31, 63, 125, 250 }; /* Hz */
137static int bma250_scale_table[] = { 0, 0, 0, 38344, 0, 76590, 0, 0, 153180, 0,
138 0, 0, 306458 };
139
110static int bma180_get_data_reg(struct bma180_data *data, enum bma180_chan chan) 140static int bma180_get_data_reg(struct bma180_data *data, enum bma180_chan chan)
111{ 141{
112 int ret; 142 int ret;
@@ -145,7 +175,8 @@ static int bma180_set_bits(struct bma180_data *data, u8 reg, u8 mask, u8 val)
145 175
146static int bma180_reset_intr(struct bma180_data *data) 176static int bma180_reset_intr(struct bma180_data *data)
147{ 177{
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);
149 180
150 if (ret) 181 if (ret)
151 dev_err(&data->client->dev, "failed to reset interrupt\n"); 182 dev_err(&data->client->dev, "failed to reset interrupt\n");
@@ -155,10 +186,8 @@ static int bma180_reset_intr(struct bma180_data *data)
155 186
156static int bma180_set_new_data_intr_state(struct bma180_data *data, bool state) 187static int bma180_set_new_data_intr_state(struct bma180_data *data, bool state)
157{ 188{
158 u8 reg_val = state ? BMA180_NEW_DATA_INT : 0x00; 189 int ret = bma180_set_bits(data, data->part_info->int_enable_reg,
159 int ret = i2c_smbus_write_byte_data(data->client, BMA180_CTRL_REG3, 190 data->part_info->int_enable_mask, state);
160 reg_val);
161
162 if (ret) 191 if (ret)
163 goto err; 192 goto err;
164 ret = bma180_reset_intr(data); 193 ret = bma180_reset_intr(data);
@@ -175,7 +204,8 @@ err:
175 204
176static int bma180_set_sleep_state(struct bma180_data *data, bool state) 205static int bma180_set_sleep_state(struct bma180_data *data, bool state)
177{ 206{
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);
179 209
180 if (ret) { 210 if (ret) {
181 dev_err(&data->client->dev, 211 dev_err(&data->client->dev,
@@ -207,8 +237,8 @@ static int bma180_set_bw(struct bma180_data *data, int val)
207 237
208 for (i = 0; i < data->part_info->num_bw; ++i) { 238 for (i = 0; i < data->part_info->num_bw; ++i) {
209 if (data->part_info->bw_table[i] == val) { 239 if (data->part_info->bw_table[i] == val) {
210 ret = bma180_set_bits(data, 240 ret = bma180_set_bits(data, data->part_info->bw_reg,
211 BMA180_BW_TCS, BMA180_BW, i); 241 data->part_info->bw_mask, i);
212 if (ret) { 242 if (ret) {
213 dev_err(&data->client->dev, 243 dev_err(&data->client->dev,
214 "failed to set bandwidth\n"); 244 "failed to set bandwidth\n");
@@ -231,8 +261,8 @@ static int bma180_set_scale(struct bma180_data *data, int val)
231 261
232 for (i = 0; i < data->part_info->num_scales; ++i) 262 for (i = 0; i < data->part_info->num_scales; ++i)
233 if (data->part_info->scale_table[i] == val) { 263 if (data->part_info->scale_table[i] == val) {
234 ret = bma180_set_bits(data, 264 ret = bma180_set_bits(data, data->part_info->scale_reg,
235 BMA180_OFFSET_LSB1, BMA180_RANGE, i); 265 data->part_info->scale_mask, i);
236 if (ret) { 266 if (ret) {
237 dev_err(&data->client->dev, 267 dev_err(&data->client->dev,
238 "failed to set scale\n"); 268 "failed to set scale\n");
@@ -247,9 +277,9 @@ static int bma180_set_scale(struct bma180_data *data, int val)
247 277
248static int bma180_set_pmode(struct bma180_data *data, bool mode) 278static int bma180_set_pmode(struct bma180_data *data, bool mode)
249{ 279{
250 u8 reg_val = mode ? BMA180_LOW_POWER : BMA180_LOW_NOISE; 280 u8 reg_val = mode ? data->part_info->lowpower_val : 0;
251 int ret = bma180_set_bits(data, BMA180_TCO_Z, BMA180_MODE_CONFIG, 281 int ret = bma180_set_bits(data, data->part_info->power_reg,
252 reg_val); 282 data->part_info->power_mask, reg_val);
253 283
254 if (ret) { 284 if (ret) {
255 dev_err(&data->client->dev, "failed to set power mode\n"); 285 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)
263static int bma180_soft_reset(struct bma180_data *data) 293static int bma180_soft_reset(struct bma180_data *data)
264{ 294{
265 int ret = i2c_smbus_write_byte_data(data->client, 295 int ret = i2c_smbus_write_byte_data(data->client,
266 BMA180_RESET, BMA180_RESET_VAL); 296 data->part_info->softreset_reg, BMA180_RESET_VAL);
267 297
268 if (ret) 298 if (ret)
269 dev_err(&data->client->dev, "failed to reset the chip\n"); 299 dev_err(&data->client->dev, "failed to reset the chip\n");
@@ -290,7 +320,11 @@ static int bma180_chip_init(struct bma180_data *data)
290 */ 320 */
291 msleep(20); 321 msleep(20);
292 322
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);
294} 328}
295 329
296static int bma180_chip_config(struct bma180_data *data) 330static int bma180_chip_config(struct bma180_data *data)
@@ -305,19 +339,37 @@ static int bma180_chip_config(struct bma180_data *data)
305 ret = bma180_set_ee_writing_state(data, true); 339 ret = bma180_set_ee_writing_state(data, true);
306 if (ret) 340 if (ret)
307 goto err; 341 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);
309 if (ret) 343 if (ret)
310 goto err; 344 goto err;
311 ret = bma180_set_bits(data, BMA180_OFFSET_LSB1, BMA180_SMP_SKIP, 1); 345 ret = bma180_set_bw(data, 20); /* 20 Hz */
312 if (ret) 346 if (ret)
313 goto err; 347 goto err;
314 ret = bma180_set_pmode(data, BMA180_DEF_PMODE); 348 ret = bma180_set_scale(data, 2452); /* 2 G */
315 if (ret) 349 if (ret)
316 goto err; 350 goto err;
317 ret = bma180_set_bw(data, BMA180_DEF_BW); 351
352 return 0;
353
354err:
355 dev_err(&data->client->dev, "failed to config the chip\n");
356 return ret;
357}
358
359static 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 */
318 if (ret) 366 if (ret)
319 goto err; 367 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);
321 if (ret) 373 if (ret)
322 goto err; 374 goto err;
323 375
@@ -343,6 +395,19 @@ err:
343 dev_err(&data->client->dev, "failed to disable the chip\n"); 395 dev_err(&data->client->dev, "failed to disable the chip\n");
344} 396}
345 397
398static 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
407err:
408 dev_err(&data->client->dev, "failed to disable the chip\n");
409}
410
346static ssize_t bma180_show_avail(char *buf, const int *vals, unsigned n, 411static ssize_t bma180_show_avail(char *buf, const int *vals, unsigned n,
347 bool micros) 412 bool micros)
348{ 413{
@@ -545,14 +610,43 @@ static const struct iio_chan_spec bma180_channels[] = {
545 IIO_CHAN_SOFT_TIMESTAMP(4), 610 IIO_CHAN_SOFT_TIMESTAMP(4),
546}; 611};
547 612
613static 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
548static const struct bma180_part_info bma180_part_info[] = { 621static const struct bma180_part_info bma180_part_info[] = {
549 [BMA180] = { 622 [BMA180] = {
550 bma180_channels, ARRAY_SIZE(bma180_channels), 623 bma180_channels, ARRAY_SIZE(bma180_channels),
551 bma180_scale_table, ARRAY_SIZE(bma180_scale_table), 624 bma180_scale_table, ARRAY_SIZE(bma180_scale_table),
552 bma180_bw_table, ARRAY_SIZE(bma180_bw_table), 625 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,
553 bma180_chip_config, 633 bma180_chip_config,
554 bma180_chip_disable, 634 bma180_chip_disable,
555 }, 635 },
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 },
556}; 650};
557 651
558static irqreturn_t bma180_trigger_handler(int irq, void *p) 652static irqreturn_t bma180_trigger_handler(int irq, void *p)
@@ -628,11 +722,10 @@ static int bma180_probe(struct i2c_client *client,
628 goto err_chip_disable; 722 goto err_chip_disable;
629 723
630 mutex_init(&data->mutex); 724 mutex_init(&data->mutex);
631
632 indio_dev->dev.parent = &client->dev; 725 indio_dev->dev.parent = &client->dev;
633 indio_dev->channels = data->part_info->channels; 726 indio_dev->channels = data->part_info->channels;
634 indio_dev->num_channels = data->part_info->num_channels; 727 indio_dev->num_channels = data->part_info->num_channels;
635 indio_dev->name = BMA180_DRV_NAME; 728 indio_dev->name = id->name;
636 indio_dev->modes = INDIO_DIRECT_MODE; 729 indio_dev->modes = INDIO_DIRECT_MODE;
637 indio_dev->info = &bma180_info; 730 indio_dev->info = &bma180_info;
638 731
@@ -646,7 +739,7 @@ static int bma180_probe(struct i2c_client *client,
646 739
647 ret = devm_request_irq(&client->dev, client->irq, 740 ret = devm_request_irq(&client->dev, client->irq,
648 iio_trigger_generic_data_rdy_poll, IRQF_TRIGGER_RISING, 741 iio_trigger_generic_data_rdy_poll, IRQF_TRIGGER_RISING,
649 BMA180_IRQ_NAME, data->trig); 742 "bma180_event", data->trig);
650 if (ret) { 743 if (ret) {
651 dev_err(&client->dev, "unable to request IRQ\n"); 744 dev_err(&client->dev, "unable to request IRQ\n");
652 goto err_trigger_free; 745 goto err_trigger_free;
@@ -743,7 +836,8 @@ static SIMPLE_DEV_PM_OPS(bma180_pm_ops, bma180_suspend, bma180_resume);
743#endif 836#endif
744 837
745static struct i2c_device_id bma180_ids[] = { 838static struct i2c_device_id bma180_ids[] = {
746 { BMA180_DRV_NAME, BMA180 }, 839 { "bma180", BMA180 },
840 { "bma250", BMA250 },
747 { } 841 { }
748}; 842};
749 843
@@ -751,7 +845,7 @@ MODULE_DEVICE_TABLE(i2c, bma180_ids);
751 845
752static struct i2c_driver bma180_driver = { 846static struct i2c_driver bma180_driver = {
753 .driver = { 847 .driver = {
754 .name = BMA180_DRV_NAME, 848 .name = "bma180",
755 .owner = THIS_MODULE, 849 .owner = THIS_MODULE,
756 .pm = BMA180_PM_OPS, 850 .pm = BMA180_PM_OPS,
757 }, 851 },
@@ -764,5 +858,5 @@ module_i2c_driver(bma180_driver);
764 858
765MODULE_AUTHOR("Kravchenko Oleksandr <x0199363@ti.com>"); 859MODULE_AUTHOR("Kravchenko Oleksandr <x0199363@ti.com>");
766MODULE_AUTHOR("Texas Instruments, Inc."); 860MODULE_AUTHOR("Texas Instruments, Inc.");
767MODULE_DESCRIPTION("Bosch BMA180 triaxial acceleration sensor"); 861MODULE_DESCRIPTION("Bosch BMA180/BMA250 triaxial acceleration sensor");
768MODULE_LICENSE("GPL"); 862MODULE_LICENSE("GPL");