hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/drivers/input/misc/bma150.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * Copyright (c) 2011 Bosch Sensortec GmbH
34 * Copyright (c) 2011 Unixphere
....@@ -8,26 +9,11 @@
89 *
910 * The datasheet for the BMA150 chip can be found here:
1011 * 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.
2512 */
2613 #include <linux/kernel.h>
2714 #include <linux/module.h>
2815 #include <linux/i2c.h>
2916 #include <linux/input.h>
30
-#include <linux/input-polldev.h>
3117 #include <linux/interrupt.h>
3218 #include <linux/delay.h>
3319 #include <linux/slab.h>
....@@ -136,7 +122,6 @@
136122
137123 struct bma150_data {
138124 struct i2c_client *client;
139
- struct input_polled_dev *input_polled;
140125 struct input_dev *input;
141126 u8 mode;
142127 };
....@@ -349,13 +334,16 @@
349334 return IRQ_HANDLED;
350335 }
351336
352
-static void bma150_poll(struct input_polled_dev *dev)
337
+static void bma150_poll(struct input_dev *input)
353338 {
354
- bma150_report_xyz(dev->private);
339
+ struct bma150_data *bma150 = input_get_drvdata(input);
340
+
341
+ bma150_report_xyz(bma150);
355342 }
356343
357
-static int bma150_open(struct bma150_data *bma150)
344
+static int bma150_open(struct input_dev *input)
358345 {
346
+ struct bma150_data *bma150 = input_get_drvdata(input);
359347 int error;
360348
361349 error = pm_runtime_get_sync(&bma150->client->dev);
....@@ -375,44 +363,18 @@
375363 return 0;
376364 }
377365
378
-static void bma150_close(struct bma150_data *bma150)
366
+static void bma150_close(struct input_dev *input)
379367 {
368
+ struct bma150_data *bma150 = input_get_drvdata(input);
369
+
380370 pm_runtime_put_sync(&bma150->client->dev);
381371
382372 if (bma150->mode != BMA150_MODE_SLEEP)
383373 bma150_set_mode(bma150, BMA150_MODE_SLEEP);
384374 }
385375
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
-
414376 static int bma150_initialize(struct bma150_data *bma150,
415
- const struct bma150_cfg *cfg)
377
+ const struct bma150_cfg *cfg)
416378 {
417379 int error;
418380
....@@ -452,84 +414,14 @@
452414 return bma150_set_mode(bma150, BMA150_MODE_SLEEP);
453415 }
454416
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
-
526417 static int bma150_probe(struct i2c_client *client,
527
- const struct i2c_device_id *id)
418
+ const struct i2c_device_id *id)
528419 {
529420 const struct bma150_platform_data *pdata =
530421 dev_get_platdata(&client->dev);
531422 const struct bma150_cfg *cfg;
532423 struct bma150_data *bma150;
424
+ struct input_dev *idev;
533425 int chip_id;
534426 int error;
535427
....@@ -544,7 +436,7 @@
544436 return -EINVAL;
545437 }
546438
547
- bma150 = kzalloc(sizeof(struct bma150_data), GFP_KERNEL);
439
+ bma150 = devm_kzalloc(&client->dev, sizeof(*bma150), GFP_KERNEL);
548440 if (!bma150)
549441 return -ENOMEM;
550442
....@@ -557,7 +449,7 @@
557449 dev_err(&client->dev,
558450 "IRQ GPIO conf. error %d, error %d\n",
559451 client->irq, error);
560
- goto err_free_mem;
452
+ return error;
561453 }
562454 }
563455 cfg = &pdata->cfg;
....@@ -567,14 +459,42 @@
567459
568460 error = bma150_initialize(bma150, cfg);
569461 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;
571495
572496 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,
578498 NULL, bma150_irq_thread,
579499 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
580500 BMA150_DRIVER, bma150);
....@@ -582,13 +502,8 @@
582502 dev_err(&client->dev,
583503 "irq request failed %d, error %d\n",
584504 client->irq, error);
585
- input_unregister_device(bma150->input);
586
- goto err_free_mem;
505
+ return error;
587506 }
588
- } else {
589
- error = bma150_register_polled_device(bma150);
590
- if (error)
591
- goto err_free_mem;
592507 }
593508
594509 i2c_set_clientdata(client, bma150);
....@@ -596,33 +511,16 @@
596511 pm_runtime_enable(&client->dev);
597512
598513 return 0;
599
-
600
-err_free_mem:
601
- kfree(bma150);
602
- return error;
603514 }
604515
605516 static int bma150_remove(struct i2c_client *client)
606517 {
607
- struct bma150_data *bma150 = i2c_get_clientdata(client);
608
-
609518 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);
620519
621520 return 0;
622521 }
623522
624
-#ifdef CONFIG_PM
625
-static int bma150_suspend(struct device *dev)
523
+static int __maybe_unused bma150_suspend(struct device *dev)
626524 {
627525 struct i2c_client *client = to_i2c_client(dev);
628526 struct bma150_data *bma150 = i2c_get_clientdata(client);
....@@ -630,14 +528,13 @@
630528 return bma150_set_mode(bma150, BMA150_MODE_SLEEP);
631529 }
632530
633
-static int bma150_resume(struct device *dev)
531
+static int __maybe_unused bma150_resume(struct device *dev)
634532 {
635533 struct i2c_client *client = to_i2c_client(dev);
636534 struct bma150_data *bma150 = i2c_get_clientdata(client);
637535
638536 return bma150_set_mode(bma150, BMA150_MODE_NORMAL);
639537 }
640
-#endif
641538
642539 static UNIVERSAL_DEV_PM_OPS(bma150_pm, bma150_suspend, bma150_resume, NULL);
643540