Back to home page

OSCL-LXR

 
 

    


0001 /**
0002  * CPCAP Power Button Input Driver
0003  *
0004  * Copyright (C) 2017 Sebastian Reichel <sre@kernel.org>
0005  *
0006  * This file is subject to the terms and conditions of the GNU General
0007  * Public License. See the file "COPYING" in the main directory of this
0008  * archive for more details.
0009  *
0010  * This program is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  * GNU General Public License for more details.
0014  */
0015 
0016 #include <linux/module.h>
0017 #include <linux/init.h>
0018 #include <linux/kernel.h>
0019 #include <linux/errno.h>
0020 #include <linux/input.h>
0021 #include <linux/interrupt.h>
0022 #include <linux/regmap.h>
0023 #include <linux/of.h>
0024 #include <linux/platform_device.h>
0025 #include <linux/mfd/motorola-cpcap.h>
0026 
0027 #define CPCAP_IRQ_ON 23
0028 #define CPCAP_IRQ_ON_BITMASK (1 << (CPCAP_IRQ_ON % 16))
0029 
0030 struct cpcap_power_button {
0031     struct regmap *regmap;
0032     struct input_dev *idev;
0033     struct device *dev;
0034 };
0035 
0036 static irqreturn_t powerbutton_irq(int irq, void *_button)
0037 {
0038     struct cpcap_power_button *button = _button;
0039     int val;
0040 
0041     val = cpcap_sense_virq(button->regmap, irq);
0042     if (val < 0) {
0043         dev_err(button->dev, "irq read failed: %d", val);
0044         return IRQ_HANDLED;
0045     }
0046 
0047     pm_wakeup_event(button->dev, 0);
0048     input_report_key(button->idev, KEY_POWER, val);
0049     input_sync(button->idev);
0050 
0051     return IRQ_HANDLED;
0052 }
0053 
0054 static int cpcap_power_button_probe(struct platform_device *pdev)
0055 {
0056     struct cpcap_power_button *button;
0057     int irq;
0058     int err;
0059 
0060     irq = platform_get_irq(pdev, 0);
0061     if (irq < 0)
0062         return irq;
0063 
0064     button = devm_kmalloc(&pdev->dev, sizeof(*button), GFP_KERNEL);
0065     if (!button)
0066         return -ENOMEM;
0067 
0068     button->idev = devm_input_allocate_device(&pdev->dev);
0069     if (!button->idev)
0070         return -ENOMEM;
0071 
0072     button->regmap = dev_get_regmap(pdev->dev.parent, NULL);
0073     if (!button->regmap)
0074         return -ENODEV;
0075 
0076     button->dev = &pdev->dev;
0077 
0078     button->idev->name = "cpcap-pwrbutton";
0079     button->idev->phys = "cpcap-pwrbutton/input0";
0080     input_set_capability(button->idev, EV_KEY, KEY_POWER);
0081 
0082     err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
0083         powerbutton_irq, IRQF_ONESHOT, "cpcap_pwrbutton", button);
0084     if (err < 0) {
0085         dev_err(&pdev->dev, "IRQ request failed: %d\n", err);
0086         return err;
0087     }
0088 
0089     err = input_register_device(button->idev);
0090     if (err) {
0091         dev_err(&pdev->dev, "Input register failed: %d\n", err);
0092         return err;
0093     }
0094 
0095     device_init_wakeup(&pdev->dev, true);
0096 
0097     return 0;
0098 }
0099 
0100 #ifdef CONFIG_OF
0101 static const struct of_device_id cpcap_pwrbutton_dt_match_table[] = {
0102     { .compatible = "motorola,cpcap-pwrbutton" },
0103     {},
0104 };
0105 MODULE_DEVICE_TABLE(of, cpcap_pwrbutton_dt_match_table);
0106 #endif
0107 
0108 static struct platform_driver cpcap_power_button_driver = {
0109     .probe      = cpcap_power_button_probe,
0110     .driver     = {
0111         .name   = "cpcap-pwrbutton",
0112         .of_match_table = of_match_ptr(cpcap_pwrbutton_dt_match_table),
0113     },
0114 };
0115 module_platform_driver(cpcap_power_button_driver);
0116 
0117 MODULE_ALIAS("platform:cpcap-pwrbutton");
0118 MODULE_DESCRIPTION("CPCAP Power Button");
0119 MODULE_LICENSE("GPL");
0120 MODULE_AUTHOR("Sebastian Reichel <sre@kernel.org>");