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 BMA_180_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 250 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.000130 0.000190 0.000250 0.000380 0.000500 0.000990 0.001980" 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 bw_table[] = { 10, 20, 40, 75, 150, 300 }; /* Hz */ 97 static int scale_table[] = { 130, 190, 250, 380, 500, 990, 1980 }; 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 registers\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(bw_table); ++i) { 189 if (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(scale_table); ++i) 213 if (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 != BMA_180_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 mutex_lock(&data->mutex); 380 ret = bma180_set_bw(data, val); 381 mutex_unlock(&data->mutex); 382 return ret; 383 default: 384 return -EINVAL; 385 } 386 } 387 388 static int bma180_update_scan_mode(struct iio_dev *indio_dev, 389 const unsigned long *scan_mask) 390 { 391 struct bma180_data *data = iio_priv(indio_dev); 392 393 if (data->buff) 394 devm_kfree(&indio_dev->dev, data->buff); 395 data->buff = devm_kzalloc(&indio_dev->dev, 396 indio_dev->scan_bytes, GFP_KERNEL); 397 if (!data->buff) 398 return -ENOMEM; 399 400 return 0; 401 } 402 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, 409 }; 410 411 static const char * const bma180_power_modes[] = { 412 BMA180_LOW_NOISE_STR, 413 BMA180_LOW_POWER_STR, 414 }; 415 416 static int bma180_get_power_mode(struct iio_dev *indio_dev, 417 const struct iio_chan_spec *chan) 418 { 419 struct bma180_data *data = iio_priv(indio_dev); 420 421 return data->pmode; 422 } 423 424 static int bma180_set_power_mode(struct iio_dev *indio_dev, 425 const struct iio_chan_spec *chan, unsigned int mode) 426 { 427 struct bma180_data *data = iio_priv(indio_dev); 428 int ret; 429 430 mutex_lock(&data->mutex); 431 ret = bma180_set_pmode(data, mode); 432 mutex_unlock(&data->mutex); 433 434 return ret; 435 } 436 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, 442 }; 443 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), 447 { }, 448 }; 449 450 #define BMA180_CHANNEL(_index) { \ 451 .type = IIO_ACCEL, \ 452 .indexed = 1, \ 453 .channel = (_index), \ 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 = (_index), \ 458 .scan_type = IIO_ST('s', 14, 16, 2), \ 459 .ext_info = bma180_ext_info, \ 460 } 461 462 static const struct iio_chan_spec bma180_channels[] = { 463 BMA180_CHANNEL(AXIS_X), 464 BMA180_CHANNEL(AXIS_Y), 465 BMA180_CHANNEL(AXIS_Z), 466 IIO_CHAN_SOFT_TIMESTAMP(4), 467 }; 468 469 static irqreturn_t bma180_trigger_handler(int irq, void *p) 470 { 471 struct iio_poll_func *pf = p; 472 struct iio_dev *indio_dev = pf->indio_dev; 473 struct bma180_data *data = iio_priv(indio_dev); 474 int bit, ret, i = 0; 475 476 mutex_lock(&data->mutex); 477 if (indio_dev->scan_timestamp) { 478 ret = indio_dev->scan_bytes / sizeof(s64) - 1; 479 ((s64 *)data->buff)[ret] = iio_get_time_ns(); 480 } 481 482 for_each_set_bit(bit, indio_dev->buffer->scan_mask, 483 indio_dev->masklength) { 484 ret = bma180_get_acc_reg(data, bit); 485 if (ret < 0) { 486 mutex_unlock(&data->mutex); 487 goto err; 488 } 489 ((s16 *)data->buff)[i++] = ret; 490 } 491 mutex_unlock(&data->mutex); 492 493 iio_push_to_buffers(indio_dev, (u8 *)data->buff); 494 err: 495 iio_trigger_notify_done(indio_dev->trig); 496 497 return IRQ_HANDLED; 498 } 499 500 static int bma180_data_rdy_trigger_set_state(struct iio_trigger *trig, 501 bool state) 502 { 503 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); 504 struct bma180_data *data = iio_priv(indio_dev); 505 506 return bma180_set_new_data_intr_state(data, state); 507 } 508 509 static int bma180_trig_try_reen(struct iio_trigger *trig) 510 { 511 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); 512 struct bma180_data *data = iio_priv(indio_dev); 513 514 return bma180_reset_intr(data); 515 } 516 517 static const struct iio_trigger_ops bma180_trigger_ops = { 518 .set_trigger_state = bma180_data_rdy_trigger_set_state, 519 .try_reenable = bma180_trig_try_reen, 520 .owner = THIS_MODULE, 521 }; 522 523 static int bma180_probe(struct i2c_client *client, 524 const struct i2c_device_id *id) 525 { 526 struct bma180_data *data; 527 struct iio_dev *indio_dev; 528 struct iio_trigger *trig; 529 int ret; 530 531 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 532 if (!indio_dev) 533 return -ENOMEM; 534 535 data = iio_priv(indio_dev); 536 i2c_set_clientdata(client, indio_dev); 537 data->client = client; 538 539 ret = bma180_chip_init(data); 540 if (ret < 0) 541 goto err_chip_disable; 542 543 mutex_init(&data->mutex); 544 545 indio_dev->dev.parent = &client->dev; 546 indio_dev->channels = bma180_channels; 547 indio_dev->num_channels = ARRAY_SIZE(bma180_channels); 548 indio_dev->name = BMA180_DRV_NAME; 549 indio_dev->modes = INDIO_DIRECT_MODE; 550 indio_dev->info = &bma180_info; 551 552 trig = iio_trigger_alloc("%s-dev%d", indio_dev->name, indio_dev->id); 553 if (!trig) { 554 ret = -ENOMEM; 555 goto err_chip_disable; 556 } 557 558 ret = devm_request_irq(&client->dev, client->irq, 559 iio_trigger_generic_data_rdy_poll, 560 IRQF_TRIGGER_RISING, BMA180_IRQ_NAME, trig); 561 if (ret) { 562 dev_err(&client->dev, "unable to request IRQ\n"); 563 goto err_trigger_free; 564 } 565 566 trig->dev.parent = &client->dev; 567 trig->ops = &bma180_trigger_ops; 568 iio_trigger_set_drvdata(trig, indio_dev); 569 data->trig = trig; 570 indio_dev->trig = trig; 571 572 ret = iio_trigger_register(trig); 573 if (ret) 574 goto err_trigger_free; 575 576 ret = iio_triggered_buffer_setup(indio_dev, NULL, 577 bma180_trigger_handler, NULL); 578 if (ret < 0) { 579 dev_err(&client->dev, "unable to setup iio triggered buffer\n"); 580 goto err_trigger_unregister; 581 } 582 583 ret = iio_device_register(indio_dev); 584 if (ret < 0) { 585 dev_err(&client->dev, "unable to register iio device\n"); 586 goto err_buffer_cleanup; 587 } 588 589 return 0; 590 591 err_buffer_cleanup: 592 iio_triggered_buffer_cleanup(indio_dev); 593 err_trigger_unregister: 594 iio_trigger_unregister(trig); 595 err_trigger_free: 596 iio_trigger_free(trig); 597 err_chip_disable: 598 bma180_chip_disable(data); 599 600 return ret; 601 } 602 603 static int bma180_remove(struct i2c_client *client) 604 { 605 struct iio_dev *indio_dev = i2c_get_clientdata(client); 606 struct bma180_data *data = iio_priv(indio_dev); 607 608 iio_device_unregister(indio_dev); 609 iio_triggered_buffer_cleanup(indio_dev); 610 iio_trigger_unregister(data->trig); 611 iio_trigger_free(data->trig); 612 613 mutex_lock(&data->mutex); 614 bma180_chip_disable(data); 615 mutex_unlock(&data->mutex); 616 617 return 0; 618 } 619 620 #ifdef CONFIG_PM_SLEEP 621 static int bma180_suspend(struct device *dev) 622 { 623 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 624 struct bma180_data *data = iio_priv(indio_dev); 625 int ret; 626 627 mutex_lock(&data->mutex); 628 ret = bma180_set_sleep_state(data, 1); 629 mutex_unlock(&data->mutex); 630 631 return ret; 632 } 633 634 static int bma180_resume(struct device *dev) 635 { 636 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 637 struct bma180_data *data = iio_priv(indio_dev); 638 int ret; 639 640 mutex_lock(&data->mutex); 641 ret = bma180_set_sleep_state(data, 0); 642 mutex_unlock(&data->mutex); 643 644 return ret; 645 } 646 647 static SIMPLE_DEV_PM_OPS(bma180_pm_ops, bma180_suspend, bma180_resume); 648 #define BMA180_PM_OPS (&bma180_pm_ops) 649 #else 650 #define BMA180_PM_OPS NULL 651 #endif 652 653 static struct i2c_device_id bma180_id[] = { 654 { BMA180_DRV_NAME, 0 }, 655 { } 656 }; 657 658 MODULE_DEVICE_TABLE(i2c, bma180_id); 659 660 static struct i2c_driver bma180_driver = { 661 .driver = { 662 .name = BMA180_DRV_NAME, 663 .owner = THIS_MODULE, 664 .pm = BMA180_PM_OPS, 665 }, 666 .probe = bma180_probe, 667 .remove = bma180_remove, 668 .id_table = bma180_id, 669 }; 670 671 module_i2c_driver(bma180_driver); 672 673 MODULE_AUTHOR("Kravchenko Oleksandr <x0199363@ti.com>"); 674 MODULE_AUTHOR("Texas Instruments, Inc."); 675 MODULE_DESCRIPTION("Bosch BMA180 triaxial acceleration sensor"); 676 MODULE_LICENSE("GPL"); 677