2 * bma180.c - IIO driver for Bosch BMA180 triaxial acceleration sensor
4 * Copyright 2013 Oleksandr Kravchenko <x0199363@ti.com>
6 * 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
8 * directory of this archive for more details.
11 #include <linux/module.h>
12 #include <linux/i2c.h>
13 #include <linux/interrupt.h>
14 #include <linux/delay.h>
16 #include <linux/bitops.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
21 #include <linux/iio/buffer.h>
22 #include <linux/iio/trigger.h>
23 #include <linux/iio/trigger_consumer.h>
24 #include <linux/iio/triggered_buffer.h>
26 #define BMA180_DRV_NAME "bma180"
27 #define BMA180_IRQ_NAME "bma180_event"
30 #define BMA180_CHIP_ID 0x00 /* Need to distinguish BMA180 from other */
31 #define BMA180_ACC_X_LSB 0x02 /* First of 6 registers of accel data */
32 #define BMA180_CTRL_REG0 0x0d
33 #define BMA180_RESET 0x10
34 #define BMA180_BW_TCS 0x20
35 #define BMA180_CTRL_REG3 0x21
36 #define BMA180_TCO_Z 0x30
37 #define BMA180_OFFSET_LSB1 0x35
39 /* BMA180_CTRL_REG0 bits */
40 #define BMA180_DIS_WAKE_UP BIT(0) /* Disable wake up mode */
41 #define BMA180_SLEEP BIT(1) /* 1 - chip will sleep */
42 #define BMA180_EE_W BIT(4) /* Unlock writing to addr from 0x20 */
43 #define BMA180_RESET_INT BIT(6) /* Reset pending interrupts */
45 /* BMA180_CTRL_REG3 bits */
46 #define BMA180_NEW_DATA_INT BIT(1) /* Intr every new accel data is ready */
48 /* BMA180_OFFSET_LSB1 skipping mode bit */
49 #define BMA180_SMP_SKIP BIT(0)
51 /* Bit masks for registers bit fields */
52 #define BMA180_RANGE 0x0e /* Range of measured accel values*/
53 #define BMA180_BW 0xf0 /* Accel bandwidth */
54 #define BMA180_MODE_CONFIG 0x03 /* Config operation modes */
56 /* We have to write this value in reset register to do soft reset */
57 #define BMA180_RESET_VAL 0xb6
59 #define BMA_180_ID_REG_VAL 0x03
61 /* Chip power modes */
62 #define BMA180_LOW_NOISE 0x00
63 #define BMA180_LOW_POWER 0x03
65 #define BMA180_LOW_NOISE_STR "low_noise"
66 #define BMA180_LOW_POWER_STR "low_power"
69 #define BMA180_DEF_PMODE 0
70 #define BMA180_DEF_BW 20
71 #define BMA180_DEF_SCALE 250
73 /* Available values for sysfs */
74 #define BMA180_FLP_FREQ_AVAILABLE \
76 #define BMA180_SCALE_AVAILABLE \
77 "0.000130 0.000190 0.000250 0.000380 0.000500 0.000990 0.001980"
80 struct i2c_client
*client
;
81 struct iio_trigger
*trig
;
96 static int bw_table
[] = { 10, 20, 40, 75, 150, 300 }; /* Hz */
97 static int scale_table
[] = { 130, 190, 250, 380, 500, 990, 1980 };
99 static int bma180_get_acc_reg(struct bma180_data
*data
, enum bma180_axis axis
)
101 u8 reg
= BMA180_ACC_X_LSB
+ axis
* 2;
104 if (data
->sleep_state
)
107 ret
= i2c_smbus_read_word_data(data
->client
, reg
);
109 dev_err(&data
->client
->dev
,
110 "failed to read accel_%c registers\n", 'x' + axis
);
115 static int bma180_set_bits(struct bma180_data
*data
, u8 reg
, u8 mask
, u8 val
)
117 int ret
= i2c_smbus_read_byte_data(data
->client
, reg
);
118 u8 reg_val
= (ret
& ~mask
) | (val
<< (ffs(mask
) - 1));
123 return i2c_smbus_write_byte_data(data
->client
, reg
, reg_val
);
126 static int bma180_reset_intr(struct bma180_data
*data
)
128 int ret
= bma180_set_bits(data
, BMA180_CTRL_REG0
, BMA180_RESET_INT
, 1);
131 dev_err(&data
->client
->dev
, "failed to reset interrupt\n");
136 static int bma180_set_new_data_intr_state(struct bma180_data
*data
, int state
)
138 u8 reg_val
= state
? BMA180_NEW_DATA_INT
: 0x00;
139 int ret
= i2c_smbus_write_byte_data(data
->client
, BMA180_CTRL_REG3
,
144 ret
= bma180_reset_intr(data
);
151 dev_err(&data
->client
->dev
,
152 "failed to set new data interrupt state %d\n", state
);
156 static int bma180_set_sleep_state(struct bma180_data
*data
, int state
)
158 int ret
= bma180_set_bits(data
, BMA180_CTRL_REG0
, BMA180_SLEEP
, state
);
161 dev_err(&data
->client
->dev
,
162 "failed to set sleep state %d\n", state
);
165 data
->sleep_state
= state
;
170 static int bma180_set_ee_writing_state(struct bma180_data
*data
, int state
)
172 int ret
= bma180_set_bits(data
, BMA180_CTRL_REG0
, BMA180_EE_W
, state
);
175 dev_err(&data
->client
->dev
,
176 "failed to set ee writing state %d\n", state
);
181 static int bma180_set_bw(struct bma180_data
*data
, int val
)
185 if (data
->sleep_state
)
188 for (i
= 0; i
< ARRAY_SIZE(bw_table
); ++i
) {
189 if (bw_table
[i
] == val
) {
190 ret
= bma180_set_bits(data
,
191 BMA180_BW_TCS
, BMA180_BW
, i
);
193 dev_err(&data
->client
->dev
,
194 "failed to set bandwidth\n");
205 static int bma180_set_scale(struct bma180_data
*data
, int val
)
209 if (data
->sleep_state
)
212 for (i
= 0; i
< ARRAY_SIZE(scale_table
); ++i
)
213 if (scale_table
[i
] == val
) {
214 ret
= bma180_set_bits(data
,
215 BMA180_OFFSET_LSB1
, BMA180_RANGE
, i
);
217 dev_err(&data
->client
->dev
,
218 "failed to set scale\n");
228 static int bma180_set_pmode(struct bma180_data
*data
, int mode
)
230 u8 reg_val
= mode
? BMA180_LOW_POWER
: BMA180_LOW_NOISE
;
231 int ret
= bma180_set_bits(data
, BMA180_TCO_Z
, BMA180_MODE_CONFIG
,
235 dev_err(&data
->client
->dev
, "failed to set power mode\n");
243 static int bma180_soft_reset(struct bma180_data
*data
)
245 int ret
= i2c_smbus_write_byte_data(data
->client
,
246 BMA180_RESET
, BMA180_RESET_VAL
);
249 dev_err(&data
->client
->dev
, "failed to reset the chip\n");
254 static int bma180_chip_init(struct bma180_data
*data
)
256 /* Try to read chip_id register. It must return 0x03. */
257 int ret
= i2c_smbus_read_byte_data(data
->client
, BMA180_CHIP_ID
);
261 if (ret
!= BMA_180_ID_REG_VAL
) {
266 ret
= bma180_soft_reset(data
);
270 * No serial transaction should occur within minimum 10 us
271 * after soft_reset command
275 ret
= bma180_set_bits(data
, BMA180_CTRL_REG0
, BMA180_DIS_WAKE_UP
, 1);
278 ret
= bma180_set_ee_writing_state(data
, 1);
281 ret
= bma180_set_new_data_intr_state(data
, 0);
284 ret
= bma180_set_bits(data
, BMA180_OFFSET_LSB1
, BMA180_SMP_SKIP
, 1);
287 ret
= bma180_set_pmode(data
, BMA180_DEF_PMODE
);
290 ret
= bma180_set_bw(data
, BMA180_DEF_BW
);
293 ret
= bma180_set_scale(data
, BMA180_DEF_SCALE
);
300 dev_err(&data
->client
->dev
, "failed to init the chip\n");
304 static void bma180_chip_disable(struct bma180_data
*data
)
306 if (bma180_set_new_data_intr_state(data
, 0))
308 if (bma180_set_ee_writing_state(data
, 0))
310 if (bma180_set_sleep_state(data
, 1))
316 dev_err(&data
->client
->dev
, "failed to disable the chip\n");
319 static IIO_CONST_ATTR(in_accel_filter_low_pass_3db_frequency_available
,
320 BMA180_FLP_FREQ_AVAILABLE
);
321 static IIO_CONST_ATTR(in_accel_scale_available
, BMA180_SCALE_AVAILABLE
);
323 static struct attribute
*bma180_attributes
[] = {
324 &iio_const_attr_in_accel_filter_low_pass_3db_frequency_available
.dev_attr
.attr
,
325 &iio_const_attr_in_accel_scale_available
.dev_attr
.attr
,
329 static const struct attribute_group bma180_attrs_group
= {
330 .attrs
= bma180_attributes
,
333 static int bma180_read_raw(struct iio_dev
*indio_dev
,
334 struct iio_chan_spec
const *chan
, int *val
, int *val2
,
337 struct bma180_data
*data
= iio_priv(indio_dev
);
341 case IIO_CHAN_INFO_RAW
:
342 mutex_lock(&data
->mutex
);
343 if (iio_buffer_enabled(indio_dev
))
346 ret
= bma180_get_acc_reg(data
, chan
->scan_index
);
347 mutex_unlock(&data
->mutex
);
350 *val
= (s16
)ret
>> chan
->scan_type
.shift
;
352 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
:
355 case IIO_CHAN_INFO_SCALE
:
358 return IIO_VAL_INT_PLUS_MICRO
;
364 static int bma180_write_raw(struct iio_dev
*indio_dev
,
365 struct iio_chan_spec
const *chan
, int val
, int val2
, long mask
)
367 struct bma180_data
*data
= iio_priv(indio_dev
);
371 case IIO_CHAN_INFO_SCALE
:
374 mutex_lock(&data
->mutex
);
375 ret
= bma180_set_scale(data
, val2
);
376 mutex_unlock(&data
->mutex
);
378 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
:
379 mutex_lock(&data
->mutex
);
380 ret
= bma180_set_bw(data
, val
);
381 mutex_unlock(&data
->mutex
);
388 static int bma180_update_scan_mode(struct iio_dev
*indio_dev
,
389 const unsigned long *scan_mask
)
391 struct bma180_data
*data
= iio_priv(indio_dev
);
394 devm_kfree(&indio_dev
->dev
, data
->buff
);
395 data
->buff
= devm_kzalloc(&indio_dev
->dev
,
396 indio_dev
->scan_bytes
, GFP_KERNEL
);
403 static const struct iio_info bma180_info
= {
404 .attrs
= &bma180_attrs_group
,
405 .read_raw
= bma180_read_raw
,
406 .write_raw
= bma180_write_raw
,
407 .update_scan_mode
= bma180_update_scan_mode
,
408 .driver_module
= THIS_MODULE
,
411 static const char * const bma180_power_modes
[] = {
412 BMA180_LOW_NOISE_STR
,
413 BMA180_LOW_POWER_STR
,
416 static int bma180_get_power_mode(struct iio_dev
*indio_dev
,
417 const struct iio_chan_spec
*chan
)
419 struct bma180_data
*data
= iio_priv(indio_dev
);
424 static int bma180_set_power_mode(struct iio_dev
*indio_dev
,
425 const struct iio_chan_spec
*chan
, unsigned int mode
)
427 struct bma180_data
*data
= iio_priv(indio_dev
);
430 mutex_lock(&data
->mutex
);
431 ret
= bma180_set_pmode(data
, mode
);
432 mutex_unlock(&data
->mutex
);
437 static const struct iio_enum bma180_power_mode_enum
= {
438 .items
= bma180_power_modes
,
439 .num_items
= ARRAY_SIZE(bma180_power_modes
),
440 .get
= bma180_get_power_mode
,
441 .set
= bma180_set_power_mode
,
444 static const struct iio_chan_spec_ext_info bma180_ext_info
[] = {
445 IIO_ENUM("power_mode", true, &bma180_power_mode_enum
),
446 IIO_ENUM_AVAILABLE("power_mode", &bma180_power_mode_enum
),
450 #define BMA180_CHANNEL(_axis) { \
453 .channel2 = IIO_MOD_##_axis, \
454 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
455 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
456 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
457 .scan_index = AXIS_##_axis, \
464 .ext_info = bma180_ext_info, \
467 static const struct iio_chan_spec bma180_channels
[] = {
471 IIO_CHAN_SOFT_TIMESTAMP(3),
474 static irqreturn_t
bma180_trigger_handler(int irq
, void *p
)
476 struct iio_poll_func
*pf
= p
;
477 struct iio_dev
*indio_dev
= pf
->indio_dev
;
478 struct bma180_data
*data
= iio_priv(indio_dev
);
479 int64_t time_ns
= iio_get_time_ns();
482 mutex_lock(&data
->mutex
);
484 for_each_set_bit(bit
, indio_dev
->buffer
->scan_mask
,
485 indio_dev
->masklength
) {
486 ret
= bma180_get_acc_reg(data
, bit
);
488 mutex_unlock(&data
->mutex
);
491 ((s16
*)data
->buff
)[i
++] = ret
;
493 mutex_unlock(&data
->mutex
);
495 iio_push_to_buffers_with_timestamp(indio_dev
, data
->buff
, time_ns
);
497 iio_trigger_notify_done(indio_dev
->trig
);
502 static int bma180_data_rdy_trigger_set_state(struct iio_trigger
*trig
,
505 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
506 struct bma180_data
*data
= iio_priv(indio_dev
);
508 return bma180_set_new_data_intr_state(data
, state
);
511 static int bma180_trig_try_reen(struct iio_trigger
*trig
)
513 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
514 struct bma180_data
*data
= iio_priv(indio_dev
);
516 return bma180_reset_intr(data
);
519 static const struct iio_trigger_ops bma180_trigger_ops
= {
520 .set_trigger_state
= bma180_data_rdy_trigger_set_state
,
521 .try_reenable
= bma180_trig_try_reen
,
522 .owner
= THIS_MODULE
,
525 static int bma180_probe(struct i2c_client
*client
,
526 const struct i2c_device_id
*id
)
528 struct bma180_data
*data
;
529 struct iio_dev
*indio_dev
;
530 struct iio_trigger
*trig
;
533 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
537 data
= iio_priv(indio_dev
);
538 i2c_set_clientdata(client
, indio_dev
);
539 data
->client
= client
;
541 ret
= bma180_chip_init(data
);
543 goto err_chip_disable
;
545 mutex_init(&data
->mutex
);
547 indio_dev
->dev
.parent
= &client
->dev
;
548 indio_dev
->channels
= bma180_channels
;
549 indio_dev
->num_channels
= ARRAY_SIZE(bma180_channels
);
550 indio_dev
->name
= BMA180_DRV_NAME
;
551 indio_dev
->modes
= INDIO_DIRECT_MODE
;
552 indio_dev
->info
= &bma180_info
;
554 trig
= iio_trigger_alloc("%s-dev%d", indio_dev
->name
, indio_dev
->id
);
557 goto err_chip_disable
;
560 ret
= devm_request_irq(&client
->dev
, client
->irq
,
561 iio_trigger_generic_data_rdy_poll
,
562 IRQF_TRIGGER_RISING
, BMA180_IRQ_NAME
, trig
);
564 dev_err(&client
->dev
, "unable to request IRQ\n");
565 goto err_trigger_free
;
568 trig
->dev
.parent
= &client
->dev
;
569 trig
->ops
= &bma180_trigger_ops
;
570 iio_trigger_set_drvdata(trig
, indio_dev
);
572 indio_dev
->trig
= trig
;
574 ret
= iio_trigger_register(trig
);
576 goto err_trigger_free
;
578 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
579 bma180_trigger_handler
, NULL
);
581 dev_err(&client
->dev
, "unable to setup iio triggered buffer\n");
582 goto err_trigger_unregister
;
585 ret
= iio_device_register(indio_dev
);
587 dev_err(&client
->dev
, "unable to register iio device\n");
588 goto err_buffer_cleanup
;
594 iio_triggered_buffer_cleanup(indio_dev
);
595 err_trigger_unregister
:
596 iio_trigger_unregister(trig
);
598 iio_trigger_free(trig
);
600 bma180_chip_disable(data
);
605 static int bma180_remove(struct i2c_client
*client
)
607 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
608 struct bma180_data
*data
= iio_priv(indio_dev
);
610 iio_device_unregister(indio_dev
);
611 iio_triggered_buffer_cleanup(indio_dev
);
612 iio_trigger_unregister(data
->trig
);
613 iio_trigger_free(data
->trig
);
615 mutex_lock(&data
->mutex
);
616 bma180_chip_disable(data
);
617 mutex_unlock(&data
->mutex
);
622 #ifdef CONFIG_PM_SLEEP
623 static int bma180_suspend(struct device
*dev
)
625 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
626 struct bma180_data
*data
= iio_priv(indio_dev
);
629 mutex_lock(&data
->mutex
);
630 ret
= bma180_set_sleep_state(data
, 1);
631 mutex_unlock(&data
->mutex
);
636 static int bma180_resume(struct device
*dev
)
638 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
639 struct bma180_data
*data
= iio_priv(indio_dev
);
642 mutex_lock(&data
->mutex
);
643 ret
= bma180_set_sleep_state(data
, 0);
644 mutex_unlock(&data
->mutex
);
649 static SIMPLE_DEV_PM_OPS(bma180_pm_ops
, bma180_suspend
, bma180_resume
);
650 #define BMA180_PM_OPS (&bma180_pm_ops)
652 #define BMA180_PM_OPS NULL
655 static struct i2c_device_id bma180_id
[] = {
656 { BMA180_DRV_NAME
, 0 },
660 MODULE_DEVICE_TABLE(i2c
, bma180_id
);
662 static struct i2c_driver bma180_driver
= {
664 .name
= BMA180_DRV_NAME
,
665 .owner
= THIS_MODULE
,
668 .probe
= bma180_probe
,
669 .remove
= bma180_remove
,
670 .id_table
= bma180_id
,
673 module_i2c_driver(bma180_driver
);
675 MODULE_AUTHOR("Kravchenko Oleksandr <x0199363@ti.com>");
676 MODULE_AUTHOR("Texas Instruments, Inc.");
677 MODULE_DESCRIPTION("Bosch BMA180 triaxial acceleration sensor");
678 MODULE_LICENSE("GPL");