Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ebiederm...
[cascardo/linux.git] / drivers / input / misc / sirfsoc-onkey.c
1 /*
2  * Power key driver for SiRF PrimaII
3  *
4  * Copyright (c) 2013 Cambridge Silicon Radio Limited, a CSR plc group company.
5  *
6  * Licensed under GPLv2 or later.
7  */
8
9 #include <linux/module.h>
10 #include <linux/interrupt.h>
11 #include <linux/delay.h>
12 #include <linux/platform_device.h>
13 #include <linux/input.h>
14 #include <linux/rtc/sirfsoc_rtciobrg.h>
15 #include <linux/of.h>
16
17 struct sirfsoc_pwrc_drvdata {
18         u32                     pwrc_base;
19         struct input_dev        *input;
20 };
21
22 #define PWRC_ON_KEY_BIT                 (1 << 0)
23
24 #define PWRC_INT_STATUS                 0xc
25 #define PWRC_INT_MASK                   0x10
26
27 static irqreturn_t sirfsoc_pwrc_isr(int irq, void *dev_id)
28 {
29         struct sirfsoc_pwrc_drvdata *pwrcdrv = dev_id;
30         u32 int_status;
31
32         int_status = sirfsoc_rtc_iobrg_readl(pwrcdrv->pwrc_base +
33                                                         PWRC_INT_STATUS);
34         sirfsoc_rtc_iobrg_writel(int_status & ~PWRC_ON_KEY_BIT,
35                                  pwrcdrv->pwrc_base + PWRC_INT_STATUS);
36
37         /*
38          * For a typical Linux system, we report KEY_SUSPEND to trigger apm-power.c
39          * to queue a SUSPEND APM event
40          */
41         input_event(pwrcdrv->input, EV_PWR, KEY_SUSPEND, 1);
42         input_sync(pwrcdrv->input);
43
44         /*
45          * Todo: report KEY_POWER event for Android platforms, Android PowerManager
46          * will handle the suspend and powerdown/hibernation
47          */
48
49         return IRQ_HANDLED;
50 }
51
52 static const struct of_device_id sirfsoc_pwrc_of_match[] = {
53         { .compatible = "sirf,prima2-pwrc" },
54         {},
55 }
56 MODULE_DEVICE_TABLE(of, sirfsoc_pwrc_of_match);
57
58 static int sirfsoc_pwrc_probe(struct platform_device *pdev)
59 {
60         struct device_node *np = pdev->dev.of_node;
61         struct sirfsoc_pwrc_drvdata *pwrcdrv;
62         int irq;
63         int error;
64
65         pwrcdrv = devm_kzalloc(&pdev->dev, sizeof(struct sirfsoc_pwrc_drvdata),
66                                GFP_KERNEL);
67         if (!pwrcdrv) {
68                 dev_info(&pdev->dev, "Not enough memory for the device data\n");
69                 return -ENOMEM;
70         }
71
72         /*
73          * we can't use of_iomap because pwrc is not mapped in memory,
74          * the so-called base address is only offset in rtciobrg
75          */
76         error = of_property_read_u32(np, "reg", &pwrcdrv->pwrc_base);
77         if (error) {
78                 dev_err(&pdev->dev,
79                         "unable to find base address of pwrc node in dtb\n");
80                 return error;
81         }
82
83         pwrcdrv->input = devm_input_allocate_device(&pdev->dev);
84         if (!pwrcdrv->input)
85                 return -ENOMEM;
86
87         pwrcdrv->input->name = "sirfsoc pwrckey";
88         pwrcdrv->input->phys = "pwrc/input0";
89         pwrcdrv->input->evbit[0] = BIT_MASK(EV_PWR);
90
91         irq = platform_get_irq(pdev, 0);
92         error = devm_request_irq(&pdev->dev, irq,
93                                  sirfsoc_pwrc_isr, IRQF_SHARED,
94                                  "sirfsoc_pwrc_int", pwrcdrv);
95         if (error) {
96                 dev_err(&pdev->dev, "unable to claim irq %d, error: %d\n",
97                         irq, error);
98                 return error;
99         }
100
101         sirfsoc_rtc_iobrg_writel(
102                 sirfsoc_rtc_iobrg_readl(pwrcdrv->pwrc_base + PWRC_INT_MASK) |
103                         PWRC_ON_KEY_BIT,
104                 pwrcdrv->pwrc_base + PWRC_INT_MASK);
105
106         error = input_register_device(pwrcdrv->input);
107         if (error) {
108                 dev_err(&pdev->dev,
109                         "unable to register input device, error: %d\n",
110                         error);
111                 return error;
112         }
113
114         platform_set_drvdata(pdev, pwrcdrv);
115         device_init_wakeup(&pdev->dev, 1);
116
117         return 0;
118 }
119
120 static int sirfsoc_pwrc_remove(struct platform_device *pdev)
121 {
122         device_init_wakeup(&pdev->dev, 0);
123
124         return 0;
125 }
126
127 #ifdef CONFIG_PM_SLEEP
128 static int pwrc_resume(struct device *dev)
129 {
130         struct platform_device *pdev = to_platform_device(dev);
131         struct sirfsoc_pwrc_drvdata *pwrcdrv = platform_get_drvdata(pdev);
132
133         /*
134          * Do not mask pwrc interrupt as we want pwrc work as a wakeup source
135          * if users touch X_ONKEY_B, see arch/arm/mach-prima2/pm.c
136          */
137         sirfsoc_rtc_iobrg_writel(
138                 sirfsoc_rtc_iobrg_readl(
139                 pwrcdrv->pwrc_base + PWRC_INT_MASK) | PWRC_ON_KEY_BIT,
140                 pwrcdrv->pwrc_base + PWRC_INT_MASK);
141
142         return 0;
143 }
144 #endif
145
146 static SIMPLE_DEV_PM_OPS(sirfsoc_pwrc_pm_ops, NULL, pwrc_resume);
147
148 static struct platform_driver sirfsoc_pwrc_driver = {
149         .probe          = sirfsoc_pwrc_probe,
150         .remove         = sirfsoc_pwrc_remove,
151         .driver         = {
152                 .name   = "sirfsoc-pwrc",
153                 .owner  = THIS_MODULE,
154                 .pm     = &sirfsoc_pwrc_pm_ops,
155                 .of_match_table = sirfsoc_pwrc_of_match,
156         }
157 };
158
159 module_platform_driver(sirfsoc_pwrc_driver);
160
161 MODULE_LICENSE("GPLv2");
162 MODULE_AUTHOR("Binghua Duan <Binghua.Duan@csr.com>, Xianglong Du <Xianglong.Du@csr.com>");
163 MODULE_DESCRIPTION("CSR Prima2 PWRC Driver");
164 MODULE_ALIAS("platform:sirfsoc-pwrc");