blob: fed5102e1802075b6fd9318c2bd8aad044b317b0 [file] [log] [blame]
Binghua Duan9b5f9532013-06-02 23:38:46 -07001/*
2 * Power key driver for SiRF PrimaII
3 *
Barry Songa9728f92014-02-14 08:48:21 -08004 * Copyright (c) 2013 - 2014 Cambridge Silicon Radio Limited, a CSR plc group
5 * company.
Binghua Duan9b5f9532013-06-02 23:38:46 -07006 *
7 * Licensed under GPLv2 or later.
8 */
9
10#include <linux/module.h>
Binghua Duan9b5f9532013-06-02 23:38:46 -070011#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>
16#include <linux/of.h>
Xianglong Dua1a75212014-02-15 12:30:58 -080017#include <linux/workqueue.h>
Binghua Duan9b5f9532013-06-02 23:38:46 -070018
19struct sirfsoc_pwrc_drvdata {
20 u32 pwrc_base;
21 struct input_dev *input;
Xianglong Dua1a75212014-02-15 12:30:58 -080022 struct delayed_work work;
Binghua Duan9b5f9532013-06-02 23:38:46 -070023};
24
25#define PWRC_ON_KEY_BIT (1 << 0)
26
27#define PWRC_INT_STATUS 0xc
28#define PWRC_INT_MASK 0x10
Xianglong Dua1a75212014-02-15 12:30:58 -080029#define PWRC_PIN_STATUS 0x14
30#define PWRC_KEY_DETECT_UP_TIME 20 /* ms*/
31
32static int sirfsoc_pwrc_is_on_key_down(struct sirfsoc_pwrc_drvdata *pwrcdrv)
33{
34 u32 state = sirfsoc_rtc_iobrg_readl(pwrcdrv->pwrc_base +
35 PWRC_PIN_STATUS);
36 return !(state & PWRC_ON_KEY_BIT); /* ON_KEY is active low */
37}
38
39static void sirfsoc_pwrc_report_event(struct work_struct *work)
40{
41 struct sirfsoc_pwrc_drvdata *pwrcdrv =
42 container_of(work, struct sirfsoc_pwrc_drvdata, work.work);
43
44 if (sirfsoc_pwrc_is_on_key_down(pwrcdrv)) {
45 schedule_delayed_work(&pwrcdrv->work,
46 msecs_to_jiffies(PWRC_KEY_DETECT_UP_TIME));
47 } else {
48 input_event(pwrcdrv->input, EV_KEY, KEY_POWER, 0);
49 input_sync(pwrcdrv->input);
50 }
51}
Binghua Duan9b5f9532013-06-02 23:38:46 -070052
53static irqreturn_t sirfsoc_pwrc_isr(int irq, void *dev_id)
54{
55 struct sirfsoc_pwrc_drvdata *pwrcdrv = dev_id;
56 u32 int_status;
57
58 int_status = sirfsoc_rtc_iobrg_readl(pwrcdrv->pwrc_base +
59 PWRC_INT_STATUS);
60 sirfsoc_rtc_iobrg_writel(int_status & ~PWRC_ON_KEY_BIT,
61 pwrcdrv->pwrc_base + PWRC_INT_STATUS);
62
Xianglong Dua1a75212014-02-15 12:30:58 -080063 input_event(pwrcdrv->input, EV_KEY, KEY_POWER, 1);
Binghua Duan9b5f9532013-06-02 23:38:46 -070064 input_sync(pwrcdrv->input);
Xianglong Dua1a75212014-02-15 12:30:58 -080065 schedule_delayed_work(&pwrcdrv->work,
66 msecs_to_jiffies(PWRC_KEY_DETECT_UP_TIME));
Binghua Duan9b5f9532013-06-02 23:38:46 -070067
68 return IRQ_HANDLED;
69}
70
Dmitry Torokhov864d3c02014-02-13 23:38:43 -080071static void sirfsoc_pwrc_toggle_interrupts(struct sirfsoc_pwrc_drvdata *pwrcdrv,
72 bool enable)
73{
74 u32 int_mask;
75
76 int_mask = sirfsoc_rtc_iobrg_readl(pwrcdrv->pwrc_base + PWRC_INT_MASK);
77 if (enable)
78 int_mask |= PWRC_ON_KEY_BIT;
79 else
80 int_mask &= ~PWRC_ON_KEY_BIT;
81 sirfsoc_rtc_iobrg_writel(int_mask, pwrcdrv->pwrc_base + PWRC_INT_MASK);
82}
83
84static int sirfsoc_pwrc_open(struct input_dev *input)
85{
86 struct sirfsoc_pwrc_drvdata *pwrcdrv = input_get_drvdata(input);
87
88 sirfsoc_pwrc_toggle_interrupts(pwrcdrv, true);
89
90 return 0;
91}
92
93static void sirfsoc_pwrc_close(struct input_dev *input)
94{
95 struct sirfsoc_pwrc_drvdata *pwrcdrv = input_get_drvdata(input);
96
97 sirfsoc_pwrc_toggle_interrupts(pwrcdrv, false);
Xianglong Dua1a75212014-02-15 12:30:58 -080098 cancel_delayed_work_sync(&pwrcdrv->work);
Dmitry Torokhov864d3c02014-02-13 23:38:43 -080099}
100
Binghua Duan9b5f9532013-06-02 23:38:46 -0700101static const struct of_device_id sirfsoc_pwrc_of_match[] = {
102 { .compatible = "sirf,prima2-pwrc" },
103 {},
104}
105MODULE_DEVICE_TABLE(of, sirfsoc_pwrc_of_match);
106
107static int sirfsoc_pwrc_probe(struct platform_device *pdev)
108{
109 struct device_node *np = pdev->dev.of_node;
110 struct sirfsoc_pwrc_drvdata *pwrcdrv;
111 int irq;
112 int error;
113
114 pwrcdrv = devm_kzalloc(&pdev->dev, sizeof(struct sirfsoc_pwrc_drvdata),
115 GFP_KERNEL);
116 if (!pwrcdrv) {
117 dev_info(&pdev->dev, "Not enough memory for the device data\n");
118 return -ENOMEM;
119 }
120
121 /*
Dmitry Torokhov864d3c02014-02-13 23:38:43 -0800122 * We can't use of_iomap because pwrc is not mapped in memory,
Binghua Duan9b5f9532013-06-02 23:38:46 -0700123 * the so-called base address is only offset in rtciobrg
124 */
125 error = of_property_read_u32(np, "reg", &pwrcdrv->pwrc_base);
126 if (error) {
127 dev_err(&pdev->dev,
128 "unable to find base address of pwrc node in dtb\n");
129 return error;
130 }
131
132 pwrcdrv->input = devm_input_allocate_device(&pdev->dev);
133 if (!pwrcdrv->input)
134 return -ENOMEM;
135
136 pwrcdrv->input->name = "sirfsoc pwrckey";
137 pwrcdrv->input->phys = "pwrc/input0";
Xianglong Dua1a75212014-02-15 12:30:58 -0800138 pwrcdrv->input->evbit[0] = BIT_MASK(EV_KEY);
Xianglong Du9732e5b2014-03-28 00:38:37 -0700139 input_set_capability(pwrcdrv->input, EV_KEY, KEY_POWER);
Xianglong Dua1a75212014-02-15 12:30:58 -0800140
141 INIT_DELAYED_WORK(&pwrcdrv->work, sirfsoc_pwrc_report_event);
Binghua Duan9b5f9532013-06-02 23:38:46 -0700142
Dmitry Torokhov864d3c02014-02-13 23:38:43 -0800143 pwrcdrv->input->open = sirfsoc_pwrc_open;
144 pwrcdrv->input->close = sirfsoc_pwrc_close;
145
146 input_set_drvdata(pwrcdrv->input, pwrcdrv);
147
148 /* Make sure the device is quiesced */
149 sirfsoc_pwrc_toggle_interrupts(pwrcdrv, false);
150
Binghua Duan9b5f9532013-06-02 23:38:46 -0700151 irq = platform_get_irq(pdev, 0);
152 error = devm_request_irq(&pdev->dev, irq,
Barry Song50998172014-02-14 08:43:05 -0800153 sirfsoc_pwrc_isr, 0,
Binghua Duan9b5f9532013-06-02 23:38:46 -0700154 "sirfsoc_pwrc_int", pwrcdrv);
155 if (error) {
156 dev_err(&pdev->dev, "unable to claim irq %d, error: %d\n",
157 irq, error);
158 return error;
159 }
160
Binghua Duan9b5f9532013-06-02 23:38:46 -0700161 error = input_register_device(pwrcdrv->input);
162 if (error) {
163 dev_err(&pdev->dev,
164 "unable to register input device, error: %d\n",
165 error);
166 return error;
167 }
168
Xianglong Dua5e46642014-02-14 08:45:56 -0800169 dev_set_drvdata(&pdev->dev, pwrcdrv);
Binghua Duan9b5f9532013-06-02 23:38:46 -0700170 device_init_wakeup(&pdev->dev, 1);
171
172 return 0;
173}
174
175static int sirfsoc_pwrc_remove(struct platform_device *pdev)
176{
177 device_init_wakeup(&pdev->dev, 0);
178
179 return 0;
180}
181
182#ifdef CONFIG_PM_SLEEP
Xianglong Du3a800352014-02-14 08:45:13 -0800183static int sirfsoc_pwrc_resume(struct device *dev)
Binghua Duan9b5f9532013-06-02 23:38:46 -0700184{
Xianglong Dua5e46642014-02-14 08:45:56 -0800185 struct sirfsoc_pwrc_drvdata *pwrcdrv = dev_get_drvdata(dev);
Dmitry Torokhov864d3c02014-02-13 23:38:43 -0800186 struct input_dev *input = pwrcdrv->input;
Binghua Duan9b5f9532013-06-02 23:38:46 -0700187
188 /*
189 * Do not mask pwrc interrupt as we want pwrc work as a wakeup source
190 * if users touch X_ONKEY_B, see arch/arm/mach-prima2/pm.c
191 */
Dmitry Torokhov864d3c02014-02-13 23:38:43 -0800192 mutex_lock(&input->mutex);
193 if (input->users)
194 sirfsoc_pwrc_toggle_interrupts(pwrcdrv, true);
195 mutex_unlock(&input->mutex);
Binghua Duan9b5f9532013-06-02 23:38:46 -0700196
197 return 0;
198}
199#endif
200
Xianglong Du3a800352014-02-14 08:45:13 -0800201static SIMPLE_DEV_PM_OPS(sirfsoc_pwrc_pm_ops, NULL, sirfsoc_pwrc_resume);
Binghua Duan9b5f9532013-06-02 23:38:46 -0700202
203static struct platform_driver sirfsoc_pwrc_driver = {
204 .probe = sirfsoc_pwrc_probe,
205 .remove = sirfsoc_pwrc_remove,
206 .driver = {
207 .name = "sirfsoc-pwrc",
208 .owner = THIS_MODULE,
209 .pm = &sirfsoc_pwrc_pm_ops,
Sachin Kamatdee964c2013-10-06 00:51:28 -0700210 .of_match_table = sirfsoc_pwrc_of_match,
Binghua Duan9b5f9532013-06-02 23:38:46 -0700211 }
212};
213
214module_platform_driver(sirfsoc_pwrc_driver);
215
Bjorn Helgaas67f4aef2014-07-18 10:05:38 -0700216MODULE_LICENSE("GPL v2");
Binghua Duan9b5f9532013-06-02 23:38:46 -0700217MODULE_AUTHOR("Binghua Duan <Binghua.Duan@csr.com>, Xianglong Du <Xianglong.Du@csr.com>");
218MODULE_DESCRIPTION("CSR Prima2 PWRC Driver");
219MODULE_ALIAS("platform:sirfsoc-pwrc");