| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Copyright (c) 2011 Bosch Sensortec GmbH |
|---|
| 3 | 4 | * Copyright (c) 2011 Unixphere |
|---|
| .. | .. |
|---|
| 8 | 9 | * |
|---|
| 9 | 10 | * The datasheet for the BMA150 chip can be found here: |
|---|
| 10 | 11 | * http://www.bosch-sensortec.com/content/language1/downloads/BST-BMA150-DS000-07.pdf |
|---|
| 11 | | - * |
|---|
| 12 | | - * This program is free software; you can redistribute it and/or modify |
|---|
| 13 | | - * it under the terms of the GNU General Public License as published by |
|---|
| 14 | | - * the Free Software Foundation; either version 2 of the License, or |
|---|
| 15 | | - * (at your option) any later version. |
|---|
| 16 | | - * |
|---|
| 17 | | - * This program is distributed in the hope that it will be useful, |
|---|
| 18 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 19 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 20 | | - * GNU General Public License for more details. |
|---|
| 21 | | - * |
|---|
| 22 | | - * You should have received a copy of the GNU General Public License |
|---|
| 23 | | - * along with this program; if not, write to the Free Software |
|---|
| 24 | | - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|---|
| 25 | 12 | */ |
|---|
| 26 | 13 | #include <linux/kernel.h> |
|---|
| 27 | 14 | #include <linux/module.h> |
|---|
| 28 | 15 | #include <linux/i2c.h> |
|---|
| 29 | 16 | #include <linux/input.h> |
|---|
| 30 | | -#include <linux/input-polldev.h> |
|---|
| 31 | 17 | #include <linux/interrupt.h> |
|---|
| 32 | 18 | #include <linux/delay.h> |
|---|
| 33 | 19 | #include <linux/slab.h> |
|---|
| .. | .. |
|---|
| 136 | 122 | |
|---|
| 137 | 123 | struct bma150_data { |
|---|
| 138 | 124 | struct i2c_client *client; |
|---|
| 139 | | - struct input_polled_dev *input_polled; |
|---|
| 140 | 125 | struct input_dev *input; |
|---|
| 141 | 126 | u8 mode; |
|---|
| 142 | 127 | }; |
|---|
| .. | .. |
|---|
| 349 | 334 | return IRQ_HANDLED; |
|---|
| 350 | 335 | } |
|---|
| 351 | 336 | |
|---|
| 352 | | -static void bma150_poll(struct input_polled_dev *dev) |
|---|
| 337 | +static void bma150_poll(struct input_dev *input) |
|---|
| 353 | 338 | { |
|---|
| 354 | | - bma150_report_xyz(dev->private); |
|---|
| 339 | + struct bma150_data *bma150 = input_get_drvdata(input); |
|---|
| 340 | + |
|---|
| 341 | + bma150_report_xyz(bma150); |
|---|
| 355 | 342 | } |
|---|
| 356 | 343 | |
|---|
| 357 | | -static int bma150_open(struct bma150_data *bma150) |
|---|
| 344 | +static int bma150_open(struct input_dev *input) |
|---|
| 358 | 345 | { |
|---|
| 346 | + struct bma150_data *bma150 = input_get_drvdata(input); |
|---|
| 359 | 347 | int error; |
|---|
| 360 | 348 | |
|---|
| 361 | 349 | error = pm_runtime_get_sync(&bma150->client->dev); |
|---|
| .. | .. |
|---|
| 375 | 363 | return 0; |
|---|
| 376 | 364 | } |
|---|
| 377 | 365 | |
|---|
| 378 | | -static void bma150_close(struct bma150_data *bma150) |
|---|
| 366 | +static void bma150_close(struct input_dev *input) |
|---|
| 379 | 367 | { |
|---|
| 368 | + struct bma150_data *bma150 = input_get_drvdata(input); |
|---|
| 369 | + |
|---|
| 380 | 370 | pm_runtime_put_sync(&bma150->client->dev); |
|---|
| 381 | 371 | |
|---|
| 382 | 372 | if (bma150->mode != BMA150_MODE_SLEEP) |
|---|
| 383 | 373 | bma150_set_mode(bma150, BMA150_MODE_SLEEP); |
|---|
| 384 | 374 | } |
|---|
| 385 | 375 | |
|---|
| 386 | | -static int bma150_irq_open(struct input_dev *input) |
|---|
| 387 | | -{ |
|---|
| 388 | | - struct bma150_data *bma150 = input_get_drvdata(input); |
|---|
| 389 | | - |
|---|
| 390 | | - return bma150_open(bma150); |
|---|
| 391 | | -} |
|---|
| 392 | | - |
|---|
| 393 | | -static void bma150_irq_close(struct input_dev *input) |
|---|
| 394 | | -{ |
|---|
| 395 | | - struct bma150_data *bma150 = input_get_drvdata(input); |
|---|
| 396 | | - |
|---|
| 397 | | - bma150_close(bma150); |
|---|
| 398 | | -} |
|---|
| 399 | | - |
|---|
| 400 | | -static void bma150_poll_open(struct input_polled_dev *ipoll_dev) |
|---|
| 401 | | -{ |
|---|
| 402 | | - struct bma150_data *bma150 = ipoll_dev->private; |
|---|
| 403 | | - |
|---|
| 404 | | - bma150_open(bma150); |
|---|
| 405 | | -} |
|---|
| 406 | | - |
|---|
| 407 | | -static void bma150_poll_close(struct input_polled_dev *ipoll_dev) |
|---|
| 408 | | -{ |
|---|
| 409 | | - struct bma150_data *bma150 = ipoll_dev->private; |
|---|
| 410 | | - |
|---|
| 411 | | - bma150_close(bma150); |
|---|
| 412 | | -} |
|---|
| 413 | | - |
|---|
| 414 | 376 | static int bma150_initialize(struct bma150_data *bma150, |
|---|
| 415 | | - const struct bma150_cfg *cfg) |
|---|
| 377 | + const struct bma150_cfg *cfg) |
|---|
| 416 | 378 | { |
|---|
| 417 | 379 | int error; |
|---|
| 418 | 380 | |
|---|
| .. | .. |
|---|
| 452 | 414 | return bma150_set_mode(bma150, BMA150_MODE_SLEEP); |
|---|
| 453 | 415 | } |
|---|
| 454 | 416 | |
|---|
| 455 | | -static void bma150_init_input_device(struct bma150_data *bma150, |
|---|
| 456 | | - struct input_dev *idev) |
|---|
| 457 | | -{ |
|---|
| 458 | | - idev->name = BMA150_DRIVER; |
|---|
| 459 | | - idev->phys = BMA150_DRIVER "/input0"; |
|---|
| 460 | | - idev->id.bustype = BUS_I2C; |
|---|
| 461 | | - idev->dev.parent = &bma150->client->dev; |
|---|
| 462 | | - |
|---|
| 463 | | - idev->evbit[0] = BIT_MASK(EV_ABS); |
|---|
| 464 | | - input_set_abs_params(idev, ABS_X, ABSMIN_ACC_VAL, ABSMAX_ACC_VAL, 0, 0); |
|---|
| 465 | | - input_set_abs_params(idev, ABS_Y, ABSMIN_ACC_VAL, ABSMAX_ACC_VAL, 0, 0); |
|---|
| 466 | | - input_set_abs_params(idev, ABS_Z, ABSMIN_ACC_VAL, ABSMAX_ACC_VAL, 0, 0); |
|---|
| 467 | | -} |
|---|
| 468 | | - |
|---|
| 469 | | -static int bma150_register_input_device(struct bma150_data *bma150) |
|---|
| 470 | | -{ |
|---|
| 471 | | - struct input_dev *idev; |
|---|
| 472 | | - int error; |
|---|
| 473 | | - |
|---|
| 474 | | - idev = input_allocate_device(); |
|---|
| 475 | | - if (!idev) |
|---|
| 476 | | - return -ENOMEM; |
|---|
| 477 | | - |
|---|
| 478 | | - bma150_init_input_device(bma150, idev); |
|---|
| 479 | | - |
|---|
| 480 | | - idev->open = bma150_irq_open; |
|---|
| 481 | | - idev->close = bma150_irq_close; |
|---|
| 482 | | - input_set_drvdata(idev, bma150); |
|---|
| 483 | | - |
|---|
| 484 | | - bma150->input = idev; |
|---|
| 485 | | - |
|---|
| 486 | | - error = input_register_device(idev); |
|---|
| 487 | | - if (error) { |
|---|
| 488 | | - input_free_device(idev); |
|---|
| 489 | | - return error; |
|---|
| 490 | | - } |
|---|
| 491 | | - |
|---|
| 492 | | - return 0; |
|---|
| 493 | | -} |
|---|
| 494 | | - |
|---|
| 495 | | -static int bma150_register_polled_device(struct bma150_data *bma150) |
|---|
| 496 | | -{ |
|---|
| 497 | | - struct input_polled_dev *ipoll_dev; |
|---|
| 498 | | - int error; |
|---|
| 499 | | - |
|---|
| 500 | | - ipoll_dev = input_allocate_polled_device(); |
|---|
| 501 | | - if (!ipoll_dev) |
|---|
| 502 | | - return -ENOMEM; |
|---|
| 503 | | - |
|---|
| 504 | | - ipoll_dev->private = bma150; |
|---|
| 505 | | - ipoll_dev->open = bma150_poll_open; |
|---|
| 506 | | - ipoll_dev->close = bma150_poll_close; |
|---|
| 507 | | - ipoll_dev->poll = bma150_poll; |
|---|
| 508 | | - ipoll_dev->poll_interval = BMA150_POLL_INTERVAL; |
|---|
| 509 | | - ipoll_dev->poll_interval_min = BMA150_POLL_MIN; |
|---|
| 510 | | - ipoll_dev->poll_interval_max = BMA150_POLL_MAX; |
|---|
| 511 | | - |
|---|
| 512 | | - bma150_init_input_device(bma150, ipoll_dev->input); |
|---|
| 513 | | - |
|---|
| 514 | | - bma150->input_polled = ipoll_dev; |
|---|
| 515 | | - bma150->input = ipoll_dev->input; |
|---|
| 516 | | - |
|---|
| 517 | | - error = input_register_polled_device(ipoll_dev); |
|---|
| 518 | | - if (error) { |
|---|
| 519 | | - input_free_polled_device(ipoll_dev); |
|---|
| 520 | | - return error; |
|---|
| 521 | | - } |
|---|
| 522 | | - |
|---|
| 523 | | - return 0; |
|---|
| 524 | | -} |
|---|
| 525 | | - |
|---|
| 526 | 417 | static int bma150_probe(struct i2c_client *client, |
|---|
| 527 | | - const struct i2c_device_id *id) |
|---|
| 418 | + const struct i2c_device_id *id) |
|---|
| 528 | 419 | { |
|---|
| 529 | 420 | const struct bma150_platform_data *pdata = |
|---|
| 530 | 421 | dev_get_platdata(&client->dev); |
|---|
| 531 | 422 | const struct bma150_cfg *cfg; |
|---|
| 532 | 423 | struct bma150_data *bma150; |
|---|
| 424 | + struct input_dev *idev; |
|---|
| 533 | 425 | int chip_id; |
|---|
| 534 | 426 | int error; |
|---|
| 535 | 427 | |
|---|
| .. | .. |
|---|
| 544 | 436 | return -EINVAL; |
|---|
| 545 | 437 | } |
|---|
| 546 | 438 | |
|---|
| 547 | | - bma150 = kzalloc(sizeof(struct bma150_data), GFP_KERNEL); |
|---|
| 439 | + bma150 = devm_kzalloc(&client->dev, sizeof(*bma150), GFP_KERNEL); |
|---|
| 548 | 440 | if (!bma150) |
|---|
| 549 | 441 | return -ENOMEM; |
|---|
| 550 | 442 | |
|---|
| .. | .. |
|---|
| 557 | 449 | dev_err(&client->dev, |
|---|
| 558 | 450 | "IRQ GPIO conf. error %d, error %d\n", |
|---|
| 559 | 451 | client->irq, error); |
|---|
| 560 | | - goto err_free_mem; |
|---|
| 452 | + return error; |
|---|
| 561 | 453 | } |
|---|
| 562 | 454 | } |
|---|
| 563 | 455 | cfg = &pdata->cfg; |
|---|
| .. | .. |
|---|
| 567 | 459 | |
|---|
| 568 | 460 | error = bma150_initialize(bma150, cfg); |
|---|
| 569 | 461 | if (error) |
|---|
| 570 | | - goto err_free_mem; |
|---|
| 462 | + return error; |
|---|
| 463 | + |
|---|
| 464 | + idev = devm_input_allocate_device(&bma150->client->dev); |
|---|
| 465 | + if (!idev) |
|---|
| 466 | + return -ENOMEM; |
|---|
| 467 | + |
|---|
| 468 | + input_set_drvdata(idev, bma150); |
|---|
| 469 | + bma150->input = idev; |
|---|
| 470 | + |
|---|
| 471 | + idev->name = BMA150_DRIVER; |
|---|
| 472 | + idev->phys = BMA150_DRIVER "/input0"; |
|---|
| 473 | + idev->id.bustype = BUS_I2C; |
|---|
| 474 | + |
|---|
| 475 | + idev->open = bma150_open; |
|---|
| 476 | + idev->close = bma150_close; |
|---|
| 477 | + |
|---|
| 478 | + input_set_abs_params(idev, ABS_X, ABSMIN_ACC_VAL, ABSMAX_ACC_VAL, 0, 0); |
|---|
| 479 | + input_set_abs_params(idev, ABS_Y, ABSMIN_ACC_VAL, ABSMAX_ACC_VAL, 0, 0); |
|---|
| 480 | + input_set_abs_params(idev, ABS_Z, ABSMIN_ACC_VAL, ABSMAX_ACC_VAL, 0, 0); |
|---|
| 481 | + |
|---|
| 482 | + if (client->irq <= 0) { |
|---|
| 483 | + error = input_setup_polling(idev, bma150_poll); |
|---|
| 484 | + if (error) |
|---|
| 485 | + return error; |
|---|
| 486 | + |
|---|
| 487 | + input_set_poll_interval(idev, BMA150_POLL_INTERVAL); |
|---|
| 488 | + input_set_min_poll_interval(idev, BMA150_POLL_MIN); |
|---|
| 489 | + input_set_max_poll_interval(idev, BMA150_POLL_MAX); |
|---|
| 490 | + } |
|---|
| 491 | + |
|---|
| 492 | + error = input_register_device(idev); |
|---|
| 493 | + if (error) |
|---|
| 494 | + return error; |
|---|
| 571 | 495 | |
|---|
| 572 | 496 | if (client->irq > 0) { |
|---|
| 573 | | - error = bma150_register_input_device(bma150); |
|---|
| 574 | | - if (error) |
|---|
| 575 | | - goto err_free_mem; |
|---|
| 576 | | - |
|---|
| 577 | | - error = request_threaded_irq(client->irq, |
|---|
| 497 | + error = devm_request_threaded_irq(&client->dev, client->irq, |
|---|
| 578 | 498 | NULL, bma150_irq_thread, |
|---|
| 579 | 499 | IRQF_TRIGGER_RISING | IRQF_ONESHOT, |
|---|
| 580 | 500 | BMA150_DRIVER, bma150); |
|---|
| .. | .. |
|---|
| 582 | 502 | dev_err(&client->dev, |
|---|
| 583 | 503 | "irq request failed %d, error %d\n", |
|---|
| 584 | 504 | client->irq, error); |
|---|
| 585 | | - input_unregister_device(bma150->input); |
|---|
| 586 | | - goto err_free_mem; |
|---|
| 505 | + return error; |
|---|
| 587 | 506 | } |
|---|
| 588 | | - } else { |
|---|
| 589 | | - error = bma150_register_polled_device(bma150); |
|---|
| 590 | | - if (error) |
|---|
| 591 | | - goto err_free_mem; |
|---|
| 592 | 507 | } |
|---|
| 593 | 508 | |
|---|
| 594 | 509 | i2c_set_clientdata(client, bma150); |
|---|
| .. | .. |
|---|
| 596 | 511 | pm_runtime_enable(&client->dev); |
|---|
| 597 | 512 | |
|---|
| 598 | 513 | return 0; |
|---|
| 599 | | - |
|---|
| 600 | | -err_free_mem: |
|---|
| 601 | | - kfree(bma150); |
|---|
| 602 | | - return error; |
|---|
| 603 | 514 | } |
|---|
| 604 | 515 | |
|---|
| 605 | 516 | static int bma150_remove(struct i2c_client *client) |
|---|
| 606 | 517 | { |
|---|
| 607 | | - struct bma150_data *bma150 = i2c_get_clientdata(client); |
|---|
| 608 | | - |
|---|
| 609 | 518 | pm_runtime_disable(&client->dev); |
|---|
| 610 | | - |
|---|
| 611 | | - if (client->irq > 0) { |
|---|
| 612 | | - free_irq(client->irq, bma150); |
|---|
| 613 | | - input_unregister_device(bma150->input); |
|---|
| 614 | | - } else { |
|---|
| 615 | | - input_unregister_polled_device(bma150->input_polled); |
|---|
| 616 | | - input_free_polled_device(bma150->input_polled); |
|---|
| 617 | | - } |
|---|
| 618 | | - |
|---|
| 619 | | - kfree(bma150); |
|---|
| 620 | 519 | |
|---|
| 621 | 520 | return 0; |
|---|
| 622 | 521 | } |
|---|
| 623 | 522 | |
|---|
| 624 | | -#ifdef CONFIG_PM |
|---|
| 625 | | -static int bma150_suspend(struct device *dev) |
|---|
| 523 | +static int __maybe_unused bma150_suspend(struct device *dev) |
|---|
| 626 | 524 | { |
|---|
| 627 | 525 | struct i2c_client *client = to_i2c_client(dev); |
|---|
| 628 | 526 | struct bma150_data *bma150 = i2c_get_clientdata(client); |
|---|
| .. | .. |
|---|
| 630 | 528 | return bma150_set_mode(bma150, BMA150_MODE_SLEEP); |
|---|
| 631 | 529 | } |
|---|
| 632 | 530 | |
|---|
| 633 | | -static int bma150_resume(struct device *dev) |
|---|
| 531 | +static int __maybe_unused bma150_resume(struct device *dev) |
|---|
| 634 | 532 | { |
|---|
| 635 | 533 | struct i2c_client *client = to_i2c_client(dev); |
|---|
| 636 | 534 | struct bma150_data *bma150 = i2c_get_clientdata(client); |
|---|
| 637 | 535 | |
|---|
| 638 | 536 | return bma150_set_mode(bma150, BMA150_MODE_NORMAL); |
|---|
| 639 | 537 | } |
|---|
| 640 | | -#endif |
|---|
| 641 | 538 | |
|---|
| 642 | 539 | static UNIVERSAL_DEV_PM_OPS(bma150_pm, bma150_suspend, bma150_resume, NULL); |
|---|
| 643 | 540 | |
|---|