1 /* 2 * bma180.c - IIO driver for Bosch BMA180 triaxial acceleration sensor 3 * 4 * Copyright 2013 Oleksandr Kravchenko <x0199363@ti.com> 5 * 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. 9 */ 10 11 #include <linux/module.h> 12 #include <linux/i2c.h> 13 #include <linux/interrupt.h> 14 #include <linux/delay.h> 15 #include <linux/of.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> 25 26 #define BMA180_DRV_NAME "bma180" 27 #define BMA180_IRQ_NAME "bma180_event" 28 29 /* Register set */ 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 38 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 */ 44 45 /* BMA180_CTRL_REG3 bits */ 46 #define BMA180_NEW_DATA_INT BIT(1) /* Intr every new accel data is ready */ 47 48 /* BMA180_OFFSET_LSB1 skipping mode bit */ 49 #define BMA180_SMP_SKIP BIT(0) 50 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 */ 55 56 /* We have to write this value in reset register to do soft reset */ 57 #define BMA180_RESET_VAL 0xb6 58 59 #define BMA180_ID_REG_VAL 0x03 60 61 /* Chip power modes */ 62 #define BMA180_LOW_NOISE 0x00 63 #define BMA180_LOW_POWER 0x03 64 65 #define BMA180_LOW_NOISE_STR "low_noise" 66 #define BMA180_LOW_POWER_STR "low_power" 67 68 /* Defaults values */ 69 #define BMA180_DEF_PMODE 0 70 #define BMA180_DEF_BW 20 71 #define BMA180_DEF_SCALE 2452 72 73 /* Available values for sysfs */ 74 #define BMA180_FLP_FREQ_AVAILABLE \ 75 "10 20 40 75 150 300" 76 #define BMA180_SCALE_AVAILABLE \ 77 "0.001275 0.001863 0.002452 0.003727 0.004903 0.009709 0.019417" 78 79 struct bma180_data { 80 struct i2c_client *client; 81 struct iio_trigger *trig; 82 struct mutex mutex; 83 int sleep_state; 84 int scale; 85 int bw; 86 int pmode; 87 char *buff; 88 }; 89 90 enum bma180_axis { 91 AXIS_X, 92 AXIS_Y, 93 AXIS_Z, 94 }; 95 96 static int bma180_bw_table[] = { 10, 20, 40, 75, 150, 300 }; /* Hz */ 97 static int bma180_scale_table[] = { 1275, 1863, 2452, 3727, 4903, 9709, 19417 }; 98 99 static int bma180_get_acc_reg(struct bma180_data *data, enum bma180_axis axis) 100 { 101 u8 reg = BMA180_ACC_X_LSB + axis * 2; 102 int ret; 103 104 if (data->sleep_state) 105 return -EBUSY; 106 107 ret = i2c_smbus_read_word_data(data->client, reg); 108 if (ret < 0) 109 dev_err(&data->client->dev, 110 "failed to read accel_%c register\n", 'x' + axis); 111 112 return ret; 113 } 114 115 static int bma180_set_bits(struct bma180_data *data, u8 reg, u8 mask, u8 val) 116 { 117 int ret = i2c_smbus_read_byte_data(data->client, reg); 118 u8 reg_val = (ret & ~mask) | (val << (ffs(mask) - 1)); 119 120 if (ret < 0) 121 return ret; 122 123 return i2c_smbus_write_byte_data(data->client, reg, reg_val); 124 } 125 126 static int bma180_reset_intr(struct bma180_data *data) 127 { 128 int ret = bma180_set_bits(data, BMA180_CTRL_REG0, BMA180_RESET_INT, 1); 129 130 if (ret) 131 dev_err(&data->client->dev, "failed to reset interrupt\n"); 132 133 return ret; 134 } 135 136 static int bma180_set_new_data_intr_state(struct bma180_data *data, int state) 137 { 138 u8 reg_val = state ? BMA180_NEW_DATA_INT : 0x00; 139 int ret = i2c_smbus_write_byte_data(data->client, BMA180_CTRL_REG3, 140 reg_val); 141 142 if (ret) 143 goto err; 144 ret = bma180_reset_intr(data); 145 if (ret) 146 goto err; 147 148 return 0; 149 150 err: 151 dev_err(&data->client->dev, 152 "failed to set new data interrupt state %d\n", state); 153 return ret; 154 } 155 156 static int bma180_set_sleep_state(struct bma180_data *data, int state) 157 { 158 int ret = bma180_set_bits(data, BMA180_CTRL_REG0, BMA180_SLEEP, state); 159 160 if (ret) { 161 dev_err(&data->client->dev, 162 "failed to set sleep state %d\n", state); 163 return ret; 164 } 165 data->sleep_state = state; 166 167 return 0; 168 } 169 170 static int bma180_set_ee_writing_state(struct bma180_data *data, int state) 171 { 172 int ret = bma180_set_bits(data, BMA180_CTRL_REG0, BMA180_EE_W, state); 173 174 if (ret) 175 dev_err(&data->client->dev, 176 "failed to set ee writing state %d\n", state); 177 178 return ret; 179 } 180 181 static int bma180_set_bw(struct bma180_data *data, int val) 182 { 183 int ret, i; 184 185 if (data->sleep_state) 186 return -EBUSY; 187 188 for (i = 0; i < ARRAY_SIZE(bma180_bw_table); ++i) { 189 if (bma180_bw_table[i] == val) { 190 ret = bma180_set_bits(data, 191 BMA180_BW_TCS, BMA180_BW, i); 192 if (ret) { 193 dev_err(&data->client->dev, 194 "failed to set bandwidth\n"); 195 return ret; 196 } 197 data->bw = val; 198 return 0; 199 } 200 } 201 202 return -EINVAL; 203 } 204 205 static int bma180_set_scale(struct bma180_data *data, int val) 206 { 207 int ret, i; 208 209 if (data->sleep_state) 210 return -EBUSY; 211 212 for (i = 0; i < ARRAY_SIZE(bma180_scale_table); ++i) 213 if (bma180_scale_table[i] == val) { 214 ret = bma180_set_bits(data, 215 BMA180_OFFSET_LSB1, BMA180_RANGE, i); 216 if (ret) { 217 dev_err(&data->client->dev, 218 "failed to set scale\n"); 219 return ret; 220 } 221 data->scale = val; 222 return 0; 223 } 224 225 return -EINVAL; 226 } 227 228 static int bma180_set_pmode(struct bma180_data *data, int mode) 229 { 230 u8 reg_val = mode ? BMA180_LOW_POWER : BMA180_LOW_NOISE; 231 int ret = bma180_set_bits(data, BMA180_TCO_Z, BMA180_MODE_CONFIG, 232 reg_val); 233 234 if (ret) { 235 dev_err(&data->client->dev, "failed to set power mode\n"); 236 return ret; 237 } 238 data->pmode = mode; 239 240 return 0; 241 } 242 243 static int bma180_soft_reset(struct bma180_data *data) 244 { 245 int ret = i2c_smbus_write_byte_data(data->client, 246 BMA180_RESET, BMA180_RESET_VAL); 247 248 if (ret) 249 dev_err(&data->client->dev, "failed to reset the chip\n"); 250 251 return ret; 252 } 253 254 static int bma180_chip_init(struct bma180_data *data) 255 { 256 /* Try to read chip_id register. It must return 0x03. */ 257 int ret = i2c_smbus_read_byte_data(data->client, BMA180_CHIP_ID); 258 259 if (ret < 0) 260 goto err; 261 if (ret != BMA180_ID_REG_VAL) { 262 ret = -ENODEV; 263 goto err; 264 } 265 266 ret = bma180_soft_reset(data); 267 if (ret) 268 goto err; 269 /* 270 * No serial transaction should occur within minimum 10 us 271 * after soft_reset command 272 */ 273 msleep(20); 274 275 ret = bma180_set_bits(data, BMA180_CTRL_REG0, BMA180_DIS_WAKE_UP, 1); 276 if (ret) 277 goto err; 278 ret = bma180_set_ee_writing_state(data, 1); 279 if (ret) 280 goto err; 281 ret = bma180_set_new_data_intr_state(data, 0); 282 if (ret) 283 goto err; 284 ret = bma180_set_bits(data, BMA180_OFFSET_LSB1, BMA180_SMP_SKIP, 1); 285 if (ret) 286 goto err; 287 ret = bma180_set_pmode(data, BMA180_DEF_PMODE); 288 if (ret) 289 goto err; 290 ret = bma180_set_bw(data, BMA180_DEF_BW); 291 if (ret) 292 goto err; 293 ret = bma180_set_scale(data, BMA180_DEF_SCALE); 294 if (ret) 295 goto err; 296 297 return 0; 298 299 err: 300 dev_err(&data->client->dev, "failed to init the chip\n"); 301 return ret; 302 } 303 304 static void bma180_chip_disable(struct bma180_data *data) 305 { 306 if (bma180_set_new_data_intr_state(data, 0)) 307 goto err; 308 if (bma180_set_ee_writing_state(data, 0)) 309 goto err; 310 if (bma180_set_sleep_state(data, 1)) 311 goto err; 312 313 return; 314 315 err: 316 dev_err(&data->client->dev, "failed to disable the chip\n"); 317 } 318 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); 322 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, 326 NULL, 327 }; 328 329 static const struct attribute_group bma180_attrs_group = { 330 .attrs = bma180_attributes, 331 }; 332 333 static int bma180_read_raw(struct iio_dev *indio_dev, 334 struct iio_chan_spec const *chan, int *val, int *val2, 335 long mask) 336 { 337 struct bma180_data *data = iio_priv(indio_dev); 338 int ret; 339 340 switch (mask) { 341 case IIO_CHAN_INFO_RAW: 342 mutex_lock(&data->mutex); 343 if (iio_buffer_enabled(indio_dev)) 344 ret = -EBUSY; 345 else 346 ret = bma180_get_acc_reg(data, chan->scan_index); 347 mutex_unlock(&data->mutex); 348 if (ret < 0) 349 return ret; 350 *val = (s16)ret >> chan->scan_type.shift; 351 return IIO_VAL_INT; 352 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: 353 *val = data->bw; 354 return IIO_VAL_INT; 355 case IIO_CHAN_INFO_SCALE: 356 *val = 0; 357 *val2 = data->scale; 358 return IIO_VAL_INT_PLUS_MICRO; 359 default: 360 return -EINVAL; 361 } 362 } 363 364 static int bma180_write_raw(struct iio_dev *indio_dev, 365 struct iio_chan_spec const *chan, int val, int val2, long mask) 366 { 367 struct bma180_data *data = iio_priv(indio_dev); 368 int ret; 369 370 switch (mask) { 371 case IIO_CHAN_INFO_SCALE: 372 if (val) 373 return -EINVAL; 374 mutex_lock(&data->mutex); 375 ret = bma180_set_scale(data, val2); 376 mutex_unlock(&data->mutex); 377 return ret; 378 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: 379 if (val2) 380 return -EINVAL; 381 mutex_lock(&data->mutex); 382 ret = bma180_set_bw(data, val); 383 mutex_unlock(&data->mutex); 384 return ret; 385 default: 386 return -EINVAL; 387 } 388 } 389 390 static int bma180_update_scan_mode(struct iio_dev *indio_dev, 391 const unsigned long *scan_mask) 392 { 393 struct bma180_data *data = iio_priv(indio_dev); 394 395 if (data->buff) 396 devm_kfree(&indio_dev->dev, data->buff); 397 data->buff = devm_kzalloc(&indio_dev->dev, 398 indio_dev->scan_bytes, GFP_KERNEL); 399 if (!data->buff) 400 return -ENOMEM; 401 402 return 0; 403 } 404 405 static const struct iio_info bma180_info = { 406 .attrs = &bma180_attrs_group, 407 .read_raw = bma180_read_raw, 408 .write_raw = bma180_write_raw, 409 .update_scan_mode = bma180_update_scan_mode, 410 .driver_module = THIS_MODULE, 411 }; 412 413 static const char * const bma180_power_modes[] = { 414 BMA180_LOW_NOISE_STR, 415 BMA180_LOW_POWER_STR, 416 }; 417 418 static int bma180_get_power_mode(struct iio_dev *indio_dev, 419 const struct iio_chan_spec *chan) 420 { 421 struct bma180_data *data = iio_priv(indio_dev); 422 423 return data->pmode; 424 } 425 426 static int bma180_set_power_mode(struct iio_dev *indio_dev, 427 const struct iio_chan_spec *chan, unsigned int mode) 428 { 429 struct bma180_data *data = iio_priv(indio_dev); 430 int ret; 431 432 mutex_lock(&data->mutex); 433 ret = bma180_set_pmode(data, mode); 434 mutex_unlock(&data->mutex); 435 436 return ret; 437 } 438 439 static const struct iio_enum bma180_power_mode_enum = { 440 .items = bma180_power_modes, 441 .num_items = ARRAY_SIZE(bma180_power_modes), 442 .get = bma180_get_power_mode, 443 .set = bma180_set_power_mode, 444 }; 445 446 static const struct iio_chan_spec_ext_info bma180_ext_info[] = { 447 IIO_ENUM("power_mode", true, &bma180_power_mode_enum), 448 IIO_ENUM_AVAILABLE("power_mode", &bma180_power_mode_enum), 449 { }, 450 }; 451 452 #define BMA180_CHANNEL(_axis) { \ 453 .type = IIO_ACCEL, \ 454 .modified = 1, \ 455 .channel2 = IIO_MOD_##_axis, \ 456 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 457 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ 458 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \ 459 .scan_index = AXIS_##_axis, \ 460 .scan_type = { \ 461 .sign = 's', \ 462 .realbits = 14, \ 463 .storagebits = 16, \ 464 .shift = 2, \ 465 }, \ 466 .ext_info = bma180_ext_info, \ 467 } 468 469 static const struct iio_chan_spec bma180_channels[] = { 470 BMA180_CHANNEL(X), 471 BMA180_CHANNEL(Y), 472 BMA180_CHANNEL(Z), 473 IIO_CHAN_SOFT_TIMESTAMP(3), 474 }; 475 476 static irqreturn_t bma180_trigger_handler(int irq, void *p) 477 { 478 struct iio_poll_func *pf = p; 479 struct iio_dev *indio_dev = pf->indio_dev; 480 struct bma180_data *data = iio_priv(indio_dev); 481 int64_t time_ns = iio_get_time_ns(); 482 int bit, ret, i = 0; 483 484 mutex_lock(&data->mutex); 485 486 for_each_set_bit(bit, indio_dev->buffer->scan_mask, 487 indio_dev->masklength) { 488 ret = bma180_get_acc_reg(data, bit); 489 if (ret < 0) { 490 mutex_unlock(&data->mutex); 491 goto err; 492 } 493 ((s16 *)data->buff)[i++] = ret; 494 } 495 mutex_unlock(&data->mutex); 496 497 iio_push_to_buffers_with_timestamp(indio_dev, data->buff, time_ns); 498 err: 499 iio_trigger_notify_done(indio_dev->trig); 500 501 return IRQ_HANDLED; 502 } 503 504 static int bma180_data_rdy_trigger_set_state(struct iio_trigger *trig, 505 bool state) 506 { 507 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); 508 struct bma180_data *data = iio_priv(indio_dev); 509 510 return bma180_set_new_data_intr_state(data, state); 511 } 512 513 static int bma180_trig_try_reen(struct iio_trigger *trig) 514 { 515 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); 516 struct bma180_data *data = iio_priv(indio_dev); 517 518 return bma180_reset_intr(data); 519 } 520 521 static const struct iio_trigger_ops bma180_trigger_ops = { 522 .set_trigger_state = bma180_data_rdy_trigger_set_state, 523 .try_reenable = bma180_trig_try_reen, 524 .owner = THIS_MODULE, 525 }; 526 527 static int bma180_probe(struct i2c_client *client, 528 const struct i2c_device_id *id) 529 { 530 struct bma180_data *data; 531 struct iio_dev *indio_dev; 532 int ret; 533 534 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 535 if (!indio_dev) 536 return -ENOMEM; 537 538 data = iio_priv(indio_dev); 539 i2c_set_clientdata(client, indio_dev); 540 data->client = client; 541 542 ret = bma180_chip_init(data); 543 if (ret < 0) 544 goto err_chip_disable; 545 546 mutex_init(&data->mutex); 547 548 indio_dev->dev.parent = &client->dev; 549 indio_dev->channels = bma180_channels; 550 indio_dev->num_channels = ARRAY_SIZE(bma180_channels); 551 indio_dev->name = BMA180_DRV_NAME; 552 indio_dev->modes = INDIO_DIRECT_MODE; 553 indio_dev->info = &bma180_info; 554 555 if (client->irq > 0) { 556 data->trig = iio_trigger_alloc("%s-dev%d", indio_dev->name, 557 indio_dev->id); 558 if (!data->trig) { 559 ret = -ENOMEM; 560 goto err_chip_disable; 561 } 562 563 ret = devm_request_irq(&client->dev, client->irq, 564 iio_trigger_generic_data_rdy_poll, IRQF_TRIGGER_RISING, 565 BMA180_IRQ_NAME, data->trig); 566 if (ret) { 567 dev_err(&client->dev, "unable to request IRQ\n"); 568 goto err_trigger_free; 569 } 570 571 data->trig->dev.parent = &client->dev; 572 data->trig->ops = &bma180_trigger_ops; 573 iio_trigger_set_drvdata(data->trig, indio_dev); 574 indio_dev->trig = data->trig; 575 576 ret = iio_trigger_register(data->trig); 577 if (ret) 578 goto err_trigger_free; 579 } 580 581 ret = iio_triggered_buffer_setup(indio_dev, NULL, 582 bma180_trigger_handler, NULL); 583 if (ret < 0) { 584 dev_err(&client->dev, "unable to setup iio triggered buffer\n"); 585 goto err_trigger_unregister; 586 } 587 588 ret = iio_device_register(indio_dev); 589 if (ret < 0) { 590 dev_err(&client->dev, "unable to register iio device\n"); 591 goto err_buffer_cleanup; 592 } 593 594 return 0; 595 596 err_buffer_cleanup: 597 iio_triggered_buffer_cleanup(indio_dev); 598 err_trigger_unregister: 599 if (data->trig) 600 iio_trigger_unregister(data->trig); 601 err_trigger_free: 602 iio_trigger_free(data->trig); 603 err_chip_disable: 604 bma180_chip_disable(data); 605 606 return ret; 607 } 608 609 static int bma180_remove(struct i2c_client *client) 610 { 611 struct iio_dev *indio_dev = i2c_get_clientdata(client); 612 struct bma180_data *data = iio_priv(indio_dev); 613 614 iio_device_unregister(indio_dev); 615 iio_triggered_buffer_cleanup(indio_dev); 616 if (data->trig) { 617 iio_trigger_unregister(data->trig); 618 iio_trigger_free(data->trig); 619 } 620 621 mutex_lock(&data->mutex); 622 bma180_chip_disable(data); 623 mutex_unlock(&data->mutex); 624 625 return 0; 626 } 627 628 #ifdef CONFIG_PM_SLEEP 629 static int bma180_suspend(struct device *dev) 630 { 631 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 632 struct bma180_data *data = iio_priv(indio_dev); 633 int ret; 634 635 mutex_lock(&data->mutex); 636 ret = bma180_set_sleep_state(data, 1); 637 mutex_unlock(&data->mutex); 638 639 return ret; 640 } 641 642 static int bma180_resume(struct device *dev) 643 { 644 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 645 struct bma180_data *data = iio_priv(indio_dev); 646 int ret; 647 648 mutex_lock(&data->mutex); 649 ret = bma180_set_sleep_state(data, 0); 650 mutex_unlock(&data->mutex); 651 652 return ret; 653 } 654 655 static SIMPLE_DEV_PM_OPS(bma180_pm_ops, bma180_suspend, bma180_resume); 656 #define BMA180_PM_OPS (&bma180_pm_ops) 657 #else 658 #define BMA180_PM_OPS NULL 659 #endif 660 661 static struct i2c_device_id bma180_id[] = { 662 { BMA180_DRV_NAME, 0 }, 663 { } 664 }; 665 666 MODULE_DEVICE_TABLE(i2c, bma180_id); 667 668 static struct i2c_driver bma180_driver = { 669 .driver = { 670 .name = BMA180_DRV_NAME, 671 .owner = THIS_MODULE, 672 .pm = BMA180_PM_OPS, 673 }, 674 .probe = bma180_probe, 675 .remove = bma180_remove, 676 .id_table = bma180_id, 677 }; 678 679 module_i2c_driver(bma180_driver); 680 681 MODULE_AUTHOR("Kravchenko Oleksandr <x0199363@ti.com>"); 682 MODULE_AUTHOR("Texas Instruments, Inc."); 683 MODULE_DESCRIPTION("Bosch BMA180 triaxial acceleration sensor"); 684 MODULE_LICENSE("GPL"); 685