2 * Power key driver for SiRF PrimaII
4 * Copyright (c) 2013 - 2014 Cambridge Silicon Radio Limited, a CSR plc group
7 * Licensed under GPLv2 or later.
10 #include <linux/module.h>
11 #include <linux/interrupt.h>
12 #include <linux/delay.h>
13 #include <linux/platform_device.h>
14 #include <linux/input.h>
15 #include <linux/rtc/sirfsoc_rtciobrg.h>
17 #include <linux/workqueue.h>
19 struct sirfsoc_pwrc_drvdata {
21 struct input_dev *input;
22 struct delayed_work work;
25 #define PWRC_ON_KEY_BIT (1 << 0)
27 #define PWRC_INT_STATUS 0xc
28 #define PWRC_INT_MASK 0x10
29 #define PWRC_PIN_STATUS 0x14
30 #define PWRC_KEY_DETECT_UP_TIME 20 /* ms*/
32 static int sirfsoc_pwrc_is_on_key_down(struct sirfsoc_pwrc_drvdata *pwrcdrv)
34 u32 state = sirfsoc_rtc_iobrg_readl(pwrcdrv->pwrc_base +
36 return !(state & PWRC_ON_KEY_BIT); /* ON_KEY is active low */
39 static void sirfsoc_pwrc_report_event(struct work_struct *work)
41 struct sirfsoc_pwrc_drvdata *pwrcdrv =
42 container_of(work, struct sirfsoc_pwrc_drvdata, work.work);
44 if (sirfsoc_pwrc_is_on_key_down(pwrcdrv)) {
45 schedule_delayed_work(&pwrcdrv->work,
46 msecs_to_jiffies(PWRC_KEY_DETECT_UP_TIME));
48 input_event(pwrcdrv->input, EV_KEY, KEY_POWER, 0);
49 input_sync(pwrcdrv->input);
53 static irqreturn_t sirfsoc_pwrc_isr(int irq, void *dev_id)
55 struct sirfsoc_pwrc_drvdata *pwrcdrv = dev_id;
58 int_status = sirfsoc_rtc_iobrg_readl(pwrcdrv->pwrc_base +
60 sirfsoc_rtc_iobrg_writel(int_status & ~PWRC_ON_KEY_BIT,
61 pwrcdrv->pwrc_base + PWRC_INT_STATUS);
63 input_event(pwrcdrv->input, EV_KEY, KEY_POWER, 1);
64 input_sync(pwrcdrv->input);
65 schedule_delayed_work(&pwrcdrv->work,
66 msecs_to_jiffies(PWRC_KEY_DETECT_UP_TIME));
71 static void sirfsoc_pwrc_toggle_interrupts(struct sirfsoc_pwrc_drvdata *pwrcdrv,
76 int_mask = sirfsoc_rtc_iobrg_readl(pwrcdrv->pwrc_base + PWRC_INT_MASK);
78 int_mask |= PWRC_ON_KEY_BIT;
80 int_mask &= ~PWRC_ON_KEY_BIT;
81 sirfsoc_rtc_iobrg_writel(int_mask, pwrcdrv->pwrc_base + PWRC_INT_MASK);
84 static int sirfsoc_pwrc_open(struct input_dev *input)
86 struct sirfsoc_pwrc_drvdata *pwrcdrv = input_get_drvdata(input);
88 sirfsoc_pwrc_toggle_interrupts(pwrcdrv, true);
93 static void sirfsoc_pwrc_close(struct input_dev *input)
95 struct sirfsoc_pwrc_drvdata *pwrcdrv = input_get_drvdata(input);
97 sirfsoc_pwrc_toggle_interrupts(pwrcdrv, false);
98 cancel_delayed_work_sync(&pwrcdrv->work);
101 static const struct of_device_id sirfsoc_pwrc_of_match[] = {
102 { .compatible = "sirf,prima2-pwrc" },
105 MODULE_DEVICE_TABLE(of, sirfsoc_pwrc_of_match);
107 static int sirfsoc_pwrc_probe(struct platform_device *pdev)
109 struct device_node *np = pdev->dev.of_node;
110 struct sirfsoc_pwrc_drvdata *pwrcdrv;
114 pwrcdrv = devm_kzalloc(&pdev->dev, sizeof(struct sirfsoc_pwrc_drvdata),
117 dev_info(&pdev->dev, "Not enough memory for the device data\n");
122 * We can't use of_iomap because pwrc is not mapped in memory,
123 * the so-called base address is only offset in rtciobrg
125 error = of_property_read_u32(np, "reg", &pwrcdrv->pwrc_base);
128 "unable to find base address of pwrc node in dtb\n");
132 pwrcdrv->input = devm_input_allocate_device(&pdev->dev);
136 pwrcdrv->input->name = "sirfsoc pwrckey";
137 pwrcdrv->input->phys = "pwrc/input0";
138 pwrcdrv->input->evbit[0] = BIT_MASK(EV_KEY);
139 input_set_capability(pwrcdrv->input, EV_KEY, KEY_POWER);
141 INIT_DELAYED_WORK(&pwrcdrv->work, sirfsoc_pwrc_report_event);
143 pwrcdrv->input->open = sirfsoc_pwrc_open;
144 pwrcdrv->input->close = sirfsoc_pwrc_close;
146 input_set_drvdata(pwrcdrv->input, pwrcdrv);
148 /* Make sure the device is quiesced */
149 sirfsoc_pwrc_toggle_interrupts(pwrcdrv, false);
151 irq = platform_get_irq(pdev, 0);
152 error = devm_request_irq(&pdev->dev, irq,
154 "sirfsoc_pwrc_int", pwrcdrv);
156 dev_err(&pdev->dev, "unable to claim irq %d, error: %d\n",
161 error = input_register_device(pwrcdrv->input);
164 "unable to register input device, error: %d\n",
169 dev_set_drvdata(&pdev->dev, pwrcdrv);
170 device_init_wakeup(&pdev->dev, 1);
175 static int sirfsoc_pwrc_remove(struct platform_device *pdev)
177 device_init_wakeup(&pdev->dev, 0);
182 static int __maybe_unused sirfsoc_pwrc_resume(struct device *dev)
184 struct sirfsoc_pwrc_drvdata *pwrcdrv = dev_get_drvdata(dev);
185 struct input_dev *input = pwrcdrv->input;
188 * Do not mask pwrc interrupt as we want pwrc work as a wakeup source
189 * if users touch X_ONKEY_B, see arch/arm/mach-prima2/pm.c
191 mutex_lock(&input->mutex);
193 sirfsoc_pwrc_toggle_interrupts(pwrcdrv, true);
194 mutex_unlock(&input->mutex);
199 static SIMPLE_DEV_PM_OPS(sirfsoc_pwrc_pm_ops, NULL, sirfsoc_pwrc_resume);
201 static struct platform_driver sirfsoc_pwrc_driver = {
202 .probe = sirfsoc_pwrc_probe,
203 .remove = sirfsoc_pwrc_remove,
205 .name = "sirfsoc-pwrc",
206 .pm = &sirfsoc_pwrc_pm_ops,
207 .of_match_table = sirfsoc_pwrc_of_match,
211 module_platform_driver(sirfsoc_pwrc_driver);
213 MODULE_LICENSE("GPL v2");
214 MODULE_AUTHOR("Binghua Duan <Binghua.Duan@csr.com>, Xianglong Du <Xianglong.Du@csr.com>");
215 MODULE_DESCRIPTION("CSR Prima2 PWRC Driver");
216 MODULE_ALIAS("platform:sirfsoc-pwrc");