2 * Power key driver for SiRF PrimaII
4 * Copyright (c) 2013 Cambridge Silicon Radio Limited, a CSR plc group company.
6 * Licensed under GPLv2 or later.
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>
17 struct sirfsoc_pwrc_drvdata {
19 struct input_dev *input;
22 #define PWRC_ON_KEY_BIT (1 << 0)
24 #define PWRC_INT_STATUS 0xc
25 #define PWRC_INT_MASK 0x10
27 static irqreturn_t sirfsoc_pwrc_isr(int irq, void *dev_id)
29 struct sirfsoc_pwrc_drvdata *pwrcdrv = dev_id;
32 int_status = sirfsoc_rtc_iobrg_readl(pwrcdrv->pwrc_base +
34 sirfsoc_rtc_iobrg_writel(int_status & ~PWRC_ON_KEY_BIT,
35 pwrcdrv->pwrc_base + PWRC_INT_STATUS);
38 * For a typical Linux system, we report KEY_SUSPEND to trigger apm-power.c
39 * to queue a SUSPEND APM event
41 input_event(pwrcdrv->input, EV_PWR, KEY_SUSPEND, 1);
42 input_sync(pwrcdrv->input);
45 * Todo: report KEY_POWER event for Android platforms, Android PowerManager
46 * will handle the suspend and powerdown/hibernation
52 static void sirfsoc_pwrc_toggle_interrupts(struct sirfsoc_pwrc_drvdata *pwrcdrv,
57 int_mask = sirfsoc_rtc_iobrg_readl(pwrcdrv->pwrc_base + PWRC_INT_MASK);
59 int_mask |= PWRC_ON_KEY_BIT;
61 int_mask &= ~PWRC_ON_KEY_BIT;
62 sirfsoc_rtc_iobrg_writel(int_mask, pwrcdrv->pwrc_base + PWRC_INT_MASK);
65 static int sirfsoc_pwrc_open(struct input_dev *input)
67 struct sirfsoc_pwrc_drvdata *pwrcdrv = input_get_drvdata(input);
69 sirfsoc_pwrc_toggle_interrupts(pwrcdrv, true);
74 static void sirfsoc_pwrc_close(struct input_dev *input)
76 struct sirfsoc_pwrc_drvdata *pwrcdrv = input_get_drvdata(input);
78 sirfsoc_pwrc_toggle_interrupts(pwrcdrv, false);
81 static const struct of_device_id sirfsoc_pwrc_of_match[] = {
82 { .compatible = "sirf,prima2-pwrc" },
85 MODULE_DEVICE_TABLE(of, sirfsoc_pwrc_of_match);
87 static int sirfsoc_pwrc_probe(struct platform_device *pdev)
89 struct device_node *np = pdev->dev.of_node;
90 struct sirfsoc_pwrc_drvdata *pwrcdrv;
94 pwrcdrv = devm_kzalloc(&pdev->dev, sizeof(struct sirfsoc_pwrc_drvdata),
97 dev_info(&pdev->dev, "Not enough memory for the device data\n");
102 * We can't use of_iomap because pwrc is not mapped in memory,
103 * the so-called base address is only offset in rtciobrg
105 error = of_property_read_u32(np, "reg", &pwrcdrv->pwrc_base);
108 "unable to find base address of pwrc node in dtb\n");
112 pwrcdrv->input = devm_input_allocate_device(&pdev->dev);
116 pwrcdrv->input->name = "sirfsoc pwrckey";
117 pwrcdrv->input->phys = "pwrc/input0";
118 pwrcdrv->input->evbit[0] = BIT_MASK(EV_PWR);
120 pwrcdrv->input->open = sirfsoc_pwrc_open;
121 pwrcdrv->input->close = sirfsoc_pwrc_close;
123 input_set_drvdata(pwrcdrv->input, pwrcdrv);
125 /* Make sure the device is quiesced */
126 sirfsoc_pwrc_toggle_interrupts(pwrcdrv, false);
128 irq = platform_get_irq(pdev, 0);
129 error = devm_request_irq(&pdev->dev, irq,
130 sirfsoc_pwrc_isr, IRQF_SHARED,
131 "sirfsoc_pwrc_int", pwrcdrv);
133 dev_err(&pdev->dev, "unable to claim irq %d, error: %d\n",
138 error = input_register_device(pwrcdrv->input);
141 "unable to register input device, error: %d\n",
146 platform_set_drvdata(pdev, pwrcdrv);
147 device_init_wakeup(&pdev->dev, 1);
152 static int sirfsoc_pwrc_remove(struct platform_device *pdev)
154 device_init_wakeup(&pdev->dev, 0);
159 #ifdef CONFIG_PM_SLEEP
160 static int pwrc_resume(struct device *dev)
162 struct platform_device *pdev = to_platform_device(dev);
163 struct sirfsoc_pwrc_drvdata *pwrcdrv = platform_get_drvdata(pdev);
164 struct input_dev *input = pwrcdrv->input;
167 * Do not mask pwrc interrupt as we want pwrc work as a wakeup source
168 * if users touch X_ONKEY_B, see arch/arm/mach-prima2/pm.c
170 mutex_lock(&input->mutex);
172 sirfsoc_pwrc_toggle_interrupts(pwrcdrv, true);
173 mutex_unlock(&input->mutex);
179 static SIMPLE_DEV_PM_OPS(sirfsoc_pwrc_pm_ops, NULL, pwrc_resume);
181 static struct platform_driver sirfsoc_pwrc_driver = {
182 .probe = sirfsoc_pwrc_probe,
183 .remove = sirfsoc_pwrc_remove,
185 .name = "sirfsoc-pwrc",
186 .owner = THIS_MODULE,
187 .pm = &sirfsoc_pwrc_pm_ops,
188 .of_match_table = sirfsoc_pwrc_of_match,
192 module_platform_driver(sirfsoc_pwrc_driver);
194 MODULE_LICENSE("GPLv2");
195 MODULE_AUTHOR("Binghua Duan <Binghua.Duan@csr.com>, Xianglong Du <Xianglong.Du@csr.com>");
196 MODULE_DESCRIPTION("CSR Prima2 PWRC Driver");
197 MODULE_ALIAS("platform:sirfsoc-pwrc");